Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5526 Accepted Submission(s): 2209
php
/* * @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; }