surajumang08

GGCURR

Jan 24th, 2017
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. /* the gcd function used here is from geeksforgeeks.com*/
  2. #include<iostream>
  3. #include<cstdlib>
  4. #include<algorithm>
  5. #include<cmath>
  6. #include<cstdio>
  7.  
  8. using namespace std;
  9. #define MOD 100007
  10.  
  11. // code for extended euclid's algorithm
  12. int gcd(int a, int b, int *x, int *y)
  13. {
  14.  
  15.     if (a == 0)
  16.     {
  17.         *x = 0;
  18.         *y = 1;
  19.         return b;
  20.     }
  21.  
  22.     int x1, y1;
  23.     int gc = gcd(b%a, a, &x1, &y1);
  24.  
  25.     *x = y1 - (b/a) * x1;
  26.     *y = x1;
  27.  
  28.     return gc;
  29. }
  30.  
  31. int main()
  32. {
  33.   int t,n,a,b,num;
  34.   float c;
  35.   cin>>t;
  36.   while(t--)
  37.   {
  38.     cin>>n>>a>>b;
  39.     int x0,y0;//these are the coefficient of a and b respec
  40.     int g1=gcd(a,b,&x0,&y0);
  41.  
  42.     int cash=0,digital=0;
  43.     //cerr<<g1<<" "<<x0<<" "<<y0<<endl;
  44.     for(int i=0;i<n;i++)
  45.       {
  46.         scanf("%d",&num);
  47.         //if num is a multiple of a or b then it is possible
  48.         if(num%a==0 || num%b==0)
  49.           {
  50.             cash++; continue;
  51.           }
  52.         if(num % g1==0)  //change can be made by cash
  53.         {
  54.           //redue a and b such that gcd(a,b)=1
  55.           int ta=a/g1;
  56.           int tb=b/g1;
  57.           float tc=num/g1;
  58.           //cout<<ta<<" "<<tb<<" "<<tc<<endl;
  59.           //find whether positive solutions are there or not
  60.           if(x0 >0 && y0 >0)cash++;
  61.           else if(x0 <0 && y0 < 0 )
  62.           digital++;
  63.           else
  64.           {
  65.             //find the negetive number
  66.             if(y0 < 0)
  67.             {
  68.               int mul=ceil((-1*y0*tc)/ta);
  69.               if((tc*x0 - mul*tb)<=0)
  70.                 digital++;
  71.               else
  72.                 cash++;
  73.  
  74.  
  75.             }
  76.             else
  77.             {
  78.               int mul=ceil((-1*x0*tc)/tb);
  79.               if((tc*y0 - mul*ta)<=0)
  80.                 digital++;
  81.               else
  82.                 cash++;
  83.  
  84.             }
  85.           }
  86.         }
  87.         else
  88.           digital++;
  89.  
  90.  
  91.  
  92.     }
  93.       printf("%d %d\n",cash,digital);
  94.   }
  95. }
Add Comment
Please, Sign In to add comment