L = LCM(x, y)=x*y/gcd(x, y);如果把x,y,L写成素因子之积的方式会很容易发现 L 就是 x 和 y 中素因子指数较大的那些数之积;

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<math.h>
#include<string>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define N 1000006
LL gcd(LL a,LL b)
{
    return b==0?a:gcd(b,a%b);
}
int main()
{
    int T,t=1;
    scanf("%d",&T);
    while(T--)
    {
        LL a,b,l;
        scanf("%lld%lld%lld",&a,&b,&l);
        LL x=a/gcd(a,b)*b;
        printf("Case %d: ", t++);
        if(l%x)
        {
            printf("impossible\n");
            continue;
        }
        LL y=l/x,z;
        while(z=gcd(x,y),z!=1)
        {
            x=x/z;
            y=y*z;
        }
        printf("%lld\n",y);
    }
    return 0;
}

 

posted on 2017-10-24 16:52  云胡不喜。  阅读(302)  评论(0编辑  收藏  举报