LightOJ 1236 Pairs Forming LCM(唯一分解定理+素数刷选)
2015-06-19 00:11
453 查看
Pairs Forming LCM
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分解,记下各个因子的个数,假设(a,b)满足LCM(a,b)==n,
则对于n的每种因子num[i],a,b中至少有一个数该种因子的个数为num[i];
另外一个则有k种情况,还有一种情况是a=b,即两个数该种因子都为num[i].
所以,一种因子的情况为C(2,1)*num[i]+1.将每种因子的情况数相乘就是答案了。
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分解,记下各个因子的个数,假设(a,b)满足LCM(a,b)==n,
则对于n的每种因子num[i],a,b中至少有一个数该种因子的个数为num[i];
另外一个则有k种情况,还有一种情况是a=b,即两个数该种因子都为num[i].
所以,一种因子的情况为C(2,1)*num[i]+1.将每种因子的情况数相乘就是答案了。
#include<cstring> #include<cstdio> #include<iostream> #include<algorithm> #include<cmath> #include<vector> #define ll long long #define N 10000010 using namespace std; ll n; vector<int>vec; bool _is ; void init() { memset(_is,0,sizeof _is); _is[0]=_is[1]=1; for(int i=2; i<N; i++) { if(!_is[i]) { vec.push_back(i); for(int j=i+i; j<N; j+=i) _is[j]=1; } } } int main() { freopen("test.in","r",stdin); init(); int t; int ca=1; cin>>t; while(t--) { scanf("%lld",&n); ll ans=1; int k; for(int i=0; i<vec.size(); i++) { if(n<vec[i])break; k=0; if(n%vec[i]==0) { while(n%vec[i]==0) { k++; n/=vec[i]; } } if(k) ans*=(2*(k)+1); } if(n!=1)ans*=3;//最后一个因子 printf("Case %d: %lld\n",ca++,(ans+1)/2);//由题意,答案要除以二 } return 0; }
相关文章推荐
- Thunderbird Mail 支持 Microsoft Exchange
- Thunderbird Mail 支持 Microsoft Exchange
- CF 8A Train and Peter
- [leetCode] Contains Duplicate II
- [leetCode] Contains Duplicate
- Contains Duplicate II
- megacli创建RAID1
- rails scaffold生成的crud显示自定义的列名
- 使用Thumbnails对一个文件夹下的所有图片进行压缩处理
- 220 Contains Duplicate III
- loader constraint violation错误
- Auto Layout NSLayoutConstraint 界面布局
- handler.OpenRegionHandler: Failed open of region=[表名] ---异常的解决思路
- Uva - 11853 - Paintball
- Uva - 11853 - Paintball
- updateViewConstraints和updateConstraints
- ofbiz中的container-2
- Mybaits深入
- Mybaits的Jave API
- ofbiz中的container-1