BZOJ2818: Gcd
来源:互联网 发布:手机壁纸diy软件 编辑:程序博客网 时间:2024/05/17 22:40
Portal
就直接用一下别人的题解好了。。
枚举每个素数,然后每个素数p对于答案的贡献就是(1 ~ n / p) 中有序互质对的个数而求1~m中有序互质对x,y的个数,可以令y >= x, 当y = x时,有且只有y = x = 1互质,当y > x时,确定y以后符合条件的个数x就是phiy所以有序互质对的个数为(1 ~ n/p)的欧拉函数之和乘2减1(要求的是有序互质对,乘2以后减去(1, 1)多算的一次)那么就只需要先筛出欧拉函数再求个前缀和就可以了
【代码】
#include <iostream>#include <cstdio>#include <algorithm>#define N 10000005#define mod 1000000007#define INF 0x7fffffffusing namespace std;typedef long long ll;typedef pair<int,int> pa;int read(){ int x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();} while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();} return x*f;} int n;ll ans,sum[N];int p[N],not_Prime[N]={1,1},phi[N];void getphi(){ phi[1]=1; for(int i=2;i<=n;i++) { if(!not_Prime[i]) p[++p[0]]=i,phi[i]=i-1; for(int j=1;j<=p[0]&&i*p[j]<=n;j++) { not_Prime[i*p[j]]=1; if(!(i%p[j])) { phi[i*p[j]]=phi[i]*p[j]; break; } phi[i*p[j]]=phi[i]*phi[p[j]]; } } }int main(){ n=read(); getphi(); for(int i=1;i<=n;i++) sum[i]=sum[i-1]+phi[i]; for(int i=1;i<=p[0];i++) ans+=(sum[n/p[i]]<<1)-1; printf("%lld\n",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 素数筛+欧拉筛
- struct inode 和 struct file
- 华中科技大学 2010 奇偶校验
- I2C通信 读写数据过程
- Java、JFrame制作录制GIF小工具
- initialize方法和init区别详解
- BZOJ2818: Gcd
- AES/CBC/PKCS5Padding加解密
- interactivePopGestureRecognizer
- NodeJS入门(一)- 基本文件路由实现
- open-GOP
- 挑战练习题2.3动态规划 poj1742 Coins 多重背包
- JavaFX TableCell经验分享
- Problem--546A--Codeforces--Soldier and Bananas
- 关于IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]的解决