模板
EarnForLive
这个作者很懒,什么都没留下…
展开
-
罗老师讲刑法笔记
法无明文规定不为罪,法无明文规定不处罚。原创 2022-05-01 19:07:59 · 291 阅读 · 0 评论 -
智能读取一行的数字
#include<bits/stdc++.h>using namespace std;int main(){ priority_queue<int,vector<int>,less<int> > q; int a,n; scanf("%d",&a); q.push(a); //过滤空格的方法 while(getchar...原创 2020-04-21 00:35:46 · 184 阅读 · 0 评论 -
用两个队列实现一个栈
C++面向对象编程#include<bits/stdc++.h>using namespace std;template<typename T>class CStack{//定义类型public://方法 CStack(){}; ~CStack(){}; void push(const T& node); void pop(...原创 2020-04-19 18:19:31 · 103 阅读 · 0 评论 -
在C中使用string类输入与输出
方法一string str;str.assign(100,'\n'); //这里初始化一个大笑为100的,内容为‘\0'的字符串数组。scanf("%s",str.c_str());printf("%s\n",str.c_str());方法二//完成对一个字符串的输入与输出的基本操作char str[100]; //指定内存大小为100,若输入输出超出内存长度则会报错,提示数组越界...原创 2019-03-24 23:39:03 · 561 阅读 · 0 评论 -
最小公倍数
方法:利用最大公约数若两个数存在整除关系,则大的那个是最小公倍数,否则,两数之积除以他们俩的最大公约数即为他们的最小公倍数。超级精简模板(赛版)int gongbei(int n,int m){ if(n<m) swap(n,m); return n%m==0?n:n*m/__gcd(n,m);//__gcd 要写两个下划线,不是一个 } 以下为举例:#includ...原创 2019-03-08 01:17:41 · 155 阅读 · 0 评论 -
背包问题(简单动态规划)几个模板
01背包问题:无优化for(int i=1;i&lt;=n;i++){ for(int c=0;c&lt;=m;c++) { f[i][c]=f[i-1][c]; if(c&gt;=w[i]) f[i][c]=max(f[i][c],f[i-1][c-w[i]]+v[i]); }}一维数组优化:for(int i=...原创 2019-03-11 16:05:48 · 183 阅读 · 0 评论 -
用getline读取整行文本
普通读入int main(){ string line: while(getline(cin,line)) cout<<line<<endl; return 0;}从文件中读入int main(){ ifstream ifs("unicode_big_endian.txt"); string st...原创 2019-03-15 17:20:24 · 408 阅读 · 0 评论 -
在程序的任何一个地方正常退出程序
exit(0);https://blog.csdn.net/qq_29350001/article/details/53096908原创 2019-03-16 00:49:29 · 145 阅读 · 0 评论 -
有依赖的01背包问题
题目: https://www.luogu.org/problemnew/show/P1064题解:注意: 每种购买方案都要试一下,因为总权值的大小不仅与本次买的主件附件个数有关,还与剩下的钱可买的权值有关。#include&lt;bits/stdc++.h&gt;using namespace std;int v[65][3],p[65][3];//同时存储主件和附件的(价格)与(重要...原创 2019-03-11 23:38:12 · 622 阅读 · 0 评论 -
扩展欧几里得算法
扩展欧几里得算法,简称 exgcd,一般用来求解不定方程,求解线性同余方程,求解模的逆元等inline void exgcd(int a,int b) { if (b) { exgcd(b,a%b); int k=x; x=y; y=k-a/b*y; ...原创 2019-03-08 00:05:55 · 72 阅读 · 0 评论 -
欧几里得算法模板——求最大公约数
又称辗转相除法int gcd(int a,int b){ if(b==0) return a; else return gcd(b,a%b);}原创 2019-03-07 23:56:40 · 322 阅读 · 0 评论 -
最大公约数和最小公倍数问题
题目: https://www.luogu.org/problemnew/solution/P1029题解:1.数学问题要有数学推导的过程2.最大公约数和最小公倍数的乘积就是原两个数的乘积由题意得,gcd(P,Q) = x0;lcm(P,Q) = y0;设P = x0 * t1,Q = x0 * t2P=x0∗t1,Q=x0∗t2因为gcd(P,Q) * lcm(P,Q) = P * ...原创 2019-03-07 23:47:31 · 676 阅读 · 0 评论 -
DFS+并查集模板(并查集思想的标记数组)
**题目:**https://www.luogu.org/problemnew/show/P1141题解:题目的查询次数在10万次级别,因此每次查询就进行一次搜索是不现实的,一定会超时。因此,就要用数组来记忆。另外,每个点能到达的格子数就是这个点所在的连通块的大小,也就是说每个连通块中的各个点的查询结果是相同的,那么就可以用直接查询这个点所在并查集的方法快速得到答案。方法一:DFS+并查集...原创 2019-03-07 22:40:13 · 359 阅读 · 0 评论 -
字符串+BFS
题目:广搜首解最优开两个数组记录串的转换关系,然后以a串(原串)为起点开始搜索,搜索目标是b串。map记录某个串是不是被搜到过,如果已经搜过了就不再继续搜 。枚举当前队列中队头那个串的每一个位置,对每一个位置枚举所有可能的转换手段,然后去尝试拼接。对于一个试图要改变的串str,我们试图在它的第i位用第j种手段改变,首先判断是否可行,然后再逐位拼接。并且如果拼接出的串是合法的,那么我们就把这个串...原创 2019-03-10 21:38:03 · 210 阅读 · 0 评论 -
BFS——层序遍历(也可以叫圈序遍历)
题目: https://www.luogu.org/problemnew/show/P1443题解:在本题中,用bfs遍历不会出现到某点的多条路径,bfs搜索到某点有唯一的最短路径。dfs到某点会有多条路径,需要判优。1.BFS做法:切记:队列用front,栈用top,优先队列用top#include <bits/stdc++.h>using namespace std;...原创 2019-03-10 15:31:35 · 723 阅读 · 0 评论 -
DFS经典——八皇后问题
题目:https://www.luogu.org/problemnew/show/P1219题解check[0][i]标记列check[1][line+i]标记斜向上的对角线,对于同一条这个方向得对角线行列和相同check[2][line-i+n]标记斜向下的对角线,对于同一条这个方向得对角线行列差相同但是可能是负数,所以加上n使其恒为正数#include&lt;bits/stdc++...原创 2019-03-04 20:16:09 · 356 阅读 · 0 评论 -
全排列——模拟全排列
**题目:**https://www.luogu.org/problemnew/show/P1088题解:方法一:直接调用next_permutation方法二:手打next_permutation算法思想:1.首先从最尾端开始往前寻找两个相邻元素,令第一元素为i,第二元素为ii,且满足i&lt;ii。2.找到这样一组相邻元素后,再从最尾端开始往前检验,找出第一个大于i的元素,令为j,...原创 2019-03-10 01:44:05 · 297 阅读 · 0 评论 -
高精快速幂+压位
**题目:**https://www.luogu.org/problemnew/show/P1045题解:如果数据规模小一点完全可以用进制转换来做,但是数据在百万级别就决定了普通进制转换做法不能AC。因此优先考虑快速幂+高精度乘法做法或者压位+高精度乘法。方法一:快速幂+高精度乘法做法:#include &lt;bits/stdc++.h&gt;using namespace std;...原创 2019-03-10 00:45:59 · 604 阅读 · 0 评论 -
BFS——洛谷P1162填涂颜色
题目:https://www.luogu.org/problemnew/solution/P1162题解解法一:BFS#include<bits/stdc++.h>using namespace std;const int M=31;#define map mapp//避免因为使用了万能头文件而出现的重名的情况 bool vis[M][M];int n,m,a,b,c,...原创 2019-03-06 19:33:11 · 196 阅读 · 0 评论 -
完全背包问题
定义:有N种物品和一个容量为V的背包,每种物品都有无限件可用。第i种物品的费用是c[i],价值是w[i]。求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大。题目: https://www.luogu.org/problemnew/show/P1616题解:#include<bits/stdc++.h>using namespace std;int t...原创 2019-03-12 00:47:42 · 96 阅读 · 0 评论 -
等差数列问题——数学推导
题目: https://www.luogu.org/problemnew/show/P1147题解:数据规模在百万级别,用两个for循环嵌套一定会超时方法一:数论分析设首项为L,末项为R,那么sum(L,R)=(L+R)(R-L+1)/2=M即(L+R)(R-L+1)=2M可以把2M分解成两个数之积,假设分成了两个数K1,K2,且K1<K2时,可以列一个二元一次方程组R-L+...原创 2019-03-08 11:18:10 · 355 阅读 · 0 评论 -
二叉排序树 北邮 机试 静态实现
#include<bits/stdc++.h>using namespace std;//二叉树的静态实现 struct node{ int l,r,data;}Node[105];int inde=0;int newnode(int x){ Node[inde].l=Node[inde].r=-1; Node[inde].data=x; return in...原创 2019-03-28 18:35:25 · 170 阅读 · 0 评论 -
矩阵快速幂 北邮机试 矩阵幂
#include<bits/stdc++.h>using namespace std;int a[10][10],b[10][10],c[10][10],d[10][10];int n,kk;int main(){ cin>>n>>kk; for(int i=0;i<n;i++) for(int j=0;j<n;j++) ...原创 2019-03-27 21:53:25 · 152 阅读 · 0 评论 -
矩阵乘法模拟 北邮机试 矩阵幂
#include<bits/stdc++.h>using namespace std;int a[10][10];int b[10][10];int c[10][10];int d[10][10];int n,kk;int main(){ //freopen("kkk.txt","r",stdin); cin>>n>>kk; for(in...原创 2019-03-27 21:41:02 · 123 阅读 · 0 评论 -
二叉排序树的建立及前中后序遍历
#include<bits/stdc++.h>using namespace std;#define index indexxstruct node{int data,l,r;}Node[105];int index,n,k;int newnode(int x){ Node[index].l=-1;Node[index].r=-1;Node[index].data=x;...原创 2019-03-28 03:10:58 · 1313 阅读 · 0 评论 -
邻接表Dijkstra模板———2014 北邮网研 D题 网络传输
题解:#include<bits/stdc++.h>using namespace std;const int INF=1000000000;const int maxv=1005;struct Node{ int v,w; Node(int _v,int _w):v(_v),w(_w) {}//构造函数 方便赋值 }; vector<Node> A...原创 2019-03-24 01:32:37 · 136 阅读 · 0 评论 -
复杂字符串模拟——2014 北邮 计院 D题 Python List
题解:#include<bits/stdc++.h>using namespace std;string str;map<string,vector<int> > mp;//数组名到变长数组的映射 void qingkong(){ int pos=str.find("=",0);//查找 必须是查找字符串 不能是字符 string name=...原创 2019-03-24 00:09:48 · 135 阅读 · 0 评论 -
Floyed模板——邻接矩阵实现
在有向图或无相同中求全源最短路径#include<bits/stdc++.h>using namespace std;const int INF=100000000;const int maxv=200;//根据需要改int n,m;int dis[maxv][maxv];//记录数组 void Floyed(){ for(int k=0;k<n;k++)...原创 2019-03-23 21:55:58 · 220 阅读 · 0 评论 -
Floyed算法——北邮 2014计算机 网络的核
题解:原创 2019-03-23 00:57:04 · 122 阅读 · 0 评论 -
freopen - C/C++文件输入输出利器
#include <stdio.h> // 实际使用中发现freopen也包含在iostream.h中,C++代码#include <iostream.h>即可。 int main(){ freopen("sample.in", "r", stdin); freopen("sample.out", "w", stdout);...原创 2019-03-23 00:42:00 · 286 阅读 · 0 评论 -
二进制问题
题目: https://www.luogu.org/problemnew/show/P1582题解:纯二进制题目。因为所有的水都是由两份相同的水合并而成的,因此每瓶水的体积一定是2^i,(i\in N)2 i ,(i∈N)升。最后保留k个瓶子,那么最后总的升数的二进制表示中,1的个数一定<=k。本题实质上是用不超过k个1和无数个0生成一个最接近且大于n的二进制数方法一:#inclu...原创 2019-03-13 19:07:43 · 586 阅读 · 0 评论 -
欧拉函数——求小于N且与N互质的数的个数
对一个正整数N,欧拉函数是小于N且与N互质的数的个数.。题目: https://www.luogu.org/problemnew/show/P2158题解:裸的欧拉函数题。e(i)为有多少个小于i的与i互质的数。可以把点看做坐标轴。求左下角的点是(1,1),然后求有多少个点(1,1)相连斜率不同的一次函数y=kx。求k的个数。因为k=y/x。想要y/x不同,y与x一定互质(k=y/x=yz/...原创 2019-03-13 17:37:39 · 3394 阅读 · 0 评论 -
全排列中的数学问题
题目: https://www.luogu.org/problemnew/show/P1338题解:本题要用数学知识来做,如果用next_permutation来做,会严重超时。正解:如何让逆序对数为m的序列字典序最小呢? 假设位置p是第一个非原始序列的位置,那这个点应该尽量靠右,才能使得字典序最小。而为了保证有m个逆序对,要求p后面的逆序对数尽量大。怎么才能尽量大呢?当然是降序排列。这样...原创 2019-03-13 16:56:12 · 246 阅读 · 0 评论 -
多次查询避免重复计算的方法(前缀和)
题目: https://www.luogu.org/problemnew/show/P1865题解本题是百万次查询,因此必须想办法优化查询。最好的方法就是记录下前缀和,每次查询根据记录数组计算出结果,而不是再模拟一次查询过程。#include&lt;bits/stdc++.h&gt;using namespace std;bool a[1000005];int f[1000005];...原创 2019-03-13 00:46:07 · 239 阅读 · 0 评论 -
大整数进制转换
大数运算一般用数组来做,这里用数组来模拟短除法原创 2019-03-08 20:23:14 · 500 阅读 · 0 评论 -
进制转换模板——短除法
**题目:**https://www.luogu.org/problemnew/show/P1017题解:原理:短除法中每次除以进制数之后的余数的积累就是新进制的数,因此短除法又被称为除基取余法友情链接:https://jingyan.baidu.com/album/91f5db1b23669c1c7e05e36c.html?picindex=1首先,不管对于什么语言,被除数=商*除数+余...原创 2019-03-08 19:53:49 · 5589 阅读 · 3 评论 -
带权并查集
原题:https://www.luogu.org/problemnew/show/P1196题解:带权并查集(根搭积木很像):对于每个点,分别记录所属链的头结点、该点到头结点的距离以及它所在集合的大小。每次合并将y接在x的尾部,改变y头的权值和所属链的头结点,同时改变x的尾节点。注意:每次查找的时候也要维护每个节点的权值。每次查询时计算两点的权值差。#include&lt;bits/std...原创 2019-02-25 02:29:19 · 130 阅读 · 0 评论 -
种类并查集——多个并查集同时使用技巧
原题:https://www.luogu.org/problemnew/show/P2024题解并查集能维护连通性、传递性,通俗地说,亲戚的亲戚是亲戚。然而当我们需要维护一些对立关系,比如 敌人的敌人是朋友 时,正常的并查集就很难满足我们的需求。这时,种类并查集就诞生了。常见的做法是将原并查集扩大一倍规模,并划分为两个种类。在同个种类的并查集中合并,和原始的并查集没什么区别,仍然表达他们是朋友...原创 2019-02-25 01:27:33 · 464 阅读 · 0 评论 -
归并排序(适用于数据基本有序)
原题https://www.luogu.org/problemnew/show/P1309题解:这个题目全程调用sort()会超时,考虑到数据都是基本有序的这一特点,应选择归并排序。快速排序适用于数据顺序比较随机的情况。#include&lt;bits/stdc++.h&gt;using namespace std;struct NODE{ int score; int sl; i...原创 2019-02-13 21:13:08 · 1002 阅读 · 0 评论 -
【模板】线性筛素数
原题 https://www.luogu.org/problemnew/show/P3383题解//普通筛法(倍增标记) #include&lt;bits/stdc++.h&gt;using namespace std;const int maxn=10000010;bool prime[maxn];int main(){ int m,x,n;//64位系统中,int可以表示的数...原创 2019-02-18 22:17:18 · 187 阅读 · 0 评论