bzoj 2818: Gcd GCD(a,b) = 素数
2014-08-26 23:41
399 查看
2818: Gcd
Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1566 Solved: 691
[Submit][Status]
Description
给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对.
Input
一个整数NOutput
如题Sample Input
4Sample Output
4HINT
hint对于样例(2,2),(2,4),(3,3),(4,2)
1<=N<=10^7
题解:http://wzimpha.sinaapp.com/archives/499#comment-41
#include<iostream> #include<stdio.h> #include<cstring> #include<cstdlib> using namespace std; typedef long long LL; const int maxn = 1e7+1; bool s[maxn]; int prime[maxn],len = 0; int mu[maxn]; int g[maxn]; int sum1[maxn]; void init() { memset(s,true,sizeof(s)); mu[1] = 1; for(int i=2; i<maxn; i++) { if(s[i] == true) { prime[++len] = i; mu[i] = -1; g[i] = 1; } for(int j=1; j<=len && (long long)prime[j]*i<maxn; j++) { s[i*prime[j]] = false; if(i%prime[j]!=0) { mu[i*prime[j]] = -mu[i]; g[i*prime[j]] = mu[i] - g[i]; } else { mu[i*prime[j]] = 0; g[i*prime[j]] = mu[i]; break; } } } for(int i=1; i<maxn; i++) sum1[i] = sum1[i-1]+g[i]; } int main() { int a; init(); while(scanf("%d",&a)>0) { LL sum = 0; for(int i=1,la = 0 ; i<=a; i = la+1) { la = a/(a/i); sum = sum + (long long)(sum1[la] - sum1[i-1])*(a/i)*(a/i); } printf("%lld\n",sum); } return 0; }
spoj
4491. Primes in GCD TableProblem code: PGCD |
Input
First, t ≤ 10, the number of test cases. Each test case consists of two integers, 1 ≤ a,b < 107.Output
For each test case write one number - the number of prime numbers Johnny wrote in that test case.Example
Input:
2 10 10 100 100
Output:
30 2791 一样的题,只不过 GCD(x,y) = 素数 . 1<=x<=a ; 1<=y<=b;
#include<iostream> #include<stdio.h> #include<cstring> #include<cstdlib> using namespace std; typedef long long LL; const int maxn = 1e7+1; bool s[maxn]; int prime[maxn],len = 0; int mu[maxn]; int g[maxn]; int sum1[maxn]; void init() { memset(s,true,sizeof(s)); mu[1] = 1; for(int i=2;i<maxn;i++) { if(s[i] == true) { prime[++len] = i; mu[i] = -1; g[i] = 1; } for(int j=1;j<=len && (long long)prime[j]*i<maxn;j++) { s[i*prime[j]] = false; if(i%prime[j]!=0) { mu[i*prime[j]] = -mu[i]; g[i*prime[j]] = mu[i] - g[i]; } else { mu[i*prime[j]] = 0; g[i*prime[j]] = mu[i]; break; } } } for(int i=1;i<maxn;i++) sum1[i] = sum1[i-1]+g[i]; } int main() { int T,a,b; init(); scanf("%d",&T); while(T--) { scanf("%d%d",&a,&b); if(a>b) swap(a,b); LL sum = 0; for(int i=1,la = 0 ;i<=a;i = la+1) { la = min(a/(a/i),b/(b/i)); sum = sum + (long long)(sum1[la] - sum1[i-1])*(a/i)*(b/i); } printf("%lld\n",sum); } return 0; }
相关文章推荐
- BZOJ 2818 Gcd(gcd(x,y)为素数/欧拉函数/莫比乌斯反演)
- BZOJ2818 Gcd 素数筛+欧拉筛
- BZOJ 2818: Gcd区间内最大公约数 为素数的对数(欧拉函数的应用)
- HYSBZ - 2818(gcd(n,m)为素数的对数
- [BZOJ2818]Gcd
- BZOJ 2818: Gcd
- bzoj 2818 Gcd 欧拉函数
- 【BZOJ 2818】Gcd
- HYSBZ - 2818(gcd(n,m)为素数的对数
- Bzoj 2818: Gcd 莫比乌斯,分块,欧拉函数,线性筛
- [BZOJ2818]Gcd
- bzoj 2818: Gcd
- 【bzoj2818】Gcd 线性筛法
- 【欧拉函数】BZOJ2818-GCD
- [BZOJ 2818]Gcd:莫比乌斯反演
- BZOJ 2818 Gcd 线性欧拉筛(Eratosthenes银幕)
- [BZOJ 2818] Gcd 线性筛+欧拉函数前缀和
- BZOJ-2818-Gcd(欧拉函数/Mobius反演)
- bzoj 2818 Gcd 【欧拉函数】
- [BZOJ2818] Gcd