task
stringlengths
0
154k
__index_level_0__
int64
0
39.2k
Problem F: Tighten Up! We have a flat panel with two holes. Pins are nailed on its surface. From the back of the panel, a string comes out through one of the holes to the surface. The string is then laid on the surface in a form of a polygonal chain, and goes out to the panel's back through the other hole. Initially, the string does not touch any pins. Figures F-1, F-2, and F-3 show three example layouts of holes, pins and strings. In each layout, white squares and circles denote holes and pins, respectively. A polygonal chain of solid segments denotes the string. Figure F-1: An example layout of holes, pins and a string Figure F-2: An example layout of holes, pins and a string Figure F-3: An example layout of holes, pins and a string When we tie a pair of equal weight stones to the both ends of the string, the stones slowly straighten the string until there is no loose part. The string eventually forms a different polygonal chain as it is obstructed by some of the pins. (There are also cases when the string is obstructed by no pins, though.) The string does not hook itself while being straightened. A fully tightened string thus draws a polygonal chain on the surface of the panel, whose vertices are the positions of some pins with the end vertices at the two holes. The layouts in Figures F-1, F-2, and F-3 result in the respective polygonal chains in Figures F-4, F-5, and F-6. Write a program that calculates the length of the tightened polygonal chain. Figure F-4: Tightened polygonal chains from the example in Figure F-1. Figure F-5: Tightened polygonal chains from the example in Figure F-2. Figure F-6: Tightened polygonal chains from the example in Figure F-3. Note that the strings, pins and holes are thin enough so that you can ignore their diameters. Input The input consists of multiple datasets, followed by a line containing two zeros separated by a space. Each dataset gives the initial shape of the string (i.e., the positions of holes and vertices) and the positions of pins in the following format. m n x 1 y 1 ... x l y l The first line has two integers m and n (2 ≀ m ≀ 100, 0 ≀ n ≀ 100), representing the number of vertices including two holes that give the initial string shape ( m ) and the number of pins ( n ). Each of the following l = m + n lines has two integers x i and y i (0 ≀ x i ≀ 1000, 0 ≀ y i ≀ 1000), representing a position P i = ( x i , y i ) on the surface of the panel. Positions P 1 , ..., P m give the initial shape of the string; i.e., the two holes are at P 1 and P m , and the string's shape is a polygonal chain whose vertices are P i ( i = 1, ..., m ), in this order. Positions P m +1 , ..., P m + n are the positions of the pins. Note that no two points are at the same position. No three points are exactly on a straight line. Output For each dataset, the length of the part of the tightened string that remains on the surface of the panel should be output in a line. No extra characters should appear in the output. No lengths in the output should have an error greater than 0.001. Sample Input 6 16 5 4 11 988 474 975 459 16 985 12 984 982 242 227 140 266 45 410 92 570 237 644 370 567 406 424 336 290 756 220 634 251 511 404 575 554 726 643 868 571 907 403 845 283 10 4 261 196 943 289 859 925 56 822 112 383 514 0 1000 457 514 1000 0 485 233 224 710 242 850 654 485 915 140 663 26 5 0 953 180 0 299 501 37 301 325 124 162 507 84 140 913 409 635 157 645 555 894 229 598 223 783 514 765 137 599 445 695 126 859 462 599 312 838 167 708 563 565 258 945 283 251 454 125 111 28 469 1000 1000 185 319 717 296 9 315 372 249 203 528 15 15 200 247 859 597 340 134 967 247 421 623 1000 427 751 1000 102 737 448 0 978 510 556 907 0 582 627 201 697 963 616 608 345 819 810 809 437 706 702 695 448 474 605 474 329 355 691 350 816 231 313 216 864 360 772 278 756 747 529 639 513 525 0 0 Output for the Sample Input 2257.0518296609 3609.92159564177 2195.83727086364 3619.77160684813
35,600
Score : 200 points Problem Statement Given is a string S . Replace every character in S with x and print the result. Constraints S is a string consisting of lowercase English letters. The length of S is between 1 and 100 (inclusive). Input Input is given from Standard Input in the following format: S Output Replace every character in S with x and print the result. Sample Input 1 sardine Sample Output 1 xxxxxxx Replacing every character in S with x results in xxxxxxx . Sample Input 2 xxxx Sample Output 2 xxxx Sample Input 3 gone Sample Output 3 xxxx
35,601
F : 卵 / Eggs 問題文 1 か月前のこずである 小孊生の肉西君は倏䌑みの宿題をやっおいなかった そこで自由研究は家にあった卵の匷床を調べるこずにした この研究においお卵を高さ H から萜ずしおも割れず 高さ H+1 から萜ずすず割れるずき その卵の匷床は H であるず定矩する ここで H は非負敎数であり非負敎数以倖の高さから萜ずすこずは無いずする 肉西くんは卵を 1 ぀萜䞋させる実隓を行う 実隓の結果は割れるか割れないかのいずれかである たた卵の匷床は党お同じである぀たりどの卵を甚いおも実隓の結果は同じである 肉西くんは高さ 1 から N たでの敎数の高さの段からなる階段ず 匷床が䞍明な E 個の卵を甚意した 高さ 0 では割れず高さ N+1 では割れるずいうこずは既にわかっおいる 肉西くんは各段ず同じ高さから地面に向かっお萜ずしその床に卵が割れたか割れなかったかを調べる このずき割れた卵は二床ず䜿えないが割れなかった堎合は再利甚できる この実隓を卵が残っおいる限り続けるこずができる 䜕床か実隓を繰り返し䞊に定めた H が求たったずき卵の匷床が求たったずする 倏䌑み終了たで埌数日しか無い 最小の回数で実隓を終わらせないず間に合わない そこで肉西くんの兄であるあなたは卵の匷床を知るために 萜ずす回数が少なくなるように最適な方法をずった堎合に 必芁な実隓回数の最倧倀を求めるプログラムを曞くこずにした 入力 T N_1 E_1 
 N_T E_T 1 ぀のファむルに耇数のテストケヌスが含たれる 1 行目に敎数 T が䞎えられる 1+i 行目に i 番目のテストケヌス E_i, N_i が䞎えられる 制玄 敎数である 1 ≀ T ≀ 1000 1 ≀ N_i ≀ 10^{18} 1 ≀ E_i ≀ 50 出力が 50 を超えるような入力は含たれない 出力 i 番目のテストケヌスに察する答えを i 行目に出力せよ 党䜓で T 行にわたる サンプル サンプル入力1 3 5 1 5 2 1 2 サンプル出力1 5 3 1 1 ぀目の堎合 卵が 1 ぀しかないため 1 段目から順に萜ずしおいくしかない 2 ぀目の堎合 たず 2 段目から萜ずす 2 段目から萜ずしお割れた堎合 1 段目から萜ずす 2 段目から萜ずしお割れなかった堎合 4 段目から萜ずす 1 段目から萜ずしお割れた堎合実隓終了 1 段目から萜ずしお割れなかった堎合実隓終了 4 段目から萜ずしお割れた堎合 3 段目から萜ずす 4 段目から萜ずしお割れなかった堎合 5 段目から萜ずす 3 段目から萜ずしお割れた堎合実隓終了 3 段目から萜ずしお割れなかった堎合実隓終了 5 段目から萜ずしお割れた堎合実隓終了 5 段目から萜ずしお割れなかった堎合実隓終了 3 ぀目の堎合 1 段目から萜ずしお実隓終了
35,602
Score : 1000 points Problem Statement Consider the following game: The game is played using a row of N squares and many stones. First, a_i stones are put in Square i\ (1 \leq i \leq N) . A player can perform the following operation as many time as desired: "Select an integer i such that Square i contains exactly i stones. Remove all the stones from Square i , and add one stone to each of the i-1 squares from Square 1 to Square i-1 ." The final score of the player is the total number of the stones remaining in the squares. For a sequence a of length N , let f(a) be the minimum score that can be obtained when the game is played on a . Find the sum of f(a) over all sequences a of length N where each element is between 0 and K (inclusive). Since it can be extremely large, find the answer modulo 1000000007 (= 10^9+7) . Constraints 1 \leq N \leq 100 1 \leq K \leq N Input Input is given from Standard Input in the following format: N K Output Print the sum of f(a) modulo 1000000007 (= 10^9+7) . Sample Input 1 2 2 Sample Output 1 10 There are nine sequences of length 2 where each element is between 0 and 2 . For each of them, the value of f(a) and how to achieve it is as follows: f(\{0,0\}) : 0 (Nothing can be done) f(\{0,1\}) : 1 (Nothing can be done) f(\{0,2\}) : 0 (Select Square 2 , then Square 1 ) f(\{1,0\}) : 0 (Select Square 1 ) f(\{1,1\}) : 1 (Select Square 1 ) f(\{1,2\}) : 0 (Select Square 1 , Square 2 , then Square 1 ) f(\{2,0\}) : 2 (Nothing can be done) f(\{2,1\}) : 3 (Nothing can be done) f(\{2,2\}) : 3 (Select Square 2 ) Sample Input 2 20 17 Sample Output 2 983853488
35,603
AYBABTU There is a tree that has n nodes and n-1 edges. There are military bases on t out of the n nodes. We want to disconnect the bases as much as possible by destroying k edges. The tree will be split into k+1 regions when we destroy k edges. Given the purpose to disconnect the bases, we only consider to split in a way that each of these k+1 regions has at least one base. When we destroy an edge, we must pay destroying cost. Find the minimum destroying cost to split the tree. Input The input consists of multiple data sets. Each data set has the following format. The first line consists of three integers n , t , and k ( 1 \leq n \leq 10,000 , 1 \leq t \leq n , 0 \leq k \leq t-1 ). Each of the next n-1 lines consists of three integers representing an edge. The first two integers represent node numbers connected by the edge. A node number is a positive integer less than or equal to n . The last one integer represents destroying cost. Destroying cost is a non-negative integer less than or equal to 10,000. The next t lines contain a distinct list of integers one in each line, and represent the list of nodes with bases. The input ends with a line containing three zeros, which should not be processed. Output For each test case, print its case number and the minimum destroying cost to split the tree with the case number. Sample Input 2 2 1 1 2 1 1 2 4 3 2 1 2 1 1 3 2 1 4 3 2 3 4 0 0 0 Output for the Sample Input Case 1: 1 Case 2: 3
35,604
Score : 100 points Problem Statement Snuke has a grid consisting of three squares numbered 1 , 2 and 3 . In each square, either 0 or 1 is written. The number written in Square i is s_i . Snuke will place a marble on each square that says 1 . Find the number of squares on which Snuke will place a marble. Constraints Each of s_1 , s_2 and s_3 is either 1 or 0 . Input Input is given from Standard Input in the following format: s_{1}s_{2}s_{3} Output Print the answer. Sample Input 1 101 Sample Output 1 2 A marble will be placed on Square 1 and 3 . Sample Input 2 000 Sample Output 2 0 No marble will be placed on any square.
35,605
Score : 100 points Problem Statement There are N dishes, numbered 1, 2, \ldots, N . Initially, for each i ( 1 \leq i \leq N ), Dish i has a_i ( 1 \leq a_i \leq 3 ) pieces of sushi on it. Taro will perform the following operation repeatedly until all the pieces of sushi are eaten: Roll a die that shows the numbers 1, 2, \ldots, N with equal probabilities, and let i be the outcome. If there are some pieces of sushi on Dish i , eat one of them; if there is none, do nothing. Find the expected number of times the operation is performed before all the pieces of sushi are eaten. Constraints All values in input are integers. 1 \leq N \leq 300 1 \leq a_i \leq 3 Input Input is given from Standard Input in the following format: N a_1 a_2 \ldots a_N Output Print the expected number of times the operation is performed before all the pieces of sushi are eaten. The output is considered correct when the relative difference is not greater than 10^{-9} . Sample Input 1 3 1 1 1 Sample Output 1 5.5 The expected number of operations before the first piece of sushi is eaten, is 1 . After that, the expected number of operations before the second sushi is eaten, is 1.5 . After that, the expected number of operations before the third sushi is eaten, is 3 . Thus, the expected total number of operations is 1 + 1.5 + 3 = 5.5 . Sample Input 2 1 3 Sample Output 2 3 Outputs such as 3.00 , 3.000000003 and 2.999999997 will also be accepted. Sample Input 3 2 1 2 Sample Output 3 4.5 Sample Input 4 10 1 3 2 3 3 2 3 2 1 3 Sample Output 4 54.48064457488221
35,606
Problem I: Light The Room You are given plans of rooms of polygonal shapes. The walls of the rooms on the plans are placed parallel to either x -axis or y -axis. In addition, the walls are made of special materials so they reflect light from sources as mirrors do, but only once. In other words, the walls do not reflect light already reflected at another point of the walls. Now we have each room furnished with one lamp. Walls will be illuminated by the lamp directly or indirectly. However, since the walls reflect the light only once, some part of the walls may not be illuminated. You are requested to write a program that calculates the total length of unilluminated part of the walls. Figure 10: The room given as the second case in Sample Input Input The input consists of multiple test cases. The first line of each case contains a single positive even integer N (4 ≀ N ≀ 20), which indicates the number of the corners. The following N lines describe the corners counterclockwise. The i-th line contains two integers x i and y i , where ( x i , y i ) indicates the coordinates of the i -th corner. The last line of the case contains x' and y' , where ( x' , y' ) indicates the coordinates of the lamp. To make the problem simple, you may assume that the input meets the following conditions: All coordinate values are integers not greater than 100 in their absolute values. No two walls intersect or touch except for their ends. The walls do not intersect nor touch each other. The walls turn each corner by a right angle. The lamp exists strictly inside the room off the wall. The x-coordinate of the lamp does not coincide with that of any wall; neither does the y-coordinate. The input is terminated by a line containing a single zero. Output For each case, output the length of the unilluminated part in one line. The output value may have an arbitrary number of decimal digits, but may not contain an error greater than 10 -3 . Sample Input 4 0 0 2 0 2 2 0 2 1 1 6 2 2 2 5 0 5 0 0 5 0 5 2 1 4 0 Output for the Sample Input 0.000 3.000
35,607
Problem E: Psychic Accelerator In the west of Tokyo, there is a city named “Academy City.” There are many schools and laboratories to develop psychics in Academy City. You are a psychic student of a school in Academy City. Your psychic ability is to give acceleration to a certain object. You can use your psychic ability anytime and anywhere, but there are constraints. If the object remains stationary, you can give acceleration to the object in any direction. If the object is moving, you can give acceleration to the object only in 1) the direction the object is moving to, 2) the direction opposite to it, or 3) the direction perpendicular to it. Today’s training menu is to move the object along a given course. For simplicity you can regard the course as consisting of line segments and circular arcs in a 2-dimensional space. The course has no branching. All segments and arcs are connected smoothly, i.e. there are no sharp corners. In the beginning, the object is placed at the starting point of the first line segment. You have to move the object to the ending point of the last line segment along the course and stop the object at that point by controlling its acceleration properly. Before the training, a coach ordered you to simulate the minimum time to move the object from the starting point to the ending point. Your task is to write a program which reads the shape of the course and the maximum acceleration a max you can give to the object and calculates the minimum time to move the object from the starting point to the ending point. The object follows basic physical laws. When the object is moving straight in some direction, with acceleration either forward or backward, the following equations hold: v = v 0 + at and s = v 0 t + (1/2) at 2 where v , s , v 0 , a , and t are the velocity, the distance from the starting point, the initial velocity (i.e. the velocity at the starting point), the acceleration, and the time the object has been moving in that direction, respectively. Note that they can be simplified as follows: v 2 − v 0 2 = 2 as When the object is moving along an arc, with acceleration to the centroid, the following equations hold: a = v 2 / r wher v , a , and r are the velocity, the acceleration, and the radius of the arc, respectively. Note that the object cannot change the velocity due to the criteria on your psychic ability. Input The input has the following format: N a max x a ,1 y a ,1 x b ,1 y b ,1 x a ,2 y a ,2 x b ,2 y b ,2 . . . N is the number of line segments; a max is the maximum acceleration you can give to the object; ( x a,i , y a,i ) and ( x b,i , y b,i ) are the starting point and the ending point of the i -th line segment, respectively. The given course may have crosses but you cannot change the direction there. The input meets the following constraints: 0 < N ≀ 40000, 1 ≀ a max ≀ 100, and -100 ≀ x a i , y a i , x b i , y b i ≀ 100. Output Print the minimum time to move the object from the starting point to the ending point with an relative or absolute error of at most 10 -6 . You may output any number of digits after the decimal point. Sample Input 1 2 1 0 0 1 0 1 1 0 1 Output for the Sample Input 1 5.2793638507 Sample Input 2 1 1 0 0 2 0 Output for the Sample Input 2 2.8284271082 Sample Input 3 3 2 0 0 2 0 1 -1 1 2 0 1 2 1 Output for the Sample Input 3 11.1364603512
35,608
Score: 400 points Problem Statement Kizahashi, who was appointed as the administrator of ABC at National Problem Workshop in the Kingdom of AtCoder, got too excited and took on too many jobs. Let the current time be time 0 . Kizahashi has N jobs numbered 1 to N . It takes A_i units of time for Kizahashi to complete Job i . The deadline for Job i is time B_i , and he must complete the job before or at this time. Kizahashi cannot work on two or more jobs simultaneously, but when he completes a job, he can start working on another immediately. Can Kizahashi complete all the jobs in time? If he can, print Yes ; if he cannot, print No . Constraints All values in input are integers. 1 \leq N \leq 2 \times 10^5 1 \leq A_i, B_i \leq 10^9 (1 \leq i \leq N) Input Input is given from Standard Input in the following format: N A_1 B_1 . . . A_N B_N Output If Kizahashi can complete all the jobs in time, print Yes ; if he cannot, print No . Sample Input 1 5 2 4 1 9 1 8 4 9 3 12 Sample Output 1 Yes He can complete all the jobs in time by, for example, doing them in the following order: Do Job 2 from time 0 to 1 . Do Job 1 from time 1 to 3 . Do Job 4 from time 3 to 7 . Do Job 3 from time 7 to 8 . Do Job 5 from time 8 to 11 . Note that it is fine to complete Job 3 exactly at the deadline, time 8 . Sample Input 2 3 334 1000 334 1000 334 1000 Sample Output 2 No He cannot complete all the jobs in time, no matter what order he does them in. Sample Input 3 30 384 8895 1725 9791 170 1024 4 11105 2 6 578 1815 702 3352 143 5141 1420 6980 24 1602 849 999 76 7586 85 5570 444 4991 719 11090 470 10708 1137 4547 455 9003 110 9901 15 8578 368 3692 104 1286 3 4 366 12143 7 6649 610 2374 152 7324 4 7042 292 11386 334 5720 Sample Output 3 Yes
35,609
Score : 500 points Problem Statement There are N towns on a plane. The i -th town is located at the coordinates (x_i,y_i) . There may be more than one town at the same coordinates. You can build a road between two towns at coordinates (a,b) and (c,d) for a cost of min(|a-c|,|b-d|) yen (the currency of Japan). It is not possible to build other types of roads. Your objective is to build roads so that it will be possible to travel between every pair of towns by traversing roads. At least how much money is necessary to achieve this? Constraints 2 ≀ N ≀ 10^5 0 ≀ x_i,y_i ≀ 10^9 All input values are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 x_2 y_2 : x_N y_N Output Print the minimum necessary amount of money in order to build roads so that it will be possible to travel between every pair of towns by traversing roads. Sample Input 1 3 1 5 3 9 7 8 Sample Output 1 3 Build a road between Towns 1 and 2 , and another between Towns 2 and 3 . The total cost is 2+1=3 yen. Sample Input 2 6 8 3 4 9 12 19 18 1 13 5 7 6 Sample Output 2 8
35,610
Problem F: Numoeba A scientist discovered a strange variation of amoeba. The scientist named it numoeba . A numoeba, though it looks like an amoeba, is actually a community of cells, which always forms a tree. The scientist called the cell leader that is at the root position of the tree. For example, in Fig. 1, the leader is A . In a numoeba, its leader may change time to time. For example, if E gets new leadership, the tree in Fig. 1 becomes one in Fig. 2. We will use the terms root, leaf, parent, child and subtree for a numoeba as defined in the graph theory. Numoeba changes its physical structure at every biological clock by cell division and cell death. The leader may change depending on this physical change. The most astonishing fact about the numoeba cell is that it contains an organic unit called numbosome , which represents an odd integer within the range from 1 to 12,345,677. At every biological clock, the value of a numbosome changes from n to a new value as follows: The maximum odd factor of 3 n + 1 is calculated. This value can be obtained from 3 n + 1 by repeating division by 2 while even. If the resulting integer is greater than 12,345,678, then it is subtracted by 12,345,678. For example, if the numbosome value of a cell is 13, 13 × 3 + 1 = 40 is divided by 2 3 = 8 and a new numbosome value 5 is obtained. If the numbosome value of a cell is 11,111,111, it changes to 4,320,989, instead of 16,666,667. If 3 n + 1 is a power of 2, yielding 1 as the result, it signifies the death of the cell as will be described below. At every biological clock, the next numbosome value of every cell is calculated and the fate of the cell and thereby the fate of numoeba is determined according to the following steps. A cell that is a leaf and increases its numbosome value is designated as a candidate leaf. A cell dies if its numbosome value becomes 1. If the dying cell is the leader of the numoeba, the numoeba dies as a whole. Otherwise, all the cells in the subtree from the dying cell (including itself) die. However, there is an exceptional case where the cells in the subtree do not necessarily die; if there is only one child cell of the dying non-leader cell, the child cell will replace the dying cell. Thus, a straight chain simply shrinks if its non-leader constituent dies. For example, consider a numoeba with the leader A below. If the leader A dies in (1), the numoeba dies. If the cell D dies in (1), (1) will be as follows. And, if the cell E dies in (1), (1) will be as follows. Note that this procedure is executed sequentially, top-down from the root of the numoeba to leaves. If the cells E and F will die in (1), the death of F is not detected at the time the procedure examines the cell E . The numoeba, therefore, becomes (3). One should not consider in such a way that the death of F makes G the only child of E , and, therefore, G will replace the dying E . If a candidate leaf survives with the numbosome value of n , it spawns a cell as its child, thereby a new leaf, whose numbosome value is the least odd integer greater than or equal to ( n + 1)/2. We call the child leaf bonus. Finally, a new leader of the numoeba is selected, who has a unique maximum numbosome value among all the constituent cells. The tree structure of the numoeba is changed so that the new leader is its root, like what is shown in Fig. 1 and Fig. 2. Note that the parent-child relationship of some cells may be reversed by this leader change. When a new leader of a unique maximum numbosome value, say m , is selected (it may be the same cell as the previous leader), it spawns a cell as its child with the numbosome whose value is the greatest odd integer less than or equal to ( m + 1)/2. We call the child leader bonus . If there is more than one cell of the same maximum numbosome value, however, the leader does not change for the next period, and there is no leader bonus. The following illustrates the growth and death of a numoeba starting from a single cell seed with the numbosome value 15, which plays both roles of the leader and a leaf at the start. In the figure, a cell is nicknamed with its numbosome value. Note that the order of the children of a parent is irrelevant. The numoeba continues changing its structure, and at clock 104, it looks as follows. Here, two ambitious 2429's could not become the leader. The leader 5 will die without promoting these talented cells at the next clock. This alludes the fragility of a big organization. And, the numoeba dies at clock 105. Your job is to write a program that outputs statistics about the life of numoebae that start from a single cell seed at clock zero. Input A sequence of odd integers, each in a line. Each odd integer k i (3 ≀ k i ≀ 9,999) indicates the initial numbosome value of the starting cell. This sequence is terminated by a zero. Output A sequence of pairs of integers:an integer that represents the numoeba's life time and an integer that represents the maximum number of constituent cells in its life. These two integers should be separated by a space character, and each pair should be followed immediately by a newline. Here, the lifetime means the clock when the numoeba dies. You can use the fact that the life time is less than 500, and that the number of cells does not exceed 500 in any time, for any seed value given in the input. You might guess that the program would consume a lot of memory. It is true in general. But, don't mind. Referees will use a test data set consisting of no more than 10 starting values, and, starting from any of the those values, the total numbers of cells spawned during the lifetime will not exceed 5000. Sample Input 3 5 7 15 655 2711 6395 7195 8465 0 Output for the Sample Input 2 3 1 1 9 11 105 65 398 332 415 332 430 332 428 332 190 421
35,611
問題 B : Evacuation Route 問題文 日本では防灜研究が盛んに行われおおり近幎その重芁性がたすたす増しおいる 避難経路の評䟡も重芁な研究のひず぀である 今回は盎線状の通路の安党評䟡を行う 通路は W 個のナニットに分けられおおり䞀方の端のナニットからもう䞀方の端のナニットたで 0, 1, 2, 
 , W-1 の番号が぀けられおいる 通路内の各ナニットには入口の扉出口の扉防火扉のいずれか1぀が存圚する 入り口の扉出口の扉防火扉はそれぞれ通路内に耇数個存圚しうる この問題では時刻 t=0 で火灜が発生したず想定する それにより通路の倖郚にいお避難しようずしおいる人々が入口の扉を通じお通路ぞ入りより安党な堎所ぞ出るために出口の扉ぞ脱出しようずするものずする 避難䞭のそれぞれの人は単䜍時刻ごずに 1 ぀のナニットを移動するか今のナニットに留たるこずができる すなわち時刻 t にある人がナニット i にいたずするずきその人は時刻 t+1 ではナニット i-1, i, i+1 の3぀の数字のうち 0 以䞊 W-1 以䞋であるものを遞択しその番号のナニットぞ移動するこずができる 防火扉があるナニットはある䞀定時刻以降になるず完党に遮断されおしたい避難䞭の人々はそのナニットに立ち入りできなくなるたたそのナニット内に居た人々もそこから他のナニットに移動できなくなっおしたう この問題における目的はそれぞれの扉の情報が䞎えられるので避難䞭の人々が最適に行動した時に最倧で䜕人が出口の扉ぞたどり着けるか蚈算するこずである 通路の情報が W 個の敎数 a_i で䞎えられる a_i = 0 のずき i 番目のナニットが出口の扉であるこずをあらわす a_i < 0 のずき i 番目のナニットが防火扉により時間 |a_i| 以降出入りできなくなるこずを衚す a_i > 0 のずき時刻 t=0, 1, 2, 
 , a_{i}-1 のそれぞれにおいおちょうど䞀人の人が i 番目のナニットに珟れる時刻 t に珟れた人は時刻 t+1 以降から移動を開始する なお1぀のナニットに耇数の人々が存圚しおもかたわない 出口の扉ぞたどり着ける最倧の人数を求めよ 入力圢匏 入力は以䞋の圢匏で䞎えられる W a_0 a_1 ... a_{W-1} 出力圢匏 最倧人数を1行で出力せよ 制玄 1 ≀ W ≀ 10^5 |a_i| ≀ 10^4 入力倀はすべお敎数である 入出力䟋 入力䟋 1 7 2 0 -2 3 2 -2 0 出力䟋 1 4 0, 3, 5 番目のナニットに入り口の扉があり 1, 6 番目のナニットに出口の扉がある 0 番目のナニットからは 1 番目のナニットぞ人出るこずができる 3 番目のナニットからは 1 番目のナニットぞ人出るこずができる 5 番目のナニットからは 6 番目のナニットぞ人出るこずができる よっお合わせお人が出口ぞずたどり着ける 入力䟋 2 4 1 1 1 1 出力䟋 2 0 出口がないので誰も脱出できない 入力䟋 3 9 10 -10 10 -10 10 -10 10 -10 0 出力䟋 3 24
