QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#619449 | #8761. 另一个计数问题 | Komorebie | WA | 2ms | 11940kb | C++17 | 2.8kb | 2024-10-07 14:14:24 | 2024-10-07 14:14:25 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define PII pair<int, int>
#define pi acos(-1.0)
constexpr int N = 1e6 + 10, mod = 1e9 + 7;
ll n, sq;
ll primes[N], st[N], sp1[N], sp2[N], cnt;
ll w[N], g1[N], g2[N], tot, id1[N], id2[N];
ll qpow(ll aa, int bb = mod - 2)
{
ll res = 1;
while (bb) {
if (bb & 1) res = (res * aa) % mod;
aa = (aa * aa) % mod;
bb >>= 1;
}
return res;
}
ll inv2 = qpow(2), inv6 = qpow(6);
void sieve()
{
for (int i = 2; i <= sq; i++) {
if (!st[i]) {
st[i] = primes[++cnt] = i;
sp1[cnt] = (sp1[cnt - 1] + i) % mod;
sp2[cnt] = (sp2[cnt - 1] + 1ll * i * i % mod) % mod;
}
for (int j = 1; j <= cnt && primes[j] * i < N; j++) {
st[primes[j] * i] = primes[j];
if (i % primes[j] == 0) break;
}
}
}
ll s1(ll x)
{
x %= mod;
return ((x * (x + 1) % mod) * inv2 % mod - 1 + mod) % mod;
}
ll s2(ll x)
{
x %= mod;
return (((x * (x + 1) % mod) * (2 * x + 1) % mod) * inv6 % mod - 1 + mod) % mod;
}
ll S(ll i, ll j)
{
if (primes[j] >= i) return 0;
ll p = i <= sq ? id1[i] : id2[n / i];
ll ans = ((g2[p] - g1[p] + mod) % mod - (sp2[j] - sp1[j] + mod) % mod + mod) % mod;
for (int k = j + 1; primes[k] * primes[k] <= i && k <= cnt; k++) {
ll pe = primes[k];
for (int e = 1; pe <= i; e++, pe = pe * primes[k]) {
ll x = pe % mod;
ans = (ans + x * (x - 1) % mod * ((S(i / pe, k) + (e > 1)) % mod) % mod) % mod;
}
}
return ans;
}
int main()
{
ios::sync_with_stdio(false), cin.tie(nullptr);
cin >> n;
sq = sqrt(n);
sieve();
for (ll l = 1, r; l <= n; l = r + 1) { // tot 记录所有的整除分块
r = min(n, n / (n / l));
w[++tot] = n / l;
g1[tot] = s1(w[tot]);
g2[tot] = s2(w[tot]);
if (w[tot] <= sq)
id1[w[tot]] = tot;
else
id2[n / w[tot]] = tot;
}
for (int j = 1; j <= cnt; j++) // g(n, j)
{
for (int i = 1; i <= tot && primes[j] * primes[j] <= w[i]; i++) {
ll tmp = w[i] / primes[j];
ll p = tmp <= sq ? id1[tmp] : id2[n / tmp];
g1[i] = (g1[i] - primes[j] * (g1[p] - sp1[j - 1] + mod) % mod + mod) % mod;
g2[i] = (g2[i] - (primes[j] * primes[j] % mod) * (g2[p] - sp2[j - 1] + mod) % mod + mod) % mod;
}
}
ll res1 = (g1[1] - g1[2] + mod) % mod;
ll res2 = (g2[1] - g2[2] + mod) % mod;
ll sum = s1(n);
ll ans = ((sum * sum % mod - s2(n) + mod) % mod) * inv2 % mod;
ans = (ans - res1 * sum % mod + mod + res2) % mod;
ans = (ans + ((res1 * res1 % mod - res2 + mod) % mod) * inv2 % mod) % mod;
cout << ans << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 11752kb
input:
4
output:
8
result:
ok 1 number(s): "8"
Test #2:
score: 0
Accepted
time: 2ms
memory: 11900kb
input:
5
output:
8
result:
ok 1 number(s): "8"
Test #3:
score: 0
Accepted
time: 0ms
memory: 11812kb
input:
6
output:
80
result:
ok 1 number(s): "80"
Test #4:
score: 0
Accepted
time: 0ms
memory: 11848kb
input:
7
output:
80
result:
ok 1 number(s): "80"
Test #5:
score: 0
Accepted
time: 2ms
memory: 11912kb
input:
8
output:
200
result:
ok 1 number(s): "200"
Test #6:
score: 0
Accepted
time: 0ms
memory: 11752kb
input:
9
output:
407
result:
ok 1 number(s): "407"
Test #7:
score: 0
Accepted
time: 2ms
memory: 11912kb
input:
10
output:
937
result:
ok 1 number(s): "937"
Test #8:
score: 0
Accepted
time: 0ms
memory: 11940kb
input:
79
output:
3224298
result:
ok 1 number(s): "3224298"
Test #9:
score: 0
Accepted
time: 2ms
memory: 11880kb
input:
123
output:
21077222
result:
ok 1 number(s): "21077222"
Test #10:
score: 0
Accepted
time: 0ms
memory: 11880kb
input:
158
output:
57411585
result:
ok 1 number(s): "57411585"
Test #11:
score: 0
Accepted
time: 0ms
memory: 11880kb
input:
285
output:
605750829
result:
ok 1 number(s): "605750829"
Test #12:
score: -100
Wrong Answer
time: 0ms
memory: 11876kb
input:
355
output:
508107466
result:
wrong answer 1st numbers differ - expected: '509863120', found: '508107466'