Accoders 6.20 NOI模拟
psoet
·
·
个人记录
game
考虑最终为了保证每行合法,每行一定形如44...411...122...2或者44...433...322...2。1的上方一定是1,3的下方一定是3,2和4不能在竖直方向相邻。
于是一个合法方案可以刻画成把矩形用折线分成4个部分,跑4次dp即可,复杂度O(n^2)。
差点MLE了;忘记取前缀最大值过了所有样例,只挂了一个点,随随便便就拍出来了,充分证明对拍的必要性和充分性。
#include <bits/stdc++.h>
#define For(i, a, b) for (int i = a, i##end = b; i <= i##end; ++i)
#define rFor(i, b, a) for (int i = b, i##end = a; i >= i##end; --i)
#define eFor(i, u, v) for (int i = head[u], v = e[i].to; i; i = e[i].next, v = e[i].to)
typedef long long ll;
using std::cin;
using std::cout;
using std::endl;
typedef std::pair<int, int> pii;
#define fi first
#define se second
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template<typename T> T myrand(T l, T r) {
return std::uniform_int_distribution<T>(l, r)(rnd);
}
void close_stdio() {
std::ios::sync_with_stdio(false);
std::cerr << "DO NOT use scanf/printf!\n";
}
const int kN = 2005;
const ll kInf = 0x3f3f3f3f3f3f3f3fll;
int n, a[kN][kN], c[kN][kN];
ll s[kN][kN][5], d[4][kN][kN], pre[kN], suf[kN];
struct IO_tp {
static const int BS = 1 << 22;
char ibuf[BS], *i1 = ibuf, *i2 = ibuf;
char gc() {
if (i1 != i2) return *i1++;
i1 = ibuf, i2 = ibuf + fread(ibuf, 1, BS, stdin);
return i1 == i2 ? EOF : *i1++;
}
char obuf[BS], *o = obuf;
void pc(char c) {
o - obuf == BS ? fwrite(o = obuf, 1, BS, stdout), *o++ = c : *o++ = c;
}
~IO_tp() {
fwrite(obuf, 1, o - obuf, stdout);
}
template<typename Tp> IO_tp& operator >> (Tp &x) {
x = 0;
int sgn = 1;
char ch = gc();
while (!isdigit(ch)) {
if (ch == '-') sgn = -1;
ch = gc();
}
while (isdigit(ch)) x = x * 10 + ch - '0', ch = gc();
x *= sgn;
return *this;
}
template<typename Tp> IO_tp& operator << (Tp x) {
if (x < 0) pc('-'), x = -x;
if (x < 10) {
pc(x + '0');
} else {
(*this) << (x / 10);
pc(x % 10 + '0');
}
return *this;
}
} io;
int main() {
freopen("game.in", "r", stdin);
freopen("game.out", "w", stdout);
io >> n;
For(i, 1, n) For(j, 1, n) io >> c[i][j];
// fprintf(stderr, "io done1\n");
For(i, 1, n) {
// fprintf(stderr, "%d\n", i);
For(j, 1, n) io >> a[i][j];
}
// fprintf(stderr, "io done\n");
For(k, 1, 4) {
For(i, 1, n + 1) For(j, 1, n + 1) s[i][j][k] = s[i][j - 1][k] + (c[i][j] != k) * a[i][j];
For(j, 1, n + 1) For(i, 1, n + 1) s[i][j][k] += s[i - 1][j][k];
}
For(i, 1, n + 1) For(j, 1, n + 1) {
s[i][j][0] = s[i][j - 1][0] + a[i][j];
}
#define row(i, j, k) (s[i][j][k] - (i ? s[i - 1][j][k] : 0))
#define col(i, j, k) (s[i][j][k] - (j ? s[i][j - 1][k] : 0))
For(j, 1, n + 1) For(i, 1, n + 1) s[i][j][0] += s[i - 1][j][0];
memset(d, 0x3f, sizeof(d));
d[0][0][0] = 0;
// fprintf(stderr, "dp 0\n");
For(i, 0, n) For(j, 0, n) {
// fprintf(stderr, "d[0][%d][%d] = %lld\n", i, j, d[0][i][j]);
if (i == n && j == n) break;
if (j < n) {
d[0][i][j + 1] = std::min(d[0][i][j + 1], d[0][i][j] + (c[i][j + 1] != 4) * a[i][j + 1] + col(i - 1, j + 1, 1));
}
if (i < n) {
d[0][i + 1][j] = std::min(d[0][i + 1][j], d[0][i][j] + row(i + 1, j, 4));
}
}
// fprintf(stderr, "dp 1\n");
d[1][0][n + 1] = 0;
For(i, 0, n) rFor(j, n + 1, 1) {
// fprintf(stderr, "d[1][%d][%d] = %lld\n", i, j, d[1][i][j]);
if (i == n && j == 1) break;
if (j > 1) {
d[1][i][j - 1] = std::min(d[1][i][j - 1], d[1][i][j] + (c[i][j - 1] != 2) * a[i][j - 1] + col(i - 1, j - 1, 1));
}
if (i < n) {
d[1][i + 1][j] = std::min(d[1][i + 1][j], d[1][i][j] + (row(i + 1, n, 2) - row(i + 1, j - 1, 2)));
}
}
// fprintf(stderr, "dp 2\n");
d[2][n + 1][0] = 0;
rFor(i, n + 1, 1) For(j, 0, n) {
// fprintf(stderr, "d[2][%d][%d] = %lld\n", i, j, d[2][i][j]);
if (i == 1 && j == n) break;
if (j < n) {
d[2][i][j + 1] = std::min(d[2][i][j + 1], d[2][i][j] + (c[i][j + 1] != 4) * a[i][j + 1] + (col(n, j + 1, 3) - col(i, j + 1, 3)));
}
if (i > 1) {
d[2][i - 1][j] = std::min(d[2][i - 1][j], d[2][i][j] + row(i - 1, j, 4));
}
}
// fprintf(stderr, "dp 3\n");
d[3][n + 1][n + 1] = 0;
rFor(i, n + 1, 1) rFor(j, n + 1, 1) {
// fprintf(stderr, "d[3][%d][%d] = %lld\n", i, j, d[3 ][i][j]);
if (i == 1 && j == 1) break;
if (j > 1) {
d[3][i][j - 1] = std::min(d[3][i][j - 1], d[3][i][j] + (c[i][j - 1] != 2) * a[i][j - 1] + (col(n, j - 1, 3) - col(i, j - 1, 3)));
}
if (i > 1) {
d[3][i - 1][j] = std::min(d[3][i - 1][j], d[3][i][j] + (row(i - 1, n, 2) - row(i - 1, j - 1, 2)));
}
}
ll ans = kInf;
For(i, 0, n) {
memset(pre, 0x3f, sizeof(pre));
memset(suf, 0x3f, sizeof(suf));
ll t = kInf;
// get pre
For(j, 0, n) {
t = std::min(t, d[0][i][j] - s[i][j][1]);
pre[j] = std::min(pre[j], t + d[2][i + 1][j] - (s[n][j][3] - s[i][j][3]));
}
t = kInf;
For(j, 0, n) {
t = std::min(t, d[2][i + 1][j] - (s[n][j][3] - s[i][j][3]));
pre[j] = std::min(pre[j], t + d[0][i][j] - s[i][j][1]);
}
// get suf
t = kInf;
rFor(j, n + 1, 1) {
t = std::min(t, d[1][i][j] + s[i][j - 1][1]);
suf[j] = std::min(suf[j], t + d[3][i + 1][j] + (s[n][j - 1][3] - s[i][j - 1][3]));
}
t = kInf;
rFor(j, n + 1, 1) {
t = std::min(t, d[3][i + 1][j] + (s[n][j - 1][3] - s[i][j - 1][3]));
suf[j] = std::min(suf[j], t + d[1][i][j] + s[i][j - 1][1]);
}
t = kInf;
For(j, 0, n) {
t = std::min(t, pre[j]);
ans = std::min(ans, t + suf[j + 1]); // ERR
}
}
printf("%lld", ans);
return 0;
}
/* PAY ATTENTION TO: */
/* 1. Memory Limit, Array Size */
/* 2. Integer Overflow */
/* 3. Multi-test */
/* 4. Potential Constant-speedup */
/* Stay organized and write things down. */
huawei
一直在想什么多项式循环卷积下求逆之类的东西。
显然转移是个循环卷积,但是还要带一个常数。不过常数处理起来并不困难,因为显然对于每一行,这一行的所有常数相同,于是也可以递推出每一行的常数。循环卷积快速幂使用NTT优化,总复杂度$O(m^3 + Tm \log n \log m)$。
看起来非常麻烦但其实实现并不困难,所以动作搞快点。
```cpp
#include <bits/stdc++.h>
#define For(i, a, b) for (int i = a, i##end = b; i <= i##end; ++i)
#define rFor(i, b, a) for (int i = b, i##end = a; i >= i##end; --i)
#define eFor(i, u, v) for (int i = head[u], v = e[i].to; i; i = e[i].next, v = e[i].to)
typedef long long ll;
using std::cin;
using std::cout;
using std::endl;
typedef std::pair<int, int> pii;
#define fi first
#define se second
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template<typename T> T myrand(T l, T r) {
return std::uniform_int_distribution<T>(l, r)(rnd);
}
void close_stdio() {
std::ios::sync_with_stdio(false);
std::cerr << "DO NOT use scanf/printf!\n";
}
const int kM = 405;
const int P = 998244353;
int n, m, p, q, invp, invq, test, a[kM][kM], val[kM];
ll qpow(ll b, int k) {
ll res = 1;
for (; k; k >>= 1, b = b * b % P)
if (k & 1) res = res * b % P;
return res;
}
inline int norm(int x) {
return x >= P ? x - P : x;
}
void elim(int lim) { // [0, lim - 1] * [0, lim]
// For(i, 0, lim - 1) {
// For(j, 0, lim) fprintf(stderr, "%d ", a[i][j]);
// fprintf(stderr, "\n");
// }
For(i, 0, lim - 1) {
if (!a[i][i]) {
int p = -1;
For(j, i + 1, lim - 1) if (a[j][i]) {
p = j;
break;
}
assert(~p);
For(k, i, lim) std::swap(a[i][k], a[p][k]);
}
For(j, i + 1, lim - 1) if (a[j][i]) {
ll t = qpow(a[i][i], P - 2) * a[j][i] % P;
For(k, i, lim) a[j][k] = norm(a[j][k] + P - a[i][k] * t % P);
}
}
rFor(i, lim - 1, 0) {
ll t = qpow(a[i][i], P - 2);
a[i][i] = 1, a[i][lim] = a[i][lim] * t % P;
rFor(j, i - 1, 0) if (a[j][i]) {
a[j][lim] = norm(a[j][lim] + P - (ll)a[j][i] * a[i][lim] % P);
a[j][i] = 0;
}
val[i] = a[i][lim];
}
}
void getpq() {
ll t = qpow(p + q, P - 2);
p = p * t % P, q = q * t % P;
}
int rev[kM << 2];
void NTT(int *f, int lim, bool idft) {
For(i, 1, lim - 1) {
rev[i] = rev[i >> 1] >> 1;
if (i & 1) rev[i] |= lim >> 1;
if (rev[i] < i) std::swap(f[i], f[rev[i]]);
}
for (int h = 2; h <= lim; h <<= 1) {
int wn = qpow(3, (P - 1) / h);
for (int i = 0; i < lim; i += h) {
ll w = 1, t;
for (int j = 0; j < (h >> 1); ++j, w = w * wn % P) {
t = w * f[i + j + (h >> 1)] % P;
f[i + j + (h >> 1)] = norm(f[i + j] + P - t), f[i + j] = norm(f[i + j] + t);
}
}
}
if (idft) {
std::reverse(f + 1, f + lim);
ll inv = qpow(lim, P - 2);
For(i, 0, lim - 1) f[i] = f[i] * inv % P;
}
}
void mul(int *a, int *b, int *c, int len) { // circular convolution
static int A[kM << 2], B[kM << 2];
int lim = 1;
while (2 * (len - 1) >= lim) lim <<= 1;
std::copy(a, a + len, A);
std::fill(A + len, A + lim, 0);
std::copy(b, b + len, B);
std::fill(B + len, B + lim, 0);
NTT(A, lim, false), NTT(B, lim, false);
For(i, 0, lim - 1) A[i] = (ll)A[i] * B[i] % P;
NTT(A, lim, true);
For(i, 0, len - 1) c[i] = A[i];
For(i, len, lim - 1) c[i - len] = norm(c[i - len] + A[i]);
}
void qpow(int *a, int *res, int len, int lim) {
static int b[kM << 2];
std::copy(a, a + len, b);
std::fill(res, res + len, 0);
res[0] = 1;
for (; lim; lim >>= 1, mul(b, b, b, len)) if (lim & 1)
mul(b, res, res, len);
}
struct Matrix {
ll mt[2][2];
Matrix() {
memset(mt, 0, sizeof(mt));
}
Matrix operator *(const Matrix &rhs) const {
Matrix res;
res.mt[0][0] = (mt[0][0] * rhs.mt[0][0] + mt[0][1] * rhs.mt[1][0]) % P;
res.mt[0][1] = (mt[0][0] * rhs.mt[0][1] + mt[0][1] * rhs.mt[1][1]) % P;
res.mt[1][0] = (mt[1][0] * rhs.mt[0][0] + mt[1][1] * rhs.mt[1][0]) % P;
res.mt[1][1] = (mt[1][0] * rhs.mt[0][1] + mt[1][1] * rhs.mt[1][1]) % P;
return res;
}
};
Matrix qpow(Matrix b, int k) {
Matrix res;
res.mt[0][0] = res.mt[1][1] = 1;
for (; k; k >>= 1, b = b * b)
if (k & 1) res = res * b;
return res;
}
int coe[kM], base[kM];
int main() {
freopen("huawei.in", "r", stdin);
freopen("huawei.out", "w", stdout);
scanf("%d%d%d%d", &n, &m, &p, &q);
getpq();
invp = qpow(p, P - 2);
coe[0] = invp, coe[1] = norm(P - (ll)invp * q % P);
// For(i, 0, m - 1) fprintf(stderr, "%d ", coe[i]);
// fprintf(stderr, "\n");
qpow(coe, coe, m, n - 1);
Matrix t;
t.mt[0][0] = (ll)invp * (P + 1 - q) % P, t.mt[0][1] = P - invp, t.mt[1][1] = 1;
int C = qpow(t, n - 1).mt[0][1];
// fprintf(stderr, "C = %d\n", C);
For(i, 0, m - 1) a[0][(m - i) % m] = coe[i];
a[0][m] = norm(P - C);
For(i, 1, m - 1) {
For(j, 0, m - 1) a[i][(i - j + m) % m] = norm(a[i][(i - j + m) % m] + coe[j]);
a[i][m] = norm(P - C);
a[i][i] = norm(a[i][i] + P - p);
if ((i + m - 1) % m) {
For(j, 0, m - 1) a[i][(i - 1 - j + m * 2) % m] = norm(a[i][(i - 1 - j + m * 2) % m] + P - (ll)q * coe[j] % P);
a[i][m] = (a[i][m] + (ll)q * C) % P;
}
a[i][m] = norm(a[i][m] + 1);
}
elim(m);
// For(i, 0, m - 1) fprintf(stderr, "d[%d][%d] = %d\n", n - 1, i, val[i]);
scanf("%d", &test);
while (test--) {
int x, y;
scanf("%d%d", &x, &y);
if (x == 0 && y == 0) {
printf("0\n");
continue;
}
std::fill(coe, coe + m, 0);
coe[0] = invp, coe[1] = norm(P - (ll)invp * q % P);
qpow(coe, coe, m, n - 1 - x);
C = qpow(t, n - 1 - x).mt[0][1];
int ans = C;
For(i, 0, m - 1) ans = (ans + (ll)coe[i] * val[(y - i + m) % m]) % P;
printf("%d\n", ans);
}
return 0;
}
/* PAY ATTENTION TO: */
/* 1. Memory Limit, Array Size */
/* 2. Integer Overflow */
/* 3. Multi-test */
/* 4. Potential Constant-speedup */
/* Stay organized and write things down. */
/*
6 12 8 7
5
1 5
2 4
2 8
3 9
5 2
*/
```
#### dist
压位跑得飞快,就算卡了极限数据也只有6到7秒。
不过一般适用条件都没有这题合适(值域正好是5,ull正好开下,算的东西极其简单)。
##### 题解做法
一个简单的暴力是用`bitset`,每次询问时枚举$< x$和$\geq x$的部分算贡献,复杂度$O({cnq \over w})$。
考虑把这个东西搞到$O({cn^2 \over w})$。考虑我们在每次处理询问的时候求得无非就是`(bitset[a] << x) xor bitset[b]`的一个区间的`popcount`之和。考虑零散的偏移量后$x$,我们求的是$cnt(x,i,j)$:从$(i,j)$开始的斜线和,这个直接做复杂就是$O(w\cdot {n\over w}\cdot {n\over w}) = O({n^2\over w})$的。
```cpp
#include <bits/stdc++.h>
#pragma GCC target("popcnt")
#define For(i, a, b) for (int i = a, i##end = b; i <= i##end; ++i)
#define rFor(i, b, a) for (int i = b, i##end = a; i >= i##end; --i)
#define eFor(i, u, v) for (int i = head[u], v = e[i].to; i; i = e[i].next, v = e[i].to)
typedef long long ll;
typedef unsigned long long ull;
using std::cin;
using std::cout;
using std::endl;
typedef std::pair<int, int> pii;
#define fi first
#define se second
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template<typename T> T myrand(T l, T r) {
return std::uniform_int_distribution<T>(l, r)(rnd);
}
void close_stdio() {
std::ios::sync_with_stdio(false);
std::cerr << "DO NOT use scanf/printf!\n";
}
const int kN = 1e5 + 20;
int n, q, a[kN], b[kN];
ull mska[16][kN], mskb[16][kN];
struct IO_tp {
static const int BS = 1 << 22;
char ibuf[BS], *i1 = ibuf, *i2 = ibuf;
char gc() {
if (i1 != i2) return *i1++;
i1 = ibuf, i2 = ibuf + fread(ibuf, 1, BS, stdin);
return i1 == i2 ? EOF : *i1++;
}
char obuf[BS], *o = obuf;
void pc(char c) {
o - obuf == BS ? fwrite(o = obuf, 1, BS, stdout), *o++ = c : *o++ = c;
}
~IO_tp() {
fwrite(obuf, 1, o - obuf, stdout);
}
template<typename Tp> IO_tp& operator >> (Tp &x) {
x = 0;
int sgn = 1;
char ch = gc();
while (!isdigit(ch)) {
if (ch == '-') sgn = -1;
ch = gc();
}
while (isdigit(ch)) x = x * 10 + ch - '0', ch = gc();
x *= sgn;
return *this;
}
template<typename Tp> IO_tp& operator << (Tp x) {
if (x < 0) pc('-'), x = -x;
if (x < 10) {
pc(x + '0');
} else {
(*this) << (x / 10);
pc(x % 10 + '0');
}
return *this;
}
} io;
int main() {
freopen("dist.in", "r", stdin);
freopen("dist.out", "w", stdout);
double tim = clock();
io >> n >> q;
For(i, 1, n) io >> a[i], --a[i];
For(i, 1, n) io >> b[i], --b[i];
#define eval(x) ((1 << x) - 1)
For(i, 1, n) {
rFor(j, i + 15, i) mska[i & 15][i >> 4] = mska[i & 15][i >> 4] << 4 | eval(a[j]);
rFor(j, i + 15, i) mskb[i & 15][i >> 4] = mskb[i & 15][i >> 4] << 4 | eval(b[j]);
}
while (q--) {
int p1, p2, x, ans = 0, i = 0;
io >> p1 >> p2 >> x;
int r1 = p1 & 15, r2 = p2 & 15, b1 = p1 >> 4, b2 = p2 >> 4;
for (; i * 16 + 16 <= x; ++i) {
ans += __builtin_popcountll(mska[r1][i + b1] ^ mskb[r2][i + b2]);
}
i = i * 16;
for (; i < x; ++i) {
ans += std::abs(a[i + p1] - b[i + p2]);
}
(io << ans).pc('\n');
}
fprintf(stderr, "%lf\n", (clock() - tim) / CLOCKS_PER_SEC);
return 0;
}
/* PAY ATTENTION TO: */
/* 1. Memory Limit, Array Size */
/* 2. Integer Overflow */
/* 3. Multi-test */
/* 4. Potential Constant-speedup */
/* Stay organized and write things down. */
```