LightOJ 1236 - Pairs Forming LCM(素因子分解)
2016-03-19 19:54
483 查看
B - 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 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 求满足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中的指数 ai < ei 即 ai 在 [0, ei) 中 那么 pi 在 b 中的指数只能取 ei 有 ei 种情况
那么对于每个素因子都有 2*ei + 1种情况 也就是满足条件的 (a, b) 有π(2*ei + 1)个 考虑大小时除了 (n, n) 所有的情况都出现了两次 那么满足a<=b的有(π(2*ei + 1)) / 2 + 1个
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 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 求满足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中的指数 ai < ei 即 ai 在 [0, ei) 中 那么 pi 在 b 中的指数只能取 ei 有 ei 种情况
那么对于每个素因子都有 2*ei + 1种情况 也就是满足条件的 (a, b) 有π(2*ei + 1)个 考虑大小时除了 (n, n) 所有的情况都出现了两次 那么满足a<=b的有(π(2*ei + 1)) / 2 + 1个
#include <iostream> #include <cstdio> using namespace std; typedef long long ll; const int maxn=1e7+5;//这里不能用typedef 1e7 int prime[maxn/10]; //这里一定要除以10 否则会超内存 一共664579个素数 int cnt=0; bool isprime[maxn]; void getprime() //素筛打表 { for(int i=2;i<=maxn;i++) { if(!isprime[i]) { prime[cnt++]=i; for(int j=i+i;j<=maxn;j+=i) isprime[j]=1; } } } int main() { getprime(); //先get素数表 //cout<<cnt<<endl; int t,cas=1; cin>>t; while(t--) { ll n,sum,ans=1; cin>>n; for(int i=0;i<cnt;i++) { sum=0; if(prime[i]*prime[i]>n) //记得break break; if(n%prime[i]==0) { while(n%prime[i]==0) { n/=prime[i]; sum++; } ans=ans*(sum*2+1); } } if(n>1) ans*=3; ans=ans/2+1; printf("Case %d: %lld\n",cas++,ans); } return 0; }
相关文章推荐
- Fast-fail原理
- SSDC_从0到1,手腕上的人工智能
- Rejected because no crossdomain.xml policy file was found
- RAID浅谈
- raid浅析
- raid浅析
- 对于人工智能的思考
- Failed to find the style corresponding to the id 2130772027
- ACM学习历程—HDU2476 String painter(动态规划)
- Fail2ban 防止暴力破解centos服务器的SSH或者FTP账户
- Fail2ban 防止暴力破解centos服务器的SSH或者FTP账户
- IntelliJ 创建main函数快捷
- FOJ-1001-Duplicate Pair
- LeetCode-70-Climbing Stairs(动态规划)-Easy
- 建议0:不要让main返回0
- ElasticsearchIllegalArgumentException[failed to find analyzer [ik]]问题解决
- ElasticsearchIllegalArgumentException[failed to find analyzer [ik]]问题解决
- hdu1022 Train Problem I 栈的应用
- 利用DataGridView 的 RowPostPaint 事件 和 CellFormatting 事件 对行划线
- 如何理解wait前面需要加while循环