FZU 2282 Wand 【组合数学】
来源:互联网 发布:减肥软件有哪些 编辑:程序博客网 时间:2024/06/03 11:34
Accept: 47 Submit: 191
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
Sample Output
Source
第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院)思路:全部的排列减去不符合情况的排列数量(不符合的情况是:0~k-1个东西不在自己的位置上)
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<queue>#include<stack>#include<vector>#include<map>#include<set>#include<algorithm>using namespace std;#define ll long long#define ms(a,b) memset(a,b,sizeof(a))#define maxn 510const int M=1e6+10;const int inf=0x3f3f3f3f;const int mod=1e9+7;int i,j,k,n,m;int r1,r2,l1,l2;ll d[M];ll c[M];ll a[M];void init(){ d[0]=0;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; } a[0]=1; for(int i=1;i<=10000;i++){ a[i]=(a[i-1]*i)%mod; }}ll ex_gcd(ll a,ll b,ll &x,ll &y){ ll d=a; if(b!=0){ d=ex_gcd(b,a%b,y,x); y-=(a/b)*x; } else { x=1; y=0; } return d;}ll mod_inverse(int b,int mod){ ll x,y; ex_gcd(b,mod,x,y); return (mod+x%mod)%mod;}void solve(int n,int k){ c[0]=1; for(int i=1;i<=k;i++){ ll kk=mod_inverse(i,mod); c[i]=(c[i-1]*kk)%mod; c[i]=(c[i]*(n-i+1))%mod; }}int main(){ int t; scanf("%d",&t); init(); while(t--){ scanf("%d%d",&n,&k); solve(n,k); ll ans=0; for(int i=0;i<=k-1;i++){ ans+=((d[n-i])*c[i])%mod; ans%=mod; } ans=(a[n]-ans+mod)%mod; printf("%lld\n",ans); } return 0;}
- FZU 2282 Wand 【组合数学】
- FZU 2282 Wand (组合数学 错排应用)
- FZU 2282 Wand(组合)
- fzu 2282 Wand
- fzu-Wand
- FZU 1876 组合数学
- 【组合数+全错排】fzuoj 2282 Wand
- FZU 2282 Wand(错排+费马小定理)
- FZU 2282 Wand(错排+费马小定理)
- FZU -- 2282 和 DHU -- 2049 【简单错排 + 组合数学】
- fzu2282 Wand 数学
- FZU 2282Problem 2282 Wand(错排+快速幂+费马小定理)
- FZU 2282 Wand,利用扩展欧几里得求逆元快速求C(n,m) , 错排公式推导
- fzu 2103 Bin & Jing in wonderland(组合数学)
- FZU 2103(Bin & Jing in wonderland)组合数学
- FZUOJ 2282 Wand
- FZU 2020 组合数求模
- FZU 2020 组合
- JS四舍五入问题处理
- C++中const迭代器 和 const_iterator的区别
- [leetcode]175 Second Highest Salary
- C++中字符作为数组下标
- css3
- FZU 2282 Wand 【组合数学】
- easyui的tree加载数据过滤匹配
- Docker模拟多人协作进行Git操作
- Android 7.0中apn FOTA升级问题解决
- 微信小程序tabBar底部导航栏菜单详细介绍
- Python I/O多路复用
- R分类算法-Logistic回归算法
- XPath、XML
- Producer 性能调优公式及验证