GDCPC2021热身 - Calculation(数学)
题目
Consider n's prime factorization is \(n=p_1^{a_1}p_2^{a_2}...p_k^{a_k}\), and \(\lambda(n)=(-1)^{a_1+a_2+...+a_k}\).
Calculate the value of \(\sum_{i=1}^{n}{\sum_{j=1}^{n}{\sum_{k=1}^{n}{\lambda(i)*\lambda(j)*[i|j \ and \ j|k]}}}\), mod 998244353.
题解
交换一下累加次序可得
\[\sum_{j=1}^{n}{\sum_{k=1}^{n}{\sum_{i=1}^{n}{\lambda(i)*\lambda(j)*[i|j ]*[j|k]}}}
\]
重新排列一下各项
\[\sum_{j=1}^{n}{\sum_{k=1}^{n}{\lambda(j)*[j|k]*(\sum_{i=1}^{n}{\lambda(i)*[i|j]})}}
\]
令括号内的式子为函数g
\[g(j)=\sum_{i=1}^{n}{\lambda(i)*[i|j]}=\sum\limits_{i|j}{\lambda(i)}
\]
可得答案即为
\[\sum\limits_{j=1}^{n}{\lfloor\frac{n}{j}\rfloor*\lambda(j)*g(j)}
\]
观察函数g,可以发现其就是求\(j\)的关于\(\lambda\)的约数和。约数是成对出现的,总约数个数为\(\prod\limits_{i=1}^{k}{(a_i+1)}\)。只有\(j\)的约数个数为奇数时,函数g的值才为1,否则互相抵消结果为0。当约数个数为奇数时,说明\(j\)是完全平方数。
可得答案即为
\[\sum\limits_{j=1}^{\lfloor\sqrt{n}\rfloor}{\lfloor\frac{n}{j^2}\rfloor}
\]
#include<iostream>
using namespace std;
const int N = 1e5 + 10;
const int M = 998244353;
typedef long long ll;
int main() {
ll n;
ll ans = 0;
cin >> n;
for(ll i = 1; i * i <= n; i++) {
ans += n / (i * i);
ans %= M;
}
cout << ans << endl;
}