35,612
æ–­å±€(Geologic Fault) 遠い昔IOI 文明ずいう高床な文明が栄えおいたしかし火山の噎火によりこの高床な文明は぀いに滅んでしたったIOI 文明は盎線状の河川に沿っお繁栄しおおりIOI 文明が滅んだずきその地衚面は平らであったIOI 文明の跡地は座暙平面のx 軞ず芋なすこずができるy 軞は高さ方向を衚すすなわち座暙平面においお盎線 $y = 0$ は地衚を領域 $y > 0$ は地䞊を領域 $y < 0$ は地䞋を衚すたたIOI 文明が滅んだずき$a$ 幎前$(a \geq 0)$ の地局は盎線 $y = -a$ の䜍眮にあった IOI 文明が滅んだ埌IOI 文明の跡地では $Q$ 回の地殻倉動が起きた$i$ 回目$(1 \leq i \leq Q)$ の地殻倉動は䜍眮 $X_i$方向 $D_i$倉動の量 $L_i$ で衚される$D_i$ は 1 たたは 2 である$i$ 回目の地殻倉動は以䞋のように起きる 地局の移動が次のように起きる $D_i = 1$ のずき断局が点$(X_i, 0)$ を通る傟き $1$ の盎線に沿っお造られこの盎線より䞊の領域にある地局が盎線に沿っお高さ $L_i$ だけ移動するすなわちこの盎線より䞊の点 $(x, y)$ は点$(x + L_i, y + L_i)$ に移動する $D_i = 2$ のずき断局が点$(X_i, 0)$ を通る傟き $-1$ の盎線に沿っお造られこの盎線より䞊の領域にある地局が盎線に沿っお高さ $L_i$ だけ移動するすなわちこの盎線より䞊の点$(x, y)$ は点$(x - L_i, y + L_i)$ に移動する そのすぐ埌に領域 $y > 0$ の地局が颚化によっおすべお消える 時は倉わり珟代考叀孊者のJOI 博士はIOI 文明の遺跡を発掘するこずにしたJOI 博士はどの䜍眮の地衚の地局がIOI 文明が滅ぶ䜕幎前の地局であるかを知りたいどのような地殻倉動が起きたかは分かっおいるあなたの仕事はJOI 博士にかわっお$1 \leq i \leq N$ を満たす各敎数 $i$ に぀いお点$(i-1, 0)$ ず点$(i, 0)$の間の地衚の地局がIOI 文明が滅ぶ䜕幎前の地局であるかを求めるこずである 課題 IOI 文明の跡地に起きたの情報が䞎えられたずきすべおの敎数 $i$ $(1 \leq i \leq N)$ に察し点$(i - 1, 0)$ ず点$(i, 0)$ の間の地衚の地局がIOI 文明が滅ぶ䜕幎前の地局であるかを出力せよ 入力 暙準入力から以䞋の入力を読み蟌め 1 行目には 2 個の敎数 $N, Q$ が空癜を区切りずしお曞かれおいるこれは答えを求める地点の数が $N$地殻倉動の回数が $Q$ であるこずを衚す 続く $Q$ 行のうちの $i$ 行目$(1 \leq i \leq Q)$ には3 個の敎数 $X_i, D_i, L_i$ が空癜を区切りずしお曞かれおいるこれは$i$ 回目の地殻倉動の䜍眮が $X_i$方向が $D_i$倉動の量が $L_i$ であるこずを衚す 出力 出力は $N$ 行からなる暙準出力の $i$ 行目$(1 \leq i \leq N)$ には点$(i - 1, 0)$ ず点$(i, 0)$ の間の地衚の地局がIOI文明が滅ぶ䜕幎前の地局であるかを衚す敎数を出力せよ 制限 すべおの入力デヌタは以䞋の条件を満たす $1 \leq N \leq 200 000$ $1 \leq Q \leq 200 000$ $ -1 000 000 000 \leq X_i \leq 1 000 000 000$ $(1 \leq i \leq Q)$ $1 \leq D_i \leq 2$ $(1 \leq i \leq Q)$ $1 \leq L_i \leq 1 000 000 000$ $(1 \leq i \leq Q)$ 入出力䟋 入力䟋1 10 2 12 1 3 2 2 2 出力䟋1 3 3 5 5 5 5 5 5 2 2 この入力䟋は以䞋の図に察応する 入力䟋2 10 6 14 1 1 17 1 1 -6 2 1 3 2 1 4 1 1 0 2 1 出力䟋2 5 5 4 5 5 5 5 5 4 4 入力䟋3 15 10 28 1 7 -24 2 1 1 1 1 8 1 1 6 2 1 20 1 3 12 2 2 -10 1 3 7 2 1 5 1 2 出力䟋3 15 14 14 14 14 12 12 12 12 12 12 12 15 15 12 第15回 日本情報オリンピック本遞 課題 2016 幎 2 月 14 日
35,613
H: Colorful Tree Story Yamiuchi (assassination) is a traditional event that is held annually in JAG summer camp. Every team displays a decorated tree in the dark and all teams' trees are compared from the point of view of their colorfulness. In this competition, it is allowed to cut the other teams’ tree to reduce its colorfulness. Despite a team would get a penalty if it were discovered that the team cuts the tree of another team, many teams do this obstruction. You decided to compete in Yamiuchi and write a program that maximizes the colorfulness of your team’s tree. The program has to calculate maximum scores for all subtrees in case the other teams cut your tree. Problem Statement You are given a rooted tree G with N vertices indexed with 1 through N . The root is vertex 1 . There are K kinds of colors indexed with 1 through K . You can paint vertex i with either color c_i or d_i . Note that c_i = d_i may hold, and if so you have to paint vertex i with c_i ( =d_i ). Let the colorfulness of tree T be the number of different colors in T . Your task is to write a program that calculates maximum colorfulness for all rooted subtrees. Note that coloring for each rooted subtree is done independently, so previous coloring does not affect to other coloring. Input N K u_1 v_1 : u_{N-1} v_{N-1} c_1 d_1 : c_N d_N The first line contains two integers N and K in this order. The following N-1 lines provide information about the edges of G . The i -th line of them contains two integers u_i and v_i , meaning these two vertices are connected with an edge. The following N lines provide information about color constraints. The i -th line of them contains two integers c_i and d_i explained above. Constraints 1 \leq N \leq 10^5 1 \leq K \leq 2\times 10^5 1 \leq u_i , v_i \leq N 1 \leq c_i , d_i \leq K The input graph is a tree. All inputs are integers. Output Output N lines. The i -th line of them contains the maximum colorfulness of the rooted subtree of G whose root is i . Sample Input 1 2 10 1 2 1 9 8 7 Output for Sample Input 1 2 1 Sample Input 2 3 2 1 2 1 3 1 2 1 1 1 2 Output for Sample Input 2 2 1 1 Note that two color options of a vertex can be the same. Sample Input 3 5 100000 4 3 3 5 1 3 2 1 3 2 1 3 2 1 4 2 1 4 Output for Sample Input 3 4 1 3 1 1
35,614
Matrix-like Computation Your task is to develop a tiny little part of spreadsheet software. Write a program which adds up columns and rows of given table as shown in the following figure: Input The input consists of several datasets. Each dataset consists of: n (the size of row and column of the given table) 1st row of the table 2nd row of the table : : n th row of the table The input ends with a line consisting of a single 0. Output For each dataset, print the table with sums of rows and columns. Each item of the table should be aligned to the right with a margin for five digits. Please see the sample output for details. Sample Input 4 52 96 15 20 86 22 35 45 45 78 54 36 16 86 74 55 4 52 96 15 20 86 22 35 45 45 78 54 36 16 86 74 55 0 Output for the Sample Input 52 96 15 20 183 86 22 35 45 188 45 78 54 36 213 16 86 74 55 231 199 282 178 156 815 52 96 15 20 183 86 22 35 45 188 45 78 54 36 213 16 86 74 55 231 199 282 178 156 815
35,615
コンピュヌタシステムの䞍具合 あなたは䞖界最高性胜のコンピュヌタシステム「那由倚なゆた」を蚭蚈しおいる。しかし、このシステムのプロトタむプの実装䞭に、呜什列がある条件を満たすずシステムが停止するずいう䞍具合が芋぀かった。 このシステムは、長さ$N$の呜什列をプログラムずしお䞎えるこずで動䜜する。呜什列の䞭の$m$番目の呜什を数$X_m$で衚したずき、䞍具合が起こる条件は、ある敎数$i,j$ ($2 \leq ij \leq N$)に察しお$X_i + X_{j-1} = X_j + X_{i-1}$ずなる呜什のパタヌンが呜什列に存圚するこずであるず刀明した。 あなたはこの䞍具合がどの皋床の圱響になるのかを調べるため、ある長さで䜜るこずができる呜什列のうち、䜕皮類の呜什列が䞍具合を起こすかを調べるこずにした。 長さ$N$の呜什列のうち、䞍具合が起こる呜什列が䜕通りあるかを求めるプログラムを䜜成せよ。ただし、呜什は$1$以䞊$K$以䞋の敎数で衚せるこずずする。答えは䞎えられた玠数$M$で割った䜙りずする。 入力 入力は以䞋の圢匏で䞎えられる。 $N$ $K$ $M$ 行に、呜什列の長さ$N$ ($3 \leq N \leq 100,000$)、呜什の皮類の数$K$ ($1 \leq K \leq 10$)、玠数$M$ ($100,000,007 \leq M \leq 1,000,000,007$)が䞎えられる。 出力 䞍具合を起こす呜什列の数をMで割った䜙りを出力する。 入出力䟋 入力䟋 3 2 100000007 出力䟋 2 呜什列を$(X_1,X_2,X_3)$のように衚すず、考えられる呜什列は$(1,1,1)$、$(1,1,2)$、$(1,2,1)$、$(1,2,2)$、$(2,1,1)$、$(2,1,2)$、$(2,2,1)$、$(2,2,2)$の通り。このうち、䞍具合が起こる呜什列は$(1,1,1)$、$(2,2,2)$の通り。 入力䟋 9 10 100000037 出力䟋 66631256 䞍具合が起こる呜什列は866631552通りあるが、その数を玠数100000037で割った䜙りが出力ずなる。
35,616
Score : 500 points Problem Statement We have a two-dimensional grid with H \times W squares. There are M targets to destroy in this grid - the position of the i -th target is \left(h_i, w_i \right) . Takahashi will choose one square in this grid, place a bomb there, and ignite it. The bomb will destroy all targets that are in the row or the column where the bomb is placed. It is possible to place the bomb at a square with a target. Takahashi is trying to maximize the number of targets to destroy. Find the maximum number of targets that can be destroyed. Constraints All values in input are integers. 1 \leq H, W \leq 3 \times 10^5 1 \leq M \leq \min\left(H\times W, 3 \times 10^5\right) 1 \leq h_i \leq H 1 \leq w_i \leq W \left(h_i, w_i\right) \neq \left(h_j, w_j\right) \left(i \neq j\right) Input Input is given from Standard Input in the following format: H W M h_1 w_1 \vdots h_M w_M Output Print the answer. Sample Input 1 2 3 3 2 2 1 1 1 3 Sample Output 1 3 We can destroy all the targets by placing the bomb at \left(1, 2\right) . Sample Input 2 3 3 4 3 3 3 1 1 1 1 2 Sample Output 2 3 Sample Input 3 5 5 10 2 5 4 3 2 3 5 5 2 2 5 4 5 3 5 1 3 5 1 4 Sample Output 3 6
35,617
カップゲヌム 3 ぀のカップがふせお眮かれおいたす。カップの眮かれおいる堎所を、順に A,B,C ず呌ぶこずにしたす。最初は A に眮かれおいるカップの䞭にボヌルが隠されおいるずしたす。カップの䜍眮を入れ替えるず、䞭に入っおいるボヌルも䞀緒に移動したす。 入れ替える぀のカップの䜍眮を読み蟌んで、最終的にどの堎所のカップにボヌルが隠されおいるかを出力するプログラムを䜜成しおください。 Input 入れ替える぀のカップの䜍眮が順番に耇数行にわたり䞎えられたす。各行に、入れ替える぀のカップの䜍眮を衚す文字A, B, たたは Cがカンマ区切りで䞎えられたす。 入れ替える操䜜は 50 回を超えたせん。 Output ボヌルが入っおいるカップの堎所A, B, たたは Cを行に出力したす。 Sample Input B,C A,C C,B A,B C,B Output for the Sample Input A
35,618
Score : 1100 points Problem Statement Alice, Bob and Charlie are playing Card Game for Three , as below: At first, each of the three players has a deck consisting of some number of cards. Alice's deck has N cards, Bob's deck has M cards, and Charlie's deck has K cards. Each card has a letter a , b or c written on it. The orders of the cards in the decks cannot be rearranged. The players take turns. Alice goes first. If the current player's deck contains at least one card, discard the top card in the deck. Then, the player whose name begins with the letter on the discarded card, takes the next turn. (For example, if the card says a , Alice takes the next turn.) If the current player's deck is empty, the game ends and the current player wins the game. There are 3^{N+M+K} possible patters of the three player's initial decks. Among these patterns, how many will lead to Alice's victory? Since the answer can be large, print the count modulo 1\,000\,000\,007 (=10^9+7) . Constraints 1 \leq N \leq 3×10^5 1 \leq M \leq 3×10^5 1 \leq K \leq 3×10^5 Partial Scores 500 points will be awarded for passing the test set satisfying the following: 1 \leq N \leq 1000 , 1 \leq M \leq 1000 , 1 \leq K \leq 1000 . Input The input is given from Standard Input in the following format: N M K Output Print the answer modulo 1\,000\,000\,007 (=10^9+7) . Sample Input 1 1 1 1 Sample Output 1 17 If Alice's card is a , then Alice will win regardless of Bob's and Charlie's card. There are 3×3=9 such patterns. If Alice's card is b , Alice will only win when Bob's card is a , or when Bob's card is c and Charlie's card is a . There are 3+1=4 such patterns. If Alice's card is c , Alice will only win when Charlie's card is a , or when Charlie's card is b and Bob's card is a . There are 3+1=4 such patterns. Thus, there are total of 9+4+4=17 patterns that will lead to Alice's victory. Sample Input 2 4 2 2 Sample Output 2 1227 Sample Input 3 1000 1000 1000 Sample Output 3 261790852
35,619
Problem E: Black Force A dam construction project was designed around an area called Black Force. The area is surrounded by mountains and its rugged terrain is said to be very suitable for constructing a dam. However, the project is now almost pushed into cancellation by a strong protest campaign started by the local residents. Your task is to plan out a compromise proposal. In other words, you must find a way to build a dam with sufficient capacity, without destroying the inhabited area of the residents. The map of Black Force is given as H × W cells (0 < H , W ≀ 20). Each cell h i, j is a positive integer representing the height of the place. The dam can be constructed at a connected region surrounded by higher cells, as long as the region contains neither the outermost cells nor the inhabited area of the residents. Here, a region is said to be connected if one can go between any pair of cells in the region by following a sequence of left-, right-, top-, or bottom-adjacent cells without leaving the region. The constructed dam can store water up to the height of the lowest surrounding cell. The capacity of the dam is the maximum volume of water it can store. Water of the depth of 1 poured to a single cell has the volume of 1. The important thing is that, in the case it is difficult to build a sufficient large dam, it is allowed to choose (at most) one cell and do groundwork to increase the height of the cell by 1 unit. Unfortunately, considering the protest campaign, groundwork of larger scale is impossible. Needless to say, you cannot do the groundwork at the inhabited cell. Given the map, the required capacity, and the list of cells inhabited, please determine whether it is possible to construct a dam. Input The input consists of multiple data sets. Each data set is given in the following format: H W C R h 1,1 h 1,2 . . . h 1, W ... h H ,1 h H ,2 . . . h H , W y 1 x 1 ... y R x R H and W is the size of the map. is the required capacity. R (0 < R < H × W ) is the number of cells inhabited. The following H lines represent the map, where each line contains W numbers separated by space. Then, the R lines containing the coordinates of inhabited cells follow. The line “ y x ” means that the cell h y,x is inhabited. The end of input is indicated by a line “0 0 0 0”. This line should not be processed. Output For each data set, print “Yes” if it is possible to construct a dam with capacity equal to or more than C . Otherwise, print “No”. Sample Input 4 4 1 1 2 2 2 2 2 1 1 2 2 1 1 2 2 1 2 2 1 1 4 4 1 1 2 2 2 2 2 1 1 2 2 1 1 2 2 1 2 2 2 2 4 4 1 1 2 2 2 2 2 1 1 2 2 1 1 2 2 1 1 2 1 1 3 6 6 1 1 6 7 1 7 1 5 1 2 8 1 6 1 4 3 1 5 1 1 4 5 6 21 1 1 3 3 3 3 1 3 1 1 1 1 3 3 1 1 3 2 2 3 1 1 1 1 3 1 3 3 3 3 1 3 4 0 0 0 0 Output for the Sample Input Yes No No No Yes
35,620
Score : 700 points Problem Statement You are given sequences A and B consisting of non-negative integers. The lengths of both A and B are N , and the sums of the elements in A and B are equal. The i -th element in A is A_i , and the i -th element in B is B_i . Tozan and Gezan repeats the following sequence of operations: If A and B are equal sequences, terminate the process. Otherwise, first Tozan chooses a positive element in A and decrease it by 1 . Then, Gezan chooses a positive element in B and decrease it by 1 . Then, give one candy to Takahashi, their pet. Tozan wants the number of candies given to Takahashi until the process is terminated to be as large as possible, while Gezan wants it to be as small as possible. Find the number of candies given to Takahashi when both of them perform the operations optimally. Constraints 1 \leq N \leq 2 × 10^5 0 \leq A_i,B_i \leq 10^9(1\leq i\leq N) The sums of the elements in A and B are equal. All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 B_1 : A_N B_N Output Print the number of candies given to Takahashi when both Tozan and Gezan perform the operations optimally. Sample Input 1 2 1 2 3 2 Sample Output 1 2 When both Tozan and Gezan perform the operations optimally, the process will proceed as follows: Tozan decreases A_1 by 1 . Gezan decreases B_1 by 1 . One candy is given to Takahashi. Tozan decreases A_2 by 1 . Gezan decreases B_1 by 1 . One candy is given to Takahashi. As A and B are equal, the process is terminated. Sample Input 2 3 8 3 0 1 4 8 Sample Output 2 9 Sample Input 3 1 1 1 Sample Output 3 0
35,621
Score : 1600 points Problem Statement There is a blackboard on which all integers from -10^{18} through 10^{18} are written, each of them appearing once. Takahashi will repeat the following sequence of operations any number of times he likes, possibly zero: Choose an integer between 1 and N (inclusive) that is written on the blackboard. Let x be the chosen integer, and erase x . If x-2 is not written on the blackboard, write x-2 on the blackboard. If x+K is not written on the blackboard, write x+K on the blackboard. Find the number of possible sets of integers written on the blackboard after some number of operations, modulo M . We consider two sets different when there exists an integer contained in only one of the sets. Constraints 1 \leq K\leq N \leq 150 10^8\leq M\leq 10^9 N , K , and M are integers. Input Input is given from Standard Input in the following format: N K M Output Print the number of possible sets of integers written on the blackboard after some number of operations, modulo M . Sample Input 1 3 1 998244353 Sample Output 1 7 Every set containing all integers less than 1 , all integers greater than 3 , and at least one of the three integers 1 , 2 , and 3 satisfies the condition. There are seven such sets. Sample Input 2 6 3 998244353 Sample Output 2 61 Sample Input 3 9 4 702443618 Sample Output 3 312 Sample Input 4 17 7 208992811 Sample Output 4 128832 Sample Input 5 123 45 678901234 Sample Output 5 256109226
35,622
Problem J Post Office Investigation In this country, all international mails from abroad are first gathered to the central post office, and then delivered to each destination post office relaying some post offices on the way. The delivery routes between post offices are described by a directed graph $G = (V,E)$, where $V$ is the set of post offices and $E$ is the set of possible mail forwarding steps. Due to the inefficient operations, you cannot expect that the mails are delivered along the shortest route. The set of post offices can be divided into a certain number of groups. Here, a group is defined as a set of post offices where mails can be forwarded from any member of the group to any other member, directly or indirectly. The number of post offices in such a group does not exceed 10. The post offices frequently receive complaints from customers that some mails are not delivered yet. Such a problem is usually due to system trouble in a single post office, but identifying which is not easy. Thus, when such complaints are received, the customer support sends staff to check the system of each candidate post office. Here, the investigation cost to check the system of the post office $u$ is given by $c_u$, which depends on the scale of the post office. Since there are many post offices in the country, and such complaints are frequently received, reducing the investigation cost is an important issue. To reduce the cost, the post service administration determined to use the following scheduling rule: When complaints on undelivered mails are received by the post offices $w_1, ..., w_k$ one day, staff is sent on the next day to investigate a single post office $v$ with the lowest investigation cost among candidates. Here, the post office $v$ is a candidate if all mails from the central post office to the post offices $w_1, ... , w_k$ must go through $v$. If no problem is found in the post office $v$, we have to decide the order of investigating other post offices, but the problem is left to some future days. Your job is to write a program that finds the cost of the lowest-cost candidate when the list of complained post offices in a day, described by $w_1, ... , w_k$, is given as a query. Input The input consists of a single test case, formatted as follows. $n$ $m$ $u_1$ $v_1$ ... $u_m$ $v_m$ $c_1$ ... $c_n$ $q$ $k_1$ $w_{11}$ ... $w_{1k_1}$ ... $k_q$ $w_{q1}$ ... $w_{qk_q}$ $n$ is the number of post offices $(2 \leq n \leq 50,000)$, which are numbered from 1 to $n$. Here, post office 1 corresponds to the central post office. $m$ is the number of forwarding pairs of post offices $(1 \leq m \leq 100,000)$. The pair, $u_i$ and $v_i$, means that some of the mails received at post office $u_i$ are forwarded to post office $v_i$ $(i = 1, ..., m)$. $c_j$ is the investigation cost for the post office $j$ $(j = 1, ..., n, 1 \leq c_j \leq 10^9)$. $q$ $(q \geq 1)$ is the number of queries, and each query is specified by a list of post offices which received undelivered mail complaints. $k_i$ $(k_i \geq 1)$ is the length of the list and $w_{i1}, ..., w_{ik_i}$ are the distinct post offices in the list. $\sum_{i=1}^{q} k_i \leq 50,000$. You can assume that there is at least one delivery route from the central post office to all the post offices. Output For each query, you should output a single integer that is the lowest cost of the candidate of troubled post office. Sample Input 1 8 8 1 2 1 3 2 4 2 5 2 8 3 5 3 6 4 7 1000 100 100 10 10 10 1 1 3 2 8 6 2 4 7 2 7 8 Sample Output 1 1000 10 100 Sample Input 2 10 12 1 2 2 3 3 4 4 2 4 5 5 6 6 7 7 5 7 8 8 9 9 10 10 8 10 9 8 7 6 5 4 3 2 1 3 2 3 4 3 6 7 8 3 9 6 3 Sample Output 2 8 5 8
35,623
A - 旧総合研究号通 時は平成50幎春䌑み情報孊研究科に所属する京子さんは京郜にある某研究パヌクからの研究宀の匕越しがやっず終わりホッず䞀息぀いおいた 今回の匕越しは叀くなった校舎の改修工事によるもので新幎床から工事に䌎い校舎の名前が倉曎されるこずになっおいる ホッずしおいたのも぀かの間京子さんは先生から倧孊の資料に茉っおいる校舎名を新しい名前に倉曎するお仕事を頌たれおしたった しかし線集しなければならない資料には今たでの担圓者が仕事を怠っおいたせいかひず぀叀い名前よりもっず叀い名前が䜿われおいるものもあった なんずか京子さんは平成の間に校舎が改名が行われた幎床ずその名前のリストを芋぀けるこずができた平成元幎床の校舎名は "kogakubu10gokan" であった だが匕越し䜜業にずおも疲れたのでその資料が䜜られた幎床にこの校舎がどんな名前だったのかを自分の手で調べるのには我慢ならなかった そこで京子さんはプログラミングが埗意なあなたに手䌝っおずお願いするこずにした 改名の歎史ず資料が䜜られた幎床が䞎えられるのでその幎床の校舎の名前を出力するプログラミングを曞いおあげよう 入力圢匏 入力は以䞋の圢匏で䞎えられる N Q year 1 name 1 year 2 name 2 
 year N name N N は平成2幎床から平成50幎床たでに行われた改名ず改名された幎床の組の個数であり Q は資料が䜜られた幎床を衚す year i は改名が行われた幎床であり name i は改名された名前をあらわす 出力圢匏 平成 Q 幎床の校舎の名前を出力せよ 制玄 1 ≀ N < 50 1 ≀ Q < 50 2 ≀ year 1 < year 2 < 
 < year n ≀ 50 すべおの i に察しお name i は長さ1以䞊30以䞋で含たれる文字は英数字('a'-'z', 'A'-'Z', '0'-'9') である 平成元幎床の校舎の名前は"kogakubu10gokan" である 入出力䟋 入力䟋 1 3 12 5 sogo5gokan 10 sogo10gokan 15 sogo15gokan 出力䟋 1 sogo10gokan 平成12幎床の校舎名を出力すればよい 校舎は平成元幎床の kogakubu10gokan から始たり平成5幎床に sogo5gokan 平成10幎床に sogo10gokan および平成15幎床に sogo15gokan に改名されおいる 入力䟋 2 3 10 5 kogakubu11gokan 10 sogo10gokan 15 KyotoUniversityResearchPark 出力䟋 2 sogo10gokan 平成10幎床の校舎名を出力すればよい 校舎は平成10幎床に sogo10gokan に改名されおいる 入力䟋 3 3 3 5 kogakubu11gokan 10 sogo10gokan 15 KyotoUniversityResearchPark 出力䟋 3 kogakubu10gokan 平成3幎床の校舎名を出力すればよい 平成元幎床から平成4幎床たでの校舎の名前は "kogakubu10gokan" である
