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.

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

题解:因为 a和c都是从1开始的 所以我们两区间都除k 找互质的就可以了,因为区间会重合,所以,区间重合部分用欧拉函数求,另外一部分容斥一下就可以,数据最大1e5 2*3*5*7*11*13=30030 最多6个 复杂度小于3e6

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
#define lowbit(x) x&(-x)
ll a,b,c,d,k;
ll prime[101000],ok[101000],len,p[10],eul[100100];
void init()
{
    for(int i=2;i<=100000;i++)
    {
        if(ok[i]==0)prime[len++]=i;
        for(int j=0;j<len&&(long long)prime[j]*i<=100000;j++)
        {
            ok[prime[j]*i]=1;
            if(i%prime[j]==0)break;
        }
    }
    eul[1]=1;
    for(int i=0;i<len;i++)
    {
        for(int j=prime[i];j<=100000;j+=prime[i])
        {
            if(eul[j]==0) eul[j]=j;
            eul[j]=eul[j]*(prime[i]-1)/prime[i];
        }
    }
}
int main()
{
    int T,nn=1;
    init();
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld%lld%lld%lld%lld",&a,&b,&c,&d,&k);
        printf("Case %d: ",nn++);
        if(k==0) printf("0\n");
        else
        {
            a=(a)/k;
            b=(b)/k;
            c=(c)/k;
            d=(d)/k;
            ll ans=0;
            ll s1;
            if(b<d) swap(a,c),swap(b,d);
            for(int i=d+1;i<=b;i++)
            {
                int tmp=i;
                int cnt=0;
                for(int j=0;j<len&&(long long)prime[j]*prime[j]<=tmp;j++)
                {
                    if(tmp%prime[j]==0)
                    {
                        p[cnt++]=prime[j];
                        while(tmp%prime[j]==0) tmp/=prime[j];
                    }
                }
                if(tmp!=1) p[cnt++]=tmp;
                s1=0;
                for(int j=1;j<(1<<cnt);j++)
                {
                    tmp=0;
                    ll mm=j;
                    ll res=1;
                    while(mm)
                    {
                        tmp++;
                        mm-=lowbit(mm);
                    }
                    for(int k=0;k<cnt;k++)
                    {
                        if(j&(1<<k))
                            res*=p[k];
                    }
                    if(tmp&1) s1+=d/res;
                    else s1-=d/res;
                }
                ans+=d-s1;
            }
            for(int i=1;i<=d;i++)
                ans+=eul[i];
            printf("%lld\n",ans);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值