QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#213500 | #6551. Forever Young | ucup-team087# | TL | 1890ms | 46992kb | C++14 | 6.7kb | 2023-10-14 14:31:39 | 2023-10-14 14:31:40 |
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")
////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
static constexpr unsigned M = M_;
unsigned x;
constexpr ModInt() : x(0U) {}
constexpr ModInt(unsigned x_) : x(x_ % M) {}
constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
constexpr 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) { x = (static_cast<unsigned long long>(x) * a.x) % 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; }
};
////////////////////////////////////////////////////////////////////////////////
constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;
constexpr int LIM_INV = 2'000'010;
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;
}
}
}
#include <ext/pb_ds/assoc_container.hpp>
using __gnu_pbds::gp_hash_table;
int N[2];
int S[2], A[2][70];
int K;
gp_hash_table<Int, Mint> dp[2][70];
Mint F[70];
int main() {
prepare();
for (; ~scanf("%d", &N[0]); ) {
for (int i = 0; i < N[0]; ++i) scanf("%d", &A[0][i]);
scanf("%d", &N[1]);
for (int i = 0; i < N[1]; ++i) scanf("%d", &A[1][i]);
scanf("%d", &K);
for (int h = 0; h < 2; ++h) {
{
Int ini = 1 << 0;
S[h] = 0;
for (int i = 0; i < N[h]; ++i) {
S[h] += A[h][i];
ini |= 1LL << S[h];
}
for (int s = 0; s <= S[h]; ++s) {
dp[h][s].clear();
}
dp[h][S[h]][ini] = 1;
}
{
int es[70];
for (int s = S[h]; s; --s) {
for (const auto &kv : dp[h][s]) {
const Int u = kv.first;
int len = 0;
for (int e = 0; e <= s; ++e) if (u >> e & 1) es[len++] = e;
es[len] = s;
for (int i = 0; i < len - 1; ++i) if (es[i + 1] - es[i] > es[i + 2] - es[i + 1]) {
const int e = es[i] + 1;
const Int v = (u & ((1ULL << e) - 1)) | (u >> (e + 1) << e);
dp[h][s - 1][v] += kv.second;
}
}
}
}
}
memset(F, 0, sizeof(F));
for (int s = 0; s <= min(S[0], S[1]); ++s) {
for (const auto &kv : dp[0][s]) {
auto it = dp[1][s].find(kv.first);
if (it != dp[1][s].end()) {
F[s] += kv.second * it->second;
}
}
}
// cerr<<"F = ";pv(F,F+min(S[0],S[1])+1);
// e^(x^2/2)
vector<Mint> coef(K + 1);
{
Mint t = 1;
for (int k = 0; 2 * k <= K; ++k) {
coef[2 * k] = invFac[k] * t;
t /= 2;
}
}
Mint ans = 0;
// U^i0 D^i1
for (int s = 0; s <= min(S[0], S[1]); ++s) {
const int i0 = S[0] - s;
const int i1 = S[1] - s;
const int k = K - i0 - i1;
if (k >= 0) {
ans += coef[k] * invFac[i0] * invFac[i1] * F[s];
}
}
ans *= fac[K];
printf("%u\n", ans.x);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 13ms
memory: 27152kb
input:
3 3 2 1 3 3 2 1 2
output:
7
result:
ok 1 number(s): "7"
Test #2:
score: 0
Accepted
time: 10ms
memory: 27284kb
input:
3 3 2 1 3 3 2 1 1111
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 21ms
memory: 27096kb
input:
0 0 10
output:
945
result:
ok 1 number(s): "945"
Test #4:
score: 0
Accepted
time: 617ms
memory: 40180kb
input:
10 10 9 8 7 6 5 4 4 4 3 10 10 9 8 7 6 5 4 4 4 3 1000000
output:
591072522
result:
ok 1 number(s): "591072522"
Test #5:
score: 0
Accepted
time: 311ms
memory: 35904kb
input:
10 10 9 8 7 6 5 4 4 4 3 6 10 10 10 10 10 10 1000000
output:
954562178
result:
ok 1 number(s): "954562178"
Test #6:
score: 0
Accepted
time: 22ms
memory: 30716kb
input:
1 59 1 60 999999
output:
621240518
result:
ok 1 number(s): "621240518"
Test #7:
score: 0
Accepted
time: 23ms
memory: 28116kb
input:
6 10 10 10 10 10 10 5 12 12 12 12 12 122220
output:
996858520
result:
ok 1 number(s): "996858520"
Test #8:
score: 0
Accepted
time: 31ms
memory: 31492kb
input:
5 5 4 3 2 1 5 20 10 10 10 10 999999
output:
395659998
result:
ok 1 number(s): "395659998"
Test #9:
score: 0
Accepted
time: 204ms
memory: 33812kb
input:
9 10 9 8 7 6 5 5 5 5 9 10 9 8 7 6 5 5 5 5 100000
output:
350064296
result:
ok 1 number(s): "350064296"
Test #10:
score: 0
Accepted
time: 48ms
memory: 33328kb
input:
6 13 11 8 7 6 5 7 11 11 9 8 7 6 5 1000000
output:
0
result:
ok 1 number(s): "0"
Test #11:
score: 0
Accepted
time: 168ms
memory: 35872kb
input:
7 13 12 9 8 7 6 5 7 13 12 9 8 7 6 5 1000000
output:
130449432
result:
ok 1 number(s): "130449432"
Test #12:
score: 0
Accepted
time: 162ms
memory: 35872kb
input:
7 13 12 9 8 7 6 5 7 13 11 9 8 7 6 5 999999
output:
449198110
result:
ok 1 number(s): "449198110"
Test #13:
score: 0
Accepted
time: 346ms
memory: 39056kb
input:
7 15 12 9 8 7 6 3 7 15 12 9 8 7 6 3 1000000
output:
553218647
result:
ok 1 number(s): "553218647"
Test #14:
score: 0
Accepted
time: 25ms
memory: 30588kb
input:
0 0 1000000
output:
765860359
result:
ok 1 number(s): "765860359"
Test #15:
score: 0
Accepted
time: 8ms
memory: 30592kb
input:
1 1 1 1 1000000
output:
71283935
result:
ok 1 number(s): "71283935"
Test #16:
score: 0
Accepted
time: 22ms
memory: 30908kb
input:
3 20 20 20 20 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1000000
output:
708368272
result:
ok 1 number(s): "708368272"
Test #17:
score: 0
Accepted
time: 17ms
memory: 27160kb
input:
3 8 1 1 3 5 4 1 5
output:
0
result:
ok 1 number(s): "0"
Test #18:
score: 0
Accepted
time: 10ms
memory: 27152kb
input:
3 7 2 1 3 5 4 1 3
output:
0
result:
ok 1 number(s): "0"
Test #19:
score: 0
Accepted
time: 20ms
memory: 27176kb
input:
4 5 2 2 1 4 4 3 2 1 5
output:
0
result:
ok 1 number(s): "0"
Test #20:
score: 0
Accepted
time: 17ms
memory: 27248kb
input:
4 4 3 2 1 4 3 3 2 2 4
output:
60
result:
ok 1 number(s): "60"
Test #21:
score: 0
Accepted
time: 17ms
memory: 27224kb
input:
5 4 2 2 1 1 5 4 2 2 1 1 5
output:
0
result:
ok 1 number(s): "0"
Test #22:
score: 0
Accepted
time: 12ms
memory: 27180kb
input:
5 4 2 2 1 1 5 3 3 2 1 1 3
output:
0
result:
ok 1 number(s): "0"
Test #23:
score: 0
Accepted
time: 17ms
memory: 27152kb
input:
6 3 2 2 1 1 1 6 3 2 2 1 1 1 5
output:
0
result:
ok 1 number(s): "0"
Test #24:
score: 0
Accepted
time: 21ms
memory: 27152kb
input:
6 4 2 1 1 1 1 6 3 2 2 1 1 1 5
output:
0
result:
ok 1 number(s): "0"
Test #25:
score: 0
Accepted
time: 17ms
memory: 27224kb
input:
7 2 2 2 1 1 1 1 7 2 2 2 1 1 1 1 5
output:
0
result:
ok 1 number(s): "0"
Test #26:
score: 0
Accepted
time: 17ms
memory: 27148kb
input:
7 4 1 1 1 1 1 1 7 3 2 1 1 1 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #27:
score: 0
Accepted
time: 22ms
memory: 27224kb
input:
8 2 2 1 1 1 1 1 1 8 2 2 1 1 1 1 1 1 5
output:
0
result:
ok 1 number(s): "0"
Test #28:
score: 0
Accepted
time: 18ms
memory: 27188kb
input:
8 2 2 1 1 1 1 1 1 8 2 2 1 1 1 1 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #29:
score: 0
Accepted
time: 12ms
memory: 27172kb
input:
9 2 1 1 1 1 1 1 1 1 9 2 1 1 1 1 1 1 1 1 5
output:
0
result:
ok 1 number(s): "0"
Test #30:
score: 0
Accepted
time: 20ms
memory: 27224kb
input:
9 2 1 1 1 1 1 1 1 1 9 2 1 1 1 1 1 1 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #31:
score: 0
Accepted
time: 17ms
memory: 27188kb
input:
10 1 1 1 1 1 1 1 1 1 1 10 1 1 1 1 1 1 1 1 1 1 5
output:
0
result:
ok 1 number(s): "0"
Test #32:
score: 0
Accepted
time: 21ms
memory: 27272kb
input:
10 1 1 1 1 1 1 1 1 1 1 10 1 1 1 1 1 1 1 1 1 1 3
output:
0
result:
ok 1 number(s): "0"
Test #33:
score: 0
Accepted
time: 18ms
memory: 27180kb
input:
1 20 1 20 0
output:
1
result:
ok 1 number(s): "1"
Test #34:
score: 0
Accepted
time: 18ms
memory: 27180kb
input:
1 20 1 21 1
output:
1
result:
ok 1 number(s): "1"
Test #35:
score: 0
Accepted
time: 10ms
memory: 27172kb
input:
1 20 1 22 1
output:
0
result:
ok 1 number(s): "0"
Test #36:
score: 0
Accepted
time: 17ms
memory: 27108kb
input:
3 11 7 2 4 14 3 2 1 0
output:
0
result:
ok 1 number(s): "0"
Test #37:
score: 0
Accepted
time: 21ms
memory: 30560kb
input:
60 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 60 1000000
output:
876113637
result:
ok 1 number(s): "876113637"
Test #38:
score: 0
Accepted
time: 17ms
memory: 30620kb
input:
1 60 60 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1000000
output:
876113637
result:
ok 1 number(s): "876113637"
Test #39:
score: 0
Accepted
time: 20ms
memory: 30092kb
input:
60 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 60 853105
output:
0
result:
ok 1 number(s): "0"
Test #40:
score: 0
Accepted
time: 20ms
memory: 29472kb
input:
1 60 60 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 727238
output:
177047655
result:
ok 1 number(s): "177047655"
Test #41:
score: 0
Accepted
time: 8ms
memory: 30484kb
input:
1 60 1 60 1000000
output:
921463967
result:
ok 1 number(s): "921463967"
Test #42:
score: 0
Accepted
time: 13ms
memory: 27812kb
input:
1 60 1 60 243870
output:
509116683
result:
ok 1 number(s): "509116683"
Test #43:
score: 0
Accepted
time: 20ms
memory: 30548kb
input:
2 50 10 2 55 5 1000000
output:
861843225
result:
ok 1 number(s): "861843225"
Test #44:
score: 0
Accepted
time: 18ms
memory: 27212kb
input:
2 45 15 2 51 9 111774
output:
12281419
result:
ok 1 number(s): "12281419"
Test #45:
score: 0
Accepted
time: 23ms
memory: 30880kb
input:
3 23 21 16 3 27 21 12 1000000
output:
652026360
result:
ok 1 number(s): "652026360"
Test #46:
score: 0
Accepted
time: 23ms
memory: 30980kb
input:
3 31 19 10 3 27 23 10 979678
output:
673323950
result:
ok 1 number(s): "673323950"
Test #47:
score: 0
Accepted
time: 29ms
memory: 31804kb
input:
4 22 22 9 7 4 28 16 10 6 1000000
output:
5948265
result:
ok 1 number(s): "5948265"
Test #48:
score: 0
Accepted
time: 23ms
memory: 31128kb
input:
4 28 12 11 9 4 28 25 5 2 880286
output:
21805708
result:
ok 1 number(s): "21805708"
Test #49:
score: 0
Accepted
time: 73ms
memory: 33288kb
input:
5 23 13 8 8 8 5 23 17 12 6 2 1000000
output:
587098080
result:
ok 1 number(s): "587098080"
Test #50:
score: 0
Accepted
time: 43ms
memory: 31676kb
input:
5 30 14 12 3 1 5 28 13 12 6 1 748190
output:
2626249
result:
ok 1 number(s): "2626249"
Test #51:
score: 0
Accepted
time: 166ms
memory: 34936kb
input:
6 22 11 9 8 5 5 6 19 18 11 9 2 1 1000000
output:
488941862
result:
ok 1 number(s): "488941862"
Test #52:
score: 0
Accepted
time: 244ms
memory: 34804kb
input:
6 18 13 13 8 4 4 6 20 13 9 8 6 4 648799
output:
0
result:
ok 1 number(s): "0"
Test #53:
score: 0
Accepted
time: 527ms
memory: 38992kb
input:
7 22 16 9 6 4 2 1 7 19 16 11 8 3 2 1 1000000
output:
493787537
result:
ok 1 number(s): "493787537"
Test #54:
score: 0
Accepted
time: 227ms
memory: 34148kb
input:
7 14 14 12 11 5 2 2 7 22 18 8 6 4 1 1 516703
output:
0
result:
ok 1 number(s): "0"
Test #55:
score: 0
Accepted
time: 1078ms
memory: 43836kb
input:
8 20 12 7 7 4 4 3 3 8 18 13 8 6 6 5 2 2 1000000
output:
468062792
result:
ok 1 number(s): "468062792"
Test #56:
score: 0
Accepted
time: 725ms
memory: 38240kb
input:
8 18 14 9 7 6 4 1 1 8 24 13 10 7 2 2 1 1 384607
output:
0
result:
ok 1 number(s): "0"
Test #57:
score: 0
Accepted
time: 1890ms
memory: 46976kb
input:
9 17 12 10 5 5 4 3 3 1 9 22 12 8 6 4 3 2 2 1 1000000
output:
829982699
result:
ok 1 number(s): "829982699"
Test #58:
score: 0
Accepted
time: 1069ms
memory: 40800kb
input:
9 22 11 10 4 3 3 3 2 2 9 15 14 7 7 6 5 3 2 1 285215
output:
0
result:
ok 1 number(s): "0"
Test #59:
score: 0
Accepted
time: 1851ms
memory: 46992kb
input:
10 20 10 7 5 4 3 3 3 3 2 10 17 10 8 8 6 4 4 1 1 1 1000000
output:
750051767
result:
ok 1 number(s): "750051767"
Test #60:
score: 0
Accepted
time: 903ms
memory: 42560kb
input:
10 12 12 9 5 5 5 3 3 3 3 10 16 8 7 7 6 6 4 3 2 1 370698
output:
377561452
result:
ok 1 number(s): "377561452"
Test #61:
score: 0
Accepted
time: 798ms
memory: 41972kb
input:
11 10 9 8 6 6 5 4 4 4 3 1 11 20 6 6 6 5 5 5 3 2 1 1 1000000
output:
461938227
result:
ok 1 number(s): "461938227"
Test #62:
score: -100
Time Limit Exceeded
input:
11 12 10 8 6 5 5 4 4 2 2 2 11 15 11 9 8 6 4 2 2 1 1 1 752918