前3个点AC,后面全RE
查看原帖
前3个点AC,后面全RE
692274
hgckythgcfhk楼主2023/5/13 10:26

程序如下:

#include <bits/stdc++.h>
using namespace std;
#define void inline void
#define int long long
#define getchar() getchar_unlocked()
#define putchar(c) putchar_unlocked(c)
#define aa(c) (c<=58&&c>=48)
inline int read(){int s=0;char c=getchar();
while(!aa(c))c=getchar();while(aa(c))s=s*10+c-48,c=getchar();
	return s;}
void write(int &n){static char c[20];memset(c,0,sizeof c);
if(n<0){putchar('-');n=-n;}if(n==0){putchar(48);return;}
while(n)c[++c[0]]=n%10+48,n/=10;while(c[0]--)putchar(c[c[0]+1]);}
int n;
unordered_map<int,int>sphi,smu;
const int N=5e6+5;
int phi[N],mu[N],p[N>>2],cnt=0;bool vis[N];
void init(){vis[0]=vis[1]=phi[1]=mu[1]=1;
	for(int i=2;i<N;i++){if(!vis[i])p[++cnt]=i,mu[i]=-1,phi[i]=i-1;
	for(int j=1;j<=cnt&&i*p[j]<N;j++){vis[i*p[j]]=1;
		if(!(i%p[j])){mu[i*p[j]]=0,phi[i*p[j]]=phi[i]*p[j];break;}
		else mu[i*p[j]]=-mu[i],phi[i*p[j]]=phi[i]*phi[p[j]];
	}}for(int i=2;i<=N-5;i++)phi[i]+=phi[i-1],mu[i]+=mu[i-1];}
inline int Sm(int x){if(x<N-5)return mu[x];
	if(smu.count(x))return smu[x];int ans=x*(x-1)>>1,l=2,r;
	for(;l<=x;l=r+1)r=x/(x/l),ans-=(r-l+1)*Sm(x/l);
	return smu[x]=ans;}
inline int Sp(int x){if(x<N-5)return phi[x];
	if(sphi.count(x))return phi[x];int ans=x*(x+1)>>1,l=2,r;
	for(;l<=x;l=r+1)r=x/(x/l),ans-=(r-l+1)*Sp(x/l);
	return sphi[x]=ans;}
signed main(){ios::sync_with_stdio(false);init();
	char t=read();while(t--){
		n=read();int ans1=Sp(n),ans2=Sm(n);
		write(ans1);putchar(' ');write(ans2);putchar('\n');}}
2023/5/13 10:26
加载中...