【模拟赛】搜索综合

· · 个人记录

考试链接。

概述

题号 难度 主要知识点 其它知识点
1 橙 BFS 拆位、质数判断
2 黄 棋盘 DFS 小剪枝、路径输出
3 绿 DFS STL 全排列
4 蓝 棋盘 BFS 图论
5 橙 模拟 时间复杂度证明

个人评价:

分数线:

\textbf{T1} 质数序列

前言

本题主要考察 BFS,注意多测清空。

预估难度:橙或黄。

题意

给定四位质数 a,b,从 a 变换到 b,每一步只能变一位,且必须变成质数,求最少步数。

题解

每次转移时,对于当前队首的质数 x,我们枚举改变第几位数,以及改变成多少,并计算出新的数 nx。当 nx 是质数且没有被访问过时,更新 nx 的答案,标记被访问,然后入队。如果队首恰好等于 b,那么直接输出距离。如果一直没有访问到,最后输出 impossible。注意多测清空!!!

复杂度分析

这里定义值域上限 c=9999,质数个数 p=1061。

显然空间复杂度 O(c),下面分析时间复杂度。

每个质数最多被入栈出栈一次,O(p);每一次转移枚举 4\times10 种情况,然后用 O(\sqrt c) 判断质数,复杂度 O(40\times\sqrt c)。所以单测 O(40\times p\times\sqrt c),总复杂度 O(40\times n\times p\times \sqrt c),约为 4.2\times10^7,可以通过。

标程

#include <bits/stdc++.h>
using namespace std;
int T, a, b;
inline bool isPrime(int x) {
    if (x < 2) return 0;
    for (int i = 2; i * i <= x; i++)
        if (x % i == 0)
            return 0;
    return 1;
}
const int N = 1e4 + 5;
int dis[N], p[5];
inline int Bfs(void) {
    memset(dis, 0x3f, sizeof dis);
    queue<int> q;
    q.push(a);
    dis[a] = 0;
    while (!q.empty()) {
        int x = q.front(); q.pop();
        if (x == b) return dis[x];
        p[0] = x % 10, p[1] = x / 10 % 10, p[2] = x / 100 % 10, p[3] = x / 1000;
        int Pow = 1;
        for (int k = 0; k <= 3; k++) {
            for (int i = 0; i <= 9; i++) {
                if (k == 3 && i == 0) continue;
                int nx = x + (i - p[k]) * Pow;
                if (!isPrime(nx)) continue;
                if (dis[nx] <= dis[x] + 1) continue;
                dis[nx] = dis[x] + 1;
                q.push(nx);
            }
            Pow *= 10;
        }
    }
    return -1;
}
int main(void) {
    ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
    cin >> T;
    while (T--) {
        cin >> a >> b;
        int res = Bfs();
        if (res == -1)
            cout << "impossible" << endl;
        else
            cout << res << endl;
    }
    return 0;
}

\textbf{T2} 马的路径

前言

本题考查棋盘 DFS。不需要任何剪枝。

预估难度:黄。

题意

给定一个棋盘,马从左上角跳到右下角,不重复。已知每一行、每一列有多少个格子被经过。求所有可能的路径。

题解

DFS 到点 (x,y) 时,先标记访问过这个点,然后将 a_x 和 b_y 分别减 1。枚举 8 个方向,算出对应的 (nx,ny)。如果合法,未被访问过并且 a_{nx} 和 b_{ny} 都大于 0,那么去 DFS 这一个点。DFS 结束时将 a,b 和标记还原。

当 (x,y)=(m,n) 时,如果所有的 a,b 都为 0,那么将路径加入答案列表。最后输出答案。

注:事实上,如果你想要骗分,你可以输出 1,然后不给出路径(按照个数输出任意整数)。在这道题中,有 4 个测试点答案为 1,所以你能得到 4\times(5\times40\%)=8 分。

标程

