算法导论 第三版 第31章 答案 英

合集下载

算法导论习题答案 (5)

算法导论习题答案 (5)
c(j) = min {c(i − 1) + linecost(i, j)} .
Three-hole punch your paper on submissions. You will often be called upon to “give an algorithm” to solve a certain problem. Your write-up should take the form of a short essay. A topic paragraph should summarize the problem you are solving and what your results are. The body of the essay should provide the following:
(a) Argue that this problem exhibits optimal substructure.
Solution: First, notice that linecost(i, j) is defined to be � if the words i through j do not fit on a line to guarantee that no lines in the optimal solution overflow. (This relies on the assumption that the length of each word is not more than M .) Second, notice that linecost(i, j) is defined to be 0 when j = n, where n is the total number of words; only the actual last line has zero cost, not the recursive last lines of subprob­ lems, which, since they are not the last line overall, have the same cost formula as any other line.

算法导论 第三版 第22章 答案 英

算法导论 第三版 第22章 答案 英
Chapter 22
Michelle Bodnar, Andrew Lohr April 12, 2016
Exercise 22.1-1 Since it seems as though the list for the neighbors of each vertex v is just an undecorated list, to find the length of each would take time O(out − degree(v )). So, the total cost will be v∈V O(outdegree(v )) = O(|E | + |V |). Note that the |V | showing up in the asymptotics is necessary, because it still takes a constant amount of time to know that a list is empty. This time could be reduced to O(|V |) if for each list in the adjacency list representation, we just also stored its length. To compute the in degree of each vertex, we will have to scan through all of the adjacency lists and keep counters for how many times each vertex has appeared. As in the previous case, the time to scan through all of the adjacency lists takes time O(|E | + |V |). Exercise 22.1-2 The adjacency list representation: 1 : 2, 3 2 : 1, 4, 5 3 : 1, 6, 7 4:2 5:5 6:3 7 : 3.

新概念英语第三册 31-40 课后作文标准答案

新概念英语第三册 31-40 课后作文标准答案

Lesson 31 A lovable eccentricKey to CompositionA possible answerA true eccentricMr. Chew is a man who has lived in our small town for years. He is a large man (some would say "fat"), he has a round face, a big black moustache and a bald head. He always wears the same clothes—black trousers, a white shirt and a red waistcoat, and is always po-lite to everyone he meets.He owns an antiques shop near the river, and he lives "over the shop". No one knows where he gets his things, but there are always lots of different interesting antiques in the shop window every week.Mr. Chew is interested in politics, too. Every week he puts up strange notices in his shop window to passers-by. Usually the notices are trying to make people save a local building or stop a plan to build a road through the town. Every time there is a national election, he automatically stands for parliament and gives speeches almost every day in the town park saying what he would do if he were Prime Minis-ter. (He usually says he would make sure that antiques dealers did not have to pay Income Tax and that no cars should be allowed into our little town at all. ) Every year he gets a few votes, but not enough to worry the serious candidates.For most people Mr. Chew is a harmless eccentric, and everyone likes talking to him. After all, he is someone who adds colour to the dull routine of their daily lives. (248words)Lesson 32 A lost shipKey to CompositionA possible answerThe last dayThe journey has gone well so far. We are now on the way to Russia and hope that we shall arrive with no more problems. But we are watching the sea and sky. The convoy successfully fought off an air attack early this morning. No ships were lost and we managed to shoot , down threeenemy aircraft before they broke off the attack.At 10 a. m. this morning we were attacked by a U-boat. This was the first time we had been attacked and for most men on board this was the first time they had been in action at sea. We were fortunate, but the ship ahead of us, the Dauntless, was torpedoed and sunk. After the ship sank, there were hundreds of men in the sea. We picked up as many survivors as we could in the Karen. In fact in [ the end we picked up 720 men, which meant that 50 men lost their lives in the attack on the Dauntless,The Karen was packed with men, many crowded on the open decks, when we turned and attacked the U-boat. Depth charges were I dropped and the U-boat was put out of action.It is now 3. 15 p. m. , the light is already beginning to fade and storm clouds are gathering. We are about to be attacked by a second U-boat, this time from the north. The captain has just announced —(239words)Lesson 33 A day to rememberKey to CompositionA possible answerMistaken identityWe have all experienced days when everything seems to go wrong. Things certainly went wrong for Ray, a friend of mine, one day last month. It all started, as these things do, with a simple case of "mistaken identity". Ray had been shopping and was loaded with par-cels when he got back to the multi-storey car park to look for his car. He knew he was on the correct level, but he couldn't see his car any-where.Then, suddenly, while he was looking, he saw one exactly like it. It was a red Nissan. It was exactly the same as his own car, and naturally he mistook it for his own. Still holding the parcels, he felt in his jacket pockets and found his key. He tried to open the driver's door, but the key just wouldn't turn. He couldn't understand it. In the end, he forced the lock — and naturally broke the key.At that point he dropped the parcels as well. This infuriated him. The only way he could get into his car was to break a window, so he deliberately smashed the window of the car. As he was putting his hand in, the owner came back and saw him. The owner rushed to-wards Ray, held him against the car, and called a policeman on his mobile phone.When Ray was arrested, he tried to explain, but the police did not believe him — until they found Ray's car on a level below! (246words)Lesson 34 A happy discoveryKey to CompositionA possible answerAn antique shop"The Antique Shop" has been in the little street near the church for years. As you look at it from across the road, it draws you to-wards it. It has one large window display designed to attract all sorts of customers. There is expensive glass, porcelain and jewellery to at-tract people with a lot of money; there is second-hand furniture, modem silver and other things (hardly antique!) to attract those with not so much.When you enter the shop, a little bell rings and the owner, a little grey-haired old lady called Mrs, Century, comes out from a room at the back and greets you like a lost relative! She greets everyone the same and always with a smile.The shop sells all kinds of antiques. There are shelves full of old books along the back of the shop. There are two large tables in the middle of the shop covered with pieces of glass and porcelain. Then, around the walls on the floor are large pots, brass statues and things. There are lots of painting on the walls, too. At the weekend, the shop usually has five or six customers in it at one time. You might find a professional antique collector, a holiday couple and a young mother (with child in pram) looking for a cheap antique for her husband's birthday.We all like looking for unusual things and hope to find a bargain one day. You might find one in Mrs. Century's "Antique Shop". (249words)Lesson 35 Justice was doneKey to CompositionA possible answerA burglary that went wrongThere is a large jewellery store in the town that I have often wanted to rob. A few weeks ago I started planning the theft and kept a close watch on the shop. I noticed when people went in and out, when they started and finished work, and so on. And I noticed the chimneys, too.One night I climbed onto the roof of the store and looked for a way in. One of the chimneys seemed wide enough for me to get in, ss I started climbing down. Very soon, however, I got stuck and had to climb out again. I didn't give up. I looked around, found another chimney that looked quite wide at the top and climbed down that one. Again I got stuck.This time, however, I was really stuck. I could climb neither up nor down. At first I struggled to try to free myself, but I couldn't get out. Then I started to get scared and started sweating. I tried to calm myself by quietly counting and thinking of pleasant things. But nothing worked and finally I started shouting for help. Nothing happened, everything was dark and silent, and I got more and more frightened.I think I went to sleep because I suddenly realized that light was shining down the chimney. I shouted and shouted. Eventually I heard tapping and was finally freed by Fire fighters who had smashed a hole in the chimney. (240words)Lesson 36 A chance in a millionkey to CompositionA Possible answerThe pastWhen the war finished, Franz Bussman did everything he could to get information about his brother Hans. With no information, he reluctantly assumed his brother was dead, and gave up the searchWith no family, and having been unable to find his brother, Franze found it difficult to settle down. Over the years he moved from place to place and from job to job, never staying very long in one place.When he met Anna (now Mrs. Bussman), he was working as a waiter in a hotel. He and Anna got married and Franz settled down at last. He moved from the dinning room into the kitchen of the hotel and became a cook. But this did not last very long. He was talking to a friend one day, a taxi driver, who suggested that they should go into partnership So they did, and Franz became a taxi driver. He and the friend drove taxis themselves, but they also soon owned four more taxis and employed four drivers.He visited his home town once to visit his old house, but it was a sad visit. There was a large modem block of flats where his family house used to be. ' And although he spoke to some of the apartment owners, no one remembered him or his family. Now that he has finally found his brother Hans, he and Anna are planning to invite Hans to come and live near them and work as a taxi driver in the company. (250words)Lesson 37 The Westhaven ExpressKey to CompositionA possible answerA disastrous train journeyWhen. I finally boarded the train. I was looking forward to a pleasant journey to the village of Slowleigh where my friends live. I sat in my seat, got out a book and was already reading when the train startedAccording to the timetable, the train was due to arrive in Slowleight at 4. 30. I had been so interested in my book that I had a shock when I looked at my watch. It was almost 4. 30. I closed my book and waited for the train to slow down. It didn't. In fact, the train was going very fast —and that was Slowleigh, wasn't it?! The train went straight on. I asked the other passengers why the train hadn't stopped and they told me it was the express to the city. I didn't believe it.Then the ticket collector came along. He looked at my ticket and I tried to explain, but in the end I had to pay the full fare to the city.By the time we arrived in the city it was six o'clock. The journey had lasted two hours and I was miles away from my original destination. I rang my friends and said I would get a fast train back. Then I checked the timetable: there was no fast train back to Slowleigh, only a slow one, at 7 o'clock. By the time I finally reached Slowleigh Station, it was nine o'clock at night. My pleasant little train journey had taken 4. 5 hours! (250words)Lesson 38 The first calendarKey to CompositionA possible answerStudying the pastFuture historians will have plenty of sources from which to learn about twentieth-century man. Not only will they have the written word, they will also have films, videos, audio cassettes, CDs and . CD-ROMs. In fact, they will have so much source material that they will hardly know where to start!If they study all the material available, they will be able to build up a complete social and political history of our time. They will know exactly how we dressed, what we ate in every different country and they will know exactly what our homes were like. They will know what our towns and cities were like and what forms of entertainment we enjoyed. In fact, they will not only learn about our forms of en-tertainment, they will be able to enjoy a lot of them, too — our music, plays, musical shows, video games, our art, our literature, . . . . And they will be able to read and see the news day by day as it happened.They will learn in detail the way we fought our wars — the way we used jet fighters, helicopters, ships and tanks. They will be able to learn every detail of great moments in history, and everything about leading figures of the time as well as the lives of ordinary, men and women.In future, the study of history will provide interest and excitement for a lot of people, the past will be brought to life and history will no longer be boring.(249 words)Lesson 39 Nothing to worry aboutKey to CompositionA possible answerBruce remained unperturbedIt was typical of Bruce to announce "cheerfully" that there was no oil in the engine! For the restof us, it was a disaster. We all got out and began shouting at him and then at each other. What could we do? We were standing in the middle of a very large pool up to our ankles in water with a car that was useless.We tried to push it, but of course it was absolutely impossible. All we managed to do was to push it deeper into the soft mud. In the end we all walked to next village where we tried to get a taxi so that we could take some oil (and petrol) back to the car.We couldn't find one driver who would take us over the rough road. Fortunately there was a small garage and we paid a large sum of money to the garage owner to rent a jeep. With a can of oil and an extra can of petrol we all climbed in and set off.When we eventually got back to the pool, we attached a rope from the jeep to the car and pulled the car out of the water. We were not surprised to find that the engine was badly damaged, and would not even start with the oil we had brought back. Disaster again, but Bruce was undismayed!(230 words)Lesson 40 Who's whoKey lo CompositionA possible answerArrest the policeThe policeman who accompanied the workman lo the pay phone still did not realize that they had been the victims of a practical joke. When he and the worker returned to the scene of the hoax right outside the university gates, the other workman was still quarrelling with the police and resisting arrest.Following the worker's call to the police station, it was not long before more police arrived on the scene — and it is at this point that the story becomes very complicated! The workmen told the police who had just arrived that the first lot of policemen were actually stu-dents dressed up as policemen. The second lot of police therefore threatened to arrest the first lot of police, but before they did so, they asked for their identity cards. The first lot ignored this request and said that they really were policemen, but that the workmen were stu-dents. The workers had to prove their identity, too, they said.None of them had to prove their identity by showing identity cards, because at this point two or three of the policemen started laugh-ing, and then the workmen started laughing, and in the endthey were all laughing. They finally realized that they had all been victims of a hoax — and not one of them could remember what the student had looked like. "After all, " said one workman, "they all look the same tome. " (237 wards)。

算法导论答案

算法导论答案

8.3-3 8.3-4
8.3-5(*) 8.4-1 见图 8-4 8.4-2
8.4-3 3/2,1/2 8.4-4(*) 8.4-5(*)
9.1-1
9.1-2 9.2-1 9.3-1
第九章
9.3-2 9.3-3
9.3-4 9.3-5
9.3-6 9.3-7
9.3-8
9.3-9
15.1-1
6.4-4
6.4-5
6.5-1 据图 6-5 6.5-2
6.5-3 6.5-4 6.5-5
6.5-6 6.5-7
6.5-8
7.1-1 见图 7-1 7.1-2
7.1-3 7.1-4 7.2-1 7.2-2
7.2-3 7.2-4 7.2-5
第七章
7.2-6 7.3-1
7.3-2
7.4-1 7.4-2
len=j;//更新 len
} cout<<len<<endl; } return 0; } 15.5-1
15.5-2 15.5-3
15.5-4
16.1-1
第 16 章
16.1-2 16.1-3
16.1-4 16.2-1 16.2-2
16.2-3
16.2-4
16.2-5 16.2-6
16.2-7
16.3-1 16.3-2
16.3-3 16.3-4
16.3-5
16.3-6 那就推广到树的结点有三个孩子结点,证明过程同引理 16.3 的证明。 16.3-7 16.3-8
第 24 章
24.1-1 同源顶点 s 的运行过程,见图 24-4 24.1-2
24.1-3
24.1-4 24.1-5* 24.1-6 修改 Bellman-Ford 算法,先找到负环上的一个节点,再依次找到负环上的一个节点,再依 次找到负环上的其他节点。 24.2-1 见图 24-5 24.2-2 最后一次不影响结果

