QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#411030 | #8339. Rooted Tree | AINgray# | AC ✓ | 266ms | 24196kb | C++17 | 4.2kb | 2024-05-14 20:21:24 | 2024-05-14 20:21:24 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 9;
template<long long P>
struct ModInt {
public:
ModInt() : v(0) {}
template<typename T>
ModInt(T x) { v = ((long long) x % P + P) % P; }
long long val() const { return v; }
ModInt &operator++() {
++v;
if (v == P)
v = 0;
return *this;
}
ModInt &operator--() {
if (v == 0)
v = P;
--v;
return *this;
}
ModInt operator++(int) {
ModInt oldVal = *this;
++*this;
return oldVal;
}
ModInt operator--(int) {
ModInt oldVal = *this;
--*this;
return oldVal;
}
ModInt &operator+=(const ModInt &rhs) {
v += rhs.v;
v %= P;
return *this;
}
ModInt &operator-=(const ModInt &rhs) {
v -= rhs.v;
v = (v + P) % P;
return *this;
}
ModInt &operator*=(const ModInt &rhs) {
v *= rhs.v;
v %= P;
return *this;
}
ModInt &operator/=(const ModInt &rhs) { return *this = *this * rhs.inv(); }
ModInt operator+() const { return *this; }
ModInt operator-() const { return ModInt() - *this; }
ModInt pow(long long exp) const {
// assert(exp>=0);
ModInt res = 1;
ModInt base = *this;
while (exp) {
if (exp & 1)
res *= base;
base *= base;
exp >>= 1;
}
return res;
}
ModInt inv() const {
// assert(v);
return pow(P - 2);
}
friend ModInt operator+(const ModInt &lhs, const ModInt &rhs) {
ModInt res = lhs;
res += rhs;
return res;
}
friend ModInt operator-(const ModInt &lhs, const ModInt &rhs) {
ModInt res = lhs;
res -= rhs;
return res;
}
friend ModInt operator*(const ModInt &lhs, const ModInt &rhs) {
ModInt res = lhs;
res *= rhs;
return res;
}
friend ModInt operator/(const ModInt &lhs, const ModInt &rhs) {
ModInt res = lhs;
res /= rhs;
return res;
}
friend bool operator==(const ModInt &lhs, const ModInt &rhs) { return lhs.v == rhs.v; }
friend bool operator!=(const ModInt &lhs, const ModInt &rhs) { return lhs.v != rhs.v; }
friend std::istream &operator>>(std::istream &is, ModInt &aim) {
long long tmp;
is >> tmp;
aim = ModInt(tmp);
return is;
}
friend std::ostream &operator<<(std::ostream &os, const ModInt &aim) { return os << aim.val(); }
private:
long long v;
};
using mint = ModInt<MOD>;
constexpr int P = 1e9 + 9;
namespace Inversion {
static constexpr int B = (1 << 10), T = (1 << 20);
std::array < int, T + 1 > f, p;
std::array < int, T * 3 + 3 > buf;
int *I = buf.begin() + T;
void init() {
for (int i = 1; i <= B; i++) {
int s = 0, d = (i << 10);
for (int j = 1; j <= T; j++) {
if ((s += d) >= P) s -= P;
if (s <= T) {
if (!f[j]) f[j] = i, p[j] = s;
}
else if (s >= P - T) {
if (!f[j]) f[j] = i, p[j] = s - P;
}
else {
int t = (P - T - s - 1) / d;
s += t * d, j += t;
}
}
}
I[1] = f[0] = 1;
for (int i = 2; i <= (T << 1); i++)
I[i] = 1ll * (P - P / i) * I[P % i] % P;
for (int i = -1; i >= -T; i--)
I[i] = P - I[-i];
}
int inv(int x) {
return 1ll * I[p[x >> 10] + (x & 1023) * f[x >> 10]] * f[x >> 10] % P;
}
};
void init(int p) {
Inversion::init();
}
int inv(int x) {
return Inversion::inv(x);
}
void Solve() {
int n, k;
cin >> k >> n;
mint avg_dep = 0;
mint yenum = 1;
mint rem = 0;
while (n--) {
int nxtnum = yenum.val() + k - 1;
rem += avg_dep;
avg_dep = (avg_dep * (yenum - 1) * inv(nxtnum) + (k * (avg_dep + 1) * inv(nxtnum)));
yenum += k - 1;
}
cout << avg_dep * yenum + rem;
}
int main() {
std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
init(P);
Solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 20ms
memory: 24140kb
input:
6 2
output:
18
result:
ok 1 number(s): "18"
Test #2:
score: 0
Accepted
time: 17ms
memory: 24196kb
input:
2 6
output:
600000038
result:
ok 1 number(s): "600000038"
Test #3:
score: 0
Accepted
time: 36ms
memory: 24132kb
input:
83 613210
output:
424200026
result:
ok 1 number(s): "424200026"
Test #4:
score: 0
Accepted
time: 175ms
memory: 24052kb
input:
48 6713156
output:
198541581
result:
ok 1 number(s): "198541581"
Test #5:
score: 0
Accepted
time: 20ms
memory: 24052kb
input:
1 111
output:
6216
result:
ok 1 number(s): "6216"
Test #6:
score: 0
Accepted
time: 184ms
memory: 24124kb
input:
28 7304152
output:
457266679
result:
ok 1 number(s): "457266679"
Test #7:
score: 0
Accepted
time: 119ms
memory: 24028kb
input:
38 4101162
output:
232117382
result:
ok 1 number(s): "232117382"
Test #8:
score: 0
Accepted
time: 266ms
memory: 24124kb
input:
51 9921154
output:
340670552
result:
ok 1 number(s): "340670552"
Test #9:
score: 0
Accepted
time: 66ms
memory: 24112kb
input:
79 1801157
output:
620550406
result:
ok 1 number(s): "620550406"
Test #10:
score: 0
Accepted
time: 145ms
memory: 24152kb
input:
22 5417157
output:
457449071
result:
ok 1 number(s): "457449071"
Test #11:
score: 0
Accepted
time: 89ms
memory: 24044kb
input:
25 3210162
output:
36368303
result:
ok 1 number(s): "36368303"
Test #12:
score: 0
Accepted
time: 89ms
memory: 24044kb
input:
67 2919160
output:
935195555
result:
ok 1 number(s): "935195555"
Test #13:
score: 0
Accepted
time: 232ms
memory: 24028kb
input:
77 8613163
output:
482832472
result:
ok 1 number(s): "482832472"
Test #14:
score: 0
Accepted
time: 256ms
memory: 24188kb
input:
90 10000000
output:
275581651
result:
ok 1 number(s): "275581651"
Test #15:
score: 0
Accepted
time: 264ms
memory: 24100kb
input:
99 9999999
output:
126087169
result:
ok 1 number(s): "126087169"
Test #16:
score: 0
Accepted
time: 263ms
memory: 24120kb
input:
100 10000000
output:
451590067
result:
ok 1 number(s): "451590067"
Extra Test:
score: 0
Extra Test Passed