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)*(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(容斥)的更多相关文章
- 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,问你刚好固定 ...
- Codeforces.449D.Jzzhu and Numbers(容斥 高维前缀和)
题目链接 \(Description\) 给定\(n\)个正整数\(a_i\).求有多少个子序列\(a_{i_1},a_{i_2},...,a_{i_k}\),满足\(a_{i_1},a_{i_2}, ...
- lightoj 1095 - Arrange the Numbers(dp+组合数)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1095 题解:其实是一道简单的组合数只要推导一下错排就行了.在这里就推导一下错排 ...
- LightOJ - 1095 - Arrange the Numbers(错排)
链接: https://vjudge.net/problem/LightOJ-1095 题意: Consider this sequence {1, 2, 3 ... N}, as an initia ...
- Light OJ 1095
题意: 给你 N 个数, 总共有 N! 种排列, 现在 要你统计前 M 个数 刚好 有K 个数 在原来的位置上 的排列个数 思路: 首先 M 中选 K C(m,k): 则 共 剩下 n - k 个数, ...
- light oj 1095 组合数学
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...
- Xtreme9.0 - Light Gremlins 容斥
Xtreme9.0 - Light Gremlins 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenge ...
- LightOJ 1095 Arrange the Numbers-容斥
给出n,m,k,求1~n中前m个正好有k个在原来位置的种数(i在第i个位置) 做法:容斥,先选出k个放到原来位置,然后剩下m-k个不能放到原来位置的,用0个放到原来位置的,有C(m-k,0)*(n-k ...
随机推荐
- 一定要学会OutputDebugString,方便源码级调试
省得到处自己print,麻烦的要死...
- javascript 中ASCII字符值转换
char-->ascii var a = "123"; a.charAt(1).charCodeAt();ascii-->char String.fro ...
- dictionary (key-value) (map容器)
#dictionary可以保存不同类型的值 menu = {'fish0':14.50} list = [] menu['fish1'] = 10.1 # Adding new key-value p ...
- hdu 2817 A sequence of numbers(快速幂)
Problem Description Xinlv wrote some sequences on the paper a long time ago, they might be arithmeti ...
- Error when launching Quest Central for DB2: "QCC10000E - Unable to allocate environment handle fo
标题 Error when launching Quest Central for DB2: "QCC10000E - Unable to allocate environment hand ...
- oracle 双机热备,oracle dataguard 和oracle rac的区别和联系(转)
Data Guard 是Oracle的远程复制技术,它有物理和逻辑之分,但是总的来说,它需要在异地有一套独立的系统,这是两套硬件配置可以不同的系统,但是这两套系统的软件结构保持一致,包括软件的版本,目 ...
- IOS 原生解析JSON 问题
服务器----WebService 返回的是JSON数据 IOS解析报错: Error Domain=NSCocoaErrorDomain Code=3840 "Unable to conv ...
- 编译U-boot时,make[1]: *** 没有规则可以创建mkimage.o”
执行完make smdk2440_config 对Uboot重行编译怎么会出现这样的错误 make[1]: Entering directory `/home/win/S3-ARM/Part4/ubo ...
- 未能的导入项目,请确认<Import>声明中的路径正确
对于这样的错误,根据提示应该是项目的管理文件(.csproj)中有问题.找到对应的位置修改即可
- (转)一小时搞定DIV+CSS布局-固定页面开度布局
本文讲解使用DIV+CSS布局最基本的内容,读完本文你讲会使用DIV+CSS进行简单的页面布局. 转载请标明:http://www.kwstu.com/ArticleView/divcss_20139 ...