Test48提高组考试总结

· · 题解

Test48提高组考试总结

Data

80 = 40 + 0 + 30 + 10

Preface

Manba Out! 坠大机了孩子们 想我了吗?

好吧好吧,我没辙了 T1题读错了 T2“Ctrl + Z”按多了(实际上是自己唐了,忘记初始化了) T3复杂度就多了个log就直接少了50pts T4的暴力又没读题!!!写坠机了 好吧,我没辙了

Statements

A、仓鼠的石子游戏

时间限制: 1 s 空间限制: 512 MB

题目描述

仓鼠和兔子被禁止玩电脑,无聊的他们跑到一块空地上,空地上有许多小石子。兔子捡了很多石子,然后将石子摆成 n 个圈,每个圈由 a[i] 个石子组成。然后兔子有两根彩色笔,一支红色一支蓝色。兔子和仓鼠轮流选择一个没有上色的石子涂上颜色,兔子每次可以选择一个还未染色的石子将其染成红色,而仓鼠每次可以选择一个还未染色的石子将其染成蓝色,并且仓鼠和兔子约定,轮流染色的过程中不能出现相邻石子同色,谁不能操作他就输了。假设他们两个都使用了最优策略来玩这个游戏,并且兔子先手,最终谁会赢得游戏?

输入描述

第一行输入一个正整数 T,表示有 T 组测试案例。

每组测试案例的第一行输入一个 n,表示有 n 圈石子。第二行输入 n 个正整数 a[i],表示每个圈的石子数量。

输出描述

对于每组测试案例,如果兔子赢了,输出 "rabbit" (不含引号);如果仓鼠赢了,输出 "hamster" (不含引号)。

输入样例

4
1
3
1
2
2
1 3
3
999 1000 1000000000

输出样例

hamster
rabbit
rabbit
hamster

说明

对于第一组案例:只有 1 圈石子,并且石圈的大小为 3。兔子先手,随便找了一个石子染成红色,接下来仓鼠后手找一个未染色的石子染成蓝色,此时结果如下图所示。如果兔子将最后一个石子染成红色,这将导致相邻石子同色,根据规则,他输掉了比赛,所以仓鼠获得了最终的胜利。

对于第二组案例:只有 1 圈石子,并且石圈的大小为 1。兔子先手,将唯一的一个石子染成了红色,接下来由于没有未着色的石子,所以仓鼠由于无法操作而输掉了比赛,兔子取得了最终的胜利。

对于第三组案例:有两个石圈,大小分别为 1, 3,兔子首先将大小为 1 的石圈中唯一一个石子染成了红色,接下来仓鼠由于类似第一组案例中的原因输掉比赛,兔子取得了最终的胜利。

Solution

好吧好吧,是我太烫了

我题读错了好吧

这道题是n个环,但一个环没填完也可以填下一个

我们只需要关注长度为1的环的个数,因为只有这个环会交换先后手顺序

注意到此游戏先手必输

就这样,我没辙了

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
    x = 0;
    LL f = 1;
    char c = getchar();
    for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
    for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    x *= f;
}
template <typename T> inline void print(T x){
    if(x < 0) x = -x,putchar('-');
    if(x > 9) print(x / 10);
    putchar(x % 10 + 48);
}
inline void sprint(const string s){
    for(auto x : s) putchar(x);
    return;
}
const int N = 1e5 + 11;
LL t;
int main(){
//  freopen("a.in","r",stdin);
//  freopen("a.out","w",stdout);
    read(t);
    while(t --){
        LL n,cnt = 0;
        read(n);
        for(LL i=1;i<=n;i++){
            LL x;
            read(x);
            if(x == 1){
                cnt ++;
                continue;
            }
        }
        if(cnt % 2 == 1) sprint("rabbit");
        else sprint("hamster");
        putchar('\n');
    }
    return 0;
}

B、乘法师

时间限制: 1 s 空间限制: 512 MB

题目描述

给定一个长为 n 的数组 A_1, A_2, \ldots, A_n,满足所有数都是非负整数。求 A 有多少个非空子区间,满足区间中所有数的积 \geq v。

输入描述

第一行一个正整数 T,表示有 T 组数据。每组数据第一行两个整数 n, v。接下来一行 n 个整数,第 i 个整数为 A_i。

输出描述

对于每组数据,输出一行一个整数,表示答案。

输入样例

1
3 7
2 3 4

输出样例

2

备注

对于前 20% 的数据,n \leq 100。

对于前 50% 的数据,n \leq 1000。

对于所有数据,1 \leq T \leq 5,1 \leq n \leq 10^5,0 \leq A_i, v \leq 10^9。

