写在前面
考前太焦虑了。。想8出题,就来写板子吧。去年列的目录找不到了,重新列了一个。感觉不是很全,代码一点点填吧,,拜托各位大佬在下方补充目录啊~也可以帮助一下读到这篇笔记的人$qwq$。
PS:这个板子用博客园的代码插入是真的丑,不过没时间修板子了。。。凑合用吧。。。。
成功不必在我,而功力必不唐捐。
算法
数位dp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 #include<set> 8 using namespace std; 9 typedef long long ll; 10 11 ll read() { 12 ll x = 0,f = 1;char c = getchar(); 13 while(c < '0' || c > '9') { 14 if(c == '-') f = -1;c = getchar(); 15 } 16 while(c >= '0' && c <= '9') { 17 x = x * 10 + c - '0';c = getchar(); 18 } 19 return x * f; 20 } 21 int a[12],f[12][10][2],cnt;//f[i][j][k]表示前i位,第i - 1位为j,是(1)否(0)有限制的方案数 22 int dfs(int pos,int lst,int lim) { 23 if(pos == 0) return 1; 24 if(f[pos][lst][lim] != -1) return f[pos][lst][lim]; 25 int ret = 0; 26 int k = 9; 27 if(lim) k = a[pos]; 28 for(int i = 0;i <= k;++i) { 29 if(i == 4 || (lst == 6 && i == 2)) continue; 30 ret += dfs(pos - 1,i,lim && (i == k)); 31 } 32 return f[pos][lst][lim] = ret; 33 34 } 35 int solve(int x) { 36 memset(f,-1,sizeof(f)); 37 cnt = 0; 38 while(x) { 39 a[++cnt] = x % 10; 40 x /= 10; 41 } 42 return dfs(cnt,0,1); 43 } 44 int main() { 45 while(1) { 46 int l = read(),r = read(); 47 if(!l && !r) return 0; 48 printf("%d ",solve(r) - solve(l - 1)); 49 } 50 return 0; 51 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<vector> #include<queue> #include<set> using namespace std; typedef long long ll; const int mod = 1000000007; ll read() { ll x = 0,f = 1;char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1;c = getchar(); } while(c >= '0' && c <= '9') { x = x * 10 + c - '0';c = getchar(); } return x * f; } char s[110]; int a[110],f[110][10][2][2][2];//f[i][j][0/1][0/1][0/1]表示前i位第i-1位填的是j,是否有限制,是否在递增,是否为前导0 int dfs(int pos,int lst,int flag,int lim,int rea) { if(!pos) return rea; if(f[pos][lst][flag][lim][rea] != -1) return f[pos][lst][flag][lim][rea]; int l = 0,r = 9; if(lim) r = a[pos]; if(flag) l = lst; int ret = 0; for(int i = l;i <= r;++i) { ret += dfs(pos - 1,i,flag || (i > lst && rea),lim && (i == r),((!rea) && (!i)) ^ 1); ret %= mod; } return f[pos][lst][flag][lim][rea] = ret; } int main() { int T = read(); while(T--) { memset(f,-1,sizeof(f)); scanf("%s",s + 1); int n = strlen(s + 1); for(int i = 1;i <= n;++i) a[n - i + 1] = s[i] - '0'; cout<<dfs(n,0,0,1,0)<<endl; } return 0; }
矩阵乘法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 #include<set> 8 using namespace std; 9 typedef long long ll; 10 const int mod = 1e9 + 7; 11 ll read() { 12 ll x = 0,f = 1;char c = getchar(); 13 while(c < '0' || c > '9') { 14 if(c == '-') f = -1;c = getchar(); 15 } 16 while(c >= '0' && c <= '9') { 17 x = x * 10 + c - '0';c = getchar(); 18 } 19 return x * f; 20 } 21 22 struct node { 23 int a[10][10],n,m; 24 node() { 25 memset(a,0,sizeof(a));n = m = 0; 26 } 27 node(int x) { 28 n = m = x; 29 memset(a,0,sizeof(a)); for(int i = 1;i <= n;++i) a[i][i] = 1; 30 } 31 node(int x,int y) { 32 n = x,m = y; memset(a,0,sizeof(a)); 33 } 34 }; 35 36 node operator * (const node &A,const node &B) { 37 int n = A.n,m = B.m,K = A.m; 38 node C(n,m); 39 for(int k = 1;k <= K;++k) 40 for(int i = 1;i <= n;++i) 41 for(int j = 1;j <= m;++j) 42 C.a[i][j] += 1ll * A.a[i][k] * B.a[k][j] % mod,C.a[i][j] %= mod; 43 44 return C; 45 } 46 node operator ^ (node A,int y) { 47 node ret(A.n); 48 for(;y;y >>= 1,A = A * A) 49 if(y & 1) ret =ret * A; 50 return ret; 51 } 52 53 int main() { 54 int T = read(); 55 node A(1,3); 56 A.a[1][1] = A.a[1][2] = A.a[1][3] = 1; 57 58 node C(3,3); 59 C.a[1][1] = C.a[1][2] = C.a[2][3] = C.a[3][1] = 1; 60 61 while(T--) { 62 int n = read(); 63 if(n <= 3) {puts("1");continue;} 64 printf("%d ",(A * (C ^ (n - 3))).a[1][1]); 65 66 } 67 return 0; 68 }
manacher
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 #include<set> 8 using namespace std; 9 typedef long long ll; 10 const int N = 31000000; 11 ll read() { 12 ll x = 0,f = 1;char c = getchar(); 13 while(c < '0' || c > '9') { 14 if(c == '-') f = -1;c = getchar(); 15 } 16 while(c >= '0' && c <= '9') { 17 x = x * 10 + c - '0';c = getchar(); 18 } 19 return x * f; 20 } 21 int f[N],id,mx; 22 char s[N],t[N]; 23 int main() { 24 scanf("%s",t + 1); 25 int n = strlen(t + 1); 26 int m = 0; 27 s[++m] = '#'; 28 for(int i = 1;i <= n;++i) { 29 s[++m] = t[i]; 30 s[++m] = '#'; 31 } 32 n = m; 33 int ans = 0; 34 for(int i = 1;i <= n;++i) { 35 36 if(i <= id + mx) f[i] = min(f[id * 2 - i],id + mx - i); 37 while(i - f[i] - 1 >= 1 && i + f[i] + 1 <= n && s[i + f[i] + 1] == s[i - f[i] - 1]) ++f[i]; 38 if(i + f[i] > id + mx) { 39 id = i;mx = f[i]; 40 } 41 ans = max(ans,f[i]); 42 } 43 cout<<ans; 44 45 return 0; 46 }
kmp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //n-nxt[n]为最小循环节的长度 2 #include<cstdio> 3 #include<iostream> 4 #include<cstring> 5 #include<algorithm> 6 #include<vector> 7 #include<queue> 8 #include<set> 9 using namespace std; 10 typedef long long ll; 11 const int N = 2000100; 12 ll read() { 13 ll x = 0,f = 1;char c = getchar(); 14 while(c < '0' || c > '9') { 15 if(c == '-') f = -1;c = getchar(); 16 } 17 while(c >= '0' && c <= '9') { 18 x = x * 10 + c - '0';c = getchar(); 19 } 20 return x * f; 21 } 22 int n,m,nxt[N]; 23 char s[N],t[N]; 24 void get_nxt() { 25 26 int p = 0; 27 for(int i = 2;i <= m;++i) { 28 while(p && t[i] != t[p + 1]) p = nxt[p]; 29 if(t[i] == t[p + 1]) ++p; 30 nxt[i] = p; 31 } 32 33 } 34 int main() { 35 scanf("%s",s + 1); 36 scanf("%s",t + 1); 37 n = strlen(s + 1); 38 m = strlen(t + 1); 39 40 get_nxt(); 41 42 int p = 0; 43 for(int i = 1;i <= n;++i) { 44 while(p && s[i] != t[p + 1]) p = nxt[p]; 45 if(s[i] == t[p + 1]) { 46 ++p; 47 if(p == m) { 48 printf("%d ",i - m + 1); 49 p = nxt[p]; 50 } 51 } 52 } 53 for(int i = 1;i <= m;++i) printf("%d ",nxt[i]); 54 return 0; 55 }
AC自动机
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 #include<set> 8 using namespace std; 9 typedef long long ll; 10 const int N = 2000100; 11 ll read() { 12 ll x = 0,f = 1;char c = getchar(); 13 while(c < '0' || c > '9') { 14 if(c == '-') f = -1;c = getchar(); 15 } 16 while(c >= '0' && c <= '9') { 17 x = x * 10 + c - '0';c = getchar(); 18 } 19 return x * f; 20 } 21 int trie[N][26],tot,ans[N],bz[N]; 22 void ins(char *s,int pos) {//插入字符串 23 int now = 0,len = strlen(s + 1); 24 for(int i = 1;i <= len;++i) { 25 int k = s[i] - 'a'; 26 if(!trie[now][k]) trie[now][k] = ++tot; 27 now = trie[now][k]; 28 } 29 bz[pos] = now; 30 } 31 queue<int>q; 32 vector<int>e[N]; 33 int fail[N]; 34 void pre() { //构造trie图 35 for(int i = 0;i < 26;++i) if(trie[0][i]) q.push(trie[0][i]); 36 while(!q.empty()) { 37 int u = q.front();q.pop(); 38 39 for(int i = 0;i < 26;++i) { 40 if(!trie[u][i]) trie[u][i] = trie[fail[u]][i]; 41 else fail[trie[u][i]] = trie[fail[u]][i],q.push(trie[u][i]); 42 } 43 } 44 45 for(int i = 1;i <= tot;++i) e[fail[i]].push_back(i);//构建fail树 46 47 } 48 char s[N]; 49 void solve() { 50 int len = strlen(s + 1); 51 int now = 0; 52 for(int i = 1;i <= len;++i) { 53 now = trie[now][s[i] - 'a']; 54 ans[now]++; 55 } 56 } 57 void dfs(int u) { 58 for(vector<int>::iterator it = e[u].begin();it != e[u].end();++it) { 59 dfs(*it); 60 ans[u] += ans[*it];//某个节点在母串中出现的次数是fail树上其子树答案之和。 61 } 62 } 63 int main() { 64 int T = read(); 65 for(int i = 1;i <= T;++i) { 66 scanf("%s",s + 1); 67 ins(s,i); 68 } 69 pre(); 70 scanf("%s",s + 1); 71 solve(); 72 dfs(0); 73 74 for(int i = 1;i <= T;++i) printf("%d ",ans[bz[i]]); 75 76 return 0; 77 }
线性基
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 #include<set> 8 using namespace std; 9 typedef long long ll; 10 const int N = 110; 11 ll read() { 12 ll x = 0,f = 1;char c = getchar(); 13 while(c < '0' || c > '9') { 14 if(c == '-') f = -1;c = getchar(); 15 } 16 while(c >= '0' && c <= '9') { 17 x = x * 10 + c - '0';c = getchar(); 18 } 19 return x * f; 20 } 21 ll p[N]; 22 void ins(ll x) { 23 for(int i = 50;i >= 0;--i) { 24 if(!((x >> i) & 1)) continue; 25 if(!p[i]) {p[i] = x;break;} 26 else x ^= p[i]; 27 } 28 } 29 int main() { 30 int n = read(); 31 for(int i = 1;i <= n;++i) { 32 ll x = read(); 33 ins(x); 34 } 35 ll ans = 0; 36 for(int i = 50;i >= 0;--i) 37 if((ans ^ p[i]) > ans) ans ^= p[i]; 38 cout<<ans; 39 return 0; 40 }
meet in the middle
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<unordered_map> 6 #include<vector> 7 #include<queue> 8 #include<set> 9 using namespace std; 10 typedef long long ll; 11 12 ll read() { 13 ll x = 0,f = 1;char c = getchar(); 14 while(c < '0' || c > '9') { 15 if(c == '-') f = -1;c = getchar(); 16 } 17 while(c >= '0' && c <= '9') { 18 x = x * 10 + c - '0';c = getchar(); 19 } 20 return x * f; 21 } 22 unordered_map<ll,ll>ma[30]; 23 ll a[30],n,K,S,jc[30]; 24 void dfs1(int pos,ll now,int k) { 25 if(pos == n / 2 + 1) { 26 ma[k][now]++;return; 27 } 28 29 dfs1(pos + 1,now,k); 30 dfs1(pos + 1,now + a[pos],k); 31 if(a[pos] <= 19 && jc[a[pos]] <= S && k + 1 <= K) dfs1(pos + 1,now + jc[a[pos]],k + 1); 32 } 33 ll ans; 34 void dfs2(int pos,ll now,int k) { 35 if(pos == n / 2) { 36 for(int j = 0;j + k <= K;++j) ans += ma[j][S - now]; 37 return; 38 } 39 dfs2(pos - 1,now,k); 40 dfs2(pos - 1,now + a[pos],k); 41 if(a[pos] <= 19 && jc[a[pos]] <= S && k + 1 <= K) dfs2(pos - 1,now + jc[a[pos]],k + 1); 42 43 } 44 int main() { 45 n = read(),K = read(),S = read(); 46 for(int i = 1;i <= n;++i) a[i] = read(); 47 jc[0] = 1; 48 for(int i = 1;i <= 19;++i) jc[i] = jc[i - 1] * i; 49 50 dfs1(1,0,0); 51 dfs2(n,0,0); 52 cout<<ans; 53 return 0; 54 }
tarjan
点双
边双
割点
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 #include<set> 8 using namespace std; 9 typedef long long ll; 10 const int N = 200100; 11 ll read() { 12 ll x = 0,f = 1;char c = getchar(); 13 while(c < '0' || c > '9') { 14 if(c == '-') f = -1;c = getchar(); 15 } 16 while(c >= '0' && c <= '9') { 17 x = x * 10 + c - '0';c = getchar(); 18 } 19 return x * f; 20 } 21 int dfn[N],low[N],tot; 22 struct node { 23 int v,nxt; 24 }e[N << 1]; 25 int head[N],ejs; 26 void add(int u,int v) { 27 e[++ejs].v = v;e[ejs].nxt = head[u];head[u] = ejs; 28 } 29 int du,ans[N],root; 30 void tarjan(int u,int fa) { 31 if(!dfn[u]) dfn[u] = low[u] = ++tot; 32 for(int i = head[u];i;i = e[i].nxt) { 33 int v = e[i].v; 34 if(v == fa) continue; 35 if(!dfn[v]) { 36 tarjan(v,u); 37 low[u] = min(low[u],low[v]); 38 if(u == root) du++; 39 if(low[v] >= dfn[u] && u != root) ans[u] = 1; 40 } 41 else low[u] = min(low[u],low[v]); 42 } 43 if(du >= 2 && u == root) ans[u] = 1; 44 } 45 46 int main() { 47 int n = read(),m = read(); 48 for(int i = 1;i <= m;++i) { 49 int u = read(),v = read(); 50 add(u,v);add(v,u); 51 } 52 53 for(int i = 1;i <= n;++i) { 54 if(!dfn[i]) { 55 root = i; 56 du = 0; 57 tarjan(i,0); 58 } 59 } 60 int cnt = 0; 61 for(int i = 1;i <= n;++i) cnt += ans[i]; 62 printf("%d ",cnt); 63 for(int i = 1;i <= n;++i) if(ans[i]) printf("%d ",i); 64 65 return 0; 66 }
强连通分量
莫队
朴素莫队
待带修莫队
树上莫队
分数规划
匈牙利
差分约束
分层图
欧拉回路
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 #include<set> 8 using namespace std; 9 typedef long long ll; 10 const int N = 100100; 11 ll read() { 12 ll x = 0,f = 1;char c = getchar(); 13 while(c < '0' || c > '9') { 14 if(c == '-') f = -1;c = getchar(); 15 } 16 while(c >= '0' && c <= '9') { 17 x = x * 10 + c - '0';c = getchar(); 18 } 19 return x * f; 20 } 21 int rc() { 22 char c = getchar(); 23 while(c < 'A' || c > 'z') c = getchar(); 24 if(c > 'Z') c -= 6; 25 return c - 'A'; 26 } 27 int n,a[100][100],du[100],ans[N],cnt; 28 void dfs(int u) { 29 for(int i = 0;i < 52;++i) 30 if(a[u][i]) a[u][i]--,a[i][u]--,dfs(i); 31 ans[++cnt] = u; 32 } 33 34 int main() { 35 n = read(); 36 int rt = 52; 37 for(int i = 1;i <= n;++i) { 38 int u = rc(),v = rc(); 39 if(!a[u][v]) { 40 a[u][v]++; 41 a[v][u]++; 42 du[u]++;du[v]++; 43 44 } 45 } 46 47 int js = 0; 48 for(int i = 0;i < 52;++i) 49 if(du[i] & 1) js++; 50 51 if(js == 0) { 52 for(int i = 0;i < 52;++i) if(du[i]) rt = min(rt,i); 53 } 54 else if(js == 2) { 55 for(int i = 0;i < 52;++i) if(du[i] & 1) rt = min(rt,i); 56 } 57 else {puts("No Solution");return 0;} 58 59 dfs(rt); 60 61 for(int i = cnt;i >= 1;--i) putchar(ans[i] + 'A' > 'Z' ? ans[i] + 'A' + 6 : ans[i] + 'A'); 62 63 return 0; 64 }
数学
高斯消元
线性筛
线性筛素数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 #include<set> 8 using namespace std; 9 typedef long long ll; 10 const int N = 10000010; 11 ll read() { 12 ll x = 0,f = 1;char c = getchar(); 13 while(c < '0' || c > '9') { 14 if(c == '-') f = -1;c = getchar(); 15 } 16 while(c >= '0' && c <= '9') { 17 x = x * 10 + c - '0';c = getchar(); 18 } 19 return x * f; 20 } 21 int dis[N],vis[N],cnt; 22 int main() { 23 int n = read(),m = read(); 24 vis[1] = 1; 25 for(int i = 2;i <= n;++i) { 26 if(!vis[i]) dis[++cnt] = i; 27 for(int j = 1;j <= cnt && dis[j] * i <= n;++j) { 28 vis[dis[j] * i] = 1; 29 if(i % dis[j] == 0) break; 30 } 31 } 32 while(m--) puts(vis[read()] ? "No" : "Yes"); 33 return 0; 34 }
线性筛欧拉函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<ctime> 6 #include<queue> 7 #include<map> 8 using namespace std; 9 typedef long long ll; 10 const int N = 10000100; 11 ll read() { 12 ll x = 0,f = 1;char c = getchar(); 13 while(c < '0' || c > '9') { 14 if(c == '-') f = -1;c = getchar(); 15 } 16 while(c >= '0' && c <= '9') { 17 x = x * 10 + c - '0';c = getchar(); 18 } 19 return x * f; 20 } 21 int vis[N],pri[N],phi[N],cnt; 22 int main() { 23 int n = read(); 24 phi[1] = 1; 25 for(int i = 2;i <= n;++i) { 26 if(!vis[i]) { 27 phi[i] = i - 1; 28 pri[++cnt] = i; 29 } 30 for(int j = 1;j <= cnt && pri[j] * i <= n;++j) { 31 vis[pri[j] * i] = 1; 32 if(i % pri[j] == 0) { 33 phi[pri[j] * i] = pri[j] * phi[i];break; 34 } 35 phi[pri[j] * i] = (pri[j] - 1) * phi[i]; 36 } 37 printf("phi(%d) = %d ",i,phi[i]); 38 } 39 return 0; 40 }
欧拉函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 #include<set> 8 using namespace std; 9 typedef long long ll; 10 11 ll read() { 12 ll x = 0,f = 1;char c = getchar(); 13 while(c < '0' || c > '9') { 14 if(c == '-') f = -1;c = getchar(); 15 } 16 while(c >= '0' && c <= '9') { 17 x = x * 10 + c - '0';c = getchar(); 18 } 19 return x * f; 20 } 21 int phi(int x) { 22 int ret = 1; 23 for(int i = 2;i * i <= x;++i) { 24 if(x % i == 0) { 25 ret *= i - 1; 26 x /= i; 27 } 28 while(x % i == 0) { 29 ret *= i; 30 x /= i; 31 } 32 } 33 if(x != 1) ret *= x - 1; 34 return ret; 35 } 36 ll mod,a; 37 ll qm(ll x,ll y) { 38 ll ret = 1; 39 for(;y;y >>= 1,x = x * x % mod) 40 if(y & 1) ret = ret * x % mod; 41 return ret; 42 } 43 int main() { 44 a = read(),mod = read(); 45 cout<<qm(a,phi(mod) - 1); 46 return 0; 47 }
欧拉降幂
扩展欧几里得
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 #include<set> 8 using namespace std; 9 typedef long long ll; 10 11 ll read() { 12 ll x = 0,f = 1;char c = getchar(); 13 while(c < '0' || c > '9') { 14 if(c == '-') f = -1;c = getchar(); 15 } 16 while(c >= '0' && c <= '9') { 17 x = x * 10 + c - '0';c = getchar(); 18 } 19 return x * f; 20 } 21 ll exgcd(ll a,ll b,ll &x,ll &y) { 22 if(!b) { 23 x = 1,y = 0; 24 return a; 25 } 26 ll ret = exgcd(b,a % b,y,x); 27 y -= a / b * x; 28 return ret; 29 } 30 int main() { 31 ll x = read(),y = read(),m = read(),n = read(),L = read(); 32 ll A = L,B = n - m,C = x - y; 33 if(B < 0) B = -B,C = -C; 34 ll X,Y; 35 int k = exgcd(A,B,X,Y); 36 if(C % k != 0) return puts("Impossible"),0; 37 int t = L / k; 38 cout<<(C / k * Y % t + t) % t; 39 return 0; 40 }
卢卡斯
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 #include<set> 8 using namespace std; 9 typedef long long ll; 10 const int N = 200100; 11 ll read() { 12 ll x = 0,f = 1;char c = getchar(); 13 while(c < '0' || c > '9') { 14 if(c == '-') f = -1;c = getchar(); 15 } 16 while(c >= '0' && c <= '9') { 17 x = x * 10 + c - '0';c = getchar(); 18 } 19 return x * f; 20 } 21 int jc[N],inv[N],mod; 22 ll qm(ll x,ll y) { 23 ll ret = 1; 24 for(;y;y >>= 1,x = x * x % mod) 25 if(y & 1) ret = ret * x % mod; 26 return ret; 27 } 28 ll C(ll n,ll m) { 29 30 if(n < m) return 0; 31 32 if(n < mod && m < mod) 33 return 1ll * jc[n] * inv[m] % mod * inv[n - m] % mod; 34 35 return C(n / mod,m / mod) * C(n % mod,m % mod) % mod; 36 37 } 38 int main() { 39 int T = read(); 40 while(T--) { 41 int n = read(),m = read();mod = read(); 42 n += m; 43 jc[0] = 1; 44 45 for(int i = 1;i < mod;++i) 46 jc[i] = 1ll * jc[i - 1] * i % mod; 47 48 inv[mod - 1] = qm(jc[mod - 1],mod - 2); 49 50 for(int i = mod - 2;i >= 0;--i) inv[i] = 1ll * inv[i + 1] * (i + 1) % mod; 51 cout<<C(n,m)<<endl; 52 53 } 54 return 0; 55 }
线性求逆元
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 #include<set> 8 using namespace std; 9 typedef long long ll; 10 11 ll read() { 12 ll x = 0,f = 1;char c = getchar(); 13 while(c < '0' || c > '9') { 14 if(c == '-') f = -1;c = getchar(); 15 } 16 while(c >= '0' && c <= '9') { 17 x = x * 10 + c - '0';c = getchar(); 18 } 19 return x * f; 20 } 21 int inv[3000001]; 22 int main() { 23 int n = read(),mod = read(); 24 25 inv[1] = 1; 26 puts("1"); 27 for(int i = 2;i <= n;++i) { 28 inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod; 29 printf("%d ",inv[i]); 30 } 31 32 return 0; 33 }
数据结构
st表
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 * @Author: wxyww 3 * @Date: 2019-11-16 16:48:36 4 * @Last Modified time: 2019-11-16 16:54:26 5 */ 6 #include<cstdio> 7 #include<iostream> 8 #include<cstdlib> 9 #include<cmath> 10 #include<ctime> 11 #include<bitset> 12 #include<cstring> 13 #include<algorithm> 14 #include<string> 15 #include<queue> 16 #include<vector> 17 using namespace std; 18 typedef long long ll; 19 const int N = 1000100,logN = 20; 20 ll read() { 21 ll x=0,f=1;char c=getchar(); 22 while(c<'0'||c>'9') { 23 if(c=='-') f=-1; 24 c=getchar(); 25 } 26 while(c>='0'&&c<='9') { 27 x=x*10+c-'0'; 28 c=getchar(); 29 } 30 return x*f; 31 } 32 int a[N],st[N][logN + 1],cf[N],lg[N]; 33 int query(int l,int r) { 34 int len = r - l + 1; 35 return max(st[l][lg[len]],st[r - cf[lg[len]] + 1][lg[len]]); 36 } 37 int main() { 38 int n = read(),m = read(); 39 for(int i = 2;i <= n;++i) lg[i] = lg[i >> 1] + 1; 40 cf[0] = 1; 41 for(int i = 1;i <= logN;++i) cf[i] = cf[i - 1] << 1; 42 43 for(int i = 1;i <= n;++i) st[i][0] = read(); 44 45 for(int j = 1;j <= lg[n];++j) { 46 for(int i = 1;i <= n;++i) { 47 st[i][j] = max(st[i][j - 1],st[i + cf[j - 1]][j - 1]); 48 } 49 } 50 while(m--) { 51 int l = read(),r = read(); 52 printf("%d ",query(l,r)); 53 } 54 return 0; 55 }
lca
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 #include<set> 8 using namespace std; 9 typedef long long ll; 10 const int N = 500010,logN = 22; 11 ll read() { 12 ll x = 0,f = 1;char c = getchar(); 13 while(c < '0' || c > '9') { 14 if(c == '-') f = -1;c = getchar(); 15 } 16 while(c >= '0' && c <= '9') { 17 x = x * 10 + c - '0';c = getchar(); 18 } 19 return x * f; 20 } 21 struct node { 22 int v,nxt; 23 }e[N << 1]; 24 int head[N],ejs; 25 void add(int u,int v) { 26 e[++ejs].v = v;e[ejs].nxt = head[u];head[u] = ejs; 27 } 28 int n,m,S,lca[N][logN + 1],dep[N]; 29 void dfs(int u,int fa) { 30 dep[u] = dep[fa] + 1; 31 for(int i = 1;i <= logN;++i) 32 lca[u][i] = lca[lca[u][i - 1]][i - 1]; 33 for(int i = head[u];i;i = e[i].nxt) { 34 int v = e[i].v; 35 if(v == fa) continue; 36 lca[v][0] = u; 37 dfs(v,u); 38 } 39 } 40 41 int LCA(int x,int y) { 42 if(dep[x] < dep[y]) swap(x,y); 43 for(int i = logN;i >= 0;--i) 44 if(dep[lca[x][i]] >= dep[y]) x = lca[x][i]; 45 for(int i = logN;i >= 0;--i) 46 if(lca[x][i] != lca[y][i]) x = lca[x][i],y = lca[y][i]; 47 if(x != y) return lca[x][0]; 48 return x; 49 } 50 int main() { 51 n = read(),m = read(),S = read(); 52 for(int i = 1;i < n;++i) { 53 int u = read(),v = read(); 54 add(u,v);add(v,u); 55 } 56 57 dfs(S,0); 58 59 while(m--) { 60 int x = read(),y = read(); 61 printf("%d ",LCA(x,y)); 62 } 63 64 return 0; 65 }
树剖
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 #include<set> 8 using namespace std; 9 typedef long long ll; 10 const int N = 200100; 11 ll read() { 12 ll x = 0,f = 1;char c = getchar(); 13 while(c < '0' || c > '9') { 14 if(c == '-') f = -1;c = getchar(); 15 } 16 while(c >= '0' && c <= '9') { 17 x = x * 10 + c - '0';c = getchar(); 18 } 19 return x * f; 20 } 21 22 struct node { 23 int v,nxt; 24 }e[N << 1]; 25 int head[N],ejs; 26 void add(int u,int v) { 27 e[++ejs].v = v;e[ejs].nxt = head[u];head[u] = ejs; 28 } 29 int son[N],dep[N],top[N],fa[N],siz[N],id[N],w[N],ww[N]; 30 int tree[N << 2],lazy[N << 2],mod; 31 void build(int rt,int l,int r) { 32 if(l == r) { 33 tree[rt] = w[l];return; 34 } 35 int mid = (l + r) >> 1; 36 build(rt << 1,l,mid); 37 build(rt << 1 | 1,mid + 1,r); 38 tree[rt] = (tree[rt << 1] + tree[rt << 1 | 1]) % mod; 39 } 40 void pushdown(int rt,int ln,int rn) { 41 if(lazy[rt]) { 42 int k = lazy[rt]; 43 tree[rt << 1] += 1ll * k * ln % mod;tree[rt << 1] %= mod; 44 tree[rt << 1 | 1] += 1ll * k * rn % mod;tree[rt << 1 | 1] %= mod; 45 lazy[rt << 1] += k;lazy[rt << 1] %= mod; 46 lazy[rt << 1 | 1] += k;lazy[rt << 1 | 1] %= mod; 47 lazy[rt] = 0; 48 } 49 } 50 void update(int rt,int l,int r,int L,int R,int c) { 51 if(L <= l && R >= r) { 52 lazy[rt] += c; 53 tree[rt] += 1ll * c * (r - l + 1) % mod; 54 return; 55 } 56 int mid = (l + r) >> 1; 57 pushdown(rt,mid - l + 1,r - mid); 58 if(L <= mid) update(rt << 1,l,mid,L,R,c); 59 if(R > mid) update(rt << 1 | 1,mid + 1,r,L,R,c); 60 tree[rt] = (tree[rt << 1] + tree[rt << 1 | 1]) % mod; 61 } 62 int query(int rt,int l,int r,int L,int R) { 63 if(L <= l && R >= r) return tree[rt]; 64 int mid = (l + r) >> 1; 65 pushdown(rt,mid - l + 1,r - mid); 66 ll ret = 0; 67 if(L <= mid) ret += query(rt << 1,l,mid,L,R),ret %= mod; 68 if(R > mid) ret += query(rt << 1 | 1,mid + 1,r,L,R),ret %= mod; 69 return ret; 70 } 71 72 73 void dfs1(int u,int father) { 74 dep[u] = dep[father] + 1; 75 siz[u] = 1; 76 fa[u] = father; 77 for(int i = head[u];i;i = e[i].nxt) { 78 int v = e[i].v; 79 if(v == father) continue; 80 dfs1(v,u); 81 siz[u] += siz[v]; 82 if(siz[v] > siz[son[u]]) son[u] = v; 83 } 84 } 85 int tot; 86 void dfs2(int u,int father,int ttop) { 87 id[u] = ++tot; 88 w[id[u]] = ww[u]; 89 top[u] = ttop; 90 if(!son[u]) return; 91 dfs2(son[u],u,ttop); 92 for(int i = head[u];i;i = e[i].nxt) { 93 int v = e[i].v; 94 if(v == father || v == son[u]) continue; 95 dfs2(v,u,v); 96 } 97 } 98 int n,m,root; 99 void LCA(int x,int y,int c) { 100 int fx = top[x],fy = top[y]; 101 while(fx != fy) { 102 if(dep[fx] > dep[fy]) { 103 update(1,1,n,id[fx],id[x],c); 104 x = fa[fx]; fx = top[x]; 105 } 106 else { 107 update(1,1,n,id[fy],id[y],c); 108 y = fa[fy]; fy = top[y]; 109 } 110 } 111 if(dep[x] > dep[y]) swap(x,y); 112 update(1,1,n,id[x],id[y],c); 113 } 114 int LCA(int x,int y) { 115 int fx = top[x],fy = top[y];ll ret = 0; 116 while(fx != fy) { 117 if(dep[fx] > dep[fy]) { 118 ret += query(1,1,n,id[fx],id[x]); 119 ret %= mod; 120 x = fa[fx]; 121 fx = top[x]; 122 } 123 else { 124 ret += query(1,1,n,id[fy],id[y]); 125 ret %= mod; 126 y = fa[fy]; 127 fy = top[y]; 128 } 129 } 130 if(dep[x] > dep[y]) swap(x,y); 131 ret += query(1,1,n,id[x],id[y]); 132 return ret % mod; 133 } 134 int main() { 135 n = read(),m = read(),root = read();mod = read(); 136 for(int i = 1;i <= n;++i) ww[i] = read(); 137 for(int i = 1;i < n;++i) { 138 int u = read(),v = read(); 139 add(u,v);add(v,u); 140 } 141 dfs1(root,0); 142 dfs2(root,0,root); 143 build(1,1,n); 144 while(m--) { 145 int opt = read(); 146 if(opt == 1) { 147 int x = read(),y = read(),z = read(); 148 LCA(x,y,z); 149 } 150 else if(opt == 2) { 151 int x = read(),y = read(); 152 printf("%d ",LCA(x,y)); 153 } 154 else if(opt == 3) { 155 int x = read(),y = read(); 156 update(1,1,n,id[x],id[x] + siz[x] - 1,y); 157 } 158 else { 159 int x = read(); 160 printf("%d ",query(1,1,n,id[x],id[x] + siz[x] - 1)); 161 } 162 } 163 return 0; 164 }
set
线段树
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 * @Author: wxyww 3 * @Date: 2019-11-15 15:25:47 4 * @Last Modified time: 2019-11-15 15:49:54 5 */ 6 #include<cstdio> 7 #include<iostream> 8 #include<cstdlib> 9 #include<cmath> 10 #include<ctime> 11 #include<bitset> 12 #include<cstring> 13 #include<algorithm> 14 #include<string> 15 #include<queue> 16 #include<vector> 17 using namespace std; 18 typedef long long ll; 19 const int N = 200100; 20 ll read() { 21 ll x=0,f=1;char c=getchar(); 22 while(c<'0'||c>'9') { 23 if(c=='-') f=-1; 24 c=getchar(); 25 } 26 while(c>='0'&&c<='9') { 27 x=x*10+c-'0'; 28 c=getchar(); 29 } 30 return x*f; 31 } 32 ll a[N],tree[N << 1],mod,lazy1[N << 2],lazy2[N << 2];//lazy1存乘,lazy2存加 33 34 void pushup(int rt) { 35 tree[rt] = (tree[rt << 1] + tree[rt << 1 | 1]) % mod; 36 } 37 38 void pushdown(int rt,int ln,int rn) { 39 if(lazy1[rt] != 1) { 40 lazy2[rt << 1] = lazy2[rt << 1] * lazy1[rt] % mod; 41 lazy2[rt << 1 | 1] = lazy2[rt << 1 | 1] * lazy1[rt] % mod; 42 43 lazy1[rt << 1] = lazy1[rt << 1] * lazy1[rt] % mod; 44 lazy1[rt << 1 | 1] = lazy1[rt << 1 | 1] * lazy1[rt] % mod; 45 46 tree[rt << 1] = tree[rt << 1] * lazy1[rt] % mod; 47 tree[rt << 1 | 1] = tree[rt << 1 | 1] * lazy1[rt] % mod; 48 lazy1[rt] = 1; 49 } 50 if(lazy2[rt]) { 51 lazy2[rt << 1] += lazy2[rt];lazy2[rt << 1] % mod; 52 lazy2[rt << 1 | 1] += lazy2[rt];lazy2[rt << 1 | 1] %= mod; 53 tree[rt << 1] += lazy2[rt] * ln % mod;tree[rt << 1] %= mod; 54 tree[rt << 1 | 1] += lazy2[rt] * rn % mod;tree[rt << 1 | 1] %= mod; 55 lazy2[rt] = 0; 56 } 57 } 58 59 void build(int rt,int l,int r) { 60 lazy1[rt] = 1; 61 if(l == r) { 62 tree[rt] = a[l];return; 63 } 64 int mid = (l + r) >> 1; 65 build(rt << 1,l,mid); 66 build(rt << 1 | 1,mid + 1,r); 67 pushup(rt); 68 } 69 70 void update1(int rt,int l,int r,int L,int R,int c) { 71 if(L <= l && R >= r) { 72 tree[rt] = tree[rt] * c % mod; 73 lazy1[rt] = lazy1[rt] * c % mod; 74 lazy2[rt] = lazy2[rt] * c % mod; 75 return; 76 } 77 int mid = (l + r) >> 1; 78 pushdown(rt,mid - l + 1,r - mid); 79 if(L <= mid) update1(rt << 1,l,mid,L,R,c); 80 if(R > mid) update1(rt << 1 | 1,mid + 1,r,L,R,c); 81 pushup(rt); 82 } 83 void update2(int rt,int l,int r,int L,int R,int c) { 84 if(L <= l && R >= r) { 85 tree[rt] += 1ll * (r - l + 1) * c % mod;tree[rt] %= mod; 86 lazy2[rt] += c;lazy2[rt] %= mod; 87 return; 88 } 89 int mid = (l + r) >> 1; 90 pushdown(rt,mid - l + 1,r - mid); 91 if(L <= mid) update2(rt << 1,l,mid,L,R,c); 92 if(R > mid) update2(rt << 1 | 1,mid + 1,r,L,R,c); 93 pushup(rt); 94 } 95 int query(int rt,int l,int r,int L,int R) { 96 if(L <= l && R >= r) return tree[rt]; 97 int mid = (l + r) >> 1; 98 pushdown(rt,mid - l + 1,r - mid); 99 int ret = 0; 100 if(L <= mid) ret += query(rt << 1,l,mid,L,R),ret %= mod; 101 if(R > mid) ret += query(rt << 1 | 1,mid + 1,r,L,R),ret %= mod; 102 return ret; 103 } 104 int main() { 105 int n = read(),m = read();mod = read(); 106 for(int i = 1;i <= n;++i) a[i] = read(); 107 build(1,1,n); 108 while(m--) { 109 int opt = read(); 110 if(opt == 1) { 111 int x = read(),y = read(),c = read(); 112 update1(1,1,n,x,y,c); 113 } 114 else if(opt == 2) { 115 int x = read(),y = read(),c = read(); 116 update2(1,1,n,x,y,c); 117 } 118 else { 119 int x = read(),y = read(); 120 printf("%d ",query(1,1,n,x,y)); 121 } 122 } 123 return 0; 124 }