hdu 5212 code 容斥~或者莫比乌斯

Code

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 489    Accepted Submission(s): 191


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;

}
 

Input
There are Multiple Cases.(At MOST  10 )

For each case:

The first line contains an integer  N(1N10000) .

The next line contains  N  integers  a1,a2,...,aN(1ai10000) .
 

Output
For each case:

Print an integer,denoting what the function returns.
 

Sample Input
  
  
5 1 3 4 2 4
 

Sample Output
  
  
64
Hint
gcd(x,y) means the greatest common divisor of x and y.
 

Source

这个博客写的很好啦。我就不写了

http://blog.csdn.net/lyhvoyage/article/details/45306211


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;

int num[10001];
long long f[10001];
int main(){
    int n,u;
    while(scanf("%d",&n)!=EOF){
        memset(num,0,sizeof(num));
        for(int i = 0;i < n; i++){
            scanf("%d",&u);
            num[u]++;
        }
        long long ans = 0, mod = 10007;
        memset(f,0,sizeof(f));

        for(int i = 10000; i >= 1; i--){
            long long r = 0;
            for(int j = i; j <= 10000; j += i){
                r += f[j];
                f[i] += num[j];
            }
            f[i] = (f[i]*f[i] - r) % mod;
            ans += f[i]*i*(i-1) % mod;
        }
        cout<<ans % mod<<endl;
    }
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值