QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#183697 | #4900. 数列重排 | hos_lyric | 100 ✓ | 176ms | 91204kb | C++14 | 7.8kb | 2023-09-19 19:24:22 | 2023-09-19 19:24:22 |
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;
/*
(N-L) 0's, L 1's
maximize # of intervals containing K 1's
0^x[0] 1^y[0] 0^x[1] 1^y[1] ... 0^x[p-1] 1^y[p-1] 0^x[p]
y[i] >= K (looks good)
bad intervals:
0...0: \sum[0<=i<=p] x[i]*(x[i]+1)/2
1...1: \sum[1<=k<=K-1] (L+1-k) (fixed)
0...1, 1...0: (K-1) x[0] + \sum[1<=i<=p-1] 2 (K-1) x[i] + (K-1) x[p]
x[0], x[p]: almost same
x[1], ..., x[p-1]: almost same
maximize p
*/
Int slow(Int K, Int N, Int L) {
assert(K <= L); assert(L <= N);
if (K == 0) {
return N * (N + 1) / 2;
}
auto costEnd = [&](Int x) -> Int {
return x*(x+1)/2 + (K-1) * x;
};
auto costMid = [&](Int x) -> Int {
return x*(x+1)/2 + 2 * (K-1) * x;
};
Int mn = INF;
const Int p = L / K;
if (p == 1) {
const Int x0 = (N-L) / 2;
const Int xp = (N-L) - x0;
Int cost = 0;
cost += costEnd(x0);
cost += costEnd(xp);
chmin(mn, cost);
} else {
// fix x[0] + x[p] = s
for (Int s = 0; s <= N-L; ++s) {
const Int x0 = s / 2;
const Int xp = s - x0;
const Int q = (N-L - s) / (p-1);
const Int r = (N-L - s) % (p-1);
Int cost = 0;
cost += costEnd(x0);
cost += costEnd(xp);
cost += (p-1 - r) * costMid(q);
cost += r * costMid(q+1);
chmin(mn, cost);
}
}
Int ret = N * (N + 1) / 2;
ret -= (K-1) * (L + (L-K+2)) / 2;
ret -= mn;
return ret;
}
/*
x[0] + x[1] + ... + x[p-1] + x[p] = N-L
minimize f(x[0]) + g(x[1]) + ... + g(x[p-1]) + f(x[p])
f(x) := x*(x+1)/2 + (K-1) x
g(x) := x*(x+1)/2 + 2 (K-1) x
f(x+1) - f(x) = x + K
g(x+1) - g(x) = x + (2K-1)
*/
Int fast(Int K, Int N, Int L) {
assert(K <= L); assert(L <= N);
if (K == 0) {
return N * (N + 1) / 2;
}
const Int p = L / K;
Int cost = 0;
auto f = [&](Int x) -> Int {
return x*(x+1)/2 + (K-1) * x;
};
auto g = [&](Int x) -> Int {
return x*(x+1)/2 + 2 * (K-1) * x;
};
if (N-L < 2 * (K-1)) {
const Int x0 = (N-L) / 2;
const Int xp = (N-L) - x0;
cost += f(x0);
cost += f(xp);
} else {
const Int lot = (N-L) - 2 * (K-1);
const Int q = lot / (p + 1);
const Int r = lot % (p + 1);
cost += f((K-1) + q + ((0 < r) ? 1 : 0));
cost += f((K-1) + q + ((1 < r) ? 1 : 0));
cost += max(r - 2, 0LL) * g(q + 1);
cost += (p-1 - max(r - 2, 0LL)) * g(q);
}
Int ret = N * (N + 1) / 2;
ret -= (K-1) * (L + (L-K+2)) / 2;
ret -= cost;
return ret;
}
void stress() {
constexpr int lim = 20;
for (int k = 0; k <= lim; ++k) {
printf("k = %2d\n", k);
for (int n = k; n <= lim; ++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 slw = slow(k, n, l);
const Int fst = fast(k, n, l);
printf("%2d %2d %2d: %3d %3lld %3lld ", k, n, l, mx, slw, fst);
for (int i = 0; i < n; ++i) printf("%d", pm >> i & 1);
puts("");
assert(mx == slw);
assert(mx == fst);
}
}
fflush(stdout);
}
}
int M, L, R;
Int X, N;
char S[10'000'010];
int main() {
// stress(); 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(M + 1, 0);
Int sum = 0;
for (int k = 0; ; ++k) {
ans[k] = fast(k, N, sum);
if (k == M) break;
sum += X + (S[k] - '0');
}
// cerr<<"ans = "<<ans<<endl;
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: 3732kb
input:
2 0 2 2 01
output:
541257
result:
ok 1 number(s): "541257"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3612kb
input:
4 1 4 2 00001
output:
525797597
result:
ok 1 number(s): "525797597"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3700kb
input:
9 0 9 1 000000000
output:
711136343
result:
ok 1 number(s): "711136343"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3616kb
input:
1 0 1 9 0
output:
10456
result:
ok 1 number(s): "10456"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3748kb
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: 1ms
memory: 3668kb
input:
21 0 21 9 111010011100100100000
output:
171658329
result:
ok 1 number(s): "171658329"
Test #7:
score: 0
Accepted
time: 0ms
memory: 3784kb
input:
200 0 200 1 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
output:
287932632
result:
ok 1 number(s): "287932632"
Test #8:
score: 0
Accepted
time: 1ms
memory: 3680kb
input:
120 3 119 1 101000110101001100011100001011101110101010000011101110010101101000111100111100001001010010110001110011001010110001101111
output:
856785458
result:
ok 1 number(s): "856785458"
Test #9:
score: 0
Accepted
time: 0ms
memory: 3728kb
input:
2 0 2 99 10
output:
67513337
result:
ok 1 number(s): "67513337"
Subtask #3:
score: 15
Accepted
Dependency #2:
100%
Accepted
Test #10:
score: 15
Accepted
time: 0ms
memory: 3684kb
input:
10 1 9 499 0110011010
output:
47418354
result:
ok 1 number(s): "47418354"
Test #11:
score: 0
Accepted
time: 0ms
memory: 3700kb
input:
100 0 100 49 1100100011111101111111001000000100010000101010110000011011110100100011111000111101100010001000001100
output:
100314042
result:
ok 1 number(s): "100314042"
Test #12:
score: 0
Accepted
time: 1ms
memory: 3688kb
input:
1000 0 1000 4 1011110001101000100110000111011110101100110011100010001100001101000111100011100011110101000010000100101011010110000110100011011010011000111100100100100001000011001000000000111001010001000000110001001011100010011101010011011110001101000010010000101000100001111101001100100001010010001100...
output:
738329201
result:
ok 1 number(s): "738329201"
Test #13:
score: 0
Accepted
time: 1ms
memory: 3776kb
input:
5000 0 5000 1 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
76076468
result:
ok 1 number(s): "76076468"
Subtask #4:
score: 5
Accepted
Test #14:
score: 5
Accepted
time: 0ms
memory: 3732kb
input:
2 0 1 114514 10
output:
934764137
result:
ok 1 number(s): "934764137"
Test #15:
score: 0
Accepted
time: 0ms
memory: 3680kb
input:
2 0 1 1919810 01
output:
685371514
result:
ok 1 number(s): "685371514"
Test #16:
score: 0
Accepted
time: 0ms
memory: 3700kb
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: 12ms
memory: 14036kb
input:
1000000 1000000 1000000 928 01100010010000000101111110001111011101111000011110100101011110011001001000011000110101101100111110000100101010111001111100010011100110000000111110110100001100000000011101100001010001010000010000001001000110011111010101111100001001110110010100000011000010010001111010011100...
output:
437299311
result:
ok 1 number(s): "437299311"
Test #18:
score: 0
Accepted
time: 0ms
memory: 3696kb
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: 11ms
memory: 11924kb
input:
1000000 0 1000000 1 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
852768823
result:
ok 1 number(s): "852768823"
Test #20:
score: 0
Accepted
time: 14ms
memory: 12024kb
input:
1000000 0 1000000 1 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
852768823
result:
ok 1 number(s): "852768823"
Subtask #7:
score: 15
Accepted
Test #21:
score: 15
Accepted
time: 13ms
memory: 11996kb
input:
1000000 0 9823 627 01110001011101001100010011100101001011000011011110001101010000000101010111110111110010010001110100101001111000111100011101111001000000100111000010010100010101110110111110100010101010001110111001100011010001111000101010000110010010101110101010111110110001110111111000001110000110011...
output:
383638431
result:
ok 1 number(s): "383638431"
Test #22:
score: 0
Accepted
time: 11ms
memory: 11888kb
input:
1000000 456755 465755 982 0100111111100111100010100011110111111101011111101110010011101011110011111010110000110001101001011101000110111100110100101111101011111010101011101000011101100000000111000010101011011000111010101101111011100101010010000110101011110010101011111101110101100010000100001110000100...
output:
982882798
result:
ok 1 number(s): "982882798"
Test #23:
score: 0
Accepted
time: 1ms
memory: 3684kb
input:
1000 0 1000 999999 11101000001100101110100011011111010000110101000000010101111010110110100110000001101110100011010111001000000010110101110001010111101000100010010010110000000001011110010010101111110000100001000111000010110001100100011100001000111001110110001010100000110110000110001111101101000010111...
output:
337235666
result:
ok 1 number(s): "337235666"
Test #24:
score: 0
Accepted
time: 12ms
memory: 11888kb
input:
1000000 990001 1000000 999 110110000110101111001000001001011000010000101001111110001101100011001001111001010001110110010111110100000101110000101101000111001100010100010100001001001011011100001010111100110110011001110010001110011001111100011001100111110000111011011100100100011111101100011000100011011...
output:
794028825
result:
ok 1 number(s): "794028825"
Subtask #8:
score: 15
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Dependency #5:
100%
Accepted
Dependency #6:
100%
Accepted
Dependency #7:
100%
Accepted
Test #25:
score: 15
Accepted
time: 14ms
memory: 14024kb
input:
1000000 1 999999 632 111111100000011110001111101100010010111100000010101111111001010001101011101110001010010101001000111110101010100010011001101111011111011000011111011100101011110011000100100110111100101010000110010011110010111011001011001001001111000100000011101001100011011001100100011010000010100...
output:
610044514
result:
ok 1 number(s): "610044514"
Test #26:
score: 0
Accepted
time: 18ms
memory: 13524kb
input:
1000000 0 1000000 888 11011001110110111010100110110001000101110100001111110000011110101010110001100101010001110101010111001110100000110101000000011001111100100000010101100000011100110001011010110100001100111010100111011000110100100101110010100001100100000101000001101011010111001000101001110001111000...
output:
255140225
result:
ok 1 number(s): "255140225"
Test #27:
score: 0
Accepted
time: 21ms
memory: 11988kb
input:
1000000 0 1000000 1000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
1066353724
result:
ok 1 number(s): "1066353724"
Test #28:
score: 0
Accepted
time: 0ms
memory: 3676kb
input:
100 0 100 9999999 1000101010111010100001001100001001100101000000111000000000111010000000000000101001110011001000111011
output:
583823543
result:
ok 1 number(s): "583823543"
Test #29:
score: 0
Accepted
time: 40ms
memory: 22760kb
input:
2000000 0 2000000 499 11100010011000101100101001000010010011110000000110011101001000100010101101010001011001001011100000111010100110011111001100100101000001000010010000000000011111011100100100110001110011101100110101001011011101001111010100010011001101111110110100010011110111010110100000101100101100...
output:
356463868
result:
ok 1 number(s): "356463868"
Test #30:
score: 0
Accepted
time: 36ms
memory: 20868kb
input:
2000000 0 2000000 499 00010101111100001110110010101100100111001010011100011011001100101001001111110001010011100000001101010001111000000010111011111110111010111010010010000011011110001110010000000000110000101110110101010011101101011001011001011110100010101000000011010100010010101000010111101110000111...
output:
111807544
result:
ok 1 number(s): "111807544"
Subtask #9:
score: 10
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Dependency #5:
100%
Accepted
Dependency #6:
100%
Accepted
Dependency #7:
100%
Accepted
Dependency #8:
100%
Accepted
Test #31:
score: 10
Accepted
time: 171ms
memory: 90948kb
input:
10000000 0 10000000 99 0011100100001001101101010011001111100000001110100000000100110100110110111110010010001001100100000111111100111100001110101001011101000101001001001010001011110101001101011001110011100011010101001101001111000010110011010000011011110110100001001101110111101101010011010011111111011...
output:
704917900
result:
ok 1 number(s): "704917900"
Test #32:
score: 0
Accepted
time: 176ms
memory: 91204kb
input:
10000000 0 10000000 99 1101011001100000100101000101100000011010001111111111000000001011001111101101100100001100001100000001000011110001101010001001100001011110100101011010000011001011110010011000111101100000001100001100000100000011000101110000001001100111001011011000101110001101110010001110001110111...
output:
850705320
result:
ok 1 number(s): "850705320"