蒟蒻 Tweetuzki 的信心赛 赛后题解

· · 个人记录

感谢各位巨神对我第一次举办公开赛的关心与支持。我对 T6 的重题以及 T3, T4 的数据错误表示深深的歉意……

首先恭喜 @猪脑子、@interestingLSY、@Great_Influence、@KirisameMarisa、@_rqy、@142857cs、@SSerxhs、@DDOSvoid 等 8 位 AK!

接下来是我的一些思路和程序,如果有不一样的做法欢迎提出。

Solution

T1:Tweetuzki 爱整除

任意的 x (x > k) 只要满足 x \equiv 1 (\text{mod}\ k),就能满足题目所述条件。

我们设原数化为 a_n \times x^{n} + a_{n - 1} \times x^{n - 1} + \cdots + a_1 \times x^1 + a_0 \times x_0 的形式,这样原数各位之和就是 a_n + a_{n - 1} + \cdots + a_1 + a_0。根据条件,各数位之和 a_n + a_{n - 1} + \cdots + a_1 + a_0 能够被 k 整除,且原数 a_n \times x^{n} + a_{n - 1} \times x^{n - 1} + \cdots + a_1 \times x^1 + a_0 \times x_0 也能被 k 整除。因而

a_n + a_{n - 1} + \cdots + a_1 + a_0\equiv a_n \times x^{n} + a_{n - 1} \times x^{n - 1} + \cdots + a_1 \times x^1 + a_0 \times x_0 (\text{mod}\ k)

根据恒等定理,

x^n = x^{n - 1} = \cdots = x_1 = x \equiv 1 (\text{mod}\ k)

显然该方程对于任意一个 x \equiv 1 (\text{mod}\ k) 都成立。又因为题目要求 x > k,输出 k+ 1 即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main() {
    int k;
    cin >> k;
    cout << k + 1 << endl;
    return 0;
}

T2:Tweetuzki 爱等差数列

设等差数列的首项为 x,项数为 n,由公差为 1 可得末项为 x + n - 1。

代入高斯求和的公式,即 (首项 + 末项) × 项数 \div 2,可得

s = (x + x + n - 1) \times n \div 2

变形一下可以得到

