标签:style onclick fc7 -- hid 数论 false amp abd
1 --------------------------------------卢卡斯--------------------------- 2 ll mg(ll a,ll b,ll c) 3 { 4 ll ans=1; 5 a=a%c; 6 while(b) { 7 if(b&1) ans=ans*a%c; 8 a=a*a%c; 9 b>>=1; 10 } 11 return ans; 12 } 13 14 ll c(ll a,ll b) 15 { 16 if(a<b) return 0; 17 if(a==b) return 1; 18 if(b>a-b) b=a-b; 19 20 ll A=1,B=1; 21 for(ll i=0; i<b; ++i) { 22 A=(A*(a-i))%mod; 23 B=(B*(b-i))%mod; 24 } 25 return (A*mg(B,mod-2,mod))%mod; 26 27 } 28 29 ll lucas(ll n,ll m) 30 { 31 if(!m) return 1; 32 return c(n%mod,m%mod)*lucas(n/mod,m/mod)%mod; 33 } 34 --------------------------------------卢卡斯---------------------------
1 --------------------------------------扩欧---------------------------- 2 ll exgcd(ll l,ll r,ll &x1,ll &y1) 3 { 4 ll d,x2,y2; 5 if(r==0) { 6 x1=1; 7 y1=0; 8 return l; 9 } 10 d=exgcd(r,l%r,x2,y2); 11 x1=y2,y1=x2-l/r*y2; 12 return d; 13 } 14 --------------------------------------扩欧----------------------------
1 -------------------------------------扩欧求逆元----------------------------- 2 ll ni(ll a,ll n) 3 { 4 ll x,y; 5 if(exgcd(a,n,x,y)==1) return (x+n)%n; 6 else return -1; 7 } 8 -------------------------------------扩欧求逆元-----------------------------
1 -------------------------------------线性求逆元----------------------------- 2 ll ni() 3 { 4 ni[1]=1; 5 for(int i=2; i<=n; i++) 6 ni[i]=(p-(p/i))*ni[p%i]%p; 7 } 8 -------------------------------------线性求逆元-----------------------------
1 --------------------------------------素数测试------------------------ 2 ll mod-mul(ll a, ll b,ll n) 3 { 4 ll res=0; 5 while(b) { 6 if(b & 1) res=(res+a) % n; 7 a=(a+a)%n; 8 b>>=1; 9 } 10 return res; 11 } 12 13 ll pow-mod(ll a,ll b,ll n) 14 { 15 ll res=1; 16 while(b) { 17 if(b & 1) res=mod-mul(res, a, n); 18 a=mod-mul(a,a,n); 19 b>>=1; 20 } 21 return res; 22 } 23 24 bool test(ll n,ll a,ll d) 25 { 26 if(n==2) return true; 27 if(n==a) return false; 28 if(!(n&1)) return false; 29 while(!(d&1)) d>>=1; 30 ll t=pow-mod(a,d,n); 31 while(d!=n-1&&t!=n-1&&t!=1) { 32 t=t*t%n; 33 d<<=1; 34 } 35 return t==n-1||(d&1)==1; 36 } 37 38 bool isprime(ll n) 39 { 40 int a[]= {2,3,5,7}; 41 for(int i=0; i<=3; ++i) { 42 if(n==a[i]) return true; 43 if(!test(n,a[i],n-1)) return false; 44 } 45 return true; 46 } 47 --------------------------------------素数测试------------------------
1 --------------------------------------中国剩余定理-------------------- 2 int China(int B[],int W[],int k) 3 { 4 int i,d,x,y,ans=0,Mi,P=1; 5 for(i=1; i<=k; i++) P*=W[i]; 6 for(i=1; i<=k; i++) { 7 Mi=P/W[i]; 8 d=exgcd(Mi,W[i],x,y); 9 ans=(ans+xMiB[i])%P; 10 } 11 if(ans>0)return ans; 12 else return(ans+P); 13 } 14 --------------------------------------中国剩余定理--------------------
1 --------------------------------------直接计算欧拉函数---------------- 2 int euler(int x) 3 { 4 int i,ans= x, a = x; 5 for(i=2; i*i<= a; i++) { 6 if(a%i==0) { 7 ans=ans-ans/i; 8 while(a%i == 0) a=a/i; 9 } 10 } 11 if(a>1) ans=ans-ans/a; 12 return ans; 13 } 14 --------------------------------------直接计算欧拉函数-----------------
1 --------------------------------------线性筛--------------------------- 2 void getPhi() 3 { 4 int i,j,tot=0; 5 for(i=2; i<=N; i++) { 6 if(Mark[i]==false) { 7 Prime[++tot]=i; 8 phi[i]=i-1; 9 } 10 for(j=1; j<=tot && i*Prime[j]<=N; j++) { 11 Mark[i*Prime[ j]]=true; 12 if(i%Prime[ j]==0) { 13 phi[i*Prime[j]]=phi[i]*Prime[ j]; 14 break; 15 } else phi[i*Prime[j]]=phi[i]*(Prime[j]-1); 16 } 17 } 18 } 19 --------------------------------------线性筛---------------------------
1 ------------------------------扩展中国剩余定理------------------- 2 void exchina(ll c[],ll m[],ll n) 3 { 4 for (ll i=2; i<=n; ++i) { 5 ll m1=m[i-1],m2=m[i],c1=c[i-1],c2=c[i]; 6 ll gcd=exgcd(m1,m2,x,y); 7 if((c2-c1)%gcd) break; 8 m[i]=m1m2/gcd; 9 c[i]=ni(m1/gcd,m2/gcd)((c2-c1)/gcd)%(m2/gcd)*m1+c1; 10 c[i]=(c[i]%m[i]+m[i])%m[i]; 11 } 12 } 13 ------------------------------扩展中国剩余定理-------------------
标签:style onclick fc7 -- hid 数论 false amp abd
原文地址:https://www.cnblogs.com/KatouKatou/p/9500636.html