标签:大于 现在 math bre mod return href 一个 fine
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int e = 1e6 + 5, mod = 1e9 + 7;
ll g1[e], g2[e], n, s1[e], s2[e], a[e];
bool bo[e];
int cnt, p[e], tot, id1[e], id2[e], s, inv6;
inline int plu(int x, int y)
{
(x += y) >= mod && (x -= mod);
return x;
}
inline int sub(int x, int y)
{
(x -= y) < 0 && (x += mod);
return x;
}
inline int ksm(int x, int y)
{
int res = 1;
while (y)
{
if (y & 1) res = (ll)res * x % mod;
y >>= 1;
x = (ll)x * x % mod;
}
return res;
}
inline int calc1(ll x)
{
x %= mod;
return (ll)x * (x + 1) / 2 % mod;
}
inline int calc2(ll x)
{
x %= mod;
return (ll)x * (x + 1) % mod * (2 * x + 1) % mod * inv6 % mod;
}
inline void init()
{
inv6 = ksm(6, mod - 2); s = sqrt(n);
ll i, j;
for (i = 1; i <= n; i = j + 1)
{
a[++tot] = n / i;
j = n / a[tot];
g1[tot] = sub(calc1(a[tot]), 1);
g2[tot] = sub(calc2(a[tot]), 1);
if (a[tot] <= s) id1[a[tot]] = tot;
else id2[n / a[tot]] = tot;
}
}
inline void sieve()
{
int i, j;
for (i = 2; i <= s; i++)
{
if (!bo[i])
{
p[++cnt] = i;
s1[cnt] = plu(s1[cnt - 1], i);
s2[cnt] = plu(s2[cnt - 1], (ll)i * i % mod);
}
for (j = 1; j <= cnt && i * p[j] <= s; j++)
{
bo[i * p[j]] = 1;
if (i % p[j] == 0) break;
}
}
}
inline void solve_g()
{
int i, j;
for (i = 1; i <= cnt; i++)
{
for (j = 1; j <= tot; j++)
if ((ll)p[i] * p[i] <= a[j])
{
ll x = a[j], y = x / p[i];
int k = y <= s ? id1[y] : id2[n / y], z = x <= s ? id1[x] : id2[n / x];
g1[z] = sub(g1[z], (ll)p[i] * sub(g1[k], s1[i - 1]) % mod);
g2[z] = sub(g2[z], (ll)p[i] * p[i] % mod * sub(g2[k], s2[i - 1]) % mod);
}
}
}
inline int dfs(ll x, int y)
{
if (p[y] >= x) return 0;
int i, j, k = x <= s ? id1[x] : id2[n / x];
int res = sub(sub(g2[k], g1[k]), sub(s2[y], s1[y]));
ll pj;
for (i = y + 1; i <= cnt && (ll)p[i] * p[i] <= x; i++)
for (j = 1, pj = p[i]; pj <= x; j++, pj = pj * p[i])
{
int f = pj % mod;
f = (ll)f * sub(f, 1) % mod;
res = plu(res, (ll)f * plu(dfs(x / pj, i), j != 1) % mod);
}
return res;
}
int main()
{
cin >> n; init(); sieve(); solve_g();
cout << plu(dfs(n, 0), 1) << endl;
return 0;
}
标签:大于 现在 math bre mod return href 一个 fine
原文地址:https://www.cnblogs.com/cyf32768/p/12196141.html