P13862 [SWERC 2020] Mentors
Nostopathy · · 题解
这么水的蓝没人做?
::::info[题意]{open}
有
::::info[题解]{open} 题目给出可以顺序处理,也就是每个人在之前的点中选学生的问题,且之前没有老师的学生可以被后面的人选择。考虑 DP。
设
那么对于
对于
三项分别为:
- 等待后面人选他,没有老师的人增加一个;
- 在
1 基础上选一个学生,有j 种选择; - 在
1 基础上选两个学生,有\frac{j(j+1)}{2} 种选择。
答案为
::::success[参考程序] 超短。
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2026;
int n, m, r, dp[N][N];
signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> r >> n >> m;
dp[1][1] = 1;
for(int i = 2; i <= n; ++ i){
if(i == r){
for(int j = 1; j <= i; ++ j)
dp[i][j] = dp[i - 1][j - 1];
}
else{
for(int j = 1; j <= i; ++ j)
dp[i][j] = ((dp[i - 1][j - 1] + dp[i - 1][j] * j % m) % m + dp[i - 1][j + 1] * ((j * (j + 1) / 2) % m) % m) % m;
}
}
cout << dp[n][1];
return 0;
}
::::