标签:\n std put for main 个数 ace AC cst
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 <= 1015) and (1 <=N <= 109).
题解:先求出n的所有质因数,因为n最大为1e9所以最多10个
利用二进制来模拟是否乘上某个质因数,例如有个n为2*3*5=30
1-x中共有ans个数与其互质 ans=-x/2-x/3-x/5+x/6+x/10+x/15-x/30
#include<cstdio> #include<cstring> #include<iostream> using namespace std; long long fa[20]; long long factor(long long x) { long long num=0; for(long long i=2;i*i<=x;i++) { if(x%i==0) { fa[num++]=i; while(x%i==0)x/=i; } } if(x>1)fa[num++]=x; return num; } long long un(long long x,long long num) { long long res=0; for(long long i=0;i<(1<<num);i++) { long long g=1,k=0; for(long long j=0;j<num;j++) { if(i&(1<<j)) g*=fa[j],k++; } if(k%2) res-=x/g; else res+=x/g; } return res; } int main() { long long T,n,i=1; long long a,b,ans; scanf("%I64d",&T); while(T--) { scanf("%I64d %I64d %I64d",&a,&b,&n); long long num=factor(n); ans=un(b,num)-un(a-1,num); printf("Case #%I64d: ",i++); printf("%I64d\n",ans); } return 0; }
标签:\n std put for main 个数 ace AC cst
原文地址:https://www.cnblogs.com/carcar/p/9058317.html