H - Pairs Forming LCM(唯一分解定理)

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)‘.

大体题意:问1-n有多少对数使得lcm=n;

解题思路:

先来看个知识点:

素因子分解:n = p1 ^ e1 * p2 ^ e2 *..........*pn ^ en

for i in range(1,n):

ei 从0取到ei的所有组合

必能包含所有n的因子。

现在取n的两个因子a,b

a=p1 ^ a1 * p2 ^ a2 *..........*pn ^ an

b=p1 ^ b1 * p2 ^ b2 *..........*pn ^ bn

gcd(a,b)=p1 ^ min(a1,b1) * p2 ^ min(a2,b2) *..........*pn ^ min(an,bn)

lcm(a,b)=p1 ^ max(a1,b1) * p2 ^ max(a2,b2) *..........*pn ^ max(an,bn)

题解:

先对n素因子分解,n = p1 ^ e1 * p2 ^ e2 *..........*pk ^ ek,

lcm(a,b)=p1 ^ max(a1,b1) * p2 ^ max(a2,b2) *..........*pk ^ max(ak,bk)

所以,当lcm(a,b)==n时,max(a1,b1)==e1,max(a2,b2)==e2,…max(ak,bk)==ek

当ai == ei时,bi可取 [0, ei] 中的所有数  有 ei+1 种情况,bi==ei时同理。

那么就有2(ei+1)种取法,但是当ai = bi = ei 时有重复,所以取法数为2(ei+1)-1=2*ei+1。
除了 (n, n) 所有的情况都出现了两次  那么满足a<=b的有 (2*ei + 1)) / 2 + 1个

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4
 5 using namespace std;
 6
 7
 8 const int maxn=1e7+10;
 9 const int maxnn=1e6;
10 int T;
11 long long a;
12 bool ip[maxn];
13 unsigned int p[maxnn],num;//输入数据太大会导致超时
14
15 void is_prime()
16 {
17     memset(ip,1,sizeof(ip));
18     ip[0]=ip[1]=0;
19     for(long long i=2;i<=maxn;i++)
20     {
21         if(ip[i])
22         {
23             p[num++]=i;
24             for(long long j=i+i;j<=maxn;j+=i)
25             {
26                 ip[j]=0;
27             }
28         }
29     }
30 }
31
32 int main()
33 {
34     ios::sync_with_stdio(false);
35     cin>>T;
36     is_prime();
37     for(int i=1;i<=T;i++)
38     {
39         cin>>a;
40         long long ans=1;
41         for(int i=0;i<num&&p[i]*p[i]<=a;i++)
42         {
43             if(a%p[i]==0)
44             {
45                 int cnt=0;
46                 while(a%p[i]==0)
47                 {
48                     a/=p[i];
49                     cnt++;
50                 }
51                 ans*=(2*cnt+1);
52             }
53         }
54         if(a>1) ans*=(2*1+1);
55
56         cout<<"Case"<<" "<<i<<":"<<" "<<(ans+1)/2<<endl;
57     }
58     return 0;
59 }

原文地址:https://www.cnblogs.com/Fy1999/p/8948183.html

时间: 2024-08-13 02:52:28

H - Pairs Forming LCM(唯一分解定理)的相关文章

LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)

http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1236 Description Find the result of the following code: long long pairs

LightOJ 1236 - Pairs Forming LCM (LCM&#183;唯一分解)

题意  给你一个数n  求满足lcm(a, b) == n, a <= b 的 (a,b) 的个数 容易知道 n 是a, b的所有素因子取在a, b中较大指数的积 先将n分解为素数指数积的形式  n = π(pi^ei)    那么对于每个素因子pi  pi在a,b中的指数ai, bi 至少有一个等于pi, 另一个小于等于pi 先不考虑a, b的大小  对于每个素因子pi 1. 在a中的指数 ai == ei   那么 pi 在 b 中的指数可取 [0, ei] 中的所有数  有 ei + 1

Pairs Forming LCM

题目: B - Pairs Forming LCM Time Limit:2000MS     Memory Limit:32768KB 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 )

uva 10791 Minimum Sum LCM ( 唯一分解定理 )

使用唯一分解定理的时候不一定要打出素数表,这句话是相对上一篇来讲的.做这道题目之前我对唯一分解定理方法的理解不完全. 现在多想到了一些 唯一分解,将当前需要分解的n用因子将其分解表达.需要试因子. 因子的枚举应该是从2开始(从1开始没有意义),当当前数字n可以整除当前因子i时,就使其不断除以i,直到不能整除. 这个步骤实际上已经在根本上避免了出现像4.6这种因子在唯一分解式中的出现--之前的因子2和3已经将其代替了.所以可证明唯一分解时并不一定需要构造素数表 针对本题来说,最小公倍数的最小和,有

LightOJ 1236 - Pairs Forming LCM(素因子分解)

题意 给你一个数n 求满足lcm(a, b) == n, a <= b 的 (a,b) 的个数 容易知道 n 是a, b的所有素因子取在a, b中较大指数的积 先将n分解为素数指数积的形式 n = π(pi^ei) 那么对于每个素因子pi pi在a,b中的指数ai, bi 至少有一个等于pi, 另一个小于等于pi 先不考虑a, b的大小 对于每个素因子pi 1. 在a中的指数 ai == ei 那么 pi 在 b 中的指数可取 [0, ei] 中的所有数 有 ei + 1 种情况 2. 在a中的

1236 - Pairs Forming LCM -- LightOj1236 (LCM) 给你一个数n,让你求1到n之间的数(a,b &amp;&amp; a&lt;=b)两个数的最小公倍数等于n有多少对这样的ab.

题意:http://www.lightoj.com/volume_showproblem.php?problem=1236 解答:http://www.cnblogs.com/linliu/p/5549544.html 素数太大用bool #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue> #include<map

lightoj 1236 正整数唯一分解定理

A - (例题)整数分解 Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description Find the result of the following code: long long pairsFormLCM( int n ) {    long long res = 0;   

hdu4497-GCD and LCM-(欧拉筛+唯一分解定理+组合数)

GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 3409    Accepted Submission(s): 1503 Problem Description Given two positive integers G and L, could you tell me how many solutions of

NOIP2009Hankson 的趣味题[唯一分解定理|暴力]

题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现 在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上,老师讲解了如何求两个正整数 c1 和 c2 的最大公约数和最小公倍数.现 在 Hankson 认为自己已经熟练地掌握了这些知识,他开始思考一个“求公约数”和“求公 倍数”之类问题的“逆问题”,这个问题是这样的:已知正整数 a0,a1,b0,b1,设某未知正整 数 x 满足: 1. x 和 a0 的最大公约