12.23-补题赛
2025-12-18 15:35:00 ~ 2026-06-27 13:22:57
作业一:总结
作业二:题目
https://www.luogu.com.cn/contest/299628
邀请码
ymxm
ask1-解析
对于小数据范围的答案,可以手动打表对应的输出。
比如
~~~cpp
NNN
NNY
NYN
NYY
YNN
YNY
YYN
YYY
~~~
剩余的,只需要给出 $n = 1$ 以及 $n = 2$ 的所有情况。
$n = 1$ , 答案输出应该是
~~~cpp
N
Y
~~~
$n = 2$ , 答案输出应该是
~~~cpp
NN
NY
YN
YY
~~~
当然可以采用循环枚举的方式,考虑每个点的选取情况,用0代表不选,用1代表选。
代码会比较长
~~~cpp
#include <bits/stdc++.h>
using namespace std;
int n;
int ans[15];
void out()
{
for (int i = 1; i <= n; i++)
{
if (ans[i] == 0) cout << "N";
if (ans[i] == 1) cout << "Y";
}
cout << "\n";
}
int main()
{
cin >> n;
if (n == 1)
{
for (int i = 0; i <= 1; i++)
{
ans[1] = i;
out();
}
}
if (n == 2)
{
for (int i = 0; i <= 1; i++)
{
ans[1] = i;
for (int j = 0; j <= 1; j++)
{
ans[2] = j;
out();
}
}
}
if (n == 3)
{
for (int i = 0; i <= 1; i++)
{
ans[1] = i;
for (int j = 0; j <= 1; j++)
{
ans[2] = j;
for (int k = 0; k <= 1; k++)
{
ans[3] = k;
out();
}
}
}
}
if (n == 4)
{
for (int i = 0; i <= 1; i++)
{
ans[1] = i;
for (int j = 0; j <= 1; j++)
{
ans[2] = j;
for (int k = 0; k <= 1; k++)
{
ans[3] = k;
for (int x = 0; x <= 1; x++)
{
ans[4] = x;
out();
}
}
}
}
}
if (n == 5)
{
for (int i = 0; i <= 1; i++)
{
ans[1] = i;
for (int j = 0; j <= 1; j++)
{
ans[2] = j;
for (int k = 0; k <= 1; k++)
{
ans[3] = k;
for (int x = 0; x <= 1; x++)
{
ans[4] = x;
for (int y = 0; y <= 1; y++)
{
ans[5] = y;
out();
}
}
}
}
}
}
if (n == 6)
{
for (int i = 0; i <= 1; i++)
{
ans[1] = i;
for (int j = 0; j <= 1; j++)
{
ans[2] = j;
for (int k = 0; k <= 1; k++)
{
ans[3] = k;
for (int x = 0; x <= 1; x++)
{
ans[4] = x;
for (int y = 0; y <= 1; y++)
{
ans[5] = y;
for (int z = 0; z <= 1; z++)
{
ans[6] = z;
out();
}
}
}
}
}
}
}
if (n == 7)
{
for (int i = 0; i <= 1; i++)
{
ans[1] = i;
for (int j = 0; j <= 1; j++)
{
ans[2] = j;
for (int k = 0; k <= 1; k++)
{
ans[3] = k;
for (int x = 0; x <= 1; x++)
{
ans[4] = x;
for (int y = 0; y <= 1; y++)
{
ans[5] = y;
for (int z = 0; z <= 1; z++)
{
ans[6] = z;
for (int a = 0; a <= 1; a++)
{
ans[7] = a;
out();
}
}
}
}
}
}
}
}
if (n == 8)
{
for (int i = 0; i <= 1; i++)
{
ans[1] = i;
for (int j = 0; j <= 1; j++)
{
ans[2] = j;
for (int k = 0; k <= 1; k++)
{
ans[3] = k;
for (int x = 0; x <= 1; x++)
{
ans[4] = x;
for (int y = 0; y <= 1; y++)
{
ans[5] = y;
for (int z = 0; z <= 1; z++)
{
ans[6] = z;
for (int a = 0; a <= 1; a++)
{
ans[7] = a;
for (int b = 0; b <= 1; b++)
{
ans[8] = b;
out();
}
}
}
}
}
}
}
}
}
if (n == 9)
{
for (int i = 0; i <= 1; i++)
{
ans[1] = i;
for (int j = 0; j <= 1; j++)
{
ans[2] = j;
for (int k = 0; k <= 1; k++)
{
ans[3] = k;
for (int x = 0; x <= 1; x++)
{
ans[4] = x;
for (int y = 0; y <= 1; y++)
{
ans[5] = y;
for (int z = 0; z <= 1; z++)
{
ans[6] = z;
for (int a = 0; a <= 1; a++)
{
ans[7] = a;
for (int b = 0; b <= 1; b++)
{
ans[8] = b;
for (int c = 0; c <= 1; c++)
{
ans[9] = c;
out();
}
}
}
}
}
}
}
}
}
}
if (n == 10)
{
for (int i = 0; i <= 1; i++)
{
ans[1] = i;
for (int j = 0; j <= 1; j++)
{
ans[2] = j;
for (int k = 0; k <= 1; k++)
{
ans[3] = k;
for (int x = 0; x <= 1; x++)
{
ans[4] = x;
for (int y = 0; y <= 1; y++)
{
ans[5] = y;
for (int z = 0; z <= 1; z++)
{
ans[6] = z;
for (int a = 0; a <= 1; a++)
{
ans[7] = a;
for (int b = 0; b <= 1; b++)
{
ans[8] = b;
for (int c = 0; c <= 1; c++)
{
ans[9] = c;
for (int d = 0; d <= 1; d++)
{
ans[10] = d;
out();
}
}
}
}
}
}
}
}
}
}
}
return 0;
}
~~~
对于 $100 \%$ 的数据范围,我们可以直接发现,把每个位置看成点的话,每个点只有两种选择情况 `Y` 或者 `N` 。
所以考虑递归每一个位置的选择,要么是 `N` 要么是 `Y` ,按照字典序的话,先递归 $N$ 。

