GCD
Time limit3000 ms
Memory limit32768 kB
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).
题意
找到gcd(x,y) == k 的(x,y)的对数,其中a <= x <= b , c <= y <= d
注意(7,5),(5,7)被认为是一样的
题解
莫比乌斯反演基础。。
代码
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <cmath>
using namespace std;
#define me(x,y) memset(x,y,sizeof x)
#define MIN(x,y) x < y ? x : y
#define MAX(x,y) x > y ? x : y
typedef long long ll;
const int maxn = 1e5+10;
const double INF = 0x3f3f3f3f;
const int MOD = 1e6;
const double eps = 1e-06;
bool check[maxn];
int prime[maxn];
int mu[maxn];
void Moblus(){ //线性筛
me(check,false);
mu[1] = 1;
int tot = 0;
for(int i = 2; i <= maxn-10; i++){
if(!check[i]){
prime[tot++] = i;
mu[i] = -1;
}
for(int j = 0; j < tot; j++){
if(i*prime[j] > maxn-10) break;
check[i*prime[j]] = true;
if(i%prime[j] == 0){
mu[i*prime[j]] = 0;
break;
}
else{
mu[i*prime[j]] = -mu[i];
}
}
}
}
int main(){
//freopen("E:/TanJX/in.txt","r",stdin);
//freopen("E:/TanJX/out.txt","w",stdout);
Moblus();
int t,a,b,c,d,k,cas = 1;
cin>>t;
while(t--){
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
if(!k){
printf("Case %d: 0\n",cas++);
continue;
}
b /= k;
d /= k;
if(b > d) swap(b,d);
ll ans = 0,rep = 0;
for(int i = 1; i <= b; i++){
ans += (ll) mu[i]*(b/i)*(d/i);
rep += (ll) mu[i]*(b/i)*(b/i); //去重
}
printf("Case %d: %lld\n",cas++,ans-rep/2);
}
return 0;
}
/*
*/