|
楼主 |
发表于 2021-10-17 15:30:07
|
显示全部楼层
T3- #include<bits/stdc++.h>
- #define vis viss
- #define check checkk
- #define stop stopp
- #define int long long
- using namespace std;
- int a,b,p,q,num,T,fn[50001],pow_ab[55],pow_p[55],pow_q[55];
- bool stop;
- bool u=true;
- inline bool check(int yours[],int expected[],bool flag){
- if (flag){
- for(int i=1;i<=num;i++)
- if (yours[i]>expected[i])
- return true;
- return false;
- }
- for(int i=1;i<=num;i++)
- if (yours[i]!=expected[i])
- return false;
- return true;
- }
- bool dfs(int now,int depth,int yours[],int expected[]){
- if (now==depth){
- stop=stop&&check(yours,expected,1);
- return check(yours,expected,0);
- }
- if (u){
- for (int i=1;i<=num;i++)
- yours[i]+=pow_p[i];
- if (dfs(now+1,depth,yours,expected))
- return true;
- }
- for (int i=1;i<=num;i++)
- yours[i]=(yours[i]-u*pow_p[i])<<1;
- if (dfs(now+1,depth,yours,expected))
- return true;
- for (int i=1;i<=num;i++)
- yours[i]=yours[i]>>1;
- return false;
- }
- signed main(){
- //freopen("digital.in","r",stdin);
- //freopen("digital.out","w",stdout);
- scanf("%lld",&T);
- while (T--){
- scanf("%lld%lld%lld%lld",&a,&b,&p,&q);
- int w,v;
- num=0;
- memset(fn,0,sizeof(fn));
- for (int i=1;i<=q;i++){
- scanf("%lld,%lld",&w,&v);
- //floor(sqrt(50000))=223
- for (int j=2;j<=223;j++){
- if (w%j)
- continue;
- if ((!fn[j])&&v)
- num++,pow_q[num]=0,fn[j]=num;
- while (!(w%j))
- w/=j,pow_q[fn[j]]+=v;
- }
- if (w>1&&v&&(!fn[w]))
- num++,pow_q[num]=v,fn[w]=num;
- else if (w>1)
- pow_q[fn[w]]+=v;
- }
- u=true;
- bool vis;
- if (p==1)
- u=false;
- for (int i=2;i<50000;i++){
- vis=false;
- int num=fn[i];
- if (!(a%i))
- pow_ab[num]=pow_p[num]=0;
- while (!(a%i)) a/=i,vis=true,pow_ab[num]++;
- while (!(b%i)) b/=i,pow_ab[num]++;
- while (!(p%i)) p/=i,u=u&&vis,pow_p[num]++;
- }
- int numm=0;
- if (a<50000)
- numm=fn[a];
- pow_ab[numm]=pow_p[numm]=0;
- if (a>1) pow_ab[numm]++;
- if (b>1) pow_ab[numm]++;
- if (u&&p>1) u=u&&(p==a),pow_p[numm]++;
- int depth=-1;
- while (1){
- depth++;
- stop=true;
- bool k=dfs(0,depth,pow_ab,pow_q);
- if (k){
- printf("%lld\n",depth);
- break;
- }
- if (stop){
- printf("-1\n");
- break;
- }
- }
- }
- //fclose(stdin);
- //fclose(stdout);
- return 0;
- }
复制代码
|
|