HDU 1695 GCD(容斥)

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]中选出一个x,在区间[1,d]中选出一个y,对于x和y满足的条件是gcd(x,y)=k。

那么这道题就可以简化为求在区间[1,b/k]中选出一个x,在区间[1,d/k]中选出一个y,满足gcd(x,y)=1

因为d始终是大于b的,所以在计算的时候会有重复计算的部分,所以要分两部分计算。

首先是计算[1,b/k]部分:

直接使用欧拉筛,欧拉函数的作用是计算当前数前面有多少个数与它是互素的,如果说两个数互素不难想象gcd=1,计算一个数的欧拉函数不能满足条件,我们要使用筛法欧拉函数遍历区间[1,b/k],接下来进行求前缀和操作。

接下来是计算[b/k+1,d/k]部分:

这部分直接使用求指定区间内与n互素的数的个数的方法,同样是遍历整个区间,最后得到答案。

 

#include <string.h>
#include <stdio.h>
#include <math.h>
#include <queue>
#include<iostream>
#include<set>
#include<map>
#include<vector>
#include <ctime>
#include <stack>//sTL stack
#include <algorithm>
#include<string>//这个是string的头文件
using namespace std;
//const double PI = acos(-1.0);
#define M(a,x) memset(a,x,sizeof(a))
#define AU(i,x,y) for(llt i=x;i<=y;i++)
const int maxn = 1e5+10;
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;//无穷大
typedef long long ll;//以后出现的ll为long long 类型
int Ss=8;
ll  eulerr[maxn];

int solve (int n, int r)
{
    vector<int> p;
    for (int i=2; i*i<=n; ++i)
        if (n % i == 0)
        {
            p.push_back (i);
            while (n % i == 0)
                n /= i;
        }
    if (n > 1)
        p.push_back (n);
    int sum = 0;
    for (int msk=1; msk<(1<<p.size()); ++msk)
    {
        int mult = 1,
            bits = 0;
        for (int i=0; i<(int)p.size(); ++i)
            if (msk & (1<<i))
            {
                ++bits;
                mult *= p[i];
            }
        int cur = r / mult;
        if (bits % 2 == 1)
            sum += cur;
        else
            sum -= cur;
    }
    return r - sum;
}

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

int main()
{
    geteulerr();
    int T;
    int a,b,c,d,k,number=1;
    cin>>T;
    while(T--)
    {
        scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
        if(k==0||k>b||k>d)
        {
            printf("Case %d: 0\n",number++);
            continue;
        }
        int q=d/k;
        int p=b/k;
        int x=min(q,p);
        int y=max(q,p);
        ll sum=eulerr[x];
        for(int i=x+1; i<=y; ++i)
        {
            sum+=solve(i,x);
        }
        printf("Case %d: %lld\n",number++,sum);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值