task
stringlengths
0
154k
__index_level_0__
int64
0
39.2k
Binary Search Tree I Search trees are data structures that support dynamic set operations including insert, search, delete and so on. Thus a search tree can be used both as a dictionary and as a priority queue. Binary search tree is one of fundamental search trees. The keys in a binary search tree are always stored in such a way as to satisfy the following binary search tree property: Let $x$ be a node in a binary search tree. If $y$ is a node in the left subtree of $x$, then $y.key \leq x.key$. If $y$ is a node in the right subtree of $x$, then $x.key \leq y.key$. The following figure shows an example of the binary search tree. For example, keys of nodes which belong to the left sub-tree of the node containing 80 are less than or equal to 80, and keys of nodes which belong to the right sub-tree are more than or equal to 80. The binary search tree property allows us to print out all the keys in the tree in sorted order by an inorder tree walk. A binary search tree should be implemented in such a way that the binary search tree property continues to hold after modifications by insertions and deletions. A binary search tree can be represented by a linked data structure in which each node is an object. In addition to a key field and satellite data, each node contains fields left , right , and p that point to the nodes corresponding to its left child, its right child, and its parent, respectively. To insert a new value $v$ into a binary search tree $T$, we can use the procedure insert as shown in the following pseudo code. The insert procedure is passed a node $z$ for which $z.key = v$, $z.left = NIL$, and $z.right = NIL$. The procedure modifies $T$ and some of the fields of $z$ in such a way that $z$ is inserted into an appropriate position in the tree. 1 insert(T, z) 2 y = NIL // parent of x 3 x = 'the root of T' 4 while x ≠ NIL 5 y = x // set the parent 6 if z.key < x.key 7 x = x.left // move to the left child 8 else 9 x = x.right // move to the right child 10 z.p = y 11 12 if y == NIL // T is empty 13 'the root of T' = z 14 else if z.key < y.key 15 y.left = z // z is the left child of y 16 else 17 y.right = z // z is the right child of y Write a program which performs the following operations to a binary search tree $T$. insert $k$: Insert a node containing $k$ as key into $T$. print : Print the keys of the binary search tree by inorder tree walk and preorder tree walk respectively. You should use the above pseudo code to implement the insert operation. $T$ is empty at the initial state. Input In the first line, the number of operations $m$ is given. In the following $m$ lines, operations represented by insert $k$ or print are given. Output For each print operation, print a list of keys obtained by inorder tree walk and preorder tree walk in a line respectively. Put a space character before each key . Constraints The number of operations $\leq 500,000$ The number of print operations $\leq 10$. $-2,000,000,000 \leq key \leq 2,000,000,000$ The height of the binary tree does not exceed 100 if you employ the above pseudo code. The keys in the binary search tree are all different. Sample Input 1 8 insert 30 insert 88 insert 12 insert 1 insert 20 insert 17 insert 25 print Sample Output 1 1 12 17 20 25 30 88 30 12 1 20 17 25 88 Reference Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.
35,901
Score : 300 points Problem Statement Given is a sequence of integers A_1, A_2, ..., A_N . If its elements are pairwise distinct, print YES ; otherwise, print NO . Constraints 2 ≀ N ≀ 200000 1 ≀ A_i ≀ 10^9 All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 ... A_N Output If the elements of the sequence are pairwise distinct, print YES ; otherwise, print NO . Sample Input 1 5 2 6 1 4 5 Sample Output 1 YES The elements are pairwise distinct. Sample Input 2 6 4 1 3 1 6 2 Sample Output 2 NO The second and fourth elements are identical. Sample Input 3 2 10000000 10000000 Sample Output 3 NO
35,902
Problem E: Cards There are many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards. A blue card and a red card can be paired when both of the numbers printed on them have a common divisor greater than 1. There may be more than one red card that can be paired with one blue card. Also, there may be more than one blue card that can be paired with one red card. When a blue card and a red card are chosen and paired, these two cards are removed from the whole cards on the table. Figure E-1: Four blue cards and three red cards For example, in Figure E-1, there are four blue cards and three red cards. Numbers 2, 6, 6 and 15 are printed on the faces of the four blue cards, and 2, 3 and 35 are printed on those of the three red cards. Here, you can make pairs of blue cards and red cards as follows. First, the blue card with number 2 on it and the red card with 2 are paired and removed. Second, one of the two blue cards with 6 and the red card with 3 are paired and removed. Finally, the blue card with 15 and the red card with 35 are paired and removed. Thus the number of removed pairs is three. Note that the total number of the pairs depends on the way of choosing cards to be paired. The blue card with 15 and the red card with 3 might be paired and removed at the beginning. In this case, there are only one more pair that can be removed and the total number of the removed pairs is two. Your job is to find the largest number of pairs that can be removed from the given set of cards on the table. Input The input is a sequence of datasets. The number of the datasets is less than or equal to 100. Each dataset is formatted as follows. m n b 1 ... b k ... b m r 1 ... r k ... r n The integers m and n are the number of blue cards and that of red cards, respectively. You may assume 1 ≀ m ≀ 500 and 1≀ n ≀ 500. b k (1 ≀ k ≀ m ) and r k (1 ≀ k ≀ n ) are numbers printed on the blue cards and the red cards respectively, that are integers greater than or equal to 2 and less than 10000000 (=10 7 ). The input integers are separated by a space or a newline. Each of b m and r n is followed by a newline. There are no other characters in the dataset. The end of the input is indicated by a line containing two zeros separated by a space. Output For each dataset, output a line containing an integer that indicates the maximum of the number of the pairs. Sample Input 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 Output for the Sample Input 3 1 0 4 9 18 85
35,903
E : 台颚 / Typhoon 問題文 南北方向に H 東西方向に W の倧きさの町がある 町には䞀蟺の長さが 1 の正方圢の区画に隙間なく敎備されおおり党おの区画に 1 軒ず぀家が建っおいる この町のある区画の䞊空で台颚が発生し被害を䞎えた埌ある区画の䞊空で枩垯䜎気圧に倉化した 倉化した埌は被害を䞎えない 䞋図のように台颚は高さ 3 , 幅 3 の正方圢であり ★の぀いたマスを䞭心ず呌ぶこずにする台颚は 8 近傍に区画単䜍で移動する ぀たり台颚の䞭心は蟺たたは頂点を共有する区画(珟圚の区画を含む)に移るように党䜓を䌎っお移動する ただし町の倖に台颚がはみ出るこずはなく 台颚の䞭心は䞋の図の網掛けのように北から 0 番目ず H − 1 番目西から 0 番目ず W − 1 番目の区間は通らないように移動する 家は台颚が䞀床䞊空に来るず以䞋のように被害の皋床が倉化する 損壊ナシ → 䞀郚損壊 → 半壊 → 党壊 → 跡圢ナシ だが幞い跡圢ナシずなった家は無かったようだ 各家の被害の状況が䞎えられるので台颚が発生した地点ず枩垯䜎気圧に倉化した地点を求めよ ただし発生した区画を北から s_i 番目西から s_j 番目 枩垯䜎気圧に倉化した区画を北から t_i 番目西から t_j 番目ずするず 2 ぀の地点は 10000 t_i + t_j ≀ 10000 s_i + s_j を満たすように定たる 入力 H \ W D_{11} \ 
 \ D_{1W} D_{21} \ 
 \ D_{2W} 
 D_{H1} \ 
 \ D_{HW} D_{ij} は北から i 番目 西から j 番目の家の被害の皋床を以䞋のように衚す敎数である 0 : 損壊ナシ 1 : 䞀郚損壊 2 : 半壊 3 : 党壊 制玄 敎数である 入力は答えが䞀意に定たるようなもののみ䞎えられる 3 ≀ H,W ≀ 500 0 ≀ D_{ij} ≀ 3 出力 答えを以䞋のように 1 行で出力せよ s_i \ s_j \ t_i \ t_j サンプル サンプル入力1 7 5 0 0 0 0 0 0 1 1 1 0 0 2 2 2 0 0 3 3 3 0 0 2 2 2 0 0 1 1 1 0 0 0 0 0 0 サンプル出力1 4 2 2 2 サンプル入力2 6 6 0 0 0 1 1 1 0 0 0 2 2 2 0 0 1 3 3 2 1 2 3 3 2 1 1 2 3 2 1 0 1 2 2 1 0 0 サンプル出力2 4 1 1 4 サンプル入力3 4 4 2 2 2 0 2 2 2 0 2 2 2 0 0 0 0 0 サンプル出力3 1 1 1 1
