[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 ...
随机推荐
- 2018年长沙理工大学第十三届程序设计竞赛 E 小木乃伊到我家 【最短路】
时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format: %lld 题目描述 AA的欧尼酱qwb是个考古学家,有一天qwb发 ...
- SQL语法结构
目录 一.增 1.增加字段 2.新建约束 二.删 1.删除字段 2.删除约束 三.改 1.修改字段 一.增 1.增加字段: ALTER TABLE [表名] ADD [字段名] NVARCHAR () ...
- 【leetcode刷题笔记】Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- JsonSchema使用详解
JSON Schema是一个用于验证JSON数据结构的强大工具, 我查看并学习了JSON Schema的官方文档, 做了详细的记录, 分享一下. 我们可以使用JSON Schema在后续做接口测试中做 ...
- python第六篇:Python复制超大文件、复制二进制文件
Python文件复制 # 写程序实现复制文件的功能 # 要求: # 1. 源文件路径和目标文件路径需要手动输入 # 2. 要考虑文件关闭的问题 # 3. 要考虑复制超大文件的问题 # 4. 要能复制二 ...
- Codeforces 402D Upgrading Array:贪心 + 数学
题目链接:http://codeforces.com/problemset/problem/402/D 题意: 给你一个长度为n的数列a[i],又给出了m个“坏质数”b[i]. 定义函数f(s),其中 ...
- Mysql5.5 InnoDB存储引擎配置和优化
环境为CentOS系统,1G内存,Mysql5.5.30.在/etc/my.cnf内添加: 复制代码代码如下: skip-external-lockingskip-name-resolvemax_co ...
- vue2.0项目实战使用axios发送请求
在Vue1.0的时候有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,官方就不再更新 vue-resource. 关于为什么放弃推荐? -> 尤 ...
- 防止未登录用户操作—struts2拦截器简单实现(转)
原文地址:http://blog.csdn.net/zhutulang/article/details/38351629 尊重原创,请访问原地址 一般,我们的web应用都是只有在用户登录之后才允许操作 ...
- 7_DoubleBuffer 游戏编程中的双缓存模式
### double buffer 双缓存 简单说: 当一个缓存被读取的时候,往另一个缓存里写入, 如此交替 #### the pattern 有两个缓存实例,一个是 current buffer, ...