编译原理第三版课后习题解答

编译原理第三版课后习题解答

第二章习题解答之五兆芳芳创作P36-6(1)()L G1是0~9组成的数字串(2)最左推导:最右推导:P36-7G(S)P36-8文法:最左推导:最右推导:语法树:/******************************** *****************/P36-9句子iiiei有两个语法树:P36-10/*****************************/P36-11/***************L1: L2: L3: L4:***************/第三章习题参考答案P64–7 (1)101101(|)*确定化:1 01 1 1 最小化:1 01 1 1 P64–8 (1) (2) (3) P64–12 (a)aa,ba 确定化:给状态编号:aaab b ba 最小化:a ab ab (b)b a b a ba a a1已经确定化了,进行最小化 最小化:a b aP64–14 (1) 01 0 (2):(|1εε0 确定化:给状态编号:YY1 1最小化:1 1第四章P81–1(1) 依照T,S的顺序消除左递归递归子程序:procedure S;beginif sym='a' or sym='^'then abvanceelse if sym='('then beginadvance;T;if sym=')' then advance;else error;endelse errorend;procedure T;beginS;'Tend;procedure 'T;beginif sym=','then beginadvance;S;'Tendend;其中:sym:是输入串指针IP所指的符号advance:是把IP调至下一个输入符号error:是出错诊察程序(2)FIRST(S)={a,^,(} FIRST(T)={a,^,(} FIRST('T)={,,ε} FOLLOW(S)={),,,#} FOLLOW(T)={)} FOLLOW('T)={)}预测阐发表是LL(1)文法P81–2文法:(1)FIRST(E)={(,a,b,^} FIRST(E')={+,ε} FIRST(T)={(,a,b,^} FIRST(T')={(,a,b,^,ε} FIRST(F)={(,a,b,^} FIRST(F')={*,ε} FIRST(P)={(,a,b,^} FOLLOW(E)={#,)} FOLLOW(E')={#,)}FOLLOW(T)={+,),#}FOLLOW(T')={+,),#}FOLLOW(F)={(,a,b,^,+,),#}FOLLOW(F')={(,a,b,^,+,),#}FOLLOW(P)={*,(,a,b,^,+,),#}(2)考虑下列产生式:FIRST(+E)∩FIRST(ε)={+}∩{ε}=φFIRST(+E)∩FOLLOW(E')={+}∩{#,)}=φFIRST(T)∩FIRST(ε)={(,a,b,^}∩{ε}=φFIRST(T)∩FOLLOW(T')={(,a,b,^}∩{+,),#}=φFIRST(*F')∩FIRST(ε)={*}∩{ε}=φFIRST(*F')∩FOLLOW(F')={*}∩{(,a,b,^,+,),#}=φFIRST((E))∩FIRST(a)∩FIRST(b)∩FIRST(^)=φ所以,该文法度LL(1)文法.(3)(4)procedure E;beginif sym='(' or sym='a' or sym='b' or sym='^' then begin T; E' endelse errorendprocedure E';beginif sym='+'then begin advance; E endelse if sym<>')' and sym<>'#' then error endprocedure T;beginif sym='(' or sym='a' or sym='b' or sym='^' then begin F; T' endelse errorendprocedure T';beginif sym='(' or sym='a' or sym='b' or sym='^' then Telse if sym='*' then errorendprocedure F;beginif sym='(' or sym='a' or sym='b' or sym='^' then begin P; F' endelse errorendprocedure F';beginif sym='*'then begin advance; F' endendprocedure P;beginif sym='a' or sym='b' or sym='^'then advanceelse if sym='(' thenbeginadvance; E;if sym=')' then advanceelse errorendelse errorend;P81–3/***************(1)是,满足三个条件.(2)不是,对于A不满足条件3.(3)不是,A、B均不满足条件3.(4)是,满足三个条件.***************/第五章P133–1短语: E+T*F, T*F,直接短语: T*F句柄: T*FP133–2文法:(1)最左推导:最右推导:(2)(((a,a),^,(a)),a)(((S,a),^,(a)),a)(((T,a),^,(a)),a)(((T,S),^,(a)),a)(((T),^,(a)),a)((S,^,(a)),a)((T,^,(a)),a)((T,S,(a)),a)((T,(a)),a)((T,(S)),a)((T,(T)),a)((T,S),a)((T),a)(S,a)(T,S)(T)S“移进-归约”进程:步调栈输入串动作0 # (((a,a),^,(a)),a)# 预备1 #( ((a,a),^,(a)),a)# 进2 #(( (a,a),^,(a)),a)# 进3 #((( a,a),^,(a)),a)# 进4 #(((a ,a),^,(a)),a)# 进5 #(((S ,a),^,(a)),a)# 归6 #(((T ,a),^,(a)),a)# 归7 #(((T, a),^,(a)),a)# 进8 #(((T,a ),^,(a)),a)# 进9 #(((T,S ),^,(a)),a)# 归10 #(((T ),^,(a)),a)#归11 #(((T) ,^,(a)),a)# 进12 #((S ,^,(a)),a)# 归13 #((T ,^,(a)),a)# 归14 #((T, ^,(a)),a)# 进15 #((T,^ ,(a)),a)# 进16 #((T,S ,(a)),a)# 归17 #((T ,(a)),a)# 归18 #((T, (a)),a)# 进19 #((T,( a)),a)# 进20 #((T,(a )),a)# 进21 #((T,(S )),a)# 归22 #((T,(T )),a)# 归23 #((T,(T) ),a)# 进24 #((T,S ),a)# 归25 #((T ),a)# 归26 #((T) ,a)# 进27 #(S ,a)# 归28 #(T ,a)# 归29 #(T, a)# 进30 #(T,a )# 进31 #(T,S )# 归32 #(T )# 归33 #(T) # 进34 #S # 归P133–3(1)FIRSTVT(S)={a,^,(}FIRSTVT(T)={,,a,^,(}LASTVT(S)={a,^,)}LASTVT(T)={,,a,^,)}(2)G是算符文法,并且是算符优先文法6(3)优先函数(4)栈输入字符串动作# (a,(a,a))# 预备#( a, (a,a))# 进#(a , (a,a))# 进#(t , (a,a))# 归#(t, (a,a))# 进#(t,(a,a))# 进#(t,(a ,a))# 进#(t,(t ,a))# 归#(t,(t, a))# 进#(t,(t,a ))# 进#(t,(t,s ))# 归#(t,(t ))# 归#(t,(t))# 进#(t,s )# 归#(t )# 归#(t )# 进# s # 归successP134–5(1)0.'→⋅S S 1.'→⋅S S 2.S AS →⋅ 3.S A S →⋅4.S AS →⋅5.S b →⋅6.S b →⋅7.A SA →⋅8.A S A →⋅ 9.A SA →⋅ 10.A a →⋅11.A a →⋅(2)Aεε εd确定化:AabS S b69S A ba Abba机关LR(0)项目集标准族也可以用GO 函数来计较得到.所得到的项目集标准族与上图中的项目集一样: 0I ={'→⋅S S ,S AS →⋅,S b →⋅,A SA →⋅,A a →⋅} GO(0I ,a)={ A a →⋅}=1I GO(0I ,b)={ S b →⋅}=2IGO(0I ,S)={ '→⋅S S ,A S A →⋅,A SA →⋅,A a →⋅,S AS →⋅,S b →⋅}=3IGO(0I ,A)={ S A S →⋅,S AS →⋅,S b →⋅,A SA →⋅,A a →⋅}=4I GO(3I ,a)={ A a →⋅}=1I GO(3I ,b)={ S b →⋅}=2IGO(3I ,S)={ A S A →⋅,S AS →⋅,S b →⋅,A SA →⋅,A a →⋅}=5I GO(3I ,A)={ A SA →⋅,S A S →⋅,S AS →⋅,S b →⋅,A SA →⋅,A a →⋅}=6IGO(4I ,a)={ A a →⋅}=1I GO(4I ,b)={ S b →⋅}=2IGO(4I ,S)={ S AS →⋅,A S A →⋅,S AS →⋅,S b →⋅,A SA →⋅,A a →⋅}=7IGO(4I ,A)={ S A S →⋅,S AS →⋅,S b →⋅,A SA →⋅,A a →⋅}=4I GO(5I ,a)={ A a →⋅}=1I GO(5I ,b)={ S b →⋅}=2IGO(5I ,S)={ A S A →⋅,S AS →⋅,S b →⋅,A SA →⋅,A a →⋅}=5I GO(5I ,A)={ A SA →⋅,S A S →⋅,S AS →⋅,S b →⋅,A SA →⋅,A a →⋅}=6IGO(6I ,a)={ A a →⋅}=1I GO(6I ,b)={ S b →⋅}=2IGO(6I ,S)={ S AS →⋅,A S A →⋅,S AS →⋅,S b →⋅,A SA →⋅,A a →⋅}=7IGO(6I ,A)={ S A S →⋅,S AS →⋅,S b →⋅,A SA →⋅,A a →⋅}=4I GO(7I ,a)={ A a →⋅}=1I GO(7I ,b)={ S b →⋅}=2IGO(7I ,S)={ A S A →⋅,S AS →⋅,S b →⋅,A SA →⋅,A a →⋅}=5I GO(7I ,A)={ A SA →⋅,S A S →⋅,S AS →⋅,S b →⋅,A SA →⋅,A a →⋅}=6I项目集标准族为C={1I ,2I ,3I ,4I ,5I ,6I ,7I } (3)不是SLR 文法状态3,6,7有移进归约冲突状态3:FOLLOW(S’)={#}不包含a,b状态6:FOLLOW(S)={#,a,b}包含a,b,;移进归约冲突无法消解状态7:FOLLOW(A)={a,b}包含a,b ;移进归约冲突消解 所以不是SLR 文法.(4)机关例如LR(1)项目集标准族 见下图:对于状态5,因为包含项目[b a AS A / ⋅→],所以遇到搜索符号a 或b 时,应该用AS A →归约.又因为状态5包含项目[b a a A / ⋅→],所以遇到搜索符号a 时,应该移进.因此存在“移进-归约”矛盾,所以这个文法不是LR(1)文法.b b bASa SS aSSA ba a Sb bAA/********************第六章会有点难P164–5(1)E→E1+T {if (E1.type = int) and (T.type = int ) then E.type := intelse E.type := real}E→T {E.type := T.type}T→num.num {T.type := real}T→num {T.type := int}(2)P164–7S→L1|L2lengthL.2)}S→L {S.val:=L.val}L→L1B {L.val:=2*L1.val + B.val;L.length:=L1.length+1}L→B {L.val:=B.c;L.length :=1}B→0 {B.c:=0}B→1 {B.c:=1}***********************/第七章P217–1a*(-b+c) ab@c+*a+b*(c+d/e) abcde/+*+-a+b*(-c+d) a@bc@d+*+if (x+y)*z =0 then (a+b)↑c else a↑b↑c xy+z*0= ab+c↑abc↑↑¥或xy+z*0= P1 jez ab+c↑ P2 jump abc↑↑P1 P2P217–3-(a+b)*(c+d)-(a+b+c)的三元式序列:(1)+, a, b(2)@, (1), -(3)+, c, d(4)*, (2), (3)(5)+, a, b(6)+, (5), c(7)-, (4), (6)直接三元式序列:三元式表:(1)+, a, b(2)@, (1), -(3)+, c, d(4)*, (2), (3)(5)+, (1), c(6)-, (4), (5)直接码表:(1)(2)(3)(4)(1)(5)(6)四元式序列:(1)+, a, b, 1T(2)@, 1T, -, 2T(3)+, c, d, 3T(4)*, 2T, 3T, 4T(5)+, a, b, 5T(6)+, 5T, c, 6T(7)-, 4T, 6T, 7TP218–4自下而上阐发进程中把赋值句翻译成四元式的步调:A:=B*(-C+D)步调输入串栈PLACE 四元式(1) A:=B*(-C+D)(2) :=B*(-C+D) iA (3) B*(-C+D) i:=A- (4) *(-C+D)i:=i A-B (5) *(-C+D)i:=E A-B (6) *(-C+D)i:=E A-B (7) (-C+D)i:=E* A-B- (8) -C+D)i:=E*( A-B-- (9)C+D) i:=E*(- A-B--- (10) +D) i:=E*(-i A-B---C (11) +D) i:=E*(-E A-B---C (@,C,-, T 1) (12) +D) i:=E*(E A-B--T 1 (13) D) i:=E*(E+ A-B--T 1- (14) ) i:=E*(E+i A-B--T 1-D (15) ) i:=E*(E+E A-B--T 1-D (+,T 1,D,T 2) (16) ) i:=E(E A-B--T 2 (17) i:=E*(E) A-B--T 2- (18) i:=E+E A-B-T 2 (*,B,T 2,T 3) (19) i:=E A-T 3 (:=,T 3,-,A) (20) A产生的四元式: (@,C,-, T 1) (+,T 1,D,T 2) (*,B,T 2,T 3) (:=,T 3,-,A)P218–5/****************设A :10*20,B 、C 、D :20,宽度为w =4 则T1:= i * 20T1:=T1+jT2:=A –84T3:=4*T1Tn:=T2[T3] //这一步是多余的T4:= i + jT5:=B–4T6:=4*T4T7:=T5[T6]T8:= i * 20T8:=T8+jT9:=A–84T10:=4*T8T11:=T9[T10]T12:= i + jT13:=D–4T14:=4*T12T15:= T13[T14]T16:=T11+T15T17:=C–4T18:=4*T16T19:=T17[T18]T20:=T7+T19Tn:=T20******************/P218–6100.(jnz, A, -, 0)101.(j, -, -, 102)102.(jnz, B, -, 104)103.(j, -, -, 0)104.(jnz, C, -, 103)105.(j, -, -, 106)106.(jnz, D, -, 104) --假链链首107.(j, -, -, 100) --真链链首假链:{106,104,103}真链:{107,100}P218–7100.(j<, A, C, 102)101.(j, -, -, 0)102.(j<, B, D, 104)103.(j, -, -, 101)104.(j=, A, ‘1’, 106)105.(j, -, -, 109)106.(+, C, ‘1’, T1)107.(:=, T1, -, C)108.(j, -, -,100)109.(j≤, A, D, 111)110. (j, -, -, 100)111. (+, A, ‘2’, T2)112. (:=, T2, -, A)113. (j, -, -, 109)114. (j, -, - 100)P219–12/********************(1)MAXINT – 5MAXINT – 4MAXINT – 3MAXINT – 2MAXINT – 1MAXINT(2)翻译模式办法1:for E1 := E2 to E3 do S1 do MS F S → {backpatch(S1.nextlist,nextquad);backpatch(F.truelist,M.quad);emit(F.place ‘:=’F.place ‘+’1);emit(‘j ≤,’F.place ‘,’F.end ‘,’M.quad); S.nextlist := F.falselist;}21 to :For E E I F =→{F.falselist:= makelist(nextquad); emit(‘j>,’E1.place ‘,’E2.place ‘,0’);emit(I.Place ‘:=’E1.place);F.truelist := makelist(nextquad);emit(‘j,-,-,-’);F.place := I.place;F.end := E2.place;} id I →{p:=lookup(); if p <> nil thenI.place := pelse error}ε→M {M.quad := nextquad}****************/办法2:S→ for id:=E1 to E2 do S1S→ F S1F→ for id:=E1 to E2 do21:toE E forid F =→do{INITIAL=NEWTEMP;emit(‘:=,’E1.PLACE’,-,’ INITIAL); FINAL=NEWTEMP;emit(‘:=,’E2.PLACE’,-,’ FINAL);p:= nextquad+2;emit(‘j,’ INITIAL ‘,’ FINAL ’,’ p);F.nextlist:=makelist(nextquad);emit(‘j,-,-,-’);F.place:=lookup();if F.place nil thenemit(F.place ‘:=’ INITIAL)F.quad:=nextquad;F.final:=FINAL;}{backpatch(S1.nextlist, nextquad)p:=nextquad+2;emit(‘j,’ F.place‘,’ F.final ’,’ p );S.nextlist := merge(F.nextlist, makelist(nextquad)); emit(‘j,-,-,-’);emit(‘su cc,’ F.place ’,-,’ F.place);emit(‘j,-,-,’ F.quad);}第九章P270–9(1) 传名即当进程调用时,其作用相当于把被调用段的进程体抄到调用出现处,但必须将其中出现的任一形式参数都代之以相应的实在参数.A:=2;B:=3;A:=A+1;A:=A+(A+B);print A;∴A=9(2) 传地址即当程序控制转入被调用段后,被调用段首先把实在参数抄进相应的形式参数的形式单元中,进程体对形参的任何引用或赋值都被处理成对形式单元的直接拜访.当被调用段任务完毕前往时,形式单元(都是指示器)所指的实参单元就持有所希望的值.①A:=2;B:=3;T:=A+B②把T,A,A的地址抄进已知单元J1,J2,J3③x:=J1;y:=J2;z:=J3 //把实参地址抄进形式单元,且J2=J3④Y↑:=y↑+1Z↑:=z↑+x↑ // Y↑:对y的直接拜访Z↑:对z的直接拜访⑤print AA=8(3) 得结果每个形参均对应两个单元,第一个存放实参地址,第二个存放实参值,在进程体中对形参的任何引用或赋值都看成是对它的第二个单元的直接拜访,但在进程任务完毕前往前必须把第二个单元的内容放到第一个单元所指的那个实参单元中①A:=2;B:=3;T:=A+B②把T,A,A的地址抄进已知单元J1,J2,J3③x1:=J1;x2:=T;y1:=J2;y2:=A;z1:=J3;z2:=A; //将实参的地址和值辨别放进两个形式单元中④y2:=y2+1; z2:=z2+x2; //对形参第二个单元的直接拜访⑤x1↑:=x2; y1↑:=y2; z1↑:=z2 //前往前把第二个单元的内容存放到第一个单元所指的实参地址中⑥print AA=7(4) 传值即被调用段开始任务时,首先把实参的值写进相应的形参单元中,然后就仿佛使用局部变量一样使用这些形式单元 A:=2;B:=3;x:=A+By:=Az:=Ay:=y+1z:=z+xprint AA=2进程调用不改动A 的值第十章P306-1P306-2read A,BF:=1C:=A*A 1BD:=B*Bif C<D goto 1L---------------------------E:=A*AF:=F+1E:=E+F2B write Ehalt---------------------------1L : E:=B*BF:=F+2E:=E+F3B write Eif E>100 goto 2L ---------------------------halt4B ---------------------------2L : F:=F-1goto 1L 5B---------------------------根本块为1B 、2B 、3B 、4B 、5B P307-4B2有回路,所以{B2}是循环,B2既是入口节点,又是出口节点(1)代码外提:不存在不变运算,故无代码外提 (2)强度削弱:A:=K*I B:=J*I *→+ (3) 删除根本归结变量:I<100 可以用A<100*K 或B<100*J代替P307-5{B2,B3}(1) (2)。

