LightOJ 1236 Pairs Forming LCM (唯一分解定理)
2016-02-28 11:33
525 查看
Pairs Forming LCM 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;
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^c1*p2^c2*...pm^cm
假设已找到一对(a,b)的lcm=n
有a=p1^d1*p2^d2*...pm^dm
b=p1^e1*p2^e2*...pm^em
易知max(di,ei)=ci
所以必有一个di或ei = ci;
种数为 (2 * (ci + 1) - 1),都为ci是重复了,所以减一;
所以有序对(a,b)的种数ans=(2*c1+1)*(2*c2+1)*(2*c3+1)*...*(2*cm+1)
但是要求求无序对的种数,已知(a,b)(b,a)重复,除了(n,n)只算了一个之外,所以ans = ans/2 +1
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
long long a, c;
long long prime[700000];
bool vis[10000005];//用bool定义减少内存
int tot;
void isprime() // 线性素数筛选
{
tot = 0;
for(int i = 2; i <= 10000000; ++ i)
{
if(!vis[i])
{
prime[tot ++] = i;
}
for(int j = 0; j < tot && prime[j] * i < 10000000; ++ j)
{
vis[prime[j] * i] = 1;
if(i % prime[j] == 0)
break;
}
}
}
//char b[1000005];
int main()
{
int t;
long long n;
/*printf("%d\n", 32768 * 1024 / sizeof(double));
printf("%d\n", 32768 * 1024 / sizeof(bool));
printf("%d\n", 32768 * 1024 / sizeof(long long));*/
scanf("%d", &t);
int tt = 1;
isprime();
while(t --)
{
scanf("%lld", &n);
long long ans = 1;
for(int i = 0; i < tot; ++ i)
{
if(prime[i] * prime[i] > n)
break;
long long c = 0;
while(n % prime[i] == 0)
{
c ++;
n /= prime[i];
}
if(c)
ans *= (2 * c + 1);
}
if(n > 1)
ans *= 3;
printf("Case %d: %lld\n", tt ++, ans / 2 + 1);
}
}
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;
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^c1*p2^c2*...pm^cm
假设已找到一对(a,b)的lcm=n
有a=p1^d1*p2^d2*...pm^dm
b=p1^e1*p2^e2*...pm^em
易知max(di,ei)=ci
所以必有一个di或ei = ci;
种数为 (2 * (ci + 1) - 1),都为ci是重复了,所以减一;
所以有序对(a,b)的种数ans=(2*c1+1)*(2*c2+1)*(2*c3+1)*...*(2*cm+1)
但是要求求无序对的种数,已知(a,b)(b,a)重复,除了(n,n)只算了一个之外,所以ans = ans/2 +1
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
long long a, c;
long long prime[700000];
bool vis[10000005];//用bool定义减少内存
int tot;
void isprime() // 线性素数筛选
{
tot = 0;
for(int i = 2; i <= 10000000; ++ i)
{
if(!vis[i])
{
prime[tot ++] = i;
}
for(int j = 0; j < tot && prime[j] * i < 10000000; ++ j)
{
vis[prime[j] * i] = 1;
if(i % prime[j] == 0)
break;
}
}
}
//char b[1000005];
int main()
{
int t;
long long n;
/*printf("%d\n", 32768 * 1024 / sizeof(double));
printf("%d\n", 32768 * 1024 / sizeof(bool));
printf("%d\n", 32768 * 1024 / sizeof(long long));*/
scanf("%d", &t);
int tt = 1;
isprime();
while(t --)
{
scanf("%lld", &n);
long long ans = 1;
for(int i = 0; i < tot; ++ i)
{
if(prime[i] * prime[i] > n)
break;
long long c = 0;
while(n % prime[i] == 0)
{
c ++;
n /= prime[i];
}
if(c)
ans *= (2 * c + 1);
}
if(n > 1)
ans *= 3;
printf("Case %d: %lld\n", tt ++, ans / 2 + 1);
}
}
相关文章推荐
- 内核线程与用户线程的区别 http://blog.csdn.net/wangkechuang/article/details/7883170
- 1106 Lowest Price in Supply Chain
- AIDL 一
- lintcdoe: Number of Airplanes in the Sky
- Leetcode ☞ 217. Contains Duplicate
- 【UVa】11413 – Fill the Containers
- GCJ--Millionaire (2008 APAC local onsites C)
- 关于main主函数的两个参数argc和argv
- ZOJ 3781 Paint the Grid Reloaded 图论 bfs
- 11.2 RAC: In "crsctl stat res -t" State Details May Be Missing or Incorrect (文档 ID 1086563.1)
- Hanoi Tower Troubles Again! (找规律)
- GetPostBackEventReference加RaisePostBackEvent实现自定义控件中回调传参
- Codeforces - AIM Tech Round (Div. 2)C - Graph and String
- ZOJ 3780 Paint the Grid Again
- 使用Keychain配置
- Leet Code OJ 70. Climbing Stairs [Difficulty: Easy]
- 1021 Fibonacci Again
- 集训队专题(7)1001 Drainage Ditches
- train validation test
- 1090 Higest Price in Supply Chain