#include <bits/stdc++.h>
using namespace std;
const int N = 10;
const int dx[8] = {-2, -2, -1, -1, 1, 1, 2, 2}, dy[8] = {-1, 1, -2, 2, -2, 2, -1, 1};
int n, m, a[N], b[N];
bool vis[N][N];
struct route {
    vector<int> vx, vy;
    inline bool operator < (const route &cmp) const {
        for (int i = 0; i < vx.size(); i++) {
            if (vx[i] != cmp.vx[i])
                return vx[i] < cmp.vx[i];
            if (vy[i] != cmp.vy[i])
                return vy[i] < cmp.vy[i];
        }
        return 0;
    }
    inline void push_back(int x, int y) {
        vx.push_back(x);
        vy.push_back(y);
    }
    inline void pop_back(void) {
        vx.pop_back();
        vy.pop_back();
    }
    inline void output(void) {
        cout << vx.size() << ' ';
        for (int i = 0; i < vx.size(); i++)
            cout << vx[i] << ' ' << vy[i] << ' ';
        cout << endl;
    }
} now;
set<route> ans;
inline bool check(void) {
    for (int i = 1; i <= n; i++)
        if (a[i] != 0)
            return 0;
    for (int j = 1; j <= m; j++)
        if (b[j] != 0)
            return 0;
    return 1;
}
void Dfs(int x, int y) {
    vis[x][y] = 1;
    a[x]--, b[y]--;
    now.push_back(x, y);
    if (x == n && y == m) {
        if (check())
            ans.insert(now);
        vis[x][y] = 0;
        a[x]++, b[y]++;
        now.pop_back();
        return;
    }
    for (int i = 0; i < 8; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        if (nx < 1 || nx > n || ny < 1 || ny > m) continue;
        if (vis[nx][ny]) continue;
        if (a[nx] == 0 || b[ny] == 0) continue;
        Dfs(nx, ny);
    }
    vis[x][y] = 0;
    a[x]++, b[y]++;
    now.pop_back();
}
int main(void) {
    ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int j = 1; j <= m; j++)
        cin >> b[j];
    Dfs(1, 1);
    cout << ans.size() << endl;
    for (route r : ans)
        r.output();
    return 0;
}

\textbf{T3} 货物运输

前言

本题改编自吃奶酪,但只考察 DFS(而不是状压 DP)。

预估难度:黄或绿。

题意

从 1 出发,到 2\sim n 送货。货车只能装一定的体积,并且货物不可被分割。求送完所有货物后回到 1 的最小距离。

题解

首先,我们知道,一辆车从 1 出发了,中途一定不会回到 1。
否则剩下的那些货物装上去,占用体积而不运输,不如再来一趟车运输。

对于每个货物 x\;(2\le x\le n),假设前面的货物一共用了 tot 趟车(注意:用了不代表用完,也不代表用的最少),第 i 趟车装 p_{i,1}\sim p_{i,t_i} 号货物,总体积 m_i。

那么对于货物 x,它有以下两种选择:

注意:我们并不知道运输的顺序,只是记录运输哪些东西。

全部选完后,枚举每一辆车货物如何运输最优(全排列),将所有车的最小值相加,更新答案。

标程

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 12;
int n, m, ans = 1e18, v[N], idx[N], a[N][N];
int tot, status[N], sum[N];
inline int Dist(int s) {
    int ans = 1e18;
    int cnt = 0;
    for (int i = 2; i <= n; i++)
        if (s >> (i - 2) & 1)
            idx[++cnt] = i;
    do {
        int res = a[1][idx[1]] + a[idx[cnt]][1];
        for (int i = 2; i <= cnt; i++) {
            res += a[idx[i - 1]][idx[i]];
            if (res >= ans) break;
        }
        ans = min(ans, res);
    } while (next_permutation(idx + 1, idx + cnt + 1));
    return ans;
}
void Dfs(int step) {
    if (step > n) {
        int res = 0;
        for (int i = 1; i <= tot; i++)
            res += Dist(status[i]);
        ans = min(ans, res);
        return;
    }
    for (int i = 1; i <= tot; i++) {
        if (sum[i] + v[step] > m) continue;
        sum[i] += v[step];
        status[i] ^= (1 << step - 2);
        Dfs(step + 1);
        sum[i] -= v[step];
        status[i] ^= (1 << step - 2);
    }
    tot++;
    status[tot] = (1 << step - 2), sum[tot] = v[step];
    Dfs(step + 1);
    tot--;
}
signed main(void) {
    ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for (int i = 2; i <= n; i++)
        cin >> v[i];
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            cin >> a[i][j];
//  for (int k = 1; k <= n; k++)
//      for (int i = 1; i <= n; i++)
//          for (int j = 1; j <= n; j++)
//              a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
    Dfs(2);
    cout << ans << endl;
    return 0;
}

