/*RMQ、树状数组*/ #include<cstdio> #include<cmath> #include<iostream> #include<algorithm> #include<vector> #include<stack> #include<cstring> #include<queue> #include<set> #include<string> #include<map> #include <time.h> #define PI acos(-1) using namespace std; typedef long long ll; typedef double db; const int maxn = 1e5+5; const ll maxm = 1e7; const int mod = 1e9 + 7; const int INF = 0x3f3f3f; const ll inf = 1e15 + 5; const db eps = 1e-9; int c[maxn], a[maxn], n, ca=1; ll l[maxn], lm[maxn]; /*void RMQ_init() { for (int i = 1; i <= n; i++) { dmax[i][0] = dmin[i][0] = a[i]; } for (int j = 1; (1<<j) <= n; j++) { for (int i = 1; i+(1<<j)-1<=n; i++) { dmax[i][j] = max(dmax[i][j-1], dmax[i+(1<<(j-1))][j-1]); dmin[i][j] = min(dmin[i][j-1], dmin[i+(1<<(j-1))][j-1]); } } } int RMQma(int l, int r) { int k = 0; while((1<<(k+1))<=r-l+1) k++; return max(dmax[l][k], dmax[r-(1<<k)+1][k]); } int RMQmi(int l, int r) { int k = 0; while((1<<(k+1))<=r-l+1) k++; return min(dmin[l][k], dmin[r-(1<<k)+1][k]); }*/ int lowbit(int x) { return x&-x; } int sum(int x) { int ret=0; while(x>0) { ret+=c[x], x-=lowbit(x); } return ret; } void add(int x, int d) { while(x<maxn) { c[x] += d; x+=lowbit(x); } } void solve() { scanf("%d", &n); memset(c, 0, sizeof(c)); for (int i = 1; i <= n; i++) { scanf("%d", a+i); } ll ans=0; for (int i = 1; i <= n; i++) { l[i] = sum(a[i]); lm[i] = i-1-l[i]; add(a[i], 1); } memset(c, 0, sizeof(c)); for (int i = n; i > 0; i--) { ll tmp=sum(a[i]); ans += tmp*lm[i]; tmp = n-i-tmp; ans += (tmp)*l[i]; add(a[i], 1); } cout << ans << endl; } int main() { int t = 1; scanf("%d", &t); while(t--) solve(); return 0; }
矩阵快速幂板子
#include<cstdio> #include<cmath> #include<iostream> #include<algorithm> #include<vector> #include<stack> #include<cstring> #include<queue> #include<set> #include<string> #include<map> #include <time.h> #define PI acos(-1) using namespace std; typedef long long ll; typedef double db; const int maxn = 12; const int N = 10; const ll maxm = 1e7; const int mod = 9973; const int INF = 0x3f3f3f; const ll inf = 1e15 + 5; const db eps = 1e-9; int n, k; int a[10], f[maxn]; struct Matrix{ int mat[maxn][maxn]; Matrix operator*(const Matrix& m)const{ Matrix tmp; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { tmp.mat[i][j]=0; for (int k = 0; k < n; k++) { tmp.mat[i][j] += mat[i][k]*m.mat[k][j]%mod; tmp.mat[i][j] %= mod; } } } return tmp; } }; int Pow(Matrix &m, int k) { Matrix ans; memset(ans.mat , 0 , sizeof(ans.mat)); for(int i = 0 ; i < maxn ; i++) ans.mat[i][i] = 1; //k -= 9; while(k){ if(k&1) ans = ans*m; k >>= 1; m = m*m; } int sum = 0; /* for(int i = 0 ; i < maxn ; i++){ sum += ans.mat[0][i]*f[maxn-i-1]%mod; sum %= mod; }*/ for (int i = 0; i < n; i++) { sum+= ans.mat[i][i]; } return sum%mod; } void solve() { Matrix m; while(scanf("%d%d", &n, &k)!=EOF) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%d", &m.mat[i][j]); m.mat[i][j]%=mod; } } printf("%d\n", Pow(m, k)); } } int main() { int t = 1; // freopen("in.txt", "r", stdin); scanf("%d", &t); while(t--) solve(); return 0; }
最小树形图 poj3164
/* gyt Live up to every day */ #include<cstdio> #include<cmath> #include<iostream> #include<algorithm> #include<vector> #include<stack> #include<cstring>3 #include<queue> #include<set> #include<string> #include<map> #include <time.h> #define PI acos(-1) using namespace std; typedef long long ll; typedef double db; const int maxn = 10000+5; const ll maxm = 1e7; const int mod = 1e9+7; const int INF = 1<<30; const db eps = 1e-9; const ll Max=1e19; int n, m; struct point{ double x, y; }p[maxn]; struct Node{ int u, v; double cost; }e[maxn]; int pre[maxn], id[maxn], vis[maxn]; double in[maxn]; double dist(point a, point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } double Zhu_Liu(int root, int nv, int ne) { double ret=0; while(1) { for (int i=0; i<nv; i++) in[i]=INF; for (int i=0; i<ne; i++) { int u=e[i].u; int v=e[i].v; if (e[i].cost<in[v] && u!=v) { pre[v]=u; in[v]=e[i].cost; } } for (int i=0; i<nv; i++) { if (i==root) continue; if (in[i]==INF) return -1; } int cntcode=0; memset(id, -1, sizeof(id)); memset(vis, -1, sizeof(vis)); in[root]=0; for (int i=0; i<nv; i++) { ret+=in[i]; int v=i; while(vis[v]!=i&&id[v]==-1&&v!=root) { vis[v]=i; v=pre[v]; } if (v!=root&&id[v]==-1) { for(int u=pre[v] ; u!= v; u=pre[u]) { id[u]=cntcode; } id[v]=cntcode++; } } if (cntcode==0) break; for (int i=0; i<nv; i++) { if (id[i]==-1) { id[i]=cntcode++; } } for (int i=0; i<ne; i++) { int v=e[i].v; e[i].u=id[e[i].u]; e[i].v=id[e[i].v]; if (e[i].u!=e[i].v) { e[i].cost-=in[v]; } } nv=cntcode; root=id[root]; } return ret; } void solve() { while(scanf("%d%d", &n, &m)!=EOF) { for (int i=0; i<n; i++) { scanf("%lf%lf", &p[i].x, &p[i].y); } for (int i=0; i<m; i++) { scanf("%d%d", &e[i].u, &e[i].v); e[i].u--, e[i].v--; if (e[i].v!=e[i].u) e[i].cost = dist(p[e[i].u], p[e[i].v]); else e[i].cost=INF; } double ans=Zhu_Liu(0, n, m); if (ans==-1) puts("poor snoopy"); else { printf("%.2f\n", ans); } } } int main() { int t=1; //freopen("in.txt", "r", stdin); while(t--) { solve(); } }
生成树计数
/* gyt Live up to every day */ #include<cstdio> #include<cmath> #include<iostream> #include<algorithm> #include<vector> #include<stack> #include<cstring>3 #include<queue> #include<set> #include<string> #include<map> #include <time.h> #define PI acos(-1) using namespace std; typedef long long ll; typedef double db; const int maxn = 15+10; const ll maxm = 1e7; const int mod = 1000000007; const int INF = 1<<30; const db eps = 1e-9; const ll Max=1e19; int d[maxn]; ll c[maxn][maxn]; void init() { memset(d, 0, sizeof(d)); memset(c, 0, sizeof(c)); } ll det(ll a[][maxn], int n) { ll ret=1; for (int i=1; i<n; i++) { for (int j=i+1; j<n; j++) { while(a[j][i]) { ll t=a[i][i]/a[j][i]; for (int k=i; k<n; k++) a[i][k]=(a[i][k]-a[j][k]*t); for (int k=i; k<n; k++) swap(a[i][k], a[j][k]); ret=-ret; } } if (a[i][i]==0) return 0; ret=ret*a[i][i]; } if (ret<0) ret=-ret; return ret; } void solve() { int n, m; scanf("%d%d", &n, &m); init(); for (int i=0; i<m; i++) { int a, b; scanf("%d%d", &a, &b); a--, b--; d[a]++, d[b]++; c[a][b]=c[b][a]=-1; } for (int i=0; i<n; i++) c[i][i]=d[i]; ll ans=det(c, n); printf("%lld\n", ans); } int main() { int t=1; //freopen("in.txt", "r", stdin); scanf("%d", &t); for (int T=1; T<=t; T++) { solve(); } }
最小表示法
char a[maxn]; void solve() { int len; scanf("%d", &len); scanf("%s", a); int nowxiao=0, next=1, jia=0; while(1) { if (nowxiao>=len||next>=len||jia>=len) break; int tmp=a[(next+jia)%len]-a[(nowxiao+jia)%len]; //cout << nowxiao << " " << next << endl; //cout << a[(nowxiao+jia)%len]<<" " << a[(next+jia)%len]<< endl; if (tmp==0) jia++; else { if (tmp>0) next+=jia+1; else { nowxiao+=jia+1; } if (nowxiao==next) next++; jia=0; } //cout << nowxiao << " " << next << endl; } int ans=min(nowxiao, next) ; printf("%d\n", ans); } int main() { int t = 1; //freopen("in.txt", "r", stdin); scanf("%d", &t); while(t--) solve(); return 0; }
最长公共子序列
char s1[1000], s2[1000]; int dp[1000][1000]; void solve() { while(scanf("%s%s", s1, s2)!=EOF) { int len1=strlen(s1), len2=strlen(s2); memset(dp, 0, sizeof(dp)); for (int i=0; i<len1; i++) { for (int j=0; j<len2; j++) { if (s1[i]==s2[j]) { dp[i+1][j+1]=dp[i][j]+1; } else { dp[i+1][j+1]=max(dp[i+1][j], dp[i][j+1]); } } } /*for (int i=0; i<len1; i++) { for (int j=0; j<len2; j++) { cout << i << " " << j << " " << dp[i][j] << endl; } }*/ printf("%d\n", dp[len1][len2]); } } int main() { int t = 1; //freopen("in.txt", "r", stdin); //scanf("%d", &t); while(t--) solve(); return 0; }
刘汝佳的Dinic HDU - 1532
/* gyt Live up to every day */ #include<cstdio> #include<cmath> #include<iostream> #include<algorithm> #include<vector> #include<stack> #include<cstring> #include<queue> #include<set> #include<string> #include<map> #include <time.h> #define PI acos(-1) using namespace std; typedef long long ll; typedef double db; const int maxn = 1000+10; const ll maxm = 1e7; const int modd = 10000007; const int INF = 1<<30; const db eps = 1e-9; struct Edge { int u, v, cap, flow; }e[maxn*40]; vector<int>G[maxn]; int d[maxn], cur[maxn]; bool vis[maxn]; int m, n, cnt; int s, t; void init() { for (int i=0; i<=200; i++) { G[i].clear(); } cnt=0; } void add(int u, int v, int cap, int f) { e[cnt].u=u, e[cnt].cap=cap, e[cnt].flow=f, e[cnt].v=v; } void Addedge(int u, int v, int cost) { add(u, v, cost, 0); G[u].push_back(cnt++); add(v, u, 0, 0); G[v].push_back(cnt++); } bool BFS() { memset(vis, 0, sizeof(vis)); queue<int>q; q.push(s); vis[s]=1; d[s]=0; while(!q.empty()) { int v=q.front(); q.pop(); for (int i=0; i<G[v].size(); i++) { Edge &te=e[G[v][i]]; if (!vis[te.v] && te.cap>te.flow) { vis[te.v]=1; d[te.v]=d[v]+1; q.push(te.v); } } } return vis[t]; } int dfs(int x, int a) { if (x==t||a==0) return a; int flow=0, f; for (int &i=cur[x]; i<G[x].size(); i++) { Edge &te=e[G[x][i]]; if (d[x]+1==d[te.v] && (f=dfs(te.v, min(a, te.cap-te.flow)))>0) { te.flow+=f; e[G[x][i]^1].flow -= f; flow += f; a -= f; if (a == 0) break; } } return flow; } int Dinic() { int flow=0; while(BFS()) { memset(cur, 0, sizeof(cur)); flow+=dfs(s, INF); } return flow; } void solve() { while(scanf("%d%d", &m, &n)!=EOF) { init(); for (int i=0; i<m; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); Addedge(a, b, c); } s=1, t=n; int ans=Dinic(); printf("%d\n", ans); } } int main() { int t = 1; //freopen("in.txt", "r", stdin); //scanf("%d", &t); while(t--) solve(); return 0; }
连通分量板子
/* gyt Live up to every day */ #include<cstdio> #include<cmath> #include<iostream> #include<algorithm> #include<vector> #include<stack> #include<cstring> #include<queue> #include<set> #include<string> #include<map> #include <time.h> #define PI acos(-1) using namespace std; typedef long long ll; typedef double db; const int maxn = 100000+10; const ll maxm = 1e7; const int modd = 10000007; const int INF = 1<<30; const db eps = 1e-9; struct Edge{ int u, v, next; }e[maxn]; int n, m, cnt,scnt, tot; stack<int>sta; int dfn[maxn], low[maxn], vis[maxn], head[maxn]; void init() { memset(head, -1, sizeof(head)); memset(dfn, 0, sizeof(dfn)); memset(low, 0, sizeof(low)); memset(vis, 0, sizeof(vis)); while(!sta.empty()) sta.pop(); cnt=0; scnt=0; tot=0; } void add(int u, int v) { e[cnt].v=v, e[cnt].next=head[u]; head[u]=cnt++; } void Tarjan(int s) { int minn, t; dfn[s]=low[s]=++tot; vis[s]=2; sta.push(s); for (int i=head[s]; ~i; i=e[i].next) { int t=e[i].v; if (!dfn[t]) { Tarjan(t); low[s]=min(low[s], low[t]); } else { if (vis[t]==2) { low[s]=min(low[s], dfn[t]); } } } if (low[s]==dfn[s]) { scnt++; while(!sta.empty()) { int t=sta.top(); sta.pop(); vis[t]=1; if (t==s) break; } } } void solve() { while(scanf("%d%d", &n, &m)!=EOF) { if (!n&&!m) break; init(); for (int i=0; i<m; i++) { int a, b; scanf("%d%d", &a, &b); add(a, b); } for (int i=1; i<=n; i++) { if (!dfn[i]) Tarjan(i); } if (scnt==1) puts("Yes"); else puts("No"); } } int main() { int t = 1; //freopen("in.txt", "r", stdin); //scanf("%d", &t); while(t--) solve(); return 0; }
sg函数打表
//f[]:可以取走的石子个数 //sg[]:0~n的SG函数值 //hash[]:mex{} int f[N],sg[N],hash[N]; void getSG(int n) { int i,j; memset(sg,0,sizeof(sg)); for(i=1;i<=n;i++) { memset(hash,0,sizeof(hash)); for(j=1;f[j]<=i;j++) hash[sg[i-f[j]]]=1; for(j=0;j<=n;j++) //求mes{}中未出现的最小的非负整数 { if(hash[j]==0) { sg[i]=j; break; } } } }