35,624
Strange Key Professor Tsukuba invented a mysterious jewelry box that can be opened with a special gold key whose shape is very strange. It is composed of gold bars joined at their ends. Each gold bar has the same length and is placed parallel to one of the three orthogonal axes in a three dimensional space, i.e., x-axis, y-axis and z-axis. The locking mechanism of the jewelry box is truly mysterious, but the shape of the key is known. To identify the key of the jewelry box, he gave a way to describe its shape. The description indicates a list of connected paths that completely defines the shape of the key: the gold bars of the key are arranged along the paths and joined at their ends. Except for the first path, each path must start from an end point of a gold bar on a previously defined path. Each path is represented by a sequence of elements, each of which is one of six symbols (+x, -x, +y, -y, +z and -z) or a positive integer. Each symbol indicates the direction from an end point to the other end point of a gold bar along the path. Since each gold bar is parallel to one of the three orthogonal axes, the 6 symbols are enough to indicate the direction. Note that a description of a path has direction but the gold bars themselves have no direction. An end point of a gold bar can have a label, which is a positive integer. The labeled point may be referred to as the beginnings of other paths. In a key description, the first occurrence of a positive integer defines a label of a point and each subsequent occurrence of the same positive integer indicates the beginning of a new path at the point. An example of a key composed of 13 gold bars is depicted in Figure 1. The following sequence of lines 19 1 +x 1 +y +z 3 +z 3 +y -z +x +y -z -x +z 2 +z 2 +y is a description of the key in Figure 1. Note that newlines have the same role as space characters in the description, so that "19 1 +x 1 +y +z 3 +z 3 +y -z +x +y -z -x +z 2 +z 2 +y" has the same meaning. The meaning of this description is quite simple. The first integer "19" means the number of the following elements in this description. Each element is one of the 6 symbols or a positive integer. The integer "1" at the head of the second line is a label attached to the starting point of the first path. Without loss of generality, it can be assumed that the starting point of the first path is the origin, i.e., (0,0,0), and that the length of each gold bar is 1. The next element "+x" indicates that the first gold bar is parallel to the x-axis, so that the other end point of the gold bar is at (1,0,0). These two elements "1" and "+x" indicates the first path consisting of only one gold bar. The third element of the second line in the description is the positive integer "1", meaning that the point with the label "1", i.e., the origin (0,0,0) is the beginning of a new path. The following elements "+y", "+z", "3", and "+z" indicate the second path consisting of three gold bars. Note that this "3" is its first occurrence so that the point with coordinates (0,1,1) is labeled "3". The head of the third line "3" indicates the beginning of the third path and so on. Consequently, there are four paths by which the shape of the key in Figure 1 is completely defined. Note that there are various descriptions of the same key since there are various sets of paths that cover the shape of the key. For example, the following sequence of lines 19 1 +x 1 +y +z 3 +y -z +x +y -z -x +z 2 +y 3 +z 2 +z is another description of the key in Figure 1, since the gold bars are placed in the same way. Furthermore, the key may be turned 90-degrees around x-axis, y-axis or z-axis several times and may be moved parallelly. Since any combinations of rotations and parallel moves don't change the shape of the key, a description of a rotated and moved key also represent the same shape of the original key. For example, a sequence 17 +y 1 +y -z +x 1 +z +y +x +z +y -x -y 2 -y 2 +z is a description of a key in Figure 2 that represents the same key as in Figure 1. Indeed, they are congruent under a rotation around x-axis and a parallel move. Your job is to write a program to judge whether or not the given two descriptions define the same key. Note that paths may make a cycle. For example, "4 +x +y -x -y" and "6 1 +x 1 +y +x -y" are valid descriptions. However, two or more gold bars must not be placed at the same position. For example, key descriptions "2 +x -x" and "7 1 +x 1 +y +x -y -x" are invalid. Input An input data is a list of pairs of key descriptions followed by a zero that indicates the end of the input. For p pairs of key descriptions, the input is given in the following format. key-description 1-a key-description 1-b key-description 2-a key-description 2-b ... key-description p -a key-description p -b 0 Each key description ( key-description ) has the following format. n e 1 e 2 ... e k ... e n The positive integer n indicates the number of the following elements e 1 , ..., e n . They are separated by one or more space characters and/or newlines. Each element e k is one of the six symbols ( +x , -x , +y , -y , +z and -z ) or a positive integer. You can assume that each label is a positive integer that is less than 51, the number of elements in a single key description is less than 301, and the number of characters in a line is less than 80. You can also assume that the given key descriptions are valid and contain at least one gold bar. Output The number of output lines should be equal to that of pairs of key descriptions given in the input. In each line, you should output one of two words "SAME", when the two key descriptions represent the same key, and "DIFFERENT", when they are different. Note that the letters should be in upper case. Sample Input 19 1 +x 1 +y +z 3 +z 3 +y -z +x +y -z -x +z 2 +z 2 +y 19 1 +x 1 +y +z 3 +y -z +x +y -z -x +z 2 +y 3 +z 2 +z 19 1 +x 1 +y +z 3 +z 3 +y -z +x +y -z -x +z 2 +y 2 +z 18 1 -y 1 +y -z +x 1 +z +y +x +z +y -x -y 2 -y 2 +z 3 +x +y +z 3 +y +z -x 0 Output for the Sample Input SAME SAME DIFFERENT
35,625
察空シヌルド 時は3xxx幎倪陜系倖の惑星に進出した人類は倧量の隕石の飛来による基地の被害で頭を悩たせおいた囜際宇宙防護䌚瀟International Cosmic Protection Companyはこの問題を解決するために新たな察空シヌルドを開発した 防護察象の基地は同じサむズの N 個のナニットが䞀盎線䞊に等間隔で䞊んだ圢をしおおり 1 から N たでの番号が順に付けられおいるICPCはこれらのナニットに合蚈で M 個のシヌルドを蚭眮するこずにした i 番目のシヌルドが胜力 a i を持ちナニット x i に蚭眮されおいるずするこのずきあるナニット u における匷床は以䞋の匏で衚される Σ i=1 M max(a i -(u-x i ) 2 ,0) シヌルドはナニットにのみ蚭眮するこずができ耇数のシヌルドを同じナニットに蚭眮するこずもできるそしおICPCに支払われる報酬は N 個のナニットの匷床の最小倀に比䟋した額ずなる シヌルドの胜力は党お既に決たっおおり䜍眮も最埌の 1 ぀以倖は決定しおいる最埌の 1 ぀のシヌルドの䜍眮を決めるにあたっお報酬がなるべく倧きくなるようにしたいこのように最埌のシヌルドの䜍眮を決めたずきの匷床の最小倀を求めよ Input 入力は最倧で 30 個のデヌタセットからなる各デヌタセットは次の圢匏で衚される N M a 1 x 1 
 a M-1 x M-1 a M N はナニットの個数 M はシヌルドの個数を衚す N ず M は敎数であり 1 ≀ N ≀ 10 6  1 ≀ M ≀ 10 5 を満たす続く M 行には各シヌルドの情報が䞎えられる a i ず x i はそれぞれシヌルドの胜力ず䜍眮を衚す敎数であり 1 ≀ a i ≀ 10 9  1 ≀ x i ≀ N を満たす M 番目のシヌルドの䜍眮はただ決定しおいないため入力で䞎えられないこずに泚意せよ 入力の終わりは 2 ぀のれロからなる行で衚される Output 各デヌタセットに぀いお M 番目のシヌルドの蚭眮䜍眮を適切に決めたずきの匷床の最小倀を 1 行に出力せよ Sample Input 3 3 2 1 2 2 10 10 4 1 1 1 5 1 9 1 5 7 1000000000 1 1000000000 1 1000000000 3 1000000000 3 1000000000 5 1000000000 5 1 10000 11 10934235 560 3155907 1508 10901182 2457 3471816 3590 10087848 4417 16876957 5583 23145027 6540 15162205 7454 1749653 8481 6216466 9554 7198514 701 14 8181 636 4942 273 1706 282 6758 20 7139 148 6055 629 8765 369 5487 95 6111 77 2302 419 9974 699 108 444 1136 495 2443 0 0 Output for the Sample Input 10 0 5999999960 23574372 985
35,626
RSQ and RUQ Write a program which manipulates a sequence $A$ = {$a_0, a_1, ..., a_{n-1}$} with the following operations: $update(s, t, x)$: change $a_s, a_{s+1}, ..., a_t$ to $x$. $getSum(s, t)$: print the sum of $a_s, a_{s+1}, ..., a_t$. Note that the initial values of $a_i ( i = 0, 1, ..., n-1 )$ are 0. Input $n$ $q$ $query_1$ $query_2$ : $query_q$ In the first line, $n$ (the number of elements in $A$) and $q$ (the number of queries) are given. Then, $i$-th query $query_i$ is given in the following format: 0 $s$ $t$ $x$ or 1 $s$ $t$ The first digit represents the type of the query. ' 0 ' denotes $update(s, t, x)$ and ' 1 ' denotes $find(s, t)$. Output For each $getSum$ query, print the sum in a line. Constraints $1 ≀ n ≀ 100000$ $1 ≀ q ≀ 100000$ $0 ≀ s ≀ t < n$ $-1000 ≀ x ≀ 1000$ Sample Input 1 6 7 0 1 3 1 0 2 4 -2 1 0 5 1 0 1 0 3 5 3 1 3 4 1 0 5 Sample Output 1 -5 1 6 8
35,627
Score : 500 points Problem Statement There are N cities numbered 1 to N , connected by M railroads. You are now at City 1 , with 10^{100} gold coins and S silver coins in your pocket. The i -th railroad connects City U_i and City V_i bidirectionally, and a one-way trip costs A_i silver coins and takes B_i minutes. You cannot use gold coins to pay the fare. There is an exchange counter in each city. At the exchange counter in City i , you can get C_i silver coins for 1 gold coin. The transaction takes D_i minutes for each gold coin you give. You can exchange any number of gold coins at each exchange counter. For each t=2, ..., N , find the minimum time needed to travel from City 1 to City t . You can ignore the time spent waiting for trains. Constraints 2 \leq N \leq 50 N-1 \leq M \leq 100 0 \leq S \leq 10^9 1 \leq A_i \leq 50 1 \leq B_i,C_i,D_i \leq 10^9 1 \leq U_i < V_i \leq N There is no pair i, j(i \neq j) such that (U_i,V_i)=(U_j,V_j) . Each city t=2,...,N can be reached from City 1 with some number of railroads. All values in input are integers. Input Input is given from Standard Input in the following format: N M S U_1 V_1 A_1 B_1 : U_M V_M A_M B_M C_1 D_1 : C_N D_N Output For each t=2, ..., N in this order, print a line containing the minimum time needed to travel from City 1 to City t . Sample Input 1 3 2 1 1 2 1 2 1 3 2 4 1 11 1 2 2 5 Sample Output 1 2 14 The railway network in this input is shown in the figure below. In this figure, each city is labeled as follows: The first line: the ID number i of the city ( i for City i ) The second line: C_i / D_i Similarly, each railroad is labeled as follows: The first line: the ID number i of the railroad ( i for the i -th railroad in input) The second line: A_i / B_i You can travel from City 1 to City 2 in 2 minutes, as follows: Use the 1 -st railroad to move from City 1 to City 2 in 2 minutes. You can travel from City 1 to City 3 in 14 minutes, as follows: Use the 1 -st railroad to move from City 1 to City 2 in 2 minutes. At the exchange counter in City 2 , exchange 3 gold coins for 3 silver coins in 6 minutes. Use the 1 -st railroad to move from City 2 to City 1 in 2 minutes. Use the 2 -nd railroad to move from City 1 to City 3 in 4 minutes. Sample Input 2 4 4 1 1 2 1 5 1 3 4 4 2 4 2 2 3 4 1 1 3 1 3 1 5 2 6 4 Sample Output 2 5 5 7 The railway network in this input is shown in the figure below: You can travel from City 1 to City 4 in 7 minutes, as follows: At the exchange counter in City 1 , exchange 2 gold coins for 6 silver coins in 2 minutes. Use the 2 -nd railroad to move from City 1 to City 3 in 4 minutes. Use the 4 -th railroad to move from City 3 to City 4 in 1 minutes. Sample Input 3 6 5 1 1 2 1 1 1 3 2 1 2 4 5 1 3 5 11 1 1 6 50 1 1 10000 1 3000 1 700 1 100 1 1 100 1 Sample Output 3 1 9003 14606 16510 16576 The railway network in this input is shown in the figure below: You can travel from City 1 to City 6 in 16576 minutes, as follows: Use the 1 -st railroad to move from City 1 to City 2 in 1 minute. At the exchange counter in City 2 , exchange 3 gold coins for 3 silver coins in 9000 minutes. Use the 1 -st railroad to move from City 2 to City 1 in 1 minute. Use the 2 -nd railroad to move from City 1 to City 3 in 1 minute. At the exchange counter in City 3 , exchange 8 gold coins for 8 silver coins in 5600 minutes. Use the 2 -nd railroad to move from City 3 to City 1 in 1 minute. Use the 1 -st railroad to move from City 1 to City 2 in 1 minute. Use the 3 -rd railroad to move from City 2 to City 4 in 1 minute. At the exchange counter in City 4 , exchange 19 gold coins for 19 silver coins in 1900 minutes. Use the 3 -rd railroad to move from City 4 to City 2 in 1 minute. Use the 1 -st railroad to move from City 2 to City 1 in 1 minute. Use the 2 -nd railroad to move from City 1 to City 3 in 1 minute. Use the 4 -th railroad to move from City 3 to City 5 in 1 minute. At the exchange counter in City 5 , exchange 63 gold coins for 63 silver coins in 63 minutes. Use the 4 -th railroad to move from City 5 to City 3 in 1 minute. Use the 2 -nd railroad to move from City 3 to City 1 in 1 minute. Use the 5 -th railroad to move from City 1 to City 6 in 1 minute. Sample Input 4 4 6 1000000000 1 2 50 1 1 3 50 5 1 4 50 7 2 3 50 2 2 4 50 4 3 4 50 3 10 2 4 4 5 5 7 7 Sample Output 4 1 3 5 The railway network in this input is shown in the figure below: Sample Input 5 2 1 0 1 2 1 1 1 1000000000 1 1 Sample Output 5 1000000001 The railway network in this input is shown in the figure below: You can travel from City 1 to City 2 in 1000000001 minutes, as follows: At the exchange counter in City 1 , exchange 1 gold coin for 1 silver coin in 1000000000 minutes. Use the 1 -st railroad to move from City 1 to City 2 in 1 minute.
35,628
Score : 1600 points Problem Statement There are N integers written on a blackboard. The i -th integer is A_i . Takahashi and Aoki will arrange these integers in a row, as follows: First, Takahashi will arrange the integers as he wishes. Then, Aoki will repeatedly swap two adjacent integers that are coprime, as many times as he wishes. We will assume that Takahashi acts optimally so that the eventual sequence will be lexicographically as small as possible, and we will also assume that Aoki acts optimally so that the eventual sequence will be lexicographically as large as possible. Find the eventual sequence that will be produced. Constraints 1 ≩ N ≩ 2000 1 ≩ A_i ≩ 10^8 Input The input is given from Standard Input in the following format: N A_1 A_2 
 A_N Output Print the eventual sequence that will be produced, in a line. Sample Input 1 5 1 2 3 4 5 Sample Output 1 5 3 2 4 1 If Takahashi arranges the given integers in the order (1,2,3,4,5) , they will become (5,3,2,4,1) after Aoki optimally manipulates them. Sample Input 2 4 2 3 4 6 Sample Output 2 2 4 6 3
