【拓展Lucas】模板
求\(C_n^m \mod p\),写得太丑了qwq。
第一次写拓展Lucas竟然是在胡策的时候qwq写了两个半小时啊_(:з」∠)_还写挂了一个地方qwq
当然今天胡策我也是第一次写中国剩余定理(ˇˍˇ)
↑平时懒得动手的后果→_→
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int ipow2(int a, int b) {
int ret = 1, w = a;
while (b) {
if (b & 1) ret = ret * w;
w = w * w;
b >>= 1;
}
return ret;
}
int ipow(int a, int b, int c) {
int ret = 1, w = a;
while (b) {
if (b & 1) ret = 1ll * ret * w % c;
w = 1ll * w * w % c;
b >>= 1;
}
return ret;
}
int pic;
const int N = 100003;
struct data {int num, zhi;};
data work(int n, int p, int c) {
if (n == 1 || n == 0) return (data) {1, 0};
int ret1, las = n % pic, ret2 = 1;
for (int i = 1; i <= las; ++i) {
if (i % p == 0) continue;
ret2 = 1ll * ret2 * i % pic;
}
if (n >= pic) {
ret1 = ret2;
for (int i = las + 1; i < pic; ++i) {
if (i % p == 0) continue;
ret1 = 1ll * ret1 * i % pic;
}
ret1 = ipow(ret1, n / pic, pic);
} else
ret1 = 1;
int tim = n / p;
data s = work(n / p, p, c);
return (data) {1ll * ret1 * ret2 % pic * s.num % pic, tim + s.zhi};
}
void exgcd(int a, int b, ll &x, ll &y) {
if (b == 0) {x = 1; y = 0;}
else {
exgcd(b, a % b, x, y);
ll t = x;
x = y;
y = t - a / b * y;
}
}
int inv(int a, int b) {
ll x, y;
exgcd(a, b, x, y);
return (x % b + b) % b;
}
int Lucas(int n, int m, int p, int c) {
data s1, s2, s3;
pic = ipow2(p, c);
s1 = work(n, p, c);
s2 = work(m, p, c);
s3 = work(n - m, p, c);
int tim = s1.zhi - s2.zhi - s3.zhi;
int num1 = s1.num, num2 = s2.num, num3 = s3.num;
num2 = inv(num2, pic); num3 = inv(num3, pic);
num1 = 1ll * num1 * num2 % pic * num3 % pic;
for (int i = 1; i <= tim; ++i)
num1 = 1ll * num1 * p % pic;
return num1;
}
int prime[N], cnt = 0, cc[N], pc[N];
void Div(int p) {
int sq = ceil(sqrt(p));
for (int i = 2; i <= sq; ++i) {
if (p % i == 0) {
prime[++cnt] = i;
pc[cnt] = 1;
cc[cnt] = 0;
while (p % i == 0) {++cc[cnt]; p /= i; pc[cnt] *= i;}
}
}
if (p > 1) prime[++cnt] = p, cc[cnt] = 1, pc[cnt] = p;
}
int n, m, k, p, x;
int num[N];
int main() {
int T; scanf("%d", &T);
while (T--) {
scanf("%d%d%d%d", &n, &m, &k, &p);
cnt = 0;
Div(p);
for (int i = 1; i <= cnt; ++i)
num[i] = Lucas(n, m, prime[i], cc[i]);
x = 0;
for (int i = 1; i <= cnt; ++i)
(x += (1ll * (p / pc[i]) * inv((p / pc[i]) % pc[i], pc[i]) % p * num[i] % p)) %= p;
printf("%d\n", x);
}
return 0;
}
【拓展Lucas】模板的更多相关文章
- 拓展lucas结论及模板
lucas及其拓展 模板题 洛谷 P4720 本文侧向结论和代码实现, 推导请转至lucas定理及其拓展的推导 https://blog.csdn.net/yuyilahanbao/article/d ...
- BZOJ 3129 [SDOI2013]方程 (拓展Lucas)
题目大意:给定一个方程$X_{1}+X_{2}+X_{3}+X_{4}+...+X_{n}=M$,$\forall X_{i}<=A_{i} (i<=n1)$ $\forall X_{i} ...
- 数学:拓展Lucas定理
拓展Lucas定理解决大组合数取模并且模数为任意数的情况 大概的思路是把模数用唯一分解定理拆开之后然后去做 然后要解决的一个子问题是求模质数的k次方 将分母部分转化成逆元再去做就好了 这里贴一份别人的 ...
- 【bzoj2142】【礼物】拓展Lucas定理+孙子定理
(上不了p站我要死了,侵权度娘背锅) Description 一年一度的圣诞节快要来到了.每年的圣诞节小E都会收到许多礼物,当然他也会送出许多礼物.不同的人物在小E 心目中的重要性不同,在小E心中分量 ...
- nefu 84 ( 拓展欧几里德模板题 )
链接:传送门 思路:拓展欧几里德模板题,设大圣至少翻转 t 次,大圣起始位置为 x ,大圣目标位置为 y + n * s ( 大圣到达目标位置 y 可能需要多圈,所以用 s 来表示圈数 ),因为只能逆 ...
- 拓展Lucas小结
拓展Lucas是解决大组合数取模非质数(尤其是含平方因子的合数)问题的有力工具... 首先对模数质因数分解,把每个质因子单独拎出来处理答案,然后用中国剩余定理(excrt)合并 问题转化为,对于每个质 ...
- BZOJ2142: 礼物(拓展lucas)
Description 一年一度的圣诞节快要来到了.每年的圣诞节小E都会收到许多礼物,当然他也会送出许多礼物.不同的人物在小E 心目中的重要性不同,在小E心中分量越重的人,收到的礼物会越多.小E从商店 ...
- BZOJ2982: combination Lucas模板
2982: combination Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 734 Solved: 437[Submit][Status][Di ...
- 【BZOJ-2142】礼物 拓展Lucas定理
2142: 礼物 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1313 Solved: 541[Submit][Status][Discuss] ...
随机推荐
- 【uva11987】带删除的并查集
题意:初始有N个集合,分别为 1 ,2 ,3 .....n.有三种操件1 p q 合并元素p和q的集合2 p q 把p元素移到q集合中3 p 输出p元素集合的个数及全部元素的和. 题解: 并查集.只是 ...
- 【51NOD】独木舟
[算法]贪心 [题解]比较经典,用l,r两个定位指针分别从左右向中间推进. #include<cstdio> #include<algorithm> #include<c ...
- 20151024_003_C#基础知识(File / FileStream / StreamReader/StreamWriter)
1:绝对路径和相对路径 绝对路径:通过给定的路径直接能在我的电脑中找到这个文件. 相对路径:文件相对于应用程序的路径. 2:编码格式 乱码:产生乱码的原因,就是你保存这个文件所采用的编码,跟你打开这个 ...
- C++获取系统时间的方法
//首先是了解这个结构体,_SYSTEMTIME ,然后通过系统函数GetLocalTime往这个结构体的变量中写入当前系统时间typedef struct _SYSTEMTIME { WORD wY ...
- python进行机器学习(二)之特征选择
毫无疑问,解决一个问题最重要的是恰当选取特征.甚至创造特征的能力,这叫做特征选取和特征工程.对于特征选取工作,我个人认为分为两个方面: 1)利用python中已有的算法进行特征选取. 2)人为分析各个 ...
- MHA切换过程:
1.监测master的状态Ping(SELECT) succeeded, waiting until MySQL doesn't respond.. 2.当监控发现master异常时发出warning ...
- 根据日期查询年龄js
function ages(str) { var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/); if(r==null)return f ...
- leetcode 136 137 Single Number
题目描述(面试常考题) 借助了异或的思想 class Solution { public: int singleNumber(vector<int>& nums) { ; ; i ...
- Leetcode 之Count and Say(35)
很有意思的一道题,不好想啊. string getNext(string &s) { ]; ; stringstream ss; ; i < s.size(); i++) { if (s ...
- opencv python基本操作
Python usage crop frame: croppedframe = frame[ymin:ymax, xmin:xmax] resize frame: reszframe = cv2.re ...