组合数取余模板
2015-09-25 16:12
204 查看
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; ll mod_pow(ll x, ll n, ll p){ ll res = 1; while(n){ if(n & 1) res =res * x % p; x = x * x % p; n >>= 1; } return res; } ll comb(ll n, ll m, ll p){ if(m > n) return 0; ll ret = 1; m = min(n - m, m); for(int i = 1; i <= m; i ++){ ll a = (n + i - m) % p; ll b = i % p; ret = ret * (a * mod_pow(b, p - 2, p) % p) % p; } return ret; } ll Lucas(ll n, ll m, ll p){ if(m == 0) return 1; return comb(n % p, m % p, p) * Lucas(n / p, m / p, p) % p; } int main(){ int T; ll n, m, p; scanf("%d", &T); while(T--){ scanf("%I64d%I64d%I64d", &n, &m, &p); printf("%I64d\n", Lucas(n, m, p)); } return 0; }
相关文章推荐
- iOS Programming GitHub
- Integer to English Words
- Light oj 1140 - How Many Zeroes?(数位dp)
- 支付宝sdk集成,报系统繁忙 请稍后再试(ALI64)
- 支付宝sdk集成,报系统繁忙 请稍后再试(ALI64)
- MD5简单的登陆系统
- jQuery源码学习 之 ready与load事件 ---- day4
- 十佳Web开发资源
- ICA 分类语音分离
- vim插件之AutoComplPop和OmniCppComplete的安装和使用
- B2C电子商务系统研发——商品SKU分析和设计(二)
- android点击屏幕空白处隐藏软键盘
- Qt Webengine Widget常见问题
- Resource Shrinking去掉无用的资源
- c++中格式化输出。十六进制浮点数0x p输出问题
- java web 编解码
- node 单元测试实践
- NetJson 解析字符串
- UINavigationController方法pushViewController:参数不能传递UITabBarController原因猜想
- comet 推送消息到客户端