Co-prime
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 988 Accepted Submission(s): 357
Problem Description
Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N.
Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than 1 or, equivalently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.
Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than 1 or, equivalently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.
Input
The first line on input contains T (0 < T <= 100) the number of test cases, each of the next T lines contains three integers A, B, N where (1 <= A <= B <= 10
15) and (1 <=N <= 10
9).
Output
For each test case, print the number of integers between A and B inclusive which are relatively prime to N. Follow the output format below.
Sample Input
2 1 10 2 3 15 5
Sample Output
Case #1: 5 Case #2: 10HintIn the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.
Source
Recommend
lcy
题解:基本容斥定理,求不互素的个数就可以了,注意别用long long...不然会wa惨
简单递归写法:
#include<stdio.h>
__int64 a[10008],all,res1,res2,temp;
void div(__int64 x)
{
int i;
all=0;
for(i=2;i*i<=x;i++)
{
if(x%i==0)
{
a[all++]=i;
while(x%i==0) x=x/i;
}
}
if(x>1) a[all++]=x;
}
void cal(__int64 x,int cou,int now,__int64 d)
{
int i;
d=d*a[now];
if(cou&1) temp=temp-x/d;
else temp=temp+x/d;
for(i=now+1;i<all;i++) cal(x,cou+1,i,d);
}
int main()
{
int t,n,i,cas=1;
__int64 s,e;
scanf("%d",&t);
while(t--)
{
scanf("%I64d%I64d%d",&s,&e,&n);
div(n);
for(i=temp=0;i<all;i++) cal(s-1,0,i,1);
res1=temp;
for(i=temp=0;i<all;i++) cal(e,0,i,1);
res2=temp;
printf("Case #%d: %I64d\n",cas++,e-(s-1)-(res2-res1));
}
return 0;
}
队列写法:
#include<stdio.h>
__int64 a[10008],que[100008],all;
void div(__int64 x)
{
int i;
all=0;
for(i=2;i*i<=x;i++)
{
if(x%i==0)
{
a[all++]=i;
while(x%i==0) x=x/i;
}
}
if(x>1) a[all++]=x;
}
__int64 cal(__int64 n)
{
int i,j,k,fin=0;
__int64 res=0;
que[fin++]=1;
for(i=0;i<all;i++)
{
k=fin;
for(j=0;j<k;j++)
que[fin++]=a[i]*que[j]*(-1);
}
for(i=0;i<fin;i++) res+=n/que[i];
return res;
}
int main()
{
int t,n,cas=1;
__int64 s,e,res1,res2;
scanf("%d",&t);
while(t--)
{
scanf("%I64d%I64d%d",&s,&e,&n);
div(n);
res1=cal(s-1);
res2=cal(e);
printf("Case #%d: %I64d\n",cas++,res2-res1);
}
return 0;
}
高级点的递归写法:
#include<stdio.h>
__int64 a[10008],all;
void div(__int64 x)
{
int i;
all=0;
for(i=2;i*i<=x;i++)
{
if(x%i==0)
{
a[all++]=i;
while(x%i==0) x=x/i;
}
}
if(x>1) a[all++]=x;
}
__int64 cal(__int64 x,int now)
{
__int64 i,res=0;
for(i=now;i<all;i++)
res=res+x/a[i]-cal(x/a[i],i+1);
return res;
}
int main()
{
int t,cas=1;
__int64 s,e,res1,res2,n;
scanf("%d",&t);
while(t--)
{
scanf("%I64d%I64d%I64d",&s,&e,&n);
div(n);
res1=cal(s-1,0);
res2=cal(e,0);
printf("Case #%d: %I64d\n",cas++,e-s+1-res2+res1);
}
}