链接:

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(错排)的更多相关文章

  1. lightoj 1095 - Arrange the Numbers(dp+组合数)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1095 题解:其实是一道简单的组合数只要推导一下错排就行了.在这里就推导一下错排 ...

  2. light oj 1095 - Arrange the Numbers排列组合(错排列)

    1095 - Arrange the Numbers Consider this sequence {1, 2, 3 ... N}, as an initial sequence of first N ...

  3. Light oj 1095 - Arrange the Numbers (组合数学+递推)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1095 题意: 给你包含1~n的排列,初始位置1,2,3...,n,问你刚好固定 ...

  4. 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) ...

  5. LightOJ 1095 Arrange the Numbers-容斥

    给出n,m,k,求1~n中前m个正好有k个在原来位置的种数(i在第i个位置) 做法:容斥,先选出k个放到原来位置,然后剩下m-k个不能放到原来位置的,用0个放到原来位置的,有C(m-k,0)*(n-k ...

  6. codeforces 340E Iahub and Permutations(错排or容斥)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Iahub and Permutations Iahub is so happy ...

  7. Codeforces 888D: Almost Identity Permutations(错排公式,组合数)

    A permutation \(p\) of size \(n\) is an array such that every integer from \(1\) to \(n\) occurs exa ...

  8. 清北学堂模拟赛day7 错排问题

    /* 考虑一下已经放回m本书的情况,已经有书的格子不要管他,考虑没有书的格子,不考虑错排有(n-m)!种,在逐步考虑有放回原来位置的情况,已经放出去和已经被占好的格子,不用考虑,剩下全都考虑,设t=x ...

  9. UVA 11481 Arrange the Numbers(组合数学 错位排序)

    题意:长度为n的序列,前m位恰好k位正确排序,求方法数 前m位选k个数正确排,为cm[m][k],剩余m - k个空位,要错排,这m - k个数可能是前m个数中剩下的,也可能来自后面的n - m个数 ...

随机推荐

  1. 21 javaweb开发--bug调试技巧

    1.当修改代码后,测试时没有任何效果 解决方案:换个浏览器试试,可能是浏览器缓存的原因.

  2. Redhat7.6Linux本地的yum源配置

    安装好虚拟机,配置Ip地址,见博客https://www.cnblogs.com/xuzhaoyang/p/11264573.html 我是使用的Xshell在外部链接进行的操作,比较简单,见博客ht ...

  3. [转帖]中信银行信用卡业务数据库实现国产替换,湖北银行新核心系统项目正式验收,阿里云与MongoDB达成战略合作

    中信银行信用卡业务数据库实现国产替换,湖北银行新核心系统项目正式验收,阿里云与MongoDB达成战略合作 http://www.itpub.net/2019/10/31/3942/ 中信银行 gold ...

  4. Python第六章 面向对象

    第六章 面向对象 1.面向对象初了解 ​ 面向对象的优点: ​ 1.对相似功能的函数,同一个业务下的函数进行归类,分类 ​ 2.类是一个公共的模板,对象就是从具体的模板中实例化出来的,得到对象就得到一 ...

  5. Java开发环境的搭建02——IntelliJ IDEA篇(Windows)

    1.IntelliJ IDEA的下载与安装 IntelliJ IDEA简称IDEA,由JetBrains公司开发,是java语言开发的集成环境,也是目前业界被公认的最好的java开发工具之一.尤其在智 ...

  6. 3.02定义常量之const

    [注:本程序验证是使用vs2013版] #include <stdio.h> #include <stdlib.h> #include <string.h> #pr ...

  7. ArcGIS 字段计算器 Python 坑

    最近要处理个简单数据,一个字段中为文本类型,包含各种描述.要求是包含平方米的数值提取出来,变成数值,如果包含多个,则把各个值累加起来. 比如 字段值为 “非法占用100平方米” 处理后结果为 100 ...

  8. wpf 把两个Bitmap 合并为一个

    水平合并 /// <summary> /// 将两个Bitmap水平合并为一个 /// </summary> /// <param name="first&qu ...

  9. C# vb .net实现移除透明度效果

    在.net中,如何简单快捷地实现Photoshop滤镜组中的移除透明度效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 ...

  10. AD域控服务器更改IP地址步骤

    1.cmd命令行,先停止netlogon服务:net stop netlogon 2.修改本机的IP地址 3.清楚IP缓存:ipconfig /flushdns 4.重启netlogon服务:net ...