hdu 1695(欧拉函数 容斥定理)

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.

InputThe 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.
OutputFor each test case, print the number of choices. Use the format in the example.

 

 


 

 

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). 

 

 

Sample Input

 

 

 

 

//分析:题目是要求a<=x<=b,c<=y<=d(其中a,c题目中已经说了必为1),中有多少对(x,y)(无序的)满足GCD(x,y)=k,那么可以转化成求(x,y)分别在区间1<=x<=b/k,1<=y<=d/k中有多少对满足GCD(x,y)=1; 转换b/=k,d/=k,之后,假设b<=d,那么我们可以分两步来求值:

1、求[1,b]与[1,b]之间有多少对数互质,显然就是phi[1]+phi[2]+...phi[b]即可,用线性求欧拉函数即可!

2\求[1,b]与[b+1,d]之间有多少对数互质,使用容斥定理

两步求值之和即为题意所求!

 

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=100000;
int q[N+5];
long long p[N+5];
int f(int a,int n)
{
    int w[110],l=0;
    for(int i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            w[++l]=i;
            while(n%i==0)n/=i;
        }
        if(n==1)break;
    }
    if(n!=1)w[++l]=n;
    int sum=0;
    for(int i=1;i< (1<<l);i++)
    {
        int flat=0,s=1;
        for(int j=0;j<l;j++)

            if(i&(1<<j))
            {flat=!flat;
             s*=w[j+1];
            }
            s=a/s;
        if(flat)
            sum+=s;
        else
            sum-=s;
    }
    return (a-sum);
}
int main()
{
    memset(q,0,sizeof(q));
    q[0]=q[1]=1;
    int n=0;
    for(int i=2;i<=N;i++)
    {
        if(!q[i])
        {

            for(int j=i;j<=N;j+=i)
            {
                if(!q[j])q[j]=j;
                q[j]=q[j]/i*(i-1);
            }
        }
    }
    p[1]=1;
    for(int i=2;i<=N;i++)
    {
        p[i]=p[i-1]+q[i];
    }
    int m,a,b,c,d,k;
    int t,kase=0;
    scanf("%d",&t);
    while(t--)
    {
        long long ans;
        scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
        if(k==0||b<k||d<k)
            ans=0;
        else
        {
         b/=k;d/=k;
          if(b>d)swap(b,d);
             ans=0;
            for(int i=b+1;i<=d;i++)
                {int k=f(b,i);
                //cout<<i<<"  * "<<k<<endl;
                 ans+=k;
                }
            ans+=p[b];
        }
           printf("Case %d: %lld\n",++kase,ans);
    }
    return 0;
}

转载于:https://www.cnblogs.com/552059511wz/p/6741424.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值