题目链接:https://vjudge.net/contest/381841#problem/I
题目描述:
定义LCM(a,b,c)是a,b,c的最小公倍数,现在给你a,b,L,求满足LCM(a,b,c)=L的最小的c是多少。
解题思路:
很有趣的一道题,突破口是通过唯一分解定理来理解lcm。
先求出a,b的最小公倍数设为m,先把L/m,得到a,b中肯定不含或L指数大的减去m指数小的素因子,然后求出L和m都有的公因子按照需求累乘上去就行了。
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b)
{
return b ? gcd(b, a%b) : a;
}
int main()
{
int T,cas=0;
scanf("%d",&T);
while(T--) {
ll a, b, L;
scanf("%lld%lld%lld",&a,&b,&L);
printf("Case %d: ",++cas);
ll m = a*b/gcd(a,b);
if(L % m) {
printf("impossible\n");
continue;
}
ll c = L/m ;
ll g = gcd(c, m);
while(g != 1) {
c*=g;
m/=g;
g = gcd(c,m);
}
printf("%lld\n",c);
}
}
京公网安备 11010502036488号