\textbf{T4} 开灯

前言

本题考查 BFS 求解棋盘问题。

这道题是 USACO 2015 年 12 月银组原题。

预估难度:黄或绿。(这是个人评价。洛谷上评为蓝,有点虚。)

题意

给定一个棋盘,贝茜只能在亮着灯的格子走动,一些房间有其他房间灯的开关。贝茜从左上角(开始亮着灯)开始,只能上下左右走,求最终最多能开多少灯,而不是能走到多少个格子!(样例如果这样理解还可以通过。)

题解

首先建图,用一个二维 vector(定义:vector<pair<int, int>> graph[105][105])。

记录两个状态:vis 表示是否走到过(也表示是否可以走到),on 表示是否开着灯。

BFS 的过程如下:

答案为 \sum_{1\le x,y\le n}on[x][y],即开着的灯的数量。

时间复杂度:每个点至多入队一次,每次转移 O(n^2),所以总时间复杂度 O(n^4)。n\le100,刚好可以通过(有可能需要一点卡常)。

有更优的解法,请自行思考。

标程

#include <bits/stdc++.h>
#define r q.front()
using namespace std;
const int N = 105;
const int dx[4] = {-1, 0, 0, 1};
const int dy[4] = {0, -1, 1, 0};
int n, m, a, b, c, d;
struct node {
    int x, y;
};
vector<node> v[N][N];
queue<node> q;
bool vis[N][N], on[N][N];
inline bool check(int x, int y) {
    for (int i = 0; i < 4; i++) {
        int lx = x + dx[i], ly = y + dy[i];
        if (lx < 1 || lx > n || ly < 1 || ly > n) continue;
        if (vis[lx][ly]) return 1;
    }
    return 0;
}
int main(void) {
    ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        cin >> a >> b >> c >> d;
        v[a][b].push_back({c, d});
    }
    vis[1][1] = on[1][1] = 1;
    q.push({1, 1});
    while (!q.empty()) {
        int x = r.x, y = r.y; q.pop();
        for (auto i : v[x][y]) {
            int nx = i.x, ny = i.y;
            on[nx][ny] = 1;
        }
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                if (on[i][j] && !vis[i][j] && check(i, j)) {
                    vis[i][j] = 1;
                    q.push({i, j});
                }
    }
    int ans = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            ans += on[i][j];
    cout << ans << endl;
    return 0;
}

\textbf{T5} 数学题

前言

骗人题,这么大的数据范围怎么可能是搜索。

本题考查枚举和时间复杂度计算。

预估难度:橙。

初步想法

看到向下取整,心生厌恶。

如果没有向下取整,即 \begin{cases}y\cdot z=a\\x\cdot z=b\\x\cdot y=c\end{cases},结合 a,b,c>0 可以直接解出 x=\sqrt{\dfrac{bc}{a}},y=\sqrt{\dfrac{ca}{b}},z=\sqrt{\dfrac{ab}{c}},非常轻松。

于是,我们暴力枚举三个向下取整(范围:1\sim m 的整数),解出 x,y,z 后再看向下取整是否满足条件。

然而……这样是 O(m^3) 的,肯定会超时。

小优化

记 p=\min\{a,b,c\}。我们可以只用枚举 1\sim p 的整数。

另外,在枚举的时候,我们需要保证 \lfloor x\rfloor\cdot\lfloor y\rfloor\cdot\lfloor z\rfloor\le p。

这样,复杂度立刻变成 O(p\ln^2p) 的!

你肯定不信,请看下面的证明:

