【FZU2020】【Lucas模板题】【求组合数取模】
来源:互联网 发布:中国酒类行业产量数据 编辑:程序博客网 时间:2024/06/06 00:33
传送门:Problem 2020 组合
描述:
Problem 2020 组合
Accept: 969 Submit: 2348
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
给出组合数C(n,m), 表示从n个元素中选出m个元素的方案数。例如C(5,2) = 10, C(4,2) = 6.可是当n,m比较大的时候,C(n,m)很大!于是xiaobo希望你输出 C(n,m) mod p的值!
Input
输入数据第一行是一个正整数T,表示数据组数 (T <= 100)接下来是T组数据,每组数据有3个正整数 n, m, p (1 <= m <= n <= 10^9, m <= 10^4, m < p < 10^9, p是素数)
Output
对于每组数据,输出一个正整数,表示C(n,m) mod p的结果。
Sample Input
25 2 35 2 61
Sample Output
110
Source
FOJ有奖月赛-2011年04月(校赛热身赛)思路:
由于本题p比较大,所以组合数只能一个一个算,用未加预处理的lucas算就可以了
代码:
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#define pr(x) cout << #x << "= " << x << " " ;#define pl(x) cout << #x << "= " << x << endl;#define ll __int64using namespace std;template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar()); F && (num=-num);}ll n,m,p;ll pow_mod(ll x, ll n){ ll res=1; while(n>0){ if(n&1)res=res*x%p; x=x*x%p; n>>=1; } return res; } ll cm(ll n,ll m,ll mod){ if(m>n) return 0; ll i,ans=1,a,b; for(i=0; i<m; i++){ a=(n-i)%mod; b=(m-i)%mod; ans=ans*( a*pow_mod(b,mod-2)%mod )%mod; } return ans;}ll lucas(ll n,ll m,ll p){ if(m==0) return 1; return ( cm(n%p, m%p, p)*lucas(n/p, m/p, p) )%p;}int main(){ #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif int t; read(t); while(t--){ read(n);read(m);read(p); printf("%I64d\n", lucas(n, m, p)); } return 0;}
0 0
- 【FZU2020】【Lucas模板题】【求组合数取模】
- 求大组合数取模的模板(lucas定理)
- 【Lucas定理】FZU2020
- lucas定理求大数组合数取模
- HDU3037(Lucas定理求大组合数取模)
- Lucas求组合数
- fzu2020 组合,逆元
- FZU 2020 组合(组合数 lucas定理模板题)
- Lucas定理求组合数
- Lucas大组合数模板
- Lucas求解组合数模板
- 【模板】组合数求余--lucas定理
- hdu-3037(lucas定理求大组合数取模)
- HDU3037Saving Beans 【组合数取模,即,LUCAS定理模板】
- 【 Lucas定理 】 组合数取模
- Lucas定理---组合数取模
- 组合数取模(Lucas)
- lucas 定理,组合数取模
- 使用 Aircrack-ng 破解 WEP 和 WPA/WPA2 加密的 Wi-Fi 密码。
- 求Sn=a+aa+aaa+aaaa+aaaaa的前5项之和,其中a是一个数字
- String对象的常用方法
- 笔试笔记————判断单向链表是否存在循环
- 创建自己的内容提供器
- 【FZU2020】【Lucas模板题】【求组合数取模】
- TankWar 单机(JAVA版)版本2.1 坦克之间不能相互穿越
- Ext扩展原生JavaScript
- 打乱数组
- UVa - 11059 - Maximum Product(枚举)
- POJ1028 利用栈求解的问题
- Apache配置虚拟主机
- opencv3.0学习笔记【10/10】图像线性混合和ROI
- 通知的写法