QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#515634#8339. Rooted TreeyaoyTL 1156ms110632kbC++201.3kb2024-08-11 19:53:452024-08-11 19:53:46

Judging History

你现在查看的是最新测评结果

  • [2024-08-11 19:53:46]
  • 评测
  • 测评结果:TL
  • 用时:1156ms
  • 内存:110632kb
  • [2024-08-11 19:53:45]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

constexpr int P = 1E9 + 9;
constexpr int N = 1E7 + 1;
int pre[N], inv[N], E[N];

int norm(int x) {
    if (x >= P) {
        x -= P;
    } else if (x < 0) {
        x += P;
    }
    return x;
}

int power(int x, int exp) {
    int ans = 1;
    x = norm(x);
    while (exp > 0) {
        if (exp % 2) {
            ans = 1LL * ans * x % P;
        }
        x = 1LL * x * x % P;
        exp /= 2;
    }
    return ans;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int M, K;
    std::cin >> M >> K;

    inv[0] = pre[0] = 1;
    for (int i = 1; i < N; i++) {
        pre[i] = 1LL * pre[i - 1] * ((M - 1LL) * i % P + 1) % P;
    }

    int suf = power(pre[N - 1], P - 2);
    for (int i = N - 1; i >= 1; i--) {
        inv[i] = 1LL * suf * pre[i - 1] % P;
        suf = 1LL * suf * ((M - 1LL) * i % P + 1) % P;
    }

    int ans = 0;
    E[0] = 0;
    for (int i = 1; i <= K; i++) {
        assert(inv[i - 1] == power((M - 1LL) * (i - 1) % P + 1, P - 2));
        int E_dep = 1LL * E[i - 1] * inv[i - 1] % P;
        ans = norm(ans + (E_dep + 1LL) * M % P);
        E[i] = norm(norm(E[i - 1] + E_dep * (M - 1LL) % P) + M);
    }
    std::cout << ans << "\n";

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 96ms
memory: 83696kb

input:

6 2

output:

18

result:

ok 1 number(s): "18"

Test #2:

score: 0
Accepted
time: 100ms
memory: 82496kb

input:

2 6

output:

600000038

result:

ok 1 number(s): "600000038"

Test #3:

score: 0
Accepted
time: 177ms
memory: 85992kb

input:

83 613210

output:

424200026

result:

ok 1 number(s): "424200026"

Test #4:

score: 0
Accepted
time: 1067ms
memory: 110632kb

input:

48 6713156

output:

198541581

result:

ok 1 number(s): "198541581"

Test #5:

score: 0
Accepted
time: 96ms
memory: 83212kb

input:

1 111

output:

6216

result:

ok 1 number(s): "6216"

Test #6:

score: 0
Accepted
time: 1156ms
memory: 110504kb

input:

28 7304152

output:

457266679

result:

ok 1 number(s): "457266679"

Test #7:

score: 0
Accepted
time: 677ms
memory: 100316kb

input:

38 4101162

output:

232117382

result:

ok 1 number(s): "232117382"

Test #8:

score: -100
Time Limit Exceeded

input:

51 9921154

output:

340670552

result: