9.29总结:超级无敌推式子

· · 算法·理论

一、比赛基本情况

二、比赛整体回顾

这T3怎么这么难

三、题目复盘

T1:串串串(string)

初始思路

太难了吧,这怎么比较啊,去做T3

Later\dots

(看了一会) 太简单了吧,ans\mod 2了那随便做啊,交换等于没换

考场解题过程

很简单,所以没错

考场代码

/*

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

赛后题解

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

复杂度

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

:::

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

c_1 = \text{preS}[r_1] - \text{preS}[l_1 - 1], \quad c_2 = \text{preT}[r_2] - \text{preT}[l_2 - 1]

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

\Large{WHY?}

对于每一位 i,设 a_i = S[l_1 + i],b_i = T[l_2 + i]。

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

观察:只有当两个都是 1 时,对 c_1 + c_2 的贡献是 2,而对不同位数的贡献是 0。
其他情况下,两者的贡献完全相同。

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

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

所以:

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

右边是 2 的倍数,因此左边模 2 为 0,即:

c_1 + c_2 \equiv \text{不同位数} \pmod 2

所以答案就是对 (c_1 + c_2) 取模 2。

设两个 01 串分别为 S 和 T,长度分别为 n 和 m。
对于一次询问,给定 l_1, r_1, l_2, r_2,满足 r_1 - l_1 + 1 = r_2 - l_2 + 1。
区间长度 L = r_1 - l_1 + 1。
我们取出两个等长的子串:

a = S[l_1 \ldots r_1], \quad b = T[l_2 \ldots r_2]

令:

a_i = S[l_1 + i], \quad b_i = T[l_2 + i], \quad i = 0, 1, \dots, L-1

题目要求的是满足 a_i \ne b_i 的位置个数对 2 取模的结果,即:

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

因为 a_i, b_i 都是 0 或 1,所以:

[a_i \ne b_i] = a_i \oplus b_i

于是:

\text{ans} = \left( \sum_{i=0}^{L-1} (a_i \oplus b_i) \right) \bmod 2

对于 0/1 变量,恒有:

a_i \oplus b_i \equiv a_i + b_i \pmod 2

这是因为当 a_i = b_i = 0 时,两边都是 0;当 a_i = 0, b_i = 1 或 a_i = 1, b_i = 0 时,两边都是 1;当 a_i = b_i = 1 时,a_i \oplus b_i = 0,而 a_i + b_i = 2 \equiv 0 \pmod 2。
所以:

\sum_{i=0}^{L-1} (a_i \oplus b_i) \equiv \sum_{i=0}^{L-1} (a_i + b_i) \pmod 2

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

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

记:

c_1 = \sum_{i=0}^{L-1} a_i, \quad c_2 = \sum_{i=0}^{L-1} b_i

其中 c_1 就是子串 a 中 1 的个数,c_2 就是子串 b 中 1 的个数。

由前面的等价关系:

\text{ans} \equiv c_1 + c_2 \pmod 2

即:

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

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

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

那么对于询问:

c_1 = \text{preS}[r_1] - \text{preS}[l_1 - 1] c_2 = \text{preT}[r_2] - \text{preT}[l_2 - 1]

答案即为:

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

复杂度

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

复杂度

:::success[代码] 同 70%。 :::

100% 部分分

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

复杂度

:::success[代码] 同 70%。 :::

本题总结

还行,一道~推式子~好题

T2:方格计数(grid)

初始思路

这...........................不想推式子啊

不对,数据这么小

枚举!

考场解题过程

怎么没过?

(调了一辈子)出来了!

考场代码

/*

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

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

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

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

\binom{len-(N-1)(g-1)}{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;
}

:::

思路
沿用 20% 的思路,但加入剪枝:对于每个方向,先计算该方向在矩形内可能达到的最大长度:

max\_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+1,直接跳过该方向。

复杂度

:::success[代码] 同 20%。 :::

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

复杂度

:::success[代码] 同 20%。 :::

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

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

然后对每个 len,累加:

cnt \cdot \binom{len-(N-1)(g-1)}{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)

初始思路

(这里是看错题了)因为每个数字只能在他两边,而一定有个父亲,所以就是直径!

考场解题过程

这样例咋的出来的?

偶偶偶,~是祖先不是父亲啊~

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 为根 DFS 求出每个节点的父节点,从而快速判断祖先关系。
枚举序列的第一个元素,然后递归尝试加入每个尚未使用且与当前末尾节点有祖先关系的节点。
记录过程中序列的最大长度。

复杂度

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

:::

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

f[u] = 1 + \max_{v \in child(u)} f[v] g[u] = 1 + \max_{v_1 \ne v_2 \in child(u)} (f[v_1] + f[v_2])

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

复杂度

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

:::

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

复杂度

:::success[代码] 同 3~6 档。 :::

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

复杂度

:::success[代码] 同 3~6 档。 :::

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

复杂度

:::success[代码] 同 3~6 档。 :::

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

复杂度

:::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)

初始思路

哇,5pts是0,写完了!

考场解题过程

wu

考场代码

cout << 0;

赛后题解

思路

答案概率为 $0$。 **复杂度** - 时间:$\mathcal{O}(1)$。 - 空间:$\mathcal{O}(1)$。 :::success[**代码**] ```cpp /* 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 = 100$) **思路** 用状压 DP。设 $dp[i][mask]$ 表示前 $i$ 位,最后 $10$ 位的覆盖状态为 $mask$ 的概率。 转移时枚举下一位数字 $d$,更新 $mask$。 由于 $n = 100$ 较小,$2^{10} = 1024$,总状态数 $100 \times 1024 \approx 10^5$,非常小。 **复杂度** - 时间:$\mathcal{O}(n \cdot 10 \cdot 2^{10})$。 - 空间:$\mathcal{O}(n \cdot 2^{10})$。 :::success[**代码**] ```cpp /* 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 = 3000$) **思路** 与 $n = 100$ 完全相同,只是数据范围更大。 $n = 3000$ 时,总状态数 $3000 \times 1024 \approx 3 \times 10^6$,每次转移 $10$ 次,总操作约 $3 \times 10^7$,可以接受。 **复杂度** - 时间:$\mathcal{O}(n \cdot 10 \cdot 2^{10})$。 - 空间:$\mathcal{O}(n \cdot 2^{10})$。 :::success[**代码**] ```cpp /* 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想出来了不会写很亏 **$\Large{我不Define了}$** **$\Large{我不误触了}$** ### 做得较好的地方 T2写出来了,T3思路对了 ### 需要改进的地方 **$\Large{我不Define了}$** **$\Large{我不误触了}$** ### 时间安排总结 不是很好不是很差