Spoj GCD2 - GCD2 Solution

   ///La ilaha illellahu muhammadur rasulullah

///******Bismillahir-Rahmanir-Rahim******///

///Abul Hasnat  Tonmoy

///Department of CSE,23rd batch

    ///Islamic University,Bangladesh

    1. #include <bits/stdc++.h>
    2. using namespace std;
    3. typedef long long int ll;
    4. ll gcd(ll a, ll b)
    5. {
    6. if(a==0)
    7. return b;
    8. return gcd(b%a,a);
    9. }
    10. ll reduceB(ll a, string b)
    11. {
    12. ll mod=0;
    13. for(int i=0; i<b.size(); i++)
    14. mod=(mod*10+b[i]-'0')%a;
    15.  
    16. return mod;
    17. }
    18. ll largegcd(ll a, string b)
    19. {
    20. ll num=reduceB(a,b);
    21. return gcd(a,num);
    22. }
    23. int main()
    24. {
    25. ll t,a;
    26. cin>>t;
    27. while(t--)
    28. {
    29. string b;
    30. cin>>a>>b;
    31. if(a==0)
    32. cout<<b<<endl;
    33. else
    34. cout<<largegcd(a,b)<<endl;
    35. }
    36. return 0;
    37. }
    38.  

    No comments

    Most View Post

    Recent post

    Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

      Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

    Powered by Blogger.