# ask2-解析
本题重点考察的是有三种选择,其中喝水只能有一次。
对于 $k = 0$ 的情况下,可以直接使用选择 $A$ 或者选择 $B$ 的方式,具体的时间复杂度看递归的层数。
代码参考:
~~~cpp
#include <bits/stdc++.h>
using namespace std;
int T, A, B;
int ans = 0;
int k;
void dfs(int sum, int cnt)
{
if (sum > T) // 吃不下了
{
return; // 说明需要停止了
}
ans = max(ans, sum);
dfs(sum + A, cnt); // 吃橙子
dfs(sum + B, cnt); // 吃柠檬
return;
}
int main()
{
cin >> T >> A >> B;
cin >> k;
dfs(0, 0); // 没有吃东西
cout << ans;
return 0;
}
~~~
对于 $k = 1$ 的情况下,多了一个选择减半一次的选项,可以在搜索的过程之中添加一个参数,代表减半的使用次数。
当然极限情况下,按理搜索是无法通过本题的,那么选择放在这里,说明我们可以添加一部分的优化过程,比如这里的如果已经到达 $T$ 了,
就不用继续了,此时就是最优答案。(这就是最优性减枝)
~~~cpp
#include <bits/stdc++.h>
using namespace std;
int T, A, B;
int ans = 0;
int k;
void dfs(int sum, int cnt)
{
if (sum > T) // 吃不下了
{
return; // 说明需要停止了
}
if (sum == T)
{
cout << sum;
exit(0); // 直接结束整个代码 只能在函数之中使用
}
ans = max(ans, sum);
dfs(sum + A, cnt); // 吃橙子
dfs(sum + B, cnt); // 吃柠檬
if (k == 1 && cnt == 0) // 之前没有喝过水
{
dfs(sum / 2, cnt + 1); // 喝水
}
return;
}
int main()
{
cin >> T >> A >> B;
cin >> k;
dfs(0, 0); // 没有吃东西
cout << ans;
return 0;
}
~~~
# ask3-解析
对于小数据范围, 我们可以直接暴力枚举区间 $[1, n]$ 之中合法数字的数量,其中 $check$ 函数的实现拆分完成数字之后判断是否 3, 5, 7 都出现过即可。
参考代码:
~~~cpp
#include <bits/stdc++.h>
using namespace std;
bool check(int x)
{
bool vis[10] = {0};
while (x)
{
vis[x % 10]++;
x /= 10;
}
for (int i = 0; i <= 9; i++)
{
if (i != 3 && i != 5 && i != 7 && vis[i])
{
return false;
}
}
return vis[3] && vis[5] && vis[7];
}
int main()
{
int n;
cin >> n;
int cnt = 0;
for (int i = 1; i <= n; i++)
{
if (check(i))
{
cnt++;
}
}
cout << cnt << endl;
return 0;
}
~~~
对于数据范围来说,其实每个数字只能是 $3, 5, 7$ 构成的,我们可以考虑构建1到 N内所有的三位数。
总共的位数就 9 位,所以时间复杂度是 $O(3 ^ 9)$ , 可以顺利通过本题。
~~~cpp
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
int cnt = 0;
bool vis[100];
int check(int x)
{
vis[3] = vis[5] = vis[7] = 0;
while (x)
{
vis[x % 10] = 1;
x /= 10;
}
if (vis[3] && vis[5] && vis[7])
{
return 1;
}
return 0;
}
void dfs(int sum) // sum
{
if (sum > n)
{
return;
}
if (check(sum))
{
cnt++;
}
dfs(sum * 10 + 3);
dfs(sum * 10 + 5);
dfs(sum * 10 + 7);
return;
}
signed main()
{
cin >> n;
dfs(0);
cout << cnt;
return 0;
}
~~~
# ask4-解析
本题和第二题比较类似,只需要表示对应的选取关系即可,此时只有可能选或者不选,
添加最优性剪枝,如果已经出现了差值为 $0$ 的,此时就不用再继续了。
参考代码:
~~~cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 7;
int n, v, tot;
int ans = INT_MAX;
int a[maxn], b[maxn];
void dfs(int cur, int sum, int A, int B)
{
if (sum > v)
{
ans = min(ans, abs(A - B));
}
if (ans == 0) // 好感值已经到0了就可以直接停止
{
cout << ans;
exit(0);
}
if (cur == n + 1)
{
return;
}
dfs(cur + 1, sum + a[cur] + b[cur], A + a[cur], B + b[cur]);
dfs(cur + 1, sum, A, B);
}
int main()
{
cin >> n >> v;
for (int i = 1; i <= n; i++)
{
cin >> a[i] >> b[i];
}
dfs(1, 0, 0, 0);
if (ans == INT_MAX)
{
cout << "-1";
}
else
{
cout << ans;
}
}
~~~
# ask5-解析
本题是五道题之中最难的一题,需要用到的减枝方式多样,
和之前不一样的是,需要考虑每根木棍的分组情况,所以下方的代码就是考虑第 $cur$ 根木棍分在第几组。
~~~~cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 7;
int a[maxn];
int len[maxn]; // 每一段的长度
int n;
int total = 0;
int pri_duan = 0;
int leng = 0;
int vis[maxn];
void dfs(int cur)
{
if (cur == n + 1)
{
for (int i = 1; i <= pri_duan; i++)
{
if (len[i] != leng)
{
return ;
}
}
cout << leng << endl;
exit(0);
}
for (int i = 1; i <= pri_duan; i++)
{
if (len[i] + a[cur] <= leng)
{
len[i] += a[cur]; // 选择第i组
dfs(cur + 1);
len[i] -= a[cur]; // 选择第i组
}
}
return ;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
total += a[i];
}
for (int i = 1; i <= total; ++i)
{
if (total % i == 0)
{
leng = i; // 木棍长度
pri_duan = total / i; // 段数
dfs(0);
}
}
return 0;
}
~~~~
但是很显然上述的时间复杂度过高,
所以我们换一种思路,考虑每一组内部有的木棍,一组一组进行划分,如果一组内部已经无法构建,可以直接退出。
这里有一个剪枝方式是按照木棍大小从大到小排序,如果已经超过了范围,退出的话会快很多。
参考代码:
~~~cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
int a[maxn];
int n;
int total = 0;
int pri_duan = 0;
int leng = 0;
int vis[maxn];
// tol代表当前组建好的木棍个数 idx表示当前选中哪一段 len代表当前组件的木棍还需要多长
void dfs(int tol, int idx, int len)
{
if (pri_duan == tol)
{
cout << leng << endl;
exit(0);
}
// 找好一根了,继续找下一根
if (len == 0)
{
dfs(tol + 1, 1, leng);
return;
}
for (int k = idx; k <= n; ++k) // 搜索顺序剪枝
{
if (!vis[k] && a[k] <= len) // 可行性剪枝
{
vis[k] = 1;
dfs(tol, k + 1, len - a[k]);
vis[k] = 0;
}
}
return;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
total += a[i];
}
sort(a + 1, a + n + 1, greater<int>());
// 从大到小开始枚举长度
for (int i = a[1]; i <= total; ++i)
{
if (total % i == 0)
{
leng = i; // 木棍长度
pri_duan = total / i; // 段数
dfs(0, 1, leng);
}
}
return 0;
}
~~~