【bzoj2818】Gcd
来源:互联网 发布:宋为什么灭大理知乎 编辑:程序博客网 时间:2024/05/29 08:16
Description
给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的
数对(x,y)有多少对.
Input
一个整数N
Output
如题
Sample Input
4
Sample Output
4
HINT
对于样例(2,2),(2,4),(3,3),(4,2)
1<=N<=10^7
题解:
首先求出phi的值,然后做一个前缀和。
然后枚举小于n的每一个质数。
所有<=n/p[i]的互质的两个数乘上p[i]都小于n且gcd为p[i];
因为已经做前缀和所以直接加上phi[n/p[i]]*2-1即可。
代码
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<cmath>#define N 10001#define M 1000001#define ll long longll sum[10000005],ans;bool mark[10000005];int phi[10000005],p[10000005];int n,tot;using namespace std;inline int read(){ int x=0;char ch=getchar(); while (ch<'0'||ch>'9') ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x;}int main(){ n=read();phi[1]=1; for (int i=2;i<=n;i++) { if (!mark[i]) phi[i]=i-1,p[++tot]=i; for (int j=1;j<=tot&&i*p[j]<=n;j++) { mark[i*p[j]]=1; if (i%p[j]==0) { phi[i*p[j]]=phi[i]*p[j]; break; } phi[i*p[j]]=phi[i]*(p[j]-1); } } for (int i=1;i<=n;i++) sum[i]=sum[i-1]+phi[i]; for (int i=1;i<=tot;i++) ans+=sum[n/p[i]]*2-1; printf("%lld",ans); return 0; }
0 0
- 【BZOJ2818】Gcd
- [BZOJ2818] Gcd
- [BZOJ2818]Gcd
- bzoj2818 Gcd
- [BZOJ2818]Gcd
- [bzoj2818]gcd
- [bzoj2818]gcd
- 【bzoj2818】Gcd
- BZOJ2818: Gcd
- bzoj2818 Gcd
- 【bzoj2818】Gcd
- bzoj2818: Gcd
- bzoj2818 Gcd
- BZOJ2818: Gcd
- BZOJ2818 Gcd
- 【bzoj2818】【GCD】【数论】
- bzoj2818: Gcd(第二次做)
- BZOJ2818 Gcd 素数筛+欧拉筛
- 直接打Jar包几种方式
- [AtCoder AGC002 F][DP]Leftmost Ball
- JAVA虚拟机基本概念
- 随手记录--Spring 基础与容器(1)
- 8天学通MongoDB——第一天 基础入门
- 【bzoj2818】Gcd
- mybatis执行批量更新batch update 的方法(oracle,mysql)
- JAVA——finally块中的代码什么时候被执行、执行的效果是什么?
- java时间
- HashMap实现原理分析
- sizeof()+length()+strlen() 的区别
- Python屏蔽敏感字
- EOJ-大学生程序设计邀请赛(华东师范大学)-E-黑心啤酒厂
- 使用MyEclipse工具快速搭建Struts2框架