Lucas求解组合数模板
2016-06-14 20:22
323 查看
#include <iostream> #include <string.h> #include <stdio.h> using namespace std; typedef long long LL; LL n,m,p; LL quick_mod(LL a, LL b) { LL ans = 1; a %= p; while(b) { if(b & 1) { ans = ans * a % p; b--; } b >>= 1; a = a * a % p; } return ans; } LL C(LL n, LL m) { if(m > n) return 0; LL ans = 1; for(int i=1; i<=m; i++) { LL a = (n + i - m) % p; LL b = i % p; ans = ans * (a * quick_mod(b, p-2) % p) % p; } return ans; } LL Lucas(LL n, LL m) { if(m == 0) return 1; return C(n % p, m % p) * Lucas(n / p, m / p) % p; } int main() { int T; scanf("%d", &T); while(T--) { scanf("%I64d%I64d%I64d", &n, &m, &p); printf("%I64d\n", Lucas(n,m)); } return 0; } //求解组合数取模
相关文章推荐
- 变态组合数C(n,m)求解
- 关于Fibonacci算法及Lucas算法说明
- 蓝桥练习系统 历届试题 公式求值
- LightOj 1067(Lucas)
- HDU 4349(Lucas 变形)
- lightOJ 1067 - Combinations
- Bzoj1951 [Sdoi2010]古代猪文
- 组合(Lucas)
- 1951: [Sdoi2010]古代猪文
- BZOJ 4403: 序列统计|Lucas定理
- 【杭电】[4349]Xiao Ming's Hope
- FZU 2020 组合 lucas
- 【BZOJ3656】异或【扩展Lucas】【线性无关】
- hdu 4349(Lucas定理)
- fzu 2020 Lucas 定理,组合数求模模板
- 数论--Lucas Theorem
- HDU - 6129 Just do it(找规律)
- 2017百度之星初赛(B) 1001 Chess(思维+Lucas)
- Lucas定理(大数组合数取模)
- 2016 Multi-University Training Contest 6 解题报告