拓展Lucas定理解决大组合数取模并且模数为任意数的情况

大概的思路是把模数用唯一分解定理拆开之后然后去做

然后要解决的一个子问题是求模质数的k次方

将分母部分转化成逆元再去做就好了

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = + ;
  4. typedef long long LL;
  5.  
  6. LL Pow(LL n, LL m, LL mod) {
  7. LL ans = ;
  8. while(m > ) {
  9. if(m & ) ans = (LL)ans * n % mod;
  10. n = (LL)n * n % mod; m >>= ;
  11. }
  12. return ans;
  13. }
  14. LL Pow(LL n,LL m) {
  15. LL ans = ;
  16. while(m > ) {
  17. if(m & ) ans = ans * n;
  18. n = n * n; m >>= ;
  19. }
  20. return ans;
  21. }
  22. LL x, y;
  23. LL exgcd(LL a, LL b) {
  24. if(a == ) {
  25. x = , y = ;
  26. return b;
  27. }LL r = exgcd(b%a, a);
  28. LL t = x; x = y - (b/a)*x; y = t;
  29. return r;
  30. }
  31. LL rev(LL a, LL b) { exgcd(a, b); return ((x % b) + b) % b; }
  32. LL Calc(LL n, LL p, LL t) {
  33. if(n == ) return ;
  34.  
  35. LL s = Pow(p, t), k = n / s, tmp = ;
  36. for(LL i=; i<=s; i++) if(i % p) tmp = (LL)tmp * i % s;
  37.  
  38. LL ans = Pow(tmp, k, s);
  39. for(LL i=s*k + ; i<=n; i++) if(i % p) ans = (LL)ans * i % s;
  40.  
  41. return (LL)ans * Calc(n / p, p, t) % s;
  42. }
  43. LL C(LL n, LL m, LL p, LL t) {
  44. LL s = Pow(p, t), q = ;
  45. for(LL i=n; i; i/=p) q += i / p;
  46. for(LL i=m; i; i/=p) q -= i / p;
  47. for(LL i=n-m; i; i/=p) q -= i / p;
  48.  
  49. LL ans = Pow(p, q);
  50. LL a = Calc(n, p, t), b = Calc(m, p, t), c = Calc(n-m, p, t);
  51. return (LL)(ans * a * rev(b, s) * rev(c, s)) % s;
  52. }
  53. LL China(LL A[], LL M[], LL cnt) {
  54. LL ans = , m, n = ;
  55. for(LL i=; i<=cnt; i++) n *= M[i];
  56. for(LL i=; i<=cnt; i++) {
  57. m = n / M[i];
  58. exgcd(M[i], m);
  59. ans = (ans + (LL)y * m * A[i]) % n;
  60. }
  61. return (ans + n) % n;
  62. }
  63. LL A[maxn], M[maxn], cnt;
  64. LL Lucas(LL n, LL m, LL mod) {
  65. for(LL i=; i*i <= mod; i++) if(mod % i == ) {
  66. LL t = ;
  67. while(mod % i == ) t++, mod /= i;
  68. M[++cnt] = Pow(i, t);
  69. A[cnt] = C(n, m, i, t);
  70. }if(mod > ) {
  71. M[++cnt] = mod;
  72. A[cnt] = C(n, m, mod, );
  73. }
  74. return China(A, M, cnt);
  75. }
  76. LL n, k, p;
  77. int main() {
  78. cin >> n >> k >> p;
  79. cout << Lucas(n, k, p) << endl;
  80. return ;
  81. }

然后补充一个内容,线性时间复杂度内求出所有的逆元

  1. A[i] = -(p / i) * A[p % i];

