http://www.lydsy.com/JudgeOnline/problem.php?id=3656

大意:经过一通推导,问题变成求$$\binom N M \mod P$$,其中N,M<=1e9, P<=1e5,P可以是合数。

参考这位神犇的博客:http://blog.csdn.net/braketbn/article/details/50752153

作为一个蒟蒻,稍微写一点自己的理解...

如果P是素数,我们可以用lucas定理直接解决,那么P是合数应该怎么办呢?

首先,考虑把P分解质因数,拆成$${p_0} ^ {k_0} {p_1}^{k_1} {p_2} ^ {k_2}...$$的形式,再对每一个p^k计算,最后用CRT(中国剩余定理)合并。

然后就是怎么算组合数对质数的幂取模的问题了。我们设$$f(n)=\prod_{i=1}^n i[i\mod p\ne0]\mod {pk}$$,就是1~n中非p的倍数的积对pk取模,那么只要先把n!,m!,(n-m)!里的p的倍数的因子p全部提出来,然后求出f(n),f(m),f(n-m)的值就行了。

求f(n)可以用递归,把n按照p^k分段,整段的可以一起算,零散的一段直接暴力计算,然后乘上f(n/p),表示所有的p的倍数除以p后f的值。总的时间复杂度$$O(P log N)$$。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define TR(x) printf(#x"=%d\n", x)
const int MAXN=100005;
int f[MAXN];
int p[15], k[15], pk[15], s[15], phi[15], np;
int mpow(int a,int b,int m){int r=1;for(;b;b>>=1,a=(ll)a*a%m)if(b&1)r=(ll)r*a%m;return r;}
int calc(int n, int x){
if(!n) return 1;
int q=pk[x], t=n/q, r=1;
if(t){
for(int i=1; i<q; ++i) if(i%p[x]) r=(ll)r*i%q;
r=mpow(r,t,q);
}
for(int i=n%q; i>=1; --i) if(i%p[x]) r=(ll)r*i%q;
return (ll)r*calc(n/p[x], x)%pk[x];
}
int cntp(int n, int p){
int r=0;
while(n) r+=(n/=p);
return r;
}
int main(){
int n, m, P, ans=0;
scanf("%d%d%d", &n, &m, &P);
for(int i=2, tp=P; i<=tp; ++i) if(tp%i==0){
p[np]=i; pk[np]=1;
while(tp%i==0) tp/=i, k[np]++, pk[np]*=i;
phi[np]=pk[np]/i*(i-1); np++;
}
for(int i=0; i<np; ++i){
int t=cntp(n,p[i])-cntp(m,p[i])-cntp(n-m,p[i]);
s[i]=mpow(p[i],t,pk[i]);
s[i]=(ll)s[i]*calc(n,i)%pk[i];
s[i]=(ll)s[i]*mpow(calc(m,i),phi[i]-1,pk[i])%pk[i];
s[i]=(ll)s[i]*mpow(calc(n-m,i),phi[i]-1,pk[i])%pk[i];
}
for(int i=0; i<np; ++i){
ans=(ans+(ll)s[i]*(P/pk[i])%P*mpow(P/pk[i],phi[i]-1,pk[i])%P)%P;
}
printf("%d\n", ans);
return 0;
}

BZOJ 3656: 异或 (组合数取模 CRT)的更多相关文章

  1. BZOJ_2142_礼物_扩展lucas+组合数取模+CRT

    BZOJ_2142_礼物_扩展lucas+组合数取模 Description 一年一度的圣诞节快要来到了.每年的圣诞节小E都会收到许多礼物,当然他也会送出许多礼物.不同的人物在小E 心目中的重要性不同 ...

  2. [BZOJ 3129] [Sdoi2013] 方程 【容斥+组合数取模+中国剩余定理】

    题目链接:BZOJ - 3129 题目分析 使用隔板法的思想,如果没有任何限制条件,那么方案数就是 C(m - 1, n - 1). 如果有一个限制条件是 xi >= Ai ,那么我们就可以将 ...

  3. 组合数取模(lucas定理+CRT合并)(AC)

    #include<bits/stdc++.h> #define re register #define int long long using namespace std; ; inlin ...

  4. 组合数取模Lucas定理及快速幂取模

    组合数取模就是求的值,根据,和的取值范围不同,采取的方法也不一样. 下面,我们来看常见的两种取值情况(m.n在64位整数型范围内) (1)  , 此时较简单,在O(n2)可承受的情况下组合数的计算可以 ...

  5. 排列组合+组合数取模 HDU 5894

    // 排列组合+组合数取模 HDU 5894 // 题意:n个座位不同,m个人去坐(人是一样的),每个人之间至少相隔k个座位问方案数 // 思路: // 定好m个人 相邻人之间k个座位 剩下就剩n-( ...

  6. hdu 3944 DP? 组合数取模(Lucas定理+预处理+帕斯卡公式优化)

    DP? Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0 ...

  7. lucas定理解决大组合数取模

    LL MyPow(LL a, LL b) { LL ret = ; while (b) { ) ret = ret * a % MOD; a = a * a % MOD; b >>= ; ...

  8. 2015 ICL, Finals, Div. 1 Ceizenpok’s formula(组合数取模,扩展lucas定理)

    J. Ceizenpok’s formula time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  9. 组合数取模&&Lucas定理题集

    题集链接: https://cn.vjudge.net/contest/231988 解题之前请先了解组合数取模和Lucas定理 A : FZU-2020  输出组合数C(n, m) mod p (1 ...

随机推荐

  1. JS内存空间详细图解

    JS内存空间详细图解 变量对象与堆内存 var a = 20; var b = 'abc'; var c = true; var d = { m: 20 } 因为JavaScript具有自动垃圾回收机 ...

  2. BZOJ5329: [SDOI2018]战略游戏——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=5329 https://www.luogu.org/problemnew/show/P4606 省选 ...

  3. 第一次BC

    BestCoder Round #90 1001 Kblack loves flag 太弱只写了这一道水题. 首先这个题面就是,完全不知道它在说什么.开始5mins后我还完全不知道这个题想要表达什么. ...

  4. bzoj2326:[HNOI2011]数学作业(分段矩阵乘法)

    题目大意:输入n(n<=10^18)和m,将1~n的整数连起来模m输出,比如n=13则输出12345678910111213模m的数. 设f[i]为1~i整数连起来模m的数,i的位数为k,则有f ...

  5. HDU1828线段树(扫描线)

    Picture Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  6. Apache Flume - File通道设计

    原文链接:https://blogs.apache.org/flume/entry/apache_flume_filechannel 说明:翻译在尽量符合原文表达的基础上,尽量保证行文流畅.水平有限, ...

  7. CSS3知识之阴影box-shadow

    一.定义和用法 box-shadow 属性向框添加一个或多个阴影. box-shadow: h-shadow v-shadow blur spread color inset; h-shadow   ...

  8. 51Nod 1004 n^n末尾数字 | 快速幂

    #include "bits/stdc++.h" using namespace std; #define LL long long #define INF 0x3f3f3f3f3 ...

  9. python读文件和写入文件复习

    with open("name.txt",'r') as read_file: for name in read_file: list_name = (name.split(',' ...

  10. Spring Security 集成CAS实现单点登录

    参考:http://elim.iteye.com/blog/2270446 众所周知,Cas是对单点登录的一种实现.本文假设读者已经了解了Cas的原理及其使用,这些内容在本文将不会讨论.Cas有Ser ...