LightOJ 1236 Pairs Forming LCM(唯一分解定理)
阿新 • • 發佈:2019-02-08
題目分析
思路:把n分解成素因數的形式n=p1^c1+p2^c2+…pm^cm
假設已找到一對(a,b)的lcm=n
有a=p1^d1+p2^d2+…pm^dm
b=p1^e1+p2^e2+…pm^em
易知max(di,ei)=ci
先考慮有序數對(a,b),由唯一分解定理知,a的每一個素因數的冪的大小都決定一個獨一無二的數。
所以(a,b)的種數就是(di,ei)的種數,即2*(ci+1)-1(因為有序對(c1,c1)重複了一次所以-1)
所以有序對(a,b)的種數ans=(2*c1+1)(2*c2+1)(2*c3+1)…(2*cm+1)
但是要求求無序對的種數,已知(a,b)(b,a)重複,除了(n,n)只算了一個之外,所以ans = ans/2 +1。
轉自kalilili
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
const int maxn = 1e7+100;
bool vis[maxn];
int prime[maxn/10];
int tot;
void init(){
tot = 0;
for(int i = 2; i < maxn; i++)if(!vis[i]){
prime[tot++] = i;
for (LL j = (LL)i*i; j < maxn; j += i) vis[j] = true;
}
}
int main(){
init();
int T;
scanf("%d", &T);
LL n;
for(int kase = 1; kase <= T; kase++){
scanf("%lld", &n);
LL ans = 1;
for(int i = 0; i < tot && (LL)prime[i]*prime[i] <= n; i++)if (n%prime[i] == 0){
int cnt = 0;
while(n%prime[i] == 0){
cnt++;
n /= prime[i];
}
ans *= (2*cnt+1);
}
if(n > 1) ans *= 3;
ans = ans/2 + 1;
printf("Case %d: %lld\n", kase, ans);
}
return 0;
}