题目分析
思路:把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;
}