GCD
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9637 Accepted Submission(s): 3597
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: 9Case 2: 736427
#include<stdio.h> #include<string.h> #include<math.h> #include<vector> using namespace std; const int maxm=100005; vector<int>v[maxm]; int work(int u,int s,int w); int flag[maxm]={0}; int main() { int i,j,k,sum,cas=1,b,d,t,a,c;long long ans; for(i=1;i<maxm;i++) v[i].clear(); for(i=2;i<maxm;i+=2) v[i].push_back(2); for(i=3;i<maxm;i+=2) { if(!flag[i]) { for(j=i;j<maxm;j+=i) { v[j].push_back(i); flag[j]=1; } } } scanf("%d",&t); while(t--) { scanf("%d%d%d%d%d",&a,&b,&c,&d,&k); if(k==0) { printf("Case %d: 0\n",cas++); continue; } b/=k;d/=k;ans=0; if(b>d) {a=b;b=d;d=a;} for(i=1;i<=d;i++) { k=min(i,b); ans+=k; for(j=1;j<(1<<v[i].size());j++) ans-=work(k,j,i); } printf("Case %d: %lld\n",cas++,ans); } return 0; } int work(int u,int s,int w) { int i,j,sum,x; sum=0;x=1; for(i=0;i<v[w].size();i++) { if((1<<i)&s) { sum++; x*=v[w][i]; } } int a=u/x; if(sum%2==0) return -a; else return a; }