// x, y: 坐标 // d: 方向编号 // u: 深度 // cnt: 旋转次数 voiddfs(int x, int y, int d, int u, int cnt) { if (x < 0 || y < 0 || x >= r || y >= c || g[x][y] != s[u]) return; if (u == len - 1) res ++; else { dfs(x + dx[d], y + dy[d], d, u + 1, cnt); if (!cnt && u) { dfs(x, y, d ^ 2, u, cnt + 1); dfs(x, y, d ^ 2 ^ 4, u, cnt + 1); } } }
voidsolve() { cin >> s >> r >> c; len = strlen(s);
for (int i = 0; i < r; i++) for (int j = 0; j < c; j++) cin >> g[i][j];
for (int i = 0; i < r; i++) for (int j = 0; j < c; j++) for (int k = 0; k < 8; k++) dfs(i, j, k, 0, 0);
voidsolve() { memset(f, 0x3f, sizeof f); int n; cin >> n; for (int i = 0; i < n; i++) cin >> h[i]; for (int i = 0; i < n; i++) { for (int l = i, r = i, s = 0; l >= 0 && r < n; l--, r++) { s += abs(h[l] - h[r]); f[r - l + 1] = min(f[r - l + 1], s); } for (int l = i, r = i + 1, s = 0; l >= 0 && r < n; l--, r++) { s += abs(h[l] - h[r]); f[r - l + 1] = min(f[r - l + 1], s); } } for (int i = 1; i <= n; i++) cout << f[i] << ' '; }
// a ^ b mod p LL q_pow(int a, int b) { LL res = 1; while (b) { if (b & 1) res = res * a % MOD; a = (LL) a * a % MOD; b >>= 1; } return res; }
LL C(int a, int b) { LL fa = 1, fb = 1, fab = 1; // 只用算一次所以不用开 frac 数组了 for (int i = 1; i <= a; i++) fa = fa * i % MOD; for (int i = 1; i <= b; i++) fb = fb * i % MOD; for (int i = 1; i <= a - b; i++) fab = fab * i % MOD; return fa * q_pow(fb, MOD - 2) % MOD * q_pow(fab, MOD - 2) % MOD; }
voidsolve() { cin >> n; int res = 0; for (int i = 0; i < 2; i++) for (int j = 0; j < n; j++) { cin >> w[i][j]; if (w[i][j]) res += 3; if (j && w[i][j] && w[i][j - 1]) res -= 2; if (i && j % 2 == 0 && w[i - 1][j]) res -= 2; } cout << res << endl; }
#include<bits/stdc++.h> #define x first #define y second
usingnamespace std; typedef pair<int, int> PII;
constint N = 105; int T = 1;
int n, t;
PII tr[N];
intwork() { int res = 0; sort(tr, tr + t); for (int i = 0; i < t; i++) { int up = n + 1, down = 0; for (int j = i + 1; j < t; j++) { if (tr[i].x == tr[j].x) continue; if (tr[j].x - tr[i].x > up - down) break; res = max(res, tr[j].x - tr[i].x - 1); if (tr[j].y >= tr[i].y) up = min(up, tr[j].y); if (tr[j].y <= tr[i].y) down = max(down, tr[j].y); } } return res; }
voidsolve() { cin >> n >> t; for (int i = 0; i < t; i++) cin >> tr[i].x >> tr[i].y;
intto_bit(vector<int>& a, int i) { int res = 0; vector<int> aa = a; reverse(aa.begin(), aa.end()); for (int j = 0; j < aa.size(); j++) res += (aa[j] << j); res <<= (i + 1); return res; }
voidsolve() { int l, r; cin >> l >> r; auto it = st.lower_bound(l); if (it == st.end()) { cout << -1 << endl; return; } int x = *it; if (l <= x && x <= r) cout << x << endl; else cout << -1 << endl; }
signedmain(){ ios::sync_with_stdio(false); cin.tie(0); // 枚举尾导零的个数 for (int i = 1; i <= 15; i++) { t.clear(); vector<int> a; for (int j = 0; j < 30 - 2 * i; j++) a.push_back(0); for (int j = 0; j < i - 1; j++) a.push_back(1); // 全排列前面 30 - i - 1 个位置 do { int num = to_bit(a, i) + (1 << i); if (num <= M) st.insert(num); } while (next_permutation(a.begin(), a.end())); }
// 1 区间的个数 int last = -1; for (int i = 1; i <= n; i++) { if (a[i] == '1') { if (last != i - 1) s[i] = 1, last = i; else last = -1; } s[i] += s[i - 1]; }
// 左边数起 结尾为 i 连续 1 的个数 for (int i = 1; i <= n; i++) { if (a[i] == '1') suf[i] = suf[i - 1] + 1; else suf[i] = 0; }
for (int i = n; i >= 1; i--) { if (a[i] == '1') pre[i] = pre[i + 1] + 1; }