Accept: 20 Submit: 54
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
第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院)输入一个序列长度N(1-n) 问他的全排列中至少有K个位置是正确数字的排列的数目
还记得错排吗 至少K个 那就先拿出K个(C n k)种剩下的错排就 OK 加上 K+1 ..........
中间会用到一个乘法的逆
#include<stdio.h>
using namespace std;
#define mod 1000000007
#define ll long long
long long aa[10100];
long long f[10100];
long long quickpowmod(long long x, long long y)
{
long long ret = 1;
while(y)
{
if(y&1)
ret = ret*x%mod;
x = x*x%mod;
y >>= 1;
}
return ret;
}
int main()
{
int t;
f[0] = 1;
for(int i = 1; i <= 10010; ++i)
{
f[i] = f[i-1]*i%mod; //阶乘
}
aa[1]=0,aa[2]=1;aa[0]=1;
for(int i=3; i<=10010; i++)
{
aa[i]=((ll)(i-1)*(aa[i-1]+aa[i-2]))%mod;//错排
}
scanf("%d",&t);
int n,k;
// printf("---%d---\n",aa[1]);
while(t--)
{
scanf("%d %d",&n,&k);
long long ans=0;
long long xf;
for(int i=k; i<=n; i++)
{
xf=f[n]*quickpowmod(f[i], mod-2)%mod*quickpowmod(f[n-i], mod-2)%mod;
xf=xf*aa[n-i]%mod;
ans=(ans+xf)%mod;
}
printf("%I64d\n",ans);
}
return 0;
}