hdu 5212(莫比乌斯反演)
来源:互联网 发布:用c语言怎么打出图形 编辑:程序博客网 时间:2024/05/19 22:03
Code
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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.
解题思路:这道题关键是找到相同的gcd(x,y)的对数。可以用莫比乌斯反演。
参考博客:http://blog.csdn.net/Yukizzz/article/details/51541973
#include<iostream>#include<cstdio>#include<cstring>using namespace std;typedef long long LL;const int maxn = 10005;const int mod = 10007;bool check[maxn];int mu[maxn],prime[maxn];int n,m,a[maxn],cnt[maxn];LL F[maxn];void moblus(){int tot = 0;memset(check,false,sizeof(check));mu[1] = 1;for(int i = 2; i <= maxn; i++){if(!check[i]){prime[tot++] = i;mu[i] = -1;}for(int j = 0; j < tot; j++){if(i * prime[j] > maxn) break;check[i * prime[j]] = true;if(i % prime[j] == 0){mu[i * prime[j]] = 0;break;}else mu[i * prime[j]] = -mu[i];}}}int main(){moblus();while(scanf("%d",&n)!=EOF){memset(cnt,0,sizeof(cnt));m = 0;for(int i = 1; i <= n; i++){scanf("%d",&a[i]);cnt[a[i]]++;m = max(m,a[i]);}for(int i = 1; i <= m; i++){F[i] = 0;for(int j = i; j <= m; j += i)F[i] += cnt[j];F[i] = F[i] * F[i];}LL tmp,ans = 0;for(int i = 1; i <= m; i++) //枚举最大公约数{tmp = 0;for(int j = i; j <= m; j += i)tmp = (tmp + mu[j / i] * F[j] % mod) % mod;ans = (ans + tmp * i % mod * (i - 1) % mod) % mod;}printf("%lld\n",ans);}return 0;}
0 0
- hdu 5212(莫比乌斯反演)
- hdu 1695 莫比乌斯反演
- hdu 5072 莫比乌斯反演
- hdu 1695 莫比乌斯反演
- hdu 1695 莫比乌斯反演
- HDU 5656 (莫比乌斯反演)
- HDU 4675 (莫比乌斯反演)
- hdu 1695 莫比乌斯反演
- HDU 1695 莫比乌斯反演
- HDU 6053 莫比乌斯反演
- hdu 6053-莫比乌斯反演
- HDU 6134 莫比乌斯反演
- HDU 6134 莫比乌斯反演
- hdu 6134(莫比乌斯反演)
- HDU 6053 莫比乌斯反演
- hdu 1695 莫比乌斯反演
- HDU 5212 Code【莫比乌斯反演】
- HDU 5212 code (莫比乌斯反演)
- 整理uc/os的46个函数
- java基础之----匿名内部类
- HDU 1071 The area (定积分求面积)
- HDU 4507 吉哥系列故事——恨7不成妻
- 装配bean
- hdu 5212(莫比乌斯反演)
- leetcode---Remove Duplicates from Sorted Array II
- IPC之消息队列
- Activity启动模式
- javascript之Date
- 网络协议及网络软件框架设计
- linux下查看文件编码及修改编码
- iOS流媒体开发之一:总结系统提供的接口
- Spring+MyBatis