ahhhh...终于处理完基础数论了...码了少量代码...
1 int GCD(int a,int b) 2 { 3 return !b?a:GCD(b,a%b); 4 }
void exgcd(int a,int b,int &x,int &y) { if(b==0) { x=1,y=0;return; } exgcd(b,a%b,x,y); int t=x;x=y;y=t-a/b*y; }
1 int GCD(int a,int b) 2 { 3 return !b?a:GCD(b,a%b); 4 } 5 int lcm(int a,int b) 6 { 7 return a*b/GCD(a,b); 8 }
1 int FastPower(int Tp,int Up,int mod) 2 { 3 int r=1,base=Tp; 4 while(b) 5 { 6 if(b&1) 7 r*=base; 8 base*=base; 9 b>>1; 10 } 11 return r; 12 }
1 int prime[1100000],primesize,phi[11000000]; 2 bool isprime[11000000]; 3 void getlist(int listsize) 4 { 5 memset(isprime,1,sizeof(isprime)); 6 isprime[1]=false; 7 for(int i=2;i<=listsize;i++) 8 { 9 if(isprime[i])prime[++primesize]=i; 10 for(int j=1;j<=primesize&&i*prime[j]<=listsize;j++) 11 { 12 isprime[i*prime[j]]=false; 13 if(i%prime[j]==0)break; 14 } 15 } 16 }
1 int getphi(int x){ 2 int ret=1; 3 for(int i=1;prime[i]*prime[i]<=x;i++){ 4 if(x%prime[i]==0){ 5 ret*=prime[i]-1;x/=prime[i]; 6 while(x%prime[i]==0) x/=prime[i],ret*=prime[i]; 7 } 8 } 9 if(x>1) ret*=x-1; 10 return ret; 11 }
待填的坑:
BSGS.
More Template(Packaged)...
1 #define N 1000000 2 struct SPFA 3 { 4 queue<int> q; 5 bool vis[N]; 6 int dis[N]; 7 SPFA() 8 { 9 memset(vis,0,sizeof(vis)); 10 memset(dis,0,sizeof(dis)); 11 } 12 void spfa(int p) 13 { 14 q.push(p); 15 vis[p]=true; 16 while(!q.empty()) 17 { 18 int t=q.front(); 19 q.pop(); 20 vis[t]=false; 21 for(int i=1; i<=n; i++) 22 { 23 if(dis[i]>dis[t]+map[t][i]) 24 { 25 dis[i]=dis[t]+map[t][i]; 26 if(!vis[i]) 27 q.push(i); 28 } 29 } 30 } 31 printf("%d",dis[ep]); 32 } 33 };
1 struct BIT{ 2 long long c[N]; 3 int n; 4 BIT() 5 { 6 memset(c,0,sizeof(c)); 7 n=0; 8 } 9 10 int lowbit(int x) 11 { 12 return x&(-x); 13 } 14 void modify(int x,long long y) 15 { 16 for(;x<=n;x+=lowbit(x)) 17 c[x]+=y; 18 } 19 long long query(int x) 20 { 21 long long ret=0; 22 for(;x;x-=lowbit(x)) 23 ret+=c[x]; 24 return ret; 25 } 26 long long query(int l,int r) 27 { 28 return query(r)-query(l-1); 29 } 30 };
1 #define Maxn 1000000 2 #define Maxm 1000 3 struct KMP{ 4 int pre[Maxn]; 5 int len1,len2; 6 int p; 7 8 KMP() 9 { 10 for(int i=0;i<Maxn;i++) 11 pre[i]=0; 12 } 13 14 inline void preprocess(char*pattern) 15 { 16 len2=strlen(pattern),p=-1; 17 pre[0]=-1; 18 for(int i=1;i<len2;i++) 19 { 20 while(p!=-1&&pattern[p+1]!=pattern[i]) 21 p=pre[p]; 22 p+=(pattern[p+1]==pattern[i]); 23 pre[i]=p; 24 } 25 } 26 27 inline void kmp(char*original,char*pattern) 28 { 29 len1=strlen(original); 30 p=-1; 31 for(int i=0;i<len1;i++) 32 { 33 while(p!=-1&&original[i]!=pattern[p+1]) 34 p=pre[p]; 35 p+=(pattern[p+1]==original[i]); 36 if(p+1==len2) 37 printf("%d ",i-len2+2),p=pre[p]; 38 } 39 for(int i=0;i<len2;i++) 40 printf("%d ",pre[i]+1); 41 } 42 };
1 struct SegmentTreeTypeAdd 2 { 3 struct node 4 { 5 int l,r,w,f;//left,right,weight,flag; 6 node() 7 { 8 l=r=w=f=0; 9 } 10 } tree[400001]; 11 inline void build(int k,int ll,int rr)//建树 12 { 13 //用法:build(节点编号,左孩子,右孩子); 14 //初始化:build(1,1,节点个数); 15 tree[k].l=ll,tree[k].r=rr; 16 if(tree[k].l==tree[k].r) 17 { 18 scanf("%d",&tree[k].w); 19 return; 20 } 21 int m=(ll+rr)/2; 22 build(k*2,ll,m); 23 build(k*2+1,m+1,rr); 24 tree[k].w=tree[k*2].w+tree[k*2+1].w; 25 } 26 inline void down(int k)//标记下传 27 { 28 //用法:down(需要下传标记的根节点); 29 tree[k*2].f+=tree[k].f; 30 tree[k*2+1].f+=tree[k].f; 31 tree[k*2].w+=tree[k].f*(tree[k*2].r-tree[k*2].l+1); 32 tree[k*2+1].w+=tree[k].f*(tree[k*2+1].r-tree[k*2+1].l+1); 33 tree[k].f=0; 34 } 35 inline void ask_point(int k)//单点查询 36 { 37 //用法:ask_point(需要查询的点的编号); 38 if(tree[k].l==tree[k].r) 39 { 40 ans=tree[k].w; 41 return ; 42 } 43 if(tree[k].f) down(k); 44 int m=(tree[k].l+tree[k].r)/2; 45 if(x<=m) ask_point(k*2); 46 else ask_point(k*2+1); 47 } 48 inline void change_point(int k)//单点修改 49 { 50 //用法:change_point(需要修改的点的编号); 51 if(tree[k].l==tree[k].r) 52 { 53 tree[k].w+=y; 54 return; 55 } 56 int m=(tree[k].l+tree[k].r)/2; 57 if(x<=m) change_point(k*2); 58 else change_point(k*2+1); 59 tree[k].w=tree[k*2].w+tree[k*2+1].w; 60 } 61 inline void ask_interval(int k)//区间查询 62 { 63 //用法:ask_iterval(查询起点); 64 if(tree[k].l>=a&&tree[k].r<=b)//a与b为需要查询的区间 65 { 66 ans+=tree[k].w; 67 return; 68 } 69 if(tree[k].f) down(k); 70 int m=(tree[k].l+tree[k].r)/2; 71 if(a<=m) ask_interval(k*2); 72 if(b>m) ask_interval(k*2+1); 73 } 74 inline void change_interval(int k)//区间修改 75 { 76 //用法:change_interval(修改起点); 77 if(tree[k].l>=a&&tree[k].r<=b)//a与b为需要修改的区间 78 { 79 tree[k].w+=(tree[k].r-tree[k].l+1)*y; 80 tree[k].f+=y; 81 return; 82 } 83 if(tree[k].f) down(k);//若有孩子节点,下传标记 84 int m=(tree[k].l+tree[k].r)/2;//二分处理 85 if(a<=m) change_interval(k*2); 86 if(b>m) change_interval(k*2+1); 87 tree[k].w=tree[k*2].w+tree[k*2+1].w; 88 } 89 };
1 #define N 1000000 2 struct Graph 3 { 4 struct node 5 { 6 int next,to,dis; 7 } edge[N]; 8 int head[N]; 9 void add(int u,int v,int w) 10 { 11 edge[++cnt].next=head[u]; 12 edge[cnt].to=v; 13 edge[cnt].dis=w; 14 head[u]=cnt; 15 } 16 }; 17 Graph g; 18 struct Dijkstra 19 { 20 struct NODE 21 { 22 int x,y; 23 bool operator < (NODE a)const 24 { 25 return x>a.x; 26 } 27 }; 28 priority_queue<NODE>q; 29 int cnt,n,m,s,t,dis[N/2]; 30 bool visit[N/2]; 31 void dijkstra() 32 { 33 memset(dis,1,sizeof(dis)); 34 dis[s]=0; 35 NODE a; 36 a.x=dis[s]; 37 a.y=s; 38 q.push(a); 39 while(!q.empty()) 40 { 41 NODE a=q.top(); 42 q.pop(); 43 if(visit[a.x]) continue; 44 int v=a.y; 45 visit[v]=1; 46 for(int i=g.head[v]; i; i=g.edge[i].next) 47 { 48 if(dis[g.edge[i].to]>g.edge[i].dis+dis[v]) 49 { 50 dis[g.edge[i].to]=g.edge[i].dis+dis[v]; 51 NODE a; 52 a.x=dis[g.edge[i].to]; 53 a.y=g.edge[i].to; 54 q.push(a); 55 } 56 } 57 } 58 printf("%d",dis[t]); 59 } 60 };
1 #define N 1000000 2 struct Graph 3 { 4 struct node 5 { 6 int next,to,dis; 7 } edge[N<<1]; 8 int head[N/2]; 9 void add(int u,int v,int w) 10 { 11 edge[++cnt].next=head[u]; 12 edge[cnt].to=v; 13 edge[cnt].dis=w; 14 head[u]=cnt; 15 } 16 };
1 #define maxn 10005 2 struct UnionFindSet 3 { 4 int x,y,v; 5 int fat[maxn]; 6 UnionFindSet() 7 { 8 for(int i=0; i<maxn; i++) 9 fat[i]=i; 10 } 11 inline int father(int x) 12 { 13 if(fat[x]!=x) 14 fat[x]=father(fat[x]); 15 return fat[x]; 16 } 17 inline void unionn(int x,int y) 18 { 19 int fa=father(x); 20 int fb=father(y); 21 if(fa!=fb) 22 fat[fa]=fb; 23 } 24 }; 25 struct Edge 26 { 27 int pre,to,w; 28 bool operator < (const Edge &b) const 29 { 30 return this->w < b.w; 31 } 32 }; 33 struct Graph 34 { 35 Edge edge[maxn]; 36 int cnt=0; 37 inline void AddEdge(int u,int v,int w) 38 { 39 edge[++cnt].pre=u; 40 edge[cnt].to=v; 41 edge[cnt].w=w; 42 } 43 }; 44 Graph g; 45 UnionFindSet s; 46 int Kruskal(int EdgeNumber) 47 { 48 sort(g.edge+1,g.edge+1+EdgeNumber); 49 int n=1,ans=0; 50 while(n<EdgeNumber-1) 51 { 52 if(s.father(g.edge[n].pre)!=s.father(g.edge[n].to)) 53 { 54 s.unionn(g.edge[n].pre,g.edge[n].to); 55 ans+=g.edge[n].w; 56 } 57 n++; 58 } 59 return ans; 60 }
1 #define maxn 10005 2 struct UnionFindSet 3 { 4 int x,y,v; 5 int fat[maxn]; 6 UnionFindSet() 7 { 8 for(int i=0; i<maxn; i++) 9 fat[i]=i; 10 } 11 inline int father(int x) 12 { 13 if(fat[x]!=x) 14 fat[x]=father(fat[x]); 15 return fat[x]; 16 } 17 inline void unionn(int x,int y) 18 { 19 int fa=father(x); 20 int fb=father(y); 21 if(fa!=fb) 22 fat[fa]=fb; 23 } 24 };
hhhh...上面的板子编译都不一定过...得手调一下...
下面是splay&&treap&&dinic板子...编译过了...
1 #include<cstdio> 2 #define N 100010 3 using namespace std; 4 int root,nn,n,tot; 5 class SPLAY 6 { 7 private: 8 int fa[N],ch[N][2],siz[N],cnt[N]; 9 10 int son(int x) 11 { 12 return x==ch[fa[x]][1]; 13 } 14 15 void pushup(int rt) 16 { 17 int l=ch[rt][0],r=ch[rt][1]; 18 siz[rt]=siz[l]+siz[r]+cnt[rt]; 19 } 20 21 void rotate(int x) 22 { 23 int y=fa[x],z=fa[y],b=son(x),c=son(y),a=ch[x][!b]; 24 if(z) ch[z][c]=x; 25 else root=x; 26 fa[x]=z; 27 if(a) fa[a]=y; 28 ch[y][b]=a; 29 ch[x][!b]=y; 30 fa[y]=x; 31 pushup(y); 32 pushup(x); 33 } 34 35 void splay(int x,int i) 36 { 37 while(fa[x]!=i) 38 { 39 int y=fa[x],z=fa[y]; 40 if(z==i) 41 { 42 rotate(x); 43 } 44 else 45 { 46 if(son(x)==son(y)) 47 { 48 rotate(y); 49 rotate(x); 50 } 51 else 52 { 53 rotate(x); 54 rotate(x); 55 } 56 } 57 } 58 } 59 public: 60 int data[N]; 61 void ins(int &rt,int x) 62 { 63 if(rt==0) 64 { 65 rt=++nn; 66 data[nn]=x; 67 siz[nn]=cnt[nn]=1; 68 return; 69 } 70 if(x==data[rt]) 71 { 72 cnt[rt]++; 73 siz[rt]++; 74 return; 75 } 76 if(x<data[rt]) 77 { 78 ins(ch[rt][0],x); 79 fa[ch[rt][0]]=rt; 80 pushup(rt); 81 } 82 else 83 { 84 ins(ch[rt][1],x); 85 fa[ch[rt][1]]=rt; 86 pushup(rt); 87 } 88 } 89 90 int getpre(int rt,int x) 91 { 92 int p=rt,ans; 93 while(p) 94 { 95 if(x<=data[p]) 96 { 97 p=ch[p][0]; 98 } 99 else 100 { 101 ans=p; 102 p=ch[p][1]; 103 } 104 } 105 return ans; 106 } 107 108 int getsuc(int rt,int x) 109 { 110 int p=rt,ans; 111 while(p) 112 { 113 if(x>=data[p]) 114 { 115 p=ch[p][1]; 116 } 117 else 118 { 119 ans=p; 120 p=ch[p][0]; 121 } 122 } 123 return ans; 124 } 125 126 int getmn(int rt) 127 { 128 int p=rt,ans=-1; 129 while(p) 130 { 131 ans=p; 132 p=ch[p][0]; 133 } 134 return ans; 135 } 136 137 void del(int rt,int x) 138 { 139 if(data[rt]==x) 140 { 141 if(cnt[rt]>1) 142 { 143 cnt[rt]--; 144 siz[rt]--; 145 } 146 else 147 { 148 splay(rt,0); 149 int p=getmn(ch[rt][1]); 150 if(p!=-1) 151 { 152 splay(p,rt); 153 root=p; 154 fa[p]=0; 155 ch[p][0]=ch[rt][0]; 156 fa[ch[rt][0]]=p; 157 pushup(p); 158 } 159 else 160 { 161 root=ch[rt][0]; 162 fa[ch[rt][0]]=0; 163 } 164 } 165 return; 166 } 167 if(x<data[rt]) 168 { 169 del(ch[rt][0],x); 170 pushup(rt); 171 } 172 else 173 { 174 del(ch[rt][1],x); 175 pushup(rt); 176 } 177 } 178 179 int getk(int rt,int k) 180 { 181 if(data[rt]==k) 182 { 183 splay(rt,0); 184 if(ch[rt][0]==0) 185 { 186 return 1; 187 } 188 else 189 { 190 return siz[ch[rt][0]]+1; 191 } 192 } 193 if(k<data[rt]) return getk(ch[rt][0],k); 194 else return getk(ch[rt][1],k); 195 } 196 197 int getkth(int rt,int k) 198 { 199 int l=ch[rt][0]; 200 if(siz[l]+1<=k&&k<=siz[l]+cnt[rt]) return data[rt]; 201 if(k<siz[l]+1) return getkth(ch[rt][0],k); 202 if(siz[l]+cnt[rt]<k) return getkth(ch[rt][1],k-(siz[l]+cnt[rt])); 203 } 204 }; 205 206 SPLAY s; 207 int main() 208 { 209 scanf("%d",&n); 210 while(n--) 211 { 212 int opt,x; 213 scanf("%d%d",&opt,&x); 214 if(opt==1) 215 { 216 tot++; 217 s.ins(root,x); 218 } 219 if(opt==2) 220 { 221 tot--; 222 s.del(root,x); 223 } 224 if(opt==3) 225 { 226 printf("%d ",s.getk(root,x)); 227 } 228 if(opt==4) 229 { 230 printf("%d ",s.getkth(root,x)); 231 } 232 if(opt==5) 233 { 234 printf("%d ",s.data[s.getpre(root,x)]); 235 } 236 if(opt==6) 237 { 238 printf("%d ",s.data[s.getsuc(root,x)]); 239 } 240 } 241 return 0; 242 }
1 #include<cstdio> 2 #include<algorithm> 3 #define N 100010 4 using namespace std; 5 6 struct TreapType 7 { 8 TreapType *l,*r; 9 int key,fix,size; 10 TreapType(int _key):fix(rand()),key(_key),l(NULL),r(NULL),size(1) {} 11 inline void update() 12 { 13 size=1+(l?l->size:0)+(r?r->size:0); 14 } 15 }*root; 16 17 int T; 18 19 typedef pair<TreapType*,TreapType*> Droot; 20 21 inline int Size(TreapType *x) 22 { 23 return x?x->size:0; 24 } 25 26 Droot Split(TreapType *x,int k) 27 { 28 if(!x) return Droot(NULL,NULL); 29 Droot y; 30 if(Size(x->l)>=k) 31 { 32 y=Split(x->l,k); 33 x->l=y.second; 34 x->update(); 35 y.second=x; 36 } 37 else 38 { 39 y=Split(x->r,k-Size(x->l)-1); 40 x->r=y.first; 41 x->update(); 42 y.first=x; 43 } 44 return y; 45 } 46 47 TreapType* Merge(TreapType *A,TreapType *B) 48 { 49 if(!A) return B; 50 if(!B) return A; 51 if(A->fix<B->fix) 52 { 53 A->r=Merge(A->r,B); 54 A->update(); 55 return A; 56 } 57 else 58 { 59 B->l=Merge(A,B->l); 60 B->update(); 61 return B; 62 } 63 } 64 65 int Getkth(TreapType *x,int k) 66 { 67 if(!x) return 0; 68 return k<=x->key?Getkth(x->l,k):Getkth(x->r,k)+Size(x->l)+1; 69 } 70 71 int Findkth(int k) 72 { 73 Droot x=Split(root,k-1); 74 Droot y=Split(x.second,1); 75 TreapType *ans=y.first; 76 root=Merge(Merge(x.first,ans),y.second); 77 return ans->key; 78 } 79 80 void Insert(int v) 81 { 82 int k=Getkth(root,v); 83 Droot x=Split(root,k); 84 TreapType *now=new TreapType(v); 85 root=Merge(Merge(x.first,now),x.second); 86 } 87 88 void Delete(int v) 89 { 90 int k=Getkth(root,v); 91 Droot x=Split(root,k); 92 Droot y=Split(x.second,1); 93 root=Merge(x.first,y.second); 94 } 95 96 int Pre(int v) 97 { 98 int k=Getkth(root,v); 99 Droot x=Split(root,k-1); 100 Droot y=Split(x.second,1); 101 TreapType *ans=y.first; 102 root=Merge(Merge(x.first,ans),y.second); 103 return ans->key; 104 } 105 106 int Suc(int v) 107 { 108 int k=Getkth(root,v+1); 109 Droot x=Split(root,k); 110 Droot y=Split(x.second,1); 111 TreapType *ans=y.first; 112 root=Merge(Merge(x.first,ans),y.second); 113 return ans->key; 114 } 115 116 int main() 117 { 118 scanf("%d",&T); 119 while(T--) 120 { 121 int opt,x; 122 scanf("%d%d",&opt,&x); 123 if(opt==1) 124 { 125 Insert(x); 126 } 127 if(opt==2) 128 { 129 Delete(x); 130 } 131 if(opt==3) 132 { 133 printf("%d ",Getkth(root,x)+1); 134 } 135 if(opt==4) 136 { 137 printf("%d ",Findkth(x)); 138 } 139 if(opt==5) 140 { 141 printf("%d ",Pre(x)); 142 } 143 if(opt==6) 144 { 145 printf("%d ",Suc(x)); 146 } 147 } 148 return 0; 149 }
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 #define N 1000000 6 #define inf 0x3f3f3f3f 7 struct Graph 8 { 9 struct node 10 { 11 int next,to,dis; 12 } edge[N<<1]; 13 int head[N/2],cnt; 14 Graph(){cnt=1;} 15 void add(int u,int v,int w) 16 { 17 edge[++cnt].next=head[u]; 18 edge[cnt].to=v; 19 edge[cnt].dis=w; 20 head[u]=cnt; 21 } 22 }; 23 Graph g; 24 class Dinic 25 { 26 private: 27 int q[N<<1],h[N<<1],ans,n,S,T; 28 inline void insert(int u,int v,int f) 29 { 30 g.add(u,v,f); 31 g.add(v,u,0); 32 } 33 34 bool bfs() 35 { 36 int t=0,w=1; 37 memset(h,-1,sizeof(h)); 38 q[t]=S; 39 h[S]=0; 40 while(t<w) 41 { 42 int now=q[t++]; 43 for(int i=g.head[now]; i; i=g.edge[i].next) 44 { 45 int v=g.edge[i].to; 46 if(h[v]==-1&&g.edge[i].to) 47 { 48 h[v]=h[now]+1; 49 q[w++]=v; 50 } 51 } 52 } 53 if(h[T]!=-1) return 1; 54 return 0; 55 } 56 57 int dfs(int x,int f) 58 { 59 if(x==T) return f; 60 int w,used=0; 61 for(int i=g.head[x]; i; i=g.edge[i].next) 62 { 63 int v=g.edge[i].to; 64 if(h[v]==h[x]+1) 65 { 66 w=dfs(v,min(f-used,g.edge[i].to)); 67 g.edge[i].to-=w; 68 g.edge[i^1].to+=w; 69 used+=w; 70 if(used==f) return f; 71 } 72 73 } 74 if(!used) h[x]=-1; 75 return used; 76 } 77 public: 78 void dinic() 79 { 80 while(bfs()) 81 ans+=dfs(S,inf); 82 } 83 };
Trie.
1 #include<algorithm> 2 #include<iostream> 3 #include<cstring> 4 #include<string> 5 #include<cstdio> 6 using namespace std; 7 8 struct Node{ 9 Node* Alphabet[26]; 10 int count;bool isword; 11 }root; 12 13 struct Trie{ 14 Node* Create_node(){ 15 Node* node=(Node*)malloc(sizeof(root)); 16 node->count=false; 17 node->isword=false; 18 memset(node->Alphabet,NULL,sizeof(node->Alphabet)); 19 return node; 20 } 21 void Insert_word(char* str){ 22 Node* node=&root; 23 char* word=str; 24 int id; 25 while(*word){ 26 id=*word-'a'; 27 if(node->Alphabet[id]==NULL) 28 node->Alphabet[id]=Create_node(); 29 node=node->Alphabet[id]; 30 ++word; 31 node->count+=1; 32 } 33 node->isword=true; 34 } 35 int Query_word(char* str){ 36 Node* node=&root; 37 char* word=str; 38 int id; 39 while(*word){ 40 id=*word-'a'; 41 if(node->Alphabet[id]==NULL) 42 return false; 43 node=node->Alphabet[id]; 44 ++word; 45 } 46 return node->isword; 47 } 48 };