摘要:
寻找最大公约数方法代码如下:1 int gcd (int a, int b) {2 return b ? gcd (b, a % b) : a;3 }应用:求最小公倍数代码如下:1 int lcm (int a, int b) {2 return a / gcd (a, b) * b;3 } 阅读全文
摘要:
欧拉函数。phi(n)表示比n小的与n互质的数的个数,比如phi(1) = 1;phi(2) = 1;phi(3) = 2;phi(4) = 2;phi(5) = 4;性质:1. 如果p为质数,则phi(p) = p-1;2. 如果p为质数并且a为正整数,则phi(p^a) = p^a - p^(a-1);证明:p为质数,所以所有可以和p相乘小于p^a的数有p^a/p = p^(a-1)个,剩下的都与p^a互质。3. phi(ab) = phi(a)*phi(b)4. n = p1^a1*p2^a2*...*pk^akphi(n) = phi(p1^a1)*phi(p2^a2)*...*phi 阅读全文
摘要:
1. hypothsis2. cost function:3. Goal:4. Gradient descent algorithmrepeat until convergence { (for j = 0 and j = 1)}note: simultaneous updateα:learning rateifα is too small, gradient descent can be slow.ifα is too large, gradient descent can overshoot the minimum. It may fail to converge, or ... 阅读全文
摘要:
例子: poj3041解法: 摘自http://blog.csdn.net/lyy289065406/article/details/6647040把方阵看做一个特殊的二分图(以行列分别作为两个顶点集V1、V2,其中| V1|=| V2|)然后把每行x或者每列y看成一个点,而障碍物(x,y)可以看做连接x和y的边。按照这种思路构图后。问题就转化成为选择最少的一些点(x或y),使得从这些点与所有的边相邻,其实这就是最小点覆盖问题。再利用二分图最大匹配的König定理:最小点覆盖数 = 最大匹配数(PS:最小点覆盖:假如选了一个点就相当于覆盖了以它为端点的所有边,你需要选择最少的点来覆盖 阅读全文
摘要:
摘自百度:对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若 ∈E(G),则u在线性序列中出现在v之前。方法:拓扑排序方法如下:(1)从有向图中选择一个没有前驱(即入度为0)的顶点并且输出它.(2)从网中删去该顶点,并且删去从该顶点发出的全部有向边.(3)重复上述两步,直到剩余的网中不再存在没有前趋的顶点为止.例子:poj1094解法: 摘自http://www.cnblogs.com/pushing-my-way/archive/2012/08/23/2652033.html题意:给你一些 阅读全文
摘要:
看了别人的答案,这题的关键在于数据结构的选择,一开始选择的multimap肯定MLE 1 class Solution { 2 public: 3 void getPath(vector > &ansSet, vector &ans, int now, vector > &pa, vector &que) 4 { 5 if (now == -1) 6 { 7 ansSet.push_back(vector(0)); 8 for (int i = ans.size() - 1; i >= 0; i-... 阅读全文
摘要:
这题看了网上答案 1 class Solution { 2 public: 3 bool isScramble(string s1, string s2) { 4 // Start typing your C/C++ solution below 5 // D... 阅读全文
摘要:
看了别人答案 1 class Solution { 2 public: 3 int romanToInt(string s) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int... 阅读全文
摘要:
这题因为不知道罗马数是怎么一回事。。就去网上找了答案 1 class Solution { 2 public: 3 string intToRoman(int num) { 4 // Start typing your C/C++ solution below 5 ... 阅读全文
摘要:
这道题把二维图转换成行数个的柱状图题,就简单了 1 class Solution { 2 public: 3 int maximalRectangle(vector > &matrix) { 4 int x = matrix.size(); 5 if(0 ==... 阅读全文