UVa 11481 (计数) Arrange the Numbers
居然没有往错排公式那去想,真是太弱了。
先在前m个数中挑出k个位置不变的数,有C(m, k)种方案,然后枚举后面n-m个位置不变的数的个数i,剩下的n-k-i个数就是错排了。
所以这里要递推一个组合数和错排数。
顺便再复习一下错排递推公式,Dn = (n-1)(Dn-1 + Dn-2),D0 = 1,D1 = 0.
这里强调一下D0的值,我之前就是因为直接从D1和D2开始递推的结果WA
#include <cstdio>
typedef long long LL; const int maxn = + ;
const LL M = ;
LL c[maxn][maxn], d[maxn]; inline LL mul(LL a, LL b) { return (a * b) % M; } void init()
{
for(int i = ; i < maxn; i++) c[i][] = c[i][i] = ;
for(int i = ; i < maxn; i++)
for(int j = ; j < i; j++)
c[i][j] = (c[i-][j] + c[i-][j-]) % M;
d[] = ; d[] = ; d[] = ;
for(int i = ; i < maxn; i++) d[i] = mul(i-, (d[i-] + d[i-]) % M);
} int main()
{
//freopen("in.txt", "r", stdin); init();
int T; scanf("%d", &T);
for(int kase = ; kase <= T; kase++)
{
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
LL ans = ;
for(int i = ; i <= n - m; i++)
ans = (ans + mul(c[n-m][i], d[n-k-i])) % M;
ans = (ans * c[m][k]) % M;
printf("Case %d: %lld\n", kase, ans);
} return ;
}
代码君
UVa 11481 (计数) Arrange the Numbers的更多相关文章
- 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 数学
Consider this sequence {1, 2, 3, . . . , N}, as a initial sequence of first N natural numbers. You ca ...
- 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 1640 (计数) The Counting Problem
题意: 统计[a, b]或[b, a]中0~9这些数字各出现多少次. 分析: 这道题可以和UVa 11361比较来看. 同样是利用这样一个“模板”,进行区间的分块,加速运算. 因为这里没有前导0,所以 ...
- LightOJ - 1095 - Arrange the Numbers(错排)
链接: https://vjudge.net/problem/LightOJ-1095 题意: Consider this sequence {1, 2, 3 ... N}, as an initia ...
- UVA 10564 计数DP
也是经典的计数DP题,想练练手,故意不写记忆化搜索,改成递推,还是成功了嘞...不过很遗憾一开始WA了,原来是因为判断结束条件写个 n或s为0,应该要一起为0的,搞的我以为自己递推写挫了,又改了一下, ...
- UVa 11529 (计数) Strange Tax Calculation
枚举一个中心点,然后将其他点绕着这个点按照极角排序. 统计这个中心点在外面的三角形的个数,然后用C(n-1, 3)减去这个数就是包含这个点的三角形的数量. 然后再枚举一个起点L,终点为弧度小于π的点R ...
- UVa 11609 (计数 公式推导) Teams
n个人里选k个人有C(n, k)中方法,再从里面选一人当队长,有k中方法. 所以答案就是 第一步的变形只要按照组合数公式展开把n提出来即可. #include <cstdio> typed ...
随机推荐
- SOA之(1)——SOA架构基础概念
在深入探讨什么是面向服务的架构(SOA)之前,先建立一些基本的概念和术语的基本描述而非严格定义,所以也许有些定义在业内还存留争议,此处暂且忽略. 架构基础 技术架构(Technology Archit ...
- ubuntu学习之一
1终端 在右键后菜单中找到 终端,(如果没有终端,使用 Ctrl+Alt+[F1~F6] ,您可以切换到1~6号控制台,输入: sudo apt-get install nautilus-open-t ...
- 还原TexturePacker plist 文件以及图片的方法 (切开各小图片)
原地址:http://blog.csdn.net/linuxchen/article/details/16865645 Python 脚本:(来自网络) unpack_plist.py 命令行: py ...
- logback日志文件需要注意点
1.支持的jar包 logback-access-1.1.1.jarlogback-classic-1.1.1.jarlogback-core-1.1.1.jar 2.logback.xml文件,we ...
- POJ 1547
#include<iostream> #include<string> using namespace std; int main() { int length; int hi ...
- Ehcache使用
http://www.360doc.com/content/14/0423/17/16946725_371472946.shtml http://www.myexception.cn/web-appl ...
- poj3415 Common Substrings(后缀数组,单调栈 | 后缀自动机)
[题目链接] http://poj.org/problem?id=3415 [题意] A与B长度至少为k的公共子串个数. [思路] 基本思想是将AB各个后缀的lcp-k+1的值求和.首先将两个字符串拼 ...
- hibernate初次配置问题
1.自动创建表结构 在hibernate.cfg.xml配置文件中修改 <property name="hibernate.hbm2ddl.auto">update&l ...
- MySQL数据库的基本操作命令
MySQL数据库的基本操作命令 [mysql]mysql 常用建表语句 一.mysql服务操作 net start mysql //启动mysql服务 net stop mysql //停止mysql ...
- Hadoop基础教程之重新认识Hadoop
之前,我们把hadoop从下载包部署到编写了helloworld,看到了结果.现是得开始稍微更深入地了解hadoop了. Hadoop包含了两大功能DFS和MapReduce, DFS可以理解为一 ...