HDU1695 GCD(容斥原理)

GCD

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


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


题意:给出[l,r],[L,R]区间,问有多少个排列的最大公约数为k(1,2和2,1这种算两个)


要算最大公约数为k,那么所有没有k因数的数就可以不用考虑了,对区间/k映射到一个更小的区间,此时区间内的数x都表示k的x倍

此时只需要考虑两个区间内互质的数的组合数就行了,因为互质即最大公因子为1,gcd即为k(因为是/k映射后的区间)

然后考虑关于排列的问题,只有两个区间相交的区间才会出现正反计算两次的情况,所以,对不相交的区间,直接容斥原理计算出每一个数在另一个区间的互质个数,加起来即可

对于相交的区间,发现(1,1)是个特例,因为只有1与自己本身互质,而其他所有的互质对都可以正着和反着出现两次,于是,容斥原理求出区间所有的互质对数sum,(sum-1)*2+1即为相交区间的所有排列数

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<vector>
#include<algorithm>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
vector<ll>v;
ll a[100005];
ll cont(ll n)
{
    ll g=0,sum=n;
    a[++g]=1;
    for(ll i=0; i<v.size(); i++)
    {
        ll t=g;
        for(ll j=1; j<=g; j++)
        {
            a[++t]=-1*a[j]*v[i];
            sum+=n/a[t];
        }
        g=t;
    }
    return sum;
}
int main()
{
    ll n,m,t,q=0,k,l,L,r,R;
    scanf("%lld",&t);
    while(t--)
    {
        ll ans=0;
        scanf("%lld%lld%lld%lld%lld",&l,&r,&L,&R,&k);
        if(k==0)
        {
            printf("Case %lld: 0\n",++q);
            continue;
        }
        ll sum=r+R;
        r=min(r,R);
        R=sum-r;
        l=r/k,L=R/k;
        for(ll tt=1; tt<=l; tt++)
        {
            ll cnt=tt;
            v.clear();
            for(ll i=2; i*i<=cnt; i++)
            {
                if(cnt%i==0)
                {
                    v.push_back(i);
                    while(cnt%i==0)cnt/=i;
                }
            }
            if(cnt>1)v.push_back(cnt);
            ans+=cont(l);
        }
        ans=(ans+1)/2;
        for(ll tt=l+1; tt<=L; tt++)
        {
            ll cnt=tt;
            v.clear();
            for(ll i=2; i*i<=cnt; i++)
            {
                if(cnt%i==0)
                {
                    v.push_back(i);
                    while(cnt%i==0)cnt/=i;
                }
            }
            if(cnt>1)v.push_back(cnt);
            ans+=cont(l);
        }
        printf("Case %lld: %lld\n",++q,ans);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值