同样是容斥原理,我昨晚调了大半天TAT,最后才发现数据范围少看了个0。。0.0真是醉了
另外还有个问题,这能用phi么。。。
1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<algorithm> 5 #include<iostream> 6 #define inc(i,l,r) for(i=l;i<=r;i++) 7 #define dec(i,a,b) for(i=a;i>=b;i--) 8 #define inf 1e9 9 #define NM 100000+5 10 #define mem(a) memset(a,0,sizeof(a)) 11 using namespace std; 12 bool v[NM]; 13 int f[NM],p[NM],i,j,k,n,x,T,u,a,b,c,d; 14 long long ans; 15 long long work(int x,int m){ 16 int t=0; 17 long long ans=0; 18 if(x==0||m==0)return 0; 19 if(x==1)return m; 20 if(!v[x])f[++t]=x; 21 else{ 22 inc(j,1,n) 23 if(x%p[j]==0){ 24 f[++t]=p[j]; 25 while(x%p[j]==0)x/=p[j]; 26 if(!v[x]){ 27 if(x>1)f[++t]=x; 28 break; 29 } 30 } 31 } 32 inc(j,1,(1<<t)-1){ 33 int s=1,r=0; 34 inc(k,0,t-1) 35 if(j&(1<<k)){ 36 r++;s*=f[k+1]; 37 } 38 if(r&1)ans+=m/s;else ans-=m/s; 39 } 40 return m-ans; 41 } 42 void init(){ 43 v[1]++; 44 inc(i,2,100000) 45 if(!v[i]){ 46 p[++n]=i; 47 inc(j,2,100000/i)v[i*j]++; 48 } 49 } 50 int main(){ 51 init(); 52 scanf("%d",&T); 53 inc(u,1,T){ 54 scanf("%d%d%d%d%d",&a,&b,&c,&d,&k); 55 printf("Case %d: ",u); 56 if(k==0||b<k||d<k){ 57 printf("0 "); 58 continue; 59 } 60 a=max(b,d); 61 b=min(b,d); 62 a/=k;b/=k; 63 ans=(long long)a; 64 inc(i,2,b)ans+=work(i,a)-work(i,i-1); 65 printf("%lld ",ans); 66 } 67 return 0; 68 }