QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#252646 | #7774. 基础寄术练习题 | hos_lyric | 100 ✓ | 37ms | 8312kb | C++14 | 6.8kb | 2023-11-15 23:47:27 | 2023-11-15 23:47:27 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
////////////////////////////////////////////////////////////////////////////////
// Barrett
struct ModInt {
static unsigned M;
static unsigned long long NEG_INV_M;
static void setM(unsigned m) { M = m; NEG_INV_M = -1ULL / M; }
unsigned x;
ModInt() : x(0U) {}
ModInt(unsigned x_) : x(x_ % M) {}
ModInt(unsigned long long x_) : x(x_ % M) {}
ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
ModInt &operator*=(const ModInt &a) {
const unsigned long long y = static_cast<unsigned long long>(x) * a.x;
const unsigned long long q = static_cast<unsigned long long>((static_cast<unsigned __int128>(NEG_INV_M) * y) >> 64);
const unsigned long long r = y - M * q;
x = r - M * (r >= M);
return *this;
}
ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
ModInt pow(long long e) const {
if (e < 0) return inv().pow(-e);
ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
}
ModInt inv() const {
unsigned a = M, b = x; int y = 0, z = 1;
for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
assert(a == 1U); return ModInt(y);
}
ModInt operator+() const { return *this; }
ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
explicit operator bool() const { return x; }
bool operator==(const ModInt &a) const { return (x == a.x); }
bool operator!=(const ModInt &a) const { return (x != a.x); }
friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
unsigned ModInt::M;
unsigned long long ModInt::NEG_INV_M;
// !!!Use ModInt::setM!!!
////////////////////////////////////////////////////////////////////////////////
using Mint = ModInt;
constexpr int LIM_INV = 10010;
Mint inv[LIM_INV], fac[LIM_INV], invFac[LIM_INV];
void prepare() {
inv[1] = 1;
for (int i = 2; i < LIM_INV; ++i) {
inv[i] = -((Mint::M / i) * inv[Mint::M % i]);
}
fac[0] = invFac[0] = 1;
for (int i = 1; i < LIM_INV; ++i) {
fac[i] = fac[i - 1] * i;
invFac[i] = invFac[i - 1] * inv[i];
}
}
Mint binom(Int n, Int k) {
if (n < 0) {
if (k >= 0) {
return ((k & 1) ? -1 : +1) * binom(-n + k - 1, k);
} else if (n - k >= 0) {
return (((n - k) & 1) ? -1 : +1) * binom(-k - 1, n - k);
} else {
return 0;
}
} else {
if (0 <= k && k <= n) {
assert(n < LIM_INV);
return fac[n] * invFac[k] * invFac[n - k];
} else {
return 0;
}
}
}
/*
insert b
1/(s[1]+b)(s[2]+b)(s[3]+b)...(s[n-1]+b)(s[n]+b)
+ 1/(s[1]+b)(s[2]+b)(s[3]+b)...(s[n-1]+b)(s[n]+b)
+ 1/(s[2])(s[2]+b)(s[3]+b)...(s[n-1]+b)(s[n]+b)
+ 1/(s[2])(s[3])(s[3]+b)...(s[n-1]+b)(s[n]+b)
...
+ 1/(s[2])(s[3])(s[4])...(s[n])(s[n]+b)
= 2/(s[1]+b)(s[2]+b)(s[3]+b)...(s[n-1]+b)(s[n]+b)
+ (1/b)(1/s[2] - 1/(s[2]+b)) 1/(s[3]+b)...(s[n-1]+b)(s[n]+b)
+ 1/(s[2]) (1/b)(1/s[3] - 1/(s[3]+b)) 1/(s[4]+b)...(s[n-1]+b)(s[n]+b)
...
+ 1/(s[2])(s[3])(s[4])...(s[n-1]) (1/b)(1/s[n] - 1/(s[n]+b))
= 2/(s[1]+b)(s[2]+b)(s[3]+b)...(s[n-1]+b)(s[n]+b)
+ (1/b) (1/s[2]...s[n] - 1/(s[2]+b)...(s[n]+b))
f(x;a[1],...,a[n]) := 1/(x+a[1])(x+a[1]+a[2])...(x+a[1]+...+a[n])
g(x;a[1],...,a[n]) := 1/ (x+a[1]+a[2])...(x+a[1]+...+a[n])
f(x;b,a[1],...,a[n]) + f(x;a[1],b,...,a[n]) + ... + f(x;a[1],...,a[n],b)
= (1/(x+b)) f(x+b;a[1],...,a[n])
+ (1/b) (f(x;a[1],...,a[n]) - f(x+b;a[1],...,a[n]))
g(x;b,a[1],...,a[n]) + g(x;a[1],b,...,a[n]) + ... + g(x;a[1],...,a[n],b)
= 2f(x+b;a[1],...,a[n])
+ (1/b) (g(x;a[1],...,a[n]) - g(x+b;a[1],...,a[n]))
f(x;) = 1
g(x;b) = 1
*/
int N, M, K, MO;
Mint f[110][5110], g[110][5110];
int main() {
for (; ~scanf("%d%d%d%d", &N, &M, &K, &MO); ) {
Mint::setM(MO);
prepare();
memset(f, 0, sizeof(f));
memset(g, 0, sizeof(g));
fill(f[0], f[0] + (M * (M + 1) / 2 + 1), 1);
for (int b = M; b >= 1; --b) {
const int s = (b - 1) * b / 2;
for (int n = M - b; n >= 1; --n) for (int x = 0; x <= s; ++x) {
g[n + 1][x] += 2 * f[n][x + b];
g[n + 1][x] += inv[b] * (g[n][x] - g[n][x + b]);
}
for (int x = 0; x <= s; ++x) {
g[1][x] += 1;
}
for (int n = M - b; n >= 0; --n) for (int x = 0; x <= s; ++x) {
f[n + 1][x] += inv[x + b] * f[n][x + b];
f[n + 1][x] += inv[b] * (f[n][x] - f[n][x + b]);
}
}
const Mint ans = (K == 1) ? f[N][0] : g[N][0];
printf("%u\n", ans.x);
}
return 0;
}
詳細信息
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 2ms
memory: 8292kb
input:
9 16 1 327134593
output:
162102742
result:
ok single line: '162102742'
Test #2:
score: 0
Accepted
time: 2ms
memory: 8092kb
input:
11 18 1 834359503
output:
256188485
result:
ok single line: '256188485'
Test #3:
score: 0
Accepted
time: 0ms
memory: 8076kb
input:
18 18 1 614802701
output:
552168146
result:
ok single line: '552168146'
Test #4:
score: 0
Accepted
time: 2ms
memory: 8300kb
input:
7 16 2 861918403
output:
306693876
result:
ok single line: '306693876'
Test #5:
score: 0
Accepted
time: 0ms
memory: 8256kb
input:
11 17 2 617904383
output:
393900291
result:
ok single line: '393900291'
Subtask #2:
score: 25
Accepted
Test #6:
score: 25
Accepted
time: 31ms
memory: 8124kb
input:
60 98 1 715015339
output:
690737273
result:
ok single line: '690737273'
Test #7:
score: 0
Accepted
time: 36ms
memory: 8060kb
input:
96 97 1 507892589
output:
481151247
result:
ok single line: '481151247'
Test #8:
score: 0
Accepted
time: 27ms
memory: 8124kb
input:
90 95 1 621080027
output:
255353202
result:
ok single line: '255353202'
Test #9:
score: 0
Accepted
time: 30ms
memory: 8012kb
input:
85 94 1 297115421
output:
122254364
result:
ok single line: '122254364'
Test #10:
score: 0
Accepted
time: 23ms
memory: 8068kb
input:
81 91 1 460412027
output:
148037986
result:
ok single line: '148037986'
Subtask #3:
score: 15
Accepted
Test #11:
score: 15
Accepted
time: 3ms
memory: 8304kb
input:
29 29 2 545875273
output:
171843225
result:
ok single line: '171843225'
Test #12:
score: 0
Accepted
time: 0ms
memory: 8292kb
input:
29 29 2 342070607
output:
291380196
result:
ok single line: '291380196'
Test #13:
score: 0
Accepted
time: 2ms
memory: 8100kb
input:
30 30 2 293965439
output:
148471965
result:
ok single line: '148471965'
Test #14:
score: 0
Accepted
time: 0ms
memory: 8068kb
input:
30 30 2 528219961
output:
203632962
result:
ok single line: '203632962'
Test #15:
score: 0
Accepted
time: 3ms
memory: 8140kb
input:
30 30 1 202836509
output:
158493990
result:
ok single line: '158493990'
Subtask #4:
score: 10
Accepted
Test #16:
score: 10
Accepted
time: 0ms
memory: 8064kb
input:
27 30 2 360712453
output:
80987914
result:
ok single line: '80987914'
Test #17:
score: 0
Accepted
time: 0ms
memory: 8312kb
input:
26 29 2 377615957
output:
278812897
result:
ok single line: '278812897'
Test #18:
score: 0
Accepted
time: 0ms
memory: 8120kb
input:
22 30 2 163686233
output:
19517828
result:
ok single line: '19517828'
Test #19:
score: 0
Accepted
time: 0ms
memory: 8064kb
input:
20 29 2 785657729
output:
713061509
result:
ok single line: '713061509'
Test #20:
score: 0
Accepted
time: 3ms
memory: 8040kb
input:
24 29 1 374090597
output:
312615700
result:
ok single line: '312615700'
Subtask #5:
score: 15
Accepted
Test #21:
score: 15
Accepted
time: 0ms
memory: 8308kb
input:
29 38 2 909155077
output:
745973305
result:
ok single line: '745973305'
Test #22:
score: 0
Accepted
time: 0ms
memory: 8304kb
input:
40 40 2 1067474879
output:
995503334
result:
ok single line: '995503334'
Test #23:
score: 0
Accepted
time: 0ms
memory: 8076kb
input:
32 37 2 751116719
output:
699924081
result:
ok single line: '699924081'
Test #24:
score: 0
Accepted
time: 0ms
memory: 8060kb
input:
33 37 2 496100951
output:
21741458
result:
ok single line: '21741458'
Test #25:
score: 0
Accepted
time: 3ms
memory: 8292kb
input:
34 38 1 499914887
output:
386116226
result:
ok single line: '386116226'
Subtask #6:
score: 10
Accepted
Test #26:
score: 10
Accepted
time: 8ms
memory: 8064kb
input:
57 66 2 767174999
output:
315351738
result:
ok single line: '315351738'
Test #27:
score: 0
Accepted
time: 10ms
memory: 8140kb
input:
52 69 2 399947623
output:
237685494
result:
ok single line: '237685494'
Test #28:
score: 0
Accepted
time: 8ms
memory: 8064kb
input:
63 64 2 903693961
output:
520250635
result:
ok single line: '520250635'
Test #29:
score: 0
Accepted
time: 7ms
memory: 8132kb
input:
65 70 2 268454909
output:
255864893
result:
ok single line: '255864893'
Test #30:
score: 0
Accepted
time: 7ms
memory: 8124kb
input:
58 68 1 562105223
output:
175445185
result:
ok single line: '175445185'
Subtask #7:
score: 15
Accepted
Test #31:
score: 15
Accepted
time: 32ms
memory: 8064kb
input:
96 96 2 453296971
output:
222864385
result:
ok single line: '222864385'
Test #32:
score: 0
Accepted
time: 25ms
memory: 8064kb
input:
85 96 2 859572601
output:
457416092
result:
ok single line: '457416092'
Test #33:
score: 0
Accepted
time: 29ms
memory: 8088kb
input:
89 94 2 753918677
output:
366789523
result:
ok single line: '366789523'
Test #34:
score: 0
Accepted
time: 28ms
memory: 8064kb
input:
91 92 2 202806031
output:
64270709
result:
ok single line: '64270709'
Test #35:
score: 0
Accepted
time: 37ms
memory: 8140kb
input:
100 100 1 493945957
output:
109570004
result:
ok single line: '109570004'