算法导论 第三版 第九章 答案 英

算法导论 第三版 第九章 答案 英

2Hale Waihona Puke is less than roughly 4n/14 of the elements. So, we are never calling it recursively on more than 10n/14 elements. So, T (n) ≤ T (n/7) + T (10n/14) + O(n). So, we can show by substitution this is linear. Suppose T (n) < cn for n < k , then, for m ≥ k , T (m) ≤ T (m/7) + T (10m/14) + O(m) ≤ cm(1/7 + 10/14) + O(m). So, as long as we have that the constant hidden in the big-Oh notation is less than c/7, we have the desired result. Suppose now that we use groups of size 3 instead. So, For similar reasons, we have that the recurrence we are able to get is T (n) = T ( n/3 ) + T (4n/6) + O(n) ≥ T (n/3) + T (2n/3) + O(n). So, we will show it is ≥ cn lg(n). T (m) ≥ c(m/3) lg(m/3) + c(2m/3) lg(2m/3) + O(m) ≥ cm lg(m) + O(m). So, we have that it grows more quickly than linear. Exercise 9.3-2 We know that the number of elements greater than or equal to x and the number of elements less than or equal to x is at least 3n/10 − 6. Then for n ≥ 140 we have 3n/10 − 6 = Exercise 9.3-3 We can modify quicksort to run in worst case n lg(n) time by choosing our pivot element to be the exact median by using quick select. Then, we are guaranteed that our pivot will be good, and the time taken to find the median is on the same order of the rest of the partitioning. Exercise 9.3-4 Create a graph with n vertices and draw a directed edge from vertex i to vertex j if the ith and j th elements of the array are compared in the algorithm and we discover that A[i] ≥ A[j ]. Observe that A[i] is one of the i − 1 smaller elements if there exists a path from x to i in the graph, and A[i] is one of the n − i larger elements if there exists a path from i to x in the graph. Every vertex i must either lie on a path to or from x because otherwise the algorithm can’t distinguish between i ≤ x and i ≥ x. Moreover, if a vertex i lies on both a path to x and a path from x then it must be such that x ≤ A[i] ≤ x, so x = A[i]. In this case, we can break ties arbitrarily. Exercise 9.3-5 To use it, just find the median, partition the array based on that median. If i is less than half the length of the original array, recurse on the first half, if i is half the length of the array, return the element coming from the median finding 3 n n + − 6 ≥ n/4 + 140/20 − 6 = n/4 + 1 ≥ n/4 . 4 20

