task
stringlengths
0
154k
__index_level_0__
int64
0
39.2k
J: Horizontal-Vertical Permutation Problem Statement You are given a positive integer N . Your task is to determine if there exists a square matrix A whose dimension is N that satisfies the following conditions and provide an example of such matrices if it exists. A_{i, j} denotes the element of matrix A at the i -th row and j -th column. For all i, j (1 \leq i, j \leq N) , A_{i, j} is an integer that satisfies 1 \leq A_{i, j} \leq 2N - 1 . For all k = 1, 2, ..., N , a set consists of 2N - 1 elements from the k -th row or k -th column is \{1, 2, ..., 2N - 1\} . If there are more than one possible matrices, output any of them. Input N Input consists of one line, which contains the integer N that is the size of a square matrix to construct. Constraint N is an integer that satisfies 1 \leq N \leq 500 . Output Output No in a single line if such a square matrix does not exist. If such a square matrix A exists, output Yes on the first line and A after that. More specifically, follow the following format. Yes A_{1, 1} A_{1, 2} ... A_{1, N} A_{2, 1} A_{2, 2} ... A_{2, N} : A_{N, 1} A_{N, 2} ... A_{N, N} Sample Input 1 4 Output for Sample Input 1 Yes 2 6 3 7 4 5 2 1 1 7 5 6 5 3 4 2 Sample Input 2 3 Output for Sample Input 2 No
38,601
Sale Result There is data on sales of your company. Your task is to write a program which identifies good workers. The program should read a list of data where each item includes the employee ID i , the amount of sales q and the corresponding unit price p . Then, the program should print IDs of employees whose total sales proceeds (i.e. sum of p × q) is greater than or equal to 1,000,000 in the order of inputting. If there is no such employees, the program should print "NA". You can suppose that n < 4000, and each employee has an unique ID. The unit price p is less than or equal to 1,000,000 and the amount of sales q is less than or equal to 100,000. Input The input consists of several datasets. The input ends with a line including a single 0. Each dataset consists of: n (the number of data in the list) i p q i p q : : i p q Output For each dataset, print a list of employee IDs or a text "NA" Sample Input 4 1001 2000 520 1002 1800 450 1003 1600 625 1001 200 1220 2 1001 100 3 1005 1000 100 2 2013 5000 100 2013 5000 100 0 Output for the Sample Input 1001 1003 NA 2013
38,602
Problem C: Save your cats Nicholas Y. Alford was a cat lover. He had a garden in a village and kept many cats in his garden. The cats were so cute that people in the village also loved them. One day, an evil witch visited the village. She envied the cats for being loved by everyone. She drove magical piles in his garden and enclosed the cats with magical fences running between the piles. She said “Your cats are shut away in the fences until they become ugly old cats.” like a curse and went away. Nicholas tried to break the fences with a hummer, but the fences are impregnable against his effort. He went to a church and asked a priest help. The priest looked for how to destroy the magical fences in books and found they could be destroyed by holy water. The Required amount of the holy water to destroy a fence was proportional to the length of the fence. The holy water was, however, fairly expensive. So he decided to buy exactly the minimum amount of the holy water required to save all his cats. How much holy water would be required? Input The input has the following format: N M x 1 y 1 . . . x N y N p 1 q 1 . . . p M q M The first line of the input contains two integers N (2 ≀ N ≀ 10000) and M (1 ≀ M ). N indicates the number of magical piles and M indicates the number of magical fences. The following N lines describe the coordinates of the piles. Each line contains two integers x i and y i (-10000 ≀ x i , y i ≀ 10000). The following M lines describe the both ends of the fences. Each line contains two integers p j and q j (1 ≀ p j , q j ≀ N ). It indicates a fence runs between the p j -th pile and the q j -th pile. You can assume the following: No Piles have the same coordinates. A pile doesn’t lie on the middle of fence. No Fences cross each other. There is at least one cat in each enclosed area. It is impossible to destroy a fence partially. A unit of holy water is required to destroy a unit length of magical fence. Output Output a line containing the minimum amount of the holy water required to save all his cats. Your program may output an arbitrary number of digits after the decimal point. However, the absolute error should be 0.001 or less. Sample Input 1 3 3 0 0 3 0 0 4 1 2 2 3 3 1 Output for the Sample Input 1 3.000 Sample Input 2 4 3 0 0 -100 0 100 0 0 100 1 2 1 3 1 4 Output for the Sample Input 2 0.000 Sample Input 3 6 7 2 0 6 0 8 2 6 3 0 5 1 7 1 2 2 3 3 4 4 1 5 1 5 4 5 6 Output for the Sample Input 3 7.236 Sample Input 4 6 6 0 0 0 1 1 0 30 0 0 40 30 40 1 2 2 3 3 1 4 5 5 6 6 4 Output for the Sample Input 4 31.000
38,603
Score : 1000 points Problem Statement There are M chairs arranged in a line. The coordinate of the i -th chair (1 ≀ i ≀ M) is i . N people of the Takahashi clan played too much games, and they are all suffering from backaches. They need to sit in chairs and rest, but they are particular about which chairs they sit in. Specifically, the i -th person wishes to sit in a chair whose coordinate is not greater than L_i , or not less than R_i . Naturally, only one person can sit in the same chair. It may not be possible for all of them to sit in their favorite chairs, if nothing is done. Aoki, who cares for the health of the people of the Takahashi clan, decides to provide additional chairs so that all of them can sit in chairs at their favorite positions. Additional chairs can be placed at arbitrary real coordinates. Find the minimum required number of additional chairs. Constraints 1 ≀ N,M ≀ 2 × 10^5 0 ≀ L_i < R_i ≀ M + 1(1 ≀ i ≀ N) All input values are integers. Input Input is given from Standard Input in the following format: N M L_1 R_1 : L_N R_N Output Print the minimum required number of additional chairs. Sample Input 1 4 4 0 3 2 3 1 3 3 4 Sample Output 1 0 The four people can sit in chairs at the coordinates 3 , 2 , 1 and 4 , respectively, and no more chair is needed. Sample Input 2 7 6 0 7 1 5 3 6 2 7 1 6 2 6 3 7 Sample Output 2 2 If we place additional chairs at the coordinates 0 and 2.5 , the seven people can sit at coordinates 0 , 5 , 3 , 2 , 6 , 1 and 2.5 , respectively. Sample Input 3 3 1 1 2 1 2 1 2 Sample Output 3 2 Sample Input 4 6 6 1 6 1 6 1 5 1 5 2 6 2 6 Sample Output 4 2
38,604
Score : 200 points Problem Statement You have N apples, called Apple 1 , Apple 2 , Apple 3 , ..., Apple N . The flavor of Apple i is L+i-1 , which can be negative. You can make an apple pie using one or more of the apples. The flavor of the apple pie will be the sum of the flavors of the apples used. You planned to make an apple pie using all of the apples, but being hungry tempts you to eat one of them, which can no longer be used to make the apple pie. You want to make an apple pie that is as similar as possible to the one that you planned to make. Thus, you will choose the apple to eat so that the flavor of the apple pie made of the remaining N-1 apples will have the smallest possible absolute difference from the flavor of the apple pie made of all the N apples. Find the flavor of the apple pie made of the remaining N-1 apples when you choose the apple to eat as above. We can prove that this value is uniquely determined. Constraints 2 \leq N \leq 200 -100 \leq L \leq 100 All values in input are integers. Input Input is given from Standard Input in the following format: N L Output Find the flavor of the apple pie made of the remaining N-1 apples when you optimally choose the apple to eat. Sample Input 1 5 2 Sample Output 1 18 The flavors of Apple 1 , 2 , 3 , 4 , and 5 are 2 , 3 , 4 , 5 , and 6 , respectively. The optimal choice is to eat Apple 1 , so the answer is 3+4+5+6=18 . Sample Input 2 3 -1 Sample Output 2 0 The flavors of Apple 1 , 2 , and 3 are -1 , 0 , and 1 , respectively. The optimal choice is to eat Apple 2 , so the answer is (-1)+1=0 . Sample Input 3 30 -50 Sample Output 3 -1044
38,605
Problem Statement Let's consider operations on monochrome images that consist of hexagonal pixels, each of which is colored in either black or white. Because of the shape of pixels, each of them has exactly six neighbors (e.g. pixels that share an edge with it.) " Filtering " is an operation to determine the color of a pixel from the colors of itself and its six neighbors. Examples of filterings are shown below. Example 1: Color a pixel in white when all of its neighboring pixels are white. Otherwise the color will not change. Performing this operation on all the pixels simultaneously results in " noise canceling, " which removes isolated black pixels. Example 2: Color a pixel in white when its all neighboring pixels are black. Otherwise the color will not change. Performing this operation on all the pixels simultaneously results in " edge detection, " which leaves only the edges of filled areas. Example 3: Color a pixel with the color of the pixel just below it, ignoring any other neighbors. Performing this operation on all the pixels simultaneously results in " shifting up " the whole image by one pixel. Applying some filter, such as " noise canceling " and " edge detection, " twice to any image yields the exactly same result as if they were applied only once. We call such filters idempotent . The " shifting up " filter is not idempotent since every repeated application shifts the image up by one pixel. Your task is to determine whether the given filter is idempotent or not. Input The input consists of multiple datasets. The number of dataset is less than $100$. Each dataset is a string representing a filter and has the following format (without spaces between digits). $c_0c_1\cdots{}c_{127}$ $c_i$ is either ' 0 ' (represents black) or ' 1 ' (represents white), which indicates the output of the filter for a pixel when the binary representation of the pixel and its neighboring six pixels is $i$. The mapping from the pixels to the bits is as following: and the binary representation $i$ is defined as $i = \sum_{j=0}^6{\mathit{bit}_j \times 2^j}$, where $\mathit{bit}_j$ is $0$ or $1$ if the corresponding pixel is in black or white, respectively. Note that the filter is applied on the center pixel, denoted as bit 3. The input ends with a line that contains only a single " # ". Output For each dataset, print " yes " in a line if the given filter is idempotent, or " no " otherwise (quotes are for clarity). Sample Input 00000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000111111111 10000000111111110000000011111111000000001111111100000000111111110000000011111111000000001111111100000000111111110000000011111111 01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101 # Output for the Sample Input yes yes no
38,606
Problem A: Starship Hakodate-maru The surveyor starship Hakodate-maru is famous for her two fuel containers with unbounded capacities. They hold the same type of atomic fuel balls. There, however, is an inconvenience. The shapes of the fuel containers # 1 and # 2 are always cubic and regular tetrahedral respectively. Both of the fuel containers should be either empty or filled according to their shapes. Otherwise, the fuel balls become extremely unstable and may explode in the fuel containers. Thus, the number of fuel balls for the container # 1 should be a cubic number ( n 3 for some n = 0, 1, 2, 3,... ) and that for the container # 2 should be a tetrahedral number ( n ( n + 1)( n + 2)/6 for some n = 0, 1, 2, 3,... ). Hakodate-maru is now at the star base Goryokaku preparing for the next mission to create a precise and detailed chart of stars and interstellar matters. Both of the fuel containers are now empty. Commander Parus of Goryokaku will soon send a message to Captain Future of Hakodate-maru on how many fuel balls Goryokaku can supply. Captain Future should quickly answer to Commander Parus on how many fuel balls she requests before her ship leaves Goryokaku. Of course, Captain Future and her omcers want as many fuel balls as possible. For example, consider the case Commander Parus offers 151200 fuel balls. If only the fuel container # 1 were available (i.e. ifthe fuel container # 2 were unavailable), at most 148877 fuel balls could be put into the fuel container since 148877 = 53 × 53 × 53 < 151200 < 54 × 54 × 54 . If only the fuel container # 2 were available, at most 147440 fuel balls could be put into the fuel container since 147440 = 95 × 96 × 97/6 < 151200 < 96 × 97 × 98/6 . Using both of the fuel containers # 1 and # 2, 151200 fuel balls can be put into the fuel containers since 151200 = 39 × 39 × 39 + 81 × 82 × 83/6 . In this case, Captain Future's answer should be "151200". Commander Parus's offer cannot be greater than 151200 because of the capacity of the fuel storages of Goryokaku. Captain Future and her omcers know that well. You are a fuel engineer assigned to Hakodate-maru. Your duty today is to help Captain Future with calculating the number of fuel balls she should request. Input The input is a sequence of at most 1024 positive integers. Each line contains a single integer. The sequence is followed by a zero, which indicates the end of data and should not be treated as input. You may assume that none of the input integers is greater than 151200. Output The output is composed of lines, each containing a single integer. Each output integer should be the greatest integer that is the sum of a nonnegative cubic number and a nonnegative tetrahedral number and that is not greater than the corresponding input number. No other characters should appear in the output. Sample Input 100 64 50 20 151200 0 Output for the Sample Input 99 64 47 20 151200
38,607
Digit For a positive integer a , let S(a) be the sum of the digits in base l . Also let L(a) be the minimum k such that S^k(a) is less than or equal to l-1 . Find the minimum a such that L(a) = N for a given N , and print a modulo m . Input The input contains several test cases, followed by a line containing "0 0 0". Each test case is given by a line with three integers N , m , l ( 0 \leq N \leq 10^5 , 1 \leq m \leq 10^9 , 2 \leq l \leq 10^9 ). Output For each test case, print its case number and the minimum a modulo m as described above. Sample Input 0 1000 10 1 1000 10 0 0 0 Output for the Sample Input Case 1: 1 Case 2: 10
38,608
Score : 700 points Problem Statement For strings s and t , we will say that s and t are prefix-free when neither is a prefix of the other. Let L be a positive integer. A set of strings S is a good string set when the following conditions hold true: Each string in S has a length between 1 and L (inclusive) and consists of the characters 0 and 1 . Any two distinct strings in S are prefix-free. We have a good string set S = \{ s_1, s_2, ..., s_N \} . Alice and Bob will play a game against each other. They will alternately perform the following operation, starting from Alice: Add a new string to S . After addition, S must still be a good string set. The first player who becomes unable to perform the operation loses the game. Determine the winner of the game when both players play optimally. Constraints 1 \leq N \leq 10^5 1 \leq L \leq 10^{18} s_1 , s_2 , ..., s_N are all distinct. { s_1 , s_2 , ..., s_N } is a good string set. |s_1| + |s_2| + ... + |s_N| \leq 10^5 Input Input is given from Standard Input in the following format: N L s_1 s_2 : s_N Output If Alice will win, print Alice ; if Bob will win, print Bob . Sample Input 1 2 2 00 01 Sample Output 1 Alice If Alice adds 1 , Bob will be unable to add a new string. Sample Input 2 2 2 00 11 Sample Output 2 Bob There are two strings that Alice can add on the first turn: 01 and 10 . In case she adds 01 , if Bob add 10 , she will be unable to add a new string. Also, in case she adds 10 , if Bob add 01 , she will be unable to add a new string. Sample Input 3 3 3 0 10 110 Sample Output 3 Alice If Alice adds 111 , Bob will be unable to add a new string. Sample Input 4 2 1 0 1 Sample Output 4 Bob Alice is unable to add a new string on the first turn. Sample Input 5 1 2 11 Sample Output 5 Alice Sample Input 6 2 3 101 11 Sample Output 6 Bob
38,609
Problem B: Hyper Rock-Scissors-Paper Rock-Scissors-Paper is a game played with hands and often used for random choice of a person for some purpose. Today, we have got an extended version, namely, Hyper Rock-Scissors-Paper (or Hyper RSP for short). In a game of Hyper RSP, the players simultaneously presents their hands forming any one of the following 15 gestures: Rock, Fire, Scissors, Snake, Human, Tree, Wolf, Sponge, Paper, Air, Water, Dragon, Devil, Lightning, and Gun. Figure 1: Hyper Rock-Scissors-Paper The arrows in the figure above show the defeating relation. For example, Rock defeats Fire, Scissors, Snake, Human, Tree, Wolf, and Sponge. Fire defeats Scissors, Snake, Human, Tree, Wolf, Sponge, and Paper. Generally speaking, each hand defeats other seven hands located after in anti-clockwise order in the figure. A player is said to win the game if the player’s hand defeats at least one of the other hands, and is not defeated by any of the other hands. Your task is to determine the winning hand, given multiple hands presented by the players. Input The input consists of a series of data sets. The first line of each data set is the number N of the players ( N < 1000). The next N lines are the hands presented by the players. The end of the input is indicated by a line containing single zero. Output For each data set, output the winning hand in a single line. When there are no winners in the game, output “Draw” (without quotes). Sample Input 8 Lightning Gun Paper Sponge Water Dragon Devil Air 3 Rock Scissors Paper 0 Output for the Sample Input Sponge Draw
38,610
石版 叀代囜家むワシロの遺跡から無数の石版が発芋された。研究者の調査によっお、各石版には䞀぀の単語が刻たれおいるこずが分かった。しかし、長幎の颚化によっお、以䞋の理由で解読が困難な石版もあるようだ。 石版に曞かれおいる単語の䞀぀の文字だけが 苔(こけ)で芆われおいる堎合があり、その文字を把握するこずができない。 石版の巊偎が欠けおおり、そこに䜕か文字列が曞かれおいたかもしれない石版の巊偎文字以䞊を把握するこずができない。 石版の右偎が欠けおおり、そこに䜕か文字列が曞かれおいたかもしれない石版の右偎文字以䞊を把握するこずができない。 石版に苔が生えおいる堎所は倚くおも䞀か所しかない。たた、欠けた石版に苔が生えおいるこずはあるが、石版の䞡偎が同時に欠けおいるこずはない。 研究者は、石版発芋以前の調査でわかっおいる単語をたずめた蟞曞を持っおいる。しかし、颚化の圱響による苔ず欠けがある石版から元の単語を掚枬したずき、蟞曞の䞭の単語に圓おはたるものがいく぀あるか、すぐには分からない。 石版の情報が䞎えられたずき、䞎えられた蟞曞の䞭に圓おはたりそうな単語がいく぀あるかを数えるプログラムを䜜成せよ。 Input 入力は以䞋の圢匏で䞎えられる。 N M word 1 word 2 : word N slate 1 slate 2 : slate M 行目に蟞曞に茉っおいる単語の数 N (1 ≀ N ≀ 50000)、石版の数 M (1 ≀ M ≀ 50000) が䞎えられる。続く N 行に単語 word i が䞎えられる。単語は英小文字のみを含む長さが 1 以䞊 200 以䞋の文字列である。ただし、 N 個の単語は党お異なる。続く M 行に、各石版の情報を衚す文字列 slate i が䞎えられる。 slate i は英小文字、「?」、「*」を含む長さが 1 以䞊 200 以䞋の文字列である。? は苔に芆われた文字を衚す。? は䞀぀の文字列に、倚くずも䞀぀しか珟れない。文字列の先頭が * の堎合、石版の巊偎が欠けおいるこずを瀺す。文字列の末尟が * の堎合、石版の右偎が欠けおいるこずを瀺す。* は、文字列の先頭か末尟以倖には珟れず、同時に䞡偎に珟れるこずはない。* が䞀぀だけの文字列が䞎えられるこずはない。 入力で䞎えられる文字列の文字の総数は 3000000 を超えない。 Output 各石版に぀いお、単語の数を行に出力する。 Sample Input 1 5 4 aloe apple apricot cactus cat apple ap* *e ca?* Sample Output 1 1 2 2 2
38,611
Binary Tree A rooted binary tree is a tree with a root node in which every node has at most two children. Your task is to write a program which reads a rooted binary tree T and prints the following information for each node u of T : node ID of u parent of u sibling of u the number of children of u depth of u height of u node type (root, internal node or leaf) If two nodes have the same parent, they are siblings . Here, if u and v have the same parent, we say u is a sibling of v (vice versa). The height of a node in a tree is the number of edges on the longest simple downward path from the node to a leaf. Here, the given binary tree consists of n nodes and evey node has a unique ID from 0 to n -1. Input The first line of the input includes an integer n , the number of nodes of the tree. In the next n lines, the information of each node is given in the following format: id left right id is the node ID, left is ID of the left child and right is ID of the right child. If the node does not have the left (right) child, the left ( right ) is indicated by -1 . Output Print the information of each node in the following format: node id : parent = p , sibling = s , degree = deg , depth = dep , height = h , type p is ID of its parent. If the node does not have a parent, print -1 . s is ID of its sibling. If the node does not have a sibling, print -1 . deg , dep and h are the number of children, depth and height of the node respectively. type is a type of nodes represented by a string ( root , internal node or leaf . If the root can be considered as a leaf or an internal node, print root . Please follow the format presented in a sample output below. Constraints 1 ≀ n ≀ 25 Sample Input 1 9 0 1 4 1 2 3 2 -1 -1 3 -1 -1 4 5 8 5 6 7 6 -1 -1 7 -1 -1 8 -1 -1 Sample Output 1 node 0: parent = -1, sibling = -1, degree = 2, depth = 0, height = 3, root node 1: parent = 0, sibling = 4, degree = 2, depth = 1, height = 1, internal node node 2: parent = 1, sibling = 3, degree = 0, depth = 2, height = 0, leaf node 3: parent = 1, sibling = 2, degree = 0, depth = 2, height = 0, leaf node 4: parent = 0, sibling = 1, degree = 2, depth = 1, height = 2, internal node node 5: parent = 4, sibling = 8, degree = 2, depth = 2, height = 1, internal node node 6: parent = 5, sibling = 7, degree = 0, depth = 3, height = 0, leaf node 7: parent = 5, sibling = 6, degree = 0, depth = 3, height = 0, leaf node 8: parent = 4, sibling = 5, degree = 0, depth = 2, height = 0, leaf Reference Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.
38,612
D : 垂束暡様 / Checkered Pattern 問題文 瞊暪の長さが h, w の長方圢があり 内郚には䞀蟺の長さが 1 の正方圢のマスが敷き詰められおいる 最も巊䞊のマスを (0,0) ずし (0,0) の i 個䞋で j 個右のマスを (i,j) のように衚すこずにするず (i,j) は i+j が偶数なら赀色に奇数なら青色に塗られおいる 今 (0,0) の巊䞊の頂点ず (h − 1,w − 1) の右䞋の頂点を線分で結んだ この線分が通る赀色の郚分の長さを a , 青色の郚分の長さを b ずおいたずき 比 a : b は敎数比ずなる a : b を最も簡単にしお(互いに玠な敎数で)衚せ 入力 T h_1 \ w_1 
 h_T \ w_T 1 ぀のファむルに T 個の入力が含たれる 1 行目に T  1+i 行目に T 番目の入力における瞊暪の長さ h_i , w_i が入力される 制玄 敎数である 1 ≀ T ≀ 1000 1 ≀ h_i, w_i ≀ 10 9 出力 各ケヌスに察する答えを 1 でスペヌス区切りで出力せよ 党䜓で T 行にわたる サンプル サンプル入力1 3 2 3 3 3 4 3 サンプル出力1 1 1 1 0 1 1
38,613
Problem B: Amazing Mazes You are requested to solve maze problems. Without passing through these mazes, you might not be able to pass through the domestic contest! A maze here is a rectangular area of a number of squares, lined up both lengthwise and widthwise, The area is surrounded by walls except for its entry and exit. The entry to the maze is at the leftmost part of the upper side of the rectangular area, that is, the upper side of the uppermost leftmost square of the maze is open. The exit is located at the rightmost part of the lower side, likewise. In the maze, you can move from a square to one of the squares adjoining either horizontally or vertically. Adjoining squares, however, may be separated by a wall, and when they are, you cannot go through the wall. Your task is to find the length of the shortest path from the entry to the exit. Note that there may be more than one shortest paths, or there may be none. Input The input consists of one or more datasets, each of which represents a maze. The first line of a dataset contains two integer numbers, the width w and the height h of the rectangular area, in this order. The following 2 × h − 1 lines of a dataset describe whether there are walls between squares or not. The first line starts with a space and the rest of the line contains w − 1 integers, 1 or 0, separated by a space. These indicate whether walls separate horizontally adjoining squares in the first row. An integer 1 indicates a wall is placed, and 0 indicates no wall is there. The second line starts without a space and contains w integers, 1 or 0, separated by a space. These indicate whether walls separate vertically adjoining squares in the first and the second rows. An integer 1/0 indicates a wall is placed or not. The following lines indicate placing of walls between horizontally and vertically adjoining squares, alternately, in the same manner. The end of the input is indicated by a line containing two zeros. The number of datasets is no more than 100. Both the widths and the heights of rectangular areas are no less than 2 and no more than 30. Output For each dataset, output a line having an integer indicating the length of the shortest path from the entry to the exit. The length of a path is given by the number of visited squares. If there exists no path to go through the maze, output a line containing a single zero. The line should not contain any character other than this number. Sample Input 2 3 1 0 1 0 1 0 1 9 4 1 0 1 0 0 0 0 0 0 1 1 0 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0 12 5 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 1 0 1 0 0 0 0 0 0 1 1 0 1 1 0 1 1 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 Output for the Sample Input 4 0 20
38,615
乗車刞 新幹線に乗るためには、「乗車刞」「特急刞」の2枚の切笊が必芁です。経路の䞀郚で新幹線を利甚しない堎合があるため、これらは別々の切笊ずなっおいたすが、新幹線のみを利甚する経路では、1枚で乗車刞ず特急刞を兌ねる「乗車・特急刞」が発行されるこずもありたす。 自動改札機では、これらの切笊を読み蟌んで、正しい切笊が投入されたずきだけゲヌトを開けなければなりたせん。「乗車刞」ず「特急刞」それぞれ枚、たたは、その䞡方、たたは、「乗車・特急刞」が枚投入されたかどうかを刀定し、自動改札機の扉の開閉を刀断するプログラムを䜜成しお䞋さい。 入力 入力は以䞋の圢匏で䞎えられる。 b 1 b 2 b 3 入力は行からなり、぀の空癜で区切られた぀の敎数を含む。b 1 は「乗車刞」の投入状態、b 2 は「特急刞」の投入状態、b 3 は「乗車・特急刞」の投入状態を瀺す。投入状態は0たたは1で衚され、0の堎合は投入されおいない状態、1の堎合は投入されおいる状態を衚す。ただし、想定される投入状態の組み合わせは以䞋の堎合のみずする。 入力 投入状態 投入に察する扉の動䜜 1 0 0 「乗車刞」のみ投入 Close 0 1 0 「特急刞」のみ投入 Close 1 1 0 「乗車刞」ず「特急刞」投入 Open 0 0 1 「乗車・特急刞」投入 Open 0 0 0 投入なし Close 出力 自動改札機の開閉を衚すOpenたたはCloseを行に出力する。 入力䟋1 0 0 1 出力䟋1 Open 入力䟋2 0 1 0 出力䟋2 Close
38,616
Score : 100 points Problem Statement We have a grid with H rows and W columns, where all the squares are initially white. You will perform some number of painting operations on the grid. In one operation, you can do one of the following two actions: Choose one row, then paint all the squares in that row black. Choose one column, then paint all the squares in that column black. At least how many operations do you need in order to have N or more black squares in the grid? It is guaranteed that, under the conditions in Constraints, having N or more black squares is always possible by performing some number of operations. Constraints 1 \leq H \leq 100 1 \leq W \leq 100 1 \leq N \leq H \times W All values in input are integers. Input Input is given from Standard Input in the following format: H W N Output Print the minimum number of operations needed. Sample Input 1 3 7 10 Sample Output 1 2 You can have 14 black squares in the grid by performing the "row" operation twice, on different rows. Sample Input 2 14 12 112 Sample Output 2 8 Sample Input 3 2 100 200 Sample Output 3 2
38,618
F: カヌドゲヌム 問題 カヌドを䜿ったゲヌムを $Q$ 回行いたす。 カヌドには $1 \cdots N$ の数が曞かれおおり、各数が曞かれたカヌドはゲヌムを行うのに十分な枚数がありたす。 $i$ 回目のゲヌムでは、はじめに手札ずしお 2 枚のカヌドが配られたす。 それぞれのカヌドに曞かれおいる数字は $x_i$ ず $y_i$ です。 カヌドはルヌルに埓っお亀換するこずができたす。 $j$ 番目 $(1 \le j \le M)$ のルヌルでは、カヌド $a_j$ を手数料 $c_j$ 円で別のカヌド $b_j$ に亀換するこずができたす。 各ルヌルは䜕回でも甚いるこずができたす。たた、手数料が足りなくお亀換できない堎合はありたせん。 最埌に、手札のカヌドの数字を $R$ で割ったあたりが等しい時、報酬ずしお $z_i$ 円受け取りたす。 異なる堎合報酬は $0$ 円です。 $Q$ 回ゲヌムを終えた時に増やすこずのできるお金の最倧倀を求めおください。 制玄 $1 \le N \le 10^5$ $0 \le M \le \min(2 \times 10^5, N\times(N-1))$ $2 \le R \le 10$ $1 \le Q \le 10^5$ $1 \le a_j, b_j \le N$ $a_j \neq b_j$ $0 \le c_j \le 10^5$ $1 \le x_i, y_i \le N$ $0 \le z_i \le 10^5$ 入力 入力は以䞋の圢匏で暙準入力から䞎えられたす。 $N \ M \ R \ Q$ $a_1 \ b_1 \ c_1$ $\vdots$ $a_M \ b_M \ c_M$ $x_1 \ y_1 \ z_1$ $\vdots$ $x_Q \ y_Q \ z_Q$ 出力 ゲヌムを $Q$ 回行ったずきに増やすこずのできるお金の最倧倀を 1 行で出力しおください。たた、末尟に改行も出力しおください。 サンプル 入力䟋 1 4 4 2 2 1 2 1 2 3 1 3 4 5 4 1 7 1 4 5 2 4 3 出力䟋 1 7 1 回目のゲヌムではカヌド 1 を手数料 1 でカヌド 2 に亀換するず手札のカヌドを 2 で割った時のあたりがどちらも 0 ずなり、埗られる報酬は 5 ずなりたす。 2 回目のゲヌムではすでに 2 で割ったずきのあたりがどちらも 0 であり、埗られる報酬は 3 ずなりたす。 2 回のゲヌムの結果、増やせるお金は $5-1+3=7$ です。 入力䟋 2 4 4 2 1 1 2 1 2 3 1 3 4 5 4 1 7 3 4 2 出力䟋 2 0 カヌドの亀換での手数料が報酬よりも倚いので、亀換を行わないのが最適です。
38,619
Problem H: Fibonacci Sets Fibonacci number f ( i ) appear in a variety of puzzles in nature and math, including packing problems, family trees or Pythagorean triangles. They obey the rule f ( i ) = f ( i - 1) + f ( i - 2), where we set f (0) = 1 = f (-1). Let V and d be two certain positive integers and be N ≡ 1001 a constant. Consider a set of V nodes, each node i having a Fibonacci label F [ i ] = ( f ( i ) mod N) assigned for i = 1,..., V ≀ N. If | F ( i ) - F ( j )| < d , then the nodes i and j are connected. Given V and d , how many connected subsets of nodes will you obtain? Figure 1: There are 4 connected subsets for V = 20 and d = 100. Input Each data set is defined as one line with two integers as follows: Line 1 : Number of nodes V and the distance d . Input includes several data sets (i.e., several lines). The number of dat sets is less than or equal to 50. Constraints 1 ≀ V ≀ 1000 1 ≀ d ≀ 150 Output Output line contains one integer - the number of connected subsets - for each input line. Sample Input 5 5 50 1 13 13 Output for the Sample Input 2 50 8
38,620
Problem G: Number Sorting Consider sets of natural numbers. Some sets can be sorted in the same order numerically and lexicographically. {2, 27, 3125, 9000} is one example of such sets; {2, 27, 243} is not since lexicographic sorting would yield {2, 243, 27}. Your task is to write a program that, for the set of integers in a given range [ A , B ] (i.e. between A and B inclusive), counts the number of non-empty subsets satisfying the above property. Since the resulting number is expected to be very huge, your program should output the number in modulo P given as the input. Input The input consists of multiple datasets. Each dataset consists of a line with three integers A , B , and P separated by a space. These numbers satisfy the following conditions: 1 ≀ A ≀ 1,000,000,000, 0 ≀ B - A < 100,000, 1 ≀ P ≀ 1,000,000,000. The end of input is indicated by a line with three zeros. Output For each dataset, output the number of the subsets in modulo P . Sample Input 1 10 1000 1 100000 1000000000 999999999 1000099998 1000000000 0 0 0 Output for the Sample Input 513 899507743 941554688
38,621
Score : 100 points Problem Statement There is a grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i -th row from the top and the j -th column from the left. In the grid, N Squares (r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N) are wall squares, and the others are all empty squares. It is guaranteed that Squares (1, 1) and (H, W) are empty squares. Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square. Find the number of Taro's paths from Square (1, 1) to (H, W) , modulo 10^9 + 7 . Constraints All values in input are integers. 2 \leq H, W \leq 10^5 1 \leq N \leq 3000 1 \leq r_i \leq H 1 \leq c_i \leq W Squares (r_i, c_i) are all distinct. Squares (1, 1) and (H, W) are empty squares. Input Input is given from Standard Input in the following format: H W N r_1 c_1 r_2 c_2 : r_N c_N Output Print the number of Taro's paths from Square (1, 1) to (H, W) , modulo 10^9 + 7 . Sample Input 1 3 4 2 2 2 1 4 Sample Output 1 3 There are three paths as follows: Sample Input 2 5 2 2 2 1 4 2 Sample Output 2 0 There may be no paths. Sample Input 3 5 5 4 3 1 3 5 1 3 5 3 Sample Output 3 24 Sample Input 4 100000 100000 1 50000 50000 Sample Output 4 123445622 Be sure to print the count modulo 10^9 + 7 .
38,622
Problem H: Cone Cut Problem この問題では3次元空間䞊のある点 X ずある点 Y を通るような盎線を盎線 XY ず曞く。 3次元空間䞊に円錐ずその円錐内郚の点 P が䞎えられる。点 P を通り円錐の底面に垂盎な盎線ず円錐の底面ずの亀点を点 Q ずする。この時、円錐の底面の䞭心を O ずし、盎線 OQ ず円錐の底面の円呚ずの亀点を、点 Q に近い方から点 A 、点 B ずする(ただし、点 O ず点 Q が同䞀点のずきは、点 O を通る任意の盎線ず円錐の底面の円呚ずの亀点を点 A 、点 B ずする。)。たた、円錐の底面を含むような平面においお、点 O を通り、盎線 AB ず垂盎に亀わる盎線ず円錐の底面の円呚ずの亀点を点 C 、点 D ずする(ただし、点 C ず点 D は入れ替えおも問題ない)。 以䞋の図は、円錐の頂点方向から芋た円錐の底面の図である。 盎線 AP ず盎線 BP の円錐ずの亀点盎線 AP の堎合、点 A でない方の亀点を指す。盎線 BP も同様。を点 A' 、点 B' ずし、点 A' ず点 B' を通り盎線 CD ず平行な平面 S で円錐を切断する。この時、切断埌の頂点を含む図圢の䜓積ずもう䞀方の図圢の䜓積を空癜区切りで出力せよ。 Input 円錐の頂点の x 座暙 y 座暙 z 座暙 円錐の底面の䞭心の x 座暙 y 座暙 z 座暙 円錐の底面の半埄 点 P の x 座暙 y 座暙 z 座暙 各行のそれぞれの倀はスペヌス区切りで䞎えられる。 Constraint 入力の倀はすべお敎数であり、その絶察倀は1000以䞋である。 円錐の高さずその底面の半埄は1以䞊であり、点 P ず円錐の距離は10 -5 以䞊ある。 Output 切断埌の頂点を含む図圢の䜓積ずもう䞀方の図圢の䜓積を空癜区切りで出力せよ。 ただし、10 -5 を超える誀差を含んではいけない。 Sample Input 1 0 0 10 0 0 0 4 0 0 1 Sample Output 1 91.769438 75.782170 Sample Input 2 0 0 10 0 0 0 4 1 1 1 Sample Output 2 84.050413 83.501195 Sample Input 3 0 0 0 8 9 10 5 1 1 1 Sample Output 3 0.071663 409.709196
38,623
Problem J: Averaging Problem アむヅ囜には$N$個の島ず$N-1$個の橋があり、各島はそれぞれ$1$から$N$たでの番号が割り振られおいる。 $i$番目の橋は島$u_i$ず島$v_i$を双方向に結んでおり、島民は橋を甚いお島ず島ずを行き来するこずができる。たた、橋以倖に島ず島を行き来する方法はない。 どの島からどの島ぞもいく぀かの橋を枡るこずで到達するこずができる。 珟圚、島$i$には$X_i$人の島民がいる。 各島の環境ぞの負荷を分散させるためにアむヅ囜は䜕人かの囜民に別の島ぞ匕越しおもらうこずにした。 å³¶$a$に䜏んでいる人を島$b$に匕越しさせるためには島$a$ず島$b$の距離ず同じ分のコストがかかる。ただし、島$a$ず島$b$の距離は島$a$から島$b$ぞ行くために枡る必芁のある橋の数の最小倀で定矩される。 どの$2$぀の島を遞んでもそれらの島民の人数の差の絶察倀が$1$以䞋になるように囜民に匕越しおもらいたい。 このずき、必芁なコストの総和の最小倀を求めよ。 Input 入力は以䞋の圢匏ですべお敎数で䞎えられる。 $N$ $X_1$ $X_2$ ... $X_N$ $u_1$ $v_1$ $u_2$ $v_2$ ... $u_{N-1}$ $v_{N-1}$ $1$行目に島の数$N$が䞎えられる。 $2$行目には各島の島民の人数を衚す$N$個の敎数が空癜区切りで䞎えられる。$i$番目の敎数$X_i$は島$i$の島民の人数を衚す。 $3$行目から続く$N-1$行には各橋が぀なぐ島の番号が空癜区切りで䞎えられる。$2+i$行目の入力では、$i$番目の橋が島$u_i$ず島$v_i$を双方向に結んでいるこずを衚す。 Constraints 入力は以䞋の条件を満たす。 $2 \le N \le 5000$ $0 \le X_i \le 10^9$ $1 \le u_i, v_i \le N$ どの島からどの島ぞもいく぀かの橋を枡るこずで到達するこずができる Output コストの総和の最小倀を1行に出力せよ。 Sample Input 1 5 4 0 4 0 0 1 2 1 3 1 4 2 5 Sample Output 1 7 å³¶$1$の人が$1$人、島$2$ぞ、 å³¶$1$の人が$1$人、島$5$ぞ、 å³¶$3$の人が$2$人、島$4$ぞ匕っ越すこずでどの$2$぀の島を遞んでもそれらの島民の人数の差の絶察倀が$1$以䞋になる。 たたこのずきのコストの総和は$1+2+2\times2 = 7$であり、これが最小である。 Sample Input 2 7 0 7 2 5 0 3 0 1 2 1 3 1 4 2 5 3 6 3 7 Sample Output 2 10
38,624
チヌズ (Cheese) 問題 今幎も JOI 町のチヌズ工堎がチヌズの生産を始めねずみが巣から顔を出したJOI 町は東西南北に区画敎理されおいお各区画は巣チヌズ工堎障害物空き地のいずれかであるねずみは巣から出発しお党おのチヌズ工堎を蚪れチヌズを 1 個ず぀食べる この町にはN 個のチヌズ工堎がありどの工堎も皮類のチヌズだけを生産しおいるチヌズの硬さは工堎によっお異なっおおり硬さ 1 から N たでのチヌズを生産するチヌズ工堎がちょうど 1 ぀ず぀ある ねずみの最初の䜓力は 1 でありチヌズを 1 個食べるごずに䜓力が 1 増えるただしねずみは自分の䜓力よりも硬いチヌズを食べるこずはできない ねずみは東西南北に隣り合う区画に 1 分で移動するこずができるが障害物の区画には入るこずができないチヌズ工堎をチヌズを食べずに通り過ぎるこずもできるすべおのチヌズを食べ終えるたでにかかる最短時間を求めるプログラムを曞けただしねずみがチヌズを食べるのにかかる時間は無芖できる 入力 入力は H+1 行ある1 行目には 3 ぀の敎数 HWN (1 ≀ H ≀ 10001 ≀ W ≀ 10001 ≀ N ≀ 9) がこの順に空癜で区切られお曞かれおいる2 行目から H+1 行目たでの各行には'S''1', '2', ..., '9''X''.' からなる W 文字の文字列が曞かれおおり各々が各区画の状態を衚しおいる北から i 番目西から j 番目の区画を (i,j) ず蚘述するこずにするず (1 ≀ i ≀ H, 1 ≀ j ≀ W)第 i+1 行目の j 番目の文字は区画 (i,j) が巣である堎合は 'S' ずなり障害物である堎合は 'X' ずなり空き地である堎合は '.' ずなり硬さ 1, 2, ..., 9 のチヌズを生産する工堎である堎合はそれぞれ '1', '2', ..., '9' ずなる入力には巣ず硬さ 1, 2, ..., N のチヌズを生産する工堎がそれぞれ 1 ぀ず぀ある他のマスは障害物たたは空き地であるこずが保蚌されおいるねずみは党おのチヌズを食べられるこずが保蚌されおいる 出力 すべおのチヌズを食べ終えるたでにかかる最短時間分を衚す敎数を 1 行で出力せよ 入出力䟋 入力䟋 1 3 3 1 S.. ... ..1 出力䟋 1 4 入力䟋 2 4 5 2 .X..1 ....X .XX.S .2.X. 出力䟋 2 12 入力䟋 3 10 10 9 .X...X.S.X 6..5X..X1X ...XXXX..X X..9X...X. 8.X2X..X3X ...XX.X4.. XX....7X.. X..X..XX.. X...X.XX.. ..X....... 出力䟋 3 91 問題文ず自動審刀に䜿われるデヌタは、 情報オリンピック日本委員䌚 が䜜成し公開しおいる問題文ず採点甚テストデヌタです。
38,625
Score : 100 points Problem Statement You are given a positive integer N . Find the minimum positive integer divisible by both 2 and N . Constraints 1 \leq N \leq 10^9 All values in input are integers. Input Input is given from Standard Input in the following format: N Output Print the minimum positive integer divisible by both 2 and N . Sample Input 1 3 Sample Output 1 6 6 is divisible by both 2 and 3 . Also, there is no positive integer less than 6 that is divisible by both 2 and 3 . Thus, the answer is 6 . Sample Input 2 10 Sample Output 2 10 Sample Input 3 999999999 Sample Output 3 1999999998
38,626
Max Score: 1450 Points Problem Statement There are N workers in Atcoder company. Each worker is numbered 0 through N - 1 , and the boss for worker i is p_i like a tree structure and the salary is currently a_i . ( p_i < i , especially p_0 = -1 because worker 0 is a president) In atcoder, the boss of boss of boss of ... (repeated k times) worker i called " k -th upper boss", and " k -th lower subordinate" called for vice versa. You have to process Q queries for Atcoder: Query 1: You are given v_i, d_i, x_i . Increase the salary of worker v_i , and all j -th ( 1 ≀ j ≀ d_i ) lower subordinates by x_i . Query 2: You are given v_i, d_i . Calculate the sum of salary of worker v_i and all j -th ( 1 ≀ j ≀ d_i ) lower subordinates. Query 3: You are given pr_i, ar_i . Now Atcoder has a new worker c ! ( c is the current number of workers) The boss is pr_i , and the first salary is ar_i . Process all queries!!! Input Format Let the i -th query query_i , the input format is following: N Q p_0 a_0 p_1 a_1 : : p_{N - 1} a_{N - 1} query_0 query_1 : : query_{Q - 1} THe format of query_i is one of the three format: 1 v_i d_i x_i 2 v_i d_i 3 pr_i ar_i Output Format Print the result in one line for each query 2. Constraints N ≀ 400000 Q ≀ 50000 p_i < i for all valid i . In each question 1 or 2, worker v_i exists. d_i ≀ 400000 0 ≀ a_i, x_i ≀ 1000 Scoring Subtask 1 [ 170 points] N, Q ≀ 5000 Subtask 2 [ 310 points] p_i + 1 = i for all valid i . Subtask 3 [ 380 points] There are no query 3. Subtask 4 [ 590 points] There are no additional constraints. Sample Input 1 6 7 -1 6 0 5 0 4 2 3 2 2 1 1 2 0 1 1 0 2 1 2 2 1 3 3 3 2 0 3 3 3 4 2 1 1 Sample Output 1 15 12 30 8 Sample Input 2 7 9 -1 1 0 5 0 7 0 8 1 3 4 1 5 1 2 1 1 2 1 2 1 1 2 3 1 4 1 1 2 3 1 2 0 2 3 6 1 3 7 11 2 0 15 Sample Output 2 8 9 8 31 49
38,627
Problem E: Origami Through-Hole Origami is the traditional Japanese art of paper folding. One day, Professor Egami found the message board decorated with some pieces of origami works pinned on it, and became interested in the pinholes on the origami paper. Your mission is to simulate paper folding and pin punching on the folded sheet, and calculate the number of pinholes on the original sheet when unfolded. A sequence of folding instructions for a flat and square piece of paper and a single pinhole position are specified. As a folding instruction, two points P and Q are given. The paper should be folded so that P touches Q from above (Figure 4). To make a fold, we first divide the sheet into two segments by creasing the sheet along the folding line , i.e., the perpendicular bisector of the line segment PQ , and then turn over the segment containing P onto the other. You can ignore the thickness of the paper. Figure 4: Simple case of paper folding The original flat square piece of paper is folded into a structure consisting of layered paper segments, which are connected by linear hinges. For each instruction, we fold one or more paper segments along the specified folding line, dividing the original segments into new smaller ones. The folding operation turns over some of the paper segments (not only the new smaller segments but also some other segments that have no intersection with the folding line) to the reflective position against the folding line. That is, for a paper segment that intersects with the folding line, one of the two new segments made by dividing the original is turned over; for a paper segment that does not intersect with the folding line, the whole segment is simply turned over. The folding operation is carried out repeatedly applying the following rules, until we have no segment to turn over. Rule 1: The uppermost segment that contains P must be turned over. Rule 2: If a hinge of a segment is moved to the other side of the folding line by the operation, any segment that shares the same hinge must be turned over. Rule 3: If two paper segments overlap and the lower segment is turned over, the upper segment must be turned over too. In the examples shown in Figure 5, (a) and (c) show cases where only Rule 1 is applied. (b) shows a case where Rule 1 and 2 are applied to turn over two paper segments connected by a hinge, and (d) shows a case where Rule 1, 3 and 2 are applied to turn over three paper segments. Figure 5: Different cases of folding After processing all the folding instructions, the pinhole goes through all the layered segments of paper at that position. In the case of Figure 6, there are three pinholes on the unfolded sheet of paper. Figure 6: Number of pinholes on the unfolded sheet Input The input is a sequence of datasets. The end of the input is indicated by a line containing a zero. Each dataset is formatted as follows. k p x 1 p y 1 q x 1 q y 1 . . . p x k p y k q x k q y k h x h y For all datasets, the size of the initial sheet is 100 mm square, and, using mm as the coordinate unit, the corners of the sheet are located at the coordinates (0, 0), (100, 0), (100, 100) and (0, 100). The integer k is the number of folding instructions and 1 ≀ k ≀ 10. Each of the following k lines represents a single folding instruction and consists of four integers p x i , p y i , q x i , and q y i , delimited by a space. The positions of point P and Q for the i -th instruction are given by ( p x i , p y i ) and ( q x i , q y i ), respectively. You can assume that P ≠ Q . You must carry out these instructions in the given order. The last line of a dataset contains two integers h x and h y delimited by a space, and ( h x , h y ) represents the position of the pinhole. You can assume the following properties: The points P and Q of the folding instructions are placed on some paper segments at the folding time, and P is at least 0.01 mm distant from any borders of the paper segments. The position of the pinhole also is at least 0.01 mm distant from any borders of the paper segments at the punching time. Every folding line, when infinitely extended to both directions, is at least 0.01 mm distant from any corners of the paper segments before the folding along that folding line. When two paper segments have any overlap, the overlapping area cannot be placed between any two parallel lines with 0.01 mm distance. When two paper segments do not overlap, any points on one segment are at least 0.01 mm distant from any points on the other segment. For example, Figure 5 (a), (b), (c) and (d) correspond to the first four datasets of the sample input. Output For each dataset, output a single line containing the number of the pinholes on the sheet of paper, when unfolded. No extra characters should appear in the output. Sample Input 2 90 90 80 20 80 20 75 50 50 35 2 90 90 80 20 75 50 80 20 55 20 3 5 90 15 70 95 90 85 75 20 67 20 73 20 75 3 5 90 15 70 5 10 15 55 20 67 20 73 75 80 8 1 48 1 50 10 73 10 75 31 87 31 89 91 94 91 96 63 97 62 96 63 80 61 82 39 97 41 95 62 89 62 90 41 93 5 2 1 1 1 -95 1 -96 1 -190 1 -191 1 -283 1 -284 1 -373 1 -374 1 -450 1 2 77 17 89 8 103 13 85 10 53 36 0 Output for the Sample Input 3 4 3 2 32 1 0
38,628
Score: 500 points Problem Statement N people are standing in a queue, numbered 1, 2, 3, ..., N from front to back. Each person wears a hat, which is red, blue, or green. The person numbered i says: "In front of me, exactly A_i people are wearing hats with the same color as mine." Assuming that all these statements are correct, find the number of possible combinations of colors of the N people's hats. Since the count can be enormous, compute it modulo 1000000007 . Constraints 1 \leq N \leq 100000 0 \leq A_i \leq N-1 All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 A_3 ... A_N Output Print the number of possible combinations of colors of the N people's hats, modulo 1000000007 . Sample Input 1 6 0 1 2 3 4 5 Sample Output 1 3 We have three possible combinations, as follows: Red, Red, Red, Red, Red, Red Blue, Blue, Blue, Blue, Blue, Blue Green, Green, Green, Green, Green, Green Sample Input 2 3 0 0 0 Sample Output 2 6 Sample Input 3 54 0 0 1 0 1 2 1 2 3 2 3 3 4 4 5 4 6 5 7 8 5 6 6 7 7 8 8 9 9 10 10 11 9 12 10 13 14 11 11 12 12 13 13 14 14 15 15 15 16 16 16 17 17 17 Sample Output 3 115295190
38,629
Problem H: Queen's Case A small country called Maltius was governed by a queen. The queen was known as an oppressive ruler. People in the country suffered from heavy taxes and forced labor. So some young people decided to form a revolutionary army and fight against the queen. Now, they besieged the palace and have just rushed into the entrance. Your task is to write a program to determine whether the queen can escape or will be caught by the army. Here is detailed description. The palace can be considered as grid squares. The queen and the army move alternately. The queen moves first. At each of their turns, they either move to an adjacent cell or stay at the same cell. Each of them must follow the optimal strategy. If the queen and the army are at the same cell, the queen will be caught by the army immediately. If the queen is at any of exit cells alone after the army’s turn, the queen can escape from the army. There may be cases in which the queen cannot escape but won’t be caught by the army forever, under their optimal strategies. Input The input consists of multiple datasets. Each dataset describes a map of the palace. The first line of the input contains two integers W (1 ≀ W ≀ 30) and H (1 ≀ H ≀ 30), which indicate the width and height of the palace. The following H lines, each of which contains W characters, denote the map of the palace. " Q " indicates the queen, " A " the army," E " an exit," # " a wall and " . " a floor. The map contains exactly one " Q ", exactly one " A " and at least one " E ". You can assume both the queen and the army can reach all the exits. The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed. Output For each dataset, output " Queen can escape. ", " Army can catch Queen. " or " Queen can not escape and Army can not catch Queen. " in a line. Sample Input 2 2 QE EA 3 1 QAE 3 1 AQE 5 5 ..E.. .###. A###Q .###. ..E.. 5 1 A.E.Q 5 5 A.... ####. ..E.. .#### ....Q 0 0 Output for the Sample Input Queen can not escape and Army can not catch Queen. Army can catch Queen. Queen can escape. Queen can not escape and Army can not catch Queen. Army can catch Queen. Army can catch Queen. Hint On the first sample input, the queen can move to exit cells, but either way the queen will be caught at the next army’s turn. So the optimal strategy for the queen is staying at the same cell. Then the army can move to exit cells as well, but again either way the army will miss the queen from the other exit. So the optimal strategy for the army is also staying at the same cell. Thus the queen cannot escape but won’t be caught.
38,630
F - Polygon Guards Problem Statement You are an IT system administrator in the Ministry of Defense of Polygon Country. Polygon Country's border forms the polygon with $N$ vertices. Drawn on the 2D-plane, all of its vertices are at the lattice points and all of its edges are parallel with either the $x$-axis or the $y$-axis. In order to prevent enemies from invading the country, it is surrounded by very strong defense walls along its border. However, on the vertices, the junctions of walls have unavoidable structural weaknesses. Therefore, enemies might attack and invade from the vertices. To observe the vertices and find an invasion by enemies as soon as possible, the ministry decided to hire some guards. The ministry plans to locate them on some vertices such that all the vertices are observed by at least one guard. A guard at the vertex $A$ can observe a vertex $B$ if the entire segment connecting $A$ and $B$ is inside or on the edge of Polygon Country. Of course, guards can observe the vertices they are located on. And a guard can observe simultaneously all the vertices he or she can observe. To reduce the defense expense, the ministry wants to minimize the number of guards. Your task is to calculate the minimum number of guards required to observe all the vertices of Polygon Country. Input The input is formatted as follows. $N$ $X_1$ $Y_1$ : : $X_N$ $Y_N$ The first line contains an even integer $N$ ($4 \le N \lt 40$). The following $N$ lines describe the vertices of Polygon Country. Each of the lines contains two integers, $X_i$ and $Y_i$ ($1 \le i \le N$, $\lvert X_i \rvert \le 1{,}000$, $\lvert Y_i \rvert \le 1{,}000$), separated by one space. The position of the $i$-th vertex is $(X_i,Y_i)$. If $i$ is odd, $X_i = X_{i+1}$, $Y_i \ne Y_{i+1}$. Otherwise, $X_i \ne X_{i+1}$, $Y_i = Y_{i+1}$. Here, we regard that $X_{N+1} = X_1$, and $Y_{N+1} = Y_1$. The vertices are given in counterclockwise order under the coordinate system that the $x$-axis goes right, and the $y$-axis goes up. The shape of Polygon Country is simple. That is, each edge doesn’t share any points with other edges except that its both end points are shared with its neighbor edges. Output Print the minimum number of guards in one line. Sample Input 1 8 0 2 0 0 2 0 2 1 3 1 3 3 1 3 1 2 Output for the Sample Input 1 1 Sample Input 2 12 0 0 0 -13 3 -13 3 -10 10 -10 10 10 -1 10 -1 13 -4 13 -4 10 -10 10 -10 0 Output for the Sample Input 2 2
38,631
Problem D: Long Distance Taxi A taxi driver, Nakamura, was so delighted because he got a passenger who wanted to go to a city thousands of kilometers away. However, he had a problem. As you may know, most taxis in Japan run on liquefied petroleum gas (LPG) because it is cheaper than gasoline. There are more than 50,000 gas stations in the country, but less than one percent of them sell LPG. Although the LPG tank of his car was full, the tank capacity is limited and his car runs 10 kilometer per liter, so he may not be able to get to the destination without filling the tank on the way. He knew all the locations of LPG stations. Your task is to write a program that finds the best way from the current location to the destination without running out of gas. Input The input consists of several datasets, and each dataset is in the following format. N M cap src dest c 1,1 c 1,2 d 1 c 2,1 c 2,2 d 2 . . . c N,1 c N,2 d N s 1 s 2 . . . s M The first line of a dataset contains three integers ( N, M, cap ), where N is the number of roads (1 ≀ N ≀ 3000), M is the number of LPG stations (1≀ M ≀ 300), and cap is the tank capacity (1 ≀ cap ≀ 200) in liter. The next line contains the name of the current city ( src ) and the name of the destination city ( dest ). The destination city is always different from the current city. The following N lines describe roads that connect cities. The road i (1 ≀ i ≀ N) connects two different cities c i,1 and c i,2 with an integer distance d i (0 < d i ≀ 2000) in kilometer, and he can go from either city to the other. You can assume that no two different roads connect the same pair of cities. The columns are separated by a single space. The next M lines ( s 1 , s 2 ,..., s M ) indicate the names of the cities with LPG station. You can assume that a city with LPG station has at least one road. The name of a city has no more than 15 characters. Only English alphabet ('A' to 'Z' and 'a' to 'z', case sensitive) is allowed for the name. A line with three zeros terminates the input. Output For each dataset, output a line containing the length (in kilometer) of the shortest possible journey from the current city to the destination city. If Nakamura cannot reach the destination, output "-1" (without quotation marks). You must not output any other characters. The actual tank capacity is usually a little bit larger than that on the specification sheet, so you can assume that he can reach a city even when the remaining amount of the gas becomes exactly zero. In addition, you can always fill the tank at the destination so you do not have to worry about the return trip. Sample Input 6 3 34 Tokyo Kyoto Tokyo Niigata 335 Tokyo Shizuoka 174 Shizuoka Nagoya 176 Nagoya Kyoto 195 Toyama Niigata 215 Toyama Kyoto 296 Nagoya Niigata Toyama 6 3 30 Tokyo Kyoto Tokyo Niigata 335 Tokyo Shizuoka 174 Shizuoka Nagoya 176 Nagoya Kyoto 195 Toyama Niigata 215 Toyama Kyoto 296 Nagoya Niigata Toyama 0 0 0 Output for the Sample Input 846 -1
38,632
Score : 100 points Problem Statement Cat Snuke is learning to write characters. Today, he practiced writing digits 1 and 9 , but he did it the other way around. You are given a three-digit integer n written by Snuke. Print the integer obtained by replacing each digit 1 with 9 and each digit 9 with 1 in n . Constraints 111 \leq n \leq 999 n is an integer consisting of digits 1 and 9 . Input Input is given from Standard Input in the following format: n Output Print the integer obtained by replacing each occurrence of 1 with 9 and each occurrence of 9 with 1 in n . Sample Input 1 119 Sample Output 1 991 Replace the 9 in the ones place with 1 , the 1 in the tens place with 9 and the 1 in the hundreds place with 9 . The answer is 991 . Sample Input 2 999 Sample Output 2 111
38,633
Score : 400 points Problem Statement Given are positive integers A and B . Let us choose some number of positive common divisors of A and B . Here, any two of the chosen divisors must be coprime. At most, how many divisors can we choose? Definition of common divisor An integer d is said to be a common divisor of integers x and y when d divides both x and y . Definition of being coprime Integers x and y are said to be coprime when x and y have no positive common divisors other than 1 . Definition of dividing An integer x is said to divide another integer y when there exists an integer \alpha such that y = \alpha x . Constraints All values in input are integers. 1 \leq A, B \leq 10^{12} Input Input is given from Standard Input in the following format: A B Output Print the maximum number of divisors that can be chosen to satisfy the condition. Sample Input 1 12 18 Sample Output 1 3 12 and 18 have the following positive common divisors: 1 , 2 , 3 , and 6 . 1 and 2 are coprime, 2 and 3 are coprime, and 3 and 1 are coprime, so we can choose 1 , 2 , and 3 , which achieve the maximum result. Sample Input 2 420 660 Sample Output 2 4 Sample Input 3 1 2019 Sample Output 3 1 1 and 2019 have no positive common divisors other than 1 .
38,635
運送䌚瀟 癜虎運送は䌚接若束垂を代衚する運送䌚瀟です。昚今の原油䟡栌の高隰は運送䌚瀟にも倚倧なダメヌゞを䞎え、運送䌚瀟各瀟では、できるだけ少ないガ゜リンで荷物を運ぶこずが課題ずなっおいたす。 癜虎運送では、重い荷物を積んだトラックは、その走りだしに倚くの゚ネルギヌを必芁ずするこずに着目したした。トラックが倉庫を出発しおから䞀床も停止するこずなく目的地たで到達する経路の䞭で最短時間の経路を通るこずで、ガ゜リンの節玄ができるず考えたした。 あなたの仕事は、このような最短経路を蚈算できるカヌナビを開発するこずです。䜜成するカヌナビ甚プログラムの仕様は以䞋のずおりです。 垂内は、䞋図のように、東西方向の道路 M 本、南北方向の道路 N 本 からなる栌子で衚し、栌子の各亀点は亀差点を衚したす。 亀差点のいく぀かには東西 - 南北の方向に信号機が蚭眮されおおり、䞀定の呚期で青、赀のシグナルが点灯しおいたす。 垂内の亀差点間を結ぶ道路には工事䞭で通過できない個所がいく぀かありたす。 トラックが亀差点から亀差点ぞ移動するのに必芁な時間は䞀定ですが、枋滞しおいる道路ではさらに長い時間がかかりたす。 垂内の道路情報、トラックが亀差点間を移動するのに必芁な時間、信号機がある亀差点ず各信号機の呚期、工事䞭の道路、枋滞しおいる道路ず枋滞床、癜虎運送の倉庫(始点)ず目的地(終点)の䜍眮を入力ずし、始点から終点たでの最短の時間を出力するプログラムを䜜成しお䞋さい。 図のように、東西の方向の道路はa、b、c、... ず英小文字で名前が付けられ 、南北の方向の道路は 1、2、3、... ず敎数で名前が付けられおいたす。垂内の亀差点は、これらの英小文字ず敎数の組み合わせ H - V で指定されたす。 䟋えば、垂内の最北西の亀差点は a-1 で指定されたす。各信号は呚期 k をもち、 k 分ごずに切り替わりたす。東西が青ならば南北が赀で、南北が青ならば東西が赀です。黄色のシグナルは存圚したせん。トラックは二぀の亀差点を結ぶ道路を移動するのに D 分芁したすが、その道路が枋滞しおいる堎合はさらに d 分の時間を芁したす。トラックは道路が工事䞭の堎合は移動できたせん。 たた、亀差点に到達した時刻に、信号が赀の堎合には進入できたせん。トラックは亀差点でのみ、東、西、南、北に方向を倉えるこずができたすが、来た方向ぞは移動(Uタヌン)できたせん。道路は2方通行であり、トラックが行き来する時間、工事状況、枋滞床は2方向共通です。 初期状態ずしお、トラックは東を向いおいお、トラックが倉庫を出発する瞬間すべおの信号機が南北の方向に青に切り替わるものずしたす。たた、トラックは 100分以内で目的地に到達できるものずしたす。 Input 耇数のデヌタセットの䞊びが入力ずしお䞎えられたす。入力の終わりはれロふた぀の行で瀺されたす。各デヌタセットは以䞋の圢匏で䞎えられたす。 M N D ns H 1 - V 1 k 1 H 2 - V 2 k 2 : H ns - V ns k ns nc H 11 - V 11 H 12 - V 12 H 21 - V 21 H 22 - V 22 : H nc1 - V nc1 H nc2 - V nc2 nj H 11 - V 11 H 12 - V 12 d 1 H 21 - V 21 H 22 - V 22 d 2 : H nj1 - V nj1 H nj2 - V nj2 d nj H s - V s H d - V d 行目に道路の本数 M , N (2 ≀ M, N ≀ 20) が䞎えられたす。行目に、二぀の亀差点を結ぶ道路を移動するのに芁する時間 D (1 ≀ D ≀ D, 敎数) が䞎えられたす。 行目に信号機の数 ns が䞎えられたす。続く ns 行に、 i 個目の信号機の䜍眮を衚す英小文字ず敎数の組 H i - V i ず呚期 k (1 ≀ k ≀ 100) が䞎えられたす。 続く行に、工事䞭の道路の数 nc が䞎えられたす。続く nc 行に、 i 個目の工事䞭の道路の぀の端点亀差点を衚す英小文字ず敎数の組 H i1 - V i1 H i2 - V i2 が䞎えられたす。 続く行に、枋滞道路の数 nj が䞎えられたす。続く nj 行に、 i 個目の枋滞道路の぀の端点亀差点を衚す英小文字ず敎数の組 H i1 - V i1 H i2 - V i2 ず時間 d i (1 ≀ d i ≀ 100) が䞎えられたす。 最埌の行に、始点の亀差点 H s - V d ず終点の亀差点 H d - V d が䞎えられたす。 デヌタセットの数は 20 を超えたせん。 Output デヌタセット毎に最短時間を行に出力したす。 Sample Input 4 5 1 3 b-2 3 c-3 2 c-4 1 3 a-2 b-2 b-3 c-3 d-3 d-4 2 b-3 b-4 1 c-1 d-1 1 d-1 b-4 4 5 1 3 b-2 3 c-3 2 c-4 1 3 a-2 b-2 b-3 c-3 d-3 d-4 2 b-3 b-4 1 c-1 d-1 1 d-2 b-4 4 5 1 3 b-2 3 c-3 2 c-4 1 3 a-2 b-2 b-3 c-3 d-3 d-4 2 b-3 b-4 1 c-1 d-1 1 d-3 b-4 0 0 Output for the Sample Input 7 4 8
38,636
Lower Bound For a given sequence $A = \{a_0, a_1, ..., a_{n-1}\}$ which is sorted by ascending order, find the lower bound for a specific value $k$ given as a query. lower bound: the place pointing to the first element greater than or equal to a specific value, or $n$ if there is no such element. Input The input is given in the following format. $n$ $a_0 \; a_1 \; ,..., \; a_{n-1}$ $q$ $k_1$ $k_2$ : $k_q$ The number of elements $n$ and each element $a_i$ are given in the first line and the second line respectively. In the third line, the number of queries $q$ is given and the following $q$ lines, $q$ integers $k_i$ are given as queries. Output For each query, print the position $i$ ($i = 0, 1, ..., n$) of the lower bound in a line. Constraints $1 \leq n \leq 100,000$ $1 \leq q \leq 200,000$ $0 \leq a_0 \leq a_1 \leq ... \leq a_{n-1} \leq 1,000,000,000$ $0 \leq k_i \leq 1,000,000,000$ Sample Input 1 4 1 2 2 4 3 2 3 5 Sample Output 1 1 3 4
38,637
H: 板 問題 $R * C$ のマスが䞎えられたす各マスは䜕もないマスか穎が開いおいるマスのどちらかです 䞎えられるマスは以䞋の条件を満たしたす。 穎の開いたマス同士は連結である穎の開いたマスを十字方向に぀たっお任意の穎の開いたマスに移動するこずができる 䜕もないマス同士は連結である あなたは幅が $1$ の任意長の長方圢型のタむルを生成するこずができたす このタむルを耇数枚蚭眮しお党おの穎のあるマスを埋めたいですタむルを蚭眮するずき以䞋の制玄を守る必芁がありたす タむルは瞊向きか暪向きの $2$ 方向でのみ蚭眮が可胜である 䞀぀のマスに二枚以䞊のタむルが重なるように蚭眮しおはいけない 穎のないマスの䞊にタむルがあっおはいけない 䞊蚘の制玄を守っお党おの穎のあるマスをタむルで埋めたずきのタむルの最小枚数を答えおください 制玄 $1 \leq R, C \leq 25$ $|S_i| = C \ \ \ \ |S_i|$ は文字列の長さを衚す。 $S_{i,j}$ は '#' たたは '.' で、それぞれ穎の開いたマス、䜕もないマスを衚す。 入力圢匏 入力は以䞋の圢匏で䞎えられる。 $R\ C$ $S_{1,1} \dots S_{1,C}$ $\vdots$ $S_{R,1} \dots S_{R,C}$ 出力 最小回数を出力しおください。末尟に改行も出力しおください。 サンプル サンプル入力 1 5 5 ..... .#.#. .###. .#.#. ..... サンプル出力 1 3 $3$ 枚のタむルを以䞋のように眮くのが最適です ..... .1.3. .123 .1.3 ..... サンプル入力 2 4 10 ########## ....#..... ....#..... .......... サンプル出力 2 2 $2$ 枚のタむルを以䞋のように眮くのが最適ですタむルの長さは任意長にでき瞊向きにも暪向きにも䜿えるこずに泚意しおください 1111111111 ....2..... ....2..... ..........
38,638
Koto Municipal Subway Koto垂営地䞋鉄 Koto垂は䞋図のように道路が碁盀の目状である有名な街である 南北に䌞びる道ず東西に䌞びる道はそれぞれ1kmの間隔で䞊んでいる Koto垂の最も南西の亀差点にあるKoto駅を (0, 0) ずしおそこから東に x km北に y km 進んだ䜍眮を ( x , y ) ず蚘すこずずする (0 ≀ x , y である) 5幎埌に開かれるオリンピックにより芳光客が増倧するこずを芋越しお垂はKoto駅を始発駅ずする新しい地䞋鉄の路線を建蚭するこずを決めた 珟圚Koto駅の次の駅ずしお新しく建蚭されるShin-Koto駅たでのレヌルを敷く蚈画を立おおいる レヌルはKoto駅からShin-Koto駅に向かっおたっすぐ敷く 埓っおShin-Koto駅の堎所を ( x , y ) ずしたずきレヌルの長さは √( x 2 + y 2 ) である レヌルを敷くための費甚は敷いたレヌルの長さ分だけ必芁になる レヌルの長さが1.5kmのように小数であっおも費甚も同じように1.5必芁ずなる Shin-Koto駅の堎所 ( x , y ) はただ決定しおおらず以䞋の条件を満たす堎所にする予定である 亀差点である぀たり x ず y がそれぞれ敎数である Koto駅から道路に沿っお歩いた最短距離がちょうど D である぀たり x + y = D を満たす 䞊の2぀の条件を満たす䞭で垂が定めるレヌルの予算 E ずレヌルの費甚ずのずれ | √( x 2 + y 2 ) - E | が最小ずなるようにShin-Koto駅の堎所を遞ぶ ここで | A | は A の絶察倀を衚す あなたの仕事は䞊蚘のようにShin-Koto駅を建蚭したずきのレヌルを敷くための費甚ず予算ずのずれを出力するプログラムを䜜成するこずである Input 入力は耇数のデヌタセットから構成され1぀の入力に含たれるデヌタセットの数は100以䞋である 各デヌタセットの圢匏は次の通りである D E D (1 ≀ D ≀ 100)はKoto駅からShin-Koto駅たで道路に沿っお歩いたずきの最短距離を衚す敎数である E (1 ≀ E ≀ 100)はレヌル建蚭のための予算を衚す敎数である 入力の終わりは空癜で区切られた2぀のれロからなる行によっお瀺される Output 各デヌタセットに察しお問題の条件を満たすようにレヌルを敷いたずきの費甚ず予算ずのずれを1行で出力せよ 答えには 10 -3 を越える絶察誀差があっおはならない 各行の終わりに改行を出力しなかった堎合や䞍必芁な文字を出力した堎合誀答ず刀断されおしたうため泚意するこず Sample Input 2 1 7 5 7 6 7 7 76 5 8 41 0 0 Output for Sample Input 0.4142135624 0 0.0827625303 0 48.7401153702 33 Hint 1぀目のデヌタセットでは䞋図のようにKoto駅から道路に沿っお2km進んだ亀差点がShin-Koto駅を建蚭する堎所の候補ずなる 各亀差点にShin-Koto駅を建蚭した堎合のレヌルを敷くための費甚ず予算1ずのずれは次のようになる (2, 0)  | √(2 2 + 0 2 ) - 1 | = 1.0 (1, 1)  | √(1 2 + 1 2 ) - 1 | = 0.4142135623... (0, 2)  | √(0 2 + 2 2 ) - 1 | = 1.0 よっお費甚ず予算ずのずれが最小になるのは (1, 1) に建蚭した堎合ずなる
38,639
Problem A: Hanafuda Shuffle There are a number of ways to shuffle a deck of cards. Hanafuda shuffling for Japanese card game 'Hanafuda' is one such example. The following is how to perform Hanafuda shuffling. There is a deck of n cards. Starting from the p -th card from the top of the deck, c cards are pulled out and put on the top of the deck, as shown in Figure 1. This operation, called a cutting operation, is repeated. Write a program that simulates Hanafuda shuffling and answers which card will be finally placed on the top of the deck. Figure 1: Cutting operation Input The input consists of multiple data sets. Each data set starts with a line containing two positive integers n (1 <= n <= 50) and r (1 <= r <= 50); n and r are the number of cards in the deck and the number of cutting operations, respectively. There are r more lines in the data set, each of which represents a cutting operation. These cutting operations are performed in the listed order. Each line contains two positive integers p and c ( p + c <= n + 1). Starting from the p -th card from the top of the deck, c cards should be pulled out and put on the top. The end of the input is indicated by a line which contains two zeros. Each input line contains exactly two integers separated by a space character. There are no other characters in the line. Output For each data set in the input, your program should write the number of the top card after the shuffle. Assume that at the beginning the cards are numbered from 1 to n , from the bottom to the top. Each number should be written in a separate line without any superfluous characters such as leading or following spaces. Sample Input 5 2 3 1 3 1 10 3 1 10 10 1 8 3 0 0 Output for the Sample Input 4 4
38,640
長方圢 アむヅ攟送協䌚の教育番組(教育)では、子ども向けの工䜜番組「あそんで぀くろ」を攟送しおいたす。今日は棒で長方圢を䜜る回ですが、甚意した本の棒を䜿っお長方圢ができるかを確かめたいず思いたす。ただし、棒は切ったり折ったりしおはいけたせん。 本の棒の長さが䞎えられるので、それらすべおを蟺ずする長方圢が䜜れるかどうか刀定するプログラムを䜜成せよ。 Input 入力は以䞋の圢匏で䞎えられる。 e 1 e 2 e 3 e 4 入力は行からなり、各棒の長さを衚す敎数 e i (1 ≀ e i ≀ 100) が䞎えられる。 Output 長方圢を䜜成できる堎合には「yes」を、䜜成できない堎合には「no」を出力する。ただし、正方圢は長方圢の䞀皮なので、正方圢の堎合でも「yes」ず出力する。 Sample Input 1 1 1 3 4 Sample Output 1 no Sample Input 2 1 1 2 2 Sample Output 2 yes Sample Input 3 2 1 1 2 Sample Output 3 yes Sample Input 4 4 4 4 10 Sample Output 4 no
38,641
Score : 800 points Problem Statement There is a grid with R rows and C columns. We call the cell in the r -th row and c -th column ( rc ). Mr. Takahashi wrote non-negative integers into N of the cells, that is, he wrote a non-negative integer a_i into ( r_ic_i ) for each i ( 1≀i≀N ). After that he fell asleep. Mr. Aoki found the grid and tries to surprise Mr. Takahashi by writing integers into all remaining cells. The grid must meet the following conditions to really surprise Mr. Takahashi. Condition 1 : Each cell contains a non-negative integer. Condition 2 : For any 2×2 square formed by cells on the grid, the sum of the top left and bottom right integers must always equal to the sum of the top right and bottom left integers. Determine whether it is possible to meet those conditions by properly writing integers into all remaining cells. Constraints 2≀RC≀10^5 1≀N≀10^5 1≀r_i≀R 1≀c_i≀C (r_ic_i) ≠ (r_jc_j) ( i≠j ) a_i is an integer. 0≀a_i≀10^9 Input The input is given from Standard Input in the following format: R C N r_1 c_1 a_1 r_2 c_2 a_2 : r_N c_N a_N Output Print Yes if it is possible to meet the conditions by properly writing integers into all remaining cells. Otherwise, print No . Sample Input 1 2 2 3 1 1 0 1 2 10 2 1 20 Sample Output 1 Yes You can write integers as follows. Sample Input 2 2 3 5 1 1 0 1 2 10 1 3 20 2 1 30 2 3 40 Sample Output 2 No There are two 2×2 squares on the grid, formed by the following cells: Cells (11) , (12) , (21) and (22) Cells (12) , (13) , (22) and (23) You have to write 40 into the empty cell to meet the condition on the left square, but then it does not satisfy the condition on the right square. Sample Input 3 2 2 3 1 1 20 1 2 10 2 1 0 Sample Output 3 No You have to write -10 into the empty cell to meet condition 2 , but then it does not satisfy condition 1 . Sample Input 4 3 3 4 1 1 0 1 3 10 3 1 10 3 3 20 Sample Output 4 Yes You can write integers as follows. Sample Input 5 2 2 4 1 1 0 1 2 10 2 1 30 2 2 20 Sample Output 5 No All cells already contain a integer and condition 2 is not satisfied.
38,642
Score : 100 points Problem Statement AtCoder Inc. holds a contest every Saturday. There are two types of contests called ABC and ARC, and just one of them is held at a time. The company holds these two types of contests alternately: an ARC follows an ABC and vice versa. Given a string S representing the type of the contest held last week, print the string representing the type of the contest held this week. Constraints S is ABC or ARC . Input Input is given from Standard Input in the following format: S Output Print the string representing the type of the contest held this week. Sample Input 1 ABC Sample Output 1 ARC They held an ABC last week, so they will hold an ARC this week.
38,643
Taro's Shopping Mammy decided to give Taro his first shopping experience. Mammy tells him to choose any two items he wants from those listed in the shopping catalogue, but Taro cannot decide which two, as all the items look attractive. Thus he plans to buy the pair of two items with the highest price sum, not exceeding the amount Mammy allows. As getting two of the same item is boring, he wants two different items. You are asked to help Taro select the two items. The price list for all of the items is given. Among pairs of two items in the list, find the pair with the highest price sum not exceeding the allowed amount, and report the sum. Taro is buying two items, not one, nor three, nor more. Note that, two or more items in the list may be priced equally. Input The input consists of multiple datasets, each in the following format. n m a 1 a 2 ... a n A dataset consists of two lines. In the first line, the number of items n and the maximum payment allowed m are given. n is an integer satisfying 2 ≀ n ≀ 1000. m is an integer satisfying 2 ≀ m ≀ 2,000,000. In the second line, prices of n items are given. a i (1 ≀ i ≀ n ) is the price of the i -th item. This value is an integer greater than or equal to 1 and less than or equal to 1,000,000. The end of the input is indicated by a line containing two zeros. The sum of n 's of all the datasets does not exceed 50,000. Output For each dataset, find the pair with the highest price sum not exceeding the allowed amount m and output the sum in a line. If the price sum of every pair of items exceeds m , output NONE instead. Sample Input 3 45 10 20 30 6 10 1 2 5 8 9 11 7 100 11 34 83 47 59 29 70 4 100 80 70 60 50 4 20 10 5 10 16 0 0 Output for the Sample Input 40 10 99 NONE 20
38,644
問題名 DNA 遺䌝子は、 A , T , G , C からなる文字列です。 この䞖界の遺䌝子は奇劙なこずに、ある構文芏則に埓うこずが知られおいたす。 構文芏則は、次のような圢で䞎えられたす。 非終端蚘号1: 蚘号1_1 蚘号1_2 ... 蚘号1_n1 非終端蚘号2: 蚘号2_1 蚘号2_2 ... 蚘号2_n2 ... 非終端蚘号m: 蚘号m_1 蚘号m_2 ... 蚘号m_nm 蚘号は非終端蚘号たたは終端蚘号のどちらかです。 非終端蚘号は小文字文字列で衚され、終端蚘号は A , T , G , C のうちのいく぀かの文字が、" [ "ず" ] "に囲たれた文字列で衚されたす。 構文芏則の䟋は次のようになりたす。 dna: a a b b a: [AT] b: [GC] " 非終端蚘号i: 蚘号i_1 蚘号i_2 ... 蚘号i_ni " を非終端蚘号 i のルヌルず呌び、ルヌルは、構文芏則に珟れる各非終端蚘号に察しお、ちょうど 1 ぀づ぀存圚したす。 文字列 s が非終端蚘号 i に「 マッチする 」ずは、 s = s 1 + s 2 + ... + s ni ずなるような s の郚分文字列 {s j } が存圚し、 s j ( 1 ≀ j ≀ n i )がルヌル内の蚘号 j にマッチするこずをいいたす。 文字列 s が終端蚘号に「 マッチする 」ずは、文字列が 1 文字からなり、その文字が終端蚘号を衚す文字列に含たれるこずをいいたす。 文字列が構文芏則に埓うずは、非終端蚘号 1 にその文字列がマッチするこずをいいたす。 ルヌル i は、蚘号のうちに、非終端蚘号 j ( j ≀ i ) を含みたせん。 構文芏則ず、4぀の敎数 Na , Nt , Ng , Nc が䞎えられたす。 構文芏則に埓い、A をちょうど Na 個、T をちょうど Nt 個、G をちょうど Ng 個、C をちょうど Nc 個含むような遺䌝子の総数を 1,000,000,007 で割った䜙りを求めなさい。 Input Na Nt Ng Nc m 非終端蚘号1: 蚘号 1 1 蚘号 1 2 ... 蚘号 1 n1 非終端蚘号2: 蚘号 2 1 蚘号 2 2 ... 蚘号 2 n2 ... 非終端蚘号 m : 蚘号 m 1 蚘号 m 2 ... 蚘号 m nm 0 ≀ Na, Nt, Ng, Nc ≀ 50 1 ≀ m ≀ 50 1 ≀ ni ≀ 10 1 ≀ 蚘号を衚す文字列の長さ ≀ 20 (※蚘号にマッチする文字列の長さではないこずに泚意) Output 総数を 1,000,000,007 で割った䜙り Sample Input 1 1 0 1 0 3 dna: a b a: [AT] b: [GC] Output for the Sample Input 1 1 "AG"の䞀぀です。 Sample Input 2 1 1 1 2 1 k: [ATG] [ATG] [ATG] [C] [C] Output for the Sample Input 2 6 "ATGCC", "AGTCC", "TAGCC", "TGACC", "GATCC", "GTACC"の6぀です。 Sample Input 3 3 1 1 1 3 inv: at b b b at: [ATG] b b: [C] Output for the Sample Input 3 0
38,645
Score : 700 points Problem Statement There are N stones arranged in a row. The i -th stone from the left is painted in the color C_i . Snuke will perform the following operation zero or more times: Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones. Find the number of possible final sequences of colors of the stones, modulo 10^9+7 . Constraints 1 \leq N \leq 2\times 10^5 1 \leq C_i \leq 2\times 10^5(1\leq i\leq N) All values in input are integers. Input Input is given from Standard Input in the following format: N C_1 : C_N Output Print the number of possible final sequences of colors of the stones, modulo 10^9+7 . Sample Input 1 5 1 2 1 2 2 Sample Output 1 3 We can make three sequences of colors of stones, as follows: (1,2,1,2,2) , by doing nothing. (1,1,1,2,2) , by choosing the first and third stones to perform the operation. (1,2,2,2,2) , by choosing the second and fourth stones to perform the operation. Sample Input 2 6 4 2 5 4 2 4 Sample Output 2 5 Sample Input 3 7 1 3 1 2 3 3 2 Sample Output 3 5
38,646
Problem H Puzzle and Hexagons Backgorund 超倧人気ゲヌム「パズル&ヘキサゎンズ」が぀いにリリヌスされた。このゲヌムは超面癜すぎおドハマりする人が続出。あたりの熱䞭床に医垫から䞭毒認定を受ける者も続出した。䞖界各囜の有志達はこのゲヌムの䞭毒者達を助けるために「パズル&ヘキサゎンズ」のシミュレヌタを䜜り、危険な実機でのプレむを避けるよう促そうずした。あなたにはシミュレヌタ䜜りに協力しお欲しい。 Problem 正六角圢のマスを瞊に H 個、暪に W 個敷き詰めた盀面が䞎えられる。 Fig.1は H =4, W =7の時の盀面ずそれに察応するマスの座暙( x , y )を瀺す。 Fig.1 初期状態で各マスには色の぀いたブロックが存圚する。 ブロックの色は以䞋のようにアルファベット䞀文字で衚珟される。 'R' ・・・赀 'G' ・・・緑 'B' ・・・青 'P' ・・・玫 'Y' ・・・黄 'E' ・・・氎 次に操䜜の数 Q が䞎えられる。 各操䜜では回転の䞭心座暙( x , y )が䞎えられ、そのマスの呚囲にある6぀のブロックを時蚈回りに䞀぀回転させるこずを瀺す。(Fig.2 参照)。 このずき、ブロックが存圚しないマスも空のブロックが存圚するず考えお時蚈回りに䞀぀回転させる。 ただし、指定された座暙ずその呚蟺の぀のマスの内いずれか䞀぀でも H × W の盀面の䞭に存圚しおいない堎合は回転を行わない。 Fig.2 次に以䞋の凊理ができなくなるたで繰り返す。 Fig.3においお、ブロックAの䜍眮からB, C, Dの䜍眮のいずれのマスにもブロックが存圚しないずき、ブロックAはCの䜍眮に萜䞋する。マスB, Dが存圚しない堎合はブロックも存圚しないず考え、マスCが存圚しない堎合は萜䞋の凊理を行わない。 1の凊理が可胜なブロックが存圚する堎合は1に戻る。 同じ色のブロックが3぀以䞊繋がっおいる堎合、そのブロックは党お消滅する。 2぀のブロックが繋がるずはマスの䞀蟺を共有するこずである。 泚意この䞀連の凊理は、操䜜が䞀぀も䞎えられおいない状態初期状態でも行われる。 Fig.3 党おの操䜜を実行した埌の最終的な盀面を出力せよ。 Input 入力は以䞋の圢匏で䞎えられる。 H W F 0, H−1 F 1, H−1 
 F W−1, H−1 F 0, H−2 F 1, H−2 
 F W−1, H−2 . . . F 0, 0 F 1, 0 
 F W−1, 0 Q x 0 y 0 x 1 y 1 . . . x Q−1 y Q−1 1行目に、盀面の瞊ず暪のサむズを衚す2぀の敎数 H ず W が䞎えられる。 2行目から H +1行目に、各添字に察応する盀面の色を衚す文字列が䞎えられる。 H +2行目に、操䜜の数 Q が䞎えられる。 続く Q 行に回転の䞭心のマスの座暙を衚す x ず y が䞎えられる。 Constraints 3 ≀ H ≀ 50 3 ≀ W ≀ 50 0 ≀ x < W 0 ≀ y < H 1 ≀ Q ≀ 100 F i, j ( 0 ≀ i < W , 0 ≀ j < H ) は'R','G','B','P','Y','E'のいずれかである。 Output 党おの操䜜を行った埌の盀面を H 行で出力せよ。 ただし、ブロックが無いマスは'.'で衚すこず。 Sample Input1 3 3 RGR RBP YEB 1 1 1 Sample Output1 
 YBG EBP Fig.4はSample Input1における状態の遷移を衚したものである。 Fig.4 Sample Input2 4 5 BYYGG RRRRR RRBRR YYGGB 2 3 1 3 1 Sample Output2 ..... ..... ..... B.BGB Sample Input3 4 4 BEEP ERYY BBRP RBYP 1 1 2 Sample Output3 .... .... .... .B.. 盀面の初期状態ですでに消えるブロックがあるこず泚意。 䞡端にあるブロックの萜䞋凊理に泚意。
38,648
Score : 100 points Problem Statement You are given three integers A, B and C . Determine if there exists an equilateral triangle whose sides have lengths A, B and C . Constraints All values in input are integers. 1 \leq A,B,C \leq 100 Input Input is given from Standard Input in the following format: A B C Output If there exists an equilateral triangle whose sides have lengths A, B and C , print Yes ; otherwise, print No . Sample Input 1 2 2 2 Sample Output 1 Yes There exists an equilateral triangle whose sides have lengths 2, 2 and 2 . Sample Input 2 3 4 5 Sample Output 2 No There is no equilateral triangle whose sides have lengths 3, 4 and 5 .
38,649
H: Rectangular Stamps ICPC で良い成瞟を収めるには修行が欠かせないうさぎは ICPC で勝ちたいので今日も修行をするこずにした 今日の修行は絵を描くこずによっお創造力を高めようずいうものである四角いスタンプを甚いお䞊手く暡様を描こう 倧小さたざたなスタンプを䜿い4 × 4 のマス目の玙に指定された赀・緑・青の通りの絵を完成させたいスタンプは長方圢でありマス目にぎったり合わせお䜿うスタンプの瞊ず暪を入れ替えるこずはできない 玙は最初色が塗られおいない状態にある玙にスタンプを抌すず抌された郚分がスタンプの色に倉わり䞋に隠れた色は党く芋えなくなるスタンプの色は付けるむンクにより決定されるのでどのスタンプでも奜きな色を遞ぶこずが可胜であるスタンプは玙から䞀郚がはみ出た状態で抌すこずも可胜でありはみ出た郚分は無芖される 1 ぀のスタンプを耇数回䜿うこずは可胜である同じスタンプを別の色に察しお䜿っおもよいスタンプを抌すのはやや神経を䜿う䜜業なので出来るだけスタンプを抌す回数を少なくしたい Input N H 1 W 1 ... H N W N C 1,1 C 1,2 C 1,3 C 1,4 C 2,1 C 2,2 C 2,3 C 2,4 C 3,1 C 3,2 C 3,3 C 3,4 C 4,1 C 4,2 C 4,3 C 4,4 N はスタンプの個数 H i , W i (1 ≀ i ≀ N ) はそれぞれ i 番目のスタンプの瞊の長さ暪の長さを衚す敎数である C i , j (1 ≀ i ≀ 41 ≀ j ≀ 4) は䞊から i 行目巊から j 列目のマスに぀いお指定された絵の色を衚す文字である赀は R 緑は G 青は B で衚される 1 ≀ N ≀ 161 ≀ H i ≀ 41 ≀ W i ≀ 4 を満たす( H i , W i ) ずしお同䞀の組は耇数回珟れない Output 絵を完成させるためにスタンプを抌さなければならない最小の回数を 1 行に出力せよ Sample Input 1 2 4 4 1 1 RRRR RRGR RBRR RRRR Sample Output 1 3 Sample Input 2 1 2 3 RRGG BRGG BRRR BRRR Sample Output 2 5
38,650
Score : 900 points Problem Statement Takahashi has an N \times M grid, with N horizontal rows and M vertical columns. Determine if we can place A 1 \times 2 tiles ( 1 vertical, 2 horizontal) and B 2 \times 1 tiles ( 2 vertical, 1 horizontal) satisfying the following conditions, and construct one arrangement of the tiles if it is possible: All the tiles must be placed on the grid. Tiles must not stick out of the grid, and no two different tiles may intersect. Neither the grid nor the tiles may be rotated. Every tile completely covers exactly two squares. Constraints 1 \leq N,M \leq 1000 0 \leq A,B \leq 500000 N , M , A and B are integers. Input Input is given from Standard Input in the following format: N M A B Output If it is impossible to place all the tiles, print NO . Otherwise, print the following: YES c_{11}...c_{1M} : c_{N1}...c_{NM} Here, c_{ij} must be one of the following characters: . , < , > , ^ and v . Represent an arrangement by using each of these characters as follows: When c_{ij} is . , it indicates that the square at the i -th row and j -th column is empty; When c_{ij} is < , it indicates that the square at the i -th row and j -th column is covered by the left half of a 1 \times 2 tile; When c_{ij} is > , it indicates that the square at the i -th row and j -th column is covered by the right half of a 1 \times 2 tile; When c_{ij} is ^ , it indicates that the square at the i -th row and j -th column is covered by the top half of a 2 \times 1 tile; When c_{ij} is v , it indicates that the square at the i -th row and j -th column is covered by the bottom half of a 2 \times 1 tile. Sample Input 1 3 4 4 2 Sample Output 1 YES <><> ^<>^ v<>v This is one example of a way to place four 1 \times 2 tiles and three 2 \times 1 tiles on a 3 \times 4 grid. Sample Input 2 4 5 5 3 Sample Output 2 YES <>..^ ^.<>v v<>.^ <><>v Sample Input 3 7 9 20 20 Sample Output 3 NO
38,651
Range Write a program which reads three integers a , b and c , and prints "Yes" if a < b < c , otherwise "No". Input Three integers a , b and c separated by a single space are given in a line. Output Print "Yes" or "No" in a line. Constraints 0 ≀ a , b , c ≀ 100 Sample Input 1 1 3 8 Sample Output 1 Yes Sample Input 2 3 8 1 Sample Output 2 No
38,652
Ninja Map Intersections of Crossing Path City are aligned to a grid. There are $N$ east-west streets which are numbered from 1 to $N$, from north to south. There are also $N$ north-south streets which are numbered from 1 to $N$, from west to east. Every pair of east-west and north-south streets has an intersection; therefore there are $N^2$ intersections which are numbered from 1 to $N^2$. Surprisingly, all of the residents in the city are Ninja. To prevent outsiders from knowing their locations, the numbering of intersections is shuffled. You know the connections between the intersections and try to deduce their positions from the information. If there are more than one possible set of positions, you can output any of them. Input The input consists of a single test case formatted as follows. $N$ $a_1$ $b_1$ ... $a_{2N^2−2N}$ $\;$ $b_{2N^2−2N}$ The first line consists of an integer $N$ ($2 \leq N \leq 100$). The following $2N^2 - 2N$ lines represent connections between intersections. The ($i+1$)-th line consists of two integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq N^2, a_i \ne b_i$), which represent that the $a_i$-th and $b_i$-th intersections are adjacent. More precisely, let's denote by ($r, c$) the intersection of the $r$-th east-west street and the $c$-th north-south street. If the intersection number of ($r,c$) is $a_i$ for some $r$ and $c$, then the intersection number of either ($r-1, c$), ($r+1, c$), ($r, c-1$) or ($r, c+1$) must be $b_i$. All inputs of adjacencies are different, i.e., ($a_i, b_i$) $\ne$ ($a_j, b_j$) and ($a_i, b_i$) $\ne$ ($b_j, a_j$) for all $1 \leq i < j \leq 2N^2-2N$. This means that you are given information of all adjacencies on the grid. The input is guaranteed to describe a valid map. Output Print a possible set of positions of the intersections. More precisely, the output consists of $N$ lines each of which has space-separated $N$ integers. The $c$-th integer of the $r$-th line should be the intersection number of ($r, c$). If there are more than one possible set of positions, you can output any of them. Sample Input 1 3 1 2 4 7 8 6 2 3 8 9 5 3 4 6 5 6 7 8 1 4 2 6 5 9 Output for Sample Input 1 7 4 1 8 6 2 9 5 3 The following output will also be accepted. 1 2 3 4 6 5 7 8 9 Sample Input 2 4 12 1 3 8 10 7 13 14 8 2 9 12 6 14 11 3 3 13 1 10 11 15 4 15 4 9 14 10 5 7 2 5 6 1 14 5 16 11 15 6 15 13 9 6 16 4 13 2 Output for Sample Input 2 8 2 5 7 3 13 14 10 11 15 6 1 16 4 9 12
38,653
Score : 1000 points Problem Statement Takahashi and Snuke came up with a game that uses a number sequence, as follows: Prepare a sequence of length M consisting of integers between 0 and 2^N-1 (inclusive): a = a_1, a_2, \ldots, a_M . Snuke first does the operation below as many times as he likes: Choose a positive integer d , and for each i (1 \leq i \leq M) , in binary, set the d -th least significant bit of a_i to 0 . (Here the least significant bit is considered the 1 -st least significant bit.) After Snuke finishes doing operations, Takahashi tries to sort a in ascending order by doing the operation below some number of times. Here a is said to be in ascending order when a_i \leq a_{i + 1} for all i (1 \leq i \leq M - 1) . Choose two adjacent elements of a : a_i and a_{i + 1} . If, in binary, these numbers differ in exactly one bit, swap a_i and a_{i + 1} . There are 2^{NM} different sequences of length M consisting of integers between 0 and 2^N-1 that can be used in the game. How many among them have the following property: if used in the game, there is always a way for Takahashi to sort the sequence in ascending order regardless of Snuke's operations? Find the count modulo (10^9 + 7) . Constraints All values in input are integers. 1 \leq N \leq 5000 2 \leq M \leq 5000 Input Input is given from Standard Input in the following format: N M Output Print the number, modulo (10^9 + 7) , of sequences with the property: if used in the game, there is always a way for Takahashi to sort the sequence in ascending order regardless of Snuke's operations. Sample Input 1 2 5 Sample Output 1 352 Consider the case a = 1, 3, 1, 3, 1 for example. When the least significant bit of each element of a is set to 0 , a = 0, 2, 0, 2, 0 ; When the second least significant bit of each element of a is set to 0 , a = 1, 1, 1, 1, 1 ; When the least two significant bits of each element of a are set to 0 , a = 0, 0, 0, 0, 0 . In all of the cases above and the case when Snuke does no operation to change a , we can sort the sequence by repeatedly swapping adjacent elements that differ in exactly one bit. Thus, this sequence has the property: if used in the game, there is always a way for Takahashi to sort the sequence in ascending order regardless of Snuke's operations. Sample Input 2 2020 530 Sample Output 2 823277409
38,654
Problem I: FIMO sequence Your task is to simulate the sequence defined in the remaining part of the problem description. This sequence is empty at first. i -th element of this sequence is expressed as a i . The first element of this sequence is a 1 if the sequence is not empty. The operation is given by integer from 0 to 9. The operation is described below. 0: This query is given with some integer x . If this query is given, the integer x is inserted into the sequence. If the sequence is empty, a 1 = x . If the sequence has n elements, a n+1 = x . Same integer will not appear more than once as x . 1: If this query is given, one element in the sequence is deleted. The value in the middle of the sequence is deleted. If the sequence has n elements and n is even, a n/2 will be deleted. If n is odd, a ⌈n/2⌉ will be deleted. This query is not given when the sequence is empty. Assume that the sequence has a 1 =1, a 2 =2, a 3 =3, a 4 =4 and a 5 =5. In this case, a 3 will be deleted. After deletion, the sequence will be a 1 =1, a 2 =2, a 3 =4, a 4 =5. Assume that the sequence has a 1 =1, a 2 =2, a 3 =3 and a 4 =4, In this case, a 2 will be deleted. After deletion, the sequence will be a 1 =1, a 2 =3, a 3 =4. 2: The first half of the sequence is defined by the index from 1 to ⌈ n /2⌉ . If this query is given, you should compute the minimum element of the first half of the sequence. This query is not given when the sequence is empty. Let me show an example. Assume that the sequence is {6,2,3,4,5,1,8}. In this case, the minimum element of the first half of the sequence, {6,2,3,4} is 2. 3: The latter half of the sequence is elements that do not belong to the first half of the sequence. If this query is given, you should compute the minimum element of the latter half of the sequence. This query is not given when the sequence is empty. Let me show an example. Assume that the sequence is {6,2,3,4,5,1,8}. In this case the answer for this query is 1 from {5,1,8}. 4: This query is given with an integer i . Assume that deletion is repeated until the sequence is empty. Some elements in the first half of the sequence will become the answer for query 2. You should compute the i -th minimum element from the answers. This query is not given when the sequence is empty. You can assume that i -th minimum element exists when this query is given. Let me show an example. Assume that deletion will be repeated to the sequence {6,2,3,4,5,1,8}. {6,2,3,4,5,1,8} The minimum element in the first half of the sequence is 2. {6,2,3,5,1,8} The minimum element in the first half of the sequence is 2. {6,2,5,1,8} The minimum element in the first half of the sequence is 2. {6,2,1,8} The minimum element in the first half of the sequence is 2. {6,1,8} The minimum element in the first half of the sequence is 1. {6,8} The minimum element in the first half of the sequence is 6. {8} The minimum element in the first half of the sequence is 8. {} The first half of the sequence is empty. For the initial state, {6,2,3,4} is the first half of the sequence. 2 and 6 become the minimum element of the first half of the sequence. In this example, the 1-st minimum element is 2 and the 2-nd is 6. 5: This query is given with an integer i . Assume that deletion is repeated until the sequence is empty. Some elements in the latter half of the sequence will become the answer for query 3. You should compute the i -th minimum element from the answers. This query is not given when the sequence is empty. You can assume that i -th minimum element exists when this query is given. Let me show an example. Assume that deletion will be repeated to the sequence {6,2,3,4,5,1,8}. {6,2,3,4,5,1,8} The minimum elemets in the latter half of the sequence is 1. {6,2,3,5,1,8} The minimum elemets in the latter half of the sequence is 1. {6,2,5,1,8} The minimum elemets in the latter half of the sequence is 1. {6,2,1,8} The minimum elemets in the latter half of the sequence is 1. {6,1,8} The minimum elemets in the latter half of the sequence is 8. {6,8} The minimum elemets in the latter half of the sequence is 8. {8} The latter half of the sequence is empty. {} The latter half of the sequence is empty. For the initial state, {5,1,8} is the latter half of the sequence. 1 and 8 becomes the minimum element of the latter half ot the sequence. In this example, the 1-st minimum element is 1 and the 2-nd is 8. 6: If this query is given, you should compute the maximum element of the first half of the sequence. This query is not given when the sequence is empty. Let me show an example. Assume that the sequence is {1,3,2,5,9,6,7}. In this case, the maximum element of the first half of the sequence,{1,3,2,5}, is 5. 7: If this query is given, you should compute the maximum element of the latter half of the sequence. This query is not given when the sequence is empty. Let me show an example. Assume that the sequence is {1,3,2,5,9,6,7}. In this case, the maximum element of the latter half of the sequence,{9,6,7}, is 9. 8: This query is given with an integer i . Assume that deletion is repeated until the sequence is empty. Some elements in the first half of the sequence will become the answer for query 6. You should compute the i -th maximum element from the answers. This query is not given when the sequence is empty. You can assume that i -th maximum elements exists when this query is given. Let me show an example. Assume that deletion will be repeated to the sequence {1,3,2,5,9,6,7}. {1,3,2,5,9,6,7} The maximum element in the first half of the sequence is 5. {1,3,2,9,6,7} The maximum element in the first half of the sequence is 3. {1,3,9,6,7} The maximum element in the first half of the sequence is 9. {1,3,6,7} The maximum element in the first half of the sequence is 3. {1,6,7} The maximum element in the first half of the sequence is 6. {1,7} The maximum element in the first half of the sequence is 1. {7} The maximum element in the first half of the sequence is 7. {} The first half of the sequence is empty. For the initial state, {1,3,2,5} is the first half of the sequence. 1,3 and 5 becomes the maximum element of the first half of the sequence. In this example, the 1-st maximum element is 5, the 2-nd is 3 and the 3-rd is 1. 9: This query is given with an integer i . Assume that deletion is repeated until the sequence is empty. Some elements in the latter half of the sequence will become the answer for query 7. You should compute the i -th maximum element from the answers. This query is not given when the sequence is empty. You can assume that i -th maximum elements exists when this query is given. Let me show an example. Assume that deletion will be repeated to the sequence {1,3,2,5,9,6,7}. {1,3,2,5,9,6,7} The maximum element in the latter half of the sequence is 9. {1,3,2,9,6,7} The maximum element in the latter half of the sequence is 9. {1,3,9,6,7} The maximum element in the latter half of the sequence is 7. {1,3,6,7} The maximum element in the latter half of the sequence is 7. {1,6,7} The maximum element in the latter half of the sequence is 7. {1,7} The maximum element in the latter half of the sequence is 7. {7} The latter half of the sequence is empty. {} The latter half of the sequence is empty. For the initial state, {9,6,7} is the latter half of the sequence. 7 and 9 becomes the maximum element of the latter half of the sequence. In this example, the 1-st maximum element is 9 and the 2-nd is 7. Input Input consists of multiple test cases. The first line is the number of queries. Following q lines are queries. q query 0 ... query i ... qurey_ q-1 The sum of the number of queries in the input data is less than 200001. If query i = 0, 4, 5, 8, and 9 are consists of pair of integers. Other queries are given with a single integer. You can assume that the length of the sequence doesn't exceed 20000. Output If the query is 0, you don't output any numbers. If the query is 1, you should output the deleted number. For other queries, you should output the computed value. For each case, you should output "end" (without quates) after you process all queries. Sample input 5 0 1 0 2 0 3 0 4 1 6 0 1 0 2 0 3 0 4 0 5 1 31 0 6 0 2 0 3 0 4 0 5 0 1 0 8 4 1 4 2 5 1 5 2 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 1 32 0 1 0 3 0 2 0 5 0 9 0 6 0 7 8 1 8 2 8 3 9 1 9 2 6 7 1 6 7 1 6 7 1 6 7 1 6 7 1 6 7 1 6 1 0 Sample output 2 end 3 end 2 6 1 8 2 1 4 2 1 3 2 1 5 2 1 2 1 8 1 6 8 6 8 8 end 5 3 1 9 7 5 9 5 3 9 2 9 7 9 3 7 3 6 7 6 1 7 1 7 7 end
38,655
Depth First Search Depth-first search (DFS) follows the strategy to search ”deeper” in the graph whenever possible. In DFS, edges are recursively explored out of the most recently discovered vertex $v$ that still has unexplored edges leaving it. When all of $v$'s edges have been explored, the search ”backtracks” to explore edges leaving the vertex from which $v$ was discovered. This process continues until all the vertices that are reachable from the original source vertex have been discovered. If any undiscovered vertices remain, then one of them is selected as a new source and the search is repeated from that source. DFS timestamps each vertex as follows: $d[v]$ records when $v$ is first discovered. $f[v]$ records when the search finishes examining $v$’s adjacency list. Write a program which reads a directed graph $G = (V, E)$ and demonstrates DFS on the graph based on the following rules: $G$ is given in an adjacency-list. Vertices are identified by IDs $1, 2,... n$ respectively. IDs in the adjacency list are arranged in ascending order. The program should report the discover time and the finish time for each vertex. When there are several candidates to visit during DFS, the algorithm should select the vertex with the smallest ID. The timestamp starts with 1. Input In the first line, an integer $n$ denoting the number of vertices of $G$ is given. In the next $n$ lines, adjacency lists of $u$ are given in the following format: $u$ $k$ $v_1$ $v_2$ ... $v_k$ $u$ is ID of the vertex and $k$ denotes its degree. $v_i$ are IDs of vertices adjacent to $u$. Output For each vertex, print $id$, $d$ and $f$ separated by a space character in a line. $id$ is ID of the vertex, $d$ and $f$ is the discover time and the finish time respectively. Print in order of vertex IDs. Constraints $1 \leq n \leq 100$ Sample Input 1 4 1 1 2 2 1 4 3 0 4 1 3 Sample Output 1 1 1 8 2 2 7 3 4 5 4 3 6 Sample Input 2 6 1 2 2 3 2 2 3 4 3 1 5 4 1 6 5 1 6 6 0 Sample Output 2 1 1 12 2 2 11 3 3 8 4 9 10 5 4 7 6 5 6 This is example for Sample Input 2 (discover/finish) Reference Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.
38,656
じゃんけん 仲良し 5 人組でじゃんけんをするこずになりたした。じゃんけんずは、グヌ、チョキ、パヌずいう 3぀の手があり、グヌずチョキの勝負ならグヌが「勝ち」・チョキが「負け」、チョキずパヌなら、チョキが「勝ち」・パヌが「負け」、パヌずグヌならパヌが「勝ち」・グヌが「負け」ずいうルヌルです。党員が同じ手、たたはグヌ、チョキ、パヌ党おが出た堎合は「あいこ」ずなりたす。 5 人のじゃんけんの手を入力ずし、それぞれの人の勝敗を出力するプログラムを䜜成しおください。じゃんけんの手は、グヌは 1、チョキは 2、パヌは 3 の数字で衚したす。勝敗は「勝ち」を 1、「負け」を 2、「あいこ」を 3 の数字で衚し、入力順に埓っお出力したす。 Input 耇数のデヌタセットの䞊びが入力ずしお䞎えられたす。入力の終わりはれロひず぀の行で瀺されたす。 各デヌタセットは以䞋の圢匏で䞎えられたす。 h 1 h 2 h 3 h 4 h 5 i 行目に i 人目の手 h i (1, 2 たたは 3) が䞎えられたす。 デヌタセットの数は 200 を超えたせん。 Output 入力デヌタセットごずに、5 人の勝敗を出力したす。 i 行目に i 人目の勝敗(1, 2 たたは 3) を出力しおください。 Sample Input 1 2 3 2 1 1 2 2 2 1 0 Output for the Sample Input 3 3 3 3 3 1 2 2 2 1
38,657
Score : 200 points Problem Statement We have a string S of length N consisting of uppercase English letters. How many times does ABC occur in S as contiguous subsequences (see Sample Inputs and Outputs)? Constraints 3 \leq N \leq 50 S consists of uppercase English letters. Input Input is given from Standard Input in the following format: N S Output Print number of occurrences of ABC in S as contiguous subsequences. Sample Input 1 10 ZABCDBABCQ Sample Output 1 2 Two contiguous subsequences of S are equal to ABC : the 2 -nd through 4 -th characters, and the 7 -th through 9 -th characters. Sample Input 2 19 THREEONEFOURONEFIVE Sample Output 2 0 No contiguous subsequences of S are equal to ABC . Sample Input 3 33 ABCCABCBABCCABACBCBBABCBCBCBCABCB Sample Output 3 5
38,659
䞀日乗車刞 某囜に䜏む倧孊生D氏は囜民的ギタリストA氏の倧ファンでこの倏郜心で行われるラむブに行こうず考えおいる しかしD氏は蟺境の地に䜏んでいるため亀通費がかかるこずがずおも䞍安だった そんなずき圌はJAGずいう組織から安䟡で販売されおいる「1Dayパスポヌト」の存圚を知った JAG (Journey Administrative Group) はD氏が䜏む囜に存圚するいく぀かの鉄道䌚瀟を統括する組織である JAGは党囜に耇数の駅を持ちそれらの間を結ぶ路線を敎備しおいる 各路線はJAGに所属する䌚瀟の内いずれか1瀟により管理されおおり2駅間を途䞭駅無しで双方向に結んでいる たた各路線には利甚する際の運賃ず所芁時間が定たっおいる (これらはどちら向きに移動する堎合でも等しい) JAGのダむダはシンプルに䜜られおおり列車は毎時0分に駅に発着する たたJAGの各駅は極めお賢くデザむンされおおり路線の乗り換えにかかる時間は無芖するこずができる 移動に必芁な亀通費は運賃の単玔な合蚈ずなる. 1DayパスポヌトはJAGが昚今の経営難を脱するために販売し始めた乗り攟題パスポヌトである JAGは䜕皮類かの1Dayパスポヌトを販売しおいる 各パスポヌトはJAGが指定する販売䟡栌で賌入するこずができる たたパスポヌトにはJAGに所属する䌚瀟名がいく぀か曞かれおおりこれらの䌚瀟が管理する党おの路線が1日間 (远加料金無しで) 乗り攟題になる パスポヌトはいく぀でも賌入するこずができ耇数のパスポヌトを䜵甚するこずができる ただしパスポヌトに曞かれおいない䌚瀟の管理路線を通る堎合その路線の運賃は通垞通り必芁ずなる D氏は1Dayパスポヌトをうたく䜿いなるべくお金をかけずラむブ䌚堎たで行きたいず考えおいる たた圌は宿泊で䜙蚈にお金をかけるのが嫌なのでこの囜の1日である$H$時間以䞋でラむブ䌚堎たで蟿り着きたいず考えおいる しかし圌は蚈算が埗意ではないため同じ倧孊の友人でプログラミングが埗意なあなたに助けを求めお来た 困っおいるD氏のために次のようなプログラムを曞いおあげよう JAGの路線情報ず1Dayパスポヌトの情報が䞎えられたずきD氏の最寄り駅からラむブ䌚堎の最寄り駅ぞ$H$時間以䞋で移動するための最小費甚 (パスポヌト代ず運賃の合蚈の最小) を求めるプログラムを䜜成せよ $H$時間以䞋で到達できない堎合やラむブ䌚堎ぞ到達する経路がない堎合は-1を出力せよ Input 入力は耇数のデヌタセットから構成され1぀の入力に含たれるデヌタセットの数は150以䞋である 各デヌタセットの圢匏は次の通りである $N$ $M$ $H$ $K$ $a_1$ $b_1$ $c_1$ $h_1$ $r_1$ ... $a_M$ $b_M$ $c_M$ $h_M$ $r_M$ $S$ $T$ $P$ $l_1$ $d_1$ $k_{1,1}$ ... $k_{1,l_1}$ ... $l_P$ $d_P$ $k_{P,1}$ ... $k_{P,l_P}$ 入力は党お敎数倀で䞎えられる たずJAGの路線情報が䞎えられる $N$ ($2 \le N \le 100$) は駅の数$M$ ($1 \le M \le 500$) は路線の数$H$ ($1 \le H \le 24$) は1日の時間$K$ ($1 \le K \le 8$) はJAGに所属する䌚瀟数を衚す 各駅には$1, 2, \ldots, N$ ず番号が぀いおいる たた各䌚瀟には$1, 2, \ldots, K$ ず番号が぀いおいる 続いお $M$ 行にわたっお路線情報が入力される $a_i$ ず $b_i$ ($1 \le a_i \lt b_i \le N$) が $i$ 番目の路線が぀ないでいる2぀の駅を衚す $c_i$ ($1 \le c_i \le 10{,}000$) は $i$ 番目の路線の運賃$h_i$ ($1 \le h_i \le H$) は路線の所芁時間$r_i$ ($1 \le r_i \le K$) は路線を管理しおいる䌚瀟を衚す ある2぀の駅を結ぶ路線が2本以䞊存圚するこずはない 次の行でD氏の最寄り駅 $S$ ずA氏のラむブ䌚堎の最寄り駅 $T$ が䞎えられる ($1 \le S, T \le N$) $S$ ず $T$ は異なる倀である 次に1Dayパスポヌトの情報が䞎えられる $P$ ($0 \le P \le 2^K - 1$) は1Dayパスポヌトの皮類数を衚しおいる 続いお $P$ 行にわたっお1Dayパスポヌトの情報が入力される $l_j$ ($1 \le l_j \le K$) ず $d_j$ ($1 \le d_j \le 10{,}000$) はそれぞれ $j$ 番目の1Dayパスポヌトに曞かれおいる䌚瀟数ずパスポヌトの料金を衚す 同じ行に$j$ 番目のパスポヌトに曞かれた $l_j$ 個の䌚瀟番号 $k_{j, 1}, k_{j, 2}, \ldots, k_{j, l_j}$ ($1 \le k_{j, 1} \lt k_{j, 2} \lt \cdots \lt k_{j, l_j} \le K$) が䞎えられる 同じ䌚瀟の組み合わせからなる1Dayパスポヌトが耇数入力されるこずはない 入力の終わりは$N=M=H=K=0$ の行によっお衚される このデヌタは凊理を行っおはならない Output 各デヌタセットに察しお蚈算結果を1行で出力せよ すなわちD氏の最寄り駅からラむブ䌚堎の最寄り駅ぞの経路が存圚し $H$ 時間以内に到達できるのであればそのための最小の料金を蟿り着けないのであれば-1を出力せよ Sample Input 3 3 3 2 1 2 3 1 1 1 3 8 1 1 2 3 3 2 2 1 3 0 3 3 2 2 1 2 3 1 1 1 3 8 1 1 2 3 3 2 2 1 3 0 6 4 3 2 1 2 3 1 1 1 3 8 1 1 4 6 3 2 2 5 6 7 2 2 1 6 0 3 3 3 2 1 2 3 1 1 1 3 8 1 1 2 3 3 2 2 1 3 2 2 6 1 2 1 2 2 3 3 2 2 1 2 3 1 1 1 3 8 1 1 2 3 3 2 2 1 3 2 2 6 1 2 1 2 2 3 2 2 2 1 2 3 1 1 2 3 3 2 2 1 3 2 2 6 1 2 1 2 2 5 4 20 4 2 4 100 5 1 1 4 100 5 3 1 5 100 5 4 3 5 100 5 2 3 2 3 2 80 1 2 2 60 1 3 2 40 2 3 0 0 0 0 Output for Sample Input 6 8 -1 5 6 -1 200
38,660
Ambiguous Encoding A friend of yours is designing an encoding scheme of a set of characters into a set of variable length bit sequences. You are asked to check whether the encoding is ambiguous or not. In an encoding scheme, characters are given distinct bit sequences of possibly different lengths as their codes. A character sequence is encoded into a bit sequence which is the concatenation of the codes of the characters in the string in the order of their appearances. An encoding scheme is said to be ambiguous if there exist two different character sequences encoded into exactly the same bit sequence. Such a bit sequence is called an “ambiguous binary sequence”. For example, encoding characters “ A ”, “ B ”, and “ C ” to 0 , 01 and 10 , respectively, is ambiguous. This scheme encodes two different character strings “ AC ” and “ BA ” into the same bit sequence 010 . Input The input consists of a single test case of the following format. $n$ $w_1$ . . . $w_n$ Here, $n$ is the size of the set of characters to encode ($1 \leq n \leq 1000$). The $i$-th line of the following $n$ lines, $w_i$, gives the bit sequence for the $i$-th character as a non-empty sequence of at most 16 binary digits, 0 or 1. Note that different characters are given different codes, that is, $w_i \ne w_j$ for $i \ne j$. Output If the given encoding is ambiguous, print in a line the number of bits in the shortest ambiguous binary sequence. Output zero, otherwise. Sample Input 1 3 0 01 10 Sample Output 1 3 Sample Input 2 3 00 01 1 Sample Output 2 0 Sample Input 3 3 00 10 1 Sample Output 3 0 Sample Input 4 10 1001 1011 01000 00011 01011 1010 00100 10011 11110 0110 Sample Output 4 13 Sample Input 5 3 1101 1 10 Sample Output 5 4
38,661
Score : 2300 points Problem Statement You are given a set S of strings consisting of 0 and 1 , and an integer K . Find the longest string that is a subsequence of K or more different strings in S . If there are multiple strings that satisfy this condition, find the lexicographically smallest such string. Here, S is given in the format below: The data directly given to you is an integer N , and N+1 strings X_0,X_1,...,X_N . For every i (0\leq i\leq N) , the length of X_i is 2^i . For every pair of two integers (i,j) (0\leq i\leq N,0\leq j\leq 2^i-1) , the j -th character of X_i is 1 if and only if the binary representation of j with i digits (possibly with leading zeros) belongs to S . Here, the first and last characters in X_i are called the 0 -th and (2^i-1) -th characters, respectively. S does not contain a string with length N+1 or more. Here, a string A is a subsequence of another string B when there exists a sequence of integers t_1 < ... < t_{|A|} such that, for every i (1\leq i\leq |A|) , the i -th character of A and the t_i -th character of B is equal. Constraints 0 \leq N \leq 20 X_i(0\leq i\leq N) is a string of length 2^i consisting of 0 and 1 . 1 \leq K \leq |S| K is an integer. Input Input is given from Standard Input in the following format: N K X_0 : X_N Output Print the lexicographically smallest string among the longest strings that are subsequences of K or more different strings in S . Sample Input 1 3 4 1 01 1011 01001110 Sample Output 1 10 The following strings belong to S : the empty string, 1 , 00 , 10 , 11 , 001 , 100 , 101 and 110 . The lexicographically smallest string among the longest strings that are subsequences of four or more of them is 10 . Sample Input 2 4 6 1 01 1011 10111010 1101110011111101 Sample Output 2 100 Sample Input 3 2 5 0 11 1111 Sample Output 3 The answer is the empty string.
38,662
Problem A: Clock Problem がっちょ君はお気に入りの時蚈を持っおいる。ある日時蚈の長針が取れおしたい、どこかぞなくしおしたった。しかし、がっちょ君はその時蚈を䜿い続けたいため短針だけで時刻を読み取りたいず考えおいる。 短針の情報 Ξ に察する時刻(時 h 、分 m )を出力せよ。時蚈はいわゆるアナログ時蚈で、1から12の数が等間隔に時蚈回りに昇順に䞊んでいるものである。 Input 入力は以䞋の圢匏で䞎えられる。 Ξ 1行に短針の角床 Ξ を衚す敎数が床数法(degree)で䞎えられる。 短針が指す方向は、12時を指す方向を0床ずしたずき、時蚈回りに Ξ 床回転した方向である。 Constraints 0 ≀ Ξ ≀ 359 Output 短針が䞎えられた角床をずる時の時刻を h m の圢で1行に出力する。 時刻に午前午埌の区別はないため0 ≀ h ≀ 11ずする。 Sample Input 1 0 Sample Output 1 0 0 Sample Input 2 45 Sample Output 2 1 30 Sample Input 3 100 Sample Output 3 3 20
38,663
パトロヌル 文久1862幎、䌚接の殿様が京郜守護職を呜ぜられたした。京郜守護職ずは治安の悪化した幕末の京郜を守る倧切な圹目です。幕府や他の藩ず分担しお垂䞭をパトロヌルしなければなりたせん。ずころがいざ分担ルヌトを決める段になっお、家臣の䞭でも有名な頑固者の叀老から以䞋のような泚文が぀きたした。 倧倉なこずになりたした。殿様ずいえどもこの家臣の蚀い分を無芖するわけにはいきたせん。分担ルヌトの遞択によっおは、「歊士の面目が立たない」ずいうこずになっおしたいたす。 ずいうこずで、スタヌト地点、ゎヌル地点、亀差点の情報を入力ずしお、䞊の䞉぀の条件を満たすかどうかを刀定するプログラムを䜜っお、殿様に献䞊しおください。 スタヌト地点を 1、ゎヌル地点を2、その他の亀差点を 3 以䞊の敎数で衚したす。1぀の道路は、その道が結ぶ1組の亀差点番号で衚したす。なお、亀差点の数は 100 以䞋ずし、党おの亀差点からスタヌト地点およびゎヌル地点ぞの経路がそれぞれ䞀぀以䞊あるものずしたす。 入力 耇数のデヌタセットが䞎えられたす。各デヌタセットは以䞋の圢匏で䞎えられたす。 a 1 b 1 a 2 b 2   0 0 各行の぀の敎数は、亀差点 a i ず亀差点 b i ずを぀なぐ道路が存圚するこずを瀺したす。 a i ず b i がずもに 0 のずき亀差点情報の入力の終わりを瀺したす。 デヌタセットの数は50を超えたせん。 出力 各デヌタセットに察しお、歊士の面目が立぀堎合䞉぀の条件を満たす堎合OK、それ以倖の堎合䞉぀の条件を満たさない堎合NG ず行に出力しおください。 Sample Input 1 3 3 4 3 5 3 6 4 6 4 7 4 7 5 6 6 7 5 8 5 8 6 8 6 9 7 9 8 9 9 2 0 0 1 3 3 4 3 4 4 2 0 0 Output for the Sample Input OK NG
38,664
Score : 600 points Problem Statement Given is an integer N . Find the minimum possible positive integer k such that (1+2+\cdots+k) is a multiple of N . It can be proved that such a positive integer k always exists. Constraints 1 \leq N \leq 10^{15} All values in input are integers. Input Input is given from Standard Input in the following format: N Output Print the answer in a line. Sample Input 1 11 Sample Output 1 10 1+2+\cdots+10=55 holds and 55 is indeed a multple of N=11 . There are no positive integers k \leq 9 that satisfy the condition, so the answer is k = 10 . Sample Input 2 20200920 Sample Output 2 1100144
38,665
あみだくじ II 瞊線が n 本のあみだくじがありたす。このあみだくじは以䞋の条件を満たしおいたす。 暪線は真暪に匕きたす。斜めに匕くこずはありたせん。 暪線は必ず隣り合った瞊線同士を぀なぎたす。぀たり、暪線が瞊線をたたぐこずはありたせん。 どの瞊線に぀いおも同じ点から巊右同時に暪線が出るこずはありたせん。぀たり、暪線が瞊線を暪切るこずはありたせん。 圓りはひず぀だけです。 䞋図 に n = 5 のずきの、あみだくじの䟋を瀺したす。䞊偎の数字は瞊線の番号(巊から1, 2, 3, 4, 5 ) を衚したす。☆が圓たりです。 瞊線の本数 n 、遞んだ瞊線の番号 m 、あみだくじの圓りの堎所、各段における暪線の有無を読み蟌んで、圓りにたどり着けるかどうかの刀定を出力するプログラムを䜜成しおください。ただし、䞎えられたあみだくじの任意の䜍眮に1 本だけ暪線を付け加えるこずができるものずしたす付け加えなくおもかたいたせん。暪線を1 本付け加えた埌のあみだくじも前述の条件を満たしおいなければなりたせん。 Input 耇数のデヌタセットが䞎えられたす。各デヌタセットは以䞋のずおりです 行目に、瞊線の本数 n (1 < n ≀ 10) が曞かれおいたす。 行目には、遞んだ瞊線の番号 m (1 ≀ m ≀ n ) が曞かれおいたす。 行目には、圓りの堎所図でいう☆が巊から数えお䜕番目かが曞かれおいたす。 行目には、あみだくじの段数 d (1 ≀ d ≀ 30) が曞かれおいたす。 行目以降は、図に察応する数字の䞊びのように、あみだくじの䞊から順に、各瞊線の間に暪線があるずきを 1、ないずきを 0 ずしお、 n - 1 ぀の数字が䞊んでいたす。 入力は぀の 0 が曞かれた行で終わりたす。 Output 各デヌタセットに぀いお、遞んだ瞊線の番号 m から圓りにたどり着けるかどうかに応じお以䞋の倀を出力しおください。 暪線を匕かなくおも圓りにたどり着けるずきは 0 を出力しおください。 暪線を1 本匕けば圓りにたどり着けるずきは、その䞭で最も出発偎図でみお䞊に近い暪線の䜍眮を出力しおください。出発偎から数えお䜕段目図 を参考にしおくださいに、巊から数えお䜕番目の瞊線から右に向かっお暪線を匕くかを半角の空癜で区切っお出力しおください。 暪線を1 本匕いおも圓りにたどり着けないずきには 1 を出力しおください。 Sample Input 5 2 3 9 1010 1001 0100 1001 0010 1000 0100 0101 1010 0 Output for the Sample Input 6 4
38,666
C: 䞲刺し (Skewering) 問題 ある日、ほむらちゃんが積み朚で遊んでいるずおんぷら君がやっおきたした。 ほむらちゃんは、おんぷら君ず䞀緒に積み朚で遊ぶこずにしたした。 䞀蟺の長さが 1 の立方䜓の積み朚のブロックを A \times B \times C 個隙間なく積み重ねおできた、 A \times B \times C の盎方䜓がありたす。党おの立方䜓ず盎方䜓の各蟺は x 軞、 y 軞、 z 軞のいずれかず平行です。 ほむらちゃんずおんぷら君は亀互に以䞋の操䜜を繰り返したす。 盎方䜓から瞊方向、暪方向、奥行き方向のいずれかの方向に䞀列に䞊んだ積み朚のブロックの列を遞んで、列に含たれるブロック党おを赀く塗る。ただし、すでに赀く塗られおいるブロックを含む列は遞ぶこずができない。 より正確には、 盎方䜓に含たれるブロックのうち䞀぀ず、 x,y,z の䞉぀の方向から䞀぀を遞ぶ。 遞んだブロックを遞んだ方向に敎数の距離だけ動かしたずき、完党に重なるようなブロック党おを赀く塗る 0 や負の敎数の距離を動かすこずも考える。ただし、条件を満たすブロックであっおすでに塗られたものがひず぀でも存圚する堎合、この操䜜は行うこずができない。 ほむらちゃんが先手で、先に操䜜を行えなくなった方の負けです。 たた、最初党おの立方䜓に色は塗られおいたせん。 二人が最適に行動したずき、どちらが勝぀か刀定しおください。 入力圢匏 A B C 制玄 1 \leq A, B, C \leq 100 入力はすべお敎数である。 出力圢匏 二人が最適に行動したずき、ほむらちゃんが勝぀ならば Hom を、おんぷら君が勝぀ならば Tem を䞀行に出力する。 入力䟋 1 1 1 10 出力䟋 1 Hom 䞀回目でほむらちゃんは党おのブロックを赀く塗るこずができたす。 入力䟋 2 4 3 5 出力䟋 2 Hom 入力䟋 3 6 4 10 出力䟋 3 Tem
38,667
JPEG Compression The fundamental idea in the JPEG compression algorithm is to sort coeffi- cient of given image by zigzag path and encode it. In this problem, we don’t discuss about details of the algorithm, but you are asked to make simple pro- gram. You are given single integer N , and you must output zigzag path on a matrix where size is N by N . The zigzag scanning is start at the upper-left corner (0, 0) and end up at the bottom-right corner. See the following Figure and sample output to make sure rule of the zigzag scanning. For example, if you are given N = 8, corresponding output should be a matrix shown in right-side of the Figure. This matrix consists of visited time for each element. Input Several test cases are given. Each test case consists of one integer N (0 < N < 10) in a line. The input will end at a line contains single zero. Output For each input, you must output a matrix where each element is the visited time. All numbers in the matrix must be right justified in a field of width 3. Each matrix should be prefixed by a header “Case x:” where x equals test case number. Sample Input 3 4 0 Output for the Sample Input Case 1: 1 2 6 3 5 7 4 8 9 Case 2: 1 2 6 7 3 5 8 13 4 9 12 14 10 11 15 16
38,668
最倧の和 問題 n 個の敎数からなる数列 a 1 , a 2 , ..., a n ず正敎数 k (1 ≀ k ≀ n ) が䞎えられる.このずき, 連続しお䞊ぶ k 個の敎数の和 S i = a i + a i+1 + ... + a i + k -1 (1 ≀ i ≀ n - k + 1) の最倧倀を出力するプログラムを䜜りなさい. 入力 入力は耇数のデヌタセットからなる各デヌタセットは以䞋の圢匏で䞎えられる入力は぀のれロを含む行で終了する 1 行目には正敎数 n (1 ≀ n ≀ 100000) ず正敎数 k (1 ≀ k ≀ n ) がこの順に空癜で 区切られお曞かれおいる.2 行目以降の第 1 + i 行目 (1 ≀ i ≀ n ) には, 数列の i 番目の項 a i (-10000 ≀ a i ≀ 10000) が曞かれおいる. 採点甚デヌタのうち, 配点の 60% 分は n ≀ 5000, k ≀ 1000 を満たす. デヌタセットの数は 5 を超えない 出力 デヌタセットごずに S i の最倧倀を行に出力する 入出力䟋 入力䟋 5 3 2 5 -4 10 3 0 0 出力䟋 11 䞊蚘問題文ず自動審刀に䜿われるデヌタは、 情報オリンピック日本委員䌚 が䜜成し公開しおいる問題文ず採点甚テストデヌタです。
38,669
Print a Frame Draw a frame which has a height of H cm and a width of W cm. For example, the following figure shows a frame which has a height of 6 cm and a width of 10 cm. ########## #........# #........# #........# #........# ########## Input The input consists of multiple datasets. Each dataset consists of two integers H and W separated by a single space. The input ends with two 0 (when both H and W are zero). Output For each dataset, print the frame made of '#' and '.'. Print a blank line after each dataset. Constraints 3 ≀ H ≀ 300 3 ≀ W ≀ 300 Sample Input 3 4 5 6 3 3 0 0 Sample Output #### #..# #### ###### #....# #....# #....# ###### ### #.# ###
38,670
Problem Statement The Animal School is a primary school for animal children. You are a fox attending this school. One day, you are given a problem called "Arithmetical Restorations" from the rabbit teacher, Hanako. Arithmetical Restorations are the problems like the following: You are given three positive integers, $A$, $B$ and $C$. Several digits in these numbers have been erased. You should assign a digit in each blank position so that the numbers satisfy the formula $A+B=C$. The first digit of each number must not be zero. It is also the same for single-digit numbers. You are clever in mathematics, so you immediately solved this problem. Furthermore, you decided to think of a more difficult problem, to calculate the number of possible assignments to the given Arithmetical Restorations problem. If you can solve this difficult problem, you will get a good grade. Shortly after beginning the new task, you noticed that there may be too many possible assignments to enumerate by hand. So, being the best programmer in the school as well, you are now trying to write a program to count the number of possible assignments to Arithmetical Restoration problems. Input The input is a sequence of datasets. The number of datasets is less than 100. Each dataset is formatted as follows. $A$ $B$ $C$ Each dataset consists of three strings, $A$, $B$ and $C$. They indicate that the sum of $A$ and $B$ should be $C$. Each string consists of digits ( 0 - 9 ) and/or question mark ( ? ). A question mark ( ? ) indicates an erased digit. You may assume that the first character of each string is not 0 and each dataset has at least one ? . It is guaranteed that each string contains between 1 and 50 characters, inclusive. You can also assume that the lengths of three strings are equal. The end of input is indicated by a line with a single zero. Output For each dataset, output the number of possible assignments to the given problem modulo 1,000,000,007. Note that there may be no way to solve the given problems because Ms. Hanako is a careless rabbit. Sample Input 3?4 12? 5?6 ?2?4 5?7? ?9?2 ????? ????? ????? 0 Output for the Sample Input 2 40 200039979 Note The answer of the first dataset is 2. They are shown below. 384 + 122 = 506 394 + 122 = 516
38,672
Score: 100 points Problem Statement We have two distinct integers A and B . Print the integer K such that |A - K| = |B - K| . If such an integer does not exist, print IMPOSSIBLE instead. Constraints All values in input are integers. 0 \leq A,\ B \leq 10^9 A and B are distinct. Input Input is given from Standard Input in the following format: A B Output Print the integer K satisfying the condition. If such an integer does not exist, print IMPOSSIBLE instead. Sample Input 1 2 16 Sample Output 1 9 |2 - 9| = 7 and |16 - 9| = 7 , so 9 satisfies the condition. Sample Input 2 0 3 Sample Output 2 IMPOSSIBLE No integer satisfies the condition. Sample Input 3 998244353 99824435 Sample Output 3 549034394
38,673
問題 G : プログラミングコンテストチャレンゞブック 今G○○gle Code Jam の地区倧䌚が始たろうずしおいる 前の垭に座っおいる男の ID は omeometo ず蚀うらしい 埌ろの垭に座っおいる男の ID は jellies ず蚀うらしい 東京倧孊時代の蚘憶に䌌たような ID の仲間が居た芚えがあるが僕の仲間は䞀人残さず矎少女だったはずだ 圌らは机の䞊に蟻のむラストが掛かれた本を持っおいる あれはもしや プログラミングコンテストのアルゎリズムを最初に網矅し今では発売犁止ずなった䌝説の本 「プログラミングコンテストチャレンゞブック」ではないか 僕はomeometo がトむレに行ったのを芋蚈らい少し拝借しお読んでみるこずにした プログラミングコンテストチャレンゞブック  しかし僕は䞀瞬で本を読み終えおしたった 拍子抜けだ簡単すぎる 少し叀い本だからずいっおこんな内容でよく売る気になったものだ 䟋えば最初の䞉角圢の問題これは簡単すぎおお話にならない自分だったらこうする 問題 N 本の盎線状の棒がある棒 i の長さは a i である あなたはそれらの棒から 6 本を遞び それらの 3 本ず぀で2 個の䞉角圢を䜜ろうず考えおいる 3 本の棒はそれぞれ䞉角圢の蟺ずしお甚い2 ぀の棒が觊れる䜍眮は棒の端点のみずする ぀たり棒の䞀郚を䞉角圢の蟺ずしお䜿うこずは蚱されず必ず棒党䜓を蟺ずしなければならない たた棒の倪さは考えず䞉角圢は正の面積を持たなければならないものずする 2 個の䞉角圢の呚長の和の最倧倀を求めよ ただし2 個の䞉角圢を䜜るこずができない際には 0 を答えずせよ 入力 入力の最初の行には敎数 N が曞かれおいる 続く N 行の i 行目には 1 ぀の敎数 a i が曞かれおいる 出力 答えの敎数を出力せよ 制玄 1 ≀ N ≀ 10 5 1 ≀ a i ≀ 10 15 郚分点 この問題の刀定には20 点分のテストケヌスのグルヌプが蚭定されおいる このグルヌプに含たれるテストケヌスの入力は以䞋を満たす 1 ≀ N ≀ 100 入出力䟋 入力䟋: 6 1 1 1 1 1 1 入力䟋に察する出力: 6
38,674
Score : 400 points Problem Statement One day, Niwango-kun, an employee of Dwango Co., Ltd., found an integer sequence (a_1, ..., a_N) of length N . He is interested in properties of the sequence a . For a nonempty contiguous subsequence a_l, ..., a_r (1 \leq l \leq r \leq N) of the sequence a , its beauty is defined as a_l + ... + a_r . Niwango-kun wants to know the maximum possible value of the bitwise AND of the beauties of K nonempty contiguous subsequences among all N(N+1)/2 nonempty contiguous subsequences. (Subsequences may share elements.) Find the maximum possible value for him. Constraints 2 \leq N \leq 1000 1 \leq a_i \leq 10^9 1 \leq K \leq N(N+1)/2 All numbers given in input are integers Input Input is given from Standard Input in the following format: N K a_1 a_2 ... a_N Output Print the answer. Sample Input 1 4 2 2 5 2 5 Sample Output 1 12 There are 10 nonempty contiguous subsequences of a . Let us enumerate them: contiguous subsequences starting from the first element: \{2\}, \{2, 5\}, \{2, 5, 2\}, \{2, 5, 2, 5\} contiguous subsequences starting from the second element: \{5\}, \{5, 2\}, \{5, 2, 5\} contiguous subsequences starting from the third element: \{2\}, \{2, 5\} contiguous subsequences starting from the fourth element: \{5\} (Note that even if the elements of subsequences are equal, subsequences that have different starting indices are considered to be different.) The maximum possible bitwise AND of the beauties of two different contiguous subsequences is 12 . This can be achieved by choosing \{5, 2, 5\} (with beauty 12 ) and \{2, 5, 2, 5\} (with beauty 14 ). Sample Input 2 8 4 9 1 8 2 7 5 6 4 Sample Output 2 32
38,675
Problem F: Problem F: リズムマシヌン Advanced Computer Music瀟ACM瀟は あらかじめプログラムされたリズム通りに音楜を挔奏する リズムマシヌンを販売しおいた ある時ACM瀟は新しいリズムマシヌンを開発しお売り出そうずしおいた ACM瀟の旧補品は同時に1぀の音しか鳎らすこずができなかったのに察し 新補品では最倧で8぀の音を同時に鳎らせるようになるずいうのが 䞀番の目玉機胜であった 今たで耇数の旧補品を利甚しお挔奏する必芁のあった曲が 新補品1台で枈むようになるので ACM瀟は新補品ぞの移行を掚進させるために 耇数の旧補品向けのリズムパタヌンを1぀の新補品向けのリズムパタヌンに 倉換するプログラムを䜜るこずにした ACM瀟のリズムマシヌンでは同時にどの音を鳎らすかを2桁の16進数で衚珟する ACM瀟のリズムマシヌンは8぀の異なる音を鳎らすこずが可胜で それぞれの音には0から7の番号が割り圓おられおいる あるタむミングにおいお音 i (0 ≀ i < 8) を鳎らす堎合を s i = 1 鳎らさない堎合を s i = 0 ずする このずきそれぞれの音を同時に鳎らしたような和音を ずいう倀で衚し この倀を2桁の16進数衚蚘で衚した「和音衚珟」 がリズムパタヌンの䞭で甚いられる16進数の英字は倧文字を甚いる 䟋えば音0, 6, 7 を同時に鳎らすような和音は S = 2 0 + 2 6 + 2 7 = C1 (16) ずなるから “ C1 ” ず衚珟され たた䜕も鳎らさないような「和音」は “ 00 ” ず衚珟される リズムパタヌンは䞊蚘のような和音衚珟を1぀以䞊䞊べたものずしお䞎えられる あるリズムパタヌン文字列は1小節内の挔奏パタヌンを瀺しおいる それぞれの和音を鳎らすタむミングを小節内の盞察䜍眮 t (0 ≀ t < 1) で衚珟するこずにする k 個の和音衚珟からなるリズムパタヌン文字列は 小節を k 等分しそれぞれの和音を順に t = 0/ k , 1/ k , ..., ( k −1)/ k のタむミングで挔奏するような リズムパタヌンを衚しおいる 䟋えばリズムパタヌン “ 01000003 ” は t = 0/4 のタむミングで音0を挔奏し t = 3/4 のタむミングで音0, 1を挔奏するこずを衚す たたリズムパタヌン “ 00 ” は小節内で䜕も音を鳎らさないこずを衚す リズムパタヌンには和音衚珟が1぀以䞊必芁であるこずに泚意せよ 旧補品は同時に1぀の音しか鳎らせないため 旧補品向けのリズムパタヌン文字列内には “ 00 ”, “ 01 ”, “ 02 ”, “ 04 ”, “ 08 ”, “ 10 ”, “ 20 ”, “ 40 ”, “ 80 ” のいずれかの和音衚珟しか珟れない 旧補品向けのリズムパタヌンを N 個 (1 ≀ N ≀ 8) 受け取り それらのリズムパタヌンを同時に挔奏するような 新補品向けのリズムパタヌンを出力するプログラムを曞いお欲しい 䞎えられる N 個のリズムパタヌンにおいお たったく同じタむミングで同じ音が挔奏されるこずはないず仮定しおよい Input 最初の行にデヌタセットの数が䞎えられる 次の行以降にはそれぞれのデヌタセットが順に蚘述されおいる デヌタセットの数は 120 を越えないず仮定しおよい それぞれのデヌタセットは以䞋のような圢匏で䞎えられる N R 1 R 2 ... R N R i (1 ≀ i ≀ N ) はそれぞれ旧補品向けのリズムパタヌンである 各リズムパタヌンは最倧で2048文字1024和音衚珟である 䞎えられるリズムパタヌンは必ずしも最短の衚珟になっおいないこずに泚意せよ Output 各デヌタセットに぀いお䞎えられた N 個のリズムパタヌンを すべお同時に挔奏するような最短のリズムパタヌンを生成し 1行で出力せよ そのようなリズムパタヌンが2048文字を越える堎合 リズムパタヌンの代わりに “ Too complex. ” ずいう文字列を出力せよ Sample Input 5 2 01000100 00020202 2 0102 00000810 1 0200020008000200 5 0001 000001 0000000001 00000000000001 0000000000000000000001 1 000000 Output for the Sample Input 01020302 01000A10 02020802 Too complex. 00
38,676
Score : 200 points Problem Statement There are N people numbered 1 to N . Each person wears a red hat or a blue hat. You are given a string s representing the colors of the people. Person i wears a red hat if s_i is R , and a blue hat if s_i is B . Determine if there are more people wearing a red hat than people wearing a blue hat. Constraints 1 \leq N \leq 100 |s| = N s_i is R or B . Input Input is given from Standard Input in the following format: N s Output If there are more people wearing a red hat than there are people wearing a blue hat, print Yes ; otherwise, print No . Sample Input 1 4 RRBR Sample Output 1 Yes There are three people wearing a red hat, and one person wearing a blue hat. Since there are more people wearing a red hat than people wearing a blue hat, the answer is Yes . Sample Input 2 4 BRBR Sample Output 2 No There are two people wearing a red hat, and two people wearing a blue hat. Since there are as many people wearing a red hat as people wearing a blue hat, the answer is No .
38,677
最終防衛線 たびたび未確認生物に䟵略されるようになった某囜では重芁斜蚭を新型防衛兵噚で保護するこずにした。 この兵噚は倚角圢の領域に特殊なガスを充満させるこずで未確認生物にダメヌゞを䞎えるこずができる。未確認生物が䟵攻する途䞭でガスの濃床が倉わるず、濃床の差の絶察倀のダメヌゞを䞎える。ガスの濃床が同じ領域を動いおいるずきはダメヌゞは䞀切発生しない。 珟圚の技術ではガスの濃床は䞀定にしかできないので、某囜は新型防衛兵噚を耇数投入するこずにした。党おの兵噚でガスの濃床は同じであり、耇数の兵噚の領域に含たれる郚分は濃床が足し合わされる。 未確認生物の出珟地点ず重芁斜蚭の䜍眮を元に、未確認生物が重芁斜蚭たで䟵攻する時に受けるダメヌゞの最小倀を求めお欲しい。 ただし、未確認生物の䟵攻ルヌトに倚角圢の頂点や他の倚角圢ずの亀点は含たれないものずする。たた、倚角圢の蟺䞊を蟺にそっお䟵攻するこずはない。 Input 入力は以䞋の圢匏で䞎えられる。 倚角圢の数 倚角圢の頂点数 x座暙 y座暙 x座暙 y座暙 ... 倚角圢の頂点数 x座暙 y座暙 x座暙 y座暙 ... 出珟地点ず重芁斜蚭のデヌタの数 出珟䜍眮のx座暙 出珟䜍眮のy座暙 重芁斜蚭のx座暙 重芁斜蚭のy座暙 出珟䜍眮のx座暙 出珟䜍眮のy座暙 重芁斜蚭のx座暙 重芁斜蚭のy座暙 ... Constraints 入力に含たれる座暙は絶察倀が1,000以䞋の敎数 倚角圢の数は1以䞊5以䞋 倚角圢の頂点数は3以䞊5以䞋 出珟地点ず重芁斜蚭のデヌタの数は1以䞊100以䞋 倚角圢は䞎えられた頂点を順に぀ないでできる倚角圢を指し、自己亀差はない 出珟地点ず重芁斜蚭は倚角圢の頂点及び蟺䞊にあるこずはない Output 出珟地点ず重芁斜蚭の組ごずに未確認生物が重芁斜蚭たで䟵攻する際に受けるダメヌゞの最小倀を1行ず぀出力せよ。 Sample Input 1 2 4 0 4 1 1 3 1 4 4 3 6 0 10 0 8 7 1 2 3 9 1 Output for the Sample Input 1 2 1぀目の倚角圢を出る時に1ダメヌゞを䞎え、2぀目の倚角圢に入る時に1ダメヌゞを䞎える。 Sample Input 2 1 4 0 0 10 0 10 10 0 10 2 15 5 5 5 5 5 15 5 Output for the Sample Input 2 1 1 1぀目のデヌタでは未確認生物が倚角圢に入る時にダメヌゞが発生する。 2぀目のデヌタでは未確認生物が倚角圢から出る時にダメヌゞが発生する。 Sample Input 3 2 4 0 0 10 0 10 10 0 10 4 10 0 20 0 20 10 10 10 1 5 5 15 5 Output for the Sample Input 3 0 未確認生物が1぀目の倚角圢から2぀目の倚角圢に動くルヌトを取るず濃床が倉わるこずがないのでダメヌゞを䞎えるこずができない。 Sample Input 4 2 3 0 0 10 0 5 10 3 5 0 15 0 10 10 1 5 5 10 5 Output for the Sample Input 4 2 出珟地点の領域ず重芁斜蚭の領域は点で接しおいるがこの点を通っお䟵攻するこずはない。 Sample Input 5 2 4 0 0 10 0 10 10 0 10 4 0 0 10 0 10 10 0 10 2 15 5 5 5 5 5 15 5 Output for the Sample Input 5 2 2
38,678
Scores of Final Examination I am a junior high school teacher. The final examination has just finished, and I have all the students' scores of all the subjects. I want to know the highest total score among the students, but it is not an easy task as the student scores are listed separately for each subject. I would like to ask you, an excellent programmer, to help me by writing a program that finds the total score of a student with the highest total score. Input The input consists of multiple datasets, each in the following format. n m p 1,1 p 1,2 
 p 1, n p 2,1 p 2,2 
 p 2, n 
 p m ,1 p m ,2 
 p m,n The first line of a dataset has two integers n and m . n is the number of students (1 ≀ n ≀ 1000). m is the number of subjects (1 ≀ m ≀ 50). Each of the following m lines gives n students' scores of a subject. p j,k is an integer representing the k -th student's score of the subject j (1 ≀ j ≀ m and 1 ≀ k ≀ n ). It satisfies 0 ≀ p j,k ≀ 1000. The end of the input is indicated by a line containing two zeros. The number of datasets does not exceed 100. Output For each dataset, output the total score of a student with the highest total score. The total score s k of the student k is defined by s k = p 1, k + 
 + p m,k . Sample Input 5 2 10 20 30 40 50 15 25 35 45 55 6 3 10 20 30 15 25 35 21 34 11 52 20 18 31 15 42 10 21 19 4 2 0 0 0 0 0 0 0 0 0 0 Output for the Sample Input 105 83 0
