fzu-Wand
来源:互联网 发布:百度联盟和淘宝联盟 编辑:程序博客网 时间:2024/06/05 23:48
题目:
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
题目大意:
给你n个物品和一个数k,要你将其进行重新排列,使得这n个物品中至少存在k个物品是在自己原来的位置上的。
题目思路:
错排+排列组合
注意求组合数时要用逆元,这里用的费马小定理。
代码:
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<string>#include<vector>#include<stack>#include<bitset>#include<cstdlib>#include<cmath>#include<set>#include<list>#include<deque>#include<map>#include<queue>using namespace std;typedef long long ll;const double PI = acos(-1.0);const double eps = 1e-6;const int mod = 1e9+7;const int INF = 0x3f3f3f3f;const int maxn = 12345;int T,n,m;ll D[maxn];ll fac[maxn];ll ans[maxn];void Get_D(){ D[0]=1; D[1]=0; D[2]=1; for(int i=3;i<=10000;i++) D[i]=(((i-1)%mod)*((D[i-1]+D[i-2])%mod))%mod;}void Get_fac(){ fac[0]=1; for(int i=1;i<=10000;i++) fac[i]=(fac[i-1]*i)%mod;}ll Pow_q(ll a,ll b){ ll ans=1; while(b) { if(b&1) ans=(ans*a)%mod; a=(a*a)%mod; b>>=1; } return ans%mod;}ll Get_C(ll a,ll b){ ll aa,bb; bb=fac[b]; aa=(fac[b-a]*fac[a])%mod; ll aa_ni=Pow_q(aa,mod-2); return ((bb%mod)*(aa_ni%mod))%mod;}int main(){ scanf("%d",&T); Get_D(); Get_fac(); while(T--) { scanf("%d%d",&n,&m); ll ans=0; for(int i=m;i<=n;i++) { ll c=Get_C(n-i,n)%mod; ll d=D[n-i]%mod; ans=(ans+(c*d)%mod)%mod; } printf("%d\n",ans); } return 0;}
- fzu-Wand
- fzu 2282 Wand
- FZU 2282 Wand 【组合数学】
- FZU 2282 Wand(组合)
- FZU 2282 Wand(错排+费马小定理)
- FZU 2282 Wand (组合数学 错排应用)
- FZU 2282 Wand(错排+费马小定理)
- FZU 2282 Wand,利用扩展欧几里得求逆元快速求C(n,m) , 错排公式推导
- FZU 2282Problem 2282 Wand(错排+快速幂+费马小定理)
- FZU2282-Wand
- Wand的布鲁克纳第七
- Michael Wand 的个人主页
- fzu2282 Wand 数学
- FZUOJ 2282 Wand
- FZU
- FZU
- FZU
- FZU
- 高德地图初体验
- zooKeeper学习(一)
- 1027. Colors in Mars (20)
- 【altium designer】画原理图
- python【笔记】:opencv库(一) 输入输出图像
- fzu-Wand
- 【dede】织梦建站汇总
- There is a cycle in the hierarchy原因
- java集合框架图
- js中cookie的使用
- 回到顶部按钮
- [BZOJ]2338: [HNOI2011]数矩形 叉积
- R的数据清洗
- HTTP请求中8种请求方法