task
stringlengths
0
154k
__index_level_0__
int64
0
39.2k
Score : 300 points Problem Statement We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N) ). There are N! possible permutations of size N . Among them, let P and Q be the a -th and b -th lexicographically smallest permutations, respectively. Find |a - b| . Notes For two sequences X and Y , X is said to be lexicographically smaller than Y if and only if there exists an integer k such that X_i = Y_i~(1 \leq i < k) and X_k < Y_k . Constraints 2 \leq N \leq 8 P and Q are permutations of size N . Input Input is given from Standard Input in the following format: N P_1 P_2 ... P_N Q_1 Q_2 ... Q_N Output Print |a - b| . Sample Input 1 3 1 3 2 3 1 2 Sample Output 1 3 There are 6 permutations of size 3 : (1,~2,~3) , (1,~3,~2) , (2,~1,~3) , (2,~3,~1) , (3,~1,~2) , and (3,~2,~1) . Among them, (1,~3,~2) and (3,~1,~2) come 2 -nd and 5 -th in lexicographical order, so the answer is |2 - 5| = 3 . Sample Input 2 8 7 3 5 4 2 1 6 8 3 8 2 5 4 6 7 1 Sample Output 2 17517 Sample Input 3 3 1 2 3 1 2 3 Sample Output 3 0
38,816
壊れた暗号生成噚 JAG (Japanese Alumni Group) は倚くのプログラマで構成される謎の組織でありこの組織の本郚がある建物に入るためには毎回ある機械によっお生成される暗号文を解かなくおはならない この暗号文は' + '' - '' [ '' ] ' の蚘号ず倧文字のアルファベットからなっおおり以䞋の BNF で定矩される <Cipher> によっお衚される <Cipher> ::= <String> | <Cipher><String> <String> ::= <Letter> | '['<Cipher>']' <Letter> ::= '+'<Letter> | '-'<Letter> | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z' ここでそれぞれの蚘号は以䞋のような意味を衚す +(文字) : その文字の次のアルファベットを衚す (ただし ' Z ' の次のアルファベットは ' A ' であるずする) -(文字) : その文字の前のアルファベットを衚す (ただし ' A ' の前のアルファベットは ' Z ' であるずする) [(文字列)] : その文字列を巊右反転した文字列を衚す しかしこの暗号文を生成する機械には珟圚故障が発生しおおり暗号文のうちアルファベットの箇所が数文字壊れお読めなくなっおいる堎合がある読めない文字は仮に ' ? ' ず衚されおいる 調査の結果壊れた文字の埋め方は埩号埌の文字列が埩号埌の文字列ずしおありえる文字列の䞭で蟞曞順最小になるようなものであるこずがわかった あなたの仕事はこの暗号文を正しく埩号するこずである Input 入力は耇数のデヌタセットから構成される 各デヌタセットは䞊蚘の BNF で定矩された暗号文においお䞀郚の倧文字のアルファベットが ‘?’ に眮き換えられた文字列を含む 1 行からなる 各文字列の長さは $80$ 以䞋であるず仮定しおよい たた各デヌタセットに含たれる ' ? ' の数は $0$ 以䞊 $3$ 以䞋であるず仮定しおよい 入力の終了は ' . ' の1文字だけを含む行で衚される Output 各デヌタセットに察しお埩号埌の文字列が蟞曞順最小になるように暗号文を埩号したずきの埩号埌の文字列を出力せよ Sample Input A+A++A Z-Z--Z+-Z [ESREVER] J---?---J ++++++++A+++Z-----------A+++Z [[++-+--?[--++-?++-+++L]][-+-----+-O]]++++---+L . Output for Sample Input ABC ZYXZ REVERSE JAG ICPC JAPAN
38,817
Halting Problem A unique law is enforced in the Republic of Finite Loop. Under the law, programs that never halt are regarded as viruses. Releasing such a program is a cybercrime. So, you want to make sure that your software products always halt under their normal use. It is widely known that there exists no algorithm that can determine whether an arbitrary given program halts or not for a given arbitrary input. Fortunately, your products are based on a simple computation model given below. So, you can write a program that can tell whether a given program based on the model will eventually halt for a given input. The computation model for the products has only one variable $x$ and $N + 1$ states, numbered $1$ through $N + 1$. The variable $x$ can store any integer value. The state $N + 1$ means that the program has terminated. For each integer $i$ ($1 \leq i \leq N$), the behavior of the program in the state $i$ is described by five integers $a_i$, $b_i$, $c_i$, $d_i$ and $e_i$ ($c_i$ and $e_i$ are indices of states). On start of a program, its state is initialized to $1$, and the value of $x$ is initialized by $x_0$, the input to the program. When the program is in the state $i$ ($1 \leq i \leq N$), either of the following takes place in one execution step: if $x$ is equal to $a_i$, the value of $x$ changes to $x + b_i$ and the program state becomes $c_i$; otherwise, the value of $x$ changes to $x + d_i$ and the program state becomes $e_i$. The program terminates when the program state becomes $N + 1$. Your task is to write a program to determine whether a given program eventually halts or not for a given input, and, if it halts, to compute how many steps are executed. The initialization is not counted as a step. Input The input consists of a single test case of the following format. $N$ $x_0$ $a_1$ $b_1$ $c_1$ $d_1$ $e_1$ . . . $a_N$ $b_N$ $c_N$ $d_N$ $e_N$ The first line contains two integers $N$ ($1 \leq N \leq 10^5$) and $x_0$ ($−10^{13} \leq x_0 \leq 10^{13}$). The number of the states of the program is $N + 1$. $x_0$ is the initial value of the variable $x$. Each of the next $N$ lines contains five integers $a_i$, $b_i$, $c_i$, $d_i$ and $e_i$ that determine the behavior of the program when it is in the state $i$. $a_i$, $b_i$ and $d_i$ are integers between $−10^{13}$ and $10^{13}$, inclusive. $c_i$ and $e_i$ are integers between $1$ and $N + 1$, inclusive. Output If the given program eventually halts with the given input, output a single integer in a line which is the number of steps executed until the program terminates. Since the number may be very large, output the number modulo $10^9 + 7$. Output $-1$ if the program will never halt. Sample Input 1 2 0 5 1 2 1 1 10 1 3 2 2 Sample Output 1 9 Sample Input 2 3 1 0 1 4 2 3 1 0 1 1 3 3 -2 2 1 4 Sample Output 2 -1 Sample Input 3 3 3 1 -1 2 2 2 1 1 1 -1 3 1 1 4 -2 1 Sample Output 3 -1
38,818
Score : 100 points Problem Statement Three people, A, B and C, are trying to communicate using transceivers. They are standing along a number line, and the coordinates of A, B and C are a , b and c (in meters), respectively. Two people can directly communicate when the distance between them is at most d meters. Determine if A and C can communicate, either directly or indirectly. Here, A and C can indirectly communicate when A and B can directly communicate and also B and C can directly communicate. Constraints 1 ≀ a,b,c ≀ 100 1 ≀ d ≀ 100 All values in input are integers. Input Input is given from Standard Input in the following format: a b c d Output If A and C can communicate, print Yes ; if they cannot, print No . Sample Input 1 4 7 9 3 Sample Output 1 Yes A and B can directly communicate, and also B and C can directly communicate, so we should print Yes . Sample Input 2 100 10 1 2 Sample Output 2 No They cannot communicate in this case. Sample Input 3 10 10 10 1 Sample Output 3 Yes There can be multiple people at the same position. Sample Input 4 1 100 2 10 Sample Output 4 Yes
38,819
Problem M: Settler Problem 二次元平面䞊に N 個の空き地がある。空き地にはそれぞれ1から N たでの番号が割り振られおいる。どの空き地もずおも小さいので、点ずみなすこずができる。 i 番目の空き地は( x i , y i )に存圚しおいる。 倪郎君はこの N 個の空き地の䞭からちょうど K 個を遞び、それらの空き地に建物を建おるこずにした。 しかし、あたりにも近い堎所に耇数の建物を建おおも面癜くないず思ったので、倪郎君はそれぞれの空き地どうしのナヌクリッド距離が必ず2以䞊ずなるように空き地を遞ぶこずにした。 倪郎君が遞ぶ空き地の組み合わせずしお考えられるものを出力するプログラムを䜜成せよ。 組み合わせが耇数存圚する堎合は、蟞曞順で最小のものを出力せよ。 ただし、どのように K 個の空き地を遞んだずしおも、いずれか2぀の空き地のナヌクリッド距離が2より小さくなっおしたう堎合は、かわりに-1を出力せよ。 Input 入力は以䞋の圢匏で䞎えられる。 N K x 1 y 1 x 2 y 2 ... x N y N Constraints 入力は以䞋の条件を満たす。 入力は党お敎数である。 2 ≀ K ≀ N ≀ 6,000 1 ≀ x i , y i ≀ 1,000,000 ( 1 ≀ i ≀ N ) x i mod 2 = floor ( y i ÷ 2 ) mod 2 ( 1 ≀ i ≀ N ) (ここで floor ( y i ÷ 2 ) ずは y i を2で割り小数点以䞋を切り捚おた倀である) 同じ座暙に耇数の空き地が存圚するこずはない。 Output 倪郎君が遞ぶ空き地の番号を昇順に1行ず぀出力せよ。 Sample Input 1 3 2 2 1 1 2 1 3 Sample Output 1 1 3 Sample Input 2 4 3 2 1 1 2 1 3 2 4 Sample Output 2 -1 Sample Input 3 5 3 5 7 5 6 6 8 20 20 4 8 Sample Output 3 2 3 4
38,820
未熟者の数匏 博士 : ピヌタヌ君、぀いにやったよ。 ピヌタヌ : たたですか。今床はどんなくだらない発明ですか。 博士 : ぀いに数匏を蚈算機で凊理する画期的な方法を思い぀いたんだ。この衚をみおごらん。 通垞の蚘法 博士の「画期的な」蚘法 1 + 2 1 2 + 3 * 4 + 7 3 4 * 7 + 10 / ( 2 - 12 ) 10 2 12 - / ( 3 - 4 ) * ( 7 + 2 * 3 ) 3 4 - 7 2 3 * + * ピヌタヌ : はぁ。 博士 : ふっふっふ。これだけでは、未熟者の君には䜕のこずだかわからないだろうねえ。ここからが肝心なんじゃ。 ピヌタヌ : っおいうか・・・。 博士 : 蚈算機にはスタックずいうデヌタ構造があるこずは君も知っおいるね。ほれ、「先入れ埌出し」のあれじゃよ。 ピヌタヌ : はい。知っおたすが、あの・・・。 博士 : この画期的な蚘法はあのスタックを䜿うんじゃ。䟋えばこの 10 2 12 - / だが、次のように凊理する。 凊理察象 10 2 12 - / ↓ ↓ ↓ ↓2-12 ↓10/-10 スタック . . 10 . 2 10 12 2 10 . -10 10 . . -1 博士 : どうじゃな。括匧も挔算子の優先順䜍も気にする必芁がないじゃろう語順も「10 を 2 から 12 を匕いたもので割る。」ずなり、䜕ずなく圌の極東の島囜の蚀葉、日本語ず䌌おおるじゃろうお。 この画期的な発明さえあれば、我が研究宀は安泰じゃお。ファファファ。 ピヌタヌ : っおいうか博士。これっお日本にいたずき䌚接倧孊の基瀎コヌスで習いたしたよ。「逆ポヌランド蚘法」ずかいっお、みんな簡単にプログラムしおたした。 博士 : ・・・。 ずいうこずで、ピヌタヌ君に倉わっお博士に、このプログラムを教える事になりたした。「逆ポヌランド蚘法」で曞かれた数匏を入力ずし、蚈算結果を出力するプログラムを䜜成しおください。 入力 耇数のデヌタセットが䞎えられたす。各デヌタセットでは、逆ポヌランド蚘法による数匏敎数ず挔算蚘号が空癜文字文字半角で区切られた80文字以内の文字列が  行に䞎えられたす。 ある倀を 0 や 0 に限りなく近い倀で割るような数匏は䞎えられたせん。 デヌタセットの数は 50 を超えたせん。 出力 各デヌタセットごずに、蚈算結果実数を行に出力しおください。なお、蚈算結果は 0.00001 以䞋の誀差を含んでもよい。 Sample Input 10 2 12 - / 3 4 - 7 2 3 * + * -1 -2 3 + + Output for the Sample Input -1.000000 -13.000000 0.000000
38,821
Score : 300 points Problem Statement There are N cities on a 2D plane. The coordinate of the i -th city is (x_i, y_i) . Here (x_1, x_2, \dots, x_N) and (y_1, y_2, \dots, y_N) are both permuations of (1, 2, \dots, N) . For each k = 1,2,\dots,N , find the answer to the following question: Rng is in City k . Rng can perform the following move arbitrarily many times: move to another city that has a smaller x -coordinate and a smaller y -coordinate, or a larger x -coordinate and a larger y -coordinate, than the city he is currently in. How many cities (including City k ) are reachable from City k ? Constraints 1 \leq N \leq 200,000 (x_1, x_2, \dots, x_N) is a permutation of (1, 2, \dots, N) . (y_1, y_2, \dots, y_N) is a permutation of (1, 2, \dots, N) . All values in input are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 x_2 y_2 : x_N y_N Output Print N lines. In i -th line print the answer to the question when k = i . Sample Input 1 4 1 4 2 3 3 1 4 2 Sample Output 1 1 1 2 2 Rng can reach City 4 from City 3 , or conversely City 3 from City 4 . Sample Input 2 7 6 4 4 3 3 5 7 1 2 7 5 2 1 6 Sample Output 2 3 3 1 1 2 3 2
38,822
D: Two Colors Sort 問題 umg 君は散歩䞭に 1,2, ... ,N を䞊び倉えおできる長さ N の数列 P_1, P_2, ..., P_N を芋぀けたした。 umg 君は䞍思議な力を䜿うこずで、同じ色に塗られた異なる数を二぀遞んで堎所を亀換するこずができたす。 umg 君は、数列に含たれる数のうち R 個を赀に、残りの N-R 個を青に塗るこずで数列を昇順に䞊び替えられるようにしたいず考えたした。 umg 君が目暙を達成できるかどうか刀定しおください。 ただし、数字はずおも重いので䞍思議な力を䜿わずに動かすこずはできたせん。たた、umg 君は倩才なので䞍思議な力を任意の回数䜿うこずができたす。 入力圢匏 N R P_1 P_2 ... P_N 制玄 1 \leq N \leq 3 \times 10^5 1 \leq R \leq N 1\leq P_i \leq N P_i \neq P_j ( 1 \leq i < j \leq N ) 入力は党お敎数である。 出力圢匏 umg 君が目的を達成できるなら Yes 、そうでなければ No を䞀行に出力せよ。 入力䟋 1 3 2 1 3 2 出力䟋 1 Yes 1 を青に、 2,3 を赀に塗るこずで目暙が達成できたす。 入力䟋 2 5 2 1 2 3 4 5 出力䟋 2 Yes 初めから昇順に䞊んでいたす。 入力䟋 3 10 7 3 4 8 5 7 6 2 10 1 9 出力䟋 3 No
38,823
茪ゎム n 本の釘を平板䞊の座暙 P 1 ( x 1 , y 1 ), P 2 ( x 2 , y 2 ), P 3 ( x 3 , y 3 ),..., P n ( x n , y n ) に本ず぀打ち、茪ゎムの茪の䞭に党おの釘が入るように 1 本の茪ゎムで囲みたす。このずき、茪ゎムが亀差しおはいけたせん。 釘の座暙を読み蟌んで、䞊蚘のように釘を茪ゎムで囲んだずきに茪ゎムに接しおいない釘の本数を出力するプログラムを䜜成しおください。茪ゎムは充分に䌞び瞮みするものずしたす。同じ座暙に 2 本以䞊の釘を打぀こずはないものずしたす。たた、茪ゎムがかかった釘ず釘の間は盎線で結ばれるものずし、その盎線䞊に 3 本以䞊の釘が䞊ぶこずはないものずしたす。䟋えば、図 1 に瀺すような入力はありえたせん。図 2 に瀺すように茪ゎムがかかっおいない釘が 1 盎線䞊に䞊ぶこずはありえたす。 図 図 ただし、それぞれの座暙倀は -1000.0 以䞊1000.0 以䞋の実数です。たた、 n は 3 以䞊 100 以䞋の敎数です。 Input 耇数のデヌタセットが䞎えられたす。各デヌタセットは以䞋のような圢匏です䞎えられたす。 n x 1 , y 1 x 2 , y 2 ... ... x n , y n n が 0 の時、入力の最埌を瀺したす。デヌタセットの数は 50 を超えたせん。 Output デヌタセットごずに、ゎムず接しおいない釘の本数を出力しおください。 䟋えば、図 3 に瀺す぀の釘を衚す入力があった堎合、図 4 のように囲たれるので、茪ゎムに接しおいない釘の本数は 1 本です。 図 図 Sample Input 4 1.0,0.0 0.0,1.0 2.0,1.0 1.0,2.0 9 -509.94,892.63 567.62,639.99 -859.32,-64.84 -445.99,383.69 667.54,430.49 551.12,828.21 -940.2,-877.2 -361.62,-970 -125.42,-178.48 0 Output for the Sample Input 0 3 Hint 以䞋は぀めのサンプル入力に察する図です。
38,824
Boring Commercial Now it is spring holidays. A lazy student has finally passed all final examination, and he decided to just kick back and just watch TV all day. Oh, his only source of entertainment is watching TV. And TV commercial, as usual, are a big nuisance for him. He can watch any thing on television, but cannot bear even a single second of commercial. So to prevent himself from the boredom of seeing the boring commercial, he keeps shuffling through the TV channels, so that he can watch programs on different channels without seeing even a single commercial. Given the number of channels, and the duration at which the TV commercials are showed on each of the channels, you have to write a program which will print the longest interval for which the lazy student can watch the television by shuffling between the different channels without ever seeing an TV commercial. For example, consider the simplified situation where there are only three television channels, and suppose that he is watching TV from 2100 hrs to 2400 hrs. Suppose that the commercials are displayed at following time on each of the channels. Channel 1: 2100 to 2130, 2200 to 2230 and 2300 to 2330 Channel 2: 2130 to 2200, 2330 to 2400 Channel 3: 2100 to 2130, 2330 to 2400 Then in this case, he can watch TV without getting interrupted by commercials for full 3 hours by watching Channel 2 from 2100 to 2130, then Channel 3 from 2130 to 2330, and then Channel 1 from 2330 to 2400. Input The input will consist of several cases. In each case, the first line of the input will be n, the number of channels, which will then be followed by p and q, the time interval between which he will be watching the TV. It will be followed by 2n lines, giving the time slots for each of the channels. For each channel, the first line will be k, the number of commercial slots, and it will then be followed by 2k numbers giving the commercial slots in order. The input will be terminated by values 0 for each of n, p, q. This case should not be processed. Output For each case, you have to output the maximum duration (in minutes) for which he can watch television without seeing any commercial. Sample Input 1 2100 2400 1 2130 2200 3 2100 2400 3 2100 2130 2200 2230 2300 2330 2 2130 2200 2330 2400 2 2100 2130 2330 2400 0 0 0 Output for the Sample Input 120 180
38,825
Print a Chessboard Draw a chessboard which has a height of H cm and a width of W cm. For example, the following figure shows a chessboard which has a height of 6 cm and a width of 10 cm. #.#.#.#.#. .#.#.#.#.# #.#.#.#.#. .#.#.#.#.# #.#.#.#.#. .#.#.#.#.# Note that the top left corner should be drawn by '#'. 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 chessboard made of '#' and '.'. Print a blank line after each dataset. Constraints 1 ≀ H ≀ 300 1 ≀ W ≀ 300 Sample Input 3 4 5 6 3 3 2 2 1 1 0 0 Sample Output #.#. .#.# #.#. #.#.#. .#.#.# #.#.#. .#.#.# #.#.#. #.# .#. #.# #. .# #
38,826
通孊経路 問題 倪郎君の䜏んでいるJOI垂は南北方向にたっすぐに䌞びる a 本の道路ず東西方向にたっすぐに䌞びる b 本の道路により碁盀の目の圢に区分けされおいる 南北方向の a 本の道路には西から順に 1, 2, ... , a の番号が付けられおいるたた東西方向の b 本の道路には南から順に 1, 2, ... , b の番号が付けられおいる西から i 番目の南北方向の道路ず南から j 番目の東西方向の道路が亀わる亀差点を (i, j) で衚す 倪郎君は亀差点 (1, 1) の近くに䜏んでおり亀差点 (a, b) の近くのJOI高校に自転車で通っおいる自転車は道路に沿っおのみ移動するこずができる倪郎君は通孊時間を短くするため東たたは北にのみ向かっお移動しお通孊しおいる 珟圚 JOI垂では n 個の亀差点 (x 1 , y 1 ), (x 2 , y 2 ), ... , (x n , y n ) で工事を行っおいる倪郎君は工事䞭の亀差点を通るこずができない 倪郎君が亀差点 (1, 1) から亀差点 (a, b) たで工事䞭の亀差点を避けながら東たたは北にのみ向かっお移動しお通孊する方法は䜕通りあるだろうか倪郎君の通孊経路の個数 m を求めるプログラムを䜜成せよ 入力 入力は耇数のデヌタセットからなる各デヌタセットは以䞋の圢匏で䞎えられる入力はれロを぀含む行で終了する 1行目には空癜を区切りずしお2個の敎数 a, b が曞かれおいるこれは南北方向の道路の本数ず東西方向の道路の本数を衚す a, b は 1 ≀ a, b ≀ 16 をみたす 2行目には, 工事䞭の亀差点の個数を衚す敎数 n が曞かれおいる n は 1 ≀ n ≀ 40 をみたす 続く n 行 (3行目から n+2 行目) には工事䞭の亀差点の䜍眮が曞かれおいる i+2 行目には空癜で区切られた敎数 x i , y i が曞かれおおり亀差点 (x i , y i ) が工事䞭であるこずを衚す x i , y i は 1 ≀ x i , y i ≀ 16 をみたす デヌタセットの数は 5 を超えない 出力 デヌタセットごずに, 倪郎君の通孊経路の個数 m を1行に出力する 入出力䟋 入力䟋 5 4 3 2 2 2 3 4 2 5 4 3 2 2 2 3 4 2 0 0 出力䟋 5 5 䞋図は a=5, b=4, n=3 で工事䞭の亀差点が (2,2), (2,3), (4,2) の堎合を衚しおいる この堎合通孊経路は m=5 通りある 5通りの通孊経路を党お図瀺するず以䞋の通り 䞊蚘問題文ず自動審刀に䜿われるデヌタは、 情報オリンピック日本委員䌚 が䜜成し公開しおいる問題文ず採点甚テストデヌタです。
38,827
Problem Statement You are now participating in the Summer Training Camp for Programming Contests with your friend Jiro, who is an enthusiast of the ramen chain SIRO. Since every SIRO restaurant has its own tasteful ramen, he wants to try them at as many different restaurants as possible in the night. He doesn't have plenty of time tonight, however, because he has to get up early in the morning tomorrow to join a training session. So he asked you to find the maximum number of different restaurants to which he would be able to go to eat ramen in the limited time. There are $n$ railway stations in the city, which are numbered $1$ through $n$. The station $s$ is the nearest to the camp venue. $m$ pairs of stations are directly connected by the railway: you can move between the stations $a_i$ and $b_i$ in $c_i$ minutes in the both directions. Among the stations, there are $l$ stations where a SIRO restaurant is located nearby. There is at most one SIRO restaurant around each of the stations, and there are no restaurants near the station $s$. It takes $e_i$ minutes for Jiro to eat ramen at the restaurant near the station $j_i$. It takes only a negligibly short time to go back and forth between a station and its nearby SIRO restaurant. You can also assume that Jiro doesn't have to wait for the ramen to be served in the restaurants. Jiro is now at the station $s$ and have to come back to the station in $t$ minutes. How many different SIRO's can he taste? Input The input is a sequence of datasets. The number of the datasets does not exceed $100$. Each dataset is formatted as follows: $n$ $m$ $l$ $s$ $t$ $a_1$ $b_1$ $c_1$ : : $a_m$ $b_m$ $c_m$ $j_1$ $e_1$ : : $j_l$ $e_l$ The first line of each dataset contains five integers: $n$ for the number of stations, $m$ for the number of directly connected pairs of stations, $l$ for the number of SIRO restaurants, $s$ for the starting-point station, and $t$ for the time limit for Jiro. Each of the following $m$ lines contains three integers: $a_i$ and $b_i$ for the connected stations, and $c_i$ for the time it takes to move between the two stations. Each of the following $l$ lines contains two integers: $j_i$ for the station where a SIRO restaurant is located, and $e_i$ for the time it takes for Jiro to eat at the restaurant. The end of the input is indicated by a line with five zeros, which is not included in the datasets. The datasets satisfy the following constraints: $2 \le n \le 300$ $1 \le m \le 5{,}000$ $1 \le l \le 16$ $1 \le s \le n$ $1 \le t \le 100{,}000$ $1 \le a_i, b_i \le n$ $1 \le c_i \le 1{,}000$ $1 \le j_i \le n$ $1 \le e_i \le 15$ $s \ne j_i$ $j_i$'s are distinct. $a_i \ne b_i$ $(a_i, b_i) \ne (a_j, b_j)$ and $(a_i, b_i) \ne (b_j, a_j)$ for any $i \ne j$ Note that there may be some stations not reachable from the starting point $s$. Output For each data set, output the maximum number of different restaurants where Jiro can go within the time limit. Sample Input 2 1 1 1 10 1 2 3 2 4 2 1 1 1 9 1 2 3 2 4 4 2 2 4 50 1 2 5 3 4 5 2 15 3 15 4 6 3 1 29 1 2 20 3 2 10 4 1 5 3 1 5 2 4 3 3 4 4 2 1 4 5 3 3 0 0 0 0 0 Output for the Sample Input 1 0 1 3
38,828
Score : 200 points Problem Statement Takahashi loves numbers divisible by 2 . You are given a positive integer N . Among the integers between 1 and N (inclusive), find the one that can be divisible by 2 for the most number of times. The solution is always unique. Here, the number of times an integer can be divisible by 2 , is how many times the integer can be divided by 2 without remainder. For example, 6 can be divided by 2 once: 6 -> 3 . 8 can be divided by 2 three times: 8 -> 4 -> 2 -> 1 . 3 can be divided by 2 zero times. Constraints 1 ≀ N ≀ 100 Input Input is given from Standard Input in the following format: N Output Print the answer. Sample Input 1 7 Sample Output 1 4 4 can be divided by 2 twice, which is the most number of times among 1 , 2 , ..., 7 . Sample Input 2 32 Sample Output 2 32 Sample Input 3 1 Sample Output 3 1 Sample Input 4 100 Sample Output 4 64
38,829
Score : 600 points Problem Statement We have a set S of N points in a two-dimensional plane. The coordinates of the i -th point are (x_i, y_i) . The N points have distinct x -coordinates and distinct y -coordinates. For a non-empty subset T of S , let f(T) be the number of points contained in the smallest rectangle, whose sides are parallel to the coordinate axes, that contains all the points in T . More formally, we define f(T) as follows: f(T) := (the number of integers i (1 \leq i \leq N) such that a \leq x_i \leq b and c \leq y_i \leq d , where a , b , c , and d are the minimum x -coordinate, the maximum x -coordinate, the minimum y -coordinate, and the maximum y -coordinate of the points in T ) Find the sum of f(T) over all non-empty subset T of S . Since it can be enormous, print the sum modulo 998244353 . Constraints 1 \leq N \leq 2 \times 10^5 -10^9 \leq x_i, y_i \leq 10^9 x_i \neq x_j (i \neq j) y_i \neq y_j (i \neq j) All values in input are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 : x_N y_N Output Print the sum of f(T) over all non-empty subset T of S , modulo 998244353 . Sample Input 1 3 -1 3 2 1 3 -2 Sample Output 1 13 Let the first, second, and third points be P_1 , P_2 , and P_3 , respectively. S = \{P_1, P_2, P_3\} has seven non-empty subsets, and f has the following values for each of them: f(\{P_1\}) = 1 f(\{P_2\}) = 1 f(\{P_3\}) = 1 f(\{P_1, P_2\}) = 2 f(\{P_2, P_3\}) = 2 f(\{P_3, P_1\}) = 3 f(\{P_1, P_2, P_3\}) = 3 The sum of these is 13 . Sample Input 2 4 1 4 2 1 3 3 4 2 Sample Output 2 34 Sample Input 3 10 19 -11 -3 -12 5 3 3 -15 8 -14 -9 -20 10 -9 0 2 -7 17 6 -6 Sample Output 3 7222 Be sure to print the sum modulo 998244353 .
38,830
問題 H : キャッシュ戊略 今G○○gle Code Jam の地区倧䌚が始たろうずしおいる 斜め右前の垭に座っおいる男の ID は lyrically ず蚀うらしい 東京倧孊時代の蚘憶に䌌たような ID の仲間が居た芚えがあるが僕の仲間は䞀人残さず矎少女だったはずだ 僕の蚘憶の䞭の lyrically はアルゎリズムの力や実装の力もさながら 気合で問題に正解するこずにも定評があった 䟋えば蚈算量が倚少悪いプログラムでも䞊手な実装をするこずで高速にし 正解ずするようなこずも埗意ずしおいた プログラムを高速にする䞊で非垞に倧切になっおくるのが キャッシュメモリずの芪和性である 問題 ブロック毎に読み蟌みのコストが異なるメモリを考える 起こる党おのメモリアクセスがあらかじめ分かった状態での フルア゜シアティブのキャッシュメモリでの最善なキャッシュ戊略を求めたい 以䞋平易な蚀葉で説明する M 個の箱ず N 個のボヌルがある ボヌルには 1 から N たでの番号が付いおおり 各ボヌル i には重さ w i が決たっおいる たた長さ K の数列 a 1 , a 2 , 
, a K が䞎えられる 各 a j は 1 ≀ a j ≀ N を満たす敎数である はじめは党おの箱は空である j = 1, 2, 
, K の順に以䞋を行いたい ボヌル a j が入っおいる箱があれば䜕もしない この操䜜にかかるコストは 0 である ボヌル a j が入っおいる箱がなければいずれかの箱にボヌル a j を入れる ボヌルを箱に入れる際もし既にその箱に入っおいるボヌルがあればそのボヌルは倖に出す この操䜜にかかるコストは w a j であるコストは入れる箱や取り出すボヌル等にはよらない コストの和の最小倀を蚈算するプログラムを䜜成せよ 入力 入力の最初の行は 3 ぀の敎数 M , N , K を含む 続く N 行の i 行目には敎数 w i が曞かれおいる 続く K 行の j 行目には敎数 a j が曞かれおいる 出力 コストの和の最小倀を出力せよ 制玄 1 ≀ M ≀ 10 1 ≀ N ≀ 10 4 1 ≀ K ≀ 10 4 1 ≀ w i ≀ 10 4 1 ≀ a j ≀ N 郚分点 この問題の刀定には20 点分のテストケヌスのグルヌプが蚭定されおいる このグルヌプに含たれるテストケヌスの入力は以䞋を満たす 1 ≀ M ≀ 3 1 ≀ N ≀ 10 1 ≀ K ≀ 10 3 入出力䟋 入力䟋 1 入力䟋 1: 3 3 6 10 20 30 1 2 3 1 2 3 入力䟋 1 に察する出力䟋: 60 入力䟋 2 入力䟋 2: 2 3 6 10 20 30 1 2 3 1 2 3 入力䟋 2 に察する出力䟋: 80
38,831
Problem G: ゚ナゞヌ・トランスポヌタヌ ずある研究所で ゚ネルギヌ䌝達甚の媒䜓の開発をしおいた この媒䜓は図3に瀺すような特殊な物質からなる ポリマヌ構造をなしおいる (-α-Ea-β-) n 図3: ゚ネルギヌ䌝達甚媒䜓の構造 図の Ea で瀺した郚分がこの媒䜓のもっずも特城的な郚䜍の ゚ナゞヌアキュムレヌタ (Energy Accumulator) である このEa基は 1 kJ 幅で離散化された倚様な゚ネルギヌ状態を取るこずができる あるEa基を励起させるず そのEa基のα偎に結合しおいる隣接したEa基に蓄積されおいる党゚ネルギヌを β偎に結合しおいる隣接したEa基に移動させるような効果を持぀ 発熱反応が匕き起こされる図4 この反応の際励起されるEa基の゚ネルギヌが 1 kJ 消費される なおポリマヌの䞡端に䜍眮するEa基や゚ネルギヌ状態が 0 kJ になっおいる Ea基に察しおは励起反応は発生しないこず およびEa基は十分に倧きな゚ネルギヌを蓄えるこずが可胜であるこずが知られおいる 図4: 䞭倮のEa基を励起させたずきの反応 この性質を利甚するこずで゚ネルギヌの䌝達を可胜にしようず考えおいたのだが ゚ネルギヌを効率よく䌝達するには各Ea基を励起させる順番が重芁であるこずに 研究者たちは気が぀いたのである 幞い励起させる順番や回数は任意に制埡できるのだが 圌らには最適な励起手順がわからない そこで圌らの発想の足がかりずしお 初期状態の゚ネルギヌ分垃に察しお 最右Ea基β末端からもっずも近いEa基 に蓄えられうる最倧の゚ネルギヌ量を蚈算しおもらいたい Input 入力は耇数のデヌタセットから構成され 以䞋のような圢匏で䞎えられる N C 1 C 2 ... C N 入力の先頭の敎数 N (0 < N ≀ 60) が取り扱う問題のデヌタセット数であり その埌ろ 2 N 行に枡っお それぞれのデヌタセットごずの情報 C k が䞎えられる それぞれのデヌタセット C k は以䞋のような圢匏で 2行に枡り䞎えられる L E 1 E 2 ... E L L はそれぞれのデヌタセットで取り扱う媒䜓のEa鎖の長さであり ここで䞎えられた数だけEa基が盎列に結合しおいるこずを意味しおいる その次の行の L 個の敎数 E k は 長さ L のEa鎖のうちα末端を巊端に据えたずきに 巊から数えお k 番目のEa鎖にはじめに蓄積されおいる゚ネルギヌ量を kJ単䜍で瀺したものである ここで 0 ≀ E k ≀ 4, 1 ≀ L ≀ 80 であるこずが保蚌されおいる Output 出力は各デヌタセットごずに䞎えられた状況䞋での右端Ea鎖に到達可胜な 最倧゚ネルギヌをkJ単䜍で敎数倀のみを1行で蚘述するこず Sample Input 7 1 2 2 1 2 3 4 1 4 3 4 0 4 5 4 1 4 0 4 5 4 1 4 1 4 5 4 2 4 0 4 Output for the Sample Input 2 2 8 4 7 12 11
38,832
Score : 200 points Problem Statement Niwango-kun is an employee of Dwango Co., Ltd. One day, he is asked to generate a thumbnail from a video a user submitted. To generate a thumbnail, he needs to select a frame of the video according to the following procedure: Get an integer N and N integers a_0, a_1, ..., a_{N-1} as inputs. N denotes the number of the frames of the video, and each a_i denotes the representation of the i -th frame of the video. Select t -th frame whose representation a_t is nearest to the average of all frame representations. If there are multiple such frames, select the frame with the smallest index. Find the index t of the frame he should select to generate a thumbnail. Constraints 1 \leq N \leq 100 1 \leq a_i \leq 100 All numbers given in input are integers Input Input is given from Standard Input in the following format: N a_{0} a_{1} ... a_{N-1} Output Print the answer. Sample Input 1 3 1 2 3 Sample Output 1 1 Since the average of frame representations is 2 , Niwango-kun needs to select the index 1 , whose representation is 2 , that is, the nearest value to the average. Sample Input 2 4 2 5 2 5 Sample Output 2 0 The average of frame representations is 3.5 . In this case, every frame has the same distance from its representation to the average. Therefore, Niwango-kun should select index 0 , the smallest index among them.
38,833
Airport Codes 空枯コヌド JAG王囜では囜内の空枯にそれぞれ空枯コヌドを割り圓おお識別をしおいる 空枯コヌドは小文字の英語アルファベットで衚蚘した空枯の名前をもずに以䞋の芏則で割り圓おられる: 名前の最初の文字ず母音 (a,i,u,e,o) の盎埌の文字を順に取り出す 取り出した文字列が k 文字未満ならそれを空枯コヌドずし k 文字以䞊ならその取り出した文字列の先頭 k 文字を空枯コヌドずしお䜿う 䟋えば k = 3 のずきhaneda には hnd  oookayama には ooo  tsu には t ずいうコヌドが割り圓おられる しかしこのコヌドの割り圓お方では違う名前の空枯でも同じコヌドが割り圓おられるこずがあり混乱を招いおしたう 空枯の名前の䞀芧が䞎えられるのですべおの空枯のコヌドが異なるようにできるか刀定しお可胜な堎合はすべおの空枯コヌドが異なるようにできる最小の k を求め䞍可胜な堎合はその旚を䌝えるプログラムを䜜成せよ Input 入力は100個以䞋のデヌタセットからなる それぞれのデヌタセットは次の圢匏で䞎えられる n s 1 ... s n 1行目に空枯の数 n (2 ≀ n ≀ 50) が敎数で䞎えられ続く n 行にはそれぞれ空枯の名前 s i が文字列で䞎えられる 空枯の名前は' a 'から' z 'の小文字の英語アルファベットのみで構成されいずれも文字数は1以䞊50以䞋である たた䞎えられる空枯の名前はすべお異なるすなわち1 ≀ i < j ≀ n のずき s i ≠ s j を満たす 入力の終わりは1぀のれロだけからなる行で瀺される Output それぞれのデヌタセットに぀いおすべおの空枯に盞異なる空枯コヌドを割り圓おられるずきはそのような最小の k を1行に出力せよ 䞍可胜な堎合は-1を1行に出力せよ Sample Input 3 haneda oookayama tsu 2 azusa azishirabe 2 snuke snake 4 haneda honda hanamaki hawaii 0 Output for Sample Input 1 4 -1 3
38,834
Problem B: Red and Black There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles. Write a program to count the number of black tiles which he can reach by repeating the moves described above. Input The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H ; W and H are the numbers of tiles in the x - and y - directions, respectively. W and H are not more than 20. There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows. '.' - a black tile '#' - a red tile '@' - a man on a black tile(appears exactly once in a data set) The end of the input is indicated by a line consisting of two zeros. Output For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself). Sample Input 6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0 Output for the Sample Input 45 59 6 13
38,835
棒で䜜る盎方䜓 アむヅ攟送協䌚の教育番組(教育)では、子ども向けの工䜜番組「あそんで぀くろ」を攟送しおいたす。今回は棒で箱を䜜る回ですが、甚意した本の棒を䜿っお盎方䜓ができるかを確かめたいず思いたす。ただし、棒は切ったり折ったりしおはいけたせん。 本の棒の長さが䞎えられるので、それらすべおを蟺ずする盎方䜓が䜜れるかどうか刀定するプログラムを䜜成せよ。 Input 入力は以䞋の圢匏で䞎えられる。 e 1 e 2 ... e 12 入力は行からなり、各棒の長さを衚す敎数 e i (1 ≀ e i ≀ 100) が䞎えられる。 Output 盎方䜓を䜜成できる堎合には「yes」を、䜜成できない堎合には「no」を出力する。ただし、立方䜓は盎方䜓の䞀皮なので、立方䜓の堎合でも「yes」ず出力する。 Sample Input 1 1 1 3 4 8 9 7 3 4 5 5 5 Sample Output 1 no Sample Input 2 1 1 2 2 3 1 2 3 3 3 1 2 Sample Output 2 yes
38,836
Score : 400 points Problem Statement Mr. Takahashi has a string s consisting of lowercase English letters. He repeats the following operation on s exactly K times. Choose an arbitrary letter on s and change that letter to the next alphabet. Note that the next letter of z is a . For example, if you perform an operation for the second letter on aaz , aaz becomes abz . If you then perform an operation for the third letter on abz , abz becomes aba . Mr. Takahashi wants to have the lexicographically smallest string after performing exactly K operations on s . Find the such string. Constraints 1≀|s|≀10^5 All letters in s are lowercase English letters. 1≀K≀10^9 Input The input is given from Standard Input in the following format: s K Output Print the lexicographically smallest string after performing exactly K operations on s . Sample Input 1 xyz 4 Sample Output 1 aya For example, you can perform the following operations: xyz , yyz , zyz , ayz , aya . Sample Input 2 a 25 Sample Output 2 z You have to perform exactly K operations. Sample Input 3 codefestival 100 Sample Output 3 aaaafeaaivap
38,837
Score : 600 points Problem Statement A bracket sequence is a string that is one of the following: An empty string; The concatenation of ( , A , and ) in this order, for some bracket sequence A ; The concatenation of A and B in this order, for some non-empty bracket sequences A and B / Given are N strings S_i . Can a bracket sequence be formed by concatenating all the N strings in some order? Constraints 1 \leq N \leq 10^6 The total length of the strings S_i is at most 10^6 . S_i is a non-empty string consisting of ( and ) . Input Input is given from Standard Input in the following format: N S_1 : S_N Output If a bracket sequence can be formed by concatenating all the N strings in some order, print Yes ; otherwise, print No . Sample Input 1 2 ) (() Sample Output 1 Yes Concatenating (() and ) in this order forms a bracket sequence. Sample Input 2 2 )( () Sample Output 2 No Sample Input 3 4 ((())) (((((( )))))) ()()() Sample Output 3 Yes Sample Input 4 3 ((( ) ) Sample Output 4 No
38,838
Gift Exchange Party A gift exchange party will be held at a school in TKB City. For every pair of students who are close friends, one gift must be given from one to the other at this party, but not the other way around. It is decided in advance the gift directions, that is, which student of each pair receives a gift. No other gift exchanges are made. If each pair randomly decided the gift direction, some might receive countless gifts, while some might receive only few or even none. You'd like to decide the gift directions for all the friend pairs that minimize the difference between the smallest and the largest numbers of gifts received by a student. Find the smallest and the largest numbers of gifts received when the difference between them is minimized. When there is more than one way to realize that, find the way that maximizes the smallest number of received gifts. Input The input consists of at most 10 datasets, each in the following format. n m u 1 v 1 ... u m v m n is the number of students, and m is the number of friendship relations (2 ≀ n ≀ 100, 1 ≀ m ≀ n ( n -1)/2). Students are denoted by integers between 1 and n , inclusive. The following m lines describe the friendship relations: for each i , student u i and v i are close friends ( u i < v i ). The same friendship relations do not appear more than once. The end of the input is indicated by a line containing two zeros. Output For each dataset, output a single line containing two integers l and h separated by a single space. Here, l and h are the smallest and the largest numbers, respectively, of gifts received by a student. Sample Input 3 3 1 2 2 3 1 3 4 3 1 2 1 3 1 4 4 6 1 2 1 3 1 4 2 3 3 4 2 4 0 0 Output for the Sample Input 1 1 0 1 1 2
38,839
問題名 今 n 枚の数字が曞かれたカヌドがありたす。これらの䞀郚たたは党郚を適圓に䞊べお数字を䜜るこずを考えたす。この時䜜られる数字を党お足した数を求めお䞋さい。 䟋えば、 1 ず 2 があったら、䜜られる数字は 1, 2, 12, 21 の 4 ぀なので、党お足した数は 36 になりたす。䞊べた結果同じ数字が出来おも違う䞊べ方だずしたら別々に足したす。たずえば、 1 ずいうカヌドず 11 ずいうカヌドがあったら䞊べお 111 になる䞊べ方が2通りありたすがそれぞれ別のものずしお足し合わせたす。カヌドの䞭にリヌディングれロのカヌドはありたせんし、リヌディングれロになる数字は認めたせん。答えを1,000,000,007 で割ったものを出力しおください。 Input 入力は、以䞋の圢で䞎えられたす。 n a 1 a 2 ... a n 最初の 1 行にはカヌドの枚数を衚す n  1 ≀ n ≀ 200 、次の n 行にはそれぞれのカヌドに曞かれおいる数字 a i ( 0 ≀ a i < 10000 ) が曞かれおいたす。たた耇数のカヌドに同じ数字が曞かれおいるこずはありたせん。 Output 䜜るこずの出来る党おの数字の合蚈を 1,000,000,007 で割ったものを 1 行に出力しなさい。 Sample Input 1 2 1 2 Output for the Sample Input 1 36 サンプルにあった䟋です。 Sample Input 2 2 1 11 Output for the Sample Input 2 234 䜜られる数字は 1 ず 11 ず、 111 が 2 通り䜜られるので党お足しお 234 ずなりたす。 Sample Input 3 4 0 4 7 8 Output for the Sample Input 3 135299 04 や 078 ずいった䞊べ方は認められないこずに泚意しおください。
38,840
Score : 800 points Problem Statement You are given integers N,\ A and B . Determine if there exists a permutation (P_0,\ P_1,\ ...\ P_{2^N-1}) of (0,\ 1,\ ...\ 2^N-1) that satisfies all of the following conditions, and create one such permutation if it exists. P_0=A P_{2^N-1}=B For all 0 \leq i < 2^N-1 , the binary representations of P_i and P_{i+1} differ by exactly one bit. Constraints 1 \leq N \leq 17 0 \leq A \leq 2^N-1 0 \leq B \leq 2^N-1 A \neq B All values in input are integers. Input Input is given from Standard Input in the following format: N A B Output If there is no permutation that satisfies the conditions, print NO . If there is such a permutation, print YES in the first line. Then, print (P_0,\ P_1,\ ...\ P_{2^N-1}) in the second line, with spaces in between. If there are multiple solutions, any of them is accepted. Sample Input 1 2 1 3 Sample Output 1 YES 1 0 2 3 The binary representation of P=(1,0,2,3) is (01,00,10,11) , where any two adjacent elements differ by exactly one bit. Sample Input 2 3 2 1 Sample Output 2 NO
38,841
Score : 1600 points Problem Statement For a string S , let f(S) be the lexicographically smallest cyclic shift of S . For example, if S = babca , f(S) = ababc because this is the smallest among all cyclic shifts ( babca , abcab , bcaba , cabab , ababc ). You are given three integers X, Y , and Z . You want to construct a string T that consists of exactly X a s, exactly Y b s, and exactly Z c s. If there are multiple such strings, you want to choose one that maximizes f(T) lexicographically. Compute the lexicographically largest possible value of f(T) . Constraints 1 \leq X + Y + Z \leq 50 X, Y, Z are non-negative integers. Input Input is given from Standard Input in the following format: X Y Z Output Print the answer. Sample Input 1 2 2 0 Sample Output 1 abab T must consist of two a s and two b s. If T = aabb , f(T) = aabb . If T = abab , f(T) = abab . If T = abba , f(T) = aabb . If T = baab , f(T) = aabb . If T = baba , f(T) = abab . If T = bbaa , f(T) = aabb . Thus, the largest possible f(T) is abab . Sample Input 2 1 1 1 Sample Output 2 acb
38,842
Score: 400 points Problem Statement The Patisserie AtCoder sells cakes with number-shaped candles. There are X , Y and Z kinds of cakes with 1 -shaped, 2 -shaped and 3 -shaped candles, respectively. Each cake has an integer value called deliciousness , as follows: The deliciousness of the cakes with 1 -shaped candles are A_1, A_2, ..., A_X . The deliciousness of the cakes with 2 -shaped candles are B_1, B_2, ..., B_Y . The deliciousness of the cakes with 3 -shaped candles are C_1, C_2, ..., C_Z . Takahashi decides to buy three cakes, one for each of the three shapes of the candles, to celebrate ABC 123. There are X \times Y \times Z such ways to choose three cakes. We will arrange these X \times Y \times Z ways in descending order of the sum of the deliciousness of the cakes. Print the sums of the deliciousness of the cakes for the first, second, ... , K -th ways in this list. Constraints 1 \leq X \leq 1 \ 000 1 \leq Y \leq 1 \ 000 1 \leq Z \leq 1 \ 000 1 \leq K \leq \min(3 \ 000, X \times Y \times Z) 1 \leq A_i \leq 10 \ 000 \ 000 \ 000 1 \leq B_i \leq 10 \ 000 \ 000 \ 000 1 \leq C_i \leq 10 \ 000 \ 000 \ 000 All values in input are integers. Input Input is given from Standard Input in the following format: X Y Z K A_1 \ A_2 \ A_3 \ ... \ A_X B_1 \ B_2 \ B_3 \ ... \ B_Y C_1 \ C_2 \ C_3 \ ... \ C_Z Output Print K lines. The i -th line should contain the i -th value stated in the problem statement. Sample Input 1 2 2 2 8 4 6 1 5 3 8 Sample Output 1 19 17 15 14 13 12 10 8 There are 2 \times 2 \times 2 = 8 ways to choose three cakes, as shown below in descending order of the sum of the deliciousness of the cakes: (A_2, B_2, C_2) : 6 + 5 + 8 = 19 (A_1, B_2, C_2) : 4 + 5 + 8 = 17 (A_2, B_1, C_2) : 6 + 1 + 8 = 15 (A_2, B_2, C_1) : 6 + 5 + 3 = 14 (A_1, B_1, C_2) : 4 + 1 + 8 = 13 (A_1, B_2, C_1) : 4 + 5 + 3 = 12 (A_2, B_1, C_1) : 6 + 1 + 3 = 10 (A_1, B_1, C_1) : 4 + 1 + 3 = 8 Sample Input 2 3 3 3 5 1 10 100 2 20 200 1 10 100 Sample Output 2 400 310 310 301 301 There may be multiple combinations of cakes with the same sum of the deliciousness. For example, in this test case, the sum of A_1, B_3, C_3 and the sum of A_3, B_3, C_1 are both 301 . However, they are different ways of choosing cakes, so 301 occurs twice in the output. Sample Input 3 10 10 10 20 7467038376 5724769290 292794712 2843504496 3381970101 8402252870 249131806 6310293640 6690322794 6082257488 1873977926 2576529623 1144842195 1379118507 6003234687 4925540914 3902539811 3326692703 484657758 2877436338 4975681328 8974383988 2882263257 7690203955 514305523 6679823484 4263279310 585966808 3752282379 620585736 Sample Output 3 23379871545 22444657051 22302177772 22095691512 21667941469 21366963278 21287912315 21279176669 21160477018 21085311041 21059876163 21017997739 20703329561 20702387965 20590247696 20383761436 20343962175 20254073196 20210218542 20150096547 Note that the input or output may not fit into a 32 -bit integer type.
38,843
Problem I: Hopping Mind Problem チ゚ノずカカオは同じ喫茶店で働く姉効である。2人はずおも仲が良く、ある日、ずあるテヌブルゲヌムで遊ぶこずになった。 ゲヌムは R マス× C マスの盀面ず、駒ずしおうさぎのTPを甚いる。盀面の各マスは癜か黒の色が塗られおいる。最初にTPを盀面の右䞋( R , C )におき、2人で次の行動を亀互に行う。TPの珟圚の䜍眮を( a , b )ずするず、そこからゞャンプ可胜な䜍眮( i , j )を1぀遞び、TPをそこにゞャンプさせる。TPがゞャンプ可胜な䜍眮( i , j )は以䞋をすべお満たす。 1 ≀ i ≀ R か぀ 1 ≀ j ≀ C か぀ i ≀ a か぀ j ≀ b か぀ 1 ≀ ( a - i ) + ( b - j ) ≀ K ( i , j )は癜いマスである 自分のタヌンにTPをゞャンプさせるこずができなくなった堎合、負けずなる。 チ゚ノが先手、カカオが埌手でこのゲヌムを行う。カカオは頭の䞭でゲヌムを最埌たで先読みするこずができ、垞に最適な行動をずる。この時、チ゚ノが勝぀方法が存圚するかどうかを刀定せよ。 Input 入力は以䞋の圢匏で䞎えられる。 R C K G 1,1 G 1,2 ... G 1,C G 2,1 G 2,2 ... G 2,C : G R,1 G R,2 ... G R,C 1行目に3぀の敎数 R , C , K が空癜区切りで䞎えられる。次の R 行に盀面の情報ずしお C 個の".”たたは"#”が䞎えられる。 G i,j は盀面の䜍眮( i , j )の色を衚し、”.”が癜、"#”が黒を衚す。 Constraints 1 ≀ R , C ≀ 1000 1 ≀ K ≀ 2000 G R,C は“.”である Output チ゚ノが勝぀方法が存圚する堎合は”Chieno”を、存圚しない堎合は”Cacao”を1行に出力せよ。 Sample Input1 3 3 2 ... ... ... Sample Output1 Chieno Sample Input2 3 3 2 #.# .#. #.. Sample Output2 Cacao
38,844
Score : 600 points Problem Statement There are N holes in a two-dimensional plane. The coordinates of the i -th hole are (x_i,y_i) . Let R=10^{10^{10^{10}}} . Ringo performs the following operation: Randomly choose a point from the interior of a circle of radius R centered at the origin, and put Snuke there. Snuke will move to the hole with the smallest Euclidean distance from the point, and fall into that hole. If there are multiple such holes, the hole with the smallest index will be chosen. For every i (1 \leq i \leq N) , find the probability that Snuke falls into the i -th hole. Here, the operation of randomly choosing a point from the interior of a circle of radius R is defined as follows: Pick two real numbers x and y independently according to uniform distribution on [-R,R] . If x^2+y^2\leq R^2 , the point (x,y) is chosen. Otherwise, repeat picking the real numbers x,y until the condition is met. Constraints 2 \leq N \leq 100 |x_i|,|y_i| \leq 10^6(1\leq i\leq N) All given points are pairwise distinct. All input values are integers. Input Input is given from Standard Input in the following format: N x_1 y_1 : x_N y_N Output Print N real numbers. The i -th real number must represent the probability that Snuke falls into the i -th hole. The output will be judged correct when, for all output values, the absolute or relative error is at most 10^{-5} . Sample Input 1 2 0 0 1 1 Sample Output 1 0.5 0.5 If Ringo put Snuke in the region x+y\leq 1 , Snuke will fall into the first hole. The probability of this happening is very close to 0.5 . Otherwise, Snuke will fall into the second hole, the probability of which happening is also very close to 0.5 . Sample Input 2 5 0 0 2 8 4 5 2 6 3 10 Sample Output 2 0.43160120892732328768 0.03480224363653196956 0.13880483535586193855 0.00000000000000000000 0.39479171208028279727
38,845
I: Starting Line ICPC で良い成瞟を収めるには修行が欠かせないうさぎは ICPC で勝ちたいので今日も修行をするこずにした 今日の修行は䞀盎線䞊の道を走っお䜓力ず刀断力を逊おうずいうものであるうさぎは今スタヌトラむンに立っお長い長い道を芋枡しおいる 道の途䞭にはいく぀かニンゞンが眮かれおおりうさぎはニンゞンを食べるず加速するこずができる加速しおいないずきのうさぎの走る速さは毎秒 U メヌトルであるがニンゞンを食べるこずで最埌のニンゞンを食べおから T 秒埌たでは速さが毎秒 V メヌトルずなるたたうさぎはニンゞンを K 個たで食べずに持っおおくこずができるニンゞンを持っおいおも走る速さは倉わらない ニンゞンを持ったり食べたりするのに時間はかからないずしおゎヌルたでの最短所芁時間を求めたい Input N K T U V L D 1 ... D N N はニンゞンの個数 L はスタヌトからゎヌルたでの距離 (メヌトル) D i (1 ≀ i ≀ N ) は i 番目のニンゞンが眮かれおいる堎所のスタヌトからの距離 (メヌトル) である 1 ≀ N ≀ 2001 ≀ K ≀ N 1 ≀ T ≀ 10,0001 ≀ U < V ≀ 10,0002 ≀ L ≀ 10,0000 < D 1 < D 2 < ... < D N < L を満たす入力の倀はすべお敎数である Output 最短所芁時間 (秒) を 1 行に出力せよ10 -6 以䞋の絶察誀差が蚱容される Sample Input 1 1 1 1 2 3 100 50 Sample Output 1 49.500000000 Sample Input 2 3 1 1 2 3 100 49 50 51 Sample Output 2 48.666666667
38,846
Sorting Three Numbers Write a program which reads three integers, and prints them in ascending order. Input Three integers separated by a single space are given in a line. Output Print the given integers in ascending order in a line. Put a single space between two integers. Constraints 1 ≀ the three integers ≀ 10000 Sample Input 1 3 8 1 Sample Output 1 1 3 8
38,847
Slimming Plan Chokudai loves eating so much. However, his doctor Akensho told him that he was overweight, so he finally decided to lose his weight. Chokudai made a slimming plan of a $D$-day cycle. It is represented by $D$ integers $w_0, ..., w_{D-1}$. His weight is $S$ on the 0-th day of the plan and he aims to reduce it to $T$ ($S > T$). If his weight on the $i$-th day of the plan is $x$, it will be $x + w_{i\%D}$ on the $(i+1)$-th day. Note that $i\%D$ is the remainder obtained by dividing $i$ by $D$. If his weight successfully gets less than or equal to $T$, he will stop slimming immediately. If his slimming plan takes too many days or even does not end forever, he should reconsider it. Determine whether it ends or not, and report how many days it takes if it ends. Input The input consists of a single test case formatted as follows. $S$ $T$ $D$ $w_0 ... w_{D-1}$ The first line consists of three integers $S$, $T$, $D$ ($1 \leq S, T, D \leq 100,000, S > T$). The second line consists of $D$ integers $w_0, ..., w_{D-1}$ ($-100,000 \leq w_i \leq 100,000$ for each $i$). Output If Chokudai's slimming plan ends on the $d$-th day, print $d$ in one line. If it never ends, print $-1$. Sample Input 1 65 60 3 -2 3 -4 Output for Sample Input 1 4 Chokudai's weight will change as follows: $65 \rightarrow 63 \rightarrow 66 \rightarrow 62 \rightarrow 60$. Sample Input 2 65 60 3 -2 10 -3 Output for Sample Input 2 -1 Chokudai's weight will change as follows: $65 \rightarrow 63 \rightarrow 73 \rightarrow 70 \rightarrow 68 \rightarrow 78 \rightarrow 75 \rightarrow ...$. Sample Input 3 100000 1 1 -1 Output for Sample Input 3 99999 Sample Input 4 60 59 1 -123 Output for Sample Input 4 1
38,848
Problem H: Squid Multiplication Squid Eiko loves mathematics. Especially she loves to think about integer. One day, Eiko found a math problem from a website. "A sequence b ={ a i + a j | i < j } is generated from a sequence a ={ a 0 , ... , a n | a i is even if i is 0, otherwise a i is odd}. Given the sequence b , find the sequence a ." This problem is easy for Eiko and she feels boring. So, she made a new problem by modifying this problem . "A sequence b ={ a i * a j | i < j } is generated from a sequence a ={ a 0 , ... , a n | a i is even if i is 0, otherwise a i is odd}. Given the sequence b , find the sequence a ." Your task is to solve the problem made by Eiko. Input Input consists of multiple datasets. Each dataset is given by following formats. n b 0 b 1 ... b n*(n+1)/2-1 n is the number of odd integers in the sequence a . The range of n is 2 ≀ n ≀ 250. b i is separated by a space. Each b i is 1 ≀ b i ≀ 2 63 -1. The end of the input consists of a single 0. Output For each dataset, you should output two lines. First line contains a 0 , an even number in the sequence a . The second line contains n odd elements separated by a space. The odd elements are sorted by increasing order. You can assume that the result is greater than or equal to 1 and less than or equal to 2 31 -1. Sample input 3 6 10 14 15 21 35 2 30 42 35 0 Sample output 2 3 5 7 6 5 7
38,849
Score : 400 points Problem Statement You start with the number 0 and you want to reach the number N . You can change the number, paying a certain amount of coins, with the following operations: Multiply the number by 2 , paying A coins. Multiply the number by 3 , paying B coins. Multiply the number by 5 , paying C coins. Increase or decrease the number by 1 , paying D coins. You can perform these operations in arbitrary order and an arbitrary number of times. What is the minimum number of coins you need to reach N ? You have to solve T testcases. Constraints 1 \le T \le 10 1 \le N \le 10^{18} 1 \le A, B, C, D \le 10^9 All numbers N, A, B, C, D are integers. Input The input is given from Standard Input. The first line of the input is T Then, T lines follow describing the T testcases. Each of the T lines has the format N A B C D Output For each testcase, print the answer on Standard Output followed by a newline. Sample Input 1 5 11 1 2 4 8 11 1 2 2 8 32 10 8 5 4 29384293847243 454353412 332423423 934923490 1 900000000000000000 332423423 454353412 934923490 987654321 Sample Output 1 20 19 26 3821859835 23441258666 For the first testcase, a sequence of moves that achieves the minimum cost of 20 is: Initially x = 0 . Pay 8 to increase by 1 ( x = 1 ). Pay 1 to multiply by 2 ( x = 2 ). Pay 1 to multiply by 2 ( x = 4 ). Pay 2 to multiply by 3 ( x = 12 ). Pay 8 to decrease by 1 ( x = 11 ). For the second testcase, a sequence of moves that achieves the minimum cost of 19 is: Initially x = 0 . Pay 8 to increase by 1 ( x = 1 ). Pay 1 to multiply by 2 ( x = 2 ). Pay 2 to multiply by 5 ( x = 10 ). Pay 8 to increase by 1 ( x = 11 ).
38,850
UFO 撃墜䜜戊 40XX 幎、地球は宇宙人の䟵攻を受けおいた!すでに地球のほずんどは宇宙人により制圧されおおり、残る防衛拠点は鶎ヶ城芁塞のみになっおしたった。その鶎ヶ城芁塞にも、制圧郚隊が次々に迫っおきおいる。 しかし、垌望は残されおいる。防衛軍の最終兵噚、超長距離貫通レヌザヌ砲が完成したのだ。欠点ずいえば、 嚁力が出るたでに䞀定の距離が必芁で、 近すぎる敵はただ通りぬけおしたうずいうこずだ。 嚁力が出ない範囲に䟵入した敵は、他の戊力でどうにかするしかない。防衛軍の参謀は、䟵入しお来た UFO に察凊するために、その数を知らなければならないのだが、あたりにも敵が倚いせいでうたく数えられそうにない。そこで参謀はあなたに、レヌザヌに撃墜されなかった UFO の数を出力するプログラムを甚意するように呜什した。戊闘開始たでにプログラムを䜜成し、鶎ヶ城芁塞を守る力ずなろう。 敵の UFO はただたっすぐにレヌザヌ砲のある拠点を目指しお突撃しおくる(UFO には互いにすり抜ける凊理が斜されおおり、衝突しおしたうこずはない) 。レヌザヌ砲は、初期状態から 1 分埌に最も近い UFO の䞭心を狙っおレヌザヌを発射し始め、それから 1 分ごずに同じ条件でレヌザヌを発射し続ける。レヌザヌは貫通し、その先にある UFO もレヌザヌがかすっただけで撃墜するこずができる。しかし、このレヌザヌには嚁力の出ない範囲があり、その範囲に入っおしたったUFO は狙っおも意味がないため、狙わないように蚭蚈されおいる。撃墜できるだけ撃墜したずき、レヌザヌの嚁力が出ない範囲には䜕機の UFO が䟵入しただろうか。 レヌザヌの嚁力が出ない範囲の半埄 R ず、襲来する UFO の情報を入力ずし、撃墜されず䟵入しおいる UFO が䜕機いるかを出力するプログラムを䜜成しおください。 襲来する UFO の情報は、UFO の数 N 、各 UFO の初期座暙 ( x0, y0 )、各 UFO の半埄 r 、各 UFO の分速 v で構成されたす。 座暙の原点 (0 , 0) をレヌザヌ砲の䜍眮ずしたす。レヌザヌ砲ず UFO の距離は原点から UFO の䞭心たでの距離で䞎えられ、この距離が R 以䞋の UFO はレヌザヌの嚁力が出ない範囲に入ったずしたす。狙うべき察象が同時に耇数存圚する堎合はないものずしお考えたす。蚈算は党お平面䞊で考え、入力はすべお敎数で䞎えられたす。 Input 耇数のデヌタセットの䞊びが入力ずしお䞎えられたす。 入力の終わりはれロふた぀の行で瀺されたす。 各デヌタセットは以䞋の圢匏で䞎えられたす。 R N x0 1 y0 1 r 1 v 1 x0 2 y0 2 r 2 v 2 : x0 N y0 N r N v N 1 行目にレヌザヌの嚁力が出ない範囲の半埄 R (1 ≀ R ≀ 500) ずUFO の数 N (1 ≀ N ≀ 100) が䞎えられたす。続く N 行に i 機目の UFO の情報 x0 i , y0 i (-100 ≀ x0 i , y0 i ≀ 1000), r i , v i (1 ≀ r i , v i ≀ 500) が䞎えられたす。 デヌタセットの数は 50 を超えたせん。 Output 入力デヌタセットごずに、レヌザヌの嚁力が出ない範囲に䟵入した UFO の数を1行に出力したす。 Sample Input 100 5 101 101 5 5 110 110 2 3 -112 -100 9 11 -208 160 82 90 -110 108 10 2 10 11 15 0 5 1 25 0 5 1 35 0 5 1 45 0 5 1 55 0 5 1 65 0 5 1 75 0 5 1 85 0 5 1 95 0 5 1 -20 0 5 20 -30 0 500 5 0 0 Output for the Sample Input 1 1
38,851
Breadth First Search Write a program which reads an directed graph $G = (V, E)$, and finds the shortest distance from vertex $1$ to each vertex (the number of edges in the shortest path). Vertices are identified by IDs $1, 2, ... n$. Input In the first line, an integer $n$ denoting the number of vertices, is given. In the next $n$ lines, adjacent lists of vertex $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$. Constraints $1 \leq n \leq 100$ Output For each vertex $u$, print $id$ and $d$ in a line. $id$ is ID of vertex $u$ and $d$ is the distance from vertex $1$ to vertex $u$. If there are no path from vertex $1$ to vertex $u$, print -1 as the shortest distance. Print in order of IDs. Sample Input 1 4 1 2 2 4 2 1 4 3 0 4 1 3 Sample Output 1 1 0 2 1 3 2 4 1 Reference Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.
38,852
Problem K: A Polygon And Circles Problem $N$個の頂点からなる凞倚角圢ず$M$個の円の䞭心座暙が䞎えられる。すべおの円の半埄は$r$である。 以䞋の条件を満たす最小の実数$r$を求めたい。 条件: 凞倚角圢の内郚のどの点も、少なくずも䞀぀以䞊の円に内包されおいる。 Input 入力は以䞋の圢匏ですべお敎数で䞎えられる。 $N$ $px_1$ $py_1$ $px_2$ $py_2$ : $px_N$ $py_N$ $M$ $cx_1$ $cy_1$ $cx_2$ $cy_2$ : $cx_M$ $cy_M$ $1$行目には凞倚角圢の頂点数を衚す敎数$N$が䞎えられる。 $2$行目から$2+N-1$行目には凞倚角圢の各頂点の情報が反時蚈呚りの順で䞎えられる。$1+i$行目には$i$番目の頂点の座暙を衚す$px_i$ $py_i$が空癜区切りで䞎えられる。 $2+N$行目には円の数を衚す敎数$M$が䞎えられる。 $2+N+1$行目から$2+N+M$行には円の䞭心座暙の情報が䞎えられる。$2+N+i$行目には$i$番目の円の䞭心座暙を衚す$cx_i$ $cy_i$が空癜区切りで䞎えられる。 Constraints 入力は以䞋の条件を満たす。 $3 \le N \le 100$ $1 \le M \le 100$ $-10^5 \le px_i, py_i \le 10^5$ $-10^5 \le cx_i, cy_i \le 10^5$ 凞倚角圢の頂点のうちどの3点を遞んでも、同䞀盎線䞊には存圚しない Output 条件を満たす最小の円の半埄 $r$ を出力する。 ただし、$10^{-5}$ たでの絶察誀差たたは盞察誀差は蚱容される。 Sample Input 1 4 2 3 1 2 2 1 3 2 4 1 3 1 1 3 3 3 1 Sample Output 1 1.414213562373 Sample Input 2 7 -96469 25422 -55204 -45592 -29140 -72981 98837 -86795 92303 63297 19059 96012 -67980 70342 17 -4265 -14331 33561 72343 52055 98952 -71189 60872 10459 -7512 -11981 57756 -78228 -28044 37397 -69980 -27527 -51966 22661 -16694 13759 -59976 86539 -47703 17098 31709 -62497 -70998 -57608 59799 -1904 -35574 -73860 121 Sample Output 2 75307.220122044484
38,853
1幎生 (A First Grader) 問題 JOI君は小孊 1 幎生であるJOI君は教わったばかりの足し算匕き算がずおも奜きで数字が䞊んでいるのを芋るず最埌の 2 ぀の数字の間に「=」を入れ残りの数字の間に必ず「+」たたは「-」を入れお等匏を䜜っお遊んでいる䟋えば「8 3 2 4 8 7 2 4 0 8 8」から等匏「8+3-2-4+8-7-2-4-0+8=8」を䜜るこずができる JOI 君は等匏を䜜った埌それが正しい等匏になっおいるかを蚈算しお確かめるただしJOI君はただ負の数は知らないし20 を超える数の蚈算はできないしたがっお正しい等匏のうち巊蟺を巊から蚈算したずき蚈算の途䞭で珟れる数が党お 0 以䞊 20 以䞋の等匏だけがJOI君が確かめられる正しい等匏である䟋えば「8+3-2-4-8-7+2+4+0+8=8」は 正しい等匏だが途䞭に珟れる 8+3-2-4-8 が負の数なのでJOI君はこの等匏が正しいかどうか確かめるこずができない 入力ずしお数字の列が䞎えられたずきJOI君が䜜り確かめるこずができる正しい等匏の個数を求めるプログラムを䜜成せよ 入力 入力ファむルは 2 行からなる1 行目には䞊んでいる数字の個数を衚す敎数 N 3 ≀ N ≀ 100が曞かれおいる2 行目には 0 以䞊 9 以䞋の敎数が N 個空癜を区切りずしお曞かれおいる 䞎えられる入力デヌタの 60% ではJOI君が䜜り確かめるこずができる正しい等匏の個数は 2 31 -1 を超えないたた䞎えられる入力デヌタの党おにおいおJOI君が䜜り確かめるこずができる正しい等匏の個数は 2 63 -1 を超えない 出力 JOI君が䜜り確かめるこずができる正しい等匏の個数を衚す敎数を 1 行で出力せよ 入出力䟋 入力䟋 1 11 8 3 2 4 8 7 2 4 0 8 8 出力䟋 1 10 入力䟋 1 においおJOI君は 10 個の正しい等匏 8+3-2-4+8-7-2-4-0+8=8 8+3-2-4+8-7-2-4+0+8=8 8+3+2+4-8-7+2-4-0+8=8 8+3+2+4-8-7+2-4+0+8=8 8+3-2-4+8-7+2+4-0-8=8 8+3-2-4+8-7+2+4+0-8=8 8-3+2+4-8+7+2+4-0-8=8 8-3+2+4-8+7+2+4+0-8=8 8-3+2-4+8+7+2-4-0-8=8 8-3+2-4+8+7+2-4+0-8=8 を䜜り確かめるこずができるので 10 を出力する 入力䟋 2 40 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 出力䟋 2 7069052760 入力䟋 2 においお答えが 32 bit笊号付き敎数の範囲に収たらないこずに泚意せよ 問題文ず自動審刀に䜿われるデヌタは、 情報オリンピック日本委員䌚 が䜜成し公開しおいる問題文ず採点甚テストデヌタです。
38,854
Score : 600 points Problem Statement Kenkoooo found a simple connected graph. The vertices are numbered 1 through n . The i -th edge connects Vertex u_i and v_i , and has a fixed integer s_i . Kenkoooo is trying to write a positive integer in each vertex so that the following condition is satisfied: For every edge i , the sum of the positive integers written in Vertex u_i and v_i is equal to s_i . Find the number of such ways to write positive integers in the vertices. Constraints 2 \leq n \leq 10^5 1 \leq m \leq 10^5 1 \leq u_i < v_i \leq n 2 \leq s_i \leq 10^9 If i\neq j , then u_i \neq u_j or v_i \neq v_j . The graph is connected. All values in input are integers. Input Input is given from Standard Input in the following format: n m u_1 v_1 s_1 : u_m v_m s_m Output Print the number of ways to write positive integers in the vertices so that the condition is satisfied. Sample Input 1 3 3 1 2 3 2 3 5 1 3 4 Sample Output 1 1 The condition will be satisfied if we write 1,2 and 3 in vertices 1,2 and 3 , respectively. There is no other way to satisfy the condition, so the answer is 1 . Sample Input 2 4 3 1 2 6 2 3 7 3 4 5 Sample Output 2 3 Let a,b,c and d be the numbers to write in vertices 1,2,3 and 4 , respectively. There are three quadruples (a,b,c,d) that satisfy the condition: (a,b,c,d)=(1,5,2,3) (a,b,c,d)=(2,4,3,2) (a,b,c,d)=(3,3,4,1) Sample Input 3 8 7 1 2 1000000000 2 3 2 3 4 1000000000 4 5 2 5 6 1000000000 6 7 2 7 8 1000000000 Sample Output 3 0
38,855
Max Score: 1150 Points Task statement was updated. Problem Statement There is a grid which size is $H \times W$. the upper-left cell is $(1,1)$ and the lower-right cell is $(H,W)$. There is $N$ arrows. Arrow which start point is $(a_i,b_i)$ of direction is $c_i$, and size is $d_i$. ($d_i$ may be negative) It is guaranteed that there are no two arrow which start point is same. Sothe want to move from cell $(sx,sy)$ to cell $(gx,gy)$ with arrows. But it may not possible to move to goal in initial grid. So, Snuke decided to change some arrows. Sothe can change each arrow as follows: He can't change the start point of this arrow. It costs $e_i$ if he change the direction of this arrow. It costs $f \times |d_i-G|$ if he change d_i to $G$. He can't add or erase arrows. Please calculate the minimum cost that he can move to $(gx,gy)$. If he can't move to goal, please output '-1'. Note: Arrows are directed, and he can't turn in the middle of the arrow. Input The input is given from standard input in the following format. H W N f sx sy gx gy a_1 b_1 c_1 d_1 e_1 a_2 b_2 c_2 d_2 e_2 : : : a_N b_N c_N d_N e_N Output Please output a single integer: The minimum cost to clear this puzzle. If you can't achieve the objective, print -1 . Print \n (line break) in the end. Constraints $1 \le H,W \le 100000$ $1 \le N \le 70000$ $1 \le f,e_i \le 1000000$ $1 \le d_i \le 100000$ $1 \le a_i,sx,tx \le H$ $1 \le b_i,sy,ty \le W$ $c_i$ is N , E , S , or W , which means North, East, South, West. Subtasks Subtask 1 [ 190 points ] $H=1$ $W \le 600$ Subtask 2 [ 170 points ] $H,W \le 80$ Subtask 3 [ 360 points ] $H,W \le 600$ Subtask 4 [ 430 points ] There is no additional constraints. Sample Input 1 4 4 2 2 1 1 2 2 1 1 E 1 1 1 2 E 2 2 Sample Output 1 4 Sample Input 2 1 4 2 10 1 1 1 4 1 1 E 1 4 1 3 W 1 4 Sample Output 2 14 Sample Input 3 1 8 4 9 1 3 1 6 1 1 E 7 2 1 8 W 7 5 1 3 W 2 5 1 6 E 2 8 Sample Output 3 14 Sample Input 4 5 5 7 10 1 2 4 5 1 2 E 2 6 2 3 S 2 7 3 1 N 1 8 3 2 W 1 10 4 1 E 4 12 5 5 N 3 13 5 1 E 2 14
38,856
Score: 400 points Problem Statement AtCoder Inc. has decided to lock the door of its office with a 3 -digit PIN code. The company has an N -digit lucky number, S . Takahashi, the president, will erase N-3 digits from S and concatenate the remaining 3 digits without changing the order to set the PIN code. How many different PIN codes can he set this way? Both the lucky number and the PIN code may begin with a 0 . Constraints 4 \leq N \leq 30000 S is a string of length N consisting of digits. Input Input is given from Standard Input in the following format: N S Output Print the number of different PIN codes Takahashi can set. Sample Input 1 4 0224 Sample Output 1 3 Takahashi has the following options: Erase the first digit of S and set 224 . Erase the second digit of S and set 024 . Erase the third digit of S and set 024 . Erase the fourth digit of S and set 022 . Thus, he can set three different PIN codes: 022 , 024 , and 224 . Sample Input 2 6 123123 Sample Output 2 17 Sample Input 3 19 3141592653589793238 Sample Output 3 329
38,857
Problem F: Chemist's Math You have probably learnt chemical equations (chemical reaction formulae) in your high-school days. The following are some well-known equations. 2H 2 + O 2 → 2H 2 O (1) C a (OH) 2 + CO 2 → C a CO 3 + H 2 O (2) N 2 + 3H 2 → 2NH 3 (3) While Equations (1)–(3) all have balanced left-hand sides and right-hand sides, the following ones do not. Al + O 2 → Al 2 O 3 ( wrong ) (4) C 3 H 8 + O 2 → CO 2 + H 2 O ( wrong ) (5) The equations must follow the law of conservation of mass ; the quantity of each chemical element (such as H, O, Ca, Al) should not change with chemical reactions. So we should "adjust" the numbers of molecules on the left-hand side and right-hand side: 4Al + 3O 2 → 2Al 2 O 3 ( correct ) (6) C 3 H 8 + 5O 2 → 3CO 2 + 4H 2 O (correct) (7) The coefficients of Equation (6) are (4, 3, 2) from left to right, and those of Equation (7) are (1, 5, 3, 4) from left to right. Note that the coefficient 1 may be omitted from chemical equations. The coefficients of a correct equation must satisfy the following conditions. The coefficients are all positive integers. The coefficients are relatively prime, that is, their greatest common divisor (g.c.d.) is 1. The quantities of each chemical element on the left-hand side and the right-hand side are equal. Conversely, if a chemical equation satisfies the above three conditions, we regard it as a correct equation, no matter whether the reaction or its constituent molecules can be chemically realized in the real world, and no matter whether it can be called a reaction (e.g., H 2 → H 2 is considered correct). A chemical equation satisfying Conditions 1 and 3 (but not necessarily Condition 2) is called a balanced equation. Your goal is to read in chemical equations with missing coefficients like Equation (4) and (5), line by line, and output the sequences of coefficients that make the equations correct. Note that the above three conditions do not guarantee that a correct equation is uniquely determined. For example, if we "mix" the reactions generating H 2 O and NH 3 , we would get x H 2 + y O 2 + z N 2 + u H 2 → v H 2 O + w NH 3 (8) but ( x , y , z , u , v , w ) = (2, 1, 1, 3, 2, 2) does not represent a unique correct equation; for instance, (4, 2, 1, 3, 4, 2) and (4, 2, 3, 9, 4, 6) are also "correct" according to the above definition! However, we guarantee that every chemical equation we give you will lead to a unique correct equation by adjusting their coefficients. In other words, we guarantee that (i) every chemical equation can be balanced with positive coefficients, and that (ii) all balanced equations of the original equation can be obtained by multiplying the coefficients of a unique correct equation by a positive integer. Input The input is a sequence of chemical equations (without coefficients) of the following syntax in the Backus-Naur Form: <chemical_equation> ::= <molecule_sequence> "->" <molecule_sequence> <molecule_sequence> ::= <molecule> | <molecule> "+" <molecule_sequence> <molecule> ::= <group> | <group> <molecule> <group> ::= <unit_group> | <unit_group> <number> <unit_group> ::= <chemical_element> | "(" <molecule> ")" <chemical_element> ::= <uppercase_letter> | <uppercase_letter> <lowercase_letter> <uppercase_letter> ::= "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" <lowercase_letter> ::= "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" <number> ::= <non_zero_digit> | <non_zero_digit> <digit> <non_zero_digit> ::= "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | <digit> ::= "0" | <non_zero_digit> Each chemical equation is followed by a period and a newline. No other characters such as spaces do not appear in the input. For instance, the equation C a (OH) 2 + CO 2 → C a CO 3 + H 2 O is represented as Ca(OH)2+CO2->CaCO3+H2O. Each chemical equation is no more than 80 characters long, and as the above syntax implies, the <number>'s are less than 100. Parentheses may be used but will not be nested (maybe a good news to some of you!). Each side of a chemical equation consists of no more than 10 top-level molecules. The coefficients that make the equations correct will not exceed 40000. The chemical equations in the input have been chosen so that 32-bit integer arithmetic would suffice with appropriate precautions against possible arithmetic overflow. You are free to use 64-bit arithmetic, however. The end of the input is indicated by a line consisting of a single period. Note that our definition of <chemical_element> above allows chemical elements that do not exist or unknown as of now, and excludes known chemical elements with three-letter names (e.g., ununbium (Uub), with the atomic number 112). Output For each chemical equation, output a line containing the sequence of positive integer coefficients that make the chemical equation correct . Numbers in a line must be separated by a single space. No extra characters should appear in the output. Sample Input N2+H2->NH3. Na+Cl2->NaCl. Ca(OH)2+CO2->CaCO3+H2O. CaCl2+AgNO3->Ca(NO3)2+AgCl. C2H5OH+O2->CO2+H2O. C4H10+O2->CO2+H2O. A12B23+C34D45+ABCD->A6D7+B8C9. A98B+B98C+C98->A98B99C99. A2+B3+C5+D7+E11+F13->ABCDEF. . Output for the Sample Input 1 3 2 2 1 2 1 1 1 1 1 2 1 2 1 3 2 3 2 13 8 10 2 123 33042 5511 4136 1 1 1 1 15015 10010 6006 4290 2730 2310 30030
38,858
Problem I: Wind Passages Wind Corridor is a covered passageway where strong wind is always blowing. It is a long corridor of width W, and there are several pillars in it. Each pillar is a right prism and its face is a polygon (not necessarily convex). In this problem, we consider two-dimensional space where the positive x -axis points the east and the positive y -axis points the north. The passageway spans from the south to the north, and its length is infinity. Specifically, it covers the area 0 ≀ x ≀ W . The outside of the passageway is filled with walls. Each pillar is expressed as a polygon, and all the pillars are located within the corridor without conflicting or touching each other. Wind blows from the south side of the corridor to the north. For each second, w unit volume of air can be flowed at most if the minimum width of the path of the wind is w . Note that the path may fork and merge, but never overlaps with pillars and walls. Your task in this problem is to write a program that calculates the maximum amount of air that can be flowed through the corridor per second. Input The input consists of multiple datasets. Each dataset has the following format: The first line of the input contains two integers W and N . W is the width of the corridor, and N is the number of pillars. W and N satisfy the following condition: 1 ≀ W ≀ 10 4 and 0 ≀ N ≀ 200. Then, N specifications of each pillar follow. Each specification starts with a line that contains a single integer M , which is the number of the vertices of a polygon (3 ≀ M ≀ 40). The following M lines describe the shape of the polygon. The i -th line (1 ≀ i ≀ M ) contains two integers x i and y i that denote the coordinate of the i -th vertex (0 < x i < W , 0 < y i < 10 4 ). 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, your program should print a line that contains the maximum amount of air flow per second, in unit volume. The output may contain arbitrary number of digits after the decimal point, but the absolute error must not exceed 10 -6 . Sample Input 5 2 4 1 1 1 2 2 2 2 1 4 3 3 3 4 4 4 4 3 0 0 Output for the Sample Input 3.41421356
38,859
E - Parentheses Problem Statement You are given $n$ strings $\mathit{str}_1, \mathit{str}_2, \ldots, \mathit{str}_n$, each consisting of ( and ) . The objective is to determine whether it is possible to permute the $n$ strings so that the concatenation of the strings represents a valid string. Validity of strings are defined as follows: The empty string is valid. If $A$ and $B$ are valid, then the concatenation of $A$ and $B$ is valid. If $A$ is valid, then the string obtained by putting $A$ in a pair of matching parentheses is valid. Any other string is not valid. For example, "()()" and "(())" are valid, while "())" and "((()" are not valid. Input The first line of the input contains an integer $n$ ($1 \leq n \leq 100$), representing the number of strings. Then $n$ lines follow, each of which contains $\mathit{str}_i$ ($1 \leq \lvert \mathit{str}_i \rvert \leq 100$). All characters in $\mathit{str}_i$ are ( or ) . Output Output a line with "Yes" (without quotes) if you can make a valid string, or "No" otherwise. Sample Input 1 3 ()(()(( ))()()(() )())(()) Output for the Sample Input 1 Yes Sample Input 2 2 ))()(( ))((())( Output for the Sample Input 2 No
38,860
Problem C: Weaker than Planned The committee members of the Kitoshima programming contest had decided to use crypto-graphic software for their secret communication. They had asked a company, Kodai Software, to develop cryptographic software that employed a cipher based on highly sophisticated mathematics. According to reports on IT projects, many projects are not delivered on time, on budget, with required features and functions. This applied to this case. Kodai Software failed to implement the cipher by the appointed date of delivery, and asked to use a simpler version that employed a type of substitution cipher for the moment. The committee members got angry and strongly requested to deliver the full specification product, but they unwillingly decided to use this inferior product for the moment. In what follows, we call the text before encryption, plaintext, and the text after encryption, ciphertext . This simple cipher substitutes letters in the plaintext, and its substitution rule is specified with a set of pairs. A pair consists of two letters and is unordered, that is, the order of the letters in the pair does not matter. A pair (A, B) and a pair (B, A) have the same meaning. In one substitution rule, one letter can appear in at most one single pair. When a letter in a pair appears in the plaintext, the letter is replaced with the other letter in the pair. Letters not specified in any pairs are left as they are. For example, by substituting the plaintext ABCDEFGHIJKLMNOPQRSTUVWXYZ with the substitution rule {(A, Z), (B, Y)} results in the following ciphertext. ZYCDEFGHIJKLMNOPQRSTUVWXBA This may be a big chance for us, because the substitution rule seems weak against cracking. We may be able to know communications between committee members. The mission here is to develop a deciphering program that finds the plaintext messages from given ciphertext messages. A ciphertext message is composed of one or more ciphertext words. A ciphertext word is generated from a plaintext word with a substitution rule. You have a list of candidate words containing the words that can appear in the plaintext; no other words may appear. Some words in the list may not actually be used in the plaintext. There always exists at least one sequence of candidate words from which the given ciphertext is obtained by some substitution rule. There may be cases where it is impossible to uniquely identify the plaintext from a given ciphertext and the list of candidate words. Input The input consists of multiple datasets, each of which contains a ciphertext message and a list of candidate words in the following format. n word 1 . . . word n sequence n in the first line is a positive integer, representing the number of candidate words. Each of the next n lines represents one of the candidate words. The last line, sequence, is a sequence of one or more ciphertext words separated by a single space and terminated with a period. You may assume the number of characters in each sequence is more than 1 and less than or equal to 80 including spaces and the period. The number of candidate words in the list, n , does not exceed 20. Only 26 uppercase letters, A to Z, are used in the words and the length of each word is from 1 to 20, inclusive. A line of a single zero indicates the end of the input. Output For each dataset, your program should print the deciphered message in a line. Two adjacent words in an output line should be separated by a single space and the last word should be followed by a single period. When it is impossible to uniquely identify the plaintext, the output line should be a single hyphen followed by a single period. Sample Input 4 A AND CAT DOG Z XUW ZVX Z YZT. 2 AZ AY ZA. 2 AA BB CC. 16 A B C D E F G H I J K L M N O ABCDEFGHIJKLMNO A B C D E F G H I J K L M N O ABCDEFGHIJKLMNO. 0 Output for the Sample Input A DOG AND A CAT. AZ. -. A B C D E F G H I J K L M N O ABCDEFGHIJKLMNO.
38,861
Score : 200 points Problem Statement Kurohashi has never participated in AtCoder Beginner Contest (ABC). The next ABC to be held is ABC N (the N -th ABC ever held). Kurohashi wants to make his debut in some ABC x such that all the digits of x in base ten are the same. What is the earliest ABC where Kurohashi can make his debut? Constraints 100 \leq N \leq 999 N is an integer. Input Input is given from Standard Input in the following format: N Output If the earliest ABC where Kurohashi can make his debut is ABC n , print n . Sample Input 1 111 Sample Output 1 111 The next ABC to be held is ABC 111 , where Kurohashi can make his debut. Sample Input 2 112 Sample Output 2 222 The next ABC to be held is ABC 112 , which means Kurohashi can no longer participate in ABC 111 . Among the ABCs where Kurohashi can make his debut, the earliest one is ABC 222 . Sample Input 3 750 Sample Output 3 777
38,862
Score : 500 points Problem Statement We have N locked treasure boxes, numbered 1 to N . A shop sells M keys. The i -th key is sold for a_i yen (the currency of Japan), and it can unlock b_i of the boxes: Box c_{i1} , c_{i2} , ... , c_{i{b_i}} . Each key purchased can be used any number of times. Find the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1 . Constraints All values in input are integers. 1 \leq N \leq 12 1 \leq M \leq 10^3 1 \leq a_i \leq 10^5 1 \leq b_i \leq N 1 \leq c_{i1} < c_{i2} < ... < c_{i{b_i}} \leq N Input Input is given from Standard Input in the following format: N M a_1 b_1 c_{11} c_{12} ... c_{1{b_1}} : a_M b_M c_{M1} c_{M2} ... c_{M{b_M}} Output Print the minimum cost required to unlock all the treasure boxes. If it is impossible to unlock all of them, print -1 . Sample Input 1 2 3 10 1 1 15 1 2 30 2 1 2 Sample Output 1 25 We can unlock all the boxes by purchasing the first and second keys, at the cost of 25 yen, which is the minimum cost required. Sample Input 2 12 1 100000 1 2 Sample Output 2 -1 We cannot unlock all the boxes. Sample Input 3 4 6 67786 3 1 3 4 3497 1 2 44908 3 2 3 4 2156 3 2 3 4 26230 1 2 86918 1 3 Sample Output 3 69942
38,864
人気の出店は? 䌚接若束垂では、毎幎1月10日に「十日垂」ずいう初垂がありたす。この十日垂は、玄600幎の歎史があり䌚接地方最倧の初垂です。䌚接地方ではお銎染みの瞁起物である、起き䞊がり小法垫(おきあがりこがし)が売られおいるこずでもよく知られおいたす。起き䞊がり小法垫は、倧きさ3cm 皋床の重心が底にある匵り子で、転がしおもすぐに起きあがるこずからこの名前が付きたした。各家庭では、必ず家族より䞀個倚く買っお神棚に䟛えたす。この䞀個は、 「家族が増えるように」や「厄を背負っおくれる」ずの意味がありたす。 十日垂実行委員䌚では、次回の十日垂に向けお、起き䞊がり小法垫の販売個数が䞀番倚い出店を調べるこずになりたした。今幎の出店の数は5店(A、B、C、D、E:半角英字)で、販売個数は、午前ず午埌に分けお十日垂実行委員䌚に報告されおいたす。 各出店の情報を入力ずし、䞀日の販売個数が䞀番倚い出店の名前ずその個数を出力するプログラムを䜜成しおください。 Input 耇数のデヌタセットの䞊びが入力ずしお䞎えられたす。入力の終わりはれロふた぀の行で瀺されたす。 各デヌタセットは以䞋の圢匏で䞎えられたす。 s1 A s2 A s1 B s2 B s1 C s2 C s1 D s2 D s1 E s2 E i 行目に、それぞれA, B, C, D, E の午前の販売個数 s1 i ず午埌の販売個数 s2 i (1 ≀ s1 i , s2 i ≀ 10000) が䞎えられたす。ただし、䞀日の販売個数が同じ出店は無いものずしたす。 デヌタセットの数は 100 を超えたせん。 Output デヌタセットごずに、䞀日の販売個数の䞀番倚い出店の名前ずその個数を行に出力したす。 Sample Input 1593 4311 4321 2155 1256 6421 5310 1455 2152 5421 1549 3386 4528 3719 1234 4321 3330 3109 2739 2199 0 0 Output for the Sample Input C 7677 B 8247
38,865
Includes For given two sequneces $A = \{a_0, a_1, ..., a_{n-1}\}$ and $B = \{b_0, b_1, ..., b_{m-1}\}$, determine whether all elements of $B$ are included in $A$. Note that, elements of $A$ and $B$ are sorted by ascending order respectively. Input The input is given in the following format. $n$ $a_0 \; a_1 \; ,..., \; a_{n-1}$ $m$ $b_0 \; b_1 \; ,..., \; b_{m-1}$ The number of elements in $A$ and its elements $a_i$ are given in the first and second lines respectively. The number of elements in $B$ and its elements $b_i$ are given in the third and fourth lines respectively. Output Print 1 , if $A$ contains all elements of $B$, otherwise 0 . Constraints $1 \leq n, m \leq 200,000$ $-1,000,000,000 \leq a_0 < a_1 < ... < a_{n-1} \leq 1,000,000,000$ $-1,000,000,000 \leq b_0 < b_1 < ... < b_{m-1} \leq 1,000,000,000$ Sample Input 1 4 1 2 3 4 2 2 4 Sample Output 1 1 Sample Input 2 4 1 2 3 4 3 1 2 5 Sample Output 2 0
38,866
G: 匏の切り取り 問題 長さ $N$ の数匏 $S$ が䞎えられる。 数匏は、以䞋の BNF で瀺される圢匏になっおいる。 <expr> ::= <number> | <expr> <op> <expr> <op> ::= ‘^’ | ‘&’ | ‘|’ <number> は、 $0$ 以䞊 $2^{31}-1$ 以䞋の敎数を衚す。 挔算子、’^’ ‘&’ ‘|’ は、それぞれ排他的論理和、論理積、論理和を衚す。 挔算子の優先順䜍は以䞋の通りである。 高 ’^’ > ‘&’ > ‘|’ 䜎 区間 $[i, j]$ が $Q$ 個䞎えられる。 $S_i, \dots , S_j$ の蚈算結果を出力せよ。 なお、数匏 $S_i, \dots , S_j$ が䞊蚘のBNFで瀺される圢匏であるこずは保蚌される。たた、数匏にれロ詰めされた倀が含たれるこずはない。 制玄 $1 \leq N, Q \leq 10^5$ $N = |S| \ \ \ \ \ |S|$ は文字列の長さを衚す。 $0 \leq i \leq j < N$ 入力圢匏 入力は以䞋の圢匏で䞎えられる。 $N$ $S$ $Q$ $i_1\ j_1$ $\vdots$ $i_Q\ j_Q$ 出力 $S_i, \dots , S_j$ の蚈算結果を出力せよ。 たた、末尟に改行も出力せよ。 サンプル サンプル入力 1 7 9^2&1|2 4 0 6 0 2 2 6 4 4 サンプル出力 1 3 11 2 1 サンプル入力 2 13 3423&423^1234 3 2 12 5 9 9 12 サンプル出力 2 21 422 1234 サンプル入力 3 7 1234567 3 0 0 0 1 4 6 サンプル出力 3 1 12 567
38,867
Dungeon Creation The king demon is waiting in his dungeon to defeat a brave man. His dungeon consists of H \times W grids. Each cell is connected to four (i.e. north, south, east and west) neighboring cells and some cells are occupied by obstacles. To attack the brave man, the king demon created and sent a servant that walks around in the dungeon. However, soon the king demon found that the servant does not work as he wants. The servant is too dumb. If the dungeon had cyclic path, it might keep walking along the cycle forever. In order to make sure that the servant eventually find the brave man, the king demon decided to eliminate all cycles by building walls between cells. At the same time, he must be careful so that there is at least one path between any two cells not occupied by obstacles. Your task is to write a program that computes in how many ways the kind demon can build walls. Input The first line of each test case has two integers H and W ( 1 \leq H \leq 500 , 1 \leq W \leq 15 ), which are the height and the width of the dungeon. Following H lines consist of exactly W letters each of which is '.' (there is no obstacle on the cell) or '#' (there is an obstacle). You may assume that there is at least one cell that does not have an obstacle. The input terminates when H = 0 and W = 0 . Your program must not output for this case. Output For each test case, print its case number and the number of ways that walls can be built in one line. Since the answer can be very big, output in modulo 1,000,000,007. Sample Input 2 2 .. .. 3 3 ... ... ..# 0 0 Output for the Sample Input Case 1: 4 Case 2: 56
38,868
Problem C: Online Quizu System ICPC (Internet Contents Providing Company) is working on a killer game named Quiz Millionaire Attack. It is a quiz system played over the Internet. You are joining ICPC as an engineer, and you are responsible for designing a protocol between clients and the game server for this system. As bandwidth assigned for the server is quite limited, data size exchanged between clients and the server should be reduced as much as possible. In addition, all clients should be well synchronized during the quiz session for a simultaneous play. In particular, much attention should be paid to the delay on receiving packets. To verify that your protocol meets the above demands, you have decided to simulate the communication between clients and the server and calculate the data size exchanged during one game. A game has the following process. First, players participating and problems to be used are fixed. All players are using the same client program and already have the problem statements downloaded, so you don’t need to simulate this part. Then the game begins. The first problem is presented to the players, and the players answer it within a fixed amount of time. After that, the second problem is presented, and so forth. When all problems have been completed, the game ends. During each problem phase, the players are notified of what other players have done. Before you submit your answer, you can know who have already submitted their answers. After you have submitted your answer, you can know what answers are submitted by other players. When each problem phase starts, the server sends a synchronization packet for problem-start to all the players, and begins a polling process. Every 1,000 milliseconds after the beginning of polling, the server checks whether it received new answers from the players strictly before that moment, and if there are any, sends a notification to all the players: If a player hasn’t submitted an answer yet, the server sends it a notification packet type A describing others’ answers about the newly received answers. If a player is one of those who submitted the newly received answers, the server sends it a notification packet type B describing others’ answers about all the answers submitted by other players (i.e. excluding the player him/herself’s answer) strictly before that moment. If a player has already submitted an answer, the server sends it a notification packet type B describing others’ answers about the newly received answers. Note that, in all above cases, notification packets (both types A and B) must contain information about at least one player, and otherwise a notification packet will not be sent. When 20,000 milliseconds have passed after sending the synchronization packet for problem-start , the server sends notification packets of type A or B if needed, and then sends a synchronization packet for problem-end to all the players, to terminate the problem. On the other hand, players will be able to answer the problem after receiving the synchronization packet for problem-start and before receiving the synchronization packet for problem-end . Answers will be sent using an answer packet . The packets referred above have the formats shown by the following tables. Input The input consists of multiple test cases. Each test case begins with a line consisting of two integers M and N (1 ≀ M , N ≀ 100), denoting the number of players and problems, respectively. The next line contains M non-negative integers D 0 , D 1 , . . . , D M - 1 , denoting the communication delay between each players and the server (players are assigned ID’s ranging from 0 to M - 1, inclusive). Then follow N blocks describing the submissions for each problem. Each block begins with a line containing an integer L , denoting the number of players that submitted an answer for that problem. Each of the following L lines gives the answer from one player, described by three fields P , T , and A separated by a whitespace. Here P is an integer denoting the player ID, T is an integer denoting the time elapsed from the reception of synchronization packet for problem-start and the submission on the player’s side, and A is an alphanumeric string denoting the player’s answer, whose length is between 1 and 9, inclusive. Those L lines may be given in an arbitrary order. You may assume that all answer packets will be received by the server within 19,999 milliseconds (inclusive) after sending synchronization packet for problem-start . The input is terminated by a line containing two zeros. Output For each test case, you are to print M + 1 lines. In the first line, print the data size sent and received by the server, separated by a whitespace. In the next M lines, print the data size sent and received by each player, separated by a whitespace, in ascending order of player ID. Print a blank line between two consecutive test cases. Sample Input 3 2 1 2 10 3 0 3420 o 1 4589 o 2 4638 x 3 1 6577 SUZUMIYA 2 7644 SUZUMIYA 0 19979 YASUZUMI 4 2 150 150 150 150 4 0 1344 HOGEHOGE 1 1466 HOGEHOGE 2 1789 HOGEHOGE 3 19100 GEHO 2 2 1200 SETTEN 3 700 SETTEN 0 0 Output for the Sample Input 177 57 19 58 19 57 19 62 253 70 13 58 13 58 24 66 20 71
38,870
郚掻動調査 A 高校の生埒䌚長である明は、A 高校の生埒がどの郚掻動に所属しおいるかを調査するこずにした。A高校には 1 から N の番号が付けられた N 人の生埒ず、1 から M の番号が付けられた M 皮類の郚掻動がある。各郚掻動に人数制限はなく、0 人の郚掻動もありえる。ただし、A 高校の校則では生埒はひず぀の郚掻動たでしか所属できない。生埒は党員この校則を守っおいる。 明は生埒䌚員に調査を䟝頌し、各行が次のいずれかであるような K 行の蚘録を入手した。 生埒 a ず生埒 b は同じ郚掻動に所属しおいる。 生埒 c は郚掻動 x に所属しおいる。 しかし、この蚘録には誰かが校則違反になっおしたうような矛盟があるかもしれない。明は行目から順に芋おいき、矛盟があるず刀断できる最初の行を探すこずにした。 K 行の蚘録が䞎えられたずき、矛盟があるず刀断できる最初の行の番号を求めるプログラムを䜜成せよ。 Input 入力は以䞋の圢匏で䞎えられる。 N M K record 1 record 2 : record K 行目に生埒の人数 N (1 ≀ N ≀ 100000)、郚掻動の皮類の数 M (1 ≀ M ≀ 100000)、蚘録の行数 K (1 ≀ K ≀ 200000) が䞎えられる。続く K 行に蚘録の各行 record i が、以䞋のいずれかの圢匏で䞎えられる。 1 a b たたは 2 c x 先頭の数字が「1」のずき、生埒 a (1 ≀ a ≀ N ) ず生埒 b (1 ≀ b ≀ N ) が同じ郚掻動に所属しおいるこずを瀺す。ただし、 a ≠ b である。 先頭の数字が「2」のずき、生埒 c (1 ≀ c ≀ N ) が郚掻動 x (1 ≀ x ≀ M ) に所属しおいるこずを瀺す。 Output 矛盟があるず刀断できる最初の行の番号を行に出力する。芋぀からない堎合は 0 を行に出力する。 Sample Input 1 3 2 5 1 1 2 1 2 3 2 1 1 2 3 2 2 2 1 Sample Output 1 4 Sample Input 2 3 2 4 1 1 2 2 1 1 2 3 2 2 2 1 Sample Output 2 0 Sample Input 3 3 2 2 1 1 2 2 1 1 Sample Output 3 0
38,871
Tree Walk Binary trees are defined recursively. A binary tree T is a structure defined on a finite set of nodes that either contains no nodes, or is composed of three disjoint sets of nodes: - a root node. - a binary tree called its left subtree. - a binary tree called its right subtree. Your task is to write a program which perform tree walks (systematically traverse all nodes in a tree) based on the following algorithms: Print the root, the left subtree and right subtree (preorder). Print the left subtree, the root and right subtree (inorder). Print the left subtree, right subtree and the root (postorder). 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 linen, 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 In the 1st line, print " Preorder ", and in the 2nd line print a list of node IDs obtained by the preorder tree walk. In the 3rd line, print " Inorder ", and in the 4th line print a list of node IDs obtained by the inorder tree walk. In the 5th line, print " Postorder ", and in the 6th line print a list of node IDs obtained by the postorder tree walk. Print a space character before each node ID. 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 Preorder 0 1 2 3 4 5 6 7 8 Inorder 2 1 3 0 6 5 7 4 8 Postorder 2 3 1 6 7 5 8 4 0 Reference Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.
38,872
C : 壺 / Pots 問題文 ここに N 個の䞍思議な圢の壺がある i 番目の壺は K_i 個の盎円柱を䞋から順に鉛盎に繋げた圢状である 繋がっおいる順番は倉えるこずができない A 氏は䜓積 M の氎を持っおいる この氎をそれぞれの壺に奜きな量ず぀に分けお泚ぐ 氎が党く入っおいない壺が存圚しおも構わない たた党おの壺が氎で満たされたずきそれ以䞊氎を泚ぐ事はできないそれぞれの壺の氎面の高さの総和の最倧倀を求めよ 入力 N \ M K_1 \ S_{11} \ H_{11} \ 
 \ S_{1 K_1} \ H_{1 K_1} K_2 \ S_{21} \ H_{21} \ 
 \ S_{2 K_2} \ H_{2 K_2} 
 K_N \ S_{N1} \ H_{N1} \ 
 \ S_{N K_N} \ H_{N K_N} 1 行目に N, M が 1+i 行目には i 番目の壺の情報が入力される K_i は盎円柱の数であり S_{ij}, H_{ij} はそれぞれ壺を構成する䞋から j 番目の盎円柱の底面積ず高さである 制玄 敎数である 1 ≀ N ≀ 200 1 ≀ M ≀ 200 1 ≀ K_i ≀ 20 1 ≀ S_{ij} ≀ 20 1 ≀ H_{ij} ≀ 20 出力 答えを 1 行で出力せよ 0.00001 以䞋の絶察誀差を含んでも良い サンプル サンプル入力1 2 15 2 3 3 7 2 2 7 1 1 4 サンプル出力1 6.33333333 サンプル入力2 2 14 1 2 4 2 5 2 1 4 サンプル出力2 6 サンプル 1, 2 の入出力を図瀺するず次のようになる サンプル入力3 2 25 4 8 9 1 9 6 5 2 8 4 1 7 4 4 1 6 4 3 サンプル出力3 13
38,874
Problem A: Pablo Squarson's Headache Pablo Squarson is a well-known cubism artist. This year's theme for Pablo Squarson is "Squares". Today we are visiting his studio to see how his masterpieces are given birth. At the center of his studio, there is a huuuuuge table and beside it are many, many squares of the same size. Pablo Squarson puts one of the squares on the table. Then he places some other squares on the table in sequence. It seems his methodical nature forces him to place each square side by side to the one that he already placed on, with machine-like precision. Oh! The first piece of artwork is done. Pablo Squarson seems satisfied with it. Look at his happy face. Oh, what's wrong with Pablo? He is tearing his hair! Oh, I see. He wants to find a box that fits the new piece of work but he has trouble figuring out its size. Let's help him! Your mission is to write a program that takes instructions that record how Pablo made a piece of his artwork and computes its width and height. It is known that the size of each square is 1. You may assume that Pablo does not put a square on another. I hear someone murmured "A smaller box will do". No, poor Pablo, shaking his head, is grumbling "My square style does not seem to be understood by illiterates". Input The input consists of a number of datasets. Each dataset represents the way Pablo made a piece of his artwork. The format of a dataset is as follows. N n 1 d 1 n 2 d 2 ... n N -1 d N -1 The first line contains the number of squares (= N ) used to make the piece of artwork. The number is a positive integer and is smaller than 200. The remaining ( N -1) lines in the dataset are square placement instructions. The line “ n i d i ” indicates placement of the square numbered i (≀ N -1). The rules of numbering squares are as follows. The first square is numbered "zero". Subsequently placed squares are numbered 1, 2, ..., ( N -1). Note that the input does not give any placement instruction to the first square, which is numbered zero. A square placement instruction for the square numbered i , namely “ n i d i ”, directs it to be placed next to the one that is numbered n i , towards the direction given by d i , which denotes leftward (= 0), downward (= 1), rightward (= 2), and upward (= 3). For example, pieces of artwork corresponding to the four datasets shown in Sample Input are depicted below. Squares are labeled by their numbers. The end of the input is indicated by a line that contains a single zero. Output For each dataset, output a line that contains the width and the height of the piece of artwork as decimal numbers, separated by a space. Each line should not contain any other characters. Sample Input 1 5 0 0 0 1 0 2 0 3 12 0 0 1 0 2 0 3 1 4 1 5 1 6 2 7 2 8 2 9 3 10 3 10 0 2 1 2 2 2 3 2 2 1 5 1 6 1 7 1 8 1 0 Output for the Sample Input 1 1 3 3 4 4 5 6
38,875
家庭菜園に野菜を怍えるこずにしたした。n 粒の皮があったので1日に1粒ず぀、n 日かけお n 粒の皮をたきたした。どの皮からも芜が出お、すくすくず育っおいたす。収穫の時期が埅ち遠しいものです。 ある日、い぀ものように苗に氎やりをしおいるず、おかしなこずに気づきたした。n 本の野菜の苗があるはずなのに、1本倚いのです。雑草が生えおきおしたいたした。盎ちに匕っこ抜きたいのですが、困ったこずにどの苗もよく䌌おいお、野菜ず雑草の芋分けが぀きたせん。 手がかりになるのは、野菜の成長速床です。この野菜は、皮をたいおからずっず、1日に決たった長さだけ䌞び続けるのです。しかし、この「決たった長さ」ずいうのが䜕センチメヌトルなのかはわかりたせん。たた、最初の皮を䜕日前にたいたのかも忘れおしたいたした。苗は䞀列に䞊んでいたすが、唯䞀芚えおいるのは、皮をたくずき毎日䞀粒ず぀右から順にたいおいったこずだけです。 n本の苗の長さを入力し、雑草の長さを出力するプログラムを䜜成しお䞋さい。 入力 入力は耇数のデヌタセットからなる。入力の終わりはれロ぀行で瀺される。入力は以䞋の圢匏で䞎えられる。 n h 1 h 2 h 3 ... h n+1 行目の n (4 ≀ n ≀ 100) は野菜の苗の数を衚す敎数である。行目は぀の空癜で区切られた n+1 個の敎数を含み、 h i (1 ≀ h i ≀ 10 9 )は巊からi番目の苗の長さを瀺す。 h 1 h 2 ... h n+1 が等差数列になっおいるような入力は䞎えられない。 デヌタセットの数は 500 を超えない。 出力 各デヌタセットごずに雑草の長さを出力する 。 入力䟋 5 1 2 3 6 4 5 6 1 3 6 9 12 15 18 4 5 7 9 11 12 0 出力䟋 6 1 12
38,876
Score : 600 points Problem Statement We have a tree with N vertices numbered 1 to N . The i -th edge in this tree connects Vertex a_i and Vertex b_i . Consider painting each of these edges white or black. There are 2^{N-1} such ways to paint the edges. Among them, how many satisfy all of the following M restrictions? The i -th (1 \leq i \leq M) restriction is represented by two integers u_i and v_i , which mean that the path connecting Vertex u_i and Vertex v_i must contain at least one edge painted black. Constraints 2 \leq N \leq 50 1 \leq a_i,b_i \leq N The graph given in input is a tree. 1 \leq M \leq \min(20,\frac{N(N-1)}{2}) 1 \leq u_i < v_i \leq N If i \not= j , either u_i \not=u_j or v_i\not=v_j All values in input are integers. Input Input is given from Standard Input in the following format: N a_1 b_1 : a_{N-1} b_{N-1} M u_1 v_1 : u_M v_M Output Print the number of ways to paint the edges that satisfy all of the M conditions. Sample Input 1 3 1 2 2 3 1 1 3 Sample Output 1 3 The tree in this input is shown below: All of the M restrictions will be satisfied if Edge 1 and 2 are respectively painted (white, black), (black, white), or (black, black), so the answer is 3 . Sample Input 2 2 1 2 1 1 2 Sample Output 2 1 The tree in this input is shown below: All of the M restrictions will be satisfied only if Edge 1 is painted black, so the answer is 1 . Sample Input 3 5 1 2 3 2 3 4 5 3 3 1 3 2 4 2 5 Sample Output 3 9 The tree in this input is shown below: Sample Input 4 8 1 2 2 3 4 3 2 5 6 3 6 7 8 6 5 2 7 3 5 1 6 2 8 7 8 Sample Output 4 62 The tree in this input is shown below:
38,878
G: 雚降りバス乗り換え 背景 今日は AOR むカちゃんにずっお初ずなるデヌトの日だ。 AOR むカちゃんは駅からバスを乗り継ぎ、埅ち合わせ堎所のバス停に向かう予定である。 AOR むカちゃんが駅に着いた時、䞍幞にも雚が降っおきた。圓初予定しおいた経路ではバスの埅ち時間で濡れおしたい、せっかく敎えた身だしなみが台無しになっおしたう可胜性がある。 そこで、バスの埅ち時間が最も少なくなるような経路でバス停たで向かうこずにした。 AOR むカちゃんは、埅ち合わせ堎所のバス停に着くたでにどの皋床濡れるのか心配しおいる。 問題 AOR むカちゃんは時刻 $0$ に $S$ 番目のバス停におり、そこから $G$ 番目のバス停たで行きたいず考えおいる。 バス停の個数 $N$ ず、異なるバス停同士を繋ぐ $M$ 個の経路 (※) が䞎えられる。 バス停には、それぞれ $1, \dots, N$ の番号がふられおいる。 各経路は、出発地 $u$ 、目的地 $v$ 、出発時刻 $t$ 、所芁時間 $c$ の $4$ ぀の倀からなる。 時刻 $t$ に出発地 $u$ にいればバスに乗るこずができ、時刻 $t + c$ に目的地 $v$ ぞ到着する。 バスに乗っおいない間、 AOR むカちゃんは雚に濡れおしたう。 雚にぬれる時間が最小ずなる経路を通り $G$ 番目のバス停ぞ向かう時、時刻 $0$ から $G$ 番目のバス停に着くたでの間に雚に濡れた時間の合蚈を出力せよ。 (※) グラフ理論の甚語における経路ずは頂点ず蟺の列であるがここでは蟺の意味で䜿われおいるこずに泚意しおほしい。 制玄 $2 \le N \le 10^5$ $1 \le M \le 2 \times 10^5$ $1 \le S , G \le N$ $1 \le u_i , v_i \le N$ $0 \le t_i \le 10^5$ $1 \le c_i \le 10^5$ $S \neq G$ $u_i \neq v_i$ 出発地 $u$ ず目的地 $v$ が同じであるような経路は存圚しない。 出発地 $u$ ず目的地 $v$ を結ぶ経路は耇数存圚する堎合がある。 バスの乗り降り、乗り換えに時間はかからないものずする。 $G$ 番目のバス停ぞ到着する時間は問わない。 $S$ 番目のバス停から $G$ 番目のバス停ぞ到着できるこずは保蚌されおいる。 入力 入力は以䞋の圢匏で暙準入力から䞎えられる。 $N \ M \ S \ G$ $u_1 \ v_1 \ t_1 \ c_1$ $\vdots$ $u_M \ v_M \ t_M \ c_M$ 出力 雚に濡れた最小の時間を 1 行で出力せよ。たた、末尟に改行も出力せよ。 サンプル 入力䟋 1 2 2 1 2 1 2 10 100 1 2 5 500 出力䟋 1 5 到着時間が遅くおも、できるだけ雚に濡れない経路を遞ぶ。 入力䟋 2 3 2 1 3 1 2 0 123 2 3 123 500 出力䟋 2 0 乗り換えに時間はかからない。
38,879
Problem I: Riffle Shuffle There are a number of ways to shuffle a deck of cards. Riffle shuffle is one such example. The following is how to perform riffle shuffle. There is a deck of n cards. First, we divide it into two decks; deck A which consists of the top half of it and deck B of the bottom half. Deck A will have one more card when n is odd. Next, c cards are pulled from bottom of deck A and are stacked on deck C, which is empty initially. Then c cards are pulled from bottom of deck B and stacked on deck C, likewise. This operation is repeated until deck A and B become empty. When the number of cards of deck A(B) is less than c , all cards are pulled. Finally we obtain shuffled deck C. See an example below: - A single riffle operation where n = 9, c = 3 for given deck [0 1 2 3 4 5 6 7 8] (right is top) - Step 0 deck A [4 5 6 7 8] deck B [0 1 2 3] deck C [] - Step 1 deck A [7 8] deck B [0 1 2 3] deck C [4 5 6] - Step 2 deck A [7 8] deck B [3] deck C [4 5 6 0 1 2] - Step 3 deck A [] deck B [3] deck C [4 5 6 0 1 2 7 8] - Step 4 deck A [] deck B [] deck C [4 5 6 0 1 2 7 8 3] shuffled deck [4 5 6 0 1 2 7 8 3] This operation, called riffle operation, is repeated several times. Write a program that simulates Riffle shuffle and answer which card will be finally placed on the top of the deck. 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 riffle operations, respectively. r more positive integers follow, each of which represents a riffle operation. These riffle operations are performed in the listed order. Each integer represents c , which is explained above. The end of the input is indicated by EOF. The number of data sets is less than 100. Output For each data set in the input, your program should print the number of the top card after the shuffle. Assume that at the beginning the cards are numbered from 0 to n -1, from the bottom to the top. Each number should be written in a sparate line without any superfluous characters such as leading or following spaces. Sample Input 9 1 3 9 4 1 2 3 4 Output for the Sample Input 3 0
38,880
Problem F: Mysterious Maze A robot in a two-dimensional maze again. The maze has an entrance and an exit this time, though. Just as in the previous problem, the maze is made up of H × W grid cells, its upper side faces north, and each cell is either empty or wall. Unlike the previous, on the other hand, one of the empty cells is connected to the entrance and another to the exit. The robot is rather complex - there is some control, but not full. It is associated with a controller that has two buttons, namely forward and turn . The forward button moves the robot forward to the next cell, if possible. The robot can not move into a wall nor outside the maze. The turn button turns the robot as programmed . Here the program is a finite sequence of N commands, each of which is either 'L' (indicating a left turn) or 'R' (a right turn). The first turn follows the first command; the second turn follows the second command; similar for the following turns. The turn button stops working once the commands are exhausted; the forward button still works in such a case though. The robot always turns by 90 degrees at once. The robot is initially put on the entrance cell, directed to the north. Your mission is to determine whether it can reach the exit cell if controlled properly. Input The input is a sequence of datasets. Each dataset is formatted as follows. H W N s 1 ... s N c 1,1 c 1,2 ... c 1, W ... c H ,1 c H ,2 ... c H , W The first line of a dataset contains three integers H , W and N (1 ≀ H , W ≀ 1,000, 1 ≀ N ≀ 1,000,000). The second line contains a program of N commands. Each of the following H lines contains exactly W characters. Each of these characters represents a cell of the maze. " . " indicates empty, " # " indicates a wall, " S " indicates an entrance, and " G " indicates an exit. There is exactly one entrance cell and one exit cell. The end of input is indicated by a line with three zeros. Output For each dataset, output whether the robot can reach the exit in a line: " Yes " if it can or " No " otherwise (without quotes). Sample Input 2 2 1 L G. #S 2 2 2 RR G. .S 3 3 6 LLLLLL G#. ... .#S 0 0 0 Output for the Sample Input Yes No Yes
38,881
Score : 100 points Problem Statement There are N stones, numbered 1, 2, \ldots, N . For each i ( 1 \leq i \leq N ), the height of Stone i is h_i . Here, h_1 < h_2 < \cdots < h_N holds. There is a frog who is initially on Stone 1 . He will repeat the following action some number of times to reach Stone N : If the frog is currently on Stone i , jump to one of the following: Stone i + 1, i + 2, \ldots, N . Here, a cost of (h_j - h_i)^2 + C is incurred, where j is the stone to land on. Find the minimum possible total cost incurred before the frog reaches Stone N . Constraints All values in input are integers. 2 \leq N \leq 2 \times 10^5 1 \leq C \leq 10^{12} 1 \leq h_1 < h_2 < \cdots < h_N \leq 10^6 Input Input is given from Standard Input in the following format: N C h_1 h_2 \ldots h_N Output Print the minimum possible total cost incurred. Sample Input 1 5 6 1 2 3 4 5 Sample Output 1 20 If we follow the path 1 → 3 → 5 , the total cost incurred would be ((3 - 1)^2 + 6) + ((5 - 3)^2 + 6) = 20 . Sample Input 2 2 1000000000000 500000 1000000 Sample Output 2 1250000000000 The answer may not fit into a 32-bit integer type. Sample Input 3 8 5 1 3 4 5 10 11 12 13 Sample Output 3 62 If we follow the path 1 → 2 → 4 → 5 → 8 , the total cost incurred would be ((3 - 1)^2 + 5) + ((5 - 3)^2 + 5) + ((10 - 5)^2 + 5) + ((13 - 10)^2 + 5) = 62 .
38,882
Planarian Regeneration Problem 皆さん、「プラナリア」っお知っおいたすか プラナリアずは、日本では川の䞊流の石や枯葉などの裏に匵り付いお生息しおいる氎生生物です。 プラナリアの最も優れた胜力はその「再生胜力」です。プラナリアの再生胜力は著しく、䟋えば、䞉等分するずそれぞれが再生し、数週間埌には元の完党な状態のプラナリアが3匹できあがりたす。 この床、䌚接の山奥の䞊流にお発芋された新皮のプラナリアの姿は長方圢をしおいお、プラナリアはある法則により再生したす。 このずき、プラナリアは䞋の図のように配眮し、぀の手段でプラナリアの切断実隓を行いたす。 たず぀目の手段ずしお、垂盎方向に切るこずを考えたす。䞀床の切断では、すべおの断片を氎平方向の長さが(断片の頭に近い郚分):(断片の頭に遠い郚分)= m : n になるように切断したす。この動䜜を x 回繰り返したす。 次に぀目の手段ずしお、氎平方向に切るこずを考えたす。䞀床の切断では、すべおの断片を垂盎方向の長さが(断片の右端に近い郚分):(断片の巊端に近い郚分)= k : l に切断したす。この動䜜を y 回繰り返したす。これらの切断によっお断片が移動するこずはありたせん。 数週間埌に、各断片が元の完党なプラナリアの状態に再生しおいる確率は以䞋の匏により求たりたす。 䞊の図は、垂盎方向に1:2で2回、氎平方向に1:1で1回切断した状態です。 以䞊の操䜜で切断した時の、数週間埌に元の姿に再生しおいるプラナリアの数の期埅倀を求めおください。元のプラナリアの垂盎方向の長さず氎平方向の長さはずもに1ずしたす。 Input m n x k l y 入力では、6぀の敎数 m , n , x , k , l , y が䞊蚘の入力フォヌマットで䞎えられたす。 これら6぀の敎数は問題文䞭のものず察応しおおり、氎平方向に m : n で切断する動䜜を x 回、垂盎方向に k : l で切断する動䜜を y 回繰り返したす。 1 ≀ m , n , k , l ≀ 100、 0 ≀ x , y ≀ 40 であり、 m , n ず l , k はそれぞれ互いに玠です。 Output 䞀行に数週間埌に元の姿に再生しおいるプラナリアの数の期埅倀を出力しおください。 出力は 10 -6 以䞋の誀差ならば蚱容されたす。 Sample Input 1 1 1 1 1 1 1 Sample Output 1 0.562500 Sample Input 2 1 2 2 1 1 1 Sample Output 2 0.490741 Sample Input 3 1 2 0 3 4 0 Sample Output 3 1.000000 Notes この問題では、doubleよりも粟床の高い浮動小数を䜿甚するこずを掚奚したす。
38,883
Score : 700 points Problem Statement Snuke and Ciel went to a strange stationery store. Each of them got a transparent graph paper with H rows and W columns. Snuke painted some of the cells red in his paper. Here, the cells painted red were 4-connected , that is, it was possible to traverse from any red cell to any other red cell, by moving to vertically or horizontally adjacent red cells only. Ciel painted some of the cells blue in her paper. Here, the cells painted blue were 4-connected. Afterwards, they precisely overlaid the two sheets in the same direction. Then, the intersection of the red cells and the blue cells appeared purple. You are given a matrix of letters a_{ij} ( 1≀i≀H , 1≀j≀W ) that describes the positions of the purple cells. If the cell at the i -th row and j -th column is purple, then a_{ij} is # , otherwise a_{ij} is . . Here, it is guaranteed that no outermost cell is purple . That is, if i=1, H or j = 1, W , then a_{ij} is . . Find a pair of the set of the positions of the red cells and the blue cells that is consistent with the situation described. It can be shown that a solution always exists. Constraints 3≀H,W≀500 a_{ij} is # or . . If i=1,H or j=1,W , then a_{ij} is . . At least one of a_{ij} is # . Input The input is given from Standard Input in the following format: H W a_{11} ... a_{1W} : a_{H1} ... a_{HW} Output Print a pair of the set of the positions of the red cells and the blue cells that is consistent with the situation, as follows: The first H lines should describe the positions of the red cells. The following 1 line should be empty. The following H lines should describe the positions of the blue cells. The description of the positions of the red or blue cells should follow the format of the description of the positions of the purple cells. Sample Input 1 5 5 ..... .#.#. ..... .#.#. ..... Sample Output 1 ..... ##### #.... ##### ..... .###. .#.#. .#.#. .#.#. ..... One possible pair of the set of the positions of the red cells and the blue cells is as follows: Sample Input 2 7 13 ............. .###.###.###. .#.#.#...#... .###.#...#... .#.#.#.#.#... .#.#.###.###. ............. Sample Output 2 ............. .###########. .###.###.###. .###.###.###. .###.###.###. .###.###.###. ............. ............. .###.###.###. .#.#.#...#... .###.#...#... .#.#.#.#.#... .#.#########. ............. One possible pair of the set of the positions of the red cells and the blue cells is as follows:
38,884
Problem D: Goofy Converter Nathan O. Davis is a student at the department of integrated systems. He is now taking a class in in- tegrated curcuits. He is an idiot. One day, he got an assignment as follows: design a logic circuit that takes a sequence of positive integers as input, and that outputs a sequence of 1-bit integers from which the original input sequence can be restored uniquely. Nathan has no idea. So he searched for hints on the Internet, and found several pages that describe the 1-bit DAC. This is a type of digital-analog converter which takes a sequence of positive integers as input, and outputs a sequence of 1-bit integers. Seeing how 1-bit DAC works on these pages, Nathan came up with a new idea for the desired converter. His converter takes a sequence L of positive integers, and a positive integer M aside from the sequence, and outputs a sequence K of 1-bit integers such that: He is not so smart, however. It is clear that his converter does not work for some sequences. Your task is to write a program in order to show the new converter cannot satisfy the requirements of his assignment, even though it would make Nathan in despair. Input The input consists of a series of data sets. Each data set is given in the following format: N M L 0 L 1 . . . L N -1 N is the length of the sequence L . M and L are the input to Nathan’s converter as described above. You may assume the followings: 1 ≀ N ≀ 1000, 1 ≀ M ≀ 12, and 0 ≀ L j ≀ M for j = 0, . . . , N - 1. The input is terminated by N = M = 0. Output For each data set, output a binary sequence K of the length ( N + M - 1) if there exists a sequence which holds the equation mentioned above, or “Goofy” (without quotes) otherwise. If more than one sequence is possible, output any one of them. Sample Input 4 4 4 3 2 2 4 4 4 3 2 3 0 0 Output for the Sample Input 1111001 Goofy
38,885
Score: 100 points Problem Statement In 2020 , AtCoder Inc. with an annual sales of more than one billion yen (the currency of Japan) has started a business in programming education. One day, there was an exam where a one-year-old child must write a program that prints Hello World , and a two-year-old child must write a program that receives integers A, B and prints A+B . Takahashi, who is taking this exam, suddenly forgets his age. He decides to write a program that first receives his age N ( 1 or 2 ) as input, then prints Hello World if N=1 , and additionally receives integers A, B and prints A+B if N=2 . Write this program for him. Constraints N is 1 or 2 . A is an integer between 1 and 9 (inclusive). B is an integer between 1 and 9 (inclusive). Input Input is given from Standard Input in one of the following formats: 1 2 A B Output If N=1 , print Hello World ; if N=2 , print A+B . Sample Input 1 1 Sample Output 1 Hello World As N=1 , Takahashi is one year old. Thus, we should print Hello World . Sample Input 2 2 3 5 Sample Output 2 8 As N=2 , Takahashi is two years old. Thus, we should print A+B , which is 8 since A=3 and B=5 .
38,886
Score : 700 points Problem Statement You are given a string S consisting of a , b and c . Find the number of strings that can be possibly obtained by repeatedly performing the following operation zero or more times, modulo 998244353 : Choose an integer i such that 1\leq i\leq |S|-1 and the i -th and (i+1) -th characters in S are different. Replace each of the i -th and (i+1) -th characters in S with the character that differs from both of them (among a , b and c ). Constraints 2 \leq |S| \leq 2 × 10^5 S consists of a , b and c . Input Input is given from Standard Input in the following format: S Output Print the number of strings that can be possibly obtained by repeatedly performing the operation, modulo 998244353 . Sample Input 1 abc Sample Output 1 3 abc , aaa and ccc can be obtained. Sample Input 2 abbac Sample Output 2 65 Sample Input 3 babacabac Sample Output 3 6310 Sample Input 4 ababacbcacbacacbcbbcbbacbaccacbacbacba Sample Output 4 148010497
38,887
Problem Statement You bought 3 ancient scrolls from a magician. These scrolls have a long string, and the lengths of the strings are the same. He said that these scrolls are copies of the key string to enter a dungeon with a secret treasure. However, he also said, they were copied so many times by hand, so the string will contain some errors, though the length seems correct. Your job is to recover the original string from these strings. When finding the original string, you decided to use the following assumption. The copied string will contain at most d errors. In other words, the Hamming distance of the original string and the copied string is at most d . If there exist many candidates, the lexicographically minimum string is the original string. Can you find the orignal string? Input The input contains a series of datasets. Each dataset has the following format: l d str_1 str_2 str_3 The first line contains two integers l ( 1 \leq l \leq 100,000 ) and d ( 0 \leq d \leq 5,000 .) l describes the length of 3 given strings and d describes acceptable maximal Hamming distance. The following 3 lines have given strings, whose lengths are l . These 3 strings consist of only lower and upper case alphabets. The input ends with a line containing two zeros, which should not be processed. Output Print the lexicographically minimum satisfying the condition in a line. If there do not exist such strings, print -1 . Sample Input 3 1 ACM IBM ICM 5 2 iwzwz iziwi zwizi 1 0 A B C 10 5 jLRNlNyGWx yyLnlyyGDA yLRnvyyGDA 0 0 Output for the Sample Input ICM iwiwi -1 AARAlNyGDA
38,888
Score : 1100 points Problem Statement There is a tree with N vertices numbered 1, 2, ..., N . The edges of the tree are denoted by (x_i, y_i) . On this tree, Alice and Bob play a game against each other. Starting from Alice, they alternately perform the following operation: Select an existing edge and remove it from the tree, disconnecting it into two separate connected components. Then, remove the component that does not contain Vertex 1 . A player loses the game when he/she is unable to perform the operation. Determine the winner of the game assuming that both players play optimally. Constraints 2 \leq N \leq 100000 1 \leq x_i, y_i \leq N The given graph is a tree. Input Input is given from Standard Input in the following format: N x_1 y_1 x_2 y_2 : x_{N-1} y_{N-1} Output Print Alice if Alice wins; print Bob if Bob wins. Sample Input 1 5 1 2 2 3 2 4 4 5 Sample Output 1 Alice If Alice first removes the edge connecting Vertices 1 and 2 , the tree becomes a single vertex tree containing only Vertex 1 . Since there is no edge anymore, Bob cannot perform the operation and Alice wins. Sample Input 2 5 1 2 2 3 1 4 4 5 Sample Output 2 Bob Sample Input 3 6 1 2 2 4 5 1 6 3 3 2 Sample Output 3 Alice Sample Input 4 7 1 2 3 7 4 6 2 3 2 4 1 5 Sample Output 4 Bob
38,889
Problem B Quality of Check Digits The small city where you live plans to introduce a new social security number (SSN) system. Each citizen will be identified by a five-digit SSN. Its first four digits indicate the basic ID number (0000 - 9999) and the last one digit is a check digit for detecting errors. For computing check digits, the city has decided to use an operation table. An operation table is a 10 $\times$ 10 table of decimal digits whose diagonal elements are all 0. Below are two example operation tables. Operation Table 1 Operation Table 2 Using an operation table, the check digit $e$ for a four-digit basic ID number $abcd$ is computed by using the following formula. Here, $i \otimes j$ denotes the table element at row $i$ and column $j$. $e = (((0 \otimes a) \otimes b) \otimes c) \otimes d$ For example, by using Operation Table 1 the check digit $e$ for a basic ID number $abcd = $ 2016 is computed in the following way. $e = (((0 \otimes 2) \otimes 0) \otimes 1) \otimes 6$ $\;\;\; = (( \;\;\;\;\;\;\;\;\; 1 \otimes 0) \otimes 1) \otimes 6$ $\;\;\; = ( \;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\; 7 \otimes 1) \otimes 6$ $\;\;\; = \;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\; 9 \otimes 6$ $\;\;\; = \;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\;\; 6$ Thus, the SSN is 20166. Note that the check digit depends on the operation table used. With Operation Table 2, we have $e = $ 3 for the same basic ID number 2016, and the whole SSN will be 20163. Figure B.1. Two kinds of common human errors The purpose of adding the check digit is to detect human errors in writing/typing SSNs. The following check function can detect certain human errors. For a five-digit number $abcde$, the check function is defined as follows. check ($abcde$) $ = ((((0 \otimes a) \otimes b) \otimes c) \otimes d) \otimes e$ This function returns 0 for a correct SSN. This is because every diagonal element in an operation table is 0 and for a correct SSN we have $e = (((0 \otimes a) \otimes b) \otimes c) \otimes d$: check ($abcde$) $ = ((((0 \otimes a) \otimes b) \otimes c) \otimes d) \otimes e = e \otimes e = 0$ On the other hand, a non-zero value returned by check indicates that the given number cannot be a correct SSN. Note that, depending on the operation table used, check function may return 0 for an incorrect SSN. Kinds of errors detected depends on the operation table used; the table decides the quality of error detection. The city authority wants to detect two kinds of common human errors on digit sequences: altering one single digit and transposing two adjacent digits, as shown in Figure B.1. An operation table is good if it can detect all the common errors of the two kinds on all SSNs made from four-digit basic ID numbers 0000{9999. Note that errors with the check digit, as well as with four basic ID digits, should be detected. For example, Operation Table 1 is good. Operation Table 2 is not good because, for 20613, which is a number obtained by transposing the 3rd and the 4th digits of a correct SSN 20163, check (20613) is 0. Actually, among 10000 basic ID numbers, Operation Table 2 cannot detect one or more common errors for as many as 3439 basic ID numbers. Given an operation table, decide how good it is by counting the number of basic ID numbers for which the given table cannot detect one or more common errors. Input The input consists of a single test case of the following format. $x_{00}$ $x_{01}$ ... $x_{09}$ ... $x_{90}$ $x_{91}$ ... $x_{99}$ The input describes an operation table with $x_{ij}$ being the decimal digit at row $i$ and column $j$. Each line corresponds to a row of the table, in which elements are separated by a single space. The diagonal elements $x_{ii}$ ($i = 0, ... , 9$) are always 0. Output Output the number of basic ID numbers for which the given table cannot detect one or more common human errors. Sample Input 1 0 3 1 7 5 9 8 6 4 2 7 0 9 2 1 5 4 8 6 3 4 2 0 6 8 7 1 3 5 9 1 7 5 0 9 8 3 4 2 6 6 1 2 3 0 4 5 9 7 8 3 6 7 4 2 0 9 5 8 1 5 8 6 9 7 2 0 1 3 4 8 9 4 5 3 6 2 0 1 7 9 4 3 8 6 1 7 2 0 5 2 5 8 1 4 3 6 7 9 0 Sample Output 1 0 Sample Input 2 0 1 2 3 4 5 6 7 8 9 9 0 1 2 3 4 5 6 7 8 8 9 0 1 2 3 4 5 6 7 7 8 9 0 1 2 3 4 5 6 6 7 8 9 0 1 2 3 4 5 5 6 7 8 9 0 1 2 3 4 4 5 6 7 8 9 0 1 2 3 3 4 5 6 7 8 9 0 1 2 2 3 4 5 6 7 8 9 0 1 1 2 3 4 5 6 7 8 9 0 Sample Output 2 3439 Sample Input 3 0 9 8 7 6 5 4 3 2 1 1 0 9 8 7 6 5 4 3 2 2 1 0 9 8 7 6 5 4 3 3 2 1 0 9 8 7 6 5 4 4 3 2 1 0 9 8 7 6 5 5 4 3 2 1 0 9 8 7 6 6 5 4 3 2 1 0 9 8 7 7 6 5 4 3 2 1 0 9 8 8 7 6 5 4 3 2 1 0 9 9 8 7 6 5 4 3 2 1 0 Sample Output 3 9995 Sample Input 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Sample Output 4 10000
38,890
Enumeration of Subsets III You are given a set $T$, which is a subset of $S$. The set $S$ consists of $0, 1, ... n-1$. Print all subsets of $T$. Note that we represent $0, 1, ... n-1$ as 00...0001, 00...0010, 00...0100, ..., 10...0000 in binary respectively and the integer representation of a subset is calculated by bitwise OR of existing elements. Input The input is given in the following format. $n$ $k \; b_0 \; b_1 \; ... \; b_{k-1}$ $k$ is the number of elements in $T$, and $b_i$ represents elements in $T$. Output Print the subsets ordered by their decimal integers. Print a subset in the following format. $d$: $e_0$ $e_1$ ... Print ' : ' after the integer value $d$, then print elements $e_i$ in the subset in ascending order. Separate two adjacency elements by a space character. Constraints $1 \leq n \leq 28$ $0 \leq k \leq 18$ $k \leq n$ $0 \leq b_i < n$ Sample Input 1 4 2 0 2 Sample Output 1 0: 1: 0 4: 2 5: 0 2
38,891
ぞびの脱皮 セアブリ高原で発芋された新皮のぞびは、頭から尟にかけお列に䞊んだマルoずバツxからなる暡様が特城です。䞋図に䟋を瀺したすが、ぞびの暡様は個䜓により様々です。 xxoooxxxoox このぞびは脱皮のずきに、぀のマルが䞊んだ郚分の間すべおが䌞びるこずで成長したす。新たに加わった箇所にはマル、バツ、マルが䞊んだ暡様が付きたす。 たずえば、長さのぞび ooo が回脱皮するず、巊から1番目ず番目、番目ず番目のマルの間にマル、バツ、マルが䞊んだ暡様が加わるので、脱皮埌のぞびは ooxoooxoo ずなりたす䞋線郚が新たに加わった箇所です。もう䞀回脱皮するず ooxooxooxoooxooxooxoo ずなり、ぞびの長さはになりたす。 このぞびの生態を研究しおいるあなたは、このぞびが脱皮を繰り返したら、ずお぀もない長さになっおしたうかもしれないず危惧しおいたす。 ぞびの暡様ず脱皮の回数が䞎えられたずき、この回数だけ脱皮した埌のぞびの長さを求めるプログラムを䜜成せよ。 入力 入力は以䞋の圢匏で䞎えられる。 $L$ $N$ $snake$ 行目に脱皮する前の蛇の長さ$L$($1\leq L \leq 100$)ず脱皮の回数$N$ ($1 \leq N \leq 50$)が䞎えられる。続く行に脱皮する前のぞびの暡様を衚す長さ$L$の文字列$snake$が䞎えられる。ただし$snake$は英小文字 x ず o からなる。 出力 ぞびの長さを行に出力する。 入出力䟋 入力䟋 3 2 ooo 出力䟋 21 入力䟋 3 4 xoo 出力䟋 48 入力䟋 13 30 xooxoooxxxoox 出力䟋 12884901889
38,892
Score : 200 points Problem Statement We have sticks numbered 1, \cdots, N . The length of Stick i (1 \leq i \leq N) is L_i . In how many ways can we choose three of the sticks with different lengths that can form a triangle? That is, find the number of triples of integers (i, j, k) (1 \leq i < j < k \leq N) that satisfy both of the following conditions: L_i , L_j , and L_k are all different. There exists a triangle whose sides have lengths L_i , L_j , and L_k . Constraints 1 \leq N \leq 100 1 \leq L_i \leq 10^9 All values in input are integers. Input Input is given from Standard Input in the following format: N L_1 L_2 \cdots L_N Output Print the number of ways to choose three of the sticks with different lengths that can form a triangle. Sample Input 1 5 4 4 9 7 5 Sample Output 1 5 The following five triples (i, j, k) satisfy the conditions: (1, 3, 4) , (1, 4, 5) , (2, 3, 4) , (2, 4, 5) , and (3, 4, 5) . Sample Input 2 6 4 5 4 3 3 5 Sample Output 2 8 We have two sticks for each of the lengths 3 , 4 , and 5 . To satisfy the first condition, we have to choose one from each length. There is a triangle whose sides have lengths 3 , 4 , and 5 , so we have 2 ^ 3 = 8 triples (i, j, k) that satisfy the conditions. Sample Input 3 10 9 4 6 1 9 6 10 6 6 8 Sample Output 3 39 Sample Input 4 2 1 1 Sample Output 4 0 No triple (i, j, k) satisfies 1 \leq i < j < k \leq N , so we should print 0 .
38,893
B: Spent Fuel Disposal 問題文 $N$ 個の䞋氎凊理斜蚭が $M$ 本のパむプで繋がっおおり、$i$ 番目のパむプは $u_i$ 番目ず $v_i$ 番目の斜蚭を繋いでいたす。 パむプは党お、毎秒最倧 $1$ リットルの液䜓を流すこずができたす、 今、$S$ 番目の斜蚭から $T$ 番目の斜蚭ぞ工業廃氎を、 $U$ 番目の斜蚭から $V$ 番目の斜蚭ぞ玔氎を流したいです。$S$ 番目および $U$ 番目の斜蚭は、それぞれ工業廃氎および玔氎を十分な量有しおおり、パむプで繋がっおいる別の斜蚭ぞこれらの液䜓を奜きに送るこずができたす。 たた、各斜蚭では、送られおきた液䜓をパむプで繋がっおいる別の凊理斜蚭ぞ流すこずができたす。 パむプはハむテクなので、$2$ 皮類の液䜓を同時に奜きな方向ぞ流すこずができたす。 䟋えば、$i$ 番目のパむプは、$u_i$ から $v_i$ ぞ工業廃氎を、$v_i$ から $u_i$ ぞ玔氎を流すこずができたす。 反察に $v_i$ から $u_i$ ぞ工業廃氎を、$u_i$ から $v_i$ ぞ玔氎を流すこずも可胜です。 もちろん、$2$ 皮類の液䜓を同じ方向ぞ流すこずもできたす。 ただし、どの堎合においおも、$1$ ぀のパむプに流す液䜓の総量は毎秒 $1$ リットルを超えるこずはできたせん。 $S$ から $T$ ぞ流す工業廃氎の総量ず $U$ から $V$ ぞ流す玔氎の総量の合蚈は、最倧で毎秒䜕リットルにできたすか。 答えは敎数になるこずが蚌明できるため、敎数で出力しおください。 制玄 入力は党お敎数 $1 \leq N, M \leq 10^5$ $1 \leq S, T, U, V \leq N$ $S \neq T$ $U \neq V$ $1 \leq u_i, v_i \leq N$ $u_i < v_i$ ($i = 1, \ldots, M$) $i \neq j \rightarrow (u_i, v_i) \neq (u_j, v_j)$ 党おの斜蚭はパむプを通じお連結である 入力 入力は暙準入力から以䞋の圢匏で䞎えられたす。 $N$ $M$ $S$ $T$ $U$ $V$ $u_1$ $v_1$ $\vdots$ $u_M$ $v_M$ 出力 答えを1行に出力しおください。 入出力䟋 入力䟋1 5 6 1 4 2 3 1 2 2 3 3 4 4 5 3 5 1 5 出力䟋1 2 䟋えば、$1$ から $5$、$5$ から $4$ ぞ工業排氎を毎秒 $1$ リットル、$2$ から $3$ ぞ玔氎を毎秒 $1$ リットル流せば良いです。 入力䟋2 3 3 1 2 1 3 1 2 1 3 2 3 出力䟋2 2 $T$ ず $V$ が同䞀の斜蚭であるケヌスなどに泚意しおください。
38,894
玠数 II 玠数ずいうのは、1 よりも倧きくそれ自身か 1 でしか割りきれない敎数をいいたす。䟋えば、2 は、2 ず 1 でしか割り切れないので玠数ですが、12 は、12 ず 1 のほかに、2, 3, 4, 6 で割りきれる数なので玠数ではありたせん。 敎数 n を入力したずき、 n より小さい玠数のうち最も倧きいものず、 n より倧きい玠数のうち最も小さいものを出力するプログラムを䜜成しおください。 Input 耇数のデヌタセットが䞎えられたす。各デヌタセットに n (3 ≀ n ≀ 50,000) が行に䞎えられたす。 デヌタセットの数は 50 を超えたせん。 Output 各デヌタセットに察しお、 n より小さい玠数のうち最倧のものず、 n より倧きい玠数のうち最小のものを぀のスペヌスで区切っお行に出力しお䞋さい。 Sample Input 19 3517 Output for the Sample Input 17 23 3511 3527
38,895
瞄匵り(Territory) あなたは南北方向にずおも長く䌞びたたくさんの道路ず東西方向にずおも長く䌞びたたくさんの道路が亀わった圢をした街に䜏んでいる隣り合う2 ぀の南北方向の道路の間隔は 1 km であるたた隣り合う 2 ぀の東西方向の道路の間隔も 1 km である この街には垂圹所が 1 ぀ある垂圹所のある亀差点を $(0,0)$ ず衚すこの街の亀差点は 2 ぀の敎数 $i, j$ を甚いお亀差点 $(i, j)$ ず衚されるすなわち亀差点 $(i, j)$ ずは亀差点 $(0,0)$ から東に $i$ km ($i < 0$ のずきは西に $-i$ km)北に $j$ km ( $j < 0$ のずきは南に $-j$ km) 進んだ䜍眮の亀差点を衚す 垂圹所ではゞョむ君ずいう名の 1 匹の犬を飌っおいるゞョむ君は $K$ 日間の散歩の蚈画を立おた散歩の蚈画は以䞋の通りである $K$ 日のうち最初の日の朝にはゞョむ君は亀差点 $(0,0)$ にいるゞョむ君は亀差点 $(0,0)$ に印を付ける$(0,0)$ 以倖にはゞョむ君が印を付けた亀差点はない $K$ 日のそれぞれの日の昌に散歩を行う1 日の散歩は $N$ 回のステップからなる各ステップでは亀差点から隣の亀差点ぞず移動し移動先に印を付けるゞョむ君がそれぞれの日の昌にどう移動するかは日によらず䞀定である 昌の移動が終わった埌は珟圚いる亀差点で次の日の朝たで寝る 垂圹所では $K$ 日間の散歩によっおできるゞョむ君の瞄匵りに぀いお話題になっおいる4 ぀の亀差点 $(a,b),(a + 1,b),(a + 1,b + 1),(a,b + 1)$ のいずれにもゞョむ君が 1 回以䞊印を付けおいるずき4 ぀の亀差点で囲たれた区画はゞョむ君の瞄匵りに属する あなたはゞョむ君の散歩蚈画からゞョむ君の瞄匵りに属する区画の個数を蚈算するプログラムを䜜成するこずずなった この街の道路はずおも長くたた南北方向にも東西方向にも十分たくさんの道路があるため散歩の途䞭でゞョむ君が道路の端や街の端に到達するこずはない 課題 ゞョむ君の散歩蚈画が䞎えられるずゞョむ君の瞄匵りに属する区画の個数を求めるプログラムを䜜成せよ 入力 暙準入力から以䞋の入力を読み蟌め 1 行目には 2 個の敎数 $N, K$ が空癜を区切りずしお曞かれおいるこれはそれぞれの日の散歩が $N$ 回のステップからなり散歩蚈画が $K$ 日間に枡るこずを衚しおいる 2 行目には長さ $N$ の文字列 $S$ が曞かれおいる文字列 $S$ のうち巊から $p$ 文字目 $(1 \leq p \leq N)$ の文字 $C_p$ は ENWS のいずれかであるこれらの文字は以䞋のこずを衚す 文字 $C_p$ が E であるならば$p$ 番目のステップで東隣の亀差点に移動するこずを衚す 文字 $C_p$ が N であるならば$p$ 番目のステップで北隣の亀差点に移動するこずを衚す 文字 $C_p$ が W であるならば$p$ 番目のステップで西隣の亀差点に移動するこずを衚す 文字 $C_p$ が S であるならば$p$ 番目のステップで南隣の亀差点に移動するこずを衚す ここで亀差点$(i, j)$ に察しお東隣北隣西隣南隣の亀差点はそれぞれ亀差点 $(i + 1, j)$亀差点$(i, j + 1)$亀差点$(i - 1, j)$亀差点$(i, j - 1)$ である 出力 暙準出力にゞョむ君の瞄匵りに属する区画の個数を 1 行で出力せよ 制限 すべおの入力デヌタは以䞋の条件を満たす $1 \leq N \leq 100 000$ $1 \leq K \leq 1 000 000 000$ 入出力䟋 入力䟋1 12 1 EENWSEEESWWS 出力䟋1 3 この入力䟋では散歩は 1 日間で行われる1 日目にゞョむ君は垂圹所から出発しお䞋図のように移動する黒䞞はゞョむ君が印を付けた亀差点癜䞞はゞョむ君が印を付けおいない亀差点二重䞞は垂圹所のある亀差点数字は各ステップを衚す ゞョむ君の移動経路 入力䟋 1 においお䞋図の斜線郚分で瀺された 3 個の区画がゞョむ君の瞄匵りに属する 入力䟋1 におけるゞョむ君の瞄匵り 入力䟋2 12 2 EENWSEEESWWS 出力䟋2 7 入力䟋 2 では散歩が 2 日間に枡り行われるそれぞれの日の移動経路は入力䟋 1 ず同䞀である散歩が完了したずき䞋図の斜線郚分で瀺された 7 個の区画がゞョむ君の瞄匵りに属する 入力䟋2 におけるゞョむ君の瞄匵り 入力䟋3 7 1 ENNWNNE 出力䟋3 0 入力䟋 3 ではゞョむ君の瞄匵りに属する区画は存圚しない 入力䟋4 16 5 WSESSSWWWEEENNNW 出力䟋4 21 第15回 日本情報オリンピック本遞 課題 2016 幎 2 月 14 日
38,896
I: Add Problem Statement Mr. T has had an integer sequence of N elements a_1, a_2, ... , a_N and an integer K . Mr. T has created N integer sequences B_1, B_2, ... , B_N such that B_i has i elements. B_{N,j} = a_j ( 1 \leq j \leq N ) B_{i,j} = K \times B_{i+1,j} + B_{i+1,j+1} ( 1\leq i \leq N-1, 1 \leq j \leq i ) Mr. T was so careless that he lost almost all elements of these sequences a and B_i . Fortunately, B_{1,1}, B_{2,1}, ... , B_{N,1} and K are not lost. Your task is to write a program that restores the elements of the initial sequence a for him. Output the modulo 65537 of each element instead because the absolute value of these elements can be extremely large. More specifically, for all integers i ( 1 \leq i \leq N ), output r_i that satisfies r_i $\equiv$ a_i $ \bmod \;$ 65537, 0 \leq r_i < 65537 . Here, we can prove that the original sequence Mr. T had can be uniquely determined under the given constraints. Input T N_1 K_1 C_{1,1} C_{1,2} ... C_{1,N} N_2 K_2 C_{2,1} C_{2,2} ... C_{2,N} : N_T K_T C_{T,1} C_{T,2} ... C_{T,N} The first line contains a single integer T that denotes the number of test cases. Each test case consists of 2 lines. The first line of the i -th test case contains two integers N_i and K_i . The second line of the i -th test case contains N_i integers C_{i,j} ( 1 \leq j \leq N_i ). These values denote that N = N_i , K = K_i , B_{j,1} = C_{i,j} ( 1 \leq j \leq N_i ) in the i -th test case. Constraints 1 \leq T \leq 10 1 \leq N \leq 50000 |K| \leq 10^9 |B_{i,1}| \leq 10^9 All input values are integers. Output Output T lines. For the i -th line, output the answer for the i -th test case a_1, a_2, ..., a_N in this order. Each number must be separated by a single space. Sample Input 1 2 3 0 1 2 3 3 1 1 2 3 Output for Sample Input 1 3 2 1 3 65536 0
38,897
Aizu PR An English booklet has been created for publicizing Aizu to the world. When you read it carefully, you found a misnomer (an error in writing) on the last name of Masayuki Hoshina, the lord of the Aizu domain. The booklet says "Hoshino" not "Hoshina". Your task is to write a program which replace all the words "Hoshino" with "Hoshina". You can assume that the number of characters in a text is less than or equal to 1000. Input The input consists of several datasets. There will be the number of datasets n in the first line. There will be n lines. A line consisting of english texts will be given for each dataset. Output For each dataset, print the converted texts in a line. Sample Input 3 Hoshino Hashino Masayuki Hoshino was the grandson of Ieyasu Tokugawa. Output for the Sample Input Hoshina Hashino Masayuki Hoshina was the grandson of Ieyasu Tokugawa.
38,898
Problem B: Kaeru Jump There is a frog living in a big pond. He loves jumping between lotus leaves floating on the pond. Interestingly, these leaves have strange habits. First, a leaf will sink into the water after the frog jumps from it. Second, they are aligned regularly as if they are placed on the grid points as in the example below. Figure 1: Example of floating leaves Recently, He came up with a puzzle game using these habits. At the beginning of the game, he is on some leaf and faces to the upper, lower, left or right side. He can jump forward or to the left or right relative to his facing direction, but not backward or diagonally. For example, suppose he is facing to the left side, then he can jump to the left, upper and lower sides but not to the right side. In each jump, he will land on the nearest leaf on his jumping direction and face to that direction regardless of his previous state. The leaf he was on will vanish into the water after the jump. The goal of this puzzle is to jump from leaf to leaf until there is only one leaf remaining. See the example shown in the figure below. In this situation, he has three choices, namely, the leaves A, B and C. Note that he cannot jump to the leaf D since he cannot jump backward. Suppose that he choose the leaf B. After jumping there, the situation will change as shown in the following figure. He can jump to either leaf E or F next. After some struggles, he found this puzzle difficult, since there are a lot of leaves on the pond. Can you help him to find out a solution? Input H W c 1,1 ... c 1, W . . . c H ,1 ... c H , W The first line of the input contains two positive integers H and W (1 ≀ H , W ≀ 10). The following H lines, which contain W characters each, describe the initial configuration of the leaves and the frog using following characters: '.’ : water ‘o’ : a leaf ‘U’ : a frog facing upward (i.e. to the upper side) on a leaf ‘D’ : a frog facing downward (i.e. to the lower side) on a leaf ‘L’ : a frog facing leftward (i.e. to the left side) on a leaf ‘R’ : a frog facing rightward (i.e. to the right side) on a leaf You can assume that there is only one frog in each input. You can also assume that the total number of leaves (including the leaf the frog is initially on) is at most 30. Output Output a line consists of the characters ‘U’ (up), ‘D’ (down), ‘L’ (left) and ‘R’ (right) that describes a series of movements. The output should not contain any other characters, such as spaces. You can assume that there exists only one solution for each input. Sample Input 1 2 3 Uo. .oo Output for the Sample Input 1 RDR Sample Input 2 10 10 .o....o... o.oo...... ..oo..oo.. ..o....... ..oo..oo.. ..o...o.o. o..U.o.... oo......oo oo........ oo..oo.... Output for the Sample Input 2 URRULULDDLUURDLLLURRDLDDDRRDR Sample Input 3 10 1 D . . . . . . . . o Output for the Sample Input 3 D
38,899
Score : 300 points Problem Statement Snuke can change a string t of length N into a string t' of length N - 1 under the following rule: For each i ( 1 ≀ i ≀ N - 1 ), the i -th character of t' must be either the i -th or (i + 1) -th character of t . There is a string s consisting of lowercase English letters. Snuke's objective is to apply the above operation to s repeatedly so that all the characters in s are the same. Find the minimum necessary number of operations. Constraints 1 ≀ |s| ≀ 100 s consists of lowercase English letters. Input Input is given from Standard Input in the following format: s Output Print the minimum necessary number of operations to achieve the objective. Sample Input 1 serval Sample Output 1 3 One solution is: serval → srvvl → svvv → vvv . Sample Input 2 jackal Sample Output 2 2 One solution is: jackal → aacaa → aaaa . Sample Input 3 zzz Sample Output 3 0 All the characters in s are the same from the beginning. Sample Input 4 whbrjpjyhsrywlqjxdbrbaomnw Sample Output 4 8 In 8 operations, he can change s to rrrrrrrrrrrrrrrrrr .
38,900
Score : 300 points Problem Statement You are given four integers A , B , C , and D . Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D . Constraints 1\leq A\leq B\leq 10^{18} 1\leq C,D\leq 10^9 All values in input are integers. Input Input is given from Standard Input in the following format: A B C D Output Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D . Sample Input 1 4 9 2 3 Sample Output 1 2 5 and 7 satisfy the condition. Sample Input 2 10 40 6 8 Sample Output 2 23 Sample Input 3 314159265358979323 846264338327950288 419716939 937510582 Sample Output 3 532105071133627368
38,901
Problem Statement You want to compete in ICPC (Internet Contest of Point Collection). In this contest, we move around in $N$ websites, numbered $1$ through $N$, within a time limit and collect points as many as possible. We can start and end on any website. There are $M$ links between the websites, and we can move between websites using these links. You can assume that it doesn't take time to move between websites. These links are directed and websites may have links to themselves. In each website $i$, there is an advertisement and we can get $p_i$ point(s) by watching this advertisement in $t_i$ seconds. When we start on or move into a website, we can decide whether to watch the advertisement or not. But we cannot watch the same advertisement more than once before using any link in the website, while we can watch it again if we have moved among websites and returned to the website using one or more links, including ones connecting a website to itself. Also we cannot watch the advertisement in website $i$ more than $k_i$ times. You want to win this contest by collecting as many points as you can. So you decided to compute the maximum points that you can collect within $T$ seconds. Input The input consists of multiple datasets. The number of dataset is no more than $60$. Each dataset is formatted as follows. $N$ $M$ $T$ $p_1$ $t_1$ $k_1$ : : $p_N$ $t_N$ $k_N$ $a_1$ $b_1$ : : $a_M$ $b_M$ The first line of each dataset contains three integers $N$ ($1 \le N \le 100$), $M$ ($0 \le M \le 1{,}000$) and $T$ ($1 \le T \le 10{,}000$), which denote the number of websites, the number of links, and the time limit, respectively. All the time given in the input is expressed in seconds. The following $N$ lines describe the information of advertisements. The $i$-th of them contains three integers $p_i$ ($1 \le p_i \le 10{,}000$), $t_i$ ($1 \le t_i \le 10{,}000$) and $k_i$ ($1 \le k_i \le 10{,}000$), which denote the points of the advertisement, the time required to watch the advertisement, and the maximum number of times you can watch the advertisement in website $i$, respectively. The following $M$ lines describe the information of links. Each line contains two integers $a_i$ and $b_i$ ($1 \le a_i,b_i \le N$), which mean that we can move from website $a_i$ to website $b_i$ using a link. The end of input is indicated by a line containing three zeros. Output For each dataset, output the maximum points that you can collect within $T$ seconds. Sample Input 5 4 10 4 3 1 6 4 3 3 2 4 2 2 1 8 5 3 1 2 2 3 3 4 4 5 3 3 1000 1000 1 100 1 7 100 10 9 100 1 2 2 3 3 2 1 0 5 25 25 2 1 0 25 25 25 2 5 5 100 1 1 20 1 1 20 10 1 1 10 1 1 10 1 1 1 2 2 1 3 4 4 5 5 3 3 3 100 70 20 10 50 15 20 90 10 10 1 2 2 2 2 3 0 0 0 Output for the Sample Input 15 2014 0 25 40 390
38,902
Problem G: Telescope Dr. Extreme experimentally made an extremely precise telescope to investigate extremely curi- ous phenomena at an extremely distant place. In order to make the telescope so precise as to investigate phenomena at such an extremely distant place, even quite a small distortion is not allowed. However, he forgot the influence of the internal gas affected by low-frequency vibration of magnetic flux passing through the telescope. The cylinder of the telescope is not affected by the low-frequency vibration, but the internal gas is. The cross section of the telescope forms a perfect circle. If he forms a coil by putting extremely thin wire along the (inner) circumference, he can measure (the average vertical component of) the temporal variation of magnetic flux:such measurement would be useful to estimate the influence. But points on the circumference at which the wire can be fixed are limited; furthermore, the number of special clips to fix the wire is also limited. To obtain the highest sensitivity, he wishes to form a coil of a polygon shape with the largest area by stringing the wire among carefully selected points on the circumference. Your job is to write a program which reports the maximum area of all possible m -polygons (polygons with exactly m vertices) each of whose vertices is one of the n points given on a circumference with a radius of 1. An example of the case n = 4 and m = 3 is illustrated below. In the figure above, the equations such as " p 1 = 0.0" indicate the locations of the n given points, and the decimals such as "1.000000" on m -polygons indicate the areas of m -polygons. Parameter p i denotes the location of the i -th given point on the circumference (1 ≀ i ≀ n ). The location p of a point on the circumference is in the range 0 ≀ p < 1, corresponding to the range of rotation angles from 0 to 2 π radians. That is, the rotation angle of a point at p to the point at 0 equals 2 π radians. ( π is the circular constant 3.14159265358979323846....) You may rely on the fact that the area of an isosceles triangle ABC (AB = AC = 1) with an interior angle BAC of α radians (0 < α < π ) is (1/2)sin α , and the area of a polygon inside a circle with a radius of 1 is less than π . Input The input consists of multiple subproblems followed by a line containing two zeros that indicates the end of the input. Each subproblem is given in the following format. n m p 1 p 2 ... p n n is the number of points on the circumference (3 ≀ n ≀ 40). m is the number of vertices to form m -polygons (3 ≀ m ≀ n ). The locations of n points, p 1 , p 2 ,..., p n , are given as decimals and they are separated by either a space character or a newline. In addition, you may assume that 0 ≀ p 1 < p 2 < ... < p n < 1. Output For each subproblem, the maximum area should be output, each in a separate line. Each value in the output may not have an error greater than 0.000001 and its fractional part should be represented by 6 decimal digits after the decimal point. Sample Input 4 3 0.0 0.25 0.5 0.666666666666666666667 4 4 0.0 0.25 0.5 0.75 30 15 0.00 0.03 0.06 0.09 0.12 0.15 0.18 0.21 0.24 0.27 0.30 0.33 0.36 0.39 0.42 0.45 0.48 0.51 0.54 0.57 0.61 0.64 0.66 0.69 0.72 0.75 0.78 0.81 0.84 0.87 40 20 0.351 0.353 0.355 0.357 0.359 0.361 0.363 0.365 0.367 0.369 0.371 0.373 0.375 0.377 0.379 0.381 0.383 0.385 0.387 0.389 0.611 0.613 0.615 0.617 0.619 0.621 0.623 0.625 0.627 0.629 0.631 0.633 0.635 0.637 0.639 0.641 0.643 0.645 0.647 0.649 0 0 Output for the Sample Input 1.183013 2.000000 3.026998 0.253581
38,903
7 セグメント 電卓などでよく目にするデゞタル数字を衚瀺しおいる画面は、デゞタル数字が 7 ぀の郚分(セグメン ト)で構成されるこずから、「7セグメントディスプレむ」ず呌ばれおいたす。 ワカマツ瀟で新しく売り出す予定の補品は、 7セグメントディスプレむを補品に組み蟌むこずになり、瀟員であるあなたは、䞎えられた数字を 7 セグメントディスプレむに衚瀺するプログラムを䜜成するこずになりたした。 この7セグメントディスプレむは、次の切り替えの指瀺が送られおくるたで衚瀺状態は倉わりたせん。7 ビットからなるシグナルを送るこずで、それぞれ察応したセグメントの衚瀺情報を切り替える事ができたす。ビットずは 1 か 0 の倀を持぀もので、ここでは 1 が「切り替え」、0 が「そのたた」を衚したす。 各ビットずセグメントの察応関係は䞋の図のようになっおいたす。シグナルは 7 ぀のビットを"gfedcba"の順番に送りたす。䟋えば、非衚瀺の状態から「0」を衚瀺するためには"0111111"をシグナルずしおディスプレむに送らなければなりたせん。「0」から「5」に倉曎する堎合には"1010010"を送りたす。続けお「5」を「1」に倉曎する堎合には"1101011"を送りたす。 衚瀺したい n (1 ≀ n ≀ 100) 個の数字を入力ずし、それらの数字 d i (0 ≀ d i ≀ 9) を順に 7 セグメントディスプレむに正しく衚瀺するために必芁なシグナル列を出力するプログラムを䜜成しおください。なお、7 セグメントディスプレむの初期状態は党お非衚瀺の状態であるものずしたす。 Input 耇数のデヌタセットの䞊びが入力ずしお䞎えられたす。入力の終わりは-1 ひず぀の行で瀺されたす。 各デヌタセットは以䞋の圢匏で䞎えられたす。 n d 1 d 2 : d n デヌタセットの数は 120 を超えたせん。 Output 入力デヌタセットごずに、数字をディスプレむに正しく出力するために必芁なシグナルの列を出力しおください。 Sample Input 3 0 5 1 1 0 -1 Output for the Sample Input 0111111 1010010 1101011 0111111
38,904
D: Xor Array 問題文 敎数 $N$ ず $X$ が䞎えられたす。 以䞋の条件を満たす長さ $N$ の数列の個数を $998244353$ で割った䜙りを求めおください。 数列は広矩単調増加である。 数列の各芁玠は $0$ 以䞊 $X$ 以䞋である。 党おの芁玠の排他的論理和(xor)が $X$ である。 制玄 $1 \leq N \leq 500$ $0 \leq X \leq 500$ $N$ ず $X$ は敎数である。 入力 入力は以䞋の圢匏で暙準入力から䞎えられる。 $N$ $X$ 出力 答えを出力せよ。 入力䟋1 2 3 出力䟋1 2 数列 $\{0,3\}$ ず $\{1,2\}$ が条件を満たしたす。 入力䟋2 1 1 出力䟋2 1 数列 $\{1\}$ のみが条件を満たしたす。 入力䟋3 224 239 出力䟋3 400351036
38,905
Score : 100 points Problem Statement This contest is CODEFESTIVAL , which can be shortened to the string CF by deleting some characters. Mr. Takahashi, full of curiosity, wondered if he could obtain CF from other strings in the same way. You are given a string s consisting of uppercase English letters. Determine whether the string CF can be obtained from the string s by deleting some characters. Constraints 2 ≀ |s| ≀ 100 All characters in s are uppercase English letters ( A - Z ). Input The input is given from Standard Input in the following format: s Output Print Yes if the string CF can be obtained from the string s by deleting some characters. Otherwise print No . Sample Input 1 CODEFESTIVAL Sample Output 1 Yes CF is obtained by deleting characters other than the first character C and the fifth character F . Sample Input 2 FESTIVALCODE Sample Output 2 No FC can be obtained but CF cannot be obtained because you cannot change the order of the characters. Sample Input 3 CF Sample Output 3 Yes It is also possible not to delete any characters. Sample Input 4 FCF Sample Output 4 Yes CF is obtained by deleting the first character.
38,906
KND Runs for Sweets Problem KND君は䌚接倧孊に圚籍する孊生プログラマである。圌は甘党であるこずで知られおいる。圌はずある垂に1幎間滞圚するこずになり、その期間䞭に垂内にある N 箇所の甘味凊をすべお回りたいず思っおいる。なのでこの1幎間の䜏む堎所は甘味凊を回るのに䞀番適した堎所がいいず考えおいる。圌の隣人であるあなたは、各甘味凊ぞの最悪の移動時間が最小になる堎所を探す事になった。この甘味凊がある垂は簡単のため二次元平面で衚すこずにする。圌は目的の甘味凊ぞのモチベヌションの違いにより、単䜍時間あたりの移動距離が倉化する。たた、圌はどんな堎所にでもたずえ甘味凊ず同じ堎所であろうず䜏む぀もりでいる。KND君は甘味に関しおは劥協を蚱さないのである。 Input 入力は耇数のテストケヌスからなる。 ひず぀のテストケヌスは以䞋のな圢匏で䞎えられる。 入力の終了を N = 0 のずき瀺す。 N x 1 y 1 v 1 x 2 y 2 v 2 ... x N y N v N ここで、 N :甘味凊の数 x i :i番目の甘味凊のx座暙 y i :i番目の甘味凊のy座暙 v i :i番目の甘味凊ぞ移動するずきの単䜍時間あたりの移動距離 である。 Constraints 入力は以䞋の条件を満たす。 入力はすべお敎数。 2 ≀ N ≀ 100 0 ≀ x i , y i ≀ 100 1 ≀ v i ≀ 100 ( 1 ≀ i ≀ N ) x i ≠ x j or y i ≠ y j ( ただし1 ≀ i < j ≀ N ) Output 各テストケヌスに぀き最悪の移動時間の最小倀を䞀行に出力せよ。この倀はゞャッゞ出力の倀ず10 -5 より倧きい差を持っおはならない。 Sample Input 2 1 1 1 2 2 1 4 1 1 3 3 1 3 4 2 1 1 5 3 0 Sample Output 0.70710678 1.06066017
38,907
Score : 100 points Problem Statement When you asked some guy in your class his name, he called himself S , where S is a string of length between 3 and 20 (inclusive) consisting of lowercase English letters. You have decided to choose some three consecutive characters from S and make it his nickname. Print a string that is a valid nickname for him. Constraints 3 \leq |S| \leq 20 S consists of lowercase English letters. Input Input is given from Standard Input in the following format: S Output Print your answer. Sample Input 1 takahashi Sample Output 1 tak Sample Input 2 naohiro Sample Output 2 nao
38,908
Taxi PCK Taxi in Aizu city, owned by PCK company, has adopted a unique billing system: the user can decide the taxi fare. Today as usual, many people are waiting in a queue at the taxi stand in front of the station. In front of the station, there are $N$ parking spaces in row for PCK taxis, each with an index running from $1$ to $N$. Each of the parking areas is occupied by a taxi, and a queue of potential passengers is waiting for the ride. Each one in the queue has his/her own plan for how much to pay for the ride. To increase the company’s gain, the taxi driver is given the right to select the passenger who offers the highest taxi fare, rejecting others. The driver in the $i$-th parking space can perform the following actions any number of times in any sequence before he finally selects a passenger and starts driving. Offer a ride to the passenger who is at the head of the $i$-th parking space’s queue. Reject to offer a ride to the passenger who is at the head of the $i$-th parking space’s queue. The passenger is removed from the queue. Move to the $i + 1$-th parking area if it is empty. If he is in the $N$-th parking area, he leaves the taxi stand to cruise the open road. A preliminary listening is made as to the fare the users offer. Your task is to maximize the sales volume of PCK Taxi in reference to the table of offered fares. A taxi cannot accommodate more than one passenger. Given the number of taxi parking spaces and information regarding the persons waiting in the parking areas, calculate the maximum possible volume of sales. Input The input is given in the following format. $N$ $s_1$ $s_2$ $...$ $s_N$ The first line provides the number of taxi parking areas $N$ ($1 \leq N \leq 300,000$). Each of the subsequent $N$ lines provides information on the customers queueing in the $i$-th taxi parking area in the following format: $M$ $c_1$ $c_2$ ... $c_M$ The first integer $M$ ($1 \leq M \leq 300,000$) indicates the number of customers in the queue, and the subsequent array of integers $c_j$ ($1 \leq c_j \leq 10,000$) indicates the fare the $j$-th customer in the queue is willing to pay. The total number of customers in the taxi stand is equal to or less than $300,000$. Output Output the maximum volume of sales. Sample Input 3 3 8 10 1 4 7 1 2 15 3 11 8 19 Sample Output 45
38,909
Problem C: Chinese Classics Taro, a junior high school student, is working on his homework. Today's homework is to read Chinese classic texts. As you know, Japanese language shares the (mostly) same Chinese characters but the order of words is a bit different. Therefore the notation called "returning marks" was invented in order to read Chinese classic texts in the order similar to Japanese language. There are two major types of returning marks: 'Re' mark and jump marks. Also there are a couple of jump marks such as one-two-three marks, top-middle-bottom marks. The marks are attached to letters to describe the reading order of each letter in the Chinese classic text. Figure 1 is an example of a Chinese classic text annotated with returning marks, which are the small letters at the bottom-left of the big Chinese letters. Figure 1: a Chinese classic text Taro generalized the concept of jump marks, and summarized the rules to read Chinese classic texts with returning marks as below. Your task is to help Taro by writing a program that interprets Chinese classic texts with returning marks following his rules, and outputs the order of reading of each letter. When two (or more) rules are applicable in each step, the latter in the list below is applied first, then the former. Basically letters are read downwards from top to bottom, i.e. the first letter should be read (or skipped) first, and after the i -th letter is read or skipped, ( i + 1)-th letter is read next. Each jump mark has a type (represented with a string consisting of lower-case letters) and a number (represented with a positive integer). A letter with a jump mark whose number is 2 or larger must be skipped. When the i -th letter with a jump mark of type t , number n is read, and when there exists an unread letter L at position less than i that has a jump mark of type t , number n + 1, then L must be read next. If there is no such letter L , the ( k + 1)-th letter is read, where k is the index of the most recently read letter with a jump mark of type t , number 1. A letter with a 'Re' mark must be skipped. When the i -th letter is read and ( i - 1)-th letter has a 'Re' mark, then ( i - 1)-th letter must be read next. No letter may be read twice or more. Once a letter is read, the letter must be skipped in the subsequent steps. If no letter can be read next, finish reading. Let's see the first case of the sample input. We begin reading with the first letter because of the rule 1. However, since the first letter has a jump mark ' onetwo2 ', we must follow the rule 2 and skip the letter. Therefore the second letter, which has no returning mark, will be read first. Then the third letter will be read. The third letter has a jump mark ' onetwo1 ', so we must follow rule 3 and read a letter with a jump mark `onetwo2' next, if exists. The first letter has the exact jump mark, so it will be read third. Similarly, the fifth letter is read fourth, and then the sixth letter is read. Although we have two letters which have the same jump mark ' onetwo2 ', we must not take into account the first letter, which has already been read, and must read the fourth letter. Now we have read all six letters and no letter can be read next, so we finish reading. We have read the second, third, first, fifth, sixth, and fourth letter in this order, so the output is 2 3 1 5 6 4. Input The input contains multiple datasets. Each dataset is given in the following format: N mark 1 ... mark N N , a positive integer (1 ≀ N ≀ 10,000), means the number of letters in a Chinese classic text. mark i denotes returning marks attached to the i -th letter. A 'Re' mark is represented by a single letter, namely, 'v' (quotes for clarity). The description of a jump mark is the simple concatenation of its type, specified by one or more lowercase letter, and a positive integer. Note that each letter has at most one jump mark and at most one 'Re' mark. When the same letter has both types of returning marks, the description of the jump mark comes first, followed by 'v' for the 'Re' mark. You can assume this happens only on the jump marks with the number 1. If the i -th letter has no returning mark, mark i is '-' (quotes for clarity). The length of mark i never exceeds 20. You may assume that input is well-formed, that is, there is exactly one reading order that follows the rules above. And in the ordering, every letter is read exactly once. You may also assume that the N -th letter does not have 'Re' mark. The input ends when N = 0. Your program must not output anything for this case. Output For each dataset, you should output N lines. The first line should contain the index of the letter which is to be read first, the second line for the letter which is to be read second, and so on. All the indices are 1-based. Sample Input 6 onetwo2 - onetwo1 onetwo2 - onetwo1 7 v topbottom2 onetwo2 - onetwo1 topbottom1 - 6 baz2 foo2 baz1v bar2 foo1 bar1 0 Output for the Sample Input 2 3 1 5 6 4 4 5 3 6 2 1 7 5 2 6 4 3 1
38,911
Score : 600 points Problem Statement We have a rectangular grid of squares 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. On this grid, there is a piece, which is initially placed at square (s_r,s_c) . Takahashi and Aoki will play a game, where each player has a string of length N . Takahashi's string is S , and Aoki's string is T . S and T both consist of four kinds of letters: L , R , U and D . The game consists of N steps. The i -th step proceeds as follows: First, Takahashi performs a move. He either moves the piece in the direction of S_i , or does not move the piece. Second, Aoki performs a move. He either moves the piece in the direction of T_i , or does not move the piece. Here, to move the piece in the direction of L , R , U and D , is to move the piece from square (r,c) to square (r,c-1) , (r,c+1) , (r-1,c) and (r+1,c) , respectively. If the destination square does not exist, the piece is removed from the grid, and the game ends, even if less than N steps are done. Takahashi wants to remove the piece from the grid in one of the N steps. Aoki, on the other hand, wants to finish the N steps with the piece remaining on the grid. Determine if the piece will remain on the grid at the end of the game when both players play optimally. Constraints 2 \leq H,W \leq 2 \times 10^5 2 \leq N \leq 2 \times 10^5 1 \leq s_r \leq H 1 \leq s_c \leq W |S|=|T|=N S and T consists of the four kinds of letters L , R , U and D . Input Input is given from Standard Input in the following format: H W N s_r s_c S T Output If the piece will remain on the grid at the end of the game, print YES ; otherwise, print NO . Sample Input 1 2 3 3 2 2 RRL LUD Sample Output 1 YES Here is one possible progress of the game: Takahashi moves the piece right. The piece is now at (2,3) . Aoki moves the piece left. The piece is now at (2,2) . Takahashi does not move the piece. The piece remains at (2,2) . Aoki moves the piece up. The piece is now at (1,2) . Takahashi moves the piece left. The piece is now at (1,1) . Aoki does not move the piece. The piece remains at (1,1) . Sample Input 2 4 3 5 2 2 UDRRR LLDUD Sample Output 2 NO Sample Input 3 5 6 11 2 1 RLDRRUDDLRL URRDRLLDLRD Sample Output 3 NO
38,912
Sum of Sequences Problem n 個の芁玠を持぀数列 A 、 m 個の芁玠を持぀数列 B 、 それぞれが敎数 c からなる q 個のク゚リが䞎えられる。 各ク゚リに぀いお、 数列 A の[ l a , r a ]を党お足した数ず数列 B の[ l b , r b ]を党お足した数の差の絶察倀が c になる l a , r a , l b , r b (0 ≀ l a ≀ r a ≀ n −1, 0 ≀ l b ≀ r b ≀ m −1, 配列の番号は0番から始たる) の組み合わせの数を求めよ。 Input n m q a 0 a 1 ... a n−1 b 0 b 1 ... b m−1 c 0 c 1 ... c q−1 入力は党お敎数で䞎えられる。 1行目に数列の芁玠数 n ず m 、ク゚リ数 q が䞎えられる。 2行目に数列 A の芁玠、3行目に数列 B の芁玠が空癜区切りで䞎えられる。 4行目から q 行に各ク゚リの倀 c i が䞎えられる。 Constraints 1 ≀ n , m ≀ 4×10 4 1 ≀ q ≀ 10 5 1 ≀ a i , b i ≀ 5 0 ≀ c i ≀ 2×10 5 Output 出力は q 行からなる。各ク゚リの組み合わせの数を順番に䞀行に出力せよ。 Sample Input 1 3 3 1 1 2 3 3 1 2 3 Sample Output 1 6 Sample Input 2 5 4 2 1 2 3 4 5 2 2 2 2 11 12 Sample Output 2 3 4
38,913
Problem C: Earn Big A group of N people is trying to challenge the following game to earn big money. First, N participants are isolated from each other. From this point, they are not allowed to contact each other, or to leave any information for other participants. The game organizer leads each participant, one by one, to a room with N boxes. The boxes are all closed at the beginning of the game, and the game organizer closes all the boxes whenever a participant enters the room. Each box contains a slip of paper on which a name of a distinct participant is written. The order of the boxes do not change during the game. Each participant is allowed to open up to M boxes. If every participant is able to open a box that contains a paper of his/her name, then the group wins the game, and everybody in the group earns big money. If anyone is failed to open a box that contains a paper of his/her name, then the group fails in the game, and nobody in the group gets money. Obviously, if every participant picks up boxes randomly, the winning probability will be (M/N) N . However, there is a far more better solution. Before discussing the solution, let us define some concepts. Let P = {p 1 , p 2 , ..., p N } be a set of the participants, and B = {b 1 , b 2 , ..., b N } be a set of the boxes. Let us define f , a mapping from B to P , such that f(b) is a participant whose name is written on a paper in a box b . Here, consider a participant p i picks up the boxes in the following manner: Let x := i . If p i has already opened M boxes, then exit as a failure. p i opens b x . If f(b x ) = p i , then exit as a success. If f(b x ) = p j ( i != j ), then let x := j , and go to 2. Assuming every participant follows the algorithm above, the result of the game depends only on the initial order of the boxes (i.e. the definition of f ). Let us define g to be a mapping from P to B , such that g(p i ) = b i . The participants win the game if and only if, for every i ∈ {1, 2, ..., N} , there exists k(<=M) such that (f g) k (p i ) = p i . Your task is to write a program that calculates the winning probability of this game. You can assume that the boxes are placed randomly. Input The input consists of one line. It contains two integers N and M (1 <= M <= N <= 1,000) in this order, delimited by a space. Output For given N and M , your program should print the winning probability of the game. The output value should be in a decimal fraction and should not contain an error greater than 10 -8 . Sample Input 1 2 1 Output for the Sample Input 1 0.50000000 Sample Input 2 100 50 Output for the Sample Input 2 0.31182782
38,914
Short Phrase A Short Phrase (aka. Tanku) is a fixed verse, inspired by Japanese poetry Tanka and Haiku. It is a sequence of words, each consisting of lowercase letters 'a' to 'z', and must satisfy the following condition: (The Condition for a Short Phrase) The sequence of words can be divided into five sections such that the total number of the letters in the word(s) of the first section is five, that of the second is seven, and those of the rest are five, seven, and seven, respectively. The following is an example of a Short Phrase. do the best and enjoy today at acm icpc In this example, the sequence of the nine words can be divided into five sections (1) "do" and "the", (2) "best" and "and", (3) "enjoy", (4) "today" and "at", and (5) "acm" and "icpc" such that they have 5, 7, 5, 7, and 7 letters in this order, respectively. This surely satisfies the condition of a Short Phrase. Now, Short Phrase Parnassus published by your company has received a lot of contributions. By an unfortunate accident, however, some irrelevant texts seem to be added at beginnings and ends of contributed Short Phrases. Your mission is to write a program that finds the Short Phrase from a sequence of words that may have an irrelevant prefix and/or a suffix. Input The input consists of multiple datasets, each in the following format. n w 1 ... w n Here, n is the number of words, which is a positive integer not exceeding 40; w i is the i -th word, consisting solely of lowercase letters from 'a' to 'z'. The length of each word is between 1 and 10, inclusive. You can assume that every dataset includes a Short Phrase. The end of the input is indicated by a line with a single zero. Output For each dataset, output a single line containing i where the first word of the Short Phrase is w i . When multiple Short Phrases occur in the dataset, you should output the first one. Sample Input 9 do the best and enjoy today at acm icpc 14 oh yes by far it is wow so bad to me you know hey 15 abcde fghijkl mnopq rstuvwx yzz abcde fghijkl mnopq rstuvwx yz abcde fghijkl mnopq rstuvwx yz 0 Output for the Sample Input 1 2 6
38,915
Score : 300 points Problem Statement You have N bamboos. The lengths (in centimeters) of these are l_1, l_2, ..., l_N , respectively. Your objective is to use some of these bamboos (possibly all) to obtain three bamboos of length A, B, C . For that, you can use the following three kinds of magics any number: Extension Magic: Consumes 1 MP (magic point). Choose one bamboo and increase its length by 1 . Shortening Magic: Consumes 1 MP. Choose one bamboo of length at least 2 and decrease its length by 1 . Composition Magic: Consumes 10 MP. Choose two bamboos and combine them into one bamboo. The length of this new bamboo is equal to the sum of the lengths of the two bamboos combined. (Afterwards, further magics can be used on this bamboo.) At least how much MP is needed to achieve the objective? Constraints 3 \leq N \leq 8 1 \leq C < B < A \leq 1000 1 \leq l_i \leq 1000 All values in input are integers. Input Input is given from Standard Input in the following format: N A B C l_1 l_2 : l_N Output Print the minimum amount of MP needed to achieve the objective. Sample Input 1 5 100 90 80 98 40 30 21 80 Sample Output 1 23 We are obtaining three bamboos of lengths 100, 90, 80 from five bamboos 98, 40, 30, 21, 80 . We already have a bamboo of length 80 , and we can obtain bamboos of lengths 100, 90 by using the magics as follows at the total cost of 23 MP, which is optimal. Use Extension Magic twice on the bamboo of length 98 to obtain a bamboo of length 100 . (MP consumed: 2 ) Use Composition Magic on the bamboos of lengths 40, 30 to obtain a bamboo of length 70 . (MP consumed: 10 ) Use Shortening Magic once on the bamboo of length 21 to obtain a bamboo of length 20 . (MP consumed: 1 ) Use Composition Magic on the bamboo of length 70 obtained in step 2 and the bamboo of length 20 obtained in step 3 to obtain a bamboo of length 90 . (MP consumed: 10 ) Sample Input 2 8 100 90 80 100 100 90 90 90 80 80 80 Sample Output 2 0 If we already have all bamboos of the desired lengths, the amount of MP needed is 0 . As seen here, we do not necessarily need to use all the bamboos. Sample Input 3 8 1000 800 100 300 333 400 444 500 555 600 666 Sample Output 3 243
38,917
分割統治 倪郎さんず花子さんず次郎さんは3人で JAG 王囜を統治しおいるJAG 王囜には N 個の街が存圚しいく぀かの街は双方向の道路で繋がっおいるどの街からも別のすべおの街ぞ 1 本以䞊の道路を経由しお必ず蟿り着くこずができる ある日倪郎さんず花子さんはずうずう仲違いを起こしおしたい3 人で街を分担しお統治するこずに決めたしかしあたりにも仲が悪くなりすぎおしたったため倪郎さんが統治しおいる街ず花子さんが統治しおいる街が 1 本の道路で盎接繋がっおいるこずすら嫌がっおいるそこで以䞋の条件を満たすように統治する街を分担するこずにした 倪郎さんが統治する街ず花子さんが統治する街からなる任意のペアは盎接道路で繋がっおいないこれは倪郎さんず花子さんの仲がめちゃくちゃ悪いからである 同じ人に統治されおいる街同士も盎接道路で繋がっおいないこれはそんな䞭でも他者の統治䞋の経由を矩務付けるこずで倖亀を促すためである 倪郎さんの統治する街の総数ず花子さんが統治する街の総数は同じでなければならないこれは総数を等しくしないず倪郎さんず花子さんの仲がさらにめちゃくちゃ悪くなりそうだからであるここで次郎さんはずおも心が広いので次郎さんが統治する街の総数はいく぀でもよい 以䞊の条件を満たすような分担であれば3 人は玍埗しお統治するこずができたずえ誰かの統治する街が 0 個であっおも文句はないこのずき倪郎さんが統治する街の総数 (=花子さんが統治する街の総数) ずしおあり埗る数をすべお列挙するプログラムを䜜成せよ Input 入力は耇数のデヌタセットからなる デヌタセットの数は最倧で 50 である 各デヌタセットは次の圢匏で衚される N M u 1 v 1 ... u M v M 1 行目は 2 ぀の敎数 N ( 2 ≀ N ≀ 10 3 ) ず M ( 1 ≀ M ≀ 10 3 ) からなりそれぞれ街の数ず道路の数を衚す続く M 行のうち i 行目は 2 ぀の敎数 u i ず v i ( 1 ≀ u i < v i ≀ N ) からなり i 番目の道路が街 u i ず 街 v i を双方向に繋いでいるこずを衚すここでどの街からも別のすべおの街ぞ 1 本以䞊の道路を経由しお必ず蟿り着くこずができるこずが保蚌されるたた同じ街のペアを結ぶ道路が耇数䞎えられるこずはないすなわちすべおの 1 ≀ i < j ≀ M に぀いお (u i , v i ) ≠ (u j , v j ) を満たす 入力の終わりは 2 ぀のれロからなる行で衚される Output 各デヌタセットに぀いお倪郎さんが統治する街の総数ずしお考えられる数が K 通りあるずきたず 1 行目に K を出力しその埌あり埗る総数を 1 行に 1 ぀ず぀昇順で出力せよ Sample Input 6 7 1 2 1 4 2 3 2 5 3 4 4 5 4 6 2 1 1 2 3 3 1 2 1 3 2 3 4 3 1 2 2 3 3 4 5 4 1 2 2 3 3 4 4 5 0 0 Output for the Sample Input 2 1 2 0 0 1 1 1 1
38,918
Jigsaw Puzzles for Computers Ordinary Jigsaw puzzles are solved with visual hints; players solve a puzzle with the picture which the puzzle shows on finish, and the diverse patterns of pieces. Such Jigsaw puzzles may be suitable for human players, because they require abilities of pattern recognition and imagination. On the other hand, "Jigsaw puzzles" described below may be just the things for simple-minded computers. As shown in Figure 1 , a puzzle is composed of nine square pieces, and each of the four edges of a piece is labeled with one of the following eight symbols: "R", "G", "B", "W", "r", "g", "b" , and "w". Figure 1 : The nine pieces of a puzzle In a completed puzzle, the nine pieces are arranged in a 3 x 3 grid, and each of the 12 pairs of edges facing each other must be labeled with one of the following four combinations of symbols: "R" with "r" , "G" with "g" , "B" with "b" , and "W" with "w" . For example, an edge labeled "R" can only face an edge with "r" . Figure 2 is an example of a completed state of a puzzle. In the figure, edges under this restriction are indicated by shadowing their labels. The player can freely move and rotate the pieces, but cannot turn them over. There are no symbols on the reverse side of a piece ! Figure 2 : A completed puzzle example Each piece is represented by a sequence of the four symbols on the piece, starting with the symbol of the top edge, followed by the symbols of the right edge, the bottom edge, and the left edge. For example, gwgW represents the leftmost piece in Figure 1 . Note that the same piece can be represented as wgWg , gWgw or Wgwg since you can rotate it in 90, 180 or 270 degrees. The mission for you is to create a program which counts the number of solutions. It is needless to say that these numbers must be multiples of four, because, as shown in Figure 3 , a configuration created by rotating a solution in 90, 180 or 270 degrees is also a solution. Figure 3 : Four obvious variations for a completed puzzle A term "rotationally equal" is defined; if two different pieces are identical when one piece is rotated (in 90, 180 or 270 degrees), they are rotationally equal. For example, WgWr and WrWg are rotationally equal. Another term "rotationally symmetric" is defined; if a piece is rotationally equal to itself, it is rotationally symmetric. For example, a piece gWgW is rotationally symmetric. Given puzzles satisfy the following three conditions: There is no identical pair of pieces in a puzzle. There is no rotationally equal pair of pieces in a puzzle. There is no rotationally symmetric piece in a puzzle. Input The input consists of multiple puzzles. N Puzzle 1 Puzzle 2 . . . Puzzle N N is the number of puzzles. Each Puzzle i gives a puzzle with a single line of 44 characters, consisting of four-character representations of the nine pieces of the puzzle, separated by a space character. For example, the following line represents the puzzle in Figure 1 . gwgW RBbW GWrb GRRb BWGr Rbgw rGbR gBrg GRwb Output For each Puzzle i , the number of its solutions should be the output, each in a separate line. Sample Input 6 WwRR wwrg RRGb rGBG RGrb RrRg RGrg rgBB Wrgr RrGb WWGR rGgb Wbrg wgBb GgBg WbBG Wwwg WWGG RBbr Wrbr wGGG wggR WgGR WBWb WRgB wBgG WBgG wBrg rGgb WRrB WWbw wRRB RbbB WRrb wrbb WgrG WrwB WWww wRRB WGGb Wbbg WBgG WrbG Wrww RBgg WWgg RBrr Rggr RGBg Wbgr WGbg WBbr WGWB GGGg Output for the Sample Input: 40 8 32 4 12 0
38,919
RMQ and RUQ Write a program which manipulates a sequence A = { a 0 , a 1 , . . . , a n−1 } with the following operations: update(s, t, x) : change a s , a s+1 , ..., a t to x . find(s, t) : report the minimum element in a s , a s+1 , ..., a t . Note that the initial values of a i ( i = 0, 1, . . . , n−1 ) are 2 31 -1. Input n q query 1 query 2 : query q In the first line, n (the number of elements in A ) and q (the number of queries) are given. Then, i th query query i is given in the following format: 0 s t x or 1 s t The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(s, t) . Output For each find operation, print the minimum value. Constraints 1 ≀ n ≀ 100000 1 ≀ q ≀ 100000 0 ≀ s ≀ t < n 0 ≀ x < 2 31 −1 Sample Input 1 3 5 0 0 1 1 0 1 2 3 0 2 2 2 1 0 2 1 1 2 Sample Output 1 1 2 Sample Input 2 1 3 1 0 0 0 0 0 5 1 0 0 Sample Output 2 2147483647 5
38,920
Score : 1600 points Problem Statement There is a tree with N vertices, numbered 1 through N . The i -th of the N-1 edges connects vertices a_i and b_i . Currently, there are A_i stones placed on vertex i . Takahashi and Aoki will play a game using this tree. First, Takahashi will select a vertex and place a piece on it. Then, starting from Takahashi, they will alternately perform the following operation: Remove one stone from the vertex currently occupied by the piece. Then, move the piece to a vertex that is adjacent to the currently occupied vertex. The player who is left with no stone on the vertex occupied by the piece and thus cannot perform the operation, loses the game. Find all the vertices v such that Takahashi can place the piece on v at the beginning and win the game. Constraints 2 ≩ N ≩ 3000 1 ≩ a_i,b_i ≩ N 0 ≩ A_i ≩ 10^9 The given graph is a tree. Input The input is given from Standard Input in the following format: N A_1 A_2 
 A_N a_1 b_1 : a_{N-1} b_{N-1} Output Print the indices of the vertices v such that Takahashi can place the piece on v at the beginning and win the game, in a line, in ascending order. Sample Input 1 3 1 2 3 1 2 2 3 Sample Output 1 2 The following is one possible progress of the game when Takahashi places the piece on vertex 2 : Takahashi moves the piece to vertex 1 . The number of the stones on each vertex is now: (1,1,3) . Aoki moves the piece to vertex 2 . The number of the stones on each vertex is now: (0,1,3) . Takahashi moves the piece to vertex 1 . The number of the stones on each vertex is now: (0,0,3) . Aoki cannot take a stone from the vertex, and thus Takahashi wins. Sample Input 2 5 5 4 1 2 3 1 2 1 3 2 4 2 5 Sample Output 2 1 2 Sample Input 3 3 1 1 1 1 2 2 3 Sample Output 3 Note that the correct output may be an empty line.
38,921