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.
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.
For each test case, print the number of choices. Use the format in the example.
2
1 3 1 5 1
1 11014 1 14409 9
Case 1: 9
Case 2: 736427
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).
#include
#define LL __int64
int p[64],u;
LL Eular(int n){
LL ans=1;
// ans=(p1^a1-p1)*(p2^a2-p2)*...*(pn^an-pn);
for(int i=2;i*i<=n;i++){
if(n%i==0){
ans*=i-1;
n/=i;
while(n%i==0) n/=i,ans*=i;
}
}
if(n>1) ans*=n-1;
return ans;
}
//Principle of Inclusion and Exclusion
int POIAE(int n,int S,int E){
//质因子分解
u=0;
for(int i=2;i*i<=n;i++)
if(n%i==0){
p[u++]=i;
while(n%i==0) n/=i;
}
if(n>1) p[u++]=n;
int qua=0;
for(int i=1;i<1<int cnt=0,com=1;
for(int k=0;kif(i>>k&1) cnt++,com*=p[k];
qua+=cnt&1?E/com-S/com:-E/com+S/com;
}
return E-S-qua;
}
int main()
{
int T,s[2],e[2],k;
scanf("%d",&T);
for(int t=1;t<=T;t++){
scanf("%d%d%d%d%d",s,e,s+1,e+1,&k);
if(k==0){
printf("Case %d: 0\n",t);
continue;
}
e[0]/=k,e[1]/=k;
LL ans=0;int max,min;
if(e[0]>e[1]){max=e[0],min=e[1];}else{max=e[1],min=e[0];}
for(int i=1;i<=min;i++)
ans+=Eular(i);
for(int i=min+1;i<=max;i++)
ans+=POIAE(i,0,min);
printf("Case %d: %lld\n",t,ans);
}
}