GCD HDU - 1695 (欧拉 + 容斥)

Posted wtsruvf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GCD HDU - 1695 (欧拉 + 容斥)相关的知识,希望对你有一定的参考价值。

GCD

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


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

 

Source
 
就是求 [1, b / k]  和 [1, d / k] 互质数的对数
 先用欧拉函数求出 相同区间的互质的对数 多出来的 用容斥去求
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d
", a)
#define plld(a) printf("%lld
", a)
#define pc(a) printf("%c
", a)
#define ps(a) printf("%s
", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 110000, INF = 0x7fffffff;
int ans;
LL tot[maxn + 10];
int prime[maxn+10], phi[maxn+10];
bool vis[maxn+10];
void getphi()
{
    ans = 0;
    phi[1] = 1;
    for(int i=2; i<=maxn; i++)
    {
        if(!vis[i])
        {
            prime[++ans] = i;
            phi[i] = i - 1;
        }
        for(int j=1; j<=ans; j++)
        {
            if(i * prime[j] > maxn) break;
            vis[i * prime[j]] = 1;
            if(i % prime[j] == 0)
            {

                phi[i * prime[j]] = phi[i] * prime[j]; break;
            }
            else
                phi[i * prime[j]] = phi[i] * (prime[j] - 1);
        }
    }
}

int get_cnt(int n, int m)
{
    int ans = 0;
    for(int i = 2; i * i <= n; i++)
    {
        if(n % i) continue;
        while(n % i == 0) n /= i;
        prime[ans++] = i;
    }
    if(n != 1) prime[ans++] = n;
    int res = 0;
    for(int i = 1; i < (1 << ans); i++)
    {
        int tmp = 1, cnt2 = 0;
        for(int j = 0; j < ans; j++)
        {
            if(((i >> j) & 1) == 0) continue;
            tmp *= prime[j];
            cnt2++;
        }
        if(cnt2 & 1) res += m / tmp;
        else res -= m / tmp;
    }
    return m - res;
}

int main()
{
    getphi();
    int a, b, c, d, k;
    for(int i = 1; i < maxn; i++)
    {
        tot[i] = tot[i - 1] + phi[i];

    }
    int T, kase = 0;
    cin >> T;
    while(T--)
    {
        scanf("%d%d%d%d%d", &a, &b, &c, &d, &k);
        if(k == 0)
        {
            printf("Case %d: 0
", ++kase);
            continue;
        }
        int n = b / k, m = d / k;
        LL sum = tot[n > m ? m : n];
       // cout << sum << endl;
        if(m > n) swap(n, m);
        for(int i =m + 1; i <= n; i++)
        {
            sum += get_cnt(i, m);
        }
        printf("Case %d: %lld
", ++kase, sum);
    }

    return 0;
}

 

GCD

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


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

 

Source
















以上是关于GCD HDU - 1695 (欧拉 + 容斥)的主要内容,如果未能解决你的问题,请参考以下文章

hdu 1695 GCD 欧拉函数 + 容斥

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

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

GCD HDU - 1695 (容斥原理)

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

HDU 1695 GCD(容斥定理)