1 条题解

  • 0
    @ 2025-10-8 17:11:59
    #include<bits/stdc++.h>
    using namespace std;
    template<typename T> void qread(T &x){
    	x=0; int f=1; char c=getchar();
    	for(; !isdigit(c); c=getchar()) if(c=='-') f=-1;
    	for(; isdigit(c); c=getchar()) x=x*10+(c-'0');
    	x*=f;
    }
    const int N=5e6+10;
    typedef long long LL;
    const LL P=1e9+7;
    int pr, prime[N];
    LL phi[N];
    bool v[N];
    void init(){
    	pr=0; memset(v, 0, sizeof(v));
    	phi[1]=1; phi[0]=0;
    	for(int i=2; i<=N-10; i++){
    		if(v[i]==0) prime[++pr]=i, phi[i]=i-1;
    		for(int j=1; (j<=pr) && (i*prime[j]<=N-10); j++){
    			v[i*prime[j]]=1;
    			if(i%prime[j]==0){
    				phi[i*prime[j]]=phi[i]*prime[j]%P;
    				break;
    			}
    			else{
    				phi[i*prime[j]]=phi[i]*phi[prime[j]]%P;
    			}
    		} 
    	}
    	for(int i=1; i<=N-10; i++){
    		phi[i]=(i*phi[i]%P+phi[i-1])%P;
    	}
    }
    LL q_pow(LL a, LL b){
    	LL res=1;
    	while(b){
    		if(b&1) res=res*a%P;
    		a=a*a%P; b/=2;
    	}
    	return res;
    }
    map<LL, LL> hs;
    LL solve(int x){
    	if(hs[x]) return hs[x];
    	if(x<=N-10) return phi[x];
    	LL res=(1ll*x*(x+1)%P*(2*x+1)%P)*q_pow(6, P-2)%P;
    	for(int i=2, j; i<=x; i=j+1){
    		j=x/(x/i);
    		res=(res-(1ll*(i+j)*(j-i+1)/2)%P*solve(x/i)%P+P)%P;
    	}
    	return hs[x]=(res%P+P)%P;
    }
    int main(){
    	init();
    	int n; qread(n);
    	printf("1\n%lld\n", solve(n));
    	return 0;
    }
    
    • 1

    *【杜教筛】mu(i^2)求和、phi(i^2)求和[神犇和蒟蒻]

    信息

    ID
    6585
    时间
    1000ms
    内存
    128MiB
    难度
    10
    标签
    递交数
    9
    已通过
    2
    上传者