QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#666807#8339. Rooted TreetilennAC ✓196ms159616kbC++142.9kb2024-10-22 20:01:002024-10-22 20:01:11

Judging History

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

  • [2024-10-22 20:01:11]
  • 评测
  • 测评结果:AC
  • 用时:196ms
  • 内存:159616kb
  • [2024-10-22 20:01:00]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
typedef long long LL;

template<class T>
T qmi(T a,LL k){
    T res = 1;
    while(k){
        if(k & 1)res *= a;
        a *= a;
        k >>= 1;
    }
    return res;
}

struct MInt{
    int v;
    const static int Mod;
    MInt() : v() {}
    MInt(LL _v) : v(norm(_v % Mod)) {}
    static int getMod(){
        return Mod;
    }
    int norm(int x) const {
        if(x < 0){
            x += Mod;
        }else if(x >= Mod){
            x -= Mod;
        }
        return x;
    }
    int val() const {return v;}
    MInt operator-() const {
        MInt res;
        res.v = norm(Mod - v);
        return res;
    }
    MInt inv() const {
        return qmi(*this,Mod - 2);
    }
    MInt &operator*=(MInt rhs) & {
        v = 1LL * v * rhs.v % Mod;
        return *this;
    }
    MInt &operator+=(MInt rhs) & {
        v = norm(v + rhs.v);
        return *this;
    }
    MInt &operator-=(MInt rhs) & {
        v = norm(v - rhs.v);
        return *this;
    }
    MInt &operator/=(MInt rhs) & {
        return *this *= rhs.inv();
    }
    friend MInt operator*(MInt lhs, MInt rhs) {
        return 1LL * lhs.val() * rhs.val();
    }
    friend MInt operator+(MInt lhs, MInt rhs) {
        return lhs.val() + rhs.val();
    }
    friend MInt operator-(MInt lhs, MInt rhs) {
        return lhs.val() - rhs.val();
    }
    friend MInt operator/(MInt lhs, MInt rhs) {
        return 1LL * lhs.val() * rhs.inv().val();
    }
    friend std::istream &operator>>(std::istream &is, MInt &a) {
        LL v;
        is >> v;
        a = MInt(v);
        return is;
    }
    friend std::ostream &operator<<(std::ostream &os, const MInt &a) {
        return os << a.val();
    }
    friend bool operator<(MInt lhs,MInt rhs) {
        return lhs.val() < rhs.val();
    }
    friend bool operator==(MInt lhs, MInt rhs) {
        return lhs.val() == rhs.val();
    }
    friend bool operator!=(MInt lhs, MInt rhs) {
        return lhs.val() != rhs.val();
    }
};
const int MInt::Mod = 1e9 + 9;
using Z = MInt;

void solve(){
    int m,k;
    cin >> m >> k;
    vector<Z> dp(k + 1);
    vector<Z> pre(k + 1),suf(k + 1);
    vector<Z> inv(k + 1);

    for(int i = 0;i <= k;i++)pre[i] = suf[i] = 1LL + i * (m - 1);
    for(int i = 1;i <= k;i++)pre[i] *= pre[i - 1];
    for(int i = k - 1;i >= 1;i--)suf[i] *= suf[i + 1];
    Z tmp = pre[k].inv();
    inv[0] = 1;
    inv[k] = tmp * pre[k - 1];
    for(int i = 1;i <= k - 1;i++)inv[i] = tmp * pre[i - 1] * suf[i + 1];

    Z res = 0;
    for(int i = 1;i <= k;i++){
        res += dp[i - 1];
        dp[i] = dp[i - 1] + Z(m) * inv[i];
    }
    res += dp[k] * (1 + k * (m - 1));
    cout << res << endl; 

}   

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL),cout.tie(NULL);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

6 2

output:

18

result:

ok 1 number(s): "18"

Test #2:

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

input:

2 6

output:

600000038

result:

ok 1 number(s): "600000038"

Test #3:

score: 0
Accepted
time: 11ms
memory: 12736kb

input:

83 613210

output:

424200026

result:

ok 1 number(s): "424200026"

Test #4:

score: 0
Accepted
time: 126ms
memory: 108160kb

input:

48 6713156

output:

198541581

result:

ok 1 number(s): "198541581"

Test #5:

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

input:

1 111

output:

6216

result:

ok 1 number(s): "6216"

Test #6:

score: 0
Accepted
time: 145ms
memory: 117328kb

input:

28 7304152

output:

457266679

result:

ok 1 number(s): "457266679"

Test #7:

score: 0
Accepted
time: 77ms
memory: 67144kb

input:

38 4101162

output:

232117382

result:

ok 1 number(s): "232117382"

Test #8:

score: 0
Accepted
time: 196ms
memory: 158344kb

input:

51 9921154

output:

340670552

result:

ok 1 number(s): "340670552"

Test #9:

score: 0
Accepted
time: 34ms
memory: 31224kb

input:

79 1801157

output:

620550406

result:

ok 1 number(s): "620550406"

Test #10:

score: 0
Accepted
time: 105ms
memory: 87788kb

input:

22 5417157

output:

457449071

result:

ok 1 number(s): "457449071"

Test #11:

score: 0
Accepted
time: 58ms
memory: 53368kb

input:

25 3210162

output:

36368303

result:

ok 1 number(s): "36368303"

Test #12:

score: 0
Accepted
time: 62ms
memory: 48752kb

input:

67 2919160

output:

935195555

result:

ok 1 number(s): "935195555"

Test #13:

score: 0
Accepted
time: 162ms
memory: 137700kb

input:

77 8613163

output:

482832472

result:

ok 1 number(s): "482832472"

Test #14:

score: 0
Accepted
time: 195ms
memory: 159532kb

input:

90 10000000

output:

275581651

result:

ok 1 number(s): "275581651"

Test #15:

score: 0
Accepted
time: 190ms
memory: 159516kb

input:

99 9999999

output:

126087169

result:

ok 1 number(s): "126087169"

Test #16:

score: 0
Accepted
time: 195ms
memory: 159616kb

input:

100 10000000

output:

451590067

result:

ok 1 number(s): "451590067"

Extra Test:

score: 0
Extra Test Passed