Solution

这道题不就是滑动窗口+双指针吗,好吧好吧

滑动窗口卡住刚刚好大于等于v的区间,然后再双指针移动就行,时间复杂度O(n)

我TM第一遍双指针写错了

Ctrl+Z撤回了几步就把初始化给忘了

我没辙了

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
    x = 0;
    LL f = 1;
    char c = getchar();
    for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
    for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    x *= f;
}
template <typename T> inline void print(T x){
    if(x < 0) x = -x,putchar('-');
    if(x > 9) print(x / 10);
    putchar(x % 10 + 48);
}
inline void sprint(const string s){
    for(auto x : s) putchar(x);
    return;
}
const int N = 1e5 + 11;
LL n,v,t,cnt;
LL a[N];
vector<LL> gp;
int main(){
//  freopen("ex_b.in","r",stdin);
//  freopen("b.out","w",stdout);
    read(t);
    while(t --){
        cnt = 0;
        read(n);read(v);
        for(LL i=1;i<=n;i++) read(a[i]);
        if(v == 0){
            print(n * (n + 1) / 2);
            putchar('\n');
            continue;
        }
        gp.clear();
        gp.emplace_back(0);
        for(LL i=1;i<=n;i++){
            if(a[i] == 0) gp.emplace_back(i);
        }
        gp.emplace_back(n + 1);
        LL tot = a[1],j = 1;
        for(int i=1;i<=n;){
            if(a[i] == 0){
                tot = 1;
                i ++;
                j = i - 1;
                continue;
            }
            while(j <= n && tot < v){
                j ++;
                if(a[j] == 0) break;
                tot *= a[j];
                if(tot >= v) break;
            }
            if(tot < v){
                tot = 1;
                i = j;
                continue; 
            }
            LL pos = lower_bound(gp.begin(),gp.end(),j) - gp.begin();
            cnt += gp[pos] - j;
            tot /= a[i];
            i ++;
        }
        print(cnt);
        putchar('\n');
    }
    return 0;
}
/*
1
10 31
1 2 3 5 6 2 2 0 1 5


1
3 7
2 3 4
*/

C、乃爱与城市拥挤程度

题目描述

乃爱天下第一可爱!乃爱居住的国家有 n 座城市,这些城市与城市之间有 n-1 条公路相连接,并且保证这些城市两两之间直接或者间接相连。我们定义两座城市之间的距离为这两座城市之间唯一简单路径上公路的总条数。当乃爱位于第 x 座城市时,距离城市 x 距离不大于 k 的城市中的人都会认为乃爱天下第一可爱!认为乃爱天下第一可爱的人们决定到乃爱所在的城市去拜访可爱的乃爱。我们定义这些城市的拥挤程度为:距离城市 x 距离不大于 k 的城市中的人到达城市 x 时经过该城市的次数。例如:

假设 k = 2,乃爱所在的城市是 1 号城市,树结构如上图所示时,受到影响的城市为 1, 2, 3, 4, 5,因为五个城市距离 1 号城市的距离分别为:0, 1, 2, 2, 2,所以这五个城市都会认为乃爱天下第一。

所以 1 号城市的拥挤程度是 5,2 号城市的拥挤程度是 4,3 号、4 号、5 号城市的拥挤程度都是 1。

现在小 w 想要问你当乃爱依次位于第 1、2、3、4、5……n 座城市时,有多少座城市中的人会认为乃爱天下第一,以及受到影响城市的拥挤程度的乘积,由于这个数字会很大,所以要求你输出认为乃爱天下第一的城市拥挤程度乘积 \mod 10^9 + 7 后的结果。

输入描述

第一行是两个正整数 n、k 表示城市数目,以及距离乃爱所在城市距离不大于 k 的城市中的人认为乃爱天下第一!

接下来 n - 1 行,每行两个正整数 u, v,表示树上一条连接两个节点的边。

输出描述

输出两行。第一行 n 个整数,表示当乃爱依次位于第 1、2、3、4、5......n 座城市时,有多少座城市中的人会认为乃爱天下第一。

第二行 n 个整数,表示当乃爱依次位于第 1、2、3、4、5......n 座城市时,受影响的城市拥挤程度乘积 \mod 10^9 + 7 后的结果。

Solution

我的天,这道题直接暴力O(n^2)有80pts?数据确实很随机

但我的暴力是O(n^2\times log_2n)的,然后就没有然后了

