给定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. cv:显示Linux命令运行进度

    cv: 显示 cp.mv 等命令的进度 2014-07-14 By toy Posted in Apps Edit on GitHub 在 Linux 系统中 , 大多数命令从来都是信奉 “ 沉默是金 ...

  2. 【转】Ubuntu重装,直接进win7,不进linux的解决方案(添加Ubuntu启动菜单)

    原文网址:http://my.oschina.net/u/1377657/blog/281875 本人重装了Ubuntu12.04.4 LTS, 将Ubuntu启动项放在了/boot分区里面,启动的时 ...

  3. Fence Repair (POJ 3253)

    农夫约翰为了修理栅栏,要将一块很长的木板切割成N块.准备切成的木板长度为L1.L2.L3...LN,未切割前的木板长度恰好为切割后木板长度的总和.每次切断木板时,需要的开销为这块木板的长度.例如长度为 ...

  4. "V租房"搭建微信租房平台,让租房人发起求租需求并接收合适房源回复,提高租房效率 | 36氪

    "V租房"搭建微信租房平台,让租房人发起求租需求并接收合适房源回复,提高租房效率 | 36氪 "V租房"搭建微信租房平台,让租房人发起求租需求并接收合适房源回复 ...

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

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

  6. java.lang.NoSuchFieldError: deferredExpression解决

       java.lang.NoSuchFieldError: deferredExpression这个问题的出现是在的lib下面有多个版本的jstl.jar包,解决办法很简单,只留下一个版本的jstl ...

  7. hdu 4640 Island and study-sister

    bfs+状态压缩求出所有的状态,然后由于第一个节点需要特殊处理,可以右移一位剔除掉,也可以特判.然后采用集合的操作, #pragma comment(linker,"/STACK:10240 ...

  8. HDOJ 1316 How Many Fibs?

    JAVA大数.... How Many Fibs? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  9. [转]Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

    Spring Boot——2分钟构建spring web mvc REST风格HelloWorld http://projects.spring.io/spring-boot/ http://spri ...

  10. Oracle11g环境设置-windows环境

    新建环境变量(系统变量),变量名:ORACLE_HOME 变量值:E:\app\Administrator\product\11.2.0\dbhome_1 新建环境变量(系统变量),变量名:ORACL ...