HDU 3037 Saving Beans(Lucas定理的直接应用)
2016-02-22 16:36
381 查看
解题思路:
直接求C(n+m , m) % p , 由于n , m ,p都非常大,所以要用Lucas定理来解决大组合数取模的问题。
直接求C(n+m , m) % p , 由于n , m ,p都非常大,所以要用Lucas定理来解决大组合数取模的问题。
#include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <cstdio> #include <stdlib.h> #include <time.h> #include <assert.h> #define LL long long #define FOR(i, x, y) for(int i=x;i<=y;i++) using namespace std; const int maxn = 100000 + 10; LL Pow_mod(LL a, LL b, LL mod) { LL ret = 1; while(b) { if(b & 1) ret = ret * a % mod; a = a * a % mod; b >>= 1; } return ret; } LL fac[maxn]; void init(LL p) { fac[0] = 1; FOR(i, 1, p) fac[i] = (fac[i-1] * i) % p; } LL Lucas(LL n, LL m, LL p) { LL ret = 1; while(n && m) { LL a = n % p, b = m % p; if(a < b) return 0; ret = (ret * fac[a] * Pow_mod(fac[b] * fac[a-b] % p , p - 2 , p)) % p; n /= p, m /= p; } return ret; } int main() { int T; scanf("%d",&T); while(T--) { int n, m, p; scanf("%d%d%d", &n, &m, &p); init(p); printf("%d\n", (int)Lucas(n + m, m, p)); } return 0; }
相关文章推荐
- 《Java程序设计》第四章-认识对象
- python+mysql抓取百度新闻的标题存到数据库
- quagga线程机制概述
- corosync+pacemaker+drbd实现web服务高可用
- dataTables-使用详细说明整理
- PeekInputStream
- hive安装配置与hive的JDBC (hadoop-0.20.2 + hive-0.7.0)
- h5之选项卡
- JAVA NIO概述(一):I/O模型
- ANDROID_MARS学习笔记_S05_004_过滤杂质,得到真正的加速度
- collections.defaultdict类的使用
- Linux NAT转发设定
- Cairo教程
- 自定义Xcode代码片段
- 值得收藏的文章(杂七杂八)
- php-fpm upstream sent too big header while reading response header from upstream
- cdh 不在 sudoers 文件中。此事将被报告。
- Tabbar上拉隐藏下拉显示
- 我的Android进阶之旅------>Java文件大小转换工具类 (B,KB,MB,GB,TB,PB之间的大小转换)
- 快速原型设计工具-Axure