2018福建省赛Wand (VJ)

K - Wand(VJ)

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.

题意:给定两个整数 n 和 k, 代表有 n 个人,每个人有一根相应的棍子,求 至少 k 个人得到相应棍子的方案数

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
2
1 1
3 1
Sample Output
1
4

#include<stdio.h>
using namespace std;
 
typedef long long LL;
const LL maxn = 1e4 + 100,mod = 1e9 + 7;
 
LL Jc[maxn];
LL a[maxn];
 
void calJc()    //求maxn以内的数的阶乘
{
    Jc[0] = Jc[1] = 1;
    for(LL i = 2; i < maxn; i++)
        Jc[i] = Jc[i - 1] * i % mod;
}
 
//费马小定理求逆元
LL pow(LL a, LL n, LL p)    //快速幂 a^n % p
{
    LL ans = 1;
    while(n)
    {
        if(n & 1)
            ans = ans * a % p;
        a = a * a % p;
        n >>= 1;
    }
    return ans;
}
 
LL niYuan(LL a, LL b)   //费马小定理求逆元
{
    return pow(a, b - 2, b);
}
 
LL C(LL a, LL b)    //计算C(a, b)
{
    return Jc[a] * niYuan(Jc[b], mod) % mod
           * niYuan(Jc[a - b], mod) % mod;
}
 
int main()
{
    int t; scanf("%d",&t);
    calJc();
    a[1] = 0; a[2] = 1;
    for(int i = 3;i < maxn;i ++){
        a[i] = ((i - 1) * (a[i-1] + a[i-2]) % mod) % mod;
    }
    while(t --){
        int n,k; scanf("%d%d",&n,&k);
        LL sum = 1;
        for(int i = k;i < n;i ++)
            sum = (sum % mod + ((C(n,i) % mod) * (a[n - i] % mod)) % mod) % mod;
        printf("%I64d\n",sum);
    }
    return 0;
}

方法二 :
DP
错排+组合数学。

设定Dp[i]表示错排的数量。

那么考虑dp转移方程,我们考虑最新加进来的这个数字i:

①如果他放在了位子k上,然后位子k上的这个数字放在了位子i上,那么对应取k个位子有(i-1)种方法,那么有:

Dp[I]=Dp[i-2]*(i-1);

②如果他放在了位子k上,然后位子k上的这个数字没有放置在位子i上,同时位子k上的数字变成了一个新的问题,那么有:

Dp[i]=Dp[i-1]*(i-1);

那么Ans=C(n,n-k)*Dp[n-k]即可;

#include<stdio.h>
#include<string.h>
using namespace std;
#define LL long long int
 
const int N = 1e5 + 7;
const int MOD = 1e9+7;
  
LL qmod(LL a,LL b) {
    LL res = 1LL;
    while(b) {
        if(b&1) res=res*a%MOD;
        b>>=1,a=a*a%MOD;
    }
    return res;
}
LL dp[N];
 
LL fac[N],inv[N];
void init() {
    fac[0]=1;
    for(LL i=1; i<N; i++) fac[i]=(fac[i-1]*i)%MOD;
    inv[N-1] = qmod(fac[N-1],MOD-2);
    for(LL i=N-2; i>=0; i--) inv[i]=(inv[i+1]*(i+1))%MOD;
}
 
LL C(int n,int m) {
    return fac[n]*inv[m]%MOD*inv[n-m]%MOD;
}
 
int main() {
    init();
    dp[0]=1;
    dp[1]=0;
    for(LL i=1; i<=10000; i++) {
        dp[i]=(i-1)*(dp[i-1]+dp[i-2]);
        dp[i]%=MOD;
    }
    int t;
    int n,kk;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%d",&n,&kk);
        LL output=0;
        for(int k=kk; k<=n; k++) {
            output+=C(n,k)*dp[n-k]%MOD;
            output%=MOD;
        }
        printf("%I64d\n",output%MOD);
 
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值