复杂度证明

注:此段用了大量的数学知识,并不要求掌握,只需了解。

一共枚举的次数就等于满足 i\times j\times k\le p 的三元正整数组 (i,j,k) 的个数,我们只需要证明这种数组的个数与 p\ln^2p 同阶。

先证明几个引理:

引理 1: 对于 \forall x>0,\dfrac{x}{1+x}<\ln(1+x)。
证明: 设 f(x)=\dfrac{x}{1+x}-\ln(1+x)。则

\begin{aligned}f'(x)&=\left(\dfrac{x}{1+x}\right)'-\ln'(1+x)\\&=\dfrac{1\cdot(1+x)-x\cdot1}{(1+x)^2}-\dfrac{1}{1+x}\cdot1\\&=\dfrac{1-(1+x)}{(1+x)^2}\\&=-\dfrac{x}{(1+x)^2}\\&<0\end{aligned}

于是 f(x) 在 (0,+\infty) 上单调递减。
所以 f(x)<f(0)=\dfrac{0}{1+0}-\ln(1+0)=0,故左式小于右式,得证。

引理 2: 对于任意正整数 n\;(\ge2),\dfrac11+\dfrac12+\dfrac13+\dots+\dfrac1n<1+\ln n。
证明: 由引理 1,\dfrac 1m=\dfrac{\frac{1}{m-1}}{1+\frac{1}{m-1}}<\ln\left(1+\dfrac{1}{m-1}\right)=\ln m-\ln(m-1),对 m=2,3,\dots,n 求和,得到 \dfrac12+\dfrac13+\dots+\dfrac1n<\ln n,两边同时加 1 得到右侧式子。

引理 3. 对于 n\in\mathbb N_+,满足 i\times j\le n 的正整数对 (i,j) 的个数不超过 n(1+\ln n)。
证明: 固定 i,有 \left\lfloor\dfrac{n}{i}\right\rfloor 个满足条件的 j,所以一共有 P=\sum_{i=1}^n\left\lfloor\dfrac{n}{i}\right\rfloor 个满足条件的正整数对。 由引理 2,P\le\sum_{i=1}^n\dfrac{n}{i}=n\sum_{i=1}^n\dfrac{1}{i}<n(1+\ln n),即为结论。

再看原题,固定 k 时,满足条件的三元数组等于 i\times j\le\dfrac pk 的个数。由引理 3,可以知道是不超过 \dfrac pk+\dfrac pk\ln\dfrac pk 的。对 k=1,2,\dots,p 求和,知满足条件的三元数组个数不超过

\begin{aligned}\sum_{k=1}^p(\dfrac pk+\dfrac pk\ln\dfrac pk)&=\sum_{k=1}^p\dfrac pk+\sum_{k=1}^p\dfrac pk\ln\dfrac pk\\&=p\sum_{k=1}^p\dfrac 1k+p\sum_{k=1}^p\dfrac 1k(\ln p-\ln k)\\&<p(1+\ln p)+p\ln p\left(\sum_{k=1}^p\dfrac 1k\right)-p\sum_{k=1}^p\dfrac{\ln k}{k}\\&<p(1+\ln p)+p\ln p(1+\ln p)\\&=p\ln^2p+2p\ln p+p\end{aligned}

这个数与 p\ln^2p 同阶,所以此算法的时间复杂度为 O(p\ln^2p)。

标程

#include <bits/stdc++.h>
#define int long long
using namespace std;
int cnt, a, b, c;
signed main(void) {
    ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
    cin >> a >> b >> c;
    int m = min({a, b, c});
    for (int i = 1; i <= m; i++)
        for (int j = 1; i * j <= m; j++)
            for (int k = 1; i * j * k <= m; k++) {
                double x = sqrt(1.0 * b * c * i / a / j / k);
                double y = sqrt(1.0 * a * c * j / b / k / i);
                double z = sqrt(1.0 * a * b * k / c / i / j);
                if ((int)floor(x) == i && (int)floor(y) == j && (int)floor(z) == k)
                    cnt++;
            }
    cout << cnt << endl;
    return 0;
}