bzoj2005 能量采集 数论
来源:互联网 发布:windows temp文件夹 编辑:程序博客网 时间:2024/05/17 23:46
这道题目本质上就是求Σ(i=1,m)Σ(j=1,n)2*gcd(i,j)-1,该式等价于2Σ(i=1,m)Σ(j=1,n)gcd(i,j) -m*n=2Σ(d)phi(d)[m/d][n/d]-mn。然后O(N)求出phi()即可。
AC代码如下:
#include<iostream>#include<cstdio>#define N 200005#define ll long longusing namespace std;int m,n,cnt,c[N]; ll phi[N];int main(){scanf("%d%d",&m,&n); int i,j; ll ans=0;if (m<n) swap(m,n); phi[1]=1;for (i=2; i<=n; i++){if (!phi[i]){ phi[i]=i-1; c[++cnt]=i; }for (j=1; j<=cnt; j++){if (i*c[j]>m) break;if (i%c[j]) phi[i*c[j]]=phi[i]*(c[j]-1); else{phi[i*c[j]]=phi[i]*c[j]; break;}}}for (i=2; i<=n; i++) phi[i]+=phi[i-1];for (i=1; i<=n; i=j+1){j=min((m/(m/i)),n/(n/i));ans+=(phi[j]-phi[i-1])*(m/i)*(n/i);}printf("%lld\n",ans*2-(ll)m*n);return 0;}
后来去网上翻了题解,发现是用素数筛的方法。大概是令f(i)表示第i排的总和,则f(i)=Σ(j|i且j为质数)f[j],那么就可以用素数筛的方法了。这里不在介绍。
by lych
2015.12.11
0 0
- bzoj2005 能量采集 数论
- 【BZOJ2005】【codevs1937】能量采集,数论练习之二维公约数求和
- 【bzoj2005】能量采集【GCD】
- 【NOI2010】【BZOJ2005】能量采集
- [BZOJ2005][Noi2010]能量采集
- bzoj2005 能量采集
- BZOJ2005能量采集
- [BZOJ2005][Noi2010]能量采集
- bzoj2005【NOI2010】能量采集
- Bzoj2005[Noi2010]能量采集
- [bzoj2005][Noi2010]能量采集
- 【NOI2010】bzoj2005 能量采集
- BZOJ2005 [Noi2010]能量采集
- bzoj2005[Noi2010]能量采集
- 【bzoj2005】[Noi2010]能量采集
- bzoj2005 [Noi2010]能量采集
- bzoj2005: [Noi2010]能量采集
- BZOJ2005: [Noi2010]能量采集
- 5.2 calendar--通用日期的相关函数(1)
- java Date(int year,int month,int date)需要注意
- hdu 1046 Gridland
- popen函数相关
- Bootstrap table的一些简单使用总结
- bzoj2005 能量采集 数论
- Segue NSTimer 和 Delayed perform
- Mac Eclipse JAVAHL问题
- leetcodeBulls and Cows
- 在ubuntu中安装eclipse for c++
- 你的能力是更适合做微商还是淘宝
- LA 4329 树状数组入门
- 排序算法的种类
- UML之C++类间关系总结