您的位置:首页 > 其它

hdu 1695 GCD(容斥原理+欧拉phi函数)

2014-10-31 22:05 113 查看
GCD

Problem Description

Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. GCD(x, y) means the greatest common divisor of x and y. Since the number of choices may be very large, you're only required to output the total number of different number
pairs.

Please notice that, (x=5, y=7) and (x=7, y=5) are considered to be the same.

Yoiu can assume that a = c = 1 in all test cases.



Input

The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 3,000 cases.

Each case contains five integers: a, b, c, d, k, 0 < a <= b <= 100,000, 0 < c <= d <= 100,000, 0 <= k <= 100,000, as described above.



Output

For each test case, print the number of choices. Use the format in the example.



Sample Input

2
1 3 1 5 1
1 11014 1 14409 9




Sample Output

Case 1: 9
Case 2: 736427

HintFor the first sample input, all the 9 pairs of numbers are (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5).




Source

2008 “Sunline Cup” National Invitational
Contest

题意:

给你5的数a,b,c,d,k ,其中a和c默认为1,求[a,b]和[c,d]两个区间有多少对数(x,y)使得gcd(x,y)==k,其中x属于[a,b],y属于[c,d]。

思路:

这个问题可以转换为x,y互质问题,因为gcd(x,y)==k,则有gcd(x/k,y/k)==1,x>k,y>k;

这样就是求[1,b/k]和[1,d/k]互质的对数了。

假设d>=b,则区间[1,d]可分为[1,b]U[b+1,d],两个区间分别对[1,b]求符合条件的对数。

[1,b]和[1,b]可以用欧拉函数求,[1,b]和[b+1,d]用容斥原理求。

My Code:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>

typedef long long ll;

using namespace std;

ll phi[100010];
ll ans;

ll solve(ll a,ll n)
{
    vector<ll>vet;
    for(ll i=2; i*i<=n; i++)
    {
        if(n%i==0)
        {
            vet.push_back(i);
            while(n%i==0)
            {
                n/=i;
            }
        }
        if(n==1)
            break;
    }
    if(n!=1)
        vet.push_back(n);
    ll sum=0;
    for(ll i=1; i<(1<<vet.size()); i++)
    {
        ll sets=0;
        ll mult=1;
        for(ll ii=0; ii<vet.size(); ii++)
        {
            if(i&(1<<ii))
            {
                sets++;
                mult*=vet[ii];
            }
        }
        sum+=sets%2?(a/mult):(-a/mult);
    }
    return a-sum;
}

void phi_table(ll n,ll *phi)///欧拉函数值表
{
    for(ll i=2; i<=n; i++)
        phi[i]=0;
    phi[1]=1;
    for(ll i=2; i<=n; i++)
    {
        if(!phi[i])
        {
            for(ll j=i; j<=n; j+=i)
            {
                if(!phi[j])
                    phi[j]=j;
                phi[j]=phi[j]/i*(i-1);
            }
        }
    }
}

int main()
{
    ll a,b,c,d,k;
    phi_table(100010,phi);
    for(ll i=2; i<=100000; i++)
        phi[i]+=phi[i-1];///前缀和
    int t;
    while(cin>>t)
    {
        int ca=1;
        while(t--)
        {
            cin>>a>>b>>c>>d>>k;
            if(k==0 || k > b || k > d)
            {
                printf("Case %d: 0\n",ca++);
                continue;
            }
            b/=k,d/=k;
            if(b>d)
                swap(b,d);
            ll tem=0;
            for(ll i=b+1; i<=d; i++)
                tem+=solve(b,i);
            printf("Case %d: %I64d\n",ca++,phi[b]+tem);
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: