[Function](Problem - F - Codeforces)
题意 :
根据式子计算1~n的f(n)的累加和
思路 :
1.n是质数或者1时,f(n) = 1;
2.n是质数的次幂时,f(n) = p^(k / 2);
3.n能分解成多个质因数相乘时,f(n) = f(最小的质因子的次幂)走一二两种情况,
AC代码 :
#include <iostream>
#include <map>
#include <cstring>
#include <algorithm>
#include <math.h>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long LL;
const int N = 1e7 + 10;
int n;
int cnt;
int primes[N];
bool st[N];
LL res = 1;
LL qmi(int a, int k)
{
LL ans = 1;
while (k)
{
if (k & 1)
ans = (LL)ans * a;
k >>= 1;
a = (LL)a * a;
}
return ans;
}
void get_primes()
{
for (int i = 2; i <= n; i++)
{
if (!st[i])
{
primes[cnt++] = i;
res++;
}
for (int j = 0; primes[j] <= n / i; j++)
{
st[i * primes[j]] = true;
if (i % primes[j] == 0)
{
int s = 1;
int t = i;
while (t % primes[j] == 0)
t /= primes[j], s++;
res += (LL)qmi(primes[j], s / 2) * t;
break;
}
res += i;
}
}
}
int main()
{
IOS;
scanf("%d", &n);
get_primes();
cout << res << endl;
return 0;
}