暴力确实很简单,但我的暴力还是太吃操作了

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
    x = 0;
    LL f = 1;
    char c = getchar();
    for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
    for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    x *= f;
}
template <typename T> inline void print(T x){
    if(x < 0) x = -x,putchar('-');
    if(x > 9) print(x / 10);
    putchar(x % 10 + 48);
}
inline void sprint(const string s){
    for(auto x : s) putchar(x);
    return;
}
const int N = 1e5 + 11,M = 35;
const LL Big = 1e9 + 7;
LL n,k;
vector<LL> g[N];
LL ct[N],c[N],ans[N],dep[N],f[N][M];
void dfs(LL u,LL fa){
    for(auto x : g[u]){
        if(x == fa) continue;
        dep[x] = dep[u] + 1;
        f[x][0] = u;
        for(LL i=1;i<=20;i++) f[x][i] = f[f[x][i - 1]][i - 1];
        dfs(x,u);
    }
}
LL lca(LL x,LL y,LL &cnt){
    if(dep[x] < dep[y]) swap(x,y);
    LL stp = dep[x] - dep[y];
    for(LL i=20;i>=0;i--){
        if(stp >= (1 << i)){
            stp -= (1 << i);
            cnt += (1 << i);
            x = f[x][i];
        }
    }
    if(x == y) return x;
    for(LL i=20;i>=0;i--){
        if(f[x][i] != f[y][i]){
            x = f[x][i];
            y = f[y][i];
            cnt += (1 << i) * 2;
        }
    }
    cnt += 2;
    return f[x][0];
}
void dfs2(LL u,LL fa){
    for(auto x : g[u]){
        if(x == fa) continue;
        dfs2(x,u);
        c[u] += c[x];
    }
}
int main(){
//  freopen("c.in","r",stdin);
//  freopen("c.out","w",stdout);
    read(n);read(k);
    for(LL i=1;i<n;i++){
        LL st,dst;
        read(st);read(dst);
        g[st].emplace_back(dst);
        g[dst].emplace_back(st);
    }
    dfs(1,0);
    for(LL i=1;i<=n;i++){
        for(LL j=1;j<=n;j++) c[j] = 0;
        LL tot = 1;
        vector<LL> v;
        v.clear();
        for(LL j=1;j<=n;j++){
            LL pth = 0;
            LL rt = lca(i,j,pth);
            if(pth <= k){
                v.emplace_back(j);
                c[i] ++;
                c[j] ++;
                c[rt] --;
                c[f[rt][0]] --;
            }
        }
        dfs2(1,0);
        for(auto x : v) tot = (tot % Big * c[x] % Big) % Big;
        ct[i] = v.size(),ans[i] = tot % Big;
    }
    for(LL i=1;i<=n;i++){
        print(ct[i]);
        putchar(' ');
    }
    putchar('\n');
    for(LL i=1;i<=n;i++){
        print(ans[i]);
        putchar(' ');
    }
    return 0;
}

我们其实发现这道题就是换根dp,然后我们就做完了

关于我TM差点把换根dp给忘了,所以变量名起得比我的**还长这回事,(这不重要)

我们先以1为根节点,先算出以1为根的情况

我们设dp_{u,i}表示以u为根节点的子树,与其距离不超过i的节点数量