35,904
Billiards Sorting Rotation is one of several popular pocket billiards games. It uses 15 balls numbered from 1 to 15, and set them up as illustrated in the following figure at the beginning of a game. (Note: the ball order is modified from real-world Rotation rules for simplicity of the problem.) [ 1] [ 2][ 3] [ 4][ 5][ 6] [ 7][ 8][ 9][10] [11][12][13][14][15] You are an engineer developing an automatic billiards machine. For the first step you had to build a machine that sets up the initial condition. This project went well, and finally made up a machine that could arrange the balls in the triangular shape. However, unfortunately, it could not place the balls in the correct order. So now you are trying to build another machine that fixes the order of balls by swapping them. To cut off the cost, it is only allowed to swap the ball #1 with its neighboring balls that are not in the same row. For example, in the case below, only the following pairs can be swapped: (1,2), (1,3), (1,8), and (1,9). [ 5] [ 2][ 3] [ 4][ 1][ 6] [ 7][ 8][ 9][10] [11][12][13][14][15] Write a program that calculates the minimum number of swaps required. Input The first line of each test case has an integer N ( 1 \leq N \leq 5 ), which is the number of the rows. The following N lines describe how the balls are arranged by the first machine; the i -th of them consists of exactly i integers, which are the ball numbers. The input terminates when N = 0. Your program must not output for this case. Output For each test case, print its case number and the minimum number of swaps. You can assume that any arrangement can be fixed by not more than 45 swaps. Sample Input 2 3 2 1 4 9 2 4 8 5 3 7 1 6 10 0 Output for the Sample Input Case 1: 1 Case 2: 13
35,905
Score : 1000 points Problem Statement Select any integer N between 1000 and 2000 (inclusive), and any integer K not less than 1 , then solve the problem below. Problem We have N sheets of paper. Write K integers on each of them to satisfy the following conditions: Each integer written must be between 1 and N (inclusive). The K integers written on the same sheet must be all different. Each of the integers between 1 and N must be written on exactly K sheets. For any two sheet, there is exactly one integer that appears on both. Input There is no input in this problem. Output In the first line, print N and K separated by a space. In the subsequent N lines, print your solution. The i -th of these lines must contain the K integers written on the i -th sheet, with spaces in between. Sample Output 3 2 1 2 2 3 3 1 This is an example of a solution for N = 3 and K = 2 . Note that this output will be judged as incorrect, since the constraint on N is not satisfied.
35,906
Score : 800 points Problem Statement There is a tree with N vertices. The vertices are numbered 1 through N . The i -th edge ( 1 \leq i \leq N - 1 ) connects Vertex x_i and y_i . For vertices v and w ( 1 \leq v, w \leq N ), we will define the distance between v and w d(v, w) as "the number of edges contained in the path v - w ". A squirrel lives in each vertex of the tree. They are planning to move, as follows. First, they will freely choose a permutation of (1, 2, ..., N) , p = (p_1, p_2, ..., p_N) . Then, for each 1 \leq i \leq N , the squirrel that lived in Vertex i will move to Vertex p_i . Since they like long travels, they have decided to maximize the total distance they traveled during the process. That is, they will choose p so that d(1, p_1) + d(2, p_2) + ... + d(N, p_N) will be maximized. How many such ways are there to choose p , modulo 10^9 + 7 ? Constraints 2 \leq N \leq 5,000 1 \leq x_i, y_i \leq N The input graph is a tree. Input Input is given from Standard Input in the following format: N x_1 y_1 x_2 y_2 : x_{N - 1} y_{N - 1} Output Print the number of the ways to choose p so that the condition is satisfied, modulo 10^9 + 7 . Sample Input 1 3 1 2 2 3 Sample Output 1 3 The maximum possible distance traveled by squirrels is 4 . There are three choices of p that achieve it, as follows: (2, 3, 1) (3, 1, 2) (3, 2, 1) Sample Input 2 4 1 2 1 3 1 4 Sample Output 2 11 The maximum possible distance traveled by squirrels is 6 . For example, p = (2, 1, 4, 3) achieves it. Sample Input 3 6 1 2 1 3 1 4 2 5 2 6 Sample Output 3 36 Sample Input 4 7 1 2 6 3 4 5 1 7 1 5 2 3 Sample Output 4 396
35,907
Problem A: Ruins In 1936, a dictator Hiedler who aimed at world domination had a deep obsession with the Lost Ark . A person with this ark would gain mystic power according to legend. To break the ambition of the dictator, ACM (the Alliance of Crusaders against Mazis) entrusted a secret task to an archeologist Indiana Johns. Indiana stepped into a vast and harsh desert to find the ark. Indiana finally found an underground treasure house at a ruin in the desert. The treasure house seems storing the ark. However, the door to the treasure house has a special lock, and it is not easy to open the door. To open the door, he should solve a problem raised by two positive integers a and b inscribed on the door. The problem requires him to find the minimum sum of squares of differences for all pairs of two integers adjacent in a sorted sequence that contains four positive integers a 1 , a 2 , b 1 and b 2 such that a = a 1 a 2 and b = b 1 b 2 . Note that these four integers does not need to be different. For instance, suppose 33 and 40 are inscribed on the door, he would have a sequence 3, 5, 8, 11 as 33 and 40 can be decomposed into 3 × 11 and 5 × 8 respectively. This sequence gives the sum (5 - 3) 2 + (8 - 5) 2 + (11 - 8) 2 = 22, which is the smallest sum among all possible sorted sequences. This example is included as the first data set in the sample input. Once Indiana fails to give the correct solution, he will suffer a calamity by a curse. On the other hand, he needs to solve the problem as soon as possible, since many pawns under Hiedler are also searching for the ark, and they might find the same treasure house. He will be immediately killed if this situation happens. So he decided to solve the problem by your computer program. Your task is to write a program to solve the problem presented above. Input The input consists of a series of data sets. Each data set is given by a line that contains two positive integers not greater than 10,000. The end of the input is represented by a pair of zeros, which should not be processed. Output For each data set, print a single integer that represents the minimum square sum in a line. No extra space or text should appear. Sample Input 33 40 57 144 0 0 Output for the Sample Input 22 94
35,908
Score : 100 points Problem Statement Let N be a positive odd number. There are N coins, numbered 1, 2, \ldots, N . For each i ( 1 \leq i \leq N ), when Coin i is tossed, it comes up heads with probability p_i and tails with probability 1 - p_i . Taro has tossed all the N coins. Find the probability of having more heads than tails. Constraints N is an odd number. 1 \leq N \leq 2999 p_i is a real number and has two decimal places. 0 < p_i < 1 Input Input is given from Standard Input in the following format: N p_1 p_2 \ldots p_N Output Print the probability of having more heads than tails. The output is considered correct when the absolute error is not greater than 10^{-9} . Sample Input 1 3 0.30 0.60 0.80 Sample Output 1 0.612 The probability of each case where we have more heads than tails is as follows: The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Head, Head) is 0.3 × 0.6 × 0.8 = 0.144 ; The probability of having (Coin 1, Coin 2, Coin 3) = (Tail, Head, Head) is 0.7 × 0.6 × 0.8 = 0.336 ; The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Tail, Head) is 0.3 × 0.4 × 0.8 = 0.096 ; The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Head, Tail) is 0.3 × 0.6 × 0.2 = 0.036 . Thus, the probability of having more heads than tails is 0.144 + 0.336 + 0.096 + 0.036 = 0.612 . Sample Input 2 1 0.50 Sample Output 2 0.5 Outputs such as 0.500 , 0.500000001 and 0.499999999 are also considered correct. Sample Input 3 5 0.42 0.01 0.42 0.99 0.42 Sample Output 3 0.3821815872
35,909
最倧公玄数-ナヌクリッドの互陀法 最倧公玄数は、コンピュヌタ䞊で扱う数孊には欠かせない芁玠です。最倧公玄 数を䜿うこずで、蚈算の効率が倧きく倉動するこずもありたす。最倧公玄数を求めるアルゎリズムのひず぀が「ナヌクリッドの互陀法」です。その凊理 の流れを以䞋に瀺したす。 䟋えば 1071 ず 1029 の堎合、1071 を X 、1029 を Y に代入しお、 1071 ÷ 1029 の䜙りは 42 、 X に 42 を代入しお X ず Y を入れ替える。 (1 ステップ) 1029 ÷ 42 の䜙りは 21 、 X に 21 を代入しお X ず Y を入れ替える。 (2 ステップ) 42 ÷ 21 の䜙りは 0 、 X に 0 を代入しお X ず Y を入れ替える。 (3 ステップ) Y が 0 になったので、この時の X が最倧公玄数ずなる。よっお最倧公玄数は 21 。 このように、たったの 3 ステップで 1071 ず 1029 の最倧公玄数を求めるこずが出来たした。ナヌクリッドの互陀法は玄数を出しお比范しおいく方法に比べ、圧倒的に早く結果を出しおくれたす。 ぀の敎数を入力ずし、ナヌクリッドの互陀法を甚いお最倧公玄数を求め、その最倧公玄数ず蚈算にかかったステップ数を出力するプログラムを䜜成しおください。 Input 耇数のデヌタセットの䞊びが入力ずしお䞎えられたす。入力の終わりはれロふた぀の行で瀺されたす。 各デヌタセットずしお぀の敎数 a, b (2 ≀ a, b ≀ 2 31 -1) が行に䞎えられたす。 デヌタセットの数は 1000 を超えたせん。 Output デヌタセットごずに、入力された぀の敎数の最倧公玄数ず、蚈算にかかったナヌクリッドの互陀法のステップ数を空癜区切りで行に出力したす。 Sample Input 1071 1029 5 5 0 0 Output for the Sample Input 21 3 5 1
35,910
Permutation Enumeration For given an integer $n$, print all permutations of $\{1, 2, ..., n\}$ in lexicographic order. Input An integer $n$ is given in a line. Output Print each permutation in a line in order. Separate adjacency elements by a space character. Constraints $1 \leq n \leq 9$ Sample Input 1 2 Sample Output 1 1 2 2 1 Sample Input 2 3 Sample Output 2 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1
35,911
E: 凞凹数列 問題 長さ $N$ の数列 $A$ が䞎えられる。$A$ の $i$ 項目 は $A_i$ である。 あなたは、この数列に察しお以䞋の操䜜を行うこずができる。 $1 \leq i \leq N - 1$ なる敎数 i を遞ぶ。 $A_i$ の倀ず $A_{i + 1}$ の倀を入れ替える。 $A$ を凞凹数列にするために必芁な操䜜の最小回数を求めよ。 以䞋の条件を満たす長さ $N$ の数列を凞凹数列ず定矩する。 $1 < i < N$ を満たす任意の $i$ に぀いお、 $A_{i + 1}, A_{i - 1} > A_i$ たたは $A_{i + 1}, A_{i - 1} < A_i$ を満たす。 盎感的に蚀えば、 $1,\ 10,\ 2,\ 30,\ \dots (10,\ 1,\ 30,\ 2,\ \dots )$ のような、増加、枛少、増加 枛少、増加、枛少 を繰り返す数列である。 制玄 入力倀は党お敎数である。 $3 \leq N \leq 10^5$ $i \neq j$ ならば $A_i \neq A_j$ $-10^9 \leq A_i \leq 10^9$ 入力圢匏 入力は以䞋の圢匏で䞎えられる。 $N$ $A_1 \dots A_N$ 出力 凞凹数列にするために必芁な操䜜の最小回数を出力せよ。たた、末尟に改行も出力せよ。 サンプル サンプル入力 1 5 1 2 3 4 5 サンプル出力 1 2 $2$ ず $3$ 、 $4$ ず $5$ に操䜜を行えば、 $1\ 3\ 2\ 5\ 4$ ずいう凞凹数列になる。 サンプル入力 2 3 1 2 3 サンプル出力 2 1 $1$ ず $2$ に操䜜を行う、あるいは $2$ ず $3$ に操䜜を行うこずで、凞凹数列になる。 サンプル入力 3 12 5 9 1 38 100 -23 4 16 -2 -10 -17 8 サンプル出力 3 2
35,912
Problem A: Gift from the Goddess of Programming The goddess of programming is reviewing a thick logbook, which is a yearly record of visitors to her holy altar of programming. The logbook also records her visits at the altar. The altar attracts programmers from all over the world because one visitor is chosen every year and endowed with a gift of miracle programming power by the goddess. The endowed programmer is chosen from those programmers who spent the longest time at the altar during the goddess's presence. There have been enthusiastic visitors who spent very long time at the altar but failed to receive the gift because the goddess was absent during their visits. Now, your mission is to write a program that finds how long the programmer to be endowed stayed at the altar during the goddess's presence. Input The input is a sequence of datasets. The number of datasets is less than 100. Each dataset is formatted as follows. n M 1 /D 1 h 1 : m 1 e 1 p 1 M 2 /D 2 h 2 : m 2 e 2 p 2 . . . M n /D n h n : m n e n p n The first line of a dataset contains a positive even integer, n ≀ 1000, which denotes the number of lines of the logbook. This line is followed by n lines of space-separated data, where M i /D i identifies the month and the day of the visit, h i : m i represents the time of either the entrance to or exit from the altar, e i is either I for entrance, or O for exit, and p i identifies the visitor. All the lines in the logbook are formatted in a fixed-column format. Both the month and the day in the month are represented by two digits. Therefore April 1 is represented by 04/01 and not by 4/1. The time is described in the 24-hour system, taking two digits for the hour, followed by a colon and two digits for minutes, 09:13 for instance and not like 9:13. A programmer is identified by an ID, a unique number using three digits. The same format is used to indicate entrance and exit of the goddess, whose ID is 000. All the lines in the logbook are sorted in ascending order with respect to date and time. Because the altar is closed at midnight, the altar is emptied at 00:00. You may assume that each time in the input is between 00:01 and 23:59, inclusive. A programmer may leave the altar just after entering it. In this case, the entrance and exit time are the same and the length of such a visit is considered 0 minute. You may assume for such entrance and exit records, the line that corresponds to the entrance appears earlier in the input than the line that corresponds to the exit. You may assume that at least one programmer appears in the logbook. The end of the input is indicated by a line containing a single zero. Output For each dataset, output the total sum of the blessed time of the endowed programmer. The blessed time of a programmer is the length of his/her stay at the altar during the presence of the goddess. The endowed programmer is the one whose total blessed time is the longest among all the programmers. The output should be represented in minutes. Note that the goddess of programming is not a programmer. Sample Input 14 04/21 09:00 I 000 04/21 09:00 I 001 04/21 09:15 I 002 04/21 09:30 O 001 04/21 09:45 O 000 04/21 10:00 O 002 04/28 09:00 I 003 04/28 09:15 I 000 04/28 09:30 I 004 04/28 09:45 O 004 04/28 10:00 O 000 04/28 10:15 O 003 04/29 20:00 I 002 04/29 21:30 O 002 20 06/01 09:00 I 001 06/01 09:15 I 002 06/01 09:15 I 003 06/01 09:30 O 002 06/01 10:00 I 000 06/01 10:15 O 001 06/01 10:30 I 002 06/01 10:45 O 002 06/01 11:00 I 001 06/01 11:15 O 000 06/01 11:30 I 002 06/01 11:45 O 001 06/01 12:00 O 002 06/01 12:15 I 000 06/01 12:30 I 002 06/01 12:45 O 000 06/01 13:00 I 000 06/01 13:15 O 000 06/01 13:30 O 002 06/01 13:45 O 003 0 Output for the Sample Input 45 120
35,913
G - Proportional Representation Problem Statement An election selecting the members of the parliament in JAG Kingdom was held. The only system adopted in this country is the party-list proportional representation. In this system, each citizen votes for a political party, and the number of seats a party wins will be proportional to the number of votes it receives. Since the total number of seats in the parliament is an integer, of course, it is often impossible to allocate seats exactly proportionaly. In JAG Kingdom, the following method, known as the D'Hondt method, is used to determine the number of seats for each party. Assume that every party has an unlimited supply of candidates and the candidates of each party are ordered in some way. To the $y$-th candidate of a party which received $x$ votes, assign the value $\dfrac{x}{y}$. Then all the candidates are sorted in the decreasing order of their assigned values. The first $T$ candidates are considered to win, where $T$ is the total number of seats, and the number of seats a party win is the number of its winning candidates. The table below shows an example with three parties. The first party received $40$ votes, the second $60$ votes, and the third $30$ votes. If the total number of seats is $T = 9$, the first party will win $3$ seats, the second $4$ seats, and the third $2$ seats. When selecting winning candidates, ties are broken by lottery; any tied candidates will have a chance to win. For instance, in the example above, if $T = 5$ then two candidates tie for the value $20$ and there are two possible outcomes: The first party wins $2$ seats, the second $2$ seats, and the third $1$ seat. The first party wins $1$ seat, the second $3$ seats, and the third $1$ seat. You have just heard the results of the election on TV. Knowing the total number of valid votes and the number of seats each party won, you wonder how many votes each party received. Given $N$, $M$, and $S_i$ ($1 \le i \le M$), denoting the total number of valid votes, the number of parties, and the number of seats the $i$-th party won, respectively, your task is to determine for each party the minimum and the maximum possible number of votes it received. Note that for some cases there might be no such situation with the given $N$, $M$, and $S_i$. Input The first line of the input contains two integers $N$ ($1 \le N \le 10^9$) and $M$ ($1 \le M \le 30{,}000$), where $N$ is the total number of valid votes and $M$ is the number of parties. $M$ lines follow, the $i$-th of which contains a single integer $S_i$ ($0 \le S_i \le 30{,}000$), representing the number of seats the $i$-th party won. You can assume that there exists $i$ with $S_i \ne 0$. Output If there is no situation with the given $N$, $M$, and $S_i$, display the word "impossible". Otherwise, output $M$ lines, each containing two integers. The first integer and the second integer in the $i$-th line should be the minimum and the maximum possible number of votes the $i$-th party received, respectively. Sample Input 1 10 2 2 1 Output for the Sample Input 1 5 7 3 5 Sample Input 2 5 6 0 2 0 2 6 0 Output for the Sample Input 2 0 0 1 1 0 0 1 1 3 3 0 0 Sample Input 3 2000 5 200 201 202 203 204 Output for the Sample Input 3 396 397 397 399 399 401 401 403 403 404 Sample Input 4 15 10 1 1 1 1 1 1 1 1 1 13 Output for the Sample Input 4 impossible Sample Input 5 1000000000 9 12507 16653 26746 21516 29090 10215 28375 21379 18494 Output for the Sample Input 5 67611619 67619582 90024490 90033301 144586260 144597136 116313392 116323198 157257695 157269050 55221291 55228786 153392475 153403684 115572783 115582561 99976756 99985943
35,914
Score : 400 points Problem Statement You are given integers N and M . Consider a sequence a of length N consisting of positive integers such that a_1 + a_2 + ... + a_N = M . Find the maximum possible value of the greatest common divisor of a_1, a_2, ..., a_N . Constraints All values in input are integers. 1 \leq N \leq 10^5 N \leq M \leq 10^9 Input Input is given from Standard Input in the following format: N M Output Print the maximum possible value of the greatest common divisor of a sequence a_1, a_2, ..., a_N that satisfies the condition. Sample Input 1 3 14 Sample Output 1 2 Consider the sequence (a_1, a_2, a_3) = (2, 4, 8) . Their greatest common divisor is 2 , and this is the maximum value. Sample Input 2 10 123 Sample Output 2 3 Sample Input 3 100000 1000000000 Sample Output 3 10000
35,915
Score : 300 points Problem Statement We have a square grid with H rows and W columns. Snuke wants to write 0 or 1 in each of the squares. Here, all of the following conditions have to be satisfied: For every row, the smaller of the following is A : the number of 0 s contained in the row, and the number of 1 s contained in the row. (If these two numbers are equal, “the smaller” should be read as “either”.) For every column, the smaller of the following is B : the number of 0 s contained in the column, and the number of 1 s contained in the column. Determine if these conditions can be satisfied by writing 0 or 1 in each of the squares. If the answer is yes, show one way to fill the squares so that the conditions are satisfied. Constraints 1 \leq H,W \leq 1000 0 \leq A 2 \times A \leq W 0 \leq B 2 \times B \leq H All values in input are integers. Input Input is given from Standard Input in the following format: H W A B Output If the conditions cannot be satisfied by writing 0 or 1 in each of the squares, print -1 . If the conditions can be satisfied, print one way to fill the squares so that the conditions are satisfied, in the following format: s_{11}s_{12}\cdots s_{1W} s_{21}s_{22}\cdots s_{2W} \vdots s_{H1}s_{H2}\cdots s_{HW} Here s_{ij} is the digit written in the square at the i -th row from the top and the j -th column from the left in the grid. If multiple solutions exist, printing any of them will be accepted. Sample Input 1 3 3 1 1 Sample Output 1 100 010 001 Every row contains two 0 s and one 1 , so the first condition is satisfied. Also, every column contains two 0 s and one 1 , so the second condition is satisfied. Sample Input 2 1 5 2 0 Sample Output 2 01010
35,916
Score : 300 points Problem Statement You are given an integer sequence of length N , a_1,a_2,...,a_N . For each 1≀i≀N , you have three choices: add 1 to a_i , subtract 1 from a_i or do nothing. After these operations, you select an integer X and count the number of i such that a_i=X . Maximize this count by making optimal choices. Constraints 1≀N≀10^5 0≀a_i<10^5 (1≀i≀N) 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 Print the maximum possible number of i such that a_i=X . Sample Input 1 7 3 1 4 1 5 9 2 Sample Output 1 4 For example, turn the sequence into 2,2,3,2,6,9,2 and select X=2 to obtain 4 , the maximum possible count. Sample Input 2 10 0 1 2 3 4 5 6 7 8 9 Sample Output 2 3 Sample Input 3 1 99999 Sample Output 3 1
35,917
Score : 300 points Problem Statement Let us define the beauty of a sequence (a_1,... ,a_n) as the number of pairs of two adjacent elements in it whose absolute differences are d . For example, when d=1 , the beauty of the sequence (3, 2, 3, 10, 9) is 3 . There are a total of n^m sequences of length m where each element is an integer between 1 and n (inclusive). Find the beauty of each of these n^m sequences, and print the average of those values. Constraints 0 \leq d < n \leq 10^9 2 \leq m \leq 10^9 All values in input are integers. Input Input is given from Standard Input in the following format: n m d Output Print the average of the beauties of the sequences of length m where each element is an integer between 1 and n . The output will be judged correct if the absolute or relative error is at most 10^{-6} . Sample Input 1 2 3 1 Sample Output 1 1.0000000000 The beauty of (1,1,1) is 0 . The beauty of (1,1,2) is 1 . The beauty of (1,2,1) is 2 . The beauty of (1,2,2) is 1 . The beauty of (2,1,1) is 1 . The beauty of (2,1,2) is 2 . The beauty of (2,2,1) is 1 . The beauty of (2,2,2) is 0 . The answer is the average of these values: (0+1+2+1+1+2+1+0)/8=1 . Sample Input 2 1000000000 180707 0 Sample Output 2 0.0001807060
35,918
Problem A: Alien's Counting Natsuki and her friends were taken to the space by an alien and made friends with a lot of aliens. During the space travel, she discovered that aliens’ hands were often very different from humans’. Generally speaking, in a kind of aliens, there are N fingers and M bend rules on a hand. Each bend rule describes that a finger A always bends when a finger B bends. However, this rule does not always imply that the finger B bends when the finger A bends. When she were counting numbers with the fingers, she was anxious how many numbers her alien friends can count with the fingers. However, because some friends had too complicated rule sets, she could not calculate those. Would you write a program for her? Input N M S 1 D 1 S 2 D 2 . . . S M D M The first line contains two integers N and M (1 ≀ N ≀ 1000, 0 ≀ M ≀ 1000) in this order. The following M lines mean bend rules. Each line contains two integers S i and D i in this order, which mean that the finger D i always bends when the finger S i bends. Any finger appears at most once in S . Output Calculate how many numbers her alien friends can count with the fingers. Print the answer modulo 1000000007 in a line. Sample Input 1 5 4 2 3 3 4 4 3 5 4 Output for the Sample Input 1 10 Sample Input 2 5 5 1 2 2 3 3 4 4 5 5 1 Output for the Sample Input 2 2 Sample Input 3 5 0 Output for the Sample Input 3 32
35,919
Score: 600 points Problem Statement Takahashi and Aoki are training for long-distance races in an infinitely long straight course running from west to east. They start simultaneously at the same point and moves as follows towards the east : Takahashi runs A_1 meters per minute for the first T_1 minutes, then runs at A_2 meters per minute for the subsequent T_2 minutes, and alternates between these two modes forever. Aoki runs B_1 meters per minute for the first T_1 minutes, then runs at B_2 meters per minute for the subsequent T_2 minutes, and alternates between these two modes forever. How many times will Takahashi and Aoki meet each other, that is, come to the same point? We do not count the start of the run. If they meet infinitely many times, report that fact. Constraints 1 \leq T_i \leq 100000 1 \leq A_i \leq 10^{10} 1 \leq B_i \leq 10^{10} A_1 \neq B_1 A_2 \neq B_2 All values in input are integers. Input Input is given from Standard Input in the following format: T_1 T_2 A_1 A_2 B_1 B_2 Output Print the number of times Takahashi and Aoki will meet each other. If they meet infinitely many times, print infinity instead. Sample Input 1 1 2 10 10 12 4 Sample Output 1 1 They will meet just once, \frac{4}{3} minutes after they start, at \frac{40}{3} meters from where they start. Sample Input 2 100 1 101 101 102 1 Sample Output 2 infinity They will meet 101, 202, 303, 404, 505, 606, ... minutes after they start, that is, they will meet infinitely many times. Sample Input 3 12000 15700 3390000000 3810000000 5550000000 2130000000 Sample Output 3 113 The values in input may not fit into a 32 -bit integer type.
35,920
Max Score: $800$ Points Problem Statement There is an undirected connected graph with $N$ vertices and $N-1$ edges. The i -th edge connects u_i and v_i . E869120 the coder moves in the graph as follows: He move to adjacent vertex, but he can't a visit vertex two or more times. He ends move when there is no way to move. Otherwise, he moves randomly. (equal probability) If he has $p$ way to move this turn, he choose each vertex with $1/p$ probability. Calculate the expected value of the number of turns, when E869120 starts from vertex i , for all i (1 ≀ i ≀ N) . Input The input is given from standard input in the following format. N u_1 v_1 u_2 v_2 : u_{N-1} v_{N-1} Output In i -th (1 ≀ i ≀ N) line, print the expected vaule of the number of turns E869120 moves. The relative error or absolute error of output should be within 10^{-6} . Constraints $1 \le N \le 150,000$ The graph is connected. Subtasks Subtask 1 [ $190$ points ] There is no vertex which degree is more than 2. This means the graph looks like a list. Subtask 2 [ $220$ points ] 1 ≀ N ≀ 1000 . Subtask 3 [ $390$ points ] There are no additional constraints. Sample Input 1 4 1 2 2 3 2 4 Sample Output 1 2.0 1.0 2.0 2.0 Sample Input 2 4 1 2 2 4 4 3
35,921
D : Hopping Hearts / こころぎょんぎょん 問題文 N 矜のうさぎが長さ L−1 の平均台の䞊にいる。 i 番目のうさぎの初期䜍眮は敎数 x_i であり、 0 ≀ x_{i} \lt x_{i+1} ≀ L−1 を満たす。座暙は右に進むほど倧きくなる。任意の i 番目のうさぎは、任意の回数だけ、ちょうど距離 a_i だけ右方向にゞャンプ(すなわち、 x_i から x_i+a_i ぞ移動)するこずができる。ただし別のうさぎを飛び越えるこず、 −1 以䞋たたは L 以䞊の䜍眮に入るこずはできない。たた、同時にゞャンプしおいられるのは高々 1 矜であり、ある座暙に存圚できるうさぎの数も高々 1 矜である。 初期状態から始めおゞャンプを任意の回数繰り返したあずの x_{0}, 
, x_{N−1} の状態ずしお、あり埗るものは䜕通りあるか。 1\,000\,000\,007 で割った䜙りで求めよ。 入力 入力は以䞋の圢匏で䞎えられる。 N L x_{0} 
 x_{N−1} a_{0} 
 a_{N−1} 制玄 入力はすべお敎数である 1 \≀ N \≀ 5\,000 N \≀ L \≀ 5\,000 0 \≀ x_{i} \lt x_{i+1} \≀ L−1 0 \≀ a_{i} \≀ L−1 出力 答えを1行で出力せよ。 サンプル サンプル入力1 1 3 0 1 サンプル出力1 3 1/0でうさぎがいる/いないを衚珟すれば、100, 010, 001 の 3 通りである。 サンプル入力2 2 4 0 1 1 2 サンプル出力2 4 1100, 1001, 0101, 0011 の 4 通りである。 サンプル入力3 10 50 0 1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1 サンプル出力3 272278100 二項係数 C(50,10) = 10\,272\,278\,170 であり、それを 1\,000\,000\,007 で割ったあたりは 272\,278\,100 である。
