随笔分类 -  算法概论

1
纸上得来终觉浅
摘要:http://acm.nyist.net/JudgeOnline/problem.php?pid=651描述We have a rope whose length is L. We will cut the rope into two or more parts, the length of each part must be an integer, and no two parts have the same length. Your task is to calculate there exists how many results after the cutting. We say t. 阅读全文
posted @ 2013-06-21 01:16 Norcy 阅读(581) 评论(0) 推荐(0)
摘要:In the hourglass to the right a path is marked. A path always starts at the first row and ends at the last row. Each cell in the path (except the first) should be directly below to the left or right of the cell in the path in the previous row. The value of a path is the sum of the values in each cel 阅读全文
posted @ 2013-06-16 14:49 Norcy 阅读(409) 评论(0) 推荐(0)
摘要:DescriptionToday is Yukari'sn-th birthday. Ran and Chen hold a celebration party for her. Now comes the most important part, birthday cake! But it's a big challenge for them to placencandles on the top of the cake. As Yukari has lived for such a long long time. Though she herself insists tha 阅读全文
posted @ 2013-06-14 21:32 Norcy 阅读(239) 评论(0) 推荐(0)
摘要:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=25019#problem/EDescriptionProf. Tigris is the head of an archaeological team who is currently in charge of an excavation in a site of ancient relics.This site contains relics of a village where civilization once flourished. One night, examining a 阅读全文
posted @ 2013-06-14 21:27 Norcy 阅读(311) 评论(0) 推荐(0)
摘要:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=25019#problem/CDescriptionmmm is learning division, she's so proud of herself that she can figure out the sum of all the divisors of numbers no larger than 100 within one day!But her teacher said "What if I ask you to give not only the s 阅读全文
posted @ 2013-06-14 21:17 Norcy 阅读(338) 评论(0) 推荐(0)
摘要:DescriptionWANGPENG is a freshman. He is requested to have a physical examination when entering the university.Now WANGPENG arrives at the hospital. Er….. There are so many students, and the number is increasing!There are many examination subjects to do, and there is a queue for every subject. The q 阅读全文
posted @ 2013-06-14 20:10 Norcy 阅读(506) 评论(0) 推荐(0)
摘要:DescriptionFor a group of people, there is an idea that everyone is equals to or less than 6 steps away from any other person in the group, by way of introduction. So that a chain of "a friend of a friend" can be made to connect any 2 persons and it contains no more than 7 persons.For exam 阅读全文
posted @ 2013-06-14 19:46 Norcy 阅读(316) 评论(0) 推荐(0)
摘要:DescriptionIn China, foreign brand commodities are often much more expensive than abroad. The main reason is that we Chinese people tend to think foreign things are better and we are willing to pay much for them. The typical example is, on the United Airline flight, they give you Haagendazs ice crea 阅读全文
posted @ 2013-06-14 17:31 Norcy 阅读(449) 评论(0) 推荐(0)
摘要:We say a sequence of characters is apalindromeif it is the same written forwards and backwards. For example, 'racecar' is a palindrome, but 'fastcar' is not.Apartitionof a sequence of characters is a list of one or more disjoint non-empty groups of consecutive characters whose concat 阅读全文
posted @ 2013-06-14 15:00 Norcy 阅读(662) 评论(0) 推荐(0)
摘要:DescriptionWe wish to tile a grid 4 units high andNunits long with rectangles (dominoes) 2 units by one unit (in either orientation). For example, the figure shows the five different ways that a grid 4 units high and 2 units wide may be tiled.Write a program that takes as input the width,W, of the g 阅读全文
posted @ 2013-06-09 23:10 Norcy 阅读(685) 评论(0) 推荐(0)
摘要:DescriptionThere is a pile ofnwooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to prepare processing a stick. The setup times are associated w 阅读全文
posted @ 2013-05-11 21:02 Norcy 阅读(293) 评论(0) 推荐(0)
摘要:并查集+贪心算法。/*对于稀疏图来说,用Kruskal写最小生成树效率更好。输入:边的起点,终点,权值 */#include <iostream>using namespace std;#define Max 100struct Edge{ int start,end,w;};Edge e[Max];int cnt, father[Max];void init(){ cnt = 0; for (int i = 1; i < Max; ++i) { father[i] = i; }}int find(int x){ if... 阅读全文
posted @ 2013-05-05 17:14 Norcy 阅读(232) 评论(0) 推荐(0)
摘要:给定一个有向图,要求寻找全部强联通分量。首先要先明确几点:1.点a和点b连通 当且仅当 存在边(a,b)和(b,a)2.将一个有向图的所有强连通分量看成顶点,则该图是有向无环图(dag)。如下图:还有明确几点性质:1.对一个"聚点"(强连通分量)的点进行DFS,则不会跑出这个连通分量。因为没有边可以出去。(如上图的h连通分量)2.DFS后,post值最高的为源点所以我们的思路可以是这样,找到聚点(某个点),DFS它,由性质1可知,能被访问的点组成的就是一个强连通分量。删除它(因为是dag,所以必定会有新的聚点),再找聚点……如何找到聚点呢?我们可以先把图G的边取反,记为GT 阅读全文
posted @ 2013-05-05 16:59 Norcy 阅读(5751) 评论(0) 推荐(0)
摘要:伪代码(伪代码是用来记住算法的)代码View Code #include <iostream>#include <vector>using namespace std;#define Max 100int n, m; //n为点数,m为边数 int clk; //用于更新pre postint pre[Max], post[Max]; bool vis[Max];vector<int> v[Max]; //邻接表 //开始访问xvoid pre_vis(int x){ pre[x] = clk; clk++;}//结束访问x void post_vis(i. 阅读全文
posted @ 2013-05-05 16:34 Norcy 阅读(226) 评论(0) 推荐(0)
摘要:题目意思,一个有向图,若有环(不仅仅是自环)或某个点入度大于等于2则不合法,否则合法,输出深度和所有连通分量在同一层的最大节点数。 判断一个没有入度>=2有向图是否有环: 只需一个判断条件:对所有入度为0的点进行DFS,若所有的点都访问到了,则无环,否则有环也就是说,只要保证每个点入度≥2,则该图的连通分量只能有几种情况:孤立点,非环,全部点收尾相接形成的环(不存在入度为0的点)所以对入度为0的点进行DFS前两种情况必定能访问到该连通分量内所有的点。 http://soj.me/show_problem.php?pid=1001&cid=970#include <iostr 阅读全文
posted @ 2013-04-13 11:48 Norcy 阅读(337) 评论(0) 推荐(0)
摘要:“如果抛开某些很NB,很强大,很邪恶的递归式不谈,如果不能有效的确定普通递归式和一些典型算法递归式的复杂度,那么这个人显然不是合格的Coder。”所以知道了解主定理的好处至少有一个,那就是可以快速分析某些递归式或某些算法的复杂度。引理:主定理:1.画个递归树,发现一共有logn层。2.为什么是d与logba比较每一层的复杂度是上一层的1/b,而子问题的个数是上一层的a倍所以第k层的工作量为ak*O(n/bk)d即O(nd)*(a/bd)k所以公比为(a/bd),就是看(a/bd)是否大于1小于1等于1啦~ 算法中的应用: 阅读全文
posted @ 2013-04-13 11:03 Norcy 阅读(353) 评论(0) 推荐(0)
摘要:RSA算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但那时想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥。在公开密钥密码体制中,加密密钥(即公开密钥)PK是公开信息,而解密密钥(即秘密密钥)SK是需要保密的。加密算法E和解密算法D也都是公开的。虽然秘密密钥SK是由公开密钥PK决定的,但却不能根据PK计算出SK。准备工作:1.选择两个质数p, q2.令N = pq, e 为 与(p-1)(q-1)互质的数 (为了快速解密,若符合条件一般选择最小的3) (N, e)为公钥3.令d为e mod (p-1)(q-1)的逆。(N,d)为私钥现在就得到3个数,N e d。设 阅读全文
posted @ 2013-04-13 10:35 Norcy 阅读(285) 评论(0) 推荐(0)
摘要:耐心的看下去终究会懂的。♦ 我们规定gcd(a,0)=a♦欧几里德算法中需要明确的是,gcd(a,b) = gcd(b,r)证明:设x=gcd(a,b),那么b能整除以x(即b/x没有余数,觉得"整除以"比"整除"更直观,下同)。∵a=bq+r∴r=a-bq∴r能整除以x∴x为b,r的公约数(没有证明是最大)假设y为gcd(b,r),所以x≤y(y是最大的约数!)显然,b能整除以y∵a=bq+r∴a能整除以y.∴y是a,b的约数∴y≤x∴x=y=gcd(b,r)∴gcd(a,b) = gcd(b,r)求两个数的最大公约数——辗转相除法(欧几里德算法) 1 阅读全文
posted @ 2013-03-23 21:50 Norcy 阅读(355) 评论(1) 推荐(0)
摘要:站在acm的角度上看,《算法概论》这本书上感觉废话好多。始终觉得学算法的最佳途径就是做题,而不是单纯的看书看伪算法。实现才是王道。快速幂取模,其实也是以前写过的二分求幂,或离散课上的同余幂。以前写的代码版本太乱,现在总结一下,以便以后碰到这类问题能直接对症下药。求b^e mod m,b,e,m都是比较大的整数。递归算法: 1 //快速幂取模 2 int modexp(int b, int e, int m) //b是底数,e是指数,m是模 3 { 4 if (e == 0) 5 return 1; 6 7 int temp = modexp(... 阅读全文
posted @ 2013-03-23 17:48 Norcy 阅读(408) 评论(0) 推荐(0)
摘要:费马小定理是初等数论四大定理(威尔逊定理,欧拉定理,中国剩余定理和费马小定理)之一。费马小定理:若p是质数,且a,p互质,则a^(p-1) ≡ 1 mod p.证明:1)ax≡bx(mod p),且x,p互质,则a≡b(mod p)2){1,2,3……,p-1} = {a mod p, 2a mod p, 3a mod p, ……(p-1)a mod p}证:i ≠ j, (1≤i,j≤p-1,p是质数)则ia ≠ ja (mod p) 由1)得证3)1*2*3*……*p-1 (mod p) ≡a*2a*3a*……*(p-1)a (mod p) 化简,得a^(p-1) ≡ 1 mod p作用— 阅读全文
posted @ 2012-10-05 15:15 Norcy 阅读(259) 评论(0) 推荐(0)

1