给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的
数对(x,y)有多少对.
标签:des style blog http color os io strong for
给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的
数对(x,y)有多少对.
一个整数N
如题
hint
对于样例(2,2),(2,4),(3,3),(4,2)
1<=N<=10^7
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<cstdlib> 5 using namespace std; 6 7 typedef long long LL; 8 const int maxn = 1e7+1; 9 bool s[maxn]; 10 int prime[maxn],len = 0; 11 int mu[maxn]; 12 int g[maxn]; 13 int sum1[maxn]; 14 void init() 15 { 16 memset(s,true,sizeof(s)); 17 mu[1] = 1; 18 for(int i=2; i<maxn; i++) 19 { 20 if(s[i] == true) 21 { 22 prime[++len] = i; 23 mu[i] = -1; 24 g[i] = 1; 25 } 26 for(int j=1; j<=len && (long long)prime[j]*i<maxn; j++) 27 { 28 s[i*prime[j]] = false; 29 if(i%prime[j]!=0) 30 { 31 mu[i*prime[j]] = -mu[i]; 32 g[i*prime[j]] = mu[i] - g[i]; 33 } 34 else 35 { 36 mu[i*prime[j]] = 0; 37 g[i*prime[j]] = mu[i]; 38 break; 39 } 40 } 41 } 42 for(int i=1; i<maxn; i++) 43 sum1[i] = sum1[i-1]+g[i]; 44 } 45 46 int main() 47 { 48 int a; 49 init(); 50 while(scanf("%d",&a)>0) 51 { 52 LL sum = 0; 53 for(int i=1,la = 0 ; i<=a; i = la+1) 54 { 55 la = a/(a/i); 56 sum = sum + (long long)(sum1[la] - sum1[i-1])*(a/i)*(a/i); 57 } 58 printf("%lld\n",sum); 59 } 60 return 0; 61 }
spoj
4491. Primes in GCD TableProblem code: PGCD |
Johnny has created a table which encodes the results of some operation -- a function of two arguments. But instead of a boring multiplication table of the sort you learn by heart at prep-school, he has created a GCD (greatest common divisor) table! So he now has a table (of height a and width b), indexed from (1,1) to (a,b), and with the value of field (i,j) equal to gcd(i,j). He wants to know how many times he has used prime numbers when writing the table.
First, t ≤ 10, the number of test cases. Each test case consists of two integers, 1 ≤ a,b < 107.
For each test case write one number - the number of prime numbers Johnny wrote in that test case.
Input:
2
10 10
100 100
Output:
30
2791
一样的题,只不过 GCD(x,y) = 素数 . 1<=x<=a ; 1<=y<=b;
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<cstdlib> 5 using namespace std; 6 7 typedef long long LL; 8 const int maxn = 1e7+1; 9 bool s[maxn]; 10 int prime[maxn],len = 0; 11 int mu[maxn]; 12 int g[maxn]; 13 int sum1[maxn]; 14 void init() 15 { 16 memset(s,true,sizeof(s)); 17 mu[1] = 1; 18 for(int i=2;i<maxn;i++) 19 { 20 if(s[i] == true) 21 { 22 prime[++len] = i; 23 mu[i] = -1; 24 g[i] = 1; 25 } 26 for(int j=1;j<=len && (long long)prime[j]*i<maxn;j++) 27 { 28 s[i*prime[j]] = false; 29 if(i%prime[j]!=0) 30 { 31 mu[i*prime[j]] = -mu[i]; 32 g[i*prime[j]] = mu[i] - g[i]; 33 } 34 else 35 { 36 mu[i*prime[j]] = 0; 37 g[i*prime[j]] = mu[i]; 38 break; 39 } 40 } 41 } 42 for(int i=1;i<maxn;i++) 43 sum1[i] = sum1[i-1]+g[i]; 44 } 45 46 int main() 47 { 48 int T,a,b; 49 init(); 50 scanf("%d",&T); 51 while(T--) 52 { 53 scanf("%d%d",&a,&b); 54 if(a>b) swap(a,b); 55 LL sum = 0; 56 for(int i=1,la = 0 ;i<=a;i = la+1) 57 { 58 la = min(a/(a/i),b/(b/i)); 59 sum = sum + (long long)(sum1[la] - sum1[i-1])*(a/i)*(b/i); 60 } 61 printf("%lld\n",sum); 62 } 63 return 0; 64 }
标签:des style blog http color os io strong for
原文地址:http://www.cnblogs.com/tom987690183/p/3938477.html