UVA 11481 - Arrange the Numbers 数学
Consider this sequence {1, 2, 3, . . . , N}, as a initial sequence of first N natural numbers. You can
earrange this sequence in many ways. There will be N! different arrangements. You have to calculate
the number of arrangement of first N natural numbers, where in first M (M ≤ N) positions, exactly
K (K ≤ M) numbers are in its initial position.
Example:
For, N = 5, M = 3, K = 2
You should count this arrangement {1, 4, 3, 2, 5}, here in first 3 positions 1 is in 1-st position and
3 in 3-rd position. So exactly 2 of its first 3 are in there initial position.
But you should not count this {1, 2, 3, 4, 5}.
Input
The first line of input is an integer T (T ≤ 1000) that indicates the number of test cases. Next T line
contains 3 integers each, N (1 ≤ N ≤ 1000), M, and K.
Output
For each case, output the case number, followed by the answer modulo 1000000007. Look at the sample
for clarification.
Sample Input
1
5 3 2
Sample Output
Case 1: 12
题意:给你 n,m,k, 表示a[i] = 1,2....,n 经过变换后-> 前m个数中只有任意 k个数满足 i = a[i]问你方案数
题解:我们 先在前m个数中任意选k个数是满足不变的 即 C(m,k);
再枚举后n-m个中有多少个数的位置是不变的,C(n−m,x),这样就有n−k−x个数为乱序排列。
对于y个数乱序排序,我们考虑dp做法,假设已经 求出 y-1,y-2个数的乱序排序数,那么 dp[y] = (y-1)*(dp[y-1]+dp[y-2]);(详见上一题)
//meek///#include<bits/stdc++.h>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<iostream>
#include<bitset>
#include<vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std ;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
typedef long long ll; const int N = +;
const int M = ;
const int inf = 0x3f3f3f3f;
const ll MOD = ; int n, m, k;
ll dp[N], c[N][N];
void init () {
for (int i = ; i < N; i++) {
c[i][] = c[i][i] = ;
for (int j = ; j < i; j++)
c[i][j] = (c[i-][j-] + c[i-][j]) % MOD;
}
dp[] = ;
dp[] = ;
dp[] = ;
for (ll i = ; i < N; i++)
dp[i] = ((dp[i-] + dp[i-]) % MOD * (i-)) % MOD;
} ll solve () {
ll ans = ;
int t = n - m;
for(int i = ;i <= n-m; i++) ans += (c[t][i]*dp[n-k-i]), ans %= MOD;
return (ans * c[m][k]) % MOD;
}
int main () {
init();
int cas = , T;
scanf("%d", &T);
while(T--) {
scanf("%d%d%d", &n, &m, &k);
printf("Case %d: %lld\n", cas++, solve());
}
return ;
}
代码
UVA 11481 - Arrange the Numbers 数学的更多相关文章
- UVA 11481 Arrange the Numbers(组合数学 错位排序)
题意:长度为n的序列,前m位恰好k位正确排序,求方法数 前m位选k个数正确排,为cm[m][k],剩余m - k个空位,要错排,这m - k个数可能是前m个数中剩下的,也可能来自后面的n - m个数 ...
- UVa 11481 Arrange the Numbers (组合数学)
题意:给定 n,m,k,问你在 1 ~ n 的排列中,前 m 个恰好有 k 个不在自己位置的排列有多少个. 析:枚举 m+1 ~ n 中有多少个恰好在自己位置,这个是C(n-m, i),然后前面选出 ...
- UVA 11582 Colossal Fibonacci Numbers(数学)
Colossal Fibonacci Numbers 想先说下最近的状态吧,已经考完试了,这个暑假也应该是最后刷题的暑假了,打完今年acm就应该会退了,但是还什么都不会呢? +_+ 所以这个暑假,一定 ...
- 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个数就是错排了. 所以这里要递推一个组合数 ...
- POJ2402/UVA 12050 Palindrome Numbers 数学思维
A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example,the ...
- UVA 11461 - Square Numbers 数学水题
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
随机推荐
- Oracle用户密码过期问题解决
一.用户密码即将过期,导致autotrace无法打开 如果用户密码即将过期,在登录数据库时会收到如下提示: ERROR: ORA-2800 ...
- ASP.NET MVC如何实现自定义验证(服务端验证+客户端验证)
ASP.NET MVC通过Model验证帮助我们很容易的实现对数据的验证,在默认的情况下,基于ValidationAttribute的声明是验证被使用,我们只需 要将相应的ValidationAttr ...
- Libevent windows/linux下编译
1.windows下: 编译环境: windows xp sp3 + vs2010 (1) 解压libevent-2.0.21-stable.tar.gz到D:\libevent-2.0.21- ...
- TFS(Taobao File System)安装方法
文章目录: 一.TFS(Taobao File System)安装方法 二.TFS(Taobao File System)配置dataServer.分区.挂载数据盘 三.TFS(Taobao File ...
- Objective-C-实例变量与属性的关系
当在一个类创建一个属性,Xcode编译器就会自动产生一个带下划线的同名实例变量: 一般来说,如果getter这个属性采用下划线的方式获取效率更高,而setter采用self.属性名更加合理. 读取实例 ...
- P1230: [Usaco2008 Nov]lites 开关灯
嗯嗯,这是一道线段树的题,询问区间内亮着的灯的个数,我们可以把区间修改的线段树改一下,原本的求和改成若有奇数次更改则取反(总长度-亮着的灯个数),而判断是否奇数次只要数组加一个delta的值,upda ...
- [转载]求平方根sqrt()函数的底层算法效率问题
我们平时经常会有一些数据运算的操作,需要调用sqrt,exp,abs等函数,那么时候你有没有想过:这个些函数系统是如何实现的?就拿最常用的sqrt函数来说吧,系统怎么来实现这个经常调用的函数呢? 虽然 ...
- 【每日scrum】5.3
团队的Backlog 初期总目标:完成需求分析,做好软件前期的一切准备. 任务 认领人 估算完成时间 查找铁大离线电子地图 验证地图的准确性 万彤 司新红 3天 接口需求 (用户界面和软件接口等) 曹 ...
- android 通过httpclient下载文件并保存
代码:(主要针对图片.gif下载无问题) /** * 下载网络文件 * @param url 请求的文件链接 * @param IsMD5Name 是否MD5加密URL来命名文件名 * @param ...
- HDU 5593 ZYB's Tree 树形dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5593 题意: http://bestcoder.hdu.edu.cn/contests/contes ...