Accoders 5.30 NOI模拟
card
这个操作交换律结合律全都没有,整体很难考虑。
对于单独的一张牌i,设
dish
写了又臭又长的55pts做法,花费了大量时间,只多拿了30pts。
主要问题在于转移是
直接把按照排名的数的顺序也存入状态中,状态数是
转移的时候不需要枚举子集。一个一个数地处理,每次把一个上一阶段的大小关系换成这一阶段的大小关系。复杂度是
#pragma GCC optimize(3)
#pragma GCC optimize("-Ofast")
#pragma GCC optimize("unroll-loops")
#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 = 10;
const int kS = 260;
const int kC = 4100;
const int P = 1e9 + 7;
int n, L, Y, X, K, dp[2][750][2][2][2][2][kN];
// (pos, msk, cx, cy, cl, csm, carry)
int f[2][2][750][2][2][2][2][kN * 2];
// (id, last, msk, cx, cy, cl, csm, carry)
inline int norm(int x) {
return x >= P ? x - P : x;
}
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;
}
int hs[kC];
std::vector<int> vc;
void init() {
int i = n - 2;
For(msk, 0, (1 << (i * 2)) - 1) {
bool fail = false;
For(j, 0, i - 1) if ((msk >> (j * 2) & 3) == 3) {
fail = true;
break;
}
if (fail)
continue;
hs[msk] = vc.size();
vc.push_back(msk);
}
}
inline int sgn(int i, int j) {
return i < j ? 0 : i > j ? 2 : 1;
}
int main() {
freopen("dish.in", "r", stdin);
freopen("dish.out", "w", stdout);
scanf("%d%d%d%d%d", &n, &L, &Y, &X, &K);
init();
int cur = 0;
int ts = 0;
For(i, 1, n - 2) ts = ts << 2 | 1;
dp[cur][hs[ts]][0][1][0][0][0] = 1;
for (int i = 0; ; ++i) {
memset(dp[cur ^ 1], 0, sizeof(dp[cur ^ 1]));
memset(f, 0, sizeof(f));
For(cy, 0, 1) For(cl, 0, 1) For(csm, 0, 1) {
int id = 0;
For(cx, 0, 1) For(c, 0, n - 1) For(idm, 0, vc.size() - 1) {
int msk = vc[idm];
int t = dp[cur][idm][cx][cy][cl][csm][c];
if (!t) continue;
For(x, 0, 1) {
int nx = cx;
if (x != (X >> i & 1)) nx = x > (X >> i & 1);
f[id][x][idm][nx][cy][cl][csm][c + x] = norm(f[id][x][idm][nx][cy][cl][csm][c + x] + t);
}
}
rFor(j, n - 3, 0) {
memset(f[id ^ 1], 0, sizeof(f[id ^ 1]));
For(cx, 0, 1) For(c, 0, n * 2 - 1) For(lst, 0, 1) For(idm, 0, vc.size() - 1) {
int msk = vc[idm];
int t = f[id][lst][idm][cx][cy][cl][csm][c];
if (!t) continue;
For(x, 0, 1) {
int cmp = msk >> (j * 2) & 3, nmsk = msk ^ (cmp << (j * 2));
if (x ^ lst) cmp = sgn(x, lst);
nmsk |= cmp << (j * 2);
f[id ^ 1][x][hs[nmsk]][cx][cy][cl][csm][c + x] = norm(f[id ^ 1][x][hs[nmsk]][cx][cy][cl][csm][c + x] + t);
}
}
id ^= 1;
}
For(lst, 0, 1) For(idm, 0, vc.size() - 1) For(cx, 0, 1) For(c, 0, n * 2 - 1) {
int msk = vc[idm];
int t = f[id][lst][idm][cx][cy][cl][csm][c];
if (!t) continue;
For(y, 0, 1) {
int ny = cy, nl = cl, nsm = csm;
if (y != (Y >> i & 1)) ny = y > (Y >> i & 1);
if (y != (L >> i & 1)) nl = y > (L >> i & 1);
if (y != (c & 1)) nsm = y < (c & 1);
dp[cur ^ 1][idm][cx][ny][nl][nsm][c >> 1] = norm(dp[cur ^ 1][idm][cx][ny][nl][nsm][c >> 1] + t);
}
}
}
cur ^= 1;
if (!(L >> i)) break;
}
int ans = 0;
For(idm, 0, vc.size() - 1) {
int msk = vc[idm];
bool fail = false;
for (int i = n - 3, t = 1; i >= 0; --i) {
int cmp = msk >> (i * 2) & 3;
if (cmp == 2) {
fail = true;
break;
}
if (cmp == 1) ++t;
else t = 1;
if (t > K) {
fail = true;
break;
}
}
if (fail) continue;
// fprintf(stderr, "dp (%d,%d,%d,%d,%d,%d) = %d\n", msk, 0, 1, 0, 1, 0, dp[cur][msk][0][1][0][1][0]);
ans = norm(ans + dp[cur][idm][0][1][0][1][0]);
For(c, 1, n - 1) {
For(i, 0, 1) { // ERR
// fprintf(stderr, "dp (%d,%d,%d,%d,%d,%d) = %d\n", msk, 0, 1, 0, i, c, dp[cur][msk][0][1][0][i][c]);
ans = norm(ans + dp[cur][idm][0][1][0][i][c]);
}
}
}
printf("%d", 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. */
string
其实贪心就能算出剩下来的东西:每次随便删两个相邻的颜色相同的就行。
然后可以简单分治。