【HDU】1695 GCD(容斥原理+线形欧拉函数)

                         Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                          Total Submission(s): 15373    Accepted Submission(s): 5889


 

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

Hint

For 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

 

Recommend

wangye   |   We have carefully selected several similar problems for you:  1689 1690 1693 1691 1698 

题目大意:

要求 a<=x<=b,c<=y<=d中,有多少个满足gcd(x ,y)=k,题目中给出a,c为1,

那么就是求在【1,b/k】和【1,d/k】 中 xy互质的对数(无序),

在b  d都做完自身除k后,假设b<d:

我们可以,先求出【1,b】上互质的数目,即线形求欧拉函数,然后相加就可以了,

其次,就是求【1,b】上的与【b+1,d】上互质的个数,可以将第二个数据范围中的数,拿出来,做容斥,求出不互质的个数,

最后用总的减去即可,

将两步的结果相加就是这个问题的结果了。。。。

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#define maxn 100005
#define LL long long
using namespace std;

int phi[maxn],cnt[maxn],vis[maxn];
void Init()
{
	int i,j;
	memset(vis,0,sizeof(vis));
	vis[0]=vis[1]=1;
	for(i=1;i<maxn;i++)
		phi[i]=i;
	for(i=2;i<maxn;i++)
	{
		if(!vis[i])
		{
			phi[i]=i-1;
			for(j=i*2;j<maxn;j+=i)
			{
				vis[j]=1;
				phi[j]=phi[j]/i*(i-1);
			}
		}
	}
}

int RC(int n,int m)
{
    int t=0;
    for(int i=2;i*i<=n;i++)
    {
        if(n&&n%i==0)
        {
            cnt[t++]=i;
            while(n&&n%i==0)
                n/=i;
        }
    }
    if(n>1) cnt[t++]=n;    //分解质因数

    int tmp,ans=0,flag;
    for(int i=1;i<(1<<t);i++)
    {
        tmp=1;flag=0;
        for(int j=0;j<t;j++)
        {
            if(i&(1<<j))
            {
                flag++;tmp=tmp*cnt[j];
            }
        }
        if(flag&1)
            ans+=m/tmp;
        else
            ans-=m/tmp;
    }
    return ans;
}


int main()
{
	Init();
	int T,t=0,a,b,c,d,k,i,j;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
		if(k==0)
		{
			printf("Case %d: 0\n",++t);
			continue;
		}
		b/=k,d/=k;
		if(b>d)
			swap(b,d);
		LL ans=0,tmp=(LL)b*(d-b);  
		for(i=1;i<=b;i++)
			ans+=phi[i];
		for(i=b+1;i<=d;i++)
			tmp-=RC(i,b);
		printf("Case %d: %I64d\n",++t,ans+tmp);
	}
	return 0;

}

猜你喜欢

转载自blog.csdn.net/wentong_Xu/article/details/81661618