GCD
Time Limit: 6000/3000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 5454 accepted submission (s): 1957
Problem descriptiongiven 5 integers: A, B, C, D, K, you're trying to find X in... 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.
Inputthe 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 abve.
Outputfor each test case, print the number of choices. Use the format in the example.
Sample Input
21 3 1 5 11 11014 1 14409 9
Sample output
Case 1: 9Case 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).
Question: calculate the number of the logarithm of (1, A) and (1, B) where the common number of the two intervals is K.
Train of Thought: If K is used to separate a and B, it can be converted to the number of mutual quality between two intervals (1, A/K) and (1, B/K, you can use the Euler's function to calculate the number of mutual quality (1, A). (A + 1, B) You can break down the prime factor. because the number of prime factors is 7 at most, you can use the volume Exclusion Principle for calculation.
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <string>#include <algorithm>#include <queue>using namespace std;const int maxn = 10000+10;const int maxxn = 100000+10;typedef long long ll;int a,b,gcd;ll ans;bool isPrime[maxn];ll minDiv[maxxn],phi[maxxn],sum[maxxn];vector<int> prime,cnt[maxxn],digit[maxxn];void getPrime(){ prime.clear(); memset(isPrime,1,sizeof isPrime); for(int i = 2;i < maxn; i++){ if(isPrime[i]){ prime.push_back(i); for(int j = i*i; j < maxn; j+=i){ isPrime[j] = 0; } } }}void getPhi(){ for(ll i = 1; i < maxxn; i++){ minDiv[i] = i; } for(ll i = 2; i*i < maxxn; i++){ if(minDiv[i]==i){ for(int j = i*i; j < maxxn; j += i){ minDiv[j] = i; } } } phi[1] = 1; sum[1] = 1; for(ll i = 2; i < maxxn; i++){ phi[i] = phi[i/minDiv[i]]; if((i/minDiv[i])%minDiv[i]==0){ phi[i] *= minDiv[i]; }else{ phi[i] *= minDiv[i]-1; } sum[i] = phi[i]+sum[i-1]; }}void getDigit(){ for(ll i = 1; i < maxxn; i++){ int x = i; for(int j = 0; j < prime.size()&&x >= prime[j]; j++){ if(x%prime[j]==0){ digit[i].push_back(prime[j]); int t = 0; while(x%prime[j]==0){ t++; x /= prime[j]; } cnt[i].push_back(t); } } if(x!=1){ digit[i].push_back(x); cnt[i].push_back(1); } }}int main(){ getPrime(); getPhi(); getDigit(); int ncase,T=1; cin >> ncase; while(ncase--){ int t1,t2; scanf("%d%d%d%d%d",&t1,&a,&t2,&b,&gcd); if(gcd==0){ printf("Case %d: 0\n",T++,ans); continue; }else{ if(a > b) swap(a,b); a /= gcd,b /= gcd; ans = sum[a]; for(ll i = a+1; i <= b; i++){ int d = digit[i].size(); int t = 0; vector<int> di; for(int k = 1; k < (1<<d); k++){ di.clear(); for(int f = 0; f < d; f++){ if(k&(1<<f)){ di.push_back(digit[i][f]); } } int ji = 1; for(int f = 0; f < di.size(); f++){ ji *= di[f]; } if(di.size()%2==0){ t -= a/ji; }else{ t += a/ji; } } ans += a-t; } printf("Case %d: ",T++); cout<<ans<<endl; } } return 0;}