bzoj 2818: Gcd GCD(a,b) = 素数

2818: Gcd

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 1566  Solved: 691
[Submit][Status]

Description

给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的
数对(x,y)有多少对.

Input

一个整数N

Output

如题

Sample Input

4

Sample Output

4

HINT

hint

对于样例(2,2),(2,4),(3,3),(4,2)

1<=N<=10^7

题解:http://wzimpha.sinaapp.com/archives/499#comment-41

 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 Table

Problem 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.

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:
210 10100 100
Output:
302791

一样的题,只不过 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 }
时间: 2024-10-29 19:11:50

bzoj 2818: Gcd GCD(a,b) = 素数的相关文章

【BZOJ 2818】Gcd

这题一开始我竟然想要用与能量采集差不多的思路去做= =(no zuo no die,why you try?) 有个显然的转化 ∑nx=1∑ny=1[gcd(x,y)==P]=∑?n/P?x=1∑?n/P?y=1[gcd(x,y)==1]=2?∑?n/P?i=2?(i)+1 然后我们线性筛出欧拉函数,在前缀和就可以O(n)求出答案了 code: #include<iostream> #include<cstdio> #include<cstring> using nam

【BZOJ 2818】gcd 欧拉筛

枚举小于n的质数,然后再枚举小于n/这个质数的Φ的和,乘2再加1即可.乘2是因为xy互换是另一组解,加1是x==y==1时的一组解.至于求和我们只需处理前缀和就可以啦,注意Φ(1)的值不能包含在前缀和里,因为这样就会把x==y==1的情况算2次了,,,貌似包含后只要乘2再减1就可以了 #include<cstdio> using namespace std; const int N=10000003; int num=0,prime[N],phi[N]; long long sum[N]; b

BZOJ 2818: Gcd区间内最大公约数 为素数的对数(欧拉函数的应用)

传送门 2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MB Submit: 3649 Solved: 1605 [Submit][Status][Discuss] Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sample Output 4 HINT hint 对于样例(2,2),(2,4),(3,3),(4,

bzoj 2818 GCD 数论 欧拉函数

bzoj[2818]Gcd Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sample Output 4 HINT hint对于样例(2,2),(2,4),(3,3),(4,2) 1<=N<=10^7 题解一(自己yy) phi[i]表示与x互质的数的个数 即gcd(x,y)=1 1<=y<x ∴对于x,y 若a为素数 则gcd(xa,

BZOJ 2818: Gcd

2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4443  Solved: 1960[Submit][Status][Discuss] Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sample Output 4 HINT hint 对于样例(2,2),(2,4),(3,3),(4,2) 1&

BZOJ 2818 Gcd (莫比乌斯反演 或 欧拉函数)

2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MB Submit: 2534  Solved: 1129 [Submit][Status][Discuss] Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sample Output 4 HINT hint 对于样例(2,2),(2,4),(3,3),(4,2)

bzoj 2818: Gcd 歐拉函數

2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1633  Solved: 724[Submit][Status] Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sample Output 4 HINT hint 对于样例(2,2),(2,4),(3,3),(4,2) 1<=N<=

【BZOJ】【2818】Gcd

欧拉函数/莫比乌斯函数 嗯……跟2910很像的一道题,在上道题的基础上我们很容易就想到先求出gcd(x,y)==1的组,然后再让x*=prime[i],y*=prime[i]这样它们的最大公约数就是prime[i]了…… 当然我们完全没必要这样做……对于每个prime[j],计算在(1,n/prime[j])范围内互质的数的对数,记为f[j],那么答案就等于sigma(f[j]) f[j]的求法还是和以前一样啦~(sigma φ(i))*2+1  (加一是因为类似 5,5 这样两个质数它俩的GC

bzoj 2818 gcd 线性欧拉函数

2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sample Output 4 HINT hint 对于样例(2,2),(2,4),(3,3),(4,2) 1<=N<=10^7 思路:gcd(x,y)