- 至于为什么是 \(\text{Part A}\) 而不是 \(\text{Day 1}\)
那是因为 Day1 T3 还没改
- (那这六题的 \(\text{solution}\) 就按难度顺序写吧)
- 感觉今年的画风和 \(\text{NOIP 2016}\) 有点像?
D1T1 code
Solution
- 直接模拟
- 如果 \(k<2^{n-1}\) 就输出 \(0\)
- 否则输出 \(1\) 并把 \(k\) 变成 \(2^n-1-k\)
- 然后 \(n\) 减掉 \(1\) 继续进行下去直到 \(n=0\) 为止
- 注意 \(k\) 要开 \(\text{unsigned long long}\)
- \(O(n)\)
Code
#include <bits/stdc++.h>
typedef unsigned long long ull;
int n;
ull k;
void solve(int n, ull k)
{
if (!n) return;
ull mid = 1ull << n - 1;
if (k < mid) putchar('0'), solve(n - 1, k);
else putchar('1'), solve(n - 1, mid - 1 - (k - mid));
}
int main()
{
std::cin >> n >> k;
solve(n, k);
return puts(""), 0;
}
D1T2 brackets
Solution
- 令 \(cnt_u\) 表示根到 \(u\) 的路径组成的括号序列,以 \(u\) 为右端点的合法括号序列个数
- 那么 \(k_u\) 就等于根到 \(u\) 的路径上所有点的 \(cnt\) 之和
- 易得如果存在 \(u\) 的一个深度最大的祖先 \(v\) 使得 \(v\) 到 \(u\) 的路径组成的括号序列是合法括号序列
- 那么 \(cnt_u=cnt_{fa_v}+1\)
- 对于求这个 \(v\) ,可以维护一个栈
- 从根到 \(u\) ,如果是左括号则直接加入,如果是右括号且栈不空则弹栈
- 那么如果 \(u\) 为右括号,那么 \(v\) 为这次弹出的括号对应的点
- 而对于求出所有的 \(u\) ,可以在对树 \(\text{DFS}\) 的过程中维护这个栈,在 \(\text{DFS}\) 回溯时把栈操作也退回即可
- \(O(n)\)
Code
#include <bits/stdc++.h>
template <class T>
inline void read(T &res)
{
res = 0; bool bo = 0; char c;
while (((c = getchar()) < '0' || c > '9') && c != '-');
if (c == '-') bo = 1; else res = c - 48;
while ((c = getchar()) >= '0' && c <= '9')
res = (res << 3) + (res << 1) + (c - 48);
if (bo) res = ~res + 1;
}
typedef long long ll;
const int N = 5e5 + 5;
int n, fa[N], ecnt, nxt[N], adj[N], go[N], stk[N], top, cnt[N];
char s[N];
ll sum[N], ans;
void add_edge(int u, int v)
{
nxt[++ecnt] = adj[u]; adj[u] = ecnt; go[ecnt] = v;
}
void dfs(int u)
{
int tf = 0;
if (s[u] == '(') stk[++top] = u;
else if (top) cnt[u] = cnt[fa[tf = stk[top--]]] + 1;
sum[u] = sum[fa[u]] + cnt[u];
ans ^= sum[u] * u;
for (int e = adj[u], v = go[e]; e; e = nxt[e], v = go[e])
dfs(v);
if (s[u] == '(') top--;
else if (tf) stk[++top] = tf;
}
int main()
{
int x;
read(n);
scanf("%s", s + 1); n = strlen(s + 1);
for (int i = 2; i <= n; i++) read(x), add_edge(fa[i] = x, i);
dfs(1);
return std::cout << ans << std::endl, 0;
}
D2T1 meal
Solution
Code
#include <bits/stdc++.h>
template <class T>
inline void read(T &res)
{
res = 0; bool bo = 0; char c;
while (((c = getchar()) < '0' || c > '9') && c != '-');
if (c == '-') bo = 1; else res = c - 48;
while ((c = getchar()) >= '0' && c <= '9')
res = (res << 3) + (res << 1) + (c - 48);
if (bo) res = ~res + 1;
}
const int N = 105, E = 205, M = 2005, rqy = 998244353;
int n, m, a[N][M], f[N][E], sum[N], tmp[N], ans = 1;
inline void add(int &a, const int &b)
{
a += b; if (a >= rqy) a -= rqy;
}
inline void sub(int &a, const int &b)
{
a -= b; if (a < 0) a += rqy;
}
int main()
{
read(n); read(m);
for (int i = 1; i <= n; i++) sum[i] = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
read(a[i][j]), add(sum[i], a[i][j]);
for (int i = 1; i <= n; i++) ans = 1ll * ans * sum[i] % rqy;
sub(ans, 1);
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++) tmp[j] = sum[j], sub(tmp[j], a[j][i]), sub(tmp[j], 1);
for (int j = -n; j <= n; j++)
for (int k = 0; k <= n; k++)
f[k][j + n] = 0;
f[0][n] = 1;
for (int j = 1; j <= n; j++)
for (int k = -n; k <= n; k++)
{
add(f[j][k + n], f[j - 1][k + n]);
if (k > -n) add(f[j][k + n], 1ll * f[j - 1][k - 1 + n] * a[j][i] % rqy);
if (k < n) add(f[j][k + n], 1ll * f[j - 1][k + 1 + n] * tmp[j] % rqy);
}
for (int j = 1; j <= n; j++) sub(ans, f[n][j + n]);
}
return std::cout << ans << std::endl, 0;
}