At first glance I thought it was the title track of water, so I did not expect this stuff downright.
Look question is obviously exgcd, however exgcd is seeking a solution rather than the number of solutions of (do not remember the time of the examination the general solution of the equation and then hung up).
For 40% of the data, it can be enumerated directly counted.
For the other of 20%, a + b = c, puts ( "1");
This is almost 60 points to send.
The rest is more disgusting is the:
Where first discuss are positive numbers: $ ax + by = c $, exgcd can find $ ax + by = gcd (a, b) $ solution x0, y0, provided t = c / gcd (a, b); the $ a * tx_0 + b * ty_0 = t * gcd (a, b) = c $.
Then we obtain a set of equations Laid solutions, general solution for $ x = x_0 + kb, y = y_0-ka $, then the count can enumerate k (add some optimization that can get 20 points), but this is too slow.
You can not enumerate it? a / = gcd (a, b ), b / = gca (a, b), c / = gcd (a, b) = t; as $ X = x_0 + kB $, the x0 mold B (if <= 0 , + b), to give the smallest positive number x0 solution (in this case the minimum K), $ C $ = AX + by to give $ y_0 = (ca * x_0) / b $,
Then we solution to a maximum of y0, y0% = a, we obtain a smallest positive number solution y0, the ans = (y0-miny) / a0 + 1。
Then for negative it?
Only need a, b become positive solving, after a, b and obtained x0, y0 passenger -1, the equation still holds.
1 #include<iostream> 2 #include<cstdio> 3 #define LL long long 4 #define int LL 5 using namespace std; 6 int T,a,b,c; 7 int gcd(int a,int b){return b==0?a:gcd(b,a%b);} 8 int exgcd(int a,int b,int &x,int &y) 9 { 10 if(!b){x=1,y=0;return a;} 11 int gcd=exgcd(b,a%b,x,y),t=x; 12 x=y,y=t-a/b*y; 13 return gcd; 14 } 15 inline int read(); 16 signed main() 17 { 18 // freopen("in.txt", "r", stdin); 19 // freopen("0.out", "w", stdout); 20 cin>>T; 21 while(T--) 22 { 23 cin>>a>>b>>c; 24 if(a==0&&b==0&&c==0){puts("ZenMeZheMeDuo");continue;} 25 if(a==0&&b==0&&c!=0){puts("0");continue;} 26 if(a==0&&c==0){puts("ZenMeZheMeDuo");continue;} 27 if(a==0&&c%b==0&&c/b>0){puts("ZenMeZheMeDuo");continue;} 28 if(a==0){puts("0");continue;} 29 if(b==0&&c==0){puts("ZenMeZheMeDuo");continue;} 30 if(b==0&&c%a==0&&c/a>0){puts("ZenMeZheMeDuo");continue;} 31 if(b==0){puts("0");continue;} 32 if(a<0&&b<0&&c<0){a=-a,b=-b,c=-c;} 33 if(a==1&&b==1)// 34 { 35 if(c>=65536){puts("ZenMeZheMeDuo");continue;} 36 else {printf("%lld\n",c-1);continue;} 37 } 38 if(a+b==c){puts("1");continue;} 39 if(a<=1000&&b<=1000&&c<=1000&&a>0&&b>0&&c>0)// 40 { 41 int ans=0; 42 for(int x=1;x<=c;x++) 43 { 44 for(int y=1;y<=c;and ++ 46 { 45) if(a*x+b*y==c)ans++; 47 if(ans>=65536)break; 48 } 49 if(ans>=65536)break; 50 } 51 if(ans>=65536){puts("ZenMeZheMeDuo");continue;} 52 else {printf("%lld\n",ans);continue;} 53 } 54 else 55 { 56 int fa=0,fb=0; 57 if(c<0)a=-a,b=-b,c=-c; 58 if(a<0)a=-a,fa=1; 59 if(b<0)b=-b,fb=1; 60 int GCD=gcd(a,b); 61 if(c%GCD!=0){puts("0");continue;} 62 int t=c/GCD,x0,y0; 63 exgcd(a,b,x0,y0);x0*=t;y0*=t; 64 int a0=a/GCD,b0=b/GCD;c=t; 65 if(fa)a0=-a0,x0=-x0; 66 if(fb)b0=-b0,y0=-y0; 67 if(a0<0)a0=-a0,b0=-b0,c=-c; 68 if(a0*b0<0){puts("ZenMeZheMeDuo");continue;} 69 x0=x0%b0; 70 if(x0<=0)x0+=b0; 71 y0=(c-a0*x0)/b0; 72 if(y0<0){puts("0");continue;} 73 LL miny=y0%a0; 74 if(miny==0)miny+=a0;// 75 if(miny>y0){puts("0");continue;} 76 else 77 { 78 int ans=(y0-miny)/a0+1; 79 if(ans>=65536){puts("ZenMeZheMeDuo");continue;} 80 else {printf("%lld\n",ans);continue;} 81 } 82 } 83 } 84 } 85 inline int read() 86 { 87 int s=0,f=1;char a=getchar(); 88 while(a<'0'||a>'9'){if(a=='-')f=-1;a=getchar();} 89 while(a>='0'&&a<='9'){s=s*10+a-'0';a=getchar();} 90 return s*f; 91 }