LCM lightoj1215

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值