欧拉函数模板
//直接求解欧拉函数 int euler(int n){ //返回euler(n) int res=n,a=n; for(int i=2;i*i<=a;i++){ if(a%i==0){ res=res/i*(i-1);//先进行除法是为了防止中间数据的溢出 while(a%i==0) a/=i; } } if(a>1) res=res/a*(a-1); return res; } //筛选法打欧拉函数表 #define Max 1000001 int euler[Max]; void Init(){ euler[1]=1; for(int i=2;i<Max;i++) euler[i]=i; for(int i=2;i<Max;i++) if(euler[i]==i) for(int j=i;j<Max;j+=i) euler[j]=euler[j]/i*(i-1);//先进行除法是为了防止中间数据的溢出 }
扩展欧几里得
long long ex_gcd(long long a, long long b, long long &x, long long &y){ if(b == 0){ x = 1, y= 0; return a; }else{ long long r = ex_gcd(b, a% b, y, x); y -= x*(a/b); return r; } }
dinic网络流
src:源点
sink:汇点
#include<queue> #include<iostream> #include<string.h> #include<stdio.h> using namespace std; const int inf = 1000000000; const int maxn = 20000, maxm = 500000; struct Edge{ int v, f, nxt; }; int src, sink; int g[maxn + 10]; int nume; Edge e[maxm*2+10]; void addedge(int u, int v, int c){ e[++nume].v = v; e[nume].f = c; e[nume].nxt = g[u]; g[u] = nume; e[++nume].v = u; e[nume].f = 0; e[nume].nxt = g[v]; g[v] = nume; } void init(){ memset(g, 0, sizeof(g)); nume = 1; } queue<int> que; bool vis[maxn +10]; int dist[maxn + 10]; void bfs(){ memset(dist, 0, sizeof(dist)); while(!que.empty()) que.pop(); vis[src] = 1; que.push(src); while(!que.empty()){ int u = que.front(); que.pop(); for(int i = g[u]; i; i = e[i].nxt) if(e[i].f && !vis[e[i].v]){ que.push(e[i].v); dist[e[i].v] = dist[u] + 1; vis[e[i].v] = true; } } } int dfs(int u, int delta){ if(u == sink){ return delta; }else{ int ret = 0; for(int i = g[u]; delta && i; i = e[i].nxt){ if(e[i].f && dist[e[i].v] == dist[u] +1){ int dd = dfs(e[i].v, min(e[i].f, delta)); e[i].f -= dd; e[i^1].f += dd; delta -= dd; ret += dd; } } return ret; } } int maxflow(){ int ret = 0; while(true){ memset(vis, 0, sizeof(vis)); bfs(); if(!vis[sink])return ret; ret += dfs(src, inf); } return ret; } int main(){ int n, m; while(scanf("%d%d", &n, &m)!=EOF){ init(); src = 1; sink = m; for(int i = 0; i < n; i++){ int x, y, z; scanf("%d%d%d", &x, &y, &z); addedge(x, y, z); } printf("%d\n", maxflow()); } }
ac自动机
#include<iostream> #include<string.h> #include<stdio.h> #include<algorithm> #include<queue> using namespace std; const int maxa = 500000; const int cha = 26; int n, m, k; struct Tire{ int next[maxa][cha], fail[maxa], end[maxa]; int root, L; int newnode(){ for(int i = 0; i < cha; i++){ next[L][i] = -1; } end[L++] = 0; return L-1; } void init(){ L = 0; root = newnode(); } void insert(char buf[]){ int len = strlen(buf); int now = root; for(int i = 0; i < len; i++){ if(next[now][buf[i] - ‘a‘] == -1) next[now][buf[i]-‘a‘] = newnode(); now = next[now][buf[i]-‘a‘]; //printf("%d ", now); }//puts(""); end[now] ++; } void build(){ queue<int>Q; fail[root] = root; for(int i = 0; i < cha; i++){ if(next[root][i] == -1) next[root][i] = root; else{ fail[next[root][i]] = root; Q.push(next[root][i]); } } while(!Q.empty()){ int now = Q.front(); Q.pop(); // end[now] |= end[fail[now]]; for(int i = 0; i < cha; i++){ if(next[now][i] == -1) next[now][i] = next[fail[now]][i]; else{ fail[next[now][i]] = next[fail[now]][i]; Q.push(next[now][i]); // printf("**%d %d\n",next[now][i],next[fail[now]][i]); } } } } int solve(char *s){ int ans = 0, k = 0; for(int i = 0; s[i]; i++){ int t = s[i] - ‘a‘; k = next[k][t]; int j = k; while(j){ ans += end[j]; //if(end[j]) printf("%d ",j); end[j] = 0; j = fail[j]; }//puts(""); } return ans; } }; char buf[1000005]; Tire ac; int main(){ int t, n; scanf("%d", &t); while(t--){ scanf("%d", &n); ac.init(); //memset(ac.end, 0, sizeof(ac.end)); for(int i = 0; i < n; i++){ scanf("%s", buf); ac.insert(buf); } ac.build(); scanf("%s", buf); printf("%d\n", ac.solve(buf)); } } /* abcdefg bcdefg cdef de e ssaabcdefg */
凸包
#include<iostream> #include<stdio.h> #include<string.h> #include<cmath> #include<algorithm> using namespace std; const int maxa = 1005; struct edge{ int x, y; }e[maxa], q[maxa]; int cmp(edge a, edge b){ if(a.x != b.x) return a.x < b.x; return a.y < b.y; } int det(int x1, int y1, int x2, int y2){ return x1*y2 - x2*y1; } int cross(edge a, edge b, edge c, edge d){ return det(b.x - a.x, b.y -a.y, d.x - c.x, d.y - c.y); } int make_tubao(int n){ sort(e, e+n, cmp); int m = 0; for(int i = 0; i < n; i++){ while(m >= 2 && cross(q[m-2], q[m-1], q[m-1], e[i])>= 0){ m--; } q[m++] = e[i]; } int k = m; for(int i = n-2; i >= 0; i--){ while(m > k && cross(q[m-2], q[m-1], q[m-1], e[i])>= 0){ m--; } q[m++] = e[i]; } return m; } double dis(edge a, edge b){ return sqrt((b.x - a.x)*(b.x - a.x) + (b.y-a.y)*(b.y-a.y)); } int print(int n, int m){ n = make_tubao(n); double ans = 0; for(int i = 0;i < n-1; i++){ // printf("%d %d\n", q[i].x, q[i].y); ans += dis(q[i], q[i+1]); } printf("%.0f\n", ans + 3.1415926*2*m); } int main(){ int n, m; while(scanf("%d%d", &n, &m)!=EOF){ for(int i = 0 ; i < n; i++){ scanf("%d%d", &e[i].x, &e[i].y); } print(n, m); } }
rmq
#include<iostream> #include<string.h> #include<stdio.h> using namespace std; const int maxa = 50005; int rmp_max[maxa][100]; int rmp_min[maxa][100]; int log(int n){ int cnt = 0; while(n){ cnt ++; n /= 2; } return cnt - 1; } int main(){ int n, m; while(scanf("%d%d", &n ,&m)!=EOF){ for(int i = 0;i < n; i++){ scanf("%d", &rmp_max[i][0]); rmp_min[i][0] = rmp_max[i][0]; } int l = log(n); for(int i = 1; i <= l; i++){ for(int j = 0; j+(1<<(i-1)) < n; j++){ rmp_max[j][i] = max(rmp_max[j][i-1], rmp_max[j+(1<<(i-1))][i-1]); rmp_min[j][i] = min(rmp_min[j][i-1], rmp_min[j+(1<<(i-1))][i-1]); } } while(m--){ int a, b; scanf("%d%d", &a, &b); a--, b--; int j = log(b-a+1); int maxn = max(rmp_max[a][j], rmp_max[b-(1<<j)+1][j]); int minn = min(rmp_min[a][j], rmp_min[b-(1<<j)+1][j]); printf("%d\n", maxn-minn); } } }
时间: 2024-10-26 12:45:20