35,922
Problem A: Whist Whist is a game played by four players with a standard deck of playing cards. The players seat around a table, namely, in north, east, south, and west. This game is played in a team-play basis: the players seating opposite to each other become a team. In other words, they make two teams we could call the north-south team and the east-west team. Remember that the standard deck consists of 52 cards each of which has a rank and a suit. The rank indicates the strength of the card and is one of the following: 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king, and ace (from the lowest to the highest). The suit refers to the type of symbols printed on the card, namely, spades, hearts, diamonds, and clubs. The deck contains exactly one card for every possible pair of a rank and a suit, thus 52 cards. One of the four players (called a dealer) shuffles the deck and deals out all the cards face down, one by one, clockwise from the player left to him or her. Each player should have thirteen cards. Then the last dealt card, which belongs to the dealer, is turned face up. The suit of this card is called trumps and has a special meaning as mentioned below. A deal of this game consists of thirteen tricks. The objective for each team is winning more tricks than another team. The player left to the dealer leads the first trick by playing one of the cards in his or her hand. Then the other players make their plays in the clockwise order. They have to play a card of the suit led if they have one; they can play any card otherwise. The trick is won by the player with the highest card of the suit led if no one plays a trump, or with the highest trump otherwise. The winner of this trick leads the next trick, and the remaining part of the deal is played similarly. After the thirteen tricks have been played, the team winning more tricks gains a score, one point per trick in excess of six. Your task is to write a program that determines the winning team and their score for given plays of a deal. Input The input is a sequence of datasets. Each dataset corresponds to a single deal and has the following format: Trump Card N,1 Card N,2 ... Card N,13 Card E,1 Card E,2 ... Card E,13 Card S,1 Card S,2 ... Card S,13 Card W,1 Card W,2 ... Card W,13 Trump indicates the trump suit. Card N, i , Card E, i , Card S, i , and Card W, i denote the card played in the i -th trick by the north, east, south, and west players respectively. Each card is represented by two characters; the first and second character indicates the rank and the suit respectively. The rank is represented by one of the following characters: ‘ 2 ’, ‘ 3 ’, ‘ 4 ’, ‘ 5 ’, ‘ 6 ’, ‘ 7 ’, ‘ 8 ’, ‘ 9 ’, ‘ T ’ (10), ‘ J ’ (jack), ‘ Q ’ (queen), ‘ K ’ (king), and ‘ A ’ (ace). The suit is represented by one of the following characters: ‘ S ’ (spades), ‘ H ’ (hearts), ‘ D ’ (diamonds), and ‘ C ’ (clubs). You should assume the cards have been dealt out by the west player. Thus the first trick is led by the north player. Also, the input does not contain any illegal plays. The input is terminated by a line with “ # ”. This is not part of any dataset and thus should not be processed. Output For each dataset, print the winner and their score of the deal in a line, with a single space between them as a separator. The winner should be either “ NS ” (the north-south team) or “ EW ” (the east-west team). No extra character or whitespace should appear in the output. Sample Input H 4C 8H QS 5D JD KS 8S AH 6H 7H 3S 7S 6D TC JC JS KD AC QC QD 2H QH 3H 3C 7C 4D 6C 9C AS TD 5H 6S 5S KH TH AD 9S 8D 2D 8C 5C 2S 7D KC 4S TS JH 4H 9H 2C 9D 3D D 8D 9D 9S QS 4H 5H JD JS 9H 6S TH 6H QH QD 9C 5S 7S 7H AC 2D KD 6C 3D 8C TC 7C 5D QC 3S 4S 3H 3C 6D KS JC AS 5C 8H TS 4D 4C 8S 2S 2H KC TD JH 2C AH 7D AD KH # Output for the Sample Input EW 1 EW 2 The winners of the tricks in the first dataset are as follows (in the order of tricks): east, north, south, east, south, north, west, north, east, west, east, east, north.
35,923
珟代的な屋敷(Modern Mansion) あなたはある倧きな屋敷に迷い蟌んでしたったこの屋敷は正方圢の郚屋が東西南北に栌子状に東西方向に M 列南北方向に N 行の合蚈 M × N 個䞊んだ構造をしおいる西から x 列目(1 ≀ x ≀ M )南から y 行目(1 ≀ y ≀ N ) にある郚屋を( x , y ) で衚す 東西南北に隣り合う 2 郚屋の間は壁の䞭倮にある扉により結ばれおいるそれぞれの扉は閉じおいお通行䞍可胜な状態か開いおいお通行可胜な状態のいずれかにある扉が開いおいるずきそれらの郚屋の䞭倮間を移動するのに 1 分間かかるたたいく぀かの郚屋の䞭倮にはスむッチがありスむッチを 1 分間抌し続けるず屋敷内のすべおの扉の開閉の状態が切り替わる 今東西に隣り合う 2 郚屋を結ぶすべおの扉は閉じおいお南北に隣り合う 2 郚屋を結ぶすべおの扉は開いおいるあなたは今郚屋 (1, 1) の䞭倮にいお郚屋 ( M , N ) の䞭倮たで最短時間で移動したい 課題 屋敷の倧きさ M , N およびスむッチのある K 個の郚屋の䜍眮 ( X 1 , Y 1 ), ( X 2 , Y 2 ),..., ( X K , Y K ) が䞎えられる東西に隣り合う 2 郚屋を結ぶすべおの扉は閉じおいお南北に隣り合う 2 郚屋を結ぶすべおの扉は開いおいる状態から始めお郚屋 (1, 1) の䞭倮から郚屋 ( M , N ) の䞭倮たで移動するのに最短で䜕分かかるかを求めるプログラムを䜜成せよただし郚屋 ( M , N ) に蟿り着くこずができないずきはそれを指摘せよ 制限 2 ≀ M ≀ 100 000 屋敷の東西方向の郚屋の個数 2 ≀ N ≀ 100 000 屋敷の南北方向の郚屋の個数 1 ≀ K ≀ 200 000 スむッチのある郚屋の個数 1 ≀ X i ≀ M スむッチのある郚屋の東西方向の䜍眮 1 ≀ Y i ≀ N スむッチのある郚屋の南北方向の䜍眮 入力 暙準入力から以䞋のデヌタを読み蟌め 1 行目には敎数 M, N, K が空癜を区切りずしお曞かれおいる M は屋敷の東西方向の郚屋の個数 N は屋敷の南北方向の郚屋の個数 K はスむッチのある郚屋の個数を衚す 続く K 行のうちの i 行目(1 ≀ i ≀ K ) には敎数 X i , Y i が空癜を区切りずしお曞かれおいるこれは郚屋( X i , Y i ) の䞭倮にスむッチがあるこずを衚す K 個の組( X 1 , Y 1 ), ( X 2 , Y 2 ),..., ( X K , Y K ) は互いに異なる 出力 暙準出力に移動に最短で䜕分かかるかを衚す敎数を 1 行で出力せよただし郚屋( M , N ) に蟿り着くこずができないずきは代わりに敎数 -1 を出力せよ 採点基準 採点甚デヌタのうち配点の20%分に぀いおは M ≀ 1 000, N ≀ 1 000 を満たす 採点甚デヌタのうち配点の30%分に぀いおは K ≀ 2 000 を満たす 採点甚デヌタのうち配点の50%分に぀いおはこれら 2 ぀の条件の少なくずも䞀方を満たすたたこれら 2 ぀の条件の䞡方を満たすような採点甚デヌタはない 入出力䟋 入力䟋 1 3 2 1 1 2 出力䟋 1 4 この䟋では以䞋の行動によっお 4 分間で郚屋(1, 1) の䞭倮から郚屋(3, 2) の䞭倮ぞ移動するこずができこれが最短である 郚屋(1, 2) の䞭倮ぞ移動する 郚屋(1, 2) の䞭倮のスむッチを抌す 郚屋(2, 2) の䞭倮ぞ移動する 郚屋(3, 2) の䞭倮ぞ移動する このずきの屋敷の様子が以䞋の図に衚されおいる図では右方向が東䞊方向が北であり×印はあなたの䜍眮○印はスむッチを衚す 入力䟋 2 3 2 1 2 1 出力䟋 2 -1 この䟋ではあなたは郚屋(3, 2) に蟿り着くこずができない 入力䟋 3 8 9 15 3 1 3 2 3 7 3 8 1 1 4 5 4 3 5 6 5 8 6 3 6 2 7 5 8 9 8 6 8 5 出力䟋 3 25 この䟋では最初の屋敷の様子は以䞋の図のようになっおいる郚屋(1, 1) や郚屋( M , N ) の䞭倮にスむッチがある可胜性もあるこずに泚意せよ 問題文ず自動審刀に䜿われるデヌタは、 情報オリンピック日本委員䌚 が䜜成し公開しおいる問題文ず採点甚テストデヌタです。
35,924
テトリス テトリスは、萜ちおくるブロックを盀面䞊に䞊べお消すゲヌムです。ここでは、それを少しアレンゞしたゲヌムを考えたしょう。 このゲヌムの盀面の倧きさは暪 5 コマで、出珟するブロックがすべお入るだけの高さがありたす。萜ちおくるブロックは盎線状で、暪向き、瞊向きの 2 皮類があり、長さは 1 から 5 たでの 5 皮類です。 以䞋に䟋を瀺したす。Step(ã‚€) からStep(ホ)たでの図はブロックが萜ちお消えおいく様子を衚したものです。 Step(ã‚€)から順にStep(ロ)、Step(ハ)ず順に進んでいきたす。 ブロックを萜ずすずきに、Step(ã‚€)のようにブロックのどこかが䞋に積んであるブロックに匕っかかったずきには、Step(ロ)のように萜ちたブロックはその堎所で止たりたす。たた、ブロックを萜ずした結果、盀面の暪䞀行の党おのコマにブロックが詰たった堎合には、Step(ニ)で瀺されるように、その行のブロックが消えたす。この埌、消えた行の䞊にあるブロックが、そのたたの圢で1行䞋にしずみたす(Step(ホ))。 1 ゲヌムは 1000 個以内のブロックが順に萜ちおくるこずずしたす。䟋えば、萜ちるブロックの長さが順番に暪向き 4 コマ, 暪向き 3 コマ, 瞊向き 2 コマ, 瞊向き 3 コマで、萜ちる堎所が巊端から 1 コマ目、1 コマ目、4 コマ目、5 コマ目であった堎合には、䞋図のStep(ã‚€)~(ト)のように萜ち、最埌に残るブロックは 2 コマになりたす。 順番に萜ちおくるブロックの情報を入力ずし、党おのブロックが萜ちた時に残るコマ数を出力するプログラムを䜜成しおください。 Input 耇数のデヌタセットの䞊びが入力ずしお䞎えられたす。入力の終わりはれロひず぀の行で瀺されたす。各デヌタセットは以䞋の圢匏で䞎えられたす。 n d 1 p 1 q 1 d 2 p 2 q 2 : d n p n q n 行目にブロックの数 n (1 ≀ n ≀ 1000) が䞎えられたす。続く n 行に i 個目のブロックの向き d i (1 たたは2)、ブロックの長さ p i (1 ≀ p i ≀ 5)、ブロックの䜍眮 q i が䞎えられたす。ブロックの向き d i は 1 が暪向きを、2 が瞊向きを衚したす。ブロックの䜍眮 q i は盀面䞊の巊端から1 ~ 5 たでの敎数ずし、暪向きのブロックの堎合は巊端のコマの萜ちる䜍眮ずしたす。 デヌタセットの数は 20 を超えたせん。 Output デヌタセット毎に最埌に残るブロックの占めるコマ数を行に出力したす。 Sample Input 4 1 4 1 1 3 1 2 2 4 2 3 5 1 1 5 1 7 2 2 2 1 4 1 2 1 3 1 4 1 1 1 1 2 5 5 1 4 2 0 Output for the Sample Input 2 0 6
35,925
Problem I: Shiritori Problem ここに文字列のリストがある。息抜きに、しりずりをしお遊ぶこずにしよう。しりずりは、以䞋のルヌルで行われる。 たず最初に、リストの䞭から奜きな文字列を䞀぀遞び、その文字列をリストから陀倖する。 続いお、䞀぀前に遞んだ文字列の最埌の䞀文字が、最初の䞀文字である文字列をリストから䞀぀遞ぶ。 遞んだ文字列をリストから陀倖する。 この埌、2., 3.を亀互に繰り返すこずになる。 さお、このしりずりを2.でリストから遞べる文字列がなくなるたで続けたずしよう。 このずきに、最埌に遞んだ文字列の「最埌の䞀文字」ずしおあり埗る文字をすべお列挙したい。 Input $N$ $s_1$ $s_2$ $s_3$ : $s_N$ 䞀行目に、文字列の数$N$が䞎えられる。 続く$N$行に、文字列のリストが䞎えられる。 Constraints 入力は以䞋の条件を満たす。 $1 \le N \lt 10^4$ $1 \le |s_i| \lt 100$ $s_i \neq s_j (i \neq j)$ 文字列には、英小文字のみ含たれる Output 最埌に遞んだ文字列の「最埌の䞀文字」ずしおあり埗る文字をaからzの順で蟞曞順で昇順に出力せよ。 Sample Input 1 5 you ate my hum toast Sample Output 1 e t u この時、以䞋のような堎合がアりトプット䟋ずなる。 ate(1぀の文字列のみ) toast hum - my - you たた、m,yで終わるパタヌンは存圚しない。 Sample Input 2 7 she sells sea shells by the seashore Sample Output 2 a e y
35,926
JOI 旗 (JOI Flag) 問題 情報オリンピック日本委員䌚では今幎の日本情報オリンピック (JOI) を宣䌝するために JOI のロゎをモチヌフにした旗を䜜るこずになった旗は「良い旗」でなければならない「良い旗」ずはアルファベットの J, O, I のいずれかの文字を瞊 M 行暪 N 列の長方圢状に䞊べたもので J, O, I が以䞋の図のように (すなわちJ の右隣に O がその J の䞋隣に I が) 䞊んでいる箇所が旗の少なくずも 1 か所にあるものである 以䞋の図に「良い旗」の䟋を 2 ぀瀺す 以䞋の図に「良い旗」ではない䟋を 2 ぀瀺す いた M, N の倀および旗の䞀郚の堎所に぀いお J, O, I のどの文字にするかが決たっおおり入力ずしおその情報が䞎えられる考えられる「良い旗」は䜕通りあるかを蚈算しその数を 100000 (=10 5 ) で割った䜙りを出力するプログラムを䜜成せよ 入力 入力は 1 + M 行からなる 1 行目には旗の倧きさを衚す 2 ぀の敎数 M, N (2 ≀ M ≀ 20, 2 ≀ N ≀ 20) が空癜で区切られお曞かれおいる 1 + i 行目 (1 ≀ i ≀ M) にはN 文字からなる文字列が曞かれおいる各文字は J, O, I, ? のいずれかであり j 文字目 (1 ≀ j ≀ N) が J, O, I のいずれかである堎合は i 行 j 列の堎所の文字がそれぞれ J, O, I に決たっおいるこず ? である堎合はただ決たっおいないこずを衚す 出力 考えられる「良い旗」の個数を 100000 (=10 5 ) で割った䜙りを 1 行で出力せよ 入出力䟋 入力䟋 1 2 3 ??O IIJ 出力䟋 1 4 入力䟋 1 においおは以䞋の図の 4 通りの「良い旗」が考えられる 入力䟋 2 2 2 ?? ?? 出力䟋 2 3 入力䟋 3 3 3 ??I ??? O?J 出力䟋 3 53 入力䟋 4 5 4 JOI? ???? ???? ???? ?JOI 出力䟋 4 28218 入力䟋 4 においおは「良い旗」は 2428218 通り考えられるのでそれを 100000 (=10 5 ) で割った䜙りである 28218 を出力する 問題文ず自動審刀に䜿われるデヌタは、 情報オリンピック日本委員䌚 が䜜成し公開しおいる問題文ず採点甚テストデヌタです。
35,927
Small, Large, or Equal Write a program which prints small/large/equal relation of given two integers a and b . Input Two integers a and b separated by a single space are given in a line. Output For given two integers a and b , print a < b if a is less than b , a > b if a is greater than b , and a == b if a equals to b . Constraints -1000 ≀ a , b ≀ 1000 Sample Input 1 1 2 Sample Output 1 a < b Sample Input 2 4 3 Sample Output 2 a > b Sample Input 3 5 5 Sample Output 3 a == b
35,928
Problem J: BD Shelf Remember the boy called Jack. He likes anime, especially programs broadcasting in midnight or early morning. In his room, there is a big shelf and it is kept organized well. And there are anime BD/DVDs which he watched or gave up because of overlapping of broadcasting time with other animes. His money is tight, but he succeeded to get W hundreds dollars because of an unexpected event. And he came up with enhancing the content of his BD shelf. He is troubled about the usage of the unexpected income. He has a good judge for anime to choose the anime he can enjoy because he is a great anime lover. Therefore, first of all, he listed up expectable animes and assigned each anime a value that represents "a magnitude of expectation". Moreover Jack tagged a mysterious string M for each anime. However Jack did not tell you the meaning of the string M. Your task is to write a program that reads a string X and that maximizes the sum of magnitudes of expectation when Jack bought BD/DVD boxes of anime with the budget of W hundreds dollars. Here, Jack must buy anime's BD/DVD box that have a string X as a sub-string of the string M assigned the anime. If he can not buy any boxes, output -1. A sequence of string X is given in order in the input as query. Assume two strings X 1 and X 2 are given as string X , and X 1 is the prefix of X 2 . If X 1 is given in the input preceding X 2 , you must not buy an anime which has the minimum magnitude of expectation among animes extracted by X 1 in the calculation for X 2 . And vice versa ( X 1 is the prefix of X 2 and if X 2 is given in the input preceding X 1 , ...) . For example, If three strings "a", "abc" and "ab" are given in this order in the input, In the processing for string "abc", you must not extract an anime which has the minimum magnitude of expectation among animes extracted by the processing of the string "a". In the processing for string "ab", you must not extract an anime has minimum magnitude of expectation among animes extracted by the processing of the string "abc" and "a". Input Input consists of multiple datasets. A dataset is given in the following format. N W string 1 expectation 1 price 1 string 2 expectation 2 price 2 ... string N expectation N price N Q query 1 query 2 ... query Q N is an integer that represents the number of anime. Following N lines contains the information of each anime. string i (1≀ i ≀ N ) is a string consists of lowercase letters that represents string M assigned i -th anime by Jack. No two characters in this string are same. expectation i (1≀ i ≀ N ) is a natural number that represents a magnitude of expectation of the i -th anime. price i (1≀ i ≀ N ) is a natural number that represents a price(cost) in hundreds to buy the i -th anime. Q is an integer that represents the number of queries. query i (1≀ i ≀ Q ) is a string consists of lowercase letters. This is a string X described in the problem statement. N = W =0 shows the end of the input. Constraints There are 4 test cases. This is given by the following order. In the first testcase, 4 datasets satisfy N ≀10 and Q ≀10, 6 datasets satisfy N ≀5000 and Q ≀5000. In the 2nd, 3rd and 4th testcase, each satisfies N ≀20000 and Q ≀20000. The time limit for this problem is the limit for each testcase. 1≀ N ≀20000 1≀ W ≀20 1≀(the length of string i (1≀ i ≀ N ))≀10 1≀ price i (1≀ i ≀ N )≀20 1≀ expectation i (1≀ i ≀ N )≀7000000 1≀ Q ≀20000 1≀(the length of query i (1≀ i ≀ Q ))≀5 It is assured that all values for expectation are different. Output For each query, output an integer S on a line that represents the maximum sum of magnitudes of expectation in the following format. S Sample Input 3 4 abc 1 1 abc 10 1 abc 100 1 3 a abc ab 3 4 ab 1 1 ab 10 1 abc 100 1 3 abc ab a 3 4 abc 1 1 abc 10 1 abc 100 1 3 ab ab ab 8 20 abcdef 100 2 bcdef 200 1 cfghj 300 3 ksjirto 400 6 ksitoew 500 2 qwertyl 600 2 kjhbvc 700 2 edfghucb 800 1 10 ks cd hj e a g h j i a 0 0 Output for the Sample Input 111 110 100 100 11 10 111 110 100 900 300 300 2200 100 1100 1500 1400 900 -1
35,929
G ほが無限グリコ - Almost Infinite Glico 問題 N 個のマスが円環状に䞊んでいるフィヌルドがありたす。 i 番目 (1 \leq i \leq N-1) のマスの 1 ぀先は、 i+1 番目のマスになりたす。ただし、 i = N の堎合、その 1 ぀先は 1 番目のマスになりたす。 あなたが最初にいるマスは 1 番目のマスです。そこから以䞋のルヌルに埓っお、じゃんけんを K 回続けお行いたす。このずき、あなたず盞手は特殊なじゃんけんを䌚埗しおいるので、じゃんけんの勝ち方が M 通りありたす。 盞手ずじゃんけんを行い、あなたが盞手に勝ったずき、今出した手、すなわち勝ち方 i に応じたマス数 p_i だけ進みたす。負けたずきはその堎に留たりたす。 K 回のじゃんけんを終えたあずに、あなたが i 番目のマスにいる堎合の数を 1,000,000,007 で割った䜙りを、それぞれのマスに぀いお求めおください。ここで 2 ぀の堎合が異なるずは、 K 回のじゃんけんの各回のうち負けた回が䞀床でも異なる、たたは勝ち方が異なる回が䞀床でも存圚するこずを蚀いたす。勝ち方も区別するこずに泚意しおください。たた、それぞれの勝ち方に぀いお、発生する可胜性が党くないものは存圚しないこずずしたす。 入力圢匏 入力は 1 + M 行䞎えられる。 N M K p_1 ... p_M 1 行目では、マスの数 N 、 じゃんけんの勝ち方の数 M 、 じゃんけんを行う回数 K が䞎えられる。 続いお M 行䞎えられる。 i+1 行目では、 i 番目の勝ち方をした時に進めるマスの数 p_i が䞎えられる。 制玄 1 \leq N \leq 8 \times 10^2 1 \leq M \leq 10^4 1 \leq K \leq 10^9 1 \leq p_i \leq 10^9 出力圢匏 出力は N 行からなる。 i 行目では、 K 回のじゃんけんを終えたあずにあなたが i 番目のマスにいる堎合の数を出力せよ。 入力䟋1 10 3 2 3 6 6 出力䟋1 1 0 4 2 0 0 5 0 0 4 䞀般的なグリコです。この入力においお、じゃんけんは 2 回行われたす。 䟋えば、 2 回じゃんけんをした埌に 1 番目のマスにいるためには、党おのじゃんけんで負けなければいけたせん。これ以倖に 1 番目のマスで終わる組み合わせは存圚しないため、 1 通りです。 2 回じゃんけんをした埌に 4 番目のマスにいるためには、 1 回目のじゃんけんで 1 番目の勝ち方で勝぀ → 2 回目のじゃんけんで負ける 1 回目のじゃんけんで負ける → 2 回目のじゃんけんで 1 番目の勝ち方で勝぀ の 2 通りがありたす。順番が異なるならば、異なる堎合ずしお扱われるこずに泚意しおください。 入力䟋2 5 1 103 4 出力䟋2 355272559 885073297 355272559 607675915 607675915 1,000,000,007 で割った䜙りを出力するこずに泚意しおください。
35,930
Score : 150 points Problem Statement There are some goats on a grid with H rows and W columns. Alice wants to put some fences at some cells where goats do not exist so that no goat can get outside the grid. Goats can move in the four directions, that is, up, down, right and left. Goats can not move onto the cells where fences are placed. If a goat exists at one of the outermost cells in the grid, it can move outside. Goats do not move until all fences are placed. Find the minimum number of fences to be placed. Constraints 1 \leq H \leq 100 1 \leq W \leq 100 There is at least one goat on the given grid. Input The input is given from the Standart Input in the following format: H W S_1 : S_H The j -th (1 \leq j \leq W) character in S_i (1 \leq i \leq H) represents whether a goat exists at the cell located at row i and column j . Character . represents there is no goat, and X represents there is one goat at the cell.
35,931
旅行はい぀? あなたは友人ず旅行に行きたいず考えおいたす。ずころが、浪費癖のある友人はなかなか旅行費甚を貯めるこずができたせん。友人が今の生掻を続けおいるず、旅行に行くのはい぀になっおしたうか分かりたせん。そこで、早く旅行に行きたいあなたは、友人が蚈画的に貯蓄するこずを助けるプログラムを䜜成するこずにしたした。 友人のある月のお小遣いを M 円、その月に䜿うお金を N 円ずするず、その月は ( M - N ) 円貯蓄されたす。毎月の収支情報 M 、 N を入力ずし、貯蓄額が旅行費甚 L に達するのにかかる月数を出力するプログラムを䜜成しおください。ただし、12 ヶ月を過ぎおも貯蓄額が旅行費甚に達しなかった堎合はNA ず出力しおください。 Input 耇数のデヌタセットの䞊びが入力ずしお䞎えられたす。 入力の終わりはれロひず぀の行で瀺されたす。 各デヌタセットは以䞋の圢匏で䞎えられたす。 L M 1 N 1 M 2 N 2 : M 12 N 12 1 行目に旅行費甚 L (1 ≀ L ≀ 1000000, 敎数) が䞎えられたす。続く 12 行に、 i 月目の収支情報 M i , N i (0 ≀ M i , N i ≀ 100000, N i ≀ M i , 敎数) が䞎えられたす。 デヌタセットの数は 1000 を超えたせん。 Output 入力デヌタセットごずに、貯蓄額が旅行費甚に達するのにかかる月数を行に出力したす。 Sample Input 10000 5000 3150 5000 5000 0 0 5000 1050 5000 3980 5000 210 5000 5000 5000 5000 0 0 5000 2100 5000 2100 5000 2100 29170 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 70831 0 Output for the Sample Input 6 NA
35,932
Score : 700 points Problem Statement N cells are arranged in a row. Some of them may contain tokens. You are given a string s that consists of 0 s and 1 s. If the i -th character of s is 1 , the i -th cell (from left) contains a token. Otherwise, it doesn't contain a token. Snuke wants to perform the following operation as many times as possible. In each operation, he chooses three consecutive cells. Let's call the cells X, Y, Z from left to right. In order for the operation to be valid, both X and Z must contain tokens and Y must not contain a token. Then, he removes these two tokens and puts a new token on Y . How many operations can he perform if he performs operations in the optimal way? Constraints 1 \leq N \leq 500,000 |s| = N Each character in s is either 0 or 1 . Input Input is given from Standard Input in the following format: N s Output Print the answer. Sample Input 1 7 1010101 Sample Output 1 2 For example, he can perform two operations in the following way: Perform an operation on the last three cells. Now the string that represents tokens becomes 1010010 . Perform an operation on the first three cells. Now the string that represents tokens becomes 0100010 . Note that the choice of operations matters. For example, if he chooses three cells in the middle first, he can perform no more operations. Sample Input 2 50 10101000010011011110001001111110000101010111100110 Sample Output 2 10
35,933
Problem K: Manhattan Warp Machine 2 Problem ずある宇宙では、3次元の栌子点䞊に星が存圚し、宇宙人達はマンハッタンワヌプ装眮ずいう装眮を䜿い、星間を移動しおいる。 このワヌプ装眮には、 N 個のボタンが付いおおり、ボタン i を抌すず、珟圚いる星からのマンハッタン距離が d i である任意の星にワヌプするこずができる。 ワヌプ装眮は改良され、コストがかからずに移動出来る。 今、(0, 0, 0)の星にいるある宇宙人がある M 個の星を蚪れたいず考えおいる。 M 個の星を党お蚪れるのに最短で䜕回ワヌプをする必芁があるか答えよ。 もし、1぀でも蚪れるこずが䞍可胜な星がある堎合は-1を出力せよ。 M 個の星は奜きな順番に蚪れるこずが出来、党お蚪れた埌に(0, 0, 0)に戻っお来る必芁は無い。 3次元䞊の党おの栌子点には必ず星が存圚する。 点( x 1 , y 1 , z 1 )ず点( x 2 , y 2 , z 2 )間のマンハッタン距離は| x 1 - x 2 | + | y 1 - y 2 | + | z 1 - z 2 |で衚される。 Input N M d 1 ... d N x 1 y 1 z 1 ... x M y M z M 入力は党お敎数で䞎えられる。 1行目に N , M が䞎えられる。 2行目に移動可胜なマンハッタン距離 d i が空癜区切りで䞎えられる。 3行目以降の M 行に、 M 個の蚪れたい星の栌子点の座暙( x i , y i , z i )が1行ず぀空癜区切りで䞎えられる。 Constraints 入力は以䞋の条件を満たす。 1 ≀ N ≀ 15 1 ≀ M ≀ 15 1 ≀ d i ≀ 3×10 5 -10 5 ≀ x i , y i , z i ≀ 10 5 䞎えられるマンハッタン距離 d i は党お異なる。 䞎えられる栌子点の座暙は党お異なる。 Output M 個の星党おを蚪れるのにかかる最短ワヌプ回数を1行に出力せよ。 蚪れるこずが䞍可胜な星がある堎合は-1を出力せよ。 Sample Input 1 2 1 1 2 5 0 0 Sample Output 1 3 Sample Input 2 2 2 9 3 3 0 0 3 9 0 Sample Output 2 2 Sample Input 3 1 1 4 3 0 0 Sample Output 3 -1
35,934
Calender Colors Taro is a member of a programming contest circle. In this circle, the members manage their schedules in the system called Great Web Calender . Taro has just added some of his friends to his calendar so that he can browse their schedule on his calendar. Then he noticed that the system currently displays all the schedules in only one color, mixing the schedules for all his friends. This is difficult to manage because it's hard to tell whose schedule each entry is. But actually this calender system has the feature to change the color of schedule entries, based on the person who has the entries. So Taro wants to use that feature to distinguish their plans by color. Given the colors Taro can use and the number of members, your task is to calculate the subset of colors to color all schedule entries. The colors are given by "Lab color space". In Lab color space, the distance between two colors is defined by the square sum of the difference of each element. Taro has to pick up the subset of colors that maximizes the sum of distances of all color pairs in the set. Input The input is like the following style. N M L_{0} a_{0} b_{0} L_{1} a_{1} b_{1} ... L_{N-1} a_{N-1} b_{N-1} The first line contains two integers N and M ( 0 \leq M \leq N \leq 20 ), where N is the number of colors in the input, and M is the number of friends Taro wants to select the colors for. Each of the following N lines contains three decimal integers L ( 0.0 \leq L \leq 100.0 ), a ( -134.0 \leq a \leq 220.0 ) and b ( -140.0 \leq b \leq 122.0 ) which represents a single color in Lab color space. Output Output the maximal of the total distance. The output should not contain an error greater than 10^{-5} . Sample Input 1 3 2 0 0 0 10 10 10 100 100 100 Output for the Sample Input 1 30000.00000000000000000000 Sample Input 2 5 3 12.0 15.0 9.0 10.0 -3.0 2.2 3.5 6.8 9.0 2.1 4.4 5.9 1.2 4.0 -5.4 Output for the Sample Input 2 1003.44000000000005456968 Sample Input 3 2 1 1.0 1.0 1.0 0.0 0.0 0.0 Output for the Sample Input 3 0.00000000000000000000
35,935
Almost Identical Programs The programming contest named Concours de Programmation Comtemporaine Interuniversitaire (CPCI) has a judging system similar to that of ICPC; contestants have to submit correct outputs for two different inputs to be accepted as a correct solution. Each of the submissions should include the program that generated the output. A pair of submissions is judged to be a correct solution when, in addition to the correctness of the outputs, they include an identical program. Many contestants, however, do not stop including a different version of their programs in their second submissions, after modifying a single string literal in their programs representing the input file name, attempting to process different input. The organizers of CPCI are exploring the possibility of showing a special error message for such close submissions, indicating contestants what's wrong with such submissions. Your task is to detect such close submissions. Input The input consists of at most 100 datasets, each in the following format. s 1 s 2 Each of s 1 and s 2 is a string written in a line, with the length between 1 and 200, inclusive. They are the first and the second submitted programs respectively. A program consists of lowercase letters ( a , b , ..., z ), uppercase letters ( A , B , ..., Z ), digits ( 0 , 1 , ..., 9 ), double quotes ( " ), and semicolons ( ; ). When double quotes occur in a program, there are always even number of them. The end of the input is indicated by a line containing one ' . ' (period). Output For each dataset, print the judge result in a line. If the given two programs are identical, print IDENTICAL . If two programs differ with only one corresponding string literal, print CLOSE . Otherwise, print DIFFERENT . A string literal is a possibly empty sequence of characters between an odd-numbered occurrence of a double quote and the next occurrence of a double quote. Sample Input print"hello";print123 print"hello";print123 read"B1input";solve;output; read"B2";solve;output; read"C1";solve;output"C1ans"; read"C2";solve;output"C2ans"; """""""" """42""""" slow"program" fast"code" "super"fast"program" "super"faster"program" X"" X I"S""CREAM" I"CE""CREAM" 11"22"11 1"33"111 . Output for the Sample Input IDENTICAL CLOSE DIFFERENT CLOSE DIFFERENT DIFFERENT DIFFERENT CLOSE DIFFERENT
35,936
問題名 YAML YAML (YAML Ain't Markup Language)ずは、オブゞェクトを文字列で衚珟する圢匏の䞀぀です。 YAMLのサブセットで衚されたオブゞェクトず、プロパティを指定するク゚リが䞎えられるので、指定されたプロパティの倀を答えおください。 YAMLのサブセット YAML のサブセットは、次のような拡匵 BNF 蚘法で衚される構文芏則に埓いたす。 yaml: mapping(0) mapping(n): mapping-item(n) | mapping-item(n) mapping(n) mapping-item(n): indent(n) key ':' ' ' string '\n' | indent(n) key ':' '\n' mapping(m) (ただしm>n) key: [a-z0-9]+ (※英字小文字たたは数字からなる1文字以䞊の文字列) string: [a-z0-9 ]+ (※英字小文字たたは数字たたはスペヌスからなる1文字以䞊の文字列) indent(0): "" (※空文字列) indent(n+1): ' ' indent(n) (※スペヌスをn+1個䞊べた文字列) '\n'は改行文字を衚したす。 mapping(n) はオブゞェクトを衚し、 mapping(n) に含たれる mapping-item(n) は、オブゞェクトに含たれるプロパティを衚したす。 mapping-item(n): indent(n) key ':' ' ' string '\n' は、 key で衚されるプロパティの倀がstringで衚される文字列であるこずを瀺したす。 mapping-item(n): indent(n) key ':' '\n' mapping(m) は、 key で衚されるプロパティの倀がmapping(m)で衚されるオブゞェクトであるこずを瀺したす。 1぀のオブゞェクトに、2぀以䞊、同じ key を持぀プロパティが含たれるこずはありたせん。 プロパティを指定するク゚リの圢匏 プロパティを指定するク゚リは、 .key_1.key_2(..省略..).key_n のように、' . 'ず key が亀互に珟れる圢で䞎えられ、これは「 yaml で䞎えられたオブゞェクトの、 key 1 ずいう key を持぀プロパティの倀であるオブゞェクトの、 key 2 ずいう key を持぀プロパティの倀であるオブゞェクトの、(..省略..) key n ずいう key を持぀プロパティ」を衚したす。 なお、あるi( 1 ≀ i ≀ n - 1 )に぀いお、.key_1.key_2.(..省略..).key_iたでで衚されるプロパティの倀がオブゞェクトでない、たたはオブゞェクトであるがkey_i+1ずいうプロパティを含んでいない堎合、.key_1.key_2(..省略..).key_n で衚されるようなプロパティは存圚しないずみなしたす。 Input .key_1.key_2(...).key_n yaml 1 ≀ n ≀ 20 入力党䜓に含たれる文字数 ≀ 50,000 Output プロパティの倀を 1 行で出力しおください。 指定されたプロパティが存圚しない堎合は no such property , プロパティの倀がオブゞェクトの堎合は object , プロパティの倀が文字列の堎合は string "<文字列の内容>" ず出力しおください。 Sample Input 1 .tweets.1 name: shimeji id: shimejitan tweets: 1: shimejilove 2: azupero Output for the Sample Input 1 string "shimejilove" Sample Input 2 .a a: sample case Output for the Sample Input 2 string "sample case" Sample Input 3 .my.obj my: str: string value obj: a: a b: b c: c Output for the Sample Input 3 object Sample Input 4 .str.inner str: inner Output for the Sample Input 4 no such property Sample Input 5 .no.such.property object: prop1: str prop2: str Output for the Sample Input 5 no such property
35,937
Score : 200 points Problem Statement You are given a string S of length N . Among its subsequences, count the ones such that all characters are different, modulo 10^9+7 . Two subsequences are considered different if their characters come from different positions in the string, even if they are the same as strings. Here, a subsequence of a string is a concatenation of one or more characters from the string without changing the order. Constraints 1 \leq N \leq 100000 S consists of lowercase English letters. |S|=N Input Input is given from Standard Input in the following format: N S Output Print the number of the subsequences such that all characters are different, modulo 10^9+7 . Sample Input 1 4 abcd Sample Output 1 15 Since all characters in S itself are different, all its subsequences satisfy the condition. Sample Input 2 3 baa Sample Output 2 5 The answer is five: b , two occurrences of a , two occurrences of ba . Note that we do not count baa , since it contains two a s. Sample Input 3 5 abcab Sample Output 3 17
35,938
Problem D: Circle and Points You are given N points in the xy -plane. You have a circle of radius one and move it on the xy -plane, so as to enclose as many of the points as possible. Find how many points can be simultaneously enclosed at the maximum. A point is considered enclosed by a circle when it is inside or on the circle. Fig 1. Circle and Points Input The input consists of a series of data sets, followed by a single line only containing a single character '0', which indicates the end of the input. Each data set begins with a line containing an integer N , which indicates the number of points in the data set. It is followed by N lines describing the coordinates of the points. Each of the N lines has two decimal fractions X and Y , describing the x - and y -coordinates of a point, respectively. They are given with five digits after the decimal point. You may assume 1 <= N <= 300, 0.0 <= X <= 10.0, and 0.0 <= Y <= 10.0. No two points are closer than 0.0001. No two points in a data set are approximately at a distance of 2.0. More precisely, for any two points in a data set, the distance d between the two never satisfies 1.9999 <= d <= 2.0001. Finally, no three points in a data set are simultaneously very close to a single circle of radius one. More precisely, let P 1 , P 2 , and P 3 be any three points in a data set, and d 1 , d 2 , and d 3 the distances from an arbitrarily selected point in the xy -plane to each of them respectively. Then it never simultaneously holds that 0.9999 <= d i <= 1.0001 ( i = 1, 2, 3). Output For each data set, print a single line containing the maximum number of points in the data set that can be simultaneously enclosed by a circle of radius one. No other characters including leading and trailing spaces should be printed. Sample Input 3 6.47634 7.69628 5.16828 4.79915 6.69533 6.20378 6 7.15296 4.08328 6.50827 2.69466 5.91219 3.86661 5.29853 4.16097 6.10838 3.46039 6.34060 2.41599 8 7.90650 4.01746 4.10998 4.18354 4.67289 4.01887 6.33885 4.28388 4.98106 3.82728 5.12379 5.16473 7.84664 4.67693 4.02776 3.87990 20 6.65128 5.47490 6.42743 6.26189 6.35864 4.61611 6.59020 4.54228 4.43967 5.70059 4.38226 5.70536 5.50755 6.18163 7.41971 6.13668 6.71936 3.04496 5.61832 4.23857 5.99424 4.29328 5.60961 4.32998 6.82242 5.79683 5.44693 3.82724 6.70906 3.65736 7.89087 5.68000 6.23300 4.59530 5.92401 4.92329 6.24168 3.81389 6.22671 3.62210 0 Output for the Sample Input 2 5 5 11
35,939
Problem L Wall Making Game The game Wall Making Game , a two-player board game, is all the rage. This game is played on an $H \times W$ board. Each cell of the board is one of empty , marked , or wall . At the beginning of the game, there is no wall on the board. In this game, two players alternately move as follows: A player chooses one of the empty cells (not marked and not wall). If the player can't choose a cell, he loses. Towards each of the four directions (upper, lower, left, and right) from the chosen cell, the player changes cells (including the chosen cell) to walls until the player first reaches a wall or the outside of the board. Note that marked cells cannot be chosen in step 1, but they can be changed to walls in step 2. Fig.1 shows an example of a move in which a player chooses the cell at the third row and the fourth column. Fig.1: An example of a move in Wall Making Game. Your task is to write a program that determines which player wins the game if the two players play optimally from a given initial board. Input The first line of the input consists of two integers $H$ and $W$ $(1 \leq H, W \leq 20)$, where $H$ and $W$ are the height and the width of the board respectively. The following $H$ lines represent the initial board. Each of the $H$ lines consists of $W$ characters. The $j$-th character of the $i$-th line is ' . ' if the cell at the $j$-th column of the $i$-th row is empty, or ' X ' if the cell is marked. Output Print " First " (without the quotes) in a line if the first player wins the given game. Otherwise, print " Second " (also without the quotes) in a line. Sample Input 1 2 2 .. .. Output for the Sample Input 1 Second Sample Input 2 2 2 X. .. Output for the Sample Input 2 First Sample Input 3 4 5 X.... ...X. ..... ..... Output for the Sample Input 3 First
35,940
必勝䞊べ トランプを䜿ったゲヌムに「䞊べ」がありたす。ここではそれを簡単にしたゲヌムを考えたす。からの番号がそれぞれ曞かれた枚のカヌドを䜿っお䞊べをしたす。察戊は、者だけで次のようにゲヌムを進めたす。 「堎」にのカヌドを眮きたす。 者には、残りのカヌドがランダムに枚ず぀配垃されたす。 先手の手持ちのカヌドのうち、堎にあるカヌドの番号ず連続する番号のカヌドがあれば、そのうちの枚を堎に眮きたす。プレむダヌはカヌドが眮ける堎合には必ず眮かなければいけたせん。無いずきに限り、カヌドを出さずに盞手の番になりたす。 埌手も同じ芁領で、手持ちのカヌドを堎に眮きたす。 手順ずを繰り返しお、䞀方の手持ちのカヌドがなくなるたで続けたす。先に手持ちのカヌドをすべお堎に眮けた方が勝者ずなりたす。 先手のカヌドの番号が䞎えられたずき、埌手がどのようにカヌドを出しおきおも、先手が勝぀手順が少なくずも䞀぀あるかを刀定しお出力するプログラムを䜜成せよ。 Input 入力は以䞋の圢匏で䞎えられる。 N game 1 game 2 : game N 行目には、ゲヌムを行う回数 N (1 ≀ N ≀ 100) が䞎えられる。続く N 行に、 i 回目のゲヌムの情報 game i が䞎えられる。各 game i は、以䞋の圢匏で䞎えられる。 f 1 f 2 f 3 f 4 f 5 f 6 f j (1 ≀ f j ≀ 13, f j ≠ 7) は先手に配られるカヌドの番号である。ただし、同じ行に番号が重耇しお珟れるこずはない j ≠ k に぀いお f j ≠ f k )。 Output 各ゲヌムに぀いお、埌手がどのようにカヌドを出しおきおも、先手が勝぀手順が少なくずも䞀぀ある堎合「yes」、そうでない堎合「no」ず行に出力する。 Sample Input 1 5 1 2 3 4 5 6 1 3 5 6 8 4 1 2 3 4 5 8 1 2 4 5 10 11 1 2 3 6 9 11 Sample Output 1 yes yes no yes no
35,941
Score: 400 points Problem Statement The Kingdom of Takahashi has N towns, numbered 1 through N . There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i . Takahashi, the king, loves the positive integer K . The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there. Help the king by writing a program that answers this question. Constraints 2 \leq N \leq 2 \times 10^5 1 \leq A_i \leq N 1 \leq K \leq 10^{18} Input Input is given from Standard Input in the following format: N K A_1 A_2 \dots A_N Output Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there. Sample Input 1 4 5 3 2 4 1 Sample Output 1 4 If we start at Town 1 and use the teleporter 5 times, our travel will be as follows: 1 \to 3 \to 4 \to 1 \to 3 \to 4 . Sample Input 2 6 727202214173249351 6 5 2 5 3 2 Sample Output 2 2
35,942
Score : 1200 points Problem Statement There are N arrays. The length of each array is M and initially each array contains integers (12...M) in this order. Mr. Takahashi has decided to perform Q operations on those N arrays. For the i -th ( 1≀i≀Q ) time, he performs the following operation. Choose an arbitrary array from the N arrays and move the integer a_i ( 1≀a_i≀M ) to the front of that array. For example, after performing the operation on a_i=2 and the array (54321) , this array becomes (25431) . Mr. Takahashi wants to make N arrays exactly the same after performing the Q operations. Determine if it is possible or not. Constraints 2≀N≀10^5 2≀M≀10^5 1≀Q≀10^5 1≀a_i≀M Input The input is given from Standard Input in the following format: N M Q a_1 a_2 ... a_Q Output Print Yes if it is possible to make N arrays exactly the same after performing the Q operations. Otherwise, print No . Sample Input 1 2 2 3 2 1 2 Sample Output 1 Yes You can perform the operations as follows. Sample Input 2 3 2 3 2 1 2 Sample Output 2 No Sample Input 3 2 3 3 3 2 1 Sample Output 3 Yes You can perform the operations as follows. Sample Input 4 3 3 6 1 2 2 3 3 3 Sample Output 4 No
35,943
Arithmetic Progressions An arithmetic progression is a sequence of numbers $a_1, a_2, ..., a_k$ where the difference of consecutive members $a_{i+1} - a_i$ is a constant ($1 \leq i \leq k-1$). For example, the sequence 5, 8, 11, 14, 17 is an arithmetic progression of length 5 with the common difference 3. In this problem, you are requested to find the longest arithmetic progression which can be formed selecting some numbers from a given set of numbers. For example, if the given set of numbers is {0, 1, 3, 5, 6, 9}, you can form arithmetic progressions such as 0, 3, 6, 9 with the common difference 3, or 9, 5, 1 with the common difference -4. In this case, the progressions 0, 3, 6, 9 and 9, 6, 3, 0 are the longest. Input The input consists of a single test case of the following format. $n$ $v_1$ $v_2$ ... $v_n$ $n$ is the number of elements of the set, which is an integer satisfying $2 \leq n \leq 5000$. Each $v_i$ ($1 \leq i \leq n$) is an element of the set, which is an integer satisfying $0 \leq v_i \leq 10^9$. $v_i$'s are all different, i.e., $v_i \ne v_j$ if $i \ne j$. Output Output the length of the longest arithmetic progressions which can be formed selecting some numbers from the given set of numbers. Sample Input 1 6 0 1 3 5 6 9 Sample Output 1 4 Sample Input 2 7 1 4 7 3 2 6 5 Sample Output 2 7 Sample Input 3 5 1 2 4 8 16 Sample Output 3 2
35,944
Score : 400 points Problem Statement Given is a string S consisting of L and R . Let N be the length of S . There are N squares arranged from left to right, and the i -th character of S from the left is written on the i -th square from the left. The character written on the leftmost square is always R , and the character written on the rightmost square is always L . Initially, one child is standing on each square. Each child will perform the move below 10^{100} times: Move one square in the direction specified by the character written in the square on which the child is standing. L denotes left, and R denotes right. Find the number of children standing on each square after the children performed the moves. Constraints S is a string of length between 2 and 10^5 (inclusive). Each character of S is L or R . The first and last characters of S are R and L , respectively. Input Input is given from Standard Input in the following format: S Output Print the number of children standing on each square after the children performed the moves, in order from left to right. Sample Input 1 RRLRL Sample Output 1 0 1 2 1 1 After each child performed one move, the number of children standing on each square is 0, 2, 1, 1, 1 from left to right. After each child performed two moves, the number of children standing on each square is 0, 1, 2, 1, 1 from left to right. After each child performed 10^{100} moves, the number of children standing on each square is 0, 1, 2, 1, 1 from left to right. Sample Input 2 RRLLLLRLRRLL Sample Output 2 0 3 3 0 0 0 1 1 0 2 2 0 Sample Input 3 RRRLLRLLRRRLLLLL Sample Output 3 0 0 3 2 0 2 1 0 0 0 4 4 0 0 0 0
35,945
Score : 600 points Problem Statement We have a sequence of length N consisting of non-negative integers. Consider performing the following operation on this sequence until the largest element in this sequence becomes N-1 or smaller. Determine the largest element in the sequence (if there is more than one, choose one). Decrease the value of this element by N , and increase each of the other elements by 1 . It can be proved that the largest element in the sequence becomes N-1 or smaller after a finite number of operations. You are given an integer K . Find an integer sequence a_i such that the number of times we will perform the above operation is exactly K . It can be shown that there is always such a sequence under the constraints on input and output in this problem. Constraints 0 ≀ K ≀ 50 \times 10^{16} Input Input is given from Standard Input in the following format: K Output Print a solution in the following format: N a_1 a_2 ... a_N Here, 2 ≀ N ≀ 50 and 0 ≀ a_i ≀ 10^{16} + 1000 must hold. Sample Input 1 0 Sample Output 1 4 3 3 3 3 Sample Input 2 1 Sample Output 2 3 1 0 3 Sample Input 3 2 Sample Output 3 2 2 2 The operation will be performed twice: [2, 2] -> [0, 3] -> [1, 1]. Sample Input 4 3 Sample Output 4 7 27 0 0 0 0 0 0 Sample Input 5 1234567894848 Sample Output 5 10 1000 193 256 777 0 1 1192 1234567891011 48 425
35,946
問題 F : 党域朚 今G○○gle Code Jam の地区倧䌚が始たろうずしおいる 巊の垭に座っおいる男の ID は wata ず蚀うらしい 東京倧孊時代の蚘憶に䌌たような ID の仲間が居た芚えがある しかし僕の仲間は䞀人残さず矎少女だったはずだ 僕の蚘憶の䞭の wata はマトロむドが奜きだった 特にマトロむド亀差が倧奜きで 様々なマトロむド達を亀差させるこずに䞀皮の興奮すら芚えるず蚀う少し倉わった奎だった マトロむドの理論の力を䜿えば 䞎えられたグラフ䞊で蟺を共有しない耇数の党域朚を求めるこずはずおも簡単な問題だず蚀っおいた気がする しかし特別なグラフに関しおは マトロむドのアルゎリズムを盎接に適甚するよりも高速なアルゎリズムがあるのではないか 問題 N 個の頂点からなる完党グラフにおいお 蟺を共有しない党域朚を K 個䜜成せよ 完党グラフずは党おの盞異なる 2 頂点間に 1 本の蟺を持぀グラフである 䞋図は4 頂点の完党グラフの䟋である 4 頂点の完党グラフ 党域朚ずは元のグラフの党おの頂点ず䞀郚の蟺からなる朚のこずである 朚ずは連結か぀閉路を持たないグラフのこずである 䞋図は4 頂点の完党グラフにおける蟺を共有しない 2 ぀の党域朚である 4 頂点の完党グラフの党域朚の䟋 4 頂点の完党グラフの党域朚であり前の䟋ず蟺を共有しないもの 入力 入力は 1 行からなり 2 ぀の敎数 N , K が曞かれおいる 出力 条件を満たす K 個の党域朚を䜜るこずができない時-1 ずだけ出力せよ 条件を満たす K 個の党域朚を䜜るこずができる時 K 個の党域朚を改行で区切り出力せよ 1 ぀の党域朚は N - 1 行で衚される その i 行目にはその党域朚の蟺 i が結ぶ 2 ぀の頂点を衚す 2 ぀の敎数をスペヌスで区切り出力する ここで頂点は 1 から N たでの敎数で衚すものずする 制玄 2 ≀ N ≀ 10000 1 ≀ K ≀ 100 郚分点 この問題の刀定には20 点分のテストケヌスのグルヌプが蚭定されおいる このグルヌプに含たれるテストケヌスの入力は以䞋を満たす 1 ≀ N ≀ 8 入出力䟋 入出力䟋 1 入力䟋 1: 4 2 入力䟋 1 に察する出力の䟋: 1 2 1 4 2 3 1 3 2 4 3 4 この入出力䟋は問題文䞭の図ず察応しおいる 入出力䟋 2 入力䟋 2: 4 3 入力䟋 2 に察する出力の䟋: -1
35,947
Score : 600 points Problem Statement In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short. The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string. Given a string S of length N and an integer k (k \geq 3) , he defines the k -DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions: 0 \leq a < b < c \leq N - 1 S[a] = D S[b] = M S[c] = C c-a < k Here S[a] is the a -th character of the string S . Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds. For a string S and Q integers k_0, k_1, ..., k_{Q-1} , calculate the k_i -DMC number of S for each i (0 \leq i \leq Q-1) . Constraints 3 \leq N \leq 10^6 S consists of uppercase English letters 1 \leq Q \leq 75 3 \leq k_i \leq N All numbers given in input are integers Input Input is given from Standard Input in the following format: N S Q k_{0} k_{1} ... k_{Q-1} Output Print Q lines. The i -th line should contain the k_i -DMC number of the string S . Sample Input 1 18 DWANGOMEDIACLUSTER 1 18 Sample Output 1 1 (a,b,c) = (0, 6, 11) satisfies the conditions. Strangely, Dwango Media Cluster does not have so much DMC-ness by his definition. Sample Input 2 18 DDDDDDMMMMMCCCCCCC 1 18 Sample Output 2 210 The number of triples can be calculated as 6\times 5\times 7 . Sample Input 3 54 DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED 3 20 30 40 Sample Output 3 0 1 2 (a, b, c) = (0, 23, 36), (8, 23, 36) satisfy the conditions except the last one, namely, c-a < k_i . By the way, DWANGO is an acronym for "Dial-up Wide Area Network Gaming Operation". Sample Output 4 30 DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC 4 5 10 15 20 Sample Output 4 10 52 110 140
35,948
Score : 100 points Problem Statement There are N children in AtCoder Kindergarten. Mr. Evi will arrange the children in a line, then give 1 candy to the first child in the line, 2 candies to the second child, ..., N candies to the N -th child. How many candies will be necessary in total? Constraints 1≩N≩100 Input The input is given from Standard Input in the following format: N Output Print the necessary number of candies in total. Sample Input 1 3 Sample Output 1 6 The answer is 1+2+3=6 . Sample Input 2 10 Sample Output 2 55 The sum of the integers from 1 to 10 is 55 . Sample Input 3 1 Sample Output 3 1 Only one child. The answer is 1 in this case.
35,949
F: MOD Rush 問題 長さ N の正の敎数列 A ず、長さ M の正の敎数列 B が䞎えられたす。 すべおの (i, j) (1 \leq i \leq N, 1 \leq j \leq M) に぀いお、 A_i を B_j で割ったあたりを求め、それらの和を出力しおください。 入力圢匏 N M A_1 A_2 ... A_N B_1 B_2 ... B_M 制玄 1 \leq N, M \leq 2 \times 10^5 1 \leq A_i, B_i \leq 2 \times 10^5 入力はすべお敎数で䞎えられる 出力圢匏 答えを 1 行に出力しおください。最埌に改行しおください。 入力䟋 1 3 3 5 1 6 2 3 4 出力䟋 1 9 数列 A の 1 番目の芁玠を、数列 B の各芁玠で割ったあたりを考えたす。 5 を 2 で割るずあたりは 1 、 3 で割るずあたりは 2 、 4 で割るずあたりは 1 です。 同様に 2 番目の芁玠に぀いおも考えるず、あたりはそれぞれ 1, 1, 1 です。 3 番目の芁玠に぀いおも考えるず、あたりはそれぞれ 0, 0, 2 です。 あたりを合蚈するず 1 + 2 + 1 + 1 + 1 + 1 + 0 + 0 + 2 = 9 ずなるので、 9 を出力したす。 入力䟋 2 2 4 2 7 3 3 4 4 出力䟋 2 16 数列内には同じ倀が耇数含たれおいるこずがありたすが、それぞれの芁玠に察しおあたりを蚈算しお和を求めたす。 入力䟋 3 3 1 12 15 21 3 出力䟋 3 0
35,950
Pair of Primes We arrange the numbers between 1 and N (1 <= N <= 10000) in increasing order and decreasing order like this: 1 2 3 4 5 6 7 8 9 . . . N N . . . 9 8 7 6 5 4 3 2 1 Two numbers faced each other form a pair. Your task is to compute the number of pairs P such that both numbers in the pairs are prime. Input Input contains several test cases. Each test case consists of an integer N in one line. Output For each line of input, output P . Sample Input 1 4 7 51 Output for the Sample Input 0 2 2 6
35,951
Reversing Numbers Write a program which reads a sequence and prints it in the reverse order. Input The input is given in the following format: n a 1 a 2 . . . a n n is the size of the sequence and a i is the i th element of the sequence. Output Print the reversed sequence in a line. Print a single space character between adjacent elements (Note that your program should not put a space character after the last element). Constraints n ≀ 100 0 ≀ a i < 1000 Sample Input 1 5 1 2 3 4 5 Sample Output 1 5 4 3 2 1 Sample Input 2 8 3 3 4 4 5 8 7 9 Sample Output 2 9 7 8 5 4 4 3 3 Note 解説
35,952
Problem B: Hating Crowd Problem ニヌト君は1幎12ヶ月毎月30日たで蚈360日の䞖界線で暮らしおいたす。その䞖界では毎幎同じ日皋の N 個の連䌑が䞖界䞭の人に適甚されおいたした。連䌑 i は M i 月 D i 日から始たる連続した V i 日間です。 ニヌト君はNEETなので連䌑に関係なく毎日䌑みです。ある日ニヌト君は珍しく出かけようず思いたしたが、人混みが嫌いなので、連䌑の圱響で混雑する日はなるべく倖に出たくありたせん。そこで、ニヌト君は以䞋の方法で各日の混雑床を蚈算し、混雑床が䞀番小さい日を探そうずしおいたす。 ある日付 x が、連䌑 i によっお受ける圱響の床合を衚す数倀は、日付 x が連䌑 i の䞭に含たれおいれば S i であり、そうでなければ、 max ( 0, S i − min ( x から連䌑 i の初日たでの日数 , 連䌑 i の最終日から x たでの日数 ) )である ある日付 x の混雑床は、 N 個の連䌑から受ける圱響の床合の䞭で、最も倧きく受ける圱響の床合ずなる 1幎の䞭で䞀番小さい混雑床を出力しおください。 ただし、連䌑 i は幎を跚ぐ事がありたす。たた、連䌑の日皋は重耇する事がありたす。 Input 入力は以䞋の圢匏で䞎えられる。 N M 1 D 1 V 1 S 1 M 2 D 2 V 2 S 2 ... M N D N V N S N 1行目に敎数 N が䞎えられる。 2行目から N +1行目に敎数 M i , D i , V i , S i が空癜区切りで䞎えられる。1 ≀ i ≀ N  Constraints 入力は以䞋の条件を満たす。 1 ≀ N ≀ 100 1 ≀ M i ≀ 12 1 ≀ D i ≀ 30 1 ≀ V i , S i ≀ 360 Output 䞀番小さい混雑床を1行に出力する。 Sample Input 1 1 1 1 359 1 Sample Output 1 0 Sample Input 2 2 2 4 25 306 1 9 7 321 Sample Output 2 158 Sample Input 3 8 2 9 297 297 8 6 359 211 8 16 28 288 7 9 113 143 3 18 315 190 10 18 277 300 9 5 276 88 3 5 322 40 Sample Output 3 297
35,953
ペセフのおむモ 昔、ペセフのおむモずいうゲヌムがありたした。 n 人が参加しおいるずしたしょう。参加者は䞭心を向いお円陣を組み、1 から順番に番号が振られたす。アツアツのおむモがひず぀、参加者 n (巊の図内偎の倧きい数字の 30 )に枡されたす。おむモを枡された参加者は右隣の参加者にそのおむモを枡したす。 m 回目に枡された人は右隣の人に枡しお円陣から抜けたす(巊の図では m = 9 の堎合を衚しおいたす) 。 回枡す毎に䞀人ず぀ぬけ、最埌に残った人が勝者ずなり、おむモをいただきたす。 n , m が決たっおから、実際におむモを枡し始める前にどこにいたら勝おるかわかるずいいですよね。䞊の図は 30 人の参加者で 9 人ごずに抜けるずいうルヌルでこのゲヌムをした堎合を曞き衚しおいたす。内偎の倧きい数字が参加者に振られた番号、倖偎の小さい数字が抜ける順番です。それによるず、9,18,27,6,16,26 ずいう順番で円陣から抜け出し、最埌には 21 が残るこずになりたす。すなわち 21 が勝者ずなりたす(小さい数字が 30 になっおいたす)。 ゲヌム参加者数 n ず円陣から抜け出す参加者の間隔 m を入力し、勝者の番号を出力するプログラムを䜜成しおください。ただし、 m , n < 1000 ずしたす。 入力 耇数のデヌタセットが䞎えられたす。各デヌタセットは以䞋の圢匏で䞎えられたす。 n m ゲヌム参加者数 n 敎数ず円陣から抜け出す参加者の間隔 m 敎数が空癜区切りで行に䞎えられたす。 入力は぀の 0 で終わりたす。デヌタセットの数は 50 を超えたせん。 出力 各デヌタセットに察しお、勝者ずなりおむモをいただく人の番号敎数を行に出力しおください。 Sample Input 41 3 30 9 0 0 Output for the Sample Input 31 21
35,954
Score : 600 points Problem Statement There is a board with N rows and M columns. The information of this board is represented by N strings S_1,S_2,\ldots,S_N . Specifically, the state of the square at the i -th row from the top and the j -th column from the left is represented as follows: S_{i,j}= . : the square is empty. S_{i,j}= # : an obstacle is placed on the square. S_{i,j}= o : a piece is placed on the square. Yosupo repeats the following operation: Choose a piece and move it to its right adjecent square or its down adjacent square. Moving a piece to squares with another piece or an obstacle is prohibited. Moving a piece out of the board is also prohibited. Yosupo wants to perform the operation as many times as possible. Find the maximum possible number of operations. Constraints 1 \leq N \leq 50 1 \leq M \leq 50 S_i is a string of length M consisting of . , # and o . 1 \leq ( the number of pieces )\leq 100 . In other words, the number of pairs (i, j) that satisfy S_{i,j}= o is between 1 and 100 , both inclusive. Input Input is given from Standard Input in the following format: N M S_1 S_2 \vdots S_N Output Print the maximum possible number of operations in a line. Sample Input 1 3 3 o.. ... o.# Sample Output 1 4 Yosupo can perform operations 4 times as follows: o.. .o. ..o ... ... ... -> ... -> ... -> ..o -> ..o o.# o.# o.# o.# .o# Sample Input 2 9 10 .#....o#.. .#..#..##o .....#o.## .###.#o..o #.#...##.# ..#..#.### #o.....#.. ....###..o o.......o# Sample Output 2 24
35,955
Score : 100 points Problem Statement Takahashi has K 500 -yen coins. (Yen is the currency of Japan.) If these coins add up to X yen or more, print Yes ; otherwise, print No . Constraints 1 \leq K \leq 100 1 \leq X \leq 10^5 Input Input is given from Standard Input in the following format: K X Output If the coins add up to X yen or more, print Yes ; otherwise, print No . Sample Input 1 2 900 Sample Output 1 Yes Two 500 -yen coins add up to 1000 yen, which is not less than X = 900 yen. Sample Input 2 1 501 Sample Output 2 No One 500 -yen coin is worth 500 yen, which is less than X = 501 yen. Sample Input 3 4 2000 Sample Output 3 Yes Four 500 -yen coins add up to 2000 yen, which is not less than X = 2000 yen.
35,956
Score : 600 points Problem Statement You are given an integer sequence of length N , a = { a_1, a_2, 
, a_N }, and an integer K . a has N(N+1)/2 non-empty contiguous subsequences, { a_l, a_{l+1}, 
, a_r } (1 ≀ l ≀ r ≀ N) . Among them, how many have an arithmetic mean that is greater than or equal to K ? Constraints All input values are integers. 1 ≀ N ≀ 2 \times 10^5 1 ≀ K ≀ 10^9 1 ≀ a_i ≀ 10^9 Input Input is given from Standard Input in the following format: N K a_1 a_2 : a_N Output Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K . Sample Input 1 3 6 7 5 7 Sample Output 1 5 All the non-empty contiguous subsequences of a are listed below: { a_1 } = { 7 } { a_1, a_2 } = { 7, 5 } { a_1, a_2, a_3 } = { 7, 5, 7 } { a_2 } = { 5 } { a_2, a_3 } = { 5, 7 } { a_3 } = { 7 } Their means are 7 , 6 , 19/3 , 5 , 6 and 7 , respectively, and five among them are 6 or greater. Note that { a_1 } and { a_3 } are indistinguishable by the values of their elements, but we count them individually. Sample Input 2 1 2 1 Sample Output 2 0 Sample Input 3 7 26 10 20 30 40 30 20 10 Sample Output 3 13
35,957
Parentheses Editor You are working with a strange text editor for texts consisting only of open and close parentheses. The editor accepts the following three keys as editing commands to modify the text kept in it. ‘ ( ’ appends an open parenthesis (‘ ( ’) to the end of the text. ‘ ) ’ appends a close parenthesis (‘ ) ’) to the end of the text. ‘ - ’ removes the last character of the text. A balanced string is one of the following. “ () ” “ ( $X$ ) ” where $X$ is a balanced string “$XY$” where both $X$ and $Y$ are balanced strings Initially, the editor keeps an empty text. You are interested in the number of balanced substrings in the text kept in the editor after each of your key command inputs. Note that, for the same balanced substring occurring twice or more, their occurrences should be counted separately. Also note that, when some balanced substrings are inside a balanced substring, both the inner and outer balanced substrings should be counted. Input The input consists of a single test case given in a line containing a number of characters, each of which is a command key to the editor, that is, either ‘ ( ’, ‘ ) ’, or ‘ - ’. The number of characters does not exceed 200 000. They represent a key input sequence to the editor. It is guaranteed that no ‘ - ’ command comes when the text is empty. Output Print the numbers of balanced substrings in the text kept in the editor after each of the key command inputs are applied, each in one line. Thus, the number of output lines should be the same as the number of characters in the input line. Sample Input 1 (()())---) Sample Output 1 0 0 1 1 3 4 3 1 1 2 Sample Input 2 ()--()()----)(()())) Sample Output 2 0 1 0 0 0 1 1 3 1 1 0 0 0 0 0 1 1 3 4 4
35,958
螏み台昇降 JAG倧孊に通う䞀暹君通称カヌ君はこの倏友達であるあなたに誘われおICPC (International Collegiate Potchari Contest) に出堎するこずになった ICPCはスポヌツ系のコンテストであり高床な運動胜力が必芁ずされる しかしカヌ君はい぀もパ゜コンの前にいおばかりで少し動くだけでも疲れおしたうほどに運動䞍足だった そこでカヌ君はICPCでいい成瞟を残すための第1ステップずしお手軜に始められる運動「螏み台昇降」を始めるこずにした 螏み台昇降ずはその名の通り螏み台ず床ずの䞊り䞋りをただひたすら繰り返すだけの単玔な運動である ただし螏み台昇降では正しい足の昇降を行わなければその効果を埗るこずはできない 正しい昇降ずは以䞋の2皮類の内いずれかを満たす足の動きである 䞡足が床に぀いた状態から巊足ず右足を螏み台の䞊に䞊げお螏み台の䞊に䞡足ずも぀いた状態になる巊足ず右足どちらを先に䞊げおもよい 螏み台の䞊に䞡足ずも぀いた状態から巊足ず右足を床に䞋げお䞡足が床に぀いた状態になる巊足ず右足どちらを先に䞋げおもよい 以䞊からわかるように床たたは螏み台の䞊にいる状態から連続で片足だけを䞊げ䞋げしおも正しい昇降ずはならない 螏み台昇降運動では䞊蚘の正しい昇降の動きのいずれかを満たすずき1回ずカりントしそのカりント数が倧きければ倧きいほど効果を埗るこずができる 床ず螏み台を埀埩しなくおも片道だけで1回ずカりントするこずに泚意しおほしい あなたはチヌムメむトであるカヌ君に少しでも匷くなっおほしいず考えおいる そこであなたはカヌ君が螏み台昇降をさがっおいないかプログラムを曞いおチェックしおあげるこずにした カヌ君が螏み台昇降で動かした足の情報が䞎えられるので正しく昇降を行った回数を求めよ ただし 䞡足ずも床に぀いおいる状態から螏み台昇降を始める ものずする Input 入力は耇数のデヌタセットから構成され1぀の入力に含たれるデヌタセットの数は150以䞋である 各デヌタセットの圢匏は次の通りである $n$ $f_1$ $f_2$ ... $f_n$ 1行目で足を動かした回数を衚す敎数 $n$ ($1 \le n \le 100$) が䞎えられる 2行目で足の動䜜を衚す文字列である $f_i$ が時系列順に $n$ 個スペヌス区切りで䞎えられる $f_i$ は以䞋の4皮類の文字列の内いずれかである " lu " : 巊足を螏み台ぞ䞊げる " ru " : 右足を螏み台ぞ䞊げる " ld " : 巊足を床ぞ䞋げる " rd " : 右足を床ぞ䞋げる 床に぀いおいる足をさらに䞋げるような動䜜や螏み台に぀いおいる足をさらに䞊げるような動䜜は入力されないず仮定しおよい $n$ が0の行は入力の終わりを衚すこのデヌタに぀いおは凊理を行っおはならない Output 各デヌタセットに察しお1行で正しい螏み台昇降を行った回数を出力せよ 各行の終わりに改行を出力しない堎合や䞍必芁な文字を出力する堎合誀答ず刀断されおしたうため泚意するこず Sample Input 4 lu ru ld rd 4 lu ld lu ru 1 lu 10 ru lu ld rd ru rd ru lu rd ld 0 Output for Sample Input 2 1 0 4
35,959
Score : 300 points Problem Statement You are given a string s . Among the different substrings of s , print the K -th lexicographically smallest one. A substring of s is a string obtained by taking out a non-empty contiguous part in s . For example, if s = ababc , a , bab and ababc are substrings of s , while ac , z and an empty string are not. Also, we say that substrings are different when they are different as strings. Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j} . Constraints 1 ≀ |s| ≀ 5000 s consists of lowercase English letters. 1 ≀ K ≀ 5 s has at least K different substrings. Partial Score 200 points will be awarded as a partial score for passing the test set satisfying |s| ≀ 50 . Input Input is given from Standard Input in the following format: s K Output Print the K -th lexicographically smallest substring of K . Sample Input 1 aba 4 Sample Output 1 b s has five substrings: a , b , ab , ba and aba . Among them, we should print the fourth smallest one, b . Note that we do not count a twice. Sample Input 2 atcoderandatcodeer 5 Sample Output 2 andat Sample Input 3 z 1 Sample Output 3 z
35,960
Problem D: 䌝説の剣 ※ この問題には厚二成分が倚く含たれたす。胞焌けにご泚意ください。 ぀いに埩掻を遂げた魔王は、再び䞖界を闇に包むために人間界に攻め入ろうずしおいた。 魔王は、本栌的な䟵攻を始める前に、たず䌝説の剣を砎壊するこずにした。 前回の戊争においお、魔王は䌝説の剣を持぀勇者によっお倒された。 魔王の䜓は闇の衣によっお守られおいるため、生半可な攻撃では魔王を傷぀けるこずはできない。 しかし、䌝説の剣は、神々の加護により、魔王の闇の衣すら容易に貫いおしたう。 そのため、今回の戊争に勝利するためには、なんずしおもその䌝説の剣を手に入れお砎壊する必芁がある。 調査の末、䌝説の剣はずある遺跡の最奥に安眮され、次代の勇者が珟われるのを埅っおいるこずがわかった。 䌝説の剣は、邪悪な者や盗賊によっお奪われるのを防ぐために、呚囲に点圚する無数の宝珠によっお封印されおいる。 魔王は、匷力な魔力を持っおいるため、觊れるだけで宝珠を砎壊するこずができる。 ただし、宝珠による封印は倚重構造になっおおり、衚局から順に砎壊しおいく必芁がある。 䟋えば、第1の封印の宝珠を砎壊する前に第2以降の封印の宝珠に觊れたずしおも、砎壊するこずはできない。 たた、耇数の宝珠が同じ封印を構成しおいるこずもあるが、魔王はそのうちの䞀぀に觊れるだけで、同時にその封印を砎壊するこずができる。 遺跡には神聖な力が満ちおおり、䞊の魔物では入るこずすらたたならない。 そこで、魔王自ら赎き䌝説の剣を回収しおくるこずになった。 いかに魔王ずいえど、長時間その力を受け続ければただではすたない。 魔王の右腕であるあなたの仕事は、念のため魔王が党おの封印を砎壊し、䌝説の剣の䞋に蟿り぀くたでの時間を求めるこずである。 Input 入力は、耇数のデヌタセットからなり、入力の終わりはスペヌスで区切られたれロ二぀からなる行である。 デヌタセットの総数は50以䞋である。 各デヌタセットは、次の圢匏をしおいる。 w h s(1,1) ... s(1,w) s(2,1) ... s(2,w) ... s(h,1) ... s(h,w) w ず h は、それぞれ遺跡のフロアを衚珟する行列デヌタの幅ず高さを瀺す敎数であり、それぞれ 1 ≀ w, h ≀ 100 であり、 2 ≀ w + h ≀ 100 ず仮定しお良い。 続く h 行はそれぞれ、スペヌスで区切られた w 個の文字から構成されおおり、文字 s(y,x) は、座暙 (y,x) の地点の状態を瀺す。 その意味は、以䞋の通りである。 S魔王が最初にいる地点。フロアに必ず1぀だけ存圚する。 G䌝説の剣の地点。必ず1぀だけ存圚する。 .なにもない。 数字宝珠がある地点。数字は構成する封印の番号を瀺す。数字は1以䞊の敎数であり、間の数字に抜けはないずしおよい。 たた、魔王は、遺跡のフロア内の䞊䞋巊右の隣接する座暙に移動するこずができ、その移動にかかる時間を1ずする。 砎壊できる宝珠は觊れるだけで砎壊できるため、宝珠の砎壊には時間はかからない。 Output 各デヌタセットに察しお、魔王が党おの封印を砎壊し、䌝説に剣に蟿り぀くために必芁な最短時間を出力せよ。 Sample Input 10 10 S . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 . . . . . . . . . . . . . . . . . 4 . . . . . . . . . . . . . . 2 . . . . . . . . G 10 10 S . . . . . 3 . . . . . 3 . . . . . . . . . . . 1 . . . . . . . . . . . 4 . . . . . 3 . . . 1 . . . . . . . . . . 3 . . . . . . . . . . . . . . . . . 4 . . . . . . . . . 5 . . . . 2 . . . . . . . . G 10 10 S . . . . . . . . 1 . . . . . 5 . . . . . 4 . . . . . . . . . . . . 8 . 9 . . . . . . . 10 . . . . . . . 7 . G . . . . . . . . 11 . . . . . . 3 . . . . . . . . 6 . . . . . . . 2 . . . . . . . . . . . . 0 0 Output for Sample Input 38 36 71
35,961
Squid Ink Problem 近幎、むカたちの間では瞄匵り争いが頻繁に起きおいる。耇数のむカたちがチヌムを組み、自らのむカスミを歊噚に戊うのが近幎のむカの戊闘スタむルである。 珟圚も瞄匵り争いが起こっおおり、その戊堎は R × C のグリッドで衚される。瞄匵り争いに参加しおいるむカのゲ゜倪はこのグリッド䞊のある堎所にいる。この争いでは、重芁な堎所を敵よりも早く占拠するこずで戊況を有利にするこずができる。そのため、ゲ゜倪は重芁そうな堎所を1぀決め、そこになるべく早く移動したいず考えおいる。 ゲ゜倪は隣接する䞊䞋巊右のマスに移動するこずができる。グリッドの各マスには味方、たたは敵のむカスミが塗られおいる堎合がある。なにも塗られおいないマスに移動する堎合、2秒かかるが、味方のむカスミが塗られおいるマスに移動する堎合、その半分の時間(1秒)で枈む。敵のむカスミが塗られおいるマスには移動するこずができない。壁があるマスや戊堎の倖ぞは圓然移動するこずができない。 たた、ゲ゜倪は䞊䞋巊右のいずれかの方向を向き、むカスミを吐くこずができる。するず、前方の3マスが味方のむカスミで䞊曞きされる。ただし途䞭に壁がある堎合はその手前たでしかむカスミは届かない。この動䜜には2秒かかる。 戊堎の情報ずゲ゜倪の䜍眮ず目的の䜍眮が䞎えられるので、最短で䜕秒で移動できるか求めおほしい。 Input 入力は以䞋の圢匏で䞎えられる。 R C a 1,1 a 1,2 ... a 1,C a 2,1 a 2,2 ... a 2,C : a R,1 a R,2 ... a R,C 1行目に2぀の敎数 R , C が空癜区切りで䞎えられる。続く R 行に戊堎の情報ずしお C 個のマスの情報が䞎えられる。 a i,j は戊堎の䜍眮( i , j )のマスの情報を衚し、以䞋のいずれかの文字である。 '.': なにも塗られおいないマス '#': 壁 'o': 味方のむカスミが塗られおいるマス 'x': 敵のむカスミが塗られおいるマス 'S': ゲ゜倪の䜍眮(入力䞭に1぀だけ存圚し、マスはなにも塗られおいない) 'G': 目的の䜍眮(入力䞭に1぀だけ存圚し、マスはなにも塗られおいない) 䞎えられる入力は、目的の䜍眮たで移動するこずが可胜であるこずが保蚌される。 Constraints 2 ≀ R , C ≀ 30 Output ゲ゜倪が目的の䜍眮たで移動するのにかかる最短の秒数を1行に出力せよ。 Sample Input 1 5 5 S.... ..... ..... ..... ....G Sample Output 1 14 Sample Input 2 5 5 Sxxxx xxxxx xxxxx xxxxx xxxxG Sample Output 2 15 Sample Input 3 4 5 S#... .#.#. .#.#. ...#G Sample Output 3 23 Sample Input 4 4 5 S#ooo o#o#o o#o#o ooo#G Sample Output 4 14 Sample Input 5 4 5 G#### ooxoo ##x#o Soooo Sample Output 5 10
35,962
Score : 1600 points Problem Statement There are A slimes lining up in a row. Initially, the sizes of the slimes are all 1 . Snuke can repeatedly perform the following operation. Choose a positive even number M . Then, select M consecutive slimes and form M / 2 pairs from those slimes as follows: pair the 1 -st and 2 -nd of them from the left, the 3 -rd and 4 -th of them, ... , the (M-1) -th and M -th of them. Combine each pair of slimes into one larger slime. Here, the size of a combined slime is the sum of the individual slimes before combination. The order of the M / 2 combined slimes remain the same as the M / 2 pairs of slimes before combination. Snuke wants to get to the situation where there are exactly N slimes, and the size of the i -th ( 1 ≀ i ≀ N ) slime from the left is a_i . Find the minimum number of operations required to achieve his goal. Note that A is not directly given as input. Assume A = a_1 + a_2 + ... + a_N . Constraints 1 ≀ N ≀ 10^5 a_i is an integer. 1 ≀ a_i ≀ 10^9 Input Input is given from Standard Input in the following format: N a_1 a_2 ... a_N Output Print the minimum number of operations required to achieve Snuke's goal. Sample Input 1 2 3 3 Sample Output 1 2 One way to achieve Snuke's goal is as follows. Here, the selected slimes are marked in bold. (1, 1 , 1 , 1 , 1 , 1) → (1, 2 , 2 , 1) ( 1 , 2 , 2 , 1 ) → ( 3 , 3 ) Sample Input 2 4 2 1 2 2 Sample Output 2 2 One way to achieve Snuke's goal is as follows. ( 1 , 1 , 1, 1, 1, 1, 1) → ( 2 , 1, 1, 1, 1, 1) (2, 1, 1 , 1 , 1 , 1 ) → (2, 1, 2 , 2 ) Sample Input 3 1 1 Sample Output 3 0 Sample Input 4 10 3 1 4 1 5 9 2 6 5 3 Sample Output 4 10
35,963
ザ・スク゚アヌズ この床、有名なテヌマパヌクに、巚倧迷路ザ・スク゚アヌズが新しく完成したした。 消防眲の指導により避難蚓緎をしなければなりたせんが、巚倧迷路なだけに蚓緎にかかる時間を予枬するこずができたせん。そこで、あなたは以䞋の仕様をもずに避難蚓緎シミュレヌタを開発するこずになりたした。 巚倧迷路は図 1 に瀺すように、暪 W 、瞊 H の W × H 個のマス目で衚わされたす。各マス目は、通路(癜いマス目)、壁(茶色いマス目) 、非垞口(緑のマス目)のいずれかです。図䞭の○は人を衚し、その䞭の英小文字(E、W、S、N)はその人が向いおいる方角(東西南北)を衚しおいたす。図は䞊方向が北になるように描かれおいたす。 図1 巚倧迷路内にいる人は最初、東西南北のいずれかの方向を向いお立っおいたす。各人は 1 秒単䜍で同時に次に瀺す手順で移動を詊みたす。 珟圚向いおいる方向の、右、前、巊、埌のマス目を順番に調べ、最初に芋぀けた、空いおいる通路たたは非垞口の方向に向きを倉えたす。そのようなマス目が無い堎合は向きを倉えたせん。 目の前のマス目が空いおいお、他の人の目の前のマス目になっおいない堎合は移動したす。同じマス目を目の前のマスずする人が耇数いる堎合は、そのマス目の、東、北、西、南のマス目にいる人の順で遞択された 1 人が移動したす。 移動埌に非垞口に到着した人は、無事避難し迷路内から消えたす。 䞎えられた巚倧迷路ず人の䜍眮情報を入力ずし、党おの人が避難し終える時間を出力するプログラムを䜜成しおください。 脱出に 180 秒より長い時間を芁する堎合は NA ず出力しお䞋さい。 迷路ず人の䜍眮情報は、 H 行 W 列の文字によっお䞎えられたす。各文字の意味は以䞋のずおりです。 # : 壁 . : 床 X : 非垞口 E : 東を向いおいる人 N : 北を向いおいる人 W : 西を向いおいる人 S : 南を向いおいる人 なお、迷路ず倖郚ずの境界は壁 # たたは非垞口 X のいずれかです。たた、巚倧迷路の䞭には、人が必ず人以䞊いたす。 Input 耇数のデヌタセットの䞊びが入力ずしお䞎えられたす。 入力の終わりはれロふた぀の行で瀺されたす。 各デヌタセットは以䞋の圢匏で䞎えられたす。 W H str 1 str 2 : str H 1 行目に迷路の暪方向の倧きさ W 、瞊方向の倧きさ H (1 ≀ W, H ≀ 30) が䞎えられたす。続く H 行に迷路の i 行目を衚す文字列 str i (長さ W ) が䞎えられたす。 デヌタセットの数は 50 を超えたせん。 Output 入力デヌタセットごずに、党おの人が避難し終える時間を行に出力したす。 Sample Input 10 3 ########## #E.......X ########## 4 4 #### #N.# #..X #### 5 5 ##### #N..# ###.X #S..# ##### 6 6 ###### #..#X# #.EE.# ####N# #....# ###### 8 8 ##X##### #....E.# #####.## #.#...## #.W.#..# #.#.N#.X #X##.#.# ######## 0 0 Output for the Sample Input 8 NA 9 16 10
35,964
Tiny Room You are an employee of Automatic Cleaning Machine (ACM) and a member of the development team of Intelligent Circular Perfect Cleaner (ICPC). ICPC is a robot that cleans up the dust of the place which it passed through. Your task is an inspection of ICPC. This inspection is performed by checking whether the center of ICPC reaches all the $N$ given points. However, since the laboratory is small, it may be impossible to place all the points in the laboratory so that the entire body of ICPC is contained in the laboratory during the inspection. The laboratory is a rectangle of $H \times W$ and ICPC is a circle of radius $R$. You decided to write a program to check whether you can place all the points in the laboratory by rotating and/or translating them while maintaining the distance between arbitrary two points. Input The input consists of a single test case of the following format. $N$ $H$ $W$ $R$ $x_1$ $y_1$ : $x_N$ $y_N$ The first line consists of four integers $N, H, W$ and $R$ ($1 \leq N \leq 100$, $1 \leq H, W \leq 10^9$, $1 \leq R \leq 10^6$). The following $N$ lines represent the coordinates of the points which the center of ICPC must reach. The ($i+1$)-th line consists of two integers $x_i$ and $y_i$ ($0 \leq x_i, y_i \leq 10^9$). $x_i$ and $y_i$ represent the $x$ and $y$ coordinates of the $i$-th point, respectively. It is guaranteed that the answer will not change even if $R$ changes by $1$. Output If all the points can be placed in the laboratory, print ' Yes '. Otherwise, print ' No '. Sample Input 1 4 20 20 1 10 0 20 10 10 20 0 10 Output for Sample Input 1 Yes All the points can be placed in the laboratory by rotating them through $45$ degrees. Sample Input 2 2 5 55 1 0 0 30 40 Output for Sample Input 2 Yes Sample Input 3 2 5 49 1 0 0 30 40 Output for Sample Input 3 No Sample Input 4 1 3 3 1 114 514 Output for Sample Input 4 Yes
35,965
Problem D: Distorted Love Saying that it is not surprising that people want to know about their love, she has checked up his address, name, age, phone number, hometown, medical history, political party and even his sleeping position, every piece of his personal information. The word "privacy" is not in her dictionary. A person like her is called "stoker" or " yandere ", but it doesn't mean much to her. To know about him, she set up spyware to his PC. This spyware can record his mouse operations while he is browsing websites. After a while, she could successfully obtain the record from the spyware in absolute secrecy. Well, we want you to write a program which extracts web pages he visited from the records. All pages have the same size H × W where upper-left corner is (0, 0) and lower right corner is ( W , H ). A page includes several (or many) rectangular buttons (parallel to the page). Each button has a link to another page, and when a button is clicked the browser leads you to the corresponding page. His browser manages history and the current page in the following way: The browser has a buffer of 1-dimensional array with enough capacity to store pages, and a pointer to indicate a page in the buffer. A page indicated by the pointer is shown on the browser. At first, a predetermined page is stored and the pointer indicates that page. When the link button is clicked, all pages recorded in the right side from the pointer are removed from the buffer. Then, the page indicated by the link button is stored into the right-most position of the buffer, and the pointer moves to right. As a result, the user browse the page indicated by the button. The browser also has special buttons 'back to the previous page' (back button) and 'forward to the next page' (forward button). When the user clicks the back button, the pointer moves to left, and the user clicks the forward button, the pointer moves to right. But in both cases, if there are no such pages in the buffer, nothing happen. The record consists of the following operations: click x y It means to click ( x , y ). If there is a button on the point ( x , y ), he moved to the corresponding page. If there is nothing in the point, nothing happen. The button is clicked if x 1 ≀ x ≀ x 2 and y 1 ≀ y ≀ y 2 where x 1, x 2 means the leftmost and rightmost coordinate and y 1, y 2 means the topmost and bottommost coordinate of the corresponding button respectively. back It means to click the Back button. forward It means to click the Forward button. In addition, there is a special operation show . Your program should print the name of current page for each show operation. By the way, setting spyware into computers of others may conflict with the law. Do not attempt, or you will be reprimanded by great men. Input Input consists of several datasets. Each dataset starts with an integer n which represents the number of pages in the dataset. Next line contains two integers W and H . Next, information of each page are given. Each page starts with a string of characters and b [ i ], the number of buttons the page has. Following b [ i ] lines give information of buttons. Each button consists of four integers representing the coordinate ( x 1, y 1) of upper left corner and the coordinate ( x 2, y 2) of lower right corner of the button and a string of characters, which represents the name of page that the link of the button represents. Next, the number of operation m is given. Following m lines represent the record of operations. Please see the above description for the operation. The first page is stored in the buffer at first. Input ends when n = 0. Output For each dataset, output the name of current page for each show operation. Constraints 1 ≀ n ≀ 100 b [ i ] ≀ 100 1 ≀ the number of characters in the name ≀ 20 Buttons are not touch, overlapped nor run over from the browser. Sample Input 3 800 600 index 1 500 100 700 200 profile profile 2 100 100 400 200 index 100 400 400 500 link link 1 100 100 300 200 index 9 click 600 150 show click 200 450 show back back show forward show 0 Output for the Sample Input profile link index profile
35,966
Hello World Welcome to Online Judge! Write a program which prints "Hello World" to standard output. Input There is no input for this problem. Output Print "Hello World" in a line. Sample Input 1 No input Sample Output 1 Hello World
35,967
Score : 150 points Problem Statement Gorillas in Kyoto University are good at math. They are currently trying to solve problems to find the value of an expression that contains two functions, _ , ^ . Each of these functions takes two input values. _ function returns the smaller of the two input values and ^ function returns the larger. Gorillas know that integers in the expression are non-negative and less than or equal to 99 , but can not find out the length of the expression until they read a terminal symbol ? that represents the end of the expression. The number of characters included in each expression is less than or equal to 1000, but they do not even know this fact. Ai, a smart gorilla, noticed that she may be able to know the value of the expression even if they don't read the whole expression. For example, Assume you read the following sentence from the left. ^(41,3)? When you read the sixth character, that is, when you read the following expression, ^(41,3 you can tell the second input value of the funcion is whether 3 or an integer between 30 and 39 , and the value turns out 41 . Since Ai wants to solve problems earlier than other gorillas, she decided to solve the problems such that she reads as fewer characters as possible from the left. For each expression, Find the value of the expression and the minimum number of characters Ai needs to read to know the value. Constraints 1 \leq Q \leq 200 The number of characters each expression contains is less than or equal to 1000 . Input The input consists of multiple test cases and is given from Standard Input in the following format: Q statement_1 ... statement_Q Each statement_i (1 \leq i \leq Q) is given in the following BNF format. <statement> ::= <expression> ? <expression> ::= ( ^ | _ ) ( <expression> , <expression> ) | <number> <number> :: = 0 | 1 | 2 | ... | 98 | 99 Output Output consists of Q lines. On line i (1 \leq i \leq Q) , print the value of the expression and the number of character Ai needs to read for the test case i separated by space. Sample Input 1 4 _(4,51)? ^(99,_(3,67))? _(0,87)? 3? Sample Output 1 4 5 99 4 0 3 3 2 For the first test case, when you read the fifth character, that is, when you read _(4,5 , you will know the value is 4 . For the second test case, when you read the fourth character, that is, when you read ^(99 , you will know the value is 99 . For the third test case, when you read the third character, that is, when you read _(0 , you will know the value is 0 . For the fourth test case, you will not know the value is 3 untill you read the terminal symbol. Sample Input 2 7 _(23,^(_(22,40),4))? _(0,99)? ^(99,_(^(19,2),5))? _(^(43,20),^(30,29))? ^(_(20,3),_(50,41))? ^(_(20,3),_(3,41))? ^(_(20,3),_(4,41))? Sample Output 2 22 18 0 3 99 4 30 17 41 17 3 14 4 15
35,968
Score : 600 points Problem Statement There is a game that involves three variables, denoted A , B , and C . As the game progresses, there will be N events where you are asked to make a choice. Each of these choices is represented by a string s_i . If s_i is AB , you must add 1 to A or B then subtract 1 from the other; if s_i is AC , you must add 1 to A or C then subtract 1 from the other; if s_i is BC , you must add 1 to B or C then subtract 1 from the other. After each choice, none of A , B , and C should be negative. Determine whether it is possible to make N choices under this condition. If it is possible, also give one such way to make the choices. Constraints 1 \leq N \leq 10^5 0 \leq A,B,C \leq 10^9 N, A, B, C are integers. s_i is AB , AC , or BC . Input Input is given from Standard Input in the following format: N A B C s_1 s_2 : s_N Output If it is possible to make N choices under the condition, print Yes ; otherwise, print No . Also, in the former case, show one such way to make the choices in the subsequent N lines. The (i+1) -th line should contain the name of the variable ( A , B , or C ) to which you add 1 in the i -th choice. Sample Input 1 2 1 3 0 AB AC Sample Output 1 Yes A C You can successfully make two choices, as follows: In the first choice, add 1 to A and subtract 1 from B . A becomes 2 , and B becomes 2 . In the second choice, add 1 to C and subtract 1 from A . C becomes 1 , and A becomes 1 . Sample Input 2 3 1 0 0 AB BC AB Sample Output 2 No Sample Input 3 1 0 9 0 AC Sample Output 3 No Sample Input 4 8 6 9 1 AC BC AB BC AC BC AB AB Sample Output 4 Yes C B B C C B A A
35,969
Reservation System The supercomputer system L in the PCK Research Institute performs a variety of calculations upon request from external institutes, companies, universities and other entities. To use the L system, you have to reserve operation time by specifying the start and end time. No two reservation periods are allowed to overlap each other. Write a program to report if a new reservation overlaps with any of the existing reservations. Note that the coincidence of start and end times is not considered to constitute an overlap. All the temporal data is given as the elapsed time from the moment at which the L system starts operation. Input The input is given in the following format. a b N s_1 f_1 s_2 f_2 : s_N f_N The first line provides new reservation information, i.e., the start time a and end time b (0 ≀ a < b ≀ 1000) in integers. The second line specifies the number of existing reservations N (0 ≀ N ≀ 100). Subsequent N lines provide temporal information for the i -th reservation: start time s_i and end time f_i (0 ≀ s_i < f_i ≀ 1000) in integers. No two existing reservations overlap. Output Output "1" if the new reservation temporally overlaps with any of the existing ones, or "0" otherwise. Sample Input 1 5 7 3 1 4 4 5 7 10 Sample Output 1 0 Sample Input 2 3 7 3 7 10 1 4 4 5 Sample Output 2 1
35,970
Strongly Connected Components A direced graph is strongly connected if every two nodes are reachable from each other. In a strongly connected component of a directed graph, every two nodes of the component are mutually reachable. Input A directed graph G(V, E) and a sequence of queries where each query contains a pair of nodes u and v . |V| |E| s 0 t 0 s 1 t 1 : s |E|-1 t |E|-1 Q u 0 v 0 u 1 v 1 : u Q-1 v Q-1 |V| is the number of nodes and |E| is the number of edges in the graph. The graph nodes are named with the numbers 0, 1,..., |V| -1 respectively. s i and t i represent source and target nodes of i -th edge (directed). u i and v i represent a pair of nodes given as the i -th query. Output For each query, pinrt "1" if the given nodes belong to the same strongly connected component, "0" otherwise. Constraints 1 ≀ |V| ≀ 10,000 0 ≀ |E| ≀ 30,000 1 ≀ Q ≀ 100,000 Sample Input 1 5 6 0 1 1 0 1 2 2 4 4 3 3 2 4 0 1 0 3 2 3 3 4 Sample Output 1 1 0 1 1
35,971
I - ツむンリバヌス 芁玠数 N の配列 A が䞎えられる。ただし、 A は (1, 2, ... , N) の順列である。 次の操䜜を 0 回以䞊 10,000 回以䞋の任意の回数行い、 A を (1, 2, ... , N) ぞ゜ヌトしたい。 敎数 i ( 1 ≀ i ≀ N ) を 1 ぀遞び、区間 A[1,\ i-1] の芁玠を逆順にし、区間 A[i+1,\ N] の芁玠を逆順にする。 ただし、区間 A[l,\ r] ずは A の l, l+1, ... , r 番目の䜍眮のこずである。 A を (1, 2, ... , N) ぞ゜ヌトできるか刀定せよ。゜ヌトできるならば、操䜜の䟋を䞀぀出力せよ。 Constraints 1 ≀ N ≀ 3,000 A は (1, 2, ... , N) の順列である。 Input Format 入力は以䞋の圢匏で暙準入力から䞎えられる。 N A_1 A_2 ... A_N Output Format A を (1, 2, ... , N) ぞ゜ヌトできないならば、 -1 ずだけ䞀行に出力せよ。 ゜ヌトできるならば、操䜜の䟋を䞀぀次のように出力せよ。 1 行目には、操䜜の回数を衚す敎数 M ( 0 ≀ M ≀ 10,000 ) を出力せよ。 2 行目からの M 行のうち k 行目には、 k 回目の操䜜で遞ぶ敎数 i ( 1 ≀ i ≀ N ) を出力せよ。 Sample Input 1 5 5 1 4 2 3 Sample Output 1 2 3 1 䟋えば、次のように 2 回の操䜜を行えばよい。 i=3 を遞ぶず (5,\ 1,\ 4,\ 2,\ 3) → (1,\ 5,\ 4,\ 3,\ 2) i=1 を遞ぶず (1,\ 5,\ 4,\ 3,\ 2) → (1,\ 2,\ 3,\ 4,\ 5) Sample Input 2 2 2 1 Sample Output 2 -1 Sample Input 3 3 1 2 3 Sample Output 3 0
35,972
When Can We Meet? The ICPC committee would like to have its meeting as soon as possible to address every little issue of the next contest. However, members of the committee are so busy maniacally developing (possibly useless) programs that it is very difficult to arrange their schedules for the meeting. So, in order to settle the meeting date, the chairperson requested every member to send back a list of convenient dates by E-mail. Your mission is to help the chairperson, who is now dedicated to other issues of the contest, by writing a program that chooses the best date from the submitted lists. Your program should find the date convenient for the most members. If there is more than one such day, the earliest is the best. Input The input has multiple data sets, each starting with a line containing the number of committee members and the quorum of the meeting. N Q Here, N , meaning the size of the committee, and Q meaning the quorum, are positive integers. N is less than 50, and, of course, Q is less than or equal to N. N lines follow, each describing convenient dates for a committee member in the following format. M Date 1 Date 2 ... Date M Here, M means the number of convenient dates for the member, which is an integer greater than or equal to zero. The remaining items in the line are his/her dates of convenience, which are positive integers less than 100, that is, 1 means tomorrow, 2 means the day after tomorrow, and so on. They are in ascending order without any repetition and separated by a space character. Lines have neither leading nor trailing spaces. A line containing two zeros indicates the end of the input. Output For each data set, print a single line containing the date number convenient for the largest number of committee members. If there is more than one such date, print the earliest. However, if no dates are convenient for more than or equal to the quorum number of members, print 0 instead. Sample Input 3 2 2 1 4 0 3 3 4 8 3 2 4 1 5 8 9 3 2 5 9 5 2 4 5 7 9 3 3 2 1 4 3 2 5 9 2 2 4 3 3 2 1 2 3 1 2 9 2 2 4 0 0 Output for the Sample Input 4 5 0 2
35,973
Score : 1200 points Problem Statement Given is a positive integer N . Find the number of permutations (P_1,P_2,\cdots,P_{3N}) of (1,2,\cdots,3N) that can be generated through the procedure below. This number can be enormous, so print it modulo a prime number M . Make N sequences A_1,A_2,\cdots,A_N of length 3 each, using each of the integers 1 through 3N exactly once. Let P be an empty sequence, and do the following operation 3N times. Among the elements that are at the beginning of one of the sequences A_i that is non-empty, let the smallest be x . Remove x from the sequence, and add x at the end of P . Constraints 1 \leq N \leq 2000 10^8 \leq M \leq 10^9+7 M is a prime number. All values in input are integers. Input Input is given from Standard Input in the following format: N M Output Print the number of permutations modulo M . Sample Input 1 1 998244353 Sample Output 1 6 All permutations of length 3 count. Sample Input 2 2 998244353 Sample Output 2 261 Sample Input 3 314 1000000007 Sample Output 3 182908545
35,974
Score : 700 points Problem Statement Today, Snuke will eat B pieces of black chocolate and W pieces of white chocolate for an afternoon snack. He will repeat the following procedure until there is no piece left: Choose black or white with equal probability, and eat a piece of that color if it exists. For each integer i from 1 to B+W (inclusive), find the probability that the color of the i -th piece to be eaten is black. It can be shown that these probabilities are rational, and we ask you to print them modulo 10^9 + 7 , as described in Notes. Notes When you print a rational number, first write it as a fraction \frac{y}{x} , where x, y are integers and x is not divisible by 10^9 + 7 (under the constraints of the problem, such representation is always possible). Then, you need to print the only integer z between 0 and 10^9 + 6 , inclusive, that satisfies xz \equiv y \pmod{10^9 + 7} . Constraints All values in input are integers. 1 \leq B,W \leq 10^{5} Input Input is given from Standard Input in the following format: B W Output Print the answers in B+W lines. In the i -th line, print the probability that the color of the i -th piece to be eaten is black, modulo 10^{9}+7 . Sample Input 1 2 1 Sample Output 1 500000004 750000006 750000006 There are three possible orders in which Snuke eats the pieces: white, black, black black, white, black black, black, white with probabilities \frac{1}{2}, \frac{1}{4}, \frac{1}{4} , respectively. Thus, the probabilities of eating a black piece first, second and third are \frac{1}{2},\frac{3}{4} and \frac{3}{4} , respectively. Sample Input 2 3 2 Sample Output 2 500000004 500000004 625000005 187500002 187500002 They are \frac{1}{2},\frac{1}{2},\frac{5}{8},\frac{11}{16} and \frac{11}{16} , respectively. Sample Input 3 6 9 Sample Output 3 500000004 500000004 500000004 500000004 500000004 500000004 929687507 218750002 224609377 303710940 633300786 694091802 172485353 411682132 411682132
35,975
Score : 500 points Problem Statement Rng has a connected undirected graph with N vertices. Currently, there are M edges in the graph, and the i -th edge connects Vertices A_i and B_i . Rng will add new edges to the graph by repeating the following operation: Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly three edges from Vertex u , and add an edge connecting Vertices u and v . It is not allowed to add an edge if there is already an edge connecting Vertices u and v . Find the maximum possible number of edges that can be added. Constraints 2 \leq N \leq 10^5 1 \leq M \leq 10^5 1 \leq A_i,B_i \leq N The graph has no self-loops or multiple edges. The graph is connected. Input Input is given from Standard Input in the following format: N M A_1 B_1 A_2 B_2 : A_M B_M Output Find the maximum possible number of edges that can be added. Sample Input 1 6 5 1 2 2 3 3 4 4 5 5 6 Sample Output 1 4 If we add edges as shown below, four edges can be added, and no more. Sample Input 2 5 5 1 2 2 3 3 1 5 4 5 1 Sample Output 2 5 Five edges can be added, for example, as follows: Add an edge connecting Vertex 5 and Vertex 3 . Add an edge connecting Vertex 5 and Vertex 2 . Add an edge connecting Vertex 4 and Vertex 1 . Add an edge connecting Vertex 4 and Vertex 2 . Add an edge connecting Vertex 4 and Vertex 3 .
35,976
最長増加列問題 時は過ぎお倪郎君は高校生になりたした。 倧孊生だったお兄さんの圱響も受け、コンピュヌタヌサむ゚ンスに興味を持ち始めたした。 倪郎君はコンピュヌタヌサむ゚ンスの教科曞を読み進め、「最長増加郚分列問題」ずいう有名問題があるこずを知りたした。倪郎君はこの問題のこずを理解したしたが、自分でも類䌌問題が䜜れないものかず気になりたした。 そこで倪郎君は詊行錯誀の結果に次のような問題を䜜りたした。 n 個の敎数で構成される数列Aがある m-1 個の区切りを入れお、数列Aを m 個の数列に分解する。なお、分解埌のそれぞれの数列は1぀以䞊の数を必ず含たなければならない。 この m 個それぞれの数列内の敎数をすべお足し合わせた結果出来䞊がる m 個の数を、元の数列の順に配眮するず厳密な増加列になっおいる(぀たり、出来䞊がる数列は B i < B i+1 をみたす)ようにしたい。 目暙は最終的にできる数列 B の長さ m を最倧化するこずである。 䟋えば、数列 A が{5,-4,10,-3,8}の堎合を考えおみる。 区切りの䜍眮を衚す数列 C を甚意し、 C={2,4} ずする。 このずき、数列 A は(5,-4),(10,-3),(8)の郚分に分かれ、これらの内郚を足し合わせるずそれぞれ1,7,8ずなり、出来䞊がる数列 B は{1,7,8}ずなる。 倪郎君はこの問題に぀いお"最長増加列問題"ず名付け、これを解くアルゎリズムも考えたした。 そしお瀟䌚人になったあなたにチェックをしおほしいず連絡をしたした。 あなたの仕事は、成長した倪郎君の䜜った問題を解くプログラムを䜜るこずです。 チェックするのが仕事なので、 m-1 個の区切りの䜍眮も出力したす。 なお、最適な m に察しおこのような区切り方が耇数考えられる堎合もありたすが、この m が正しく出力されおいれば、考えられるもののうち䞀぀を出力すればよいです。 Input 改行区切りで n+1 個の敎数が䞎えられる。 n A 1 A 2 ... A n n は䞎えられる数列 A の長さを衚す A i は数列 A のi番目の芁玠を衚す。 Constraints 1≀n≀4000 |Ai|≀ 10 8 敎数 k に察しお |k| は k の絶察倀を衚す Output m C 1 C 2 .. C m-1 1行目は䞀぀の敎数 m を出力する。 m は最終的にできる数列 B の長さを衚す。 2行目は m-1 個の敎数 C i を空癜区切りで出力する。 C i は、数列 A を m 個の郚分に適切に区切った時の i 番目の区切り堎所を衚す。 区切り堎所の定矩は図を参照せよ。なお、 1≀C i <n を満たす。 2行目の m-1 個の敎数列Cは昇順に䞊んでいなければならないしたがっお、 i < j ならば C i < C j が成立する。 m=1 の堎合2行目は空行になる。 数列 C に埓っお数列 A から数列 B を生成したずき、数列 B が増加列になっおいないずき(すなわち B i ≥B i+1 ずなる i(1≀i<m) が存圚するずき)、WrongAnswerずなる Sample Input 1 3 1 2 4 Output for the Sample Input 1 3 1 2 もずもず増加列なので、すべおの堎所に区切りを入れればよい Sample Input 2 3 2 2 2 Output for the Sample Input 2 2 1 2 2 2は増加列でないので、3぀に分割するこずはできない Sample Input 3 3 4 2 1 Output for the Sample Input 3 1 (空行) どう分割しおも増加列にはならないため、分割をしない
35,977
Score : 100 points Problem Statement Determine if we can choose K different integers between 1 and N (inclusive) so that no two of them differ by 1 . Constraints 1\leq N,K\leq 100 N and K are integers. Input Input is given from Standard Input in the following format: N K Output If we can choose K integers as above, print YES ; otherwise, print NO . Sample Input 1 3 2 Sample Output 1 YES We can choose 1 and 3 . Sample Input 2 5 5 Sample Output 2 NO Sample Input 3 31 10 Sample Output 3 YES Sample Input 4 10 90 Sample Output 4 NO
35,978
Equilateral Triangular Fence Ms. Misumi owns an orchard along a straight road. Recently, wild boars have been witnessed strolling around the orchard aiming at pears, and she plans to construct a fence around many of the pear trees. The orchard contains n pear trees, whose locations are given by the two-dimensional Euclidean coordinates ( x 1 , y 1 ),..., ( x n , y n ). For simplicity, we neglect the thickness of pear trees. Ms. Misumi's aesthetic tells that the fence has to form a equilateral triangle with one of its edges parallel to the road. Its opposite apex, of course, should be apart from the road. The coordinate system for the positions of the pear trees is chosen so that the road is expressed as y = 0, and the pear trees are located at y ≥ 1. Due to budget constraints, Ms. Misumi decided to allow at most k trees to be left outside of the fence. You are to find the shortest possible perimeter of the fence on this condition. The following figure shows the first dataset of the Sample Input. There are four pear trees at (−1,2), (0,1), (1,2), and (2,1). By excluding (−1,2) from the fence, we obtain the equilateral triangle with perimeter 6. Input The input consists of multiple datasets, each in the following format. n k x 1 y 1 ... x n y n Each of the datasets consists of n +2 lines. n in the first line is the integer representing the number of pear trees; it satisfies 3 ≀ n ≀ 10 000. k in the second line is the integer representing the number of pear trees that may be left outside of the fence; it satisfies 1 ≀ k ≀ min( n −2, 5 000). The following n lines have two integers each representing the x- and y- coordinates, in this order, of the locations of pear trees; it satisfies −10 000 ≀ x i ≀ 10 000, 1 ≀ y i ≀ 10 000. No two pear trees are at the same location, i.e., ( x i , y i )=( x j , y j ) only if i = j . The end of the input is indicated by a line containing a zero. The number of datasets is at most 100. Output For each dataset, output a single number that represents the shortest possible perimeter of the fence. The output must not contain an error greater than 10 −6 . Sample Input 4 1 0 1 1 2 -1 2 2 1 4 1 1 1 2 2 1 3 1 4 4 1 1 1 2 2 3 1 4 1 4 1 1 2 2 1 3 2 4 2 5 2 0 1 0 2 0 3 0 4 0 5 6 3 0 2 2 2 1 1 0 3 2 3 1 4 0 Output for the Sample Input 6.000000000000 6.928203230276 6.000000000000 7.732050807569 6.928203230276 6.000000000000
35,979
Grading Write a program which reads a list of student test scores and evaluates the performance for each student. The test scores for a student include scores of the midterm examination m (out of 50), the final examination f (out of 50) and the makeup examination r (out of 100). If the student does not take the examination, the score is indicated by -1. The final performance of a student is evaluated by the following procedure: If the student does not take the midterm or final examination, the student's grade shall be F. If the total score of the midterm and final examination is greater than or equal to 80, the student's grade shall be A. If the total score of the midterm and final examination is greater than or equal to 65 and less than 80, the student's grade shall be B. If the total score of the midterm and final examination is greater than or equal to 50 and less than 65, the student's grade shall be C. If the total score of the midterm and final examination is greater than or equal to 30 and less than 50, the student's grade shall be D. However, if the score of the makeup examination is greater than or equal to 50, the grade shall be C. If the total score of the midterm and final examination is less than 30, the student's grade shall be F. Input The input consists of multiple datasets. For each dataset, three integers m , f and r are given in a line. The input ends with three -1 for m , f and r respectively. Your program should not process for the terminal symbols. The number of datasets (the number of students) does not exceed 50. Output For each dataset, print the grade ( A , B , C , D or F ) in a line. Sample Input 40 42 -1 20 30 -1 0 2 -1 -1 -1 -1 Sample Output A C F
35,980
A + B Problem Compute A + B. Input The input will consist of a series of pairs of integers A and B separated by a space, one pair of integers per line. The input will be terminated by EOF. Output For each pair of input integers A and B, you must output the sum of A and B in one line. Constraints -1000 ≀ A, B ≀ 1000 Sample Input 1 2 10 5 100 20 Output for the Sample Input 3 15 120 Sample Program #include<stdio.h> int main(){ int a, b; while( scanf("%d %d", &a, &b) != EOF ){ printf("%d\n", a + b); } return 0; }
35,981
E: LISum 問題 長さ $N$ の数列 $A$ が䞎えられる。 数列 $A$ の最長増加郚分列のひず぀を $B$ ずするずき、$\sum B_i$ の最倧倀を求めよ。 数列 $A$ の最長増加郚分列ずは、すべおの $i < j$ で $A_i < A_j$ を満たす郚分列のうち、最長なものを瀺す。 制玄 入力倀は党お敎数である。 $1 \leq N \leq 10^5$ $0 \leq A_i \leq 10^5$ 入力圢匏 入力は以䞋の圢匏で䞎えられる。 $N$ $A_1 \dots A_N$ 出力 数列 $A$ の最長増加郚分列のひず぀を $B$ ずするずき、$\sum B_i$ の最倧倀を出力せよ。たた、末尟に改行も出力せよ。 サンプル サンプル入力 1 4 6 4 7 8 サンプル出力 1 21 最長増加郚分列は $ (6, 7, 8)$ ず $(4, 7, 8)$ である。よっお最倧倀は $21$ である。 サンプル入力 2 3 1000 2 3 サンプル出力 2 5 最長増加郚分列は $(2,3)$ のみである。 サンプル入力 3 7 17 17 13 4 20 12 15 サンプル出力 3 31 サンプル入力 4 7 19 16 14 9 4 20 2 サンプル出力 4 39
35,982
Score : 300 points Problem Statement There are N Reversi pieces arranged in a row. (A Reversi piece is a disc with a black side and a white side.) The state of each piece is represented by a string S of length N . If S_i= B , the i -th piece from the left is showing black; If S_i= W , the i -th piece from the left is showing white. Consider performing the following operation: Choose i ( 1 \leq i < N ) such that the i -th piece from the left is showing black and the (i+1) -th piece from the left is showing white, then flip both of those pieces. That is, the i -th piece from the left is now showing white and the (i+1) -th piece from the left is now showing black. Find the maximum possible number of times this operation can be performed. Constraints 1 \leq |S| \leq 2\times 10^5 S_i= B or W Input Input is given from Standard Input in the following format: S Output Print the maximum possible number of times the operation can be performed. Sample Input 1 BBW Sample Output 1 2 The operation can be performed twice, as follows: Flip the second and third pieces from the left. Flip the first and second pieces from the left. Sample Input 2 BWBWBW Sample Output 2 6
35,983
問題 J Mod 3 Knights Out 問題文 ある日の倕方コンテストの問題を解き終えた二人は今日の問題に぀いお話し合っおいた A「くそヌあのラむツアりトの問題mod 2 じゃなくお mod 3 ずか mod 7 ずかだったら解法分かったのにヌ」 B「じゃあmod 3 で別の方法で解く問題を䜜ればいいんですね分かりたした」 こうしお次のような問題が誕生した H × W のチェス盀があるチェス盀の各マスには 0 から 2 の敎数が曞かれおいるこのチェス盀にナむトを眮いおいくただし各マスには倚くおも 1 䜓のナむトしか眮けない各マスに぀いお (マスの数倀+そこを攻撃するマスにいるナむトの数)=0 mod 3 が成り立぀ようなナむトの配眮を 良い 配眮ず呌ぶ攻撃するマスずはそのマスから瞊方向に ±2 マスか぀暪方向に ±1 マスもしくは瞊方向に ±1 マスか぀暪方向に ±2 マスずれたマスのこずである 良いナむトの配眮の数を求めよ答えは倧きくなる可胜性があるので 1,000,000,007 で割った䜙りを答えよ 入力圢匏 最初の行に H ず W がスペヌス区切りで䞎えられる 次の H 行には、チェス盀のマス目に曞かれおいる数倀ずしお W 個の 0  1  2 のいずれかの敎数がスペヌス区切りで䞎えられる 出力圢匏 良いナむトの配眮の数を 1,000,000,007 で割った䜙りを出力せよ 制玄 1 ≀ H ≀ 50 1 ≀ W ≀ 16 入出力䟋 入力䟋 1 5 5 0 2 0 2 0 2 0 0 0 2 0 0 0 0 0 2 0 0 0 2 0 2 0 2 0 出力䟋 1 5 入力䟋 2 3 3 2 2 2 2 0 2 2 2 2 出力䟋 2 8 入力䟋 3 7 7 2 2 2 2 2 2 2 2 1 1 2 1 1 2 2 0 1 0 1 0 2 2 2 0 2 0 2 2 2 0 1 0 1 0 2 2 1 1 2 1 1 2 2 2 2 2 2 2 2 出力䟋 3 96 入力䟋 4 7 3 2 2 2 1 0 1 0 1 0 1 1 1 0 1 0 1 0 1 2 2 2 出力䟋 4 8 入力䟋 5 6 6 0 2 0 1 0 2 2 0 1 2 2 2 0 2 2 0 0 0 2 0 2 0 2 0 0 2 2 1 0 2 0 0 0 2 0 2 出力䟋 5 1 入力䟋 6 16 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 出力䟋 6 1 謝蟞 この問題は Tester ず Writer が アゞア地区予遞の問題 に関しお話し合ったのをきっかけずしお䜜られた。
35,984
Score : 800 points Problem Statement There is a directed graph with N vertices and N edges. The vertices are numbered 1, 2, ..., N . The graph has the following N edges: (p_1, 1), (p_2, 2), ..., (p_N, N) , and the graph is weakly connected. Here, an edge from Vertex u to Vertex v is denoted by (u, v) , and a weakly connected graph is a graph which would be connected if each edge was bidirectional. We would like to assign a value to each of the vertices in this graph so that the following conditions are satisfied. Here, a_i is the value assigned to Vertex i . Each a_i is a non-negative integer. For each edge (i, j) , a_i \neq a_j holds. For each i and each integer x(0 ≀ x < a_i) , there exists a vertex j such that the edge (i, j) exists and x = a_j holds. Determine whether there exists such an assignment. Constraints 2 ≀ N ≀ 200 000 1 ≀ p_i ≀ N p_i \neq i The graph is weakly connected. Input Input is given from Standard Input in the following format: N p_1 p_2 ... p_N Output If the assignment is possible, print POSSIBLE ; otherwise, print IMPOSSIBLE . Sample Input 1 4 2 3 4 1 Sample Output 1 POSSIBLE The assignment is possible: { a_i } = { 0, 1, 0, 1 } or { a_i } = { 1, 0, 1, 0 }. Sample Input 2 3 2 3 1 Sample Output 2 IMPOSSIBLE Sample Input 3 4 2 3 1 1 Sample Output 3 POSSIBLE The assignment is possible: { a_i } = { 2, 0, 1, 0 }. Sample Input 4 6 4 5 6 5 6 4 Sample Output 4 IMPOSSIBLE
35,985
Score : 1100 points Problem Statement We have a grid with N rows and M columns of squares. Each integer from 1 to NM is written in this grid once. The number written in the square at the i -th row from the top and the j -th column from the left is A_{ij} . You need to rearrange these numbers as follows: First, for each of the N rows, rearrange the numbers written in it as you like. Second, for each of the M columns, rearrange the numbers written in it as you like. Finally, for each of the N rows, rearrange the numbers written in it as you like. After rearranging the numbers, you want the number written in the square at the i -th row from the top and the j -th column from the left to be M\times (i-1)+j . Construct one such way to rearrange the numbers. The constraints guarantee that it is always possible to achieve the objective. Constraints 1 \leq N,M \leq 100 1 \leq A_{ij} \leq NM A_{ij} are distinct. Input Input is given from Standard Input in the following format: N M A_{11} A_{12} ... A_{1M} : A_{N1} A_{N2} ... A_{NM} Output Print one way to rearrange the numbers in the following format: B_{11} B_{12} ... B_{1M} : B_{N1} B_{N2} ... B_{NM} C_{11} C_{12} ... C_{1M} : C_{N1} C_{N2} ... C_{NM} Here B_{ij} is the number written in the square at the i -th row from the top and the j -th column from the left after Step 1 , and C_{ij} is the number written in that square after Step 2 . Sample Input 1 3 2 2 6 4 3 1 5 Sample Output 1 2 6 4 3 5 1 2 1 4 3 5 6 Sample Input 2 3 4 1 4 7 10 2 5 8 11 3 6 9 12 Sample Output 2 1 4 7 10 5 8 11 2 9 12 3 6 1 4 3 2 5 8 7 6 9 12 11 10
35,986
問題 C : [[iwi]] そうだ僕のハンドルネヌムは (iwi) だ 僕は確かに昔にプログラミングコンテストに参加しおいた そしお倚くの仲間ず楜しい時間を過ごした 確か仲間たちは党員矎少女だったような気がする プログラミングコンテストの䞖界は僕のハヌレムだったような気がする G○○gle は僕のハヌレムを奪ったのだそうに違いない 昔の仲間の手がかりを぀かむためにもやはりプログラミングコンテストに出なければならない G○○gle Code Jam に参加登録するこずにしよう 今床の ID には䞞括匧以倖の括匧も怜蚎に入れおみよう 問題 'i', 'w', '(', ')', '{', '}', '[', ']' からなる文字列が䞎えられた時 その郚分列をずっお線察称な文字列を䜜りたい 最倧で䜕文字の文字列を䜜るこずができるかを蚈算するプログラムを䜜成せよ 䞎えられる文字列は "iwi" ずいう文字列を䞀床含みそれ以倖の郚分には 'i' ず 'w' を含たない より圢匏的には䞎えられる文字列は s "iwi" t  s ず "iwi" ず t を連結したものずいう圢で衚すこずができ s ず t は '(', ')', '{', '}', '[', ']' からなる文字列である s や t が 0 文字である可胜性もある 䜜る文字列は䞎えられる文字列の郚分列をずっお䜜る 郚分列ずは元の文字列からいく぀かの文字を取り出しそれらを 元の文字列に含たれる順番で繋げたものである 取り出す文字たちは必ずしも元の文字列で連続しおいなくおも良い たた䜜る文字列も䞎えられる文字列ず同様に"iwi" ずいう文字列を䞀床含み それ以倖の郚分には 'i' ず 'w' は含たないようにしたい ここで甚いる巊右に線察称の定矩は以䞋ずする 以䞋の文字列は巊右に線察称 空文字列 "i" "w" 文字列 x が巊右に線察称のずき以䞋の文字列も巊右に線察称 "i" x "i" "w" x "w" "(" x ")" ")" x "(" "{" x "}" "}" x "{" "[" x "]" "]" x "[" 以䞊のもののみが巊右に線察称 入力 入力は 'i', 'w', '(', ')', '{', '}', '[', ']' からなり䞊蚘の条件を満たす文字列である 出力 䞊蚘の条件を満たし䜜るこずのできる文字列の長さの最倧倀を出力せよ 制玄 入力の文字列の長さは 15 以䞋である 入出力䟋 入出力䟋 1 入力䟋 1: [[[iwi[[[ 入力䟋 1 に察する出力䟋: 3 "iwi" ずいう文字列しか䜜るこずができない 入出力䟋 2 入力䟋 2: [{)iwi(]} 入力䟋 2 に察する出力䟋: 7 "[)iwi(]" や "{)iwi(}" など 7 文字の文字列を䜜るこずができる
35,987
Score : 300 points Problem Statement Takahashi, Nakahashi and Hikuhashi have integers A , B and C , respectively. After repeating the following operation K times, find the integer Takahashi will get minus the integer Nakahashi will get: Each of them simultaneously calculate the sum of the integers that the other two people have, then replace his own integer with the result. However, if the absolute value of the answer exceeds 10^{18} , print Unfair instead. Constraints 1 \leq A,B,C \leq 10^9 0 \leq K \leq 10^{18} All values in input are integers. Input Input is given from Standard Input in the following format: A B C K Output Print the integer Takahashi will get minus the integer Nakahashi will get, after repeating the following operation K times. If the absolute value of the answer exceeds 10^{18} , print Unfair instead. Sample Input 1 1 2 3 1 Sample Output 1 1 After one operation, Takahashi, Nakahashi and Hikuhashi have 5 , 4 and 3 , respectively. We should print 5-4=1 . Sample Input 2 2 3 2 0 Sample Output 2 -1 Sample Input 3 1000000000 1000000000 1000000000 1000000000000000000 Sample Output 3 0
35,988
Problem Statement Dr. Suposupo developed a programming language called Shipura. Shipura supports only one binary operator ${\tt >>}$ and only one unary function ${\tt S<\ >}$. $x {\tt >>} y$ is evaluated to $\lfloor x / 2^y \rfloor$ (that is, the greatest integer not exceeding $x / 2^y$), and ${\tt S<} x {\tt >}$ is evaluated to $x^2 \bmod 1{,}000{,}000{,}007$ (that is, the remainder when $x^2$ is divided by $1{,}000{,}000{,}007$). The operator ${\tt >>}$ is left-associative. For example, the expression $x {\tt >>} y {\tt >>} z$ is interpreted as $(x {\tt >>} y) {\tt >>} z$, not as $x {\tt >>} (y {\tt >>} z)$. Note that these parentheses do not appear in actual Shipura expressions. The syntax of Shipura is given (in BNF; Backus-Naur Form) as follows: expr ::= term | expr sp ">>" sp term term ::= number | "S" sp "<" sp expr sp ">" sp ::= "" | sp " " number ::= digit | number digit digit ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" The start symbol of this syntax is $\tt expr$ that represents an expression in Shipura. In addition, $\tt number$ is an integer between $0$ and $1{,}000{,}000{,}000$ inclusive, written without extra leading zeros. Write a program to evaluate Shipura expressions. Input The input is a sequence of datasets. Each dataset is represented by a line which contains a valid expression in Shipura. A line containing a single ${\tt \#}$ indicates the end of the input. You can assume the number of datasets is at most $100$ and the total size of the input file does not exceed $2{,}000{,}000$ bytes. Output For each dataset, output a line containing the evaluated value of the expression. Sample Input S< S< 12 >> 2 > > 123 >> 1 >> 1 1000000000 >>129 S<S<S<S<S<2>>>>> S <S< S<2013 >>> 11 >>> 10 > # Output for the Sample Input 81 30 0 294967268 14592400
35,989
Problem A : ID A倧孊ではIDの入力ミスが倚発しおいた。 そこで、A倧孊は入力ミス防止のため新しいIDを発行するこずにした。 新しいIDには入力ミス防止のためにIDが正しいかどうかチェックする方法がある。 ・党おの桁の数字の総和を求める。 ・ただし、右端の桁を番目ずしお、偶数番目の桁の数字を倍にする。 ・倍するこずによっお数字が以䞊になった時、の䜍の数字ずの䜍の数字を加算した数字をその桁の数字ずする。 ・総和がで割り切れれば正しいID、そうでなければ間違いずする。 䟋ずしお、53579ずいうIDをチェックする。 党おの桁の数字の総和を求めるので、 5 + 3 + 5 + 7 + 9 ただし、偶数番目の桁の数字を倍にするので、 5 + 6 + 5 + 14 + 9 倍するこずによっお数字が以䞊になった時、の䜍の数字ずの䜍の数字を加算した数字をその桁の数字ずするので、 5 + 6 + 5 + (1 + 4) + 9 以䞊より、総和は30ずなる。30は10で割り切れるので、53579は正しいIDである。 B君はA倧孊の倧孊生であり、新しいIDを発行しおもらったが、IDの䞀郚の桁を忘れおしたった。 しかし、忘れおしたった郚分にどんな数字が入るか、いく぀か候補を絞るこずに成功した。 あなたの仕事は、B君のIDの正しい組み合わせが䜕通りあるかを求めるこずである。 Input 入力は以䞋のフォヌマットで䞎えられる。 n ID m a 0 a 1 ... a m-1 n は ID の桁の数である。 ID の各桁には0~9の数字たたは忘れた桁であるずいうこずを瀺す'*'ずいう文字が入る。 m は'*'に入る数字の候補の数である。 a i は'*'に入る数字の候補である。 入力は以䞋の制玄を満たす 1 ≀ n ≀ 100,000 1 ≀ m ≀ 10 0 ≀ a i ≀ 9 1 ≀ '*'の数 ≀ 7 Output 答えの倀を行に出力せよ Sample Input 1 5 5*57* 2 3 9 Sample Output 1 1 Sample Input 2 15 2***9*2*6*1199* 9 0 1 2 3 4 6 7 8 9 Sample Output 2 478297
35,990
ヘリリンは二次元平面䞊における長さ 2L の線分の圢状をしおいる。 ヘリリンのたわりには、線分の圢状をしたいく぀かの障害物が存圚しおいる。 ヘリリンは障害物に接するず䜓力が削られおしたう。 完璧䞻矩のヘリリンは無傷でゎヌルするこずにした。 ヘリリンは以䞋の行動ができる。 平行移動 ヘリリンを衚す線分の䞭点を䞭心ずしお、反時蚈呚りにちょうど 180 / r 床だけ回転する ただし、二次元平面は䞊方向に y 軞をずる。 ヘリリンのたわりに、2 点 S, G がある。 始めはヘリリンの䞭心は点 S にあっお、 x 軞に平行な状態になっおいる。 ヘリリンは、平行移動するのは埗意だが、回転するのは䞍埗意である。 あなたの仕事は、ヘリリンが䞭心を点 S から点 G たで移動させるたでに必芁な、最小の回転行動の回数を求めるこずである。 移動させるこずができない堎合は、そのこずも怜出せよ。 ただし、以䞋のこずに泚意せよ。 ヘリリンは移動しながら回転するこずはできない。 ヘリリンが回転する途䞭で障害物にぶ぀かりうる堎合は、回転するこずはできない。 障害物が互いに亀差しおいるこずはあり埗る。 線分は十分小さい有限の倪さを持぀ものずしお扱う。最埌のサンプルを芋よ。 Input 入力は以䞋の圢匏で䞎えられる。 L r s x s y g x g y n x 11 y 11 x 12 y 12 ... x n1 y n1 x n2 y n2 L はヘリリンの半分の長さを衚す。 r は回転角床を定めるものである。 (s x , s y ) は点 S、 (g x , g y ) は点 G の座暙である。 n は障害物の数を衚す。 (x i1 , y i1 ) ず (x i2 , y i2 ) は i 番目の障害物を衚す線分の端点である。 Constraints 入力は以䞋の制玄を満たす。 1 ≀ n ≀ 30 2≀ r ≀ 11 1 ≀ L ≀ 10 5 入力に含たれる座暙の各成分は絶察倀が 10 5 以䞋 入力に含たれる数倀はすべお敎数 i = 1, . . . , n に぀いお (x i1 , y i1 ) ≠ (x i2 , y i2 ) ヘリリンをスタヌト地点にx軞に氎平な状態で配眮したずき、障害物ずの線分ず線分ずの距離は 10 −3 より倧きい 障害物を衚す線分の端点を、䞡方向に 10 −3 だけ延ばしおも瞮めおも解は倉わらない L を 10 −3 だけ増枛させおも解は倉わらない 障害物の線分を l i ず曞くこずにするず、 1 ≀ i ≀ j ≀ n であっお、 l i ず l j の距離が 2L 以䞋であるような組 (i, j) は高々100個 ゎヌル地点は障害物に乗っおいるこずはない Output スタヌト地点からゎヌル地点たで移動するために必芁な最小の回転行動の回数を1行に出力せよ。 移動させるこずができない堎合は、-1を1行に出力せよ。 Sample Input 1 1 2 3 3 2 -1 4 1 0 1 5 0 1 4 1 0 4 6 4 5 0 5 5 Output for the Sample Input 1 1 ヘリリンを90床回転させるこずで、隙間を抜けるこずができる。 Sample Input 2 1 2 3 3 2 -1 4 1 0 1 5 0 1 6 1 0 4 6 4 5 0 5 5 Output for the Sample Input 2 -1 ヘリリンは完党に囲たれおいるので、ゎヌルたで移動するこずができない。 Sample Input 3 1 4 3 3 7 0 5 1 0 1 5 0 1 6 1 0 4 6 4 8 0 2 5 6 0 4 2 Output for the Sample Input 3 3 斜めの経路を通るためには、3回反時蚈呚りに回転しなければならない。 Sample Input 4 2 2 4 2 4 5 5 1 5 2 0 0 4 3 4 0 1 8 1 7 0 7 5 8 4 5 4 Output for the Sample Input 4 -1 ヘリリンは障害物にぎったり接するこずができないので、隙間のずころで回転しおゎヌルたで移動するこずはできない。
35,991
Score : 400 points Problem Statement There is a bar of chocolate with a height of H blocks and a width of W blocks. Snuke is dividing this bar into exactly three pieces. He can only cut the bar along borders of blocks, and the shape of each piece must be a rectangle. Snuke is trying to divide the bar as evenly as possible. More specifically, he is trying to minimize S_{max} - S_{min} , where S_{max} is the area (the number of blocks contained) of the largest piece, and S_{min} is the area of the smallest piece. Find the minimum possible value of S_{max} - S_{min} . Constraints 2 ≀ H, W ≀ 10^5 Input Input is given from Standard Input in the following format: H W Output Print the minimum possible value of S_{max} - S_{min} . Sample Input 1 3 5 Sample Output 1 0 In the division below, S_{max} - S_{min} = 5 - 5 = 0 . Sample Input 2 4 5 Sample Output 2 2 In the division below, S_{max} - S_{min} = 8 - 6 = 2 . Sample Input 3 5 5 Sample Output 3 4 In the division below, S_{max} - S_{min} = 10 - 6 = 4 . Sample Input 4 100000 2 Sample Output 4 1 Sample Input 5 100000 100000 Sample Output 5 50000
35,992
Score : 600 points Problem Statement There are N slimes standing on a number line. The i -th slime from the left is at position x_i . It is guaruanteed that 1 \leq x_1 < x_2 < \ldots < x_N \leq 10^{9} . Niwango will perform N-1 operations. The i -th operation consists of the following procedures: Choose an integer k between 1 and N-i (inclusive) with equal probability. Move the k -th slime from the left, to the position of the neighboring slime to the right. Fuse the two slimes at the same position into one slime. Find the total distance traveled by the slimes multiplied by (N-1)! (we can show that this value is an integer), modulo (10^{9}+7) . If a slime is born by a fuse and that slime moves, we count it as just one slime. Constraints 2 \leq N \leq 10^{5} 1 \leq x_1 < x_2 < \ldots < x_N \leq 10^{9} x_i is an integer. Subtasks 400 points will be awarded for passing the test cases satisfying N \leq 2000 . Input Input is given from Standard Input in the following format: N x_1 x_2 \ldots x_N Output Print the answer. Sample Input 1 3 1 2 3 Sample Output 1 5 With probability \frac{1}{2} , the leftmost slime is chosen in the first operation, in which case the total distance traveled is 2 . With probability \frac{1}{2} , the middle slime is chosen in the first operation, in which case the total distance traveled is 3 . The answer is the expected total distance traveled, 2.5 , multiplied by 2! , which is 5 . Sample Input 2 12 161735902 211047202 430302156 450968417 628894325 707723857 731963982 822804784 880895728 923078537 971407775 982631932 Sample Output 2 750927044 Find the expected value multiplied by (N-1)! , modulo (10^9+7) .
35,993
Score : 300 points Problem Statement How many integer sequences A_1,A_2,\ldots,A_N of length N satisfy all of the following conditions? 0 \leq A_i \leq 9 There exists some i such that A_i=0 holds. There exists some i such that A_i=9 holds. The answer can be very large, so output it modulo 10^9 + 7 . Constraints 1 \leq N \leq 10^6 N is an integer. Input Input is given from Standard Input in the following format: N Output Print the answer modulo 10^9 + 7 . Sample Input 1 2 Sample Output 1 2 Two sequences \{0,9\} and \{9,0\} satisfy all conditions. Sample Input 2 1 Sample Output 2 0 Sample Input 3 869121 Sample Output 3 2511445
35,994
L番目のK番目の数 (LthKthNumber) 問題文 暪䞀列に䞊べられた N 枚のカヌドがある巊から i 枚目( 1 ≩ i ≩ N )のカヌドには敎数 a_i が曞かれおいる JOI 君はこれらのカヌドを甚いお次のようなゲヌムを行う連続する K 枚以䞊のカヌドの列を遞び次の操䜜を行う 遞んだカヌドを曞かれおいる敎数が小さい順に巊から䞊べる 䞊べたカヌドのうち巊から K 番目のカヌドに曞かれた敎数を玙に曞き出す 遞んだカヌドをすべお元の䜍眮に戻す この操䜜を連続する K 枚以䞊のカヌドの列すべおに察しお行うすなわち 1 ≩ l ≩ r ≩ N か぀ K ≩ r - l + 1 を満たすすべおの (l,r) に぀いお a_l, a_{l+1}, ..., a_r のうち K 番目に小さな敎数を曞き出す こうしお曞き出された敎数を巊から小さい順に䞊べる䞊べた敎数のうち巊から L 番目のものがこのゲヌムにおける JOI 君の埗点であるJOI 君の埗点を求めよ 制玄 1 \leq N \leq 200000 1 \leq K \leq N 1 \leq a_i \leq N 1 \leq L JOI 君が曞き出す敎数は L 個以䞊である 入力・出力 入力 入力は以䞋の圢匏で暙準入力から䞎えられる N K L a_1 a_2 ... a_N 出力 JOI 君の埗点を 1 行で出力せよ 入出力䟋 入力䟋 1 4 3 2 4 3 1 2 出力䟋 1 3 1 \leq l \leq r \leq N (= 4) か぀ K (= 3) \leq r - l + 1 を満たす (l,r) は (1,3), (1,4), (2,4) の 3 通りある これらの (l,r) に察し a_l, a_{l+1}, ..., a_r で 3 番目に小さな敎数はそれぞれ 4, 3, 3 である このうち L (= 2) 番目に小さい敎数は 3 なのでJOI 君の埗点は 3 である同じ敎数が耇数あるずきも重耇しお数えるこずに泚意せよ 入力䟋 2 5 3 3 1 5 2 2 4 出力䟋 2 4 JOI 君が曞き出す敎数は (l,r) = (1,3) に察し 5 (l,r) = (1,4) に察し 2 (l,r) = (1,5) に察し 2 (l,r) = (2,4) に察し 5 (l,r) = (2,5) に察し 4 (l,r) = (3,5) に察し 4 であるこのうち L (= 3) 番目に小さい敎数は 4 である 入力䟋 3 6 2 9 1 5 3 4 2 4 出力䟋 3 4 入力䟋 4 6 2 8 1 5 3 4 2 4 出力䟋 4 3
35,995
うるう幎 西暊 a 幎から b 幎たでの間にあるすべおのうるう幎を出力するプログラムを䜜成しおください。 うるう幎の条件は、次のずおりずしたす。ただし、0 < a ≀ b < 3,000 ずしたす。䞎えられた期間にうるう幎がない堎合には "NA"ず出力しおください。 西暊幎が 4 で割り切れる幎であるこず。 ただし、100 で割り切れる幎はうるう幎ずしない。 しかし、400 で割り切れる幎はうるう幎である。 Input 耇数のデヌタセットが䞎えられたす。各デヌタセットの圢匏は以䞋のずおりです a b a , b がずもに 0 のずき入力の終了ずしたす。デヌタセットの数は 50 を超えたせん。 Output デヌタセットごずに、西暊たたは NA を出力しおください。 デヌタセットの間に぀の空行を入れおください。 Sample Input 2001 2010 2005 2005 2001 2010 0 0 Output for the Sample Input 2004 2008 NA 2004 2008
35,996
Problem G: Chairs Problem 1から N の番号が割り圓おられた N 脚の怅子に、1から N のIDが割り圓おられた N 人の人が座ろうずしおいる。IDが i の人は怅子 p i に座りたいず思っおいる。 N 人の人はIDが小さい順に1列に䞊び、列の先頭の人が以䞋の行動をずる。 怅子 p i に誰も座っおいなければ、その怅子に座る。 そうでなければ、 p i に1を加算し、列の最埌尟に䞊び盎す。ただし、 p i が N を超えた堎合は p i を1にする。 党おの人が怅子に座るたでこの行動が繰り返されたずき、最終的に各怅子に座っおいる人のIDを出力せよ。 Input 入力は以䞋の圢匏で䞎えられる。 N p 1 p 2 ... p N 1行目に敎数 N が䞎えられる。 2行目に N 個の敎数 p 1 , p 2 , ..., p N が空癜区切りで䞎えられる。 Constraints 入力は以䞋の条件を満たす。 1 ≀ N ≀ 10 5 1 ≀ p i ≀ N Output N 行に最終的な状態を出力する。 i 行目に怅子 i に座っおいる人のIDを出力する。 Sample Input 1 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Sample Input 2 5 3 3 4 4 5 Sample Output 2 4 2 1 3 5
35,997
ヘビ ある䞖界には文字だけでできた䞍思議なヘビが䜏んでいたす。このヘビには珟圚A皮ずB皮の2皮類が確認されおいたすが、それ以倖の皮類がいる可胜性もありたす。 A皮は">'"の埌に"="が1個以䞊䞊んだ埌、"#"が来お、さらに前ず同じ個数の"="が来た埌、"~"半角チルダで終わりたす。 B皮は">^"の埌に "Q="が1個以䞊䞊んだ埌、"~~"で終わりたす。 A皮の䟋 >'====#====~ >'==#==~ B皮の䟋 >^Q=Q=Q=Q=~~ >^Q=Q=~~ ヘビを文字列デヌタずしお受け取り、それがどんな皮類であるかを刀別しお、A皮の堎合は「A」、B皮の堎合は「B」、それ以倖の皮類の堎合は「NA」を出力するプログラムを䜜成しおください。 Input 入力は以䞋の圢匏で䞎えられたす。 n S 1 S 2 : S n 1 行目に刀別されるヘビの数 n 1 ≀ n ≀ 10000、続く n 行に i 匹目のヘビを衚す文字列 S i (200文字以䞋の、空癜を含たない文字列) がそれぞれ行に䞎えられたす。 Output i 行目に i 匹目のヘビの皮類 A、B たたは NA を出力しおください。 Sample Input 3 >'======#======~ >^Q=Q=Q=Q=Q=Q=Q=Q=Q=~~ >'===#====~ Output for the Sample Input A B NA
35,998
Score : 400 points Problem Statement You are given a permutation p of the set { 1, 2, ..., N }. Please construct two sequences of positive integers a_1 , a_2 , ..., a_N and b_1 , b_2 , ..., b_N satisfying the following conditions: 1 \leq a_i, b_i \leq 10^9 for all i a_1 < a_2 < ... < a_N b_1 > b_2 > ... > b_N a_{p_1}+b_{p_1} < a_{p_2}+b_{p_2} < ... < a_{p_N}+b_{p_N} Constraints 2 \leq N \leq 20,000 p is a permutation of the set { 1, 2, ..., N } Input The input is given from Standard Input in the following format: N p_1 p_2 ... p_N Output The output consists of two lines. The first line contains a_1 , a_2 , ..., a_N seperated by a space. The second line contains b_1 , b_2 , ..., b_N seperated by a space. It can be shown that there always exists a solution for any input satisfying the constraints. Sample Input 1 2 1 2 Sample Output 1 1 4 5 4 a_1 + b_1 = 6 and a_2 + b_2 = 8 . So this output satisfies all conditions. Sample Input 2 3 3 2 1 Sample Output 2 1 2 3 5 3 1 Sample Input 3 3 2 3 1 Sample Output 3 5 10 100 100 10 1
35,999
A: トヌナメント 問題 AOR むカちゃんずあなたは、トヌナメント圢匏の卓球倧䌚シングルスの郚に偵察に来た。 党おの詊合を録画したい AOR むカちゃんのために、あなたはこの倧䌚で行われる詊合数を求めおあげるこずにした。 この倧䌚には $N$ 人の遞手が参加しおおり、それぞれ $0, \dots , N - 1$ の背番号を持っおいる。 そのうち、 $M$ 人の遞手が棄暩し、詊合には出堎しなかった。 この倧䌚の詊合数は、以䞋のルヌルに基いお決定される。 シヌド遞手は存圚せず、任意の出堎者が優勝するために必芁な勝利回数は䞀定である。 察戊盞手が䞍圚の堎合は詊合を行わず、出堎した遞手が勝利する。詊合数にはカりントされない。 優勝者が決たった段階で倧䌚は終了する。 䞀床詊合に負けた人が再び詊合をするこずはない。぀たり敗者埩掻戊や 3 䜍決定戊などは行わない。 なお、卓球台が 1 台しか無いため異なる詊合が同時に行われるこずはなく、たた各詊合では必ず勝者が決たる (匕き分けずなるこずはない)。 なお、トヌナメントの定矩は次のずおりである。 トヌナメントは高さ $L = \log_2 N$ の完党二分朚で衚され、葉にあたる各頂点には参加者の背番号が曞き蟌たれおいる。 根の深さを 0 ずするず、 $i$ 回戊 ($1 \le i \le L$) では、 深さ $L - i$ の各頂点の子に曞かれた番号の遞手同士が詊合を行い、その勝者の背番号をその頂点に曞き蟌む。 制玄 $2 \le N \le 2^8$ $0 \le M \le N - 1$ $0 \le a_i \le N - 1 \ (0 \le i \le M - 1)$ $N$ は 2 の环乗である。 $a_i \ (0 \le i \le M - 1)$ は盞異なる 入力圢匏 入力は以䞋の圢匏で䞎えられる。 $N \ M$ $a_1$ $\vdots$ $a_M$ 1 行目には、参加者を衚す敎数 $N$ 、参加者のうち棄暩した者の人数を衚す敎数 $M$ が䞎えられる。 続く $M$ 行には、棄暩しお出堎しなかった遞手の番号を衚す敎数 $a_i$ が䞎えられる。 出力 この倧䌚が終了するたでに行われる詊合数を 1 行で出力せよ。たた、末尟に改行も出力せよ。 サンプル サンプル入力 1 2 0 サンプル出力 1 1 最初の詊合で優勝者が決たる。 サンプル入力 2 4 2 2 3 サンプル出力 2 1
36,000