2025 暑期集训A班 模拟赛#4
_little_duck_
·
·
个人记录
A.网格连通(grid)
题意
给定 2^{60} 行 60 列的网格,其中一些网格被涂成了黑色。
网格是按照如下规律涂色的:
行和列的标号从 0 开始,记第 x 行第 y 列的网格是 (x, y)。最左上角的网格是 (0, 0)。
对于任何一个网格 (x, y) 而言,如果 x 的二进制表示的第 y 位为 0 (最低位是第 0 位),则网格 (x, y) 是白色;否则,网格 (x, y) 是黑色。
现在你需要处理 T 次询问。每次询问给定 x,y,保证 (x, y) 网格是黑色。你需要回答 存在多少黑色网格与 (x, y) 连通。当两个黑色网格之间存在只上下左右移动至相邻网格, 且不经过白色网格的可达路径时,我们认为这两个黑色网格相互连通。
思路
观察下图可得,每一次的周期都是上一次周期的两倍:
所以对于 x 的第 y 位找到它最高位连续 1 的位置,再从这个位置往前找到最低位连续 1,同时统计答案:
\sum\limits_{l \le i \le r} 2^i
## 代码
```cpp
#include<bits/stdc++.h>
using namespace std;
#define int long long
int ans;
int ksm(int a, int b){
int sum = 1;
while(b){
if(b & 1){
sum *= a;
}
a *= a;
b /= 2;
}
return sum;
}
signed main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
freopen("grid.in", "r", stdin);
freopen("grid.out", "w", stdout);
int T;
cin >> T;
while(T--){
int x, y;
cin >> x >> y;
ans = 0;
for(int i = y; i < 60; i++){
if(((x >> i) & 1) != 1){
break;
}
if(i == y){
ans += 2 * ksm(2, i) - 1;
}
else{
ans += ksm(2, i);
}
}
cout << ans << "\n";
}
return 0;
}
```
# B.好多余(mod)
## 题意
给定 $n$ 和长度为 $n$ 的数组 $a$,求:

