2021牛客寒假算法基础集训营6部分题解
C 末三位
显然 5 5 5 是一个很特殊的数字,可以打表找规律,或者有时间推出规律都可。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 1e6;
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int n;
while(~scanf("%d", &n)) {
if (n == 0) cout << "001" << endl;
else if (n == 1) cout << "005" << endl;
else if (n == 2) cout << "025" << endl;
else if (n & 1) cout << "125" << endl;
else cout << "625" << endl;
}
}
I 贪吃蛇
经典 B F S BFS BFS 找最短路
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 1e6;
int dx[5] = {0, 0, 1, -1};
int dy[5] = {1, -1, 0, 0};
struct k {
int x;
int y;
int dis;
};
int n, m, sx, sy, ex, ey;
char g[103][103];
int vis[103][103];
queue<k> q;
int bfs() {
q.push({sx, sy, 0});
while(!q.empty()) {
k temp = q.front();
q.pop();
vis[temp.x][temp.y] = 1;
if (temp.x == ex && temp.y == ey) return temp.dis;
for (int i = 0; i < 4; ++i) {
int x = temp.x + dx[i];
int y = temp.y + dy[i];
if (x >= 0 && x < n && y >= 0 && y < m && !vis[x][y] && g[x][y] != '#') q.push({x, y, temp.dis + 100});
}
}
return -1;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
scanf("%d%d", &n, &m);
scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
sx--, sy--, ex--, ey--;
for (int i = 0; i < n; ++i) {
scanf("%s", g[i]);
}
printf("%d", bfs());
}
D 划数
这题的坑点在于只有两个数的时候,需要特判。那么对于其它情况而言,我们需要注意到题目中一个很重要的条件 c n t ≥ 11 cnt ≥ 11 cnt≥11 ,那么这个条件直接说明了 c n t cnt cnt 一定不是一个融合数,它一定是本身数组里面的一个数字,那么我们要求的 x x x 就一定是一个融合数,所以,我们求这个 x x x 只需要将所有数的和去减掉 c n t cnt cnt ,再去 % 11 \%11 %11 就是答案了,这一步不懂的话去学习一下求余的一些公式就懂啦。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 1e6;
int n;
ll cnt;
ll a[N];
ll sum;
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
while(~scanf("%d%lld", &n, &cnt)) {
sum = 0ll;
for (int i = 0; i < n; ++i) {
scanf("%lld", &a[i]);
sum +=a[i];
}
if (n == 2) {
if (a[0] == cnt) printf("%lld\n", a[1]);
else printf("%lld\n", a[0]);
continue;
}
printf("%lld\n", (sum - cnt) % 11l);
}
}
A 回文括号序列计数
一言难尽题。。。这题实在是太容易看错回文串这个概念了,说个例子就一目了然: ( ( ) ) (()) (()) 并不是回文串, ( ( ) ) ( ( (())(( (())(( 这种才能叫回文!也就是得 a 1 = a n , a 2 = a n − 1 . . . . . . a_1 = a_n,a_2 = a_{n-1}...... a1=an,a2=an−1...... 这种的,不能下意识的认为翻转 180 180 180 度。懂了这个这题就好解决。我们首先可以发现根据定义,一个括号串一定是一个括号匹配串,为什么呢。我们可以发现,若括号序列定义中的 P P P, Q Q Q是括号匹配串,则组合而成的该串也一定是括号匹配串。那么我们可以得知假如对于长度为 n n n 的括号串而言,如果长度小于 n n n 的括号串都是匹配串的话,则长度为 n n n 的括号串一定是匹配串。那么说白了,只要最小的括号串是匹配串,就所有串都是匹配串,那么定义里都说明了空串是匹配串,所以所有括号串都是括号匹配串。那么不为空的括号匹配串一定有 a 1 = ′ ( ′ , a n = ′ ) ′ a_1='(',a_n=')' a1=′(′,an=′)′ ,不然肯定不匹配了,也就是说不为空的括号串 a 1 永 不 等 于 a n a_1永不等于a_n a1永不等于an 。也就是说除了空串,任何长度的括号串都不是回文串。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
typedef long long ll;
const int MAXN = 2;
const ll mod = 998244353;
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int T;
scanf("%d", &T);
while(T--) {
int n;
scanf("%d", &n);
if (n == 0) printf("1\n");
else printf("0\n");
}
}
J 天空之城
我们可以想,对于能达到最小值的走法而言,我们把只要是他走过的路,就留下来,我们可以发现这一定是一个联通且无环的图,因为有环的话,他一定可以通过少走环的一条边的方式走完环上所有点。那么到这里对最小生成树很熟悉的同学就肯定都知道怎么办了。不知道也不要紧,我们继续,那么一个联通而无环的图肯定是一颗树了,那么对于这颗树而言,答案的值跟这颗树的边有什么关系呢,答案其实就等于所有树边的和,因为我们可以就用 d f s dfs dfs 的方式去走这颗树,根据题意回溯的时候都是我们走过的边我们可以不算值,只有走没走过的边的时候需要将边加到答案里面去,所以最后答案就等于树的所有边的和。那么我们让这颗树的边和最小就行了。归结一下,就是我们要从所有边里面选一些边使得其构成一颗树且边和最小,那这不就是最小生成树了嘛。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
const int MAXN = 2;
const ll mod = 998244353;
int n, m;
struct Edge {
int u;
int v;
int val;
}edge[200005];
string x, y;
int cnt;
int fa[6000];
int g[5050][5050];
int vis[5050];
ll ans;
bool cmp(Edge a, Edge b) {
return a.val < b.val;
}
void dfs(int u) {
vis[u] = 1;
for (int i = 1; i <= n; ++i) {
if (!vis[i] && g[u][i]) dfs(i);
}
}
int find_fa(int x) {
return x == fa[x] ? x : fa[x] = find_fa(fa[x]);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int val;
while(scanf("%d%d", &n, &m) != EOF) {
cnt = 0;
map<string, int> mp;
ans = 0ll;
memset(vis, 0, sizeof vis);
memset(g, 0, sizeof g);
cin >> x;
for (int i = 1; i <= n; ++i) fa[i] = i;
mp[x] = ++cnt;
for (int i = 0; i < m; ++i) {
cin >> x >> y >> val;
int u, v;
if (!mp[x]) mp[x] = ++cnt;
if (!mp[y]) mp[y] = ++cnt;
u = mp[x], v = mp[y];
//cout << u << ' ' << v << endl;
if (u != v) g[u][v] = g[v][u] = 1;
edge[i].u = u, edge[i].v = v, edge[i].val = val;
}
dfs(1);
int flag = 1;
for (int i = 1; i <= n; ++i) if (!vis[i]) {flag = 0; break;}
if (!flag) {printf("No!\n"); continue;}
sort(edge, edge + m, cmp);
for (int i = 0; i < m; ++i) {
if (edge[i].u == edge[i].v) continue;
int fau = find_fa(edge[i].u), fav = find_fa(edge[i].v);
if (fau == fav) continue;
fa[fau] = fav;
ans += 1ll * edge[i].val;
}
printf("%lld\n", ans);
mp.clear();
}
}
F 组合数问题
神奇的题目(不会写的题目),看讨论区很多是OEIS,但是也看到了一位大佬用了二项式定理和虚数解决了这题,真的强啊。
①:
(
1
+
i
)
n
=
∑
x
=
0
n
C
n
x
⋅
(
i
)
x
{(1+i)}^n=\sum_{x=0}^n C_n^x·(i)^x
(1+i)n=∑x=0nCnx⋅(i)x
②:
(
1
−
i
)
n
=
∑
x
=
0
n
C
n
x
⋅
(
−
1
)
x
⋅
(
i
)
x
{(1-i)}^n=\sum_{x=0}^n C_n^x·(-1)^x·(i)^x
(1−i)n=∑x=0nCnx⋅(−1)x⋅(i)x
那么显然对于偶数项会相等,奇数项呈相反数,所以 ( ① + ② ) / 2 (①+②)/2 (①+②)/2 的结果就是只剩下偶数项了,那么相邻偶数项的常系数肯定是呈相反数的,因为后一个偶数项肯定比前一个多乘一个 i 2 i^2 i2 ,也就是:
③: C n 0 − C n 2 + C n 4 − C n 6 . . . . . . C_n^0 - C_n^2 + C_n^4 -C_n^6...... Cn0−Cn2+Cn4−Cn6......
那么③式具体应该等于多少呢,因为 n n n 是 4 4 4 的倍数,我们不妨算一下 ( 1 − i ) 4 (1 - i)^4 (1−i)4 和 ( 1 + i ) 4 (1 + i)^4 (1+i)4 ,发现都是 − 4 -4 −4,也就是③式等于 2 ⋅ ( − 4 ) n / 4 / 2 = ( − 4 ) n / 4 2·(-4)^{n/4} / 2 = (-4)^{n/4} 2⋅(−4)n/4/2=(−4)n/4 . 那么我们得想办法把其中的负系数的消掉就是我们的答案了,那利用题目中的提醒,我们利用:
④: C n 0 + C n 2 + C n 4 + C n 6 . . . . . . + C n n = 2 n − 1 C_n^0 + C_n^2 + C_n^4 +C_n^6......+C_n^n=2^{n-1} Cn0+Cn2+Cn4+Cn6......+Cnn=2n−1
那么 ( ③ + ④ ) / 2 (③+④)/2 (③+④)/2 就是最终答案了
那么式子化简可得:
(
−
4
)
n
/
4
+
2
n
−
1
2
\frac{(-4)^{n/4}+2{n-1}}{2}
2(−4)n/4+2n−1
注意负数求余的处理和
2
2
2 的逆元处理
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
typedef long long ll;
const ll mod = 998244353;
ll quickpow(ll base, ll n) {
ll ans = 1ll;
while(n) {
if (n & 1ll) ans = ans * base % mod;
base = base * base % mod;
n >>= 1ll;
}
return ans;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ll n, inv_2 = quickpow(2, mod - 2);
scanf("%lld", &n);
ll temp = quickpow(4, n / 4ll);
if ((n / 4ll) & 1) temp = -temp;
ll ans = (temp + quickpow(2, n - 1ll) + mod) % mod;
ans = ans * inv_2 % mod;
printf("%lld", ans);
}
G 机器人
嗯。。。这题赛后看讨论区的贪心还是看不懂证明,后面看了我队友用状压DP解的博客 ->题解链接在这里
下面是理解完后自己码的:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int n, x;
int a[30], b[30];
__int128 dp[(1 << 20) + 10];
inline void print(__int128 x) {
if (!x) {
printf("0");
return ;
}
string temp = "";
while(x) {
temp += x % 10 + 48;
x /= 10;
}
reverse(temp.begin(), temp.end());
cout << temp << endl;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
scanf("%d%d", &n, &x);
for (int i = 0; i < n; ++i) scanf("%d%d", &a[i], &b[i]);
dp[0] = x;
for (int i = 1; i < (1 << n); ++i) {
for (int j = 0; j < n; ++j) {
if ((i >> j) & 1) {
dp[i] = max(dp[i], a[j] * dp[i - (1 << j)] + b[j]);
}
}
}
print(dp[(1 << n) - 1]);
}