Hdu 5212 Code【容斥原理】
来源:互联网 发布:php极光推送原理 编辑:程序博客网 时间:2024/05/01 11:07
Code
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1028 Accepted Submission(s): 419
Problem Description
WLD likes playing with codes.One day he is writing a function.Howerver,his computer breaks down because the function is too powerful.He is very sad.Can you help him?
The function:
int calc
{
int res=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
res+=gcd(a[i],a[j])*(gcd(a[i],a[j])-1);
res%=10007;
}
return res;
}
The function:
int calc
{
int res=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
res+=gcd(a[i],a[j])*(gcd(a[i],a[j])-1);
res%=10007;
}
return res;
}
Input
There are Multiple Cases.(At MOST 10 )
For each case:
The first line contains an integerN(1≤N≤10000) .
The next line containsN integers a1,a2,...,aN(1≤ai≤10000) .
For each case:
The first line contains an integer
The next line contains
Output
For each case:
Print an integer,denoting what the function returns.
Print an integer,denoting what the function returns.
Sample Input
51 3 4 2 4
Sample Output
64Hintgcd(x,y) means the greatest common divisor of x and y.
题目大意:
计算题干给出的代码的结果。
思路:
初学容斥,推荐这篇博客:http://blog.csdn.net/LYHVOYAGE/article/details/45306211。讲解的很详细
①首先我们知道,如果直接暴力去按照代码去求解的话,时间复杂度是很爆炸的。所以我们考虑优化。
②我们知道,对于一个因子数x来讲,计算其贡献的时候,我们可以找到x这个数的所有的倍数的个数tot,那么任意从中取出两个数来,其gcd的结果都一定是x的倍数(x,2x,3x,4x,5x......................................),那么我们可以枚举因字数,然后找到其倍数的个数,整体时间复杂度为O(nlogn);
③显然,如果我们统计过了x的倍数的个数tot,使得统计答案:ans+=tot*tot*x之后,如果我们再遇到了2x这个因子,也这样计算的话,会出现重复。
那么我们就要考虑去重问题,相对而言,我们可以设定f(x)表示因子x所贡献的价值,那么显然:f(x)=cnt*cnt*x-f(2x)-f(3x)-f(4x)-......................................
④那么我们过程维护一下这个减法即可,我们可以从大到小枚举因子数,然后计算之前将其倍数的答案减去即可。
Ac代码:
#include<stdio.h>#include<string.h>using namespace std;#define mod 10007int a[35000];int cnt[35000];int F[35000];int main(){ int n; while(~scanf("%d",&n)) { memset(cnt,0,sizeof(cnt)); memset(F,0,sizeof(F)); for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<=n;i++) { for(int j=1;j*j<=a[i];j++) { if(a[i]%j!=0)continue; cnt[j]++; if(j*j!=a[i])cnt[a[i]/j]++; } } int ans=0; for(int i=10000;i>=1;i--) { F[i]=cnt[i]*cnt[i]%mod; for(int j=i*2;j<=10000;j+=i) { F[i]-=F[j]; F[i]=(F[i]%mod+mod)%mod; } int val=i*(i-1)%mod; ans+=(val*F[i]%mod+mod)%mod; } printf("%d\n",(ans%mod+mod)%mod); }}
阅读全文
0 0
- Code HDU 5212(容斥原理)
- hdu 5212 Code【容斥原理】【思维】
- Hdu 5212 Code【容斥原理】
- hdu 5212 Code (容斥)
- hdoj Code 5212 (容斥原理)
- HDU 5212 Code(容斥)
- HDU 5212 Code GCD容斥(套路).
- hdu 5212(容斥原理)
- hdu 5212 code 容斥~或者莫比乌斯
- 【容斥原理】HDU 4135
- hdu 4135 容斥原理
- hdu 4336 容斥原理
- hdu 1695 容斥原理
- hdu 4407 容斥原理
- HDU 1796 容斥原理
- hdu 1796 容斥原理
- hdu 4135 (容斥原理)
- HDU 1695 容斥原理
- 转合理配置GOMAXPROCS提升一倍以上的性能
- 2017百度之星1006小小粉丝度度熊(尺举法)
- Scrapy爬数据并存储到mysql中
- jQuery Ajax方法
- Ubuntu从待机中唤醒后鼠标键盘出现僵死情况
- Hdu 5212 Code【容斥原理】
- nexus的仓库介绍,以及配置远程仓库
- [玩转算法]滑动窗口
- intellj idea将自己常用的文件类型添加到菜单栏中
- Android跳转应用市场评分以及目前国内主要的市场包名
- spring junit 测试示例 maven 搭界
- 深入剖析jsonp跨域原理
- 做好企业流程管理的六个建议
- Maven项目中War包的打包及依赖方式