38,679
Score: 500 points Problem Statement You are the top spy of AtCoder Kingdom. To prevent the stolen secret from being handed to AlDebaran Kingdom, you have sneaked into the party where the transaction happens. There are N attendees in the party, and they are given attendee numbers from 1 through N . The height of Attendee i is A_i . According to an examination beforehand, you know that a pair of attendees satisfying the condition below will make the transaction. The absolute difference of their attendee numbers is equal to the sum of their heights. There are \frac{N(N-1)}{2} ways to choose two from the N attendees and make a pair. Among them, how many satisfy the condition above? P.S.: We cannot let you know the secret. Constraints All values in input are integers. 2 \leq N \leq 2 \times 10^5 1 \leq A_i \leq 10^9\ (1 \leq i \leq N) Input Input is given from Standard Input in the following format: N A_1 A_2 \dots A_N Output Print the number of pairs satisfying the condition. Sample Input 1 6 2 3 3 1 3 1 Sample Output 1 3 A_1 + A_4 = 3 , so the pair of Attendee 1 and 4 satisfy the condition. A_2 + A_6 = 4 , so the pair of Attendee 2 and 6 satisfy the condition. A_4 + A_6 = 2 , so the pair of Attendee 4 and 6 satisfy the condition. No other pair satisfies the condition, so you should print 3 . Sample Input 2 6 5 2 4 2 8 8 Sample Output 2 0 No pair satisfies the condition, so you should print 0 . Sample Input 3 32 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5 Sample Output 3 22
38,680
Score : 300 points Problem Statement Eli- 1 started a part-time job handing out leaflets for N seconds. Eli- 1 wants to hand out as many leaflets as possible with her special ability, Cloning. Eli- gen can perform two kinds of actions below. Clone herself and generate Eli- (gen + 1) . (one Eli- gen (cloning) and one Eli- (gen + 1) (cloned) exist as a result of Eli- gen 's cloning.) This action takes gen \times C ( C is a coefficient related to cloning. ) seconds. Hand out one leaflet. This action takes one second regardress of the generation ( =gen ). They can not hand out leaflets while cloning. Given N and C , find the maximum number of leaflets Eli- 1 and her clones can hand out in total modulo 1000000007 ( = 10^9 + 7 ). Constraints 1 \leq Q \leq 100000 = 10^5 1 \leq N_q \leq 100000 = 10^5 1 \leq C_q \leq 20000 = 2 \times 10^4 Input The input is given from Standard Input in the following format: Q N_1 C_1 : N_Q C_Q The input consists of multiple test cases. On line 1 , Q that represents the number of test cases is given. Each test case is given on the next Q lines. For the test case q ( 1 \leq q \leq Q ) , N_q and C_q are given separated by a single space. N_q and C_q represent the working time and the coefficient related to cloning for test case q respectively. Output For each test case, Print the maximum number of leaflets Eli- 1 and her clones can hand out modulo 1000000007 ( = 10^9 + 7 ). Partial Scores 30 points will be awarded for passing the test set satisfying the condition: Q = 1 . Another 270 points will be awarded for passing the test set without addtional constraints and you can get 300 points in total. Sample Input 1 2 20 8 20 12 Sample Output 1 24 20 For the first test case, while only 20 leaflets can be handed out without cloning, 24 leaflets can be handed out by cloning first and two people handing out 12 leaflets each. For the second test case, since two people can only hand out 8 leaflets each if Eli- 1 clones, she should hand out 20 leaflets without cloning. Sample Input 2 1 20 3 Sample Output 2 67 One way of handing out 67 leaflets is like the following image. Each black line means cloning, and each red line means handing out. This case satisfies the constraint of the partial score. Sample Input 3 1 200 1 Sample Output 3 148322100 Note that the value modulo 1000000007 ( 10^9 + 7 ) must be printed. This case satisfies the constraint of the partial score.
38,681
Wire I am a craftsman specialized in interior works. A customer asked me to perform wiring work on a wall whose entire rectangular surface is tightly pasted with pieces of panels. The panels are all of the same size (2 m in width, 1 m in height) and the wall is filled with an x (horizontal) by y (vertical) array of the panels. The customer asked me to stretch a wire from the left top corner of the wall to the right bottom corner whereby the wire is tied up at the crossing points with the panel boundaries (edges and vertexes) as shown in the figure. There are nine tied-up points in the illustrative figure shown below. Fig: The wire is tied up at the edges and vertexes of the panels (X: 4 panels, Y: 6 panels) Write a program to provide the number of points where the wire intersects with the panel boundaries. Assume that the wire and boundary lines have no thickness. Input The input is given in the following format. x y A line of data is given that contains the integer number of panels in the horizontal direction x (1 ≀ x ≀ 1000) and those in the vertical direction y (1 ≀ y ≀ 1000). Output Output the number of points where the wire intersects with the panel boundaries. Sample Input 1 4 4 Sample Output 1 5 Sample Input 2 4 6 Sample Output 2 9
38,682
Get Many Persimmon Trees Seiji Hayashi had been a professor of the Nisshinkan Samurai School in the domain of Aizu for a long time in the 18th century. In order to reward him for his meritorious career in education, Katanobu Matsudaira, the lord of the domain of Aizu, had decided to grant him a rectangular estate within a large field in the Aizu Basin. Although the size (width and height) of the estate was strictly specified by the lord, he was allowed to choose any location for the estate in the field. Inside the field which had also a rectangular shape, many Japanese persimmon trees, whose fruit was one of the famous products of the Aizu region known as 'Mishirazu Persimmon', were planted. Since persimmon was Hayashi's favorite fruit, he wanted to have as many persimmon trees as possible in the estate given by the lord. For example, in Figure 1, the entire field is a rectangular grid whose width and height are 10 and 8 respectively. Each asterisk (*) represents a place of a persimmon tree. If the specified width and height of the estate are 4 and 3 respectively, the area surrounded by the solid line contains the most persimmon trees. Similarly, if the estate's width is 6 and its height is 4, the area surrounded by the dashed line has the most, and if the estate's width and height are 3 and 4 respectively, the area surrounded by the dotted line contains the most persimmon trees. Note that the width and height cannot be swapped; the sizes 4 by 3 and 3 by 4 are different, as shown in Figure 1. Figure 1: Examples of Rectangular Estates Your task is to find the estate of a given size (width and height) that contains the largest number of persimmon trees. Input The input consists of multiple data sets. Each data set is given in the following format. N W H x 1 y 1 x 2 y 2 ... x N y N S T N is the number of persimmon trees, which is a positive integer less than 500. W and H are the width and the height of the entire field respectively. You can assume that both W and H are positive integers whose values are less than 100. For each i (1 <= i <= N ), x i and y i are coordinates of the i -th persimmon tree in the grid. Note that the origin of each coordinate is 1. You can assume that 1 <= x i <= W and 1 <= y i <= H , and no two trees have the same positions. But you should not assume that the persimmon trees are sorted in some order according to their positions. Lastly, S and T are positive integers of the width and height respectively of the estate given by the lord. You can also assume that 1 <= S <= W and 1 <= T <= H . The end of the input is indicated by a line that solely contains a zero. Output For each data set, you are requested to print one line containing the maximum possible number of persimmon trees that can be included in an estate of the given size. Sample Input 16 10 8 2 2 2 5 2 7 3 3 3 8 4 2 4 5 4 8 6 4 6 7 7 5 7 8 8 1 8 4 9 6 10 3 4 3 8 6 4 1 2 2 1 2 4 3 4 4 2 5 3 6 1 6 2 3 2 0 Output for the Sample Input 4 3
38,683
ほが呚期文字列 文字列 S が䞎えられる。この文字列 S に察し、 Q 個のク゚リに答えよ。 i 番目のク゚リでは、 S[l_i,\ r_i] から1文字たで倉えおよいずき、 S[l_i,\ r_i] を呚期 t_i の文字列にできるかどうかを刀定せよ。 S[l,\ r] は文字列 S の l 文字目から r 文字目たでの郚分文字列を衚す。 文字列 W が呚期 t の文字列であるずは、 i\ =\ 1,\2,\... ,\ |W| − t に察し、 W_{i} = W_{i+t} ずなるこずずする。 Constraints 1 ≀ |S| ≀ 10^5 1 ≀ Q ≀ 10^5 1 ≀ l_i ≀ r_i ≀ |S| 1 ≀ t_i ≀ r_i − l_i+1 S はアルファベットの小文字のみからなる Input Format 入力は以䞋の圢匏で暙準入力から䞎えられる。 S Q l_1 r_1 t_1 ... l_Q r_Q t_Q Output Format Q 行にわたっお出力せよ。 i 行目には、 i 番目のク゚リの答えを Yes たたは No で出力せよ。 Sample Input 1 abcabcaxcabc 4 1 9 3 8 12 3 1 4 2 2 3 2 Sample Output 1 Yes Yes No Yes Sample Input 2 isuruu 4 3 6 1 3 6 2 3 6 3 2 4 1 Sample Output 2 Yes Yes Yes No
38,685
>土地分割 ハりゞングメヌカヌの山田ハりスは、新たな目玉商品ずしお、孊校や病院など環境が充実しおいる緑ホヌムタりンの分譲地を売り出したした。この分譲地は耇数の区画に分割されおおり、奜きなだけ賌入できたすが、賌入する区画を合わせた土地の圢状は長方圢(正方圢を含む)でなければなりたせん。 山田ハりスは、党区画が完売した分譲地を管理するため賌入者ごずに境界線を匕き、その区画の 1぀に賌入者番号を曞いた看板を蚭眮しおおきたした。境界線は地面に朚の枝で線を匕いただけのものであったため、数日埌の豪雚により消えおしたい看板だけが残りたした。図 1 は看板のあった区画に、その区画を買った賌入者番号を蚘したものです。これでは分譲地がどのように買われたか分かりたせん。救いは事務所の匕き出しにあった賌入者番号 b ず賌入区画数 k のメモ(図 2)が芋぀かったこずです。 図巊看板の配眮、図2右メモ プログラマヌであるあなたは、プログラムを曞いお山田ハりスを助けるこずになりたした。分譲地の倧きさ X × Y 、賌入者数 n 、メモの情報 b 、 k 、及び看板の䜍眮情報 s を入力ずし、図 3 に瀺すように各区画の賌入者を出力するプログラムを䜜成しおください。 図3各区画の賌入者 䞎えられた情報に぀いお、以䞋の堎合は NA ず出力したす。 区画を区別する方法が存圚しない堎合 区画を区別する方法が耇数存圚する堎合 Input 耇数のデヌタセットの䞊びが入力ずしお䞎えられたす。入力の終わりはれロが぀の行で瀺されたす。各デヌタセットは以䞋の圢匏で䞎えられたす。 X Y n b 1 k 1 b 2 k 2 : b n k n s 11 s 21 ... s X1 s 12 s 22 ... s X2 : s 1Y s 2Y ... s XY 1 行目に X, Y, n (1 ≀ X, Y ≀ 10, 1 ≀ n ≀ 15) が䞎えられたす。続く n 行にメモの i 行目に曞かれた情報 b i (1 ≀ b i ≀ n ), k i (1 ≀ k i ≀ 100) が䞎えられたす。 続く Y 行に区画情報の i 行目の情報 s ij が䞎えられたす。 s ij は i 行目の巊から j 番目の区画の看板情報を衚したす。看板の情報 s ij ずしお、その区画に看板が無い堎合は 0 、看板がある堎合はその区画の賌入者番号が䞎えられたす。 デヌタセットの数は 50 を超えたせん。 Output 入力デヌタセットごずに、賌入者情報たたはNA を出力したす。 Sample Input 5 4 6 1 6 2 4 3 3 4 4 5 1 6 2 0 0 1 0 0 0 0 0 2 0 0 0 0 3 0 0 4 5 0 6 3 3 1 1 9 0 0 1 0 0 0 0 0 0 4 4 4 1 6 2 2 3 4 4 4 0 1 0 0 0 0 0 2 0 0 3 0 0 4 0 0 0 0 0 Output for the Sample Input 1 1 1 2 2 1 1 1 2 2 4 4 3 3 3 4 4 5 6 6 1 1 1 1 1 1 1 1 1 NA
38,686
Problem G: Rolling Dice The north country is conquered by the great shogun-sama (which means king). Recently many beautiful dice which were made by order of the great shogun-sama were given to all citizens of the country. All citizens received the beautiful dice with a tear of delight. Now they are enthusiastically playing a game with the dice. The game is played on grid of h * w cells that each of which has a number, which is designed by the great shogun-sama's noble philosophy. A player put his die on a starting cell and move it to a destination cell with rolling the die. After rolling the die once, he takes a penalty which is multiple of the number written on current cell and the number printed on a bottom face of the die, because of malicious conspiracy of an enemy country. Since the great shogun-sama strongly wishes, it is decided that the beautiful dice are initially put so that 1 faces top, 2 faces south, and 3 faces east. You will find that the number initially faces north is 5, as sum of numbers on opposite faces of a die is always 7. Needless to say, idiots those who move his die outside the grid are punished immediately. The great shogun-sama is pleased if some citizens can move the beautiful dice with the least penalty when a grid and a starting cell and a destination cell is given. Other citizens should be sent to coal mine (which may imply labor as slaves). Write a program so that citizens can deal with the great shogun-sama's expectations. Input The first line of each data set has two numbers h and w , which stands for the number of rows and columns of the grid. Next h line has w integers, which stands for the number printed on the grid. Top-left corner corresponds to northwest corner. Row number and column number of the starting cell are given in the following line, and those of the destination cell are given in the next line. Rows and columns are numbered 0 to h -1, 0 to w -1, respectively. Input terminates when h = w = 0. Output For each dataset, output the least penalty. Constraints 1 ≀ h , w ≀ 10 0 ≀ number assinged to a cell ≀ 9 the start point and the goal point are different. Sample Input 1 2 8 8 0 0 0 1 3 3 1 2 5 2 8 3 0 1 2 0 0 2 2 3 3 1 2 5 2 8 3 0 1 2 0 0 1 2 2 2 1 2 3 4 0 0 0 1 2 3 1 2 3 4 5 6 0 0 1 2 0 0 Output for the Sample Input 24 19 17 6 21
38,687
Low Range-Sum Matrix You received a card at a banquet. On the card, a matrix of $N$ rows and $M$ columns and two integers $K$ and $S$ are written. All the elements in the matrix are integers, and an integer at the $i$-th row from the top and the $j$-th column from the left is denoted by $A_{i,j}$. You can select up to $K$ elements from the matrix and invert the sign of the elements. If you can make a matrix such that there is no vertical or horizontal contiguous subsequence whose sum is greater than $S$, you can exchange your card for a prize. Your task is to determine if you can exchange a given card for a prize. Input The input consists of a single test case of the following form. $N$ $M$ $K$ $S$ $A_{1,1}$ $A_{1,2}$ ... $A_{1,M}$ : $A_{N,1}$ $A_{N,2}$ ... $A_{N,M}$ The first line consists of four integers $N, M, K$ and $S$ ($1 \leq N, M \leq 10, 1 \leq K \leq 5, 1 \leq S \leq 10^6$). The following $N$ lines represent the matrix in your card. The ($i+1$)-th line consists of $M$ integers $A_{i,1}, A_{i,2}, ..., A_{i, M}$ ($-10^5 \leq A_{i,j} \leq 10^5$). Output If you can exchange your card for a prize, print ' Yes '. Otherwise, print ' No '. Sample Input 1 3 3 2 10 5 3 7 2 6 1 3 4 1 Output for Sample Input 1 Yes The sum of a horizontal contiguous subsequence from $A_{1,1}$ to $A_{1,3}$ is $15$. The sum of a vertical contiguous subsequence from $A_{1,2}$ to $A_{3,2}$ is $13$. If you flip the sign of $A_{1,2}$, there is no vertical or horizontal contiguous subsequence whose sum is greater than $S$. Sample Input 2 2 3 1 5 4 8 -2 -2 -5 -3 Output for Sample Input 2 Yes Sample Input 3 2 3 1 5 9 8 -2 -2 -5 -3 Output for Sample Input 3 No Sample Input 4 2 2 3 100 0 0 0 0 Output for Sample Input 4 Yes
38,688
Dice II Construct a dice from a given sequence of integers in the same way as Dice I . You are given integers on the top face and the front face after the dice was rolled in the same way as Dice I . Write a program to print an integer on the right side face. Input In the first line, six integers assigned to faces are given in ascending order of their corresponding labels. In the second line, the number of questions $q$ is given. In the following $q$ lines, $q$ questions are given. Each question consists of two integers on the top face and the front face respectively. Output For each question, print the integer on the right side face. Constraints $0 \leq $ the integer assigned to a face $ \leq 100$ The integers are all different $1 \leq q \leq 24$ Sample Input 1 2 3 4 5 6 3 6 5 1 3 3 2 Sample Output 3 5 6
38,689
Problem E: アニペロ 猛暑が続いた、長かったようで短かったような倏がもうすぐ終わろうずしおいた。 そんな8月䞋旬のある日、ずある2D奜きな人物ずその先茩のslipは、アニペロサマヌラむブ、通称アニペロず呌ばれるむベントに参加しおいた。 アニペロずは、さたざたなアニメ゜ングアヌティストたちが集結する、日本囜内最倧のアニメ゜ングラむブむベントである。 今幎のアニペロは、公匏で公衚されおいたアヌティストの他に、シヌクレットで超豪華歌手も出挔し、倧盛況の内に幕を閉じた。 アニペロには初参戊だった2D奜きな圌は、ラむブ埌の䜙韻にひたり぀぀も、ひず぀の疑問を抱えおいた。 「アニペロに出挔するアヌティストはどのように決めおいるのだろうか」 圌は、数あるアヌティストの䞭から、出挔アヌティストの遞出をするのに、次のような方法があるのではないかず、遞出法を考えおみた。 たず、アニペロのアヌティストには、シヌクレットアヌティストずスタンダヌドアヌティストの2皮類の分類があるものずする。 シヌクレットアヌティストずは、ラむブに出挔するこずを事前公衚せず、ラむブ本番になっお突然珟れるアヌティストのこずを指す。 スタンダヌドアヌティストずは、ラむブに出挔するこずを事前に公衚しおよいアヌティストのこずを指す。 党おのアヌティストは、次のステヌタスを持぀。 アヌティスト名文字列 アヌティストを雇うための金額以䞋、雇甚金ず呌ぶ自然数 このアヌティストが出挔するこずで、お客をどれほど満足させられるか以䞋、満足床ず呌ぶ自然数 今回、ラむブに出挔するアヌティストを遞ぶために、シヌクレットアヌティスト候補 N 人、スタンダヌドアヌティスト候補 M 人が、すでに甚意されおいるものずする。さらに、ラむブの䞻催者は、アヌティストを雇甚するために䜿甚できる資金 LIMIT を持っおいるものずする。 䞻催者は、次の条件を満たすように、アヌティストを遞出しなければならない。 N 人のシヌクレットアヌティスト枠から、1人以䞊、2人以䞋を遞出する(1人or2人なのは、シヌクレットがいなかったり倚かったりするこずを避けるためである) M 人のスタンダヌドアヌティスト枠から、 X 人以䞊のアヌティストを遞出する アヌティストを党お遞出し終えたずき、雇甚金の合蚈を、 LIMIT 以䞋にする 遞出したアヌティスト党員で埗られる満足床の合蚈を最倧化する さお、ここたで遞出法を考えた2D奜きな圌は、この方法でプログラムを曞いおみようず思った。しかし、圌は遞出法を考えるのに気力を䜿っおしたい、プログラムを曞く気力がなくなっおしたったようなので、圌の代わりにプログラムを曞いおあげおほしい。 あなたの仕事は、䞊蚘の遞出法に埓い、アヌティストを遞出したずきのお客の最倧満足床を出力するプログラムを䜜成するこずである。 Input 入力は、耇数のデヌタセットからなる。デヌタセットの総数は20以䞋である。 各デヌタセットは、次の圢をしおいる。 LIMIT N M X SEC_NAME 1 SEC_E 1 SEC_S 1 ... SEC_NAME i SEC_E i SEC_S i ... SEC_NAME N SEC_E N SEC_S N NAME 1 E 1 S 1 ... NAME i E i S i ... NAME M E M S M 敎数 LIMIT ( 4 ≀ LIMIT ≀ 1000 )、 N ( 2 ≀ N ≀ 100 )、 M ( 2 ≀ M ≀ 100 )、 X ( 2 ≀ X ≀ M )は、 それぞれアヌティストを雇甚するのに䜿甚できる資金、シヌクレットアヌティスト候補の数、スタンダヌドアヌティスト候補の数、スタンダヌドアヌティストから遞出しなければならない最䜎人数を衚す。 SEC_NAME i 、 NAME i 、はそれぞれシヌクレットアヌティスト候補、スタンダヌドアヌティスト候補の名前を指す30文字以䞋の文字列である。 文字列には、アルファベット('A'-'Z', 'a'-'z')のみが䜿甚される。 同じアヌティスト名が2回以䞊出珟するこずはない。 敎数 SEC_E i 、 E i ( 1 ≀ SEC_E i , E i ≀ 10 )、 SEC_S i 、 S i ( 1 ≀ SEC_S i , S i ≀ 10 ) は、それぞれシヌクレットアヌティスト候補の雇甚金、スタンダヌドアヌティスト候補の雇甚金、シヌクレットアヌティスト候補が出挔したずきに埗られる満足床、スタンダヌドアヌティスト候補が出挔したずきに埗られる満足床を衚す。 入力の終了は、0が4぀の行で瀺される。このデヌタに぀いおは凊理する必芁はない。 Output 遞出法を適甚しおアヌティストを遞出したずき、お客の満足床の最倧倀を出力せよ。 アヌティストの遞出の仕方は、必ずあるず仮定しおよい。 Sample Input 100 2 2 2 A 5 6 B 7 8 C 1 2 D 3 4 27 2 3 3 A 8 10 B 8 10 C 6 7 D 5 4 E 8 9 27 2 3 2 A 8 10 B 8 10 C 6 7 D 5 4 E 8 9 44 3 6 5 YamatoNoHito 9 10 ZettoNoHito 9 10 TMR 10 10 SkillNoGroup 8 10 NanaSama 6 9 FRPSD 6 8 Magi3rdDeshi 5 7 Magi13thDeshi 2 2 MagicalItoh 4 6 0 0 0 0 Output for Sample Input 20 30 31 55
38,690
Movie Problem 倪郎君は倏䌑みの間、毎日぀の映画を近所の映画通で芋るこずにしたした。 倪郎君の倏䌑みは8月1日から8月31日たでの31日間ありたす。 その映画通では、倏䌑みの間に n ぀の映画が䞊映されるこずになっおいたす。 それぞれの映画には 1 から n たでの番号が割り圓おられおおり、 i 番目の映画は8月 a i 日から8月 b i 日の間だけ䞊映されたす。 倪郎君は映画を芋た時、それが初めお芋る映画だった堎合は 100 の幞犏床を埗るこずができたす。 しかし、過去に 1 床でも芋たこずのある映画だった堎合は 50 の幞犏床を埗たす。 倪郎君は䞊映される映画の予定衚をもずに、倏䌑みの蚈画を立おるこずにしたした。 倪郎君が埗られる幞犏床の合蚈倀が最倧になるように映画を芋たずきの合蚈倀を求めおください。 どの日も必ず1぀以䞊の映画が䞊映されおいるこずが保蚌されたす。 Input 入力は以䞋の圢匏で䞎えられる。 n a 1 b 1 a 2 b 2 ... a n b n 1行目に、1぀の敎数 n が䞎えられる。 2行目からの n 行のうち i 行目には 2 ぀の敎数 a i , b i が空癜区切りで䞎えられる。 Constraints 1 ≀ n ≀ 100 1 ≀ a i ≀ b i ≀ 31 (1 ≀ i ≀ n ) Output 倪郎君の埗られる幞犏床の合蚈倀の最倧倀を出力せよ。 Sample Input 1 4 1 31 2 2 2 3 3 3 Sample Output 1 1700 Sample Input 2 5 1 10 10 20 20 21 22 31 4 20 Sample Output 2 1800
38,691
Score : 1600 points Problem Statement Ringo got interested in modern art. He decided to draw a big picture on the board with N+2 rows and M+2 columns of squares constructed in the venue of CODE FESTIVAL 2017, using some people. The square at the (i+1) -th row and (j+1) -th column in the board is represented by the pair of integers (i,j) . That is, the top-left square is (0,0) , and the bottom-right square is (N+1,M+1) . Initially, the squares (x,y) satisfying 1 \leq x \leq N and 1 \leq y \leq M are painted white, and the other (outermost) squares are painted black. Ringo arranged people at some of the outermost squares, facing inward. More specifically, the arrangement of people is represented by four strings A , B , C and D , as follows: For each row except the top and bottom, if the i -th character (1 \leq i \leq N) in A is 1 , place a person facing right at the square (i,0) ; otherwise, do nothing. For each row except the top and bottom, if the i -th character (1 \leq i \leq N) in B is 1 , place a person facing left at the square (i,M+1) ; otherwise, do nothing. For each column except the leftmost and rightmost, if the i -th character (1 \leq i \leq M) in C is 1 , place a person facing down at the square (0,i) ; otherwise, do nothing. For each column except the leftmost and rightmost, if the i -th character (1 \leq i \leq M) in D is 1 , place a person facing up at the square (N+1,i) ; otherwise, do nothing. Each person has a sufficient amount of non-white paint. No two people have paint of the same color. An example of an arrangement of people (For convenience, black squares are displayed in gray) Ringo repeats the following sequence of operations until all people are dismissed from the venue. Select a person who is still in the venue. The selected person repeats the following action while the square in front of him/her is white: move one square forward, and paint the square he/she enters with his/her paint. When the square in front of him/her is not white, he/she stops doing the action. The person is now dismissed from the venue. An example of a way the board is painted How many different states of the board can Ringo obtain at the end of the process? Find the count modulo 998244353 . Two states of the board are considered different when there is a square painted in different colors. Constraints 1 \leq N,M \leq 10^5 |A|=|B|=N |C|=|D|=M A , B , C and D consist of 0 and 1 . Input Input is given from Standard Input in the following format: N M A B C D Output Print the number of the different states of the board Ringo can obtain at the end of the process, modulo 998244353 . Sample Input 1 2 2 10 01 10 01 Sample Output 1 6 There are six possible states as shown below. Sample Input 2 2 2 11 11 11 11 Sample Output 2 32 Sample Input 3 3 4 111 111 1111 1111 Sample Output 3 1276 Sample Input 4 17 21 11001010101011101 11001010011010111 111010101110101111100 011010110110101000111 Sample Output 4 548356548 Be sure to find the count modulo 998244353 . Sample Input 5 3 4 000 101 1111 0010 Sample Output 5 21 Sample Input 6 9 13 111100001 010101011 0000000000000 1010111111101 Sample Output 6 177856 Sample Input 7 23 30 01010010101010010001110 11010100100100101010101 000101001001010010101010101101 101001000100101001010010101000 Sample Output 7 734524988
38,692
Score : 600 points Problem Statement Given are two sequences A and B , both of length N . A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i ( 1 \leq i \leq N ) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints 1\leq N \leq 2 \times 10^5 1\leq A_i,B_i \leq N A and B are each sorted in the ascending order. All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print No . If there exists a reordering that satisfies the condition, print Yes on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Sample Input 1 6 1 1 1 2 2 3 1 1 1 2 2 3 Sample Output 1 Yes 2 2 3 1 1 1 Sample Input 2 3 1 1 2 1 1 3 Sample Output 2 No Sample Input 3 4 1 1 2 3 1 2 3 3 Sample Output 3 Yes 3 3 1 2
38,693
シヌルの重なり 1 蟺の長さが 10 の正方圢の折り玙に半埄 1 の円圢のシヌルを n 枚貌りたす。シヌルは重ねお貌るこずができたす。シヌルを貌る䜍眮の座暙を読み蟌んで、折り玙䞊でもっずも倚くシヌルが重なっおいる堎所(シヌルが枚だけでも"重なっおいる"ずする)でのシヌルの枚数を出力するプログラムを䜜成しおください。 折り玙の巊䞋を原点ずした x, y 座暙を䞎えたす。この x, y を円の䞭心ずしおシヌルを貌るこずずしたす。円の䞭心が折り玙の倖に出るこずはありたせん。たた、同䞀座暙に耇数のシヌルが貌られるこずはありたせん。 Input 耇数のデヌタセットが䞎えられたす。各デヌタセットは以䞋のような圢匏で䞎えられたす。 n x 1 , y 1 x 2 , y 2 : x n , y n 行目にシヌルの枚数 n (0 ≀ 100) が䞎えられたす。続く n 行に、各シヌルの䞭心座暙が䞎えられたす。 x i , y i は i 枚目のシヌルの䞭心の x 座暙ず y 座暙を衚したす。各倀は小数点以䞋最倧 6 桁たでの数字を含む実数で䞎えられたす。 n が 0 のずき、入力の最埌ずしたす。デヌタセットの数は 50 を超えたせん。 Output 各デヌタセットに察し、折り玙䞊で最も倚くシヌルが重なっおいる堎所でのシヌルの枚数敎数を出力しお䞋さい。 Sample Input 15 3.14979,8.51743 2.39506,3.84915 2.68432,5.39095 5.61904,9.16332 7.85653,4.75593 2.84021,5.41511 1.79500,8.59211 7.55389,8.17604 4.70665,4.66125 1.63470,4.42538 7.34959,4.61981 5.09003,8.11122 5.24373,1.30066 0.13517,1.83659 7.57313,1.58150 0 Output for the Sample Input 4 Hint 入力䟋のようにシヌルを貌った図です。円はシヌル、数字は入力䟋の行数を衚しおいたす。 点(2.3,4.6)では、入力䟋の 2 行目、3 行目、6 行目、10 行目の 4 枚のシヌルが重なっおいたす。 6 ず 9 のおのおのの䞭心の距離は 2.01293 なので、シヌルは重なっおいたせん。1 ず 12 のおのおのの䞭心の距離は 1.98231 なので、シヌルは重なっおいたす。 ぀の円が接しおいるずきおのおのの䞭心の距離が 2 のずきは、重なっおいるものずしたす。
38,694
Problem J: Demon's Plan BackGround デヌモンズプランでは慟を叞る108䜓の悪魔が日倜プログラミングコンテストでしのぎを削っおいる。䞻催であるパトロヌンは、党おの慟が集う堎所”リクスマグナ”に悪魔たちを招埅しようずしおいる。 パトロヌンはただ招埅を送っおいない悪魔に招埅を送りたいが、悪魔は䞖界各地におり、自分で行くのは面倒くさいので自身の䜿い魔達にやらせるこずにした。 パトロヌンは気が利くので䜿い魔同士がより平等に働くようにどの䜿い魔がどの悪魔を担圓するかを割り振ろうずしおいる。 Problem N 䜓の䜿い魔ず M 䜓の悪魔がいる。 䜿い魔 i ず 悪魔 j の距離は H i j である。 各悪魔に必ず1人の䜿い魔を向かわせたい。どの䜿い魔がどの悪魔を蚪問するかは以䞋の条件に埓っお割り振るこずにした。 䜿い魔1䜓あたりの蚪問する悪魔の数の差の最倧を最小化する。 1を満たした䞊で蚪問する悪魔ず担圓する䜿い魔の距離の最倧を最小化する。 1行目に䜿い魔1䜓あたりの蚪問する悪魔の数の差の最倧を、2行目に悪魔ず担圓する䜿い魔の距離の最倧を出力せよ。 Input 入力は以䞋の圢匏で䞎えられる。 N M H 0 0 H 0 1 
 H 0 M−1 H 1 0 H 1 1 
 H 1 M−1 . . H N−1 0 H N−1 1 
 H N−1 M−1 1行目に䜿い魔の数 N ず悪魔の数 M が敎数で空癜区切りで䞎えられる。 続く N 行に䜿い魔ず悪魔の距離が敎数で䞎えられる。 H ij は䜿い魔 i ず悪魔 j の距離を衚す。 Constraints 1 ≀ N ≀ 108 1 ≀ M ≀ 108 1 ≀ H i j ≀ 10 9 ( 0 ≀ i < N , 0 ≀ j < M ) ( 䜿い魔 i から悪魔 j たでの距離 ) Output 問題文の条件に埓い、1行目に䜿い魔1䜓あたりの蚪問する悪魔の数の差の最倧を、2行目に悪魔ず担圓する䜿い魔の距離の最倧を出力せよ。 Sample Input 1 3 3 1 2 3 2 1 2 3 2 1 Sample Output 1 0 1 䜿い魔 0 - 悪魔 0 䜿い魔 1 - 悪魔 1 䜿い魔 2 - 悪魔 2 ず割り振るこずで蚪問する悪魔の数の差が0になり、担圓䜿い魔ず悪魔の距離の最倧が1になり最適な組み合わせになる。 Sample Input 2 3 5 1 2 3 4 5 5 4 3 2 1 4 3 2 1 5 Sample Output 2 1 2 䜿い魔 0 - 悪魔 0 䜿い魔 0 - 悪魔 1 䜿い魔 1 - 悪魔 4 䜿い魔 2 - 悪魔 3 䜿い魔 2 - 悪魔 2 ず割り振るこずで蚪問する悪魔の差が1になり、担圓䜿い魔ず悪魔の距離の最倧が2になり最適な組み合わせになる。
38,695
Score : 500 points Problem Statement You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N . You would like to sort this sequence in ascending order by repeating the following operation: Choose an element in the sequence and move it to the beginning or the end of the sequence. Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation. Constraints 1 \leq N \leq 2\times 10^5 (P_1,P_2,...,P_N) is a permutation of (1,2,...,N) . All values in input are integers. Input Input is given from Standard Input in the following format: N P_1 : P_N Output Print the minimum number of operations required. Sample Input 1 4 1 3 2 4 Sample Output 1 2 For example, the sequence can be sorted in ascending order as follows: Move 2 to the beginning. The sequence is now (2,1,3,4) . Move 1 to the beginning. The sequence is now (1,2,3,4) . Sample Input 2 6 3 2 5 1 4 6 Sample Output 2 4 Sample Input 3 8 6 3 1 2 7 4 8 5 Sample Output 3 5
38,696
Problem D : Numbers n が䞎えられるので、 n 個の連続した正の敎数を求めよ。 ただしすべおの数が、ずその数自身以倖の玄数をもたなくおはならない。 Input 入力は以䞋のフォヌマットで䞎えられる。 n 入力は以䞋の制玄を満たす。 1 ≀ n ≀ 1,500 Output 最初の行に、あなたが遞んだ連続した n 個の正の敎数の䞭で䞀番小さいものを出力せよ。 2行目から n+1 行目に、それぞれの倀に察する玄数を出力せよ。 玄数は1かその数自身でなければどの倀を出力しおも良い。 1行目に出力した数を x ずしお、i行目には x+i-2 の玄数を出力せよ。 出力する倀は5,000桁を超えおはいけない。 Sample Input 1 2 Sample Output 1 8 2 3 Sample Input 2 3 Sample Output 2 8 2 3 5 Hint Sample Output 2では、8,9,10を3個の連続した敎数ずしお遞んでいる。 2行目に、8の玄数ずしお2,3行目は9の玄数ずしお3,行目には10の玄数ずしお5,を出力しおいる。
38,697
Problem Statement Fox Ciel is practicing miniature golf, a golf game played with a putter club only. For improving golf skills, she believes it is important how well she bounces the ball against walls. The field of miniature golf is in a two-dimensional plane and surrounded by $N$ walls forming a convex polygon. At first, the ball is placed at $(s_x, s_y)$ inside the field. The ball is small enough to be regarded as a point. Ciel can shoot the ball to any direction and stop the ball whenever she wants. The ball will move in a straight line. When the ball hits the wall, it rebounds like mirror reflection (i.e. incidence angle equals reflection angle). For practice, Ciel decided to make a single shot under the following conditions: The ball hits each wall of the field exactly once. The ball does NOT hit the corner of the field. Count the number of possible orders in which the ball hits the walls. Input The input contains several datasets. The number of datasets does not exceed $100$. Each dataset is in the following format. $N$ $s_x$ $s_y$ $x_1$ $y_1$ : : $x_N$ $y_N$ The first line contains an integer $N$ ($3 \leq N \leq 8$). The next line contains two integers $s_x$ and $s_y$ ($-50 \leq s_x, s_y \leq 50$), which describe the coordinates of the initial position of the ball. Each of the following $N$ lines contains two integers $x_i$ and $y_i$ ($-50 \leq x_i, y_i \leq 50$), which describe the coordinates of each corner of the field. The corners are given in counterclockwise order. You may assume given initial position $(s_x, s_y)$ is inside the field and the field is convex. It is guaranteed that there exists a shoot direction for each valid order of the walls that satisfies the following condition: distance between the ball and the corners of the field $(x_i, y_i)$ is always greater than $10^{-6}$ until the ball hits the last wall. The last dataset is followed by a line containing a single zero. Output For each dataset in the input, print the number of valid orders of the walls in a line. Sample Input 4 0 0 -10 -10 10 -10 10 10 -10 10 0 Output for the Sample Input 8
38,698
Score : 500 points Problem Statement For a finite set of integers X , let f(X)=\max X - \min X . Given are N integers A_1,...,A_N . We will choose K of them and let S be the set of the integers chosen. If we distinguish elements with different indices even when their values are the same, there are {}_N C_K ways to make this choice. Find the sum of f(S) over all those ways. Since the answer can be enormous, print it \bmod (10^9+7) . Constraints 1 \leq N \leq 10^5 1 \leq K \leq N |A_i| \leq 10^9 Input Input is given from Standard Input in the following format: N K A_1 ... A_N Output Print the answer \bmod (10^9+7) . Sample Input 1 4 2 1 1 3 4 Sample Output 1 11 There are six ways to choose S : \{1,1\},\{1,3\},\{1,4\},\{1,3\},\{1,4\}, \{3,4\} (we distinguish the two 1 s). The value of f(S) for these choices are 0,2,3,2,3,1 , respectively, for the total of 11 . Sample Input 2 6 3 10 10 10 -10 -10 -10 Sample Output 2 360 There are 20 ways to choose S . In 18 of them, f(S)=20 , and in 2 of them, f(S)=0 . Sample Input 3 3 1 1 1 1 Sample Output 3 0 Sample Input 4 10 6 1000000000 1000000000 1000000000 1000000000 1000000000 0 0 0 0 0 Sample Output 4 999998537 Print the sum \bmod (10^9+7) .
38,699
Problem D: Organize Your Train In the good old Hachioji railroad station located in the west of Tokyo, there are several parking lines, and lots of freight trains come and go every day. All freight trains travel at night, so these trains containing various types of cars are settled in your parking lines early in the morning. Then, during the daytime, you must reorganize cars in these trains according to the request of the railroad clients, so that every line contains the “right” train, i.e. the right number of cars of the right types, in the right order. As shown in Figure 7, all parking lines run in the East-West direction. There are exchange lines connecting them through which you can move cars. An exchange line connects two ends of different parking lines. Note that an end of a parking line can be connected to many ends of other lines. Also note that an exchange line may connect the East-end of a parking line and the West-end of another. Cars of the same type are not discriminated between each other. The cars are symmetric, so directions of cars don’t matter either. You can divide a train at an arbitrary position to make two sub-trains and move one of them through an exchange line connected to the end of its side. Alternatively, you may move a whole train as is without dividing it. Anyway, when a (sub-) train arrives at the destination parking line and the line already has another train in it, they are coupled to form a longer train. Your superautomatic train organization system can do these without any help of locomotive engines. Due to the limitation of the system, trains cannot stay on exchange lines; when you start moving a (sub-) train, it must arrive at the destination parking line before moving another train. In what follows, a letter represents a car type and a train is expressed as a sequence of letters. For example in Figure 8, from an initial state having a train " aabbccdee " on line 0 and no trains on other lines, you can make " bbaadeecc " on line 2 with the four moves shown in the figure. To cut the cost out, your boss wants to minimize the number of (sub-) train movements. For example, in the case of Figure 8, the number of movements is 4 and this is the minimum. Given the configurations of the train cars in the morning (arrival state) and evening (departure state), your job is to write a program to find the optimal train reconfiguration plan. Input The input consists of one or more datasets. A dataset has the following format: x y p 1 P 1 q 1 Q 1 p 2 P 2 q 2 Q 2 . . . p y P y q y Q y s 0 s 1 . . . s x -1 t 0 t 1 . . . t x -1 x is the number of parking lines, which are numbered from 0 to x -1. y is the number of exchange lines. Then y lines of the exchange line data follow, each describing two ends connected by the exchange line; p i and q i are integers between 0 and x - 1 which indicate parking line numbers, and P i and Q i are either " E " (East) or " W " (West) which indicate the ends of the parking lines. Then x lines of the arrival (initial) configuration data, s 0 , ... , s x -1 , and x lines of the departure (target) configuration data, t 0 , ... t x -1 , follow. Each of these lines contains one or more lowercase letters " a ", " b ", ..., " z ", which indicate types of cars of the train in the corresponding parking line, in west to east order, or alternatively, a single " - " when the parking line is empty. You may assume that x does not exceed 4, the total number of cars contained in all the trains does not exceed 10, and every parking line has sufficient length to park all the cars. You may also assume that each dataset has at least one solution and that the minimum number of moves is between one and six, inclusive. Two zeros in a line indicate the end of the input. Output For each dataset, output the number of moves for an optimal reconfiguration plan, in a separate line. Sample Input 3 5 0W 1W 0W 2W 0W 2E 0E 1E 1E 2E aabbccdee - - - - bbaadeecc 3 3 0E 1W 1E 2W 2E 0W aabb bbcc aa bbbb cc aaaa 3 4 0E 1W 0E 2E 1E 2W 2E 0W ababab - - aaabbb - - 0 0 Output for the Sample Input 4 2 5
38,700
Score : 800 points Problem Statement Takahashi and Aoki will play a game using a grid with H rows and W columns of square cells. There are N obstacles on this grid; the i -th obstacle is at (X_i,Y_i) . Here, we represent the cell at the i -th row and j -th column (1 \leq i \leq H, 1 \leq j \leq W) by (i,j) . There is no obstacle at (1,1) , and there is a piece placed there at (1,1) . Starting from Takahashi, he and Aoki alternately perform one of the following actions: Move the piece to an adjacent cell. Here, let the position of the piece be (x,y) . Then Takahashi can only move the piece to (x+1,y) , and Aoki can only move the piece to (x,y+1) . If the destination cell does not exist or it is occupied by an obstacle, this action cannot be taken. Do not move the piece, and end his turn without affecting the grid. The game ends when the piece does not move twice in a row. Takahashi would like to perform as many actions (including not moving the piece) as possible before the game ends, while Aoki would like to perform as few actions as possible before the game ends. How many actions will Takahashi end up performing? Constraints 1 \leq H,W \leq 2\times 10^5 0 \leq N \leq 2\times 10^5 1 \leq X_i \leq H 1 \leq Y_i \leq W If i \neq j , (X_i,Y_i) \neq (X_j,Y_j) (X_i,Y_i) \neq (1,1) X_i and Y_i are integers. Input Input is given from Standard Input in the following format: H W N X_1 Y_1 : X_N Y_N Output Print the number of actions Takahashi will end up performing. Sample Input 1 3 3 1 3 2 Sample Output 1 2 For example, the game proceeds as follows: Takahashi moves the piece to (2,1). Aoki does not move the piece. Takahashi moves the piece to (3,1). Aoki does not move the piece. Takahashi does not move the piece. Takahashi performs three actions in this case, but if both players play optimally, Takahashi will perform only two actions before the game ends. Sample Input 2 10 10 14 4 3 2 2 7 3 9 10 7 7 8 1 10 10 5 4 3 4 2 8 6 4 4 4 5 8 9 2 Sample Output 2 6 Sample Input 3 100000 100000 0 Sample Output 3 100000
38,702
問題 G XOR 回路 問題文 蚈算機科孊実隓及挔習 3 は CAD を甚いお CPU を蚭蚈する授業であるCPU は倚くの回路を組み合わせなければ動かずその䞭の 1 ぀に n ビット入力の XOR 回路があるここで XOR 回路ずは入力ビット列 x 1 x 2 ...x n に察しお x 1 + x 2 + ... + x n \ (mod 2) を出力する回路のこずを蚀うしかし完璧に動䜜する XOR 回路を蚭蚈するのは時間がかかるのでずりあえず n ビットのうち k ビットだけ䜿う XOR 回路 A を䜜るこずにした぀たりある i 1 , ... , i k が存圚し回路 A は x i 1 + x i 2 + ... + x i k \ (mod 2) を出力する 暫く埌に今床は k ビット入力の XOR 回路が欲しくなった䜕だ簡単ではないか先ほどの回路 A を䜿えばよいただ残念なこずに回路 A がどの k ビットを䜿っおいたのか忘れおしたった䞊に回路 A の蚭蚈図も間違っお削陀しおしたったしかしコンパむル枈みの回路 A は残っおいるなので入力 x 1 x 2 ...x n を入れお回路 A を実行するこずでその出力 x i 1 + x i 2 + ... + x i k \ (mod 2) を芋るこずは出来る 出来るだけ䜜業の時間を短くしたいので回路 A の実行回数には䞊限を蚭定するこずにしようどうすれば回路Aが䟝存しおいるビット i 1 , ... , i k を芋぀けられるだろうか 入出力 最初に n ず k がスペヌス区切りで䞎えられる以降プログラムは回路 A に入力を䞎えその出力を読むこずが出来る䟋えば C/C++ で回路 A にビット列 x 1 x 2 ...x n を䞎えるには printf("? x 1 x 2 ...x n \n"); fflush(stdout); ずするここで各 x i の間にスペヌスをいれおはならない 次に scanf("%d", &v); ずするず v に察応する出力 x i 1 + x i 2 + ... + x i k \ (mod 2) が入る 最終的に回路 A が䟝存するビット i 1 ,...,i k を出力するには printf("! i 1 i 2 ...i k \n"); fflush(stdout); ずするここで各 i j の間はスペヌスを䞁床 1 ぀ず぀いれる 制玄 1 ≀ n ≀ 10,000 1 ≀ k ≀ min(10, n) 各デヌタセットごずに回路 A の実行回数の䞊限は 200 回でありそれを超えるず誀答 ( Query Limit Exceeded ) ず刀定される 入出力䟋 入力䟋 1 以䞋の䟋はプログラムの入出力の䟋である巊の列はプログラムの出力右の列はプログラムぞの入力を時系列順に瀺しおいる最初に n k が入力ずしお䞎えられるここでは n = 2, k = 1 である次に回路 A に 00 ずいう入力をいれるず回路 A は 0 を返した次に回路 A に 01 ずいう入力をいれるず回路 A は再び 0 を返したこのこずから回路 A は 1 ビット目のみ利甚しおいるこずが分かりプログラムは 1 を解答ずしお出力した プログラムの出力 プログラムぞの入力 2 1 ?00 0 ?01 0 !1 入力䟋 2 以䞋の䟋では n = 2, k = 2 であり盎ちに回路 A が 1 ビット目ず 2 ビット目の䞡方を利甚しおいるこずが分かるよっおプログラムは 1 ず 2 を解答ずしお出力した プログラムの出力 プログラムぞの入力 2 2 !1 2
38,703
Score : 800 points Problem Statement There are N positive integers arranged in a circle. Now, the i -th number is A_i . Takahashi wants the i -th number to be B_i . For this objective, he will repeatedly perform the following operation: Choose an integer i such that 1 \leq i \leq N . Let a, b, c be the (i-1) -th, i -th, and (i+1) -th numbers, respectively. Replace the i -th number with a+b+c . Here the 0 -th number is the N -th number, and the (N+1) -th number is the 1 -st number. Determine if Takahashi can achieve his objective. If the answer is yes, find the minimum number of operations required. Constraints 3 \leq N \leq 2 \times 10^5 1 \leq A_i, B_i \leq 10^9 All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 ... A_N B_1 B_2 ... B_N Output Print the minimum number of operations required, or -1 if the objective cannot be achieved. Sample Input 1 3 1 1 1 13 5 7 Sample Output 1 4 Takahashi can achieve his objective by, for example, performing the following operations: Replace the second number with 3 . Replace the second number with 5 . Replace the third number with 7 . Replace the first number with 13 . Sample Input 2 4 1 2 3 4 2 3 4 5 Sample Output 2 -1 Sample Input 3 5 5 6 5 2 1 9817 1108 6890 4343 8704 Sample Output 3 25
38,704
Score : 800 points Problem Statement There are X+Y+Z people, conveniently numbered 1 through X+Y+Z . Person i has A_i gold coins, B_i silver coins and C_i bronze coins. Snuke is thinking of getting gold coins from X of those people, silver coins from Y of the people and bronze coins from Z of the people. It is not possible to get two or more different colors of coins from a single person. On the other hand, a person will give all of his/her coins of the color specified by Snuke. Snuke would like to maximize the total number of coins of all colors he gets. Find the maximum possible number of coins. Constraints 1 \leq X 1 \leq Y 1 \leq Z X+Y+Z \leq 10^5 1 \leq A_i \leq 10^9 1 \leq B_i \leq 10^9 1 \leq C_i \leq 10^9 Input Input is given from Standard Input in the following format: X Y Z A_1 B_1 C_1 A_2 B_2 C_2 : A_{X+Y+Z} B_{X+Y+Z} C_{X+Y+Z} Output Print the maximum possible total number of coins of all colors he gets. Sample Input 1 1 2 1 2 4 4 3 2 1 7 6 7 5 2 3 Sample Output 1 18 Get silver coins from Person 1 , silver coins from Person 2 , bronze coins from Person 3 and gold coins from Person 4 . In this case, the total number of coins will be 4+2+7+5=18 . It is not possible to get 19 or more coins, and the answer is therefore 18 . Sample Input 2 3 3 2 16 17 1 2 7 5 2 16 12 17 7 7 13 2 10 12 18 3 16 15 19 5 6 2 Sample Output 2 110 Sample Input 3 6 2 4 33189 87907 277349742 71616 46764 575306520 8801 53151 327161251 58589 4337 796697686 66854 17565 289910583 50598 35195 478112689 13919 88414 103962455 7953 69657 699253752 44255 98144 468443709 2332 42580 752437097 39752 19060 845062869 60126 74101 382963164 Sample Output 3 3093929975
38,705
How many ways? Write a program which identifies the number of combinations of three integers which satisfy the following conditions: You should select three distinct integers from 1 to n . A total sum of the three integers is x . For example, there are two combinations for n = 5 and x = 9. 1 + 3 + 5 = 9 2 + 3 + 4 = 9 Input The input consists of multiple datasets. For each dataset, two integers n and x are given in a line. The input ends with two zeros for n and x respectively. Your program should not process for these terminal symbols. Constraints 3 ≀ n ≀ 100 0 ≀ x ≀ 300 Output For each dataset, print the number of combinations in a line. Sample Input 5 9 0 0 Sample Output 2 Note 解説
38,706
珍しい郜垂(Unique Cities) JOI 囜には$N$ 個の郜垂があり1 から$N$ たでの番号が぀いおいるこれらの郜垂は$N - 1$ 本の道路で結ばれおいる$i$ 番目($1 \leq i \leq N - 1$) の道路は郜垂$A_i$ ず郜垂$B_i$ を結んでおり双方向に通行可胜であるどの郜垂からどの郜垂ぞも䜕本かの道路を通行するこずで移動できる JOI 囜にはいく぀かの特産品が存圚する特産品には皮類を衚す1 以䞊$M$ 以䞋の番号が付けられおいる(JOI 囜で生産されおいる特産品に察応しおいない番号があるかもしれない)各郜垂は1 ぀の特産品を生産しおおり郜垂$j$ ($1 \leq j \leq N$) では特産品$C_j$ を生産しおいる耇数の郜垂が同じ皮類の特産品を生産するこずがあるかもしれない 2 ぀の郜垂の間の距離はその間を移動するために通る道路の本数の最小倀である郜垂$x$ ($1 \leq x \leq N$)から芋お郜垂$y$ ($1 \leq y \leq N, y \ne x$) が珍しい郜垂であるずはすべおの郜垂$z$ ($1 \leq z \leq N, z \ne x, z \ne y) に぀いお郜垂$x, y$ 間の距離ず郜垂$x, z$ 間の距離が異なるこずを意味する JOI 囜の倧臣であるK 理事長はすべおの$j$ ($1 \leq j \leq N$) に぀いお郜垂$j$ から芋お珍しい郜垂で生産されおいる特産品が䜕皮類あるかを知りたい JOI 囜の道路の情報ず各郜垂で生産されおいる特産品の番号が䞎えられたずき各郜垂ごずにその郜垂から芋お珍しい郜垂で生産されおいる特産品が䜕皮類あるかを求めるプログラムを䜜成せよ 入力 入力は以䞋の圢匏で暙準入力から䞎えられる $N$ $M$ $A_1$ $B_1$ : $A_{N-1}$ $B_{N-1}$ $C_1$ ... $C_N$ 出力 暙準出力に$N$ 行で出力せよ $j$ 行目($1 \leq j \leq N$) には郜垂$j$ から芋お珍しい郜垂で生産されおいる特産品が䜕皮類あるかを出力せよ 制玄 $ 2 \leq N \leq 200 000$ $ 1 \leq M \leq N$ $ 1 \leq A_i \leq N (1 \leq i \leq N - 1)1 \leq B_i \leq N (1 \leq i \leq N - 1)$ $ A_i \ne B_i (1 \leq i \leq N - 1)$ どの郜垂からどの郜垂ぞも䜕本かの道路を通行するこずで移動できる $ 1 \leq C_j \leq M (1 \leq j \leq N)$ 入出力䟋 入力䟋1 5 4 1 2 2 3 3 4 3 5 1 2 1 2 4 出力䟋1 2 0 1 1 1 郜垂1 から芋お珍しい郜垂は郜垂2; 3 でありそこで生産される特産品は特産品2; 1 なので答えは2皮類である 郜垂2 から芋お珍しい郜垂は存圚しないので答えは0 皮類である 郜垂3 から芋お珍しい郜垂は郜垂1 でありそこで生産される特産品は特産品1 なので答えは1 皮類である 郜垂4 から芋お珍しい郜垂は郜垂1; 3 でありどちらの郜垂においおも生産される特産品は特産品1 なので答えは1 皮類である 郜垂5 から芋お珍しい郜垂は郜垂1; 3 でありどちらの郜垂においおも生産される特産品は特産品1 なので答えは1 皮類である 番号3 の特産品は存圚しないこずに泚意せよ 入力䟋2 7 1 1 2 2 3 3 4 4 5 5 6 6 7 1 1 1 1 1 1 1 出力䟋2 1 1 1 0 1 1 1 入力䟋3 10 10 2 6 5 8 10 8 1 4 10 6 4 5 10 7 6 9 3 7 1 2 3 4 5 6 7 8 9 10 出力䟋3 4 3 4 2 0 2 2 0 3 2 入力䟋4 22 12 9 6 12 13 4 20 21 22 3 19 2 9 6 18 18 11 18 3 16 2 6 4 3 17 16 10 8 16 22 1 16 14 15 8 9 21 2 12 21 5 12 7 1 1 4 8 4 11 7 6 7 11 6 11 10 4 7 5 3 12 9 6 12 2 出力䟋4 2 0 1 1 1 1 1 0 0 1 2 0 1 1 2 0 2 1 2 3 0 0 情報オリンピック日本委員䌚䜜 『第18 回日本情報オリンピック(JOI 2018/2019) 本遞』
38,707
F: Absum 問題 長さ $N$ の数列 $A$ が䞎えられるあなたは高々 $M$ 回たで数列の $i$ 番目ず $j$ 番目$0 \leq i, j \leq N-1$の芁玠を入れ替える操䜜を行うこずができる 操䜜を行なっおできる数列の $\sum_{i = 0}^{N - 1} abs(A_i - i)$ の最倧倀を求めよ 制玄 $2 \leq N \leq 10^5$ $0 \leq M \leq N$ $0 \leq A_i \leq 10^5$ 入力圢匏 入力は以䞋の圢匏で䞎えられる. $N\ M$15:15修正 $A_0\ A_1\ A_2\ \dots\ A_{N - 1}$ 出力 操䜜を行なっおできる数列の $\sum_{i = 0}^{N - 1} abs(A_i - i)$ の最倧倀を求めよ.たた, 末尟に改行も出力せよ. サンプル サンプル入力 1 5 2 0 3 2 1 4 サンプル出力 1 12 $0$ 番目の芁玠ず $4$ 番目の芁玠に操䜜を行うず操䜜埌の数列は $(4, 3, 2, 1, 0)$ ずなり $|4 - 0| + |3 - 1| + |2 - 2| + |1 - 3| + |0 - 4| = 12$ で最倧ずなる必ずしも操䜜を $M$ 回行う必芁がない こずに泚意せよ サンプル入力 2 3 2 0 0 0 サンプル出力 2 3 操䜜を行うこずなく最倧倀ずなる サンプル入力 3 6 2 1 0 3 6 5 4 サンプル出力 3 20
38,708