UVA 11481 Arrange the Numbers(组合数学 错位排序)
题意:长度为n的序列,前m位恰好k位正确排序,求方法数
前m位选k个数正确排,为cm[m][k],剩余m - k个空位,要错排,这m - k个数可能是前m个数中剩下的,也可能来自后面的n - m个数
考虑这样一个问题,共n个数,前i位错排的方法数,显然dp[i][0] = i!
递推考虑:处理到第i个数时,等价于前i - 1个数错排的方法数减去在前i - 1个数错排的情况下第i位恰好为i的方法数,后者相当于n - 1个数前i - 1位错排
所以 dp[n][i] = dp[n][i - 1] - dp[n - 1][i - 1]
故结果为:
cm[m][k] * dp[n - k][m - k]
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
const int N = 1008, INF = 0x3F3F3F3F;
const LL MOD = 1000000007; LL cm[N][N], dp[N][N]; void init(){
memset(cm, 0, sizeof(cm));
cm[0][0] = 1; for(int i = 1; i < N; i++){
cm[i][0] = 1;
for(int j = 1; j <= i; j++){
cm[i][j] = (cm[i - 1][j - 1] + cm[i - 1][j]) % MOD;
}
}
dp[0][0] = 1;
for(int i = 1; i < N; i++){
dp[i][0] = (dp[i - 1][0] * i) % MOD;
} for(int i = 1; i < N; i++){
for(int j = 1; j <= i; j++){
dp[i][j] = ((dp[i][j - 1] - dp[i - 1][j - 1] ) % MOD + MOD) % MOD;
}
} } int main(){
init();
int t;
cin >> t;
for(int i = 1; i <= t; i++){
int n, m, k;
scanf("%d %d %d", &n, &m, &k);
printf("Case %d: %lld\n", i, cm[m][k] * dp[n - k][m - k] % MOD); }
return 0;
}
UVA 11481 Arrange the Numbers(组合数学 错位排序)的更多相关文章
- UVa 11481 Arrange the Numbers (组合数学)
题意:给定 n,m,k,问你在 1 ~ n 的排列中,前 m 个恰好有 k 个不在自己位置的排列有多少个. 析:枚举 m+1 ~ n 中有多少个恰好在自己位置,这个是C(n-m, i),然后前面选出 ...
- UVA 11481 - Arrange the Numbers 数学
Consider this sequence {1, 2, 3, . . . , N}, as a initial sequence of first N natural numbers. You ca ...
- Light oj 1095 - Arrange the Numbers (组合数学+递推)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1095 题意: 给你包含1~n的排列,初始位置1,2,3...,n,问你刚好固定 ...
- uva 10712 - Count the Numbers(数位dp)
题目链接:uva 10712 - Count the Numbers 题目大意:给出n,a.b.问说在a到b之间有多少个n. 解题思路:数位dp.dp[i][j][x][y]表示第i位为j的时候.x是 ...
- UVA 10539 - Almost Prime Numbers(数论)
UVA 10539 - Almost Prime Numbers 题目链接 题意:给定一个区间,求这个区间中的Almost prime number,Almost prime number的定义为:仅 ...
- light oj 1095 - Arrange the Numbers排列组合(错排列)
1095 - Arrange the Numbers Consider this sequence {1, 2, 3 ... N}, as an initial sequence of first N ...
- UVa 11481 (计数) Arrange the Numbers
居然没有往错排公式那去想,真是太弱了. 先在前m个数中挑出k个位置不变的数,有C(m, k)种方案,然后枚举后面n-m个位置不变的数的个数i,剩下的n-k-i个数就是错排了. 所以这里要递推一个组合数 ...
- POJ 3252 Round Numbers 组合数学
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13381 Accepted: 5208 Description The ...
- UVA 10539 - Almost Prime Numbers 素数打表
Almost prime numbers are the non-prime numbers which are divisible by only a single prime number.In ...
随机推荐
- ResourceHacker(4.5.30)单文件绿色汉化版
经常编译软件的人,肯定会使用一些编译和反编译的工具,ResourceHacker作为一款资源编译工具,非常的小巧好用,ResourceHacker没有任何广告,支持打开.exe..dll..scr.. ...
- VMware安装ubuntu虚拟机
创建虚拟机完成. 安装操作系统. 中文版界面: 中文版界面: 稍等在重启 重新启动虚拟机. 安装VMtools 双击该图标 安装完成,重新启动系统. 如果在安装的时候,主机名没有修改 看着太长 如何修 ...
- <<< Java生成Md5
package cn.sevennight.test; import java.security.MessageDigest; public class md5{ public static Stri ...
- Jedis测试redis
首先:Jedis是redis的java版本的客户端. public class JedisTest { //单机版测试Jedis,不使用连接池 @Test public void testJedis( ...
- js 用途
嵌入动态文本于HTML页面.[4] 对浏览器事件做出响应.[4] 读写HTML元素.[4] 在数据被提交到服务器之前验证数据.[4] 检测访客的浏览器信息.[4] 控制cookies,包括创 ...
- mysql 递归查询
1.创建表: DROP TABLE IF EXISTS `t_areainfo`; CREATE TABLE `t_areainfo` ( `id` ) ' AUTO_INCREMENT, `) ', ...
- QQ空间HD(5)-添加左侧菜单栏内容
DJIconView.m #import "DJIconView.h" @implementation DJIconView - (instancetype)initWithFra ...
- zend studio汉化
在help菜单中选择Install New Software,在 work with栏中添加上这样的地址 http://archive.eclipse.org/technology/babel/upd ...
- 问题解决:psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
错误提示: psql: could not connect to server: No such file or directory Is the server running locally and ...
- PHP错误级别 error_reporting() 函数详解
在PHP开发的时候常常会用到error_reporting(report_level)来调试自己的程序,下面列出了report_level可能值: 值 常量 描述 1 E_ERROR 这是一个严重错误 ...