Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5526 Accepted Submission(s): 2209
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.
/* * @Author: lyuc * @Date: 2017-08-16 16:52:21 * @Last Modified by: lyuc * @Last Modified time: 2017-08-16 21:44:34 */ /* 题意:给你a,b,n让你求在区间[a,b]内有多少数与n互质 思路:求出n的所有质因子,然后[1,a]区间内的与n互质的数的数量就是,a减去不互质的数的数量,同理 [1,b]的也可以这么求,容斥求出这部分的结果 */ #include <bits/stdc++.h> #define LL long long #define MAXN 1005 #define MAXM 10005 using namespace std; int t; LL a,b,n; LL factor[MAXN]; LL tol; LL que[MAXM]; void div(LL n){//筛出来n的因子 tol=0; for(LL i=2;i*i<=n;i++){ if(n%i==0){ factor[tol++]=i; while(n%i==0){ n/=i; } } } if(n!=1) factor[tol++]=n;//如果是素数的话加上本身 } LL cal(LL x){//容斥计算出[1,x]内与n不互质的元素的个数 LL res=0; LL t=0; que[t++]=-1; for(int i=0;i<tol;i++){ int k=t; for(int j=0;j<k;j++){ que[t++]=que[j]*factor[i]*-1; } } for(int i=1;i<t;i++){ res+=x/que[i]; } return res; } int main(){ // freopen("in.txt","r",stdin); scanf("%d",&t); for(int ca=1;ca<=t;ca++){ printf("Case #%d: ",ca); scanf("%lld%lld%lld",&a,&b,&n); div(n); printf("%lld\n",b-cal(b)-(a-1-cal(a-1)) ); } return 0; }