type1 $frac{x}{y}%P,其中P是大质数$
用费马小小定理得:
$y^{P-1}equiv 1(mod P)$
故:
$frac{x}{y}%P=frac{xy^{P-1}}{y}%P=xy^{P-2}%P$
type2 $frac{x}{y}%P,其中x和y可分解质因数$
我们还是用一些例子来讲比较好一些。
求卡特兰数$frac{C_{2n}^{n}}{n+1}%P$
$frac{C_{2n}^{n}}{n+1}%P$
$=frac{(n+2)times (n+3)times ...times (2n)}{1times 2times ...times (n-1)times n}%P$
一个直接的想法是分别将分子和分母分解质因数,但是这样写起来很恶心。
我们这样想:
将$n^{i}$分解质因数:
$n^{i}=(p_{1}^{k_{1}}times p_{2}^{k_{2}}times...times p_{t}^{k_{t}})^i$
我们任取n的一个质因子,不妨为$p_{1}$。
$n=(p_{1})^{i}times (p_{1}^{k_{1}-1}times p_{2}^{k_{2}}times...times p_{t}^{k_{t}})^{i}$
其实就是$p_{1}$多了i个,$p_{1}^{k_{1}-1}times p_{2}^{k_{2}}times...times p_{t}^{k_{t}}$多了i个。
我们可以交给$p_{1}$和$p_{1}^{k_{1}-1}times p_{2}^{k_{2}}times...times p_{t}^{k_{t}}$做。
如果n本来就是质数,直接快速幂。
找n的质因子可以用线性筛。
下面是bzoj1485代码,就是求$frac{C_{2n}^{n}}{n+1}%P$。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj
using namespace std;
typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP;
#define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define fill(a,l,r,v) fill(a+l,a+r+1,v)
#define re(i,a,b) for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define ire(i,x) for(typedef(x.begin()) i=x.begin();i!=x.end();i++)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define SF scanf
#define PF printf
#define two(k) (1<<(k))
template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;}
const DB EPS=1e-9;
inline int sgn(DB x){if(abs(x)<EPS)return 0;return(x>0)?1:-1;}
const DB Pi=acos(-1.0);
inline int gint()
{
int res=0;bool neg=0;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return 0;
if(z=='-'){neg=1;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
return (neg)?-res:res;
}
inline LL gll()
{
LL res=0;bool neg=0;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return 0;
if(z=='-'){neg=1;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
return (neg)?-res:res;
}
const int maxN=2000000;
int N;LL P;
LL ans;
inline LL power(LL a,LL k){LL x=1,y=a;while(k){if(k&1)x=x*y%P;k>>=1;y=y*y%P;}return x;}
int flag[maxN+100],cnt,prime[maxN+100];
int a[maxN+100];
LL b[maxN+100];
int main()
{
freopen("bzoj1485.in","r",stdin);
freopen("bzoj1485.out","w",stdout);
int i,j;
cin>>N>>P;
re(i,2,2*N)
{
if(!flag[i])prime[++cnt]=i;
for(j=1;j<=cnt && i*prime[j]<=2*N;j++)
{
flag[i*prime[j]]=1;
a[i*prime[j]]=prime[j];
if(i%prime[j]==0)break;
}
}
re(i,2,N)b[i]=-1;
re(i,N+2,2*N)b[i]=1;
ans=1;
red(i,2*N,2)
if(!flag[i])
ans=ans*power(LL(i),b[i])%P;
else
b[a[i]]+=b[i],b[i/a[i]]+=b[i];
cout<<ans<<endl;
return 0;
}
View Code
type 3
$frac{a}{b}%m=frac{a%bm}{b}$
原文链接: https://www.cnblogs.com/maijing/p/4711854.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/220324
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!