# LOJ #6052. 「雅礼集训 2017 Day11」DIV

### 一些前置推导

$\sum_{w|\frac{k}{p^2+q^2}} p\cdot w=p\cdot\sigma(\frac{k}{p^2+q^2})$

$\sum_t \sum_{\gcd(p,q)=1\&\&p^2+q^2=t} p\sum_{y|k} \sigma(\frac{k}t)$

$\sum_{d=1}^n F(d)\cdot D(\frac{n}d)$

### 求解$D$

$D(n)=\sum_{i=1}^n\sigma(i)=\sum_{d=1}^n d\cdot\lfloor\frac{n}d\rfloor$

### 求解$F$

$G(n)=\sum_{d\ge 1} d\cdot F(\lfloor\frac{n}{d^2}\rfloor)$

$G(n)=F(n)+\sum_{d\ge 2} d\cdot F(\lfloor\frac{n}{d^2}\rfloor)$

CODE

#include<cstdio>
#include<map>
#include<cmath>
#define RI register int
#define CI const int&
using namespace std;
typedef long long LL;
const int N=5000000,mod=1004535809,inv2=502267905;
int prime[N+5],cnt,ans; bool vis[N+5];
LL n,ds[N+5],fs[N+5]; map <LL,int> _ds,_fs;
inline void inc(LL& x,const LL y)
{
if ((x+=y)>=mod) x-=mod;
}
inline void inc(int& x,CI y)
{
if ((x+=y)>=mod) x-=mod;
}
inline void dec(int& x,CI y)
{
if ((x-=y)<0) x+=mod;
}
inline int sum(CI x,CI y)
{
int t=x+y; return t>=mod?t-mod:t;
}
inline int sub(CI x,CI y)
{
int t=x-y; return t<0?t+mod:t;
}
inline int gcd(CI x,CI y)
{
return y?gcd(y,x%y):x;
}
inline int Sum(const LL& l,const LL& r)
{
return ((l+r)%mod)*((r-l+1)%mod)%mod*inv2%mod;
}
#define Pi prime[j]
inline void init(CI n)
{
RI i,j; ds[1]=vis[1]=1; for (i=2;i<=n;++i)
{
if (!vis[i]) prime[++cnt]=i,ds[i]=i+1;
for (j=1;j<=cnt&&i*Pi<=n;++j)
{
vis[i*Pi]=1; if (i%Pi) ds[i*Pi]=ds[i]*(Pi+1);
else { ds[i*Pi]=ds[i]*(Pi+1)-Pi*ds[i/Pi]; break; }
}
}
for (i=1;i*i<=n;++i)
{
int t=i*i; for (j=1;j*j+t<=n;++j) if (gcd(i,j)==1) fs[j*j+t]+=i;
}
for (i=1;i<=n;++i) ds[i]%=mod,fs[i]%=mod,inc(ds[i],ds[i-1]),inc(fs[i],fs[i-1]);
}
#undef Pi
inline int Ds(const LL& x)
{
if (x<=N) return ds[x]; if (_ds.count(x)) return _ds[x]; int ret=0;
for (LL l=1,r;l<=x;l=r+1) r=x/(x/l),inc(ret,1LL*Sum(l,r)*((x/r)%mod)%mod); return _ds[x]=ret;
}
inline int Fs(const LL& x)
{
if (x<=N) return fs[x]; if (_fs.count(x)) return _fs[x]; int ret=0; register LL i;
for (i=1;i*i<=x;++i) inc(ret,i*((LL)floor(sqrt(x-i*i))%mod)%mod);
for (i=2;i*i<=x;++i) dec(ret,i*Fs(x/(i*i))%mod); return _fs[x]=ret;
}
int main()
{
scanf("%lld",&n); init(N); for (LL l=1,r;l<=n;l=r+1)
r=n/(n/l),inc(ans,1LL*sub(Fs(r),Fs(l-1))*Ds(n/l)%mod);
return printf("%d",sum(sum(ans,ans),Ds(n))),0;
}
posted @ 2019-04-19 21:00 hl666 阅读(...) 评论(...) 编辑 收藏