ABC 288 G
\(f (x_1, x_2...x_n, i)\) 表示三进制为 \(\overline{x_1x_2...x_n}_{(3)}\),后 \(n - i + 1\) 位确定,前 \(i - 1\) 位要求实际上任意位与 \(x_j\) 相差不超过 \(1\),即 \(\forall j \leq i - 1, |fact_j - x_j| \leq 1\)
\[f (x_1, x_2...x_j(0)...x_n, i) = f (x_1, x_2...x_j(1)...x_n, i - 1) - f (x_1, x_2...x_j(2)...x_n, i - 1)
\\
f (x_1, x_2...x_j(1)...x_n, i) = f (x_1, x_2...x_j(0)...x_n, i - 1) + f (x_1, x_2...x_j(2)...x_n, i - 1) - f (x_1, x_2...x_j(1)...x_n, i - 1)
\\
f (x_1, x_2...x_j(2)...x_n, i) = f (x_1, x_2...x_j(1)...x_n, i - 1) - f (x_1, x_2...x_j(0)...x_n, i)
\]
答案为 \(b (\overline{x_1x_2...x_n}_{(3)}) = f (x_1,x_2...x_n,n)\)。
没必要开高维数组,可以将前 \(n\) 项压为一位。
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define fi first
#define se second
#define db double
#define LL long long
//#define int long long
#define PII pair <int, int>
#define ULL unsigned long long
#define MP(x,y) make_pair (x, y)
#define rep(i,j,k) for (int i = (j); i <= (k); i++)
#define per(i,j,k) for (int i = (j); i >= (k); i--)
template <typename T>
void read (T &x) {
x = 0; T f = 1;
char ch = getchar ();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar ();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar ();
}
x *= f;
}
template <typename T, typename... Args>
void read (T &x, Args&... Arg) {
read (x), read (Arg...);
}
const int MaxPrint = 1000;
int Poi_For_Print, Tmp_For_Print[MaxPrint + 5];
template <typename T>
void write (T x) {
if (x == 0) {
putchar ('0');
return;
}
bool flag = (x < 0 ? 1 : 0);
x = (x < 0 ? -x : x);
while (x) Tmp_For_Print[++Poi_For_Print] = x % 10, x /= 10;
if (flag) putchar ('-');
while (Poi_For_Print) putchar (Tmp_For_Print[Poi_For_Print--] + '0');
}
template <typename T, typename... Args>
void write (T x, Args... Arg) {
write (x); putchar (' '); write (Arg...);
}
template <typename T, typename... Args>
void print (T x, char ch) {
write (x); putchar (ch);
}
template <typename T> T Max (T x, T y) { return x > y ? x : y; }
template <typename T> T Min (T x, T y) { return x < y ? x : y; }
template <typename T> T Abs (T x) { return x > 0 ? x : -x; }
const int Maxn = 1e6;
int n, w[Maxn + 5];
int a[Maxn + 5];
int dp[2][Maxn + 5];
signed main () {
// freopen ("1.in", "r", stdin);
// freopen ("C:\\Users\\HP\\Desktop\\study\\.vscode\\1.in", "r", stdin);
// freopen ("C:\\Users\\HP\\Desktop\\study\\.vscode\\1.out", "w", stdout);
read (n);
w[0] = 1; rep (i, 1, n) w[i] = w[i - 1] * 3;
rep (i, 0, w[n] - 1) {
read (a[i]);
dp[0][i] = a[i];
}
rep (i, 1, n) {
rep (j, 0, w[n] - 1) {
#define get(i,j) (j / w[i - 1] % 3)
if (get (i, j) == 0) dp[i & 1][j] = dp[i - 1 & 1][j + w[i - 1]] - dp[i - 1 & 1][j + w[i - 1] * 2];
if (get (i, j) == 1) dp[i & 1][j] = dp[i - 1 & 1][j - w[i - 1]] + dp[i - 1 & 1][j + w[i - 1]] - dp[i - 1 & 1][j];
if (get (i, j) == 2) dp[i & 1][j] = dp[i - 1 & 1][j - w[i - 1]] - dp[i - 1 & 1][j - w[i - 1] * 2];
}
}
rep (i, 0, w[n] - 1) {
printf ("%d ", dp[n & 1][i]);
}
return 0;
}