质数的0次方和,也就是质数个数。
已通过wolfram alpha验证。
int mod;
inline ll add_mod(ll x, ll y) {
return (x + y >= mod) ? (x + y - mod) : (x + y);
}
inline ll sub_mod(ll x, ll y) {
return (x < y) ? (x - y + mod) : (x - y);
}
inline ll sum(ll n) {
n %= mod;
return n;
}
const int MAXN = 1e6 + 5;
ll ssum[MAXN];
ll lsum[MAXN];
bool mark[MAXN];
ll prime_cnt(ll n) {
const int v = sqrt(n);
ssum[0] = 0;
lsum[0] = 0;
memset(mark, 0, sizeof(mark[0]) * (v + 1));
for(ll i = 1; i <= v; ++i) {
ssum[i] = sum(i) - 1;
lsum[i] = sum(n / i) - 1;
}
for(ll p = 2; p <= v; ++p) {
if(ssum[p] == ssum[p - 1])
continue;
ll psum = ssum[p - 1];
ll q = p * p;
ll ed = min((ll)v, n / q);
ll delta1 = (p & 1) + 1;
for(ll i = 1; i <= ed; i += delta1) {
if(!mark[i]) {
ll d = i * p;
ll tmp = (d <= v) ? lsum[d] : ssum[n / d];
tmp = sub_mod(tmp, psum);
lsum[i] = sub_mod(lsum[i], tmp);
}
}
ll delta2 = p * delta1;
for(ll i = q; i <= ed; i += delta2)
mark[i] = 1;
for(ll i = v; i >= q; --i) {
ll tmp = ssum[i / p];
tmp = sub_mod(tmp, psum);
ssum[i] = sub_mod(ssum[i], tmp);
}
}
return lsum[1];
}
质数的1次方和,也就是质数求和。
已通过wolfram alpha验证。
已通过HDU的2020CCPC的1002验证。
int mod;
inline ll add_mod(ll x, ll y) {
return (x + y >= mod) ? (x + y - mod) : (x + y);
}
inline ll sub_mod(ll x, ll y) {
return (x < y) ? (x - y + mod) : (x - y);
}
inline ll mul_mod(ll x, ll y) {
return x * y % mod;
}
inline ll sum(ll n) {
n %= mod;
return (n * (n + 1)) / 2 % mod;
}
const int MAXN = 1e6 + 5;
ll ssum[MAXN];
ll lsum[MAXN];
bool mark[MAXN];
ll prime_sum(ll n) {
const int v = sqrt(n);
ssum[0] = 0;
lsum[0] = 0;
memset(mark, 0, sizeof(mark[0]) * (v + 1));
for(ll i = 1; i <= v; ++i) {
ssum[i] = sum(i) - 1;
lsum[i] = sum(n / i) - 1;
}
for(ll p = 2; p <= v; ++p) {
if(ssum[p] == ssum[p - 1])
continue;
ll psum = ssum[p - 1];
ll q = p * p;
ll ed = min((ll)v, n / q);
ll delta1 = (p & 1) + 1;
for(ll i = 1; i <= ed; i += delta1) {
if(!mark[i]) {
ll d = i * p;
ll tmp = (d <= v) ? lsum[d] : ssum[n / d];
tmp = sub_mod(tmp, psum);
tmp = mul_mod(tmp, p);
lsum[i] = sub_mod(lsum[i], tmp);
}
}
ll delta2 = p * delta1;
for(ll i = q; i <= ed; i += delta2)
mark[i] = 1;
for(ll i = v; i >= q; --i) {
ll tmp = ssum[i / p];
tmp = sub_mod(tmp, psum);
tmp = mul_mod(tmp, p);
ssum[i] = sub_mod(ssum[i], tmp);
}
}
return lsum[1];
}
低次的求和,用拉格朗日插值求出来,然后再改动一下tmp = mul_mod(tmp, p);
这里。