count_prime
Time Limit: 1000ms
Memory Limit: 65536KB
Input
第一行输入一个整数T(1 <= T <= 100),表示T组测试数据。接下来T行,每行3个整数a,b,n(1 <= a <=b <=10^15, 1<= n <= 10^9),用空格隔开。
Sample Output
5 6这题其实就是容斥题,不会容斥的点这里。AC代码:#include<iostream> #include<algorithm> #include<cstring> #include<string> #include<cstdio> #include<queue> #include<set> using namespace std; #define T 1000+50 #define inf 0x3f3f3f3f typedef long long ll; vector<int> ve; ll slove(ll n,ll val) { ve.clear(); for(ll i=2;i*i<=val;++i)//质因数分解 if(val%i==0){ ve.push_back(i); while(val%i==0)val/=i; } if(val>1)ve.push_back(val); ll i,j,num=ve.size(),cnt,c,sum=0; for(i=1;i<(1<<num);i++){//枚举状态,64位二进制1-(1<<num)就是num位数的全排列 cnt = 1;c=0; for(j=0;j<num;++j){//每次去取值,即1-num的交集(容斥) if(i&(1<<j)){ cnt*=ve[j]; c++; } } if(c&1)sum += n/cnt; else sum -= n/cnt; } return n-sum; } int main() { #ifdef zsc freopen("input.txt","r",stdin); #endif int n,i,j,k; ll L,R,m; scanf("%d",&n); while(n--) { scanf("%lld%lld%lld",&L,&R,&m); printf("%lld\n",slove(R,m)-slove(L-1,m)); } return 0; }