题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

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

Problem solving report:

Description: 问你gcd(i,j)=k有多少对,其中a<=i<=b,c<=j<=d,其中a和c恒等于1。
Problem solving: 首先缩小范围,另b=b/k,d=d/k
转化成有多少对gcd(x,y)==1。
假设b<d.
那么将d化为[1,b],[b+1,d]两部分
先求 [1,b]这一部分,gcd(x,y)==1,1<=x<=b, 1<=y<=b
只要对每个数求其欧拉函数,然后累加就可以了。
再求 [b+1,d]这一部分,gcd(x,y)==1,1<=x<=b, b+1<=y<=d
要求x,y互质,对于每一个y,如果x能够被y的一个素因子整除,那么gcd(x,y)肯定不等于1。
所以我们先求[1,b]中有多少个数和y不互素,也就是能被y的素因子整除,然后用b减去这个数就是我们
所要求的满足gcd(x,y)==1的数,用到了容斥原理,加上能被1个素因子整除的,减去两个的,加上三个的,减去四个的.....

还有一种方法就是利用莫比乌斯反演,这个就是个板子题。 

Accepted Code:

//欧拉+容斥
#include <bits/stdc++.h>
using namespace std;
const int MAXM = 400;
const int MAXN = 100000;
int cnt, Q[MAXN + 5], Eul[MAXN + 5], fact[MAXM];
void Euler() {
    for (int i = 1; i <= MAXN; i++)
        Eul[i] = i;
    for (int i = 2; i <= MAXN; i++)
        if (!(Eul[i] - i))
            for (int j = i; j <= MAXN; j += i)
                Eul[j] = Eul[j] / i * (i - 1);
}
void Divid(int n) {
    cnt = 0;
    for (int i = 2; i <= n / i; i++) {
        if (!(n % i)) {
            fact[cnt++] = i;
            while (!(n % i))
                n /= i;
        }
    }
    if (n != 1)
        fact[cnt++] = n;
}
long long slove(int n, int m) {
    Divid(m);
    int top = 0;
    long long ans = 0;
    Q[top++] = -1;
    for (int i = 0; i < cnt; i++) {
        int k = top;
        for (int j = 0; j < k; j++)
            Q[top++] = Q[j] * fact[i] * -1;
    }
    for (int i = 1; i < top; i++)
        ans += n / Q[i];
    return ans;
}
int main() {
    Euler();
    int  t, a, b, c, d, k, kase = 0;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d%d%d%d", &a, &b, &c, &d, &k);
        if (!k) {
            printf("Case %d: 0\n", ++kase);
            continue;
        }
        if (b > d)
            swap(b, d);
        b /= k, d /= k;
        long long ans = 0;
        for (int i = 1; i <= b; i++)
            ans += Eul[i];
        for (int i = b + 1; i <= d; i++)
            ans += (b - slove(b, i));
        printf("Case %d: %lld\n", ++kase, ans);
    }
    return 0;
}
//莫比乌斯反演
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5;
bool isp[MAXN + 5];
int pre[MAXN], miu[MAXN + 5];
void Mobius() {
    miu[1] = 1;
    int cnt = 0;
    for (int i = 2; i < MAXN; i++) {
        if (!isp[i]) {
            pre[cnt++] = i;
            miu[i] = -1;
        }
        for (int j = 0; j < cnt && i * pre[j] < MAXN; j++) {
            isp[i * pre[j]] = true;
            if (i % pre[j])
                miu[i * pre[j]] = -miu[i];
            else {
                miu[i * pre[j]] = 0;
                break;
            }
        }
    }
}
int main(int argc, char const *argv[]) {
    Mobius();
    int a, b, c, d, k, t, kase = 0;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d%d%d%d", &a, &b, &c, &d, &k);
        if (!k) {
            printf("Case %d: 0\n", ++kase);
            continue;
        }
        if (b > d)
            swap(b, d);
        b /= k, d /= k;
        long long ans1 = 0, ans2 = 0;
        for (int i = 1; i <= b; i++)
            ans1 += 1ll * miu[i] * (b / i) * (d / i);
        for (int i = 1; i <= b; i++)
            ans2 += 1ll * miu[i] * (b / i) * (b / i);
        printf("Case %d: %lld\n", ++kase, ans1 - (ans2 >> 1));
    }
    return 0;
}