LightOJ1236 —— 唯一分解定理 + 最小公倍数

题目链接:https://vjudge.net/problem/LightOJ-1236

 

1236 - Pairs Forming LCM
Time Limit: 2 second(s)Memory Limit: 32 MB

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

Output for Sample Input

15

2

3

4

6

8

10

12

15

18

20

21

24

25

27

29

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

 

 

题意:

求1到n(1e14)之内,有多少对数(i,j),其中i<=j,使得LCM(i,j)= n,LCM为最小公倍数。

 

题解:

1.设pi为第i个质数。设两个数A、B,他们可表示为:A = p1^a1 * p2^a2…… ,B = p1^b1 * p2^b2……。

那么他们的最小公倍数为:LCM(A, B) = p1^max(a1,b1) * p2^max(a2, b2)……

2.对n进行质因数分解,得到: n = p1^c1 * p2^c2……。当 LCM(A, B) = n时, ci = max(ai, bi),即要么 ci = ai,要么ci = bi。

3 当ci = ai时, bi的可选择范围为[0,ci]共ci+1种;同理当ci = bi时,ai也有ci+1种选择。但是 (ai=ci,bi=ci)被重复计算了一次,所以对于素数pi,总共有 2*ci+1种选择。所以,当不考虑A、B的大小时,总共有 ∏ 2*ci+1对(A,B),使得 LCM(A, B) = n。

4.再考虑回A、B的大小限制,即A<=B,可知除了A = B = n时,其他的组合都出现了两次,即(A,B)和(B,A)都存在,而要门只需要A<=B的那一个。总的来说,最终有 ((∏ 2*ci+1)+1)/2对 (A,B)满足条件。

 

 

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const int INF = 2e9;
15 const LL LNF = 9e18;
16 const int MOD = 1e9+7;
17 const int MAXN = 1e7+10;
18 
19 bool notprime[MAXN];
20 int prime[MAXN/10];
21 void getPrime()
22 {
23     memset(notprime, false, sizeof(notprime));
24     notprime[0] = notprime[1] = true;
25     prime[0] = 0;
26     for (int i = 2; i<MAXN; i++)
27     {
28         if (!notprime[i])prime[++prime[0]] = i;
29         for (int j = 1; j<=prime[0]&& prime[j]<MAXN/i; j++)
30         {
31             notprime[prime[j]*i] = true;
32             if (i%prime[j] == 0) break;
33         }
34     }
35 }
36 
37 int fatCnt;
38 LL factor[1000][2];
39 int getFactors(LL n)
40 {
41     LL tmp = n;
42     fatCnt = 0;
43     for(int i = 1; prime[i]<=tmp/prime[i]; i++)
44     {
45         if(tmp%prime[i]==0)
46         {
47             factor[++fatCnt][0] = prime[i];
48             factor[fatCnt][1] = 0;
49             while(tmp%prime[i]==0) tmp /= prime[i], factor[fatCnt][1]++;
50         }
51     }
52     if(tmp>1) factor[++fatCnt][0] = tmp, factor[fatCnt][1] = 1;
53 }
54 
55 int main()
56 {
57     getPrime();
58     int T, kase = 0;
59     scanf("%d", &T);
60     while(T--)
61     {
62         LL n;
63         scanf("%lld", &n);
64         getFactors(n);
65         LL sum = 1;
66         for(int i = 1; i<=fatCnt; i++)
67             sum *= 2*factor[i][1]+1;
68 
69         sum = (sum+1)/2;
70         printf("Case %d: %lld\n", ++kase, sum);
71     }
72 }
View Code

 

转载于:https://www.cnblogs.com/DOLFAMINGO/p/8371570.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值