跳到正文

算法·理论

9.29总结:超级无敌推式子

Luna_From_Starry约 58 分钟
原文
文章信息与版本

文章信息

发布
2026年9月29日
收录
2026年9月29日
更新
2026年11月20日
篇幅
约 58 分钟

版本

  1. v2当前
  2. v1

一、比赛基本情况

  • 比赛日期:2026 年 9 月 27 日
  • 比赛时长:4 小时
  • 比赛得分:185 分
  • 比赛排名:第 7 名

二、比赛整体回顾

这T3怎么这么难


三、题目复盘

T1:串串串(string)

  • 本题得分:100
  • 开始时间:8:00/9:40
  • 结束时间:8:05/10:00
  • 总用时:25

初始思路

太难了吧,这怎么比较啊,去做T3
Later…Later\dots
(看了一会) 太简单了吧,ansmod  2ans\mod 2了那随便做啊,交换等于没换

考场解题过程

很简单,所以没错

考场代码

C++
/*

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% 部分分

思路
直接对每次询问,逐位比较 S[l1+i]S[l_1+i] 与 T[l2+i]T[l_2+i],统计不同字符的个数,最后对 22 取模输出。
数据范围 n,m,Q≤5000n,m,Q \le 5000,总比较次数约 Q×区间长度≤5000×5000=2.5×107Q \times \text{区间长度} \le 5000 \times 5000 = 2.5 \times 10^7,可以接受。

复杂度

  • 时间:O(Q⋅L)\mathcal{O(Q \cdot L)},其中 LL 为询问区间长度,最坏 O(nQ)O(nQ)。
  • 空间:O(n+m)\mathcal{O(n + m)}。
代码
C++
/*

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% 部分分

使用前缀和优化。设 preS[i]\text{preS}[i] 表示 SS 前 ii 位中 11 的个数,preT[i]\text{preT}[i] 表示 TT 前 ii 位中 11 的个数。
对于询问,区间内 11 的个数为:

c1=preS[r1]−preS[l1−1],c2=preT[r2]−preT[l2−1]c_1 = \text{preS}[r_1] - \text{preS}[l_1 - 1], \quad c_2 = \text{preT}[r_2] - \text{preT}[l_2 - 1]

答案即为 (c1+c2) mod 2(c_1 + c_2) \bmod 2。

WHY?\Large{WHY?}

  • 正常人能看懂的做法

对于每一位 ii,设 ai=S[l1+i]a_i = S[l_1 + i],bi=T[l2+i]b_i = T[l_2 + i]。

对于每一位 ii,(ai,bi)(a_i, b_i) 有四种情况:

  • (0,0)(0,0):对 c1+c2c_1 + c_2 贡献 00,对不同位数贡献 00;
  • (0,1)(0,1):对 c1+c2c_1 + c_2 贡献 11,对不同位数贡献 11;
  • (1,0)(1,0):对 c1+c2c_1 + c_2 贡献 11,对不同位数贡献 11;
  • (1,1)(1,1):对 c1+c2c_1 + c_2 贡献 22,对不同位数贡献 00。

观察:只有当两个都是 11 时,对 c1+c2c_1 + c_2 的贡献是 22,而对不同位数的贡献是 00。
其他情况下,两者的贡献完全相同。

因此,两个区间 11 的个数之和可以写成:

c1+c2=2×(同为 1 的位数)+1×(不同的位数)c_1 + c_2 = 2 \times (\text{同为 } 1 \text{ 的位数}) + 1 \times (\text{不同的位数})

所以:

(c1+c2)−不同位数=2×(同为 1 的位数)(c_1 + c_2) - \text{不同位数} = 2 \times (\text{同为 } 1 \text{ 的位数})

右边是 22 的倍数,因此左边模 22 为 00,即:

c1+c2≡不同位数(mod2)c_1 + c_2 \equiv \text{不同位数} \pmod 2

所以答案就是对 (c1+c2)(c_1 + c_2) 取模 22。


  • 正常人看不懂的证法

设两个 01 串分别为 SS 和 TT,长度分别为 nn 和 mm。
对于一次询问,给定 l1,r1,l2,r2l_1, r_1, l_2, r_2,满足 r1−l1+1=r2−l2+1r_1 - l_1 + 1 = r_2 - l_2 + 1。
区间长度 L=r1−l1+1L = r_1 - l_1 + 1。
我们取出两个等长的子串:

a=S[l1…r1],b=T[l2…r2]a = S[l_1 \ldots r_1], \quad b = T[l_2 \ldots r_2]

令:

ai=S[l1+i],bi=T[l2+i],i=0,1,…,L−1a_i = S[l_1 + i], \quad b_i = T[l_2 + i], \quad i = 0, 1, \dots, L-1

题目要求的是满足 ai≠bia_i \ne b_i 的位置个数对 22 取模的结果,即:

ans=(∑i=0L−1[ai≠bi]) mod 2\text{ans} = \left( \sum_{i=0}^{L-1} [a_i \ne b_i] \right) \bmod 2

因为 ai,bia_i, b_i 都是 00 或 11,所以:

[ai≠bi]=ai⊕bi[a_i \ne b_i] = a_i \oplus b_i

于是:

ans=(∑i=0L−1(ai⊕bi)) mod 2\text{ans} = \left( \sum_{i=0}^{L-1} (a_i \oplus b_i) \right) \bmod 2

对于 0/10/1 变量,恒有:

ai⊕bi≡ai+bi(mod2)a_i \oplus b_i \equiv a_i + b_i \pmod 2

这是因为当 ai=bi=0a_i = b_i = 0 时,两边都是 00;当 ai=0,bi=1a_i = 0, b_i = 1 或 ai=1,bi=0a_i = 1, b_i = 0 时,两边都是 11;当 ai=bi=1a_i = b_i = 1 时,ai⊕bi=0a_i \oplus b_i = 0,而 ai+bi=2≡0(mod2)a_i + b_i = 2 \equiv 0 \pmod 2。
所以:

∑i=0L−1(ai⊕bi)≡∑i=0L−1(ai+bi)(mod2)\sum_{i=0}^{L-1} (a_i \oplus b_i) \equiv \sum_{i=0}^{L-1} (a_i + b_i) \pmod 2

将右边的求和拆成两部分:

∑i=0L−1(ai+bi)=∑i=0L−1ai+∑i=0L−1bi\sum_{i=0}^{L-1} (a_i + b_i) = \sum_{i=0}^{L-1} a_i + \sum_{i=0}^{L-1} b_i

记:

c1=∑i=0L−1ai,c2=∑i=0L−1bic_1 = \sum_{i=0}^{L-1} a_i, \quad c_2 = \sum_{i=0}^{L-1} b_i

其中 c1c_1 就是子串 aa 中 11 的个数,c2c_2 就是子串 bb 中 11 的个数。

由前面的等价关系:

ans≡c1+c2(mod2)\text{ans} \equiv c_1 + c_2 \pmod 2

即:

ans=(c1+c2) mod 2\text{ans} = (c_1 + c_2) \bmod 2

为了快速求出任意区间内 11 的个数,预处理前缀和数组:

preS[i]=∑k=1i[S[k]=1],preT[i]=∑k=1i[T[k]=1]\text{preS}[i] = \sum_{k=1}^{i} [S[k] = 1], \quad \text{preT}[i] = \sum_{k=1}^{i} [T[k] = 1]

那么对于询问:

c1=preS[r1]−preS[l1−1]c_1 = \text{preS}[r_1] - \text{preS}[l_1 - 1] c2=preT[r2]−preT[l2−1]c_2 = \text{preT}[r_2] - \text{preT}[l_2 - 1]

答案即为:

ans=(c1+c2) mod 2\text{ans} = (c_1 + c_2) \bmod 2

复杂度

  • 时间:O(n+m+Q)\mathcal{O(n + m + Q)}。
  • 空间:O(n+m)\mathcal{O(n + m)}。
代码
C++
/*

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% 完全一致,只是数据范围更大(n,m,Q≤105n,m,Q \le 10^5)。前缀和 O(1)O(1) 查询足够快。

复杂度

  • 时间:O(n+m+Q)\mathcal{O(n + m + Q)}。
  • 空间:O(n+m)\mathcal{O(n + m)}。
代码

同 70%。


100% 部分分

思路
与 70% 完全一致,只是数据范围最大(n,m,Q≤2×105n,m,Q \le 2 \times 10^5)。前缀和依然 O(1)O(1) 查询。

复杂度

  • 时间:O(n+m+Q)\mathcal{O(n + m + Q)}。
  • 空间:O(n+m)\mathcal{O(n + m)}。
代码

同 70%。


本题总结

还行,一道推式子好题


T2:方格计数(grid)

  • 本题得分:80
  • 开始时间:忘了
  • 结束时间:11:20
  • 总用时:≈2h

初始思路

这…不想推式子啊

不对,数据这么小

枚举!

考场解题过程

怎么没过?

(调了一辈子)出来了!

考场代码

C++
/*

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;
}

赛后题解

我不Define了\Large{我不Define了}

  • 20% 部分分

思路
数据范围 N,W,H,D≤10N,W,H,D \le 10,方向数与起点数都很小。
枚举所有本质不同的方向 (dx,dy)(dx,dy),满足 dx>0dx > 0 或 dx=0,dy>0dx = 0, dy > 0,且 gcd⁡(∣dx∣,∣dy∣)=1\gcd(|dx|,|dy|)=1。
对于每个方向,相邻格点距离为 dx2+dy2\sqrt{dx^2+dy^2},最小步长:

g=⌈Ddx2+dy2⌉g = \left\lceil \frac{D}{\sqrt{dx^2+dy^2}} \right\rceil

枚举所有起点 (x0,y0)(x_0,y_0),判断是否为该方向起点(即 (x0−dx,y0−dy)(x_0-dx, y_0-dy) 不在矩形内)。
计算该直线上格点总数 lenlen,若 len≥(N−1)g+1len \ge (N-1)g+1,则累加组合数:

(len−(N−1)(g−1)N)\binom{len-(N-1)(g-1)}{N}

复杂度

  • 时间:O(W2H2)\mathcal{O}(W^2H^2),W,H≤10W,H \le 10 时约 10410^4。
  • 空间:O(WH⋅N)\mathcal{O}(WH \cdot N)。
代码
C++
/*

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% 的思路,但加入剪枝:对于每个方向,先计算该方向在矩形内可能达到的最大长度:

max_len={H/∣dy∣+1,dx=0W/∣dx∣+1,dy=0min⁡(W/∣dx∣,H/∣dy∣)+1,otherwisemax\_len = \begin{cases} H/|dy|+1, & dx=0 \\ W/|dx|+1, & dy=0 \\ \min(W/|dx|, H/|dy|)+1, & \text{otherwise} \end{cases}

若 max_len<(N−1)g+1max\_len < (N-1)g+1,直接跳过该方向。

复杂度

  • 时间:O(W2H2)\mathcal{O}(W^2H^2)
  • 空间:O(WH⋅N)\mathcal{O}(WH \cdot N)。
代码

同 20%。


  • 另外 20% 部分分(N≤5N \le 5)

思路
由于 NN 很小,组合数 (len−(N−1)(g−1)N)\binom{len-(N-1)(g-1)}{N} 计算量极小。
可直接沿用 20% 或 50% 的代码,无需额外优化。

复杂度

  • 时间:O(W2H2)\mathcal{O}(W^2H^2)。
  • 空间:O(WH⋅N)\mathcal{O}(WH \cdot N)。
代码

同 20%。


  • 100% 部分分

思路
不枚举起点,而是枚举直线长度 lenlen。
对于方向 (dx,dy)(dx,dy),长度为 lenlen 的直线(即包含 lenlen 个格点)数量为:

cnt={W+1,dx=0H+1,dy=0(W−(len−1)∣dx∣+1)(H−(len−1)∣dy∣+1),otherwisecnt = \begin{cases} W+1, & dx=0 \\ H+1, & dy=0 \\ (W-(len-1)|dx|+1)(H-(len-1)|dy|+1), & \text{otherwise} \end{cases}

然后对每个 lenlen,累加:

cnt⋅(len−(N−1)(g−1)N)cnt \cdot \binom{len-(N-1)(g-1)}{N}

复杂度

  • 时间:O(WH(W+H))\mathcal{O}(WH(W+H)),W,H≤500W,H \le 500 时约 1.25×1081.25 \times 10^8,可接受。
  • 空间:O(WH⋅N)\mathcal{O}(WH \cdot N)。
代码
C++
/*

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,那么每个点的权值定义为两个最大权值子节点的权值和,没了

大样例又没过?

偶偶偶,有可能祖先只选了一边,还可以放上面去

完了,不会写代码。。。

考场代码

C++
/*

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≤10n \le 10)

思路
暴力枚举所有可能的序列。
先以 11 为根 DFS 求出每个节点的父节点,从而快速判断祖先关系。
枚举序列的第一个元素,然后递归尝试加入每个尚未使用且与当前末尾节点有祖先关系的节点。
记录过程中序列的最大长度。

复杂度

  • 时间:O(n!)\mathcal{O}(n!),n≤10n \le 10 时约 3.6×1063.6 \times 10^6。
  • 空间:O(n)\mathcal{O}(n)。
代码
C++
/*

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≤2000n \le 2000)

思路
树形 DP。
设 f[u]f[u] 表示以 uu 为端点且不连接父节点的最长好序列长度,g[u]g[u] 表示 uu 作为内部节点连接两个子树的最长序列长度。
转移:

f[u]=1+max⁡v∈child(u)f[v]f[u] = 1 + \max_{v \in child(u)} f[v] g[u]=1+max⁡v1≠v2∈child(u)(f[v1]+f[v2])g[u] = 1 + \max_{v_1 \ne v_2 \in child(u)} (f[v_1] + f[v_2])

答案即为 max⁡umax⁡(f[u],g[u])\max_{u} \max(f[u], g[u])。

复杂度

  • 时间:O(n)\mathcal{O}(n)。
  • 空间:O(n)\mathcal{O}(n)。
代码
C++
/*

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 档(链)

思路
链的特殊情况,最长好序列就是整条链,答案为 nn。

复杂度

  • 时间:O(n)\mathcal{O}(n)。
  • 空间:O(n)\mathcal{O}(n)。
代码

同 3~6 档。


  • 11~12 档(菊花)

思路
菊花图中,根 11 连接所有其他节点。
最长好序列最多包含 33 个节点:一个叶子、根、另一个叶子。
所以答案为 33(当 n≥3n \ge 3),否则 nn。

复杂度

  • 时间:O(n)\mathcal{O}(n)。
  • 空间:O(n)\mathcal{O}(n)。
代码

同 3~6 档。


  • 13~14 档(完全二叉树)

思路
完全二叉树是特殊的树形结构,可用 3~6 档的树形 DP 直接求解。
DP 转移相同。

复杂度

  • 时间:O(n)\mathcal{O}(n)。
  • 空间:O(n)\mathcal{O}(n)。
代码

同 3~6 档。


  • 15~20 档(n≤105n \le 10^5)

思路
与 3~6 档完全相同的树形 DP,只是数据范围更大,需要 O(n)\mathcal{O}(n) 算法。
使用 vector 存边,递归 DFS 即可。

复杂度

  • 时间:O(n)\mathcal{O}(n)。
  • 空间:O(n)\mathcal{O}(n)。
代码
C++
/*

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

考场代码

C++
cout << 0;

赛后题解


  • 5% 部分分(n=1n = 1)

思路
n=1n = 1 时,只有一位数字。数位和为 1010 不可能(单个数字最大为 99),因此该位永远无法被任何 se 序列覆盖。
答案概率为 00。

复杂度

  • 时间:O(1)\mathcal{O}(1)。
  • 空间:O(1)\mathcal{O}(1)。
代码
C++
/*

writer:  Luna_From_Starry
Problem: 序列
Knowledge: 特判
Time: O(1)
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; 
void work(){
    int n;
    cin >> n;
    int b[10];
    for(int i = 0; i < 10; i++) cin >> b[i];
    if(n == 1){
        cout << 0 << '\n';
        return;
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    while(t--){
        work();
    }
    return 0;
}

  • 5% 部分分(n=100n = 100)

思路
用状压 DP。设 dp[i][mask]dp[i][mask] 表示前 ii 位,最后 1010 位的覆盖状态为 maskmask 的概率。
转移时枚举下一位数字 dd,更新 maskmask。
由于 n=100n = 100 较小,210=10242^{10} = 1024,总状态数 100×1024≈105100 \times 1024 \approx 10^5,非常小。

复杂度

  • 时间:O(n⋅10⋅210)\mathcal{O}(n \cdot 10 \cdot 2^{10})。
  • 空间:O(n⋅210)\mathcal{O}(n \cdot 2^{10})。
代码
C++
/*

writer:  Luna_From_Starry
Problem: 序列
Knowledge: 状压DP
Time: O(n * 10 * 2^10)
Note:

*/
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 105;
const int M = 1505;
const int INF = 1e9;
const int mod = 1e9 + 7; 
int n;
int a[10];
int dp[N][1 << 10];
int qpow(int a, int b){
    int res = 1;
    while(b){
        if(b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}
void work(){
    cin >> n;
    int sumb = 0;
    for(int i = 0; i < 10; i++){
        cin >> a[i];
        sumb += a[i];
    }
    for(int i = 0; i < 10; i++){
        a[i] = a[i] * qpow(sumb, mod - 2) % mod;
    }
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;
    for(int i = 1; i <= n; i++){
        for(int mask = 0; mask < (1 << 10); mask++){
            if(dp[i - 1][mask] == 0) continue;
            for(int d = 0; d < 10; d++){
                int nmask = (mask << 1) & ((1 << 10) - 1);
                int s = 0;
                for(int len = 1; len <= 10 && len <= i; len++){
                    s += d;
                    if(s == 10){
                        nmask |= (1 << (len - 1));
                    }
                }
                dp[i][nmask] = (dp[i][nmask] + dp[i - 1][mask] * a[d]) % mod;
            }
        }
    }
    int ans = 0;
    for(int mask = 0; mask < (1 << 10); mask++){
        if((mask & ((1 << min(10, n)) - 1)) == ((1 << min(10, n)) - 1)){
            ans = (ans + dp[n][mask]) % mod;
        }
    }
    cout << ans << '\n';
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    while(t--){
        work();
    }
    return 0;
}

  • 20% 部分分(n=3000n = 3000)

思路
与 n=100n = 100 完全相同,只是数据范围更大。
n=3000n = 3000 时,总状态数 3000×1024≈3×1063000 \times 1024 \approx 3 \times 10^6,每次转移 1010 次,总操作约 3×1073 \times 10^7,可以接受。

复杂度

  • 时间:O(n⋅10⋅210)\mathcal{O}(n \cdot 10 \cdot 2^{10})。
  • 空间:O(n⋅210)\mathcal{O}(n \cdot 2^{10})。
代码
C++
/*
writer:  Luna_Frome_Starry
Problem: #114. 序列
Knowledge: 状态压缩动态规划
Time: O(n * 2816 * 10)

*/
/*
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;
int qpow(int x, int y, int p){
    int res = 1;
    while(y){
        if(y & 1) res = res * x % p;
        x = x * x % p;
        y >>= 1;
    }
    return res;
}
int id[1 << 10][11];
int nxt[2816][10];
void work(){
    int n;
    cin >> n;
    int b[15];
    int sum_b = 0;
    for(int i = 0; i <= 9; i++){
        cin >> b[i];
        sum_b = (sum_b + b[i]) % mod;
    }
    int inv_sum = qpow(sum_b, mod - 2, mod);
    int a[15];
    for(int i = 0; i <= 9; i++){
        a[i] = b[i] * inv_sum % mod;
    }
    for(int mask = 0; mask < (1 << 10); mask++){
        for(int u = 0; u <= 10; u++){
            id[mask][u] = -1;
        }
    }
    int cnt = 0;
    for(int mask = 0; mask < (1 << 10); mask++){
        if(!(mask & 1)) continue;
        for(int u = 0; u <= 10; u++){
            if(u == 0 || (mask >> u & 1)){
                id[mask][u] = cnt;
                cnt++;
            }
        }
    }
    for(int mask = 0; mask < (1 << 10); mask++){
        if(!(mask & 1)) continue;
        for(int u = 0; u <= 10; u++){
            if(id[mask][u] == -1) continue;
            for(int d = 0; d <= 9; d++){
                nxt[id[mask][u]][d] = -1;
                if(u + d > 10) continue;
                int shifted = mask << d;
                int hit = (shifted >> 10) & 1;
                int new_u = hit ? 0 : u + d;
                if(new_u >= 10) continue;
                int new_mask = (shifted & 1023) | 1;
                nxt[id[mask][u]][d] = id[new_mask][new_u];
            }
        }
    }
    static int dp[2816];
    static int ndp[2816];
    for(int i = 0; i < cnt; i++){
        dp[i] = 0;
    }
    dp[id[1][0]] = 1;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < cnt; j++){
            ndp[j] = 0;
        }
        for(int j = 0; j < cnt; j++){
            if(dp[j] == 0) continue;
            for(int d = 0; d <= 9; d++){
                int k = nxt[j][d];
                if(k == -1) continue;
                ndp[k] = (ndp[k] + dp[j] * a[d]) % mod;
            }
        }
        for(int j = 0; j < cnt; j++){
            dp[j] = ndp[j];
        }
    }
    int ans = 0;
    for(int mask = 0; mask < (1 << 10); mask++){
        if(!(mask & 1)) continue;
        if(id[mask][0] != -1){
            ans = (ans + dp[id[mask][0]]) % mod;
        }
    }
    int zero = qpow(a[0], n, mod);
    ans = (ans - zero + mod) % mod;
    cout << ans << '\n';
}
signed main(){
	freopen("sequence.in","r",stdin);
	freopen("sequence.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    int t = 1; while(t--){work();}
	return 0;
}

后面的不用写🤩🥰😍

本题总结

高中!?


四、整体反思

T3想出来了不会写很亏

我不Define了\Large{我不Define了}

我不误触了\Large{我不误触了}

做得较好的地方

T2写出来了,T3思路对了

需要改进的地方

我不Define了\Large{我不Define了}

我不误触了\Large{我不误触了}

时间安排总结

不是很好不是很差

原文