码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5212 反向容斥或者莫比

时间:2017-08-23 20:49:03      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:algo   amp   .com   ace   span   题解   code   for   nbsp   

http://acm.hdu.edu.cn/showproblem.php?pid=5212

题意:忽略。。

题解:把题目转化为求每个gcd的贡献。(http://www.cnblogs.com/z1141000271/p/7419717.html 和这题类似 反向容斥)这里先用容斥写了,mobious的之后再说吧23333。

然后比较想说的是这个调和级数的复杂度 nlog(n)

ac代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define mt(a) memset(a,0,sizeof(a))
using namespace std;
const int mod=10007;
typedef long long ll;
ll a[10001];
ll num[10001];
ll cnt[10001];
ll get(ll n)
{
    int f=1;
    f=n*n;
    f%=mod;
    return f;
}
int main()
{
    ll n;
    while(~scanf("%lld",&n))
    {
        ll mx=-100;
        mt(a);
        mt(cnt);
        mt(num);
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
            num[a[i]]++;
            mx=max(mx,a[i]);
        }
        ll ans=0;
        for(ll i=mx;i>=2;i--)
        {
            int ret=num[i];
            for(int j=i*2;j<=mx;j+=i)
            {
                cnt[i]=(cnt[i]-cnt[j]+mod)%mod;
                ret+=num[j];
            }
           // cout<<ret<<endl;
            cnt[i]+=get(ret);//想清楚。
            cnt[i]+=mod;
            cnt[i]%=mod;
            ll p=i*(i-1)%mod;
            ans=(ans+(cnt[i]*p))%mod;
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

hdu 5212 反向容斥或者莫比

标签:algo   amp   .com   ace   span   题解   code   for   nbsp   

原文地址:http://www.cnblogs.com/z1141000271/p/7419896.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!