9.29总结:超级无敌推式子
一、比赛基本情况
- 比赛日期:2026 年 9 月 27 日
- 比赛时长:4 小时
- 比赛得分:185 分
- 比赛排名:第 7 名
二、比赛整体回顾
这T3怎么这么难
三、题目复盘
T1:串串串(string)
- 本题得分:100
- 开始时间:8:00/9:40
- 结束时间:8:05/10:00
- 总用时:25
初始思路
太难了吧,这怎么比较啊,去做T3
(看了一会)
太简单了吧,
考场解题过程
很简单,所以没错
考场代码
/*
writer: Luna_Frome_Starry
Problem: 不知名屑题
Knowledge: 垃圾算法
Time: O(过不了)
PS:Hhy don't have dick
*/
/*
Note:
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 250005;
const int M = 1505;
const int INF = 1e9;
const int mod = 998244353;
char c[N];
int a[N], b[N];
void work(){
//please write your code here
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> c[i];
a[i] = a[i - 1] + (c[i] - '0');
}
for(int i = 1; i <= m; i++){
cin >> c[i];
b[i] = b[i - 1] + (c[i] - '0');
}
int q;
cin >> q;
while(q--){
int l, r, l_, r_;
cin >> l >> r >> l_ >> r_;
cout << abs(a[r] - a[l - 1] - b[r_] + b[l_ - 1]) % 2 << "\n";
}
}
signed main(){
freopen("string.in","r",stdin);
freopen("string.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1; while(t--){work();}
return 0;
}
赛后题解
- 30% 部分分
思路
直接对每次询问,逐位比较
数据范围
复杂度
- 时间:
\mathcal{O(Q \cdot L)} ,其中L 为询问区间长度,最坏O(nQ) 。 - 空间:
\mathcal{O(n + m)} 。
:::success[代码]
/*
writer: Luna_From_Starry
Problem: 串串串
Knowledge: 暴力
Time: O(nQ)
Note:
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 200005;
const int M = 1505;
const int INF = 1e9;
const int mod = 998244353;
void work(){
int n, m;
string s, t;
cin >> n >> m >> s >> t;
int Q;
cin >> Q;
while(Q--){
int l1, r1, l2, r2;
cin >> l1 >> r1 >> l2 >> r2;
int cnt = 0;
for(int i = 0; i <= r1 - l1; i++){
if(s[l1 - 1 + i] != t[l2 - 1 + i]){
cnt++;
}
}
cout << (cnt % 2) << '\n';
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
while(t--){
work();
}
return 0;
}
:::
- 70% 部分分
使用前缀和优化。设
对于询问,区间内
答案即为
- [x] 正常人能看懂的做法
对于每一位
对于每一位
观察:只有当两个都是
其他情况下,两者的贡献完全相同。
因此,两个区间
所以:
右边是
所以答案就是对
- [ ] 正常人看不懂的证法
设两个 01 串分别为
对于一次询问,给定
区间长度
我们取出两个等长的子串:
令:
题目要求的是满足
因为
于是:
对于
这是因为当
所以:
将右边的求和拆成两部分:
记:
其中
由前面的等价关系:
即:
为了快速求出任意区间内
那么对于询问:
答案即为:
复杂度
- 时间:
\mathcal{O(n + m + Q)} 。 - 空间:
\mathcal{O(n + m)} 。
:::success[代码]
/*
writer: Luna_From_Starry
Problem: 串串串
Knowledge: 前缀和
Time: O(n + m + Q)
Note:
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 200005;
const int M = 1505;
const int INF = 1e9;
const int mod = 998244353;
int preS[N], preT[N];
void work(){
int n, m;
string s, t;
cin >> n >> m >> s >> t;
for(int i = 1; i <= n; i++){
preS[i] = preS[i - 1] + (s[i - 1] == '1');
}
for(int i = 1; i <= m; i++){
preT[i] = preT[i - 1] + (t[i - 1] == '1');
}
int Q;
cin >> Q;
while(Q--){
int l1, r1, l2, r2;
cin >> l1 >> r1 >> l2 >> r2;
int c1 = preS[r1] - preS[l1 - 1];
int c2 = preT[r2] - preT[l2 - 1];
cout << ((c1 + c2) % 2) << '\n';
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
while(t--){
work();
}
return 0;
}
:::
80% 部分分
思路
与 70% 完全一致,只是数据范围更大(
复杂度
- 时间:
\mathcal{O(n + m + Q)} 。 - 空间:
\mathcal{O(n + m)} 。
:::success[代码] 同 70%。 :::
100% 部分分
思路
与 70% 完全一致,只是数据范围最大(
复杂度
- 时间:
\mathcal{O(n + m + Q)} 。 - 空间:
\mathcal{O(n + m)} 。
:::success[代码] 同 70%。 :::
本题总结
还行,一道~推式子~好题
T2:方格计数(grid)
- 本题得分:80
- 开始时间:忘了
- 结束时间:11:20
- 总用时:≈2h
初始思路
这...........................不想推式子啊
不对,数据这么小
枚举!
考场解题过程
怎么没过?
(调了一辈子)出来了!
考场代码
/*
writer: Luna_Frome_Starry
Problem: 不知名屑题
Knowledge: 垃圾算法
Time: O(过不了)
PS:Hhy don't have dick
*/
/*
Note:
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
const int N = 1005;
int C[N][N];
void init(){
for (int i = 0; i < N; i++){
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; j++){
C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
}
}
}
int gcd(int a, int b){
return b ? gcd(b, a % b) : a;
}
void work(){
int n, W, H, D;
cin >> n >> W >> H >> D;
if(n == 1){
cout << ((W + 1) * (H + 1)) % mod << '\n';
return;
}
int ans = 0;
for(int dx = -W; dx <= W; dx++){
for(int dy = -H; dy <= H; dy++){
if((dx == 0 && dy == 0) || dx < 0 || (dx == 0 && dy < 0)){
continue;
}
if(gcd(abs(dx), abs(dy)) != 1){
continue;
}
int now = 1;
while(now * now * (dx * dx + dy * dy) < D * D){
now++;
}
int minn = now;
int minx = (n - 1) * minn + 1;
int maxn = 0;
if(dx == 0){
maxn = H / abs(dy) + 1;
}else if(dy == 0){
maxn = W / abs(dx) + 1;
}else{
maxn = min(W / abs(dx), H / abs(dy)) + 1;
}
if(maxn < minx){
continue;
}
for(int x_ = 0; x_ <= W; x_++){
for(int y_ = 0; y_ <= H; y_++){
int fx = x_ - dx;
int fy = y_ - dy;
if(fx >= 0 && fx <= W && fy >= 0 && fy <= H) continue;
int t_max;
if(dx == 0){
t_max = (H - y_) / dy;
}else if(dy == 0){
t_max = (W - x_) / dx;
}else{
int tx = 0, ty = 0;
if(dx > 0){
tx = (W - x_) / dx;
}else{
tx = (x_ - 0) / (-dx);
}
if(dy > 0){
ty = (H - y_) / dy;
}else{
ty = (y_ - 0) / (-dy);
}
t_max = min(tx, ty);
}
int len = t_max + 1;
if(len < minx) continue;
ans = (ans + C[len - (n - 1) * (minn - 1)][n]) % mod;
}
}
}
}
cout << ans << '\n';
}
signed main(){
freopen("grid.in","r",stdin);
freopen("grid.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
init();
int T;
cin >> T;
while (T--){
work();
}
return 0;
}
赛后题解
\Large{我不Define了}
- 20% 部分分
思路
数据范围
枚举所有本质不同的方向
对于每个方向,相邻格点距离为
枚举所有起点
计算该直线上格点总数
复杂度
- 时间:
\mathcal{O}(W^2H^2) ,W,H \le 10 时约10^4 。 - 空间:
\mathcal{O}(WH \cdot N) 。
:::success[代码]
/*
writer: Luna_From_Starry
Problem: 方格计数
Knowledge: 枚举方向 + 组合数学
Time: O(T * W^2 * H^2)
Note:
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1005;
const int M = 55;
const int INF = 1e9;
const int mod = 1e9 + 7;
int C[N][M];
void init(){
for(int i = 0; i < N; i++){
C[i][0] = C[i][i] = 1;
for(int j = 1; j < i && j < M; j++){
C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
}
}
}
int gcd(int a, int b){
return b ? gcd(b, a % b) : a;
}
void work(){
int N, W, H, D;
cin >> N >> W >> H >> D;
if(N == 1){
cout << (W + 1) * (H + 1) % mod << '\n';
return;
}
int ans = 0;
for(int dx = 0; dx <= W; dx++){
for(int dy = -H; dy <= H; dy++){
if(dx == 0 && dy <= 0) continue;
if(gcd(abs(dx), abs(dy)) != 1) continue;
int g = 1;
while(g * g * (dx * dx + dy * dy) < D * D) g++;
int min_len = (N - 1) * g + 1;
int max_len;
if(dx == 0) max_len = H / abs(dy) + 1;
else if(dy == 0) max_len = W / dx + 1;
else max_len = min(W / dx, H / abs(dy)) + 1;
if(max_len < min_len) continue;
for(int x0 = 0; x0 <= W; x0++){
for(int y0 = 0; y0 <= H; y0++){
int px = x0 - dx;
int py = y0 - dy;
if(px >= 0 && px <= W && py >= 0 && py <= H) continue;
int t_max;
if(dx == 0) t_max = (H - y0) / dy;
else if(dy == 0) t_max = (W - x0) / dx;
else{
int tx, ty;
if(dx > 0) tx = (W - x0) / dx;
else tx = x0 / (-dx);
if(dy > 0) ty = (H - y0) / dy;
else ty = y0 / (-dy);
t_max = min(tx, ty);
}
int len = t_max + 1;
if(len < min_len) continue;
ans = (ans + C[len - (N - 1) * (g - 1)][N]) % mod;
}
}
}
}
cout << ans << '\n';
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
init();
int t = 1;
cin >> t;
while(t--){
work();
}
return 0;
}
:::
- 50% 部分分
思路
沿用 20% 的思路,但加入剪枝:对于每个方向,先计算该方向在矩形内可能达到的最大长度:
若
复杂度
- 时间:
\mathcal{O}(W^2H^2) - 空间:
\mathcal{O}(WH \cdot N) 。
:::success[代码] 同 20%。 :::
- 另外 20% 部分分(
N \le 5 )
思路
由于
可直接沿用 20% 或 50% 的代码,无需额外优化。
复杂度
- 时间:
\mathcal{O}(W^2H^2) 。 - 空间:
\mathcal{O}(WH \cdot N) 。
:::success[代码] 同 20%。 :::
- 100% 部分分
思路
不枚举起点,而是枚举直线长度
对于方向
然后对每个
复杂度
- 时间:
\mathcal{O}(WH(W+H)) ,W,H \le 500 时约1.25 \times 10^8 ,可接受。 - 空间:
\mathcal{O}(WH \cdot N) 。
:::success[代码]
/*
writer: Luna_Frome_Starry
Problem: 不知名屑题
Knowledge: 垃圾算法
Time: O(过不了)
PS:Hhy don't have dick
*/
/*
Note:
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
const int N = 1005;
int C[N][N];
void init(){
for (int i = 0; i < N; i++){
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; j++){
C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
}
}
}
int gcd(int a, int b){
return b ? gcd(b, a % b) : a;
}
void work(){
int n, W, H, D;
cin >> n >> W >> H >> D;
if(n == 1){
cout << ((W + 1) * (H + 1)) % mod << '\n';
return;
}
int ans = 0;
for(int dx = -W; dx <= W; dx++){
for(int dy = -H; dy <= H; dy++){
if((dx == 0 && dy == 0) || dx < 0 || (dx == 0 && dy < 0)){
continue;
}
if(gcd(abs(dx), abs(dy)) != 1){
continue;
}
int now = 1;
while(now * now * (dx * dx + dy * dy) < D * D){
now++;
}
int minn = now;
int minx = (n - 1) * minn + 1;
int maxn = 0;
if(dx == 0){
maxn = H / abs(dy) + 1;
}else if(dy == 0){
maxn = W / abs(dx) + 1;
}else{
maxn = min(W / abs(dx), H / abs(dy)) + 1;
}
if(maxn < minx){
continue;
}
for(int x_ = 0; x_ <= W; x_++){
for(int y_ = 0; y_ <= H; y_++){
int fx = x_ - dx;
int fy = y_ - dy;
if(fx >= 0 && fx <= W && fy >= 0 && fy <= H) continue;
int t_max;
if(dx == 0){
t_max = (H - y_) / dy;
}else if(dy == 0){
t_max = (W - x_) / dx;
}else{
int tx = 0, ty = 0;
if(dx > 0){
tx = (W - x_) / dx;
}else{
tx = (x_ - 0) / (-dx);
}
if(dy > 0){
ty = (H - y_) / dy;
}else{
ty = (y_ - 0) / (-dy);
}
t_max = min(tx, ty);
}
int len = t_max + 1;
if(len < minx) continue;
ans = (ans + C[len - (n - 1) * (minn - 1)][n]) % mod;
}
}
}
}
cout << ans << '\n';
}
signed main(){
freopen("grid.in","r",stdin);
freopen("grid.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
init();
int T;
cin >> T;
while (T--){
work();
}
return 0;
}
:::
本题总结
超级无敌推式子!
T3:树数树(tree)
- 本题得分:0
- 开始时间:8:02
- 结束时间:10:10
- 总用时:2h8min
初始思路
(这里是看错题了)因为每个数字只能在他两边,而一定有个父亲,所以就是直径!
考场解题过程
这样例咋的出来的?
偶偶偶,~是祖先不是父亲啊~
6,那么每个点的权值定义为两个最大权值子节点的权值和,没了
大样例又没过?
偶偶偶,有可能祖先只选了一边,还可以放上面去
完了,不会写代码。。。
考场代码
/*
writer: Luna_Frome_Starry
Problem: 不知名屑题
Knowledge: 垃圾算法
Time: O(过不了)
PS:Hhy don't have dick
*/
/*
Note:
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 150005;
const int M = 1505;
const int INF = 1e9;
const int mod = 1e9 + 7;
vector<int> g[N];
void add_edge(int x, int y){
g[x].push_back(y);
g[y].push_back(x);
}
int ans = 0, siz[N], len[N];
void dfs(int st, int fa){
// cout << st << " " << fa << "\n";
int maxn = 0;
vector<int> numone;
for(auto it : g[st]){
if(it == fa) continue;
// cout << it << " ";
dfs(it, st);
numone.push_back(len[it]);
}
sort(numone.begin(), numone.end());
len[st] = (numone.size() >= 1 ? numone[numone.size() - 1] : 0) + (numone.size() >= 2 ? numone[numone.size() - 2] : 0) + 1;
}fdggfdf
void work(){
//please write your code here
memset(siz, 0, sizeof(siz));
memset(len, 0, sizeof(len));
int n;
cin >> n;
for(int i = 1; i <= n; i++){
g[i].clear();
}
for(int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
add_edge(u, v);
}
dfs(1, 0);
// for(int i = 1; i <= n; i++){
// cout << len[i] << " ";
// }
cout << len[1] << "\n";
}
signed main(){
freopen("tree.in","r",stdin);
freopen("tree.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1; cin >> t; while(t--){work();}
return 0;
}
/*
1
7
1 2
3
2 4
2 5
3 6
3 7
*/
赛后题解
- 1~2 档(
n \le 10 )
思路
暴力枚举所有可能的序列。
先以
枚举序列的第一个元素,然后递归尝试加入每个尚未使用且与当前末尾节点有祖先关系的节点。
记录过程中序列的最大长度。
复杂度
- 时间:
\mathcal{O}(n!) ,n \le 10 时约3.6 \times 10^6 。 - 空间:
\mathcal{O}(n) 。
:::success[代码]
/*
writer: Luna_From_Starry
Problem: 树数树
Knowledge: 暴力
Time: O(n!)
Note:
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 15;
const int M = 1505;
const int INF = 1e9;
const int mod = 998244353;
int n;
vector<int> g[N];
int ans;
int fa[N];
void dfs(int u, int f){
fa[u] = f;
for(int v : g[u]){
if(v == f) continue;
dfs(v, u);
}
}
bool is_ancestor(int u, int v){
while(v){
if(v == u) return true;
v = fa[v];
}
return false;
}
void work(){
cin >> n;
for(int i = 1; i <= n; i++) g[i].clear();
for(int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 0);
ans = 0;
vector<int> path;
function<void(int)> dfs2 = [&](int u){
ans = max(ans, (int)path.size());
for(int v = 1; v <= n; v++){
if(find(path.begin(), path.end(), v) != path.end()) continue;
if(path.empty() || is_ancestor(path.back(), v) || is_ancestor(v, path.back())){
path.push_back(v);
dfs2(v);
path.pop_back();
}
}
};
for(int i = 1; i <= n; i++){
path.clear();
path.push_back(i);
dfs2(i);
}
cout << ans << '\n';
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while(t--){
work();
}
return 0;
}
:::
- 3~6 档(
n \le 2000 )
思路
树形 DP。
设
转移:
答案即为
复杂度
- 时间:
\mathcal{O}(n) 。 - 空间:
\mathcal{O}(n) 。
:::success[代码]
/*
writer: Luna_From_Starry
Problem: 树数树
Knowledge: 树形DP
Time: O(n)
Note:
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2005;
const int M = 1505;
const int INF = 1e9;
const int mod = 998244353;
int n;
vector<int> g[N];
int f[N];
int ans;
void dfs(int u, int fa){
int mx1 = 0, mx2 = 0;
for(int v : g[u]){
if(v == fa) continue;
dfs(v, u);
if(f[v] > mx1){
mx2 = mx1;
mx1 = f[v];
}else if(f[v] > mx2){
mx2 = f[v];
}
}
f[u] = 1 + mx1;
ans = max(ans, f[u]);
ans = max(ans, 1 + mx1 + mx2);
}
void work(){
cin >> n;
for(int i = 1; i <= n; i++) g[i].clear();
for(int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
ans = 0;
dfs(1, 0);
cout << ans << '\n';
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while(t--){
work();
}
return 0;
}
:::
- 7~10 档(链)
思路
链的特殊情况,最长好序列就是整条链,答案为
复杂度
- 时间:
\mathcal{O}(n) 。 - 空间:
\mathcal{O}(n) 。
:::success[代码] 同 3~6 档。 :::
- 11~12 档(菊花)
思路
菊花图中,根
最长好序列最多包含
所以答案为
复杂度
- 时间:
\mathcal{O}(n) 。 - 空间:
\mathcal{O}(n) 。
:::success[代码] 同 3~6 档。 :::
- 13~14 档(完全二叉树)
思路
完全二叉树是特殊的树形结构,可用 3~6 档的树形 DP 直接求解。
DP 转移相同。
复杂度
- 时间:
\mathcal{O}(n) 。 - 空间:
\mathcal{O}(n) 。
:::success[代码] 同 3~6 档。 :::
- 15~20 档(
n \le 10^5 )
思路
与 3~6 档完全相同的树形 DP,只是数据范围更大,需要
使用 vector 存边,递归 DFS 即可。
复杂度
- 时间:
\mathcal{O}(n) 。 - 空间:
\mathcal{O}(n) 。
:::success[代码]
/*
writer: Luna_From_Starry
Problem: 树数树
Knowledge: 树形DP
Time: O(n)
Note:
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 100005;
const int M = 1505;
const int INF = 1e9;
const int mod = 998244353;
int n;
vector<int> g[N];
int f[N];
int ans;
void dfs(int u, int fa){
int mx1 = 0, mx2 = 0;
for(int v : g[u]){
if(v == fa) continue;
dfs(v, u);
if(f[v] > mx1){
mx2 = mx1;
mx1 = f[v];
}else if(f[v] > mx2){
mx2 = f[v];
}
}
f[u] = 1 + mx1;
ans = max(ans, f[u]);
ans = max(ans, 1 + mx1 + mx2);
}
void work(){
cin >> n;
for(int i = 1; i <= n; i++) g[i].clear();
for(int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
ans = 0;
dfs(1, 0);
cout << ans << '\n';
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while(t--){
work();
}
return 0;
}
:::
本题总结
dp?.....
T4:序列(sequence)
- 本题得分:5
- 开始时间:忘了
- 结束时间:忘了
- 总用时:1min
初始思路
哇,5pts是0,写完了!
考场解题过程
wu
考场代码
cout << 0;
赛后题解
- 5% 部分分(
n = 1 )
思路