Wand FZU - 2282 全错位重排
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 现在一群 wizard 在开会,进入会议室的时候,每个人将自己的 wand 放到了桌子上,现在开会结束了,
在 wizard 们将要离开的时候,将 wand 原来的摆放顺序打乱,那 wizard 将桌子上的 wang 拿走时:
要求:至少有 K 个人拿到的是自己原来的 wand 。问,有多少种打乱顺序的方法
首先在 n 个中拿出 k 到 n 个,是放在原来的位置,方法有 C(n,i)种,然后将剩下的那些 wand 错排 预处理阶乘逆元
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("DATA.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
const int INF = 0x7fffffff;
const int mod = 1e9 + ;
const int maxn = 1e5 + ;
int n, k, t;
LL ans[maxn], a = , b = ;
void init() {
ans[] = a, ans[] = b;
for (int i = ; i <= ; i++) {
ans[i] = (((i - ) % mod) * ((a + b) % mod)) % mod;
a = b;
b = ans[i];
}
}
LL C(int k, int n) {
LL ret = ;
for (int i = n - k + ; i <= n; i++) ret *= i;
for (int i = ; i <= k; i++) ret /= i;
return ret;
}
LL c[]; long long modexp(long long a, long long b, int mod) {
long long res = ;
while(b > ) {
if (b & ) res = res * a % mod;
b = b >> ;
a = a * a % mod;
}
return res;
} void cal() {
LL ans = ;
for(int i = ; i <= 1e7; i ++) {
ans *= i;
ans %= mod;
c[i] = ans;
}
}
int main() {
cal();
c[] = ;
init();
sf(t);
while(t--) {
sff(n, k);
LL ret = ;
for (int i = k ; i <= n ; i++) {
LL fz = c[n];
LL fm = (c[i] * c[n - i]) % mod;
LL a1 = (fz * (modexp(fm, mod - , mod) % mod)) % mod;
ret = (ret + a1 * ans[n - i] % mod) % mod;
}
printf("%lld\n", ret % mod);
}
return ;
}
Wand FZU - 2282 全错位重排的更多相关文章
- [fzu 2282]置换不动点大于等于k的排列数
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2282 编号1~n的置换,不动点个数大于等于k的方案数. 参考百度百科错排公式,可以知道长度为n,每个数都不在自 ...
- HDU_2048——全错位排列递推公式
Problem Description HDU 2006'10 ACM contest的颁奖晚会隆重开始了! 为了活跃气氛,组织者举行了一个别开生面.奖品丰厚的抽奖活动,这个活动的具体要求是这样的:首 ...
- HDU_2049——部分错位排列,概率论
Problem Description 国庆期间,省城HZ刚刚举行了一场盛大的集体婚礼,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做"考新郎",具体的操作是这样 ...
- NYOJ 451
光棍节的快乐 描述 光棍们,今天是光棍节.聪明的NS想到了一个活动来丰富这个光棍节. 规则如下: 每个光棍在一个纸条上写一个自己心仪女生的名字,然后把这些纸条装进一个盒子里,这些光 棍依次抽取一张纸条 ...
- 神、上帝以及老天爷[HDU2048]
神.上帝以及老天爷 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- CCF考前注意几点
1.数组最好保持初始化习惯,且检查数组初始化在循环内还是循环外,若在循环内需要对数组进行处理,则数组初始化必须放在for循环内. 2.for循环保持好习惯,用括号括起来,以免粗心. 3.if条件判断要 ...
- NYOJ-451-光棍节的快乐
光棍节的快乐 时间限制:1000 ms | 内存限制:65535 KB 难度: 描写叙述 光棍们,今天是光棍节.聪明的NS想到了一个活动来丰富这个光棍节. 规则例如以下: 每一个光棍在一个纸条上写 ...
- [BZOJ 4563]放棋子
[BZOJ 4563]放棋子 题目 给你一个N*N的矩阵,每行有一个障碍,数据保证任意两个障碍不在同一行,任意两个障碍不在同一列,要求你在这个矩阵上放N枚棋子(障碍的位置不能放棋子),要求你放N个棋子 ...
- 12 October
次小生成树 http://poj.org/problem?id=1679 不难得出,次小生成树可以由最小生成树更换一条边得到. 首先构造原图的最小生成树,然后枚举每一条不在最小生成树中的边 (u, v ...
随机推荐
- java字符转义
之前对java字符转义这一块稍作了解,在这里理理自己主观浅显的理解 这里会谈谈字符编码的是另一种问题和转义没有关系 以下面代码做分析 System.out.println("a". ...
- Apache POI:Excel读写库
1)Apache POI 简介 Apache POI是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写 ...
- JDK源码分析:Short.java
Short是基本数据类型short的包装类. 1)声明部: public final class Short extends Number implements Comparable<Short ...
- hadoop3.0新特性及新功能
Hadoop-3.0.0-alpha2版本发布,相比之前的hadoop-2.x有一系列的功能增强.但目前还是个alpha版本,有很多bug,且不能保证API的稳定和质量. 主要变化 Java最低版本要 ...
- Java学习笔记-13.创建窗口和程序片
1.init()方法:程序片第一次被创建,初次运行初始化程序片时调用. start()方法:每当程序片进入web浏览器中,并且允许程序片启动他的常规操作时调用(特殊的程序片被stop()关闭):同样在 ...
- Mysql-表和字段操作
1.查看表 show tables; 2.创建表 create table test( id int primary key auto_increment, name varchar(40) not ...
- [LeetCode] 53. Maximum Subarray 解题思路
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- BZOJ 1503 郁闷的出纳员(平衡树)(NOI 2004)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1503 Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作 ...
- POJ 1113 Wall(计算几何の凸包)
Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall ...
- redis 编译安装错误问题
编译redis安装的时候报错如下: make[1]: [persist-settings] Error 2 (ignored) CC adlist.o/bin/sh: cc: command not ...