QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#350734 | #8339. Rooted Tree | ckiseki | AC ✓ | 222ms | 159364kb | C++20 | 1.9kb | 2024-03-11 01:50:51 | 2024-03-11 01:50:52 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
#ifdef CKISEKI
#include <experimental/iterator>
#define safe cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
void debug_(auto s, auto ...a) {
cerr << "\e[1;32m(" << s << ") = (";
int f = 0;
(..., (cerr << (f++ ? ", " : "") << a));
cerr << ")\e[0m\n";
}
void orange_(auto s, auto L, auto R) {
cerr << "\e[1;33m[ " << s << " ] = [ ";
using namespace experimental;
copy(L, R, make_ostream_joiner(cerr, ", "));
cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif
using lld = int64_t;
const int mod = 1000000009;
int add(int a, int b) {
return a + b >= mod ? a + b - mod : a + b;
}
int sub(int a, int b) {
return a - b < 0 ? a - b + mod : a - b;
}
int mul(lld a, lld b) {
return static_cast<int>(a * b % mod);
}
int modpow(int e, int p) {
int r = 1;
while (p) {
if (p & 1) r = mul(r, e);
e = mul(e, e);
p >>= 1;
}
return r;
}
int modinv(int x) {
return modpow(x, mod - 2);
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int M, K;
cin >> M >> K;
vector<int> cnt(K + 1), leaf(K + 1);
int ans = 0;
cnt[0] = 1;
for (int i = 1; i <= K; i++) {
cnt[i] = cnt[i - 1] + (M - 1);
}
vector<int> pre(K + 1), invcnt(K + 1);
pre[0] = 1;
for (int i = 1; i <= K; i++)
pre[i] = mul(pre[i - 1], cnt[i]);
int iv = modinv(pre[K]);
for (int i = K; i >= 1; i--) {
invcnt[i] = mul(iv, pre[i - 1]);
iv = mul(iv, cnt[i]);
}
for (int i = 1; i <= K; i++) {
leaf[i] = add(mul(mul(leaf[i - 1], invcnt[i - 1]), M - 1), M);
leaf[i] = add(leaf[i], leaf[i - 1]);
ans = add(add(mul(mul(leaf[i - 1], invcnt[i - 1]), M), M), ans);
}
cout << ans << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3548kb
input:
6 2
output:
18
result:
ok 1 number(s): "18"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
2 6
output:
600000038
result:
ok 1 number(s): "600000038"
Test #3:
score: 0
Accepted
time: 12ms
memory: 12720kb
input:
83 613210
output:
424200026
result:
ok 1 number(s): "424200026"
Test #4:
score: 0
Accepted
time: 139ms
memory: 107912kb
input:
48 6713156
output:
198541581
result:
ok 1 number(s): "198541581"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
1 111
output:
6216
result:
ok 1 number(s): "6216"
Test #6:
score: 0
Accepted
time: 154ms
memory: 117268kb
input:
28 7304152
output:
457266679
result:
ok 1 number(s): "457266679"
Test #7:
score: 0
Accepted
time: 89ms
memory: 67264kb
input:
38 4101162
output:
232117382
result:
ok 1 number(s): "232117382"
Test #8:
score: 0
Accepted
time: 222ms
memory: 158216kb
input:
51 9921154
output:
340670552
result:
ok 1 number(s): "340670552"
Test #9:
score: 0
Accepted
time: 40ms
memory: 31168kb
input:
79 1801157
output:
620550406
result:
ok 1 number(s): "620550406"
Test #10:
score: 0
Accepted
time: 117ms
memory: 87700kb
input:
22 5417157
output:
457449071
result:
ok 1 number(s): "457449071"
Test #11:
score: 0
Accepted
time: 73ms
memory: 53480kb
input:
25 3210162
output:
36368303
result:
ok 1 number(s): "36368303"
Test #12:
score: 0
Accepted
time: 59ms
memory: 48804kb
input:
67 2919160
output:
935195555
result:
ok 1 number(s): "935195555"
Test #13:
score: 0
Accepted
time: 194ms
memory: 137548kb
input:
77 8613163
output:
482832472
result:
ok 1 number(s): "482832472"
Test #14:
score: 0
Accepted
time: 214ms
memory: 159332kb
input:
90 10000000
output:
275581651
result:
ok 1 number(s): "275581651"
Test #15:
score: 0
Accepted
time: 210ms
memory: 159364kb
input:
99 9999999
output:
126087169
result:
ok 1 number(s): "126087169"
Test #16:
score: 0
Accepted
time: 209ms
memory: 159276kb
input:
100 10000000
output:
451590067
result:
ok 1 number(s): "451590067"
Extra Test:
score: 0
Extra Test Passed