[xdoj1158]阶乘求逆元(常用于求组合数)
http://acm.xidian.edu.cn/problem.php?id=1158
解题关键:此题注意将$\sum\limits_{i = 0}^x {C_x^iC_y^{i + k}}$转化为$C_{x + y}^{x + k}$
利用二项式定理,
一方面,
${(1 + a)^y}{(1 + \frac{1}{a})^x}$的${a^k}$项的系数,第一个二项式的${a^j}$的系数$C_y^j$,第二个二项式的${a^{ - i}}$系数为$C_x^i$,令$j - i = k$,$j = i + k$,即$\sum\limits_{i = 0}^x {C_x^iC_y^{i + k}}$
另一方面,
${(1 + a)^y}{(1 + \frac{1}{a})^x} = {(1 + a)^{x + y}}{a^{ - x}}$,此时${a^k}$项的系数为$C_{x + y}^{x + k}$,得证。
1、打表
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+;
ll x,y,k;
ll fac[],inv[]; ll mod_pow(ll x,ll n,ll mod){
ll res=;
while(n){
if(n&) res=res*x%mod;
n>>=;
x=x*x%mod;
}
return res;
} ll Inv(ll x){
return mod_pow(x,mod-,mod);
} int main(){
fac[]=;
for(int i=;i<=;i++) fac[i]=fac[i-]*i%mod;//预处理一下,阶乘
inv[]=mod_pow(fac[],mod-,mod);//Fac[N]^{MOD-2}
for(int i=-;i>=;i--) inv[i]=inv[i+]*(i+)%mod; while(~scanf("%lld%lld%lld",&x,&y,&k)){
if(y==k) printf("1\n");
else if(y<k) printf("0\n");
// else printf("%lld\n",fac[x+y]*inv(fac[x+k])%mod*inv(fac[y-k])%mod);
else printf("%lld\n",(fac[x+y]%mod*inv[x+k]%mod*inv[y-k]%mod+mod)%mod);
}
return ;
}
2、打表+逆元实现
为什么是$n{!^{\bmod - 2}}$
$n{!^{p - 2}}*n! = n{!^{p - 1}} = 1\bmod {\rm{ p;}}$(费马小定理)p为质数,此题中即为mod
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+;
ll x,y,k;
ll fac[],inv[]; ll mod_pow(ll x,ll n,ll mod){
ll res=;
while(n){
if(n&) res=res*x%mod;
n>>=;
x=x*x%mod;
}
return res;
} ll Inv(ll x){
return mod_pow(x,mod-,mod);
} int main(){
fac[]=;
for(int i=;i<=;i++) fac[i]=fac[i-]*i%mod;//预处理一下,阶乘
//inv[2000000]=mod_pow(fac[2000000],mod-2,mod);//Fac[N]^{MOD-2}
//for(int i=2000000-1;i>=0;i--) inv[i]=inv[i+1]*(i+1)%mod; while(~scanf("%lld%lld%lld",&x,&y,&k)){
if(y==k) printf("1\n");
else if(y<k) printf("0\n");
else printf("%lld\n",fac[x+y]*Inv(fac[x+k])%mod*Inv(fac[y-k])%mod);
//else printf("%lld\n",(fac[x+y]%mod*inv[x+k]%mod*inv[y-k]%mod+mod)%mod);
}
return ;
}
[xdoj1158]阶乘求逆元(常用于求组合数)的更多相关文章
- HNU 12933 Random Walks Catalan数 阶乘求逆元新技能
一个Catalan数的题,打表对每个数都求一次逆元会T,于是问到了一种求阶乘逆元的打表新方法. 比如打一个1~n的阶乘的逆元的表,假如叫inv[n],可以先用费马小定理什么的求出inv[n],再用递推 ...
- 扩展gcd求逆元
当模数为素数时可以用费马小定理求逆元. 模数为合数时,费马小定理大部分情况下失效,此时,只有与模数互质的数才有逆元(满足费马小定理的合数叫伪素数,讨论这个问题就需要新开一个博客了). (对于一个数n, ...
- 求组合数、求逆元、求阶乘 O(n)
在O(n)的时间内求组合数.求逆元.求阶乘.·.· #include <iostream> #include <cstdio> #define ll long long ;// ...
- HDU 5698——瞬间移动——————【逆元求组合数】
瞬间移动 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 【逆元求组合数 && 离散化】
任意门:http://codeforces.com/contest/689/problem/E E. Mike and Geometry Problem time limit per test 3 s ...
- 牛客小白月赛14 -B (逆元求组合数)
题目链接:https://ac.nowcoder.com/acm/contest/879/B 题意:题目意思就是求ΣC(n,i)pi(MOD+1-p)n-i (k<=i<=n),这里n,i ...
- 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)
先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...
- CodeForces 146E - Lucky Subsequence DP+扩展欧几里德求逆元
题意: 一个数只含有4,7就是lucky数...现在有一串长度为n的数...问这列数有多少个长度为k子串..这些子串不含两个相同的lucky数... 子串的定义..是从这列数中选出的数..只要序号不同 ...
- HDU 5446——Unknown Treasure——————【CRT+lucas+exgcd+快速乘+递推求逆元】
Each test case starts with three integers n,m,k(1≤m≤n≤1018,1≤k≤10) on a line where k is the number o ...
随机推荐
- MYSQL:基础——3N范式的表结构设计
基于3N范式的数据表设计 范式 设计关系数据库时,遵从不同的规范要求,设计出合理的关系型数据库,这些不同的规范要求被称为不同的范式,各种范式呈递次规范,越高的范式数据库冗余越小. 关系数据库现有六种范 ...
- HDU - 1134 Game of Connections 【DP】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1134 题意 给出一个n 然后有2n个点 给两个点连一条边,最后连N条边,要求所有的边不能够交叉 问最多 ...
- 如何让Jackson JSON生成的数据包含的中文以unicode方式编码
我们都知道,Jackson JSON以高速.方便和灵活著称.之前的文章中介绍过使用注解的形式来规定如何将一个对象序列化成JSON的方法,以及如何将一个JSON数据反序列化到一个对象上.但是美中不足的一 ...
- ubuntu16.04 docker安装
docker官网安装页面:https://docs.docker.com/engine/installation/linux/ubuntu/ 这个是ubuntu14.04 LTS需要的 $ sudo ...
- css3条纹边框效果
在线演示 本地下载
- web框架详解之tornado 一 模板语言以及框架本质
一.概要 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过 ...
- mini2440移植uboot 2014.04(一)
最新版的uboot添加了很多新功能,我决定在最新版代码基础上重新移植一遍加深理解. 我修改的代码已经上传到github上,地址:https://github.com/qiaoyuguo/u-boot- ...
- mac工作软件推荐-iterm + zsh + tmux
原文链接: http://ju.outofmemory.cn/entry/57244 tmux安装https://blog.csdn.net/nmgzywd/article/details/50915 ...
- listen and translation exercise 49
Huh? Appears to Be Universally Understood What's the most universal utterance in languages across th ...
- linux命令学习笔记(39):grep 命令
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来. grep全称是Global Regular Expression Print,表示全局正则表 ...