给定n,m,k,要求在n的全排列中,前m个数字中恰好有k个位置不变,有几种方案?
首先,前m个中k个不变,那就是C(m,k),然后利用容斥原理可得

ans=ΣC(m,k)*(-1)^i*C(m-k,i)*(n-k-i)! (0<=i<=m-k)

 #include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#define ll long long
const ll Mod=;
int n,m,k;
ll jc[],jcny[];
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void exgcd(ll a,ll b,ll &x,ll &y){
if (b==){
x=;
y=;
return;
}
exgcd(b,a%b,x,y);
ll t=x;
x=y;
y=t-(a/b)*y;
}
void init(){
jc[]=;jc[]=;jcny[]=;
for (int i=;i<=;i++)
jc[i]=(jc[i-]*i)%Mod;
ll x,y;
exgcd(jc[],Mod,x,y);
jcny[]=x%Mod;
for (int i=-;i>=;i--)
jcny[i]=(jcny[i+]*(i+))%Mod;
}
ll powit(ll x,ll y){
ll res=;
while (y){
if (y%) res=(res*x)%Mod;
x=(x*x)%Mod;
y/=;
}
return res;
}
ll C(int n,int m){
if (m==) return ;
return ((((jc[n]%Mod)*(jcny[n-m]%Mod))%Mod)*jcny[m])%Mod;
}
ll A(int x){
if (x==) return ;
return jc[x]%Mod;
}
int main(){
init();
int Tcase=;
int T=read();
while (T--){
printf("Case %d: ",++Tcase);
n=read(),m=read(),k=read();
ll ans=%Mod;
for (int i=;i<=m-k;i++)
ans=(ans+powit((ll)-,(ll)i)*A(n-k-i)*C(m-k,i)+Mod)%Mod;
ans=(ans*C(m,k))%Mod;
printf("%lld\n",ans);
}
}

Light OJ 1095 Arrange the Numbers(容斥)的更多相关文章

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

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

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

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

  3. Codeforces.449D.Jzzhu and Numbers(容斥 高维前缀和)

    题目链接 \(Description\) 给定\(n\)个正整数\(a_i\).求有多少个子序列\(a_{i_1},a_{i_2},...,a_{i_k}\),满足\(a_{i_1},a_{i_2}, ...

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

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

  5. LightOJ - 1095 - Arrange the Numbers(错排)

    链接: https://vjudge.net/problem/LightOJ-1095 题意: Consider this sequence {1, 2, 3 ... N}, as an initia ...

  6. Light OJ 1095

    题意: 给你 N 个数, 总共有 N! 种排列, 现在 要你统计前 M 个数 刚好 有K 个数 在原来的位置上 的排列个数 思路: 首先 M 中选 K C(m,k): 则 共 剩下 n - k 个数, ...

  7. light oj 1095 组合数学

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...

  8. Xtreme9.0 - Light Gremlins 容斥

    Xtreme9.0 - Light Gremlins 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenge ...

  9. LightOJ 1095 Arrange the Numbers-容斥

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

随机推荐

  1. 查询Sqlserver 表结构信息 SQL

    SELECT 表名 then d.name else '' end, 表说明 then isnull(f.value,'') else '' end, 字段序号 = a.colorder, 字段名 = ...

  2. java开发经验分享(二)

    二. 数据库 1. SQL语句中保留字.函数名要大写,表明.字段名全部小写 如:SELECT vc_name,vc_sex,i_age FROM user WHERE i_id = 100 AND i ...

  3. C++基础回顾1(数据类型, 控制语句, 数组)

    最近两天打开本科学校的C++教材,快速回顾了一下C++方面的内容.虽然书本内容比较基础,但是还是有些知识点值得自己强化记忆.分几篇文章,加上自己的理解记录如下. 先回顾面向过程的部分. C++数据类型 ...

  4. 马士兵 Servlet & JSP(1) Servlet (源代码)

    1.HTTP协议基础测试(获取页面源代码) import java.io.BufferedReader; import java.io.IOException; import java.io.Inpu ...

  5. 初入Python继承

    1.什么是继承? 新类不用从头编写 新类从现有的类继承,就自动拥有了现有类的所有功能 新类只需要编写现有类缺少的新功能 2.继承的好处 复用已有代码 自动拥有了现有类的所有功能 只需要编写缺少的新功能 ...

  6. [原创作品]web网页中的锚点

    因为近来在从事web前端开发的工作,所以写的文章也都是关于web这一块.以后将分享算法和web高级编程的内容,很多公司的web前端不够重视,以为是很low-level,给的待遇也很一般,其实,这都是很 ...

  7. 站点建设10个最好的响应的HTML5滑块插件

    大多数的最佳响应的HTML5滑块插件能够使用移动应用程序,站点建设项目,以及Web开发项目提供一些令人兴奋的功能,如无限的动画效果,百分之中的一个百响应布局设计和很多其它. 1.别急!慢慢来 功能丰富 ...

  8. qwtplot3D安装及运行-----终结解决方案

    ..\qwtplot3d\include\qwt3d_openglhelper.h:67: 错误:'gluErrorString' was not declared in this scope..\q ...

  9. 在cygwin下编译c语言

    #include <stdio.h> int main (void) { printf("Hello World!\n"); ; } 1.保存到cygwin工作目录下 ...

  10. Javascript高级程序设计读书笔记(第二章)

    第二章  在HTML中使用Javascript 2.1<script>元素 延迟脚本(defer = "defer")表明脚本在执行时不会影响页面的构造,脚本会被延迟到 ...