foj Problem 2282 Wand
Posted zefengyao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了foj Problem 2282 Wand相关的知识,希望对你有一定的参考价值。
Accept: 432 Submit: 1537
Time Limit: 1000 mSec Memory Limit :
262144 KB
Problem Description
N wizards are attending a meeting. Everyone has his own magic wand. N magic wands was put in a line, numbered from 1 to n(Wand_i owned by wizard_i). After the meeting, n wizards will take a wand one by one in the order of 1 to n. A boring wizard decided to reorder the wands. He is wondering how many ways to reorder the wands so that at least k wizards can get his own wand.
For example, n=3. Initially, the wands are w1 w2 w3. After reordering, the wands become w2 w1 w3. So, wizard 1 will take w2, wizard 2 will take w1, wizard 3 will take w3, only wizard 3 get his own wand.
Input
First line contains an integer T (1 ≤ T ≤ 10), represents there are T test cases.
For each test case: Two number n and k.
1<=n <=10000.1<=k<=100. k<=n.
Output
For each test case, output the answer mod 1000000007(10^9 + 7).
Sample Input
Sample Output
Source
第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院)#define _CRT_SECURE_NO_DEPRECATE #include <iostream> #include<vector> #include<algorithm> #include<cstring> #include<bitset> #include<set> #include<map> #include<cmath> #include<queue> using namespace std; #define N_MAX 10000+4 #define MOD 1000000007 #define INF 0x3f3f3f3f typedef long long ll; int n, k; ll dp[N_MAX];//错排数 ll C[N_MAX][100 + 2]; void init() { dp[0] = 1; dp[1] = 0; dp[2] = 1; for (int i = 3; i < N_MAX; i++) { dp[i] = ((i - 1)*(dp[i - 1] + dp[i - 2]) + MOD) % MOD; } } void C_table() { for (int i = 0; i < N_MAX; i++) { C[i][0] = 1; for (int j = 1; j <= min(100, i); j++) {//!!! C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD; } } } int main() { init(); C_table(); int t; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &k); ll num = 0, ans = 1; for (ll i = 1; i <= n; i++) { ans = ans*i%MOD; } for (int i = 0; i <= k - 1; i++) { num = (num + C[n][i] * dp[n - i]) % MOD; } printf("%lld ", (ans - num + MOD) % MOD); } return 0; }
以上是关于foj Problem 2282 Wand的主要内容,如果未能解决你的问题,请参考以下文章