## 思路
枚举模数,$cnt_i$ 表示满足 $a_j \le i$ 的 $a_j$ 个数,原式可化为:
$$\sum\limits_{i=1, cnt_i-cnt_{i-1}\neq0}^{2 \times 10^5} \sum\limits_{j=1}^{n} a_j \bmod i$$
可再化简为:
$$\sum\limits_{i=1, cnt_i-cnt_{i-1}\neq0}^{2 \times 10^5} \sum\limits_{j=1,cnt_j - cnt_{j-1}\neq0}^{2 \times 10^5} j \bmod i$$
进一步化简为:
$$\sum\limits_{i=1}^n a_i \times n - [\sum\limits_{i=1}^{2\times 10^5}\sum\limits_{j=0}^{i \times j \le 2 \times 10^5} (cnt_i - cnt_{i - 1}) \times i \times (cnt_{(j + 1) \times i - 1} - cnt_{i \times j - 1}) \times j]$$
## 代码
```cpp
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 4e5 + 10;
int n, a[N], cnt[N], ans, sum;
signed main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
freopen("mod.in", "r", stdin);
freopen("mod.out", "w", stdout);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
cnt[a[i]]++;
ans += n * a[i];
}
for(int i = 1; i <= 4e5; i++){
cnt[i] += cnt[i - 1];
}
for(int i = 1; i <= 2e5; i++){
int ttt = cnt[i] - cnt[i - 1];
for(int j = 0; j * i <= 2e5; j++){
int c = 0;
if(j * i - 1 >= 0){
c = cnt[(j + 1) * i - 1] - cnt[i * j - 1];
}
else{
c = cnt[(j + 1) * i - 1];
}
sum += c * ttt * i * j;
}
}
cout << ans - sum;
return 0;
}
```
# C.无限猴子(monkey)
## 题意
给定一个仅包含小写字母的字符串 $S$ ,和一只乱摁键盘的猴子。记显示在猴子的电 脑上的字符串是 $T$ 。最初 $T$ 是空串。
猴子会反复地从 $26$ 个字母的按键中等概率随机选择一个按下。每当猴子摁下一个按键,这个按键对应的字母就会被添加到字符串 $T$ 的尾部,使 $T$ 的长度增加 $1$。
然而,每当猴子摁下一次按键,字符串 $T$ 产生变化,若变化后 $T$ 不再是 $S$ 的子串时,猴子就会死亡。
请求出到猴子死亡为止,字符串 $T$ 的期望长度是多少?
## 思路
考虑用字典树维护。
先对字符串 $S$ 的每个字串建树接着,从根节点开始枚举字串:
1. 统计当前节点的非空儿子节点的个数 $sum
- 计算当前答案贡献 (l + 1) \times P(u) \times \frac{26-sum}{26},l 表示当前字串长度,u 表示当前节点编号。
- 转移下一步,nxt_u 表示节点 u 在树上的下一个:
(u, P(u),l) \Rightarrow (nxt_u,\frac{P(u)}{26},l+1)
代码
#include<bits/stdc++.h>
using namespace std;
using ld = long double;
#define int long long
using pii = pair<int, int>;
const int N = 2e6 + 10;
int tr[N][30], cnt = 1;
ld ans;
int n;
void insert(string s){
int u = 0;
for(auto ch : s){
if(tr[u][ch - 'a'] == 0){
tr[u][ch - 'a'] = ++cnt;
}
u = tr[u][ch - 'a'];
}
}
void query(int u, ld p, int l){
int sum = 0;
for(int i = 0; i < 26; i++){
sum += tr[u][i] != 0;
}
ans += 1.0 * (l + 1) * (26 - sum) / 26 * p;
for(int i = 0; i < 26; i++){
if(tr[u][i] != 0){
query(tr[u][i], p / 26, l + 1);
}
}
}
string s;
signed main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
freopen("monkey.in", "r", stdin);
freopen("monkey.out", "w", stdout);
cin >> s;
for(int i = 0; i < s.size(); i++){
string t = "";
for(int j = i; j < min((int)s.size(), i + 13); j++){
t += s[j];
}
insert(t);
}
query(0, 1, 0);
cout << fixed << setprecision(10) << ans << " ";
return 0;
}
D.传销(money)
题意
某个小镇上有 n 个易上当受骗的人:这 n 个人被索取一元钱时,会乐意给出一元钱 并向另外两个人索取一元钱。不过他们每个人都只愿意参与一次:如果再有第二个人向 他索取一元钱,他不会接受。 这个骗局是从小镇外传来的:某个镇外的人向这个小镇上的某个人索取了一元钱。这 将在小镇上引起一系列互相索取的活动。
已知每位镇民被索取一元钱时,他会转而向哪两位镇民索取一元钱。请判断在一次 传销模式中,有哪些镇民有可能蒙受损失?
思路
考虑三种状态 (i, 0), (x, i), (y,i),表示分别从 i, x, y 开始的骗钱方案,第二位是防止向从他索要钱的人要钱。一个人只有当三种情况都能要到钱,才算稳赚不赔,所以只需将三种状态的结果与起来判断即可。
代码
#include<bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int N = 1010;
int n;
vector<int> g[N];
pii rec[N];
queue<int> q;
bitset<N> bfs(int u, int tag){
bitset<N> vis;
q.push(u);
vis[u] = 1, vis[tag] = 1;
while(!q.empty()){
int u = q.front();
q.pop();
for(int v : g[u]){
if(vis[v]) continue;
q.push(v);
vis[v] = 1;
}
}
vis[tag] = 0;
return vis;
}
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
freopen("money.in", "r", stdin);
freopen("money.out", "w", stdout);
cin >> n;
for(int i = 1, x, y; i <= n; i++){
cin >> x >> y;
g[x].push_back(i);
g[y].push_back(i);
rec[i] = {x, y};
}
for(int i = 1; i <= n; i++){
auto [x, y] = rec[i];
bitset<N> vis = bfs(i, 0) & bfs(x, i) & bfs(y, i);
bool f = 0;
for(int j = 1; j <= n; j++){
if(vis[j]){
f = 1;
break;
}
}
if(f) cout << "Y";
else cout << "N";
}
return 0;
}