hdu1695 GCD2 容斥原理 求x属于[1,b]与y属于[1,d],gcd(x,y)=k的对数。(5,7)与(7,5)看作同一对。

Posted 天道酬勤007

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu1695 GCD2 容斥原理 求x属于[1,b]与y属于[1,d],gcd(x,y)=k的对数。(5,7)与(7,5)看作同一对。相关的知识,希望对你有一定的参考价值。

GCD
Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10992    Accepted Submission(s): 4157


Problem Description
Given 5 integers: a, b, c, d, k, youre 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, youre 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).

/**
题目:hdu1695 GCD2
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695
题意:求x属于[1,b]与y属于[1,d],gcd(x,y)=k的对数。(5,7)与(7,5)看作同一对。
思路:
gcd(x,y)=k => gcd(x/k,y/k) = 1;

则求x/k与y/k互质对数。

即求:[1,b/k]与[1,d/k]之间互质的对数

设x属于[1,b/k], y属于[1,d/k];
枚举x,求x与y互质的对数。所以要预处理所有x的质因子。然后容斥处理。由于(x,y)=>(5,7),(7,5)是同一组。
所以:答案为ans += (d/k) - x在d/k中不互质的数 - (x-1);
*/
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#define LL long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5+10;
vector<int> prime[maxn];
int flag[maxn];
void init()
{
    memset(flag, 0, sizeof flag);
    for(ll i = 2; i < maxn; i++){
        if(flag[i]==0){
            prime[i].push_back(i);
            for(ll j = 2*i; j < maxn; j+=i){
                prime[j].push_back(i);
                flag[j] = 1;
            }
        }
    }
}
ll rc(int pos,int n)
{
    ll sum = 0;
    ll mult, ones;
    ll len = prime[pos].size();
    ll m = 1<<len;
    for(int i = 1; i < m; i++){
        ones = 0;
        mult = 1;
        for(int j = 0; j < len; j++){
            if(i&(1<<j)){
                ones++;
                mult = mult*prime[pos][j];
                if(mult>n) break;
            }
        }
        if(ones%2==0){
            sum -= n/mult-(pos-1)/mult;
        }else
        {
            sum += n/mult-(pos-1)/mult;
        }
    }
    return n-(pos-1)-sum;
}
int main()
{
    init();
    int T;
    int cas = 1;
    int a, b, c, d, k;
    cin>>T;
    while(T--)
    {
        scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
        if(k==0){
            printf("Case %d: 0\n",cas++);continue;
        }
        if(b>d) swap(b,d);///for b<=d;
        b = b/k;
        d = d/k;
        ll ans = 0;
        if(b>=1){
            ans += d;
        }
        for(int i = 2; i <= b; i++){
            ans += rc(i,d);
        }
        printf("Case %d: %lld\n", cas++,ans);
    }
    return 0;
}

 

以上是关于hdu1695 GCD2 容斥原理 求x属于[1,b]与y属于[1,d],gcd(x,y)=k的对数。(5,7)与(7,5)看作同一对。的主要内容,如果未能解决你的问题,请参考以下文章

GCD HDU - 1695 (容斥原理)

hdu 1695 GCD(欧拉函数+容斥原理)

HDU1695 GCD (欧拉函数+容斥原理)

hdu (欧拉函数+容斥原理) GCD

hdu-1695 GCD---容斥定理

D - GCD HDU - 1695 -模板-莫比乌斯容斥