FZU 2282 Wand(错排+费马小定理)

 Problem 2282 Wand

Accept: 68    Submit: 250
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

 Source

第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院)

题意:
给出n个人,每个人有自己的膜杖,求出只要有k个人拿到自己的膜杖的数量数。

POINT:
错排+组合数,组合数没有很大,只要用费马小定理就行了。

如果当时看过错排就可以AC了呀!!哎!

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int N = 10000+2;
#define  LL long long
const LL p = 1e9+7;
LL cp[N];
LL k[N];
void init()
{
    cp[1]=0;
    cp[2]=1;
    for(int i=3;i<=10000;i++)
    {
        (cp[i]=(i-1)*(cp[i-1]+cp[i-2]))%=p;
    }
    k[0]=1;
    for(int i=1;i<=10000;i++)
    {
        (k[i]=k[i-1]*i)%=p;
    }
}
LL qkm(LL base,LL mi)
{
    LL ans=1;
    while(mi)
    {
        if(mi&1)
            (ans*=base)%=p;
        (base*=base)%=p;
        mi>>=1;
    }
    return ans;
}
LL C(int i,int n)
{
    return ((k[n]*qkm((k[n-i]*k[i])%p,p-2))%p);//取模,两次!很关键k[n-i]*k[i]也会爆LL
}
int main()
{
    int n,k;
    int T;
    init();
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d",&n,&k);
        LL ans=1;
        for(int i=2;i<=n-k;i++)
        {
            (ans+=cp[i]*C(i,n))%=p;
        }
        printf("%lld\n",ans);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值