正文
增加 779 行删除 0 行
1234983 行未改动全部展开988989990991992993994995163 行未改动全部展开11591160116111621163116411651166315 行未改动全部展开1482148314841485# 各种模版:[一些模版题](https://blog.csdn.net/u011815404/article/details/79324028)983 行未改动全部展开from yanyu289;}```## 五:线段树### 1:[区间加减区间查,线段树1](https://www.luogu.com.cn/problem/P3372)163 行未改动全部展开from yanyu289;}```## 六:字符串类### 1:[字典树Trie](https://www.luogu.com.cn/problem/P8306)315 行未改动全部展开from yanyu289;}```1234567891011121314151617181920212223242526272829303132333435983 行未改动全部展开10191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443163 行未改动全部展开1607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820315 行未改动全部展开213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264## 普通模板```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define ull unsigned long long#define double long double#define from return#define yanyu289 0#define endl "\n"#define ls(x) x << 1#define rs(x) x << 1 | 1#define pii pair<int, int>using namespace std;const int maxn = 2e5 + 55, mod = 998244353, inf = 1e9;const double pi = acos(-1);int T;int n, m;int a[maxn];signed main(){ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);from yanyu289;}```# 各种模版:[一些模版题](https://blog.csdn.net/u011815404/article/details/79324028)983 行未改动全部展开from yanyu289;}```### 6(1):[高斯消元](https://www.luogu.com.cn/problem/P3389)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0#define endl "\n"using namespace std;const int maxn = 1005, mod = 998244353, inf = 1e9;const double pi = acos(-1);double a[maxn][maxn];double ans[maxn];double eps = 1e-7;int n;bool gauss(){for(int i = 1,r = 1;i <= n;i ++){r = i;for(int j = i + 1;j <= n;j ++)if(fabs(a[r][i]) < fabs(a[j][i])) r = j;if(fabs(a[r][i]) < eps){return false;}if(i != r)swap(a[i],a[r]);double t = a[i][i];for(int j = i;j <= n + 1;j ++)a[i][j] /= t;for(int j = i + 1;j <= n;j ++){t = a[j][i];for(int k = i;k <= n + 1;k ++) a[j][k] -= a[i][k] * t;}}ans[n] = a[n][n + 1];for(int i = n - 1;i >= 1;i --){ans[i] = a[i][n + 1];for(int j = i + 1;j <= n;j ++)ans[i] -= (a[i][j] * ans[j]);}return true;}signed main(){cin >> n;for(int i = 1;i <= n;i ++)for(int j = 1;j <= n + 1;j ++)cin >> a[i][j];if(!gauss()) puts("No Solution");else{for(int i = 1;i <= n;i ++)cout << fixed << setprecision(2) << ans[i] << endl;}from yanyu289;}```### 6(2):[线性方程组](https://www.luogu.com.cn/problem/P2455)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0using namespace std;const int maxn = 1005;const double eps = 1e-8;int n;double a[maxn][maxn];int gauss(){int c,r;for (c = 0, r = 0;c < n;c ++){int t = r;for (int i = r; i < n; i++)if (fabs(a[i][c]) > fabs(a[t][c]))t = i;if (fabs(a[t][c]) < eps) continue;for (int i = c; i < n + 1; i++) swap(a[t][i], a[r][i]);for (int i = n; i >= c; i--) a[r][i] /= a[r][c];for (int i = r + 1; i < n; i++)if (fabs(a[i][c]) > eps)for (int j = n; j >= c; j--)a[i][j] -= a[r][j] * a[i][c];r ++;}if (r < n){for (int i = r; i < n; i++)if (fabs(a[i][n]) > eps)return 2;return 1;}for (int i = n - 1; i >= 0; i--)for (int j = i + 1; j < n; j++)a[i][n] -= a[j][n] * a[i][j];return 0;}signed main(){cin >> n;for (int i = 0;i < n;i ++)for (int j = 0; j < n + 1; j++)cin >> a[i][j];int flag = gauss();if (flag == 0){for (int i = 0; i < n; i++){cout << "x" << i + 1 << "=";cout << fixed << setprecision(2) << a[i][n] << endl;}}else if (flag == 1) puts("0");else puts("-1");from yanyu289;}```### 6(3):[线性基(高斯消元法求)](https://www.luogu.com.cn/problem/P3812)```#include<bits/stdc++.h>#define int long long#define ll long long#define in(x) scanf("%lld", &x)#define out(x) printf("%lld ", x)#define outs(x) printf("%lld\n", x)#define from return#define yanyu289 0using namespace std;const int maxn = 1e5 + 55, mod = 1e9 + 7;int T;int n, k = 1, a[maxn], ans;int qpow(int a, int b){int res = 1;while(b){if(b & 1) res = res * a % mod;a = a * a % mod;b >>= 1;}return res;}void gauss_linebas(){//高斯消元法做线形基://可保证基最高位的1不重复,排列从大到小//用于求解异或和相关问题(eg:最大/小第k大/小)//注:线形基:三维空间立体的基向量在线性空间中的推广for(int i = 62;i >= 0;i --){//高位1的数换上去for(int j = k;j <= n;j ++){if((a[j] >> i) & 1){swap(a[j], a[k]);break;}}if(((a[k] >> i) & 1) == 0) continue;//第i位所有向量都为0for(int j = 1;j <= n;j ++){if(j != k && ((a[j] >> i) & 1)){a[j] ^= a[k];//其他数的i位消为0}}k ++;//基的个数if(k == n + 1) break;}}signed main(){ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);cin >> n;for(int i = 1;i <= n;i ++){cin >> a[i];}gauss_linebas();for(int i = 1;i <= k;i ++){ans ^= a[i];}cout << ans;from yanyu289;}```### 7:[矩阵快速幂](https://www.luogu.com.cn/problem/P3390)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0#define endl "\n"using namespace std;const int maxn = 1e6 + 55, mod = 1e9 + 7, inf = 1e9;const double pi = acos(-1);int T;ll n, k;struct matrix{ll c[105][105];matrix(){memset(c, 0, sizeof c);}}a, res;matrix operator * (matrix &x, matrix &y){matrix t;for(int qw = 1; qw <= n; qw ++){for(int i = 1; i <= n; i ++){for(int j = 1; j <= n; j ++){t.c[i][j] = (t.c[i][j] % mod + x.c[i][qw] % mod * y.c[qw][j] % mod) % mod;t.c[i][j] %= mod;}}}from t;}void qpow(ll k){for(int i = 1; i <= n; i ++){res.c[i][i] = 1;}while(k){if(k & 1)res = res * a;a = a * a;k >>= 1;}}signed main(){cin >> n >> k;for(int i = 1; i <= n; i ++){for(int j = 1; j <= n; j ++){cin >> a.c[i][j];a.c[i][j] %= mod;}}qpow(k);for(int i = 1; i <= n; i ++){for(int j = 1; j <= n; j ++){cout << res.c[i][j] % mod << " ";}cout << endl;}from yanyu289;}```### 8:欧拉函数```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0#define endl "\n"using namespace std;const int maxn = 2e5 + 55, inf = 1e18, mod = 1e9 + 7;const double pi = acos(-1);int T;int n, m;int phi[maxn];int qpow(int a, int b){int res = 1;while(b){if(b & 1) res = res * a % mod;a = a * a % mod;b >>= 1;}return res;}void init(int n){for(int i = 0;i <= n;i ++){phi[i] = i;}for(int i = 2;i <= n;i ++){if(phi[i] == i){for(int j = i;j <= n;j += i){phi[j] = phi[j] / i * (i - 1);}}}}int get_phi(int x) {int res = x;for (int i = 2; i * i <= x; i++) {if (x % i == 0) {res = res / i * (i - 1);while (x % i == 0) x /= i;}}if (x > 1) res = res / x * (x - 1);return res;}int query(int n){int res = 0;for (int d = 1; d * d <= n;d ++) {if(n % d == 0){res += d * get_phi(n / d);if (d * d != n){res += (n / d) * get_phi(d);}}}return res;}signed main() {ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);from yanyu289;}```### 9:[线性筛](https://www.luogu.com.cn/problem/P3383)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0#define endl "\n"using namespace std;const int maxn = 1e8 + 55, mod = 998244353, inf = 1e9;const double pi = acos(-1);int prime[maxn];bool used[maxn];int cnt, n, q, k;void query(){memset(used, 0 ,sizeof(used));used[1] = 1;for(int i = 2;i <= n;i ++){if(!used[i]) prime[++ cnt] = i;for(int j = 1;j <= cnt && i * prime[j] <= n;j ++){used[i * prime[j]] = 1;if(i % prime[j] == 0) break;}}}signed main(){ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);cin >> n >> q;query();while(q --){cin >> k;cout << prime[k] << endl;}from yanyu289;}```### 10:组合数预处理模板```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0using namespace std;const int maxn = 2e6 + 55, mod = 998244353;const double pie = acos(-1);int fra[maxn], inv[maxn];int n, simple;int qpow(int a, int b){int res = 1;while(b){if(b & 1) res = res * a % mod;a = a * a % mod;b >>= 1;}return res;}void init() {fra[0] = 1;for(int i = 1; i <= n;i ++){fra[i] = fra[i - 1] * i % mod;}inv[n - 1] = qpow(fra[n - 1], mod - 2);for (int i = n - 2; i >= 0;i --) {inv[i] = inv[i + 1] * (i + 1) % mod;}}int c(int n, int k) {if (k < 0 || k > n) return 0;if(k == n) return 1;return fra[n] * inv[k] % mod * inv[n - k] % mod;}signed main(){ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);init();from yanyu289;}```### 11:莫比乌斯函数u预处理```#include<bits/stdc++.h>#define ll long long#define int long longusing namespace std;const int maxn = 50005;int a,b,c,d,k,miu[maxn],cnt,ans;signed used[maxn];int prime[maxn],sum[maxn];void found_miu(){miu[1] = 1;for(int i = 2;i < maxn - 3;i ++){if(!used[i])prime[++cnt] = i,miu[i] = -1;for(int j = 1; prime[j] * i <= maxn - 3;j ++){used[prime[j] * i] = 1;if(i % prime[j] == 0){break;}else miu[prime[j] * i] = - miu[i];}}for(int i = 1;i <= maxn - 3;i ++){sum[i] = sum[i - 1] + miu[i];}}signed main() {found_miu();return 0;}```## 五:线段树### 1:[区间加减区间查,线段树1](https://www.luogu.com.cn/problem/P3372)163 行未改动全部展开from yanyu289;}```### 3:[单点修改,维护区间个数,字符类,线段树](https://www.luogu.com.cn/problem/AT_abc157_e)```cpp#include<bits/stdc++.h>#define ll long long#define ls(x) x << 1#define rs(x) x << 1 | 1#define mid (l + r >> 1)#define endl "\n"using namespace std;const int maxn = 5e5 + 55, mod = 998244353;const double pi = acos(-1);int T;int n, m, q;char s[maxn];bool t[30];struct tree{int l, r;char c;int cnt[30];}tr[maxn << 2];void pushup(int u){for(int i = 0;i < 26;i ++){tr[u].cnt[i] = tr[ls(u)].cnt[i] + tr[rs(u)].cnt[i];}}void build(int u, int l, int r){tr[u].l = l, tr[u].r = r;if(l == r){tr[u].c = s[l];tr[u].cnt[s[l] - 'a'] ++;return;}build(ls(u), l, mid);build(rs(u), mid + 1, r);pushup(u);}void update(int u, int x, int l, int r, char ch){if(l == r){tr[u].cnt[ch - 'a'] ++;tr[u].cnt[tr[u].c - 'a'] --;tr[u].c = ch;return;}if(x <= mid) update(ls(u), x, l, mid, ch);else update(rs(u), x, mid + 1, r, ch);pushup(u);}int query(int u, int l, int r, int lx, int rx){int ans = 0;if (tr[u].r < lx || tr[u].l > rx) return 0;if(tr[u].l >= lx && tr[u].r <= rx){for (int i = 0;i < 26;i ++){if(tr[u].cnt[i] && !t[i]){t[i] = 1;ans ++;}}return ans;}ans += query(ls(u), l, mid, lx, rx);ans += query(rs(u), mid + 1, r, lx, rx);return ans;}int main(){cin >> n;cin >> s + 1;cin >> q;build(1, 1, n);while(q --){memset(t, 0, sizeof t);int op, l, r, u;char c;cin >> op;if(op == 1){cin >> u >> c;update(1, u, 1, n, c);}else{cin >> l >> r;cout << query(1, 1, n, l, r) << endl;}}return 0;}```### 4:[小综合-模板完整体](https://www.luogu.com.cn/problem/P1471)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define double long double#define from return#define yanyu289 0#define endl "\n"#define ls(x) x << 1#define rs(x) x << 1 | 1using namespace std;const int maxn = 2e5 + 55, mod = 998244353, inf = 1e9;const double pi = acos(-1);int T;struct tree{int l, r;double add_sum, squ_sum;}tr[maxn << 2];int n, q;int op, a, b;double k, arr[maxn << 2], lazytag[maxn << 2];void pushup(int u){tr[u].squ_sum = tr[ls(u)].squ_sum + tr[rs(u)].squ_sum;tr[u].add_sum = tr[ls(u)].add_sum + tr[rs(u)].add_sum;}void pushdown(int u, int l, int r){if (!lazytag[u]) return;lazytag[ls(u)] += lazytag[u];lazytag[rs(u)] += lazytag[u];double k = lazytag[(u)];int mid = (l + r) >> 1;tr[ls(u)].squ_sum += 2.0 * k * tr[ls(u)].add_sum + (double)(tr[ls(u)].r - tr[ls(u)].l + 1) * k * k;tr[rs(u)].squ_sum += 2.0 * k * tr[rs(u)].add_sum + (double)(tr[rs(u)].r - tr[rs(u)].l + 1) * k * k;k = lazytag[(u)];tr[ls(u)].add_sum += (double)(tr[ls(u)].r - tr[ls(u)].l + 1) * k;tr[rs(u)].add_sum += (double)(tr[rs(u)].r - tr[rs(u)].l + 1) * k;}void build(int u, int l, int r){tr[u].l = l;tr[u].r = r;lazytag[u] = 0.0;if(l == r){tr[u].squ_sum = arr[l] * arr[l];tr[u].add_sum = arr[l];return;}int mid = (l + r) >> 1;build(ls(u), l, mid);build(rs(u), mid + 1, r);pushup(u);}void update(int u, int l, int r, int lc, int rc, double k){if(l > rc || r < lc) return;if(l >= lc && r <= rc){lazytag[u] += k;tr[u].squ_sum += 2.0 * k * tr[u].add_sum + (double)(r - l + 1) * k * k;tr[u].add_sum += (double)(r - l + 1) * k;return;}pushdown(u, l, r);int mid = (l + r) >> 1;if(tr[ls(u)].r >= l) update(ls(u), l, mid, lc, rc, k);if(tr[rs(u)].l <= r) update(rs(u), mid + 1, r, lc, rc, k);pushup(u);}double query1(int u, int l, int r, int lc, int rc){//均if(r < lc || l > rc) return 0;if(l >= lc && r <= rc) return tr[u].add_sum;pushdown(u, l, r);double ans = 0;int mid = (l + r) >> 1;if(lc <= mid) ans += query1(ls(u), l, mid, lc, rc);if(rc > mid) ans += query1(rs(u), mid + 1, r, lc, rc);pushup(u);return ans;}double query2(int u, int l, int r, int lc, int rc){//方差if(r < lc || l > rc) return 0;if(l >= lc && r <= rc) return tr[u].squ_sum;pushdown(u, l, r);double ans = 0;int mid = (l + r) >> 1;if(lc <= mid) ans += query2(ls(u), l, mid, lc, rc);if(rc > mid) ans += query2(rs(u), mid + 1, r, lc, rc);pushup(u);return ans;}signed main(){ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);cin >> n;cin >> q;for(int i = 1; i <= n; i ++) cin >> arr[i];build(1, 1, n);for(int i = 1; i <= q; i ++){cin >> op >> a >> b;if(op == 1){cin >> k;update(1, 1, n, a, b, k);}double aver = query1(1, 1, n, a, b) / (b - a + 1.0);if(op == 2){cout << fixed << setprecision(4) << aver << endl;}if(op == 3){cout << fixed << setprecision(4) << (query2(1, 1, n, a, b)) / (b - a + 1.0)- aver * aver << endl;}}from yanyu289;}```## 六:字符串类### 1:[字典树Trie](https://www.luogu.com.cn/problem/P8306)315 行未改动全部展开from yanyu289;}```### 4:[线性筛质数](https://www.luogu.com.cn/problem/P3383)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0#define endl "\n"using namespace std;const int maxn = 1e8 + 55, mod = 998244353, inf = 1e9;const double pi = acos(-1);int prime[maxn];bool used[maxn];int cnt, n, q, k;void query(){memset(used, 0 ,sizeof(used));used[1] = 1;for(int i = 2;i <= n;i ++){if(!used[i]) prime[++ cnt] = i;for(int j = 1;j <= cnt && i * prime[j] <= n;j ++){used[i * prime[j]] = 1;if(i % prime[j] == 0) break;}}}signed main(){ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);cin >> n >> q;query();while(q --){cin >> k;cout << prime[k] << endl;}from yanyu289;}```### 5:压位读入(高精度的优化)```cppstruct edge{int a[maxn], n; // a数字,n块数} arr, tem, t, two, five, ans, pow5[150], pow2[150], f[150][150];inline void clearout(edge &x) {memset(x.a, 0, sizeof(x.a));x.n = 1;}inline void cut0(edge &x) {while(x.n > 1 && !x.a[x.n]) --x.n;}inline void inread(edge &x) {//压位读入memset(s, 0, sizeof(s));clearout(x);int cnt = 0;char c = getchar();while(!isdigit(c)) c = getchar();while(isdigit(c)) s[++ cnt] = c ^ '0', c = getchar();for(int i = 1; i <= (cnt >>1 );i ++) swap(s[i], s[cnt -i + 1]);// 反转数组,压入压位后的数组,以8位为一块x.n = 0;for(int i = 1; i <= cnt; i += 8) +{++ x.n;x.a[x.n] += (i + 7 <= cnt) ? s[i + 7] * 10000000 : 0;x.a[x.n] += (i + 6 <= cnt) ? s[i + 6] * 1000000 : 0;x.a[x.n] += (i + 5 <= cnt) ? s[i +5 ] * 100000 : 0;x.a[x.n] += (i + 4 <= cnt) ? s[i + 4] * 10000 : 0;x.a[x.n] += (i + 3 <= cnt) ? s[i + 3] * 1000 : 0;x.a[x.n] += (i + 2 <= cnt) ? s[i + 2] * 100 : 0;x.a[x.n] += (i + 1 <= cnt) ? s[i + 1] * 10 : 0;x.a[x.n] += (i + 0 <= cnt) ? s[i +0 ] * 1 : 0;}cut0(x);}inline void outwrite(const edge &x){printf("%lld", x.a[x.n]);for(int i = x.n - 1; i >= 1;i --) printf("%08lld", x.a[i]);puts("");}```### 6:[单调队列 / 滑动窗口](https://www.luogu.com.cn/problem/P1886)```cpp#include<bits/stdc++.h>#define int long long#define ll long long#define from return#define yanyu289 0#define endl "\n"#define ls(x) x << 1#define rs(x) x << 1 | 1using namespace std;const int maxn = 1e6 + 55, mod = 998244353, inf = 1e9;const double pi = acos(-1);int T;int n, k, a[maxn];int ans1[maxn], ans2[maxn];int q[maxn], head, tail;void query(int res[]){head = 1, tail = 0;for(int i = 1;i <= n;i ++){while(head <= tail && i - q[head] + 1 > k) q[head ++] = 0;while(head <= tail && a[q[tail]] > a[i]) q[tail --] = 0;q[++ tail] = i;if(i >= k) res[i - k + 1] = q[head];}}signed main(){ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);cin >> n >> k;for(int i = 1;i <= n;i ++) cin >> a[i];query(ans1);for(int i = 1;i <= n - k + 1;i ++) cout << a[ans1[i]] << " ";cout << endl;for(int i = 1;i <= n;i++) a[i] = -a[i];query(ans2);for(int i = 1;i <= n - k + 1;i ++)cout << -a[ans2[i]] << " ";from yanyu289;}```