hdu 1695 GCD

           容斥原理 + 欧拉函数 或 莫比乌斯反演。莫比乌斯反演要比容斥原理快的多。。

           先说一下容斥原理的思路吧。其实容斥原理方法挺暴力的,本来一直想一次容斥就把结果算出来的,未果。。然后没办法了想到,对于每一个c < x < d, 求小于x且小于等于b的所有互质的个数(d > b),然后相加就行了。注意case组数有3000之多,所以要先把每个数质因子初始化出来,不然会超时!

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#define LL long long
#define CLR(a, b) memset(a, b, sizeof(a))

using namespace std;

const int M = 100001;

vector<int> hav[M];
LL has[M];
LL phi[M];

void get_phi()
{
    int i, j;
    phi[1] = 1;has[0] = 0;has[1] = 1;
    for(i = 2; i < M; i ++)
    {
        if(!phi[i])
        {
            for(j = i; j < M; j += i)
            {
                if(!phi[j])
                    phi[j] = j;
                phi[j] -= phi[j] / i;
                hav[j].push_back(i);
            }
        }
        has[i] = has[i-1] + phi[i];
    }
}

int dfs(int u, int c, int b, int di)
{
    int ret = 0, sz = hav[di].size();
    if(c > b) return 0;
    for(; u < sz; u ++)
    {
        ret += b / (hav[di][u] * c) - dfs(u + 1, c * hav[di][u], b, di);
    }
    return ret;
}


int main()
{
    //freopen("input.txt", "r", stdin);
    int cas = 1, t, i;
    int a, b, c, d, k, flag;LL ans;
    get_phi();
    scanf("%d", &t);
    while(t --)
    {
        scanf("%d%d%d%d%d", &a, &b, &c, &d, &k);
        flag = 0;ans = 0;
        if(k == 0)
        {
            printf("Case %d: %d\n", cas ++, ans);
            continue;
        }
        if(b > d) swap(b, d);
        b /= k; d /= k;
        ans = has[b];
        for(i = b + 1; i <= d; i ++)
        {
            ans += (b - dfs(0, 1, b, i));
        }
        printf("Case %d: %I64d\n", cas ++, ans);
    }
}

   

           隔了好几天,终于把莫比乌斯反演部分完成了,详细解释见http://wenku.baidu.com/view/fbe263d384254b35eefd34eb.html,写的很好,很容易理解。莫比乌斯反演主要是用来求互质的对数的。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#define LL long long
#define CLR(a, b) memset(a, b, sizeof(a))

using namespace std;

const int N = 100010;
const int INF = 1e9;
int mob[N], p[N];
bool isp[N];

void Mobius()
{
    int tot = 0;CLR(isp, 0);
    mob[1]= 1;
    for(int i = 2; i < N; i ++)
    {
        if(!isp[i])
        {
            p[tot ++] = i;
            mob[i] = -1;
        }
        for(int j = 0; j < tot; j ++)
        {
            if(i * p[j] >= N) break;
            isp[i * p[j]] = 1;
            if(i % p[j] == 0)
            {
                mob[i * p[j]] = 0;
                break;
            }
            else
            {
                mob[i * p[j]] = -mob[i];
            }
        }
    }
}

int main()
{
    //freopen("input.txt", "r", stdin);
    int t, a, b, c, d, k, cas = 1;LL ans;
    Mobius();
    scanf("%d", &t);
    while(t --)
    {
        scanf("%d%d%d%d%d", &a, &b, &c, &d, &k);
        if(k == 0){ printf("Case %d: 0\n",cas ++);continue; }
        b /= k;d /= k;ans = 0;
        if(b > d) swap(b, d);
        for(int i = 1;i <= b; i ++)
        {
            ans += (LL)mob[i] * (b / i) * (d / i);
        }
        LL tmp = 0;
        for(int i = 1; i <= b; i ++)
        {
            tmp += (LL)mob[i] * (b / i) * (b / i);
        }
        ans -= tmp / 2;
        printf("Case %d: %I64d\n",cas ++, ans);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值