BZOJ 4916 神犇和蒟蒻

思路:杜教筛

提交:\(2\)

错因:\(sum\)函数处取模出错

题解:

首先第一问是智商检测题:\(\sum_{i=1}^n \mu(i^2)\)显然为\(1\)
第二问其实是跟杜教筛板子那篇里面说的似的:
\(f=\varphi(i^2)=\varphi(i)\cdot i\)
\(S(n)=\sum_{i=1}^{n}f(i)\)
配式子:
\(id^2=f\cdot id\)
所以式子就成了
\(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

posted @ 2019-08-24 14:17  LuitaryiJack  阅读(147)  评论(0编辑  收藏  举报