2 \times s = (x + x + n - 1) \times n 注意到左边的 $(x + x + n - 1)$ 项是一定会大于 $n$ 的,那么 $2 \times s$ 也就一定大于 $n^2$,也就是 $1 \le n < \sqrt{2s}$。由于 $s$ 只有 $10^{12}$,暴力枚举 $n$ 的值,带入公式就可以解出 $x$,合法性只需判断 $x$ 是否为整数即可。 时间复杂度 $O(\sqrt{s})$。 PS:本题的验题人巨佬说这道题有 Pollard-Rho 之类的更高效的做法,但是 Tweetuzki 太弱了,还不知道怎么做……如果有知道的同学可以在评论区发评论哦~ 代码: ```cpp #include <iostream> #include <cstdio> #include <cstring> using namespace std; typedef long long llt; llt S; llt ans_x, ans_n; int main() { cin >> S; for (llt n = 1; n * n <= S * 2; ++n) { llt fz = 2 * S - n * n + n, fm = 2 * n; if (fz % fm == 0) { llt x = fz / fm; ans_x = x; ans_n = n; } } cout << ans_x << " " << ans_x + ans_n - 1 << endl; return 0; } ``` ### T3:Tweetuzki 爱军训 对于 $n \le 8$ 的数据:暴力模拟排列,检验是否可行并更新答案即可。时间复杂度 $O(n! \times n)$。 对于 $n \le 20$ 的数据:我们发现,每个人只有两种情况——在教官从 $1$ 到 $n$ 的路上(下文简称“去途”)出列或在教官从 $n$ 到 $1$ 的路上(下文简称“回途”)出列。那么我们把去途出列看做 $0$,回途出列看做 $1$,暴力枚举每种状态,更新答案即可。时间复杂度 $O(2^n \times n)$。 枚举法已经做不动了。于是对于更大的数据,我们考虑贪心。 我们先假设所有人都在去途上出列。这时候可以得到一个最初始的答案,我们记作 $base$。这时候,我们考虑一下,如果把 $i$ 号同学从去途出列改为回途出列,会对答案造成什么影响呢? 例如对于样例数列 $\texttt{7 8 1 2 5}$,如果将坏值为 $7$ 的那名同学改为回途出列,那么数列就变成了 $\texttt{8 1 2 5 7}$;若再将坏值为 $8$ 的同学改为回途出列,那么数列就变成了 $\texttt{1 2 5 8 7}$。容易注意到:当将第 $i$ 位同学改为回途出列后,编号为 $i + 1$ 到 $n$ 的所有同学出列顺序往前挪了一位,而第 $i$ 号同学往后挪了 $n - i$ 位。那么对于答案的贡献 $d_i = w_i \times (n - i) - w_{i + 1} - w_{i + 2} - w_{i + 3} - \cdots - w_n$。容易发现:出列顺序的调换对于答案是不会有影响的,也就是说每个 $d_i$ 是相对独立、互不干扰的。那么可以贪心地选择:若 $d_i > 0$ 则选择调换,否则不调换。 于是答案可以被表示成 $base + \sum_{i = 1}^n \max(d_i, 0)$。其中 $d_i$ 可以利用后缀和预处理出来。时间复杂度 $O(n)$。 代码: ```cpp #include <iostream> #include <cstdio> #include <cstring> using namespace std; typedef long long llt; const int MaxN = 1000000 + 5; int N; llt W[MaxN]; llt D[MaxN], suf[MaxN]; bool Late[MaxN]; void init() { scanf("%d", &N); for (int i = 1; i <= N; ++i) scanf("%lld", &W[i]); } void solve() { for (int i = N; i >= 1; --i) suf[i] = suf[i + 1] + W[i]; for (int i = 1; i <= N; ++i) D[i] = W[i] * (N - i) - suf[i + 1]; llt ans = 0; for (int i = 1; i <= N; ++i) ans += W[i] * i; for (int i = 1; i <= N; ++i) if (D[i] > 0) { ans += D[i]; Late[i] = true; } cout << ans << endl; for (int i = 1; i <= N; ++i) if (Late[i] == false) printf("%lld ", W[i]); for (int i = N; i >= 1; --i) if (Late[i] == true) printf("%lld ", W[i]); putchar('\n'); } int main() { init(); solve(); return 0; } ``` ### T4:Tweetuzki 爱伊图 考虑扫(da)描(bao)线(li)。一个很显然的想法是:从上到下扫描每一行,再枚举每一行内的所有格子。若有 `#`,说明该处有数字,对该数字进行判断,然后将该数字所占矩形区域全部赋为 `.`,再继续扫即可。 这种做法可行,但是遇到 $4$ 就非常棘手。$4$ 在最上方有两个分开的 `#`,需要加上对于 $1$ 和 $4$ 的特判,相对麻烦。并且删除矩形区域所占需要带 $15$ 的常数,效率可能不佳。于是考虑从下往上进行扫描,这样一个数字就仅对应一个联通的 `#` 块。而且也无需清空该矩形区域,遇到数字时,只要加个 $\texttt{vis}$ 标记,表示这些区域上已经有过数字,不会再有了即可。 时间复杂度 $O(rc)$。 代码: ```cpp #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int MaxR = 10 + 5, MaxC = 100000 + 5; int R, C; char M[MaxR][MaxC]; bool vis[MaxC]; int Ans[MaxC]; void init() { memset(M, '.', sizeof M); memset(Ans, -1, sizeof Ans); scanf("%d %d", &R, &C); for (int i = 1; i <= R; ++i) for (int j = 1; j <= C; ++j) { M[i][j] = getchar(); while (M[i][j] != '#' && M[i][j] != '.') M[i][j] = getchar(); } } void solve() { for (int i = R; i >= 5; --i) { for (int j = 1; j <= C; ++j) { if (vis[j] == true) continue; if (M[i][j] == '#') { if (M[i][j + 1] == '#') { // 0, 2, 3, 5, 6, 8, 9 if (M[i - 2][j + 1] == '.') Ans[j] = 0; else if (M[i - 1][j + 2] == '.' && M[i - 3][j] == '.') Ans[j] = 2; else if (M[i - 1][j] == '.' && M[i - 3][j] == '.') Ans[j] = 3; else if (M[i - 1][j] == '.' && M[i - 3][j + 2] == '.') Ans[j] = 5; else if (M[i - 3][j + 2] == '.') Ans[j] = 6; else if (M[i - 1][j] == '.') Ans[j] = 9; else Ans[j] = 8; vis[j] = vis[j + 1] = vis[j + 2] = true; } else { // 1, 4, 7 if (M[i - 4][j - 1] == '#') Ans[j] = 7; else if (M[i - 2][j - 1] == '#') Ans[j] = 4; else Ans[j] = 1; vis[j] = true; if (Ans[j] != 1) vis[j - 1] = vis[j - 2] = true; } } } } for (int i = 1; i <= C; ++i) if (Ans[i] >= 0) printf("%d", Ans[i]); putchar('\n'); } int main() { init(); solve(); return 0; } ``` ### T5:Tweetuzki 爱序列 本题改编自 [CF977D Divide by three, multiply by two](https://www.luogu.org/problemnew/show/CF977D). 首先可以推出一个结论:最终的序列中不会有两个相同的数。因为从一个数开始,每乘一个 $2$,就会增加一个双数,而无论除掉多少次的 $3$,都不可能把这个 $2$ 的因子给除掉。 于是这样便满足了动态规划的前提。设 $dp_i$ 表示到 $i$ 号点时的最长路长度。那么 $dp_i$ 可以由任意满足 $a_i \div 3 = a_j$ 或 $a_i \times 2 = a_j$ 的 $j$ 转移而来($dp_i = dp_j + 1$)。 $dp$ 的转移可以通过建图的方法来解决。注意到,如果这一个数是 $x$,那么它的下一个数,要么是 $x \div 3$,要么是 $ x \times 2 $。所以我可以任意连向值为 $x \div 3$ 或 $ x \times 2$ 的点,然后这个点可以继续转移。寻找的过程可以使用 $\texttt{map}$ 优化到 $\log n$ 的复杂度。(当然也可以哈希) 根据以上的结论,这一定是一张有向无环图,于是可以在上面跑拓扑排序,来顺便进行动态规划的转移。在每次的转移后,记录前驱节点,以方便之后打印方案的路径还原。最终找到一个 $dp$ 值最大的元素,输出该值,并路径还原即可。 这样,时间复杂度是 $O(n \log n)$ 的。但是总共复杂度的瓶颈在于 $\texttt{map}$ 的查找,那么如果用哈希来替代,就可以跑到 $O(n)$ 的复杂度了。 (upt:感谢 @[一扶苏一](https://www.luogu.org/space/show?uid=65363) 构造数据,暴搜已被 Hack) 代码: ```cpp #include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <map> using namespace std; typedef long long llt; const int MaxN = 100000 + 5, MaxM = 200000 + 5; int N; int cnte; llt A[MaxN]; int Head[MaxN], To[MaxM], Next[MaxM]; int dp[MaxN], ind[MaxN], pre[MaxN]; int st[MaxN], tp; map <llt, int> mp; queue <int> que; inline void add_edge(int from, int to) { cnte++; To[cnte] = to; ind[to]++; Next[cnte] = Head[from]; Head[from] = cnte; } void init() { scanf("%d", &N); for (int i = 1; i <= N; ++i) { scanf("%lld", &A[i]); if (mp[A[i]] == 0) mp[A[i]] = i; } } void top_sort() { for (int i = 1; i <= N; ++i) if (ind[i] == 0) { dp[i] = 1; que.push(i); } while (!que.empty()) { int u = que.front(); que.pop(); for (int i = Head[u]; i; i = Next[i]) { int v = To[i]; if (dp[u] + 1 > dp[v]) { dp[v] = dp[u] + 1; pre[v] = u; } ind[v]--; if (ind[v] == 0) que.push(v); } } } void solve() { for (int i = 1; i <= N; ++i) { if (i != mp[A[i]]) continue; if (mp[A[i] * 2] != 0) add_edge(i, mp[A[i] * 2]); if (A[i] % 3 == 0 && mp[A[i] / 3] != 0) add_edge(i, mp[A[i] / 3]); } top_sort(); int v = 0; for (int i = 1; i <= N; ++i) if (dp[i] > dp[v]) v = i; cout << dp[v] << endl; do { st[++tp] = v; v = pre[v]; } while (v > 0); for (int i = tp; i > 0; --i) { printf("%lld", A[st[i]]); if (i == 1) putchar('\n'); else putchar(' '); } } int main() { init(); solve(); return 0; } ``` ### T6:Tweetuzki 爱取球 比赛不到一小时就被指出[原题](https://www.luogu.org/problemnew/show/P1291)了…… 这题灵感原本来自 Tweetuzki 班上的数学巨佬……他已经连续好几次在年段的数学考试中满分了…… 遇到这种题,如果大家没有头绪,可以先写个随机程序来帮助猜结论。这种暴力的程序是很重要的,它不仅可以帮助大家找到思路,还可能帮助大家验证结论的正确性。 ```cpp #include <bits/stdc++.h> using namespace std; const int MaxN = 1000; int N; bool used[MaxN]; double ans; int work() { int cnt = 0, res = 0; memset( used, 0, sizeof used ); while ( cnt < N ) { res++; int x = rand() % N; if ( !used[x] ) { used[x] = true; cnt++; } } return res; } int main() { cin >> N; srand( time(0) ); for ( int t = 1; t <= 1000000; ++t ) ans += work(); ans /= 1000000.0; cout << ans << endl; return 0; } ``` 写完随机程序后,就可以利用这个来猜结论了。 考虑动态规划。 设 $f(i)$ 表示 $N$ 个球中,取遍剩下 $i$ 个的期望次数。显然 $f(1) = N$。 考虑如何实现状态转移。对于 $f(i)$,则已经抽出过 $(N-i)$ 个球,还剩下 $i$ 个球没抽中。则抽出剩下的 $i$ 个球中任意一个的概率为 $\frac{i}{N}$。由于期望是反应随机变量平均取值的大小,我们可以把期望理解为,无限次数下发生该事件的平均次数;相对应地,概率也可以理解为无限次数下发生的次数。那么要发生这件事,显然期望次数是总数除以该事件发生的次数。因此对于某个事件 $A$,$E(A) = \frac{1}{P(A)}$。故在此题中,取出剩下 $i$ 个球中任意一个的期望次数为 $\frac{N}{i}$ 次。 因此 $f(i) = f(i-1) + \frac{N}{i}$。 再考虑化简这个式子,将每个 $f$ 值一层层代入,不难求得: $$f(N) = \frac{N}{1} + \frac{N}{2} + \frac{N}{3} + \cdots + \frac{N}{N}$$ 计算后与随机程序结果差不多。几乎可以肯定这就是答案了。 要求这个分数对 $20040313$ 取模的结果,我们需要预处理出 $1, 2, 3, \cdots, N$ 在模 $20040313$ 意义下的逆元。由于 $N$ 只有 $10^7$,使用线性求逆元方法即可。 > 这里给出验题巨神的做法: > > 设 $f(i)$ 表示已经抽到 $i$ 个球,那么第一次抽,抽中新的的概率为 $\frac{n - i}{n}$,第二次抽,抽中的概率为 $\frac{n - i}{n} \times \frac{i}{n}$,……,第 $m$ 次抽,抽中的概率为 $\frac{n - i}{n} \times \left( \frac{i}{n} \right)^{(m - 1)}$。 > > 然后乘上次数以后相加,可以得到抽中一个新球的期望次数为 > > $$ \sum_{m = 1}^{\infty}\frac{n - i}{n} \times \left( \frac{i}{n} \right)^{(m - 1)} \times m $$ > > 将 $\frac{n - i}{n}$ 提出来,得 > > $$ \frac{n - i}{n} \times \left( \sum_{m = 1}^{\infty} m \times \left( \frac{i}{n} \right)^{(m - 1)} \right) $$ > > 我们设 $x = \frac{i}{n}$,则原式可以简化为 > > $$(1 - x) \times \left( \sum_{m = 1}^{\infty} m \times x^{m - 1}\right)$$ > > 再设 $S = \sum_{m = 1}^{\infty} m \times x^{m - 1}$,则 $x \times S = \sum_{m = 1}^{\infty} m \times x^m$。 > > 则 > > $$(1 - x) \times S = S - x \times S = \sum_{m = 1}^{\infty} x^{m - 1}$$ > > 那么 > > $$x(1 - x) \times S = \sum_{m = 1}^{\infty} x^{m}$$ > > 用 $(1 - x) \times S$ 减去上式。可得 > > $$(1-x)^2 \times S=1$$ > > 即 > > $$S=\frac{1}{(1-x)^2}$$ > > 代入原式,答案就变成了 $\frac{(1 - x)}{(1-x)^2}$,也就是 $\frac{1}{(1-x)}$。 > > 再将 $x = \frac{i}{n}$ 代回,就得出了 $f(i + 1) = f(i) + \frac{n}{n - i}$ 的结论。同样线性求逆元即可。 代码: ```cpp #include <iostream> #include <cstdio> #include <cstring> using namespace std; typedef long long llt; const int MaxN = 10000000 + 5; const llt Mod = 20040313; int N; llt inv[MaxN]; int main() { cin >> N; inv[1] = 1; for (int i = 2; i <= N; ++i) inv[i] = (-(Mod / i) * inv[Mod % i]) % Mod; llt ans = 0; for (int i = 1; i <= N; ++i) ans = (ans + N * inv[i]) % Mod; ans = (ans % Mod + Mod) % Mod; cout << ans << endl; return 0; } ```