
优化
Aurora141592
这个作者很懒,什么都没留下…
展开
-
[图论]点分治
洛谷 – P4178 – Treehttps://www.luogu.org/problem/P4178给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K#include<bits/stdc++.h>using namespace std;typedef long long ll;typedef pair<int, int> pii;const int maxn = 4e4 + 10, mod = 1e9 + 7, inf = 0x3f3f3原创 2021-01-14 12:37:05 · 130 阅读 · 0 评论 -
[数据结构]ST表(倍增)
#include<cstdio>#include<cmath>#include<algorithm>using namespace std;int f[100001][40],a,x,LC,n,m,p,len,l,r;int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d",&a); f[i][原创 2021-01-13 21:12:02 · 114 阅读 · 0 评论 -
[数论]快速乘
当你要计算两个数相乘取模并且相乘会爆long long还不能用__int128的时候,就需要用到快速乘了(n, k >= 0)。原理就是把k二进制分解一下就完事了。ll Slow_Mul(ll n, ll k, ll mod){ ll ans = 0; while(k){ if(k & 1) ans = (ans + n) % mod; k >>= 1; n = (n + n) % mod; } return原创 2021-01-09 09:52:22 · 99 阅读 · 0 评论 -
[贪心]低买高卖的最大收益(优先队列)
http://acm.hdu.edu.cn/showproblem.php?pid=6438http://codeforces.com/contest/867/problem/Equeue没有clear只能while清空#include<bits/stdc++.h>using namespace std;typedef long long ll;priority_queue<int, vector<int>, greater<int> > buy,原创 2021-01-09 09:47:17 · 223 阅读 · 0 评论 -
[动态规划]四边形不等式优化dp
http://www.51nod.com/Challenge/Problem.html#!#problemId=1022用来优化形如 dp[i][j] = min{dp[i][k] + dp[k + 1][j] + cost[i][j]} 的dp,可以降低n倍复杂度。是否能用四边形不等式优化需要证明:证明cost为凸、证明dp为凸、证明决策单调。但是有个更简单的方法判断是否能使用,那就是写出需要优化的暴力dp程序,输出s的表,看是否满足行列单调,满足就可以四边形不等式优化。const int maxn原创 2021-01-08 13:54:26 · 378 阅读 · 0 评论 -
__int128
数据范围1e36,Linux系统才能用,要使用快读输入输出(cin/cout貌似也行)可以#define int __int128输入:inline __int128 in128(){ __int128 res=0,p=1; char c=getchar(); while(c<'0'||c>'9') {if(c=='-') p=-1; c=getchar();} while(c>='0'&&c<='9') {res=res*10原创 2021-01-06 21:23:04 · 641 阅读 · 0 评论 -
[JAVA]高精度
java写高精度真是太方便了,其他的还是用C++吧。import java.math.BigInteger;import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); BigInteger a = in.nextBigInteger(); BigInt原创 2021-01-06 21:08:57 · 83 阅读 · 0 评论 -
[数论]数的分解
约数枚举vector<int> vi;void dis(int x){ for (int i = 1; i * i <= x; ++i){ if (x % i == 0){ vi.push_back(i); if (i != x / i) vi.push_back(x / i); } }}整数分解map<int, int> mii;void dis(int x){原创 2021-01-04 10:35:26 · 136 阅读 · 0 评论 -
[算法]矩阵快速幂
注意:如果有n的k次方,那就把n的k次方写成k个n相乘,然后补k行k列就好了。求(a_n = a_{n – 1} + 2 * a_{n – 2} + n^3)首先我们需要知道立方和完全展开式:((a – b)^3 = a^3 – 3 a^2 b + 3 a b^2 – b^3)然后配就行了((i + 1)^3 = i^3 + 3 i^2 + 3 i + 1)。有一个要注意的地方是,最后要拿转移矩阵的n次方的第一行乘上初始列才是答案。(推完之后发现这矩阵好像杨辉三角形,如果有更高次项应该能按杨辉三角原创 2021-01-03 14:39:13 · 227 阅读 · 0 评论 -
[黑科技]线性递推板子
#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <vector>#include <string>#include <map>#include <set>#include <cassert>using namespace std;#define rep(i,a,n) fo原创 2020-12-31 09:54:42 · 88 阅读 · 0 评论 -
[黑科技]pb_ds库(G++)
This is a library of policy-based elementary data structures: associative containers and priority queues. It is designed for high-performance, flexibility, semantic safety, and conformance to the corresponding containers in std and std::tr1 (except for som原创 2020-12-31 09:52:36 · 479 阅读 · 1 评论 -
[字符串]manacher(回文串算法)
注意!:判断一个字符串是不是另一个的回文串,请你一定把整条字符串都判断一遍,不要只判断一半,可能另一半不匹配。#include<stdio.h>#include<algorithm>#include<string.h>#define maxn 110005using namespace std;int mp[2 * maxn];char a[2 * maxn], s[maxn];int main(){ while (~scanf("%s", s)原创 2020-12-30 13:04:08 · 84 阅读 · 0 评论 -
洛谷 – 逆序对个数(树状数组 + 离散化)
同时,这个数也是通过交换相邻的元素让这个序列变成不下降序列所需要的最小次数。s://www.luogu.org/problemnew/show/P1908#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 5e5 + 10, mod = 1e9 + 7;int n, bit[maxn], dis[maxn];ll a[maxn];//add只用执行+1操作void ad原创 2020-12-30 13:02:36 · 116 阅读 · 0 评论 -
[算法]莫队算法
2019.04.19:莫队算法主要的降低时间复杂度的技巧是分块,可以把询问离线然后分块做,块的大小(\sqrt n),即可。同块右端点排序,不同块左端点排序。不同题目分块之后处理的方法不同,具体题目具体分析。https://www.luogu.org/problemnew/show/P2709#include<bits/stdc++.h>#define maxn 50005using namespace std;int a[maxn], cnt[maxn];long long ans原创 2020-12-30 13:01:56 · 91 阅读 · 1 评论 -
[技巧]高精度
//高精度乘法string mul(string a,string b){ string s; int na[maxn]={},nb[maxn]={},nc[maxn]={},La=a.size(),Lb=b.size(); for(int i=La-1;i>=0;i--) na[La-i]=a[i]-'0'; for(int i=Lb-1;i>=0;i--) nb[Lb-i]=b[i]-'0'; for(int i=1;i<=La;i++)原创 2020-12-29 13:56:42 · 97 阅读 · 0 评论 -
[技巧]输入输出外挂
正整数fread版:inline char nc(){ //快的一批的fread快读 static char buf[100000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;}inline int read(){ char ch=nc(); int sum=0; while(!(ch>='0'&原创 2020-12-29 13:56:02 · 87 阅读 · 0 评论 -
[算法]二分
在求满足某个条件的最大的x时,如果对于任意满足条件的x,所有大于x的值也满足条件的话,这个问题就可以用二分高效的求解,最小的也是一样,有一些题比较难看出来是二分,仔细想想。下面是整数的二分模板while (l <= r){ int m = (l + r) / 2; if (check(m)) ans = m, l = m + 1; else r = m - 1;}以及浮点数的二分模板for (int i = 0; i < 64; ++i){//i的范围控制精度原创 2020-12-28 15:38:33 · 73 阅读 · 0 评论 -
[算法]01分数规划
https://blog.youkuaiyun.com/hzoi_ztx/article/details/54898323http://www.51nod.com/Challenge/Problem.html#!#problemId=1257给定n个二元组((value_i, cost_i)),(value_i)是选择此二元组获得的价值(非负),(cost_i)是选择此二元组付出的代价(非负),设(x_i (x_i \in{{0,1}} ))代表第i个二元组的选与不选,最大(小)化下式r=∑valuei⋅xi∑c原创 2020-12-28 13:35:51 · 76 阅读 · 0 评论 -
[数据结构]分块专题
洛谷 – P3870 – [TJOI2009]开关https://www.luogu.org/problem/P3870这次写的就很漂亮了啊。注意一下,num是所在块的编号,便于维护整块信息(小心多定义冲突)。L数组和R数组是保存了单个位置的左右端点,不是每块的左右端点。#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 1e5 + 10, mod = 1e9 + 7;原创 2020-11-04 21:49:09 · 109 阅读 · 0 评论 -
[随机算法]爬山算法和模拟退火
洛谷 – P3878 – [TJOI2010]分金币https://www.luogu.org/problem/P3878#include<bits/stdc++.h>using namespace std;typedef long long ll;int n, a[55];vector<int> ansa, ansb;ll suma, sumb, ans = 0x3f3f3f3f3f3f3f3f;void init(){ suma = sumb = 0;原创 2020-11-04 21:47:18 · 240 阅读 · 0 评论 -
[技巧]输入输出外挂
正整数fread版:inline char nc(){ //快的一批的fread快读 static char buf[100000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;}inline int read(){ char ch=nc(); int sum=0; while(!(ch>='0'&原创 2020-07-10 20:57:24 · 139 阅读 · 0 评论