Problem 2282 Wand

Accept: 432    Submit: 1537
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

2
1 1
3 1

Sample Output

1
4

Source

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

 
题意:有n个法师n根魔法棒,每个法师都有属于自己的1根魔法棒,求至少有k个法师没拿到自己的魔法棒的情况数。
思路:首先题目中k远远小于n,所以可以考虑相反面,考虑只有1,2,...,k-1个法师没拿到自己的魔法棒的情况数,再用n!减去这些情况的情况数即可。
若挑出x个人,这x个人都没拿到自己的魔法棒的情况数记为D(x),即为数量为x个人的错排数,C(n,x)*D(x)就是x个人没拿到魔法棒的总情况数。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<bitset>
#include<set>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
#define N_MAX 10000+4
#define MOD 1000000007
#define INF 0x3f3f3f3f
typedef long long ll;
int n, k;
ll dp[N_MAX];//错排数
ll C[N_MAX][ + ];
void init() {
dp[] = ; dp[] = ; dp[] = ;
for (int i = ; i < N_MAX; i++) {
dp[i] = ((i - )*(dp[i - ] + dp[i - ]) + MOD) % MOD;
}
}
void C_table() {
for (int i = ; i < N_MAX; i++) {
C[i][] = ;
for (int j = ; j <= min(, i); j++) {//!!!j<=i并且数组中j不能超过100
C[i][j] = (C[i - ][j] + C[i - ][j - ]) % MOD;
}
}
} int main() {
init(); C_table();
int t; scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &k);
ll num = , ans = ;
for (ll i = ; i <= n; i++) {
ans = ans*i%MOD;
}
for (int i = ; i <= k - ; i++) {
num = (num + C[n][i] * dp[n - i]) % MOD;
}
printf("%lld\n", (ans - num + MOD) % MOD);
}
return ;
}

foj Problem 2282 Wand的更多相关文章

  1. FOJ ——Problem 1759 Super A^B mod C

     Problem 1759 Super A^B mod C Accept: 1368    Submit: 4639Time Limit: 1000 mSec    Memory Limit : 32 ...

  2. FOJ Problem 1016 无归之室

     Problem 1016 无归之室 Accept: 926    Submit: 7502Time Limit: 1000 mSec    Memory Limit : 32768 KB  Prob ...

  3. FOJ Problem 1015 土地划分

    Problem 1015 土地划分 Accept: 823    Submit: 1956Time Limit: 1000 mSec    Memory Limit : 32768 KB  Probl ...

  4. foj Problem 2107 Hua Rong Dao

    Problem 2107 Hua Rong Dao Accept: 503    Submit: 1054Time Limit: 1000 mSec    Memory Limit : 32768 K ...

  5. FOJ Problem 2273 Triangles

    Problem 2273 Triangles Accept: 201    Submit: 661Time Limit: 1000 mSec    Memory Limit : 262144 KB P ...

  6. foj Problem 2275 Game

    Problem D Game Accept: 145    Submit: 844Time Limit: 1000 mSec    Memory Limit : 262144 KB Problem D ...

  7. foj Problem 2283 Tic-Tac-Toe

                                                                                                    Prob ...

  8. FOJ Problem 2257 Saya的小熊饼干

                                                                                                        ...

  9. FOJ Problem 2261 浪里个浪

                                                                                                        ...

随机推荐

  1. python scrapy实战糗事百科保存到json文件里

    编写qsbk_spider.py爬虫文件 # -*- coding: utf-8 -*- import scrapy from qsbk.items import QsbkItem from scra ...

  2. CentOS下使用Mysql

    安装过程百度,然后cd /etc->vi my.cnf修改配置文件,在mysqld下添加lower_case_table_name=1和character_set_server=utf8,保存退 ...

  3. 笔记-docker-3 使用

    笔记-docker-3 使用 1.      镜像 image是docker最重要的概念,docker运行容器前需要本地存在对应的镜像,如果没有,会尝试从默认镜像库下载. 1.1.    镜像获取 查 ...

  4. 2753: [SCOI2012]滑雪与时间胶囊

    2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 2633  Solved: 910 Descriptio ...

  5. CentOS 7.X 设置系统时间

    在CentOS 6版本,时间设置有date.hwclock命令, 硬件时钟和系统时钟 (1) 硬件时钟 RTC(Real-Time Clock)或CMOS时钟,一般在主板上靠电池供电,服务器断电后也会 ...

  6. MySQL Group Replication数据安全性保障

    本文来自数据库内核专栏 在之前的文章中,介绍了MGR对数据可靠性.可用性和一致性的实现方案.简单来说,MGR通过基于paxos协议的多副本来实现数据的可靠性,通过多副本上的majority机制来实现可 ...

  7. 树莓派Raspberry Pi 3安装步骤

    一.需要的硬件 1.Raspberry Pi 3(Model B+)树莓派.购买>https://item.jd.com/29225467867.html 2.输出5V/2A的电源 3.SD卡( ...

  8. 剑指Offer - 九度1387 - 斐波那契数列

    剑指Offer - 九度1387 - 斐波那契数列2013-11-24 03:08 题目描述: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项.斐波那契数列的定义如下: ...

  9. 《Cracking the Coding Interview》——第18章:难题——题目7

    2014-04-29 03:05 题目:给定一个词典,其中某些词可能能够通过词典里其他的词拼接而成.找出这样的组合词里最长的一个. 解法:Leetcode上有Word Break这道题,和这题基本思路 ...

  10. leetcode 【Rotate List 】python 实现

    题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...