- 博客(77)
- 收藏
- 关注
原创 gcd(a,b)=gcd(b,a%b)
设gcd(a,b)=m;a=Am,b=Bm,其中A,B互质;a-b=m(A-B),于是gcd(a-b,b)=m;简单证明:设gcd(A-B,B)=k;k|A-B,k|B;==>K|A,k|B,又因为gcd(A,B)=1;所以k=1;gcd(a,b)=gcd(a-b,b);//gcd(a,b)=gcd(b,a%b)证明gcd(a,b)=gcd(a-b,b)=gcd(a-2b,b)=gcd(a-nb,b);又a-nb=r;所以gcd(a,b)=gcd(a-b,b)=gcd(b,a
2021-02-23 20:25:57
1418
翻译 线性筛子
//暴力枚举;int isPrime(int n){ if(n<=1)return 0; for(int i=2;i*i<=n;i++) if(n%i==0)return 0; return 1; //每个数我们需要枚举sqrt(n)次,有n个数需要o(nsqrt(n));}//埃拉托斯特尼筛 memset(falg,0,sizeof(flag)); int...
2019-08-10 22:06:27
247
翻译 ACwing 149. 荷马史诗
#include<iostream>#include<vector>#include<queue>using namespace std;typedef long long LL;typedef pair<LL,int>PLI;//权值和路径;int main(){ int n,k; cin>>n>>...
2019-08-09 10:31:21
234
翻译 ACwing 148. 合并果子
#include<iostream>#include<queue>#include<algorithm>#include<vector>using namespace std;int n;int main(){ cin>>n; priority_queue<int,vector<int>,g...
2019-08-09 10:29:18
174
翻译 ACwing 146. 序列
#include<iostream>#include<vector>#include<queue>#include<algorithm>using namespace std;const int N=2010;int a[N],b[N],c[N];int m,n;typedef pair<int,int> pii;...
2019-08-09 10:27:22
178
翻译 ACwing 94. 递归实现排列型枚举
`#include<iostream>#include<vector>using namespace std;int n;vector<int>ans;vector<bool>rec;void dfs(int &&count){ if(ans.size()==n) { for(auto p:ans...
2019-08-09 10:17:28
379
翻译 ACwing 93. 递归实现组合型枚举
#include<iostream>#include<vector>using namespace std;int n,m;/*void dfs(int u,int sum,int state){ if(sum+n-u<m)return; if(sum==m) { for(int i=0;i<n;i++)...
2019-08-09 10:11:28
177
翻译 ACwing 92. 递归实现指数型枚举(二进制与非二进制)
#include<iostream>#include<vector>using namespace std;int n;/*void dfs(int u,int state){ if(u==n) { for(int i=0;i<n;i++) if( state>>i & ...
2019-08-09 10:08:12
301
翻译 ACwing 95. 费解的开关
#include<iostream>#include<cstring>using namespace std;char f[10][10];int dx[5]={0,-1,0,1,0},dy[5]={0,0,1,0,-1};//五个位置const int N=10000000;void turn(int x,int y)//只要按下周围五个位置状...
2019-08-09 10:02:11
253
翻译 ACwing 90. 64位整数乘法
#include<iostream>using namespace std;int main(){ long long a,b,p; cin>>a>>b>>p; long long res=0; while(b) { if(b&1)res=(res+a)%p;//提前%p把...
2019-08-09 09:51:10
149
翻译 ACwing 89. a^b
#include<iostream>#include<algorithm>#include<cmath>#include<vector> using namespace std;int main(){ int a, b, p; cin >> a >> b >> p; i...
2019-08-09 09:43:44
169
翻译 547. 朋友圈(并查集+dfs)
/*class unionfind{ public: vector<int>father; unionfind(int &num){ for(int i=0;i<num;i++) father.push_back(i); } int find(int &n){ if(father[n]==n)...
2019-08-09 00:04:10
238
翻译 leetcode 684. 冗余连接(并查集)
class unionfind{public: vector<int>father; unionfind(const int &num){//num表示元素的个数 for(int i = 0; i < num; i++){ father.push_back(i);//箭头指向自己 } }...
2019-08-09 00:01:48
210
翻译 leetcode 200. 岛屿数量(并查集)
class findunion{ public: vector<int>father; findunion(const int &num) { for(int i=0;i<num;i++)father.push_back(i);//让自己指向自己 } int find(const int &n) { if...
2019-08-08 23:59:26
749
翻译 ACwing 24. 机器人的运动范围
class Solution {public: typedef pair<int,int>PII; int get_single_sum(int x) { int s=0; while(x)s+=x%10,x/=10;//求下标和 return s; } int get_sum(PII p) {...
2019-08-08 23:55:44
156
翻译 letcode 42. 接雨水
class Solution {public: int trap(vector<int>& height) { int ans=0; stack<int>skt; int n=height.size(); for(int i=0;i<n;i++) { whi...
2019-08-05 10:16:23
146
翻译 leetcode 347. 前 K 个高频元素
class Solution {public: vector<int> topKFrequent(vector<int>& nums, int k) { unordered_map<int,int>F; vector<int>ans; vector<int>res(nums.si...
2019-08-02 23:45:19
180
翻译 leet code 350. 两个数组的交集 II
class Solution {public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { unordered_map<int,int>F; vector<int>ans; fo...
2019-08-02 23:44:04
158
翻译 leetcode 297. 二叉树的序列化与反序列化
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */c...
2019-08-02 11:42:04
211
翻译 leetcode 124. 二叉树中的最大路径和
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */cl...
2019-08-02 11:34:55
122
翻译 leetcode 543. 二叉树的直径
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */cl...
2019-08-02 11:32:10
208
翻译 leetcode 236. 二叉树的最近公共祖先
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */cl...
2019-08-02 11:19:46
129
翻译 leetcode 105. 从前序与中序遍历序列构造二叉树(哈希)
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */c...
2019-08-02 11:15:28
146
翻译 leetcode 101. 对称二叉树(递归与非递归)
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; *//*...
2019-08-02 10:58:39
233
翻译 leetcode 94. 二叉树的中序遍历(栈实现)
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */cl...
2019-08-02 10:46:40
174
翻译 leetcode 98. 验证二叉搜索树(边界细节处理防止溢出)
class Solution {public: bool isValidBST(TreeNode* root) { return dfs(root, LONG_MIN, LONG_MAX); /*if (root == NULL)return true; if (root->left->val >= root->val||root->...
2019-08-02 10:42:26
208
翻译 leetcode 221. 最大正方形
class Solution {public: int maximalSquare(vector<vector<char>>& matrix) { if(matrix.empty())return 0; int n=matrix.size(),m=matrix[0].size(); int res=0; vect...
2019-08-01 09:16:09
238
翻译 leetcode 260. 只出现一次的数字 III
class Solution {public: vector<int> singleNumber(vector<int>& nums) { int res=0,rec=0,k=0; for(auto p:nums) res^=p;//res存储的是此时两个单独的数的异或值; while(!(res>>...
2019-07-30 22:47:15
210
翻译 leetcode 201. 数字范围按位与
class Solution {public: int rangeBitwiseAnd(int m, int n) { int rec=0; for(int i=0;m!=n;i++) { m>>=1; n>>=1;//把两个数字同时右移,如果最高位位数相同就会留下最高为,如果...
2019-07-30 22:41:41
134
翻译 leetcode 477. 汉明距离总和
class Solution {public: int totalHammingDistance(vector<int>& nums) { int res=0; for(int i=0;i<=30;i++)//位数 { int rec=0; for(auto p:nums...
2019-07-30 22:24:59
210
翻译 leetcode 421. 数组中两个数的最大异或值
//然后再对每个数的异或值进行比较; class Solution {public: struct Node { int son[2]; }; vector<Node>nodes; int findMaximumXOR(vector<int>& nums) { nodes.pu...
2019-07-30 22:22:20
344
翻译 leetcode 54. 螺旋矩阵
class Solution {public: vector<int> spiralOrder(vector<vector<int>>& matrix) { vector<int>ans; if(matrix.empty())return ans; int n=matrix.si...
2019-07-29 23:17:00
127
翻译 leetcode 504. 七进制数
class Solution {public: string convertToBase7(int num) { if(num==0)return "0"; bool isok=false;//用bool来记录是不是负数 if(num<0)num*=-1, isok=true;//变成正数来做 string ans; w...
2019-07-29 23:12:24
178
翻译 leetcode 67. 二进制求和
class Solution {public: string addBinary(string a, string b) { reverse(a.begin(),a.end()); reverse(b.begin(),b.end()); int rec=0;//记录进位 string ans; for(int i=0;i<a.size(...
2019-07-29 23:07:38
301
翻译 leetcode 263. 丑数
class Solution {public: bool isUgly(int num) { int d[]={2,3,5};//丑数的数组 for(auto p:d) while(num>0&&num%p==0)//能被整除 num/=p; return num==1; }};...
2019-07-29 23:02:24
179
翻译 leetcode 71. 简化路径
class Solution {public: string simplifyPath(string path) { string res,s;//建立一个存储最终结果和中间结果的字符串 path+="/";//是为了防止path最后一组没有/结束的情况 for(auto p:path) { if(res.empty())res+...
2019-07-29 22:58:04
120
翻译 leetcode 68. 文本左右对齐(难度 困难)
class Solution {public: string space(int x) { string res; while(x--)res+=' '; return res; } vector<string> fullJustify(vector<string>& words, int maxWi...
2019-07-29 22:51:21
161
翻译 分组背包(一维dp)
#include<iostream>using namespace std;const int N=110;int n,m;int f[N],v[N],w[N];int main(){ cin>>n>>m; for(int i=1;i<=n;i++) { int s; ...
2019-07-28 23:23:36
174
翻译 leetcode 102. 二叉树的层次遍历
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; ...
2019-07-28 19:43:56
201
翻译 二维费用的背包问题
#include<iostream>#include<vector>using namespace std;const int N=1010;int NN,V,M;int f[N][N];int v[N],m[N],w[N];int main(){ cin>>NN>>V>>M; for(int i=...
2019-07-28 19:08:44
200
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人