35,629
Entrance Examination The International Competitive Programming College (ICPC) is famous for its research on competitive programming. Applicants to the college are required to take its entrance examination. The successful applicants of the examination are chosen as follows. The score of any successful applicant is higher than that of any unsuccessful applicant. The number of successful applicants n must be between n min and n max , inclusive. We choose n within the specified range that maximizes the gap. Here, the gap means the difference between the lowest score of successful applicants and the highest score of unsuccessful applicants. When two or more candidates for n make exactly the same gap, use the greatest n among them. Let's see the first couple of examples given in Sample Input below. In the first example, n min and n max are two and four, respectively, and there are five applicants whose scores are 100, 90, 82, 70, and 65. For n of two, three and four, the gaps will be 8, 12, and 5, respectively. We must choose three as n , because it maximizes the gap. In the second example, n min and n max are two and four, respectively, and there are five applicants whose scores are 100, 90, 80, 75, and 65. For n of two, three and four, the gap will be 10, 5, and 10, respectively. Both two and four maximize the gap, and we must choose the greatest number, four. You are requested to write a program that computes the number of successful applicants that satisfies the conditions. Input The input consists of multiple datasets. Each dataset is formatted as follows. m n min n max P 1 P 2 ... P m The first line of a dataset contains three integers separated by single spaces. m represents the number of applicants, n min represents the minimum number of successful applicants, and n max represents the maximum number of successful applicants. Each of the following m lines contains an integer P i , which represents the score of each applicant. The scores are listed in descending order. These numbers satisfy 0 < n min < n max < m ≀ 200, 0 ≀ P i ≀ 10000 (1 ≀ i ≀ m ) and P n min > P n max +1 . These ensure that there always exists an n satisfying the conditions. The end of the input is represented by a line containing three zeros separated by single spaces. Output For each dataset, output the number of successful applicants in a line. Sample Input 5 2 4 100 90 82 70 65 5 2 4 100 90 80 75 65 3 1 2 5000 4000 3000 4 2 3 10000 10000 8000 8000 4 2 3 10000 10000 10000 8000 5 2 3 100 80 68 60 45 0 0 0 Output for the Sample Input 3 4 2 2 3 2
35,630
Score : 1900 points Problem Statement Ringo has a tree with N vertices. The i -th of the N-1 edges in this tree connects Vertex A_i and Vertex B_i and has a weight of C_i . Additionally, Vertex i has a weight of X_i . Here, we define f(u,v) as the distance between Vertex u and Vertex v , plus X_u + X_v . We will consider a complete graph G with N vertices. The cost of its edge that connects Vertex u and Vertex v is f(u,v) . Find the minimum spanning tree of G . Constraints 2 \leq N \leq 200,000 1 \leq X_i \leq 10^9 1 \leq A_i,B_i \leq N 1 \leq C_i \leq 10^9 The given graph is a tree. All input values are integers. Input Input is given from Standard Input in the following format: N X_1 X_2 ... X_N A_1 B_1 C_1 A_2 B_2 C_2 : A_{N-1} B_{N-1} C_{N-1} Output Print the cost of the minimum spanning tree of G . Sample Input 1 4 1 3 5 1 1 2 1 2 3 2 3 4 3 Sample Output 1 22 We connect the following pairs: Vertex 1 and 2 , Vertex 1 and 4 , Vertex 3 and 4 . The costs are 5 , 8 and 9 , respectively, for a total of 22 . Sample Input 2 6 44 23 31 29 32 15 1 2 10 1 3 12 1 4 16 4 5 8 4 6 15 Sample Output 2 359 Sample Input 3 2 1000000000 1000000000 2 1 1000000000 Sample Output 3 3000000000
35,631
Score : 400 points Problem Statement Along a road running in an east-west direction, there are A shrines and B temples. The i -th shrine from the west is located at a distance of s_i meters from the west end of the road, and the i -th temple from the west is located at a distance of t_i meters from the west end of the road. Answer the following Q queries: Query i ( 1 \leq i \leq Q ): If we start from a point at a distance of x_i meters from the west end of the road and freely travel along the road, what is the minimum distance that needs to be traveled in order to visit one shrine and one temple? (It is allowed to pass by more shrines and temples than required.) Constraints 1 \leq A, B \leq 10^5 1 \leq Q \leq 10^5 1 \leq s_1 < s_2 < ... < s_A \leq 10^{10} 1 \leq t_1 < t_2 < ... < t_B \leq 10^{10} 1 \leq x_i \leq 10^{10} s_1, ..., s_A, t_1, ..., t_B, x_1, ..., x_Q are all different. All values in input are integers. Input Input is given from Standard Input in the following format: A B Q s_1 : s_A t_1 : t_B x_1 : x_Q Output Print Q lines. The i -th line should contain the answer to the i -th query. Sample Input 1 2 3 4 100 600 400 900 1000 150 2000 899 799 Sample Output 1 350 1400 301 399 There are two shrines and three temples. The shrines are located at distances of 100, 600 meters from the west end of the road, and the temples are located at distances of 400, 900, 1000 meters from the west end of the road. Query 1 : If we start from a point at a distance of 150 meters from the west end of the road, the optimal move is first to walk 50 meters west to visit a shrine, then to walk 300 meters east to visit a temple. Query 2 : If we start from a point at a distance of 2000 meters from the west end of the road, the optimal move is first to walk 1000 meters west to visit a temple, then to walk 400 meters west to visit a shrine. We will pass by another temple on the way, but it is fine. Query 3 : If we start from a point at a distance of 899 meters from the west end of the road, the optimal move is first to walk 1 meter east to visit a temple, then to walk 300 meters west to visit a shrine. Query 4 : If we start from a point at a distance of 799 meters from the west end of the road, the optimal move is first to walk 199 meters west to visit a shrine, then to walk 200 meters west to visit a temple. Sample Input 2 1 1 3 1 10000000000 2 9999999999 5000000000 Sample Output 2 10000000000 10000000000 14999999998 The road is quite long, and we may need to travel a distance that does not fit into a 32 -bit integer.
35,632
Score : 100 points Problem Statement An elementary school student Takahashi has come to a variety store. He has two coins, A -yen and B -yen coins (yen is the currency of Japan), and wants to buy a toy that costs C yen. Can he buy it? Note that he lives in Takahashi Kingdom, and may have coins that do not exist in Japan. Constraints All input values are integers. 1 \leq A, B \leq 500 1 \leq C \leq 1000 Input Input is given from Standard Input in the following format: A B C Output If Takahashi can buy the toy, print Yes ; if he cannot, print No . Sample Input 1 50 100 120 Sample Output 1 Yes He has 50 + 100 = 150 yen, so he can buy the 120 -yen toy. Sample Input 2 500 100 1000 Sample Output 2 No He has 500 + 100 = 600 yen, but he cannot buy the 1000 -yen toy. Sample Input 3 19 123 143 Sample Output 3 No There are 19 -yen and 123 -yen coins in Takahashi Kingdom, which are rather hard to use. Sample Input 4 19 123 142 Sample Output 4 Yes
35,633
Score : 1500 points Problem Statement Consider a circle whose perimeter is divided by N points into N arcs of equal length, and each of the arcs is painted red or blue. Such a circle is said to generate a string S from every point when the following condition is satisfied: We will arbitrarily choose one of the N points on the perimeter and place a piece on it. Then, we will perform the following move M times: move the piece clockwise or counter-clockwise to an adjacent point. Here, whatever point we choose initially, it is always possible to move the piece so that the color of the i -th arc the piece goes along is S_i , by properly deciding the directions of the moves. Assume that, if S_i is R , it represents red; if S_i is B , it represents blue. Note that the directions of the moves can be decided separately for each choice of the initial point. You are given a string S of length M consisting of R and B . Out of the 2^N ways to paint each of the arcs red or blue in a circle whose perimeter is divided into N arcs of equal length, find the number of ways resulting in a circle that generates S from every point, modulo 10^9+7 . Note that the rotations of the same coloring are also distinguished. Constraints 2 \leq N \leq 2 \times 10^5 1 \leq M \leq 2 \times 10^5 |S|=M S_i is R or B . Input Input is given from Standard Input in the following format: N M S Output Print the number of ways to paint each of the arcs that satisfy the condition, modulo 10^9+7 . Sample Input 1 4 7 RBRRBRR Sample Output 1 2 The condition is satisfied only if the arcs are alternately painted red and blue, so the answer here is 2 . Sample Input 2 3 3 BBB Sample Output 2 4 Sample Input 3 12 10 RRRRBRRRRB Sample Output 3 78
35,634
Problem K: Trading Ship You are on board a trading ship as a crew. The ship is now going to pass through a strait notorious for many pirates often robbing ships. The Maritime Police has attempted to expel those pirates many times, but failed their attempts as the pirates are fairly strong. For this reason, every ship passing through the strait needs to defend themselves from the pirates. The navigator has obtained a sea map on which the location of every hideout of pirates is shown. The strait is considered to be a rectangle of W × H on an xy-plane, where the two opposite corners have the coordinates of (0, 0) and ( W , H ). The ship is going to enter and exit the strait at arbitrary points on y = 0 and y = H respectively. To minimize the risk of attack, the navigator has decided to take a route as distant from the hideouts as possible. As a talented programmer, you are asked by the navigator to write a program that finds the best route, that is, the route with the maximum possible distance to the closest hideouts. For simplicity, your program just needs to report the distance in this problem. Input The input begins with a line containing three integers W , H , and N . Here, N indicates the number of hideouts on the strait. Then N lines follow, each of which contains two integers x i and y i , which denote the coordinates the i -th hideout is located on. The input satisfies the following conditions: 1 ≀ W , H ≀ 10 9 , 1 ≀ N ≀ 500, 0 ≀ x i ≀ W , 0 ≀ y i ≀ H . Output There should be a line containing the distance from the best route to the closest hideout(s). The distance should be in a decimal fraction and should not contain an absolute error greater than 10 -3 . Sample Input and Output Input #1 10 10 1 3 5 Output #1 7.000 Input #2 10 10 2 2 2 8 8 Output #2 4.243 Input #3 10 10 3 0 1 4 4 8 1 Output #3 2.500
35,635
E: 数列ゲヌム 問題文 長さ $N$ の正敎数列 $a_1, a_2, \ldots, a_N$ がありたす。 この数列を甚いた、$2$ 人のプレむダヌが先手ず埌手に分かれお行う以䞋のゲヌムを考えたす。 先手ず埌手は亀互に、以䞋の操䜜のどちらかを遞んで行う。 数列の正の項を $1$ ぀遞び、その倀を $1$ 枛らす。 数列の党おの項が正のずき、党おの項の倀を $1$ ず぀枛らす。 先に操䜜を行えなくなったほうが負けです。 $2$ 人のプレむダヌが最適に行動したずき、先手ず埌手どちらが勝぀かを求めおください。 制玄 $1 \leq N \leq 2 \times 10^5$ $1 \leq a_i \leq 10^9$ 入力は党お敎数である 入力 入力は以䞋の圢匏で暙準入力から䞎えられる。 $N$ $a_1$ $a_2$ $...$ $a_N$ 出力 先手が勝぀ずきは First を、埌手が勝぀ずきは Second を出力せよ。 入力䟋 1 2 1 2 出力䟋 1 First 先手が最初に第 $1$ 項の倀を $1$ 枛らすず、次に埌手は第 $2$ 項の倀を $1$ 枛らすしかありたせん。 そのあずで先手が第 $2$ 項の倀を $1$ 枛らすず、数列の党おの項の倀は $0$ になり、 埌手は操䜜を行うこずができなくなりたす。 入力䟋 2 5 3 1 4 1 5 出力䟋 2 Second 入力䟋 3 8 2 4 8 16 32 64 128 256 出力䟋 3 Second 入力䟋 4 3 999999999 1000000000 1000000000 出力䟋 4 First
35,636
Score : 200 points Problem Statement Two children are playing tag on a number line. (In the game of tag, the child called "it" tries to catch the other child.) The child who is "it" is now at coordinate A , and he can travel the distance of V per second. The other child is now at coordinate B , and she can travel the distance of W per second. He can catch her when his coordinate is the same as hers. Determine whether he can catch her within T seconds (including exactly T seconds later). We assume that both children move optimally. Constraints -10^9 \leq A,B \leq 10^9 1 \leq V,W \leq 10^9 1 \leq T \leq 10^9 A \neq B All values in input are integers. Input Input is given from Standard Input in the following format: A V B W T Output If "it" can catch the other child, print YES ; otherwise, print NO . Sample Input 1 1 2 3 1 3 Sample Output 1 YES Sample Input 2 1 2 3 2 3 Sample Output 2 NO Sample Input 3 1 2 3 3 3 Sample Output 3 NO
35,637
Score : 1300 points Problem Statement We have a pyramid with N steps, built with blocks. The steps are numbered 1 through N from top to bottom. For each 1≀i≀N , step i consists of 2i-1 blocks aligned horizontally. The pyramid is built so that the blocks at the centers of the steps are aligned vertically. A pyramid with N=4 steps Snuke wrote a permutation of ( 1 , 2 , ... , 2N-1 ) into the blocks of step N . Then, he wrote integers into all remaining blocks, under the following rule: The integer written into a block b must be equal to the median of the three integers written into the three blocks directly under b , or to the lower left or lower right of b . Writing integers into the blocks Afterwards, he erased all integers written into the blocks. Now, he only remembers that the permutation written into the blocks of step N was ( a_1 , a_2 , ... , a_{2N-1} ). Find the integer written into the block of step 1 . Constraints 2≀N≀10^5 ( a_1 , a_2 , ... , a_{2N-1} ) is a permutation of ( 1 , 2 , ... , 2N-1 ). Input The input is given from Standard Input in the following format: N a_1 a_2 ... a_{2N-1} Output Print the integer written into the block of step 1 . Sample Input 1 4 1 6 3 7 4 5 2 Sample Output 1 4 This case corresponds to the figure in the problem statement. Sample Input 2 2 1 2 3 Sample Output 2 2
35,638
C AA グラフ (AA Graph) Problem Given a graph as an ASCII Art (AA), please print the length of shortest paths from the vertex s to the vertex t . The AA of the graph satisfies the following constraints. A vertex is represented by an uppercase alphabet and symbols o in 8 neighbors as follows. ooo oAo ooo Horizontal edges and vertical edges are represented by symbols - and | , respectively. Lengths of all edges are 1, that is, it do not depends on the number of continuous symbols - or | . All edges do not cross each other, and all vertices do not overlap and touch each other. For each vertex, outgoing edges are at most 1 for each directions top, bottom, left, and right. Each edge is connected to a symbol o that is adjacent to an uppercase alphabet in 4 neighbors as follows. ..|.. .ooo. -oAo- .ooo. ..|.. Therefore, for example, following inputs are not given. .......... .ooo..ooo. .oAo..oBo. .ooo--ooo. .......... (Edges do not satisfies the constraint about their position.) oooooo oAooBo oooooo (Two vertices are adjacent each other.) Input Format H W s t a_1 $\vdots$ a_H In line 1, two integers H and W , and two characters s and t are given. H and W is the width and height of the AA, respectively. s and t is the start and end vertices, respectively. They are given in separating by en spaces. In line 1 + i where 1 \leq i \leq H , the string representing line i of the AA is given. Constraints 3 \leq H, W \leq 50 s and t are selected by uppercase alphabets from A to Z , and s \neq t . a_i ( 1 \leq i \leq H ) consists of uppercase alphabets and symbols o , - , | , and . . Each uppercase alphabet occurs at most once in the AA. It is guaranteed that there are two vertices representing s and t . The AA represents a connected graph. Output Format Print the length of the shortest paths from s to t in one line. Example 1 14 16 A L ooo.....ooo..... oAo-----oHo..... ooo.....ooo..ooo .|.......|...oLo ooo..ooo.|...ooo oKo--oYo.|....|. ooo..ooo.|....|. .|....|.ooo...|. .|....|.oGo...|. .|....|.ooo...|. .|....|.......|. ooo..ooo.....ooo oFo--oXo-----oEo ooo..ooo.....ooo Output 1 5 Exapmple 2 21 17 F L ................. .....ooo.....ooo. .....oAo-----oBo. .....ooo.....ooo. ......|.......|.. .ooo..|..ooo..|.. .oCo..|..oDo.ooo. .ooo.ooo.ooo.oEo. ..|..oFo..|..ooo. ..|..ooo..|...|.. ..|...|...|...|.. ..|...|...|...|.. ..|...|...|...|.. .ooo.ooo.ooo..|.. .oGo-oHo-oIo..|.. .ooo.ooo.ooo..|.. ..|...........|.. .ooo...ooo...ooo. .oJo---oKo---oLo. .ooo...ooo...ooo. ................. Output 2 4
35,639
Transporter In the year 30XX, an expedition team reached a planet and found a warp machine suggesting the existence of a mysterious supercivilization. When you go through one of its entrance gates, you can instantaneously move to the exit irrespective of how far away it is. You can move even to the end of the universe at will with this technology! The scientist team started examining the machine and successfully identified all the planets on which the entrances to the machine were located. Each of these N planets (identified by an index from $1$ to $N$) has an entrance to, and an exit from the warp machine. Each of the entrances and exits has a letter inscribed on it. The mechanism of spatial mobility through the warp machine is as follows: If you go into an entrance gate labeled with c, then you can exit from any gate with label c. If you go into an entrance located on the $i$-th planet, then you can exit from any gate located on the $j$-th planet where $i < j$. Once you have reached an exit of the warp machine on a planet, you can continue your journey by entering into the warp machine on the same planet. In this way, you can reach a faraway planet. Our human race has decided to dispatch an expedition to the star $N$, starting from Star $1$ and using the warp machine until it reaches Star $N$. To evaluate the possibility of successfully reaching the destination. it is highly desirable for us to know how many different routes are available for the expedition team to track. Given information regarding the stars, make a program to enumerate the passages from Star $1$ to Star $N$. Input The input is given in the following format. $N$ $s$ $t$ The first line provides the number of the stars on which the warp machine is located $N$ ($2 \leq N \leq 100,000$). The second line provides a string $s$ of length $N$, each component of which represents the letter inscribed on the entrance of the machine on the star. By the same token, the third line provides a string $t$ of length $N$ consisting of the letters inscribed on the exit of the machine. Two strings $s$ and $t$ consist all of lower-case alphabetical letters, and the $i$-th letter of these strings corresponds respectively to the entrance and exit of Star $i$ machine. Output Divide the number of possible routes from Star $1$ to Star $N$ obtained above by 1,000,000,007, and output the remainder. Sample Input 1 6 abbaba baabab Sample Output 1 5 Sample Input 2 25 neihsokcpuziafoytisrevinu universityofaizupckoshien Sample Output 2 4
35,640
Problem G: Double or Increment Problem ある日、mo3tthi君ずtubuann君は、魔法のポケットずビスケットを䜿っおゲヌムをするこずにしたした。 今ここに $K$ 個のポケットがあり、$1,2, \ldots ,K$ の番号が぀いおいたす。 $i$ 番目のポケットの容量は $M_i$ で、最初 $N_i$ 枚のビスケットが入っおいたす。 mo3tthi君ずtubuann君は、mo3tthi君から始めお、以䞋の䞀連の操䜜を亀互に行いたす。 ポケットを䞀぀遞ぶ。 以䞋のいずれか䞀方の操䜜を䞀床だけ行う。ただし、操䜜の結果遞んだポケットに入っおいるビスケットの枚数がポケットの容量を超える堎合、操䜜を行うこずはできない。 遞んだポケットを撫でる。魔法の力によっお遞んだポケットに入っおいるビスケットの枚数が $1$ 増える。 遞んだポケットを叩く。魔法の力によっお遞んだポケットに入っおいるビスケットの枚数が $2$ 倍になる。 操䜜を行えなくなった時点でゲヌムは終了し、操䜜を行えなくなった人が負け、そうでない人が勝ちになりたす。 mo3tthi君の友人であるあなたは、mo3tthi君から事前にこのゲヌムに勝おるかどうかを刀定できないか盞談されたした。 mo3tthi君のために、mo3tthi君がこのゲヌムに必ず勝぀こずができるかどうかを刀定するプログラムを䜜っおください。 Input 入力は以䞋の圢匏で䞎えられる。 $K$ $N_1$ $M_1$ $\vdots$ $N_K$ $M_K$ Constraints 入力は以䞋の条件を満たす。 $1 \leq K \leq 10^5$ $1 \leq N_i \leq M_i \leq 10^{18}$ 入力は党お敎数である Output mo3tthi君が最適に行動したずき、必ず勝぀こずができるなら"mo3tthi"を、そうでないなら"tubuann"を䞀行に出力する。 Sample Input 1 1 2 4 Sample Output 1 mo3tthi mo3tthi君が䞀番目のポケットを叩くず、䞀番目のポケットに入っおいるビスケットの枚数が $4$ になり、tubuann君は操䜜を行うこずができない。 Sample Input 2 2 2 3 3 8 Sample Output 2 tubuann Sample Input 3 10 2 8 5 9 7 20 8 41 23 48 90 112 4 5 7 7 2344 8923 1 29 Sample Output 3 mo3tthi
35,641
スパむダヌ人 正矩のヒヌロヌ「スパむダヌ人」は、腕からロヌプを出しおビルからビルぞ飛び移るこずができたす。しかし、ロヌプが短いので自分からの距離が 50 以䞋のビルにしか移動できたせん。それより遠くのビルに移動するには、䞀旊別のビルに飛び移らなくおはなりたせん。 ビルの数 n 、 n 個のビルの情報、スパむダヌ人の移動開始䜍眮及び目的地を入力ずし、その移動の最短経路を出力するプログラムを䜜成しおください。どのようにビルを経由しおも目暙のビルに移動できない堎合は NA ず出力しおください。各ビルは点ずしお扱い、最短距離で移動するビルの経由方法が぀以䞊存圚するこずはないものずしたす。 Input 耇数のデヌタセットの䞊びが入力ずしお䞎えられたす。入力の終わりはれロひず぀の行で瀺されたす。 各デヌタセットは以䞋の圢匏で䞎えられたす。 n b 1 x 1 y 1 b 2 x 2 y 2 : b n x n y n m s 1 g 1 s 2 g 2 : s m g m 行目にビルの数 n (1 ≀ n ≀ 100)、続く n 行に i 番目のビルのビル番号 b i (1 ≀ b i ≀ n )、そのビルのx座暙ずy座暙を衚す敎数 x i , y i (-1000 ≀ x i , y i ≀ 1000) が空癜区切りで䞎えられたす。 続く行に移動情報の個数 m (1 ≀ m ≀ 100)、続く m 行に i 番目の移動情報が䞎えられたす。各移動情報ずしお、移動を開始するビルの番号 s i ず目的地ビルの番号 g i が空癜区切りで䞎えられたす。 デヌタセットの数は 10 を超えたせん。 Output 入力デヌタセットごずに次の圢匏で出力したす。 i 行目に i 番目の移動情報に察する経路たたは NA を行に出力したす。各経路は以䞋の圢匏で出力したす。 s i br i1 br i2 ... g i br ij は i 番目の移動情報における、 j 番目に経由するビルの番号を衚したす。 Sample Input 4 1 0 0 2 30 0 3 60 40 4 0 60 2 1 3 1 4 22 1 0 0 2 150 40 3 30 20 4 180 150 5 40 80 6 130 130 7 72 28 8 172 118 9 50 50 10 160 82 11 90 105 12 144 131 13 130 64 14 80 140 15 38 117 16 190 90 17 60 100 18 100 70 19 130 100 20 71 69 21 200 110 22 120 150 1 1 22 0 Output for the Sample Input 1 2 3 NA 1 3 9 20 11 6 22
35,642
Score : 500 points Problem Statement You have a string S of length N . Initially, all characters in S are 1 s. You will perform queries Q times. In the i -th query, you are given two integers L_i, R_i and a character D_i (which is a digit). Then, you must replace all characters from the L_i -th to the R_i -th (inclusive) with D_i . After each query, read the string S as a decimal integer, and print its value modulo 998,244,353 . Constraints 1 \leq N, Q \leq 200,000 1 \leq L_i \leq R_i \leq N 1 \leq D_i \leq 9 All values in input are integers. Input Input is given from Standard Input in the following format: N Q L_1 R_1 D_1 : L_Q R_Q D_Q Output Print Q lines. In the i -th line print the value of S after the i -th query, modulo 998,244,353 . Sample Input 1 8 5 3 6 2 1 4 7 3 8 3 2 2 2 4 5 1 Sample Output 1 11222211 77772211 77333333 72333333 72311333 Sample Input 2 200000 1 123 456 7 Sample Output 2 641437905 Don't forget to take the modulo.
35,643
問題1 䞉角圢の圢は蟺の長さで決たる 順番に぀の正敎数が䞎えられたずき 蟺の長さがそれらの倀ず䞀臎する䞉角圢が存圚するかどうかを調べ 存圚するなら鋭角䞉角圢盎角䞉角圢鈍角䞉角圢のいずれかを刀定し 次の入力ぞ進む 䞉角圢が存圚しないずき それたでに入力された䞉角圢盎角䞉角圢鋭角䞉角圢鈍角䞉角圢の 個数を空癜で区切っお出力し それ以降の入力は無芖しお終了する 入力の䞭には必ず䞉角圢が存圚しないようなものがある ず仮定しおよい. 入力の行数は刀らないが各行には぀の正敎数が空癜で区切っお曞かれおいる ただし各敎数は100 以䞋ずする. 出力ファむルにおいおは 出力の最埌の行にも改行コヌドを入れるこず 入出力䟋 入力䟋 3 4 5 2 1 2 6 3 4 1 1 1 1 2 3 4 1 2 1 入力䟋 3 4 5 2 1 2 6 3 4 1 2 3 1 1 1 出力䟋 3 1 1 1 問題文ず自動審刀に䜿われるデヌタは、 情報オリンピック日本委員䌚 が䜜成し公開しおいる問題文ず採点甚テストデヌタです。
35,644
Score: 400 points Problem Statement In Takahashi Kingdom, there is a east-west railroad and N cities along it, numbered 1 , 2 , 3 , ..., N from west to east. A company called AtCoder Express possesses M trains, and the train i runs from City L_i to City R_i (it is possible that L_i = R_i ). Takahashi the king is interested in the following Q matters: The number of the trains that runs strictly within the section from City p_i to City q_i , that is, the number of trains j such that p_i \leq L_j and R_j \leq q_i . Although he is genius, this is too much data to process by himself. Find the answer for each of these Q queries to help him. Constraints N is an integer between 1 and 500 (inclusive). M is an integer between 1 and 200 \ 000 (inclusive). Q is an integer between 1 and 100 \ 000 (inclusive). 1 \leq L_i \leq R_i \leq N (1 \leq i \leq M) 1 \leq p_i \leq q_i \leq N (1 \leq i \leq Q) Input Input is given from Standard Input in the following format: N M Q L_1 R_1 L_2 R_2 : L_M R_M p_1 q_1 p_2 q_2 : p_Q q_Q Output Print Q lines. The i -th line should contain the number of the trains that runs strictly within the section from City p_i to City q_i . Sample Input 1 2 3 1 1 1 1 2 2 2 1 2 Sample Output 1 3 As all the trains runs within the section from City 1 to City 2 , the answer to the only query is 3 . Sample Input 2 10 3 2 1 5 2 8 7 10 1 7 3 10 Sample Output 2 1 1 The first query is on the section from City 1 to 7 . There is only one train that runs strictly within that section: Train 1 . The second query is on the section from City 3 to 10 . There is only one train that runs strictly within that section: Train 3 . Sample Input 3 10 10 10 1 6 2 9 4 5 4 7 4 7 5 8 6 6 6 7 7 9 10 10 1 8 1 9 1 10 2 8 2 9 2 10 3 8 3 9 3 10 1 10 Sample Output 3 7 9 10 6 8 9 6 7 8 10
35,645
Problem C: Minimal Backgammon Here is a very simple variation of the game backgammon, named “Minimal Backgammon”. The game is played by only one player, using only one of the dice and only one checker (the token used by the player). The game board is a line of ( N + 1) squares labeled as 0 (the start) to N (the goal). At the beginning, the checker is placed on the start (square 0). The aim of the game is to bring the checker to the goal (square N ). The checker proceeds as many squares as the roll of the dice. The dice generates six integers from 1 to 6 with equal probability. The checker should not go beyond the goal. If the roll of the dice would bring the checker beyond the goal, the checker retreats from the goal as many squares as the excess. For example, if the checker is placed at the square ( N - 3), the roll "5" brings the checker to the square ( N - 2), because the excess beyond the goal is 2. At the next turn, the checker proceeds toward the goal as usual. Each square, except the start and the goal, may be given one of the following two special instructions. Lose one turn (labeled " L " in Figure 2) If the checker stops here, you cannot move the checker in the next turn. Go back to the start (labeled " B " in Figure 2) If the checker stops here, the checker is brought back to the start. Figure 2: An example game Given a game board configuration (the size N , and the placement of the special instructions), you are requested to compute the probability with which the game succeeds within a given number of turns. Input The input consists of multiple datasets, each containing integers in the following format. N T L B Lose 1 ... Lose L Back 1 ... Back B N is the index of the goal, which satisfies 5 ≀ N ≀ 100. T is the number of turns. You are requested to compute the probability of success within T turns. T satisfies 1 ≀ T ≀ 100. L is the number of squares marked “Lose one turn”, which satisfies 0 ≀ L ≀ N - 1. B is the number of squares marked “Go back to the start”, which satisfies 0 ≀ B ≀ N - 1. They are separated by a space. Lose i 's are the indexes of the squares marked “Lose one turn”, which satisfy 1 ≀ Lose i ≀ N - 1. All Lose i 's are distinct, and sorted in ascending order. Back i 's are the indexes of the squares marked “Go back to the start”, which satisfy 1 ≀ Back i ≀ N - 1. All Back i 's are distinct, and sorted in ascending order. No numbers occur both in Lose i 's and Back i 's. The end of the input is indicated by a line containing four zeros separated by a space. Output For each dataset, you should answer the probability with which the game succeeds within the given number of turns. The output should not contain an error greater than 0.00001. Sample Input 6 1 0 0 7 1 0 0 7 2 0 0 6 6 1 1 2 5 7 10 0 6 1 2 3 4 5 6 0 0 0 0 Output for the Sample Input 0.166667 0.000000 0.166667 0.619642 0.000000
35,646
w 本の瞊棒からなり高さ(暪棒を远加するこずのできる段数) が h のあみだくじがある w は偶数であるこのあみだくじの暪棒を远加する堎所の候補のうち䞊から a 番目巊から b 番目を (a, b) ずいう( (a, b) に暪棒を远加した堎合䞊から a 段目で巊から b 番目ず b+1 番目の瞊棒が結ばれる) このような堎所は合蚈 h(w −1) 箇所(1 ≀ a ≀ h , 1 ≀ b ≀ w − 1) 存圚する すぬけ君は a ≡ b (mod 2) をみたす堎所 (a, b) に党お暪棒を远加した次にすぬけ君は (a 1 , b 1 ), . . . , (a n , b n ) の堎所の暪棒を消した䞊端で巊から i 番目を遞んだずき䞋端で巊から䜕番目になるかずいうのを党お求めよ Constraints 1 ≀ h, w, n ≀ 200000 w は偶数 1 ≀ a i ≀ h 1 ≀ b i ≀ w − 1 a i ≡ b i (mod 2) (a i , b i ) = (a j , b j ) ずなるような盞異なる i, j は存圚しない Input h w n a 1 b 1 . . . a n b n Output w 行出力せよ i 行目には䞊端で巊から i 番目を遞んだずき䞋端で巊から䜕番目になるかを出力せよ Sample Input 1 4 4 1 3 3 Sample Output 1 2 3 4 1 図1: たずえば䞊端で巊端の瞊棒を遞ぶず(1, 1), (2, 2), (4, 2) を通っお䞋端で巊から二番目の瞊棒にたどり着く Sample Input 2 10 6 10 10 4 4 4 5 1 4 2 7 3 1 3 2 4 8 2 7 5 7 1 Sample Output 2 1 4 3 2 5 6
35,647
Score : 200 points Problem Statement There is a kangaroo at coordinate 0 on an infinite number line that runs from left to right, at time 0 . During the period between time i-1 and time i , the kangaroo can either stay at his position, or perform a jump of length exactly i to the left or to the right. That is, if his coordinate at time i-1 is x , he can be at coordinate x-i , x or x+i at time i . The kangaroo's nest is at coordinate X , and he wants to travel to coordinate X as fast as possible. Find the earliest possible time to reach coordinate X . Constraints X is an integer. 1≀X≀10^9 Input The input is given from Standard Input in the following format: X Output Print the earliest possible time for the kangaroo to reach coordinate X . Sample Input 1 6 Sample Output 1 3 The kangaroo can reach his nest at time 3 by jumping to the right three times, which is the earliest possible time. Sample Input 2 2 Sample Output 2 2 He can reach his nest at time 2 by staying at his position during the first second, and jumping to the right at the next second. Sample Input 3 11 Sample Output 3 5
35,648
Problem A: Sum of Consecutive Integers あなたは数か月に枡る受隓戊争を勝ち抜き晎れおICPC倧孊に入孊するこずができた入孊手続きの日倧孊のキャンパス内では熱狂的なサヌクルの勧誘掻動が行われおおりあなたは倧量のパンフレットを受け取っお垰っおきた郚屋に戻っおきたあなたは受け取ったパンフレットの䞭から気になる䞀枚を芋぀けたそのパンフレットは倧孊の広報郚から枡されたものだった パンフレットには以䞋のような問題が蚘されおいた 和が N ずなるような連続する2぀以䞊の正の敎数の組み合わせは䜕組存圚するでしょうか䟋えば 9 は 2+3+4 ず 4+5 の 2通りの組み合わせがありたす この問題の答えが気になったあなたはプログラムを曞いおその答えを調べるこずにしたしたがっおあなたの仕事は入力ずしお䞎えられる正の敎数 N に察しお問題の答えを出力するプログラムを曞くこずである Input 入力はデヌタセットの䞊びである各デヌタセットはひず぀の敎数 N からなる䞀行であるここで 1 ≀ N ≀ 1000 である 入力の終りはひず぀のれロからなる䞀行で瀺される Output 出力は入力の各デヌタセットの衚す正の敎数に察する問題の答えを入力デヌタセットの順序通りに䞊べたものであるそれ以倖の文字が出力にあっおはならない Sample Input 9 500 0 Output for the Sample Input 2 3
35,649
Score : 300 points Problem Statement To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation: 1 yen (the currency of Japan) 6 yen, 6^2(=36) yen, 6^3(=216) yen, ... 9 yen, 9^2(=81) yen, 9^3(=729) yen, ... At least how many operations are required to withdraw exactly N yen in total? It is not allowed to re-deposit the money you withdrew. Constraints 1 \leq N \leq 100000 N is an integer. Input Input is given from Standard Input in the following format: N Output If at least x operations are required to withdraw exactly N yen in total, print x . Sample Input 1 127 Sample Output 1 4 By withdrawing 1 yen, 9 yen, 36(=6^2) yen and 81(=9^2) yen, we can withdraw 127 yen in four operations. Sample Input 2 3 Sample Output 2 3 By withdrawing 1 yen three times, we can withdraw 3 yen in three operations. Sample Input 3 44852 Sample Output 3 16
35,650
Problem J: Cubic Colonies In AD 3456, the earth is too small for hundreds of billions of people to live in peace. Interstellar Colonization Project with Cubes (ICPC) is a project that tries to move people on the earth to space colonies to ameliorate the problem. ICPC obtained funding from governments and manufactured space colonies very quickly and at low cost using prefabricated cubic blocks. The largest colony looks like a Rubik's cube. It consists of 3 × 3 × 3 cubic blocks (Figure J.1A). Smaller colonies miss some of the blocks in the largest colony. When we manufacture a colony with multiple cubic blocks, we begin with a single block. Then we iteratively glue a next block to existing blocks in a way that faces of them match exactly. Every pair of touched faces is glued. Figure J.1: Example of the largest colony and a smaller colony However, just before the first launch, we found a design flaw with the colonies. We need to add a cable to connect two points on the surface of each colony, but we cannot change the inside of the prefabricated blocks in a short time. Therefore we decided to attach a cable on the surface of each colony. If a part of the cable is not on the surface, it would be sheared off during the launch, so we have to put the whole cable on the surface. We would like to minimize the lengths of the cables due to budget constraints. The dashed line in Figure J.1B is such an example. Write a program that, given the shape of a colony and a pair of points on its surface, calculates the length of the shortest possible cable for that colony. Input The input contains a series of datasets. Each dataset describes a single colony and the pair of the points for the colony in the following format. x 1 y 1 z 1 x 2 y 2 z 2 b 0,0,0 b 1,0,0 b 2,0,0 b 0,1,0 b 1,1,0 b 2,1,0 b 0,2,0 b 1,2,0 b 2,2,0 b 0,0,1 b 1,0,1 b 2,0,1 b 0,1,1 b 1,1,1 b 2,1,1 b 0,2,1 b 1,2,1 b 2,2,1 b 0,0,2 b 1,0,2 b 2,0,2 b 0,1,2 b 1,1,2 b 2,1,2 b 0,2,2 b 1,2,2 b 2,2,2 ( x 1 , y 1 , z 1 ) and ( x 2 , y 2 , z 2 ) are the two distinct points on the surface of the colony, where x 1 , x 2 , y 1 , y 2 , z 1 , z 2 are integers that satisfy 0 ≀ x 1 , x 2 , y 1 , y 2 , z 1 , z 2 ≀ 3. b i,j,k is ' # ' when there is a cubic block whose two diagonal vertices are ( i , j , k ) and ( i + 1, j + 1, k + 1), and b i,j,k is ' . ' if there is no block. Figure J.1A corresponds to the first dataset in the sample input, whereas Figure J.1B corresponds to the second. A cable can pass through a zero-width gap between two blocks if they are touching only on their vertices or edges. In Figure J.2A, which is the third dataset in the sample input, the shortest cable goes from the point A (0, 0, 2) to the point B (2, 2, 2), passing through (1, 1, 2), which is shared by six blocks. Similarly, in Figure J.2B (the fourth dataset in the sample input), the shortest cable goes through the gap between two blocks not glued directly. When two blocks share only a single vertex, you can put a cable through the vertex (Figure J.2C; the fifth dataset in the sample input). You can assume that there is no colony consisting of all 3 × 3 × 3 cubes but the center cube. Six zeros terminate the input. Figure J.2: Dashed lines are the shortest cables. Some blocks are shown partially transparent for illustration. Output For each dataset, output a line containing the length of the shortest cable that connects the two given points. We accept errors less than 0.0001. You can assume that given two points can be connected by a cable. Sample Input 0 0 0 3 3 3 ### ### ### ### ### ### ### ### ### 3 3 0 0 0 3 #.. ### ### ### ### ### #.# ### ### 0 0 2 2 2 2 ... ... ... .#. #.. ... ##. ##. ... 0 1 2 2 1 1 ... ... ... .#. #.. ... ##. ##. ... 3 2 0 2 3 2 ### ..# ... ..# ... .#. ..# ..# .## 0 0 0 0 0 0 Output for the Sample Input 6.70820393249936941515 6.47870866461907457534 2.82842712474619029095 2.23606797749978980505 2.82842712474619029095
35,651
回文数 Problem Statement 敎数 n に最も近い回文数を求めよ なお非負敎数 x が回文数であるずは x を十進法で衚珟した文字列ずそれを反転させた文字列が等しいこずをいう 䟋えば0,7,33,10301などは回文数であり32,90,1010などは回文数でない Input 入力は以䞋の圢匏に埓う䞎えられる数は党お敎数である n Constraints 1≩n≩10^4 Output n に最も近い回文数を出力せよ そのような数が耇数ある堎合は最も小さいものを出力せよ Sample Input 1 13 Output for the Sample Input 1 11 13に最も近い回文数は11である Sample Input 2 7447 Output for the Sample Input 2 7447 7447は回文数なのでそのたた出力すればよい Sample Input 3 106 Output for the Sample Input 3 101 106に最も近い回文数は111ず101のふた぀あるがそのうち小さい方の101を出力する
35,652
Problem B: Left Hand Rule The left-hand rule, which is also known as the wall follower, is a well-known strategy that solves a two- dimensional maze. The strategy can be stated as follows: once you have entered the maze, walk around with keeping your left hand in contact with the wall until you reach the goal. In fact, it is proven that this strategy solves some kind of mazes. Your task is to write a program that determines whether the given maze is solvable by using the left-hand rule and (if the maze is solvable) the number of steps to reach the exit. Moving to a cell from the entrance or the adjacent (north, south, east or west) cell is counted as a step. In this problem, the maze is represented by a collection of walls placed on the two-dimensional grid. We use an ordinary Cartesian coordinate system; the positive x -axis points right and the positive y -axis points up. Each wall is represented by a line segment which is parallel to the x -axis or the y -axis, such that both ends of each wall are located on integer coordinates. The size of the maze is given by W and H that indicate the width and the height of the maze, respectively. A rectangle whose vertices are on (0, 0), ( W , 0), ( W , H ) and (0, H ) forms the outside boundary of the maze. The outside of the maze is always surrounded by walls except for the entrance of the maze. The entrance is represented by a line segment whose ends are ( x E , y E ) and ( x E ', y E '). The entrance has a unit length and is located somewhere on one edge of the boundary. The exit is a unit square whose bottom left corner is located on ( x X , y X ). A few examples of mazes are illustrated in the figure below. They correspond to the datasets in the sample input. Figure 1: Example Mazes (shaded squares indicate the exits) Input The input consists of multiple datasets. Each dataset is formatted as follows: W H N x 1 y 1 x 1 ' y 1 ' x 2 y 2 x 2 ' y 2 ' ... x N y N x N ' y N ' x E y E x E ' y E ' x X y X W and H (0 < W , H ≀ 100) indicate the size of the maze. N is the number of walls inside the maze. The next N lines give the positions of the walls, where ( x i , y i ) and ( x i ', y i ') denote two ends of each wall (1 ≀ i ≀ N ). The last line gives the positions of the entrance and the exit. You can assume that all the coordinates given in the input are integer coordinates and located inside the boundary of the maze. You can also assume that the wall description is not redundant, i.e. an endpoint of a wall is not shared by any other wall that is parallel to it. The input is terminated by a line with three zeros. Output For each dataset, print a line that contains the number of steps required to reach the exit. If the given maze is unsolvable, print “ Impossible ” instead of the number of steps. Sample Input 3 3 3 1 0 1 2 1 2 2 2 2 2 2 1 0 0 1 0 1 1 3 3 4 1 0 1 2 1 2 2 2 2 2 2 1 2 1 1 1 0 0 1 0 1 1 3 3 0 0 0 1 0 1 1 0 0 0 Output for the Sample Input 9 Impossible Impossible
35,653
Score : 600 points Problem Statement There is a cave consisting of N rooms and M one-directional passages. The rooms are numbered 1 through N . Takahashi is now in Room 1 , and Room N has the exit. The i -th passage connects Room s_i and Room t_i ( s_i < t_i ) and can only be traversed in the direction from Room s_i to Room t_i . It is known that, for each room except Room N , there is at least one passage going from that room. Takahashi will escape from the cave. Each time he reaches a room (assume that he has reached Room 1 at the beginning), he will choose a passage uniformly at random from the ones going from that room and take that passage. Aoki, a friend of Takahashi's, can block one of the passages (or do nothing) before Takahashi leaves Room 1 . However, it is not allowed to block a passage so that Takahashi is potentially unable to reach Room N . Let E be the expected number of passages Takahashi takes before he reaches Room N . Find the value of E when Aoki makes a choice that minimizes E . Constraints 2 \leq N \leq 600 N-1 \leq M \leq \frac{N(N-1)}{2} s_i < t_i If i != j , (s_i, t_i) \neq (s_j, t_j) . (Added 21:23 JST) For every v = 1, 2, ..., N-1 , there exists i such that v = s_i . Input Input is given from Standard Input in the following format: N M s_1 t_1 : s_M t_M Output Print the value of E when Aoki makes a choice that minimizes E . Your output will be judged as correct when the absolute or relative error from the judge's output is at most 10^{-6} . Sample Input 1 4 6 1 4 2 3 1 3 1 2 3 4 2 4 Sample Output 1 1.5000000000 If Aoki blocks the passage from Room 1 to Room 2 , Takahashi will go along the path 1 → 3 → 4 with probability \frac{1}{2} and 1 → 4 with probability \frac{1}{2} . E = 1.5 here, and this is the minimum possible value of E . Sample Input 2 3 2 1 2 2 3 Sample Output 2 2.0000000000 Blocking any one passage makes Takahashi unable to reach Room N , so Aoki cannot block a passage. Sample Input 3 10 33 3 7 5 10 8 9 1 10 4 6 2 5 1 7 6 10 1 4 1 3 8 10 1 5 2 6 6 9 5 6 5 8 3 6 4 8 2 7 2 9 6 7 1 2 5 9 6 8 9 10 3 9 7 8 4 5 2 10 5 7 3 5 4 7 4 9 Sample Output 3 3.0133333333
35,654
Score : 200 points Problem Statement Snuke loves working out. He is now exercising N times. Before he starts exercising, his power is 1 . After he exercises for the i -th time, his power gets multiplied by i . Find Snuke's power after he exercises N times. Since the answer can be extremely large, print the answer modulo 10^{9}+7 . Constraints 1 ≀ N ≀ 10^{5} Input The input is given from Standard Input in the following format: N Output Print the answer modulo 10^{9}+7 . Sample Input 1 3 Sample Output 1 6 After Snuke exercises for the first time, his power gets multiplied by 1 and becomes 1 . After Snuke exercises for the second time, his power gets multiplied by 2 and becomes 2 . After Snuke exercises for the third time, his power gets multiplied by 3 and becomes 6 . Sample Input 2 10 Sample Output 2 3628800 Sample Input 3 100000 Sample Output 3 457992974 Print the answer modulo 10^{9}+7 .
35,655
Circumscribed Circle of A Triangle. Write a program which prints the central coordinate $(p_x, p_y)$ and the radius $r$ of a circumscribed circle of a triangle which is constructed by three points $(x_1, y_1)$, $(x_2, y_2)$ and $(x_3, y_3)$ on the plane surface. Input Input consists of several datasets. In the first line, the number of datasets $n$ is given. Each dataset consists of: $x_1$ $y_1$ $x_2$ $y_2$ $x_3$ $y_3$ in a line. All the input are real numbers. Output For each dataset, print $p_x$, $p_y$ and $r$ separated by a space in a line. Print the solution to three places of decimals. Round off the solution to three decimal places. Constraints $-100 \leq x_1, y_1, x_2, y_2, x_3, y_3 \leq 100$ $ n \leq 20$ Sample Input 1 0.0 0.0 2.0 0.0 2.0 2.0 Output for the Sample Input 1.000 1.000 1.414
35,656
最長の階段 問題 1 から n たでの敎数がそれぞれ 1 ぀ず぀曞かれた n 枚のカヌドず,1 枚の癜玙の カヌドがある.これら n + 1 枚のカヌドの内, k 枚のカヌドが䞎えられる.ただし, 1 ≀ k ≀ n である.癜玙のカヌドには 1 から n たでの敎数を 1 ぀曞くこずができ る.䞎えられたカヌドだけで,できるだけ長い連続した敎数列を䜜りたい. 䞎えられるカヌドが入力されたずきに, 䞎えられたカヌドから䜜るこずができる連 続した敎数列の最倧長を出力するプログラムを䜜成せよ. 䟋1 n = 7, k = 5 ずする.6, 2, 4, 7, 1 のカヌドが䞎えられたずき,このカヌドを䜿っ お䜜れる連続した敎数列のうち最長のものは 1, 2 であり,その長さは 2 である. 䟋2 n = 7, k = 5 ずする.6, 2, 4, 7 ず癜玙のカヌドが䞎えられたずき,このカヌドを 䜿っお䜜れる連続した敎数列のうち最長のものは,癜玙のカヌドに 5 を曞くこずに よっおできる 4, 5, 6, 7 であり,その長さは 4 である. 入力 入力は耇数のデヌタセットからなる各デヌタセットは以䞋の圢匏で䞎えられる入力はれロ぀を含む行で終了する 1 行目には,2 ぀の敎数 n (1 ≀ n ≀ 100000) ず k (1 ≀ k ≀ n ) がこの順で 1 ぀の 空癜を区切りずしお曞かれおいる.続く k 行には敎数が 1 ぀ず぀曞かれおおり,侎 えられる k 枚のカヌドに曞かれおいる敎数を衚しおいる.癜玙のカヌドは 0 で衚さ れる. 採点甚デヌタのうち, 配点の 40% 分は 1 ≀ n ≀ 1000, 1 ≀ k ≀ 500 を, 配点の 20% 分は 1 ≀ n ≀ 60000, 1 ≀ k ≀ 50000 を. 配点の 40% 分は 1 ≀ n ≀ 100000, 1 ≀ k ≀ 100000 を満たす. デヌタセットの数は 5 を超えない 出力 デヌタセットごずに敎数を1行に出力する 入出力䟋 入力䟋 7 5 6 2 4 7 1 7 5 6 2 0 4 7 0 0 出力䟋 2 4 䞊蚘問題文ず自動審刀に䜿われるデヌタは、 情報オリンピック日本委員䌚 が䜜成し公開しおいる問題文ず採点甚テストデヌタです。
35,657
F: 郚分文字列分解 問題 2 ぀の文字列 S ず T および敎数 k が䞎えられる。 T の長さ k 以䞊の連続する郚分文字列を考えたずき、それらの連結で S を構成できるか刀定せよ。 ここで文字列 s = s_1 s_2 ... s_n の連続する郚分文字列 s[l, r] = s_l s_{l+1} ... s_r (1 \leq l \leq r \leq n) ずは、 s の l 文字目から r 文字目たでを切り出しおできる文字列を指し、その長さは r - l + 1 である。 入力圢匏 S T k 制玄 S ず T は小文字アルファベットからなる 1 \leq |S|, |T| \leq 2\times 10^5 1 \leq k \leq |T| 出力圢匏 S を構成できるずき Yes を、そうでないずき No を䞀行に出力せよ。 入力䟋1 abracadabra cadabra 4 出力䟋1 Yes T の長さ 4 以䞊の郚分文字列である abra ず cadabra を連結させるこずで abracadabra が構成でき、これは S ず等しい文字列である。 入力䟋2 abcd zcba 1 出力䟋2 No 入力䟋3 abc zcba 1 出力䟋3 Yes 入力䟋4 abcdefg abcddefg 4 出力䟋4 No
35,658
Score : 100 points Problem Statement There is a right triangle ABC with ∠ABC=90° . Given the lengths of the three sides, |AB|,|BC| and |CA| , find the area of the right triangle ABC . It is guaranteed that the area of the triangle ABC is an integer. Constraints 1 \leq |AB|,|BC|,|CA| \leq 100 All values in input are integers. The area of the triangle ABC is an integer. Input Input is given from Standard Input in the following format: |AB| |BC| |CA| Output Print the area of the triangle ABC . Sample Input 1 3 4 5 Sample Output 1 6 This triangle has an area of 6 . Sample Input 2 5 12 13 Sample Output 2 30 This triangle has an area of 30 . Sample Input 3 45 28 53 Sample Output 3 630 This triangle has an area of 630 .
35,659
Problem H: Ramen Shop Ron is a master of a ramen shop. Recently, he has noticed some customers wait for a long time. This has been caused by lack of seats during lunch time. Customers loses their satisfaction if they waits for a long time, and even some of them will give up waiting and go away. For this reason, he has decided to increase seats in his shop. To determine how many seats are appropriate, he has asked you, an excellent programmer, to write a simulator of customer behavior. Customers come to his shop in groups, each of which is associated with the following four parameters: T i : when the group comes to the shop P i : number of customers W i : how long the group can wait for their seats E i : how long the group takes for eating The i -th group comes to the shop with P i customers together at the time T i . If P i successive seats are available at that time, the group takes their seats immediately. Otherwise, they waits for such seats being available. When the group fails to take their seats within the time W i (inclusive) from their coming and strictly before the closing time, they give up waiting and go away. In addition, if there are other groups waiting, the new group cannot take their seats until the earlier groups are taking seats or going away. The shop has N counters numbered uniquely from 1 to N . The i -th counter has C i seats. The group prefers “seats with a greater distance to the nearest group.” Precisely, the group takes their seats according to the criteria listed below. Here, S L denotes the number of successive empty seats on the left side of the group after their seating, and S R the number on the right side. S L and S R are considered to be infinity if there are no other customers on the left side and on the right side respectively. Prefers seats maximizing min{ S L , S R }. If there are multiple alternatives meeting the first criterion, prefers seats maximizing max{ S L , S R }. If there are still multiple alternatives, prefers the counter of the smallest number. If there are still multiple alternatives, prefers the leftmost seats. When multiple groups are leaving the shop at the same time and some other group is waiting for available seats, seat assignment for the waiting group should be made after all the finished groups leave the shop. Your task is to calculate the average satisfaction over customers. The satisfaction of a customer in the i -th group is given as follows: If the group goes away without eating, -1. Otherwise, ( W i - t i )/ W i where t i is the actual waiting time for the i -th group (the value ranges between 0 to 1 inclusive). Input The input consists of multiple datasets. Each dataset has the following format: N M T C 1 C 2 ... C N T 1 P 1 W 1 E 1 T 2 P 2 W 2 E 2 ... T M P M W M E M N indicates the number of counters, M indicates the number of groups and T indicates the closing time. The shop always opens at the time 0. All input values are integers. You can assume that 1 ≀ N ≀ 100, 1 ≀ M ≀ 10000, 1 ≀ T ≀ 10 9 , 1 ≀ C i ≀ 100, 0 ≀ T 1 < T 2 < ... < T M < T , 1 ≀ P i ≀ max C i , 1 ≀ W i ≀ 10 9 and 1 ≀ E i ≀ 10 9 . The input is terminated by a line with three zeros. This is not part of any datasets. Output For each dataset, output the average satisfaction over all customers in a line. Each output value may be printed with an arbitrary number of fractional digits, but may not contain an absolute error greater than 10 -9 . Sample Input 1 4 100 7 10 1 50 50 15 2 50 50 25 1 50 50 35 3 50 50 1 2 100 5 30 3 20 50 40 4 40 50 1 2 100 5 49 3 20 50 60 4 50 30 1 2 100 5 50 3 20 50 60 4 50 30 2 3 100 4 2 10 4 20 20 30 2 20 20 40 4 20 20 0 0 0 Output for the Sample Input 0.7428571428571429 0.4285714285714285 0.5542857142857143 -0.1428571428571428 0.8000000000000000
35,660
Memory Leak Time Limit: 8 sec / Memory Limit: 64 MB D: メモリヌリヌク あなたの研究宀の同期が, 実装䞭の゜フトりェアのバグに悩たされおいた. 圌の゜フトりェアは, かなり䞋䜍のレむダで動䜜する゜フトりェアであるため, 高機胜のデバッガやテストツヌルなどは䜿甚するこずができない. OS による補助がない環境であるため, 蚀語䞊䜿えるはずの䟋倖凊理など䜿えないし, 暙準ラむブラリですら倧半のものが䜿甚できない. ガベヌゞコレクタなんお倢のたた倢である. そのため圌は, これたでトラむアルアンド゚ラヌによっお自力で怜蚌を行い, 修正を行っおきた. しかし, 今回のバグは盞圓根が深いらしく, 圌はもう䞀ヶ月近くに枡り怜蚌を繰り返しおいた. 締切たでにこのバグが修正できなかったため, 圌は論文提出を芋送っおいる. 倜䞭の研究宀には, 断続的に圌の奇声が聞こえおいる始末である. 芋兌ねたあなたは, 圌の゜フトりェアの怜蚌を手䌝うこずにした. これたでの圌のがんばりによっお, プログラムではいわゆるメモリリヌクが発生しおいる可胜性が高いらしい. どうやらプログラムのどこかで, 確保したメモリを解攟し忘れおいるこずが原因で, 䜿甚できるメモリを䜿い果たしおしたっおいるようである. 圌のプログラムはそのたたでは耇雑すぎるので, 圌はメモリの確保や解攟を行う凊理を簡単に衚珟し, パタヌン化したものを甚意しおくれた. このパタヌンごずにどの皋床メモリリヌクが発生するかを調べお欲しいずのこずである. たず前提ずしお, 圌のプログラムでは, メモリマネヌゞャずいうモゞュヌルによっお, メモリ領域の確保や解攟ずいった機胜が提䟛されおいるらしい. メモリ領域を確保するずは, 正確にはメモリマネヌゞャが管理するヒヌプず呌ばれるメモリ領域から, 必芁な分を借り受け, その領域ぞの「参照」を埗るこずである. なお, ヒヌプには䞊限があり, プログラムからそれ以䞊のメモリは利甚できない. メモリ領域の倧きさには「バむト」が甚いられる. 「参照」ずは, その領域が存圚する堎所を瀺す倀である. 参照は, 倉数に代入するか, 盎接別のメモリ領域の確保や解攟の機胜ぞ枡す匕数ずしお利甚するこずができる. メモリ領域を解攟するずは, 䜿わなくなったメモリ領域をメモリマネヌゞャに返华するこずである. そしお, 圌の甚意したパタヌンは以䞋の BNF で衚珟できるものであった. <line> ::= <expr> | <free> <expr> ::= "(" <expr> ")" | <assign> | "NULL" | <variable> | <malloc> | <clone> <assign> ::= <variable> "=" <expr> <malloc> ::= "malloc(" <number> ")" <free> ::= "free(" <expr> ")" <clone> ::= "clone(" <expr> ")" <variable> ::= "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" <number> ::= <non_zero_digit> | <number> <digit> <non_zero_digit> ::= "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" <digit> ::= "0" | <non_zero_digit> 倉数(variables)は, 参照の入れ物ずお機胜し, 名前ずしおアルファベットの倧文字䞀文字を䜿甚できる. 倉数は, 今回問題ずなるヒヌプずは別の領域に確保されるため, 倉数そのものが䜿甚するメモリ量を気にする必芁はない. なお, 倉数は明瀺的に倀を代入されない限り, 曎新されない. たた, 初期倀は特に決たっおおらず, どのような倀を持っおいるかわからないこずに泚意しなければならない. なお, 倉数は評䟡結果ずしお, その倉数の持぀参照を返す. 代入(assign)は, 参照の倀をコピヌする. 右蟺の匏を評䟡しお埗られる参照を巊蟺の倉数にコピヌする. 初期化されおいない倉数や free された倉数も代入するこずができる. これは, 参照ずは領域の堎所を瀺す「倀」でしかないため, 別の倉数に代入しおも, 実際に意図しない領域を操䜜しおしたうこずはない. なお, 代入そのものは, 倉数に代入された参照を返す. malloc, free, clone は, メモリマネヌゞャの機胜を呌び出おいるこずを瀺しおいる. malloc は, 匕数で枡された1以䞊の敎数サむズ分のメモリ領域を確保し, その領域ぞの参照を返す. free は, 匕数に確保枈みのメモリ領域ぞの参照を䞎えるこずで, その領域を解攟する. なお, 評䟡しおも䜕も返さない. clone は, 匕数に確保枈みのメモリ領域ぞの参照を䞎えるこずで, その領域をコピヌする. 今回の怜蚌では同じサむズ分の領域をもう䞀぀確保するず考えればよい. なお, malloc や clone のメモリ確保のアルゎリズム自䜓は粟錬されおいるため, 割り圓お甚のメモリ領域が断片化するこずはない. たた, free した領域は即座に解攟され, 再利甚可胜になる. ただし, 初期化されおいない領域や既に free された領域に察しお clone や free した堎合, 意図しないメモリ領域に察しおコピヌや解攟が行なわれおしたい, 䜕が起こるかわからない. このような動䜜は, 別のバグの原因ずなっおいる可胜性があるため, 圌に知らせる必芁がある. 䞎えられたメモリの䞊限超えお malloc しようずした堎合には, NULL ずいう特殊な参照が返される. 同様に clone でコピヌされる領域分のメモリが残っおいない堎合にも, NULL が返される. この NULL は特定のメモリ領域を参照しおいないこずを瀺す. 通垞の参照ず同様に NULL は倉数に代入でき, 参照を匕数にずる clone や free に枡すこずができる. ただし, free に NULL を匕数で枡しおいた堎合, 䜕も起こらないこずが保蚌されおいる. たた, clone に NULL を匕数で枡した堎合でも, 䜕も起こらず, NULL が返される. 評䟡するず参照を返す匏(expr)は, "(" ")" を䜿うこずで入れ子にするこずができる. この堎合, "(" ")"の内郚の評䟡結果がその倖偎ぞ返されるこずになる. なお, free は参照を返さないので, 入れ子にするこずができない. あなたの仕事は, パタヌンずしお䞎えられた党おの行を実行した埌に, 参照されおいない確保枈みのメモリ領域のバむト数の合蚈を蚈算するこずである. なお, 䞎えられた党おの行を実行した埌に, 䞀぀以䞊の倉数が参照を持っおいる領域に぀いおは気にしなくおよい. たた, 別のバグの原因を発芋した堎合には, 圌に知らせなければならない. あなたは, パタヌンの分析をするためのプログラムを組むこずした. そのために, 圌の瀺すいく぀かのパタヌンに぀いお考察した. 図1 図1は, ヒヌプが 100 バむト存圚する状態で, 以䞋のようなパタヌンを実行したずきの状態の遷移を瀺しおいる A=malloc(10) B=clone(A) free(A) たず, 1行目では, malloc によっお 10 バむトの領域を確保し, その領域ぞの参照を倉数 A に代入しおいる. 残りの䜿甚可胜なヒヌプは, 90 バむトずなる 次に, 2行目では, 先ほど確保した領域ぞの参照を持぀倉数 A を clone に枡しおいる. これにより, 新たに10バむトの領域が確保され, 残りの䜿甚可胜なヒヌプは, 80 バむトずなる. 新たに確保された 10 バむトの領域ぞの参照は, 倉数 B に代入される. そしお, 3行目では, 1行目で確保された領域ぞの参照を持぀倉数 A を free に枡しおいる. これにより, 10バむトの領域が解攟され, 残りの䜿甚可胜なヒヌプは, 90 バむトずなる. 以䞊で, このパタヌンの実行は終了である. この堎合では, 参照されおいない確保枈みの領域は存圚しないため, 怜蚌の結果は 0 バむトずなる. 2行目で確保された 10 バむトの領域は, 倉数 B が参照を持っおいるため, メモリリヌクずしお扱わない. たた, 倉数 A は3行目で free に枡された埌, 代入によっお曎新されおいない. そのため, この時点でも先ほど解攟された領域ぞの参照を持ったたたである. もしこの埌に, この領域を free あるいは clone するような呜什があった堎合は, 怜蚌結果ずしお Error を出力する必芁がある. 図2 図2は, ヒヌプが100存圚する状態で, 以䞋のようなパタヌンを実行したずきの状態の遷移を瀺しおいる A=clone(clone(malloc(10))) この堎合は, たず malloc(10) が実行され, 10バむトの領域が確保される. この領域ぞの参照はそのたた clone に枡され, さらに 10バむトの領域が確保される. この clone によっお確保された領域も倖偎の clone に枡され, さらに10バむトの領域が確保される. 最埌に確保された領域ぞの参照が倉数 A に代入される. 最初の malloc で確保された領域や内偎の clone で確保された領域は, どの倉数も参照を持っおおらず, 解攟するこずができなくなっおしたう. よっお, このパタヌンの怜蚌結果は 20 バむトずなる. Input 入力は以䞋の圢匏で䞎えられる M line 1 line 2 line 3 ... line i ... 入力の圢匏で甚いられる倉数の意味は次の通りである 1行目の M はヒヌプの䞊限であり, 0 <= M <= 5000 である. line i は前述 BNF で瀺される匏であり, EOF たで続き100行を超えない line i は 300 文字を超えないずする malloc に匕数ずしお䞎えられるメモリ領域の倧きさ size は 1 <= size <= 5000 である. Output 入力で䞎えられた党おの行を実行した埌に, 参照されおいない確保枈みのメモリ領域のバむト数の合蚈を出力せよ. メモリリヌク以倖の゚ラヌが発生しおいた堎合には, "Error" ず出力せよ. Sample Input 1 100 A=malloc(10) B=clone(A) free(A) Sample Output 1 0 Sample Input 2 100 A=clone(clone(malloc(10))) Sample Output 2 20 Sample Input 3 30 clone(clone(clone(clone(malloc(10))))) Sample Output 3 30 Sample Input 4 100 free(A) A=malloc(12) NULL N=clone(NULL) Sample Output 4 Error
35,661
Score : 800 points Problem Statement Snuke Festival 2017 will be held in a tree with N vertices numbered 1,2, ...,N . The i -th edge connects Vertex a_i and b_i , and has joyfulness c_i . The staff is Snuke and N-1 black cats. Snuke will set up the headquarters in some vertex, and from there he will deploy a cat to each of the other N-1 vertices. For each vertex, calculate the niceness when the headquarters are set up in that vertex. The niceness when the headquarters are set up in Vertex i is calculated as follows: Let X=0 . For each integer j between 1 and N (inclusive) except i , do the following: Add c to X , where c is the smallest joyfulness of an edge on the path from Vertex i to Vertex j . The niceness is the final value of X . Constraints 1 \leq N \leq 10^{5} 1 \leq a_i,b_i \leq N 1 \leq c_i \leq 10^{9} The given graph is a tree. All input values are integers. Partial Scores In the test set worth 200 points, N \leq 1000 . In the test set worth 200 points, c_i \leq 2 . Input Input is given from Standard Input in the following format: N a_1 b_1 c_1 : a_{N-1} b_{N-1} c_{N-1} Output Print N lines. The i -th line must contain the niceness when the headquarters are set up in Vertex i . Sample Input 1 3 1 2 10 2 3 20 Sample Output 1 20 30 30 The figure below shows the case when headquarters are set up in each of the vertices 1 , 2 and 3 . The number on top of an edge denotes the joyfulness of the edge, and the number below an vertex denotes the smallest joyfulness of an edge on the path from the headquarters to that vertex. Sample Input 2 15 6 3 2 13 3 1 1 13 2 7 1 2 8 1 1 2 8 2 2 12 2 5 2 2 2 11 2 10 2 2 10 9 1 9 14 2 4 14 1 11 15 2 Sample Output 2 16 20 15 14 20 15 16 20 15 20 20 20 16 15 20 Sample Input 3 19 19 14 48 11 19 23 17 14 30 7 11 15 2 19 15 2 18 21 19 10 43 12 11 25 3 11 4 5 19 50 4 11 19 9 12 29 14 13 3 14 6 12 14 15 14 5 1 6 8 18 13 7 16 14 Sample Output 3 103 237 71 263 370 193 231 207 299 358 295 299 54 368 220 220 319 237 370
35,662
I - 宝探し 問題文 わたしはある宝探しの䟝頌を受けた どうやらこの近くの地䞭 2W × 2H の範囲内にお宝が埋たっおいるらしい わたしの埗意なダりゞングを䜿えば珟圚の䜍眮からお宝がどの方向にねむっおいるのか知るこずができる ただわたしの胜力はその日の䜓調による圱響が倧きく今日はダりゞングをした時に最倧で E 床たで誀差が出おしたいそうだ たたわたしの䜓力にも限界はある1日に200回たでしかダりゞングを行うこずはできない わたしは考えるこずは苊手だ あなたには宝を芋぀けるためにどこでダりゞングをするべきかの指瀺を出しおいただきたい 入出力圢匏 最初の入力は以䞋の圢匏で䞎えられる入力は党お小数12桁たでの実数で䞎えられる W H E この入力は宝の埋たっおいる座暙 (x,y) が -W ≀ x ≀ W -H ≀ y ≀ H を満たしたたダりゞングの結果の誀差が E 床以䞋であるこずを衚す 以降あなたはダりゞングを行う堎所を指定し結果を埗るかもしくは宝の堎所を確定したず䌝えるこずができる 地点 (x,y) でダりゞングを行い結果を埗るには printf("? %.12f %.12f\n", x, y); fflush(stdout); ずする次に scanf("%lf", &degree); ずするずダりゞングの結果が埗られる 結果の角床 degree は (x,y) から宝のある方向を Ξ ずした時 [Ξ-E,Ξ+E] の䞀様分垃によっお生成される 宝のある地点でダりゞングを行った堎合 Ξ=0 ずなる 角床はx軞正方向が0床y軞正方向が+90床ずする もし -180 ≀ degree ≀ 180 を満たしおない堎合この範囲に収たる様に degree は修正される 地点 (x,y) に宝が存圚するず䌝える堎合には printf("! %.12f %.12f\n", x, y); fflush(stdout); ずする このずき実際の宝の䜍眮ずの絶察誀差が L ∞ ノルム で 0.5 以䞋になっおいなければならない なおダりゞングや宝の䜍眮の指定以倖の出力を行った堎合は誀答( Wrong Answer )ず刀定される 制玄 0 ≀ W ≀ 10 4 0 ≀ H ≀ 10 4 0 ≀ E ≀ 120 入力倀は党お小数12桁たでの実数である ダりゞングは最倧で 200 回たで行えるそれを越えるず誀答 ずなる この問題の刀定には3 点分のテストケヌスのグルヌプが蚭定されおいるこのグルヌプに含たれるテストケヌスは䞊蚘の制玄に加えお䞋蚘の制玄も満たす E = 0 入出力䟋 プログラムの出力 プログラムぞの入力 100 100 0 ? -97.30147375 -55.03559390 45.8958081317 ? 44.46472896 -54.50272726 110.928234444 ... ... ! 4.50000018 50.00000005 最初にプログラムは宝のある範囲 W,H ずダりゞングの粟床 E を受け取る その埌プログラムは (-97.3, -55.0) で1回目のダりゞングを行い宝のある方向は 45.8 床の方向だずいう情報を埗おいる 次にプログラムは (44.5, -54.5) で2回目のダりゞングを行い宝のある方向は 110.9 床の方向だずいう情報を埗おいる その埌䜕床かダりゞングを行い最終的に宝のある䜍眮は (4.5, 50.0) であるず出力しおいる Writer : 森槙悟楠本充花田裕䞀朗
35,663
Hierarchical Democracy The presidential election in Republic of Democratia is carried out through multiple stages as follows. There are exactly two presidential candidates. At the first stage, eligible voters go to the polls of his/her electoral district. The winner of the district is the candidate who takes a majority of the votes. Voters cast their ballots only at this first stage. A district of the k -th stage ( k > 1) consists of multiple districts of the ( k − 1)-th stage. In contrast, a district of the ( k − 1)-th stage is a sub-district of one and only one district of the k -th stage. The winner of a district of the k -th stage is the candidate who wins in a majority of its sub-districts of the ( k − 1)-th stage. The final stage has just one nation-wide district. The winner of the final stage is chosen as the president. You can assume the following about the presidential election of this country. Every eligible voter casts a vote. The number of the eligible voters of each electoral district of the first stage is odd. The number of the sub-districts of the ( k − 1)-th stage that constitute a district of the k -th stage ( k > 1) is also odd. This means that each district of every stage has its winner (there is no tie). Your mission is to write a program that finds a way to win the presidential election with the minimum number of votes. Suppose, for instance, that the district of the final stage has three sub-districts of the first stage and that the numbers of the eligible voters of the sub-districts are 123, 4567, and 89, respectively. The minimum number of votes required to be the winner is 107, that is, 62 from the first district and 45 from the third. In this case, even if the other candidate were given all the 4567 votes in the second district, s/he would inevitably be the loser. Although you might consider this election system unfair, you should accept it as a reality. Input The entire input looks like: the number of datasets (=n) 1st dataset 2nd dataset 
 n-th dataset The number of datasets, n , is no more than 100. The number of the eligible voters of each district and the part-whole relations among districts are denoted as follows. An electoral district of the first stage is denoted as [ c ], where c is the number of the eligible voters of the district. A district of the k -th stage ( k > 1) is denoted as [ d 1 d 2 
 d m ], where d 1 , d 2 , 
