您的位置:首页 > 其它

杭电1695GCD【欧拉函数】【容斥定理】

2016-12-03 14:13 330 查看


GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 10255    Accepted Submission(s): 3868


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.

 

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.

 

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: 736427

HintFor 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).题意:x∈[a,b],y∈[c,d],x=i*k,y=j*k,则i,j互质,且i∈[1,b/k=m],j∈[1,d/k=n],求存在多少对的i,j,(i,j)与(j,i)相同,(假设i>j)存在多少对(i,j).假设m>n,则分为两个部分求①i∈[1,n],j∈[1,n],i,j互质,用欧拉函数求
②i∈[n+1,m],j∈[1,n],枚举i,求[1,n]中多少个数与i互质,用容斥定理
#include<stdio.h>
#include<string.h>
#define LL long long
LL n,m,num;
LL p[15],o[100010];
void eulur()
{
LL i,j;
memset(o,0,sizeof(o));
o[1]=1;
for(i=2;i<100010;i++)
{
   if(!o[i])
   {
    for(j=i;j<100010;j+=i)
    {
    if(!o[j])
    o[j]=j;
    o[j]=o[j]*(i-1)/i;
}
}
o[i]+=o[i-1];
}
}
void get(LL k)
{
for(LL i=2;i*i<=k;i++)
{
if(k%i==0)
p[num++]=i;
while(k%i==0)
k/=i;
}
if(k>1)
p[num++]=k;
}
LL solve(LL k)
{

LL i,j,ans=0;
for(i=1;i<(1LL<<num);i++)
{
   LL s=0;
   LL l=1;
for(j=0;j<num;j++)
{
if((i>>j)&1)
{
s++;
l*=p[j];
}
}
if(s&1)
ans+=k/l;
else
ans-=k/l;
}
return ans;
}
int main()
{
LL a,b,c,d,k,t,l,sum;
scanf("%lld",&t);
eulur();
for(l=1;l<=t;l++)
{
scanf("%lld%lld%lld%lld%lld",&a,&b,&c,&d,&k);
if(k==0)
{
printf("Case %d: 0\n",l);
continue;
}
m=b/k>d/k?b/k:d/k;
n=b/k<d/k?b/k:d/k;
sum=o
;
for(LL i=n+1;i<=m;i++)
{
num=0;
get(i);
sum+=n-solve(n);
}
printf("Case %lld: %lld\n",l,sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: