QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#845019 | #7875. Queue Sorting | brendonw | AC ✓ | 412ms | 5308kb | C++20 | 5.7kb | 2025-01-06 13:57:06 | 2025-01-06 13:57:15 |
Judging History
answer
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
/* NAMESPACE */
using namespace std;
using namespace __gnu_pbds;
/* HASH */
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
template<class K, class V> using safe_map = unordered_map<K, V, custom_hash>;
template<class K, class V> using safe_ht = gp_hash_table<K, V, custom_hash>;
template<class K> using safe_set = unordered_set<K, custom_hash>;
template<class K> using safe_htset = gp_hash_table<K, null_type, custom_hash>;
/* CLASSES */
using ll = long long;
using pii = pair<int, int>;
template<class T> using pp = pair<T, T>;
using pll = pp<ll>;
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> using greater_pq = priority_queue<T, vector<T>, greater<>>;
template<class T> using V = vector<T>;
using vi = V<int>;
using vvi = V<vi>;
using vvvi = V<vvi>;
using vll = V<ll>;
/* FUNCTIONS */
template<class T, class U>
T max(T a, U b) {
return (a > b) ? a : b;
}
template<class T, class U>
T min(T a, U b) {
return (a < b) ? a : b;
}
template<class T>
T power(T x, T y) {
T res = 1;
for (T i = 0; i < y; i++) {
res *= x;
}
return res;
}
/* MACROS */
#define clearall(arr) memset((arr), 0, sizeof (arr))
#define clearn(arr, n) memset((arr), 0, n * sizeof (arr)[0])
#define resetall(arr) memset((arr), -1, sizeof (arr))
#define resetn(arr, n) memset((arr), -1, n * sizeof (arr)[0])
#define YESNO(condition) cout << ((condition) ? "YES" : "NO")
#define sfunc(a, b, c) ((a) = c((a), (b)))
#define smin(a, b) sfunc((a), (b), min)
#define smax(a, b) sfunc((a), (b), max)
#define ALL(x) begin((x)), end((x))
#define SZ(a) (int)(a).size()
#define readall(arr, n) for (int i = 0; i < n; i++) cin >> (arr)[i]
#define printall(arr, n) for (int i = 0; i < n; i++) cout << (arr)[i] << " "
#define printalldel(arr, n, del) for (int i = 0; i < n; i++) cout << (arr)[i] << del
#define mx_val(arr) (*max_element(ALL(arr)))
/* DEBUG TEMPLATE*/
template<typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &p) { return os << "(" << p.first << ", " << p.second << ")"; }
template<typename C, typename T = decay<decltype(*begin(
declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type * = nullptr>
ostream &operator<<(ostream &os, const C &c) {
bool f = true;
os << "[";
for (const auto &x: c) {
if (!f) os << ", ";
f = false;
os << x;
}
return os << "]";
}
template<typename T>
void debug(string s, T x) { cerr << "\033[1;35m" << s << "\033[0;32m = \033[33m" << x << "\033[0m\n"; }
template<typename T, typename... Args>
void debug(string s, T x, Args... args) {
for (int i = 0, b = 0; i < (int) s.size(); i++)
if (s[i] == '(' || s[i] == '{') b++;
else if (s[i] == ')' || s[i] == '}') b--;
else if (s[i] == ',' && b == 0) {
cerr << "\033[1;35m" << s.substr(0, i) << "\033[0;32m = \033[33m" << x << "\033[31m | ";
debug(s.substr(s.find_first_not_of(' ', i + 1)), args...);
break;
}
}
template<typename T>
std::vector<T> vectorize(T *a, int n) {
std::vector<T> res;
for (int i = 0; i < n; ++i) {
res.push_back(a[i]);
}
return res;
}
template<typename T, typename... Sizes>
auto vectorize(T *a, int n, Sizes... sizes) {
std::vector<decltype(vectorize(a[0], sizes...))> res;
for (int i = 0; i < n; ++i) {
res.push_back(vectorize(a[i], sizes...));
}
return res;
}
#ifdef LOCAL
#define DEBUG(...) debug(#__VA_ARGS__, __VA_ARGS__)
#else
#define DEBUG(...) 42
#endif
/* CONSTANTS */
const int inf = 2e9;
const ll infl = 4e18;
const ll MOD = 998244353;
const ll MAXN = 2e5 + 5;
template<typename T>
struct binomial {
int MOD;
vector<T> fact;
vector<T> inv;
binomial() = default;
binomial(int MOD, int MAXN) : MOD(MOD) {
fact.resize(MAXN + 1);
inv.resize(MAXN + 1);
int N = min(MAXN, MOD) - 1;
fact[0] = 1;
for (int i = 1; i <= N; i++) {
fact[i] = (T) (1LL * fact[i - 1] * i % MOD);
}
inv[N] = pow(fact[N], MOD - 2);
for (int i = N - 1; i >= 0; i--) {
inv[i] = (T) (1LL * inv[i + 1] * (i + 1) % MOD);
}
}
T pow(T a, int b) {
T res = 1;
while (b) {
if (b & 1) res = (T) (1LL * res * a % MOD);
a = (T) (1LL * a * a % MOD);
b >>= 1;
}
return res;
}
T C(int n, int k) {
if (n < k || n < 0 || k < 0) return 0;
return (T) (1LL * fact[n] * inv[k] % MOD * inv[n - k] % MOD);
}
};
int solve() {
int n;
cin >> n;
vector<int> a(n);
vector<int> suffix(n + 1);
for (auto &a_i: a) {
cin >> a_i;
}
for (int i = n - 1; i >= 0; --i) {
suffix[i] = suffix[i + 1] + a[i];
}
binomial<ll> binom(MOD, 505);
vector<vector<ll>> dp(n + 1, vector(505, 0ll));
dp[n][0] = 1;
for (int i = n - 1; i >= 0; --i) {
for (int j = 0; j <= 500; ++j) {
for (int x = 0; x < a[i]; ++x) {
for (int k = 1; k <= j; ++k) {
dp[i][x + k] += 1ll * dp[i + 1][j] * binom.C(a[i] + j - (x + k) - 1, a[i] - x - 1) % MOD;
dp[i][x + k] %= MOD;
}
}
dp[i][a[i] + j] = (dp[i][a[i] + j] + dp[i + 1][j]) % MOD;
}
}
ll ans = 0;
for (int i = 0; i <= 500; ++i) {
ans = (ans + dp[0][i]) % MOD;
}
cout << ans;
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 3552kb
input:
4 1 1 1 1
output:
14
result:
ok 1 number(s): "14"
Test #2:
score: 0
Accepted
time: 412ms
memory: 4520kb
input:
300 0 5 2 2 1 0 3 2 2 5 2 1 1 2 1 3 2 3 2 0 0 0 0 1 2 2 3 0 2 2 3 2 0 2 3 0 6 0 0 2 0 1 3 2 1 1 1 3 4 0 1 0 4 1 1 1 1 1 1 2 3 2 1 2 3 2 3 0 5 3 3 2 0 1 1 0 2 1 1 2 0 0 2 1 1 3 2 2 1 2 1 3 0 3 0 1 2 2 0 5 0 2 2 0 0 0 1 2 1 4 2 1 1 0 3 0 2 0 3 1 1 2 0 2 1 1 0 2 0 1 2 2 3 3 1 1 1 1 0 1 3 3 1 0 2 2 4 2 ...
output:
507010274
result:
ok 1 number(s): "507010274"
Test #3:
score: 0
Accepted
time: 410ms
memory: 5152kb
input:
500 1 1 0 2 1 0 2 3 2 0 0 2 0 2 1 1 0 0 1 1 1 2 1 1 1 0 1 1 2 2 1 4 0 2 1 0 2 3 1 0 1 1 0 2 1 2 2 1 0 0 3 1 4 1 1 2 1 1 0 1 3 1 2 0 0 0 2 1 2 0 0 3 2 1 1 1 1 1 2 1 0 1 0 0 0 1 0 0 2 1 1 0 1 0 1 0 1 0 0 1 0 1 1 0 0 0 1 0 0 0 2 1 1 0 1 1 0 1 1 0 0 1 0 3 1 3 0 0 2 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 1 2 0 0 ...
output:
7590964
result:
ok 1 number(s): "7590964"
Test #4:
score: 0
Accepted
time: 410ms
memory: 3892kb
input:
200 3 1 0 3 2 1 0 3 1 1 2 3 3 1 6 2 1 3 2 1 1 2 1 2 1 5 2 2 3 4 0 4 2 1 2 2 0 2 3 1 2 3 6 3 2 3 2 2 4 2 7 2 1 5 1 9 0 4 4 8 3 3 3 1 3 0 2 2 8 1 3 5 4 3 0 6 1 6 1 3 4 2 2 1 1 4 4 4 1 0 4 3 4 3 3 0 3 2 0 0 3 4 0 3 1 3 2 4 3 2 0 3 2 2 3 2 2 2 1 2 2 1 0 2 0 3 1 3 5 1 3 3 6 5 3 2 2 2 3 6 2 0 5 2 2 2 2 1 ...
output:
507844569
result:
ok 1 number(s): "507844569"
Test #5:
score: 0
Accepted
time: 242ms
memory: 3716kb
input:
100 4 8 2 5 4 4 3 0 2 7 2 3 4 4 1 2 3 4 4 4 3 3 3 3 3 2 4 1 3 5 5 1 4 6 1 1 1 3 2 3 2 1 0 1 4 4 2 4 2 5 3 5 1 6 2 3 3 1 4 4 4 1 4 4 3 4 2 0 2 3 6 1 3 3 5 4 1 1 2 3 0 3 2 2 1 3 3 2 5 6 3 2 3 3 5 4 2 3 4 4
output:
989550242
result:
ok 1 number(s): "989550242"
Test #6:
score: 0
Accepted
time: 1ms
memory: 3668kb
input:
1 1
output:
1
result:
ok 1 number(s): "1"
Test #7:
score: 0
Accepted
time: 2ms
memory: 5308kb
input:
500 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
output:
1
result:
ok 1 number(s): "1"
Test #8:
score: 0
Accepted
time: 17ms
memory: 3880kb
input:
10 2 1 3 3 2 3 1 1 3 1
output:
165452340
result:
ok 1 number(s): "165452340"
Test #9:
score: 0
Accepted
time: 2ms
memory: 3632kb
input:
20 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0
output:
2
result:
ok 1 number(s): "2"
Test #10:
score: 0
Accepted
time: 5ms
memory: 3684kb
input:
20 0 0 1 0 0 0 0 1 0 0 0 0 0 0 2 0 1 0 0 0
output:
28
result:
ok 1 number(s): "28"
Test #11:
score: 0
Accepted
time: 9ms
memory: 3592kb
input:
10 1 1 1 1 1 1 1 1 1 1
output:
16796
result:
ok 1 number(s): "16796"
Test #12:
score: 0
Accepted
time: 242ms
memory: 4436kb
input:
300 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 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 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 ...
output:
431279497
result:
ok 1 number(s): "431279497"
Test #13:
score: 0
Accepted
time: 409ms
memory: 3548kb
input:
2 232 268
output:
929717758
result:
ok 1 number(s): "929717758"
Test #14:
score: 0
Accepted
time: 408ms
memory: 3816kb
input:
1 500
output:
1
result:
ok 1 number(s): "1"
Test #15:
score: 0
Accepted
time: 408ms
memory: 3624kb
input:
3 155 180 165
output:
911108550
result:
ok 1 number(s): "911108550"
Extra Test:
score: 0
Extra Test Passed