2024犇犇第二次比赛题解
2024年犇犇内部赛Second 题解
PS:可以在团队题单中改题!
1.合成卡片
思路:
题目给定了概率,也就是说,对于
时间复杂度
代码:
#include<bits/stdc++.h>
using namespace std;
double a, b, c, d, e, f, g, h;
int main(){
cin >> a >> b >> c >> d >> e >> f >> g >> h;
cout << h + floor((g + (f + (e + (d + (c + (b + (a / 10 * 6)) / 10 * 4) / 10 * 2) / 10) / 20) / 500) / 1000);
return 0;
}
2.lattice
思路
看到如此大的数据范围,最先想到的就是
由于
分类:
-
当
k = 0 时,所有格子都未上色,那么每个格子能涂的颜色数量如下图:m m - 1 m - 1 \dots m - 1 所以,运用乘法原理,我们得到:
ans = m \times (m - 1) ^ {n - 1} 。 -
当
k = 1 时,只有一个格子上色,那么每个格子能涂的颜色数量如下图: |m - 1 |m - 1 | xxx(已染色) |\dots |m - 1 | | -----------: | -----------: | -----------: | -----------: | -----------: |所以,我们得到:
ans = (m - 1) ^ {n - 1} 。 -
当
k = 2 时,要讨论两个格子相距的距离:-
位置差
1 (相邻)-
颜色相同(不成立,非法),
ans = 0 。 -
颜色不同,那么每个格子能涂的颜色数量如下图:
m - 1 m - 1 xxx(已染色) yyy(已染色) \dots m - 1 所以,我们得到:
ans = (m - 1) ^ {n - 2} 。
-
-
位置差
2 (相距两格)-
共有两种颜色,已填格子颜色不同(不成立,非法),
ans = 0 。 -
已填格子颜色相同,那么每个格子能涂的颜色数量如下图:
m - 1 xxx(已染色) m - 1 xxx(已染色) \dots m - 1 所以,我们得到:
ans = (m - 1) ^ {n - 2} 。 -
已填格子颜色不同,那么每个格子能涂的颜色数量如下图:
m - 1 xxx(已染色) m - 2 yyy(已染色) \dots m - 1 所以,我们得到:
ans = (m - 2) \times (m - 1) ^ {n - 3} 。
-
-
位置差
3 -
已填格子颜色相同,那么每个格子能涂的颜色数量如下图:
m - 1 xxx(已染色) m - 1 m - 2 xxx(已染色) \dots m - 1 所以,我们得到:
ans = (m - 2) \times (m - 1) ^ {n - 3} 。 -
已填格子颜色不同,那么每个格子能涂的颜色数量的分析如下:
所以,我们得到:
ans = [(m - 1) \times (m - 1) - (m - 2)] \times (m - 1) ^ {n - 4} 。
-
-
PS:记得要写快速幂!
代码
#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
long long t, n, m, k, x, y, a, b;
long long qpow(long long a, long long b){
long long ans = 1;
while(b){
if(b % 2 == 1){
ans = (ans * a) % mod;
b--;
}
a = (a * a) % mod;
b /= 2;
}
return ans;
}
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
//freopen("lattice.in", "r", stdin);
//freopen("lattice.out", "w", stdout);
cin >> t;
while(t--){
cin >> n >> m >> k;
if(k == 0){
cout << 1ll * m * qpow(m - 1, n - 1) % mod << '\n';
}
else if(k == 1){
cin >> x >> y;
cout << qpow(m - 1, n - 1) << '\n';
}
else{
cin >> x >> y >> a >> b;
if(abs(x - a) == 1){
if(b == y){
cout << "0\n";
continue;
}
cout << qpow(m - 1, n - 2) << '\n';
}
else if(abs(x - a) == 2){
if(m == 2 && y != b){
cout << "0\n";
continue;
}
if(y == b){
cout << qpow(m - 1, n - 2) << '\n';
}
else{
cout << (m - 2) * qpow(m - 1, n - 3) % mod << '\n';
}
}
else if(abs(x - a) == 3){
if(y == b){
cout << (1ll * (m - 2) * (m - 1) % mod) * qpow(m - 1, n - 4) % mod << '\n';
}
else{
cout << (1ll * (m - 1) * (m - 1) - (m - 2)) % mod * qpow(m - 1, n - 4) % mod << '\n';
}
}
}
}
return 0;
}
3.专业摄影(photo)
思路
依次将元素排成
如果将
代码
#include <iostream>
using namespace std;
const int kMaxN = 1e5 + 1;
int a[kMaxN], p[kMaxN], n, x, ans;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> x;
p[x] = i;
}
for (int i = 1; i <= n; i++) {
cin >> x;
a[p[x]] = i;
}
for (int i = 1, j = 0; i <= n; i++) {//双指针
if (a[i] > a[j]) {
j = i, ans++; //统计不需要移动的数
}
}
cout << n - ans;
return 0;
}
4.mp4
题意
有
思路
首先,观察数据大小,发现时间
考虑动态规划。设
解释:
-
只要是在
(x - t_1 + 1) \sim x 时刻播放完的歌曲,后面接上第1 首歌,那么在第(x + 0.5) 时刻,听到的一定是第1 首歌。 -
-
-----------------------------------
然后想一想状态转移方程,因为每种歌曲都可以无限播放,有点儿像完全背包,即:
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 1, maxv = 1e5 + 1;
const long long mod = 998244353;
int n, x, ans, dp[maxv], t[maxn];
long long m;
long long qpow(long long a, long long b){ //快速幂
long long ans = 1;
while(b){
if(b & 1){ //相当于 b % 2 == 1
ans *= a;
ans %= mod;
b--;
}
a *= a;
a %= mod;
b /= 2;
}
return ans;
}
int main(){
cin >> n >> x;
ans = 0;
m = qpow(n, mod - 2); //求 n 的逆元
for(int i = 1; i <= n; i++){
cin >> t[i];
}
fill(&dp[0], &dp[n] + 1, 0); //考场代码,过于丑陋,可以删去
dp[0] = 1;
for(int i = 0; i <= x; i++){
for(int j = 1; j <= n; j++){
if(i + t[j] <= x){
dp[i + t[j]] = (dp[i + t[j]] + dp[i] * m) % mod;
}
}
if(i + t[1] > x){ //在(x - t[1] + 1) ~ x 之间
ans = (ans + dp[i] * m) % mod; //结尾多乘一个 n 的逆元,表示后接第 1 首歌的概率
}
}
cout << ans;
return 0;
}