Spoj CPPLCM04 - LCM & GCD 4 Solution




Problem Link:  https://www.spoj.com/PTIT/problems/CPPLCM04/  

Solution in C++: 

///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat  Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll gcd(ll a, ll b)
{
    if (!a)
        return b;
    return gcd(b % a, a);
}
ll reduceB(ll a, string b)
{
    ll mod = 0;
    for (int i = 0; i <b.size(); i++)
        mod = (mod * 10 + b[i] - '0') % a;

    return mod;
}
ll gcdLarge(ll a, string b)
{
    ll num = reduceB(a, b);
    return gcd(a, num);
}
int main()
{
    long long a,t;
    string b;
    cin>>t;
    while(t--)
    {
        cin>>a>>b;
        if (a == 0)
            cout << b << endl;
        else
            cout << gcdLarge(a, b) << endl;
    }
    return 0;
}






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.