如果给定了想输入的序列,怎么判定合法?显然是一个 DP,考虑 表示前 个是否可以完全匹配。 从 转移而来。需要判定数字可重集是否相等。
考虑原计数问题,将判定性问题的值作为状态。具体地,记 表示前 个字符,倒数三个分别是 ,目前 能否恰好完全匹配。转移枚举 的数字是什么即可。
这个复杂度已经达到了 ,不能再低了。但是状态数上的常数有点大。具体来说是 这么多。估计不太可过。考虑一些可以合并的状态。
设 位的数字为 。注意到 的作用仅在于 四个一起合并。若 无法恰好匹配,那么 无论是多少答案都一样,直接合并。同样若 与 都无法完全匹配。 也是可以合并的。同理, 和 也是这样。
进一步,我们发现若 不同时在题面中要求的四个连通块中,那么 也没有意义。同理,若 不在一起, 也没有意义。这样大部分状态都被合并了。
这么写一下发现最高状态数大概是 的样子,已经可以通过了。不推荐使用 unordered_map 代替 DP 数组,常数过大。空间开不下可以考虑滚动数组。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <random>
#include <unordered_map>
using namespace std;
using ll = int;
using ull = unsigned long long;
const int N = 1e5 + 5;
const ll MOD = 1e9 + 7;
int t, n;
string s;
hash<int> hsi;
hash<bool> hsb;
int R1 = 6172, R2 = 38182, R3 = 192838, R4 = 91922;
mt19937 rnd(114514);
uniform_int_distribution<ull> rg(1, ULLONG_MAX);
struct State
{
int a, b, c;
bool d, e, f, g;
State() = default;
State(int a, int b, int c, bool d, bool e, bool f, bool g)
: a(a), b(b), c(c), d(d), e(e), f(f), g(g)
{
}
bool operator==(const State& other) const
{
return a == other.a && b == other.b && c == other.c && d == other.d && e == other.e && f == other.f && g == other.g;
}
};
struct MyHash
{
size_t operator()(const State& x) const
{
return hsi(x.a) ^ hsi(x.b) ^ hsi(x.c) ^ hsb(x.d) * R1 ^ hsb(x.e) * R2 ^ hsb(x.f) * R3 ^ hsb(x.g) * R4;
}
};
ll f[10][10][10][2][2][2][2], g[10][10][10][2][2][2][2];
ull wv[10];
inline bool check4(int a, int b, int c, int d)
{
if (a > b) swap(a, b);
if (a > c) swap(a, c);
if (a > d) swap(a, d);
if (b > c) swap(b, c);
if (b > d) swap(b, d);
if (c > d) swap(c, d);
if (a == 1 && b == 2 && c == 4 && d == 5) return 1;
else if (a == 2 && b == 3 && c == 5 && d == 6) return 1;
else if (a == 4 && b == 5 && c == 7 && d == 8) return 1;
else if (a == 5 && b == 6 && c == 8 && d == 9) return 1;
return 0;
}
inline bool check2(int a, int b)
{
if (a > b) swap(a, b);
if (b == a + 1 && (a % 3)) return 1;
if (b - a == 3) return 1;
return 0;
}
bool ok[10][10][10];
bool ok2[10][10];
inline bool check4_2(int a, int b)
{
if (a > b) swap(a, b);
return ok2[a][b];
}
inline bool check4_3(int a, int b, int c)
{
if (a > b) swap(a, b);
if (a > c) swap(a, c);
if (b > c) swap(b, c);
return ok[a][b][c];
}
inline State trans(State x)
{
if (!check4_3(x.a, x.b, x.c)) x.a = x.d = 0;
if (!check4_2(x.b, x.c)) x.b = x.e = 0;
if (!x.d) x.a = 0;
if (!x.d && !x.e) x.b = 0;
if (!x.d && !x.e && !x.f) x.c = 0;
if (!x.d && !x.e && !x.f && !x.g) x.a = x.b = x.c = 0;
return x;
}
ull ss[N];
inline bool chk(int l, int r, ull sum)
{
if (l <= 0 || r > n) return 0;
return ss[r] - ss[l - 1] == sum;
}
bool vis[10][10][10][2][2][2][2];
int main()
{
ios::sync_with_stdio(0), cin.tie(0);
ok[1][2][4] = ok[1][2][5] = ok[2][4][5] = ok[1][4][5] = ok[2][3][5] = ok[2][3][6] = ok[2][5][6] = ok[3][5][6] = 1;
ok[4][5][7] = ok[4][5][8] = ok[4][7][8] = ok[5][7][8] = 1;
ok[5][6][8] = ok[5][6][9] = ok[5][8][9] = ok[6][8][9] = 1;
ok2[1][2] = ok2[1][4] = ok2[1][5] = ok2[2][4] = ok2[2][5] = ok2[4][5] = 1;
ok2[2][3] = ok2[2][5] = ok2[2][6] = ok2[3][5] = ok2[3][6] = ok2[5][6] = 1;
ok2[4][5] = ok2[4][7] = ok2[4][8] = ok2[5][7] = ok2[5][8] = ok2[7][8] = 1;
ok2[5][6] = ok2[5][8] = ok2[5][9] = ok2[6][8] = ok2[8][9] = ok2[6][9] = 1;
for (int i = 0; i <= 9; i++) wv[i] = rg(rnd);
cin >> t;
while (t--)
{
cin >> s;
n = s.size();
s = " " + s;
vector<State> all;
f[0][0][0][1][1][1][1] = 1;
all.emplace_back(0, 0, 0, 1, 1, 1, 1);
for (int i = 1; i <= n; i++) ss[i] = ss[i - 1] + wv[s[i] - '0'];
//int res = 0;
for (int i = 0; i < n; i++)
{
vector<State> nse;
for (auto& x : all)
{
auto& [a, b, c, d, e, f, g] = x;
if (!d && !e && !f && !g) continue;
for (int na = 0; na <= 9; na++)
{
State ns;
ns.a = b, ns.b = c, ns.c = na;
ns.d = e, ns.e = f, ns.f = g;
ns.g = (d && check4(a, b, c, na) && chk(i - 2, i + 1, wv[a] + wv[b] + wv[c] + wv[na])) || (f && check2(c, na) && chk(i, i + 1, wv[c] + wv[na])) || (g && na == s[i + 1] - '0');
//cout << "!!!: " << i + 1 << " " << ns.a << " " << ns.b << " " << ns.c << " " << ns.d << " " << ns.e << " " << ns.f << " " << ns.g << "\n";
//cout << "na: " << na << " " << s[i + 1] - '0' << "\n";
//cout << "!!!!!!!: " << (ns.g && na == s[i + 1] - '0') << "\n";
//system("pause");
ns = trans(ns);
if (!vis[ns.a][ns.b][ns.c][ns.d][ns.e][ns.f][ns.g])
{
nse.emplace_back(ns);
vis[ns.a][ns.b][ns.c][ns.d][ns.e][ns.f][ns.g] = 1;
}
::g[ns.a][ns.b][ns.c][ns.d][ns.e][ns.f][ns.g] = (::g[ns.a][ns.b][ns.c][ns.d][ns.e][ns.f][ns.g] + ::f[a][b][c][d][e][f][g]) % MOD;
}
}
for (auto& [a, b, c, d, e, ff, g] : all) f[a][b][c][d][e][ff][g] = 0ll;
for (auto& [a, b, c, d, e, f, gg] : nse) ::f[a][b][c][d][e][f][gg] = g[a][b][c][d][e][f][gg], vis[a][b][c][d][e][f][gg] = 0, g[a][b][c][d][e][f][gg] = 0;
all.swap(nse);
}
ll ans = 0ll;
for (auto& x : all)
{
if (x.g) ans = (ans + f[x.a][x.b][x.c][x.d][x.e][x.f][x.g]) % MOD;
f[x.a][x.b][x.c][x.d][x.e][x.f][x.g] = 0;
}
//cout << "max_state: " << res << "\n";
cout << ans << "\n";
}
return 0;
}