计算
∑
i
=
1
n
∑
j
=
1
i
[
g
c
d
(
i
,
j
)
=
1
]
f
(
j
)
\sum_{i=1}^{n}\sum_{j=1}^{i}[gcd(i,j)=1]f(j)
i=1∑nj=1∑i[gcd(i,j)=1]f(j)
其中
f
(
x
)
f(x)
f(x)是
x
x
x的数位和
莫比乌斯反演得到
∑
i
=
1
n
∑
j
=
1
i
f
(
j
)
∑
d
∣
g
c
d
(
i
,
j
)
μ
(
d
)
\sum_{i=1}^{n}\sum_{j=1}^{i}f(j)\sum_{d|gcd(i,j)}\mu(d)
i=1∑nj=1∑if(j)d∣gcd(i,j)∑μ(d)
优先枚举因子
d
d
d
∑
d
=
1
n
μ
(
d
)
∑
i
=
1
⌊
n
d
⌋
∑
j
=
i
⌊
n
d
⌋
f
(
i
d
)
=
∑
d
=
1
n
μ
(
d
)
∑
i
=
1
⌊
n
d
⌋
f
(
i
d
)
∑
j
=
i
⌊
n
d
⌋
1
=
∑
d
=
1
n
μ
(
d
)
∑
i
=
1
⌊
n
d
⌋
f
(
i
d
)
(
⌊
n
d
⌋
−
i
+
1
)
\sum_{d=1}^{n}\mu(d)\sum_{i=1}^{\lfloor \frac{n}{d} \rfloor}\sum_{j=i}^{\lfloor \frac{n}{d} \rfloor}f(id)=\sum_{d=1}^{n}\mu(d)\sum_{i=1}^{\lfloor \frac{n}{d} \rfloor}f(id)\sum_{j=i}^{\lfloor \frac{n}{d} \rfloor}1=\sum_{d=1}^{n}\mu(d)\sum_{i=1}^{\lfloor \frac{n}{d} \rfloor}f(id)(\lfloor \frac{n}{d} \rfloor-i+1)
d=1∑nμ(d)i=1∑⌊dn⌋j=i∑⌊dn⌋f(id)=d=1∑nμ(d)i=1∑⌊dn⌋f(id)j=i∑⌊dn⌋1=d=1∑nμ(d)i=1∑⌊dn⌋f(id)(⌊dn⌋−i+1)
枚举组合项
T
=
i
d
T=id
T=id
∑
T
=
1
n
f
(
T
)
∑
d
∣
T
μ
(
d
)
(
⌊
n
d
⌋
−
T
d
+
1
)
\sum_{T=1}^{n}f(T)\sum_{d|T}\mu(d)(\lfloor \frac{n}{d} \rfloor-\frac{T}{d}+1)
T=1∑nf(T)d∣T∑μ(d)(⌊dn⌋−dT+1)
预处理
f
,
μ
f,\mu
f,μ计算,时间复杂度
O
(
n
n
)
O(n\sqrt{n})
O(nn)
// #include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using ll=long long;
const int N=1e5+5,inf=0x3fffffff;
const long long INF=0x3fffffffffffffff,mod=1e9+7;
int prime[N],mu[N],cnt,f[N];
bitset<N>nt;
int getval(int x)
{
int ret=0;
while(x)
{
ret+=x%10;
x/=10;
}
return ret;
}
void make_prime()
{
mu[1]=1; f[1]=1;
for(int i=2;i<N;i++)
{
if(!nt[i]) prime[++cnt]=i,mu[i]=-1;
for(int j=1;j<=cnt&&i*prime[j]<N;j++)
{
nt[i*prime[j]]=true;
if(i%prime[j]==0)
{
mu[i*prime[j]]=0;
break;
}
else mu[i*prime[j]]=mu[i]*mu[prime[j]];
}
f[i]=getval(i);
}
}
int n;
int main()
{
#ifdef stdjudge
freopen("in.txt","r",stdin);
#endif
make_prime(); cin>>n;
ll ans=0;
for(int i=1;i<=n;i++)
{
ll del=0,limit=sqrt(i);
for(int j=1;j<=limit;j++)
{
if(i%j) continue;
del+=mu[j]*(n/j-i/j+1);
if(j*j!=i) del+=mu[i/j]*(n/(i/j)-i/(i/j)+1);
}
// cout<
ans+=f[i]*del;
}
cout<<ans;
return 0;
}