, d m denote its sub-districts of the ( k − 1)-th stage in this notation. For instance, an electoral district of the first stage that has 123 eligible voters is denoted as [123]. A district of the second stage consisting of three sub-districts of the first stage that have 123, 4567, and 89 eligible voters, respectively, is denoted as [[123][4567][89]]. Each dataset is a line that contains the character string denoting the district of the final stage in the aforementioned notation. You can assume the following. The character string in each dataset does not include any characters except digits ('0', '1', 
, '9') and square brackets ('[', ']'), and its length is between 11 and 10000, inclusive. The number of the eligible voters of each electoral district of the first stage is between 3 and 9999, inclusive. The number of stages is a nation-wide constant. So, for instance, [[[9][9][9]][9][9]] never appears in the input. [[[[9]]]] may not appear either since each district of the second or later stage must have multiple sub-districts of the previous stage. Output For each dataset, print the minimum number of votes required to be the winner of the presidential election in a line. No output line may include any characters except the digits with which the number is written. Sample Input 6 [[123][4567][89]] [[5][3][7][3][9]] [[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]] [[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]] [[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]] [[8231][3721][203][3271][8843]] Output for the Sample Input 107 7 175 95 21 3599
35,664
Score : 200 points Problem Statement Takahashi has many red balls and blue balls. Now, he will place them in a row. Initially, there is no ball placed. Takahashi, who is very patient, will do the following operation 10^{100} times: Place A blue balls at the end of the row of balls already placed. Then, place B red balls at the end of the row. How many blue balls will be there among the first N balls in the row of balls made this way? Constraints 1 \leq N \leq 10^{18} A, B \geq 0 0 < A + B \leq 10^{18} All values in input are integers. Input Input is given from Standard Input in the following format: N A B Output Print the number of blue balls that will be there among the first N balls in the row of balls. Sample Input 1 8 3 4 Sample Output 1 4 Let b denote a blue ball, and r denote a red ball. The first eight balls in the row will be bbbrrrrb , among which there are four blue balls. Sample Input 2 8 0 4 Sample Output 2 0 He placed only red balls from the beginning. Sample Input 3 6 2 4 Sample Output 3 2 Among bbrrrr , there are two blue balls.
35,665
Score : 300 points Problem Statement There are N people, conveniently numbered 1 through N . They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person. According to their reports, the difference above for person i is A_i . Based on these reports, find the number of the possible orders in which they were standing. Since it can be extremely large, print the answer modulo 10^9+7 . Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print 0 . Constraints 1≩N≩10^5 0≩A_i≩N-1 Input The input is given from Standard Input in the following format: N A_1 A_2 ... A_N Output Print the number of the possible orders in which they were standing, modulo 10^9+7 . Sample Input 1 5 2 4 4 0 2 Sample Output 1 4 There are four possible orders, as follows: 2,1,4,5,3 2,5,4,1,3 3,1,4,5,2 3,5,4,1,2 Sample Input 2 7 6 4 0 2 4 0 2 Sample Output 2 0 Any order would be inconsistent with the reports, thus the answer is 0 . Sample Input 3 8 7 5 1 1 7 3 5 3 Sample Output 3 16
35,666
路線バスの時刻衚 バスマニアの健次郎君は、垂内のバスをよく利甚しおいたす。ある日ふず、健次郎君の家の前のバス停から出発するすべおのバスを写真に収めるこずを思い立ちたした。このバス停には飯盛山行きず鶎ケ城行きの぀のバス路線が通りたす。各路線の時刻衚は手に入れたしたが、぀の時刻衚ずしおたずめた方がバス停で写真が撮りやすくなりたす。 健次郎君を助けるために、぀の路線の時刻衚を、時分を基準ずしお出発時刻が早い順に぀の時刻衚ずしおたずめるプログラムを䜜成しおください。 入力 入力は以䞋の圢匏で䞎えられる。 N h 1 m 1 h 2 m 2 ... h N m N M k 1 g 1 k 2 g 2 ... k M g M 行目に、飯盛山行きのバスの数を衚す敎数 N (1 ≀ N ≀ 100) ず、それに続いお飯盛山行きのバス出発時刻が早い順に䞎えられる。出発時刻は敎数 h i (0 ≀ h i ≀ 23) ず m i (0 ≀ m i ≀ 59) からなり、 h i 時 m i 分に i 番目のバスが出発するこずを衚す。行目に、鶎ケ城行きのバスの数を衚す敎数 M (1 ≀ M ≀ 100) ず、それに続いお鶎ケ城行きのバス出発時刻が早い順に䞎えられる。出発時刻は敎数 k i (0 ≀ k i ≀ 23) ず g i (0 ≀ g i ≀ 59) からなり、 k i 時 g i 分に i 番目のバスが出発するこずを衚す。同じ路線には、同じ時刻に出発するバスはないものずする。 出力 ぀の路線の時刻衚を、時分を基準ずしお出発時刻が早い順に぀にたずめた時刻衚を行に出力する。ただし、同じ時刻に耇数のバスが出発するずきは぀だけを出力する。出発時刻の時ず分を : で区切る。分が桁のずきは巊にをひず぀加えお桁で出力する。時刻ず時刻の間は空癜1 ぀で区切る。 入出力䟋 入力䟋 2 9 8 15 59 1 8 27 出力䟋 8:27 9:08 15:59 入力䟋 2 0 0 21 0 3 7 30 21 0 23 7 出力䟋 0:00 7:30 21:00 23:07
35,667
ゲヌムの攻略 あなたは所属するプログラミング郚の郚宀から叀びたボヌドゲヌムを発芋したした。面癜そうなので遊んでみるこずにしたした。 このゲヌムは M 個のむベントから成り、時刻 t i にむベント i を攻略しなければいけたせん。ただし、そのずきにあなたの匷さは s i 以䞊である必芁があり、 s i 以䞊にできない堎合はゲヌムオヌバヌになりたす。ゲヌム開始時時刻のあなたの匷さはですが、アむテムを買うこずで匷さを増加させるこずができたす。ゲヌム開始時のあなたの所持金はですが、単䜍時間あたり増加したす。 ボヌドにはそれぞれから N の番号が付けられた N 個のアむテムが順番に䞊べられおおり、アむテム i の倀段は v i で、それを賌入するずあなたの匷さが h i 増加したす。アむテムは所持金が十分であれば奜きな時刻に奜きな数だけ賌入するこずができたすが、残っおいるアむテムの䞭で番号が小さいものから順に遞ばなければなりたせん。各アむテムは床賌入するず消滅したす。 たた、同じ時刻に耇数のアむテムを連続で買うこずができ、このずき隣り合うアむテムの h i の差分の和をボヌナスずしお埗るこずができたす。䟋えば、ある時刻にアむテム,,を同時に買った堎合、 h 1 + h 2 + h 3 に加えお| h 1 - h 2 | + | h 2 - h 3 | だけあなたの匷さが増加したす。 あなたは、党おのむベントを攻略した埌の所持金を最倧化したいず考えおいたす。 アむテムの情報ずむベントの情報を入力ずし、すべおのむベントを攻略した埌の所持金の最倧倀を出力するプログラムを䜜成せよ。 Input 入力は以䞋の圢匏で䞎えられる。 N M v 1 h 1 v 2 h 2 : v N h N t 1 s 1 t 2 s 2 : t M s M 行目にアむテムの数 N (1 ≀ N ≀ 3000) ずむベントの数 M (1 ≀ M ≀ 1000) が䞎えられる。続く N 行にアむテム i の倀段 v i ず匷さの増加量 h i (1 ≀ v i , h i ≀ 100000) が䞎えられる。続く M 行にむベント i の時刻 t i ず条件 s i (1 ≀ t i , s i ≀ 100000) が䞎えられる。ただし、 i < j のずき、 t i < t j ずする。入力はすべお敎数で䞎えられる。 Output 所持金の最倧倀を行に出力する。ただし、攻略するこずができない堎合には「-1」を出力する。 Sample Input 1 5 4 3 3 2 1 1 5 4 2 2 6 4 1 8 2 10 4 12 17 Sample Output 1 2 Sample Input 2 5 4 3 3 2 1 1 5 4 2 2 6 4 1 8 2 10 4 12 30 Sample Output 2 -1
35,668
Segment Intersections: Manhattan Geometry For given $n$ segments which are parallel to X-axis or Y-axis, find the number of intersections of them. Input In the first line, the number of segments $n$ is given. In the following $n$ lines, the $i$-th segment is given by coordinates of its end points in the following format: $x_1 \; y_1 \; x_2 \; y_2$ The coordinates are given in integers. Output Print the number of intersections in a line. Constraints $1 \leq n \leq 100,000$ $ -1,000,000,000 \leq x_1, y_1, x_2, y_2 \leq 1,000,000,000$ Two parallel segments never overlap or touch. The number of intersections $\leq 1,000,000$ Sample Input 1 6 2 2 2 5 1 3 5 3 4 1 4 4 5 2 7 2 6 1 6 3 6 5 6 7 Sample Output 1 3
35,669
Score : 600 points Problem Statement Given is an integer N . Snuke will choose integers s_1 , s_2 , n_1 , n_2 , u_1 , u_2 , k_1 , k_2 , e_1 , and e_2 so that all of the following six conditions will be satisfied: 0 \leq s_1 < s_2 0 \leq n_1 < n_2 0 \leq u_1 < u_2 0 \leq k_1 < k_2 0 \leq e_1 < e_2 s_1 + s_2 + n_1 + n_2 + u_1 + u_2 + k_1 + k_2 + e_1 + e_2 \leq N For every possible choice (s_1,s_2,n_1,n_2,u_1,u_2,k_1,k_2,e_1,e_2) , compute (s_2 − s_1)(n_2 − n_1)(u_2 − u_1)(k_2 - k_1)(e_2 - e_1) , and find the sum of all computed values, modulo (10^{9} +7) . Solve this problem for each of the T test cases given. Constraints All values in input are integers. 1 \leq T \leq 100 1 \leq N \leq 10^{9} Input Input is given from Standard Input in the following format: T \mathrm{case}_1 \vdots \mathrm{case}_T Each case is given in the following format: N Output Print T lines. The i -th line should contain the answer to the i -th test case. Sample Input 1 4 4 6 10 1000000000 Sample Output 1 0 11 4598 257255556 When N=4 , there is no possible choice (s_1,s_2,n_1,n_2,u_1,u_2,k_1,k_2,e_1,e_2) . Thus, the answer is 0 . When N=6 , there are six possible choices (s_1,s_2,n_1,n_2,u_1,u_2,k_1,k_2,e_1,e_2) as follows: (0,1,0,1,0,1,0,1,0,1) (0,2,0,1,0,1,0,1,0,1) (0,1,0,2,0,1,0,1,0,1) (0,1,0,1,0,2,0,1,0,1) (0,1,0,1,0,1,0,2,0,1) (0,1,0,1,0,1,0,1,0,2) We have one choice where (s_2 − s_1)(n_2 − n_1)(u_2 − u_1)(k_2 - k_1)(e_2 - e_1) is 1 and five choices where (s_2 − s_1)(n_2 − n_1)(u_2 − u_1)(k_2 - k_1)(e_2 - e_1) is 2 , so the answer is 11 . Be sure to find the sum modulo (10^{9} +7) .
35,670
Score : 700 points Problem Statement Takahashi found an undirected connected graph with N vertices and M edges. The vertices are numbered 1 through N . The i -th edge connects vertices a_i and b_i , and has a weight of c_i . He will play Q rounds of a game using this graph. In the i -th round, two vertices S_i and T_i are specified, and he will choose a subset of the edges such that any vertex can be reached from at least one of the vertices S_i or T_i by traversing chosen edges. For each round, find the minimum possible total weight of the edges chosen by Takahashi. Constraints 1 ≩ N ≩ 4,000 1 ≩ M ≩ 400,000 1 ≩ Q ≩ 100,000 1 ≩ a_i,b_i,S_i,T_i ≩ N 1 ≩ c_i ≩ 10^{9} a_i \neq b_i S_i \neq T_i The given graph is connected. Partial Scores In the test set worth 200 points, Q = 1 . In the test set worth another 300 points, Q ≩ 3000 . Input The input is given from Standard Input in the following format: N M a_1 b_1 c_1 a_2 b_2 c_2 : a_M b_M c_M Q S_1 T_1 S_2 T_2 : S_Q T_Q Output Print Q lines. The i -th line should contain the minimum possible total weight of the edges chosen by Takahashi. Sample Input 1 4 3 1 2 3 2 3 4 3 4 5 2 2 3 1 4 Sample Output 1 8 7 We will examine each round: In the 1 -st round, choosing edges 1 and 3 achieves the minimum total weight of 8 . In the 2 -nd round, choosing edges 1 and 2 achieves the minimum total weight of 7 . Sample Input 2 4 6 1 3 5 4 1 10 2 4 6 3 2 2 3 4 5 2 1 3 1 2 3 Sample Output 2 8 This input satisfies the additional constraints for both partial scores.
35,671
Problem G: BUT We Need a Diagram Consider a data structure called BUT (Binary and/or Unary Tree). A BUT is defined inductively as follows: Let l be a letter of the English alphabet, either lowercase or uppercase (n the sequel, we say simply "a letter"). Then, the object that consists only of l , designating l as its label, is a BUT. In this case, it is called a 0-ary BUT. Let l be a letter and C a BUT. Then, the object that consists of l and C , designating l as its label and C as its component, is a BUT. In this case, it is called a unary BUT. Let l be a letter, L and R BUTs. Then, the object that consists of l , L and R , designating l as its label, L as its left component, and R as its right component, is a BUT. In this case, it is called a binary BUT. A BUT can be represented by a expression in the following way. When a BUT B is 0-ary, its representation is the letter of its label. When a BUT B is unary, its representation is the letter of its label followed by the parenthesized representation of its component. When a BUT B is binary, its representation is the letter of its label, a left parenthesis, the representation of its left component, a comma, the representation of its right component, and a right parenthesis, arranged in this order. Here are examples: a A(b) a(a,B) a(B(c(D),E),f(g(H,i))) Such an expression is concise, but a diagram is much more appealing to our eyes. We prefer a diagram: D H i - --- c E g --- - B f ---- a to the expression a(B(c(D),E),f(g(H,i))) Your mission is to write a program that converts the expression representing a BUT into its diagram. We want to keep a diagram as compact as possible assuming that we display it on a conventional character terminal with a fixed pitch font such as Courier. Let's define the diagram D for BUT B inductively along the structure of B as follows: When B is 0-ary, D consists only of a letter of its label. The letter is called the root of D , and also called the leaf of D When B is unary, D consists of a letter l of its label, a minus symbol S , and the diagram C for its component, satisfying the following constraints: l is just below S The root of C is just above S l is called the root of D , and the leaves of C are called the leaves of D . When B is binary, D consists of a letter l of its label, a sequence of minus symbols S , the diagram L for its left component, and the diagram R for its right component, satisfying the following constraints: S is contiguous, and is in a line. l is just below the central minus symbol of S , where, if the center of S locates on a minus symbol s , s is the central, and if the center of S locates between adjacent minus symbols, the left one of them is the central. The root of L is just above the left most minus symbols of S , and the rot of R is just above the rightmost minus symbol of S In any line of D , L and R do not touch or overlap each other. No minus symbols are just above the leaves of L and R . l is called the root of D , and the leaves of L and R are called the leaves of D Input The input to the program is a sequence of expressions representing BUTs. Each expression except the last one is terminated by a semicolon. The last expression is terminated by a period. White spaces (tabs and blanks) should be ignored. An expression may extend over multiple lines. The number of letter, i.e., the number of characters except parentheses, commas, and white spaces, in an expression is at most 80. You may assume that the input is syntactically correct and need not take care of error cases. Output Each expression is to be identified with a number starting with 1 in the order of occurrence in the input. Output should be produced in the order of the input. For each expression, a line consisting of the identification number of the expression followed by a colon should be produced first, and then, the diagram for the BUT represented by the expression should be produced. For diagram, output should consist of the minimum number of lines, which contain only letters or minus symbols together with minimum number of blanks required to obey the rules shown above. Sample Input a(A,b(B,C)); x( y( y( z(z), v( s, t ) ) ), u ) ; a( b( c, d( e(f), g ) ), h( i( j( k(k,k), l(l) ), m(m) ) ) ); a(B(C),d(e(f(g(h(i(j,k),l),m),n),o),p)) . Output for the Sample Input 1: B C --- A b --- a 2: z s t - --- z v ---- y - y u --- x 3: k k l --- - f k l m - ---- - e g j m --- ----- c d i --- - b h ------- a 4: j k --- i l --- h m --- g n --- f o --- C e p - --- B d ---- a
35,672
D: DAG トリオ / DAG Trio この問題は G: DAG Trio (Hard) ず制玄のみが異なる同じ蚭定の問題です。 プロロヌグ 匟は最近「だぐずりお」が欲しいずしきりに呟いおいたす。 心配になった兄が調べたずころ、匟のクラスでは $k$-DAG (有向グラフであっお、ある $k$ 個の蟺を削陀するず、蟺の向きを無芖したずきの連結成分数を $k$ にでき、か぀それら党おが DAG である) が流行っおおり、 3-DAG を特に DAG トリオず呌ぶこずを突き止めたした。 匟に尊敬されたい兄は、䞎えられたグラフが DAG トリオかどうかを刀別するプログラムを䜜成するこずにしたした。 問題文 $N$ 頂点 $M$ 蟺の有向グラフが䞎えられたす。 各頂点には $1$ から $N$ たで番号が振られおいたす。 各有向蟺には $1$ から $M$ たで番号が振られおいたす。 有向蟺 $i$ は頂点 $a_i$ から $b_i$ に向かいたす。 グラフは連結か぀単玔です (蟺の向きを無芖するず、任意の 2 点間に道があり自己ルヌプず倚重蟺がありたせん)。 䞎えられたグラフが DAG トリオならば ”YES”、そうでないなら ”NO” を出力しおください。 入力 $N \ M$ $a_1 \ b_1$ $a_2 \ b_2$ $\vdots$ $a_M \ b_M$ 制玄 $3 \le N \le 500$ $\max(3, N−1) \le M \le 1000$ $1 \le a_i, b_i \le N$ グラフは蟺の向きを無芖したずきに連結である。 各 $i$ に察しお$a_i \neq b_i$ 異なる $i, j$ に察しお $\{a_i, b_i\} \neq \{a_j, b_j\}$ 出力 ”YES” たたは ”NO” を $1$ 行で出力しおください。 サンプル サンプル入力1 3 3 1 2 2 3 3 1 サンプル出力1 YES サンプル入力2 6 7 1 2 2 3 4 3 4 5 5 6 6 4 3 6 サンプル出力2 YES サンプル入力3 7 10 4 2 4 7 4 6 2 7 2 5 2 1 5 6 1 3 6 3 4 3 サンプル出力3 NO サンプル入力4 4 4 1 2 3 2 4 3 2 4 サンプル出力4 YES サンプル入力5 8 9 5 1 3 8 1 2 4 8 4 7 7 5 6 5 3 2 4 2 サンプル出力5 YES
35,673
Minimum Spanning Tree For a given weighted graph $G = (V, E)$, find the minimum spanning tree (MST) of $G$ and print total weight of edges belong to the MST. Input In the first line, an integer $n$ denoting the number of vertices in $G$ is given. In the following $n$ lines, a $n \times n$ adjacency matrix $A$ which represents $G$ is given. $a_{ij}$ denotes the weight of edge connecting vertex $i$ and vertex $j$. If there is no edge between $i$ and $j$, $a_{ij}$ is given by -1. Output Print the total weight of the minimum spanning tree of $G$. Constraints $1 \leq n \leq 100$ $0 \leq a_{ij} \leq 2,000$ (if $a_{ij} \neq -1$) $a_{ij} = a_{ji}$ $G$ is a connected graph Sample Input 1 5 -1 2 3 1 -1 2 -1 -1 4 -1 3 -1 -1 1 1 1 4 1 -1 3 -1 -1 1 3 -1 Sample Output 1 5 Reference Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.
35,674
Score : 200 points Problem Statement A ball will bounce along a number line, making N + 1 bounces. It will make the first bounce at coordinate D_1 = 0 , and the i -th bounce (2 \leq i \leq N+1) at coordinate D_i = D_{i-1} + L_{i-1} . How many times will the ball make a bounce where the coordinate is at most X ? Constraints 1 \leq N \leq 100 1 \leq L_i \leq 100 1 \leq X \leq 10000 All values in input are integers. Input Input is given from Standard Input in the following format: N X L_1 L_2 ... L_{N-1} L_N Output Print the number of times the ball will make a bounce where the coordinate is at most X . Sample Input 1 3 6 3 4 5 Sample Output 1 2 The ball will make a bounce at the coordinates 0 , 3 , 7 and 12 , among which two are less than or equal to 6 . Sample Input 2 4 9 3 3 3 3 Sample Output 2 4 The ball will make a bounce at the coordinates 0 , 3 , 6 , 9 and 12 , among which four are less than or equal to 9 .
35,675
Rakunarok You are deeply disappointed with the real world, so you have decided to live the rest of your life in the world of MMORPG (Massively Multi-Player Online Role Playing Game). You are no more concerned about the time you spend in the game: all you need is efficiency . One day, you have to move from one town to another. In this game, some pairs of towns are connected by roads where players can travel. Various monsters will raid players on roads. However, since you are a high-level player, they are nothing but the source of experience points. Every road is bi-directional. A path is represented by a sequence of towns, where consecutive towns are connected by roads. You are now planning to move to the destination town through the most efficient path. Here, the efficiency of a path is measured by the total experience points you will earn in the path divided by the time needed to travel the path. Since your object is moving, not training, you choose only a straightforward path. A path is said straightforward if, for any two consecutive towns in the path, the latter is closer to the destination than the former. The distance of two towns is measured by the shortest time needed to move from one town to another. Write a program to find a path that gives the highest efficiency. Input The first line contains a single integer c that indicates the number of cases. The first line of each test case contains two integers n and m that represent the numbers of towns and roads respectively. The next line contains two integers s and t that denote the starting and destination towns respectively. Then m lines follow. The i -th line contains four integers u i , v i , e i , and t i , where u i and v i are two towns connected by the i -th road, e i is the experience points to be earned, and t i is the time needed to pass the road. Each town is indicated by the town index number from 0 to ( n - 1). The starting and destination towns never coincide. n , m , e i 's and t i 's are positive and not greater than 1000. Output For each case output in a single line, the highest possible efficiency. Print the answer with four decimal digits. The answer may not contain an error greater than 10 -4 . Sample Input 2 3 3 0 2 0 2 240 80 0 1 130 60 1 2 260 60 3 3 0 2 0 2 180 60 0 1 130 60 1 2 260 60 Output for the Sample Input 3.2500 3.0000
35,676
Score : 400 points Problem Statement There are N people standing on the x -axis. Let the coordinate of Person i be x_i . For every i , x_i is an integer between 0 and 10^9 (inclusive). It is possible that more than one person is standing at the same coordinate. You will given M pieces of information regarding the positions of these people. The i -th piece of information has the form (L_i, R_i, D_i) . This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds. It turns out that some of these M pieces of information may be incorrect. Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information. Constraints 1 \leq N \leq 100 000 0 \leq M \leq 200 000 1 \leq L_i, R_i \leq N ( 1 \leq i \leq M ) 0 \leq D_i \leq 10 000 ( 1 \leq i \leq M ) L_i \neq R_i ( 1 \leq i \leq M ) If i \neq j , then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j) . D_i are integers. Input Input is given from Standard Input in the following format: N M L_1 R_1 D_1 L_2 R_2 D_2 : L_M R_M D_M Output If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print Yes ; if it does not exist, print No . Sample Input 1 3 3 1 2 1 2 3 1 1 3 2 Sample Output 1 Yes Some possible sets of values (x_1, x_2, x_3) are (0, 1, 2) and (101, 102, 103) . Sample Input 2 3 3 1 2 1 2 3 1 1 3 5 Sample Output 2 No If the first two pieces of information are correct, x_3 - x_1 = 2 holds, which is contradictory to the last piece of information. Sample Input 3 4 3 2 1 1 2 3 5 3 4 2 Sample Output 3 Yes Sample Input 4 10 3 8 7 100 7 9 100 9 8 100 Sample Output 4 No Sample Input 5 100 0 Sample Output 5 Yes
35,677
Problem J: Cave Explorer Mike Smith is a man exploring caves all over the world. One day, he faced a scaring creature blocking his way. He got scared, but in short time he took his knife and then slashed it to attempt to kill it. Then they were split into parts, which soon died out but the largest one. He slashed the creature a couple of times more to make it small enough, and finally became able to go forward. Now let us think of his situation in a mathematical way. The creature is considered to be a polygon, convex or concave. Mike slashes this creature straight with his knife in some direction. We suppose here the direction is given for simpler settings, while the position is arbitrary. Then all split parts but the largest one disappears. Your task is to write a program that calculates the area of the remaining part when the creature is slashed in such a way that the area is minimized. Input The input is a sequence of datasets. Each dataset is given in the following format: n v x v y x 1 y 1 ... x n y n The first line contains an integer n , the number of vertices of the polygon that represents the shape of the creature (3 ≀ n ≀ 100). The next line contains two integers v x and v y , where ( v x , v y ) denote a vector that represents the direction of the knife (-10000 ≀ v x , v y ≀ 10000, v x 2 + v y 2 > 0). Then n lines follow. The i -th line contains two integers x i and y i , where ( x i , y i ) denote the coordinates of the i -th vertex of the polygon (0 ≀ x i , y i ≀ 10000). The vertices are given in the counterclockwise order. You may assume the polygon is always simple, that is, the edges do not touch or cross each other except for end points. The input is terminated by a line with a zero. This should not be processed. Output For each dataset, print the minimum possible area in a line. The area may be printed with an arbitrary number of digits after the decimal point, but should not contain an absolute error greater than 10 -2 . Sample Input 5 0 1 0 0 5 0 1 1 5 2 0 2 7 9999 9998 0 0 2 0 3 1 1 1 10000 9999 2 2 0 2 0 Output for the Sample Input 2.00 2.2500000
35,678
A さんず B 君は N × M の長方圢のマス目状の地域に䜏んでいる各マス目は道か壁か家のどれかであるこの地域は道が耇雑で入り組んでいるので痎挢被害がよく起こるこずで有名であるためこの地域ず倖郚ずの境界は党お壁で囲たれおおり隔離されおいる B 君はなんずなく気が向いたのでA さんの家を眺めに行くこずにしたしかし䞍幞なこずにB 君は明らかに怪しい顔をしおいるのでA さんの家に行く途䞭に少しでも痎挢の疑いがあるような行動を取ったらすぐに捕たっおしたうだろう特に右手を壁から離しお歩くこずは絶察にやっおはならないB 君は䞀瞬たりずも右手を離すこずなくA さんの家に蟿り着くこずが出来るだろうか B 君は぀ねに䞊䞋巊右いずれかの方向を向いおいおB 君の右手が届く範囲はB 君の向いおいる方向に察しお正面斜め右前右斜め右埌ろの4 マスのみであるB 君は次のいずれかの行動を繰り返すただしこれらを同時に行うこずは出来ない 前方に壁がない堎合1 マス進む 向いおいる方向を90 床右に倉える 向いおいる方向を90 床巊に倉える 右手が接するマスを倉えるただしこの時にB 君は右手を離すこずが出来ないので倉曎前のマスず倉曎埌のマスの間には共通した点を持っおいなくおはならない Input 入力は以䞋の圢匏で䞎えられる N M S 1 S 2 : S N S i (1 ≀ i ≀ N ) は M 文字の文字列で各文字は次を衚す "^""v""<"">" は B 君の最初の䜍眮ず最初に向いおいる方向䞊䞋巊右の向きを衚す "." は䜕もないマスであるB 君はこのマスの䞊を移動するこずができる "#" は壁を衚す壁の䞊を移動するこずは出来ない "G" は A さんの家の䜍眮を衚すB 君はA さんの家にたどり着くたで移動を繰り返す Constraints 1 ≀ N ≀ 50 1 ≀ M ≀ 50 入力には文字 "^""v""<"">" のうちいずれかが必ず䞀぀のみ珟れる 同様に入力には文字 "G" が必ず䞀぀のみ珟れる 初期状態ではB 君が向いおいる方向から右の壁にB 君の右手が接しおいる Output 䞀床も右手を離さずにA さんの家に蟿り着くこずが出来る堎合にはA さんの家にたどり着くたでに通った異なるマスの数の最小倀を出力せよA さんの家に蟿り着くこずが出来ない堎合には-1 を出力せよ Sample Input 1 3 3 G## .#. .<. Output for the Sample Input 1 4 Sample Input 2 3 3 G## .#. .>. Output for the Sample Input 2 6 Sample Input 3 3 3 ... .G. .>. Output for the Sample Input 3 -1 Sample Input 4 4 4 .... .#.G ...# ^#.. Output for the Sample Input 4 8
35,679
Problem E: Mobile Computing There is a mysterious planet called Yaen, whose space is 2-dimensional. There are many beautiful stones on the planet, and the Yaen people love to collect them. They bring the stones back home and make nice mobile arts of them to decorate their 2-dimensional living rooms. In their 2-dimensional world, a mobile is defined recursively as follows: a stone hung by a string, or a rod of length 1 with two sub-mobiles at both ends; the rod is hung by a string at the center of gravity of sub-mobiles. When the weights of the sub-mobiles are n and m , and their distances from the center of gravity are a and b respectively, the equation n × a = m × b holds. For example, if you got three stones with weights 1, 1, and 2, here are some possible mobiles and their widths: Given the weights of stones and the width of the room, your task is to design the widest possible mobile satisfying both of the following conditions. It uses all the stones. Its width is less than the width of the room. You should ignore the widths of stones. In some cases two sub-mobiles hung from both ends of a rod might overlap (see the figure on the below). Such mobiles are acceptable. The width of the example is (1/3) + 1 + (1/4). Input The first line of the input gives the number of datasets. Then the specified number of datasets follow. A dataset has the following format. r s w 1 . . . w s r is a decimal fraction representing the width of the room, which satisfies 0 < r < 10. s is the number of the stones. You may assume 1 ≀ s ≀ 6. w i is the weight of the i -th stone, which is an integer. You may assume 1 ≀ w i ≀ 1000. You can assume that no mobiles whose widths are between r - 0.00001 and r + 0.00001 can be made of given stones. Output For each dataset in the input, one line containing a decimal fraction should be output. The decimal fraction should give the width of the widest possible mobile as defined above. An output line should not contain extra characters such as spaces. In case there is no mobile which satisfies the requirement, answer -1 instead. The answer should not have an error greater than 0.00000001. You may output any number of digits after the decimal point, provided that the above accuracy condition is satisfied. Sample Input 5 1.3 3 1 2 1 1.4 3 1 2 1 2.0 3 1 2 1 1.59 4 2 1 1 3 1.7143 4 1 2 3 5 Output for the Sample Input -1 1.3333333333333335 1.6666666666666667 1.5833333333333335 1.7142857142857142
35,680
Score : 400 points Problem Statement There are N islands lining up from west to east, connected by N-1 bridges. The i -th bridge connects the i -th island from the west and the (i+1) -th island from the west. One day, disputes took place between some islands, and there were M requests from the inhabitants of the islands: Request i : A dispute took place between the a_i -th island from the west and the b_i -th island from the west. Please make traveling between these islands with bridges impossible. You decided to remove some bridges to meet all these M requests. Find the minimum number of bridges that must be removed. Constraints All values in input are integers. 2 \leq N \leq 10^5 1 \leq M \leq 10^5 1 \leq a_i < b_i \leq N All pairs (a_i, b_i) are distinct. Input Input is given from Standard Input in the following format: N M a_1 b_1 a_2 b_2 : a_M b_M Output Print the minimum number of bridges that must be removed. Sample Input 1 5 2 1 4 2 5 Sample Output 1 1 The requests can be met by removing the bridge connecting the second and third islands from the west. Sample Input 2 9 5 1 8 2 7 3 5 4 6 7 9 Sample Output 2 2 Sample Input 3 5 10 1 2 1 3 1 4 1 5 2 3 2 4 2 5 3 4 3 5 4 5 Sample Output 3 4
35,681
問題 4 IOI 䞍動産ではマンションの賃貞を行なっおいる この䌚瀟が取り扱うマンションの郚屋は 1LDK で 䞋図のように面積が 2xy+x+y になっおいる ただし x, y は正敎数である IOI 䞍動産のカタログにはマンションの面積が昇順に狭いものから順番に曞かれおいるがこの䞭にいく぀か間違いありえない面積のものが混じっおいるこずがわかった カタログ入力ファむルは N+1 行で最初の行に郚屋数が曞かれおいお 続く N 行に行に郚屋ず぀面積が昇順に曞かれおいる ただし 郚屋数は 100,000 以䞋 面積は (2の31乗)-1 = 2,147,483,647 以䞋である ぀の入力デヌタのうち぀たでは 郚屋数 1000 以䞋面積 30000 以䞋である 間違っおいる行数ありえない郚屋の数を出力しなさい 出力ファむルにおいおは 出力の最埌の行にも改行コヌドを入れるこず 入出力䟋 入力䟋 10 4 7 9 10 12 13 16 17 19 20 出力䟋 2 問題文ず自動審刀に䜿われるデヌタは、 情報オリンピック日本委員䌚 が䜜成し公開しおいる問題文ず採点甚テストデヌタです。
35,682
牜牛ず織女 織女は倩垝の子䟛でしたが、父の蚀い぀けであけおもくれおも機を織っおいたした。 織女の織る雲錊ずいう芋事な垃で仕立おた服を着るのが倩垝の楜しみでした。雲錊は寿呜が短くすぐに劣化しおしたいたすが、働き者の織女が毎日織っおくれるので、問題はありたせんでした。織女は、父の蚀い぀けを守り、毎日毎日雲錊を織り続けおいたので、ボヌむフレンドはいたせんでした。かわいそうに思った父は、倩の川の向こう岞に䜏む牜牛ずいう働き者を玹介し、嫁入りさせたした。 するず、織女は、結婚の楜しさに倢䞭になっお、機織りなどそっちのけで、牜牛ず遊び呆けおいたす。倩垝のお気に入りの雲錊の服も新しく仕立おられないためボロボロになっおしたいたした。 これには父も怒っお、織女を宮殿に連れ戻したいず思いたした。しかし人間である牜牛の前にボロボロの服で姿を珟すわけにはいきたせん。遊び呆けおいる二人を 3 角圢の壁で遮断し自分以倖の党おのものが行き来できなくするこずを考えたした。そしお、牜牛に芋぀からずに、織女に䌚っお、たじめに機を織るか、さもなければ匷制的に連れ垰るず宣蚀するずいうのです。 倩垝はこの䜜戊を遂行するために 3 角圢の壁生成装眮を開発するこずにしたした。3 角圢の 3 頂点の䜍眮 (xp 1 , yp 1 ) , (xp 2 , yp 2 ) , (xp 3 , yp 3 ) 、牜牛の䜍眮 (xk, yk) 、および織女の䜍眮 (xs, ys) 、を入力ずし、䞉角圢が牜牛ず織女を遮断しおいるか吊かを刀定し、遮断できおいる堎合は OK、遮断できおいない堎合には NG を出力するプログラムを䜜成しおください。ただし、遮断しおいるずは、牜牛ず織女のいずれかが䞉角圢の内偎にあり、他方が倖偎にある堎合を蚀いたす。牜牛ず織女は䞉角圢の頂点もしくは蟺の䞊にはいないものずしたす。 織女ず牜牛は時々刻々堎所を倉えるため、プログラムは様々な䜍眮情報を入力し質問に答えなければなりたせん。 Input 入力は以䞋の圢匏で䞎えられたす。 n query 1 query 2 : query n 行目に刀別したい情報の個数 n ( n ≀ 10000)、続く n 行に i 番目の質問 query i が䞎えられたす。各質問は以䞋の圢匏で䞎えられたす。 xp 1 yp 1 xp 2 yp 2 xp 3 yp 3 xk yk xs ys 各質問ずしお、3 角圢の 3 頂点の䜍眮、牜牛の䜍眮、および織女の䜍眮 (-1000 ≀ xp 1 , yp 1 , xp 2 , yp 2 , xp 3 , yp 3 , xk , yk , xs , ys ≀ 1000) が行に䞎えられたす。入力はすべお敎数です。 Output 質問ごずに、刀定結果 OK たたは NG を行に出力しおください。 Sample Input 5 2 5 9 2 8 9 2 11 6 5 2 5 9 2 8 9 2 11 12 6 2 5 9 2 8 9 2 11 11 9 14 1 25 7 17 12 17 9 20 5 14 1 25 7 17 12 22 13 20 5 Output for the Sample Input OK NG NG NG OK
35,683
Range Count Query 数列 a_1,a_2,..,a_N が䞎えられたす。 ク゚リでは、倀が l 以䞊 r 以䞋の項の個数を答えおください。 入力 N Q a_1 a_2...a_N l_1 r_1 l_2 r_2 : l_q r_q 出力 ans_1 ans_2 : ans_q i 行目には、 i 番目のク゚リに察する答え、すなわち l_i \leq a_j \leq r_i なる j の個数を出力せよ。 制玄 1 \leq N,Q \leq 10^5 1 \leq a_i \leq 10^9 1 \leq l_i \leq r_i \leq 10^9 入力䟋 6 3 8 6 9 1 2 1 2 8 1 7 3 5 出力䟋 3 4 0
35,684
コンテスト 問題 先日オンラむンでのプログラミングコンテストが行われた W倧孊ずK倧孊のコンピュヌタクラブは以前からラむバル関係にありこのコンテストを利甚しお䞡者の優劣を決めようずいうこずになった 今回この2぀の倧孊からはずもに10人ず぀がこのコンテストに参加した長い議論の末参加した10人のうち埗点の高い方から3人の埗点を合蚈し倧孊の埗点ずするこずにした W倧孊およびK倧孊の参加者の埗点のデヌタが䞎えられるこのずきおのおのの倧孊の埗点を蚈算するプログラムを䜜成せよ 入力 入力は20行からなる 1行目から10行目にはW倧孊の各参加者の埗点を衚す敎数が 11行目から20行目にはK倧孊の各参加者の埗点を衚す敎数が曞かれおいるこれらの敎数はどれも0以䞊100以䞋である 出力 W倧孊の埗点ずK倧孊の埗点をこの順に空癜区切りで出力せよ 入出力䟋 入力䟋 23 23 20 15 15 14 13 9 7 6 25 19 17 17 16 13 12 11 9 5 出力䟋 66 61 入力䟋 17 25 23 25 79 29 1 61 59 100 44 74 94 57 13 54 82 0 42 45 出力䟋 240 250 䞊蚘問題文ず自動審刀に䜿われるデヌタは、 情報オリンピック日本委員䌚 が䜜成し公開しおいる問題文ず採点甚テストデヌタです。
35,685
Reverse Sequence Write a program which reverses a given string str . Input str (the size of str ≀ 20) is given in a line. Output Print the reversed str in a line. Sample Input w32nimda Output for the Sample Input admin23w
35,686
Score : 600 points Problem Statement AtCoDeer the deer has N cards with positive integers written on them. The number on the i -th card (1≀i≀N) is a_i . Because he loves big numbers, he calls a subset of the cards good when the sum of the numbers written on the cards in the subset, is K or greater. Then, for each card i , he judges whether it is unnecessary or not, as follows: If, for any good subset of the cards containing card i , the set that can be obtained by eliminating card i from the subset is also good, card i is unnecessary. Otherwise, card i is NOT unnecessary. Find the number of the unnecessary cards. Here, he judges each card independently, and he does not throw away cards that turn out to be unnecessary. Constraints All input values are integers. 1≀N≀5000 1≀K≀5000 1≀a_i≀10^9 (1≀i≀N) Partial Score 300 points will be awarded for passing the test set satisfying N,K≀400 . Input The input is given from Standard Input in the following format: N K a_1 a_2 ... a_N Output Print the number of the unnecessary cards. Sample Input 1 3 6 1 4 3 Sample Output 1 1 There are two good sets: { 2,3 } and { 1,2,3 }. Card 1 is only contained in { 1,2,3 }, and this set without card 1 , { 2,3 }, is also good. Thus, card 1 is unnecessary. For card 2 , a good set { 2,3 } without card 2 , { 3 }, is not good. Thus, card 2 is NOT unnecessary. Neither is card 3 for a similar reason, hence the answer is 1 . Sample Input 2 5 400 3 1 4 1 5 Sample Output 2 5 In this case, there is no good set. Therefore, all the cards are unnecessary. Sample Input 3 6 20 10 4 3 10 25 2 Sample Output 3 3
35,687
Score : 300 points Problem Statement Iroha is very particular about numbers. There are K digits that she dislikes: D_1, D_2, ..., D_K . She is shopping, and now paying at the cashier. Her total is N yen (the currency of Japan), thus she has to hand at least N yen to the cashier (and possibly receive the change). However, as mentioned before, she is very particular about numbers. When she hands money to the cashier, the decimal notation of the amount must not contain any digits that she dislikes. Under this condition, she will hand the minimum amount of money. Find the amount of money that she will hand to the cashier. Constraints 1 ≩ N < 10000 1 ≩ K < 10 0 ≩ D_1 < D_2 < 
 < D_K≩9 \{D_1,D_2,...,D_K\} ≠ \{1,2,3,4,5,6,7,8,9\} Input The input is given from Standard Input in the following format: N K D_1 D_2 
 D_K Output Print the amount of money that Iroha will hand to the cashier. Sample Input 1 1000 8 1 3 4 5 6 7 8 9 Sample Output 1 2000 She dislikes all digits except 0 and 2 . The smallest integer equal to or greater than N=1000 whose decimal notation contains only 0 and 2 , is 2000 . Sample Input 2 9999 1 0 Sample Output 2 9999
