参考博客:https://blog.csdn.net/litble/article/details/72804050
hdu1695
#include<bits/stdc++.h> using namespace std; #define ll long long const double PI=acos(-1.0); const int maxn=1e5+10; int cnt=1,pri[maxn],u[maxn],is[maxn]; void getu() { u[1]=1; for(int i=2;i<maxn;i++) { if(is[i]==0)u[i]=-1,pri[cnt++]=i; for(int j=1;j<cnt;j++) { ll k=i*pri[j]; if(k>=maxn)break; is[k]=1; if(i%pri[j]==0) { u[k]=0; break; } else u[k]=-u[i]; } } } int main() { int T; getu(); scanf("%d",&T); for(int cn=1;cn<=T;cn++) { ll a,b,c,d,k; ll ans1=0,ans2=0; scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&k); if(k==0) { printf("Case %d: ",cn); printf("0 "); continue; } b/=k,d/=k; if(d<b)swap(b,d); for(int i=1;i<=d;i++)ans1+=u[i]*(b/i)*(d/i); for(int i=1;i<=d;i++)ans2+=u[i]*(b/i)*(b/i); printf("Case %d: ",cn); printf("%lld ",ans1-ans2/2); } return 0; }