【LightOJ - 121】Finding LC M【算数基本定理】

本文介绍了一个数学问题——给定三个整数a、b和L,如何找到最小的整数c,使得a、b和c的最小公倍数等于L。若无解,则输出'impossible'。文章提供了具体的输入输出格式及示例,并附带了解决该问题的C++代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LCM is an abbreviation used for Least Common Multiple in Mathematics. We say LCM (a, b, c) = L if and only if L is the least integer which is divisible by a, b and c.

You will be given a, b and L. You have to find c such that LCM (a, b, c) = L. If there are several solutions, print the one where c is as small as possible. If there is no solution, report so.

Input
Input starts with an integer T (≤ 325), denoting the number of test cases.

Each case starts with a line containing three integers a b L (1 ≤ a, b ≤ 106, 1 ≤ L ≤ 1012).

Output
For each case, print the case number and the minimum possible value of c. If no solution is found, print ‘impossible’.

Sample Input
3
3 5 30
209475 6992 77086800
2 6 10
Sample Output
Case 1: 2
Case 2: 1
Case 3: impossible

代码

#include<bits/stdc++.h>
using namespace std;
#define LL long long

const int N = (int)1e6+2;
const int M = 1E6+11;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

bool su[N+1]={1,1,0};int prm[N+1],sz=0;
void init(){
    for(LL i=2;i<N;i++){
        if(!su[i]){
            prm[sz++]=i;
            for(LL j=i*i;j<N;j+=i)
                su[j]=1;
        }
    }
   // cout<<sz<<endl;;
}
int a[N],b[N];
void solve(int *arr,int n){
    for(int i=0;i<sz ;i++){
        while(n%prm[i]==0){
            arr[i]++;
            n/=prm[i];
        }
        if(n==1) break;
    }
}

int main(){
    init();
    int cas=1;
    int T;scanf("%d",&T);
    while(T--){
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        LL A,B,L;scanf("%lld%lld%lld",&A,&B,&L);
        solve(a,A);
        solve(b,B);
        LL ans=1; int flag=1;
        for(int i=0;i<sz;i++){
            int cnt=0;
            while(L%prm[i]==0){
                cnt++; L/=prm[i];
            }
            if(a[i]>cnt || b[i]>cnt)  {
                flag=0; break;
            }
            if(a[i]==cnt ||b[i]==cnt) continue;
            ans*=pow(prm[i],cnt);
        }

        if(L!=1) ans*=L;
        printf("Case %d: ",cas++);
        if(flag) printf("%lld\n",ans);
        else puts("impossible");
    }
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值