Problem Description
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
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
Sample Input
2 1 3 1 5 1 1 11014 1 14409 9
Sample Output
Case 1: 9 Case 2: 736427
Hint
Source
#include<stdio.h>
#include<string.h>
int main()
{
int a,b,c,d,k,t,n,m,x,cnt,p[1000];
long long sum;
scanf("%d",&t);
for(int l=1;l<=t;l++)
{
scanf("%d %d %d %d %d",&a,&b,&c,&d,&k);
if(k==0||k>b||k>d)
{
printf("Case %d: 0\n",l);
continue;
}
b=b/k;
d=d/k;
n=b<=d?b:d;
m=b>=d?b:d;//n为小的那位,m为大的那位;
sum=m;
for(int i=2;i<=n;i++)
{
sum=sum+m-i;
x=i;
cnt=0;
memset(p,0,sizeof(p));
for(int j=2;j*j<=x;j++)
{
if(x%j==0)
{
p[cnt++]=j;
while(x%j==0)
x=x/j;
}
}
if(x>1)
p[cnt++]=x;
for(long long j=1;j<(1<<cnt);j++)
{
long long s=1,l=0;
for(int k=0;k<cnt;k++)
{
if(j&(1<<k))
{
l++;
s=s*p[k];
}
}
s=m/s-i/s;
if(l%2)
sum=sum-s;//减去奇数个素因子相乘
else
sum=sum+s;//加上偶数个素因子相乘;
}
}
printf("Case %d: %lld\n",l,sum);
}
return 0;
}
1809



被折叠的 条评论
为什么被折叠?



