LightOJ - 1095 - Arrange the Numbers(错排)
链接:
https://vjudge.net/problem/LightOJ-1095
题意:
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}.
思路:
错排:
F[n] = (n-1)*(F[n-1]+F[n-2]),F[i]为长度i的错排种类。
递推:
第一步,首元素插到剩下n-1个元素位置k。
第二步,可以选择将k插到首位置,此时就剩下n-2个,即F[n-2]
可以不插到首位置,将原本k位置元素删除,k插到首元素位置,看成k位置,则为F[n-1]
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<utility>
using namespace std;
typedef long long LL;
const int INF = 1e9;
const int MAXN = 1e3+10;
const int MOD = 1e9+7;
int n, k, m;
LL P[MAXN], F[MAXN];
LL PowMod(LL a, LL b)
{
LL res = 1;
while(b)
{
if (b&1)
res = (1LL*res*a)%MOD;
a = (1LL*a*a)%MOD;
b >>= 1;
}
return res;
}
LL Com(LL a, LL b)
{
if (b == 0 || a == b)
return 1;
return 1LL*P[a]*PowMod(P[b]*P[a-b]%MOD, MOD-2)%MOD;
}
void Init()
{
P[1] = 1;
for (int i = 2;i < MAXN;i++)
P[i] = 1LL*i*P[i-1]%MOD;
F[1] = 0, F[0] = F[2] = 1;
for (int i = 3;i < MAXN;i++)
F[i] = 1LL*(i-1)*((F[i-1]+F[i-2])%MOD)%MOD;
}
int main()
{
Init();
int cnt = 0;
int t;
scanf("%d", &t);
while(t--)
{
printf("Case %d:", ++cnt);
scanf("%d%d%d", &n, &m, &k);
LL res = 0LL;
for (int i = 0;i <= n-m;i++)
res = ((res + 1LL*Com(n-m, i)*F[n-k-i]%MOD)%MOD+MOD)%MOD;
res = 1LL*res*Com(m, k)%MOD;
printf(" %lld\n", res);
}
return 0;
}
LightOJ - 1095 - Arrange the Numbers(错排)的更多相关文章
- lightoj 1095 - Arrange the Numbers(dp+组合数)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1095 题解:其实是一道简单的组合数只要推导一下错排就行了.在这里就推导一下错排 ...
- light oj 1095 - Arrange the Numbers排列组合(错排列)
1095 - Arrange the Numbers Consider this sequence {1, 2, 3 ... N}, as an initial sequence of first N ...
- Light oj 1095 - Arrange the Numbers (组合数学+递推)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1095 题意: 给你包含1~n的排列,初始位置1,2,3...,n,问你刚好固定 ...
- Light OJ 1095 Arrange the Numbers(容斥)
给定n,m,k,要求在n的全排列中,前m个数字中恰好有k个位置不变,有几种方案?首先,前m个中k个不变,那就是C(m,k),然后利用容斥原理可得 ans=ΣC(m,k)*(-1)^i*C(m-k,i) ...
- LightOJ 1095 Arrange the Numbers-容斥
给出n,m,k,求1~n中前m个正好有k个在原来位置的种数(i在第i个位置) 做法:容斥,先选出k个放到原来位置,然后剩下m-k个不能放到原来位置的,用0个放到原来位置的,有C(m-k,0)*(n-k ...
- codeforces 340E Iahub and Permutations(错排or容斥)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Iahub and Permutations Iahub is so happy ...
- Codeforces 888D: Almost Identity Permutations(错排公式,组合数)
A permutation \(p\) of size \(n\) is an array such that every integer from \(1\) to \(n\) occurs exa ...
- 清北学堂模拟赛day7 错排问题
/* 考虑一下已经放回m本书的情况,已经有书的格子不要管他,考虑没有书的格子,不考虑错排有(n-m)!种,在逐步考虑有放回原来位置的情况,已经放出去和已经被占好的格子,不用考虑,剩下全都考虑,设t=x ...
- UVA 11481 Arrange the Numbers(组合数学 错位排序)
题意:长度为n的序列,前m位恰好k位正确排序,求方法数 前m位选k个数正确排,为cm[m][k],剩余m - k个空位,要错排,这m - k个数可能是前m个数中剩下的,也可能来自后面的n - m个数 ...
随机推荐
- MySQL Group Replication的安装部署
一.简介 这次给大家介绍下MySQL官方最新版本5.7.17中GA的新功能 Group Replication . Group Replication是一种可用于实现容错系统的技术.复制组是一组通过消 ...
- c# webapi 过滤器token、sign认证、访问日志
1.token认证 服务端登录成功后分配token字符串.记录缓存服务器,可设置有效期 var token = Guid.NewGuid().ToString().Replace("-&qu ...
- 【转载】 C#中List集合使用First方法查找符合条件的第一个元素
在C#的List集合相关操作中,很多时候需要从List集合中查找出符合条件的第一个元素对象,如果确认在List集合中一定存在符合条件的元素,则可以使用First方法来查找,First方法调用格式为Fi ...
- c++ 使用torchscript 加载训练好的pytorch模型
1.首先官网上下载libtorch,放到当前项目下 2.将pytorch训练好的模型使用torch.jit.trace导出为.pt格式 import torch from skimage import ...
- Jmeter学习笔记(十一)——定时器
默认情况下,Jmeter线程在发送请求之间没有间歇.不设置定时器,短时间内会产生大量访问请求,导致服务器被请求淹没,利用Jmeter进行压测时,一般会和定时器一起,控制请求的吞吐量和并发数. 一.定时 ...
- 【MySQL】你以为设置了并行复制就降低延迟了?这个你绝对想不到!
在MySQL官方版本中,为了保证其的高可用性,一般情况我们会采用主从复制的方式来解决.当然,方法很多.而我们今天所要处理的是采用GTID方式并且开了多线程复制后,仍然延迟的情况,糟糕的是,延迟还在不断 ...
- Flink原理(四)——任务及调度
本文是博主阅读官网文档.博客及书籍后自己所思所得,若是存在有误的地方,欢迎留言分享,谢谢! 一.任务调度 Flink是通过task slot的来定义执行资源的,为优化资源的利用率,Flink通过slo ...
- CentOS7.5安装python-pip报Error: Nothing to do解决方法
python中的一个十分好用的包管理工具python-pip是我们使用python必不可少的一件工具.但是在CentOS7安装时候却报Error: Nothing to do: [root@bnsf- ...
- 分布式结构化存储系统-HBase基本架构
分布式结构化存储系统-HBase基本架构 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在大数据领域中,除了直接以文件形式保存数据外,还有大量结构化和半结构化的数据,这类数据通常需 ...
- C实现栈与队列
C实现栈与队列 做了个栈和队列的基础demo,写得比较快,就没有什么注释,其实看各个函数的名字就可以知道函数的作用了. 栈的实现 #include <stdio.h> #include & ...