http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/H
Description
Find the result of the following code:
long long pairsFormLCM( int n ) {
long long res = 0;
for( int i = 1; i <= n; i++ )
for( int j = i; j <= n; j++ )
if( lcm(i, j) == n ) res++; // lcm means least common multiple
return res;
}
A straight forward implementation of the code may time out. If you analyze the code, you will find that the code actually counts the number of pairs (i, j) for which lcm(i, j) = n and (i ≤ j).
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 1014).
Output
For each case, print the case number and the value returned by the function 'pairsFormLCM(n)'.
Sample Input
15
2
3
4
6
8
10
12
15
18
20
21
24
25
27
29
Sample Output
Case 1: 2
Case 2: 2
Case 3: 3
Case 4: 5
Case 5: 4
Case 6: 5
Case 7: 8
Case 8: 5
Case 9: 8
Case 10: 8
Case 11: 5
Case 12: 11
Case 13: 3
Case 14: 4
Case 15: 2
n可以表示为:n=p1^x1*p2^x1.....pk^xk。
假设lcm(a,b) == n;
a = p1^c1 * p2^c2 ..... pk^ck。
b = p1^e1 * p2^e2 .... pk^ek。
xi = max(ci, ei)。
对于有序数对(a,b),有唯一分解定理知,每一个素因数的幂都决定了一个独一无二的数。
求(a,b)的种数就可以转化为求(ci,ei)的种数:ans = (2*x1+1)*(2*x2+1).....(2*xk+1)。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <queue>
#include <stack>
#include <vector>
#include <map>
using namespace std;
#define N 10000500
#define INF 0x3f3f3f3f
#define PI acos (-1.0)
#define EPS 1e-8
#define met(a, b) memset (a, b, sizeof (a))
typedef long long LL;
int isprim[800000], k = 0;
char a[N] = {1, 1};
LL n;
void prim ();
int main ()
{
prim ();
int t, nCase = 1;
LL n;
scanf ("%d", &t);
while (t--)
{
scanf ("%lld", &n);
int i = 0;
LL ans = 1;
while (n > 1 && i < k)
{
int t = 0;
while (n % isprim[i] == 0)
{
n /= isprim[i];
t++;
}
i++;
if (t) ans *= 2 * t + 1;
}
if (n != 1) ans *= 3;
//由于素数打表的只能打到sqrt(n)
//当n不为1的时候说明n还有一个大于sqrt(n)的素因子,所以ans*=(2*1+1);
printf ("Case %d: %lld\n", nCase++, (ans+1)/2);
}
return 0;
}
void prim ()
{
for (LL i=2; i<N; i++)
{
if (!a[i])
{
isprim[k++] = i;
for (LL j=i*i; j<N; j+=i)
a[j] = 1;
}
}
}