数论——Lucas定理模板
2011-09-17 13:42
309 查看
Formulation
For non-negative integers m and n and a prime p, the following congruence relation holds:where
and
are the base p expansions of m and n respectively.
#include <iostream> #include <cstdio> #include <cstring> usingnamespace std; typedef longlong llg; constint N =150000; llg n, m, p, fac ; void init() { int i; fac[0] =1; for(i =1; i <= p; i++) fac[i] = fac[i-1]*i % p; } llg pow(llg a, llg b) { llg tmp = a % p, ans =1; while(b) { if(b &1) ans = ans * tmp % p; tmp = tmp*tmp % p; b >>=1; } return ans; } llg C(llg n, llg m) { if(m > n) return0; return fac *pow(fac[m]*fac[n-m], p-2) % p; } llg Lucas(llg n, llg m) { if(m ==0) return1; elsereturn (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); init(); printf("%I64d\n", Lucas(n+m, n)); } return0; }
相关文章推荐
- 【数论】Lucas定理推导过程+模板
- HDU3037——Saving Beans(数论,组合数取模,lucas定理模板)
- 数论-Lucas(卢卡斯定理)
- fzu 2020 Lucas 定理,组合数求模模板
- 教你一波Lucas(卢卡斯)定理在数论解题中的应用
- BZOJ 1951 [Sdoi2010] 古代猪文 数论 Lucas定理
- Lucas定理模板【bzoj2982】【combination】
- Lucas定理模板
- HDU3037Saving Beans 【组合数取模,即,LUCAS定理模板】
- BZOJ 1951 SDOI2010 古代猪文 数论 Lucas定理
- 洛谷.3807.[模板]卢卡斯定理(Lucas)
- 【BZOJ-4591】超能粒子炮·改 数论 + 组合数 + Lucas定理
- CPC23-4-K. 喵喵的神数 (数论 Lucas定理)
- 【Luogu3807】【模板】卢卡斯定理(数论)
- HDU - 5446 Unknown Treasure (数论部分定理的综合应用,lucas+CRT)
- LightOJ - 1067 lucas定理 模板
- lucas定理模板
- HDU3037Saving Beans(lucas定理模板)
- HDU 3037 Saving Beans(Lucas定理模板题)
- hdu 3037 Lucas定理模板