已知a, b, c的最小公倍数为L, 给你a,b,问你是否存在最小的c满足题意,不存在输出impossible
素数分解
1 #include <cmath> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #define INF 0x3f3f3f3f 6 #define MOD 1000000007 7 using namespace std; 8 typedef long long LL; 9 10 int T; 11 LL A, B, L; 12 const int maxn = 1e6 + 10; 13 14 int a[maxn], b[maxn], l[maxn]; 15 16 bool is_prime[maxn]; 17 int prime[maxn]; 18 19 void init() { 20 for (int i = 0; i < maxn; i++) is_prime[i] = true; 21 is_prime[0] = is_prime[1] = false; 22 for (int i = 2; i < maxn; i++) { 23 if (is_prime[i]) { 24 prime[++prime[0]] = i; 25 for (int j = i + i; j < maxn; j += i) { 26 is_prime[j] = false; 27 } 28 } 29 } 30 } 31 32 void Break(LL &x, int c[]) { 33 for (int i = 1; i <= prime[0] && x != 1; i++) { 34 while (x % prime[i] == 0) { 35 x /= prime[i]; 36 c[prime[i]]++; 37 } 38 } 39 } 40 41 42 void solve() { 43 memset(a, 0, sizeof(a)); 44 memset(b, 0, sizeof(b)); 45 memset(l, 0, sizeof(l)); 46 Break(A, a); Break(B, b), Break(L, l); 47 bool flag = 1; 48 LL ans = 1; 49 for (int i = 2; i < maxn && flag; i++) { 50 if (a[i] > l[i] || b[i] > l[i]) { 51 flag = 0; 52 } else if (a[i] < l[i] && b[i] < l[i]) { 53 ans *= pow((LL)i, (LL)l[i]); 54 } 55 } 56 ans *= L; 57 if (!flag) { 58 puts("impossible"); 59 } else { 60 printf("%lld ", ans); 61 } 62 } 63 64 int main() { 65 init(); 66 scanf("%d", &T); 67 for (int t = 1; t <= T; t++) { 68 scanf("%lld%lld%lld", &A, &B, &L); 69 printf("Case %d: ", t); 70 solve(); 71 } 72 73 return 0; 74 }