GCD
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11247 Accepted Submission(s): 4266
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.
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.
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: 736427HintFor 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).
题意:
给出三个数a,b,找到x,y,使得x<=a,y<=b,且gcd(x,y)=k。求x,y的对数。x与y、y与x算同一对。
所有数均小于100000.
1.设a<b,否则交换a,b,然后a=a/k,b=b/k
问题即转换为求a,b内的互质整数对。
2.对于x<=a,y<=a的互质整数对,可以求i(i<=a)的欧拉函数,所有i的欧拉函数之和即为不大于a的互质整数对。
3.对于x<=a,a<y<=b的(x,y),可以对x分解质因子,利用容斥原理求出(a,b]区间内不能被x的任何质因子整除的个数。
第2步也可以用容斥原理,此时第2步和第3步即合成一步。
这题直接写的容斥,分解质因数打一个表,然后去求每个因数的容斥(这是参考了别人的代码,因为自己一直不太会写容斥,哪天钻研一下会单独开篇文聊一聊)
Code:
Status | Accepted |
---|---|
Time | 343ms |
Memory | 7048kB |
Length | 1199 |
Lang | G++ |
Submitted | 2017-07-08 19:50:20 |
Shared | |
RemoteRunId | 21030482 |
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
using namespace std;
typedef long long LL;
const int Max = 100005;
vector<int>fac[Max];
bool flg[Max];
void pre_work(){
memset(flg, 0, sizeof(flg));
for(int i = 1; i < Max; ++ i) fac[i].clear();
for(int i = 2; i < Max; i += 2) fac[i].push_back(2);
for(int i = 3; i < Max; i += 2)
if(! flg[i])
for(int j = i; j < Max; j += i)
flg[j] = 1,
fac[j].push_back(i);
}
int work(int u, int s, int w){//容斥x的质因子
int cnt = 0, v = 1, len = fac[w].size();
for(int i = 0; i < len; ++ i)
if((1 << i) & s){
++ cnt;
v *= fac[w][i];
}
int All = u / v;
if(cnt & 1) return All;
else return -All;
}
int main(){
pre_work();
int T, tot = 0;
scanf("%d", &T);
while(T --){
int a, b, c, d, k;
scanf("%d%d%d%d%d", &a, &b, &c, &d, &k);
if(! k){
printf("Case %d: 0\n", ++ tot);
continue;
}
b /= k, d /= k;
if(b > d) swap(b, d);
LL Ans = 0LL;
for(int i = 1; i <= d; ++ i){//枚举可能的x
k = min(i, b);
Ans += k;//总共的因子有k个
for(int j = 1; j < (1 << fac[i].size()); ++ j)//容斥能被x的质因子整除的数的个数
Ans -= work(k, j, i);
}
printf("Case %d: %lld\n", ++ tot, Ans);
}
return 0;
}