BZOJ 4916 Ben God and konjac

Ideas: Du teach sieve

Submission: \ (2 \) times

Errors of: \ (SUM \) functions at the error modulo

answer:

First, the first question to ask is detected IQ: \ (\ sum_ {i = 1} ^ the n-\ MU (i ^ 2) \) apparently \ (1 \)
The second question is actually a sieve with Du teach the child thing I read said there the like:
\ (F = \ varphi (I ^ 2) = \ varphi (I) \ CDOT I \)
\ (S (n-) = \ sum_. 1 = {I}} ^ {n-F (I) \)
with formula:
\ (ID = 2 ^ F \ CDOT ID \)
so equation became
\ (1 \ cdot S (n ) = \ sum_ {i = 1} ^ {n} i ^ 2- \ sum_ { i = 2} ^ {n} i \ cdot S (\ frac {n} {i}) \)

#include<cstdio>
#include<iostream>
#include<map>
#define ll long long
#define R register int
using namespace std;
namespace Luitaryi {
template<class I> inline I g(I& x) { x=0; register I f=1;
    register char ch; while(!isdigit(ch=getchar())) f=ch=='-'?-1:f;
    do x=x*10+(ch^48); while(isdigit(ch=getchar())); return x*=f;
} const int N=5000000,M=1e9+7,Inv=166666668;
int n,cnt,p[N+10];
ll phi[N+10]; bool v[N+10];
map<int,ll> mem;
inline void PRE() { v[1]=phi[1]=1;
    for(R i=2;i<=N;++i) {
        if(!v[i]) p[++cnt]=i,phi[i]=i-1;
        for(R j=1;j<=cnt&&i*p[j]<=N;++j) {
            v[i*p[j]]=true;
            if(i%p[j]==0) {
                phi[i*p[j]]=phi[i]*p[j]; break;
            } phi[i*p[j]]=phi[i]*(p[j]-1);
        }
    } for(R i=1;i<=N;++i) phi[i]=(1ll*phi[i]*i%M+phi[i-1])%M;
}
inline ll sum(ll x) {return 1ll*x*(x+1)%M*(2*x+1)%M*Inv%M;}
inline ll query(ll x) {return 1ll*x*(x+1)/2%M;}
inline ll solve(int n) {
    if(n<=N) return phi[n];
    if(mem.count(n)) return mem[n]; 
    register ll ret=sum(n);
    for(R l=2,r;l<=n;l=r+1) 
        r=min(n/(n/l),n),ret-=(query(r)-query(l-1))%M*solve(n/l)%M,ret=(ret+M)%M;
    return mem[n]=ret;
}
inline void main() {PRE(); g(n); printf("1\n%lld\n",solve(n));}
} signed main() {Luitaryi::main(); return 0;}

2019.08.23\24
77\76

Guess you like

Origin www.cnblogs.com/Jackpei/p/11404541.html