Light OJ 1095 Arrange the Numbers(计数)
2015-09-22 17:54
267 查看
Consider this sequence {1, 2, 3 ... N}, as an initial sequence of first N natural numbers. You can rearrange this sequence in many ways. There will be a total of N! arrangements. You have to calculate the
number of arrangement of first N natural numbers, where in first M positions; exactly K numbers are in their initial position.
For Example, N = 5, M = 3, K = 2
You should count this arrangement {1, 4, 3, 2, 5}, here in first 3 positions 1 is in 1st position and 3 in 3rd position. So exactly 2 of its first 3 are in there initial position.
But you should not count {1, 2, 3, 4, 5}.
Each case contains three integers N (1 ≤ N ≤ 1000), M (M ≤ N), K (0 < K ≤ M).
分析:K个数位置不变,剩余m-k个数肯定参与错排,那么n-m个数里参与错排的个数枚举下就可以算出来了
number of arrangement of first N natural numbers, where in first M positions; exactly K numbers are in their initial position.
For Example, N = 5, M = 3, K = 2
You should count this arrangement {1, 4, 3, 2, 5}, here in first 3 positions 1 is in 1st position and 3 in 3rd position. So exactly 2 of its first 3 are in there initial position.
But you should not count {1, 2, 3, 4, 5}.
Input
Input starts with an integer T (≤ 1000), denoting the number of test cases.Each case contains three integers N (1 ≤ N ≤ 1000), M (M ≤ N), K (0 < K ≤ M).
Output
For each case, print the case number and the total number of possible arrangements modulo 1000000007.Sample Input | Output for Sample Input |
2 5 3 2 10 6 3 | Case 1: 12 Case 2: 64320 |
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<set> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) const int INF=0x3f3f3f3f; typedef long long LL; const int mod=1000000007; LL dp[1010],C[1010][1010]; void init() { C[1][1]=C[1][0]=C[0][0]=1; for(int i=2;i<=1000;i++) { C[i][i]=C[i][0]=1; for(int j=1;j<i;j++) C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod; } dp[1]=0;dp[2]=1;dp[0]=1; for(int i=3;i<=1000;i++) dp[i]=1LL*(i-1)*(dp[i-1]+dp[i-2])%mod; } LL Cal(int n,int m,int k) { int x=n-m; int y=m-k; LL ans=0; for(int i=0;i<=x;i++) ans=(ans+C[x][i]*dp[y+i]%mod)%mod; return ans*C[m][k]%mod; } int main() { init(); int t,cas=1; int n,m,k; scanf("%d",&t); while(t--) { scanf("%d%d%d",&n,&m,&k); printf("Case %d: %lld\n",cas++,Cal(n,m,k)); } return 0; }
相关文章推荐
- 怎么样eclipse发达国家多重聚合关系maven项目和使用git管理
- Python应用(一) 识别网站验证码以及识别算法
- url获取参数的正则表达式
- mysql 1050 Table './crm/insight' already exists
- Unity3D中鼠标旋转物体
- thinkphp——制作一个输出调试函数,随时调试
- 定时器的使用
- maven setting详解
- 代理模式(proxy)
- 2015年开学典礼∣校长陈十一:选择,从Innovation Capital出发
- ubuntu下安装搜狗输入法
- Unity3D中鼠标拖拽物体
- Unix的管道文件
- 从递归算法思想解析汉诺塔问题
- 【bzoj4173】【数学】【数论】
- uva 11426 - GCD - Extreme (II) 欧拉函数
- SQL子查询and左右连接
- 对象已死?
- unity简单的单机登陆机制
- mysql组合索引与字段顺序