给定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. 这样就算会了PHP么?-4

    数组初步 <?php $ereg = 'tm'; $str = 'hello,tm,Tm,tM,TM.'; $rep_str = eregi_replace($ereg, 'TM', $str) ...

  2. Java7新语法 -try-with-resources

    http://docs.oracle.com/javase/7/docs/technotes/guides/language/try-with-resources.html The try-with- ...

  3. java基础进阶:SQL的运用

    SQL的基础的运用 /* --1.学生表 Student(S,Sname,Sage,Ssex) --S 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别 --2.课程表 Cour ...

  4. AllocConsole

    #include<iostream> using namespace std; AllocConsole(); freopen("CONIN$", "r+t& ...

  5. html--鼠标控制DIV移动

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  6. [ES6] Generators

    Example 1: function *topicList(){ yield "ES2015"; yield "Semi-colons: good or bad?&qu ...

  7. 字符串匹配之horspool算法(简化的BM算法)

    前面介绍在BF,KMP这些算法的时候老是提到BM这个东西,究竟这什么东西,有啥高深的,这些问题我们如今不去考虑.不知道,认真读前几篇文章的读者有没有发现前面的算法都是从模式串的前面開始匹配的,那我们就 ...

  8. [jQuery] 使用jQuery printPage plugin打印其他頁面內容

    目標: 點選按鈕後可以打印其他頁面的內容,可用於套版.內部表單套印...等等. 程式碼: 1.View(HTML布局) <h2>維修申請單</h2> <form id=& ...

  9. FTS下载地址

    http://download.microsoft.com/download/5/2/e/52e22b90-2ba7-427b-9ea4-604d3b37a2e7/vs2012_tfs_chs.iso

  10. CSS 设计模式一 元素

    1.background  内置 是一种CSS内置设计模式,支持在元素下显示图片 HTML <!DOCTYPE html> <html lang="en"> ...