B - Function
\(f(n)=\left\{\begin{array}{ll} 1, & n \in\{1\} \bigcup \text { Prime } \\ p f\left(p^{k-2}\right), & n=p^{k}(p \in \text { Prime; } k>1) \\ f\left(p_{1}^{e 1}\right) \prod_{i=2}^{r} p_{i}^{e_{i}}, & n=\prod_{i=1}^{r} p_{i}{ }^{e_{i}}\left(p_{1}<p_{2}<\cdots<p_{r} ; p_{i} \in \text { Prime; } r \geq 2\right) \end{array}\right.\)
求\(\sum_{i=1}^{n} f(i)\)
假设一个合数\(n\),代入第三个式子,可得\(f(n)=p_1f(p_1^{e_1-2})\prod_{i=1}^{r} p_{i}{ }^{e_{i}}\),再将\(p_1^{e_1-2}\)代入第二个式子,可得\(f(n)=p_1^2f(p_1^{e_1-4})\prod_{i=1}^{r} p_{i}{ }^{e_{i}}\),以此类推,最后可推出\(f(n)=p_1^{\frac{e_1}{2}}\prod_{i=1}^{r} p_{i}{ }^{e_{i}}\).
Code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define gcd __gcd
#define endl '\n'
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int maxn = 1e7 + 5;
int prime[maxn], vis[maxn] = {0}, cnt = 0;
ll power(ll a, ll b)
{
ll ans = 1;
while (b)
{
if (b & 1)
{
ans = ans * a % mod;
}
a = a * a % mod;
b >>= 1;
}
return ans;
}
ll Euler(int n)
{
ll res = 1;
for (int i = 2; i <= n; i++)
{
if (!vis[i])
{
prime[cnt++] = i;
res++;
}
for (int j = 0; prime[j] <= n / i; j++)
{
int tmp = prime[j] * i;
vis[tmp] = 1;
int count = 0;
while (tmp % prime[j] == 0)
{
count++;
tmp /= prime[j];
}
res += power(prime[j], count / 2) * tmp;
if (i % prime[j] == 0)
{
break;
}
}
}
return res;
}
signed main()
{
ll n;
cin >> n;
printf("%lld\n", Euler(n));
return 0;
}