板子

不定期更。

 

数论

 1 const int pri[12]={2,3,5,7,11,13,17,19,23,29,31,37};
 2 ll ksc(ll x,ll y,ll mod){
 3     ll tmp=x*y-(ll)((ld)x/mod*y+0.5)*mod;
 4     return tmp<0?tmp+mod:tmp;
 5 }
 6 ll ksm(ll x,ll p,ll mod){
 7     ll ret=1;
 8     for (;p;p>>=1,x=ksc(x,x,mod)) if (p&1) ret=ksc(ret,x,mod);
 9     return ret;
10 }
11 bool MR(ll n){
12     if (n<=1) return 0;
13     rep (i,0,11) if (n==pri[i]) return 1;
14     ll d=n-1; int tmp=0;
15     while (!(d&1)) d>>=1,tmp++;
16     rep (i,0,11){
17         ll x=ksm(pri[i],d,n),p=x;
18         rep (t,1,tmp){
19             x=ksc(x,x,n);
20             if (x==1 && p!=1 && p!=n-1) return 0;
21             p=x;
22         }
23         if (x!=1) return 0;
24     }
25     return 1;
26 }
Miller Rabin
 1 vector<ll> V;
 2 namespace Rho{
 3     const int pri[12]={2,3,5,7,11,13,17,19,23,29,31,37};
 4     ull Rd;
 5     ll Rand(ll mod){return (Rd+=4179340454199820289ll)%mod;}
 6     ll ksc(ll x,ll y,ll mod){
 7         ll tmp=x*y-(ll)((ld)x/mod*y+0.5)*mod;
 8         return tmp<0?tmp+mod:tmp;
 9     }
10     ll ksm(ll x,ll p,ll mod){
11         ll ret=1;
12         for (;p;p>>=1,x=ksc(x,x,mod)) if (p&1) ret=ksc(ret,x,mod);
13         return ret;
14     }
15     bool MR(ll n){
16         if (n<=1) return 0;
17         rep (i,0,11) if (n==pri[i]) return 1;
18         ll d=n-1; int tmp=0;
19         while (!(d&1)) d>>=1,tmp++;
20         rep (i,0,11){
21             ll x=ksm(pri[i],d,n),p=x;
22             rep (t,1,tmp){
23                 x=ksc(x,x,n);
24                 if (x==1 && p!=1 && p!=n-1) return 0;
25                 p=x;
26             }
27             if (x!=1) return 0;
28         }
29         return 1;
30     }
31     ll f(ll x,ll c,ll mod){return (ksc(x,x,mod)+c)%mod;}
32     ll gcd(ll a,ll b){return !b?a:gcd(b,a%b);}
33     ll get(ll c,ll n){
34         ll x=Rand(n),y=f(x,c,n),p=n;
35         while (x!=y&&(p==n||p==1)){
36             p=gcd(n,x>y?x-y:y-x);
37             x=f(x,c,n); y=f(f(y,c,n),c,n);
38         }
39         return p;
40     }
41     void work(ll n){
42         if (n<=1) return;
43         if (MR(n)){V.push_back(n); return;}
44         while (1){
45             ll tmp=get(Rand(n-1)+1,n);
46             if (tmp!=n&&tmp!=1){work(tmp); work(n/tmp); return;}
47         }
48     }
49 }
Pollard Rho

 

