BIG NUMBER 算法竞赛入门经典 刘汝佳
动态规划(二) 刘汝佳 黑书 课件 经典
定理
定理 定理(F.Yao): 若w满足四边形不等式, 则d也 满足四边形形不等式, 即 d[i,j]+d[i',j']<=d[i',j]+d[i,j'], i<=i'<=j<=j' 证明: 对长度i=j'-i归纳, 显然l<=1时正确. i=i' 或j=j'时(同一行或同一列), 等式显然成立
– 情形 i'=j, 退化为反三角不等式 情形1: – 情形 i'<j 情形2:
情形1. 反三角不等式
i'=j时, d[i,j]+d[i',j']<=d[i',j]+d[i,j']退化为 d[i,j]+d[j,j']<=d[i,j'] 设k为让d[i,j']取最小值的决策(有多个时取最 大的一个k, 后同). 若k<=j, 则k是计算d[i,j]考虑过的合法决策 合法决策 d[i,j]<=w[i,j]+d[i,k-1]+d[k,j] 两边加上d[j,j'], 得 d[i,j]+d[j,j']<=w[i,j]+d[i,k-1]+d[k,j]+d[j,j']
决策单调性
进一步地, d的凸性可以推出决策的单调性 设k[i,j]为让d[I,j]取最小值的决策, 下面证明 k[i,j]<=k[i,j+1]<=k[i+1,j+1], i<=j 即: k在同行同列上都是递增的 证明: i=j时显然成立. 由对称性, 只需证明 k[i,j]<=k[i,j+1]. 记dk[I,j]=d[I,k-1]+d[k,j]+w[I,j], 则只需要证明对于所有的i<k<=k'<=j, 有 dk'[i,j]<=dk[i,j] dk'[i,j+1]<=dk[i,j+1]
高一入坑数竞必备书单
高一入坑数竞必备书单高一是数竞的入门阶段,对于刚入坑的同学来说,选择合适的书籍是非常重要的。
下面是我为高一数竞入门推荐的必备书单。
1.《算法竞赛入门经典》这本书是数竞入门的经典教材,由刘汝佳编写。
它详细介绍了算法竞赛的基本知识和常用算法,包括排序、搜索、图论等内容。
书中的例题和习题很有代表性,对初学者非常友好。
阅读完这本书,你将对数竞的基本知识有一个全面的了解。
2.《算法竞赛入门经典训练指南》这本书是《算法竞赛入门经典》的配套训练指南,由刘汝佳和冯宇航编写。
它包含了大量的例题和习题,涵盖了各个难度级别。
通过做这些题目,你可以巩固和深入理解书中所讲的知识,并提高自己的编程能力。
3.《挑战程序设计竞赛》这本书是日本的一本经典教材,由吉田和弥和寺田康平合著。
它介绍了更多的算法和高级技巧,包括动态规划、贪心算法、网络流等。
书中的例题和习题很有挑战性,适合有一定基础的同学进一步提高自己的竞赛水平。
4.《算法导论》这本书是计算机科学领域的经典教材,由Thomas H.Cormen等人合著。
它系统地介绍了算法设计和分析的基本原理,包括分治法、动态规划、贪心算法等。
虽然这本书的内容相对较为深入,但对于对算法有浓厚兴趣的同学来说,是一本非常值得阅读的书籍。
5.《挑战程序设计竞赛2》这本书是《挑战程序设计竞赛》的续篇,由吉田和弥和寺田康平合著。
它进一步扩展了算法和高级技巧的内容,包括数据结构、字符串算法、几何算法等。
书中的例题和习题更加复杂和有趣,对于想要挑战自己的同学来说是一本很好的选择。
6.《算法笔记》这本书是浙江大学计算机学院的一本教材,由胡凡、曾磊等人合著。
它以清晰简洁的语言介绍了算法的基本概念和常用算法,适合初学者入门。
书中的例题和习题有助于巩固所学知识,并提供了一些思考和扩展的方向。
以上是我为高一入坑数竞推荐的必备书单。
当然,每个人的学习方式和兴趣爱好都不同,选择适合自己的书籍很重要。
希望这些建议对你有所帮助,祝你在数竞的道路上取得好成绩!。
数学伴你学答案
数学伴你学答案数学,作为一门重要的学科,是我们日常生活中不可或缺的一部分。
无论是做生意、计算货币、解决计划或者是进行科学研究,数学都是必不可少的工具。
然而,毫不吹捧地说,数学并不是每个人都喜欢的学科。
一些学生,甚至有点害怕数学,认为它难以理解,甚至认为数学是“毒瘤”。
事实上,并非每一个人都可以像Einstein那样,将数学视为“最高坦白的哲学。
”但是,通过改变对数学的态度和方法,每个人都可以掌握数学的基础知识,并理解如何将这些理论应用于日常生活。
通过良好的数学学习方法和技巧的掌握,我们可以大大提高我们的数学理解和能力,让我们一起来探讨一下。
首先,最重要的是改变对数学的态度。
虽然很多人觉得数学很难,但事实上数学不难。
数学对于我们的生活是至关重要的。
数学是解决现实问题的重要工具,有很多真实的例子可以证明这点。
因此我们必须对数学学习充满信心,不能放弃,要对它抱乐观的态度。
其次,要学会用正确的方法来学习数学。
除了学习数学概念和理论外,我们可以结合一些实践来掌握数学技巧。
刘汝佳的“算法竞赛入门经典”或Skiena的“算法设计和分析基础原理”等教材,在学习数字理论,组合数学,图论等数学相关技巧时是非常有用的资料,它们可以帮助我们学习数学知识,同时也培养了我们的思维和解决问题的能力。
此外,我们还可以选择一些在线数学课程或视频资源,如卡内基梅隆大学的公开课、麻省理工学院的OCW 等,它们可以帮助我们更有效地学习数学知识。
最后,我们可以通过练习来提高我们的数学能力。
做数学题并不是在考试前好好准备,或者仅在学校课堂上做作业,而是一个日常学习过程。
我们可以通过刷题来提高我们的技能,并确保我们对数学知识的掌握程度。
可以使用一些教辅资料或者在线平台,如Khan Academy、真题人生、知乎问题广场等,帮助我们更好地进行练习和复习。
总的来说,数学对我们的生活至关重要,并且学习数学可以提高我们的思维解决问题的能力,从而更好地适应现代复杂的社会格局。
信息学奥赛(NOIP)必看经典书目汇总!
信息学奥赛(NOIP)必看经典书目汇总!最近有人在群里问,为什么没有关于NOIP的复习资料推荐贴!今天小编就来整理汇总一下大神们极力推荐的复习资料!(欢迎大家查漏补缺)•基础篇1、《全国青少年信息学奥林匹克分区联赛初赛培训教材》(推荐指数:4颗星)曹文,吴涛编著,知识点大杂烩,部分内容由学生撰写,但是对初赛知识点的覆盖还是做得相当不错的。
语言是pascal的。
2、谭浩强老先生写的《C语言程序设计(第三版)》(推荐指数:5颗星)针对零基础学C语言的筒子,这本书是必推的。
3、《骗分导论》(推荐指数:5颗星)参加NOIP必看之经典4、《全国信息学奥林匹克联赛培训教程(一)》(推荐指数:5颗星)传说中的黄书。
吴文虎,王建德著,系统地介绍了计算机的基础知识和利用Pascal语言进行程序设计的方法5、《全国青少年信息学奥林匹克联赛模拟训练试卷精选》王建德著,传说中的红书。
6、《算法竞赛入门经典》(推荐指数:5颗星)刘汝佳著,算法必看经典。
7、《算法竞赛入门经典:训练指南》(推荐指数:5颗星)刘汝佳著,《算法竞赛入门经典》的重要补充•提高篇1、《算法导论》(推荐指数:5颗星)这是OI学习的必备教材。
2、《算法艺术与信息学竞赛》(推荐指数:5颗星)刘汝佳著,传说中的黑书。
3、《学习指导》(推荐指数:5颗星)刘汝佳著,《算法艺术与信息学竞赛》的辅导书。
(PS:仅可在网上搜到,格式为PDF)。
4、《奥赛经典》(推荐指数:5颗星)有难度,但是很厚重。
5、《2016版高中信息学竞赛历年真题解析红宝书》(推荐指数:5颗星)历年真题,这是绝对不能遗失的存在。
必须要做!。
String 算法竞赛入门经典 刘汝佳概要
Linear Cellular AutomataA biologist is experimenting with DNA modification of bacterial colonies being grown in a linear array of culture dishes. By changing the DNA, he is able ``program" the bacteria to respond to the population density of the neighboring dishes. Population is measured on a four point scale (from 0 to 3. The DNA information is represented as an array DNA, indexed from 0 to 9, of population density values and is interpreted as follows:In any given culture dish, let K be the sum of that culture dish's density and the densities of the dish immediately to the left and the dish immediately to the right. Then, by the next day, that dish will have a population density of DNA[K].The dish at the far left of the line is considered to have a left neighbor with population density 0.The dish at the far right of the line is considered to have a right neighbor with population density 0. Now, clearly, some DNA programs cause all the bacteria to die off (e.g., [0,0,0,0,0,0,0,0,0,0]. Others result in immediate population explosions (e.g.,[3,3,3,3,3,3,3,3,3,3]. The biologist is interested in how some of the less obvious intermediate DNA programs might behave.Write a program to simulate the culture growth in a line of 40 dishes, assuming that dish 20 starts with a population density of 1 and all other dishes start with a population density of 0.InputThe input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.For each input set your program will read in the DNA program (10 integer values on one line.OutputFor each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.For each input set it should print the densities of the 40 dishes for each of the next 50 days. Each day's printout should occupy one line of 40 characters. Each dish is represented by a single character on that line. Zero population densities are to be printed as the character ` '. Population density 1 will be printed as the character `.'. Population density 2 will be printed as the character `x'. Population density 3 will be printed as the character `W'.Sample Input10 1 2 0 1 3 3 2 3 0Sample Outputbbbbbbbbbbbbbbbbbbb.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb...bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.xbx.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.bb.bb.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.........bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.xbbbbbbbx.bbbbbbbbbbbbbbbbbbbbbbbbbbbb.bbxbbbbbxbb.bbbbbbbbbbbbbbbbbbbbbbbbbb...xxxbbbxxx...bbbbbbbbbbbbbbbbbbbbbbbb.xb.WW.xbx.WW.bx.bbbbbbbbbbbbbbbbbbbbbb.bbb.xxWb.bWxx.bbb.bbbbbbbbbbbNote: Whe show only the first ten lines of output (the total number of lines must be 50 and the spaces have been replaced with the character "b" for ease of reading. The actual output file will use the ASCII-space character, not "b".。
蓝书刘汝佳算法竞赛入门经典勘误
#《算法竞赛入门经典》勘误关于勘误¶下面的勘误很多来自于热心读者,再次向他们表示衷心的感谢!我并不清楚这些错误实际是在哪个版本中改正过来的,所以麻烦大家都看一下。
有发现新错误的欢迎大家在留言中指出,谢谢!一些一般性的问题¶运算符<?、>?已经被废弃,请用min、max代替(代码仓库中的代码已更新,g++ 4.6.2下编译通过)重大错误¶p24. 最后一行,“然后让max=INF,而min=-INF”应该是“然后让max=-INF, 而min=INF”。
(感谢imxivid)p43. 最后,判断s[i..j]是否为回文串的方法也不难写出:int ok = 1; for(k = i; i<=j; i++)应该为for(k = i; k<=j; k++)(感谢imxivid)p45. 第七行和第九行i-j+1应为i+j+1。
修改后:1. {2. for (j = 0; i - j >= 0 && i + j < m; j++)3. {4. if (s[i - j] != s[i + j]) break;5. if (j*2+1 > max) { max = j*2+1; x = p[i - j]; y = p[i + j];}6. }7. for (j = 0;i - j >= 0 && i + j + 1 < m; j++) 8. { 9. if (s[i - j] != s[i + j + 1]) break; 10. if (j*2+2 > max) 11. {max = j*2+2; x = p[i - j]; y = p[i + j + 1]; } 12. } 13. }p53. 例题4-1. 组合数. 输入非负整数n和m,这里的n和m写反了。
应是“输入非负整数m和n”。
湘潭大学ACM-ICPC新人训练导引
0 POJ 1306 Combinations
3 HDU 2074 叠筐
0 POJ 1503 Integer Inquiry
3 HDU 2516 取石子游戏
0 POJ 1936 All in All
3 HDU 2540 遮挡判断
0 POJ 1979 Red and Black
3 HDU 3809 Decrypt coordinate
期越来越快,一天刷 20-30 道水题完全无难度的。主要不要一味的刷水题过瘾,需要结合学
习的知识点,有针对性地做题,这样才能保证上水平。低水平重复是没有什么意义的。
3. 三人行必有我师。善于与周围的队员交流,乐于分享自己的学习感悟。团队的进步 和个人的进步是相互促进的。善于利用网络资源,比如 QQ 群等,和全国的 ACMer 进行交
7. 建议有一定基础的同学在网上多多参加比赛,比如 USACO,TopCoder,Codeforce,
HDOJ,ZJU,FZU 等,经常有比赛的。国内流行的做题网站如下表。
名称
地址
备注
USACO Training
/usacogate
OI 风格,进阶练习模式
USACO Contest
五、 知识点及推荐习题
考虑到时间关系,暂时推荐大家从刘汝佳的《算法竞赛入门经典》入手,掌握基本的知
识点。预计到下个学期前半段,大家能掌握这本书的大部分内容就已经很不错的。这个学期
马上进入考试阶段,暂时不会有集训或者讲座的活动。
作业题列表及 OJ 地址 难度 题目
难度 题目
0 HDU 2030 字符统计
34th ACM-ICPC 亚洲分区赛-上海 34th ACM-ICPC 亚洲分区赛-哈尔滨
算法竞赛-入门经典-作者刘汝佳
算法竞赛-入门经典-作者刘汝佳.doc第1部分语言篇第1章程序设计入门学习目标☑熟悉C语言程序的编译和运行☑学会编程计算并输出常见的算术表达式的结果☑掌握整数和浮点数的含义和输出方法☑掌握数学函数的使用方法☑初步了解变量的含义☑掌握整数和浮点数变量的声明方法☑掌握整数和浮点数变量的读入方法☑掌握变量交换的三变量法☑理解算法竞赛中的程序三步曲:输入、计算、输出☑记住算法竞赛的目标及其对程序的要求计算机速度快,很适合做计算和逻辑判断工作。
本章首先介绍顺序结构程序设计,其基本思路是:把需要计算机完成的工作分成若干个步骤,然后依次让计算机执行。
注意这里的“依次”二字——步骤之间是有先后顺序的。
这部分的重点在于计算。
接下来介绍分支结构程序设计,用到了逻辑判断,根据不同情况执行不同语句。
本章内容不复杂,但是不容忽视。
注意:编程不是看会的,也不是听会的,而是练会的,所以应尽量在计算机旁阅读本书,以便把书中的程序输入到计算机中进行调试,顺便再做做上机练习。
千万不要图快——如果没有足够的时间用来实践,那么学得快,忘得也快。
(为帮助没有分值的朋友能下载,特此修改文档,以免上传不了)1.1 算术表达式计算机的“本职”工作是计算,因此下面先从算术运算入手,看看如何用计算机进行复杂的计算。
程序1-1 计算并输出1+2的值#include<stdio.h>int main(){printf("%d\n", 1+2);return 0;}算法竞赛入门经典这是一段简单的程序,用于计算1+2的值,并把结果输出到屏幕。
如果你不知道如何编译并运行这段程序,可阅读附录或向指导教师求助。
即使你不明白上述程序除了“1+2”之外的其他内容,仍然可以进行以下探索:试着把“1+2”改成其他东西,而不要去修改那些并不明白的代码——它们看上去工作情况良好。
下面让我们做4个实验:实验1:修改程序1-1,输出3-4的结果实验2:修改程序1-1,输出5×6的结果实验3:修改程序1-1,输出8÷4的结果实验4:修改程序1-1,输出8÷5的结果直接把“1+2”替换成“3+4”即可顺利解决实验1,但读者很快就会发现:无法在键盘上找到乘号和除号。
ACM-ICPC比赛随想——刘汝佳
ACM-ICPC比赛随想——刘汝佳刘汝佳,1982年12月生,毕业于重庆外国语学校,清华大学计算机科学与技术系2005级研究生。
高二时创立“信息学初学者之家”网站(OIBH),高三入选IOI2001国家集训队。
大学一年级时获ACM/ICPC世界总决赛银牌(世界第四),IOI2002/03/04国家集训队指导老师。
曾与黄亮合作出版了《算法艺术与信息学竞赛》丛书,自2002年至今为科学委员会学生委员,在命题方面和辅导学生方面成绩突出,同时兼任NOI网站总监。
从第一次听说ACM/ICPC到现在,已经有快七个年头了。
最开始因好奇而关注,而现在因了解而关注——关注比赛,更关注参加比赛的人。
ACM/ICPC是一个五味瓶。
没有接触过它的人不会知道其中的酸甜苦辣,而一旦置身其中,每个选手都会对它产生一种特殊的感情,时间越长,这种感情也越复杂、越浓烈。
感情来源于对算法与题目的喜爱,来源于对成功的向往和失败的恐惧,来源于各种选择与放弃中的徘徊与摇摆不定,来源于程序世界与现实生活的巨大差异,也来源于通往理想的曲折道路——探索其中时的无助和艰辛。
等到退役的那一天,回过头来再看当时的自己,相信每位选手都会发现自己的很多方面成熟了许多——远不只是编程能力和算法功底。
以前我觉得这是比赛的副产品,而现在我认为这才是比赛的主要目的,至少对于选手自己是如此。
虽然我从心里喜欢这个比赛,但我并不鼓励每个人参加。
并不是每个人在每个时期都适合参加这个比赛,且适合的人选也并不代表一定能取得好成绩——比赛场上是没有“一定”的,任何一个选手都必须有勇气承担风险,就像所有其他有潜在回报的事一样。
另一方面,对于所有下定决心参加比赛的选手,我鼓励他坚持到底,因为只有这样才会受到真正的磨练。
在“参加”与“不参加”的岔路口上,大多数选手被两个问题所困绕。
第一个是“我能获奖么?奖会给我带来什么好处?”,第二个是“抛开荣誉,从比赛中学到的东西值得我花费这么多时间吗?”。
acm程序设计竞赛 数学基础 刘汝佳
数学基础(版本2009)刘汝佳例1. 同构计数•一个竞赛图是这样的有向图–任两个不同的点u、v之间有且只有一条边–不存在自环•用P表示对竞赛图顶点的一个置换。
当任两个不同顶点u、v间直接相连的边的方向与顶点P(u)、P(v)间的一样时,称该图在置换P下同构•对给定的置换P,我们想知道对多少种竞赛图在置换P下同构分析•先把置换它分解成为循环, 首先证明长度为L的偶循环将导致无解–对于点i1, 记P(i k)=i k+1, 假设i1和i L/2+1的边方向为i1->i L/2+1, 那么有i2->i L/2+2, i3->i L/2+3, …, i L/2+1->i1, 和假设矛盾!•假设确定其中k条独立边后其他边也会确定, 则答案为2k•考虑两类边: 循环内边和循环间边.分析•循环内顶点的关系–定了i 1和i j 之间的关系, i k 与i (k+j-2) mod n+1之间的关系也被确定下来了, 因此只需要确定i 1和i 2, i 3, …, i (L-1)/2+1这(L-1)/2对顶点的关系•不同循环间顶点的关系–设循环为(i 1,i 2,…,i L1)和(j 1,j 2,…,j L2), 通过类似分析得只需要确定gcd(L1, L2)对关系即可分析•最后答案为2k1+k2•其中k1=sum{(L-1)/2}, k2=sum{gcd(L1, L2)}•可以用二分法加速求幂例2. 图形变换•平面上有n个点需要依次进行m个变换处理•规则有4种, 分别把(x0,y)变为–平移M(x, y): (x0+x, y0+y)–缩放Z(L): (Lx0, Ly0)–翻转F(0): (x0, -y0); F(1): (-x0, y0)–旋转R(a): a为正时逆时针, 离原点距离不变, 旋转a度•给n(<=106)个点和m(<=106)条指令•求所有指令完成后每个点的坐标分析•如果直接模拟, 每次需要O(n)的时间, 一共O(nm), 无法承受•把点(x0, y)写成列向量[x, y]T, 则后3种变换可以都可以写成矩阵–缩放P’= Z * P, Z = [L 0; 0 L]–翻转P’= F * P, F = [1 0; 0 -1]或[-1 0; 0 1]–旋转P’= R * P, R = [cosa–sina; sina cosa]•可是无法实现平移, 怎么办呢?分析•修改表达方式, 令P = [x 0, y 0, 1]T , 则四种变换的矩阵M, Z, F, R 分别为100001,00001001x L M y Z L ⎡⎤⎡⎤⎢⎥⎢⎥==⎢⎥⎢⎥⎢⎥⎢⎥⎣⎦⎣⎦100100010010001001F −⎡⎤⎡⎤⎢⎥⎢⎥=−⎢⎥⎢⎥⎢⎥⎢⎥⎣⎦⎣⎦或cos sin 0,sin cos 0001a a R a a −⎡⎤⎢⎥=⎢⎥⎢⎥⎣⎦分析•只需要先计算所有变换矩阵的乘积A, 然后对于每个点, P’= A * P•注意: 矩阵乘法不满足交换律, 因此需要按照输入顺序进行乘法•每次矩阵乘法需要33=27次乘法, 则计算A一共需要27m次乘法, 对所有点变换需要27n 次乘法, 一共27(n+m)次例3. 染色方案•N*M(N<=10100, M<=5)的格子纸,每个格子被填上黑色或者白色。
信息学竞赛中的学习资源与参考书籍推荐
信息学竞赛中的学习资源与参考书籍推荐信息学竞赛是当今世界中备受推崇的一项学科竞赛,对于提高学生的计算机科学和算法设计能力具有重要意义。
然而,要在竞赛中脱颖而出并取得好成绩,一个合适的学习资源与参考书籍是不可或缺的。
本文将为您推荐一些优质的学习资源和参考书籍,帮助您在信息学竞赛中取得更好的成绩。
一、在线学习平台1. Luogu(洛谷)Luogu是一个综合性的在线学习平台,它提供了大量的题目和算法讲解,适合各个级别的学习者。
在Luogu上,您可以找到来自不同竞赛的真题与模拟题,同时还有详细的解题思路与讲解。
此外,Luogu还提供了在线编程环境,方便学生在线编写、提交与调试程序。
这使得Luogu成为了信息学竞赛中不可或缺的学习资源。
2. CodeforcesCodeforces是一个面向全球的在线编程竞赛平台,它旨在为竞赛选手提供一个交流与切磋的平台。
在Codeforces上,您可以找到大量来自世界各地的算法竞赛题目,这些题目涵盖了各种不同的算法和数据结构。
同时,Codeforces也有丰富的讨论板块,您可以在这里与其他竞赛选手交流经验和解题思路。
无论是初学者还是高手,Codeforces都是一个值得去探索和参与的平台。
二、参考书籍推荐1.《算法导论》(Thomas H. Cormen等著)《算法导论》是一本经典的算法教材,对于了解算法设计与分析有着重要的作用。
本书系统地介绍了各种算法和数据结构,包括排序、图算法、动态规划等。
每个算法都配有详细的算法分析与实例,有助于读者理解算法的原理和应用场景。
此外,本书还配有大量习题与解答,可以帮助读者巩固所学知识。
2.《挑战程序设计竞赛(第2版)》(Antti Laaksonen著)《挑战程序设计竞赛》是一本介绍信息学竞赛算法和数据结构的经典教材。
本书以实用性为导向,注重解题思路和编程技巧的讲解。
它涵盖了信息学竞赛中常见的题型和算法,包括贪心算法、动态规划、图算法等。
动态规划(一) 刘汝佳 黑书 课件 经典
直接书写的程序
递归树分析
关键点二: 重叠子问题
• 为了让动态规划确实发挥功效, 问题应该包含尽 量多的重叠子问题(overlapping subproblems)
解决方法: 记忆化
• 注意memoization不是memorization
分析
• 设d[i,j]为以j项结尾的i段和的最大值, 则需要 枚举此段开头y和上一段结尾x, 即 d[i,j]=max{d[i-1,x] + a[y..j]} • 每次需要枚举x<y<=j, 决策量为O(n2), 状态 为O(nm), 共O(n3m) • 注意到如果a[j-1]也是本段的, 答案变成为 d[i,j-1]+a[j], 因此方程优化为 d[i,j]=max{d[i,j-1]+a[j], d[i-1,x]+a[j]}, x<j
自底向上递推
空间优化
• 如果只需要最优值, 可以用滚动数组实现 • 按照i递增的顺序计算, d[i,j]只和d[i-1,j]和 d[i,j-1]以及d[i-1,j-1]有关系,因此只需要保 留相邻两行, 空间复杂度为O(min{m,n}) • 更进一步的, 可以只保留一行, 每次用单独 的变量x保留d[i-1,j], 则递推方程为 If(i==j) d[j]=x; else { x = d[j]; d[j]=max{d[j-1], d[j]} };
分析
• 优化后状态仍然是二维的,但决策减少为 O(n), 总O(n2m) • 可以继续优化. 注意到时间主要耗费在对x 的枚举上, 计算max{d[i-1,x]}. 这个值… • 我们把d的第一维称为”阶段”, 则本题是 典型的多阶段决策问题
NOIP2000-2009提高组解题报告-推荐下载
容易想到的思路是: 令 d[x1,y1,x2,y2]表示甲在 x1,y1,乙在 x2,y2 以后(包括取走(x1,y1))的过程中可以获得的最大和。 则 d[1,1,1,1]就是原问题的解。
X111 21 222X2
12 12 1X1 2X
变成: X222 12
对全部高中资料试卷电气设备,在安装过程中以及安装结束后进行高中资料试卷调整试验;通电检查所有设备高中资料电试力卷保相护互装作置用调与试相技互术通关,1系电过,力管根保线据护敷生高设产中技工资术艺料0不高试仅中卷可资配以料置解试技决卷术吊要是顶求指层,机配对组置电在不气进规设行范备继高进电中行保资空护料载高试与中卷带资问负料题荷试2下卷2,高总而中体且资配可料置保试时障卷,各调需类控要管试在路验最习;大题对限到设度位备内。进来在行确管调保路整机敷使组设其高过在中程正资1常料中工试,况卷要下安加与全强过,看度并22工且22作尽22下可22都能22可地护以缩1关正小于常故管工障路作高高;中中对资资于料料继试试电卷卷保破连护坏接进范管行围口整,处核或理对者高定对中值某资,些料审异试核常卷与高弯校中扁对资度图料固纸试定,卷盒编工位写况置复进.杂行保设自护备动层与处防装理腐置,跨高尤接中其地资要线料避弯试免曲卷错半调误径试高标方中高案资等,料,编试要5写、卷求重电保技要气护术设设装交备备置底4高调、动。中试电作管资高气,线料中课并敷3试资件且、设卷料中拒管技试试调绝路术验卷试动敷中方技作设包案术,技含以来术线及避槽系免、统不管启必架动要等方高多案中项;资方对料式整试,套卷为启突解动然决过停高程机中中。语高因文中此电资,气料电课试力件卷高中电中管气资壁设料薄备试、进卷接行保口调护不试装严工置等作调问并试题且技,进术合行,理过要利关求用运电管行力线高保敷中护设资装技料置术试做。卷到线技准缆术确敷指灵设导活原。。则对对:于于在调差分试动线过保盒程护处中装,高置当中高不资中同料资电试料压卷试回技卷路术调交问试叉题技时,术,作是应为指采调发用试电金人机属员一隔,变板需压进要器行在组隔事在开前发处掌生理握内;图部同纸故一资障线料时槽、,内设需,备要强制进电造行回厂外路家部须出电同具源时高高切中中断资资习料料题试试电卷卷源试切,验除线报从缆告而敷与采设相用完关高毕技中,术资要资料进料试行,卷检并主查且要和了保检解护测现装处场置理设。备高中资料试卷布置情况与有关高中资料试卷电气系统接线等情况,然后根据规范与规程规定,制定设备调试高中资料试卷方案。
Brute Force Elementary Skills 算法竞赛入门经典 刘汝佳
10167 - Birthday CakeBackgroundLucy and Lily are twins. Today is their birthday. Mother buys a birthday cake for them.Now we put the cake onto a Descartes coordinate. Its center is at (0,0), and the cake's length of radius is 100.There are 2N (N is a integer, 1<=N<=50) cherries on the cake. Mother wants to cut the cake into two halves with a knife (of course a beeline). The twins would like to be treated fairly, that means, the shape of the two halves must be the same (that means the beeline must go through the center of the cake) , and each half must have N cherrie(s). Can you help her?Note: the coordinate of a cherry (x , y) are two integers. You must give the line as form two integers A,B(stands for Ax+By=0), each number in the range [-500,500]. Cherries are not allowed lying on the beeline. For each dataset there is at least one solution.InputThe input file contains several scenarios. Each of them consists of 2 parts: The first part consists of a line with a number N, the second part consists of 2N lines, each line has two number, meaning (x,y) .There is only one space between two border numbers. The input file is ended with N=0.OutputFor each scenario, print a line containing two numbers A and B. There should be a space between them. If there are many solutions, you can only print one of them.Sample Input2-20 20-30 20-10 -5010 -5Sample Output0 111205 - The broken pedometerThe ProblemA marathon runner uses a pedometer with which he is having problems. In the pedometer the symbols are represented by seven segments (or LEDs):But the pedometer does not work properly (possibly the sweat affected the batteries) and only some of the LEDs are active. The runner wants to know if all the possible symbols:can be correctly identified. For example, when the active LEDs are:numbers 2 and 3 are seen as:so they cannot be distinguished. But when the active LEDs are:the numbers are seen as:and all of them have a different representation.Because the runner teaches algorithms at University, and he has some hours to think while he is running, he has thought up a programming problem which generalizes the problem of his sweat pedometer. The problem consists of obtaining the minimum number of active LEDs necessary to identify each one of the symbols, given a number P of LEDs, and N symbols to be represented with these LEDs (along with the codification of each symbol).For example, in the previous sample P = 7 and N = 10. Supposing the LEDs are numbered as:The codification of the symbols is: "0" = 1 1 1 0 1 1 1; "1" = 0 0 1 0 0 1 0; "2" = 1 0 1 1 1 0 1; "3" = 1 0 1 1 0 1 1; "4" = 0 1 1 1 0 1 0; "5" = 1 1 0 1 0 1 1; "6" = 1 1 0 1 1 1 1; "7" = 1 0 1 0 0 1 1; "8" = 1 1 1 1 1 1 1; "9" = 1 1 1 1 0 1 1. In this case, LEDs 5 and 6 can be suppressed without losing information, so the solution is 5.The InputThe input file consists of a first line with the number of problems to solve. Each problem consists of a first line with the number of LEDs (P), a second line with the number of symbols (N), and N lines each one with the codification of a symbol. For each symbol, the codification is a succession of 0s and 1s, with a space between them. A 1 means the corresponding LED is part of the codification of the symbol. The maximum value of P is 15 and the maximum value of N is 100. All the symbols have different codifications.The OutputThe output will consist of a line for each problem, with the minimum number of active LEDs necessary to identify all the given symbols.Sample Input27101 1 1 0 1 1 10 0 1 0 0 1 01 0 1 1 1 0 11 0 1 1 0 1 10 1 1 1 0 1 01 1 0 1 0 1 11 1 0 1 1 1 11 0 1 0 0 1 01 1 1 1 1 1 11 1 1 1 0 1 16100 1 1 1 0 01 0 0 0 0 01 0 1 0 0 01 1 0 0 0 01 1 0 1 0 01 0 0 1 0 01 1 1 0 0 01 1 1 1 0 01 0 1 1 0 00 1 1 0 0 0Sample Output54131 - The Psychic Poker PlayerIn 5-card draw poker, a player is dealt a hand of five cards (which may be looked at). The player may then discard between zero and five of his or her cards and have them replaced by the same number of cards from the top of the deck (which is face down). The object is to maximize the value of the final hand. The different values of hands in poker are given at the end of this problem.Normally the player cannot see the cards in the deck and so must use probability to decide which cards to discard. In this problem, we imagine that the poker player is psychic and knows which cards are on top of the deck. Write a program which advises the player which cards to discard so as to maximize the value of the resulting hand.Input and OutputInput will consist of a series of lines, each containing the initial five cards in the hand then the first five cards on top of the deck. Each card is represented as a two-character code. The first character is the face-value (A=Ace, 2-9, T=10, J=Jack, Q=Queen, K=King) and the second character is the suit (C=Clubs, D=Diamonds, H=Hearts, S=Spades). Cards will be separated by single spaces. Each input line will be from a single valid deck, that is there will be no duplicate cards in each hand and deck.Each line of input should produce one line of output, consisting of the initial hand, the top five cards on the deck, and the best value of hand that is possible. Input is terminated by end of file.Use the sample input and output as a guide. Note that the order of the cards in the player's hand is irrelevant, but the order of the cards in the deck is important because the discarded cards must be replaced from the top of the deck. Also note that examples of all types of hands appear in the sample output, with the hands shown in decreasing order of value. Sample InputTH JH QC QD QS QH KH AH 2S 6S2H 2S 3H 3S 3C 2D 3D 6C 9C TH2H 2S 3H 3S 3C 2D 9C 3D 6C TH2H AD 5H AC 7H AH 6H 9H 4H 3CAC 2D 9C 3S KD 5S 4D KS AS 4CKS AH 2H 3C 4H KC 2C TC 2D ASAH 2C 9S AD 3C QH KS JS JD KD6C 9C 8C 2D 7C 2H TC 4C 9S AH3D 5S 2H QD TD 6S KH 9H AD QHSample OutputHand: TH JH QC QD QS Deck: QH KH AH 2S 6S Best hand: straight-flushHand: 2H 2S 3H 3S 3C Deck: 2D 3D 6C 9C TH Best hand: four-of-a-kindHand: 2H 2S 3H 3S 3C Deck: 2D 9C 3D 6C TH Best hand: full-houseHand: 2H AD 5H AC 7H Deck: AH 6H 9H 4H 3C Best hand: flushHand: AC 2D 9C 3S KD Deck: 5S 4D KS AS 4C Best hand: straightHand: KS AH 2H 3C 4H Deck: KC 2C TC 2D AS Best hand: three-of-a-kindHand: AH 2C 9S AD 3C Deck: QH KS JS JD KD Best hand: two-pairsHand: 6C 9C 8C 2D 7C Deck: 2H TC 4C 9S AH Best hand: one-pairHand: 3D 5S 2H QD TD Deck: 6S KH 9H AD QH Best hand: highest-card146 - ID CodesIt is 2084 and the year of Big Brother has finally arrived, albeit a century late. In order to exercise greater control over its citizens and thereby to counter a chronic breakdown in law and order, the Government decides on a radical measure--all citizens are to have a tiny microcomputer surgically implanted in their left wrists. This computer will contains all sorts of personal information as well as a transmitter which will allow people's movements to be logged and monitored by a central computer. (A desirable side effect of this process is that it will shorten the dole queue for plastic surgeons.)An essential component of each computer will be a unique identification code, consisting of up to 50 characters drawn from the 26 lower case letters. The set of characters for any given code is chosen somewhat haphazardly. The complicated way in which the code is imprinted into the chip makes it much easier for the manufacturer to produce codes which are rearrangements of other codes than to produce new codes with a different selection of letters. Thus, once a set of letters has been chosen all possible codes derivable from it are used before changing the set.For example, suppose it is decided that a code will contain exactly 3 occurrences of `a', 2 of `b' and 1 of `c', then three of the allowable 60 codes under these conditions are:abaabcabaacbababacThese three codes are listed from top to bottom in alphabetic order. Among all codes generated with this set of characters, these codes appear consecutively in this order.Write a program to assist in the issuing of these identification codes. Your program will accept a sequence of no more than 50 lower case letters (which may contain repeated characters) and print the successor code if one exists or the message `No Successor' if the given code is the last in the sequence for that set of characters.Input and OutputInput will consist of a series of lines each containing a string representing a code. The entire file will be terminated by a line consisting of a single #.Output will consist of one line for each code read containing the successor code or the words `No Successor'.Sample inputabaacbcbbaa#Sample outputababacNo Successor10098 - Generating FastGenerating permutation has always been an important problem in computer science. In this problem you will have to generate the permutation of a given string in ascending order. Remember that your algorithm must be efficient.InputThe first line of the input contains an integer n, which indicates how many strings to follow. The next n lines contain n strings. Strings will only contain alpha numerals and never contain any space. The maximum length of the string is 10.OutputFor each input string print all the permutations possible in ascending order. Not that the strings should be treated, as case sensitive strings and no permutation should be repeated. A blank line should follow each output set.Sample Input3ababcbcaSample Outputabbaabcacbbacbcacabcbaabcacbbacbcacabcba729 - The Hamming Distance ProblemThe Hamming distance between two strings of bits (binary integers) is the number of corresponding bit positions that differ. This can be found by using XOR on corresponding bits or equivalently, by adding corresponding bits (base 2) without a carry. For example, in the two bit strings that follow:A 0 1 0 0 1 0 1 0 0 0B 1 1 0 1 0 1 0 1 0 0A XORB = 1 0 0 1 1 1 1 1 0 0The Hamming distance (H) between these 10-bit strings is 6, the number of 1's in the XOR string.InputInput consists of several datasets. The first line of the input contains the number of datasets, and it's followed by a blank line. Each dataset contains N, the length of the bit strings and H, the Hamming distance, on the same line. There is a blank line between test cases.OutputFor each dataset print a list of all possible bit strings of length N that are Hamming distance H from the bit string containing all 0's (origin). That is, all bit strings of length N with exactly H 1's printed in ascending lexicographical order.The number of such bit strings is equal to the combinatorial symbol C(N,H). This is the number of possible combinations of N-H zeros and H ones. It is equal toThis number can be very large. The program should work for .Print a blank line between datasets.Sample Input14 2Sample Output001101010110100110101100592 - Island of LogicThe Island of Logic has three kinds of inhabitants: divine beings that always tell the truth, evil beings that always lie, and human beings that are truthful during the day and lie at night. Every inhabitant recognizes the type of every other inhabitant.A social scientist wants to visit the island. Because he is not able to distinguish the three kinds of beings only from their looks, he asks you to provide a communication analyzer that deduces facts from conversations among inhabitants. The interesting facts are whether it is day or night and what kind of beings the speakers are.InputThe input file contains several descriptions of conversations. Each description starts with an integer n, the number of statements in the conversation. The following n lines each contain one statement by an inhabitant. Every statement line begins with the speaker's name, one of the capital letters A, B, C, D, E, followed by a colon `:'. Next is one of the following kinds of statements:∙I am [not] ( divine | human | evil | lying ).∙X is [not] ( divine | human | evil | lying ).∙It is ( day | night ).Square brackets [] mean that the word in the brackets may or may not appear, round brackets () mean that exactly one of the alternatives separated by | must appear. X stands for some name from A, B, C, D, E. There will be no two consecutive spaces in any statement line, and at most 50 statements in a conversation.The input is terminated by a test case starting with n = 0.OutputFor each conversation, first output the number of the conversation in the format shown in the sample output. Then print ``This is impossible.'', if the conversation cannot happen according to the rules or ``No facts are deducible.'', if no facts can be deduced. Otherwise print all the facts that can be deduced. Deduced facts should be printed using the following formats:∙X is ( divine | human | evil ).∙It is ( day | night ).X is to be replaced by a capital letter speaker name. Facts about inhabitants must be given first (in alphabetical order), then it may be stated whether it is day or night.The output for each conversation must be followed by a single blank line.Sample Input1A: I am divine.1A: I am lying.1A: I am evil.3A: B is human.B: A is evil.A: B is evil.Sample OutputConversation #1No facts are deducible.Conversation #2This is impossible.Conversation #3A is human.It is night.Conversation #4A is evil.B is divine.Reasoning made easyTo make things clearer, we will show the reasoning behind the third input example, where A says ``I am evil.''. What can be deduced from this? Obviously A cannot be divine, since she would be lying, similarly A cannot be evil, since she would tell the truth. Therefore, A must be human, moreover, since she is lying, it must be night. So the correct output is as shown.In the fourth input example, it is obvious that A is lying since her two statements are contradictory. So, B can be neither human nor evil, and consequently must be divine. B always tells the truth, thus A must be evil. Voil‘a!110 - Meta-Loopless SortsBackgroundSorting holds an important place in computer science. Analyzing and implementing various sorting algorithms forms an important part of the education of most computer scientists, and sorting accounts for a significant percentage of the world's computational resources. Sorting algorithms range from the bewilderingly popular Bubble sort, to Quicksort, to parallel sorting algorithms and sorting networks. In this problem you will be writing a program that creates a sorting program (a meta-sorter).The ProblemThe problem is to create several programs whose output is a standard Pascal program that sorts n numbers where n is the only input to the program you will write. The Pascal programs generated by your program must have the following properties:∙They must begin with program sort(input,output);∙They must declare storage for exactly n integer variables. The names of the variables must come from thefirst n letters of the alphabet (a,b,c,d,e,f).∙ A single readln statement must read in values for all the integer variables.∙Other than writeln statements, the only statements in the program are if then elsestatements. The boolean conditional for each if statement must consist of one strict inequality (either < or >) of two integer variables.Exactly n! writelnstatements must appear in the program.∙Exactly three semi-colons must appear in the programs1.after the program header: program sort(input,output);2.after the variable declaration: ...: integer;3.after the readln statement: readln(...);∙No redundant comparisons of integer variables should be made. For example, during program execution, once it is determined that a < b, variables a and b should not be compared again.∙Every writeln statement must appear on a line by itself.∙The programs must compile. Executing the program with input consisting of any arrangement of any n distinct integer values should result in the input values being printed in sorted order.For those unfamiliar with Pascal syntax, the example at the end of this problem completely defines the small subset of Pascal needed.The InputThe input consist on a number in the first line indicating the number M of programs to make, followed by a blank line.Then there are M test cases, each one consisting on a single integer n on a line by itself with 1 n 8. There will be a blank line between test cases.The OutputThe output is M compilable standard Pascal programs meeting the criteria specified above. Print a blank line between two consecutive programs.Sample Input13Sample Outputprogram sort(input,output);vara,b,c : integer;beginreadln(a,b,c);if a < b thenif b < c thenwriteln(a,b,c)else if a < c thenwriteln(a,c,b)elsewriteln(c,a,b)elseif a < c thenwriteln(b,a,c)else if b < c thenwriteln(b,c,a)elsewriteln(c,b,a)end.。
【免费下载】算法竞赛 入门经典 作者刘汝佳
算法竞赛入门经典
目录
目录................................................................................................................................................110 第 1 章 程序设计入门................................................................................................................112
1.1 算术表达式...................................................................................................................112 1.2 变量及其输入...............................................................................................................114 1.3 顺序结构程序设计.......................................................................................................117 1.4 分支结构程序设计.......................................................................................................120 1.5 小结与习题...................................................................................................................123
刘汝佳《算法入门经典》答案
1.double 类型的输出格式为“%lf”.2.double pi=4.0*atan(1.0);这样比较精确。
3.printf("d",m);可以在输出前补充上0,但是换做其他的字符就不可以了。
4.绝对值的输出用%d,如:printf("%d",abs(-10));不可以用%f.第二章循环结构程序设计(2011-07-21 21:40:01)[编辑][删除]分类:算法竞赛入门经典标签:杂谈1. 调试程序的新方法:IDE,gdb .(还没有学会)2.判断是否是整数 floor(m+0.5)==m.1.AABB问题:法一:列举,然后判断是否是完全平方数,m=1100*a+11*b; m==floor(m+0.5) 法二:枚举所有的数,然后分离位数,分别判断是否相等。
2.阶乘之和问题:输出结果的后六位printf("%d",s%100000)3.使用计时器,可以测试程序的效率#include<time.h>printf("%.2lf",(double)clock()/CLOCKS_PER_SEC);注意的是,将其放入程序时,测试时会把键盘输入的时间一块算上,而如果不计可以使用命令行测试。
4.文件操作:(1)输入输出重定向:#define LOCALint main(){#ifdef LOCALfreopen("data.in","r",stdin);freopen("data.out","w",stdout);#endif.........}(2)文件的保存和读取:int main(){FILE *fin,*fout;fin=fopen("data.in","rb");fout=fopen("data.out","wb");.....fscanf(fin,"%d",&a);fprintf(fout,"%d\n",a);fclose(fin);fclose(fout);}第三章:数组和字符串(2011-07-22 20:27:54)[编辑][删除]标签:分类:算法竞赛入门经典杂谈1.当数组需要开的很大的时候,数组a[1000]的定义放到main函数外,否则会异常退出。
算法竞赛入门经典(第2版)
10 数学概念与方法
10.3 其他数学专题
10.3.1 递推 10.3.2 数学期望 10.3.3 连续概率
11 图论模型与算法
11.1
A
再谈树
11.4 网
D
络流初步
11.2 最
B
小生成树
11.5 竞
E
赛题目选
讲
11.3 最
C
短路问题
11.6 训
F
练参考
11 图论模型与算 法
11.7 总结与展望
11 图论模型与算法
11.1 再谈树
11.1.1 无根树转有根树 11.1.2 表达式树
11 图论模型与算法
11.2 最小生成树
11.2.1 Kruskal算法 11.2.2 竞赛题目选解
11 图论模型与算法
11.3 最短路问题
11.3.1 Dijkstra算法 11.3.2 Bellman-Ford算法 11.3.3 Floyd算法 11.3.4 竞赛题目选讲
6.3 树和二叉树
6.3.1 二叉树的编号 6.3.2 二叉树的层次遍历 6.3.3 二叉树的递归遍历 6.3.4 非二叉树
6 数据结构基础
6.4 图
6.4.1 用DFS求连通块 6.4.2 用BFS求最短路 6.4.3 拓扑排序 6.4.4 欧拉回路
7 暴力求解法
7.1 简单 枚举
7.4 回溯 法
11 图论模型与 算法
11.4 网络流初步
11.4.1 最大流问题 11.4.2 增广路算法 11.4.3 最小割最大流定理 11.4.4 最小费用最大流问题 11.4.5 应用举例
12 高级专题
12.1 知 识点选讲
12.2 难 题选解
集合上的动态规划---最优配对问题
集合上的动态规划---最优配对问题原⽂:/lhshaoren/article/details/7526480/*提醒推荐:五星刘汝佳《算法竞赛⼊门经典》,集合上的动态规划---最优配对问题题意:空间⾥有n个点P0,P1,...,Pn-1,你的任务是把它们配成n/2对(n是偶数),使得每个点恰好在⼀个点对中。
所有点对中两点的距离之和应尽量⼩。
状态:d(i,S)表⽰把前i个点中,位于集合S中的元素两两配对的最⼩距离和状态转移⽅程为:d(i,S)=min{|PiPj|+d(i-1,S-{i}-{j}}书上的解法有些问题,正解见⽅法⼀⽅法⼆:状态可以进⾏压缩,i的值其实隐藏在S中,S中最⾼位为1的即为i,所以需要⼀次查找,从n-1到0进⾏⼀次历编即可,整个运算下来,平均查找次数仅为2。
⽽且⽅法⼆⽐⽅法⼀情况简单很多,也⽐较容易理解。
⽅法三:这道题⽤递归实现更好⼀些,因为只需要判断n为偶数的情况,这就是递归运算的好处,⽽⾮递归则需要全部都进⾏⼀次运算。
技巧:①处使⽤有个技巧,传递引⽤⽽不是下标,书写会⽅便很多。
*///⽅法⼀:正解。
#include <cstdio>#include <cstring>#include <cmath>const int nMax=21;const double INF=1e10;int n;struct Node{int x,y,z;}node[nMax];double d[nMax][1<<nMax];void init(){scanf("%d",&n);for(int i=0;i<n;i++)scanf("%d %d %d",&node[i].x,&node[i].y,&node[i].z);}double min(double a,double b){return a<b?a:b;}double dis(Node &a,Node &b)//①{return sqrt((double)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));}void solve(){for(int i=0;i<n;i++){for(int s=0;s<(1<<(i+1));s++){if(s==0) d[i][s]=0;else d[i][s]=INF;if((s & (1<<i))){for(int j=i-1;j>=0;j--)if((s & (1<<j)))d[i][s]=min(d[i][s],dis(node[i],node[j])+d[i-1][s^(1<<i)^(1<<j)]);}else if(i!=0){d[i][s]=d[i-1][s];}}}}int main(){freopen("f://data.in","r",stdin);init();solve();printf("%.3lf\n",d[n-1][(1<<n)-1]);return 0;}//⽅法⼆:推荐。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
424-Integer InquiryOne of the first users of BIT's new supercomputer was Chip Diller.He extended his exploration of powers of3to go from0 to333and he explored taking various sums of those numbers.``This supercomputer is great,''remarked Chip.``I only wish Timothy were here to see these results.''(Chip moved to a new apartment,once one became available on the third floor of the Lemon Sky apartments on Third Street.)InputThe input will consist of at most100lines of text,each of which contains a single VeryLongInteger.Each VeryLongInteger will be100or fewer characters in length,and will only contain digits(no VeryLongInteger will be negative).The final input line will contain a single zero on a line by itself.OutputYour program should output the sum of the VeryLongIntegers given in the input.Sample Input123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890Sample Output37037036703703703670370370367010106–ProductThe ProblemThe problem is to multiply two integers X,Y.(0<=X,Y<10250)The InputThe input will consist of a set of pairs of lines.Each line in pair contains one multiplyer.The OutputFor each input pair of lines the output line should consist one integer the product.Sample Input12122222222222222222222222222Sample Output144444444444444444444444444465–OverflowWrite a program that reads an expression consisting of two non-negative integer and an operator.Determine if either integer or the result of the expression is too large to be represented as a``normal''signed integer(type integer if you are working Pascal,type int if you are working in C).InputAn unspecified number of lines.Each line will contain an integer,one of the two operators+or*,and another integer. OutputFor each line of input,print the input followed by0-3lines containing as many of these three messages as are appropriate: ``first number too big'',``second number too big'',``result too big''.Sample Input300+39999999999999999999999+11Sample Output300+39999999999999999999999+11first number too bigresult too big748-ExponentiationProblems involving the computation of exact values of very large magnitude and precision are common.For example,the computation of the national debt is a taxing experience for many computer systems.This problem requires that you write a program to compute the exact value of Rn where R is a real number(0.0<R< 99.999)and n is an integer such that.InputThe input will consist of a set of pairs of values for R and n.The R value will occupy columns1through6,and the n value will be in columns8and9.OutputThe output will consist of one line for each line of input giving the exact value of Rn.Leading zeros and insignificant trailing zeros should be suppressed in the output.Sample Input95.123120.4321205.1234156.7592998.999101.010012Sample Output548815620517731830194541.899025343415715973535967221869852721.00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.92857370126648804114665499331870370751166629547672049395302429448126.76412102161816443020690903717327667290429072743629540498.1075960194566517745610440100011.12682503013196972066120110494-If We Were a Child AgainInput:standard inputOutput:standard outputTime Limit:7seconds“Oooooooooooooooh!If I could do the easy mathematics like my school days!!I can guarantee,that I’d not make any mistake this time!!”Says a smart university student!!But his teacher even smarter–“Ok!I’d assign you such projects in your software lab.Don’t be so sad.”“Really!!”-the students feels happy.And he feels so happy that he cannot see the smile in his teacher’s face.The ProblemThe first project for the poor student was to make a calculator that can just perform the basic arithmetic operations.But like many other university students he doesn’t like to do any project by himself.He just wants to collect programs from here and there.As you are a friend of him,he asks you to write the program.But,you are also intelligent enough to tackle this kind of people.You agreed to write only the(integer)division and mod(%in C/C++)operations for him.InputInput is a sequence of lines.Each line will contain an input number.One or more spaces.A sign(division or mod).Again spaces.And another input number.Both the input numbers are non-negative integer.The first one may be arbitrarily long. The second number n will be in the range(0<n<231).OutputA line for each input,each containing an integer.See the sample input and output.Output should not contain any extra space.Sample Input110/10099%102147483647/2147483647 2147483646%2147483647 Sample Output1912147483646。