HDU 1695 GCD(容斥+组合)

时间:2021-04-12 05:18:25
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).
 
分析:分段来求:转化为[1,b/k]和[1,d/k]中分别选出x,y,使gcd(x,y)=1,即互素。
1:首先保证b<d,我们先求出[1,b]即[1,b]和[1,d]公共部分的组合数。这里欧拉phi值相加就可以了。
2:依次遍历[b+1,d]中的数x,求[1,b]中和x的互素的对数,容斥解决。1+2的和即答案。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int mod = 1000000007;
const int maxn=100000+10;
int t,a,b,c,d,k;
int phi[maxn];
vector<int>v;
void phi_init()
{
    for(int i=2;i<=100000;i++)  phi[i]=0;
    phi[1]=1;
    for(int i=2;i<=100000;i++)
    {
        if(!phi[i])
        {
            for(int j=i;j<=100000;j+=i)
            {
                if(!phi[j]) phi[j]=j;
                phi[j]=phi[j]/i*(i-1);
            }
        }
    }
}
//求[1,r]与n的不互素的解
LL solve(LL r,LL n)
{
    vector<int>v;
    for(int i=2;i*i<=n;i++)
    {
        if(n&&n%i==0)
        {
            v.push_back(i);
            while(n&&n%i==0)
                n/=i;
        }
    }
    if(n>1)  v.push_back(n);
    LL sum=0;
    for(int t=1;t<(1<<v.size());t++)
    {
        LL mul=1,bits=0;
        for(int i=0;i<(int)v.size();i++)
        {
            if(t&(1<<i))
            {
                ++bits;
                mul*=v[i];
            }
        }
        if(bits&1)  sum+=r/mul;
        else   sum-=r/mul;
    }
    return r-sum;
}
int main()
{
   int cas=1;
   phi_init();
   scanf("%d",&t);
   while(t--)
   {
       LL ans=0;
       scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
       if(k==0)
       {
           printf("Case %d: 0\n",cas++);
           continue;
       }
       b/=k;d/=k;
       if(b>d)  swap(b,d);
       REPF(i,1,b)  ans+=phi[i];
       LL s=0;
       for(int i=b+1;i<=d;i++)
          s+=solve((LL)b,(LL)i);
       printf("Case %d: %I64d\n",cas++,s+ans);
   }
   return 0;
}