字符串

 1 int n,all,rk[N],tp[N],t[N],sa[N],h[N],lg[N],mi[N][23]; char s[N];
 2 void js_sort(){
 3     rep (i,1,all) t[i]=0;
 4     rep (i,1,n) t[rk[tp[i]]]++;
 5     rep (i,1,all) t[i]+=t[i-1];
 6     for (int i=n;i;i--) sa[t[rk[tp[i]]]--]=tp[i];
 7 }
 8 void get_sa(){
 9     rep (i,1,n) rk[i]=s[i],tp[i]=i;
10     all=127; js_sort(); int w=1; all=1;
11     while (all<n){
12         int k=0;
13         rep (i,n-w+1,n) tp[++k]=i;
14         rep (i,1,n) if (sa[i]>w) tp[++k]=sa[i]-w;
15         js_sort();
16         rep (i,1,n) tp[i]=rk[i];
17         rk[sa[all=1]]=1;
18         rep (i,2,n) rk[sa[i]]=(tp[sa[i]]==tp[sa[i-1]]&&tp[sa[i]+w]==tp[sa[i-1]+w])?all:++all;
19         w<<=1;
20     }
21     int k=0;
22     rep (i,1,n){
23         if (k) k--; int j=sa[rk[i]-1];
24         for (;i+k<=n&&j+k<=n&&s[i+k]==s[j+k];k++);
25         h[rk[i]]=k;
26     }
27 }
28 void st_init(){
29     rep (i,1,n-1) mi[i][0]=h[i+1],lg[i]=i==1?0:lg[i>>1]+1;
30     rep (j,1,18)
31         for (int i=2;i+(1<<j-1)<n;i++)
32             mi[i][j]=min(mi[i][j-1],mi[i+(1<<j-1)][j-1]);
33 }
34 int lcp(int x,int y){
35     if (x==y) return n-x+1;
36     x=rk[x],y=rk[y];
37     if (x>y) swap(x,y); y--;
38     int t=lg[y-x+1];
39     return min(mi[x][t],mi[y-(1<<t)+1][t]);
40 }
SA
 1 //n=strlen(s+1)
 2 int n,k,las,tot,ch[N][26],fa[N],len[N],sz[N],t[N],q[N]; char s[N];
 3 void init(){
 4     las=tot=1;
 5 }
 6 void extend(int c){
 7     int p=las,np=las=++tot,q,nq; len[np]=len[p]+1; sz[np]=1;
 8     for (;p&&!ch[p][c];p=fa[p]) ch[p][c]=np;
 9     if (!p){fa[np]=1; return;}
10     q=ch[p][c];
11     if (len[q]==len[p]+1) fa[np]=q;
12     else{
13         nq=++tot; len[nq]=len[p]+1;
14         memcpy(ch[nq],ch[q],sizeof(ch[q]));
15         fa[nq]=fa[q]; fa[q]=fa[np]=nq;
16         for (;p&&ch[p][c]==q;p=fa[p]) ch[p][c]=nq;
17     }
18 }
19 void js_sort(){//基数排序
20     rep (i,1,n) t[i]=0;
21     rep (i,1,tot) t[len[i]]++;
22     rep (i,1,n) t[i]+=t[i-1];
23     rep (i,1,tot) q[t[len[i]]--]=i;
24 }
25 void get_right(){//计算right集合大小
26     for (int i=tot;i;i--){
27         int x=q[i];
28         sz[fa[x]]+=sz[x];
29     }
30 }
SAM
 1 int n,tot,las,ch[N][26],fa[N],len[N]; char s[N];
 2 int get_node(int l){
 3     rep (i,0,25) ch[tot][i]=0;
 4     len[tot]=l; return tot++;
 5 }
 6 int get_fail(int i,int x){
 7     while (s[i-len[x]-1]!=s[i]) x=fa[x];
 8     return x;
 9 }
10 void init(){
11     las=tot=0;
12     get_node(0); get_node(-1);
13     fa[0]=1;
14 }
15 void extend(int i,int c){
16     int x=get_fail(i,las),y;
17     if (!ch[x][c]){
18         y=get_node(len[x]+2);
19         fa[y]=ch[get_fail(i,fa[x])][c];
20         ch[x][c]=y;
21     }
22     las=ch[x][c];
23 }
PAM

 

二分图

 1 const int N=512;
 2 int n1,n2,m,a[N][N],match[N],vis[N],ans;
 3 
 4 bool dfs(int u){
 5     rep (v,1,n2) if (a[u][v]&&!vis[v]){
 6         vis[v]=1;
 7         if (!match[v]||dfs(match[v])) return match[v]=u,1;
 8     }
 9     return 0;
10 }
11 
12 int main(){
13 // Init
14     rep (i,1,n1){
15         memset(vis,0,(n2+1)<<2);
16         if (dfs(i)) ans++;
17     }
18 
19     return 0;
20 }
二分图最大匹配 - 匈牙利
 1 const int N=410;
 2 const ll inf=1e15+1;
 3 int n,n1,n2,m,sx[N],sy[N],match[N],L[N],R[N]; ll lx[N],ly[N],w[N][N],val[N];
 4 
 5 bool dfs(int u){
 6     sx[u]=1;
 7     rep (v,1,n) if (!sy[v]){
 8         ll tmp=lx[u]+ly[v]-w[u][v];
 9         if (tmp==0){
10             sy[v]=1,R[v]=u,L[match[v]]=v;
11             if (!match[v]||dfs(match[v])) return match[v]=u,1;
12         } else if (tmp<val[v]) R[v]=u,val[v]=tmp;
13     }
14     return 0;
15 }
16 
17 void km(){
18     rep (i,1,n){
19         lx[i]=-inf,ly[i]=match[i]=0;
20         rep (j,1,n) lx[i]=max(lx[i],w[i][j]);
21     }
22     rep (x,1,n){
23         rep (i,1,n) val[i]=inf,sx[i]=sy[i]=L[i]=R[i]=0;
24         int u,v=0;
25         for (u=x;u&&!dfs(u);L[u=match[v]]=v){
26             ll dlt=inf;
27             rep (i,1,n) if (!sy[i]&&val[i]<dlt) dlt=val[v=i];
28             rep (i,1,n){
29                 if (sx[i]) lx[i]-=dlt;
30                 if (sy[i]) ly[i]+=dlt; else val[i]-=dlt;
31             }
32             sy[v]=1;
33         }
34         while (u!=x) u=L[u],match[u]=R[u],u=R[u];
35     }
36 }
二分图最大权匹配 - KM

 

posted @ 2018-12-19 09:48  bestfy  阅读(391)  评论(0编辑  收藏  举报