数学(hdu5212)
来源:互联网 发布:小白管理器家庭网络 编辑:程序博客网 时间:2024/05/21 22:51
Code
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 319 Accepted Submission(s): 128
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://www.cnblogs.com/JoeFan/p/4458629.html
思路:首先,我们分析每个数对最终答案的影响。
那么我们就要求出:对于每个数,以它为 gcd 的数对有多少对。
显然,对于一个数 x ,以它为 gcd 的两个数一定都是 x 的倍数。如果 x 的倍数在数列中有 k 个,那么最多有 k^2 对数的 gcd 是 x 。
同样显然的是,对于两个数,如果他们都是 x 的倍数,那么他们的 gcd 一定也是 x 的倍数。
所以,我们求出 x 的倍数在数列中有 k 个,然后就有 k^2 对数满足两个数都是 x 的倍数,这 k^2 对数的 gcd,要么是 x ,要么是 2x, 3x, 4x...
并且,一个数是 x 的倍数的倍数,它就一定是 x 的倍数。所以以 x 的倍数为 gcd 的数对,一定都包含在这 k^2 对数中。
如果我们从大到小枚举 x ,这样计算 x 的贡献时,x 的多倍数就已经计算完了。我们用 f(x) 表示以 x 为 gcd 的数对个数。
那么 f(x) = k^2 - f(2x) - f(3x) - f(4x) ... f(tx) (tx <= 10000, k = Cnt[x])
这样枚举每个 x ,然后枚举每个 x 的倍数,复杂度用调和级数计算,约为 O(n logn)。
#include<iostream>#include<cstdio>#include<cstring>using namespace std;typedef long long LL;const int maxn=100010;const int MOD=10007;int a[maxn];int N;LL F[maxn];int cnt[maxn];int main(){ while(scanf("%d",&N)!=EOF) { int maxv=0; for(int i=1;i<=N;i++) scanf("%d",&a[i]),maxv=max(maxv,a[i]); memset(cnt,0,sizeof(cnt)); for(int i=1;i<=N;i++) { for(int j=1;j*j<=a[i];j++) { if(a[i]%j==0) { cnt[j]++; if(j*j!=a[i])cnt[a[i]/j]++; } } } LL ans=0; for(int i=maxv;i>0;i--) { F[i]=(LL)cnt[i]*cnt[i]%MOD; for(int j=i*2;j<=maxv;j+=i) { F[i]=(F[i]-F[j]+MOD)%MOD; } LL tmp=i*(i-1)%MOD; ans=(ans+tmp*F[i])%MOD; } cout<<ans<<endl; } return 0;}
0 0
- 数学(hdu5212)
- HDU5212 Code
- hdu5212 Code 莫队算法
- hdu5212 Code 莫比乌斯
- 莫比乌斯反演-HDU5212
- Summer Training day4 Code hdu5212 莫比乌斯反演
- 使用数学运算(数学对象)
- 数学常数(Wikipedia)
- 数学分支(整理)
- 转(acm数学)
- POJ数学(ZZ)
- 计算数学(转)
- ACM数学(转)
- Feynman(数学)
- ACMer(数学,有意思)
- Equations(数学,水)
- HDOJ2045(组合数学)
- hdu 1061(数学)
- Not a host:port pair: �
- 关于Android5.0当中的Service启动问题--- Service Intent must be explicit: Intent
- flash as3 timer 30秒后重新播放
- oracle执行计划中的cost,time与实际执行时间
- 在Ubuntu(Debian)上安装最新版Git
- 数学(hdu5212)
- 中国古人记录的奇妙客星
- ABAP字符串控制函数
- 怎样将PDF转成普通的Word文档格式
- Nature 自然
- Android性能优化案例研究(上)
- Java获取系统信息
- 解决Android TextView使用Html多个空格只显示一个空格
- 静态函数,instance