Dev Skill Divisor - Multiple

/*
Lionel Messi is such a player that you may catch him, you may touch him, you may feel him
and definitely you may Love him.
Lionel Messi is Messi. A little Magician in this World.

*/

#include<bits/stdc++.h>

using namespace std;

#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)

typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;

const int mod = 1000007;
const int high = 100;
template<class T> T gcd(T a, T b ) {return b==0?a:gcd<T>(b , a%b);}


struct printf
{
    void ek(int n){cout << n << "\n";}
    void dui(int x , int y) { cout << x << " " << y << "\n"; }
    void tin(int x , int y , int z) { cout << x << " " << y << " " << z << "\n"; }
}tp;

struct sieve
{
    int prm[high], plen=0;
    bitset<high>bs;
    void get_prime()
    {
        LL i , j;
        bs.set();
        bs[0]=bs[1]=0;
        for(i=2; i<=high; i++)
        {
            if(bs[i])
            {
                prm[plen++] = i;

                for(j=i*i; j<=high; j+=i) bs[j] = 0;
            }
        }
    }
    void pprm(){ for(int i=0; i<100; i++) cout << prm[i] << "; "; }
}prime;

LL sol(LL n)
{
    LL i=1 , cnt=0 , vag;

    for(i=1; i * i <= n; i++)
    {
        if(n % i == 0)
        {
            vag = n / i; // here created all the multiples of A, like i=1, B=12 and A=3 so, 3*1=3,B%3==0
            // again, vag=12, B%vag==0, again, i=2, vag=4/2=2, so 3*2=6 and 12%6==0 and so on...

            if(vag != i)
            {
                cnt+=2;
            }

            else cnt+=1;

            //pf("%lld; ", cnt);
        }
    }

    return cnt;
}

int main()
{
    fast;
    int t , tc=0;
    LL A , B , goshago , ans=0;
    sf("%d", &t);
    while(t--)
    {
        sf("%lld %lld", &A , &B);

        pf("Case %d: ", ++tc);

        if(A > B)
        {
             pf("0\n");
             continue;
        }

        goshago = gcd(A , B); //pf("%lld\n" , B/goshago);

        if(A > goshago) pf("0\n");

        else
        {
            ans = sol(B/goshago); // you will get all the multiples of A if you find the divisor of gcd between A and B with O(sqrt(n)) and divide B by the result of gcd.

            pf("%lld\n", ans);
        }
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number