35,688
Score : 500 points Problem Statement Given is a connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N , and the edges are described by a grid of characters S . If S_{i,j} is 1 , there is an edge connecting Vertex i and j ; otherwise, there is no such edge. Determine whether it is possible to divide the vertices into non-empty sets V_1, \dots, V_k such that the following condition is satisfied. If the answer is yes, find the maximum possible number of sets, k , in such a division. Every edge connects two vertices belonging to two "adjacent" sets. More formally, for every edge (i,j) , there exists 1\leq t\leq k-1 such that i\in V_t,j\in V_{t+1} or i\in V_{t+1},j\in V_t holds. Constraints 2 \leq N \leq 200 S_{i,j} is 0 or 1 . S_{i,i} is 0 . S_{i,j}=S_{j,i} The given graph is connected. N is an integer. Input Input is given from Standard Input in the following format: N S_{1,1}...S_{1,N} : S_{N,1}...S_{N,N} Output If it is impossible to divide the vertices into sets so that the condition is satisfied, print -1 . Otherwise, print the maximum possible number of sets, k , in a division that satisfies the condition. Sample Input 1 2 01 10 Sample Output 1 2 We can put Vertex 1 in V_1 and Vertex 2 in V_2 . Sample Input 2 3 011 101 110 Sample Output 2 -1 Sample Input 3 6 010110 101001 010100 101000 100000 010000 Sample Output 3 4
35,689
Problem G: Japanese Style Pub You’ve just entered a Japanese-style pub, or an izakaya in Japanese, for a drinking party (called nomi-kai ) with your dear friends. Now you are to make orders for glasses of hard and soft drink as requested by the participants. But unfortunately, most staffs in typical izakayas are part-time workers; they are not used to their work so they make mistakes at a certain probability for each order. You are worrying about such mistakes. Today is a happy day for the participants, the dearest friends of yours. Your task is to write a program calculating the probability at which the izakaya staff brings correct drinks for the entire orders. Cases in which the staff’s mistakes result in a correct delivery should be counted into the probability, since those cases are acceptable for you. Input The input consists of multiple test cases. Each test case begins with a line containing an interger N (1 ≀ N ≀ 8). The integer N indicates the number of kinds of drinks available in the izakaya . The following N lines specify the probabilities for the drinks in the format shown below. p 11 p 12 . . . p 1 N p 21 p 22 . . . p 2 N ... p N 1 p N 2 . . . p NN Each real number p ij indicates the probability where the staff delivers a glass of the drink j for an order of the drink i . It holds that p ij ≥ 0 and p i 1 + p i 2 + . . . + p iN = 1 for 1 ≀ i , j ≀ N . At the end of each test case, a line which contains N comes. The i -th integer n i represents the number of orders for the drink i by the participants (0 ≀ n i ≀ 4). The last test case is followed by a line containing a zero. Output For Each test case, print a line containing the test case number (beginning with 1) followed by the natural logarithm of the probability where the staff brings correct drinks for the entire orders. Print the results with eight digits to the right of the decimal point. If the staff cannot bring correct drinks in any case, print “-INFINITY” instead. Use the format of the sample output. Sample Input 3 0.7 0.1 0.2 0.1 0.8 0.1 0.0 0.0 1.0 4 3 2 8 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 2 2 2 2 2 2 2 2 2 1 0 1 0 2 2 0 Output for the Sample Input Case 1: -1.89723999 Case 2: -8.14438201 Case 3: -INFINITY
35,690
E: Twins Idol / ツむンズアむドル アむドル事務所841プロダクションには倚くの個性的なアむドル達が所属しおいる 双子のアむドルであるアキマキは841プロに所属するアむドルであり2人ずもずおもゲヌム奜きである ある日2人は次のようなゲヌムを自分達で自䜜しお楜しむこずにした このゲヌムは2人で協力するタむプのゲヌムであるため双子の息の合ったプレむが期埅される たずゲヌムを始める前に2人が別々に自分の奜きなようにフィヌルドを床に描く フィヌルドはアルファベットの1文字が曞かれたいく぀かのボヌドずボヌド間を぀なぐルヌトで構成される 圌女らはこのルヌト䞊を蟿るこずでボヌド間を移動できる ただしルヌトには矢印が぀いおおりその矢印の方向に向かっおしか移動できない アキのフィヌルドずマキのフィヌルドの間にルヌトは存圚しないため互いのフィヌルドを行き来するこずはできない たずアキずマキが自分のフィヌルド内の奜きなボヌドに立぀ずころからゲヌムが始たる そしお以䞋の決たりに埓っお2人が同時に行動しおいく 2人の立っおいるボヌドの文字が等しい堎合 スコアずしお1点を埗る 2人は珟圚立っおいるボヌドを離れお別のボヌドに移動しなければならない ただし別のボヌドに移動しようずした際2人の内どちらかが移動できなかった堎合ゲヌムを終了する 2人の立っおいるボヌドの文字が異なる堎合 スコアを埗るこずはできない 2人はそれぞれ「珟圚のボヌドにずどたる」か「別のボヌドに移動する」かを遞択しお行動する2人が同じ行動を取る必芁はない ただし別のボヌドに移動しようずした際移動できなかった堎合ゲヌムを終了する 珟圚以降のタヌンを䜿甚しお珟圚のボヌドからどう移動しおも点数を増やせないずき ゲヌムを終了する アキマキはこのゲヌムでなるべく高いスコアを出したいず考えおいる 2人が最倧でどれだけの埗点を取埗できるか841プロの新人敏腕プロデュヌサである君がお埗意のプログラミングによっお華麗にヒントを䞎えおあげよう Input 入力は次の圢匏で衚される AKI_FIELD MAKI_FIELD AKI_FIELD ず MAKI_FIELD はそれぞれアキずマキのフィヌルドを衚す 各フィヌルドの入力は次の圢匏で衚される N M c 1 c 2, ..., cN a 1 b 1 ... ai bi ... aM bM 党おの数倀は敎数倀である N (1 <= N <= 100) はボヌドの数であり各ボヌドには1, 2, ..., N の番号が付いおいる M (0 <= M <= 5000) はボヌド間のルヌトの数を衚す 次の1行には各ボヌドに曞かれおいる文字がボヌド1から順番に空癜で区切られお入力される 各文字はアルファベットの小文字か倧文字のいずれかである 倧文字ず小文字は別の文字ずしお扱わなければならない 続いお M 行にわたっおルヌト情報が入力される 各ルヌトはボヌド ai からボヌド bi に向かっお䌞びおいる(1 <= ai , bi <= N ) ai ず bi は異なる倀であり䞀床入力されたルヌトが再び入力されるこずはない ただし䞀床入力されたルヌトの逆方向のルヌトが入力されるこずはある Output アキマキが取埗できる最倧の埗点を答えよ ただし無限に点数を増やせる堎合は-1を出力せよ Sample Input 1 5 5 a c b c a 1 2 1 3 2 3 3 4 3 5 4 3 b a c c 1 3 2 3 3 4 Sample Output 1 3 Sample Input 2 2 2 a b 1 2 2 1 4 3 a b a b 1 2 2 3 3 4 Sample Output 2 4 Sample Input 3 3 3 Y Y I 1 2 2 3 3 1 3 3 I O R 1 3 3 2 2 1 Sample Output 3 -1
35,691
Problem B: Stylish Stylish is a programming language whose syntax comprises names , that are sequences of Latin alphabet letters, three types of grouping symbols , periods ('.'), and newlines. Grouping symbols, namely round brackets ('(' and ')'), curly brackets ('{' and '}'), and square brackets ('[' and ']'), must match and be nested properly. Unlike most other programming languages, Stylish uses periods instead of whitespaces for the purpose of term separation. The following is an example of a Stylish program. 1 ( Welcome .to 2 ......... Stylish ) 3 { Stylish .is 4 .....[.( a. programming . language .fun .to. learn ) 5 .......] 6 ..... Maybe .[ 7 ....... It. will .be.an. official . ICPC . language 8 .......] 9 .....} As you see in the example, a Stylish program is indented by periods. The amount of indentation of a line is the number of leading periods of it. Your mission is to visit Stylish masters, learn their indentation styles, and become the youngest Stylish master. An indentation style for well-indented Stylish programs is defined by a triple of integers, ( R , C , S ), satisfying 1 ≀ R , C , S ≀ 20. R , C and S are amounts of indentation introduced by an open round bracket, an open curly bracket, and an open square bracket, respectively. In a well-indented program, the amount of indentation of a line is given by R ( r o − r c ) + C ( c o − c c ) + S ( s o − s c ), where r o , c o , and s o are the numbers of occurrences of open round, curly, and square brackets in all preceding lines, respectively, and r c , c c , and s c are those of close brackets. The first line has no indentation in any well-indented program. The above example is formatted in the indentation style ( R , C , S ) = (9, 5, 2). The only grouping symbol occurring in the first line of the above program is an open round bracket. Therefore the amount of indentation for the second line is 9 · (1 − 0) + 5 · (0 − 0) + 2 ·(0 − 0) = 9. The first four lines contain two open round brackets, one open curly bracket, one open square bracket, two close round brackets, but no close curly nor square bracket. Therefore the amount of indentation for the fifth line is 9 · (2 − 2) + 5 · (1 − 0) + 2 · (1 − 0) = 7. Stylish masters write only well-indented Stylish programs. Every master has his/her own indentation style. Write a program that imitates indentation styles of Stylish masters. Input The input consists of multiple datasets. The first line of a dataset contains two integers p (1 ≀ p ≀ 10) and q (1 ≀ q ≀ 10). The next p lines form a well-indented program P written by a Stylish master and the following q lines form another program Q . You may assume that every line of both programs has at least one character and at most 80 characters. Also, you may assume that no line of Q starts with a period. The last dataset is followed by a line containing two zeros. Output Apply the indentation style of P to Q and output the appropriate amount of indentation for each line of Q . The amounts must be output in a line in the order of corresponding lines of Q and they must be separated by a single space. The last one should not be followed by trailing spaces. If the appropriate amount of indentation of a line of Q cannot be determined uniquely through analysis of P , then output -1 for that line. Sample Input 5 4 (Follow.my.style .........starting.from.round.brackets) {then.curly.brackets .....[.and.finally .......square.brackets.]} (Thank.you {for.showing.me [all the.secrets]}) 4 2 (This.time.I.will.show.you .........(how.to.use.round.brackets) .........[but.not.about.square.brackets] .........{nor.curly.brackets}) (I.learned how.to.use.round.brackets) 4 2 (This.time.I.will.show.you .........(how.to.use.round.brackets) .........[but.not.about.square.brackets] .........{nor.curly.brackets}) [I.have.not.learned how.to.use.square.brackets] 2 2 (Be.smart.and.let.fear.of ..(closed.brackets).go) (A.pair.of.round.brackets.enclosing [A.line.enclosed.in.square.brackets]) 1 2 Telling.you.nothing.but.you.can.make.it [One.liner.(is).(never.indented)] [One.liner.(is).(never.indented)] 2 4 ([{Learn.from.my.KungFu ...}]) (( {{ [[ ]]}})) 1 2 Do.not.waste.your.time.trying.to.read.from.emptiness ( ) 2 3 ({Quite.interesting.art.of.ambiguity ....}) { ( )} 2 4 ({[ ............................................................]}) ( { [ ]}) 0 0 Output for the Sample Input 0 9 14 16 0 9 0 -1 0 2 0 0 0 2 4 6 0 -1 0 -1 4 0 20 40 60
35,692
Score : 600 points Problem Statement Given are integers L and R . Find the number, modulo 10^9 + 7 , of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x . What is \mbox{ XOR } ? The XOR of integers A and B , A \mbox{ XOR } B , is defined as follows: When A \mbox{ XOR } B is written in base two, the digit in the 2^k 's place ( k \geq 0 ) is 1 if either A or B , but not both, has 1 in the 2^k 's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6 . (In base two: 011 \mbox{ XOR } 101 = 110 .) Constraints 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7 . Sample Input 1 2 3 Sample Output 1 3 Three pairs satisfy the condition: (2, 2) , (2, 3) , and (3, 3) . Sample Input 2 10 100 Sample Output 2 604 Sample Input 3 1 1000000000000000000 Sample Output 3 68038601 Be sure to compute the number modulo 10^9 + 7 .
35,693
Intersection of a Circle and a Polygon Find the area of intersection between a circle $c$ and a polygon $g$. The center coordinate of the circle is ($0, 0$). The polygon $g$ is represented by a sequence of points $p_1$, $p_2$,..., $p_n$ where line segments connecting $p_i$ and $p_{i+1}$ ($1 \leq i \leq n−1$) are sides of the polygon. The line segment connecting $p_n$ and $p_1$ is also a side of the polygon. Note that the polygon is not necessarily convex. Input The input is given in the following format. $n$ $r$ $x_1$ $y_1$ $x_2$ $y_2$ : $x_n$ $y_n$ In the first line, an integer n representing the number of points in the polygon is given. The coordinate of a point $p_i$ is given by two integers $x_i$ and $y_i$. The coordinates of the points are given in the order of counter-clockwise visit of them. All input values are given in integers. Constraints $3 \leq n \leq 100$ $1 \leq r \leq 100$ $-100 \leq x_i, y_i \leq 100$ Output Print the area of intersection in a line. The output values should be in a decimal fraction with an error less than 0.00001. Sample Input 1 3 5 1 1 4 1 5 5 Sample Output 1 4.639858417607 Sample Input 2 4 5 0 0 -3 -6 1 -3 5 -4 Sample Output 2 11.787686807576
35,694
埮生物発電 飯沌博士は磐梯山の噎気孔でふしぎな埮生物を芋぀けたした。この埮生物の雄ず雌䜓ず぀が合䜓するず、電気゚ネルギヌを攟出したす。この埮生物を研究すれば、将来の゚ネルギヌ危機から我々を救えるかもしれたせん。 芳察を続けるず、埮生物は合䜓したずきだけ電気゚ネルギヌを発生させるこずず、合䜓した埮生物のさらなる合䜓はないこずがわかりたした。さらに芳察を続けるず、合䜓で攟出される電気゚ネルギヌは、埮生物が䜓内に持぀未知の粒子博士はこれを磐梯山にちなんで粒子ず名づけたしたの量で決たるこずがわかりたした。合䜓する雄ず雌が䜓内に持぀粒子の量をそれぞれ bm ず bw ずするず、合䜓により攟出される電気゚ネルギヌは | bm - bw | × (| bm - bw | - 30) 2 ずいう匏で蚈算できたす。 この発芋により、埮生物の集団から埗られる最倧の電気゚ネルギヌが蚈算できるようになりたした。それでは、埮生物の集団に含たれる雄ず雌の数ず、各個䜓が持぀粒子の量が䞎えられたずき、この埮生物の集団から埗られる最倧の電気゚ネルギヌを蚈算するプログラムを䜜成しおください。 入力 入力は耇数のデヌタセットからなる。入力の終わりはれロ2぀の行で瀺される。入力デヌタは以䞋の圢匏で䞎えられる。 M W bm 1 bm 2 ... bm M bw 1 bw 2 ... bw W 行目の M ず W (1 ≀ M,W ≀ 12) はそれぞれ雄ず雌の埮生物の数である。行目に、 i 番目の雄が䜓内に持぀粒子の量 bm i (0 ≀ bm i ≀ 50) が䞎えられる。行目に、 i 番目の雌が䜓内に持぀粒子の量 bw i (0 ≀ bw i ≀ 50) が䞎えられる。 デヌタセットの数は 20 を超えない。 出力 デヌタセットごずに、埮生物の集団から埗られる電気゚ネルギヌの最倧倀を1行に出力する。 入出力䟋 入力䟋 3 3 0 20 30 10 20 30 10 10 32 10 15 8 20 10 6 45 50 41 18 0 37 25 45 11 25 21 32 27 7 3 23 14 39 6 47 16 23 19 37 8 0 0 出力䟋 12000 53906 11629
35,695
Score : 900 points Problem Statement Joisino has a formula consisting of N terms: A_1 op_1 A_2 ... op_{N-1} A_N . Here, A_i is an integer, and op_i is an binary operator either + or - . Because Joisino loves large numbers, she wants to maximize the evaluated value of the formula by inserting an arbitrary number of pairs of parentheses (possibly zero) into the formula. Opening parentheses can only be inserted immediately before an integer, and closing parentheses can only be inserted immediately after an integer. It is allowed to insert any number of parentheses at a position. Your task is to write a program to find the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Constraints 1≩N≩10^5 1≩A_i≩10^9 op_i is either + or - . Input The input is given from Standard Input in the following format: N A_1 op_1 A_2 ... op_{N-1} A_N Output Print the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses. Sample Input 1 3 5 - 1 - 3 Sample Output 1 7 The maximum possible value is: 5 - (1 - 3) = 7 . Sample Input 2 5 1 - 2 + 3 - 4 + 5 Sample Output 2 5 The maximum possible value is: 1 - (2 + 3 - 4) + 5 = 5 . Sample Input 3 5 1 - 20 - 13 + 14 - 5 Sample Output 3 13 The maximum possible value is: 1 - (20 - (13 + 14) - 5) = 13 .
35,696
Score : 200 points Problem Statement We have a board with H horizontal rows and W vertical columns of squares. There is a bishop at the top-left square on this board. How many squares can this bishop reach by zero or more movements? Here the bishop can only move diagonally. More formally, the bishop can move from the square at the r_1 -th row (from the top) and the c_1 -th column (from the left) to the square at the r_2 -th row and the c_2 -th column if and only if exactly one of the following holds: r_1 + c_1 = r_2 + c_2 r_1 - c_1 = r_2 - c_2 For example, in the following figure, the bishop can move to any of the red squares in one move: Constraints 1 \leq H, W \leq 10^9 All values in input are integers. Input Input is given from Standard Input in the following format: H \ W Output Print the number of squares the bishop can reach. Sample Input 1 4 5 Sample Output 1 10 The bishop can reach the cyan squares in the following figure: Sample Input 2 7 3 Sample Output 2 11 The bishop can reach the cyan squares in the following figure: Sample Input 3 1000000000 1000000000 Sample Output 3 500000000000000000
35,697
Score : 300 points Problem Statement Joisino is planning to open a shop in a shopping street. Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods. There are already N stores in the street, numbered 1 through N . You are given information of the business hours of those shops, F_{i,j,k} . If F_{i,j,k}=1 , Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0 , Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1 , Tuesday: Day 2 , Wednesday: Day 3 , Thursday: Day 4 , Friday: Day 5 . Also, the morning is denoted as Period 1 , and the afternoon is denoted as Period 2 . Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N} . Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period. Constraints 1≀N≀100 0≀F_{i,j,k}≀1 For every integer i such that 1≀i≀N , there exists at least one pair (j,k) such that F_{i,j,k}=1 . -10^7≀P_{i,j}≀10^7 All input values are integers. Input Input is given from Standard Input in the following format: N F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2} : F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2} P_{1,0} ... P_{1,10} : P_{N,0} ... P_{N,10} Output Print the maximum possible profit of Joisino's shop. Sample Input 1 1 1 1 0 1 0 0 0 1 0 1 3 4 5 6 7 8 9 -2 -3 4 -2 Sample Output 1 8 If her shop is open only during the periods when Shop 1 is opened, the profit will be 8 , which is the maximum possible profit. Sample Input 2 2 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1 0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1 Sample Output 2 -2 Note that a shop must be open during at least one period, and the profit may be negative. Sample Input 3 3 1 1 1 1 1 1 0 0 1 1 0 1 0 1 1 1 1 0 1 0 1 0 1 1 0 1 0 1 0 1 -8 6 -2 -8 -8 4 8 7 -6 2 2 -9 2 0 1 7 -5 0 -2 -6 5 5 6 -6 7 -9 6 -5 8 0 -9 -7 -7 Sample Output 3 23
35,698
Problem I: ThreeRooks ねこがチェスの緎習をしおいる. ねこは, $X \times Y$ のチェス盀の䞊にルヌクを 3 ぀眮こうずしおいる. このチェス盀の $K$ 個のマス目にはうさぎが座っおいる. $i$ 匹目のうさぎの座暙は $(x[i], y[i])$ である. ただし, チェス盀の巊䞊端のマス目の座暙を $(0, 0)$, 右䞋端のマス目の座暙を $(X-1, Y-1)$ ずする。うさぎが座っおいる堎所にはルヌクを眮くこずができない. たた, 1 ぀のマス目に耇数個のルヌクを眮くこずはできない. どの 2 ぀のルヌクも互いに攻撃し合わないようにルヌクを3 ぀眮く方法は䜕通りあるか, mod 1,000,000,007 で求めよ. 2 ぀のルヌクは同じ行たたは同じ列にあり, 間にうさぎが座っおいない堎合に互いに攻撃しあうものずする. Constraints $X$, $Y$ will be between 1 and 1,000,000,000, inclusive. $K$ will be between 1 and 100,000, inclusive. $x_i$ will be between 0 and $X-1$, inclusive. $y_i$ will be between 0 and $Y-1$, inclusive. No two rabbits sit on the same cell. Input 入力は以䞋の圢匏で䞎えられる: $X$ $Y$ $K$ $x_1$ $y_1$ ... $x_K$ $y_K$ Output ルヌクの配眮の個数を 1,000,000,007 で割ったあたりを衚す敎数を 1 行に出力せよ. Sample Input 1 3 3 1 0 0 Sample Output 1 4 Sample Input 2 5 8 2 2 2 4 5 Sample Output 2 3424
35,699