由于 是大水, 是大分讨,这里我们仅考虑 。
1. 2 Directions vs 4 Directions
问题分析
首先,我们来分析游戏的规则。由于 Alice 只能在水平方向移动,而 Bob 可以在四个方向自由移动,这决定了 Alice 的防守策略必须极其严密。
然后,我们可以发现,为了不让 Bob 逃脱,Alice 必须在网格中构建一个贯穿第一行到最后一行的“安全通道”。我们记这条通道在各行的列坐标为 ,其中相邻两行的列坐标变化需要满足 。
这个通道本身的格子不需要被涂白,但为了堵住 Bob 的所有去路,通道在网格内的左右邻居 和 必须全部被涂白。
同时,由于每次移动都会改变网格坐标 的奇偶性,且双方各自移动了偶数步,所以终点格子的奇偶性必然与起点 相同。由此可见,通道 的奇偶性 必须与起点相反,即 。此外,在第 行,通道不能包含起点本身,因此通道的列坐标 只能在 中进行选择。
算法与状态设计
我们定义在第 行选择 作为通道时,涂白邻居的花费为 (超出边界的项不计入)。
为了求出最小代价,我们考虑双向动态规划。首先,从上往下递推:
然后,从下往上递推:
最后,对于给定的起点 ,令通道的奇偶性为 ,我们只需要枚举通道在第 行的列坐标 并取最小值即可:
复杂度分析
- 时间复杂度:。双向 DP 预处理需要 ,单次查询可以做到 。
- 空间复杂度:。
Code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll inf = 1e16;
ll dpt[2][505][505];
ll dpb[2][505][505];
ll a[505][505];
ll v[505][505];
void solve(){
int n; cin >> n;
for (int i = 1;i <= n;i++)
for (int j = 1;j <= n;j++)
cin >> a[i][j];
for (int i = 1;i <= n;i++)
for (int j = 1;j <= n;j++){
ll val = 0;
if (j > 1) val += a[i][j - 1];
if (j < n) val += a[i][j + 1];
v[i][j] = val;
}
for (int p = 0;p < 2;p++)
for (int i = 1;i <= n;i++)
for (int j = 1;j <= n;j++){
dpt[p][i][j] = inf;
dpb[p][i][j] = inf;
}
for (int p = 0;p < 2;p++){
for (int j = 1;j <= n;j++)
if ((1 + j) % 2 == p)
dpt[p][1][j] = v[1][j];
for (int i = 2;i <= n;i++)
for (int j = 1;j <= n;j++)
if ((i + j) % 2 == p){
ll pre = inf;
if (j > 1) pre = min(pre,dpt[p][i - 1][j - 1]);
if (j < n) pre = min(pre,dpt[p][i - 1][j + 1]);
if (pre != inf){
dpt[p][i][j] = pre + v[i][j];
}
}
}
for (int p = 0;p < 2;p++){
for (int j = 1;j <= n;j++)
if ((n + j) % 2 == p)
dpb[p][n][j] = v[n][j];
for (int i = n - 1;i >= 1;i--)
for (int j = 1;j <= n;j++)
if ((i + j) % 2 == p){
ll nxt = inf;
if (j > 1) nxt = min(nxt,dpb[p][i + 1][j - 1]);
if (j < n) nxt = min(nxt,dpb[p][i + 1][j + 1]);
if (nxt != inf)
dpb[p][i][j] = nxt + v[i][j];
}
}
for (int h = 1;h <= n;h++){
for (int w = 1;w <= n;w++){
ll ans = inf;
int p = 1 - (h + w) % 2;
for (int d = -1;d <= 1;d += 2){
int c = w + d;
if (c >= 1 && c <= n)
if (dpt[p][h][c] != inf && dpb[p][h][c] != inf)
ans = min(ans,dpt[p][h][c] + dpb[p][h][c] - v[h][c]);
}
cout << ans << (w == n ? "" : " ");
}
cout << "\n";
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t; cin >> t;
while (t--)
solve();
return 0;
}2. Shift and Add
问题分析
首先,根据题目定义,每次选择的数值 均小于 。这意味着每次加上新数时,只有最低的几位会产生复杂的进位,更高的数位只会被简单的进位传播所波及。
由此,我们可以将 拆分为高位 与低 位 ,表示为 。由于 ,它的值仅仅由最后 次的选择状态决定。这使我们可以用一个 位的二进制掩码 来表示当前的低位选择状态。
然后,考虑高位的递推关系。因为 ,其中 是低位合并时向高位产生的进位, 是新加入数字对高位的直接贡献。
算法与状态设计
我们设计状态 表示低位选择状态为 ,且当前低位向高位进位为 时,高位部分的最小数位和。由于每次转移只需要考虑上一阶段的状态,且进位 的范围仅在 内,状态空间极小。
对于每一阶段,我们枚举当前的选择 :
- 转移后的新状态为 。
- 合并低位得到 。
- 计算产生的新进位 和当前高位值 。
- 得到转移方程: 其中新的进位 。
最后,第 步的最小数位和即为所有状态中高位数位和 加上低位 的数位和的最小值。
复杂度分析
- 时间复杂度:。状态数仅为 ,跑得飞起。
- 空间复杂度:,仅需要滚动保存当前阶段的 DP 数组。
Code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll B = 1000000000LL;
int sumdig(ll x){
int s = 0;
while (x > 0){
s += x % 10;
x /= 10;
}
return s;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
vector <ll> a(n + 1),b(n + 1);
for (int i = 1;i <= n;++i) cin >> a[i];
for (int i = 1;i <= n;++i) cin >> b[i];
vector <vector<int>> dp(256,vector<int>(3,1e9));
vector <ll> curl(256,0);
dp[0][0] = 0;
dp[0][1] = 1;
dp[0][2] = 2;
for (int i = 1;i <= n;++i){
vector <vector<int>> ndp(256,vector<int>(3,1e9));
vector <ll> nxtl(256,0);
for (int s = 0;s < 256;++s){
if (dp[s][0] == 1e9 && dp[s][1] == 1e9 && dp[s][2] == 1e9) continue;
for (int c = 0;c < 2;++c){
ll val = (c == 0) ? a[i] : b[i];
int nxts = ((s & 127) << 1) | c;
ll sumval = 10 * curl[s] + val;
ll q = sumval / B;
ll d = (sumval % B) / (B / 10);
nxtl[nxts] = sumval % (B / 10);
for (int cy = 0;cy < 3;++cy){
ll tot = d + cy;
ll ncy = q + tot / 10;
if (dp[s][ncy] != 1e9)
ndp[nxts][cy] = min(ndp[nxts][cy],dp[s][ncy] + (int)(tot % 10));
}
}
}
dp = ndp;
curl = nxtl;
int ans = 1e9;
for (int s = 0;s < 256;++s)
if (dp[s][0] != 1e9)
ans = min(ans,dp[s][0] + sumdig(curl[s]));
cout << ans << (i == n ? "" : " ");
}
return 0;
}3. XOR Matching
问题分析
首先分析异或的性质。对于 ,每个元素 只能与 进行配对。由于这些数对之间互不重叠,我们很容易发现最大配对数就是每个数对中较小出现次数的和:
设 ,我们需要对所有 快速求出 的值。
然后,利用一个经典的拆分方法,由于 ,我们可以引入指示向量 。那么 就可以表示为:
其中的 表示异或卷积。
直接对所有层进行快速沃尔什变换(FWHT)的复杂度高达 ,显然会超时。
算法与优化
为了降低复杂度,我们可以采用混合阈值优化。对于不同的层 ,根据其中非零元素的个数 进行分类讨论:
- 若 (大集合):由于这类集合数量较少,我们直接对其使用 FWHT 得到频域,平方后累加到频域总和数组中,最后在算法结束前做一次逆变换。
- 若 (小集合):非零元素很少,我们直接在 内暴力两两异或并累加到结果数组中。
通过动态评估这两种方式的理论代价,选择一个最优的阈值 ,即可在极短时间内完成所有异或卷积的计算。
复杂度分析
- 时间复杂度:。
- 空间复杂度:。
Code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define ll long long
const ll mod = 998244353;
void FFTOI(vector <int> &a){
int n = a.size();
for (int len = 1;len < n;len <<= 1)
for (int i = 0;i < n;i += 2 * len)
for (int j = 0;j < len;j++){
int u = a[i + j];
int v = a[i + len + j];
a[i + j] = u + v;
a[i + len + j] = u - v;
}
}
void fftOI(vector <__int128> &a){
int n = a.size();
for (int len = 1;len < n;len <<= 1)
for (int i = 0;i < n;i += (len << 1))
for (int j = 0;j < len;j++){
__int128 u = a[i + j];
__int128 v = a[i + len + j];
a[i + j] = u + v;
a[i + len + j] = u - v;
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n,m; cin >> n >> m;
int M = 1 << m;
vector <int> cnt(M,0);
for (int i = 1;i <= n;++i){
int val;
cin >> val;
cnt[val]++;
}
vector <int> vals;
vector <vector <int> > b(n + 1);
for (int i = 0;i < M;++i)
if (cnt[i] > 0)
b[cnt[i]].push_back(i);
for (int c = 1;c <= n;c++)
if (!b[c].empty())
vals.push_back(c);
int p = vals.size();
vector <int> u(p + 1,0);
for (int i = 1;i <= p;++i)
u[i] = vals[i - 1];
vector <int> sorv;
vector <int> prend(p + 1,0);
for (int j = p;j >= 1;j--){
for (int v : b[u[j]])
sorv.push_back(v);
prend[j] = sorv.size();
}
vector <int> upds ={0};
for (int j = 1;j <= p;j++)
upds.push_back(prend[j]);
int minid = 0;
double mini = 1e18;
for (int t : upds){
double cost = 0;
for (int j = 1;j <= p;j++){
int sj = prend[j];
int wj = u[j] - u[j - 1];
if (sj > t)
cost += 3e6;
else
cost += (double)wj * sj * (sj - 1) / 2.0;
}
if (cost < mini)
mini = cost,
minid = t;
}
vector <ll> fl(M,0);
vector <ll> ws(M,0);
for (int j = 1;j <= p;j++){
int sj = prend[j];
int wj = u[j] - u[j - 1];
if (sj > minid){
vector <int> vec(M,0);
for (int i = 0;i < sj;++i)
vec[sorv[i]] = 1;
FFTOI(vec);
for (int k = 0;k < M;++k)
fl[k] += (ll)wj * vec[k] * vec[k];
} else{
for (int a_idx = 0;a_idx < sj;a_idx++){
int a = sorv[a_idx];
ws[0] += wj;
for (int b_idx = a_idx + 1;b_idx < sj;b_idx++){
int b = sorv[b_idx];
ws[a ^ b] += 2LL * wj;
}
}
}
}
vector <ll> h_large(M,0);
bool flag = false;
for (int k = 0;k < M && !flag;++k)
if (fl[k] > 0)
flag = true;
if (flag){
vector <__int128> fli(M);
for (int k = 0;k < M;++k)
fli[k] = fl[k];
fftOI(fli);
for (int k = 0;k < M;++k)
h_large[k] = (ll)(fli[k] / M);
}
vector <ll> h(M,0);
for (int x = 0;x < M;++x)
h[x] = h_large[x] + ws[x];
vector <ll> f(M,0);
ll f0 = 0;
for (int i = 0;i < M;++i)
f0 += cnt[i] / 2;
f[0] = f0;
for (int x = 1;x < M;++x)
f[x] = h[x] / 2;
ll ans = 0;
vector <ll> pow10(M,1);
for (int i = 1;i < M;++i)
pow10[i] = (pow10[i - 1] * 10) % mod;
for (int x = 0;x < M;++x){
ll term = (f[x] % mod) * pow10[x] % mod;
ans = (ans + term) % mod;
}
cout << ans << "\n";
return 0;
}4. Triple Transformation
草,就是这题差不到1s然后AK。·
问题分析
首先,通过观察变换规则,可以发现一个非常关键的性质:无论进行何种变换,三元组的总和 始终保持不变。因此,若起点和终点的总和不同,直接输出 。
然后,我们需要对总和 的奇偶性进行分类讨论。
1. 当 为偶数时
每次变换后得到的元素必然全为偶数。若目标三元组 中含有奇数,则直接判断 一步能否到达 即可。若 也全为偶数,则每次操作后都可以将三元组及 同时除以 ,这与原问题同构。由此,我们通过不断除以 可以将问题收缩为 为奇数的情况。
2. 当 为奇数时
在折叠的三角形网格中,这个变换在未折叠的平铺坐标系下仅仅相当于将所有坐标模 乘以 2。 也就是说,在第 步后,必定有:
我们可以通过扩展欧几里得算法求解同余方程组,得到唯一的比例常数 ,满足 。
最后,问题转化为了寻找最小的非负整数 ,满足:
这可以通过 BSGS 算法 在 内解决。求出候选步数后,再利用折叠映射的性质进行快速还原和验证。
复杂度分析
- 时间复杂度:单次测试用例为 。由于 ,运行速度非常快。
- 空间复杂度:。
Code
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <numeric>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){
while (b){
a %= b;
swap(a,b);
}
return a;
}
ll extgcd(ll a,ll b,ll &x,ll &y){
if (b == 0){
x = 1; y = 0;
return a;
}
ll x1,y1;
ll g = extgcd(b,a % b,x1,y1);
x = y1; y = x1 - y1 * (a / b);
return g;
}
ll Inv(ll a,ll m){
ll x,y;
ll g = extgcd(a,m,x,y);
if (g != 1) return -1;
return (x % m + m) % m;
}
// 合并同余方程 P = r1 (mod m1) 和 P = r2 (mod m2)
bool merge(ll &r1,ll &m1,ll r2,ll m2){
ll x,y;
ll g = extgcd(m1,m2,x,y);
if ((r2 - r1) % g != 0) return false;
ll mod = m2 / g;
ll inv = Inv(m1 / g,mod);
if (inv == -1) return false;
ll k = (__int128)((r2 - r1) / g) * inv % mod;
if (k < 0) k += mod;
r1 = r1 + k * m1;
m1 = m1 * mod;
r1 = (r1 % m1 + m1) % m1;
return true;
}
// 求解同余方程组 P * A[i] = B[i] (mod S)
ll solve_P(const vector<ll>& A,const vector<ll>& B,ll S){
ll r = 0,m = 1;
for (int i = 0;i < 3;++i){
ll g = gcd(A[i],S);
if (B[i] % g != 0) return -1;
ll ai = A[i] / g;
ll bi = B[i] / g;
ll mi = S / g;
ll inv = Inv(ai,mi);
if (inv == -1) return -1;
ll ri = (__int128)bi * inv % mi;
if (!merge(r,m,ri,mi)) return -1;
}
for (int i = 0;i < 3;++i)
if (1LL * r * A[i] % S != B[i])
return -1;
// __int128??
return r;
}
// 求解 a^k = b (mod mod) BSGS(粘的板子
ll bsgs(ll a,ll b,ll mod){
b %= mod;
if (b == 1) return 0;
ll m = ceil(sqrt(mod));
vector <pair<ll,ll> > vals;
vals.reserve(m);
ll cur = b;
for (ll j = 0;j < m;++j)
vals.push_back({cur,j}),
cur = (cur * a) % mod;
sort(vals.begin(),vals.end());
vector <pair<ll,ll> > tb;
for (auto& p : vals){
if (tb.empty() || tb.back().first != p.first)
tb.push_back(p);
else
tb.back().second = p.second;
}
ll am = 1;
for (ll i = 0;i < m;++i) am = (am * a) % mod;
ll lhs = 1;
for (ll i = 1;i <= m;++i){
lhs = (lhs * am) % mod;
auto it = lower_bound(tb.begin(),tb.end(),make_pair(lhs,-1LL));
if (it != tb.end() && it->first == lhs){
ll k = i * m - it->second;
if (k >= 0)
return k;
}
}
return -1;
}
vector<ll> get_next(const vector<ll>& A){
ll x = A[0],y = A[1],z = A[2];
if (y + z < x) return{x - y - z,2 * y,2 * z};
if (x + z < y) return{2 * x,y - x - z,2 * z};
if (x + y < z) return{2 * x,2 * y,z - x - y};
return{y + z - x,x + z - y,x + y - z};
}
vector<ll> get_state(const vector<ll>& A,ll k,ll S){
vector <ll> U(3);
ll p2 = 1;
ll base = 2;
ll tmpk = k;
while (tmpk){
if (tmpk & 1)
p2 = (p2 * base) % S;
base = (base * base) % S;
tmpk >>= 1;
}
for (int i = 0;i < 3;++i)
U[i] = (A[i] * p2) % S;
ll sum = U[0] + U[1] + U[2];
if (sum == S) return U;
vector <ll> res(3);
for (int i = 0;i < 3;++i)
res[i] = (S - U[i]) % S;
return res;
}
void solve(){
vector<ll> A(3),B(3);
for (int i = 0;i < 3;++i) cin >> A[i];
for (int i = 0;i < 3;++i) cin >> B[i];
ll sumA = A[0] + A[1] + A[2];
ll sumB = B[0] + B[1] + B[2];
if (sumA != sumB){
cout << -1 << "\n";
return ;
}
if (A == B){
cout << 0 << "\n";
return ;
}
if (sumA == 0){
cout << 0 << "\n";
return ;
}
ll S = sumA;
ll steps = 0;
while (S % 2 == 0){
bool even = (B[0] % 2 == 0 && B[1] % 2 == 0 && B[2] % 2 == 0);
if (!even){
if (get_next(A) == B)
cout << steps + 1 << "\n";
else
cout << -1 << "\n";
return;
} else{
vector <ll> nxt = get_next(A);
for (int i = 0;i < 3;++i){
A[i] = nxt[i] / 2;
B[i] /= 2;
}
S /= 2;
steps++;
if (A == B){
cout << steps << "\n";
return;
}
}
}
ll g = gcd(A[0],gcd(A[1],A[2]));
if (g > 1){
if (B[0] % g != 0 || B[1] % g != 0 || B[2] % g != 0)
return cout << "-1\n", void();
for (int i = 0;i < 3;++i)
A[i] /= g,
B[i] /= g;
S /= g;
if (A == B)
return cout << steps << '\n',void();
}
ll P = solve_P(A,B,S);
if (P == -1){
cout << -1 << "\n";
return;
}
ll k1 = bsgs(2,P,S);
ll k2 = bsgs(2,S - P,S);
ll ans = -1;
if (k1 != -1 && get_state(A,k1,S) == B)
ans = (ans == -1) ? k1 : min(ans,k1);
if (k2 != -1 && get_state(A,k2,S) == B)
ans = (ans == -1) ? k2 : min(ans,k2);
if (ans == -1)
cout << -1 << "\n";
else
cout << steps + ans << "\n";
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t; cin >> t;
while (t--)
solve();
return 0;
}