QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#72889 | #4843. Infectious Disease | ckiseki | AC ✓ | 2043ms | 169800kb | C++ | 2.5kb | 2023-01-19 21:34:31 | 2023-01-19 21:34:34 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
namespace {
constexpr int mod = 1'000'000'007;
constexpr int maxn = 14'000'000 + 5;
inline constexpr int mul(int64_t a, int64_t b) {
return static_cast<int>(a * b % mod);
}
inline constexpr int add(int a, int b) {
return a + b >= mod ? a + b - mod : a + b;
}
inline constexpr int qpow(int a, int64_t k) {
int r = 1 % mod;
while (k) {
if (k & 1) r = mul(r, a);
k >>= 1; a = mul(a, a);
}
return r;
}
int _inv[maxn], ifac[maxn], fac[maxn];
inline int inv(int a) {
if (a < maxn) return _inv[a];
return qpow(a, mod - 2);
}
inline int comb(int a, int b) {
if (b < 0 || a < b) return 0;
return mul(fac[a], mul(ifac[b], ifac[a - b]));
}
void pre() {
fac[0] = 1;
for (int i = 1; i < maxn; ++i)
fac[i] = mul(fac[i - 1], i);
ifac[maxn - 1] = qpow(fac[maxn - 1], mod - 2);
for (int i = maxn - 2; i >= 0; --i)
ifac[i] = mul(ifac[i + 1], i + 1);
for (int i = 1; i < maxn; ++i)
_inv[i] = mul(ifac[i], fac[i - 1]);
}
int dp[2][maxn];
} // namespace
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
pre();
int n, ans = 0; cin >> n;
int me = 0, he = 1;
dp[me][1] = 1;
int two = 1, three = 1;
for (int i = 0; true; ++i) {
bool alldone = true;
for (int j = 1; j <= two; ++j)
alldone &= dp[me][j] == 0;
if (alldone) break;
memset(dp[he], 0, sizeof(int) * (two * 2 + 1));
for (int j = 0; j <= two; j++) {
int &x = dp[he][min(j * 2, n - three)];
x = add(x, dp[me][j]);
}
swap(me, he);
memset(dp[he], 0, sizeof(int) * (two * 2 + 1));
int c = n - three;
int l = min(three * 2, n - three);
int iv = inv(comb(c, l));
if (two != 16384)
for (int j = 0; j <= two * 2; j++) {
for (int k = 0; k <= j; k++) {
int coef = mul(comb(j, j - k), comb(c - j, l - (j - k)));
dp[he][k] = add(dp[he][k], mul(coef, dp[me][j]));
}
}
for (int j = 0; j <= two * 2; ++j)
dp[he][j] = mul(dp[he][j], iv);
swap(me, he);
three *= 3;
two *= 2;
for (int j = 1; j <= two; j++) {
ans = add(ans, dp[me][j]);
}
}
cout << add(ans, 1) << '\n';
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 181ms
memory: 168488kb
input:
2
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 174ms
memory: 169012kb
input:
114
output:
505208013
result:
ok 1 number(s): "505208013"
Test #3:
score: 0
Accepted
time: 2019ms
memory: 168572kb
input:
14000000
output:
44565093
result:
ok 1 number(s): "44565093"
Test #4:
score: 0
Accepted
time: 2043ms
memory: 169416kb
input:
12345678
output:
123143093
result:
ok 1 number(s): "123143093"
Test #5:
score: 0
Accepted
time: 519ms
memory: 169216kb
input:
776777
output:
764022068
result:
ok 1 number(s): "764022068"
Test #6:
score: 0
Accepted
time: 146ms
memory: 168172kb
input:
3
output:
1
result:
ok 1 number(s): "1"
Test #7:
score: 0
Accepted
time: 144ms
memory: 169596kb
input:
4
output:
666666673
result:
ok 1 number(s): "666666673"
Test #8:
score: 0
Accepted
time: 174ms
memory: 168056kb
input:
5
output:
833333341
result:
ok 1 number(s): "833333341"
Test #9:
score: 0
Accepted
time: 148ms
memory: 168888kb
input:
6
output:
300000004
result:
ok 1 number(s): "300000004"
Test #10:
score: 0
Accepted
time: 167ms
memory: 168956kb
input:
7
output:
533333339
result:
ok 1 number(s): "533333339"
Test #11:
score: 0
Accepted
time: 127ms
memory: 167980kb
input:
8
output:
952380961
result:
ok 1 number(s): "952380961"
Test #12:
score: 0
Accepted
time: 168ms
memory: 169520kb
input:
9
output:
964285723
result:
ok 1 number(s): "964285723"
Test #13:
score: 0
Accepted
time: 168ms
memory: 167728kb
input:
10
output:
416666672
result:
ok 1 number(s): "416666672"
Test #14:
score: 0
Accepted
time: 145ms
memory: 168388kb
input:
26
output:
990086590
result:
ok 1 number(s): "990086590"
Test #15:
score: 0
Accepted
time: 154ms
memory: 167912kb
input:
27
output:
528360342
result:
ok 1 number(s): "528360342"
Test #16:
score: 0
Accepted
time: 134ms
memory: 168036kb
input:
28
output:
273239648
result:
ok 1 number(s): "273239648"
Test #17:
score: 0
Accepted
time: 1705ms
memory: 169800kb
input:
4782967
output:
332194401
result:
ok 1 number(s): "332194401"
Test #18:
score: 0
Accepted
time: 1665ms
memory: 169308kb
input:
4782968
output:
362625027
result:
ok 1 number(s): "362625027"
Test #19:
score: 0
Accepted
time: 1644ms
memory: 168480kb
input:
4782969
output:
971032452
result:
ok 1 number(s): "971032452"
Test #20:
score: 0
Accepted
time: 1643ms
memory: 169088kb
input:
4782970
output:
452836984
result:
ok 1 number(s): "452836984"
Test #21:
score: 0
Accepted
time: 1640ms
memory: 169436kb
input:
4782971
output:
349324970
result:
ok 1 number(s): "349324970"
Test #22:
score: 0
Accepted
time: 1660ms
memory: 169396kb
input:
4782972
output:
46862963
result:
ok 1 number(s): "46862963"
Test #23:
score: 0
Accepted
time: 149ms
memory: 169164kb
input:
114514
output:
972669965
result:
ok 1 number(s): "972669965"
Test #24:
score: 0
Accepted
time: 1667ms
memory: 168452kb
input:
1919810
output:
482430785
result:
ok 1 number(s): "482430785"
Test #25:
score: 0
Accepted
time: 2016ms
memory: 168708kb
input:
8877877
output:
486769120
result:
ok 1 number(s): "486769120"