FZU 2282 Wand (组合数学 错排应用)
来源:互联网 发布:梅林 访客网络设置 编辑:程序博客网 时间:2024/06/05 15:31
Accept: 83 Submit: 305
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
第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院)#include <cstdio>#include <cstring>#include <algorithm>#define ll long longusing namespace std;int const MOD = 1e9 + 7;int n, K;ll dp[10005], fac[10005];ll qpow(ll a, ll x) { ll res = 1; while(x) { if (x & 1) { res = (res * a) % MOD; } a = (a * a) % MOD; x >>= 1; } return res; } ll C(ll n, ll k) { if(n < k) { return 0; } if(k > n - k) { k = n - k; } ll a = 1, b = 1; for(int i = 0; i < k; i++) { a = a * (n - i) % MOD; b = b * (i + 1) % MOD; } return a * qpow(b, MOD - 2) % MOD; } void pre() {dp[0] = 1;dp[1] = 0;dp[2] = 1;fac[1] = 1;fac[2] = 2;for (int i = 3; i <= 10000; i ++) {dp[i] = (((i - 1) % MOD) * ((dp[i - 2] + dp[i - 1])) % MOD) % MOD;fac[i] = ((fac[i - 1] % MOD) * (i % MOD)) % MOD;}} int main() {pre();int T;scanf("%d", &T);while (T --) {scanf("%d %d", &n, &K);ll ans = 0;for (int i = 0; i < K; i ++) {ans = ((ans % MOD) + (C(n, i) * dp[n - i]) % MOD) % MOD;}printf("%I64d\n", (MOD + (fac[n] % MOD) - (ans % MOD)) % MOD);}}
- FZU 2282 Wand (组合数学 错排应用)
- FZU 2282 Wand 【组合数学】
- FZU 2282 Wand(组合)
- FZU 2282 Wand(错排+费马小定理)
- FZU 2282 Wand(错排+费马小定理)
- FZU -- 2282 和 DHU -- 2049 【简单错排 + 组合数学】
- FZU 2282Problem 2282 Wand(错排+快速幂+费马小定理)
- FZU 2282 Wand,利用扩展欧几里得求逆元快速求C(n,m) , 错排公式推导
- fzu 2282 Wand
- FZU2282Wand (组合数学+错排应用)
- fzu-Wand
- fzu2282 wand 排列组合 错排
- FZU 2282 错排
- 组合数学--错排问题
- 第八届福建省大学生程序设计竞赛 K.Wand【错排+组合数】
- FZU 1876 组合数学
- 错排问题(组合数学)
- NYOJ 451 组合数学 错排
- 连接mysql数据库时Establishing SSL connection without server's identity verification is not recommended.
- 登录Hive
- mac升级python到3.6.2
- spring学习之aop关于xml文件配置和注解方式配置
- 深度学习算法评估指标
- FZU 2282 Wand (组合数学 错排应用)
- Effictive Go(五)函数
- Android中的像素单位
- jdbc简单使用
- Cryptographic Chaining Mode
- 太棒了,我终于也有排名了!
- Java学习笔记(五)
- 五种分页显示方式
- <asp:DropDownList>中设置默认选择值