QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#183182 | #4900. 数列重排 | hos_lyric# | 45 | 43ms | 13628kb | C++14 | 8.3kb | 2023-09-19 09:22:11 | 2024-07-04 02:04:37 |
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 <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 INF = 1001001001001001001LL;
// block size >= K
Int dp[210][210];
Int calc(Int K, Int N, Int L) {
if (K == 0) {
return N * (N + 1) / 2;
}
for (Int i = 0; i <= N - K; ++i) {
fill(dp[i], dp[i] + (L + 1), -INF);
}
for (int i = 0; i <= N - K; ++i) {
chmax(dp[i][K], (i + 1) * (N - (i+K) + 1));
}
for (int i = 0; i < N - K; ++i) {
for (int j = 0; j < L; ++j) {
// extend
chmax(dp[i + 1][j + 1], dp[i][j] + (N - (i+K+1) + 1));
// skip
if (j + K <= L) {
for (Int ii = i + K + 1; ii <= N - K; ++ii) {
// +[*,ii+1), ..., +[*,ii+K-1); +[ii,ii+K); -[i+K-1,ii+K)
chmax(dp[ii][j + K], dp[i][j] + (K-1) * ((N - (ii+1) + 1) + (N - (ii+K-1) + 1)) / 2 + (ii + 1) * (N - (ii+K) + 1) - ((i+K-1) + 1) * (N - (ii+K) + 1));
}
}
}
}
Int ret = -INF;
for (int i = 0; i <= N - K; ++i) {
chmax(ret, dp[i][L]);
}
return ret;
}
void experiment() {
for (int k = 0; k <= 16; ++k) {
printf("k = %2d\n", k);
for (int n = k; n <= 16; ++n) {
for (int l = k; l <= n; ++l) {
int mx = -1;
int pm = -1;
for (int p = 0; p < 1 << n; ++p) if (__builtin_popcount(p) == l) {
int cnt = 0;
for (int i = 0; i < n; ++i) {
int now = 0;
for (int j = i; j < n; ++j) {
now += (p >> j & 1);
if (now >= k) ++cnt;
}
}
if (chmax(mx, cnt)) {
pm = p;
}
}
const Int res = calc(k, n, l);
printf("%2d %2d: %3d %3lld ", n, l, mx, res);
for (int i = 0; i < n; ++i) printf("%d", pm >> i & 1);
puts("");
assert(mx == res);
}
}
fflush(stdout);
}
}
int M, L, R;
Int X, N;
char S[10'000'010];
namespace brute {
vector<Int> run() {
vector<int> as;
for (int a = 0; a < M; ++a) {
for (int x = 0; x < X + (S[a] - '0'); ++x) {
as.push_back(a);
}
}
vector<Int> ans(M + 1, 0);
vector<vector<int>> opt(M + 1);
do {
vector<Int> cnt(M + 1, 0);
for (int i = 0; i < N; ++i) {
int app = 0;
for (int j = i; j < N; ++j) {
app |= 1 << as[j];
++cnt[__builtin_ctz(~app)];
}
}
for (int k = M; --k >= 0; ) {
cnt[k] += cnt[k + 1];
}
for (int k = L; k <= R; ++k) {
if (chmax(ans[k], cnt[k])) {
opt[k] = as;
}
}
} while (next_permutation(as.begin(), as.end()));
for(int k=L;k<=R;++k)cerr<<k<<": "<<ans[k]<<"; "<<opt[k]<<endl;
return ans;
}
} // brute
namespace sub4 {
vector<Int> run() {
vector<Int> ans(M + 1, 0);
ans[0] = N * (N + 1) / 2;
ans[1] = N * (N + 1) / 2 - ((1 < M) ? (X + (S[1] - '0')) : 0);
return ans;
}
} // sub4
namespace sub5 {
vector<Int> run() {
vector<Int> ans(M + 1, 0);
ans[M] = N * (N + 1) / 2;
for (int i = 1; i < M; ++i) {
ans[M] -= (N + 1 - i);
}
return ans;
}
} // sub5
namespace sub6 {
vector<Int> run() {
vector<Int> ans(M + 1, 0);
for (int k = L; k <= R; ++k) {
if (k) {
const Int nL = (N - k) / 2;
const Int nR = (N - k) - nL;
ans[k] = (nL + 1) * (nR + 1);
} else {
ans[k] = N * (N + 1) / 2;
}
}
return ans;
}
} // sub6
namespace slow {
vector<Int> run() {
vector<Int> pre(M + 1, 0);
for (int a = 0; a < M; ++a) {
pre[a + 1] = pre[a] + (X + (S[a] - '0'));
}
vector<Int> ans(M + 1, 0);
for (int k = L; k <= R; ++k) {
ans[k] = calc(k, N, pre[k]);
}
return ans;
}
} // slow
int main() {
// experiment(); return 0;
for (; ~scanf("%d%d%d%lld", &M, &L, &R, &X); ) {
scanf("%s", S);
N = M * X + count(S, S + M, '1');
cerr<<"M = "<<M<<", N = "<<N<<endl;
vector<Int> ans;
if (M <= 2 && L == 0 && R == 1) {
cerr<<"sub4"<<endl;
ans = sub4::run();
} else if (L == M && R == M) {
cerr<<"sub5"<<endl;
ans = sub5::run();
} else if (N == M) {
cerr<<"sub6"<<endl;
ans = sub6::run();
} else if (N <= 200) {
cerr<<"slow"<<endl;
ans = slow::run();
} else {
assert(false);
}
cerr<<"ans = "<<ans<<endl;
#ifdef LOCAL
if(N<=9){
const auto brt=brute::run();
if(brt!=ans){
cerr<<"M = "<<M<<", L = "<<L<<", R = "<<R<<", X = "<<X<<", N = "<<N<<", S = "<<S<<endl;
cerr<<"brt = "<<brt<<endl;
cerr<<"ans = "<<ans<<endl;
}
assert(brt==ans);
}
#endif
unsigned key = 0;
Mint wt = Mint(233).pow(L);
for (int k = L; k <= R; ++k) {
key ^= (wt * ans[k]).x;
wt *= 233;
}
printf("%u\n", key);
}
return 0;
}
詳細信息
Subtask #1:
score: 5
Accepted
Test #1:
score: 5
Accepted
time: 1ms
memory: 5832kb
input:
2 0 2 2 01
output:
541257
result:
ok 1 number(s): "541257"
Test #2:
score: 5
Accepted
time: 0ms
memory: 3852kb
input:
4 1 4 2 00001
output:
525797597
result:
ok 1 number(s): "525797597"
Test #3:
score: 5
Accepted
time: 0ms
memory: 3780kb
input:
9 0 9 1 000000000
output:
711136343
result:
ok 1 number(s): "711136343"
Test #4:
score: 5
Accepted
time: 0ms
memory: 3780kb
input:
1 0 1 9 0
output:
10456
result:
ok 1 number(s): "10456"
Test #5:
score: 5
Accepted
time: 0ms
memory: 3736kb
input:
2 1 2 3 11
output:
1518844
result:
ok 1 number(s): "1518844"
Subtask #2:
score: 15
Accepted
Dependency #1:
100%
Accepted
Test #6:
score: 15
Accepted
time: 43ms
memory: 4180kb
input:
21 0 21 9 111010011100100100000
output:
171658329
result:
ok 1 number(s): "171658329"
Test #7:
score: 15
Accepted
time: 1ms
memory: 3716kb
input:
200 0 200 1 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
output:
287932632
result:
ok 1 number(s): "287932632"
Test #8:
score: 15
Accepted
time: 10ms
memory: 4060kb
input:
120 3 119 1 101000110101001100011100001011101110101010000011101110010101101000111100111100001001010010110001110011001010110001101111
output:
856785458
result:
ok 1 number(s): "856785458"
Test #9:
score: 15
Accepted
time: 9ms
memory: 4064kb
input:
2 0 2 99 10
output:
67513337
result:
ok 1 number(s): "67513337"
Subtask #3:
score: 0
Runtime Error
Dependency #2:
100%
Accepted
Test #10:
score: 0
Runtime Error
input:
10 1 9 499 0110011010
output:
result:
Subtask #4:
score: 5
Accepted
Test #14:
score: 5
Accepted
time: 0ms
memory: 3976kb
input:
2 0 1 114514 10
output:
934764137
result:
ok 1 number(s): "934764137"
Test #15:
score: 5
Accepted
time: 0ms
memory: 3736kb
input:
2 0 1 1919810 01
output:
685371514
result:
ok 1 number(s): "685371514"
Test #16:
score: 5
Accepted
time: 0ms
memory: 3724kb
input:
2 0 1 500000000 00
output:
318651831
result:
ok 1 number(s): "318651831"
Subtask #5:
score: 10
Accepted
Test #17:
score: 10
Accepted
time: 6ms
memory: 13628kb
input:
1000000 1000000 1000000 928 01100010010000000101111110001111011101111000011110100101011110011001001000011000110101101100111110000100101010111001111100010011100110000000111110110100001100000000011101100001010001010000010000001001000110011111010101111100001001110110010100000011000010010001111010011100...
output:
437299311
result:
ok 1 number(s): "437299311"
Test #18:
score: 10
Accepted
time: 0ms
memory: 3740kb
input:
100 100 100 10000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
output:
119118463
result:
ok 1 number(s): "119118463"
Subtask #6:
score: 10
Accepted
Test #19:
score: 10
Accepted
time: 7ms
memory: 12000kb
input:
1000000 0 1000000 1 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
852768823
result:
ok 1 number(s): "852768823"
Test #20:
score: 10
Accepted
time: 8ms
memory: 11992kb
input:
1000000 0 1000000 1 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
852768823
result:
ok 1 number(s): "852768823"
Subtask #7:
score: 0
Runtime Error
Test #21:
score: 0
Runtime Error
input:
1000000 0 9823 627 01110001011101001100010011100101001011000011011110001101010000000101010111110111110010010001110100101001111000111100011101111001000000100111000010010100010101110110111110100010101010001110111001100011010001111000101010000110010010101110101010111110110001110111111000001110000110011...
output:
result:
Subtask #8:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
0%
Subtask #9:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
0%