您的位置:首页 > 其它

fzu 2282 Wand

2017-07-23 13:18 477 查看
Problem 2282 Wand

Accept: 8    Submit: 24

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

21 13 1



Sample Output

14 

n,k ans=∑(cni+dp[n-i])n>=i>=k;

dp[i]表示i个物品错排的情况数;

错排的状态转移:dp[i]=(i-1)*(dp[i-1]+dp[i-2]);

#include<iostream>

#include<cstring>

#include<cstdio>

#include<algorithm>

using namespace std;

const int MOD=1000000007;

  const int N=10007;

long long int qmod(long long int a,long long int b) {  

    long long int res = 1ll;  

    while(b) {  

        if(b&1) res=res*a%MOD;  

        b>>=1,a=a*a%MOD;  

    }  

    return res;  

}  

long long int dp
;  

  

long long int fac
,inv
;  

void init() {  

    fac[0]=1;  

    for(long long int i=1; i<N; i++) fac[i]=(fac[i-1]*i)%MOD;  

    inv[N-1] = qmod(fac[N-1],MOD-2);  

    for(long long int i=N-2; i>=0; i--) inv[i]=(inv[i+1]*(i+1))%MOD;  

}  

  

long long int C(int n,int m) {  

    return fac
*inv[m]%MOD*inv[n-m]%MOD;  

}  

int main() {
int T;
long long int dp[11111];
int i;
init();
dp[0]=1;
dp[1]=0;
for(i=2;i<=10000;i++)
{
dp[i]=(i-1)*(dp[i-1]+dp[i-2]);
dp[i]=dp[i]%1000000007;
}cin>>T;
while(T--)
{
int a,b;
cin>>a>>b;
    long long int ans=0;
    for(i=b;i<=a;i++)
    {
    ans+=dp[a-i]*C(a,i)%10
4000
00000007;
    ans=ans%1000000007;
}
cout<<ans<<endl;
}
return 0;

}

代码参考自http://blog.csdn.net/mengxiang000000/article/details/75799209
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: