CSU-ACM2017暑期训练3-递推与递归 H

来源:互联网 发布:蓝月羽毛升级数据 编辑:程序博客网 时间:2024/06/06 02:56
Disky and Sooma, two of the biggest mega minds of Bangladesh went to a far country. They ate, coded and wandered around, even in their holidays. They passed several months in this way. But everything has an end. A holy person, Munsiji came into their life. Munsiji took them to derby (horse racing). Munsiji enjoyed the race, but as usual Disky and Sooma did their as usual task instead of passing some romantic moments. They were thinking- in how many ways a race can finish! Who knows, maybe this is their romance! In a race there are n horses. You have to output the number of ways the race can finish. Note that, more than one horse may get the same position. For example, 2 horses can finish in 3 ways.
1. Both first
2. horse1 first and horse2 second
3. horse2 first and horse1 second
Input Input starts with an integer T (≤ 1000), denoting the number of test cases. Each case starts with a line containing an integer n (1 ≤ n ≤ 1000).
Output
For each case, print the case number and the number of ways the race can finish. The result can be very large, print the result modulo 10056.
Sample Input
3 1 2 3
Sample Output

Case 1: 1 Case 2: 3 Case 3: 13

直接给出代码吧,这个题本来想自己想出来的,后来还是看了题解,发现自己虽然思路正确了,但是还是不知道怎么写这个状态转移方程,
f[i][j]表示i匹马产生j个名次,那么第i匹马要么和前s匹马(s <= i - 1)一起占一个名次,要么单独占一个名次,那么就可以得到状态转移
方程,f[i][j] = (j * f[i - 1][j] + j * f[i - 1][j - 1]) % MOD,为什么要乘以j呢,这是因为,无论这匹马是组合占名次,还是单独占
名次,它选择占的名次总是有j种选择,因此要乘以j。
#include <cstdio>#include <cstring>#include <iostream>using namespace std;int f[1001][1001],g[1001];#define INF -10000000#define MOD 10056int main(){f[0][0] = 1;for(int i = 1;i <= 1000;i++){for(int j = 1;j <= i;j++){f[i][j] = (j * f[i - 1][j] + j * f[i - 1][j - 1]) % MOD;g[i] = (g[i] + f[i][j]) % MOD;}}int n;int T,t = 0;scanf("%d",&T);while(T--){t++;scanf("%d",&n);int sum = 0; printf("Case %d: %d\n",t,g[n]);}return 0;}


原创粉丝点击