标签:while stream pow read res 集合 using 数论 open
数论的卢卡斯,据说可以只记结论啦啦啦啦。(反正我也不会~)
实际上也是好几个知识点的集合吧。
1。快速幂
ll pow(ll x,int y,int p){
ll ans=1;
x%=p;
for(int i=y;i;i>>=1,x=x*x%p) if(i&1) ans=ans*x%p;
return ans;
}
2。组合数求法
ll c(ll x,ll y){
if(y>x) return 0;
return (a[x]*pow(a[y],p-2,p))%p*pow(a[x-y],p-2,p)%p;
}
a【i】是%p意义下的i的阶乘。(一种鬼算法)
好像还跟逆元有关cm(a,b)=(a!/(a−b)!?)(p−2)mod p
逆元:a[i]=(p-p/i)*a[p%i](这个之前证过但懒得想了)
3。快读
inline int read(){
int f=1,x=0;char ch;
do{ch=getchar();if(ch==‘-‘)f=-1;}while(ch<‘0‘||ch>‘9‘);
do{x=x*10+ch-‘0‘;ch=getchar();}while(ch>=‘0‘&&ch<=‘9‘);
return f*x;
}
试着自己打来着,写炸了。
4。卢卡斯定理
Lucas(n,m,p)=c(n%p,m%p)*Lucas(n/p,m/p,p)
所以代码是
#include <iostream>
#include <cstdio>
//#define ll long long
using namespace std;
typedef long long ll;
ll a[100005];
int p;
ll pow(ll x,int y,int p){
ll ans=1;
x%=p;
for(int i=y;i;i>>=1,x=x*x%p) if(i&1) ans=ans*x%p;
return ans;
}
ll c(ll x,ll y){
if(y>x) return 0;
return (a[x]*pow(a[y],p-2,p))%p*pow(a[x-y],p-2,p)%p;
}
ll lucas(ll n,ll m){
if(!m) return 1;
return c(n%p,m%p)*lucas(n/p,m/p)%p;
}
inline int read(){
int f=1,x=0;char ch;
do{ch=getchar();if(ch==‘-‘)f=-1;}while(ch<‘0‘||ch>‘9‘);
do{x=x*10+ch-‘0‘;ch=getchar();}while(ch>=‘0‘&&ch<=‘9‘);
return f*x;
}
int main(){
int t=read();
while(t--){
int n=read(),m=read();p=read();//p一开始定义在里面了,结果调了半天
a[0]=1;
for(int i=1;i<=p;i++) a[i]=(a[i-1]*i)%p;
printf("%d\n",lucas(n+m,n));
}
return 0;
}
我肯定还会忘的。。。
标签:while stream pow read res 集合 using 数论 open
原文地址:https://www.cnblogs.com/jindui/p/11188721.html