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
思路
看到如此大的数据范围,最先想到的就是 数学,脚推样例,最后决定用分类讨论。
由于 ,所以将 分类讨论。
分类:
-
当 时,所有格子都未上色,那么每个格子能涂的颜色数量如下图:
所以,运用乘法原理,我们得到:。
-
当 时,只有一个格子上色,那么每个格子能涂的颜色数量如下图:
xxx(已染色) 所以,我们得到:。
-
当 时,要讨论两个格子相距的距离:
-
位置差 (相邻)
-
颜色相同(不成立,非法),。
-
颜色不同,那么每个格子能涂的颜色数量如下图:
xxx(已染色) yyy(已染色) 所以,我们得到:。
-
-
位置差 (相距两格)
-
共有两种颜色,已填格子颜色不同(不成立,非法),。
-
已填格子颜色相同,那么每个格子能涂的颜色数量如下图:
xxx(已染色) xxx(已染色) 所以,我们得到:。
-
已填格子颜色不同,那么每个格子能涂的颜色数量如下图:
xxx(已染色) yyy(已染色) 所以,我们得到:。
-
-
位置差
-
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)
思路
依次将元素排成 。对于 ,找到他在 中的出现位置,然后用类似于插入排序的方式插入。有 个元素,每次寻找位置和插入 ,总时间 ,预计 分。
如果将 中的每个元素变成它在 中的位置得到排列 ,那么任务就是要对 由小到大排序。此时也可以看出,题目中的移动操作就是插入排序的插入操作。有哪些需要左移?对于⼀个元素 ,如果 中存在⼀个元素大于 ,那么需要对 进⾏左移。前缀最大值维护即可。时间复杂度 。100分。
代码
#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
题意
有 首歌,第 首歌时间为 ,从时间 开始,每次随机播放一首歌,可以重复,问在第 时刻听到第一首歌的概率是多少?
思路
首先,观察数据大小,发现时间 ,所以考虑用时间作为拓扑序。
考虑动态规划。设 表示在 时刻结束的歌曲的概率,那么答案就是 。其中 表示 的逆元。
解释:
-
只要是在 时刻播放完的歌曲,后面接上第 首歌,那么在第 时刻,听到的一定是第 首歌。
-
就是指 时刻播放完的总概率。
-
是接上第 首歌的概率。因为要取模,所以用的逆元。
然后想一想状态转移方程,因为每种歌曲都可以无限播放,有点儿像完全背包,即:
,其中 指的是 的逆元。
解释:
为我们枚举的时间轴, 为当前枚举的第 首歌,意思是当前这个时间 加上这首歌的时间 后结束歌曲的概率,相当于在这个时间 结尾的概率乘上选择第 首歌的概率加上原先的概率,即上述方程。
时间复杂度为:
代码
#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;
}