题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1101
#include<cstdio> #include<cstring> #include<algorithm> #define ll long long using namespace std; const int N=5e4+5; int T,n,m,D,u[N],pri[N];bool vis[N]; int rdn() { int ret=0;bool fx=1;char ch=getchar(); while(ch>'9'||ch<'0'){if(ch=='-')fx=0;ch=getchar();} while(ch>='0'&&ch<='9')ret=ret*10+ch-'0',ch=getchar(); return fx?ret:-ret; } void init() { int lm=5e4,cnt=0;u[1]=1; for(int i=2;i<=lm;i++) { if(!vis[i])pri[++cnt]=i,u[i]=-1; for(int j=1;j<=cnt&&(ll)i*pri[j]<=lm;j++) { vis[i*pri[j]]=1; if(i%pri[j]==0){u[i*pri[j]]=0;break;} u[i*pri[j]]=-u[i]; } u[i]+=u[i-1]; } } int main() { T=rdn();init(); while(T--) { n=rdn();m=rdn();D=rdn(); n/=D;m/=D;if(n>m)swap(n,m); ll ans=0; for(int i=1,d0,d1,j;i<=n;i=j+1) { d0=n/i;d1=m/i; j=min(n/d0,m/d1); ans+=(ll)(u[j]-u[i-1])*d0*d1; } printf("%lld\n",ans); } return 0; }