Problem: http://lightoj.com/volume_showproblem.php?problem=1215
LCM(a,b,c)=L
give you a , b and L, please find the minmum c.
solve:
m=lcm(a,b),
then lcm(m,c) is L
m分解为p1^e1 * p2^e2 * p3^e3…..
c分解为 p1^E1 * p2^E2 * p3^E3…..
L=p1^max(e1,E1) * p2^max(e2,E2) * p3^max(e3,E3)…..
L/m则中素数种类和最小的c中素数种类相同
思路是把m中所有 c中出现的素数种类 都转移到c中
t=gcd(m,c); c=c*t ; m=m/t; 一直到t为1即m与c互质为止
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn=200+10;
ll a,b,c,L;
ll gcd(ll a,ll b)
{
return b==0?a:gcd(b,a%b);
}
int main()
{
int T;
scanf("%d",&T);
int kase=1;
while(T--){
scanf("%lld%lld%lld",&a,&b,&L);
printf("Case %d: ",kase++);
ll t=gcd(a,b);
t=a/t*b;
if(t>L||L%t){
puts("impossible");
continue;
}
ll m=t;
ll c=L/m;
t=gcd(c,m);
while(t!=1){
c*=t;m/=t;
t=gcd(c,m);
}
printf("%lld\n",c);
}
return 0;
}