标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2371 Accepted Submission(s): 887
容斥原理...
/* *********************************************** Author :PK28 Created Time :2015/8/18 9:29:57 File Name :4.cpp ************************************************ */ #include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <iomanip> #include <list> #include <deque> #include <stack> #define ull unsigned long long #define ll long long #define mod 90001 #define INF 0x3f3f3f3f #define maxn 10000+10 #define cle(a) memset(a,0,sizeof(a)) const ull inf = 1LL << 61; const double eps=1e-5; using namespace std; bool cmp(int a,int b){ return a>b; } ll a,b,n; ll solve(){ ll sum=0; vector<ll>v; for(int i=2;i*i<=n;i++) if(n%i==0){ v.push_back(i); while(n%i==0)n/=i; } if(n>1)v.push_back(n); for(ll st=1;st<(1<<(v.size()));++st){//0 1 ll bits=0,mult=1; for(int i=0;i<(int)v.size();++i){ if(st&(1<<i)){ ++bits; mult*=v[i]; } } ll cur=b/mult-a/mult; if(a%mult==0)cur++; if(bits&1)sum+=cur; else sum-=cur; } return b-a-sum+1; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif //freopen("out.txt","w",stdout); int t; cin>>t; for(int i=1;i<=t;i++){ scanf("%I64d %I64d %I64d",&a,&b,&n); printf("Case #%d: %I64d\n",i,solve()); } return 0; }
标签:
原文地址:http://www.cnblogs.com/pk28/p/4738724.html