我们就有转移式为: $$ dp_{u,i} = \sum (dp_{x,i - 1}) + 1\\ fp_{u,i} = dp_{u,i} \times \prod fp_{x,i - 1} $$ 上式的$dp_{u,i}$在统计时需要加$1$是因为要包括$u$自己 然后我们根据这个式子先把根为$1$的情况算出来 然后我们开始换根 设$DP_{u,i}$表示以$u$为根节点,与其距离不超过$i$的节点数量 $FP_{u,i}$表示以$u$为根节点,与其距离不超过$i$的节点的经过次数的乘积 我们旨在换根 先给出$DP$的转移式 $$ DP_{u,i} = DP_{fa,i - 1} - dp_{u,i - 2} + dp_{u,i} $$ 我们发现,当把根从$fa$转移到$u$后 有些状态需要抛去 ![](https://cdn.luogu.com.cn/upload/image_hosting/i0nednpy.png) 然后我们再看$FP_{u,i}$的转移方程式 $$ FP_{u,i} = \frac{FP_{fa,i-1}}{fp_{u,i- 2} \times DP_{fa,i- 1}} \times [DP_{fa,i- 1} - dp_{u,i- 2}] \times [\frac{fp_{u,i}}{dp_{u,i}}\times DP_{u,i}] $$ 第一坨在计算$FP_{fa,i - 1}$对$u$为根时做的贡献 第二坨在计算从$fa$的子树转移而来的数量 第三坨在计算从$u$自己的子树转移而来的数量 然后再结合一个乘法逆元,这道题就做完了 Code: ```c++ #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef __int128 SLL; typedef unsigned long long ULL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; template <typename T> inline void read(T &x){ x = 0; LL f = 1; char c = getchar(); for(; !isdigit(c); c=getchar()) if(c == '-') f = -1; for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48); x *= f; } template <typename T> inline void print(T x){ if(x < 0) x = -x,putchar('-'); if(x > 9) print(x / 10); putchar(x % 10 + 48); } inline void sprint(const string s){ for(auto x : s) putchar(x); return; } const int N = 1e5 + 11,M = 35; const LL Big = 1e9 + 7; LL n,k; vector<LL> g[N]; LL dp[N][M],fp[N][M],dp_change_root[N][M],fp_change_root[N][M]; LL qpow(LL x,LL y){ LL ans = 1; while(y){ if((y & 1) == 1) ans = (ans % Big * x % Big) % Big; y >>= 1; x = (x % Big * x % Big) % Big; } return ans; } void dfs(LL u,LL fa){ for(int i=0;i<=k;i++) dp[u][i] = fp[u][i] = 1; for(auto x : g[u]){ if(x == fa) continue; dfs(x,u); for(int i=1;i<=k;i++) dp[u][i] += dp[x][i - 1]; for(int i=1;i<=k;i++) fp[u][i] = (fp[u][i] % Big * fp[x][i - 1] % Big) % Big; } for(int i=0;i<=k;i++) fp[u][i] = (fp[u][i] % Big * dp[u][i] % Big) % Big; return; } void dfs2(LL u,LL fa){ for(auto x : g[u]){ if(x == fa) continue; dp_change_root[x][0] = fp_change_root[x][0] = 1; dp_change_root[x][1] = dp[x][1] + 1; fp_change_root[x][1] = dp_change_root[x][1]; for(int i=2;i<=k;i++) dp_change_root[x][i] = dp_change_root[u][i - 1] - dp[x][i - 2] + dp[x][i]; for(int i=2;i<=k;i++){ LL sector1 = (fp_change_root[u][i - 1] % Big * qpow((fp[x][i - 2] * dp_change_root[u][i - 1] % Big),Big - 2) % Big) % Big; LL sector2 = (dp_change_root[u][i - 1] - dp[x][i - 2] + Big) % Big; LL sector3 = (fp[x][i] % Big * dp_change_root[x][i] % Big * qpow(dp[x][i],Big - 2) % Big) % Big; fp_change_root[x][i] = (sector1 % Big * sector2 % Big * sector3 % Big) % Big; } dfs2(x,u); } return; } int main(){ // freopen("c.in","r",stdin); // freopen("c.out","w",stdout); read(n);read(k); for(LL i=1;i<n;i++){ LL st,dst; read(st);read(dst); g[st].emplace_back(dst); g[dst].emplace_back(st); } dfs(1,0); for(int i=0;i<=k;i++){ dp_change_root[1][i] = dp[1][i]; fp_change_root[1][i] = fp[1][i]; } dfs2(1,0); for(int i=1;i<=n;i++){ print(dp_change_root[i][k]); putchar(' '); } putchar('\n'); for(int i=1;i<=n;i++){ print(fp_change_root[i][k]); putchar(' '); } return 0; } ``` ------ ## D、小w的魔术扑克 ### **题目描述** 小 $w$ 喜欢打牌,某天小 $w$ 与 $dogenya$ 在一起玩扑克牌,这种扑克牌的面值都在 1 到 $n$,原本扑克牌只有一面,而小 $w$ 手中的扑克牌是双面的魔术扑克(正反两面均有数字,可以随时进行切换),小 $w$ 这个人就准备用它来出老千作弊。小 $w$ 想要打出一些顺子,我们定义打出一个 $l$ 到 $r$ 的顺子需要面值为从 $l$ 到 $r$ 的卡牌各一张。小 $w$ 想问问你,他能否利用手中的魔术卡牌打出这些顺子呢? ### **输入描述** 首先输入一行 2 个正整数 $n$、$k$,表示牌面为 $1 \sim n$,小 $w$ 手中有 $k$ 张魔术扑克牌。 然后输入 $k$ 行,每行两个数字,表示卡牌的正和反面的面值。 接下来输入一行一个正整数 $q$,表示 $q$ 组查询,然后每组占一行查询输入两个整数 $l, r$。表示查询小 $w$ 能否打出这么一个 $l$ 到 $r$ 的顺子。 ### **输出描述** 对于输出 “Yes” 表示可以,“No” 表示不可以。(不含引号) 每个查询都是独立的,查询之间互不影响。 ### **输入样例1** ``` 5 3 1 2 2 3 4 4 3 1 2 2 4 1 4 ``` ### **输出样例1** ``` Yes Yes No ``` ### Solution 我们其实发现这道题要转化为图论,对的,就是图论 对于每个卡牌的值,我们其实只需要它在查询中出现一次即可 对于一张牌,我们尝试将它的正反两面的数值连边 然后我们就可以发现我们建边后的图形是一坨 在依托中会找到许多的联通块 当一个有$N$个点的联通块有至少$N$条边时,那么这个联通块一定可以满足每种面值都可以提供一次 我们思考问题的反面 那在什么情况下没有办法满足呢? 好像也只有当一个有$N$个点的联通块有不超过$N-1$条边时才可以不满足了 但一个联通块至少要有$N -1$条边欸! 我们发现,对于我们建好图的依托 只有在里面出现树时,这个联通块才满足每种面值没法都提供一次 那么这个问题就好办了,我们建好图,然后在里面找树 我们继续思考,对于一个查询的区间$[L,R]$,如果在其中出现一颗完整的树,那么是不是就以为着没办法满足这棵树上的每个面额都提供一次,自然就无法满足在$[L,R]$中提供 我们将树抽象化,因为我们只关心最小点和最大点,其他的点都在联通块中,我们于是就把树抽象成了一个个区间$[treeval_{min},treeval_{max}]$这样的区间 此时问题的转化似乎过于明显了 我们有一些区间,现在对于一个查询$[L,R]$,是否有一个区间被完全包含在$[L,R]$中,如果有,输出NO,否则输出YES 我们就可以预处理左端点前缀最大值,对于每个查询的$R$,如果左端点的前缀最大值比$L$还大,输出NO,因为被全包含了 对于上面找树的过程,可以使用DSU,当然dfs也行,你想做个猎奇也可以 Code: ```c++ #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef __int128 SLL; typedef unsigned long long ULL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; template <typename T> inline void read(T &x){ x = 0; LL f = 1; char c = getchar(); for(; !isdigit(c); c=getchar()) if(c == '-') f = -1; for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48); x *= f; } template <typename T> inline void print(T x){ if(x < 0) x = -x,putchar('-'); if(x > 9) print(x / 10); putchar(x % 10 + 48); } inline void sprint(const string s){ for(auto x : s) putchar(x); return; } const int N = 1e5 + 11; int n,k,q,cnt,id,m; int mxlft[N]; struct node{ int l,r; }a[N]; struct DSU{ vector<int> par,sz,mn,mx,edge; DSU(int n){ par.resize(n + 1); sz.resize(n + 1,1); edge.resize(n + 1,0); mn.resize(n + 1); mx.resize(n + 1); for(int i=1;i<=n;i++) par[i] = mn[i] = mx[i] = i; } int find(int u){ if(par[u] != u) return par[u] = find(par[u]); else return u; } void comb(int u,int v){ int rtu = find(u),rtv = find(v); if(rtu == rtv) return; if(sz[rtu] < sz[rtv]) swap(rtu,rtv); par[rtv] = rtu; sz[rtu] += sz[rtv]; edge[rtu] += edge[rtv]; mn[rtu] = min(mn[rtu],mn[rtv]); mx[rtu] = max(mx[rtu],mx[rtv]); return; } bool connected(int u,int v){ if(find(u) == find(v)) return 1; return 0; } }; bool cmp(node x,node y){ if(x.r < y.r) return 1; return 0; } int main(){ // freopen("d.in","r",stdin); // freopen("d.out","w",stdout); read(n);read(k); DSU unit(n); for(int i=1;i<=k;i++){ int st,dst; read(st);read(dst); int rt = unit.find(st); unit.edge[rt] ++; unit.comb(st,dst); } for(int i=1;i<=n;i++){ if(unit.find(i) != i) continue; if(unit.edge[i] == unit.sz[i] - 1){ a[++ m].l = unit.mn[i]; a[m].r = unit.mx[i]; } } sort(a + 1,a + 1 + m,cmp); for(int i=1;i<=m;i++) mxlft[a[i].r] = max(mxlft[a[i].r],a[i].l); for(int i=1;i<=n;i++) mxlft[i] = max(mxlft[i],mxlft[i - 1]); read(q); while(q --){ int l,r; read(l);read(r); if(mxlft[r] >= l) sprint("NO"); else sprint("YES"); putchar('\n'); } return 0; } ```