1.题目描述:点击打开链接
2.解题思路:本题通过寻找递推关系解决。设最终答案是f(n)。假设第一名有i个人,有C(n,i)种可能,接下来有f(n-i)种可能性。最终答案是∑C(n,i)f(n-i)(i从1开始)。
3.代码:
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<algorithm> #include<string> #include<sstream> #include<set> #include<vector> #include<stack> #include<map> #include<queue> #include<deque> #include<cstdlib> #include<cstdio> #include<cstring> #include<cmath> #include<ctime> #include<functional> using namespace std; #define N 1000+10 #define MOD 10056 int C[N][N]; int f[N]; int n; void init() { C[0][0] = f[0] = 1; for (int i = 1; i <= 1000; i++) { C[i][0] = C[i][i]=1; f[i] = 0; for (int j = 1; j < i; j++) C[i][j] = (C[i - 1][j - 1] + C[i - 1][j])%MOD; for (int j = 1; j <= i; j++) f[i] = (f[i] + C[i][j] * f[i - j]%MOD) % MOD; } } int main() { freopen("test.txt", "r", stdin); int cnt = 0; init(); int t; cin >> t; while (t--) { cin >> n; printf("Case %d: ", ++cnt); cout << f[n] << endl; } return 0; }
时间: 2024-10-15 15:09:02