数学:拓展Lucas定理的更多相关文章

  1. 【bzoj2142】【礼物】拓展Lucas定理+孙子定理

    (上不了p站我要死了,侵权度娘背锅) Description 一年一度的圣诞节快要来到了.每年的圣诞节小E都会收到许多礼物,当然他也会送出许多礼物.不同的人物在小E 心目中的重要性不同,在小E心中分量 ...

  2. 数学:lucas定理的总结

    今天考试的题目中有大组合数取模,不会唉,丢了45分,我真是个弱鸡,现在还不会lucas. 所以今天看了一下,定理差不多是: (1)Lucas定理:p为素数,则有: 即:lucas(n,m,p)=c(n ...

  3. 数学:Lucas定理

    利用Lucas定理解决大组合数取模 Lucas定理是用来求 C(n,m) mod p,p为素数的值.(注意:p一定是素数) Lucas定理用来解决大组合数求模是很有用的 Lucas定理最大的数据处理能 ...

  4. 【BZOJ-2142】礼物 拓展Lucas定理

    2142: 礼物 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1313  Solved: 541[Submit][Status][Discuss] ...

  5. lucas定理及其拓展的推导

    lucas定理及其拓展的推导 我的前一篇博客-- lucas定理 https://mp.csdn.net/mdeditor/100550317#主要是给出了lucas的结论和模板,不涉及推导. 本篇文 ...

  6. 『Lucas定理以及拓展Lucas』

    Lucas定理 在『组合数学基础』中,我们已经提出了\(Lucas\)定理,并给出了\(Lucas\)定理的证明,本文仅将简单回顾,并给出代码. \(Lucas\)定理:当\(p\)为质数时,\(C_ ...

  7. Lucas定理学习小记

    (1)Lucas定理:p为素数,则有: (2)证明: n=(ak...a2,a1,a0)p = (ak...a2,a1)p*p + a0 =  [n/p]*p+a0,m=[m/p]*p+b0其次,我们 ...

  8. [Swust OJ 247]--皇帝的新衣(组合数+Lucas定理)

    题目链接:http://acm.swust.edu.cn/problem/0247/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...

  9. Lucas定理学习(进阶中)

    (1)Lucas定理:p为素数,则有: (2)证明: n=(ak...a2,a1,a0)p = (ak...a2,a1)p*p + a0 =  [n/p]*p+a0,m=[m/p]*p+b0其次,我们 ...

随机推荐

  1. EditText点击出现光标但不弹出软键盘

    3.0以下版本可以用editText.setInputType(InputType.TYPE_NULL)来实现.或者设置editText.setKeyListener(null)来实现. 3.0以上版 ...

  2. 如何干净的卸载docker

    先上服务器环境信息: 卸载的原因: 宿主机过段时间就磁盘100%了,导致continart异常退出,后来找了很多解决方案,才发现是安装docker的时候有个配置文件错误(正常的应该是|Storage ...

  3. muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制

    目录 muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制 eventfd的使用 eventfd系统函数 使用示例 EventLoop对eventfd的封装 工作时序 runInLoo ...

  4. 微软职位内部推荐-Senior Dev Lead - SharePoint

    微软近期Open的职位: SharePoint is a multi-billion dollar enterprise business that has grown from an on-prem ...

  5. 说说 Python 的变量以及简单数据类型

    1 变量 先来看一个示例: news="我国第一个人工智能规划问世"print(news) 运行结果: 可以看出使用 Python 定义变量很简单,甚至都不需要指定变量的类型. 1 ...

  6. Estimation And Gain

    Estimation: Almost every is spent on ergod the text and build the dictionary. Gains: I have never us ...

  7. 使用SSH过程中遇到的几个问题及解决方案

    一.HTTP Status 500 - org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: B ...

  8. windows 64bit 服务器下安装32位oracle database 11g 问题集

    1.中文乱码 问题描述: 利用vs2008调试的时候正常,发布到IIS8.5上的时候,当查询语句中包含中文的时候会乱码,比如"select * from tb where name='小s' ...

  9. DEP

    DEP(Data execution protect)数据执行保护,这个功能需要操作系统和硬件的共同支持才可以生效.DEP的原理就是在系统的内存页中设置了一个标志位,标示这个内存页的属性(可执行). ...

  10. WebView 5.0+闪烁以及白屏问题完美解决

    Android webView 在5.0+上启动硬件加速,造成部分手机出现闪烁.白屏等现象 必须写下这篇博客,遇到的问题搞了很久,百度谷歌就是没有完整的答案,记录下来,方便博友们: 需求:一个简单的W ...