数学:拓展Lucas定理
拓展Lucas定理解决大组合数取模并且模数为任意数的情况
大概的思路是把模数用唯一分解定理拆开之后然后去做
然后要解决的一个子问题是求模质数的k次方
将分母部分转化成逆元再去做就好了
- #include<bits/stdc++.h>
- using namespace std;
- const int maxn = + ;
- typedef long long LL;
- LL Pow(LL n, LL m, LL mod) {
- LL ans = ;
- while(m > ) {
- if(m & ) ans = (LL)ans * n % mod;
- n = (LL)n * n % mod; m >>= ;
- }
- return ans;
- }
- LL Pow(LL n,LL m) {
- LL ans = ;
- while(m > ) {
- if(m & ) ans = ans * n;
- n = n * n; m >>= ;
- }
- return ans;
- }
- LL x, y;
- LL exgcd(LL a, LL b) {
- if(a == ) {
- x = , y = ;
- return b;
- }LL r = exgcd(b%a, a);
- LL t = x; x = y - (b/a)*x; y = t;
- return r;
- }
- LL rev(LL a, LL b) { exgcd(a, b); return ((x % b) + b) % b; }
- LL Calc(LL n, LL p, LL t) {
- if(n == ) return ;
- LL s = Pow(p, t), k = n / s, tmp = ;
- for(LL i=; i<=s; i++) if(i % p) tmp = (LL)tmp * i % s;
- LL ans = Pow(tmp, k, s);
- for(LL i=s*k + ; i<=n; i++) if(i % p) ans = (LL)ans * i % s;
- return (LL)ans * Calc(n / p, p, t) % s;
- }
- LL C(LL n, LL m, LL p, LL t) {
- LL s = Pow(p, t), q = ;
- for(LL i=n; i; i/=p) q += i / p;
- for(LL i=m; i; i/=p) q -= i / p;
- for(LL i=n-m; i; i/=p) q -= i / p;
- LL ans = Pow(p, q);
- LL a = Calc(n, p, t), b = Calc(m, p, t), c = Calc(n-m, p, t);
- return (LL)(ans * a * rev(b, s) * rev(c, s)) % s;
- }
- LL China(LL A[], LL M[], LL cnt) {
- LL ans = , m, n = ;
- for(LL i=; i<=cnt; i++) n *= M[i];
- for(LL i=; i<=cnt; i++) {
- m = n / M[i];
- exgcd(M[i], m);
- ans = (ans + (LL)y * m * A[i]) % n;
- }
- return (ans + n) % n;
- }
- LL A[maxn], M[maxn], cnt;
- LL Lucas(LL n, LL m, LL mod) {
- for(LL i=; i*i <= mod; i++) if(mod % i == ) {
- LL t = ;
- while(mod % i == ) t++, mod /= i;
- M[++cnt] = Pow(i, t);
- A[cnt] = C(n, m, i, t);
- }if(mod > ) {
- M[++cnt] = mod;
- A[cnt] = C(n, m, mod, );
- }
- return China(A, M, cnt);
- }
- LL n, k, p;
- int main() {
- cin >> n >> k >> p;
- cout << Lucas(n, k, p) << endl;
- return ;
- }
然后补充一个内容,线性时间复杂度内求出所有的逆元
- A[i] = -(p / i) * A[p % i];
数学:拓展Lucas定理的更多相关文章
- 【bzoj2142】【礼物】拓展Lucas定理+孙子定理
(上不了p站我要死了,侵权度娘背锅) Description 一年一度的圣诞节快要来到了.每年的圣诞节小E都会收到许多礼物,当然他也会送出许多礼物.不同的人物在小E 心目中的重要性不同,在小E心中分量 ...
- 数学:lucas定理的总结
今天考试的题目中有大组合数取模,不会唉,丢了45分,我真是个弱鸡,现在还不会lucas. 所以今天看了一下,定理差不多是: (1)Lucas定理:p为素数,则有: 即:lucas(n,m,p)=c(n ...
- 数学:Lucas定理
利用Lucas定理解决大组合数取模 Lucas定理是用来求 C(n,m) mod p,p为素数的值.(注意:p一定是素数) Lucas定理用来解决大组合数求模是很有用的 Lucas定理最大的数据处理能 ...
- 【BZOJ-2142】礼物 拓展Lucas定理
2142: 礼物 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1313 Solved: 541[Submit][Status][Discuss] ...
- lucas定理及其拓展的推导
lucas定理及其拓展的推导 我的前一篇博客-- lucas定理 https://mp.csdn.net/mdeditor/100550317#主要是给出了lucas的结论和模板,不涉及推导. 本篇文 ...
- 『Lucas定理以及拓展Lucas』
Lucas定理 在『组合数学基础』中,我们已经提出了\(Lucas\)定理,并给出了\(Lucas\)定理的证明,本文仅将简单回顾,并给出代码. \(Lucas\)定理:当\(p\)为质数时,\(C_ ...
- 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其次,我们 ...
- [Swust OJ 247]--皇帝的新衣(组合数+Lucas定理)
题目链接:http://acm.swust.edu.cn/problem/0247/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
- 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其次,我们 ...
随机推荐
- EditText点击出现光标但不弹出软键盘
3.0以下版本可以用editText.setInputType(InputType.TYPE_NULL)来实现.或者设置editText.setKeyListener(null)来实现. 3.0以上版 ...
- 如何干净的卸载docker
先上服务器环境信息: 卸载的原因: 宿主机过段时间就磁盘100%了,导致continart异常退出,后来找了很多解决方案,才发现是安装docker的时候有个配置文件错误(正常的应该是|Storage ...
- muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制
目录 muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制 eventfd的使用 eventfd系统函数 使用示例 EventLoop对eventfd的封装 工作时序 runInLoo ...
- 微软职位内部推荐-Senior Dev Lead - SharePoint
微软近期Open的职位: SharePoint is a multi-billion dollar enterprise business that has grown from an on-prem ...
- 说说 Python 的变量以及简单数据类型
1 变量 先来看一个示例: news="我国第一个人工智能规划问世"print(news) 运行结果: 可以看出使用 Python 定义变量很简单,甚至都不需要指定变量的类型. 1 ...
- Estimation And Gain
Estimation: Almost every is spent on ergod the text and build the dictionary. Gains: I have never us ...
- 使用SSH过程中遇到的几个问题及解决方案
一.HTTP Status 500 - org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: B ...
- windows 64bit 服务器下安装32位oracle database 11g 问题集
1.中文乱码 问题描述: 利用vs2008调试的时候正常,发布到IIS8.5上的时候,当查询语句中包含中文的时候会乱码,比如"select * from tb where name='小s' ...
- DEP
DEP(Data execution protect)数据执行保护,这个功能需要操作系统和硬件的共同支持才可以生效.DEP的原理就是在系统的内存页中设置了一个标志位,标示这个内存页的属性(可执行). ...
- WebView 5.0+闪烁以及白屏问题完美解决
Android webView 在5.0+上启动硬件加速,造成部分手机出现闪烁.白屏等现象 必须写下这篇博客,遇到的问题搞了很久,百度谷歌就是没有完整的答案,记录下来,方便博友们: 需求:一个简单的W ...