QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#94618 | #5546. Sharing Bread | 8BQube | AC ✓ | 945ms | 29584kb | C++20 | 3.0kb | 2023-04-06 22:29:43 | 2023-04-06 22:29:45 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define SZ(a) ((int)a.size())
#define ALL(v) v.begin(), v.end()
#define pb push_back
const int MOD = 998244353;
const int N = 1 << 19;
void add(ll &x, ll val) {
x = (x + val) % MOD;
}
ll pw(ll a, ll n) {
ll rt = 1;
for (; n; n >>= 1, a = a * a % MOD)
if (n & 1)
rt = rt * a % MOD;
return rt;
}
//(2^16)+1, 65537, 3
//7*17*(2^23)+1, 998244353, 3
//1255*(2^20)+1, 1315962881, 3
//51*(2^25)+1, 1711276033, 29
template<int MAXN, ll P, ll RT> //MAXN must be 2^k
struct NTT {
ll w[MAXN];
ll mpow(ll a, ll n) {
return pw(a, n);
}
ll minv(ll a) { return mpow(a, P - 2); }
NTT() {
ll dw = mpow(RT, (P - 1) / MAXN);
w[0] = 1;
for (int i = 1; i < MAXN; ++i) w[i] = w[i - 1] * dw % P;
}
void bitrev(ll *a, int n) {
int i = 0;
for (int j = 1; j < n - 1; ++j) {
for (int k = n >> 1; (i ^= k) < k; k >>= 1);
if (j < i) swap(a[i], a[j]);
}
}
void operator()(ll *a, int n, bool inv = false) { //0 <= a[i] < P
bitrev(a, n);
for (int L = 2; L <= n; L <<= 1) {
int dx = MAXN / L, dl = L >> 1;
for (int i = 0; i < n; i += L) {
for (int j = i, x = 0; j < i + dl; ++j, x += dx) {
ll tmp = a[j + dl] * w[x] % P;
if ((a[j + dl] = a[j] - tmp) < 0) a[j + dl] += P;
if ((a[j] += tmp) >= P) a[j] -= P;
}
}
}
if (inv) {
reverse(a + 1, a + n);
ll invn = minv(n);
for (int i = 0; i < n; ++i) a[i] = a[i] * invn % P;
}
}
};
NTT<N, MOD, 3> ntt;
int n;
ll dp[N], fac[N], ifac[N], kk[N];
ll a[N], b[N];
void dq(int l, int r) {
if (l == r) {
dp[l] = MOD - dp[l];
add(dp[l], pw(n + l + 1, l) * ifac[l] % MOD);
return;
}
int mid = (l + r) >> 1;
dq(l, mid);
int L = 1;
for (; L < (r - l + 1) * 2; L <<= 1);
fill(a, a + L, 0), fill(b, b + L, 0);
for (int i = l; i <= mid; ++i)
a[i - l] = dp[i];
for (int i = 1; i <= r - l; ++i)
b[i] = kk[i] * ifac[i] % MOD;
ntt(a, L), ntt(b, L);
for (int i = 0; i < L; ++i)
a[i] = a[i] * b[i] % MOD;
ntt(a, L, true);
for (int i = mid + 1; i <= r; ++i)
add(dp[i], a[i - l]);
dq(mid + 1, r);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int m;
cin >> n >> m, n -= m;
fac[0] = 1;
for (int i = 1; i <= m; ++i) {
fac[i] = fac[i - 1] * i % MOD;
kk[i] = pw(i, i);
}
ifac[m] = pw(fac[m], MOD - 2);
for (int i = m - 1; i >= 0; --i)
ifac[i] = ifac[i + 1] * (i + 1) % MOD;
dq(0, m);
cout << dp[m] * fac[m] % MOD << "\n";
/*dp[0] = 1;
for (int i = 1; i <= m; ++i) {
dp[i] = pw(n + i + 1, i) * ifac[i] % MOD;
for (int j = 0; j < i; ++j)
add(dp[i], MOD - pw(i - j, i - j) * dp[j] % MOD * ifac[i - j] % MOD);
}*/
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 16964kb
input:
4 3
output:
50
result:
ok single line: '50'
Test #2:
score: 0
Accepted
time: 1ms
memory: 17328kb
input:
10 1
output:
10
result:
ok single line: '10'
Test #3:
score: 0
Accepted
time: 5ms
memory: 17584kb
input:
2 2
output:
3
result:
ok single line: '3'
Test #4:
score: 0
Accepted
time: 8ms
memory: 17544kb
input:
1 1
output:
1
result:
ok single line: '1'
Test #5:
score: 0
Accepted
time: 5ms
memory: 16112kb
input:
277 277
output:
124662617
result:
ok single line: '124662617'
Test #6:
score: 0
Accepted
time: 4ms
memory: 15764kb
input:
426 1
output:
426
result:
ok single line: '426'
Test #7:
score: 0
Accepted
time: 1ms
memory: 16252kb
input:
200000 1
output:
200000
result:
ok single line: '200000'
Test #8:
score: 0
Accepted
time: 802ms
memory: 29584kb
input:
200000 200000
output:
950017432
result:
ok single line: '950017432'
Test #9:
score: 0
Accepted
time: 355ms
memory: 22772kb
input:
200000 100000
output:
280947286
result:
ok single line: '280947286'
Test #10:
score: 0
Accepted
time: 363ms
memory: 22516kb
input:
200000 84731
output:
211985425
result:
ok single line: '211985425'
Test #11:
score: 0
Accepted
time: 373ms
memory: 22760kb
input:
200000 124713
output:
716696526
result:
ok single line: '716696526'
Test #12:
score: 0
Accepted
time: 162ms
memory: 28336kb
input:
129179 49655
output:
506429515
result:
ok single line: '506429515'
Test #13:
score: 0
Accepted
time: 81ms
memory: 17964kb
input:
87518 26040
output:
808454539
result:
ok single line: '808454539'
Test #14:
score: 0
Accepted
time: 38ms
memory: 17308kb
input:
178355 10116
output:
361555714
result:
ok single line: '361555714'
Test #15:
score: 0
Accepted
time: 8ms
memory: 16976kb
input:
2 1
output:
2
result:
ok single line: '2'
Test #16:
score: 0
Accepted
time: 156ms
memory: 17636kb
input:
192733 52550
output:
67181038
result:
ok single line: '67181038'
Test #17:
score: 0
Accepted
time: 163ms
memory: 22092kb
input:
76689 36632
output:
717949287
result:
ok single line: '717949287'
Test #18:
score: 0
Accepted
time: 1ms
memory: 17168kb
input:
200000 9
output:
158524471
result:
ok single line: '158524471'
Test #19:
score: 0
Accepted
time: 779ms
memory: 26328kb
input:
200000 199998
output:
879727659
result:
ok single line: '879727659'
Test #20:
score: 0
Accepted
time: 2ms
memory: 16176kb
input:
199952 1
output:
199952
result:
ok single line: '199952'
Test #21:
score: 0
Accepted
time: 945ms
memory: 23412kb
input:
199947 199947
output:
339118685
result:
ok single line: '339118685'
Test #22:
score: 0
Accepted
time: 361ms
memory: 24692kb
input:
199956 99978
output:
135867461
result:
ok single line: '135867461'
Test #23:
score: 0
Accepted
time: 2ms
memory: 16680kb
input:
2 2
output:
3
result:
ok single line: '3'
Test #24:
score: 0
Accepted
time: 4ms
memory: 17748kb
input:
10 3
output:
968
result:
ok single line: '968'
Test #25:
score: 0
Accepted
time: 1ms
memory: 15016kb
input:
10 5
output:
87846
result:
ok single line: '87846'
Test #26:
score: 0
Accepted
time: 4ms
memory: 16888kb
input:
10 9
output:
428717762
result:
ok single line: '428717762'
Test #27:
score: 0
Accepted
time: 6ms
memory: 17200kb
input:
279 166
output:
945780025
result:
ok single line: '945780025'
Test #28:
score: 0
Accepted
time: 8ms
memory: 16104kb
input:
361 305
output:
926296326
result:
ok single line: '926296326'
Test #29:
score: 0
Accepted
time: 0ms
memory: 16212kb
input:
305 262
output:
465560336
result:
ok single line: '465560336'