Uva 11889 - Benefit( 数论 )

 

 

Uva 11889 - Benefit( 数论

 

题意: 
calculate the lowest integer B such that LCM(AB) = C


分析:
LCM(A,B) = C = A*B/GCD(A,B)
C*GCD(A,B) = A*B
C/A = B/GCD(A,B)
如果C%A != 0 无解
否则, 令t = C/A
B = t * GCD(A,B) 即B 一定是 t 的整数倍
从t开始枚举B

#include <cstdio>

typedef long long LL;

typedef unsigned long long ULL;



ULL gcd(ULL a,ULL b)

{

    return b == 0 ? a : gcd(b, a%b);

}



void Orz()

{

    LL A, C, T;

    scanf("%d",&T);

    while(T--)

    {

        scanf("%lld %lld",&A,&C);

        int flag = 1;

        if(C % A == 0)

        {

            LL t = C/A;

            for(int B = t;B <= C; B += t)

            {

                if(B/gcd(B,A) == t)

                {

                    flag = 0;

                    printf("%lld\n",B);

                    break;

                }

            }

        }

        if(flag)

            puts("NO SOLUTION");

    }

}



int main()

{

    Orz();

    return 0;

}
代码君

 





 

你可能感兴趣的:(uva)