【bzoj4916】神犇和蒟蒻

  • 本文为博主原创,未经许可不得转载

第一问答案是1…….

第二问题中$\phi(i^2)=i\phi(i)$(根据欧拉函数定义可以得到)

构造$g(n)=n$使得$f*g(n)=n\sum_{d|n}\phi(d)=n^2$

所以$S(n)=n^2-\sum_{i=2}^{n} iS(\frac{n}{i})$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<bits/stdc++.h>
#define FILE "read"
#define MAXN 2000100
using namespace std;
const int mod=1e9+7;
int n,N,cnt,inv[10],value[1010],vis[1010],check[MAXN],phi[MAXN],prime[MAXN];
inline int read(){
int x=0,f=1; char ch=getchar();
while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch=getchar();}
while(ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
inline int add(int a,int b){return (a+=b)>=mod?a-mod:a;}
inline int sub(int a,int b){return (a-=b)<0?a+mod:a;}
inline int mul(int a,int b){return 1LL*a*b%mod;}
inline int fast(int a,int b){int ret(1);for(;b;b>>=1,a=mul(a,a))if(b&1)ret=mul(ret,a);return ret;}
inline int getsum(int l,int r){return mul(mul(add(l,r),add(sub(r,l),1)),inv[2]);}
void pre(){
N=2000000; phi[1]=1; inv[2]=fast(2,mod-2); inv[6]=fast(6,mod-2);
for(int i=2;i<=N;++i){
if(!check[i]) prime[++cnt]=i,phi[i]=i-1;
for(int j=1;j<=cnt&&prime[j]*i<=N;++j){
check[prime[j]*i]=1;
if(i%prime[j]==0) {phi[prime[j]*i]=phi[i]*prime[j]; break;}
phi[prime[j]*i]=phi[i]*phi[prime[j]];
}
}
for(int i=1;i<=N;++i) phi[i]=add(phi[i-1],mul(i,phi[i]));
}
int solve(int x){
int pos=n/x;
if(x<=N) return phi[x];
if(vis[pos]) return value[pos];
vis[pos]=1; value[pos]=mul(mul(x,mul(add(x,1),add(mul(x,2),1))),inv[6]);
for(int i=2,last=0;i<=x;i=last+1){
last=x/(x/i);
value[pos]=sub(value[pos],mul(solve(x/i),getsum(i,last)));
}
return value[pos];
}
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
n=read(); pre();
printf("%d\n%d\n",1,solve(n));
return 0;
}
文章目录
,