QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#763978 | #8339. Rooted Tree | ucup-team3548 | AC ✓ | 249ms | 82784kb | C++20 | 3.7kb | 2024-11-19 23:08:45 | 2024-11-19 23:08:46 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T>
T qpow(T a, ll b) {
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
const int P = 1e9 + 9;
struct Mint{
int x;
Mint() : x(0){}
Mint(int _x){
if(_x < 0)_x += P;
if(_x >= P)_x -= P;
x = _x;
}
Mint(ll _x) {
_x %= P;
if(_x < 0)_x += P;
x = _x;
}
int val() const {return x;}
int norm(int a){
if(a >= P) a -= P;
if(a < 0)a += P;
return a;
}
Mint inv() const{
// assert(x != 0);
return qpow(*this, P - 2);
}
Mint &operator+=(const Mint &a){
x += a.x;
if(x >= P)x -= P;
return *this;
}
Mint &operator-=(const Mint &a){
x -= a.x;
if(x < 0)x += P;
return *this;
}
Mint &operator*=(const Mint &a){x = 1ll * x * a.x % P; return *this;}
Mint &operator/=(const Mint &a){return *this *= a.inv();}
Mint &operator++(){x += 1; return *this;}
Mint operator++(int){Mint before(*this); x += 1; return before;}
Mint &operator--(){x -= 1; return *this;}
Mint operator--(int){Mint before(*this); x -= 1; return before;}
friend Mint operator+(const Mint &a, const Mint &b){
Mint res = a;
res += b;
return res;
}
friend Mint operator-(const Mint &a, const Mint &b){
Mint res = a;
res -= b;
return res;
}
friend Mint operator*(const Mint &a, const Mint &b){
Mint res = a;
res *= b;
return res;
}
friend Mint operator/(const Mint &a, const Mint &b){
Mint res = a;
res /= b;
return res;
}
Mint operator+() { return *this; }
Mint operator-() { return Mint(0) -= *this; }
friend bool operator==(const Mint &a, const Mint &b) { return a.x == b.x; }
friend bool operator!=(const Mint &a, const Mint &b) { return a.x != b.x; }
friend bool operator< (const Mint &a, const Mint &b) { return a.x < b.x; }
friend bool operator<=(const Mint &a, const Mint &b) { return a.x <= b.x; }
friend bool operator> (const Mint &a, const Mint &b) { return a.x > b.x; }
friend bool operator>=(const Mint &a, const Mint &b) { return a.x >= b.x; }
friend istream &operator>>(istream &is, Mint &a){
ll b;
cin >> b;
a = Mint(b);
return is;
}
friend ostream &operator<<(ostream &os, const Mint &a){
return os << a.val();
}
};
const int N = 2e5 + 5;
vector<Mint> fac, inv;
void init(){
fac.resize(N);
inv.resize(N);
fac[0] = 1;
for(int i = 1; i < N; ++i)fac[i] = i * fac[i - 1];
inv[N - 1] = fac[N - 1].inv();
for(int i = N - 1; i > 0; --i){
inv[i - 1] = i * inv[i];
}
}
void solve(){
int m, k;
cin >> m >> k;
vector<Mint> pre(k + 1);
pre[0] = 1;
for (int i = 1; i <= k; ++i) {
pre[i] = pre[i - 1] * (1 + i * (m - 1));
}
vector<Mint> inv_pre(k + 1);
inv_pre[k] = pre[k].inv();
for (int i = k; i > 0; --i) {
inv_pre[i - 1] = inv_pre[i] * (1 + i * (m - 1));
}
Mint ans, E;
for (int i = 0; i < k; ++i) {
ans += (E + 1) * m;
E = (i * (m - 1) * E + (E + 1) * m) * inv_pre[i + 1] * pre[i];
}
cout << ans << "\n";
}
int main(void){
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
init();
int t = 1;
// cin >> t;
for (int i = 0; i < t; ++i) {
solve();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 3ms
memory: 4592kb
input:
6 2
output:
18
result:
ok 1 number(s): "18"
Test #2:
score: 0
Accepted
time: 0ms
memory: 4540kb
input:
2 6
output:
600000038
result:
ok 1 number(s): "600000038"
Test #3:
score: 0
Accepted
time: 19ms
memory: 9520kb
input:
83 613210
output:
424200026
result:
ok 1 number(s): "424200026"
Test #4:
score: 0
Accepted
time: 169ms
memory: 57348kb
input:
48 6713156
output:
198541581
result:
ok 1 number(s): "198541581"
Test #5:
score: 0
Accepted
time: 0ms
memory: 4616kb
input:
1 111
output:
6216
result:
ok 1 number(s): "6216"
Test #6:
score: 0
Accepted
time: 180ms
memory: 61896kb
input:
28 7304152
output:
457266679
result:
ok 1 number(s): "457266679"
Test #7:
score: 0
Accepted
time: 101ms
memory: 36836kb
input:
38 4101162
output:
232117382
result:
ok 1 number(s): "232117382"
Test #8:
score: 0
Accepted
time: 243ms
memory: 82264kb
input:
51 9921154
output:
340670552
result:
ok 1 number(s): "340670552"
Test #9:
score: 0
Accepted
time: 50ms
memory: 18836kb
input:
79 1801157
output:
620550406
result:
ok 1 number(s): "620550406"
Test #10:
score: 0
Accepted
time: 131ms
memory: 46996kb
input:
22 5417157
output:
457449071
result:
ok 1 number(s): "457449071"
Test #11:
score: 0
Accepted
time: 79ms
memory: 29868kb
input:
25 3210162
output:
36368303
result:
ok 1 number(s): "36368303"
Test #12:
score: 0
Accepted
time: 75ms
memory: 27572kb
input:
67 2919160
output:
935195555
result:
ok 1 number(s): "935195555"
Test #13:
score: 0
Accepted
time: 217ms
memory: 72092kb
input:
77 8613163
output:
482832472
result:
ok 1 number(s): "482832472"
Test #14:
score: 0
Accepted
time: 245ms
memory: 82748kb
input:
90 10000000
output:
275581651
result:
ok 1 number(s): "275581651"
Test #15:
score: 0
Accepted
time: 249ms
memory: 82764kb
input:
99 9999999
output:
126087169
result:
ok 1 number(s): "126087169"
Test #16:
score: 0
Accepted
time: 246ms
memory: 82784kb
input:
100 10000000
output:
451590067
result:
ok 1 number(s): "451590067"
Extra Test:
score: 0
Extra Test Passed