QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#411019#8339. Rooted TreeErrormakerTL 1488ms6824kbC++173.5kb2024-05-14 20:12:402024-05-14 20:12:41

Judging History

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

  • [2024-05-14 20:12:41]
  • 评测
  • 测评结果:TL
  • 用时:1488ms
  • 内存:6824kb
  • [2024-05-14 20:12:40]
  • 提交

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>;
mint fact[200005];
mint infact[200005];

void init() {
    infact[0] = fact[0] = 1;
    for (int i = 1; i <= 200002; i++)
        fact[i] = fact[i - 1] * i, infact[i] = infact[i - 1] / i;
}

mint C(int a, int b) {
    if (a < b || a < 0 || b < 0) return 0;
    return fact[a] * infact[b] * infact[a - b];
}

void Solve() {
    int n, k;
    cin >> k >> n;

    mint avg_dep = 0;
    mint yenum = 1;
    mint rem = 0;

    while (n--) {

        mint nxtnum = yenum + k - 1;
        rem += avg_dep;
        avg_dep = (avg_dep * (yenum - 1) / nxtnum + (k * (avg_dep + 1) / nxtnum));

        yenum += k - 1;


    }
    cout << avg_dep * yenum + rem;

}

int main() {
    std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    Solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 6824kb

input:

6 2

output:

18

result:

ok 1 number(s): "18"

Test #2:

score: 0
Accepted
time: 0ms
memory: 6820kb

input:

2 6

output:

600000038

result:

ok 1 number(s): "600000038"

Test #3:

score: 0
Accepted
time: 138ms
memory: 6672kb

input:

83 613210

output:

424200026

result:

ok 1 number(s): "424200026"

Test #4:

score: 0
Accepted
time: 1488ms
memory: 6768kb

input:

48 6713156

output:

198541581

result:

ok 1 number(s): "198541581"

Test #5:

score: 0
Accepted
time: 0ms
memory: 6760kb

input:

1 111

output:

6216

result:

ok 1 number(s): "6216"

Test #6:

score: -100
Time Limit Exceeded

input:

28 7304152

output:


result: