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 ...
随机推荐
- Why we have to use epsg:900913 in OpenLayers
reference:http://docs.openlayers.org/library/spherical_mercator.html epsg:900913 is spicfy the Soher ...
- Tomcat server分端口部署web项目
<?xml version='1.0' encoding='utf-8'?> <Server port="8006" shutdown="SHUTDOW ...
- Sqli-labs less 25a
Less-25a 不同于25关的是sql语句中对于id,没有''的包含,同时没有输出错误项,报错注入不能用.其余基本上和25示例没有差别.此处采取两种方式:延时注入和联合注入. http://127. ...
- Sqli-labs less 60
Less-60 与上述一致,同样给出一个示例payload: http://127.0.0.1/sqli-labs/Less-60/?id=-1")union select extractv ...
- iOS打电话,发短信,发邮件,打开网址
//调用自带mail [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://admin@hzl ...
- iOS多线程的初步研究(三)-- NSRunLoop
弄清楚NSRunLoop确实需要花时间,这个类的概念和模式似乎是Apple的平台独有(iOS+MacOSX),很难彻底搞懂(iOS没开源,呜呜). 官网的解释是说run loop可以用于处理异步事件, ...
- SQL技术内幕-13 SQL优化方法论之分离重量级的等待
Code -- Isolate top waits WITH Waits AS ( SELECT wait_type, wait_time_ms / . AS wait_time_s, . * wai ...
- 一个Form中2个按钮,PHP后台如何判断提交的是哪一个按钮
方法一: <div class="container theme-showcase" role="main"> <form class=&qu ...
- Java-对象数组排序
1.对对象数组排序:对象要提供一个compare方法比较对象的大小 2.代码 package Test; public class TestObjectArray { public static vo ...
- Mysql笔记——DQL
DQL就是数据查询语言,数据库执行DQL语句不会对数据进行改变,而是让数据库发送结果集给客户端. 语法: SELECTselection_list /*要查询的列名称*/ FROM table_lis ...