算法导论 第三版 第十二章 答案 英

算法导论 第三版 第十二章 答案 英

3
Algorithm 1 PREORDER-TREE-WALK(x) if x = N IL then print x PREORDER-TREE-WALK(x.left) PREORDER-TREE-WALK(x.right) end if return Algorithm 2 POSTORDER-TREE-WALK(x) if x = N IL then POSTORDER-TREE-WALK(x.left) POSTORDER-TREE-WALK(x.right) print x end if return
Exercise 12.2-3
Algorithm 5 TREE-PREDECESSOR(x) if x.lef t = N IL then return TREE-MAXIMUM(x.left) end if y = x.p while y = N IL and x == y.lef t do x=y y = y.p end while return y Exercise 12.2-4 Suppose we search for 10 in this tree. Then A = {9}, B = {8, 10} and C = ∅, and Professor Bunyan’s claim fails since 8 < 9. 8
Chapter 12
Michelle Bodnar, Andrew Lohr April 12, 2016
Exercise 12.1-1 Anytime that a node has a single child, treat it as the right child, with the left child being NIL 10

Ch10算法导论 第三版 第十章 答案 英

Ch10算法导论 第三版 第十章 答案 英

Chapter10Michelle Bodnar,Andrew LohrApril12,2016Exercise10.1-14414134141841Exercise10.1-2We will call the stacks T and R.Initially,set T.top=0and R.top=n+1. Essentially,stack T uses thefirst part of the array and stack R uses the last part of the array.In stack T,the top is the rightmost element of T.In stack R, the top is the leftmost element of R.Algorithm1PUSH(S,x)1:if S==T then2:if T.top+1==R.top then3:error“overflow”4:else5:T.top=T.top+16:T[T.top]=x7:end if8:end if9:if S==R then10:if R.top−1==T.top then11:error“overflow”12:else13:R.top=R.top−114:T[T.top]=x15:end if16:end if1Algorithm2POP(S)if S==T thenif T.top==0thenerror“underflow”elseT.top=T.top−1.return T[T.top+1]end ifend ifif S==R thenif R.top==n+1thenerror“underflow”elseR.top=R.top+1.return R[R.top−1]end ifend ifExercise10.1-34414131313838Exercise10.1-4Algorithm3ENQUEUEif Q.head==Q.tail+1,or Q.head==1and Q.tail==Q.length then error“overflow”end ifQ[Q.tail]=xif Q.tail==Q.length thenQ.tail=1elseQ.tail=Q.head+1end ifExercise10.1-5As in the example code given in the section,we will neglect to check for overflow and underflow errors.2Algorithm4DEQUEUEerror“underflow”end ifx=Q[Q.head]if Q.head==Q.length thenQ.head=1elseQ.head=Q.head+1end ifreturn xAlgorithm5HEAD-ENQUEUE(Q,x) Q[Q.head]=xif Q.head==1thenQ.head=Q.lengthelseQ.head=Q.head−1end ifAlgorithm6TAIL-ENQUEUE(Q,x) Q[Q.tail]=xif Q.tail==Q.length thenQ.tail=1elseQ.tail=Q.tail+1end ifAlgorithm7HEAD-DEQUEUE(Q,x) x=Q[Q.head]if Q.head==Q.length thenQ.head=1elseQ.head=Q.head+1end ifAlgorithm8TAIL-DEQUEUE(Q,x) x=Q[Q.tail]if Q.tail==1thenQ.tail=Q.lengthelseQ.tail=Q.tail−1end if3Exercise10.1-6The operation enqueue will be the same as pushing an element on to stack 1.This operation is O(1).To dequeue,we pop an element from stack2.If stack 2is empty,for each element in stack1we pop it off,then push it on to stack2. Finally,pop the top item from stack2.This operation is O(n)in the worst case.Exercise10.1-7The following is a way of implementing a stack using two queues,where pop takes linear time,and push takes constant time.Thefirst of these ways,consists of just enqueueing each element as you push it.Then,to do a pop,you dequque each element from one of the queues and place it in the other,but stopping just before the last element.Then,return the single element left in the original queue.Exercise10.2-1To insert an element in constant time,just add it to the head by making it point to the old head and have it be the head.To delete an element,it needs linear time because there is no way to get a pointer to the previous element in the list without starting at the head and scanning along.Exercise10.2-2The PUSH(L,x)operation is exactly the same as LIST-INSERT(L,x).The POP operation sets x equal to L.head,calls LIST-DELETE(L,L.head),then returns x.Exercise10.2-3In addition to the head,also keep a pointer to the last element in the linked list.To enqueue,insert the element after the last element of the list,and set it to be the new last element.To dequeue,delete thefirst element of the list and return it.Exercise10.2-4First let L.nil.key=k.Then run LIST-SEARCH’as usual,but remove the check that x=L.nil.Exercise10.2-5To insert,just do list insert before the current head,in constant time.To search,start at the head,check if the element is the current node being in-spected,check the next element,and so on until at the end of the list or you4found the element.This can take linear time in the worst case.To delete,again linear time is used because there is no way to get to the element immediately before the current element without starting at the head and going along the list.Exercise10.2-6Let L1be a doubly linked list containing the elements of S1and L2be a doubly linked list containing the elements of S2.We implement UNION as fol-lows:Set L1.nil.prev.next=L2.nil.next and L2.nil.next.prev=L1.nil.prev so that the last element of L1is followed by thefirst element of L2.Then set L1.nil.prev=L2.nil.prev and L2.nil.prev.next=L1.nil,so that L1.nil is the sentinel for the doubly linked list containing all the elements of L1and L2. Exercise10.2-7Algorithm9REVERSE(L)a=L.head.nextb=L.headwhile a=NIL dotmp=a.nexta.next=bb=aa=tmpend whileL.head=bExercise10.2-8We will store the pointer value for L.head separately,for convenience.In general,A XOR(A XOR C)=C,so once we know one pointer’s true value we can recover all the others(namely L.head)by applying this rule.Assuming there are at least two elements in the list,thefirst element will contain exactly the address of the second.Algorithm10LISTnp-SEARCH(L,k)p=NILx=L.headwhile x=NIL and x.key=k dotemp=xx=pXORx.npp=tempend whileTo reverse the list,we simply need to make the head be the“last”ele-5Algorithm11LISTnp-INSERT(L,x)x.np=L.headL.nil.np=xXOR(L.nil.npXORL.head)L.head=xAlgorithm12LISTnp-Delete(L,x)L.nil.np=L.nil.npXORL.headXORL.head.npL.head.np.np=L.head.np.npXORL.headment before L.nil instead of thefirst one after this.This is done by settingL.head=L.nil.npXORL.head.Exercise10.3-1A multiple array version could be L=2,/34567/124819511/23456A single array version could be L=4,127/410481371916105191311/16 Exercise10.3-2Algorithm13Allocate-Object()if free==NIL thenerror“out of space”elsex=freefree=A[x+1]end ifExercise10.3-3Allocate object just returns the index of some cells that it’s guaranteed tonot give out again until they’ve been freed.The prev attribute is not modified because only the next attribute is used by the memory manager,it’s up to the code that calls allocate to use the prev and key attributes as it seesfit.Exercise10.3-4For ALLOCATE-OBJECT,we will keep track of the next available spot inthe array,and it will always be one greater than the number of elements being stored.For FREE-OBJECT(x),when a space is freed,we will decrement the6Algorithm14Free-Object(x)A[x+1]=freefree=xposition of each element in a position greater than that of x by1and update pointers accordingly.This takes linear time.Exercise10.3-5See the algorithm COMP ACT IF Y−LIST(L,F)Exercise10.4-1181274510221Note that indices8and2in the array do not appear,and,in fact do not represent a valid tree.Exercise10.4-2See the algorithm PRINT-TREE.Exercise10.4-3Exercise10.4-4See the algorithm PRINT-TREE.Exercise10.4-5See the algorithm INORDER-PRINT’(T)Exercise10.4-6Our two pointers will be left and right.For a node x,x.left will point to the leftmost child of x and x.right will point to the sibling of x immediately to its right,if it has one,and the parent of x otherwise.Our boolean value b,stored at x,will be such that b=depth(x)mod2.To reach the parent of a node, simply keep following the“right”pointers until the parity of the boolean value changes.Tofind all the children of a node,start byfinding x.left,then follow7Algorithm15COMPACTIFY-LIST(L,F)if n=m thenreturnend ife=max{max i∈[m]{|key[i]|},max i∈L{|key[i]|}}increase every element of key[1..m]by2efor every element of L,if its key is greater than e,reduce it by2e f=1while key[f]<e dof++end whilea=L.headif a>m thennext[prev[f]]=next[f]prev[next[f]]=prev[f]next[f]=next[a]key[f]=key[a]prev[f]=prev[a]F REE−OBJECT(a)f++while key[f]<e dof++end whileend ifwhile a=L.head doif a>m thennext[prev[f]]=next[f]prev[next[f]]=prev[f]next[f]=next[a]key[f]=key[a]prev[f]=prev[a]F REE−OBJECT(a)f++while key[f]<e dof++end whileend ifend while8Algorithm16PRINT-TREE(T.root) if T.root==NIL thenreturnelsePrint T.root.keyPRINT-TREE(T.root.left)PRINT-TREE(T.root.right)end ifAlgorithm17INORDER-PRINT(T) let S be an empty stackpush(S,T)while S is not empty doU=pop(S)if U=NIL thenprint U.keypush(S,U.left)push(S,U.right)end ifend whileAlgorithm18PRINT-TREE(T.root) if T.root==NIL thenreturnelsePrint T.root.keyx=T.root.left−childwhile x=NIL doPRINT-TREE(x)x=x.right−siblingend whileend if9Algorithm19INORDER-PRINT’(T) a=T.leftprev=Twhile a=T doif prev=a.left thenprint a.keyprev=aa=a.rightelse if prev=a.right thenprev=aa=a.pelse if prev=a.p thenprev=aa=a.leftend ifend whileprint T.keya=T.rightwhile a=T doif prev=a.left thenprint a.keyprev=aa=a.rightelse if prev=a.right thenprev=aa=a.pelse if prev=a.p thenprev=aa=a.leftend ifend while10the“right”pointers until the parity of the boolean value changes,ignoring thislast node since it will be x.Problem10-1For each,we assume sorted means sorted in ascending orderunsorted,single sorted,single unsorted,double sorted,double SEARCH(L,k)n n n nINSERT(L,x)1111DELET E(L,x)n n11SUCCESSOR(L,x)n1n1P REDECESSOR(L,x)n n n1 MINIMUM(L,x)n1n1MAXIMUM(L,x)n n n1 Problem10-2In all three cases,MAKE-HEAP simply creates a new list L,sets L.head=NIL,and returns L in constant time.Assume lists are doubly linked.To realizea linked list as a heap,we imagine the usual array implementation of a binaryheap,where the children of the i th element are2i and2i+1.a.To insert,we perform a linear scan to see where to insert an element suchthat the list remains sorted.This takes linear time.Thefirst element in thelist is the minimum element,and we canfind it in constant time.Extract-minreturns thefirst element of the list,then deletes it.Union performs a mergeoperation between the two sorted lists,interleaving their entries such thatthe resulting list is sorted.This takes time linear in the sum of the lengthsof the two lists.b.To insert an element x into the heap,begin linearly scanning the list untilthefirst instance of an element y which is strictly larger than x.If no suchlarger element exists,simply insert x at the end of the list.If y does exist,replace y t by x.This maintains the min-heap property because x≤y andy was smaller than each of its children,so x must be as well.Moreover,xis larger than its parent because y was thefirst element in the list to exceedx.Now insert y,starting the scan at the node following x.Since we checkeach node at most once,the time is linear in the size of the list.To get theminimum element,return the key of the head of the list in constant time.To extract the minimum element,wefirst call MINIMUM.Next,we’ll replacethe key of the head of the list by the key of the second smallest element yin the list.We’ll take the key stored at the end of the list and use it toreplace the key of y.Finally,we’ll delete the last element of the list,and callMIN-HEAPIFY on the list.To implement this with linked lists,we need tostep through the list to get from element i to element2i.We omit this detailfrom the code,but we’ll consider it for runtime analysis.Since the value ofi on which MIN-HEAPIFY is called is always increasing and we never need11to step through elements multiple times,the runtime is linear in the length of the list.Algorithm20EXTRACT-MIN(L)min=MINIMUM(L)Linearly scan for the second smallest element,located in position i.L.head.key=L[i]L[i].key=L[L.length].keyDELETE(L,L[L.length])MIN-HEAPIFY(L[i],i)return minAlgorithm21MIN-HEAPIFY(L[i],i)1:l=L[2i].key2:r=L[2i+1].key3:p=L[i].key4:smallest=i5:if L[2i]=NIL and l<p then6:smallest=2i7:end if8:if L[2i+1]=NIL and r<L[smallest]then9:smallest=2i+110:end if11:if smallest=i then12:exchange L[i]with L[smallest]13:MIN-HEAPIFY(L[smallest],smallest)14:end ifUnion is implemented below,where we assume A and B are the two list representations of heaps to be merged.The runtime is again linear in the lengths of the lists to be merged.c.Since the algorithms in part b didn’t depend on the elements being distinct,we can use the same ones.Problem10-3a.If the original version of the algorithm takes only t iterations,then,we havethat it was only at most t random skips though the list to get to the desired value,since each iteration of the original while loop is a possible random jump followed by a normal step through the linked list.b.The for loop on lines2-7will get run exactly t times,each of which is constantruntime.After that,the while loop on lines8-9will be run exactly X t times.So,the total runtime is O(t+E[X t]).12Algorithm22UNION(A,B)1:if A.head=NIL then2:return B3:end if4:i=15:x=A.head6:while B.head=NIL do7:if B.head.key≤x.key then8:Insert a node at the end of list B with key x.key 9:x.key=B.head.key10:Delete(B,B.head)11:end if x=x.next12:end while13:return Aing equation C.25,we have that E[X t]= ∞i=1P r(X t≥i).So,we needto show that P r(X t≥i)≤(1−i/n)t.This can be seen because having X t being greater than i means that each random choice will result in an element that is either at least i steps before the desired element,or is after the desired element.There are n−i such elements,out of the total n elements that we were pricking from.So,for a single one of the choices to be from such a range, we have a probability of(n−i)/n=(1−i/n).Since each of the selections was independent,the total probability that all of them were is(1−i/n)t, as stly,we can note that since the linked list has length n,the probability that X t is greater than n is equal to zero.d.Since we have that t>0,we know that the function f(x)=x t is increasing,so,that means that x t≤f(x).So,n−1r=0r t=nr t dr≤nf(r)dr=n t+1e.E[X t]≤nr=1(1−r/n)t=nr=1ti=0ti(−r/n)i=ti=0nr=1ti(−r/n)i=ti=0ti(−1)in i−1+n−1r=0(r)t/n≤ti=0ti(−1)in i−1+n i+1/n ≤ti=0ti(−1)in ii+1=1t+1ti=0t+1i+1(−n)i≤(1−n)t+1t+1f.We just put together parts b and e to get that it runs in time O(t+n/(t+1)).But,this is the same as O(t+n/t).13g.Since we have that for any number of iterations t that the first algorithm takes to find its answer,the second algorithm will return it in time O (t +n/t ).In particular,if we just have that t =√n .The second algorithm takestime only O (√n ).This means that tihe first list search algorithm is O (√n )as well.h.If we don’t have distinct key values,then,we may randomly select an element that is further along than we had been before,but not jump to it because it has the same key as what we were currently at.The analysis will break when we try to bound the probability that X t ≥i .14。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Chapter31Michelle Bodnar,Andrew LohrApril12,2016Exercise31.1-1By the given equation,we can write c=1·a+b,with0≥b<a.By the definition of remainders given just below the division theorem,this means that b is the remainder when c is divided by a,that is b=c mod a.Exercise31.1-2Suppose that there are onlyfinitely many primes p1,p2,...,p k.Then p= p1p2···p k+1isn’t prime,so there must be some p i which divides it.However, p i·(p1···p i−1p i+1···p k)<p and p·(p1···p i−1p i+1···p k+1)>p,so p i can’t divide p.Since this holds for any choice of i,we obtain a contradiction.Thus, there are infinitely many primes.Exercise31.1-3a|b means there exists k1∈Z so that k1a=b.b|c means there exists k2∈Z so that k2b=c.This means that(k1k2)a=c.Since the integers are a ring, k1k2∈Z,so,we have that a|c.Exercise31.1-4Let g=gcd(k,p).Then g|k and g|p.Since p is prime,g=p or g=1.Since 0<k<p,g<p.Thus,g=1.Exercise31.1-5By Theorem31.2,since gcd(a,n)=1,there exist integers p,q so that pa+qn=1,so,bpa+bqn=b.Since n|ab,there exists an integer k so that kn=ab.This means that knp+pqn=(k+q)pn=b.Since n divides the left hand side,it must divide the right hand side as well.Exercise31.1-61Observe that p k =p !k !(p −k )!=p (p −1)···(p −k +1)k !.Let q =(p −1)(p −2)···(p −k +1).Since p is prime,k !|p .However,we know that p k is an integer becauseit is also a counting formula.Thus,k !divides pq .By Corollary 31.5,k !|q .Write q =ck !.Then we have p k =pc ,which is divisible by p .By the binomial theorem and the fact that p | p k for 0<k <p ,(a +b )p =p k =0p k a k b p −k ≡a p +b p (mod p ).Exercise 31.1-7First,suppose that x =yb +(x mod b ),(x mod b )=za +((x mod b )mod a ),and ka =b .Then,we have x =yka +(x mod b )=(yk +z )a +((x mod b )mod a ).So,we have that x mod a =((x mod b )mod a ).For the second part of the problem,suppose that x mod b =y mod b .Then,by the first half of the problem,applied first to x and then to b,x mod a =(x mod b )mod a =(y mod b )mod a =y mod a .So,x ≡y mod a .Exercise 31.1-8We can test in time polynomial in βwhether or not a given β-bit number is a perfect k th power.Then,since two to the βth power is longer than the number we are given,we only need to test values of k between 2and β,thereby keeping the runtime polynomial in β.To check whether a given number n is a perfect k th power,we will be using a binary search like technique.That is,if we want to find the k-th root of a number,we initially know that it lies somewhere between 0and the number itself.We then look at the number of the current range of number under con-sideration,raise it to the k th power in time polynomial in β.We can do this by the method of repeated squaring discussed in section 31.6.Then,if we get a number larger than the given number when we perform repeated squaring,we know that the true k th root is in the lower half of the range in consideration,if it is equal,it is the midpoint,if larger,it is the upper half.Since each time,we are cutting down the range by a factor of two,and it is initially a range of length Θ(2β),the number of times that we need to raise a number the the k th power is Θ(β).Putting it all together,with the O (β3)time exponentiation,we get that the total runtime of this procedure is O (β5).Exercise 31.1-9For (31.6),we see that a and b in theorem 31.2which provides a characteri-zation of gcd appear symmetrically,so swapping the two won’t change anything.For (31.7),theorem 31.2tells us that gcd’s are defined in terms of integer linear combinations.If we had some integer linear combination involving a and2b,we can changed that into one involving(-a)and b by replacing the multiplier of a with its negation.For(31.8),by repeatedly applying(31.6)and(31.7),we can get this equality for all four possible cases based on the signs of both a and b.For(31.9),consider all integer linear combinations of a and0,the thing we multiply by will not affect thefinal linear combination,so,really we are just taking the set of all integer multiples of a andfinding the smallest element. We can never decrease the absolute value of a by multiplying by an integer (|ka|=|k||a|),so,the smallest element is just what is obtained by multiplying by1,which is|a|.For(31.10),again consider possible integer linear combinations na+mka, we can rewrite this as(n+km)a,so it has absolute value|n+km||a|.Since the first factor is an integer,we can’t have it with a value less than1and still have a positivefinal answer,this means that the smallest element is when thefirst factor is1,which is achievable by setting n=1,m=0.Exercise31.1-10Consider the prime factorization of each of a,b,and c,written as a= p1p2...p k where we allow repeats of primes.The gcd of b and c is just the product of all p i such that p i appears in both factorizations.If it appears multi-ple times,we include it as many times as it appears on the side with the fewest occurrences of p i.(Explicitly,see equation31.13on page934).To get the gcd of gcd(b,c)and a,we do this again.Thus,the left hand side is just equal to the intersection(with appropriate multiplicities)of the products of prime factors of a,b,and c.For the right hand side,we consider intersectingfirst the prime factors of a and b,and then the prime factors of c,but since intersections are associative,so is the gcd operator.Exercise31.1-11Suppose to a contradiction that we had two different prime decomposition. First,we know that the set of primes they both consist of are equal,because if there were any prime p in the symmetric difference,p would divide one of them but not the other.Suppose they are given by(e1,e2,...,e r)and(f1,f2,...,f r) and suppose that e i<f i for some position.Then,we either have that p e i+1i divides a or not.If it does,then the decomposition corresponding to{e i}is wrong because it doesn’t have enough factors of p i,otherwise,the one corre-sponding to{f i}is wrong because it has too many.Exercise31.1-12Standard long division runs in O(β2),and one can easily read offthe re-mainder term at the end.3Exercise 31.1-13First,we bump up the length of the original number until it is a power of two,this will not affect the asymptotics,and we just imagine padding it with zeroes on the most significant side,so it does not change its value as a number.We split the input binary integer,and split it into two segments,a less significant half and an more significant half m ,so that the input is equal to m 2β/2+ .Then,we recursively convert m and to decimal.Also,since we’ll need it later,we compute the decimal versions of all the values of 22i up to 2β.There are only lg(β)of these numbers,so,the straightforward approach only takes time O (lg 2(β))so will be overshadowed by the rest of the algorithm.Once we’ve done that,we evaluate m 2β/2+ ,which involves computing the product of two numbers and adding two numbers,so,we have the recurrenceT (β)=2T (β/2)+M (β/2)Since we have trouble separating M from linear by a n for some epsilon,the analysis gets easier if we just forget about the fact that the difficulty of the multiplication is going down in the subcases,this concession gets us the runtime that T (β)∈O (M (β)lg(β))by master theorem.Note that there is also a procedure to convert from binary to decimal that only takes time Θ(β),instead of the given algorithm which is Θ(M (β)lg(β))∈Ω(βlg(β))that is rooted in automata theory.We can construct a deterministic finite transducer between the two languages,then,since we only need to take as many steps as there are bits in the input,the runtime will be linear.We would have states to keep track of the carryover from each digit to the next.Exercise 31.2-1First,we show that the expression given in equation (31.13)is a common divisor.To see that we just notice thata =(r i =1p e i −min(e i ,f i )i )r i =1p min(e i ,f i )i andb =(r i =1p f i −min(e i ,f i )i )ri =1p min(e i ,f i )i Since none of the exponents showing up are negative,everything in sight is an integer.Now,we show that there is no larger common divisor.We will do this by showing that for each prime,the power can be no higher.Suppose we had some common divisor d of a and b .First note that d cannot have a prime factor that doesn’t appear in both a or b ,otherwise any integer times d would also have that factor,but being a common divisor means that we can write both a and b as an integer times d .So,there is some sequence {g i }so that d = r i =1p gi i .4Now,we claim that for every i,g i≤min(e i,f i).Suppose to a contradiction that there was some i so that g i>min(e i,f i).This means that d either has more factors of p i than a or than b.However,multiplying integers can’t cause the number of factors of each prime to decrease,so this is a contradiction,since we are claiming that d is a common divisor.Since the power of each prime in d is less than or equal to the power of each prime in c,we must have that d≤c. So,c is a GCD.Exercise31.2-2We’ll create a table similar to that offigure31.1:a b a/b d x y899493129-6114934061295-640687429-1587581291-1582922901290-2910Thus,(d,x,y)=(29,−6,11).Exercise31.2-3Let c be such that a=cn+(a mod n).If k=0,it is trivial,so suppose k< 0.Then,EUCLID(a+kn,n)goes to line3,so returns EUCLID(n,a mod n). Similarly,EUCLID(a,n)=EUCLID((a mod n)+cn,n)=EUCLID(n,a mod n).So,by correctness of the Euclidean algorithm,gcd(a+kn,n)=EUCLID(a+kn,n)=EUCLID(n,a mod n)=EUCLID(a,n)=gcd(a,n)Exercise31.2-4Algorithm1ITERATIVE-EUCLID(a,b)1:while b>0do2:(a,b)=(b,a mod b)3:end while4:return aExercise31.2-5We know that for all k,if b<F k+1<φk+1/√5,then it takes fewer than ksteps.If we let k=logφb+1,then,since b<φlogφb+2/√5=φ2√5·b,we havethat it only takes1+logφ(b)steps.5We can improve this bound to1+logφ(b/gcd(a,b)).This is because we knowthat the algorithm will terminate when it reaches gcd(a,b).We will emulate the proof of lemma31.10to show a slightly different claim that Euclid’s algorithm takes k recursive calls,then a≥gcd(a,b)F k+2and b≥gcd(a,b)F k+!.We will similarly do induction on k.If it takes one recursive call,and we have a>b,we have a≥2gcd(a,b)and b=gcd(a,b).Now,suppose it holds for k−1,we want to show it holds for k.Thefirst call that is made is of EUCLID(b,a mod b).Since this then only needsk−1recursive calls,we can apply the inductive hypothesis to get that b≥gcd(a,b)F k+1and a mod b≥gcd(a,b)F k.Since we had that a>b,we havethat a≥b+(a mod b)≥gcd(a,b)(F k+1+F k)=gcd(a,b)F k+2completing the induction.Since we have that we only need k steps so long as b<gcd(a,b)F k+1<gcd(a,b)φk+1.we have that logφ(b/gcd(a,b))<k+1.This is satisfied if we setk=1+logφ(b/gcd(a,b))Exercise31.2-6Since F k+1mod F k=F k−1we have gcd(F k+1,F k)=gcd(F k,F k−1).Sincegcd(2,1)=1we have that gcd(F k+1,F k)=1for all k.Moreover,since F k is increasing,we have F k+1/F k =1for all k.When we reach the base case ofthe recursive calls to EXTENDED-EUCLID,we return(1,1,0).The following returns are given by(d,x,y)=(d ,y ,x −y ).We will show that in general, EXTENDED-EUCLID(F k+1,F k)returns(1,(−1)k+1F k−2,(−1)k F k−1).We have already shown d is correct,and handled the base case.Now suppose the claim holds for k−1.Then EXTENDED-EUCLID(F k+1,F k)returns(d ,y ,x −y ) where d =1,y =(−1)k−1F k−2,x =(−1)k F k−3,so the algorithm returns(1,(−1)k−1F k−2,(−1)k F k−3−(−1)k−1F k−2)=(1,(−1)k+1F k−2,(−1)k(F k−3+F k−2)=(1,(−1)k+1F k−2,(−1)k F k−1) as desired.Exercise31.2-7To show it is independent of the order of its arguments,we prove the fol-lowing swap property,for all a,b,c,gcd(a,gcd(b,c))=gcd(b,gcd(a,c)).By applying these swaps in some order,we can obtain an arbitrary ordering on the variables(the permutation group is generated by the set of adjacent transposi-tions).Let a i be the power of the i th prime in the prime factor decompositionof a,similarly define b i and c i.Then,we have that6gcd(a,gcd(b,c))=i p min(a i,min(b i,c i)) i=i p min(a i,b i,c i) i=i p min(b i,min(a i,c i)) i=gcd(b,gcd(a,c))Tofind the integers{x i}as described in the problem,we use a similar approach as for EXTENDED-EUCLID.Exercise31.2-8From the gcd interpretation given in equation(31.13),it is clear that lcm(a1,a2)=a1·a2 gcd(a1,a2).More generally,lcm(a1,...,a n)=a1···a ngcd(···(gcd(gcd(a1,a2),a3),...),a n).Wecan compute the denominator recursively using the two-argument gcd opera-tion.The algorithm is given below,and runs in O(n)time.Algorithm2LCM(a1,...,a n)x=a1g=a1for i=2to n dox=x·a i)g=gcd(g,a i)end forreturn x/gExercise31.2-9For two numbers to be relatively prime,we need that the set of primes that occur in both of them are disjoint.Multiplying two numbers results in a number whose set of primes is the union of the two numbers multiplied. So,if we let p(n)denote the set of primes that divide n.By testing that gcd(n1n2,n3n4)=gcd(n1n3,n2n4)=1.We get that(p(n1)∪p(n2))∩(p(n3)∪(n4))=(p(n1)∪p(n3))∩(p(n2)∪(n4))=∅.Looking at thefirst equation,it gets us that p(n1)∩p(n3)=p(n1)∩p(n4)=p(n2)∩p(n3)=p(n2)∩p(n4)=∅.The second tells,among other things,that p(n1)∩p(n2)=p(n3)∩p(n4)=∅.This tells us that the sets of primes of any two elements are disjoint,so all elements are relatively prime.A way to view this that generalizes more nicely is to consider the complete graph on n vertices.Then,we select a partition of the vertices into two parts. Then,each of these parts corresponds to the product of all the numbers cor-responding to the vertices it contains.We then know that the numbers that7any pair of vertices that are in different parts of the partition correspond to will be relatively prime,because we can distribute the intersection across the union of all the prime sets in each partition.Since partitioning into two parts is equivalent to selecting a cut,the problem reduces to selecting lg(k)cuts of K n so that every edge is cut by one of the cuts.To do this,first cut the vertex set in as close to half as possible.Then,for each part,we recursively try to cut in in close to half,since the parts are disjoint,we can arbitrarily combine cuts on each of them into a single cut of the original graph.Since the number of time you need to divide n by two to get1is lg(n) ,we have that that is the number of times we need to take gcd.Exercise31.3-1+4012300123112302230133012·5123411234224133314244321Then,we can see that these are equivalent under the mappingα(0)=1,α(1)=3,α(2)=4,α(3)=2.Exercise31.3-2The subgroups of Z9and{0},{0,3,6},and Z9itself.The subgroups of Z∗13 are{1},{1,3,9},{1,3,4,9,10,12},{1,5,8,12},{1,12}and Z∗13itself. Exercise31.3-3Since S was afinite group,every element had afinite order,so,if a∈S , there is some number of times that you can add it to itself so that you get the identity,since adding any two things in S gets us something in S ,we have that S has the identity element.Associativity is for free because is is a property of the binary operation,no the space that the operation draws it’s arguments stly,we can see that it contains the inverse of every element,because we can just add the element to itself a number of times equal to one less than its order.Then,adding the element to that gets us the identity.Exercise31.3-48The only prime divisor of p e is p.From the definition of the Euler phi function,we haveφ(p e)=p e1−1p=p e−1(p−1).Exercise31.3-5To see this fact,we need to show that the given function is a bijection.Since the two sets have equal size,we only need to show that the function is onto. To see that it is onto,suppose we want an element that maps to x.Since Z∗n is afinite Abelian group by theorem31.13,we can take inverses,in particu-lar,there exists an element a−1so that aa−1=1mod n.This means that f a(a−1x)=aa−1x mod n=(aa−1mod n)(x mod n)=x mod n.Since we canfind an element that maps to any element of the range and the sizes of domain and range are the same,the function is a bijection.Any bijection from a set to itself is a permutation by definition.Exercise31.4-1First,we run extended Euclid on35,50and get the result(5,−7,10).Then, our initial solution is−7∗10/5=−14=36.Since d=5,we have four other solutions,corresponding to adding multiples of50/5=10.So,we also have that our entire solution set is x={6,16,26,36,46}.Exercise31.4-2If ax≡ay mod n then a(x−y)≡0mod n.Since gcd(a,n)=1,n doesn’t divide a unless n=1,in which case the claim is trivial.By Corollary31.5, since n divides a(x−y),n divides x−y.Thus,x≡y mod n.To see that the condition gcd(a,n)is necessary,let a=3,n=6,x=6,and y=2.Note that gcd(a,n)=gcd(3,6)=3.Then3·6≡3·2mod6,but6=2mod6.Exercise31.4-3it will work.It just changes the initial value,and so changes the order in which solutions are output by the program.Since the program outputs all val-ues of x that are congruent to x0mod n/b,if we shift the answer by a multiple of n/b by this modification,we will not be changing the set of solutions that the procedure outputs.Exercise31.4-4The claim is clear if a≡0since we can just factor out an x.For a=0, let g(x)=g0+g1x+...+g t−1x t−1.In order for f(x)to equal(x−a)g(x) we must have g0=f0(−a)−1,g i=(f i−g i−1)(−a)−1for1≤i≤t−1and9g t −1=f t .Since p is prime,Z ∗p ={1,2,...,p −1}so every element,includ-ing −a ,has a multiplicative inverse.It is easy to satisfy each of these equa-tions as we go,until we reach the last two,at which point we need to satisfy both g t −1=(f t −1−g t −2)(−a )−1and g t −1=f t .More compactly,we need f t =(f t −1−g t −2)(−a )−1.We will show that this happens when a is a zero of f .First,we’ll proceed inductively to show that for 0≤k ≤t −1we have a k +1g k =− k i =0f i a i .For the base case we have ag 0=−f 0,which holds.Assume the claim holds for k −1.Then we havea k +1g k =a k +1(f k −g k −1)(−a )−1=−a k f k +a k g k −1=−a k f k −k −1 i =0f i a i =ki =0f i a iwhich completes the induction step.Now we show that we can satisfy the equation given above.It will suffice to show that −a t f t =a t −1(f t −1−g t −2).a t −1(f t −1−g t −2)=a t −1f t −1−a t −1g t −2=a t −1f t −1+t −2 i =0f i a i =t −1i =0f i a i =−a t f twhere the second equality is justified by our earlier claim and the last equality is justified because a is a zero of f .Thus,we can find such a g .It is clear that a polynomial of degree 1can have at most 1distinct zero modulo p since the equation x =−a has at most 1solution by Corollary 31.25.Now suppose the claim holds for t >1.Let f be a degree t +1polynomial.If f has no zeros then we’re done.Otherwise,let a be a zero of f .Write f (x )=(x −a )g (x ).Then by the induction hypothesis,since g is of degree t ,g has at most t distinct zeros modulo p .The zeros of f are a and the zeros of g ,so f has at most t +1distinct zeros modulo p .Exercise 31.5-110These equations can be viewed as a single equation in the ring Z +5×Z 11+,in particular (x 1,x 2)=(4,5).This means that x needs to be the element in Z 55+that corresponds to the element (4,5).To do this,we use the process de-scribed in the proof of Theorem 31.27.We have m 1=11,m 2=5,c 1=11(11−1mod 5)=11,c 2=5(5−1mod 11)=45.This means that the corresponding solution is x =11·4+45·5mod 55=44+225mod 55=269mod 55=49mod 55.So,all numbers of the form 49+55k are a solution.Exercise 31.5-2Since 9·8·7=504,we’ll be working mod 504.We also have m 1=56,m 2=63,and m 3=72.We compute c 1=56(5)=280,c 2=63(7)=441,and c 3=72(4)=288.Thus,a =280+2(441)+3(288)mod 504=10mod 504.Thus,the desired integers x are of the form x =10+504k for k ∈Z .Exercise 31.5-3Suppose that x ≡a −1mod n .Also,x i ≡x mod n i and a i ≡a mod n i .What we then want to show is that x i ≡a −1i mod n i .That is,we want that a i x i ≡1mod n i .To see this,we just use equation 31.30.To get that ax mod n corresponds to (a 1x 1mod n 1,...,a k x k mod n k ).This means that 1corresponds to (1mod n 1,...,1mod n k ).This is telling us exactly what we needed,in particular,that a i x i ≡1mod n i .Exercise 31.5-4Let f (x )=f 0+f 1x +...+f d x d .Using the correspondence of Theorem 31.27,f (x )≡0mod n if and only if d i =0f i j x i j ≡0mod n j for j =1to k .The product formula arises by constructing a zero of f via choosing x 1,x 2,...,x k such that f (x j )≡0mod n j for each j ,then letting x be the number associated to (x 1,...,x k ).Exercise 31.6-1elementorder 112103545556107108109510211The smallest primitive root is2,and has the following values for ind11,2(x)x ind11,2(x)1102138425469778396105Exercise31.6-2To perform modular exponentiation by examining bits from right to left, we’ll keep track of the value of a2i as we go.See below for an implementation:Algorithm3MODULAR-EXPONENTIATION-R-to-L(a,b,n)c=0d=1s=alet b k,b k−1,...,b0 be the binary representation of bfor i=0to k doif b i==1thend=s·d mod nc=2i+cend ifs=s·s mod nend forreturn dExercise31.6-3Since we knowφ(n),we know that aφ(n)≡1mod n by Euler’s theorem. This tells us that a−1=aφ(n)−1because aa−1=≡aaφ(n)−1≡aφ(n)≡1mod n. Since when we multiply this expression times a,we get the identity,it is the inverse of a.We can then compute nφ(n)efficiently,sinceφ(n)<n,so can be represented without using more bits than was used to represent n.Exercise31.7-1For the secret key’s value of e we compute the inverse of d=3modφ(n)= 280.To do this,wefirst computeφ(280)=φ(23)φ(7)φ(5)=4·6·4=96.Since12any number raised to this will be one mod280,we will raise it to one less than this.So,we compute395≡3(32)47≡3(9(92)23)≡3(9(81(812)11))≡3(9(81(121(1212)5)))≡3(9(81(121(81(812)2≡3·9·81·121·81·81≡3·9·121≡187mod280Now that we know our value of e,we compute the encryption of M=100 by computing100187mod319,which comes out to an encrypted message of122 Exercise31.7-2We know ed=1modφ(n).Since d<φ(n)and e=3,we have3d−1= k(p−1)(q−1)for k=1or2.We have k=1if3d−1<n and k=2if 3d−1>n.Once we’ve determined k,p+q=n−(3d−1)/k+1,so we can now solve for p+q in time polynomial inβ.Replacing q−1by(p+q)−p−1 in our earlier equation lets us solve for p in time polynomial inβsince we need only perform addition,multiplication,and division on numbers bounded by n. Exercise31.7-3P A(M1)P A(M2)≡M e1M e2≡(M1M2)e≡P A(M1M2)mod nSo,if the attacker can correctly decode1100of the encrypted messages,hedoes the following.If the message is one that he can decrypt,he is happy,de-crypts it and stops.If it is not one that he can decrypt,then,he picks a random element in Z m,say x encrypts it with the public key,and multiplies that by theencrypted text,he then has a1100chance to be able to decrypt the new message.He keeps doing this until he can decrypt it.The number of steps needed follows a geometric distribution with a expected value of100.Once he’s stumbled upon one that he could decrypt,he multiplies by the inverses of all the elements that he multiplied by along the way.This recovers thefinal answer,and also can be done efficiently,since for every x,x n−2is x−1by Lagrange’s theorem.13Exercise31.8-1Suppose that we can write n= ki=1p e ii,then,by the Chinese remainder the-orem,we have that Z n∼=Z p e11×···×Z p e11.Since we had that n was not aprime power,we know that k≥2.This means that we can take the elementsx=(p e11−1,1,...,1)and y=(1,p e22−1,1,...,1).Since multiplication in theproduct ring is just coordinate wise,we have that the squares of both of theseelements is the all ones element in the product ring,which corresponds to1inZ n.Also,since the correspondence from the Chinese remainder theorem was a bijection,since x and y are distinct in the product ring,they correspond todistinct elements in Z n.Thus,by taking the elements corresponding to x and yunder the Chinese remainder theorem bijection,we have that we have found twosquareroots of1that are not the identity in Z n.Since there is only one trivialnon-identity squareroot in Z n,one of the two must be non-trivial.It turns outthat both are non-trivial,but that’s more than the problem is asking. Exercise31.8-2Let c=gcd(···(gcd(gcd(φ(p e11),φ(p e22)),φ(p e33)),...),φ(p e r r)).Then we haveλ(n)=φ(e e11)···φ(p e r r)/c=φ(n)/c.Since the lcm is an integer,λ(n)|φ(n).Suppose p is prime and p2|n.Sinceφ(p2)=p2(1−1p )=p2−p=p(p−1),we have that p must divideλ(n).however,since p divides n,it cannot divide n−1,so we cannot haveλ(n)|n−1.Now,suppose that is the product of fewer than3primes,that is n=pq for some two distinct primes p<q.Since both p and q were primes,λ(n)= lcm(φ(p),φ(q))=lcm(p−1,q−1).So,mod q−1,λ(n)≡0,however,since n−1=pq−1=(q−1)(p)+p−1,we have that n−1≡p−1mod q−1. Sinceλ(n)has a factor of q−1that n−1does not,meaning thatλ(n)|n−1.Exercise31.8-3First,we prove the following lemma.For any integers a,b,n,gcd(a,n)·gcd(b,n)≥gcd(ab,n).Let{p i}be an enumeration of the primes,then,by Theorem31.8,there is exactly one set of powers of these primes so that a=i p a ii,b=ip b ii,and n=ip n ii.gcd(a,n)=ip min(a i,n i)igcd(b,n)=ip min(b i,n i)igcd(ab,n)=ip min(a i+b i,n i)i14We combine thefirst two equations to get:gcd(a,n)·gcd(b,n)=ip min(a i,n i)i·ip min(b i,n i)i=i p min(a i,n i)+min(b i,n i) i≥i p min(a i+b i,n i) i=gcd(ab,n)Since x is a non-trivial squareroot,we have that x2≡1mod n,but x=1 and x=n−1.Now,we consider the value of gcd(x2−1,n).By theorem31.9, this is equal to gcd(n,x2−1mod n)=gcd(n,1−1)=gcd(n,0)=n.So,we can then look at the factorization of x2−1=(x+1)(x−1)to get thatgcd(x+1,n)gcd(x−1,n)≥nHowever,we know that since x is a nontrivial squareroot,we know that 1<x<n−1so,neither of the factors on the right can be equal to n.This means that both of the factors on the right must be nontrivial.Exercise31.9-1The Pollard-Rho algorithm wouldfirst detect the factor of73when it con-siders the element84,when we have x12because we then notice that gcd(814−84,1387)=73.Exercise31.9-2Create an array A of length n.For each x i,if x i=j,store i in A[j].If j is thefirst position of A which must have its entry rewritten,set t to be the entry originally stored in that spot.Then count how many additional x i’s must be computed until x i=j again.This is the value of u.The running time isΘ(t+u).Exercise31.9-3Assuming that p e divides n,by the same analysis as sin the chapter,it will take timeΘ(p e/2).To see this,we look at what is happening to the sequence mod p n.15x i +1=x i +1mod p e =f n (x i )mod p e=((x 2−1)mod n )mod p e =(x 2−1)mod p e=(x i )2−1mod p e =f p e (x i )So,we again are having the birthday paradox going on,but,instead of hop-ing for a repeat from a set of size p ,we are looking at all the equivalence classes mod p e which has size p e ,so,we have that the expected number of steps be-fore gettinga repeat in that size set is just the squareroot of its size,which is Θ(√p e )=Θ(p e/2).Exercise 31.9-4Problem 31-1a.If a and b are both even,then we can write them as a =2(a/2)and b =2(b/2)where both factors in each are integers.This means that,by Corollary 31.4,gcd(a,b )=2gcd(a/2,b/2).b.If a is odd,and b is even,then we can write b =2(b/2),where b/2is an integer,so,since we know that 2does not divide a ,the factor of two that is in b cannot be part of the gcd of the two numbers.This means that we have gcd(a,b )=gcd(a,b/2).More formally,suppose that d =gcd(a,b ).Since d is a common divisor,it must divide a ,and so,it must not have any even factors.This means that it also divides a and b/2.This means that gcd(a,b )≤gcd(a,b/2).To see the reverse,suppose that d =gcd(a,b/2),then it is also a divisor of a and b ,since we can just double whatever we need to multiply it by to get b/2.Since we have inequalities both ways,we have equality.c.If a and b are both odd,then,first,in analoge to theorem 31.9,we show that gcd(a,b )=gcd(a −b,b ).Let d and d be the gcd’s on the left and right respectively.Then,we have that there exists n 1,n 2so that n 1a +n 2b =d ,but then,we can rewrite to get n 1(a −b )+(n 1+n 2)b =d .This getsus d ≥d .To see the reverse,let n 1,n 2so that n 1(a −b )+n 2b =d .We rewrite to get n 1a +(n 2−n 1)b =d ,so we have d ≥d .This meansthat gcd(a,b )=gcd(a −b,b )=gcd(b,a −b ).From there,we fall into the case of part b.This is because the first argument is odd,and the second is the difference of two odd numbers,hence is even.This means we can halve the second argument without changing the quantity.So,gcd(a,b )=gcd(b,(a −b )/2)=gcd((a −b )/2,b ).16。

相关文档
最新文档