2024犇犇第二次比赛题解

· · 题解

2024年犇犇内部赛Second 题解

PS:可以在团队题单中改题!

1.合成卡片

思路:

题目给定了概率,也就是说,对于 x 张 i 级卡片,他可以制造出 ( 概率 \times x ) 张 (i + 1) 级卡片。

时间复杂度 O(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

思路

看到如此大的数据范围,最先想到的就是 O(1) 数学,脚推样例,最后决定用分类讨论。

由于 k \le 2,所以将 k 分类讨论。

分类:

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)

思路

依次将元素排成 b_1, b_2, \dots, b_n。对于 b_i,找到他在 a 中的出现位置,然后用类似于插入排序的方式插入。有 n 个元素,每次寻找位置和插入 O(n),总时间 O(n^2),预计 68 分。

如果将 a 中的每个元素变成它在 b 中的位置得到排列 c,那么任务就是要对 c 由小到大排序。此时也可以看出,题目中的移动操作就是插入排序的插入操作。有哪些需要左移?对于⼀个元素 a_i,如果 a_1, a_2, \dots, a_i - 1 中存在⼀个元素大于 a_i,那么需要对 a_i 进⾏左移。前缀最大值维护即可。时间复杂度 O(n)。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

题意

有 n 首歌,第 i 首歌时间为 t_i,从时间 0 开始,每次随机播放一首歌,可以重复,问在第 x + 0.5 时刻听到第一首歌的概率是多少?

思路

首先,观察数据大小,发现时间 t_i \le 10^4,所以考虑用时间作为拓扑序。

考虑动态规划。设 dp[i] 表示在 i 时刻结束的歌曲的概率,那么答案就是 (\sum\limits_{i = x - t_1 + 1}^{x}dp_i) \times \dfrac{1}{n}。其中 \dfrac{1}{n} 表示 n 的逆元。

解释:

然后想一想状态转移方程,因为每种歌曲都可以无限播放,有点儿像完全背包,即:

#### 解释: $i$ 为我们枚举的时间轴,$j$ 为当前枚举的第 $j$ 首歌,意思是当前这个时间 $i$ 加上这首歌的时间 $t_j$ 后结束歌曲的概率,相当于在这个时间 $i$ 结尾的概率乘上选择第 $j$ 首歌的概率加上原先的概率,即上述方程。 时间复杂度为:$O(n * x)

代码

#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;
}