标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
#include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll __int64 #define mod 1000000007 #define inf 999999999 //#pragma comment(linker, "/STACK:102400000,102400000") int scan() { int res = 0 , ch ; while( !( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ ) ) { if( ch == EOF ) return 1 << 30 ; } res = ch - ‘0‘ ; while( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ ) res = res * 10 + ( ch - ‘0‘ ) ; return res ; } ll prime[100010]; ll vis[100010]; ll a[110]; ll ji,cnt; ll ans,x,y,z; ll gcd(ll x,ll y) { return y==0?x:gcd(y,x%y); } void Prime(ll n) { cnt=0; memset(vis,0,sizeof(vis)); for(ll i=2;i<n;i++) { if(!vis[i]) prime[cnt++]=i; for(ll j=0;j<cnt&&i*prime[j]<n;j++) { vis[i*prime[j]]=1; if(i%prime[j]==0)//关键 break; } } } void dfs(ll lcm,ll pos,ll step,ll x,ll &ans) { if(lcm>x) return; if(pos==ji) { if(step==0) return; if(step&1) ans+=(x/lcm); else ans-=(x/lcm); return; } dfs(lcm,pos+1,step,x,ans); dfs(lcm/gcd(a[pos],lcm)*a[pos],pos+1,step+1,x,ans); } int main() { ll t,i; int cs=1; Prime(100000); scanf("%I64d",&t); while(t--) { ji=0; scanf("%I64d%I64d%I64d",&x,&y,&z); for(i=0;i<cnt;i++) if(z%prime[i]==0) { a[ji++]=prime[i]; while(z%prime[i]==0) z/=prime[i]; } if(z>1) a[ji++]=z; ll gg=0; ans=0; dfs(1,0,0,x-1,gg); dfs(1,0,0,y,ans); printf("Case #%d: ",cs++); printf("%I64d\n",(y-ans)-(x-1-gg)); } return 0; }
标签:
原文地址:http://www.cnblogs.com/jhz033/p/5481142.html