QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#314382 | #8171. Cola | ucup-team2303# | AC ✓ | 320ms | 315760kb | C++17 | 4.1kb | 2024-01-25 16:42:41 | 2024-01-25 16:42:41 |
Judging History
answer
#include <bits/stdc++.h>
using u32 = unsigned;
using i64 = long long;
using u64 = unsigned long long;
template<class T>constexpr T qpw(T a, u64 b, T r = 1) {
for (; b; b >>= 1, a *= a) {
b & 1 ? r *= a : r;
}
return r;
}
template<u32 P>constexpr u32 dil(u32 x) {
return x >> 31 ? x + P : x;
}
template<u32 P>constexpr u32 sf_md(i64 x) {
return dil<P>(x % P);
}
template<u32 P>struct mint {
u32 x;
mint() = default;
constexpr mint(u32 y): x(y) {}
static constexpr mint get(i64 x) {
return sf_md<P>(x);
}
explicit constexpr operator u32() {
return x;
}
constexpr mint operator - ()const {
return dil<P>(-x);
}
constexpr mint inv()const {
return qpw(*this, P - 2);
}
friend constexpr mint operator + (mint lhs, mint rhs) {
return dil<P>(lhs.x + rhs.x - P);
}
friend constexpr mint operator - (mint lhs, mint rhs) {
return dil<P>(lhs.x - rhs.x);
}
friend constexpr mint operator * (mint lhs, mint rhs) {
return u64(lhs.x) * rhs.x % P;
}
friend constexpr mint operator / (mint lhs, mint rhs) {
return qpw(rhs, P - 2, lhs);
}
constexpr mint &operator += (mint rhs) {
return (*this) = (*this) + rhs;
}
constexpr mint &operator -= (mint rhs) {
return (*this) = (*this) - rhs;
}
constexpr mint &operator *= (mint rhs) {
return (*this) = (*this) * rhs;
}
constexpr mint &operator /= (mint rhs) {
return (*this) = (*this) / rhs;
}
friend std::istream &operator >>(std::istream &is, mint &x) {
i64 v;
is >> v, x = get(v);
return is;
}
friend std::ostream &operator <<(std::ostream &os, mint x) {
return os << x.x;
}
};
using Z = mint < u32(998244353) >;
constexpr Z operator ""_z(u64 x) {
return Z::get(x);
}
template<class T>struct binom {
std::vector<T> fac, ifac;
void reserve(int n) {
if (n >= int(fac.size())) {
n = n * 2;
} else {
return;
}
fac.resize(n + 1), ifac.resize(n + 1);
fac[0] = 1;
for (int i = 1; i <= n; ++i) {
fac[i] = fac[i - 1] * i;
}
ifac[n] = fac[n].inv();
for (int i = n; i > 0; --i) {
ifac[i - 1] = ifac[i] * i;
}
}
T operator()(int n, int k) {
if (k > n || k < 0) {
return 0;
}
reserve(n);
return fac[n] * ifac[n - k] * ifac[k];
}
};
binom<Z> C;
Z fac(int n) {
C.reserve(n);
return C.fac[n];
}
Z ifac(int n) {
C.reserve(n);
return C.ifac[n];
}
using std::cin;
using std::cout;
void solve() {
int n, k;
cin >> n >> k;
++n, --k;
Z ans = C(k + n - 1, k);
if (n >= k) {
for (int i = 1;; ++i) {
int j = (i * (3 * i - 1)) >> 1;
if (j > k) {
break;
}
auto tmp = C(k - j + n - 1, k - j);
ans += (i & 1) ? -tmp : tmp;
if (j + i <= k) {
auto tmp = C(k - i - j + n - 1, k - i - j);
ans += (i & 1) ? -tmp : tmp;
}
}
} else {
std::vector<Z> f(k + 1);
for (int i = 0; i <= k; ++i) {
f[i] = C(i + n - 1, i);
}
for (int i = 1;; ++i) {
int j = (i * (i + 1)) >> 1;
if (j > k) {
break;
}
for (int w = k - j; w >= n - i + 1; --w) {
f[w] -= f[w - n + i - 1];
}
for (int w = i; w <= k - j; ++w) {
f[w] += f[w - i];
}
auto tmp = f[k - j];
ans += (i & 1) ? -tmp : tmp;
}
}
// int now = 0;
ans *= C.ifac[n - 1];
cout << ans;
// int now = ans * ifac[n];
// cout << now;
}
int main() {
// freopen(".in", "r", stdin);
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3756kb
input:
2 1
output:
499122177
result:
ok "499122177"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3512kb
input:
1 1
output:
1
result:
ok "1"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3592kb
input:
167 91
output:
469117530
result:
ok "469117530"
Test #4:
score: 0
Accepted
time: 296ms
memory: 296560kb
input:
9806463 8975779
output:
125384417
result:
ok "125384417"
Test #5:
score: 0
Accepted
time: 293ms
memory: 282436kb
input:
9138576 8731432
output:
306972756
result:
ok "306972756"
Test #6:
score: 0
Accepted
time: 294ms
memory: 300148kb
input:
9978791 9033584
output:
932159263
result:
ok "932159263"
Test #7:
score: 0
Accepted
time: 301ms
memory: 309276kb
input:
9811954 9790000
output:
404679920
result:
ok "404679920"
Test #8:
score: 0
Accepted
time: 289ms
memory: 299276kb
input:
9685105 9276909
output:
32996715
result:
ok "32996715"
Test #9:
score: 0
Accepted
time: 320ms
memory: 315496kb
input:
10000000 10000000
output:
309225852
result:
ok "309225852"
Test #10:
score: 0
Accepted
time: 319ms
memory: 315528kb
input:
10000000 9999999
output:
635234302
result:
ok "635234302"
Test #11:
score: 0
Accepted
time: 312ms
memory: 315480kb
input:
10000000 9999998
output:
239117935
result:
ok "239117935"
Test #12:
score: 0
Accepted
time: 311ms
memory: 315580kb
input:
10000000 9999997
output:
294859983
result:
ok "294859983"
Test #13:
score: 0
Accepted
time: 314ms
memory: 315460kb
input:
9999999 9999999
output:
305530110
result:
ok "305530110"
Test #14:
score: 0
Accepted
time: 307ms
memory: 315520kb
input:
9999999 9999998
output:
164959553
result:
ok "164959553"
Test #15:
score: 0
Accepted
time: 317ms
memory: 315448kb
input:
9999999 9999997
output:
532215262
result:
ok "532215262"
Test #16:
score: 0
Accepted
time: 298ms
memory: 315644kb
input:
9999999 9999996
output:
123628609
result:
ok "123628609"
Test #17:
score: 0
Accepted
time: 318ms
memory: 315648kb
input:
9999998 9999998
output:
223852357
result:
ok "223852357"
Test #18:
score: 0
Accepted
time: 314ms
memory: 315596kb
input:
9999998 9999997
output:
75877991
result:
ok "75877991"
Test #19:
score: 0
Accepted
time: 311ms
memory: 315760kb
input:
9999998 9999996
output:
494540335
result:
ok "494540335"
Test #20:
score: 0
Accepted
time: 315ms
memory: 315584kb
input:
9999998 9999995
output:
19191738
result:
ok "19191738"
Test #21:
score: 0
Accepted
time: 319ms
memory: 315728kb
input:
9999997 9999997
output:
238385746
result:
ok "238385746"
Test #22:
score: 0
Accepted
time: 312ms
memory: 315672kb
input:
9999997 9999996
output:
138191521
result:
ok "138191521"
Test #23:
score: 0
Accepted
time: 316ms
memory: 315716kb
input:
9999997 9999995
output:
721536184
result:
ok "721536184"
Test #24:
score: 0
Accepted
time: 306ms
memory: 315452kb
input:
9999997 9999994
output:
627112720
result:
ok "627112720"
Test #25:
score: 0
Accepted
time: 152ms
memory: 158568kb
input:
8113616 1826492
output:
629546539
result:
ok "629546539"
Test #26:
score: 0
Accepted
time: 173ms
memory: 182136kb
input:
7230333 4233627
output:
870135249
result:
ok "870135249"
Test #27:
score: 0
Accepted
time: 304ms
memory: 305556kb
input:
9734872 9617286
output:
780426509
result:
ok "780426509"
Test #28:
score: 0
Accepted
time: 199ms
memory: 209076kb
input:
6780022 6393958
output:
508662111
result:
ok "508662111"
Test #29:
score: 0
Accepted
time: 115ms
memory: 111080kb
input:
4986441 1909344
output:
762587564
result:
ok "762587564"
Test #30:
score: 0
Accepted
time: 152ms
memory: 159812kb
input:
9936540 91728
output:
651924678
result:
ok "651924678"
Test #31:
score: 0
Accepted
time: 141ms
memory: 146620kb
input:
9099529 94239
output:
775532638
result:
ok "775532638"
Test #32:
score: 0
Accepted
time: 156ms
memory: 153956kb
input:
9564814 93545
output:
474538902
result:
ok "474538902"
Test #33:
score: 0
Accepted
time: 166ms
memory: 156296kb
input:
9707744 92094
output:
354024226
result:
ok "354024226"
Test #34:
score: 0
Accepted
time: 153ms
memory: 147880kb
input:
9167687 94820
output:
858989558
result:
ok "858989558"
Test #35:
score: 0
Accepted
time: 154ms
memory: 161076kb
input:
10000000 100000
output:
609345536
result:
ok "609345536"
Test #36:
score: 0
Accepted
time: 167ms
memory: 161024kb
input:
10000000 99999
output:
217258255
result:
ok "217258255"
Test #37:
score: 0
Accepted
time: 164ms
memory: 160960kb
input:
10000000 99998
output:
485057696
result:
ok "485057696"
Test #38:
score: 0
Accepted
time: 160ms
memory: 160956kb
input:
10000000 99997
output:
193579142
result:
ok "193579142"
Test #39:
score: 0
Accepted
time: 160ms
memory: 161060kb
input:
9999999 100000
output:
584105896
result:
ok "584105896"
Test #40:
score: 0
Accepted
time: 163ms
memory: 160816kb
input:
9999999 99999
output:
707014865
result:
ok "707014865"
Test #41:
score: 0
Accepted
time: 159ms
memory: 161148kb
input:
9999999 99998
output:
872987417
result:
ok "872987417"
Test #42:
score: 0
Accepted
time: 160ms
memory: 161152kb
input:
9999999 99997
output:
707304988
result:
ok "707304988"
Test #43:
score: 0
Accepted
time: 169ms
memory: 160812kb
input:
9999998 100000
output:
789028925
result:
ok "789028925"
Test #44:
score: 0
Accepted
time: 159ms
memory: 160824kb
input:
9999998 99999
output:
628266237
result:
ok "628266237"
Test #45:
score: 0
Accepted
time: 149ms
memory: 160824kb
input:
9999998 99998
output:
38358057
result:
ok "38358057"
Test #46:
score: 0
Accepted
time: 169ms
memory: 160748kb
input:
9999998 99997
output:
785931240
result:
ok "785931240"
Test #47:
score: 0
Accepted
time: 169ms
memory: 161024kb
input:
9999997 100000
output:
945452715
result:
ok "945452715"
Test #48:
score: 0
Accepted
time: 156ms
memory: 160892kb
input:
9999997 99999
output:
537126025
result:
ok "537126025"
Test #49:
score: 0
Accepted
time: 159ms
memory: 160960kb
input:
9999997 99998
output:
837196653
result:
ok "837196653"
Test #50:
score: 0
Accepted
time: 164ms
memory: 160888kb
input:
9999997 99997
output:
263045713
result:
ok "263045713"
Test #51:
score: 0
Accepted
time: 0ms
memory: 3544kb
input:
2 2
output:
1
result:
ok "1"
Test #52:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
3 3
output:
831870295
result:
ok "831870295"
Test #53:
score: 0
Accepted
time: 0ms
memory: 3800kb
input:
4 4
output:
374341633
result:
ok "374341633"
Test #54:
score: 0
Accepted
time: 0ms
memory: 3604kb
input:
3 2
output:
499122177
result:
ok "499122177"
Test #55:
score: 0
Accepted
time: 0ms
memory: 3788kb
input:
4 3
output:
623902721
result:
ok "623902721"
Test #56:
score: 0
Accepted
time: 0ms
memory: 3560kb
input:
5 4
output:
890101215
result:
ok "890101215"
Test #57:
score: 0
Accepted
time: 0ms
memory: 3512kb
input:
3 1
output:
166374059
result:
ok "166374059"
Test #58:
score: 0
Accepted
time: 0ms
memory: 3804kb
input:
4 2
output:
166374059
result:
ok "166374059"
Test #59:
score: 0
Accepted
time: 0ms
memory: 3788kb
input:
5 3
output:
16637406
result:
ok "16637406"
Test #60:
score: 0
Accepted
time: 0ms
memory: 3512kb
input:
6 4
output:
508827330
result:
ok "508827330"
Test #61:
score: 0
Accepted
time: 0ms
memory: 3516kb
input:
4 1
output:
291154603
result:
ok "291154603"
Test #62:
score: 0
Accepted
time: 0ms
memory: 3536kb
input:
5 2
output:
291154603
result:
ok "291154603"
Test #63:
score: 0
Accepted
time: 0ms
memory: 3528kb
input:
6 3
output:
859599304
result:
ok "859599304"
Test #64:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
7 4
output:
694809760
result:
ok "694809760"
Test #65:
score: 0
Accepted
time: 95ms
memory: 91516kb
input:
5629201 38642
output:
327294391
result:
ok "327294391"
Test #66:
score: 0
Accepted
time: 5ms
memory: 6144kb
input:
126092 74219
output:
27573951
result:
ok "27573951"
Test #67:
score: 0
Accepted
time: 136ms
memory: 137632kb
input:
8593075 8689
output:
393785113
result:
ok "393785113"
Test #68:
score: 0
Accepted
time: 18ms
memory: 20820kb
input:
1076972 58637
output:
600806929
result:
ok "600806929"
Test #69:
score: 0
Accepted
time: 7ms
memory: 10788kb
input:
463217 39187
output:
408712022
result:
ok "408712022"
Extra Test:
score: 0
Extra Test Passed