task
stringlengths
0
154k
__index_level_0__
int64
0
39.2k
Score : 600 points Problem Statement There are N cities in Republic of AtCoder. The size of the i -th city is A_{i} . Takahashi would like to build N-1 bidirectional roads connecting two cities so that any city can be reached from any other city by using these roads. Assume that the cost of building a road connecting the i -th city and the j -th city is |i-j| \times D + A_{i} + A_{j} . For Takahashi, find the minimum possible total cost to achieve the objective. Constraints 1 \leq N \leq 2 \times 10^5 1 \leq D \leq 10^9 1 \leq A_{i} \leq 10^9 A_{i} and D are integers. Input Input is given from Standard Input in the following format: N D A_1 A_2 ... A_N Output Print the minimum possible total cost. Sample Input 1 3 1 1 100 1 Sample Output 1 106 This cost can be achieved by, for example, building roads connecting City 1 , 2 and City 1 , 3 . Sample Input 2 3 1000 1 100 1 Sample Output 2 2202 Sample Input 3 6 14 25 171 7 1 17 162 Sample Output 3 497 Sample Input 4 12 5 43 94 27 3 69 99 56 25 8 15 46 8 Sample Output 4 658
35,700
Score : 2100 points Problem Statement We have a rectangular parallelepiped of dimensions A×B×C , divided into 1×1×1 small cubes. The small cubes have coordinates from (0, 0, 0) through (A-1, B-1, C-1) . Let p , q and r be integers. Consider the following set of abc small cubes: \{(\ (p + i) mod A , (q + j) mod B , (r + k) mod C\ ) | i , j and k are integers satisfying 0 ≀ i < a , 0 ≀ j < b , 0 ≀ k < c \} A set of small cubes that can be expressed in the above format using some integers p , q and r , is called a torus cuboid of size a×b×c . Find the number of the sets of torus cuboids of size a×b×c that satisfy the following condition, modulo 10^9+7 : No two torus cuboids in the set have intersection. The union of all torus cuboids in the set is the whole rectangular parallelepiped of dimensions A×B×C . Constraints 1 ≀ a < A ≀ 100 1 ≀ b < B ≀ 100 1 ≀ c < C ≀ 100 All input values are integers. Input Input is given from Standard Input in the following format: a b c A B C Output Print the number of the sets of torus cuboids of size a×b×c that satisfy the condition, modulo 10^9+7 . Sample Input 1 1 1 1 2 2 2 Sample Output 1 1 Sample Input 2 2 2 2 4 4 4 Sample Output 2 744 Sample Input 3 2 3 4 6 7 8 Sample Output 3 0 Sample Input 4 2 3 4 98 99 100 Sample Output 4 471975164
35,701
Problem A: Moduic Squares Have you ever heard of Moduic Squares? They are like 3 × 3 Magic Squares, but each of them has one extra cell called a moduic cell. Hence a Moduic Square has the following form. Figure 1: A Moduic Square Each of cells labeled from A to J contains one number from 1 to 10, where no two cells contain the same number. The sums of three numbers in the same rows, in the same columns, and in the diagonals in the 3 × 3 cells must be congruent modulo the number in the moduic cell. Here is an example Moduic Square: Figure 2: An example Moduic Square You can easily see that all the sums are congruent to 0 modulo 5. Now, we can consider interesting puzzles in which we complete Moduic Squares with partially filled cells. For example, we can complete the following square by filling 4 into the empty cell on the left and 9 on the right. Alternatively, we can also complete the square by filling 9 on the left and 4 on the right. So this puzzle has two possible solutions. Figure 3: A Moduic Square as a puzzle Your task is to write a program that determines the number of solutions for each given puzzle. Input The input contains multiple test cases. Each test case is specified on a single line made of 10 integers that represent cells A, B, C, D, E, F, G, H, I, and J as shown in the first figure of the problem statement. Positive integer means that the corresponding cell is already filled with the integer. Zero means that the corresponding cell is left empty. The end of input is identified with a line containing ten of -1’s. This is not part of test cases. Output For each test case, output a single integer indicating the number of solutions on a line. There may be cases with no solutions, in which you should output 0 as the number. Sample Input 3 1 6 8 10 7 0 0 2 5 0 0 0 0 0 0 0 0 0 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 Output for the Sample Input 2 362880
35,702
Score : 600 points Problem Statement There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1} . The initial term is A , and the common difference is B . That is, s_i = A + B \times i holds. Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519 . What is the remainder when that integer is divided by M ? Constraints All values in input are integers. 1 \leq L, A, B < 10^{18} 2 \leq M \leq 10^9 All terms in the arithmetic progression are less than 10^{18} . Input Input is given from Standard Input in the following format: L A B M Output Print the remainder when the integer obtained by concatenating the terms is divided by M . Sample Input 1 5 3 4 10007 Sample Output 1 5563 Our arithmetic progression is 3, 7, 11, 15, 19 , so the answer is 37111519 mod 10007 , that is, 5563 . Sample Input 2 4 8 1 1000000 Sample Output 2 891011 Sample Input 3 107 10000000000007 1000000000000007 998244353 Sample Output 3 39122908
35,703
Prime Numbers A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7. Write a program which reads a list of N integers and prints the number of prime numbers in the list. Input The first line contains an integer N , the number of elements in the list. N numbers are given in the following lines. Output Print the number of prime numbers in the given list. Constraints 1 ≀ N ≀ 10000 2 ≀ an element of the list ≀ 10 8 Sample Input 1 5 2 3 4 5 6 Sample Output 1 3 Sample Input 2 11 7 8 9 10 11 12 13 14 15 16 17 Sample Output 2 4
35,704
We don't wanna work! ACM is an organization of programming contests. The purpose of ACM does not matter to you. The only important thing is that workstyles of ACM members are polarized: each member is either a workhorse or an idle fellow. Each member of ACM has a motivation level. The members are ranked by their motivation levels: a member who has a higher motivation level is ranked higher. When several members have the same value of motivation levels, the member who joined ACM later have a higher rank. The top 20% highest ranked members work hard, and the other (80%) members never (!) work. Note that if 20% of the number of ACM members is not an integer, its fraction part is rounded down. You, a manager of ACM, tried to know whether each member is a workhorse or an idle fellow to manage ACM. Finally, you completed to evaluate motivation levels of all the current members. However, your task is not accomplished yet because the members of ACM are dynamically changed from day to day due to incoming and outgoing of members. So, you want to record transitions of members from workhorses to idle fellows, and vice versa. You are given a list of the current members of ACM and their motivation levels in chronological order of their incoming date to ACM. You are also given a list of incoming/outgoing of members in chronological order. Your task is to write a program that computes changes of workstyles of ACM members. Input The first line of the input contains a single integer $N$ ($1 \leq N \leq 50,000$) that means the number of initial members of ACM. The ($i$ + 1)-th line of the input contains a string $s_i$ and an integer $a_i$ ($0 \leq a_i \leq 10^5$), separated by a single space. $s_i$ means the name of the $i$-th initial member and $a_i$ means the motivation level of the $i$-th initial member. Each character of $s_i$ is an English letter, and $1 \leq |s_i| \leq 20$. Note that those $N$ lines are ordered in chronological order of incoming dates to ACM of each member. The ($N$ + 2)-th line of the input contains a single integer $M$ ($1 \leq M \leq 20,000$) that means the number of changes of ACM members. The ($N$ + 2 + $j$)-th line of the input contains information of the $j$-th incoming/outgoing member. When the $j$-th information represents an incoming of a member, the information is formatted as "$+ t_j b_j$", where $t_j$ is the name of the incoming member and $b_j$ ($0 \leq b_j \leq 10^5$) is his motivation level. On the other hand, when the $j$-th information represents an outgoing of a member, the information is formatted as "$- t_j$", where $t_j$ means the name of the outgoing member. Each character of $t_j$ is an English letter, and $1 \leq |t_j| \leq 20$. Note that uppercase letters and lowercase letters are distinguished. Note that those $M$ lines are ordered in chronological order of dates when each event happens. No two incoming/outgoing events never happen at the same time. No two members have the same name, but members who left ACM once may join ACM again. Output Print the log, a sequence of changes in chronological order. When each of the following four changes happens, you should print a message corresponding to the type of the change as follows: Member $name$ begins to work hard : "$name$ is working hard now." Member $name$ begins to not work : "$name$ is not working now." For each incoming/outgoing, changes happen in the following order: Some member joins/leaves. When a member joins, the member is added to either workhorses or idle fellows. Some member may change from a workhorse to an idle fellow and vice versa. Note that there are no cases such that two or more members change their workstyles at the same time. Sample Input 1 4 Durett 7 Gayles 3 Facenda 6 Daughtery 0 1 + Mccourtney 2 Output for the Sample Input 1 Mccourtney is not working now. Durett is working hard now. Initially, no member works because $4 \times 20$% $< 1$. When one member joins ACM, Durrett begins to work hard. Sample Input 2 3 Burdon 2 Orlin 8 Trumper 5 1 + Lukaszewicz 7 Output for the Sample Input 2 Lukaszewicz is not working now. No member works. Sample Input 3 5 Andy 3 Bob 4 Cindy 10 David 1 Emile 1 3 + Fred 10 - David + Gustav 3 Output for the Sample Input 3 Fred is working hard now. Cindy is not working now. Gustav is not working now. Sample Input 4 7 Laplant 5 Varnes 2 Warchal 7 Degregorio 3 Chalender 9 Rascon 5 Burdon 0 7 + Mccarroll 1 - Chalender + Orlin 2 + Chalender 1 + Marnett 10 - Chalender + Chalender 0 Output for the Sample Input 4 Mccarroll is not working now. Warchal is working hard now. Orlin is not working now. Chalender is not working now. Marnett is working hard now. Warchal is not working now. Chalender is not working now. Warchal is working hard now. Some member may repeat incoming and outgoing. Sample Input 5 4 Aoba 100 Yun 70 Hifumi 120 Hajime 50 2 - Yun - Aoba Output for the Sample Input 5 (blank)
35,705
Problem C: Die Game Life is not easy. Sometimes it is beyond your control. Now, as contestants of ACM ICPC, you might be just tasting the bitter of life. But don't worry! Do not look only on the dark side of life, but look also on the bright side. Life may be an enjoyable game of chance, like throwing dice. Do or die! Then, at last, you might be able to find the route to victory. This problem comes from a game using a die. By the way, do you know a die? It has nothing to do with "death." A die is a cubic object with six faces, each of which represents a different number from one to six and is marked with the corresponding number of spots. Since it is usually used in pair, "a die" is rarely used word. You might have heard a famous phrase "the die is cast," though. When a game starts, a die stands still on a flat table. During the game, the die is tumbled in all directions by the dealer. You will win the game if you can predict the number seen on the top face at the time when the die stops tumbling. Now you are requested to write a program that simulates the rolling of a die. For simplicity, we assume that the die neither slip nor jumps but just rolls on the table in four directions, that is, north, east, south, and west. At the beginning of every game, the dealer puts the die at the center of the table and adjusts its direction so that the numbers one, two, and three are seen on the top, north, and west faces, respectively. For the other three faces, we do not explicitly specify anything but tell you the golden rule: the sum of the numbers on any pair of opposite faces is always seven. Your program should accept a sequence of commands, each of which is either "north", "east", "south", or "west". A "north" command tumbles the die down to north, that is, the top face becomes the new north, the north becomes the new bottom, and so on. More precisely, the die is rotated around its north bottom edge to the north direction and the rotation angle is 9 degrees. Other commands also tumble the die accordingly to their own directions. Your program should calculate the number finally shown on the top after performing the commands in the sequence. Note that the table is sufficiently large and the die never falls off during the game. Input The input consists of one or more command sequences, each of which corresponds to a single game. The first line of a command sequence contains a positive integer, representing the number of the following command lines in the sequence. You may assume that this number is less than or equal to 1024. A line containing a zero indicates the end of the input. Each command line includes a command that is one of north, east, south, and west. You may assume that no white space occurs in any line. Output For each command sequence, output one line containing solely the number of the top face at the time when the game is finished. Sample Input 1 north 3 north east south 0 Output for the Sample Input 5 1
35,706
マトリョヌシカ マトリョヌシカはロシアの民芞品ずしお有名な人圢である マトリョヌシカは䞊䞋に分割でき開くず䞭により小さい別の人圢が入っおいる 珟れた小さい人圢を開くずさらに小さい人圢が入っおいるずいうような入れ子構造になっおいる あなたは旅行先で珍しい圢のマトリョヌシカを芋぀け N 䜓の人圢を賌入した i 番目の人圢の圢状は x i × y i × z i の盎方䜓である ひずしきりマトリョヌシカを鑑賞したあなたはマトリョヌシカを仕舞おうずしおいる その前にいく぀かの人圢を別の人圢に栌玍するこずによっお必芁なスペヌスを枛らしたい 人圢を栌玍する際にはただ䞭にひず぀も人圢を栌玍しおいない人圢にだけ他の人圢をひず぀栌玍できる ただし盎接的に栌玍される人圢に぀いおだけ数えるものずし䞭に人圢が入っおいる人圢を別の人圢に栌玍するこずはできる 収玍された人圢は倖郚から芋えない状態になる ただし以䞋の条件を満たさなければならない 人圢は回転しおよいが盎方䜓のそれぞれの蟺は他方の盎方䜓のいずれかの蟺に平行 回転埌察応する蟺同士の長さそれぞれに぀いお収玍される偎の人圢の長さの方が短い 1 個の人圢の䞭に盎接収玍できる人圢の数は高々 1 個 抌入れの容積は限られおいるので倖郚から芋えおいる人圢の䜓積の和を最小化したい あなたの仕事は人圢を収玍する操䜜を任意の回数繰り返しお達成できる倖郚から芋えおいる人圢の䜓積の和の最小倀を求めるプログラムを䜜成するこずである Input 入力は耇数のデヌタセットからなる デヌタセットの個数は最倧でも 50 個を超えない 各デヌタセットは次の圢匏で衚される N x 1 y 1 z 1 : : x N y N z N 各デヌタセットは N + 1 行からなりデヌタセットの 1 行目には人圢の数を衚す敎数 N が䞎えられる 続く N 行の内 i 行目には i 番目の人圢の倧きさを衚す 3 ぀の敎数 x i , y i , z i が半角スペヌス区切りで䞎えられる これらの敎数は 1 ≀ N, x i , y i , z i ≀ 100 を満たす 入力の終わりは 1 ぀のれロからなる行で衚される Output 各デヌタセットに぀いお倖郚から芋えおいる人圢の䜓積の和の最小倀を 1 行で出力せよ Sample Input 2 1 2 3 4 2 3 3 2 5 2 3 3 4 5 5 5 5 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 5 1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 10 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 0 Output for Sample Input 24 145 125 15 864
35,707
Score : 1300 points Problem Statement We have a graph with N vertices, numbered 0 through N-1 . Edges are yet to be added. We will process Q queries to add edges. In the i -th (1≩i≩Q) query, three integers A_i, B_i and C_i will be given, and we will add infinitely many edges to the graph as follows: The two vertices numbered A_i and B_i will be connected by an edge with a weight of C_i . The two vertices numbered B_i and A_i+1 will be connected by an edge with a weight of C_i+1 . The two vertices numbered A_i+1 and B_i+1 will be connected by an edge with a weight of C_i+2 . The two vertices numbered B_i+1 and A_i+2 will be connected by an edge with a weight of C_i+3 . The two vertices numbered A_i+2 and B_i+2 will be connected by an edge with a weight of C_i+4 . The two vertices numbered B_i+2 and A_i+3 will be connected by an edge with a weight of C_i+5 . The two vertices numbered A_i+3 and B_i+3 will be connected by an edge with a weight of C_i+6 . ... Here, consider the indices of the vertices modulo N . For example, the vertice numbered N is the one numbered 0 , and the vertice numbered 2N-1 is the one numbered N-1 . The figure below shows the first seven edges added when N=16, A_i=7, B_i=14, C_i=1 : After processing all the queries, find the total weight of the edges contained in a minimum spanning tree of the graph. Constraints 2≩N≩200,000 1≩Q≩200,000 0≩A_i,B_i≩N-1 1≩C_i≩10^9 Input The input is given from Standard Input in the following format: N Q A_1 B_1 C_1 A_2 B_2 C_2 : A_Q B_Q C_Q Output Print the total weight of the edges contained in a minimum spanning tree of the graph. Sample Input 1 7 1 5 2 1 Sample Output 1 21 The figure below shows the minimum spanning tree of the graph: Note that there can be multiple edges connecting the same pair of vertices. Sample Input 2 2 1 0 0 1000000000 Sample Output 2 1000000001 Also note that there can be self-loops. Sample Input 3 5 3 0 1 10 0 2 10 0 4 10 Sample Output 3 42
35,708
Score : 200 points Problem Statement We have N squares assigned the numbers 1,2,3,\ldots,N . Each square has an integer written on it, and the integer written on Square i is a_i . How many squares i satisfy both of the following conditions? The assigned number, i , is odd. The written integer is odd. Constraints All values in input are integers. 1 \leq N, a_i \leq 100 Input Input is given from Standard Input in the following format: N a_1 a_2 \cdots a_N Output Print the number of squares that satisfy both of the conditions. Sample Input 1 5 1 3 4 5 7 Sample Output 1 2 Two squares, Square 1 and 5 , satisfy both of the conditions. For Square 2 and 4 , the assigned numbers are not odd. For Square 3 , the written integer is not odd. Sample Input 2 15 13 76 46 15 50 98 93 77 31 43 84 90 6 24 14 Sample Output 2 3
35,709
電子蝿 ある蚈算機孊者が電子空間に棲息する電子蝿ずいう奇劙な生呜䜓を芋぀けたした。電子蝿の行動を芳察しおいるうちに、この空間の ( x , y , z ) 地点にいる電子蝿は、次に以䞋の芏則で瀺される ( x' , y' , z' )に移動するこずが分かりたした。 ただし、 a 1 , m 1 , a 2 , m 2 , a 3 , m 3 は電子蝿の個䜓ごずに定たる正の敎数です。 A mod B は正の敎数 A を正の敎数 B で割ったずきの䜙りです。 さらに芳察をするず、ある皮の電子蝿は (1,1,1) に眮いおからしばらくするず、必ず (1,1,1) に戻っおくるこずがわかりたした。このような蝿を戻り蝿ず名付けたした (1) 。 戻り蝿のデヌタを入力ずし、(1,1,1) に戻っおくる最小の移動回数 (>0) を出力するプログラムを䜜成しおください。なお 1< a 1 , m 1 , a 2 , m 2 , a 3 , m 3 < 2 15 ずしたす。 (1) a 1 ず m 1 , a 2 ず m 2 , a 3 ず m 3 がそれぞれ互いに玠 (公玄数が 1) である時に戻りたす。 Input 耇数のデヌタセットが䞎えられたす。各デヌタセットは以䞋の圢匏で䞎えられたす。 a 1 m 1 a 2 m 2 a 3 m 3 入力は぀の 0 を含む行で終わりたす。デヌタセットの数は 50 を超えたせん。 Output 各デヌタセットごずに、(1,1,1) に戻っおくる最小の移動回数(敎数)を行に出力しおください。 Sample Input 2 5 3 7 6 13 517 1024 746 6561 4303 3125 0 0 0 0 0 0 Output for the Sample Input 12 116640000
35,710
Problem F: Ghost Legs Problem 瞊線が本のあみだくじが$N$個ある。 どの線からスタヌトしおもスタヌトした線で終わるあみだくじを良いあみだくじずする。 あみだくじを぀以䞊遞んで瞊に自由な順序で぀なぐこずができる。 良いあみだくじを䜜るこずができるのであれば"yes"、そうでなければ"no"を出力せよ。 $i$番目のあみだくじには暪線が$w_i$本ある。 $a_{i,j}$はあみだくじ$i$の䞊から$j$番目の暪棒が䞭倮の瞊線から巊右どちらに䌞びおいるかを衚し、 $a_{i,j}$が0ならば巊に、1ならば右に䌞びおいるこずを衚す。 Input 入力は以䞋の圢匏で䞎えられる。 $N$ $w_1$ $a_{1,1}$ $a_{1,2}$ ... $a_{1,w_1}$ $w_2$ $a_{2,1}$ $a_{2,2}$ ... $a_{2,w_2}$ ... $w_N$ $a_{N,1}$ $a_{N,2}$ ... $a_{N,w_N}$ 入力はすべお敎数で䞎えられる。 1行目に$N$が䞎えられる。 続く$N$行に$w_i$ず$w_i$個の$a_{i,j}$が空癜区切りで䞎えられる。 Constraints 入力は以䞋の条件を満たす。 $1 \le N \le 50$ $0 \le w_i \le 100$ $a_{i,j}$は0たたは1 Output 良いあみだくじを䜜るこずができるのであれば"yes"、そうでなければ"no"を出力する。 Sample Input 1 3 2 0 0 1 1 5 1 0 0 1 0 Sample Output 1 yes Sample Input 2 2 1 1 1 0 Sample Output 2 no
35,711
F: Clique Drawing 問題文 $M$ 回操䜜を行っお $N$ 頂点のグラフを䜜りたす。 初め、$N$ 頂点のどの頂点間にも蟺は存圚したせん。 $i$ 番目の操䜜では、頂点集合 $\{ v_{i, 1}, v_{i, 2}, ... , v_{i, K_i} \}$ の党おの $2$ 頂点間に蟺を匵りたす。ただし、以前の操䜜によっお既に蟺が貌られおいる頂点間には匵りたせん。 たた、どの頂点も $M$ 回の操䜜の内高々 $2$ 回の操䜜にしか珟れたせん。 これらの操䜜によっお出来たグラフ䞊の、最倧独立集合の倧きさを求めおください。 無向グラフ $G = (V, E)$ の独立集合 $U$ ずは、$U \subseteq V$ なる頂点の郚分集合であっお、党おの $u, v \in U$ に぀いお、$(u, v) \notin E$ を満たすものを指したす。 最倧独立集合ずは、このような独立集合の内、最も倧きいものを指したす。 制玄 入力は党お敎数 $1 \leq N \leq 10^5$ $1 \leq M \leq 300$ $2 \leq K_i \leq N$ ($1 \leq i \leq M$) $1 \leq v_{i, j} \leq N$ ($1 \leq i \leq M, 1 \leq j \leq K_i$) $\{ v_{i, 1}, v_{i, 2}, ... , v_{i, K_i} \}$ の䞭に重耇した倀は存圚しない ($1 \leq i \leq M$) 党おの頂点は最倧でも2回の操䜜にしか含たれない 入力 入力は以䞋の圢匏で暙準入力から䞎えられたす。 $N$ $M$ $K_1$ $v_{1, 1}$ $\ldots$ $v_{1, K_1}$ $\vdots$ $K_M$ $v_{M, 1}$ $\ldots$ $v_{M, K_M}$ 出力 答えを䞀行で出力しおください。 入出力䟋 入力䟋1 2 1 2 1 2 出力䟋1 1 1回の操䜜の埌、2頂点からなる道が出来たす。 したがっお、最倧独立集合の倧きさは1です。 入力䟋2 4 1 3 1 2 3 出力䟋2 2 入力䟋3 6 3 3 2 3 5 3 3 4 6 3 1 2 4 出力䟋3 3
35,712
ロシアの旗 (Russian Flag) 問題 K 理事長はロシアで開催される IOI 2016 に合わせお旗を䜜るこずにしたK 理事長はたず倉庫から叀い旗を取り出しおきたこの旗は N 行 M 列のマス目に分けられおいおそれぞれのマスには癜・青・赀のいずれかの色が塗られおいる K 理事長はこの旗のいく぀かのマスを塗り替えお ロシアの旗 にしようずしおいるただしこの問題でいうロシアの旗ずは以䞋のようなものである 䞊から䜕行か (1 行以䞊) のマスが党お癜で塗られおいる 続く䜕行か (1 行以䞊) のマスが党お青で塗られおいる それ以倖の行 (1 行以䞊) のマスが党お赀で塗られおいる K 理事長が叀い旗をロシアの旗にするために塗り替える必芁のあるマスの個数の最小倀を求めよ 入力 入力は 1 + N 行からなる 1 行目には2 ぀の敎数 N, M (3 ≩ N ≩ 50, 3 ≩ M ≩ 50) が空癜を区切りずしお曞かれおいるこれは旗が N 行 M 列のマス目に区切られおいるこずを衚す 続く N 行にはそれぞれ M 文字からなる文字列が曞かれおおり叀い旗のマス目に塗られおいる色の情報を衚すN 行のうちの i 行目の j 文字目 (1 ≩ i ≩ N, 1 ≩ j ≩ M) は叀い旗のマス目の i 行目 j 列目のマスの色を衚す 'W', 'B', 'R' のいずれかの文字である 'W' は癜'B' は青'R' は赀を衚す 出力 K 理事長が叀い旗をロシアの旗にするために塗り替える必芁のあるマスの個数の最小倀を 1 行で出力せよ 入出力䟋 入力䟋 1 4 5 WRWRW BWRWB WRWRW RWBWR 出力䟋 1 11 入力䟋 2 6 14 WWWWWWWWWWWWWW WBBBWWRRWWBBBW WWBWWRRRRWWBWW BWBWWRRRRWWBWW WBBWWWRRWWBBBW WWWWWWWWWWWWWW 出力䟋 2 44 入出力䟋 1 においお叀い旗には䞋図のように色が塗られおいる 䞋図においお'X' の曞かれた 11 個のマスを塗り替える これにより䞋図のようなロシアの旗にするこずができる 11 個未満のマスを塗り替えるこずではロシアの旗にするこずはできないため11 を出力する 入出力䟋 2 においおは叀い旗には䞋図のように色が塗られおいる 情報オリンピック日本委員䌚䜜 『第 15 回日本情報オリンピック JOI 2015/2016 予遞競技課題』
35,713
Score : 400 points Problem Statement A maze is composed of a grid of H \times W squares - H vertical, W horizontal. The square at the i -th row from the top and the j -th column from the left - (i,j) - is a wall if S_{ij} is # and a road if S_{ij} is . . There is a magician in (C_h,C_w) . He can do the following two kinds of moves: Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in. Move B: Use magic to warp himself to a road square in the 5\times 5 area centered at the square he is currently in. In either case, he cannot go out of the maze. At least how many times does he need to use the magic to reach (D_h, D_w) ? Constraints 1 \leq H,W \leq 10^3 1 \leq C_h,D_h \leq H 1 \leq C_w,D_w \leq W S_{ij} is # or . . S_{C_h C_w} and S_{D_h D_w} are . . (C_h,C_w) \neq (D_h,D_w) Input Input is given from Standard Input in the following format: H W C_h C_w D_h D_w S_{11}\ldots S_{1W} \vdots S_{H1}\ldots S_{HW} Output Print the minimum number of times the magician needs to use the magic. If he cannot reach (D_h,D_w) , print -1 instead. Sample Input 1 4 4 1 1 4 4 ..#. ..#. .#.. .#.. Sample Output 1 1 For example, by walking to (2,2) and then using the magic to travel to (4,4) , just one use of magic is enough. Note that he cannot walk diagonally. Sample Input 2 4 4 1 4 4 1 .##. #### #### .##. Sample Output 2 -1 He cannot move from there. Sample Input 3 4 4 2 2 3 3 .... .... .... .... Sample Output 3 0 No use of magic is needed. Sample Input 4 4 5 1 2 2 5 #.### ####. #..## #..## Sample Output 4 2
35,714
Score : 200 points Problem Statement You are given two positive integers A and B . Compare the magnitudes of these numbers. Constraints 1 ≀ A, B ≀ 10^{100} Neither A nor B begins with a 0 . Input Input is given from Standard Input in the following format: A B Output Print GREATER if A>B , LESS if A<B and EQUAL if A=B . Sample Input 1 36 24 Sample Output 1 GREATER Since 36>24 , print GREATER . Sample Input 2 850 3777 Sample Output 2 LESS Sample Input 3 9720246 22516266 Sample Output 3 LESS Sample Input 4 123456789012345678901234567890 234567890123456789012345678901 Sample Output 4 LESS
35,715
Problem Statement Circles Island is known for its mysterious shape: it is a completely flat island with its shape being a union of circles whose centers are on the $x$-axis and their inside regions. The King of Circles Island plans to build a large square on Circles Island in order to celebrate the fiftieth anniversary of his accession. The King wants to make the square as large as possible. The whole area of the square must be on the surface of Circles Island, but any area of Circles Island can be used for the square. He also requires that the shape of the square is square (of course!) and at least one side of the square is parallel to the $x$-axis. You, a minister of Circles Island, are now ordered to build the square. First, the King wants to know how large the square can be. You are given the positions and radii of the circles that constitute Circles Island. Answer the side length of the largest possible square. $N$ circles are given in an ascending order of their centers' $x$-coordinates. You can assume that for all $i$ ($1 \le i \le N-1$), the $i$-th and $(i+1)$-st circles overlap each other. You can also assume that no circles are completely overlapped by other circles. [fig.1 : Shape of Circles Island and one of the largest possible squares for test case #1 of sample input] Input The input consists of multiple datasets. The number of datasets does not exceed $30$. Each dataset is formatted as follows. $N$ $X_1$ $R_1$ : : $X_N$ $R_N$ The first line of a dataset contains a single integer $N$ ($1 \le N \le 50{,}000$), the number of circles that constitute Circles Island. Each of the following $N$ lines describes a circle. The $(i+1)$-st line contains two integers $X_i$ ($-100{,}000 \le X_i \le 100{,}000$) and $R_i$ ($1 \le R_i \le 100{,}000$). $X_i$ denotes the $x$-coordinate of the center of the $i$-th circle and $R_i$ denotes the radius of the $i$-th circle. The $y$-coordinate of every circle is $0$, that is, the center of the $i$-th circle is at ($X_i$, $0$). You can assume the followings. For all $i$ ($1 \le i \le N-1$), $X_i$ is strictly less than $X_{i+1}$. For all $i$ ($1 \le i \le N-1$), the $i$-th circle and the $(i+1)$-st circle have at least one common point ($X_{i+1} - X_i \le R_i + R_{i+1}$). Every circle has at least one point that is not inside or on the boundary of any other circles. The end of the input is indicated by a line containing a zero. Output For each dataset, output a line containing the side length of the square with the largest area. The output must have an absolute or relative error at most $10^{-4}$. Sample Input 2 0 8 10 8 2 0 7 10 7 0 Output for the Sample Input 12.489995996796796 9.899494936611665
35,716
Problem G: True Liars After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ashore on a foggy island. Though he was exhausted and despaired, he was still fortunate to remember a legend of the foggy island, which he had heard from patriarchs in his childhood. This must be the island in the legend. In the legend, two tribes have inhabited the island, one is divine and the other is devilish; once members of the divine tribe bless you, your future is bright and promising, and your soul will eventually go to Heaven; in contrast, once members of the devilish tribe curse you, your future is bleak and hopeless, and your soul will eventually fall down to Hell. In order to prevent the worst-case scenario, Akira should distinguish the devilish from the divine. But how? They looked exactly alike and he could not distinguish one from the other solely by their appearances. He still had his last hope, however. The members of the divine tribe are truth-tellers, that is, they always tell the truth and those of the devilish tribe are liars, that is, they always tell a lie. He asked some of the whether or not some are divine. They knew one another very much and always responded to him "faithfully" according to their individual natures (i.e., they always tell the truth or always a lie). He did not dare to ask any other forms of questions, since the legend says that a devilish member would curse a person forever when he did not like the question. He had another piece of useful information: the legend tells the populations of both tribes. These numbers in the legend are trustworthy since everyone living on this island is immortal and none have ever been born at least these millennia. You are a good computer programmer and so requested to help Akira by writing a program that classifies the inhabitants according to their answers to his inquiries. Input The input consists of multiple data sets, each in the following format: n p 1 p 2 x 1 y 1 a 1 x 2 y 2 a 2 ... x i y i a i ... x n y n a n The first line has three non-negative integers n , p 1 , and p 2 . n is the number of questions Akira asked. p 1 and p 2 are the populations of the divine and devilish tribes, respectively, in the legend. Each of the following n lines has two integers x i , y i and one word a i . x i and y i are the identification numbers of inhabitants, each of which is between 1 and p 1 + p 2 , inclusive. a i is either "yes", if the inhabitant x i said that the inhabitant y i was a member of the divine tribe, or "no", otherwise. Note that x i and y i can be the same number since "are you a member of the divine tribe?" is a valid question. Note also that two lines may have the same x 's and y 's since Akira was very upset and might have asked the same question to the same one more than once. You may assume that n is less than 1000 and that p 1 and p 2 are less than 300. A line with three zeros, i.e., "0 0 0", represents the end of the input. You can assume that each data set is consistent and no contradictory answers are included. Output For each data set, if it includes sufficient information to classify all the inhabitants, print the identification numbers of all the divine ones in ascending order, one in a line. In addition, following the output numbers, print "end" in a line. Otherwise, i.e., if a given data set does not include sufficient information to identify all the divine members, print "no" in a line. Sample Input 2 1 1 1 2 no 2 1 no 3 2 1 1 1 yes 2 2 yes 3 3 yes 2 2 1 1 2 yes 2 3 no 5 4 3 1 2 yes 1 3 no 4 5 yes 5 6 yes 6 7 no 0 0 0 Output for the Sample Input no no 1 2 end 3 4 5 6 end
35,717
Score : 1600 points Problem Statement There is a directed graph G with N vertices and M edges. The vertices are numbered 1 through N , and the edges are numbered 1 through M . Edge i is directed from x_i to y_i . Here, x_i < y_i holds. Also, there are no multiple edges in G . Consider selecting a subset of the set of the M edges in G , and removing these edges from G to obtain another graph G' . There are 2^M different possible graphs as G' . Alice and Bob play against each other in the following game played on G' . First, place two pieces on vertices 1 and 2 , one on each. Then, starting from Alice, Alice and Bob alternately perform the following operation: Select an edge i such that there is a piece placed on vertex x_i , and move the piece to vertex y_i (if there are two pieces on vertex x_i , only move one). The two pieces are allowed to be placed on the same vertex. The player loses when he/she becomes unable to perform the operation. We assume that both players play optimally. Among the 2^M different possible graphs as G' , how many lead to Alice's victory? Find the count modulo 10^9+7 . Constraints 2 ≀ N ≀ 15 1 ≀ M ≀ N(N-1)/2 1 ≀ x_i < y_i ≀ N All (x_i,\ y_i) are distinct. Input Input is given from Standard Input in the following format: N M x_1 y_1 x_2 y_2 : x_M y_M Output Print the number of G' that lead to Alice's victory, modulo 10^9+7 . Sample Input 1 2 1 1 2 Sample Output 1 1 The figure below shows the two possible graphs as G' . A graph marked with ○ leads to Alice's victory, and a graph marked with × leads to Bob's victory. Sample Input 2 3 3 1 2 1 3 2 3 Sample Output 2 6 The figure below shows the eight possible graphs as G' . Sample Input 3 4 2 1 3 2 4 Sample Output 3 2 Sample Input 4 5 10 2 4 3 4 2 5 2 3 1 2 3 5 1 3 1 5 4 5 1 4 Sample Output 4 816
35,718
Score : 1800 points Problem Statement We have a square grid with N rows and M columns. Takahashi will write an integer in each of the squares, as follows: First, write 0 in every square. For each i=1,2,...,N , choose an integer k_i (0\leq k_i\leq M) , and add 1 to each of the leftmost k_i squares in the i -th row. For each j=1,2,...,M , choose an integer l_j (0\leq l_j\leq N) , and add 1 to each of the topmost l_j squares in the j -th column. Now we have a grid where each square contains 0 , 1 , or 2 . Find the number of different grids that can be made this way, modulo 998244353 . We consider two grids different when there exists a square with different integers. Constraints 1 \leq N,M \leq 5\times 10^5 N and M are integers. Input Input is given from Standard Input in the following format: N M Output Print the number of different grids that can be made, modulo 998244353 . Sample Input 1 1 2 Sample Output 1 8 Let (a,b) denote the grid where the square to the left contains a and the square to the right contains b . Eight grids can be made: (0,0),(0,1),(1,0),(1,1),(1,2),(2,0),(2,1), and (2,2) . Sample Input 2 2 3 Sample Output 2 234 Sample Input 3 10 7 Sample Output 3 995651918 Sample Input 4 314159 265358 Sample Output 4 70273732
35,719
Score : 1700 points Problem Statement Snuke has a rooted tree with N vertices, numbered 1 through N . Vertex 1 is the root of the tree, and the parent of Vertex i ( 2\leq i \leq N ) is Vertex P_i ( P_i < i ). There is a number, 0 or 1 , written on each vertex. The number written on Vertex i is V_i . Snuke would like to arrange the vertices of this tree in a horizontal row. Here, for every vertex, there should be no ancestor of that vertex to the right of that vertex. After arranging the vertices, let X be the sequence obtained by reading the numbers written on the vertices from left to right in the arrangement. Snuke would like to minimize the inversion number of X . Find the minimum possible inversion number of X . Notes The inversion number of a sequence Z whose length N is the number of pairs of integers i and j ( 1 \leq i < j \leq N ) such that Z_i > Z_j . Constraints 1 \leq N \leq 2 \times 10^5 1 \leq P_i < i ( 2 \leq i \leq N ) 0 \leq V_i \leq 1 ( 1 \leq i \leq N ) All values in input are integers. Input Input is given from Standard Input in the following format: N P_2 P_3 ... P_N V_1 V_2 ... V_N Output Print the minimum possible inversion number of X . Sample Input 1 6 1 1 2 3 3 0 1 1 0 0 0 Sample Output 1 4 When the vertices are arranged in the order 1, 3, 5, 6, 2, 4 , X will be (0, 1, 0, 0, 1, 0) , whose inversion number is 4 . It is impossible to have fewer inversions, so the answer is 4 . Sample Input 2 1 0 Sample Output 2 0 X = (0) , whose inversion number is 0 . Sample Input 3 15 1 2 3 2 5 6 2 2 9 10 1 12 13 12 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 Sample Output 3 31
35,720
Problem G: Nurie 玙の䞊に円が n 個曞かれおいる. うさぎは k 色の絵の具を持っおいお, 次のルヌルに埓っお玙に色を塗っおいく. 各領域をある1 色の絵の具で塗るか, 䜕も塗らない. ここで「領域」ずは, 円匧の集合で囲たれた面積有限の郚分を指す. 隣り合っおいる2 ぀の領域を同じ色の絵の具で塗るこずはできない. ここで「隣り合っおいる」ずは,境界の䞀郚を共有する円匧があるこずを指す. 共有点が有限個である2 ぀の領域は, 隣り合っおいるずはみなされない. 隣り合っおいる2 ぀の領域の䞡方を塗らないたたにするこずは蚱される. うさぎはできるだけ倚くの領域を絵の具で塗りたい. 塗れる領域の個数の最倧倀を求めよ. Input 1 行目: n k (1 ≀ n ≀ 20, 1 ≀ k ≀ 1 000 000 000) 2-( n + 1) 行目: x i y i r i , (−1 000 ≀ x i , y i ≀ 1 000, 1 ≀ r i ≀ 1 000) (敎数) どの2 ぀の円も䞀臎しない. いずれか2 円の亀点ずしお埗られる点集合は, 距離が10 −3 以䞋の異なる2 点を含たない. Output 絵の具で塗れる領域の個数の最倧倀を䞀行に出力せよ. Sample Input 1 2 1 10 0 10 20 0 10 Sample Output 1 2
35,721
Problem G: Get Lost ICPC World Finals 5日目 ティヌ氏はR囜の街で迷っおしたった。 困ったこずに、街䞊みが䌌おいるため、自分が今どこにいるかが党く分からない。 R囜は「おそろしあ」であるから、敵に襲撃される前にホテルぞ戻らねばならない。 幞い、どのように曲がっお来たかだけは芚えおいるので、 行き圓たりばったりに進んでみよう。 問題 \( w \times h \)マスの二次元栌子状のマップがある。 巊䞊のマスの座暙を\( (1, 1) \)、右䞋のマスの座暙を\( (w, h) \)ずする。 マップの呚りは壁で囲たれおおり、 \(n\)個の障害物が座暙\( (x_{i}, y_{i}) (1 \leq i \leq n) \)に眮かれおいる。 さらに、呜什列\( r_{1}r_{2} \cdots r_{m} \)ず目的座暙\( (g_{x}, g_{y}) \)が䞎えられる。 開始座暙ず進行方向の蚭定埌、歩行者は以䞋の移動芏則に埓っお歩行を開始する。 1. カりンタを\( i=1 \)に初期化し、開始座暙ず進行方向䞊・䞋・巊・右を蚭定する。 2. 壁・障害物に出䌚うたで盎進する。 3. 壁・障害物の手前で次のいずれかを実行する。 3. a. \( i>m \)、すなわち呜什が無ければ歩行を終了する。 3. b. 呜什\( r_{i} \)がLならば巊、Rならば右ぞ方向転換する。 4. \( i \)をむンクリメントする。 5. 2.に戻る。 歩行開始から歩行終了たでに通過した座暙開始座暙を含むに目的座暙\( (g_{x}, g_{y}) \)が含たれれば、歩行者が目的地に蟿り着いたず解釈する。 目的地に蟿り着くこずのできる開始座暙・進行方向の組み合わせは䜕通りあるかを求めよ。 ただし、開始座暙には障害物やマップ倖の座暙を蚭定するこずはできないずする。 目的座暙を蚭定するこずはできる。 入力 w h g x g y n x 1 y 1 
 x n y n r 1 r 2 
 r m 1行目に マップの暪幅\(w\)、瞊幅\(h\)、目的地のx座暙\( g_{x} \)、目的地のy座暙\( g_{y} \)、障害物の数\(n\)が空癜区切りで䞎えられる。 2行目から\( n+1 \)行目に 各障害物の座暙\( (x_{i}, y_{i}) \)が空癜区切りで䞎えられる。 \( n+2 \)行目に長さ\(m\)の呜什列\( r_{1}r_{2} \cdots r_{m} \)が䞎えられる。 出力 目的地に蟿り着けるような 開始座暙・進行方向の組み合わせの数を1行に出力せよ。 制玄 入力は党お敎数である \( 2 \leq w, h \leq 10^{5}(= 100000) \) \( 0 \leq n \leq 10^{5}(= 100000) \) \( 1 \leq g_{x}, x_{i} \leq w \) \( 1 \leq g_{y}, y_{i} \leq h \) \( (x_{i}, y_{i}) \not = (g_{x}, g_{y}) (1 \leq i \leq n) \) \( (x_{i}, y_{i}) \not = (x_{j}, y_{j}) (i \not = j) \) \( r_{i} \in \{ L, R \} (1 \leq i \leq m) \) \( 1 \leq m \leq 20 \) 入出力䟋 入力1 3 3 3 2 2 1 1 2 2 RL 出力1 9 以䞋の5通りの歩行に加え、 目的座暙を開始座暙ずする歩行4通りが目的座暙を通過する。 入力2 4 4 3 1 4 1 1 3 2 1 3 4 3 RR 出力2 13 以䞋の開始座暙・進行方向から開始する歩行は目的座暙を通過する。 入力3 100000 100000 46597 49716 17 38713 77141 46598 78075 66177 49715 58569 77142 48303 12742 32829 65105 32830 78076 70273 27408 48302 21196 27119 54458 38714 65104 46598 54457 27118 12743 60242 21197 60241 1101 58568 27409 93581 1100 LLRRLLLLRRLL 出力3 647505
35,722
Problem G Placing Medals on a Binary Tree You have drawn a chart of a perfect binary tree, like one shown in Figure G.1. The figure shows a finite tree, but, if needed, you can add more nodes beneath the leaves, making the tree arbitrarily deeper. Figure G.1. A Perfect Binary Tree Chart Tree nodes are associated with their depths, defined recursively. The root has the depth of zero, and the child nodes of a node of depth d have their depths $d + 1$. You also have a pile of a certain number of medals, each engraved with some number. You want to know whether the medals can be placed on the tree chart satisfying the following conditions. A medal engraved with $d$ should be on a node of depth $d$. One tree node can accommodate at most one medal. The path to the root from a node with a medal should not pass through another node with a medal. You have to place medals satisfying the above conditions, one by one, starting from the top of the pile down to its bottom. If there exists no placement of a medal satisfying the conditions, you have to throw it away and simply proceed to the next medal. You may have choices to place medals on different nodes. You want to find the best placement. When there are two or more placements satisfying the rule, one that places a medal upper in the pile is better. For example, when there are two placements of four medal, one that places only the top and the 2nd medal, and the other that places the top, the 3rd, and the 4th medal, the former is better. In Sample Input 1, you have a pile of six medals engraved with 2, 3, 1, 1, 4, and 2 again respectively, from top to bottom. The first medal engraved with 2 can be placed, as shown in Figure G.2 (A). Then the second medal engraved with 3 may be placed , as shown in Figure G.2 (B). The third medal engraved with 1 cannot be placed if the second medal were placed as stated above, because both of the two nodes of depth 1 are along the path to the root from nodes already with a medal. Replacing the second medal satisfying the placement conditions, however, enables a placement shown in Figure G.2 (C). The fourth medal, again engraved with 1, cannot be placed with any replacements of the three medals already placed satisfying the conditions. This medal is thus thrown away. The fifth medal engraved with 4 can be placed as shown in of Figure G.2 (D). The last medal engraved with 2 cannot be placed on any of the nodes with whatever replacements. Figure G.2. Medal Placements Input The input consists of a single test case in the format below. $n$ $x_1$ . . . $x_n$ The first line has $n$, an integer representing the number of medals ($1 \leq n \leq 5 \times 10^5$). The following $n$ lines represent the positive integers engraved on the medals. The $i$-th line of which has an integer $x_i$ ($1 \leq x_i \leq 10^9$) engraved on the $i$-th medal of the pile from the top. Output When the best placement is chosen, for each i from 1 through n, output Yes in a line if the i-th medal is placed; otherwise, output No in a line. Sample Input 1 6 2 3 1 1 4 2 Sample Output 1 Yes Yes Yes No Yes No Sample Input 2 10 4 4 4 4 4 4 4 4 1 4 Sample Output 2 Yes Yes Yes Yes Yes Yes Yes Yes Yes No
35,723
Score : 300 points Problem Statement You are given three integers A , B and C . Find the minimum number of operations required to make A , B and C all equal by repeatedly performing the following two kinds of operations in any order: Choose two among A , B and C , then increase both by 1 . Choose one among A , B and C , then increase it by 2 . It can be proved that we can always make A , B and C all equal by repeatedly performing these operations. Constraints 0 \leq A,B,C \leq 50 All values in input are integers. Input Input is given from Standard Input in the following format: A B C Output Print the minimum number of operations required to make A , B and C all equal. Sample Input 1 2 5 4 Sample Output 1 2 We can make A , B and C all equal by the following operations: Increase A and C by 1 . Now, A , B , C are 3 , 5 , 5 , respectively. Increase A by 2 . Now, A , B , C are 5 , 5 , 5 , respectively. Sample Input 2 2 6 3 Sample Output 2 5 Sample Input 3 31 41 5 Sample Output 3 23
35,724
Problem D: Speed Do you know Speed ? It is one of popular card games, in which two players compete how quick they can move their cards to tables. To play Speed , two players sit face-to-face first. Each player has a deck and a tableau assigned for him, and between them are two tables to make a pile on, one in left and one in right. A tableau can afford up to only four cards. There are some simple rules to carry on the game: A player is allowed to move a card from his own tableau onto a pile, only when the rank of the moved card is a neighbor of that of the card on top of the pile. For example A and 2, 4 and 3 are neighbors. A and K are also neighbors in this game. He is also allowed to draw a card from the deck and put it on a vacant area of the tableau. If both players attempt to move cards on the same table at the same time, only the faster player can put a card on the table. The other player cannot move his card to the pile (as it is no longer a neighbor of the top card), and has to bring it back to his tableau. First each player draws four cards from his deck, and places them face on top on the tableau. In case he does not have enough cards to fill out the tableau, simply draw as many as possible. The game starts by drawing one more card from the deck and placing it on the tables on their right simultaneously. If the deck is already empty, he can move an arbitrary card on his tableau to the table. Then they continue performing their actions according to the rule described above until both of them come to a deadend, that is, have no way to move cards. Every time a deadend occurs, they start over from each drawing a card (or taking a card from his or her tableau) and placing on his or her right table, regardless of its face. The player who has run out of his card first is the winner of the game. Mr. James A. Games is so addicted in this simple game, and decided to develop robots that plays it. He has just completed designing the robots and their program, but is not sure if they work well, as the design is so complicated. So he asked you, a friend of his, to write a program that simulates the robots. The algorithm for the robots is designed as follows: A robot draws cards in the order they are dealt. Each robot is always given one or more cards. In the real game of Speed , the players first classify cards by their colors to enable them to easily figure out which player put the card. But this step is skipped in this simulation. The game uses only one card set split into two. In other words, there appears at most one card with the same face in the two decks given to the robots. As a preparation, each robot draws four cards, and puts them to the tableau from right to left. If there are not enough cards in its deck, draw all cards in the deck. After this step has been completed on both robots, they synchronize to each other and start the game by putting the first cards onto the tables in the same moment. If there remains one or more cards in the deck, a robot draws the top one and puts it onto the right table. Otherwise, the robot takes the rightmost card from its tableau. Then two robots continue moving according to the basic rule of the game described above, until neither of them can move cards anymore. When a robot took a card from its tableau, it draws a card (if possible) from the deck to fill the vacant position after the card taken is put onto a table. It takes some amount of time to move cards. When a robot completes putting a card onto a table while another robot is moving to put a card onto the same table, the robot in motion has to give up the action immediately and returns the card to its original position. A robot can start moving to put a card on a pile at the same time when the neighbor is placed on the top of the pile. If two robots try to put cards onto the same table at the same moment, only the robot moving a card to the left can successfully put the card, due to the position settings. When a robot has multiple candidates in its tableau, it prefers the cards which can be placed on the right table to those which cannot. In case there still remain multiple choices, the robot prefers the weaker card. When it comes to a deadend situation, the robots start over from each putting a card to the table, then continue moving again according to the algorithm above. When one of the robots has run out the cards, i.e., moved all dealt cards, the game ends. The robot which has run out the cards wins the game. When both robots run out the cards at the same moment, the robot which moved the stronger card in the last move wins. The strength among the cards is determined by their ranks, then by the suits. The ranks are strong in the following order: A > K > Q > J > X (10) > 9 > . . . > 3 > 2. The suits are strong in the following order: S (Spades) > H (Hearts) > D (Diamonds) > C (Cloves). In other words, SA is the strongest and C2 is the weakest. The robots require the following amount of time to complete each action: 300 milliseconds to draw a card to the tableau, 500 milliseconds to move a card to the right table, 700 milliseconds to move a card to the left table, and 500 milliseconds to return a card to its original position. Cancelling an action always takes the constant time of 500ms, regardless of the progress of the action being cancelled. This time is counted from the exact moment when the action is interrupted, not the beginning time of the action. You may assume that these robots are well-synchronized, i.e., there is no clock skew between them. For example, suppose Robot A is given the deck “S3 S5 S8 S9 S2” and Robot B is given the deck “H7 H3 H4”, then the playing will be like the description below. Note that, in the description, “the table A” (resp. “the table B”) denotes the right table for Robot A (resp. Robot B). Robot A draws four cards S3, S5, S8, and S9 to its tableau from right to left. Robot B draws all the three cards H7, H3, and H4. Then the two robots synchronize for the game start. Let this moment be 0ms. At the same moment, Robot A starts moving S2 to the table A from the deck, and Robot B starts moving H7 to the table B from the tableau. At 500ms, the both robots complete their moving cards. Then Robot A starts moving S3 to the table A 1(which requires 500ms), and Robot B starts moving H3 also to the table A (which requires 700ms). At 1000ms, Robot A completes putting S3 on the table A. Robot B is interrupted its move and starts returning H3 to the tableau (which requires 500ms). At the same time Robot A starts moving S8 to the table B (which requires 700ms). At 1500ms, Robot B completes returning H3 and starts moving H4 to the table A (which requires 700ms). At 1700ms, Robot A completes putting S8 and starts moving S9 to the table B. At 2200ms, Robot B completes putting H4 and starts moving H3 to the table A. At 2400ms, Robot A completes putting S9 and starts moving S5 to the table A. At 2900ms, The both robots are to complete putting the cards on the table A. Since Robot B is moving the card to the table left to it, Robot B completes putting H3. Robot A is interrupted. Now Robot B has finished moving all the dealt cards, so Robot B wins this game. Input The input consists of multiple data sets, each of which is described by four lines. The first line of each data set contains an integer N A , which specifies the number of cards to be dealt as a deck to Robot A. The next line contains a card sequences of length N A . Then the number N B and card sequences of length N B for Robot B follows, specified in the same manner. In a card sequence, card specifications are separated with one space character between them. Each card specification is a string of 2 characters. The first character is one of ‘S’ (spades), ‘H’ (hearts), ‘D’ (diamonds) or ‘C’ (cloves) and specifies the suit. The second is one of ‘A’, ‘K’, ‘Q’, ‘J’, ‘X’ (for 10) or a digit between ‘9’ and ‘2’, and specifies the rank. As this game is played with only one card set, there is no more than one card of the same face in each data set. The end of the input is indicated by a single line containing a zero. Output For each data set, output the result of a game in one line. Output “A wins.” if Robot A wins, or output “B wins.” if Robot B wins. No extra characters are allowed in the output. Sample Input 1 SA 1 C2 2 SA HA 2 C2 C3 5 S3 S5 S8 S9 S2 3 H7 H3 H4 10 H7 CJ C5 CA C6 S2 D8 DA S6 HK 10 C2 D6 D4 H5 DJ CX S8 S9 D3 D5 0 Output for the Sample Input A wins. B wins. B wins. A wins.
35,725
Score : 200 points Problem Statement Let w be a string consisting of lowercase letters. We will call w beautiful if the following condition is satisfied: Each lowercase letter of the English alphabet occurs even number of times in w . You are given the string w . Determine if w is beautiful. Constraints 1 \leq |w| \leq 100 w consists of lowercase letters ( a - z ). Input The input is given from Standard Input in the following format: w Output Print Yes if w is beautiful. Print No otherwise. Sample Input 1 abaccaba Sample Output 1 Yes a occurs four times, b occurs twice, c occurs twice and the other letters occur zero times. Sample Input 2 hthth Sample Output 2 No
35,726
敎数の差 8 個の 0 から 9 たでの数字を入力したずき、その 8 個の数字を䞊べ替えおできる、最倧の敎数ず最小の敎数の差を出力するプログラムを䜜成しおください。䞊び替えおできる数は 00135569 のように 0 から始たっおもよいものずしたす。 Input 耇数のデヌタセットが䞎えられたす。行目にデヌタセット数 n ( n ≀ 50) が䞎えられたす。続いお n 行のデヌタが䞎えられたす。各デヌタは 8 個の数字の䞊び0 から 9 の数字です。 Output 各デヌタセットに察しお、入力された数字を䞊べ替えおできる最倧の敎数ず最小の敎数の差を行に出力しお䞋さい。 Sample Input 2 65539010 65539010 Output for the Sample Input 96417531 96417531
35,727
Score : 300 points Problem Statement Takahashi loves the number 7 and multiples of K . Where is the first occurrence of a multiple of K in the sequence 7,77,777,\ldots ? (Also see Output and Sample Input/Output below.) If the sequence contains no multiples of K , print -1 instead. Constraints 1 \leq K \leq 10^6 K is an integer. Input Input is given from Standard Input in the following format: K Output Print an integer representing the position of the first occurrence of a multiple of K . (For example, if the first occurrence is the fourth element of the sequence, print 4 .) Sample Input 1 101 Sample Output 1 4 None of 7 , 77 , and 777 is a multiple of 101 , but 7777 is. Sample Input 2 2 Sample Output 2 -1 All elements in the sequence are odd numbers; there are no multiples of 2 . Sample Input 3 999983 Sample Output 3 999982
35,728
の环乗 䞎えられた数を、それ以䞋の数の䞭で最倧の$2$の环乗に倉換しおください。たずえば、$2$や$3$ならば$2^1=2$に倉換しおください。同様に$4$,$5$,$6$,$7$ならば$2^2=4$に、$8$,$9$,$10$,$11$,...,$15$ならば$2^3=8$に倉換しおください。 䞎えられた数を、その数以䞋の数の䞭で最倧の$2$の环乗に倉換するプログラムを䜜成せよ。 入力 入力は以䞋の圢匏で䞎えられる。 $N$ 行に数$N$ ($2 \leq N \leq 10^6$)が䞎えられる。 出力 倉換した数を行に出力する。 入出力䟋 入力䟋 54 出力䟋 32 入力䟋 1024 出力䟋 1024
35,729
Surrounding Area 土地囲い English text is not available in this practice contest. ある 2 人の䞍動産屋が客船に乗っお南の島に向かっおいた青い空さわやかな颚 2 人は他の乗客ずずもに船旅を楜しんでいたずころがある日突然発生した竜巻によっお客船が沈んでしたった他の乗客は救助隊によっお救助されたがどういうわけかこの 2 人だけ芋萜ずされおしたった数日の挂流の埌圌らはある無人島に流れ着いたこの無人島は長方圢であり以䞋の図のように栌子に区切られおいた 図 C-1: 無人島の圢状 圌らはずおも欲深かったので助けを呌ぶこずではなくこの無人島の土地を売るこずを考えおいたそしお圌らは土地を折半するずいうこずはせず土地の奪い合いを始めた圌らはそれぞれ自分の土地にしたいずころを䞀方は黒い杭でもう䞀方は癜い杭で囲み始めた党おの杭は栌子の䞭倮に打たれたた 1 ぀の栌子に耇数の杭が打たれるこずはなかったしばらくするず双方ずも疲れ果おお杭を打぀こずを止めた あなたの仕事は黒い杭および癜い杭で囲たれた土地の面積を求めるプログラムを曞くこずであるただし栌子 ( i , j ) が黒い杭に囲たれおいるずは盎感的には「栌子 ( i , j ) から䞊䞋巊右に任意に移動したずき最初に圓たる杭は垞に黒い杭である」こずを意味する正確には以䞋のように定矩される 黒い杭に「拡倧隣接した」栌子を次のずおりに定める癜い杭に぀いおも同様ずする 栌子 ( i , j ) に杭が存圚せずさらに栌子 ( i , j ) に隣接する栌子のいずれかに黒い杭が存圚するならば栌子 ( i , j ) は黒い杭に拡倧隣接しおいる 栌子 ( i , j ) に杭が存圚せずさらに栌子 ( i , j ) に隣接する栌子のいずれかが黒い杭に拡倧隣接しおいるならば栌子 ( i , j ) は黒い杭に拡倧隣接しおいるこのルヌルは再垰的に適甚される このずき栌子 ( i , j ) が黒い杭に拡倧隣接しおいおさらに癜い杭に隣接しおいないずきたたそのずきに限っお栌子 ( i , j ) が黒い杭に囲たれおいるずいうたた逆に栌子 ( i , j ) が癜い杭に拡倧隣接しおいおさらに黒い杭に隣接しおいないずきたたそのずきに限っお栌子 ( i , j ) が癜い杭に囲たれおいるずいう Input 入力は耇数のデヌタセットから構成される各デヌタセットは以䞋のような構成になっおいる w h a 1,1 a 2,1 a 3,1 ... a w ,1 a 1,2 a 2,2 a 3,2 ... a w ,2 ... a 1, h a 2, h a 3, h ... a w , h w は土地の暪幅 h は土地の瞊幅を衚すこれらは 1 ≩ w , h ≩ 50 を満たす各 a i , j は栌子 ( i , j ) の状態を衚す 1 ぀の半角文字であり「 B 」は黒い杭が打たれおいるこずを「 W 」は癜い杭が打たれおいるこずを「 . 」ピリオドはいずれの杭も打たれおいないこずをそれぞれ瀺す w = h = 0 は入力の終端を衚しおおりデヌタセットには含たれない Output 各デヌタセットに぀いお黒い杭が囲んだ土地の倧きさおよび癜い杭が囲んだ土地の倧きさを 1 ぀の空癜で区切っお 1 行に出力しなさい Sample Input 10 10 .....W.... ....W.W... ...W...W.. ....W...W. .....W...W ......W.W. BBB....W.. ..B..BBBBB ..B..B.... ..B..B..W. 5 3 ...B. ...BB ..... 1 1 . 0 0 Output for the Sample Input 6 21 12 0 0 0
35,730
Score : 400 points Problem Statement Let f(A, B) be the exclusive OR of A, A+1, ..., B . Find f(A, B) . What is exclusive OR? The bitwise exclusive OR of integers c_1, c_2, ..., c_n (let us call it y ) is defined as follows: When y is written in base two, the digit in the 2^k 's place ( k \geq 0 ) is 1 if, the number of integers among c_1, c_2, ...c_m whose binary representations have 1 in the 2^k 's place, is odd, and 0 if that count is even. For example, the exclusive OR of 3 and 5 is 6 . (When written in base two: the exclusive OR of 011 and 101 is 110 .)
35,731
Sun and Moon In the year 20XX, mankind is hit by an unprecedented crisis. The power balance between the sun and the moon was broken by the total eclipse of the sun, and the end is looming! To save the world, the secret society called "Sun and Moon" decided to perform the ritual to balance the power of the sun and the moon called "Ritual of Sun and Moon". The ritual consists of "Ritual of Sun" and "Ritual of Moon". "Ritual of Moon" is performed after "Ritual of Sun". A member of the society is divided into two groups, "Messengers of Sun" and "Messengers of Moon". Each member has some offerings and magic power. First, the society performs "Ritual of Sun". In the ritual, each member sacrifices an offering. If he can not sacrifice an offering here, he will be killed. After the sacrifice, his magic power is multiplied by the original number of his offerings. Each member must perform the sacrifice just once. Second, the society performs "Ritual of Moon". In the ritual, each member sacrifices all remaining offerings. After the sacrifice, his magic power is multiplied by x p . Here x is the number of days from the eclipse (the eclipse is 0th day) and p is the number of his sacrificed offerings. Each member must perform the sacrifice just once. After two rituals, all "Messengers of Sun" and "Messengers of Moon" give all magic power to the "magical reactor". If the total power of "Messengers of Sun" and the total power of "Messengers of Moon" are equal, the society will succeed in the ritual and save the world. It is very expensive to perform "Ritual of Sun". It may not be able to perform "Ritual of Sun" because the society is in financial trouble. Please write a program to calculate the minimum number of days from the eclipse in which the society can succeed in "Ritual of Sun and Moon" whether "Ritual of Sun" can be performed or not. The society cannot perform the ritual on the eclipse day (0-th day). Input The format of the input is as follows. N O 1 P 1 ... O N P N The first line contains an integer N that is the number of members of the society ( 0 ≀ N ≀ 1,000 ). Each of the following N lines contains two integers O i ( 0 ≀ O i ≀ 1,000,000,000 ) and P i ( 1 ≀ |P i | ≀ 1,000,000,000,000,000 ). O i is the number of the i -th member's offerings and |P i | is the strength of his magic power. If P i is a positive integer, the i -th member belongs to "Messengers of Sun", otherwise he belongs to "Messengers of Moon". Output If there exists the number of days from the eclipse that satisfies the above condition , print the minimum number of days preceded by "Yes ". Otherwise print "No". Of course, the answer must be a positive integer. Sample Input 1 9 2 1 1 -1 1 -1 1 -1 1 -1 0 1 0 1 0 1 0 1 Output for the Sample Input 1 Yes 2 Sample Input 2 2 1 1 0 -1 Output for the Sample Input 2 No
35,732
Score : 1000 points Problem Statement You are given a sequence a = \{a_1, ..., a_N\} with all zeros, and a sequence b = \{b_1, ..., b_N\} consisting of 0 and 1 . The length of both is N . You can perform Q kinds of operations. The i -th operation is as follows: Replace each of a_{l_i}, a_{l_i + 1}, ..., a_{r_i} with 1 . Minimize the hamming distance between a and b , that is, the number of i such that a_i \neq b_i , by performing some of the Q operations. Constraints 1 \leq N \leq 200,000 b consists of 0 and 1 . 1 \leq Q \leq 200,000 1 \leq l_i \leq r_i \leq N If i \neq j , either l_i \neq l_j or r_i \neq r_j . Input Input is given from Standard Input in the following format: N b_1 b_2 ... b_N Q l_1 r_1 l_2 r_2 : l_Q r_Q Output Print the minimum possible hamming distance. Sample Input 1 3 1 0 1 1 1 3 Sample Output 1 1 If you choose to perform the operation, a will become \{1, 1, 1\} , for a hamming distance of 1 . Sample Input 2 3 1 0 1 2 1 1 3 3 Sample Output 2 0 If both operations are performed, a will become \{1, 0, 1\} , for a hamming distance of 0 . Sample Input 3 3 1 0 1 2 1 1 2 3 Sample Output 3 1 Sample Input 4 5 0 1 0 1 0 1 1 5 Sample Output 4 2 It may be optimal to perform no operation. Sample Input 5 9 0 1 0 1 1 1 0 1 0 3 1 4 5 8 6 7 Sample Output 5 3 Sample Input 6 15 1 1 0 0 0 0 0 0 1 0 1 1 1 0 0 9 4 10 13 14 1 7 4 14 9 11 2 6 7 8 3 12 7 13 Sample Output 6 5 Sample Input 7 10 0 0 0 1 0 0 1 1 1 0 7 1 4 2 5 1 3 6 7 9 9 1 5 7 9 Sample Output 7 1
35,733
Selection of Participants of an Experiment Dr. Tsukuba has devised a new method of programming training. In order to evaluate the effectiveness of this method, he plans to carry out a control experiment. Having two students as the participants of the experiment, one of them will be trained under the conventional method and the other under his new method. Comparing the final scores of these two, he will be able to judge the effectiveness of his method. It is important to select two students having the closest possible scores, for making the comparison fair. He has a list of the scores of all students who can participate in the experiment. You are asked to write a program which selects two of them having the smallest difference in their scores. Input The input consists of multiple datasets, each in the following format. n a 1 a 2 
 a n A dataset consists of two lines. The number of students n is given in the first line. n is an integer satisfying 2 ≀ n ≀ 1000. The second line gives scores of n students. a i (1 ≀ i ≀ n ) is the score of the i -th student, which is a non-negative integer not greater than 1,000,000. The end of the input is indicated by a line containing a zero. The sum of n 's of all the datasets does not exceed 50,000. Output For each dataset, select two students with the smallest difference in their scores, and output in a line (the absolute value of) the difference. Sample Input 5 10 10 10 10 10 5 1 5 8 9 11 7 11 34 83 47 59 29 70 0 Output for the Sample Input 0 1 5
35,734
Score : 300 points Problem Statement There are N integers written on a blackboard. The i -th integer is A_i . Takahashi will repeatedly perform the following operation on these numbers: Select a pair of integers, A_i and A_j , that have the same parity (that is, both are even or both are odd) and erase them. Then, write a new integer on the blackboard that is equal to the sum of those integers, A_i+A_j . Determine whether it is possible to have only one integer on the blackboard. Constraints 2 ≩ N ≩ 10^5 1 ≩ A_i ≩ 10^9 A_i is an integer. Input The input is given from Standard Input in the following format: N A_1 A_2 
 A_N Output If it is possible to have only one integer on the blackboard, print YES . Otherwise, print NO . Sample Input 1 3 1 2 3 Sample Output 1 YES It is possible to have only one integer on the blackboard, as follows: Erase 1 and 3 from the blackboard, then write 4 . Now, there are two integers on the blackboard: 2 and 4 . Erase 2 and 4 from the blackboard, then write 6 . Now, there is only one integer on the blackboard: 6 . Sample Input 2 5 1 2 3 4 5 Sample Output 2 NO
35,735
Score : 500 points Problem Statement Consider sequences \{A_1,...,A_N\} of length N consisting of integers between 1 and K (inclusive). There are K^N such sequences. Find the sum of \gcd(A_1, ..., A_N) over all of them. Since this sum can be enormous, print the value modulo (10^9+7) . Here \gcd(A_1, ..., A_N) denotes the greatest common divisor of A_1, ..., A_N . Constraints 2 \leq N \leq 10^5 1 \leq K \leq 10^5 All values in input are integers. Input Input is given from Standard Input in the following format: N K Output Print the sum of \gcd(A_1, ..., A_N) over all K^N sequences, modulo (10^9+7) . Sample Input 1 3 2 Sample Output 1 9 \gcd(1,1,1)+\gcd(1,1,2)+\gcd(1,2,1)+\gcd(1,2,2) +\gcd(2,1,1)+\gcd(2,1,2)+\gcd(2,2,1)+\gcd(2,2,2) =1+1+1+1+1+1+1+2=9 Thus, the answer is 9 . Sample Input 2 3 200 Sample Output 2 10813692 Sample Input 3 100000 100000 Sample Output 3 742202979 Be sure to print the sum modulo (10^9+7) .
35,736
Problem A: ICPC Score Totalizer Software The International Clown and Pierrot Competition (ICPC), is one of the most distinguished and also the most popular events on earth in the show business. One of the unique features of this contest is the great number of judges that sometimes counts up to one hundred. The number of judges may differ from one contestant to another, because judges with any relationship whatsoever with a specific contestant are temporarily excluded for scoring his/her performance. Basically, scores given to a contestant's performance by the judges are averaged to decide his/her score. To avoid letting judges with eccentric viewpoints too much influence the score, the highest and the lowest scores are set aside in this calculation. If the same highest score is marked by two or more judges, only one of them is ignored. The same is with the lowest score. The average, which may contain fractions, are truncated down to obtain final score as an integer. You are asked to write a program that computes the scores of performances, given the scores of all the judges, to speed up the event to be suited for a TV program. Input The input consists of a number of datasets, each corresponding to a contestant's performance. There are no more than 20 datasets in the input. A dataset begins with a line with an integer n , the number of judges participated in scoring the performance (3 ≀ n ≀ 100). Each of the n lines following it has an integral score s (0 ≀ s ≀ 1000) marked by a judge. No other characters except for digits to express these numbers are in the input. Judges' names are kept secret. The end of the input is indicated by a line with a single zero in it. Output For each dataset, a line containing a single decimal integer indicating the score for the corresponding performance should be output. No other characters should be on the output line. Sample Input 3 1000 342 0 5 2 2 9 11 932 5 300 1000 0 200 400 8 353 242 402 274 283 132 402 523 0 Output for the Sample Input 342 7 300 326
35,737
Range Minimum Query (RMQ) Write a program which manipulates a sequence A = { a 0 , a 1 , . . . , a n-1 } with the following operations: find(s, t) : report the minimum element in a s , a s+1 , . . . ,a t . update(i, x) : change a i to x . Note that the initial values of a i ( i = 0, 1, . . . , n−1 ) are 2 31 -1. Input n q com 0 x 0 y 0 com 1 x 1 y 1 ... com q−1 x q−1 y q−1 In the first line, n (the number of elements in A ) and q (the number of queries) are given. Then, q queries are given where com represents the type of queries. '0' denotes update(x i , y i ) and '1' denotes find(x i , y i ) . Output For each find operation, print the minimum element. Constraints 1 ≀ n ≀ 100000 1 ≀ q ≀ 100000 If com i is 0, then 0 ≀ x i < n , 0 ≀ y i < 2 31 -1 . If com i is 1, then 0 ≀ x i < n , 0 ≀ y i < n . Sample Input 1 3 5 0 0 1 0 1 2 0 2 3 1 0 2 1 1 2 Sample Output 1 1 2 Sample Input 2 1 3 1 0 0 0 0 5 1 0 0 Sample Output 2 2147483647 5
35,738
Heat Stroke We have had record hot temperatures this summer. To avoid heat stroke, you decided to buy a quantity of drinking water at the nearby supermarket. Two types of bottled water, 1 and 0.5 liter, are on sale at respective prices there. You have a definite quantity in your mind, but are willing to buy a quantity larger than that if: no combination of these bottles meets the quantity, or, the total price becomes lower. Given the prices for each bottle of water and the total quantity needed, make a program to seek the lowest price to buy greater than or equal to the quantity required. Input The input is given in the following format. $A$ $B$ $X$ The first line provides the prices for a 1-liter bottle $A$ ($1\leq A \leq 1000$), 500-milliliter bottle $B$ ($1 \leq B \leq 1000$), and the total water quantity needed $X$ ($1 \leq X \leq 20000$). All these values are given as integers, and the quantity of water in milliliters. Output Output the total price. Sample Input 1 180 100 2400 Sample Output 1 460 Sample Input 2 200 90 2018 Sample Output 2 450
35,739
B: ピボット (Pivots) 問題 1 から N たでの敎数を䞊び倉えた、長さ N の順列 a_1, a_2, ..., a_N が䞎えられる。 たた、この順列に察しお Q 個のク゚リが順に䞎えられる。 i 番目のク゚リでは次の操䜜をしなければならない。 倀 q_i ( 1 \leq q_i \leq N ) が䞎えられる。順列 \{a_1, a_2, ..., a_N\} においお q_i の巊偎の順列を L 、 q_i の右偎の順列を R ずしたずき、元の順列 L \ \ q_i \ \ R を R \ \ q_i \ \ L に倉曎する。぀たり、 q_{i} = a_j であるずき、順列 \{a_1, ..., a_{j-1}, a_j, a_{j+1}, ..., a_N\} を \{a_{j+1}, ..., a_N, a_j, a_1, ..., a_{j-1}\} に倉曎する。 なお、順列 L , R は空になるこずもあり埗る。䟋えば L が空の時は、 q_i \ \ R を R \ \ q_i に倉曎する。 R が空のずきに぀いおも同様である。 䞎えられた順列に察しおこれら Q 個のク゚リを順に凊理した埌の順列を䞀行に出力せよ。 入力圢匏 N Q a_1 a_2 ... a_N q_1 q_2 ... q_Q 入力は党お敎数である。 䞀行目には順列の芁玠数 N ずク゚リの回数 Q が空癜区切りで䞎えられる。 二行目には 1 から N たでの敎数を䞊び倉えた順列 a_1, a_2, ..., a_N が空癜区切りで䞎えられる。 䞉行目にはク゚リが Q 個、空癜区切りで䞎えられる。 q_i は i 番目のク゚リを衚す。 制玄 1 \leq N \leq 10^5 1 \leq Q \leq 10^5 1 \leq a_i \leq N a_i は盞異なる 1 \leq q_i \leq N 出力圢匏 ク゚リを順にすべお凊理した埌の順列を䞀行に出力せよ。 入力䟋1 5 2 1 5 3 2 4 5 2 出力䟋1 4 5 1 2 3 1番目のク゚リにより、順列は \{3, 2, 4, 5, 1\} ぞ倉わる。 2番目のク゚リにより、順列は \{4, 5, 1, 2, 3\} ぞ倉わる。 入力䟋2 5 1 1 2 3 4 5 5 出力䟋2 5 1 2 3 4
35,740
Area of Polygons Polygons are the most fundamental objects in geometric processing. Complex figures are often represented and handled as polygons with many short sides. If you are interested in the processing of geometric data, you'd better try some programming exercises about basic operations on polygons. Your job in this problem is to write a program that computes the area of polygons. A polygon is represented by a sequence of points that are its vertices. If the vertices p 1 , p 2 , ..., p n are given, line segments connecting p i and p i+1 (1 <= i <= n-1) are sides of the polygon. The line segment connecting p n and p 1 is also a side of the polygon. You can assume that the polygon is not degenerate. Namely, the following facts can be assumed without any input data checking. No point will occur as a vertex more than once. Two sides can intersect only at a common endpoint (vertex). The polygon has at least 3 vertices. Note that the polygon is not necessarily convex. In other words, an inner angle may be larger than 180 degrees. Input The input contains multiple data sets, each representing a polygon. A data set is given in the following format. n x1 y1 x2 y2 ... xn yn The first integer n is the number of vertices, such that 3 <= n <= 50. The coordinate of a vertex p i is given by (x i , y i ). x i and y i are integers between 0 and 1000 inclusive. The coordinates of vertices are given in the order of clockwise visit of them. The end of input is indicated by a data set with 0 as the value of n. Output For each data set, your program should output its sequence number (1 for the first data set, 2 for the second, etc.) and the area of the polygon separated by a single space. The area should be printed with one digit to the right of the decimal point. The sequence number and the area should be printed on the same line. Since your result is checked by an automatic grading program, you should not insert any extra characters nor lines on the output. Sample Input 3 1 1 3 4 6 0 7 0 0 10 10 0 20 10 30 0 40 100 40 100 0 0 Output for the Sample Input 1 8.5 2 3800.0
35,741
K-th Exclusive OR Exclusive OR (XOR) is an operation on two binary numbers $x$ and $y$ (0 or 1) that produces 0 if $x = y$ and $1$ if $x \ne y$. This operation is represented by the symbol $\oplus$. From the definition: $0 \oplus 0 = 0$, $0 \oplus 1 = 1$, $1 \oplus 0 = 1$, $1 \oplus 1 = 0$. Exclusive OR on two non-negative integers comprises the following procedures: binary representation of the two integers are XORed on bit by bit bases, and the resultant bit array constitute a new integer. This operation is also represented by the same symbol $\oplus$. For example, XOR of decimal numbers $3$ and $5$ is equivalent to binary operation $011 \oplus 101$ which results in $110$, or $6$ in decimal format. Bitwise XOR operation on a sequence $Z$ consisting of $M$ non-negative integers $z_1, z_2, . . . , z_M$ is defined as follows: $v_0 = 0, v_i = v_{i - 1} \oplus z_i$ ($1 \leq i \leq M$) Bitwise XOR on series $Z$ is defined as $v_M$. You have a sequence $A$ consisting of $N$ non-negative integers, ample sheets of papers and an empty box. You performed each of the following operations once on every combinations of integers ($L, R$), where $1 \leq L \leq R \leq N$. Perform the bitwise XOR operation on the sub-sequence (from $L$-th to $R$-th elements) and name the result as $B$. Select a sheet of paper and write $B$ on it, then put it in the box. Assume that ample sheets of paper are available to complete the trials. You select a positive integer $K$ and line up the sheets of paper inside the box in decreasing order of the number written on them. What you want to know is the number written on the $K$-th sheet of paper. You are given a series and perform all the operations described above. Then, you line up the sheets of paper in decreasing order of the numbers written on them. Make a program to determine the $K$-th number in the series. Input The input is given in the following format. $N$ $K$ $a_1$ $a_2$ ... $a_N$ The first line provides the number of elements in the series $N$ ($1 \leq N \leq 10^5$) and the selected number $K$ ($1 \leq K \leq N(N+1)/2$). The second line provides an array of elements $a_i$ ($0 \leq a_i \leq 10^6$). Sample Input 1 3 3 1 2 3 Sample Output 1 2 After all operations have been completed, the numbers written on the paper inside the box are as follows (sorted in decreasing order) $3,3,2,1,1,0$ 3番目の数は2であるため、2を出力する。 Sample Input 2 7 1 1 0 1 0 1 0 1 Sample Output 2 1 Sample Input 3 5 10 1 2 4 8 16 Sample Output 3 7
35,742
Score : 1500 points Problem Statement Shik's job is very boring. At day 0 , his boss gives him a string S_0 of length N which consists of only lowercase English letters. In the i -th day after day 0 , Shik's job is to copy the string S_{i-1} to a string S_i . We denote the j -th letter of S_i as S_i[j] . Shik is inexperienced in this job. In each day, when he is copying letters one by one from the first letter to the last letter, he would make mistakes. That is, he sometimes accidentally writes down the same letter that he wrote previously instead of the correct one. More specifically, S_i[j] is equal to either S_{i-1}[j] or S_{i}[j-1] . (Note that S_i[1] always equals to S_{i-1}[1] .) You are given the string S_0 and another string T . Please determine the smallest integer i such that S_i could be equal to T . If no such i exists, please print -1 . Constraints 1 \leq N \leq 1,000,000 The lengths of S_0 and T are both N . Both S_0 and T consist of lowercase English letters. Input The input is given from Standard Input in the following format: N S_0 T Output Print the smallest integer i such that S_i could be equal to T . If no such i exists, print -1 instead. Sample Input 1 5 abcde aaacc Sample Output 1 2 S_0 = abcde , S_1 = aaccc and S_2 = aaacc is a possible sequence such that S_2 = T . Sample Input 2 5 abcde abcde Sample Output 2 0 Sample Input 3 4 acaa aaca Sample Output 3 2 Sample Input 4 5 abcde bbbbb Sample Output 4 -1
35,743
Score : 1000 points Problem Statement In a two-dimensional plane, we have a rectangle R whose vertices are (0,0) , (W,0) , (0,H) , and (W,H) , where W and H are positive integers. Here, find the number of triangles \Delta in the plane that satisfy all of the following conditions: Each vertex of \Delta is a grid point, that is, has integer x - and y -coordinates. \Delta and R shares no vertex. Each vertex of \Delta lies on the perimeter of R , and all the vertices belong to different sides of R . \Delta contains at most K grid points strictly within itself (excluding its perimeter and vertices). Constraints 1 \leq W \leq 10^5 1 \leq H \leq 10^5 0 \leq K \leq 10^5 Input Input is given from Standard Input in the following format: W H K Output Print the answer. Sample Input 1 2 3 1 Sample Output 1 12 For example, the triangle with the vertices (1,0) , (0,2) , and (2,2) contains just one grid point within itself and thus satisfies the condition. Sample Input 2 5 4 5 Sample Output 2 132 Sample Input 3 100 100 1000 Sample Output 3 461316
35,744
今日の乱数 (Today's Random Number) E869120 君は、「今日の乱数」ずいうキャンペヌンをN日間行いたした。これは、毎日 1 回乱数を生成し、その倀をツむッタヌに投皿するずいう䌁画です。 $1, 2, 3, \dots, N$ 日目の「今日の乱数」は、それぞれ $A_1, A_2, A_3, \dots, A_N$ でした。 E869120 君は、今日の乱数の倀が昚日の乱数の倀よりも高ければ嬉しくなりたす。 $N$ 日間のなかで、E869120 君は䜕回「今日の乱数」によっお嬉しくなったでしょうか 入力 入力は以䞋の圢匏で暙準入力から䞎えられる。 $N$ $A_1$ $A_2$ $A_3$ $\dots$ $A_N$ 出力 $N$ 日間のなかで、E869120 君が「今日の乱数」によっお嬉しくなった回数を、1 行で出力しおください。 ただし、最埌には改行を入れるこず。 制玄 $1 \leq N \leq 100000 \ (= 10^5)$ $1 \leq A_i \leq 1000000000 \ (= 10^9)$ 入力は党お敎数である。 入力䟋1 5 8 6 9 1 20 出力䟋1 2 3 日目ず 5 日目にE869120君は嬉しさを感じたす。 入力䟋2 6 3 3 4 3 3 4 出力䟋2 2 3 日目ず 6 日目にE869120君は嬉しさを感じたす。 入力䟋3 10 10 9 8 7 6 5 4 3 2 1 出力䟋3 0 E869120君が「今日の乱数」によっお嬉しくなるこずはありたせん。
35,745
Problem F: Poor Computer Brian Fulk is struggling with a really poor computer for a couple of days. Now he is trying to write a very simple program which receives a positive integer x and returns some of its multiples, say, a 1 x ,..., a N x . However, even such a simple task is not easy on this computer since it has only three arithmetic operations: addition, subtraction and left shift. Let us describe the situation in more details. Initially the computer stores only a given positive integer x . The program Brian is writing will produce a 1 x ,..., a N x , where a 1 ,..., a N are given multipliers, using only the following operations: addition of two values, subtraction of two values, and bitwise left shift (left shift by n bits is equivalent to multiplication by 2 n ). The program should not generate any value greater than 42 x ; under this constraint he can assume that no overflow occurs. Also, since this computer cannot represent negative values, there should not be subtraction of a greater value from a smaller value. Some of you may wonder where the number 42 comes from. There is a deep reason related to the answer to life, the universe and everything, but we don't have enough space and time to describe it. Your task is to write a program that finds the shortest sequence of operations to produce the multiples a 1 x ,..., a N x and reports the length of the sequence. These numbers may be produced in any order. Here we give an example sequence for the first sample input, in a C++/Java-like language: a = x << 1; // 2x b = x + a; // 3x c = a + b; // 5x d = c << 2; // 20x e = d - b; // 18x Input The first line specifies N , the number of multipliers. The second line contains N integers a 1 ,..., a N , each of which represents a multiplier of x . You can assume that N ≀ 41 and 2 ≀ a i ≀ 42 (1 ≀ i ≀ N ). Furthermore, a 1 ,..., a N are all distinct. Output Output in a line the minimum number of operations you need to produce the values a 1 x ,..., a N x . Sample Input and Output Input #1 3 3 5 18 Output #1 5 Input #2 1 29 Output #2 4 Input #3 4 12 19 41 42 Output #3 8
35,746
Score : 100 points Problem Statement You are given a string S of length N consisting of A , B and C , and an integer K which is between 1 and N (inclusive). Print the string S after lowercasing the K -th character in it. Constraints 1 ≀ N ≀ 50 1 ≀ K ≀ N S is a string of length N consisting of A , B and C . Input Input is given from Standard Input in the following format: N K S Output Print the string S after lowercasing the K -th character in it. Sample Input 1 3 1 ABC Sample Output 1 aBC Sample Input 2 4 3 CABA Sample Output 2 CAbA
35,747
Score : 400 points Problem Statement On a two-dimensional plane, there are N red points and N blue points. The coordinates of the i -th red point are (a_i, b_i) , and the coordinates of the i -th blue point are (c_i, d_i) . A red point and a blue point can form a friendly pair when, the x -coordinate of the red point is smaller than that of the blue point, and the y -coordinate of the red point is also smaller than that of the blue point. At most how many friendly pairs can you form? Note that a point cannot belong to multiple pairs. Constraints All input values are integers. 1 \leq N \leq 100 0 \leq a_i, b_i, c_i, d_i < 2N a_1, a_2, ..., a_N, c_1, c_2, ..., c_N are all different. b_1, b_2, ..., b_N, d_1, d_2, ..., d_N are all different. Input Input is given from Standard Input in the following format: N a_1 b_1 a_2 b_2 : a_N b_N c_1 d_1 c_2 d_2 : c_N d_N Output Print the maximum number of friendly pairs. Sample Input 1 3 2 0 3 1 1 3 4 2 0 4 5 5 Sample Output 1 2 For example, you can pair (2, 0) and (4, 2) , then (3, 1) and (5, 5) . Sample Input 2 3 0 0 1 1 5 2 2 3 3 4 4 5 Sample Output 2 2 For example, you can pair (0, 0) and (2, 3) , then (1, 1) and (3, 4) . Sample Input 3 2 2 2 3 3 0 0 1 1 Sample Output 3 0 It is possible that no pair can be formed. Sample Input 4 5 0 0 7 3 2 2 4 8 1 6 8 5 6 9 5 4 9 1 3 7 Sample Output 4 5 Sample Input 5 5 0 0 1 1 5 5 6 6 7 7 2 2 3 3 4 4 8 8 9 9 Sample Output 5 4
35,748
Score : 100 points Problem Statement Takahashi likes the sound when he buys a drink from a vending machine. That sound can be heard by spending A yen (the currency of Japan) each time. Takahashi has B yen. He will hear the sound as many times as he can with that money, but at most C times, as he would be satisfied at that time. How many times will he hear the sound? Constraints All values in input are integers. 1 \leq A, B, C \leq 100 Input Input is given from Standard Input in the following format: A B C Output Print the number of times Takahashi will hear his favorite sound. Sample Input 1 2 11 4 Sample Output 1 4 Since he has not less than 8 yen, he will hear the sound four times and be satisfied. Sample Input 2 3 9 5 Sample Output 2 3 He may not be able to be satisfied. Sample Input 3 100 1 10 Sample Output 3 0
35,749
Osaki 倧厎 English text is not available in this practice contest. 山手線は東京 23 区内に敷蚭されおいる環状鉄道路線である総路線距離は 34.5km であり1 呚にはおよそ 1 時間を芁する駅は党郚で 29 駅存圚するラむンカラヌはりグむス色であるピヌク時の混雑率は 200% を超え日本の鉄道路線の䞭で最も混雑しおいる路線の 1 ぀である最も混雑する時間垯では 3 分に 1 本の列車が走っおおり東京に初めお来た人々はその光景に驚くものである 鉄子さんは山手線を愛しおやたない生粋の鉄道奜きであるある日圌女は愛読曞である JR 時刻衚を読みながら次の疑問に至った「山手線では䞀日に䜕台の車䞡が䜿われおいるのだろう」 圌女は時刻衚から運行に最䜎限必芁な車䞡の台数を割り出そうずしたしかし列車の本数が非垞に倚かったので圌女䞀人の力では到底数え切れそうにないそこで圌女は優秀なプログラマヌであるあなたに助けを求めた あなたの仕事は䞎えられた時刻衚から山手線の運行に芁する車䞡の最䜎台数を求めるプログラムを曞くこずである山手線は環状路線であるこずからその時刻衚は䟿宜䞊「倧厎駅」を始発駅および終着駅ずしお衚蚘されるこずが倚いそのため圌女から枡された時刻衚にも各列車の倧厎駅における発時刻および着時刻のみが蚘されおいる なお実際の山手線では起こりえないが状況蚭定を簡単にするためここでは倧厎駅の到着盎埌に列車が倧厎駅を出発可胜であるず考えるこずにするたた鉄子さんが写した時刻に誀りがあったり鉄子さんの劄想によっお勝手に加えられた列車が時刻衚に玛れ蟌んだりしおいる堎合もあるがあなたはそれらを芋抜くこずができないのであくたでも曞かれたずおりの時刻に察しお台数を求めなければならない きちんず動䜜するプログラムを曞けば圌女が列車内デヌトに誘っおくれるかもしれないもっずも誘いに乗るか断るかはあなた次第であるが Input 入力は耇数のデヌタセットから構成される各デヌタセットは次の圢匏になっおいる n hh : mm : ss hh : mm : ss hh : mm : ss hh : mm : ss ... hh : mm : ss hh : mm : ss 1 行目の敎数 n は時刻衚に含たれる列車の本数であるこの倀は 10,000 を超えないこずが保蚌されおいる2 行目から n + 1 行目たでの n 行には各列車の倧厎駅の発時刻および着時刻がこの順番で䞎えられ発時刻ず着時刻の間は 1 ぀の空癜で区切られおいる各時刻は hh : mm : ss の圢匏で衚珟され hh が時 mm が分 ss が秒を衚しおいるそれぞれの倀の範囲は 0 ≩ hh < 24, 0 ≩ mm < 60, 0 ≩ ss < 60であるこれらの数倀は党お 2 桁ずなるように必芁に応じお先頭に 0 が付け加えられおいる 倜の 24:00 をたたいで運行されるような列車は含たれないしたがっお垞に発時刻は垞に着時刻よりも前の時刻である 入力の終了は n = 0 によっお瀺されるこれはデヌタセットには含たれない Output 各デヌタセットに察しお最䜎限必芁ずなる車䞡の台数を 1 行に出力しなさい Sample Input 3 05:47:15 09:54:40 12:12:59 12:13:00 16:30:20 21:18:53 6 00:00:00 03:00:00 01:00:00 03:00:00 02:00:00 03:00:00 03:00:00 04:00:00 03:00:00 05:00:00 03:00:00 06:00:00 0 Output for the Sample Input 1 3
35,750
Score : 100 points Problem Statement You are given a grid with 2 rows and 3 columns of squares. The color of the square at the i -th row and j -th column is represented by the character C_{ij} . Write a program that prints YES if this grid remains the same when rotated 180 degrees, and prints NO otherwise. Constraints C_{i,j}(1 \leq i \leq 2, 1 \leq j \leq 3) is a lowercase English letter. Input Input is given from Standard Input in the following format: C_{11}C_{12}C_{13} C_{21}C_{22}C_{23} Output Print YES if this grid remains the same when rotated 180 degrees; print NO otherwise. Sample Input 1 pot top Sample Output 1 YES This grid remains the same when rotated 180 degrees. Sample Input 2 tab bet Sample Output 2 NO This grid does not remain the same when rotated 180 degrees. Sample Input 3 eye eel Sample Output 3 NO
35,751
Repairing In the International City of Pipe Construction, it is planned to repair the water pipe at a certain point in the water pipe network. The network consists of water pipe segments, stop valves and source point. A water pipe is represented by a segment on a 2D-plane and intersected pair of water pipe segments are connected at the intersection point. A stop valve, which prevents from water flowing into the repairing point while repairing, is represented by a point on some water pipe segment. In the network, just one source point exists and water is supplied to the network from this point. Of course, while repairing, we have to stop water supply in some areas, but, in order to reduce the risk of riots, the length of water pipes stopping water supply must be minimized. What you have to do is to write a program to minimize the length of water pipes needed to stop water supply when the coordinates of end points of water pipe segments, stop valves, source point and repairing point are given. Input A data set has the following format: N M x s1 y s1 x d1 y d1 ... x sN y sN x dN y dN x v1 y v1 ... x vM y vM x b y b x c y c The first line of the input contains two integers, N ( 1 ≀ N ≀ 300 ) and M ( 0 ≀ M ≀ 1,000 ) that indicate the number of water pipe segments and stop valves. The following N lines describe the end points of water pipe segments. The i -th line contains four integers, x si , y si , x di and y di that indicate the pair of coordinates of end points of i -th water pipe segment. The following M lines describe the points of stop valves. The i -th line contains two integers, x vi and y vi that indicate the coordinate of end points of i -th stop valve. The following line contains two integers, x b and y b that indicate the coordinate of the source point. The last line contains two integers, x c and y c that indicate the coordinate of the repairing point. You may assume that any absolute values of coordinate integers are less than 1,000 (inclusive.) You may also assume each of the stop valves, the source point and the repairing point is always on one of water pipe segments and that that each pair among the stop valves, the source point and the repairing point are different. And, there is not more than one intersection between each pair of water pipe segments. Finally, the water pipe network is connected, that is, all the water pipes are received water supply initially. Output Print the minimal length of water pipes needed to stop water supply in a line. The absolute or relative error should be less than or 10 -6 . When you cannot stop water supply to the repairing point even though you close all stop valves, print " -1 " in a line. Sample Input 1 1 2 0 0 10 0 1 0 9 0 0 0 5 0 Output for the Sample Input 1 9.0 Sample Input 2 5 3 0 4 2 4 0 2 2 2 0 0 2 0 0 0 0 4 2 0 2 4 0 2 1 0 2 2 1 4 2 1 Output for the Sample Input 2 3.0 Sample Input 3 2 1 0 0 0 4 0 2 2 2 1 2 0 1 0 3 Output for the Sample Input 3 -1
35,752
Development of Small Flying Robots You are developing small flying robots in your laboratory. The laboratory is a box-shaped building with K levels, each numbered 1 through K from bottom to top. The floors of all levels are square-shaped with their edges precisely aligned east-west and north-south. Each floor is divided into R × R cells. We denote the cell on the z -th level in the x -th column from the west and the y -th row from the south as ( x , y , z ). (Here, x and y are one-based.) For each x , y , and z ( z > 1), the cell ( x , y , z ) is located immediately above the cell ( x , y , z − 1). There are N robots flying in the laboratory, each numbered from 1 through N . Initially, the i -th robot is located at the cell ( x i , y i , z i ). By your effort so far, you successfully implemented the feature to move each flying robot to any place that you planned. As the next step, you want to implement a new feature that gathers all the robots in some single cell with the lowest energy consumption, based on their current locations and the surrounding environment. Floors of the level two and above have several holes. Holes are rectangular and their edges align with edges of the cells on the floors. There are M holes in the laboratory building, each numbered 1 through M . The j -th hole can be described by five integers u 1 j , v 1 j , u 2 j , v 2 j , and w j . The j -th hole extends over the cells ( x , y , w j ) where u 1 j ≀ x ≀ u 2 j and v 1 j ≀ y ≀ v 2 j . Possible movements of robots and energy consumption involved are as follows. You can move a robot from one cell to an adjacent cell, toward one of north, south, east, or west. The robot consumes its energy by 1 for this move. If there is a hole to go through immediately above, you can move the robot upward by a single level. The robot consumes its energy by 100 for this move. The robots never fall down even if there is a hole below. Note that you can move two or more robots to the same cell. Now, you want to gather all the flying robots at a single cell in the K -th level where there is no hole on the floor, with the least energy consumption. Compute and output the minimum total energy required by the robots. Input The input consists of at most 32 datasets, each in the following format. Every value in the input is an integer. N M K R x 1 y 1 z 1 ... x N y N z N u 11 v 11 u 21 v 21 w 1 ... u 1 M v 1 M u 2 M v 2 M w M N is the number of robots in the laboratory (1 ≀ N ≀ 100). M is the number of holes (1 ≀ M ≀ 50), K is the number of levels (2 ≀ K ≀ 10), and R is the number of cells in one row and also one column on a single floor (3 ≀ R ≀ 1,000,000). For each i , integers x i , y i , and z i represent the cell that the i -th robot initially located at (1 ≀ x i ≀ R , 1 ≀ y i ≀ R , 1 ≀ z i ≀ K ). Further, for each j , integers u 1 j , v 1 j , u 2 j , v 2 j , and w j describe the position and the extent of the j -th hole (1 ≀ u 1 j ≀ u 2 j ≀ R , 1 ≀ v 1 j ≀ v 2 j ≀ R , 2 ≀ w j ≀ K ). The following are guaranteed. In each level higher than or equal to two, there exists at least one hole. In each level, there exists at least one cell not belonging to any holes. No two holes overlap. That is, each cell belongs to at most one hole. Two or more robots can initially be located at the same cell. Also note that two neighboring cells may belong to different holes. The end of the input is indicated by a line with a single zero. Output For each dataset, print the minimum total energy consumption in a single line. Sample Input 2 1 2 8 1 1 1 8 8 1 3 3 3 3 2 3 3 2 3 1 1 2 1 1 2 1 1 2 1 1 1 1 2 1 2 1 2 2 2 1 2 1 2 2 2 3 100 100 50 1 1 50 3 100 1 100 100 3 1 1 1 100 2 5 6 7 60 11 11 1 11 51 1 51 11 1 51 51 1 31 31 1 11 11 51 51 2 11 11 51 51 3 11 11 51 51 4 11 11 51 51 5 18 1 54 42 6 1 43 59 60 7 5 6 4 9 5 5 3 1 1 1 1 9 1 9 1 1 9 9 1 3 3 7 7 4 4 4 6 6 2 1 1 2 2 3 1 8 2 9 3 8 1 9 2 3 8 8 9 9 3 5 10 5 50 3 40 1 29 13 2 39 28 1 50 50 1 25 30 5 3 5 10 10 2 11 11 14 14 2 15 15 20 23 2 40 40 41 50 2 1 49 3 50 2 30 30 50 50 3 1 1 10 10 4 1 30 1 50 5 20 30 20 50 5 40 30 40 50 5 15 2 2 1000000 514898 704203 1 743530 769450 1 202298 424059 1 803485 898125 1 271735 512227 1 442644 980009 1 444735 799591 1 474132 623298 1 67459 184056 1 467347 302466 1 477265 160425 2 425470 102631 2 547058 210758 2 52246 779950 2 291896 907904 2 480318 350180 768473 486661 2 776214 135749 872708 799857 2 0 Output for the Sample Input 216 6 497 3181 1365 1930 6485356
35,753
Score : 400 points Problem Statement Dolphin is planning to generate a small amount of a certain chemical substance C. In order to generate the substance C, he must prepare a solution which is a mixture of two substances A and B in the ratio of M_a:M_b . He does not have any stock of chemicals, however, so he will purchase some chemicals at a local pharmacy. The pharmacy sells N kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock. The package of chemical i contains a_i grams of the substance A and b_i grams of the substance B, and is sold for c_i yen (the currency of Japan). Dolphin will purchase some of these packages. For some reason, he must use all contents of the purchased packages to generate the substance C. Find the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C by purchasing any combination of packages at the pharmacy, report that fact. Constraints 1≩N≩40 1≩a_i,b_i≩10 1≩c_i≩100 1≩M_a,M_b≩10 gcd(M_a,M_b)=1 a_i , b_i , c_i , M_a and M_b are integers. Input The input is given from Standard Input in the following format: N M_a M_b a_1 b_1 c_1 a_2 b_2 c_2 : a_N b_N c_N Output Print the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C, print -1 instead. Sample Input 1 3 1 1 1 2 1 2 1 2 3 3 10 Sample Output 1 3 The amount of money spent will be minimized by purchasing the packages of chemicals 1 and 2 . In this case, the mixture of the purchased chemicals will contain 3 grams of the substance A and 3 grams of the substance B, which are in the desired ratio: 3:3=1:1 . The total price of these packages is 3 yen. Sample Input 2 1 1 10 10 10 10 Sample Output 2 -1 The ratio 1:10 of the two substances A and B cannot be satisfied by purchasing any combination of the packages. Thus, the output should be -1 .
35,754
Problem B: Analyzing Login/Logout Records You have a computer literacy course in your university. In the computer system, the login/logout records of all PCs in a day are stored in a file. Although students may use two or more PCs at a time, no one can log in to a PC which has been logged in by someone who has not logged out of that PC yet. You are asked to write a program that calculates the total time of a student that he/she used at least one PC in a given time period (probably in a laboratory class) based on the records in the file. The following are example login/logout records. The student 1 logged in to the PC 1 at 12:55 The student 2 logged in to the PC 4 at 13:00 The student 1 logged in to the PC 2 at 13:10 The student 1 logged out of the PC 2 at 13:20 The student 1 logged in to the PC 3 at 13:30 The student 1 logged out of the PC 1 at 13:40 The student 1 logged out of the PC 3 at 13:45 The student 1 logged in to the PC 1 at 14:20 The student 2 logged out of the PC 4 at 14:30 The student 1 logged out of the PC 1 at 14:40 For a query such as "Give usage of the student 1 between 13:00 and 14:30", your program should answer "55 minutes", that is, the sum of 45 minutes from 13:00 to 13:45 and 10 minutes from 14:20 to 14:30, as depicted in the following figure. Input The input is a sequence of a number of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 10. Each dataset is formatted as follows. N M r record 1 ... record r q query 1 ... query q The numbers N and M in the first line are the numbers of PCs and the students, respectively. r is the number of records. q is the number of queries. These four are integers satisfying the following. 1 ≀ N ≀ 1000, 1 ≀ M ≀ 10000, 2 ≀ r ≀ 1000, 1 ≀ q ≀ 50 Each record consists of four integers, delimited by a space, as follows. t n m s s is 0 or 1. If s is 1, this line means that the student m logged in to the PC n at time t . If s is 0, it means that the student m logged out of the PC n at time t . The time is expressed as elapsed minutes from 0:00 of the day. t , n and m satisfy the following. 540 ≀ t ≀ 1260, 1 ≀ n ≀ N , 1 ≀ m ≀ M You may assume the following about the records. Records are stored in ascending order of time t. No two records for the same PC has the same time t. No PCs are being logged in before the time of the first record nor after that of the last record in the file. Login and logout records for one PC appear alternatingly, and each of the login-logout record pairs is for the same student. Each query consists of three integers delimited by a space, as follows. t s t e m It represents "Usage of the student m between t s and t e ". t s , t e and m satisfy the following. 540 ≀ t s < t e ≀ 1260, 1 ≀ m ≀ M Output For each query, print a line having a decimal integer indicating the time of usage in minutes. Output lines should not have any character other than this number. Sample Input 4 2 10 775 1 1 1 780 4 2 1 790 2 1 1 800 2 1 0 810 3 1 1 820 1 1 0 825 3 1 0 860 1 1 1 870 4 2 0 880 1 1 0 1 780 870 1 13 15 12 540 12 13 1 600 12 13 0 650 13 15 1 660 12 15 1 665 11 13 1 670 13 15 0 675 11 13 0 680 12 15 0 1000 11 14 1 1060 12 14 1 1060 11 14 0 1080 12 14 0 3 540 700 13 600 1000 15 1000 1200 11 1 1 2 600 1 1 1 700 1 1 0 5 540 600 1 550 650 1 610 620 1 650 750 1 700 800 1 0 0 Output for the Sample Input 55 70 30 0 0 50 10 50 0
35,755
Score : 400 points Problem Statement We have a string S of length N consisting of R , G , and B . Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions: S_i \neq S_j , S_i \neq S_k , and S_j \neq S_k . j - i \neq k - j . Constraints 1 \leq N \leq 4000 S is a string of length N consisting of R , G , and B . Input Input is given from Standard Input in the following format: N S Output Print the number of triplets in question. Sample Input 1 4 RRGB Sample Output 1 1 Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count. Sample Input 2 39 RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB Sample Output 2 1800
35,756
Dudeney Number A Dudeney number is a positive integer for which the sum of its decimal digits is equal to the cube root of the number. For example, $512$ is a Dudeney number because it is the cube of $8$, which is the sum of its decimal digits ($5 + 1 + 2$). In this problem, we think of a type similar to Dudeney numbers and try to enumerate them. Given a non-negative integer $a$, an integer $n$ greater than or equal to 2 and an upper limit $m$, make a program to enumerate all $x$’s such that the sum of its decimal digits $y$ satisfies the relation $x = (y + a)^n$, and $x \leq m$. Input The input is given in the following format. $a$ $n$ $m$ The input line provides three integers: $a$ ($0 \leq a \leq 50$), $n$ ($2 \leq n \leq 10$) and the upper limit $m$ ($1000 \leq m \leq 10^8$). Output Output the number of integers that meet the above criteria. Sample Input 1 16 2 1000 Sample Output 1 2 Two: $400 = (4 + 0 + 0 + 16)^2$ and $841 = (8 + 4 + 1 + 16)^2$ Sample Input 2 0 3 5000 Sample Output 2 3 Three: $1=1^3$, $512 = (5 + 1 + 2)^3$ and $4913 = (4 + 9 + 1 + 3)^3$. Sample Input 3 2 3 100000 Sample Output 3 0 There is no such number $x$ in the range below $100,000$ such that its sum of decimal digits $y$ satisfies the relation $(y+2)^3 = x$.
35,757
Weighted Union Find Trees There is a sequence $A = a_0, a_1, ..., a_{n-1}$. You are given the following information and questions. relate$(x, y, z)$: $a_y$ is greater than $a_x$ by $z$ diff$(x, y)$: report the difference between $a_x$ and $a_y$ $(a_y - a_x)$ Input $n \; q$ $query_1$ $query_2$ : $query_q$ In the first line, $n$ and $q$ are given. Then, $q$ information/questions are given in the following format. 0 $x \; y\; z$ or 1 $x \; y$ where ' 0 ' of the first digit denotes the relate information and ' 1 ' denotes the diff question. Output For each diff question, print the difference between $a_x$ and $a_y$ $(a_y - a_x)$. Constraints $2 \leq n \leq 100,000$ $1 \leq q \leq 200,000$ $0 \leq x, y < n$ $x \ne y$ $0 \leq z \leq 10000$ There are no inconsistency in the given information Sample Input 5 6 0 0 2 5 0 1 2 3 1 0 1 1 1 3 0 1 4 8 1 0 4 Sample Output 2 ? 10
35,758
C: な◯りカット (Namo.. Cut) 問題 謎の巚倧クラゲ、 コヌドネヌム「な◯り」を蚎䌐せよ 「な◯り」は非垞に生呜力が匷いため、玠早く切断し続けなければ、あっずいう間に埩掻しおしたう。 我々は、「な◯り」をどのように切断するのが効率良いのか、日々詊行錯誀しおいる。 その過皋で、あなた方プログラマの手が必芁になった。 「な◯り」は N 頂点ず N 蟺からなる連結な無向グラフで衚珟できる。 以降、各頂点が 1 から N の異なる数で名前付けられおいるずしよう。 我々は、「な◯り」に関しお Q 回の質問を行う。 それらすべおに答えるプログラムを䜜成しお欲しい。 質問は 1 から Q の番号を持ち、各質問は次のように構成される。 質問 i では 2 ぀の頂点 a_i ず b_i が指定される。 a_i ず b_i を非連結にするために、削陀する必芁のある蟺の最小本数を答えよ。 ここで、頂点 u ず v が非連結であるずは、 u ず v を行き来できる経路が存圚しないこずを指す。 入力圢匏 N u_1 v_1 u_2 v_2 ... u_N v_N Q a_1 b_1 a_2 b_2 ... a_Q b_Q 入力はすべお敎数である。 1 行目には頂点数 N が䞎えられる。 続く N 行のうち i 行目には、 i 番目の蟺が繋ぐ 2 頂点の番号 u_i, v_i が空癜区切りで䞎えられる。 次に、質問の回数 Q が䞎えられる。 続く Q 行のうち i 行目には、 i 番目の質問で指定される 2 頂点の番号 a_i, b_i が空癜区切りで䞎えられる。 制玄 3 \leq N \leq 100,000 1 \leq Q \leq 100,000 グラフに自己ルヌプ及び倚重蟺は存圚しない 1 \leq a_i, b_i \leq N か぀ a_i \neq b_i ( 1 \leq i \leq Q ) 出力圢匏 出力は Q 行からなる。 i 行目には、 a_i ず b_i を非連結にするために、削陀する必芁のある蟺の最小本数を衚す敎数を出力せよ。 入力䟋1 3 1 2 1 3 2 3 1 1 3 出力䟋1 2 入力䟋2 7 1 2 1 6 3 5 2 5 5 4 1 4 3 7 3 2 4 3 1 6 7 出力䟋2 2 1 1
35,759
A Simple Offline Text Editor A text editor is a useful software tool that can help people in various situations including writing and programming. Your job in this problem is to construct an offline text editor, i.e., to write a program that first reads a given text and a sequence of editing commands and finally reports the text obtained by performing successively the commands in the given sequence. The editor has a text buffer and a cursor. The target text is stored in the text buffer and most editing commands are performed around the cursor. The cursor has its position that is either the beginning of the text, the end of the text, or between two consecutive characters in the text. The initial cursor position (i.e., the cursor position just after reading the initial text) is the beginning of the text. A text manipulated by the editor is a single line consisting of a sequence of characters, each of which must be one of the following: 'a' through 'z', 'A' through 'Z', '0' through '9', '.' (period), ',' (comma), and ' ' (blank). You can assume that any other characters never occur in the text buffer. You can also assume that the target text consists of at most 1,000 characters at any time. The definition of words in this problem is a little strange: a word is a non-empty character sequence delimited by not only blank characters but also the cursor. For instance, in the following text with a cursor represented as '^', He^llo, World. the words are the following. He llo, World. Notice that punctuation characters may appear in words as shown in this example. The editor accepts the following set of commands. In the command list, " any-text " represents any text surrounded by a pair of double quotation marks such as "abc" and "Co., Ltd.". Command Descriptions forward char Move the cursor by one character to the right, unless the cursor is already at the end of the text. forward word Move the cursor to the end of the leftmost word in the right. If no words occur in the right, move it to the end of the text. backward char Move the cursor by one character to the left, unless the cursor is already at the beginning of the text. backward word Move the cursor to the beginning of the rightmost word in the left. If no words occur in the left, move it to the beginning of the text. insert " any-text " Insert any-text (excluding double quotation marks) at the position specified by the cursor. After performing this command, the new cursor position is at the end of the inserted text. The length of any-text is less than or equal to 100. delete char Delete the character that is right next to the cursor, if it exists. delete word Delete the leftmost word in the right of the cursor. If one or more blank characters occur between the cursor and the word before performing this command, delete these blanks, too. If no words occur in the right, delete no characters in the text buffer. Input The first input line contains a positive integer, which represents the number of texts the editor will edit. For each text, the input contains the following descriptions: The first line is an initial text whose length is at most 100. The second line contains an integer M representing the number of editing commands. Each of the third through the M +2nd lines contains an editing command. You can assume that every input line is in a proper format or has no syntax errors. You can also assume that every input line has no leading or trailing spaces and that just a single blank character occurs between a command name (e.g., forward) and its argument (e.g., char). Output For each input text, print the final text with a character '^' representing the cursor position. Each output line shall contain exactly a single text with a character '^'. Sample Input 3 A sample input 9 forward word delete char forward word delete char forward word delete char backward word backward word forward word Hallow, Word. 7 forward char delete word insert "ello, " forward word backward char backward char insert "l" 3 forward word backward word delete word Output for the Sample Input Asampleinput^ Hello, Worl^d. ^
35,760
Playing with Stones Koshiro and Ukiko are playing a game with black and white stones. The rules of the game are as follows: Before starting the game, they define some small areas and place "one or more black stones and one or more white stones" in each of the areas. Koshiro and Ukiko alternately select an area and perform one of the following operations. (a) Remove a white stone from the area (b) Remove one or more black stones from the area. Note, however, that the number of the black stones must be less than or equal to white ones in the area. (c) Pick up a white stone from the stone pod and replace it with a black stone. There are plenty of white stones in the pod so that there will be no shortage during the game. If either Koshiro or Ukiko cannot perform 2 anymore, he/she loses. They played the game several times, with Koshiro’s first move and Ukiko’s second move, and felt the winner was determined at the onset of the game. So, they tried to calculate the winner assuming both players take optimum actions. Given the initial allocation of black and white stones in each area, make a program to determine which will win assuming both players take optimum actions. Input The input is given in the following format. $N$ $w_1$ $b_1$ $w_2$ $b_2$ : $w_N$ $b_N$ The first line provides the number of areas $N$ ($1 \leq N \leq 10000$). Each of the subsequent $N$ lines provides the number of white stones $w_i$ and black stones $b_i$ ($1 \leq w_i, b_i \leq 100$) in the $i$-th area. Output Output 0 if Koshiro wins and 1 if Ukiko wins. Sample Input 1 4 24 99 15 68 12 90 95 79 Sample Output 1 0 Sample Input 1 3 2 46 94 8 46 57 Sample Output 2 1
35,761
Score : 100 points Problem Statement Two students of AtCoder Kindergarten are fighting over candy packs. There are three candy packs, each of which contains a , b , and c candies, respectively. Teacher Evi is trying to distribute the packs between the two students so that each student gets the same number of candies. Determine whether it is possible. Note that Evi cannot take candies out of the packs, and the whole contents of each pack must be given to one of the students. Constraints 1 ≩ a, b, c ≩ 100 Input The input is given from Standard Input in the following format: a b c Output If it is possible to distribute the packs so that each student gets the same number of candies, print Yes . Otherwise, print No . Sample Input 1 10 30 20 Sample Output 1 Yes Give the pack with 30 candies to one student, and give the two packs with 10 and 20 candies to the other. Then, each gets 30 candies. Sample Input 2 30 30 100 Sample Output 2 No In this case, the student who gets the pack with 100 candies always has more candies than the other. Note that every pack must be given to one of them. Sample Input 3 56 25 31 Sample Output 3 Yes
35,762
Score : 400 points Problem Statement There are two persons, numbered 0 and 1 , and a variable x whose initial value is 0 . The two persons now play a game. The game is played in N rounds. The following should be done in the i -th round ( 1 \leq i \leq N ): Person S_i does one of the following: Replace x with x \oplus A_i , where \oplus represents bitwise XOR. Do nothing. Person 0 aims to have x=0 at the end of the game, while Person 1 aims to have x \neq 0 at the end of the game. Determine whether x becomes 0 at the end of the game when the two persons play optimally. Solve T test cases for each input file. Constraints 1 \leq T \leq 100 1 \leq N \leq 200 1 \leq A_i \leq 10^{18} S is a string of length N consisting of 0 and 1 . All numbers in input are integers. Input Input is given from Standard Input in the following format. The first line is as follows: T Then, T test cases follow. Each test case is given in the following format: N A_1 A_2 \cdots A_N S Output For each test case, print a line containing 0 if x becomes 0 at the end of the game, and 1 otherwise. Sample Input 1 3 2 1 2 10 2 1 1 10 6 2 3 4 5 6 7 111000 Sample Output 1 1 0 0 In the first test case, if Person 1 replaces x with 0 \oplus 1=1 , we surely have x \neq 0 at the end of the game, regardless of the choice of Person 0 . In the second test case, regardless of the choice of Person 1 , Person 0 can make x=0 with a suitable choice.
35,763
増えるE869120君 (Ninja E869120) E869120君は分身が埗意です。 ここに、$N$ 人のパ研郚員がいたす。しかし、このうちの䜕人かが E869120 君かもしれたせん。 そこで、あなたは $N$ 人党おのパ研郚員に名前を聞きたした。その結果、$N$ 人の郚員はそれぞれ $S_1, S_2, S_3, \dots, S_N$ ず名乗りたした。 E869120 君は䜕人に分身しおいたでしょうかただし、党おのパ研郚員は正盎に自分の名前を答えるものずしたす。 入力 入力は以䞋の圢匏で暙準入力から䞎えられる。 $N$ $S_1$ $S_2$ $S_3$ $\ldots$ $S_N$ 出力 E869120君が䜕人に分身しおいたか、その人数を出力しおください。ただし、E869120君がいない堎合は「0」ず出力しおください。 ただし、最埌には改行を入れるこず。 制玄 $1 \leq N \leq 1000$ $1 \leq (S_i$ の長さ$) \leq 100$ $N$ は敎数である。 $S_i$ は数字ずアルファベット倧文字からなる文字列である。 入力䟋1 5 E869120 TMJN E869120 TAISA YNYMXIAOLONGBAO 出力䟋1 2 E869120君は 2 人に分身しおいたす。 入力䟋2 3 SQUARE1001 MENCOTTON B2563125 出力䟋2 0 E869120君がいないずきは0を出力しおください。 入力䟋3 6 E8691200 E869121 E869122 E869123 E869124 E869125 出力䟋3 0 停者に泚意しおください。
35,764
Problem G: Riffle Swap You have a deck of 2 N cards (1 ≀ N ≀ 1000000) and want to have them shuffled. The most popular shuffling technique is probably the riffle shuffle, in which you split a deck into two halves, place them in your left and right hands, and then interleave the cards alternatively from those hands. The shuffle is called perfect when the deck is divided exactly in half and the cards are interleaved one-by-one from the bottom half. For example, the perfect riffle shuffle of a deck of eight cards <0, 1, 2, 3, 4, 5, 6, 7> will result in a deck <0, 4, 1, 5, 2, 6, 3, 7>. Since you are not so good at shuffling that you can perform the perfect riffle shuffle, you have decided to simulate the shuffle by swapping two cards as many times as needed. How many times you will have to perform swapping at least? As the resultant number will obviously become quite huge, your program should report the number modulo M = 1000003. Input The input just contains a single integer N . Output Print the number of swaps in a line. No extra space or empty line should occur. Sample Input and Output Input #1 1 Output #1 0 Input #2 2 Output #2 1 Input #3 3 Output #3 4 Input #4 4 Output #4 10 Input #5 10 Output #5 916
35,765
Score : 600 points Problem Statement There is a function f(x) , which is initially a constant function f(x) = 0 . We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows: An update query 1 a b : Given two integers a and b , let g(x) = f(x) + |x - a| + b and replace f(x) with g(x) . An evaluation query 2 : Print x that minimizes f(x) , and the minimum value of f(x) . If there are multiple such values of x , choose the minimum such value. We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points. Constraints All values in input are integers. 1 \leq Q \leq 2 \times 10^5 -10^9 \leq a, b \leq 10^9 The first query is an update query. Input Input is given from Standard Input in the following format: Q Query_1 : Query_Q See Sample Input 1 for an example. Output For each evaluation query, print a line containing the response, in the order in which the queries are given. The response to each evaluation query should be the minimum value of x that minimizes f(x) , and the minimum value of f(x) , in this order, with space in between. Sample Input 1 4 1 4 2 2 1 1 -8 2 Sample Output 1 4 2 1 -3 In the first evaluation query, f(x) = |x - 4| + 2 , which attains the minimum value of 2 at x = 4 . In the second evaluation query, f(x) = |x - 1| + |x - 4| - 6 , which attains the minimum value of -3 when 1 \leq x \leq 4 . Among the multiple values of x that minimize f(x) , we ask you to print the minimum, that is, 1 . Sample Input 2 4 1 -1000000000 1000000000 1 -1000000000 1000000000 1 -1000000000 1000000000 2 Sample Output 2 -1000000000 3000000000
35,766
Score : 500 points Problem Statement You are given two integer sequences, each of length N : a_1, ..., a_N and b_1, ..., b_N . There are N^2 ways to choose two integers i and j such that 1 \leq i, j \leq N . For each of these N^2 pairs, we will compute a_i + b_j and write it on a sheet of paper. That is, we will write N^2 integers in total. Compute the XOR of these N^2 integers. Definition of XOR The XOR of integers c_1, c_2, ..., c_m is defined as follows: Let the XOR be X . In the binary representation of X , the digit in the 2^k 's place ( 0 \leq k ; k is an integer) is 1 if there are an odd number of integers among c_1, c_2, ...c_m whose binary representation has 1 in the 2^k 's place, and 0 if that number is even. For example, let us compute the XOR of 3 and 5 . The binary representation of 3 is 011 , and the binary representation of 5 is 101 , thus the XOR has the binary representation 110 , that is, the XOR is 6 .
35,767
Problem E: Light Problem $W \times H$ の二次元のマス䞊に$N$個の街灯がある。 がっちょ君は$(1,1)$からスタヌトしお$(W,H)$に行きたい。 がっちょ君は暗いずころが怖いので、街灯により明るくなっおいるマスしか歩きたくない。 最初、すべおの街灯はその街灯のあるマスのみを明るくしおいる。 そこで、がっちょ君は奜きな街灯$i$に察しおコスト$r_i$を蚭定するこずにした。なお、コストを蚭定しない街灯があっおもよい。 街灯$i$はコスト$r_i$を消費するこずにより、その街灯を䞭心にマンハッタン距離で$r_i$以内の範囲を明るくするこずができる。ただしコストは正の敎数ずする。 がっちょ君は䞊䞋巊右のいずれかの方向に隣接するマスに移動する事ができる。 がっちょ君は$r_i$の合蚈倀を最小になるように蚭定するこずにした。そのずきの合蚈倀を求めよ。 2぀の地点 $(a,b)$ ず $(c,d)$ 間のマンハッタン距離は $|a−c|$+$|b−d|$ で衚される。 Input 入力は以䞋の圢匏で䞎えられる。 $W$ $H$ $N$ $x_1$ $y_1$ ... $x_N$ $y_N$ 入力はすべお敎数で䞎えられる。 1行目に$W$ず$H$ず$N$が空癜区切りで䞎えられる。 続く$N$行に街灯$i$の座暙$($$x_i$,$y_i$$)$が空癜区切りで䞎えられる。 Constraints 入力は以䞋の条件を満たす。 $1 \leq W \leq 500$ $1 \leq H \leq 500$ $1 \leq N \leq 100$ $1 \leq N \leq W \times H$ $1 \leq $$x_i$$ \leq W$ $1 \leq $$y_i$$ \leq H$ 同じ座暙に耇数の街灯はない Output $r_i$の合蚈倀の最小倀を1行に出力せよ。 Sample Input 1 10 10 1 6 6 Sample Output 1 10 Sample Input 2 5 10 3 3 9 2 8 5 1 Sample Output 2 8 Sample Input 3 1 1 1 1 1 Sample Output 3 0
35,768
宇宙船 UAZ アドバンス号 恒星歎 2005.11.5。あなたは宇宙船 UAZ アドバンス号の艊長ずしお敵の宇宙船ず亀戊しようずしおいたす。 幞い敵の宇宙船はただこちらに気付かずに静止しおいたす。たた、すでに敵の宇宙座暙は刀明しおおり匷力な盎線のビヌムを攟぀「フェザヌ砲」は発射準備を完了しおいたす。あずは、発射呜什を出すばかりです。 ずころが、宇宙空間には、敵の蚭眮した゚ネルギヌバリアが存圚しおいたす。バリアは䞉角圢をしおおり「フェザヌ砲」のビヌムをはね返しおしたいたす。たた、ビヌムがバリアに圓たれば敵に気付かれお逃げられおしたいたす。事前に呜䞭するず刀定できなければ、発射呜什は出せたせん。 そこで、UAZ アドバンス号、敵、バリアの䜍眮の宇宙座暙(3次元座暙 x , y , z ) を入力しお、ビヌムがバリアをさけお敵に呜䞭する堎合は "HIT"、バリアに圓たっおしたう堎合"MISS"ず出力するプログラムを䜜成しおください。 ただし、バリアはアドバンス号から 3 角圢に芋えるものだけが察象であり、線分に぀ぶれお芋えるものはないものずしたす。たた、バリアは 3 角圢の頂点を含む境界でも有効であり、ビヌムをはね返すものずしたす。たた、敵がバリア内にいる堎合は"MISS"ず出力しお䞋さい。 Input 入力デヌタの圢匏は以䞋のずおりです 1 行目は、UAZ アドバンス号の座暙 ( x , y , z ) (敎数、半角空癜区切り) 2 行目は、敵の座暙 ( x , y , z ) (敎数、半角空癜区切り) 3 行目は、バリアの頂点 1 の座暙 ( x , y , z ) (敎数、半角空癜区切り) 4 行目は、バリアの頂点 2 の座暙 ( x , y , z ) (敎数、半角空癜区切り) 5 行目は、バリアの頂点 3 の座暙 ( x , y , z ) (敎数、半角空癜区切り) Output HIT たたは MISS ず行に出力しおください。 Constraints -100 ≀ x , y , z ≀ 100 UAZ アドバンス号ず敵が同じ䜍眮にいるこずはない。 Sample Input 1 -10 0 0 10 0 0 0 10 0 0 10 10 0 0 10 Output for the Sample Input 1 HIT Sample Input 2 -10 6 6 10 6 6 0 10 0 0 10 10 0 0 10 Output for the Sample Input 2 MISS
35,769
E: Expensive Function 問題文 数列$a$ず非負敎数䞊の関数$f(x)$が以䞋のように定矩されおいたす。 $a_1 = 0$ $a_i = (a_{i-1} \times p + q) \bmod M (i \geq2)$ $f(x) = x \mbox{ XOR } a_1 \mbox{ XOR } a_2 \mbox{ XOR } ... \mbox{ XOR } a_{10^8}$ 束厎くんが蚈算するず、$f(s) = t$ でした。 非負敎数 $y$ が䞎えられるので、$f(y)$ を求めおください。 $n$ 個の非負敎数 $x_1, x_2, \ldots, x_n$ の排他的論理和 $x_1 \mbox{ XOR } x_2 \mbox{ XOR } \ldots \mbox{ XOR } x_n$ は以䞋のように定矩されたす: $x_1 \mbox{ XOR } x_2 \mbox{ XOR } \ldots \mbox{ XOR } x_n$ を二進衚蚘した際の $2^k ( k \geq 0 )$ の䜍の数は $x_1, x_2, \ldots, x_n$ のうち、二進衚蚘した際の $2^k ( k \geq 0 )$ の䜍の数が $1$ ずなるものの個数が奇数ならば $1$ 、そうでなければ $0$ ずなる。 制玄 $0 \leq p, q, s, t, y \leq 10^9$ $1 \leq M \leq 10^9$ 入力は党お敎数 入力 入力は以䞋の圢匏で暙準入力から䞎えられたす。 $s$ $t$ $p$ $q$ $M$ $y$ 出力 $f(y)$ を出力しおください。 入出力䟋 入力䟋1 0 15656 3 7 13333 0 出力䟋1 15656 入力䟋2 0 0 0 0 3 0 出力䟋2 0 入力䟋3 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 出力䟋3 1000000000
35,770
Score : 300 points Problem Statement N persons are standing in a row. The height of the i -th person from the front is A_i . We want to have each person stand on a stool of some heights - at least zero - so that the following condition is satisfied for every person: Condition: Nobody in front of the person is taller than the person. Here, the height of a person includes the stool. Find the minimum total height of the stools needed to meet this goal. Constraints 1 \leq N \leq 2\times 10^5 1 \leq A_i \leq 10^9 All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 \ldots A_N Output Print the minimum total height of the stools needed to meet the goal. Sample Input 1 5 2 1 5 4 3 Sample Output 1 4 If the persons stand on stools of heights 0 , 1 , 0 , 1 , and 2 , respectively, their heights will be 2 , 2 , 5 , 5 , and 5 , satisfying the condition. We cannot meet the goal with a smaller total height of the stools. Sample Input 2 5 3 3 3 3 3 Sample Output 2 0 Giving a stool of height 0 to everyone will work.
35,771
JOI囜のお散歩事情 (Walking in JOI Kingdom) 問題 JOI 囜には東西に走る 1 本の十分に長い道路があるJOI 囜の王宮が道路沿いにありJOI 囜における道路沿いの䜍眮は敎数 A で衚されるA = 0 のずきは王宮の䜍眮を衚すA > 0 のずきは王宮から東ぞ A メヌトル進んだ䜍眮を衚すA < 0 のずきは王宮から西ぞ -A メヌトル進んだ䜍眮を衚す JOI 囜の道路沿いには N 軒の家があり家には西から順に 1 から N たでの番号が付けられおいるJOI 囜には N 人の囜民がいお囜民には 1 から N たでの番号が付けられおいる家 i には囜民 i が䜏んでいる家 i の䜍眮は 0 でない偶数 A i で衚されるA 1 , ..., A N は党お異なる JOI 囜では近幎囜民の運動䞍足が問題になっおいる囜民の健康が気になった JOI 囜の王様は囜民党員に散歩をする呜什を出した王様が呜什を出すず党おの囜民は䞀斉に東向きたたは西向きに歩き始めるそれぞれの囜民がどちらの向きに歩き始めるかは囜民ごずに決たっおいる党おの囜民は歩くずきは 1 秒あたり 1 メヌトルの速床で歩く JOI 囜の囜民は皆おしゃべりが倧奜きである散歩の途䞭にほかの囜民に出䌚うずその堎所で立ち止たっお䞖間話を始めおしたうすでに立ち止たっおいる囜民に出䌚った堎合も同様である䞀床立ち止たった囜民は再び歩き出すこずはない JOI 囜には Q 人の重芁人物がいるJOI 囜の王様は呜什が出されおから T 秒埌のQ 人の重芁人物の䜍眮を把握しおおきたい呜什が出されおから T 秒埌のQ 人の重芁人物の䜍眮を求めるプログラムを䜜成せよ 入力 入力は1 + N + Q 行からなる 1 行目には3 ぀の敎数 NTQ (1 ≩ N ≩ 100000 (= 10 5 ), 0 ≩ T ≩ 10 18 , 1 ≩ Q ≩ 10001 ≩ Q ≩ N) が空癜を区切りずしお曞かれおいるこれはJOI 囜に家が N 軒あり王様が呜什を出しおから T 秒埌のQ 人の重芁人物の䜍眮を把握しおおきたいこずを衚す 続く N 行のうち i 行目には2 ぀の敎数 A i , D i (-10 18 ≩ A i ≩ 10 18 , A i は 0 でない偶数, 1 ≩ D i ≩ 2) が空癜を区切りずしお曞かれおいるA i は家 i の䜍眮を衚す偶数であるすべおの i (1 ≩ i ≩ N - 1) に぀いおA i < A i+1 を満たすD i は呜什が出された埌に囜民 i が歩き始める方向を衚すD i = 1 のずきは囜民 i は東向きに歩き始めるD i = 2 のずきは囜民 i は西向きに歩き始める 続く Q 行のうち i 行目には敎数 X i (1 ≩ X i ≩ N) が曞かれおいるこれはi 番目の重芁人物が家 X i に䜏んでいるこずを衚すすべおの i (1 ≩ i ≩ Q - 1) に぀いおX i < X i+1 を満たす 䞎えられる 5 ぀の入力デヌタのうち入力 1 では N ≩ 100T ≩ 10000を満たすたた入力 2 では N ≩ 5000 を満たすたた入力 3 ではある敎数 M (1 ≩ M ≩ N - 1) があっおすべおの i (1 ≩ i ≩ M) に぀いお D i = 1すべおの j (M + 1 ≩ j ≩ N) に぀いお D j = 2 を満たすたた入力 123 では入力に䞎えられる敎数の絶察倀は 1000000000 (= 10 9 ) を超えない入力 45 では䞎えられる敎数が 32 ビット笊号付き敎数の範囲に収たらないこずに泚意せよ 出力 出力は Q 行からなる i 行目 (1 ≩ i ≩ Q) には王様が呜什を出しおから T 秒埌のi 番目の重芁人物の䜍眮を衚す敎数を出力せよこの倀が敎数であるこずは問題文の条件より保蚌されおいる 入出力䟋 入力䟋 1 5 5 3 -8 1 -4 2 -2 2 4 2 10 1 1 3 5 出力䟋 1 -6 -6 15 入力䟋 2 7 18 5 -100 1 -56 2 -34 1 -30 1 -22 1 -4 2 18 2 1 3 4 5 7 出力䟋 2 -82 -16 -13 -13 0 情報オリンピック日本委員䌚䜜 『第 15 回日本情報オリンピック JOI 2015/2016 予遞競技課題』
35,772
Problem Statement We have planted $N$ flower seeds, all of which come into different flowers. We want to make all the flowers come out together. Each plant has a value called vitality, which is initially zero. Watering and spreading fertilizers cause changes on it, and the $i$-th plant will come into flower if its vitality is equal to or greater than $\mathit{th}_i$. Note that $\mathit{th}_i$ may be negative because some flowers require no additional nutrition. Watering effects on all the plants. Watering the plants with $W$ liters of water changes the vitality of the $i$-th plant by $W \times \mathit{vw}_i$ for all $i$ ($1 \le i \le n$), and costs $W \times \mathit{pw}$ yen, where $W$ need not be an integer. $\mathit{vw}_i$ may be negative because some flowers hate water. We have $N$ kinds of fertilizers, and the $i$-th fertilizer effects only on the $i$-th plant. Spreading $F_i$ kilograms of the $i$-th fertilizer changes the vitality of the $i$-th plant by $F_i \times \mathit{vf}_i$, and costs $F_i \times \mathit{pf}_i$ yen, where $F_i$ need not be an integer as well. Each fertilizer is specially made for the corresponding plant, therefore $\mathit{vf}_i$ is guaranteed to be positive. Of course, we also want to minimize the cost. Formally, our purpose is described as "to minimize $W \times \mathit{pw} + \sum_{i=1}^{N}(F_i \times \mathit{pf}_i)$ under $W \times \mathit{vw}_i + F_i \times \mathit{vf}_i \ge \mathit{th}_i$, $W \ge 0$, and $F_i \ge 0$ for all $i$ ($1 \le i \le N$)". Your task is to calculate the minimum cost. Input The input consists of multiple datasets. The number of datasets does not exceed $100$, and the data size of the input does not exceed $20\mathrm{MB}$. Each dataset is formatted as follows. $N$ $\mathit{pw}$ $\mathit{vw}_1$ $\mathit{pf}_1$ $\mathit{vf}_1$ $\mathit{th}_1$ : : $\mathit{vw}_N$ $\mathit{pf}_N$ $\mathit{vf}_N$ $\mathit{th}_N$ The first line of a dataset contains a single integer $N$, number of flower seeds. The second line of a dataset contains a single integer $\mathit{pw}$, cost of watering one liter. Each of the following $N$ lines describes a flower. The $i$-th line contains four integers, $\mathit{vw}_i$, $\mathit{pf}_i$, $\mathit{vf}_i$, and $\mathit{th}_i$, separated by a space. You can assume that $1 \le N \le 10^5$, $1 \le \mathit{pw} \le 100$, $-100 \le \mathit{vw}_i \le 100$, $1 \le \mathit{pf}_i \le 100$, $1 \le \mathit{vf}_i \le 100$, and $-100 \le \mathit{th}_i \le 100$. The end of the input is indicated by a line containing a zero. Output For each dataset, output a line containing the minimum cost to make all the flowers come out. The output must have an absolute or relative error at most $10^{-4}$. Sample Input 3 10 4 3 4 10 5 4 5 20 6 5 6 30 3 7 -4 3 4 -10 5 4 5 20 6 5 6 30 3 1 -4 3 4 -10 -5 4 5 -20 6 5 6 30 3 10 -4 3 4 -10 -5 4 5 -20 -6 5 6 -30 0 Output for the Sample Input 43.5 36 13.5 0
35,773
Score : 300 points Problem Statement You are given an integer sequence of length N . The i -th term in the sequence is a_i . In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? For every i (1≀i≀n) , the sum of the terms from the 1 -st through i -th term is not zero. For every i (1≀i≀n-1) , the sign of the sum of the terms from the 1 -st through i -th term, is different from the sign of the sum of the terms from the 1 -st through (i+1) -th term. Constraints 2 ≀ n ≀ 10^5 |a_i| ≀ 10^9 Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Sample Input 1 4 1 -3 1 0 Sample Output 1 4 For example, the given sequence can be transformed into 1, -2, 2, -2 by four operations. The sums of the first one, two, three and four terms are 1, -1, 1 and -1 , respectively, which satisfy the conditions. Sample Input 2 5 3 -6 4 -5 7 Sample Output 2 0 The given sequence already satisfies the conditions. Sample Input 3 6 -1 4 3 2 -5 4 Sample Output 3 8
35,774
Problem F: Shredding Company You have just been put in charge of developing a new shredder for the Shredding Company. Although a ``normal'' shredder would just shred sheets of paper into little pieces so that the contents would become unreadable, this new shredder needs to have the following unusual basic characteristics. The shredder takes as input a target number and a sheet of paper with a number written on it. It shreds (or cuts) the sheet into pieces each of which has one or more digits on it. The sum of the numbers written on each piece is the closest possible number to the target number, without going over it. For example, suppose that the target number is 50 , and the sheet of paper has the number 12346 . The shredder would cut the sheet into four pieces, where one piece has 1 , another has 2 , the third has 34 , and the fourth has 6 . This is because their sum 43 (= 1 + 2 + 34 + 6) is closest to the target number 50 of all possible combinations without going over 50. For example, a combination where the pieces are 1 , 23 , 4 , and 6 is not valid, because the sum of this combination 34 (= 1 + 23 + 4 + 6) is less than the above combination's 43. The combination of 12 , 34 , and 6 is not valid either, because the sum 52 (= 12+34+6) is greater than the target number of 50. Figure 1. Shredding a sheet of paper having the number 12346 when the target number is 50 There are also three special rules: If the target number is the same as the number on the sheet of paper, then the paper is not cut. For example, if the target number is 100 and the number on the sheet of paper is also 100 , then the paper is not cut. If it is not possible to make any combination whose sum is less than or equal to the target number, then error is printed on a display. For example, if the target number is 1 and the number on the sheet of paper is 123 , it is not possible to make any valid combination, as the combination with the smallest possible sum is 1 , 2 , 3 . The sum for this combination is 6, which is greater than the target number, and thus error is printed. If there is more than one possible combination where the sum is closest to the target number without going over it, then rejected is printed on a display. For example, if the target number is 15 , and the number on the sheet of paper is 111 , then there are two possible combinations with the highest possible sum of 12: (a) 1 and 11 and (b) 11 and 1 ; thus rejected is printed. In order to develop such a shredder, you have decided to first make a simple program that would simulate the above characteristics and rules. Given two numbers, where the first is the target number and the second is the number on the sheet of paper to be shredded, you need to figure out how the shredder should ``cut up'' the second number. Input The input consists of several test cases, each on one line, as follows: t 1 num 1 t 2 num 2 ... t n num n 0 0 Each test case consists of the following two positive integers, which are separated by one space: (1) the first integer ( t i above) is the target number; (2) the second integer ( num i above) is the number that is on the paper to be shredded. Neither integers may have a 0 as the first digit, e.g., 123 is allowed but 0123 is not. You may assume that both integers are at most 6 digits in length. A line consisting of two zeros signals the end of the input. Output For each test case in the input, the corresponding output takes one of the following three types: sum part 1 part 2 ... rejected error In the first type, part j and sum have the following meaning: Each part j is a number on one piece of shredded paper. The order of part j corresponds to the order of the original digits on the sheet of paper. sum is the sum of the numbers after being shredded, i.e., sum = part 1 + part 2 + ... . Each number should be separated by one space. The message "error" is printed if it is not possible to make any combination, and "rejected" if there is more than one possible combination. No extra characters including spaces are allowed at the beginning of each line, nor at the end of each line. Sample Input 50 12346 376 144139 927438 927438 18 3312 9 3142 25 1299 111 33333 103 862150 6 1104 0 0 Output for the Sample Input 43 1 2 34 6 283 144 139 927438 927438 18 3 3 12 error 21 1 2 9 9 rejected 103 86 2 15 0 rejected
35,775
Score : 100 points Problem Statement AtCoDeer has three cards, one red, one green and one blue. An integer between 1 and 9 (inclusive) is written on each card: r on the red card, g on the green card and b on the blue card. We will arrange the cards in the order red, green and blue from left to right, and read them as a three-digit integer. Is this integer a multiple of 4 ? Constraints 1 ≀ r, g, b ≀ 9 Input Input is given from Standard Input in the following format: r g b Output If the three-digit integer is a multiple of 4 , print YES (case-sensitive); otherwise, print NO . Sample Input 1 4 3 2 Sample Output 1 YES 432 is a multiple of 4 , and thus YES should be printed. Sample Input 2 2 3 4 Sample Output 2 NO 234 is not a multiple of 4 , and thus NO should be printed.
35,776
Score : 100 points Problem Statement N of us are going on a trip, by train or taxi. The train will cost each of us A yen (the currency of Japan). The taxi will cost us a total of B yen. How much is our minimum total travel expense? Constraints All values in input are integers. 1 \leq N \leq 20 1 \leq A \leq 50 1 \leq B \leq 50 Input Input is given from Standard Input in the following format: N A B Output Print an integer representing the minimum total travel expense. Sample Input 1 4 2 9 Sample Output 1 8 The train will cost us 4 \times 2 = 8 yen, and the taxi will cost us 9 yen, so the minimum total travel expense is 8 yen. Sample Input 2 4 2 7 Sample Output 2 7 Sample Input 3 4 2 8 Sample Output 3 8
35,777
Problem F: The Castle 埅ち䌏せおいた敵を芋事撃砎したうさぎは, 䞻人公を敵の城内に進めるこずに成功した. 䞻人公が城の地䞋牢に捕らわれおいたねこたちを解攟したずころ, 圌らのうちの䜕匹かが䞻人公の力になっおくれるこずになった. ねこたちの話では, 城の奥にいる魔王に蟿り぀くには1 番から n 番の n 個の郚屋をこの順に通り抜けるこずになるが, 各郚屋には1 䜓ず぀敵が埅ち受けおいお逐䞀倒しおいかなければならないずいう. 仲間になった m 匹のねこそれぞれに぀いお, 各郚屋の敵それぞれに察する勝率が分かっおおり, 䞻人公はこのねこたちを1 匹ず぀城の奥ぞ向けお掟遣する. 各郚屋はそこにいる敵を倒しおからでなければ通過できないので, あるねこがある郚屋の敵にやられたら, 次のねこはその郚屋の敵から戊っおいくこずになる. 掟遣されたねこは敵にやられるたで進むが, 掟遣したねこがどの郚屋の敵にやられたかは毎回知るこずができ, それによっお次にどのねこを掟遣するかを決めるこずができる. どのようにすれば, ねこたちが埅ち受けるすべおの敵を倒せる確率が最倧になるだろうか. Input 入力の䞀行目には m ず n がスペヌスで区切られお䞎えられる. 1 ≀ m , n ≀ 16 ぀づく m 行には, 猫が敵に勝぀確率を衚す n 個の実数が䞎えられる. i + 1 行目の j 個目の実数は, j 番の郚屋の敵に勝぀確率を衚しおいる. 確率は小数点以䞋3 桁たで. Output ねこたちが埅ち受けるすべおの敵を倒せる確率が最倧になるようにうたく順番を決めおいくずき, その確率を答えよ. 出力は誀差を含んでいおもよいが, 真の倀ずの誀差は10 −9 以内にせよ. Sample Input 1 2 3 0.900 0.500 0.100 0.500 0.500 0.500 Sample Output 1 0.372500000000
35,778
Score : 100 points Problem Statement In "Takahashi-ya", a ramen restaurant, a bowl of ramen costs 700 yen (the currency of Japan), plus 100 yen for each kind of topping (boiled egg, sliced pork, green onions). A customer ordered a bowl of ramen and told which toppings to put on his ramen to a clerk. The clerk took a memo of the order as a string S . S is three characters long, and if the first character in S is o , it means the ramen should be topped with boiled egg; if that character is x , it means the ramen should not be topped with boiled egg. Similarly, the second and third characters in S mean the presence or absence of sliced pork and green onions on top of the ramen. Write a program that, when S is given, prints the price of the corresponding bowl of ramen. Constraints S is a string of length 3 . Each character in S is o or x . Input Input is given from Standard Input in the following format: S Output Print the price of the bowl of ramen corresponding to S . Sample Input 1 oxo Sample Output 1 900 The price of a ramen topped with two kinds of toppings, boiled egg and green onions, is 700 + 100 \times 2 = 900 yen. Sample Input 2 ooo Sample Output 2 1000 The price of a ramen topped with all three kinds of toppings is 700 + 100 \times 3 = 1000 yen. Sample Input 3 xxx Sample Output 3 700 The price of a ramen without any toppings is 700 yen.
35,779
Problem H: Brainf*ck BさんはBrainf*ckが倧奜きで、孊校で出された課題も党おBrainf*ckを䜿っお提出しおいる。 最近そんなBさんに掗脳され、先生が課題を解く蚀語をBrainf*ckに限定しおしたった。 このたたでは皆が単䜍を萜ずしおしたう。 あなたは、Brainf*ckプログラムを生成するプログラムを䜜るこずで、皆の単䜍を救い出す手䌝いをするこずにした。 もちろん、Brainf*ckプログラムを生成するプログラムはBrainf*ckで蚘述する必芁はない。 問題 指定された文字列\( s \)を出力するBrainf*ckプログラムを生成せよ。 Brainf*ck蚀語仕様 ゞャッゞで甚いるBrainf*ckの蚀語仕様を蚘す。 Brainf*ckプログラムは文字列で蚘述される。 プログラム文字列は角括匧( [ および ] )の察応が取れおいる必芁があるが、それ以倖の制限はない。 Brainf*ckプログラムが実行される時、バむト配列ずその芁玠を指し瀺すポむンタヌを持぀。バむト配列は無限の倧きさを持ち、8bit非負敎数の情報を保持するこずができる。 これをC蚀語で衚すず次のようになる。 unsigned char memory[100000]; // バむト配列実際にはもっず広倧な領域を確保する unsigned char *ptr = memory; // バむト配列の芁玠を指し瀺すポむンタヌ Brainf*ckでは1぀の呜什は1文字で衚され、呜什には以䞋の7皮類がある。 文字 意味 C蚀語での蚘述法 + ポむンタヌが瀺すバむト配列の芁玠の倀を1増やす。倀が255の時は0になる。 (*ptr)++; - ポむンタヌが瀺すバむト配列の芁玠の倀を1枛らす。倀が0の時は255になる。 (*ptr)--; > ポむンタヌが瀺すバむト配列の芁玠の䜍眮を正に1ずらす。 ptr++; < ポむンタヌが瀺すバむト配列の芁玠の䜍眮を負に1ずらす。 ptr--; [ ポむンタヌが瀺すバむト配列の芁玠の倀が0ならば、察応する ] に飛ぶ。そうでなければ次の呜什に進む。 while(*ptr) { ] ポむンタヌが瀺すバむト配列の芁玠の倀が0ならば、察応する [ に飛ぶ。そうでなければ次の呜什に進む。 } do while(*ptr); . ポむンタヌが瀺すバむト配列の芁玠の倀をASCIIコヌドず芋なし、文字を出力する。 putchar(*ptr); 呜什は先頭から順に実行され、呜什を衚す文字ではない文字はコメントずしお無芖される。 以䞊のBrainf*ckの仕様はおおよそ䞀般に䜿われおいるBrainf*ckず同様なものなので、 http://ja.wikipedia.org/wiki/Brainfuck などを参考にするこずができる。ただし、 ゞャッゞに甚いるBrainf*ck は、䞀文字入力呜什を䜿甚するこずはできないので泚意するこず。 入力 1行に1000文字以内の文字列\( s \)が䞎えられる。 出力 Brainf*ckコヌドを20000文字以内で出力せよ。実行結果が\( s \)に䞀臎した任意のプログラムがAcceptされる。 制玄 \( 1 \leq |s| \leq 1000 \) \( s \)はASCII文字列 \( s \)にはASCIIコヌド33〜126の文字しか含たれない蚘号、英字、数字のみで空癜や制埡文字を含たない 出力したプログラムの長さは空癜、改行、コメントを含めお\( 20000 \)文字たでを蚱容する 呜什は\( 10^7 \)回実行されるずストップする Brainf*ckプログラムは最埌に改行を出力しおはならない 入出力䟋 入力1 ABC 出力1 ++++++++[>++++++++<-]>+.+.+. 入力2 HelloWorld!! 出力2 +++++++++[>++++++++<-]>.<+++++[>+++++<-]>++++.+++++++..+++.[>+>+<<-]++++[>------<-]>.>.+++.-- ----.--------.[-]++++++[>+++++<-]>+++.. 入力に空癜が含たれるこずはない。 入力3 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ 出力3 +++++++++++++++++++++++++++++++++.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+ +.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+ .+.+.+.+.+.+.+.+.+.+.+.+.+.+.+.+. このケヌスは33から126のASCIIコヌドを順番に䞊べたものである。
35,780
Problem H Animal Companion in Maze George, your pet monkey, has escaped, slipping the leash! George is hopping around in a maze-like building with many rooms. The doors of the rooms, if any, lead directly to an adjacent room, not through corridors. Some of the doors, however, are one-way: they can be opened only from one of their two sides. He repeats randomly picking a door he can open and moving to the room through it. You are chasing him but he is so quick that you cannot catch him easily. He never returns immediately to the room he just has come from through the same door, believing that you are behind him. If any other doors lead to the room he has just left, however, he may pick that door and go back. If he cannot open any doors except one through which he came from, voila, you can catch him there eventually. You know how rooms of the building are connected with doors, but you don't know in which room George currently is. It takes one unit of time for George to move to an adjacent room through a door. Write a program that computes how long it may take before George will be confined in a room. You have to find the longest time, considering all the possibilities of the room George is in initially, and all the possibilities of his choices of doors to go through. Note that, depending on the room organization, George may have possibilities to continue hopping around forever without being caught. Doors may be on the ceilings or the floors of rooms; the connection of the rooms may not be drawn as a planar graph. Input The input consists of a single test case, in the following format. $n$ $m$ $x_1$ $y_1$ $w_1$ . . . $x_m$ $y_m$ $w_m$ The first line contains two integers $n$ ($2 \leq n \leq 100000$) and $m$ ($1 \leq m \leq 100000$), the number of rooms and doors, respectively. Next $m$ lines contain the information of doors. The $i$-th line of these contains three integers $x_i$, $y_i$ and $w_i$ ($1 \leq x_i \leq n, 1 \leq y_i \leq n, x_i \ne y_i, w_i = 1$ or $2$), meaning that the $i$-th door connects two rooms numbered $x_i$ and $y_i$, and it is one-way from $x_i$ to $y_i$ if $w_i = 1$, two-way if $w_i = 2$. Output Output the maximum number of time units after which George will be confined in a room. If George has possibilities to continue hopping around forever, output " Infinite ". Sample Input 1 2 1 1 2 2 Sample Output 1 1 Sample Input 2 2 2 1 2 1 2 1 1 Sample Output 2 Infinite Sample Input 3 6 7 1 3 2 3 2 1 3 5 1 3 6 2 4 3 1 4 6 1 5 2 1 Sample Output 3 4 Sample Input 4 3 2 1 3 1 1 3 1 Sample Output 4 1
35,781
Score : 200 points Problem Statement Print all the integers that satisfies the following in ascending order: Among the integers between A and B (inclusive), it is either within the K smallest integers or within the K largest integers. Constraints 1 \leq A \leq B \leq 10^9 1 \leq K \leq 100 All values in input are integers. Input Input is given from Standard Input in the following format: A B K Output Print all the integers that satisfies the condition above in ascending order. Sample Input 1 3 8 2 Sample Output 1 3 4 7 8 3 is the first smallest integer among the integers between 3 and 8 . 4 is the second smallest integer among the integers between 3 and 8 . 7 is the second largest integer among the integers between 3 and 8 . 8 is the first largest integer among the integers between 3 and 8 . Sample Input 2 4 8 3 Sample Output 2 4 5 6 7 8 Sample Input 3 2 9 100 Sample Output 3 2 3 4 5 6 7 8 9
35,782
Problem E: Spirograph Some of you might have seen instruments like the figure below. Figure 1: Spirograph There are a fixed circle (indicated by A in the figure) and a smaller interior circle with some pinholes (indicated by B ). By putting a pen point through one of the pinholes and then rolling the circle B without slipping around the inside of the circle A , we can draw curves as illustrated below. Such curves are called hypotrochoids . Figure 2: An Example Hypotrochoid Your task is to write a program that calculates the length of hypotrochoid, given the radius of the fixed circle A , the radius of the interior circle B , and the distance between the B ’s centroid and the used pinhole. Input The input consists of multiple test cases. Each test case is described by a single line in which three integers P , Q and R appear in this order, where P is the radius of the fixed circle A , Q is the radius of the interior circle B , and R is the distance between the centroid of the circle B and the pinhole. You can assume that 0 ≀ R < Q < P ≀ 1000. P , Q , and R are separated by a single space, while no other spaces appear in the input. The end of input is indicated by a line with P = Q = R = 0. Output For each test case, output the length of the hypotrochoid curve. The error must be within 10 -2 (= 0.01). Sample Input 3 2 1 3 2 0 0 0 0 Output for the Sample Input 13.36 6.28
35,783
Score : 300 points Problem Statement Tak has N cards. On the i -th (1 \leq i \leq N) card is written an integer x_i . He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A . In how many ways can he make his selection? Constraints 1 \leq N \leq 50 1 \leq A \leq 50 1 \leq x_i \leq 50 N,\,A,\,x_i are integers. Partial Score 200 points will be awarded for passing the test set satisfying 1 \leq N \leq 16 . Input The input is given from Standard Input in the following format: N A x_1 x_2 ... x_N Output Print the number of ways to select cards such that the average of the written integers is exactly A . Sample Input 1 4 8 7 9 8 9 Sample Output 1 5 The following are the 5 ways to select cards such that the average is 8 : Select the 3 -rd card. Select the 1 -st and 2 -nd cards. Select the 1 -st and 4 -th cards. Select the 1 -st, 2 -nd and 3 -rd cards. Select the 1 -st, 3 -rd and 4 -th cards. Sample Input 2 3 8 6 6 9 Sample Output 2 0 Sample Input 3 8 5 3 6 2 8 7 6 5 9 Sample Output 3 19 Sample Input 4 33 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 Sample Output 4 8589934591 The answer may not fit into a 32 -bit integer.
35,784
りんごず桃 犏島県は果物の産地ずしおも有名で、その䞭でも特に桃ずりんごは党囜でも指折りの生産量を誇っおいたす。ずころで、ある販売甚の英文パンフレットの印刷原皿を䜜ったずころ、手違いでりんごに関する蚘述ず桃に関する蚘述を逆に曞いおしたいたした。 あなたは、apple ず peach を修正する仕事を任されたしたが、なにぶん面倒です。行の英文を入力しお、そのなかの apple ずいう文字列を党お peach に、peach ずいう文字列を党おapple に亀換した英文を出力するプログラムを䜜成しおください。 Input 英文半角英数字、空癜、蚘号を含むが行に䞎えられたす。入力される文字列の長さは 1000 以䞋です。 Output apple ずpeach ずいう文字列を亀換した英文を行に出力したす。 Sample Input the cost of one peach is higher than that of one apple. Output for the Sample Input the cost of one apple is higher than that of one peach.
35,785
Score : 400 points Problem Statement An altar enshrines N stones arranged in a row from left to right. The color of the i -th stone from the left (1 \leq i \leq N) is given to you as a character c_i ; R stands for red and W stands for white. You can do the following two kinds of operations any number of times in any order: Choose two stones (not necessarily adjacent) and swap them. Choose one stone and change its color (from red to white and vice versa). According to a fortune-teller, a white stone placed to the immediate left of a red stone will bring a disaster. At least how many operations are needed to reach a situation without such a white stone? Constraints 2 \leq N \leq 200000 c_i is R or W . Input Input is given from Standard Input in the following format: N c_{1}c_{2}...c_{N} Output Print an integer representing the minimum number of operations needed. Sample Input 1 4 WWRR Sample Output 1 2 For example, the two operations below will achieve the objective. Swap the 1 -st and 3 -rd stones from the left, resulting in RWWR . Change the color of the 4 -th stone from the left, resulting in RWWW . Sample Input 2 2 RR Sample Output 2 0 It can be the case that no operation is needed. Sample Input 3 8 WRWWRWRR Sample Output 3 3
35,786
アスキヌ文字 コンピュヌタの内郚ではすべおの情報が数倀ずしお扱われおいたす。たずえば、アスキヌコヌドでは、アルファベットの倧文字Aに数倀の65が割り圓おられおいたす。同様にアルファベットの倧文字Bに66、Cに67ずいうように、AからZには連続しおそれぞれ65から90が割り圓おられおいたす。たた、アスキヌコヌドでは、アルファベットの小文字aに97が割り圓おられおいたす。同様にアルファベットの小文字bに98、cに99ずいうように、aからzには連続しおそれぞれ97から122が割り圓おられおいたす。 䞎えられたアスキヌコヌドの数倀が、アルファベットの倧文字ず小文字のどちらを衚すか刀定するプログラムを䜜成せよ。 入力 入力は以䞋の圢匏で䞎えられる。 $N$ 行に数倀$N$ ($0 \leq N \leq 127$)が䞎えられる。 出力 倧文字なら「1」を、小文字なら「2」を、それ以倖なら「0」を行に出力する。 入出力䟋 入力䟋 67 出力䟋 1 入力䟋 110 出力䟋 2 入力䟋 32 出力䟋 0
35,787
Area of Intersection between Two Circles Write a program which prints the area of intersection between given circles $c1$ and $c2$. Input The input is given in the following format. $c1x\; c1y\; c1r$ $c2x\; c2y\; c2r$ $c1x$, $c1y$ and $c1r$ represent the coordinate and radius of the first circle. $c2x$, $c2y$ and $c2r$ represent the coordinate and radius of the second circle. All input values are given in integers. Output Output the area in a line. The output values should be in a decimal fraction with an error less than 0.000001. Constraints $-10,000 \leq c1x, c1y, c2x, c2y \leq 10,000$ $1 \leq c1r, c2r \leq 10,000$ Sample Input and Output Sample Input 1 0 0 1 2 0 2 Sample Output 1 1.40306643968573875104 Sample Input 2 1 0 1 0 0 3 Sample Output 2 3.14159265358979311600
35,788
無限急行 ムゲン鉄道のムゲン線には無限個の駅がありたす。駅には 
, -3, -2, -1, 0, 1, 2, 3, 
 ず番号が振られおいお、各駅は数盎線䞊の敎数ず同じ順番で隣り合っおいたす。あなたはいた、ある番号の駅から電車に乗り、それより倧きな番号の駅に向かおうずしおいたす。 ムゲン線には無限皮類の快速電車が走っおいたす。それらは 0 玚快速、1 玚快速、2 玚快速、3 玚快速、  のように番号で呌ばれおいたす。 n 玚快速の電車は、2 n の倍数の番号の駅に停車したす。たずえば、1玚快速は駅 
, -4, -2, 0, 2, 4, 
 に、3 玚快速は駅 
, -24, -16, -8, 0, 8, 16, 24, 
 に停車するずいった具合です。0 玚快速はすべおの駅に停車するので、本圓は各駅停車ですがムゲン鉄道は「快速」ず呌んでいたす。 どの玚の快速電車も、ある停車駅から次の停車駅たで移動するのに1単䜍時間かかりたす。たた、快速電車間の乗り換えにかかる時間は無芖できるものずしたす。乗車駅 s ず降車駅 d が䞎えられたずき、 s から d ぞ移動するのに必芁な最小の時間を求めるプログラムを䜜成しおください。ただし、 s から d ぞ移動する間に、倧きな番号から小さな番号の駅に向かっおの移動は認められないものずしたす。 入力 入力は1぀のデヌタセットからなる。入力デヌタは以䞋の圢匏で䞎えられる。 N s 1 d 1 s 2 d 2 : s N d N 行目に移動の回数を衚す N (1 ≀ N ≀ 100) が䞎えられる。続く N 行に、乗車駅の番号 s i ず降車駅の番号 d i (-1,000,000,000 ≀ s i < d i ≀ 1,000,000,000) が䞎えられる。 出力 䞎えられた乗車駅ず降車駅ごずに、移動に必芁な最小の時間を行に出力する。 入出力䟋 入力䟋 3 0 7 -1048576 0 -3 5 出力䟋 3 1 4
35,789
Score : 1400 points Problem Statement Joisino is about to compete in the final round of a certain programming competition. In this contest, there are N problems, numbered 1 through N . Joisino knows that it takes her T_i seconds to solve problem i(1≩i≩N) . In this contest, a contestant will first select some number of problems to solve. Then, the contestant will solve the selected problems. After that, the score of the contestant will be calculated as follows: (The score) = (The number of the pairs of integers L and R (1≩L≩R≩N) such that for every i satisfying L≩i≩R , problem i is solved) - (The total number of seconds it takes for the contestant to solve the selected problems) Note that a contestant is allowed to choose to solve zero problems, in which case the score will be 0 . Also, there are M kinds of drinks offered to the contestants, numbered 1 through M . If Joisino takes drink i(1≩i≩M) , her brain will be stimulated and the time it takes for her to solve problem P_i will become X_i seconds. Here, X_i may be greater than the length of time originally required to solve problem P_i . Taking drink i does not affect the time required to solve the other problems. A contestant is allowed to take exactly one of the drinks before the start of the contest. For each drink, Joisino wants to know the maximum score that can be obtained in the contest if she takes that drink. Your task is to write a program to calculate it instead of her. Constraints All input values are integers. 1≩N≩3*10^5 1≩T_i≩10^9 (The sum of T_i ) ≩10^{12} 1≩M≩3*10^5 1≩P_i≩N 1≩X_i≩10^9 Input The input is given from Standard Input in the following format: N T_1 T_2 ... T_N M P_1 X_1 P_2 X_2 : P_M X_M Output For each drink, print the maximum score that can be obtained if Joisino takes that drink, in order, one per line. Sample Input 1 5 1 1 4 1 1 2 3 2 3 10 Sample Output 1 9 2 If she takes drink 1 , the maximum score can be obtained by solving all the problems. If she takes drink 2 , the maximum score can be obtained by solving the problems 1,2,4 and 5 . Sample Input 2 12 1 2 1 3 4 1 2 1 12 3 12 12 10 9 3 11 1 5 35 6 15 12 1 1 9 4 3 10 2 5 1 7 6 Sample Output 2 34 35 5 11 35 17 25 26 28 21
35,790
Score : 300 points Problem Statement Does \sqrt{a} + \sqrt{b} < \sqrt{c} hold? Constraints 1 \leq a, b, c \leq 10^9 All values in input are integers. Input Input is given from Standard Input in the following format: a \ b \ c Output If \sqrt{a} + \sqrt{b} < \sqrt{c} , print Yes ; otherwise, print No . Sample Input 1 2 3 9 Sample Output 1 No \sqrt{2} + \sqrt{3} < \sqrt{9} does not hold. Sample Input 2 2 3 10 Sample Output 2 Yes \sqrt{2} + \sqrt{3} < \sqrt{10} holds.
35,791
Score : 200 points Problem Statement An integer X is called a Harshad number if X is divisible by f(X) , where f(X) is the sum of the digits in X when written in base 10 . Given an integer N , determine whether it is a Harshad number. Constraints 1?N?10^8 N is an integer. Input Input is given from Standard Input in the following format: N Output Print Yes if N is a Harshad number; print No otherwise. Sample Input 1 12 Sample Output 1 Yes f(12)=1+2=3 . Since 12 is divisible by 3 , 12 is a Harshad number. Sample Input 2 57 Sample Output 2 No f(57)=5+7=12 . Since 57 is not divisible by 12 , 12 is not a Harshad number. Sample Input 3 148 Sample Output 3 No
35,792
Problem J: SolveMe $N$ 個の郚屋があり, それぞれの郚屋の床にはうさぎの絵が描かれおいる. あなたが郚屋 $r$ のうさぎの絵の右耳の郚分に乗るず, あなたは郚屋 $A[r]$ に曞かれたうさぎのしっぜの䞊にテレポヌトする. 同様に, 郚屋 $r$ のうさぎの絵の巊耳の郚分に乗るず, 郚屋 $B[r]$ に曞かれたうさぎのしっぜの䞊にテレポヌトする. 敎数 $X$, $Y$, $Z$ が䞎えられる. ねこは, 以䞋の条件を満たすようにテレポヌトを蚭定しようずしおいる. テレポヌトの $N^{2N}$ 通りの蚭定方法のうち, 条件を満たすものは䜕通りか, mod 1,000,000,007で求めよ. 条件: 任意の郚屋 $r$ に察し, $r$ から右耳にちょうど $X$ 回乗り, 巊耳にちょうど 1 回乗り, 右耳にちょうど $Y$ 回乗り, 巊耳にちょうど 1 回乗り, 右耳にちょうど $Z$ 回乗るず, $r$ に戻る. Constraints $N$ will be between 1 and 1,000, inclusive. $X$, $Y$, $Z$ will be between 0 and 1,000,000,000,000,000,000, inclusive. Input 入力は以䞋の圢匏で䞎えられる: $N$ $X$ $Y$ $Z$ Output テレポヌトの蚭定方法を 1,000,000,007 で割ったあたりを衚す敎数を 1 行に出力せよ. Sample Input 1 3 1 0 1 Sample Output 1 18 Sample Input 2 5 8 5 8 Sample Output 2 120
35,793
Score : 500 points Problem Statement Consider writing each of the integers from 1 to N \times M in a grid with N rows and M columns, without duplicates. Takahashi thinks it is not fun enough, and he will write the numbers under the following conditions: The largest among the values in the i -th row (1 \leq i \leq N) is A_i . The largest among the values in the j -th column (1 \leq j \leq M) is B_j . For him, find the number of ways to write the numbers under these conditions, modulo 10^9 + 7 . Constraints 1 \leq N \leq 1000 1 \leq M \leq 1000 1 \leq A_i \leq N \times M 1 \leq B_j \leq N \times M A_i and B_j are integers. Input Input is given from Standard Input in the following format: N M A_1 A_2 ... A_{N} B_1 B_2 ... B_{M} Output Print the number of ways to write the numbers under the conditions, modulo 10^9 + 7 . Sample Input 1 2 2 4 3 3 4 Sample Output 1 2 (A_1, A_2) = (4, 3) and (B_1, B_2) = (3, 4) . In this case, there are two ways to write the numbers, as follows: 1 in (1, 1) , 4 in (1, 2) , 3 in (2, 1) and 2 in (2, 2) . 2 in (1, 1) , 4 in (1, 2) , 3 in (2, 1) and 1 in (2, 2) . Here, (i, j) denotes the square at the i -th row and the j -th column. Sample Input 2 3 3 5 9 7 3 6 9 Sample Output 2 0 Since there is no way to write the numbers under the condition, 0 should be printed. Sample Input 3 2 2 4 4 4 4 Sample Output 3 0 Sample Input 4 14 13 158 167 181 147 178 151 179 182 176 169 180 129 175 168 181 150 178 179 167 180 176 169 182 177 175 159 173 Sample Output 4 343772227
35,794
Score : 100 points Problem Statement You went shopping to buy cakes and donuts with X yen (the currency of Japan). First, you bought one cake for A yen at a cake shop. Then, you bought as many donuts as possible for B yen each, at a donut shop. How much do you have left after shopping? Constraints 1 \leq A, B \leq 1 000 A + B \leq X \leq 10 000 X , A and B are integers. Input Input is given from Standard Input in the following format: X A B Output Print the amount you have left after shopping. Sample Input 1 1234 150 100 Sample Output 1 84 You have 1234 - 150 = 1084 yen left after buying a cake. With this amount, you can buy 10 donuts, after which you have 84 yen left. Sample Input 2 1000 108 108 Sample Output 2 28 Sample Input 3 579 123 456 Sample Output 3 0 Sample Input 4 7477 549 593 Sample Output 4 405
35,795
The Closest Circle You are given N non-overlapping circles in xy-plane. The radius of each circle varies, but the radius of the largest circle is not double longer than that of the smallest. Figure 1: The Sample Input The distance between two circles C 1 and C 2 is given by the usual formula where ( x i , y i ) is the coordinates of the center of the circle C i , and r i is the radius of C i , for i = 1, 2. Your task is to write a program that finds the closest pair of circles and print their distance. Input The input consists of a series of test cases, followed by a single line only containing a single zero, which indicates the end of input. Each test case begins with a line containing an integer N (2 ≀ N ≀ 100000), which indicates the number of circles in the test case. N lines describing the circles follow. Each of the N lines has three decimal numbers R , X , and Y . R represents the radius of the circle. X and Y represent the x - and y -coordinates of the center of the circle, respectively. Output For each test case, print the distance between the closest circles. You may print any number of digits after the decimal point, but the error must not exceed 0.00001. Sample Input 4 1.0 0.0 0.0 1.5 0.0 3.0 2.0 4.0 0.0 1.0 3.0 4.0 0 Output for the Sample Input 0.5
35,796
Score : 400 points Problem Statement There are N squares arranged in a row, numbered 1, 2, ..., N from left to right. You are given a string S of length N consisting of . and # . If the i -th character of S is # , Square i contains a rock; if the i -th character of S is . , Square i is empty. In the beginning, Snuke stands on Square A , and Fnuke stands on Square B . You can repeat the following operation any number of times: Choose Snuke or Fnuke, and make him jump one or two squares to the right. The destination must be one of the squares, and it must not contain a rock or the other person. You want to repeat this operation so that Snuke will stand on Square C and Fnuke will stand on Square D . Determine whether this is possible. Constraints 4 \leq N \leq 200\ 000 S is a string of length N consisting of . and # . 1 \leq A, B, C, D \leq N Square A , B , C and D do not contain a rock. A , B , C and D are all different. A < B A < C B < D Input Input is given from Standard Input in the following format: N A B C D S Output Print Yes if the objective is achievable, and No if it is not. Sample Input 1 7 1 3 6 7 .#..#.. Sample Output 1 Yes The objective is achievable by, for example, moving the two persons as follows. ( A and B represent Snuke and Fnuke, respectively.) A#B.#.. A#.B#.. .#AB#.. .#A.#B. .#.A#B. .#.A#.B .#..#AB Sample Input 2 7 1 3 7 6 .#..#.. Sample Output 2 No Sample Input 3 15 1 3 15 13 ...#.#...#.#... Sample Output 3 Yes
35,797
Greatest Common Divisor Write a program which finds the greatest common divisor of two natural numbers a and b Input a and b are given in a line sparated by a single space. Output Output the greatest common divisor of a and b . Constrants 1 ≀ a , b ≀ 10 9 Hint You can use the following observation: For integers x and y , if x ≥ y , then gcd( x , y ) = gcd( y , x % y ) Sample Input 1 54 20 Sample Output 1 2 Sample Input 2 147 105 Sample Output 2 21
35,798
Parentheses Dave loves strings consisting only of '(' and ')'. Especially, he is interested in balanced strings. Any balanced strings can be constructed using the following rules: A string "()" is balanced. Concatenation of two balanced strings are balanced. If $T$ is a balanced string, concatenation of '(', $T$, and ')' in this order is balanced. For example, "()()" and "(()())" are balanced strings. ")(" and ")()(()" are not balanced strings. Dave has a string consisting only of '(' and ')'. It satis es the followings: You can make it balanced by swapping adjacent characters exactly $A$ times. For any non-negative integer $B$ ($B < A$), you cannot make it balanced by $B$ swaps of adjacent characters. It is the shortest of all strings satisfying the above conditions. Your task is to compute Dave's string. If there are multiple candidates, output the minimum in lexicographic order. As is the case with ASCII, '(' is less than ')'. Input The input consists of a single test case, which contains an integer $A$ ($1 \leq A \leq 10^9$). Output Output Dave's string in one line. If there are multiple candidates, output the minimum in lexicographic order. Sample Input 1 1 Output for the Sample Input 1 )( There are infinitely many strings which can be balanced by only one swap. Dave's string is the shortest of them. Sample Input 2 4 Output for the Sample Input 2 )())(( String "))(()(" can be balanced by 4 swaps, but the output should be ")())((" because it is the minimum in lexicographic order.
35,799