task
stringlengths
0
154k
__index_level_0__
int64
0
39.2k
あみだくじ 問題 あなたは J 君ず䞀緒にあみだくじを䜿っお遊んでいる.あみだくじは n 本の瞊棒ず m 本の暪棒からなる.瞊棒には巊から順に 1 から n の番号が぀いおおり,瞊棒 i の䞋端には正敎数 s i が曞かれおいる. 図 3-1 あみだくじの䟋( n = 4, m = 5, s 1 = 20, s 2 = 80, s 3 = 100, s 4 = 50) 瞊棒 i の䞀番䞊から順に道をたどっおいき到達した䞋端に曞かれおいる敎数が, 瞊 棒 i を遞んだ堎合の埗点である.䟋えば,図 3-1 では,瞊棒 1 を遞ぶず埗点は 80 点 であり,瞊棒 2 を遞ぶず埗点は 100 点である. 図 3-2 道のたどり方の䟋 J 君は瞊棒 1 から瞊棒 k たでの連続した k 本を遞ぶこずにした.それら k 本の瞊棒を遞んだずきの点数の合蚈が J 君の埗点ずなる.ただし,あなたはあみだくじ内の 暪棒を䞀本遞び,その暪棒をあみだくじから削陀するこずができる. (削陀しなくおもよい.) もし,あなたが暪棒を䞀本削陀した堎合は,削陀埌のあみだくじにおいお, 瞊棒 1 から瞊棒 k たでの連続した k 本の瞊棒を遞んだずきの点数の合蚈が J 君の埗点ずなる. 入力ずしおあみだくじの圢ず J 君の遞ぶ瞊棒の本数 k が䞎えられたずき,J 君の埗 点の最小倀を求めるプログラムを䜜成せよ. 入力 入力は耇数のデヌタセットからなる各デヌタセットは以䞋の圢匏で䞎えられる 1 行目には 4 ぀の敎数 n , m , h , k が空癜区切りで曞かれおいる. n (2 ≀ n ≀ 1000) は瞊棒の本数を, m (1 ≀ m ≀ 100000) は暪棒の本数を, h (2 ≀ h ≀ 1000) は瞊棒の長さを, k (1 ≀ k ≀ n) は J 君が遞ぶ瞊棒の本数を衚す. 続く n 行には瞊棒の䞋端に曞かれおいる点数が曞かれおいる. i + 1 行目 (1 ≀ i ≀ n)には正敎数 s i が曞かれおいる. たた, s 1 + s 2 + ... + s n ≀ 2000000000 = 2 × 10 9 を満たす. 続く m 行には暪棒の䜍眮が曞かれおいる.暪棒には 1 から m たでの番号が぀いおいる. i + n + 1 行目 (1 ≀ i ≀ m ) には, 暪棒 i の䜍眮を衚す 2 ぀の敎数 a i , b i (1 ≀ a i ≀ n - 1, 1 ≀ b i ≀ h - 1) が空癜区切りで曞かれおおり,暪棒 i が瞊棒 a i ず瞊棒 a i + 1 を結び, 暪棒 i の䞊端からの距離が b i であるこずを衚す. ただし,どの 2 ぀の暪棒も端点を共有するこずはない. 採点甚デヌタのうち,配点の 20 % 分は暪棒を削陀しない堎合に J 君の埗点が最少ずなる.たた,配点の 30 % 分は n ≀ 20, m ≀ 30, h ≀ 10 を満たし,配点の 60 % 分は m ≀ 1000 を満たす. 入力の終わりは 4぀のれロを含む行で瀺される. デヌタセットの数は 10 を超えない 出力 デヌタセットごずに, J 君の埗点の最小倀を1 行に出力する. 入出力䟋 入力䟋 4 5 7 2 20 80 100 50 1 1 2 6 2 3 1 5 3 1 2 2 5 1 10 20 1 1 1 3 0 0 0 0 出力䟋 100 10 1぀目の䟋 は図 3-1 に察応し,あなたが暪棒 4 (瞊棒 1 ず瞊棒 2 を䞊端から距離 5 の堎所で結ぶ暪棒)を削陀したずき,J 君の埗点は最小になる.䟋 2 では,あなたが暪棒を削陀しない堎合に J 君の埗点が最小になる. (図3-3 を芋よ.) 図 3-3 䞊蚘問題文ず自動審刀に䜿われるデヌタは、 情報オリンピック日本委員䌚 が䜜成し公開しおいる問題文ず採点甚テストデヌタです。
39,132
難儀な人たちが座る怅子 4぀の囜の䞭心にある䞭立郜垂アむズシティには、倧陞暪断列車バンダむ号のホヌムがありたす。 ホヌムには、バンダむ号を埅぀乗客甚の怅子が䞀列に甚意されおおり、ホヌムに入っおきた人は自由に怅子を利甚するこずができたす。 このバンダむ号、安いし早いし快適ずいうこずで、呚囲の4぀の囜からの利甚者が絶えたせん。今日は開通蚘念日ずいうこずで、ホヌムに座っおいる人たちに特別なこずをしおあげようず思っおいたす。そのためには、改札を通った人たちがどこに座っおいるか把握しなければなりたせん。4぀の囜の人たちの難儀な性栌を考慮しお、怅子がどのように埋たっおいるかをシミュレヌトするプログラムを䜜成しおください。改札を通った人たちは、次々に䞀列に䞊んだ怅子に座っおいきたす。4぀の囜の人たちは、囜ごずに決たった性栌ず座り方がありたす。それぞれの座り方は以䞋の通りです。 A囜人の性栌 A 囜人は座れればいい。巊端から芋おいき空いおいる怅子に座る。 B囜人の性栌 B 囜人は A 囜人が苊手。 A 囜人の隣以倖で、右端から空いおいる怅子に座る。ただし、 A囜人の隣しか空いおいない堎合、我慢しお巊端から空いおいる怅子に座る。 C囜人の性栌 C 囜人は人の隣に座りたい。巊偎から順に座っおいる人を芋おいき、䞀番巊偎に座っおいる人 の右隣に座ろうずするが、そこが埋たっおいるならその人の巊隣に座ろうずする。そこも埋たっ おいるなら次の人の隣に同条件で座ろうずする。どの怅子にも人が座っおいなければ真ん䞭(怅子の数 n が奇数の堎合 ( n +1)/2 、偶数の堎合 n /2+1 )の怅子に座る。 D囜人の性栌 D 囜人は人の隣に座りたくない。䞀番近い人ずの距離が、䞀番倧きくなる怅子に座ろうずする。同条件の怅子が耇数ある堎合や、どうしおも誰かの隣に座らなければならない堎合、その䞭で䞀番巊偎にある怅子に座る。誰も座っおいない堎合は巊端の怅子に座る。 バンダむ号に乗ろうずしおいる乗客の情報を入力ずし、 怅子にどのように座っおいるかを出力するプログラムを䜜成しおください。巊から順番に座っおいる人の囜籍を出力したす。ただし、空垭の堎合は # (半角シャヌプ)を出力しおください。 Input 耇数のデヌタセットの䞊びが入力ずしお䞎えられたす。 入力の終わりはれロふた぀の行で瀺されたす。 各デヌタセットは以䞋の圢匏で䞎えられたす。 n m a 1 a 2 : a m 1 行目に怅子の数 n (1 ≀ n ≀ 100) ず乗客の数 m ( m ≀ n ) が䞎えられたす。続く m 行に i 人目の情報 a i が䞎えられたす。 a i は぀の文字で、'A' ならば A 囜人、'B' ならば B 囜人、'C' ならば C 囜人、'D' ならば D 囜人を衚したす。 デヌタセットの数は 100 を超えたせん。 Output デヌタセットごずに、最終的な怅子の状態を行に出力したす。 Sample Input 5 4 A B C D 5 4 D C B A 0 0 Output for the Sample Input ACD#B DCA#B
39,133
双子の読曞感想文 双子のアミずマミが通う孊校では早くも倏䌑みに突入し今幎もおびただしい量の宿題が出された しかし遊び盛りの2人は今日も宿題には䞀切手を぀けずに倖ぞ遊びに行こうずしおいた このたたでは倏䌑み最終日に泣きを芋るのは明らかなので保護者圹のあなたは心を鬌にしお今日は読曞感想文の宿題が終わるたで2人を家から出さないこずにした 準備の良いあなたは既に図曞通から党おの課題図曞を借りおある ただし図曞通の芏則により各本は1冊ず぀しか借りられおいない さお教育䞊の理由から2人には互いに協力するこずなくそれぞれ党おの本を読み感想を曞いおもらう事にした 加えお本の返华期限が近いためたずはなるべく早くすべおの本を読み終えるようにしおもらう事にした そしおあなたはその条件䞋でなるべく早く宿題が終わるような宿題の進め方を考えるこずにした ここで本を読み終えた時間宿題を終えた時間はそれぞれ双子の䞡方が䜜業を終えた時間で考える 各本は1冊ず぀しかないため2人が同時刻に同じ本を読むこずはできない 加えお倧人の事情によりある本を読み始めたらそれを䞭断する事はできずある本に぀いおの感想文を曞き始めたらそれを䞭断するこずもできない 圓然ながら読んでいない本に぀いお感想を曞くこずもできない なおアミずマミは双子であるため各本に぀いお読むのにかかる時間感想文を曞くのにかかる時間は2人で共通しおいる 䟋えば3冊の本がありそれぞれ本を読むのにかかる時間感想文を曞くのにかかる時間が以䞋の通りであるずする 本を読む時間 感想文を曞く時間 本1 5 3 本2 1 2 本3 1 2 この堎合は図C-1のように宿題を進めるず時間10で党おの本を読み終え時間15で宿題を終える事ができる 図C-2の進め方では時間14で宿題を終えおいるが本を読み終える時間が最短でないため今回は採甚できない たた図C-3のように2人が同時に同じ本を読んだり 図C-4のように本を読むのを䞭断したり読んでいない本の感想を曞く事もできない 図 C-1: 最短で宿題を終わらせる進め方の䟋 図 C-2: 本を読み終える時間が最短でない䟋 図 C-3: 2人が同時に同じ本を読んでしたっおいる䟋 図 C-4: 読んでいない本の感想を曞いたり䜜業を䞭断したりしおいる䟋 様々な倧人の事情を考慮し぀぀遊びに行きたがっおいる双子のためになるべく早く宿題が終わるような進め方を考えおあげよう Input 入力は耇数のデヌタセットから構成される各デヌタセットの圢匏は次の通りである N r 1 w 1 r 2 w 2 ... r N w N Nは課題図曞の数を衚す敎数であり1以䞊1,000以䞋ず仮定しお良い 続くN行は課題図曞に぀いおの情報を衚す 各行はスペヌスで区切られた2぀の敎数を含みr i (1 ≀ r i ≀ 1,000) は i 番目の本を読むのにかかる時間w i (1 ≀ w i ≀ 1,000) は i 番目の本の感想文を曞くのにかかる時間を衚す N=0 は入力の終わりを瀺す これはデヌタセットには含めない Output 各デヌタセットに぀いお2人が党おの本を読み終えるたでの時間を最小にしたずきの党おの感想文を曞き終えるたでの最小の時間を1行に出力しなさい Sample Input 4 1 1 3 1 4 1 2 1 3 5 3 1 2 1 2 1 1000 1000 10 5 62 10 68 15 72 20 73 25 75 30 77 35 79 40 82 45 100 815 283 6 74 78 53 55 77 77 12 13 39 42 1 1 0 Output for Sample Input 14 15 3000 2013 522
39,134
Problem G: Flipping Parentheses A string consisting only of parentheses '(' and ')' is called balanced if it is one of the following. A string "()" is balanced. Concatenation of two balanced strings are balanced. When a string $s$ is balanced, so is the concatenation of three strings "(", $s$, and ")" in this order. Note that the condition is stronger than merely the numbers of '(' and ')' are equal. For instance, "())(()" is not balanced. Your task is to keep a string in a balanced state, under a severe condition in which a cosmic ray may flip the direction of parentheses. You are initially given a balanced string. Each time the direction of a single parenthesis is flipped, your program is notified the position of the changed character in the string. Then, calculate and output the leftmost position that, if the parenthesis there is flipped, the whole string gets back to the balanced state. After the string is balanced by changing the parenthesis indicated by your program, next cosmic ray flips another parenthesis, and the steps are repeated several times. Input The input consists of a single test case formatted as follows. $N$ $Q$ $s$ $q_1$ . . . $q_Q$ The first line consists of two integers $N$ and $Q$ ($2 \leq N \leq 300000$, $1 \leq Q \leq 150000$). The second line is a string $s$ of balanced parentheses with length $N$. Each of the following $Q$ lines is an integer $q_i$ ($1 \leq q_i \leq N$) that indicates that the direction of the $q_i$-th parenthesis is flipped. Output For each event $q_i$, output the position of the leftmost parenthesis you need to flip in order to get back to the balanced state. Note that each input flipping event $q_i$ is applied to the string after the previous flip $q_{i−1}$ and its fix. Sample Input 1 6 3 ((())) 4 3 1 Sample Output 1 2 2 1 Sample Input 2 20 9 ()((((()))))()()()() 15 20 13 5 3 10 3 17 18 Sample Output 2 2 20 8 5 3 2 2 3 18 In the first sample, the initial state is "((()))". The 4th parenthesis is flipped and the string becomes "(((())". Then, to keep the balance you should flip the 2nd parenthesis and get "()(())". The next flip of the 3rd parenthesis is applied to the last state and yields "())())". To rebalance it, you have to change the 2nd parenthesis again yielding "(()())".
39,135
Problem F: Shore Erosion あなたは地元の垂圹所に勀めるプログラマヌであるあなたの䜏んでいる町は芳光業が盛んでずくに離れ小島にある海岞の海氎济堎が人気があるこの島の呚囲は砂浜により構成されおいるのだが近幎海岞浞食によっお砂浜の面積が枛っおきた事態を重く芋た芳光課はあなたに砂浜の圢状倉化のシミュレヌションを䟝頌しおきた シミュレヌションでは珟圚の砂浜の圢状をもずにある期間が経過した埌の砂浜の圢状を求める簡単のため島の海岞線の圢状は倚角圢凞ずは限らないずみなす期間のうちに砂浜は珟圚の海岞線からマンハッタン距離で R だけ浞食されお海に沈むものず仮定するここでマンハッタン距離ずは x 座暙同士の差ず y 座暙同士の差を足したものであるすなわち( x 1 , y 1 ) ず ( x 2 , y 2 ) のマンハッタン距離は | x 1 - x 2 | + | y 1 - y 2 | で䞎えられる あなたの仕事は入力ずしお䞎えられた砂浜の海岞線からシミュレヌションの結果ずしお埗られる海岞線の長さを求めるプログラムを曞くこずであるなお島が海岞浞食により 2 ぀以䞊に分割された堎合は分割されたそれぞれの島に぀いお海岞線の長さを求めおそれらの和を出力するこず なお海岞線に含たれる 2 本の線分に぀いお平行でか぀マンハッタン距離がちょうど 2 R になるこずはない Input 入力は耇数のデヌタセットからなる各デヌタセットは次の圢匏で䞎えられる N R x 1 y 1 x 2 y 2 ... x N y N 最初の行では 2 ぀の非負の敎数 N (3 ≀ N ≀ 100) R (0 < R ≀ 100) が䞎えられる これらの敎数はそれぞれ海岞線の頂点数ず浞食される距離を衚す 続く N 行では頂点の情報が各行に 1 ぀ず぀䞎えられる 頂点の情報は 2 ぀の敎数 x i , y i (-10000 ≀ x i , y i ≀ 10000) で䞎えられる 座暙 ( x i , y i ) はそれぞれ海岞線の i 番目の頂点の座暙を衚す なお島の頂点座暙は垞に反時蚈回りで䞎えられる 入力の終わりは空癜で区切られた 2 ぀の 0 を含む 1 行で瀺される Output 各デヌタセットに察しお浞食された海岞線の長さを出力せよなお島が完党に浞食され消倱した堎合は海岞線の長さは 0.0 ず扱うこず出力する倀は 0.01 以䞋の誀差を含んでいおも構わないたた倀は小数点以䞋䜕桁衚瀺しおも構わない Sample Input 3 1 0 0 10 0 5 10 3 1 0 0 10 0 0 10 4 1 0 0 10 0 10 10 0 10 0 0 Output for the Sample Input 22.6524758425 23.8994949366 32.0000000000
39,136
I - Tokyo Olympics Center Problem Statement You are participating in the Summer Training Camp for Programming Cotests. The camp is held in an accommodation facility called Tokyo Olympics Center . Today is the last day of the camp. Unfortunately, you are ordered to check if all the participants have properly cleaned their rooms. The accommodation facility is a rectangular field whose height is $H$ and width is $W$. The field is divided into square cells. The rows are numbered $1$ through $H$ from top to bottom and the columns are numbered $1$ through $W$ from left to right. The cell in row $i$, column $j$ is denoted by $(i, j)$. Two cells are adjacent if they share an edge. Each cell is called either a wall cell or a floor cell . A wall cell represents a wall which no one can enter. A floor cell is a part of the inside of the accommodation facility. Everybody can move between two adjacent floor cells. The floor cells are divided into several units. Each unit is assigned an uppercase English letter ( A , B , C , ...). A floor cell adjacent to exactly one floor cell is called a room . Otherwise the floor cell is called an aisle . For example, the accommodation facility can be shown as the following figure. We denote a wall cell by . (single period). ................... .....AAABBBBBBB.... ...A.AA.A...B.B..B. ..AAAAAAAABBBBBBBB. ...A..A.A.....B.... ......A.......BBBB. ....A.AA..C.C...B.. ...AAAAACCCCCCBBBB. ...A..A...C.C...B.. ................... In the above figure, there are $7$ rooms in unit A, $4$ rooms in unit B, and $4$ rooms in unit C. Because the accommodation facility is too large to explore alone, you asked the other participants of the camp to check the rooms. For simplicity's sake, we call them staffs . Now, there are $K$ staffs at the cell $(s, t)$. You decided to check the rooms according to the following procedure. First, you assign each staff to some of units. Every unit must be assigned to exactly one staff. Note that it is allowed to assign all the units to one staff or to assign no units to some staffs. Then, each staff starts to check the rooms in the assigned units at the same time. The staffs can move between two adjacent floor cells in $T_{\mathit{move}}$ time. To check the room at $(i, j)$, the staffs must move to the cell $(i, j)$ and spend $T_{\mathit{check}}$ time there. Each staff first determines the order of the units to check and he or she must check the rooms according to the order. For example, suppose that there is a staff who is assigned units A, C, and E. He may decide that the order is E->A->C. After that, he must check all the rooms in unit E at first. Then, he must check all the rooms in unit A, and so on. The staffs can pass any floor cells. However, the staffs cannot check rooms that are not assigned to them. Further, the staffs cannot check rooms against the order of units that they have decided. After checking all the assigned rooms, the staffs must return to the cell $(s, t)$. When every staff returns to the cell $(s, t)$, the task is done. Because you do not have enough time before the next contest, you want to minimize the total time to check all the rooms. Input The first line of the input contains three integers $H$, $W$, and $K$ ($1 \le H \le 50$, $1 \le W \le 50$, $1 \le K \le 12$). The second line contains four integers $s$, $t$, $T_{\mathit{move}}$, and $T_{\mathit{check}}$ ($1 \le s \le H$, $1 \le t \le W$, $1 \le T_{\mathit{move}},T_{\mathit{check}} \le 10{,}000$). Each of the following $H$ lines contains exactly $W$ characters. Each character represents a cell in the accommodation facility. The $j$-th character in the $i$-th line of these lines is . if the cell $(i, j)$ is a wall cell. Otherwise, the character is an uppercase English letter ( A - L ) representing the unit to which the cell $(i, j)$ belongs. You can assume that the following conditions are satisfied. The number of rooms in each unit is between $1$ and $12$, inclusive. The cell $(s, t)$ is guaranteed to be an aisle. Floor cells in each unit are connected. Floor cells in the field are connected. Every unit contains at least two cells. Output Output the minimum required time to check all the rooms in one line. Sample Input 1 3 3 1 1 1 10 10 AAA A.. A.. Output for the Sample Input 1 100 Sample Input 2 3 3 2 1 1 10 10 ABB A.. A.. Output for the Sample Input 2 50 Sample Input 3 5 10 3 3 6 1 100 ...G.H.A.. .AAGAHAABB FFAAAAAAA. .EEAAADACC ..E...D... Output for the Sample Input 3 316 Sample Input 4 10 19 2 6 15 3 10 ................... .....AAABBBBBBB.... ...A.AA.A...B.B..B. ..AAAAAAAABBBBBBBB. ...A..A.A.....B.... ......A.......BBBB. ....A.AA..C.C...B.. ...AAAAACCCCCCBBBB. ...A..A...C.C...B.. ................... Output for the Sample Input 4 232 Sample Input 5 27 36 6 24 19 616 1933 .................................... ..........B...............B......... ..........BBB..........BBBB......... ..........BBBBB.......BBBB.......... ...........BBBBBBBBBBBBBBB.......... ...........BBBBBBBBBBBBBBB.......... ...........BBBBBBBBBBBBBB........... ............BBBBBBBBBBBBB........... ...........BBBBBBBBBBBBBBB.......... ..........BBBBBBBBBBBBBBBBB......... ......B...BBBBBBBBBBBBBBBBB...B..... ......BB.BBBBBBBBBBBBBBBBBB..BB..... .......BBBBBBBBBBBBBBBBBBBB.BB...... .........BBBBBBBBBBBBBBBBBBBB....... ........BBBBB..BBBBBBBB..BBBBB...... ......BBBBBB....BBBBB....BBBBBB..... .....BBBBBBBB...BBBBB..BBBBBBBB.B... ...BBBBBBB..B...BBBBB..B..BBBBBBBB.. .BBBBBBBBB.....BBBBBBB......BBBBBBB. ..BBBBBBB......BBBBBBB........BBB... .BBBB.........BBBBBBBBB........BBB.. ..............BBBBBBBBB............. .............BBBBBBBBBB............. .............BBBBBBBBBB............. ..............BBBBBBBB.............. ..............BBBBBBBB.............. .................................... Output for the Sample Input 5 137071
39,137
Barter Problem ナナツ君は、うたか棒を x 本、ふがしを y 本持っおいる。 n 人のお菓子亀換人がいる。 それぞれの亀換人 i は、 ナナツ君のうたか棒 a i 本ず亀換人のふがし b i 本 ナナツ君のふがし c i 本ず亀換人のブタメ゜ d i 個 のどちらか1぀の方法で1回だけ亀換しおくれる。 この制玄の䞋で、最終的に所持しおいるブタメ゜の個数を最倧化せよ。 Input 入力は以䞋の圢匏で䞎えられる。 n x y a 1 b 1 c 1 d 1 a 2 b 2 c 2 d 2 ... a n b n c n d n 1行目に敎数 n が䞎えられる。 2行目に敎数 x , y が空癜区切りで䞎えられる。 3行目から n +2行目に敎数 a i , b i , c i , d i が空癜区切りで䞎えられる。 Constraints 1 ≀ n ≀ 100 0 ≀ x , y ≀ 300 1 ≀ a i , b i , c i , d i ≀ 300 \(\sum_{i=1}^na_i\) ≀ 300, \(\sum_{i=1}^nb_i\) ≀ 300, \(\sum_{i=1}^nc_i\) ≀ 300, \(\sum_{i=1}^nd_i\) ≀ 300 Output 最終的にナナツ君が所持しおいるブタメ゜の個数の最倧倀を1行に出力する Sample Input 1 3 3 3 3 2 4 1 5 1 2 1 3 1 3 2 Sample Output 1 3 Sample Input 2 3 3 4 3 1 3 1 3 1 4 1 3 1 2 1 Sample Output 2 2 Sample Input 3 4 5 0 5 1 1 1 2 1 2 1 4 1 1 1 3 1 3 1 Sample Output 3 2 Sample Input 4 4 0 0 1 10 1 10 2 5 2 5 3 25 5 6 1 15 2 20 Sample Output 4 0
39,138
Score : 2200 points Problem Statement You are given a tree T with N vertices and an undirected graph G with N vertices and M edges. The vertices of each graph are numbered 1 to N . The i -th of the N-1 edges in T connects Vertex a_i and Vertex b_i , and the j -th of the M edges in G connects Vertex c_j and Vertex d_j . Consider adding edges to G by repeatedly performing the following operation: Choose three integers a , b and c such that G has an edge connecting Vertex a and b and an edge connecting Vertex b and c but not an edge connecting Vertex a and c . If there is a simple path in T that contains all three of Vertex a, b and c in some order, add an edge in G connecting Vertex a and c . Print the number of edges in G when no more edge can be added. It can be shown that this number does not depend on the choices made in the operation. Constraints 2 \leq N \leq 2000 1 \leq M \leq 2000 1 \leq a_i, b_i \leq N a_i \neq b_i 1 \leq c_j, d_j \leq N c_j \neq d_j G does not contain multiple edges. T is a tree. Input Input is given from Standard Input in the following format: N M a_1 b_1 : a_{N-1} b_{N-1} c_1 d_1 : c_M d_M Output Print the final number of edges in G . Sample Input 1 5 3 1 2 1 3 3 4 1 5 5 4 2 5 1 5 Sample Output 1 6 We can have at most six edges in G by adding edges as follows: Let (a,b,c)=(1,5,4) and add an edge connecting Vertex 1 and 4 . Let (a,b,c)=(1,5,2) and add an edge connecting Vertex 1 and 2 . Let (a,b,c)=(2,1,4) and add an edge connecting Vertex 2 and 4 . Sample Input 2 7 5 1 5 1 4 1 7 1 2 2 6 6 3 2 5 1 3 1 6 4 6 4 7 Sample Output 2 11 Sample Input 3 13 11 6 13 1 2 5 1 8 4 9 7 12 2 10 11 1 9 13 7 13 11 8 10 3 8 4 13 8 12 4 7 2 3 5 11 1 4 2 11 8 10 3 5 6 9 4 10 Sample Output 3 27
39,139
問題文 キュゥべえ はワルプルギスの倜に向けお魔女の合成に明け暮れおいるワルプルギスの倜のために特別な グリヌフシヌド (魔女の卵) を䜿っお手に入れやすい魔女をうたく合成し目的である舞台装眮の魔女を創り䞊げなければならないからである キュゥべえ は手に入れやすい皮類の魔女の グリヌフシヌド から魔女の玠ずなるものを取り出し空の特別な グリヌフシヌド に入れるこずでその グリヌフシヌド に魔女を宿らせるこずができるたたこの䞖界にはある組み合わせで耇数の魔女を合成するず新たな 1 ぀の魔女を生み出すこずができるずいう合成法則がいく぀かあるそこでいく぀かの特別な グリヌフシヌド を䜿いそれらの䞭にある魔女の玠ずなるものを合成し 1 ぀の特別な グリヌフシヌド に新たな魔女を宿らせるこずができるたた合成した結果空ずなった特別な グリヌフシヌド は再利甚するこずができる䟋えば 3 個の特別な グリヌフシヌド に入った魔女を合成するずそれらの 3 個の グリヌフシヌド は新たな魔女が宿った 1 個の特別な グリヌフシヌド ず 2 個の空の特別な グリヌフシヌド ずなるしかし同じ皮類の魔女を同時に耇数の グリヌフシヌド に入れるこずは出来ない キュゥべえ は魔女の特別な グリヌフシヌド ぞの寄宿ず魔女の合成を繰り返すこずにより舞台装眮の魔女を創りだしたい舞台装眮の魔女を創りだすにはいく぀の特別な グリヌフシヌド が必芁か求めよ 入力圢匏 入力は以䞋のような圢匏で䞎えられる N\ E\ T\\ W_1\ W_2\ 
\ W_N\\ G_1\ C_1\ S_{1,1}\ S_{1,2}\ 
\ S_{1,C_1}\\ G_2\ C_2\ S_{2,1}\ S_{2,2}\ 
\ S_{2,C_2}\\ 
\\ G_E\ C_E\ S_{E,1}\ S_{E,2}\ 
\ S_{E,C_E}\\ N は魔女の皮類の個数 E は合成法則の個数 T は舞台装眮の魔女の番号である W_i は i 番目の魔女が手に入れやすい魔女であるかどうかを衚し 1 ならば手に入れやすい魔女であり 0 ならば手に入れやすい魔女ではない 続く E 行に合成法則が次のように衚される「番号が S_{i,1}\ S_{i,2}\ 
\ S_{i,C_i} の魔女を合成するこずによっお G_i の番号の魔女が埗られる」 出力圢匏 舞台装眮の魔女を創りだすのに最䜎限必芁な特別な グリヌフシヌド の個数を出力せよ創りだすこずができない堎合は -1 を出力せよ 制玄 1 \lt N \leq 300 0 \leq E \leq 1000 1 \leq T \leq N 0 \leq W_i \leq 1\ (1 \leq i \leq N) 1 \leq G_i \leq N\ (1 \leq i \leq E) 2 \leq C_i \leq 10\ (1 \leq i \leq E) 1 \leq S_{i,j} \leq N\ (1 \leq i \leq E,\ 1 \leq j \leq C_i) \{G_i, S_{i,1}\ S_{i,2}\ 
\ S_{i,C_i}\} は党お異なる 入力䞭に同䞀の合成法則が 2 回以䞊珟れるこずはない 入出力䟋 入力䟋 1 3 1 3 1 1 0 3 2 1 2 出力䟋1 2 入力䟋 2 5 0 1 1 1 1 1 1 出力䟋 2 1 入力䟋 3 18 5 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 2 3 4 5 2 2 6 7 3 3 8 9 10 4 4 11 12 13 14 5 4 15 16 17 18 出力䟋 3 5 入力䟋 4 6 4 6 1 1 1 0 0 0 4 2 1 2 5 2 2 3 6 2 4 5 6 3 1 2 4 出力䟋 4 3 入力䟋 5 13 9 13 1 1 0 1 1 0 1 1 0 1 1 0 0 3 2 1 2 6 2 4 5 9 2 7 8 12 2 10 11 6 2 2 3 9 2 5 6 12 2 8 9 3 2 11 12 13 4 3 6 9 12 出力䟋 5 5 入力䟋 6 4 4 4 1 1 0 0 1 2 2 3 2 2 3 1 3 2 1 2 3 3 1 2 4 出力䟋 6 -1 Problem Setter: Flat35
39,140
Score : 200 points Problem Statement Takahashi has N blue cards and M red cards. A string is written on each card. The string written on the i -th blue card is s_i , and the string written on the i -th red card is t_i . Takahashi will now announce a string, and then check every card. Each time he finds a blue card with the string announced by him, he will earn 1 yen (the currency of Japan); each time he finds a red card with that string, he will lose 1 yen. Here, we only consider the case where the string announced by Takahashi and the string on the card are exactly the same. For example, if he announces atcoder , he will not earn money even if there are blue cards with atcoderr , atcode , btcoder , and so on. (On the other hand, he will not lose money even if there are red cards with such strings, either.) At most how much can he earn on balance? Note that the same string may be written on multiple cards. Constraints N and M are integers. 1 \leq N, M \leq 100 s_1, s_2, ..., s_N, t_1, t_2, ..., t_M are all strings of lengths between 1 and 10 (inclusive) consisting of lowercase English letters. Input Input is given from Standard Input in the following format: N s_1 s_2 : s_N M t_1 t_2 : t_M Output If Takahashi can earn at most X yen on balance, print X . Sample Input 1 3 apple orange apple 1 grape Sample Output 1 2 He can earn 2 yen by announcing apple . Sample Input 2 3 apple orange apple 5 apple apple apple apple apple Sample Output 2 1 If he announces apple , he will lose 3 yen. If he announces orange , he can earn 1 yen. Sample Input 3 1 voldemort 10 voldemort voldemort voldemort voldemort voldemort voldemort voldemort voldemort voldemort voldemort Sample Output 3 0 If he announces voldemort , he will lose 9 yen. If he announces orange , for example, he can avoid losing a yen. Sample Input 4 6 red red blue yellow yellow red 5 red red yellow green blue Sample Output 4 1
39,141
Conveyor Belt Awesome Conveyor Machine (ACM) is the most important equipment of a factory of Industrial Conveyor Product Corporation (ICPC). ACM has a long conveyor belt to deliver their products from some points to other points. You are a programmer hired to make efficient schedule plan for product delivery. ACM's conveyor belt goes through $N$ points at equal intervals. The conveyor has plates on each of which at most one product can be put. Initially, there are no plates at any points. The conveyor belt moves by exactly one plate length per unit time; after one second, a plate is at position 1 while there are no plates at the other positions. After further 1 seconds, the plate at position 1 is moved to position 2 and a new plate comes at position 1, and so on. Note that the conveyor has the unlimited number of plates: after $N$ seconds or later, each of the $N$ positions has exactly one plate. A delivery task is represented by positions $a$ and $b$; delivery is accomplished by putting a product on a plate on the belt at $a$, and retrieving it at $b$ after $b - a$ seconds ($a < b$). (Of course, it is necessary that an empty plate exists at the position at the putting time.) In addition, putting and retrieving products must bedone in the following manner: When putting and retrieving a product, a plate must be located just at the position. That is, products must be put and retrieved at integer seconds. Putting and retrieving at the same position cannot be done at the same time. On the other hand, putting and retrieving at the different positions can be done at the same time. If there are several tasks, the time to finish all the tasks may be reduced by changing schedule when each product is put on the belt. Your job is to write a program minimizing the time to complete all the tasks... wait, wait. When have you started misunderstanding that you can know all the tasks initially? New delivery requests are coming moment by moment, like plates on the conveyor! So you should update your optimal schedule per every new request. A request consists of a start point $a$, a goal point $b$, and the number $p$ of products to deliver from $a$ to $b$. Delivery requests will be added $Q$ times. Your (true) job is to write a program such that for each $1 \leq i \leq Q$, minimizing the entire time to complete delivery tasks in requests 1 to $i$. Input The input consists of a single test case formatted as follows. $N$ $Q$ $a_1$ $b_1$ $p_1$ : $a_Q$ $b_Q$ $p_Q$ A first line includes two integers $N$ and $Q$ ($2 \leq N \leq 10^5, 1 \leq Q \leq 10^5$): $N$ is the number of positions the conveyor belt goes through and $Q$ is the number of requests will come. The $i$-th line of the following $Q$ lines consists of three integers $a_i, b_i,$ and $p_i$ ($1 \leq a_i < b_i \leq N, 1 \leq p_i \leq 10^9$), which mean that the $i$-th request requires $p_i$ products to be delivered from position $a_i$ to position $b_i$. Output In the $i$-th line, print the minimum time to complete all the tasks required by requests $1$ to $i$. Sample Input 1 5 2 1 4 1 2 3 1 Output for Sample Input 1 4 4 Sample Input 2 5 2 1 4 1 2 3 5 Output for Sample Input 2 4 8 Sample Input 3 5 2 1 3 3 3 5 1 Output for Sample Input 3 5 6 Sample Input 4 10 4 3 5 2 5 7 5 8 9 2 1 7 5 Output for Sample Input 4 6 11 11 16 Regarding the first example, the minimum time to complete only the first request is 4 seconds. All the two requests can be completed within 4 seconds too. See the below figure.
39,142
Score : 800 points Problem Statement Given are N pairwise distinct non-negative integers A_1,A_2,\ldots,A_N . Find the number of ways to choose a set of between 1 and K numbers (inclusive) from the given numbers so that the following two conditions are satisfied: The bitwise AND of the chosen numbers is S . The bitwise OR of the chosen numbers is T . Constraints 1 \leq N \leq 50 1 \leq K \leq N 0 \leq A_i < 2^{18} 0 \leq S < 2^{18} 0 \leq T < 2^{18} A_i \neq A_j ( 1 \leq i < j \leq N ) Input Input is given from Standard Input in the following format: N K S T A_1 A_2 ... A_N Output Print the answer. Sample Input 1 3 3 0 3 1 2 3 Sample Output 1 2 The conditions are satisfied when we choose \{1,2\} or \{1,2,3\} . Sample Input 2 5 3 1 7 3 4 9 1 5 Sample Output 2 2 Sample Input 3 5 4 0 15 3 4 9 1 5 Sample Output 3 3
39,143
Problem F: Strawberry Cake 䌚接倧孊附属小孊校 (䌚接倧小) に入孊したシャ、ネロ、゚リ、コヌは競技プログラマずしお掻躍するために IPPC ずいうプログラミングコンテストに出堎するこずにした。しかし IPPC は 3 人 1 組でチヌムずしおコンテストに出堎する決たりで、4 人でチヌムを組むこずはできない。そこで圌らは 2 人ず぀に分かれおチヌムを組み、2 人 1 組で参加できる IPOCH ずいうプログラミングコンテストに出堎するこずにした。 チヌムは分かれおしたったが、実力は拮抗しおおり互いに良い緎習盞手になっおいる。 ある日圌らのコヌチが差し入れにホヌルケヌキを買っおきた。圌らはそれをシャの家で食べるこずにした。しかし家に着いお箱を開けおみるずケヌキが倉圢しおいお、圢は䞊から芋るず円圢ではなく凞倚角圢のようになっおいた。ネロに運ばせたのが悪かったようだ。 ケヌキの䞊にはむチゎが 1 ぀乗っおいる。ずりあえず 2 チヌムで分けるために、いたこのケヌキをむチゎを通る盎線にそっお半分になるようにナむフで切るこずが決定した。ケヌキを切る前にむチゎを先にずっおしたうのは圌らの矎意識に反する。 あなたの仕事は 2 次元平面䞊に䞎えられる凞倚角圢 K を原点 (0, 0) を通る盎線 L で分割したずき、分割で出来た 2 ぀の凞倚角圢の面積が等しくなるように盎線 L を定めるこずである。もし耇数あるならいずれかひず぀で構わない。 Input 入力は耇数のテストケヌスからなる。各テストケヌスは以䞋の圢匏に埓う。 N x 1 y 1 x 2 y 2 ... x N y N N は䞎えられる凞倚角圢 K の頂点数である。点 (x i , y i ) ( 1 ≀ i ≀ N ) はすべお凞倚角圢 K を成す頂点の座暙であり、互いに異なる。これらは反時蚈回りの順で䞎えられる。たた、凞倚角圢 K のどの蟺䞊の点も原点ずの距離が 1 以䞊で、凞倚角圢 K は垞に原点を内包する。入力の終了はひず぀の0を含む行で瀺される。原点を含む、入力で䞎えられる異なる 3 点は共線ずなりうるこずに泚意しお欲しい。 Constraints 入力はすべお敎数 3 ≀ N ≀ 20 -100 ≀ x i ≀ 100 ( 1 ≀ i ≀ N ) -100 ≀ y i ≀ 100 ( 1 ≀ i ≀ N ) テストケヌスの数は 1,000 を超えない。 Output 以䞋の圢匏で盎線 L 䞊の点 A (A x , A y ) を出力せよ。ただし点 A ず原点ずの距離は 1 以䞊でなければならない。 A x A y 原点ずこの点 A を通る盎線で凞倚角圢 K を分割したずき 2 ぀の凞倚角圢の面積の差が 10 -5 未満でなければならない。点 A の座暙は小数点以䞋 15 桁出力しお欲しい。 Sample Input 4 -100 -100 100 -100 100 100 -100 100 4 -99 -100 100 -100 100 100 -99 100 4 -100 -99 100 -99 100 100 -100 100 14 -99 -70 -92 -79 10 -98 37 -100 62 -95 77 -69 88 -47 92 -10 96 28 100 91 42 92 -62 92 -88 91 -98 64 0 Sample Output 100.000000000000000 0.000000000000000 100.000000000000000 0.000000000000000 0.000000000000000 100.000000000000000 -96.983291994836122 66.745111613942484
39,144
H: 魔法䜿いの塔 問題文 あなたが持っおいる魔導曞には、 $N$ 個の魔法が茉っおいたす。 魔法には $1$ から $N$ たでの番号が぀いおいお、魔法 $i (1 \le i \le N)$ のコストははじめ敎数 $A_i$ です。 あなたの目的は、魔導曞に茉っおいるすべおの魔法を $1$ 回ず぀唱えるこずです。 魔法を唱えはじめる前に、あなたはクッキヌを $K$ 枚食べるこずができたす。クッキヌを食べるのに時間はかかりたせん。 あなたはクッキヌを $1$ 枚食べるたびに、コストが正の魔法を $1$ ぀遞び、そのコストを $1$ 䞋げるこずができたす。 クッキヌを食べたあず、あなたは魔法を唱え始めたす。 あなたの MP ははじめ $M$ です。あなたは以䞋のどちらかを繰り返しお、 $N$ 個の魔法を任意の順番で $1$ 回ず぀唱えたす。 敎数 $i (1 \le i \le N)$ を $1$ ぀遞び、魔法 $i$ を唱える。ただし、 珟圚の MP は魔法 $i$ のコスト以䞊でなければならない。 時間は経過しない。 MP を魔法 $i$ のコストだけ消費する。 䌑憩する。ただし、珟圚の MP を $m$ ずするず、 $m < M$ でなければならない。 時間が $M - m$ 経過する。 MP を $1$ 回埩する。 あなたが $N$ 個の魔法を任意の順番で $1$ 回ず぀唱えるためにかかる時間の最小倀を求めおください。 制玄 $1 \leq N \leq 10^5$ $1 \leq M \leq 10^6$ $0 \leq K \leq \sum_{i=1}^{N} A_i$ $1 \leq A_i \leq M$ 入力はすべお敎数である。 入力 入力は以䞋の圢匏で暙準入力から䞎えられる。 $N$ $M$ $K$ $A_1$ $A_2$ $\ldots$ $A_N$ 出力 $N$ 個の魔法を $1$ 回ず぀唱えるためにかかる時間の最小倀を出力せよ。 入力䟋1 2 4 0 2 4 出力䟋1 3 どの魔法のコストも枛らすこずができないので、このたた魔法を唱えおいくこずにしたす。 たず、魔法 $1$ を唱えたす。 MP を $2$ 消費するので、残りの MP は $4 - 2 = 2$ になりたす。 魔法 $2$ を唱えるには MP が $4$ 必芁なので、このたたでは魔法 $2$ を唱えるこずはできたせん。 䌑憩したす。時間が $4 - 2 = 2$ 秒経過したす。 MP を $1$ 回埩するので、残りの MP は $2 + 1 = 3$ になりたす。 䌑憩したす。時間が $4 - 3 = 1$ 秒経過したす。 MP を $1$ 回埩するので、残りの MP は $3 + 1 = 4$ になりたす。 魔法 $2$ を唱えたす。 MP を $4$ 消費するので、残りの MP は $4 - 4 = 0$ になりたす。 以䞊より、時間を $2 + 1 = 3$ 秒かければ、魔法 $1,2$ を $1$ 回ず぀唱えるこずができたす。 これより短い時間ですべおの魔法を唱えるこずはできないので、求める答えは $3$ ずなりたす。 入力䟋2 3 9 6 2 3 9 出力䟋2 0 最終的な魔法のコストを $2, 2, 4$ ずするず、䌑憩するこずなくすべおの魔法を唱えるこずができたす。 入力䟋3 3 16 2 6 9 9 出力䟋3 21 入力䟋4 2 1000000 0 1000000 1000000 出力䟋4 500000500000 答えは 32bit 敎数で衚せる範囲に収たらないこずがありたす。
39,145
自転車でダむ゚ット A さんは甘いものが倧奜きですが、最近は奥さんからダむ゚ットするように蚀われおいたす。ある日、A さんが自宅から垂圹所に出かけるずき、奥さんは自転車で行くこずを勧めたした。そこでA さんはしぶしぶ自転車に乗っお出かけたしたが、甘い物の奜きな A さんは、途䞭にあるケヌキ屋さんに立ち寄りケヌキの食べ歩きをしようず思い぀きたした。 自転車をこげば走行距離に応じおカロリヌが消費されたすが、ケヌキを食べればその分カロリヌが摂取されたす。正味の消費カロリヌは、自転車をこいで消費したカロリヌからケヌキを食べお摂取したカロリヌを匕いた倀になりたす。したがっお、正味の消費カロリヌが 0 より小さくなるこずもありたす。 ケヌキ屋さんでケヌキを買ったら、 Aさんはその堎でケヌキを党郚食べおしたいたす。 Aさんがすべおのケヌキ屋さんに立ち寄るずは限りたせんが、ケヌキ屋さんが存圚する地点を通るずきには、必ず立ち寄っおケヌキを 1 ぀買っお食べるものずしたす。ただし、同じケヌキ屋さんの前を䜕床も通るのはさすがに気がひけるので、各ケヌキ屋さんに蚪れられるのは 1 回だけずしたす。たた、目的地の垂圹所をいったん通り過ぎおからケヌキ屋さんに立ち寄り、その埌で垂圹所に戻っお甚を枈たせおもよいものずし、ケヌキ屋さん以倖は䜕床蚪れおも良いものずしたす。 A さんの自宅から垂圹所たでの地図情報ず、その途䞭にあるケヌキ屋さんで食べられるケヌキのカロリヌの䞀芧、単䜍距離の走行による消費カロリヌを入力ずし、自宅を出発しおから垂圹所に入るたでの正味の消費カロリヌの最小倀を出力するプログラムを䜜成しおください。 地図には、A さんの自宅ず垂圹所、ケヌキ屋さんずランドマヌク(目印)になる建物が曞かれおいたす。地図を衚す入力デヌタは、A さんの自宅、垂圹所、ケヌキ屋さんずランドマヌクの各地点の間を぀なぐ道があるずき、2 ぀の地点を衚す蚘号ずその間の距離からなる行を含みたす。たずえば、5 番目のケヌキ屋さんず 3 番目のランドマヌクの間の距離が 10 なら、入力デヌタは以䞋のような行を含みたす。 C5 L3 10 このようにケヌキ屋さんには C、ランドマヌクには L を番号の前に぀けお衚したす。たた、A さんの自宅は H、垂圹所は D で衚したす。入力デヌタに 2 ぀の地点ずその間の距離が䞎えられおいるなら、2 地点の間をどちら向きにも進めたす。たずえば、䞊の䟋ではケヌキ屋さんからランドマヌクぞも、その逆向きぞも進むこずができたす。たた、自宅から垂圹所たでは必ずたどり着けるものずしたす。それ以倖に䞎えられる入力デヌタは、ケヌキ屋さんの数 m 、ランドマヌクの数 n 、単䜍距離あたりの消費カロリヌ k 、 1 番目のケヌキ屋さんから m 番目のケヌキ屋さんたでのそれぞれで買えるケヌキのカロリヌを衚す m 個のデヌタ、距離のデヌタの総数 d です。 Input 耇数のデヌタセットの䞊びが入力ずしお䞎えられたす。入力の終わりは四぀の 0 の行で瀺されたす。 各デヌタセットは以䞋の圢匏で䞎えられたす。 m n k d c 1 c 2 ... c m s 1 t 1 e 1 s 2 t 2 e 2 : s d t d e d 1 行目にケヌキ屋さんの数 m (1 ≀ m ≀ 6)、 ランドマヌクの数 n (1 ≀ n ≀ 100) 、単䜍距離あたりの消費カロリヌ k (1 ≀ k ≀ 5)、距離のデヌタの総数 d (5 ≀ d ≀ 256) が䞎えられたす。 2 行目に各ケヌキ屋で買うケヌキのカロリヌ c i (1 ≀ c i ≀ 100) が䞎えられたす。 続く d 行に i 番目の 2 ぀の地点間の距離デヌタ s i , t i , e i (1 ≀ e i ≀ 20) が䞎えられたす。 デヌタセットの数は 100 を超えたせん。 Output 入力デヌタセットごずに、党䜓の消費カロリヌの最小倀を行に出力したす。 Sample Input 1 1 2 5 35 H L1 5 C1 D 6 C1 H 12 L1 D 10 C1 L1 20 2 1 4 6 100 70 H L1 5 C1 L1 12 C1 D 11 C2 L1 7 C2 D 15 L1 D 8 0 0 0 0 Output for the Sample Input 1 -2
39,146
Problem F: Marching Course Since members of Kitafuji High School Brass Band Club succeeded in convincing their stern coach of their playing skills, they will be able to participate in Moon Light Festival as a marching band. This festival is a prelude in terms of appealing their presence for the coming domestic contest. Hence, they want to attract a festival audience by their performance. Although this festival restricts performance time up to $P$ minutes, each team can freely determine their performance course from a provided area. The provided area consists of $N$ checkpoints, numbered 1 through $N$, and $M$ bidirectional roads connecting two checkpoints. Kitafuji Brass Band already has the information about each road: its length and the expected number of people on its roadside. Each team must start at the checkpoint 1, and return back to the checkpoint 1 in $P$ minutes. In order to show the performance ability of Kitafuji Brass Band to a festival audience, their stern coach would like to determine their performance course so that many people listen their march as long as possible. The coach uses "impression degree" to determine their best course. If they play $m$ minutes on the road with length $d$ and the expected number $v$ of people, then the impression degree will be $m \times v/d$. The impression degree of a course is the sum of impression degree of their performance on the course. Marching bands must move at a constant speed during marching: 1 unit length per 1 minute. On the other hand, they can turn in the opposite direction at any time, any place including a point on a road. The impression degree is accumulated even if they pass the same interval two or more times. Your task is to write a program to determine a course with the maximum impression degree in order to show the performance ability of Kitafuji Brass Band to an audience as much as possible. Input The input is formatted as follows. $N$ $M$ $P$ $s_1$ $t_1$ $d_1$ $v_1$ : : : $s_M$ $t_M$ $d_M$ $v_M$ The first line contains three integers $N$, $M$, and $P$: the number of checkpoints $N$ ($2 \leq N \leq 200$), the number of roads $M$ ($N - 1 \leq M \leq N(N - 1)/2$), and the performance time $P$ ($1 \leq P \leq 1,000$). The following $M$ lines represent the information about roads. The $i$-th line of them contains four integers $s_i$, $t_i$, $d_i$, and $v_i$: the $i$-th road bidirectionally connects between checkpoints $s_i$ and $t_i$ ($1 \leq s_i, t_i \leq N, s_i \ne t_i$) with length $d_i$ ($1 \leq d_i \leq 1,000$) and the expected number $v_i$ ($1 \leq v_i \leq 1,000$) of people. You can assume that any two checkpoints are directly or indirectly connected with one or more roads. You can also assume that there are no pair of roads having the same pair of endpoints. Output Output the maximum impression degree of a course for a $P$-minute performance. The absolute error should be less than $10^{-4}$. Sample Input 3 3 4 1 2 1 1 2 3 2 4 3 1 1 1 Output for the Sample Input 6 Sample Input 4 3 9 1 2 2 1 1 3 2 2 1 4 2 3 Output for the Sample Input 13.5 Sample Input 4 3 5 1 2 10 1 2 3 2 100 1 4 3 10 Output for the Sample Input 16.6666666667 Sample Input 3 3 10 1 2 3 1 1 3 4 5 2 3 2 10 Output for the Sample Input 22
39,147
Problem D: Cliff Climbing At 17:00, special agent Jack starts to escape from the enemy camp. There is a cliff in between the camp and the nearest safety zone. Jack has to climb the almost vertical cliff by stepping his feet on the blocks that cover the cliff. The cliff has slippery blocks where Jack has to spend time to take each step. He also has to bypass some blocks that are too loose to support his weight. Your mission is to write a program that calculates the minimum time to complete climbing. Figure D-1 shows an example of cliff data that you will receive. The cliff is covered with square blocks. Jack starts cliff climbing from the ground under the cliff, by stepping his left or right foot on one of the blocks marked with 'S' at the bottom row. The numbers on the blocks are the "slippery levels". It takes t time units for him to safely put his foot on a block marked with t , where 1 ≀ t ≀ 9. He cannot put his feet on blocks marked with 'X'. He completes the climbing when he puts either of his feet on one of the blocks marked with 'T' at the top row. Figure D-1: Example of Cliff Data Jack's movement must meet the following constraints. After putting his left (or right) foot on a block, he can only move his right (or left, respectively) foot. His left foot position ( lx , ly ) and his right foot position ( rx , ry ) should satisfy lx < rx and | lx - rx | + | ly - ry | ≀ 3 . This implies that, given a position of his left foot in Figure D-2 (a), he has to place his right foot on one of the nine blocks marked with blue color. Similarly, given a position of his right foot in Figure D-2 (b), he has to place his left foot on one of the nine blocks marked with blue color. Figure D-2: Possible Placements of Feet Input The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. Each dataset is formatted as follows: w h s(1,1) ... s(1,w) s(2,1) ... s(2,w) ... s(h,1) ... s(h,w) The integers w and h are the width and the height of the matrix data of the cliff. You may assume 2 ≀ w ≀ 30 and 5 ≀ h ≀ 60. Each of the following h lines consists of w characters delimited by a space. The character s(y, x) represents the state of the block at position ( x , y ) as follows: 'S': Jack can start cliff climbing from this block. 'T': Jack finishes climbing when he reaches this block. 'X': Jack cannot put his feet on this block. '1' - '9' ( = t ): Jack has to spend t time units to put either of his feet on this block. You can assume that it takes no time to put a foot on a block marked with 'S' or 'T'. Output For each dataset, print a line only having a decimal integer indicating the minimum time required for the cliff climbing, when Jack can complete it. Otherwise, print a line only having "-1" for the dataset. Each line should not have any characters other than these numbers. Sample Input 6 6 4 4 X X T T 4 7 8 2 X 7 3 X X X 1 8 1 2 X X X 6 1 1 2 4 4 7 S S 2 3 X X 2 10 T 1 1 X 1 X 1 X 1 1 1 X 1 X 1 1 1 X S S 2 10 T X 1 X 1 X 1 X 1 1 1 X 1 X 1 1 1 X S S 10 10 T T T T T T T T T T X 2 X X X X X 3 4 X 9 8 9 X X X 2 9 X 9 7 7 X 7 3 X X 8 9 X 8 9 9 9 6 3 X 5 X 5 8 9 9 9 6 X X 5 X 5 8 6 5 4 6 8 X 5 X 5 8 9 3 9 6 8 X 5 X 5 8 3 9 9 6 X X X 5 X S S S S S S S S S S 10 7 2 3 2 3 2 3 2 3 T T 1 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 4 3 2 3 2 3 2 3 2 3 5 3 2 3 1 3 2 3 2 3 5 2 2 3 2 4 2 3 2 3 5 S S 2 3 2 1 2 3 2 3 0 0 Output for the Sample Input 12 5 -1 22 12
39,148
Road Improvement Aizu is a country famous for its rich tourism resources and has N cities, each of which is uniquely identified with a number (0 to N -1). It has a road network consisting of M one-way roads connecting a city to another. All the roads connecting the cities in Aizu have a row of cherry trees along their routes. For enhancing the cherry-viewing experience, a proposal was made to modify the road network so that a tourist can travel around all the roads. To achieve this target, it was decided to construct several one-way roads, each connecting two cities and abiding by the following rules. The newly constructed road is for one-way traffic Starting from any city, a tourist is able to make a roundtrip and return to the city, whereby he/she drives all the roads exhaustively, including the newly constructed ones. Multiple passages of some of the roads are allowed. You, as a tourism promotion officer, are assigned with the task of writing a program for the road construction project. Write a program to determine the minimum number of roads to be constructed given the road network information in Aizu. Input The input is given in the following format. N M s_1 t_1 s_2 t_2 : s_M t_M The first line provides the number of cities N (1 ≀ N ≀ 10 4 ) and roads M (0 ≀ M ≀ 10 5 ). Each of the subsequent M lines provides the numbers assigned to start and destination cities for the i -th road: s_i , t_i (0 ≀ s_i , t_i ≀ N -1) , where s_i ≠ t_i . (no duplicate appearance of a road) Output Output the minimum number of roads to be newly constructed. Sample Input 1 6 7 0 2 2 1 1 0 2 3 4 3 4 5 5 4 Sample Output 1 2 Sample Input 2 6 9 0 2 2 1 1 0 2 3 4 3 4 5 5 4 5 2 3 4 Sample Output 2 0
39,149
A: IPアドレス (Internet Protocol Address) 問題 数字列が 1 ぀䞎えられるので、IPv4のIPアドレスずしお有効な区切り方の個数を求めよ。 ただし、IPv4のIPアドレスずしお有効な区切り方は以䞋の通りである。 数字列は 4 ぀に区切られ、区切られた各列に぀いお以䞋をすべお満たす。 空列でない。 10 進数ずしお読むず 0 以䞊 255 以䞋の敎数である。 その数が 0 ならば、その列は 0 である 00 などは認められない。 その数が 0 でないならば、先頭の数字は 0 でない 01 などは認められない。 入力圢匏 S 制玄 4 \leq |S| \leq 12 S に含たれる文字は数字のみである。 出力圢匏 有効な区切り方の通り数を䞀行に出力せよ。 入力䟋1 123456789 出力䟋1 1 有効な区切り方は 123.45.67.89 の 1 通りである。 入力䟋2 768426 出力䟋2 6
39,150
Tally Counters A number of tally counters are placed in a row. Pushing the button on a counter will increment the displayed value by one, or, when the value is already the maximum, it goes down to one. All the counters are of the same model with the same maximum value. Fig. D-1 Tally Counters Starting from the values initially displayed on each of the counters, you want to change all the displayed values to target values specified for each. As you don't want the hassle, however, of pushing buttons of many counters one be one, you devised a special tool. Using the tool, you can push buttons of one or more adjacent counters, one push for each, in a single operation. You can choose an arbitrary number of counters at any position in each operation, as far as they are consecutively lined up. How many operations are required at least to change the displayed values on counters to the target values? Input The input consists of multiple datasets, each in the following format. n m a 1 a 2 ... a n b 1 b 2 ... b n Each dataset consists of 3 lines. The first line contains n (1 ≀ n ≀ 1000) and m (1 ≀ m ≀ 10000), the number of counters and the maximum value displayed on counters, respectively. The second line contains the initial values on counters, a i (1 ≀ a i ≀ m ), separated by spaces. The third line contains the target values on counters, b i (1 ≀ b i ≀ m ), separated by spaces. The end of the input is indicated by a line containing two zeros. The number of datasets does not exceed 100. Output For each dataset, print in a line the minimum number of operations required to make all of the counters display the target values. Sample Input 4 5 2 3 1 4 2 5 5 2 3 100 1 10 100 1 10 100 5 10000 4971 7482 1238 8523 1823 3287 9013 9812 297 1809 0 0 Output for the Sample Input 4 0 14731
39,151
Usoperanto Usoperanto is an artificial spoken language designed and regulated by Usoperanto Academy. The academy is now in study to establish Strict Usoperanto, a variation of the language intended for formal documents. In Usoperanto, each word can modify at most one other word, and modifiers are always put before modifiees. For example, with a noun uso ("truth") modified by an adjective makka ("total"), people say makka uso , not uso makka . On the other hand, there have been no rules about the order among multiple words modifying the same word, so in case uso is modified by one more adjective beta ("obvious"), people could say both makka beta uso and beta makka uso . In Strict Usoperanto, the word order will be restricted according to modification costs . Words in a phrase must be arranged so that the total modification cost is minimized. Each pair of a modifier and a modifiee is assigned a cost equal to the number of letters between the two words; the total modification cost is the sum of the costs over all modifier-modifiee pairs in the phrase. For example, the pair of makka and uso in a phrase makka beta uso has the cost of 4 for beta (four letters). As the pair of beta and uso has no words in between and thus the cost of zero, makka beta uso has the total modification cost of 4. Similarly beta makka uso has the total modification cost of 5. Applying the "minimum total modification cost" rule, makka beta uso is preferred to beta makka uso in Strict Usoperanto. Your mission in this problem is to write a program that, given a set of words in a phrase, finds the correct word order in Strict Usoperanto and reports the total modification cost. Input The format of the input is as follows. N M 0 L 0 ... M N-1 L N-1 The first line contains an integer N ( 1 ≀ N ≀ 10 6 ). N is the number of words in a phrase. Each of the following N lines contains two integers M i ( 1 ≀ M i ≀ 10 ) and L i ( -1 ≀ L i ≀ N - 1 , L i ≠ i ) describing the i -th word ( 0 ≀ i ≀ N-1 ). M i is the number of the letters in the word. L i specifies the modification: L i = -1 indicates it does not modify any word; otherwise it modifies the L i -th word. Note the first sample input below can be interpreted as the uso - beta - makka case. Output Print the total modification cost. Sample Input 1 3 3 -1 4 0 5 0 Output for the Sample Input 1 4 Sample Input 2 3 10 -1 10 0 10 1 Output for the Sample Input 2 0 Sample Input 3 4 1 -1 1 0 1 1 1 0 Output for the Sample Input 3 1
39,152
階箚 ボクシングは䜓重によっお階玚が分けられおいたす。䜓重を読み蟌んで、その階玚を出力するプログラムを䜜成しおください。階玚ず䜓重の関係は以䞋の衚のずおりずしたす。 階箚 䜓重kg light fly 48.00kg 以䞋 fly 48.00kg 超 51.00kg 以䞋 bantam 51.00kg 超 54.00kg 以䞋 feather 54.00kg 超 57.00kg 以䞋 light 57.00kg 超 60.00kg 以䞋 light welter 60.00kg 超 64.00kg 以䞋 welter 64.00kg 超 69.00 kg 以䞋 light middle 69.00kg 超 75.00 kg 以䞋 middle 75.00kg 超 81.00 kg 以䞋 light heavy 81.00kg 超 91.00 kg 以䞋 heavy 91.00kg 超 Input 入力は耇数のデヌタセットからなりたす。各デヌタセットずしお、䜓重を衚す぀の実数 w (40 ≀ w ≀ 150) が行に䞎えられたす。デヌタセットの数は 50 を超えたせん。 Output デヌタセットごずに、察応する階玚を行に出力したす。 Sample Input 60.2 70.2 48.0 80.2 Output for the Sample Input light welter light middle light fly middle
39,153
Problem Statement Your friend, Tatsumi, is a producer of Immortal Culture Production in Chiba (ICPC). His company is planning to form a zombie rock band named Gray Faces and cheer Chiba Prefecture up. But, unfortunately, there is only one zombie in ICPC. So, Tatsumi decided to release the zombie on a platform of Soga station to produce a sufficient number of zombies. As you may know, a zombie changes a human into a new zombie by passing by the human. In other words, a human becomes a zombie when the human and a zombie are at the same point. Note that a zombie who used to be a human changes a human into a zombie too. The platform of Soga station is represented by an infinitely long line, and Tatsumi will release a zombie at a point with coordinate $x_Z$. After the release, the zombie will start walking in the positive direction at $v_Z$ per second. If $v_Z$ is negative, the zombie will walk in the negative direction at $|v_Z|$ per second. There are $N$ humans on the platform. When Tatsumi releases the zombie, the $i$-th human will be at a point with coordinate $x_i$ and will start walking in the positive direction at $v_i$ per second. If $v_i$ is negative, the human will walk in the negative direction at $|v_i|$ per second as well as the zombie. For each human on the platform, Tatsumi wants to know when the human becomes a zombie. Please help him by writing a program that calculates a time when each human on the platform becomes a zombie. Input The input consists of a single test case in the following format. $N$ $x_Z$ $v_Z$ $x_1$ $v_1$ $\vdots$ $x_N$ $v_N$ The first line consists of an integer $N \, (1 \le N \le 2 \times 10^5)$ which is the number of humans on a platform of Soga station. The second line consists of two integers $x_Z \, (-10^9 \le x_Z \le 10^9)$ and $v_Z \, (-10^9 \le v_Z \le 10^9)$ separated by a space, where $x_Z$ is an initial position of a zombie Tatsumi will release and $v_Z$ is the velocity of the zombie. The $i$-th line in the following $N$ lines contains two integers $x_i \, (-10^9 \le x_i \le 10^9)$ and $v_i \, (-10^9 \le v_i \le 10^9)$ separated by a space, where the $x_i$ is an initial position of the $i$-th human and $v_i$ is the velocity of the human. There is no human that shares their initial position with the zombie. In addition, initial positions of the humans are different from each other. Output The output consists of $N$ lines. In the $i$-th line, print how many seconds it will take for the $i$-th human to become a zombie. If the $i$-th human will never become a zombie, print $-1$ instead. The answer will be considered as correct if the values output have an absolute or relative error less than $10^{-9}$. Examples Input Output 6 3 1 -5 0 5 0 -4 -3 0 -2 6 -3 2 -1 3.66666666666667 2.00000000000000 -1 6.00000000000000 0.75000000000000 2.00000000000000 5 31415 -926 5358 979 323846 26 -433832 7950 288 -4 -1971 -69 13.67821522309711 95.61812216052499 52.41629112212708 33.76030368763558 38.95682613768962
39,154
決たりごずの倚いゞム むヅア村のスポヌツゞムには$1$から$N$たでの番号が付いた$N$台のトレヌニング機噚がありたす。トレヌニング機噚を回利甚するには、その機噚の番号が曞かれたチケットが枚必芁です。トレヌニング機噚を回利甚したずきの消費カロリヌは、機噚ごずに決たっおいたす。 このスポヌツゞムのチケットを䜕枚かもらったアツシ君は、運動䞍足解消のためにゞムに行きたした。このゞムでは、利甚者が運動をやりすぎお䜓を痛めないように、機噚の利甚回数にルヌルがありたす。たずえば、「番の機噚は番の機噚よりも回以䞊倚く䜿っおはいけたせん」ずいうようなルヌルです。機噚を䜿う人は、ルヌルを守っお機噚を利甚しなければなりたせん。 アツシ君は、もらったチケットを䜿っお、ルヌルで蚱される範囲でなるべく倚くのカロリヌを消費したいず思っおいたす。 もらったチケットずそれぞれの機噚の情報が䞎えられたずき、ルヌルを守ったずきの消費カロリヌの最倧倀を求めるプログラムを䜜成しなさい。 入力 入力は以䞋の圢匏で䞎えられる。 $N$ $R$ $t_1$ $e_1$ $t_2$ $e_2$ : $t_N$ $e_N$ $a_1$ $b_1$ $c_1$ $a_2$ $b_2$ $c_2$ : $a_R$ $b_R$ $c_R$ 行目にトレヌニング機噚の台数$N$ ($1 \leq N \leq 100,000$)ずルヌルの数$R$ ($0 \leq R \leq 100,000$)が䞎えられる。続く$N$行に、$i$番の機噚に぀いお、アツシ君がもらったチケットの枚数$t_i$ ($1 \leq t_i \leq 200,000$)ずその機噚を䜿ったずきの消費カロリヌ$e_i$ ($0 \leq e_i \leq 100,000$)が䞎えられる。続く$R$行に、「$a_i$番の機噚は$b_i$番の機噚よりも$c_i$回以䞊倚く䜿っおはいけたせん」ずいうルヌルを衚す数$a_i$ ($1 \leq a_i \leq N$)、$b_i$ ($1 \leq b_i \leq N$)、$c_i$ ($1 \leq c_i \leq 100,000$)が䞎えられる。 入力は以䞋の制玄を満たす。 同じ機噚のペアに察するルヌルは床しか䞎えられない($i \ne j$なら$a_i \ne a_j$たたは$b_i \ne b_j$)。 同じ機噚自身に察するルヌルは䞎えられない($a_i \ne b_i$)。 出力 消費カロリヌの最倧倀を行に出力する。 入出力䟋 入力䟋 3 2 5 1 10 4 6 2 2 1 3 3 2 1 出力䟋 45 もらったチケットを䜿っお、ルヌルを守っお䜿える各機噚の最倧の回数は、$1$番が$5$回、$2$番が$7$回、$3$番が$6$回なので、消費カロリヌの最倧倀は$5 \times 1 + 7 \times 4 + 6 \times 2 = 45$ずなる。 入力䟋 4 5 5 1 6 2 2 3 7 1 1 2 4 2 1 3 1 3 2 3 2 3 3 4 2 出力䟋 26 入力䟋 1 0 200000 100000 出力䟋 20000000000
39,155
Bit Flag A state with $n$ flags of ON or OFF can be represented by a sequence of bits where $0, 1, ..., n-1$ -th flag corresponds to 1 (ON) or 0 (OFF). The state can be managed by the corresponding decimal integer, because the sequence of bits is a binary representation where each bit is 0 or 1. Given a sequence of bits with 64 flags which represent a state, perform the following operations. Note that each flag of the bits is initialized by OFF. test(i): Print 1 if $i$-th flag is ON, otherwise 0 set(i): Set $i$-th flag to ON clear(i): Set $i$-th flag to OFF flip(i): Inverse $i$-th flag all: Print 1 if all flags are ON, otherwise 0 any: Print 1 if at least one flag is ON, otherwise 0 none: Print 1 if all flags are OFF, otherwise 0 count: Print the number of ON flags val: Print the decimal value of the state Input The input is given in the following format. $q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given in the following format: 0 $i$ or 1 $i$ or 2 $i$ or 3 $i$ or 4 or 5 or 6 or 7 or 8 The first digit 0 , 1 ,..., 8 represents the operation test(i), set(i), clear(i), flip(i), all, any, none, count or val respectively. Output Print the result in a line for each test, all, any, none, count and val operation. Constraints $1 \leq q \leq 200,000$ $0 \leq i < 64$ Sample Input 1 14 1 0 1 1 1 2 2 1 0 0 0 1 0 2 0 3 3 3 4 5 6 7 8 Sample Output 1 1 0 1 0 0 1 0 3 13
39,156
Score : 900 points Problem Statement Mole decided to live in an abandoned mine. The structure of the mine is represented by a simple connected undirected graph which consists of N vertices numbered 1 through N and M edges. The i -th edge connects Vertices a_i and b_i , and it costs c_i yen (the currency of Japan) to remove it. Mole would like to remove some of the edges so that there is exactly one path from Vertex 1 to Vertex N that does not visit the same vertex more than once. Find the minimum budget needed to achieve this. Constraints 2 \leq N \leq 15 N-1 \leq M \leq N(N-1)/2 1 \leq a_i, b_i \leq N 1 \leq c_i \leq 10^{6} There are neither multiple edges nor self-loops in the given graph. The given graph is connected. Input Input is given from Standard Input in the following format: N M a_1 b_1 c_1 : a_M b_M c_M Output Print the answer. Sample Input 1 4 6 1 2 100 3 1 100 2 4 100 4 3 100 1 4 100 3 2 100 Sample Output 1 200 By removing the two edges represented by the red dotted lines in the figure below, the objective can be achieved for a cost of 200 yen. Sample Input 2 2 1 1 2 1 Sample Output 2 0 It is possible that there is already only one path from Vertex 1 to Vertex N in the beginning. Sample Input 3 15 22 8 13 33418 14 15 55849 7 10 15207 4 6 64328 6 9 86902 15 7 46978 8 14 53526 1 2 8720 14 12 37748 8 3 61543 6 5 32425 4 11 20932 3 12 55123 8 2 45333 9 12 77796 3 9 71922 12 15 70793 2 4 25485 11 6 1436 2 7 81563 7 11 97843 3 1 40491 Sample Output 3 133677
39,157
D - カヌペット 総合研究7号通の匕越しに䌎い研究宀にカヌペットを敷くこずになった この問題では研究宀を䞊から芋たずきの床を二次元平面䞊の倚角圢ずみなす 床の圢状を衚す芁玠数 N の数列 \{a_i\} が䞎えられる R_i を巊䞋の座暙が (i,0) で右䞊の座暙が (i+1,a_i) である各蟺がx軞たたはy軞に平行な長方圢の境界及び内郚領域ずする このずき床を衚す倚角圢は R_1 ∪ R_2 ∪ R_3 ∪ 
 ∪ R_N によっお衚される カヌペットは長方圢であればどんな倧きさのものを䜕枚でも甚意するこずができる 以䞋の条件を満たすようにカヌペットを配眮し研究宀の床を党お芆い぀くしたい カヌペットは研究宀からはみ出しおはいけない カヌペットはいくらでも重ねお敷くこずが可胜であるこのずきカヌペットの厚さは無芖する カヌペットを切り離しお利甚するこずはできない カヌペットは各蟺がx軞たたはy軞に平行になるように配眮しなければならない 研究宀の床を芆い尜くすために必芁なカヌペットの最小数を求めよ 図D-1. 入力䟋1の床 図D-2. 入力䟋2の床 入力圢匏 入力は以䞋の圢匏で䞎えられる N a_1 
 a_N 出力圢匏 研究宀の床を芆い尜くすために必芁なカヌペットの最小数を行に出力せよ 制玄 1 \leq N \leq 2 \times 10^5 1 \leq a_i \leq 10^9 入力倀はすべお敎数である 入出力䟋 入力䟋1 3 1 2 1 出力䟋1 2 入力䟋2 10 1 2 2 1 3 4 3 1 2 2 出力䟋2 5
39,158
Score : 700 points Problem Statement You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N , and the i -th edge connects Vertex A_i and Vertex B_i . Takahashi will assign one of the two possible directions to each of the edges in the graph to make a directed graph. Determine if it is possible to make a directed graph with an even number of edges going out from every vertex. If the answer is yes, construct one such graph. Notes An undirected graph is said to be simple when it contains no self-loops or multiple edges. Constraints 2 \leq N \leq 10^5 N-1 \leq M \leq 10^5 1 \leq A_i,B_i \leq N (1\leq i\leq M) The given graph is simple and connected. Input Input is given from Standard Input in the following format: N M A_1 B_1 : A_M B_M Output If it is impossible to assign directions to satisfy the requirement, print -1 . Otherwise, print an assignment of directions that satisfies the requirement, in the following format: C_1 D_1 : C_M D_M Here each pair ( C_i , D_i ) means that there is an edge directed from Vertex C_i to Vertex D_i . The edges may be printed in any order. Sample Input 1 4 4 1 2 2 3 3 4 4 1 Sample Output 1 1 2 1 4 3 2 3 4 After this assignment of directions, Vertex 1 and 3 will each have two outgoing edges, and Vertex 2 and 4 will each have zero outgoing edges. Sample Input 2 5 5 1 2 2 3 3 4 2 5 4 5 Sample Output 2 -1
39,159
Score : 900 points Problem Statement There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them. Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge. Snuke has already decided K of the pairs, and the i -th of them is a pair of Point A_i and Point B_i . He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7 . Constraints 1 \leq N \leq 300 0 \leq K \leq N 1 \leq A_i,B_i \leq 2N A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct. All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 B_1 A_2 B_2 : A_K B_K Output Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs. Sample Input 1 2 0 Sample Output 1 5 There are three ways to draw line segments, as shown below, and the number of the connected parts for these ways are 2 , 2 and 1 , respectively. Thus, the answer is 2+2+1=5 . Sample Input 2 4 2 5 2 6 1 Sample Output 2 6 Sample Input 3 20 10 10 18 11 17 14 7 4 6 30 28 19 24 29 22 25 32 38 34 36 39 Sample Output 3 27087418
39,160
Problem C: ! You are one of ICPC participants and in charge of developing a library for multiprecision numbers and radix conversion. You have just finished writing the code, so next you have to test if it works correctly. You decided to write a simple, well-known factorial function for this purpose: Your task is to write a program that shows the number of trailing zeros when you compute M ! in base N , given N and M . Input The input contains multiple data sets. Each data set is described by one line in the format below: N M where N is a decimal number between 8 and 36 inclusive, and M is given in the string repre- sentation in base N . Exactly one white space character appears between them. The string representation of M contains up to 12 characters in base N . In case N is greater than 10, capital letters A, B, C, ... may appear in the string representation, and they represent 10, 11, 12, ..., respectively. The input is terminated by a line containing two zeros. You should not process this line. Output For each data set, output a line containing a decimal integer, which is equal to the number of trailing zeros in the string representation of M ! in base N . Sample Input 10 500 16 A 0 0 Output for the Sample Input 124 2
39,161
Score : 300 points Problem Statement There is an empty array. The following N operations will be performed to insert integers into the array. In the i -th operation (1≀i≀N) , b_i copies of an integer a_i are inserted into the array. Find the K -th smallest integer in the array after the N operations. For example, the 4 -th smallest integer in the array \{1,2,2,3,3,3\} is 3 . Constraints 1≀N≀10^5 1≀a_i,b_i≀10^5 1≀K≀b_1
+
b_n All input values are integers. Input Input is given from Standard Input in the following format: N K a_1 b_1 : a_N b_N Output Print the K -th smallest integer in the array after the N operations. Sample Input 1 3 4 1 1 2 2 3 3 Sample Output 1 3 The resulting array is the same as the one in the problem statement. Sample Input 2 10 500000 1 100000 1 100000 1 100000 1 100000 1 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 Sample Output 2 1
39,162
Score : 500 points Problem Statement For an integer n not less than 0 , let us define f(n) as follows: f(n) = 1 (if n < 2 ) f(n) = n f(n-2) (if n \geq 2 ) Given is an integer N . Find the number of trailing zeros in the decimal notation of f(N) . Constraints 0 \leq N \leq 10^{18} Input Input is given from Standard Input in the following format: N Output Print the number of trailing zeros in the decimal notation of f(N) . Sample Input 1 12 Sample Output 1 1 f(12) = 12 × 10 × 8 × 6 × 4 × 2 = 46080 , which has one trailing zero. Sample Input 2 5 Sample Output 2 0 f(5) = 5 × 3 × 1 = 15 , which has no trailing zeros. Sample Input 3 1000000000000000000 Sample Output 3 124999999999999995
39,163
Score : 100 points Problem Statement X and A are integers between 0 and 9 (inclusive). If X is less than A , print 0 ; if X is not less than A , print 10 . Constraints 0 \leq X, A \leq 9 All values in input are integers. Input Input is given from Standard Input in the following format: X A Output If X is less than A , print 0 ; if X is not less than A , print 10 . Sample Input 1 3 5 Sample Output 1 0 3 is less than 5 , so we should print 0 . Sample Input 2 7 5 Sample Output 2 10 7 is not less than 5 , so we should print 10 . Sample Input 3 6 6 Sample Output 3 10 6 is not less than 6 , so we should print 10 .
39,164
Problem G: Combine Two Elements Problem $N$個の非負敎数のペア$(a_i, b_i)$ず非負敎数$A$, $B$ が䞎えられる。 以䞋のいずれかの操䜜をできるだけたくさん行いたい。 $|a_i - b_i| \leq A$ たたは $B \leq |a_i - b_i| \leq 2A$を満たす芁玠$i$を取り出し、削陀する $|(a_i + a_j) - (b_i + b_j)| \leq A$ たたは $B \leq |(a_i + a_j) - (b_i + b_j)| \leq 2A$ を満たす芁玠$i$ず芁玠$j$ ($i \neq j$)の組を取り出し、削陀する 最倧の操䜜回数を求めよ。 Input 入力は以䞋の圢匏で䞎えられる。 $N$ $A$ $B$ $a_1$ $b_1$ $a_2$ $b_2$ ... $a_N$ $b_N$ 入力はすべお敎数で䞎えられる。 1行目に$N$,$A$,$B$が空癜区切りで䞎えられる。 2行目以降の$N$行に$i$個目のペア$a_i$ず$b_i$($1 \leq i \leq N$)が空癜区切りで䞎えられる。 Constraints 入力は以䞋の条件を満たす。 $1 \leq N \leq 800 $ $0 \leq A, B \leq 10^5 $ $0 \leq a_i, b_i \leq 10^5$ $A \leq B$ か぀ $B \leq 2A$ Output 最倧の操䜜回数を1行に出力せよ。 Sample Input 1 5 3 5 7 2 13 1 1 1 2 9 2 4 Sample Output 1 4 (7,2)を遞んで削陀する。 (1,1)を遞んで削陀する。 (2,4)を遞んで削陀する。 (13, 1)ず(2, 9)を遞んで削陀する。 以䞊のように操䜜するず回操䜜するこずができ、これが最倧ずなる。 Sample Input 2 10 7 12 34 70 36 0 12 50 76 46 33 45 61 21 0 1 24 3 98 41 23 84 Sample Output 2 5
39,166
Score : 100 points Problem Statement In this problem, you should process T testcases. For each testcase, you are given four integers N, M, A, B . Calculate \sum_{i = 0}^{N - 1} floor((A \times i + B) / M) . Constraints 1 \leq T \leq 100,000 1 \leq N, M \leq 10^9 0 \leq A, B < M Input Input is given from Standard Input in the following format: T N_0 M_0 A_0 B_0 N_1 M_1 A_1 B_1 : N_{T - 1} M_{T - 1} A_{T - 1} B_{T - 1} Output Print the answer for each testcase. Sample Input 1 5 4 10 6 3 6 5 4 3 1 1 0 0 31415 92653 58979 32384 1000000000 1000000000 999999999 999999999 Sample Output 1 3 13 0 314095480 499999999500000000
39,167
入堎料金 アむヅ枩泉には、济堎ずプヌルがあり、济堎を利甚するには入济刞を、プヌルを利甚するにはプヌル刞をそれぞれ買う必芁がありたす。これらの刞の倀段は、日によっお倉わるかもしれたせん。たた、アむヅ枩泉には以䞋のようなルヌルがありたす。 刞は賌入圓日回の利甚に限り有効。 「入济刞枚以䞊か぀プヌル刞枚以䞊」でたずめお買うず、すべおの刞が割匕になる。 枩泉奜きの貞吉さんずその友人たちは、アむヅ枩泉に毎日のように通っおいたす。圌らは皆気たぐれで、日によっお䜿う枚数は異なりたす。アむヅ枩泉には割匕ルヌルがあるため、グルヌプで協力しお䞊手に買えば、合蚈料金を安く枈たすこずができるかもしれたせん。 入济刞ずプヌル刞の料金、䜿甚する入济刞の枚数ずプヌル刞の枚数が入力ずしお䞎えられたずき、最も安い合蚈料金を出力するプログラムを䜜成しおください。ただし、䜿甚する枚数より倚く刞を買うこずで合蚈料金が安くなるなら、買った刞すべおを䜿わなくずもよいものずしたす。 入力 入力は以䞋の圢匏で䞎えられる。 N x 1 y 1 b 1 p 1 x 2 y 2 b 2 p 2 : x N y N b N p N 行目の N (1 ≀ N ≀ 365)は料金を蚈算したい日数である。続く N 行に、 i 日目の入济刞の料金 x i (100 ≀ x i ≀ 1000)、プヌル刞の料金 y i (100 ≀ y i ≀ 1000)、䜿甚する入济刞の枚数 b i (0 ≀ b i ≀ 6)、䜿甚するプヌル刞の枚数 p i (0 ≀ p i ≀ 6)が䞎えられる。入济刞ずプヌル刞共に料金は50円刻みである。 出力 それぞれの日に぀いお最も安い合蚈料金を行に出力する。 入力䟋 1 2 100 100 1 1 1000 500 5 2 出力䟋 1 200 4800 入力䟋 2 5 500 500 1 1 500 500 5 2 1000 100 0 6 1000 100 6 0 100 1000 0 0 出力䟋 2 1000 2800 600 4960 0
39,168
Problem B: Square Coins People in Silverland use square coins. Not only they have square shapes but also their values are square numbers. Coins with values of all square numbers up to 289 (= 17 2 ), i.e., 1-credit coins, 4-credit coins, 9-credit coins, ..., and 289-credit coins, are available in Silverland. There are four combinations of coins to pay ten credits: ten 1-credit coins, one 4-credit coin and six 1-credit coins, two 4-credit coins and two 1-credit coins, and one 9-credit coin and one 1-credit coin. Your mission is to count the number of ways to pay a given amount using coins of Silverland. Input The input consists of lines each containing an integer meaning an amount to be paid, followed by a line containing a zero. You may assume that all the amounts are positive and less than 300. Output For each of the given amount, one line containing a single integer representing the number of combinations of coins should be output. No other characters should appear in the output. Sample Input 2 10 30 0 Output for the Sample Input 1 4 27
39,169
A: 䞞付け 問題 AORむカちゃんはテストに合栌するため勉匷しおいたす。 AORむカちゃんは、 $N$ 問、問題を解きたした。 その埌、解いた問題の䞞付けを以䞋の手順で行いたす。 解答の正誀を確認する。 解答が正しい堎合はマル印、誀っおいた堎合はバツ印を解答甚玙に曞き蟌む。 解答が $2$ 問連続で誀りであるずわかった瞬間、テストに䞍合栌になっおしたう恐怖から、AORむカちゃんは倱神しおしたいたす。そしお、それ以降䞞付けを行うこずはできたせん。 倱神は手順 $1$ ず $2$ の間で起こりたす。 AORむカちゃんが解いた問題の数を衚す敎数 $N$ ず、解答の正誀を衚した長さ $N$ の文字列 $S$ が䞎えられたす。 文字列は 'o' ず 'x' からなり、 'o' は解答が正しく、 'x' は解答が誀りであるこずを衚しおいたす。 $i$ 文字目が $i$ 問目の正誀を衚しおおり、AORむカちゃんは $1$ 問目から順番に䞞付けを行いたす。 AORむカちゃんが正誀を曞き蟌めた問題数を出力しおください。 制玄 $1 \leq N \leq 10^5$ 入力圢匏 入力は以䞋の圢匏で䞎えられる。 $N$ $S$ 出力 AORむカちゃんが正誀を曞き蟌めた問題数を $1$ 行で出力せよ。たた、末尟に改行も出力せよ。 サンプル サンプル入力 1 3 oxx サンプル出力 1 2 $3$ 問目の手順 $1$ を行うず倱神するため、手順 $2$ は行えたせん。 サンプル入力 2 8 oxoxoxox サンプル出力 2 8 サンプル入力 3 4 xxxx サンプル出力 3 1
39,170
Problem C: Midnight Teatime ICPCの囜内予遞に備えお問題を解いおいた僕は, その日3぀目のAcceptを貰ったずころでキヌボヌドを叩く手を止めた. 時蚈を芋れば, もう日付が倉わろうかずいう時刻だ. 玅茶ずお菓子で䞀服しお, 今日はもう寝るこずにしよう. そう思っお僕はキッチンぞず向かった. ダヌゞリンのかぐわしい銙りがキッチンを満たした頃, 効がやっおきた. 受隓生である圌女は今日もこんな時間たで真面目に勉匷しおいたようだ. 僕は圌女を誘っお, 小さな深倜のお茶䌚を開くこずにした. 郜合の良いこずに, キッチンには4぀のお菓子があった. これをただ2人で分けるのも぀たらないので, 僕はこのお菓子を賭けお簡単なゲヌムをしないかず提案した. そのゲヌムの内容を説明しよう. たず最初に, 僕はどのノヌドも0個たたは2個の子を持぀ような二分朚を曞く. 次に, その朚の葉に圓たる(぀たり, 子を持たない)ノヌドに, それぞれ S = {a, b, c, d} の任意の郚分集合を曞き蟌む. 4぀のお菓子は, それぞれ a, b, c, d に察応する. 最埌に効は, 朚の内郚接点(぀たり、2぀の子を持぀)ノヌドに 'A', 'O', 'X' のいずれかの文字を曞き蟌む. 効は, 朚の根にあたるノヌドが瀺すお菓子を埗る. ただし, ノヌドが瀺すお菓子ずは, そのノヌドが葉であれば, そこに曞かれおいるお菓子 そのノヌドが内郚接点であれば, そのノヌドに曞かれた文字が A のずき、 sl ず sr の積集合 そのノヌドに曞かれた文字が O のずき、 sl ず sr の和集合 そのノヌドに曞かれた文字が X のずき、 sl ず sr の察称差 のこずである. ここで、sl はそのノヌドの巊の子ノヌドが瀺すお菓子, sr はそのノヌドの右の子ノヌドが瀺すお菓子を指す. 2぀の集合の察称差は, どちらか䞀方の集合にのみ含たれるような元から成る集合である. このゲヌムに効は乗っおきた. それどころか, お菓子を4぀ずも巻き䞊げおやろうず目を茝かせおいる. 出来れば僕の分も残しおおいおくれるず嬉しいのだけれども. 僕が曞いた朚に察しお, 効が党おのお菓子を埗られるような内郚接点の曞き蟌み方は, いったい䜕通りあるだろうか Input 入力ファむルは、耇数のデヌタセットを含む. デヌタセットの最初の行には, 朚の情報が䞎えられる. 朚の蚘述は, "(" <巊郚分朚の蚘述> <䞀぀のスペヌス> <右郚分朚の蚘述> ")" たたは, <䞀぀の数字> のどちらかの圢匏を取る. 前者が内郚接点の蚘述, 埌者が葉の蚘述である. 次の行は1぀の敎数 N (N < 10) を含み, 続く N 行に葉に曞き蟌たれた郚分集合の情報が䞎えられる. 郚分集合の情報は, 空癜文字で区切られた4぀の数字で衚される. 4぀の文字はそれぞれ, その郚分集合が a, b, c, d を含むかどうかを衚す. 含むならば 1 が、含たないならば 0 が䞎えられる. 葉の蚘述ずしお䞎えられた数字が n であるずき、その葉に曞き蟌たれた郚分集合はこれら N 個のうち n 番目のものである. 1 ≀ n ≀ N ず仮定しおよい. 䞎えられる朚は、最倧 8 個の内郚接点を含む. 朚の蚘述の代わりに䞎えられる "END" ずいう文字列が, 入力の終わりを衚す. Output 各デヌタセットに぀いお, 効が党おのお菓子を埗られるような内郚接点の曞き蟌み方の数を、䞀行に出力せよ. Sample Input (1 2) 2 0 1 0 1 1 0 1 0 ((1 2) 3) 3 1 1 0 0 1 0 1 0 0 0 0 1 END Output for the Sample Input 2 2
39,171
Network Reliability An undirected graph is given. Each edge of the graph disappears with a constant probability. Calculate the probability with which the remained graph is connected. Input The first line contains three integers N ( 1 \leq N \leq 14 ), M ( 0 \leq M \leq 100 ) and P ( 0 \leq P \leq 100 ), separated by a single space. N is the number of the vertices and M is the number of the edges. P is the probability represented by a percentage. The following M lines describe the edges. Each line contains two integers v_i and u_i ( 1 \leq u_i, v_i \leq N ). ( u_i, v_i ) indicates the edge that connects the two vertices u_i and v_i . Output Output a line containing the probability with which the remained graph is connected. Your program may output an arbitrary number of digits after the decimal point. However, the absolute error should be 10^{-9} or less. Sample Input 1 3 3 50 1 2 2 3 3 1 Output for the Sample Input 1 0.500000000000 Sample Input 2 3 3 10 1 2 2 3 3 1 Output for the Sample Input 2 0.972000000000 Sample Input 3 4 5 50 1 2 2 3 3 4 4 1 1 3 Output for the Sample Input 3 0.437500000000
39,172
Dock to the Future You had long wanted a spaceship, and finally you bought a used one yesterday! You have heard that the most difficult thing on spaceship driving is to stop your ship at the right position in the dock. Of course you are no exception. After a dozen of failures, you gave up doing all the docking process manually. You began to write a simple program that helps you to stop a spaceship. First, you somehow put the spaceship on the straight course to the dock manually. Let the distance to the limit line be x [m], and the speed against the dock be v [m/s]. Now you turn on the decelerating rocket. Then, your program will control the rocket to stop the spaceship at the best position. Your spaceship is equipped with a decelerating rocket with n modes. When the spaceship is in mode- i (0 ≀ i < n ), the deceleration rate is a i [m/s 2 ]. You cannot re-accelerate the spaceship. The accelerating rocket is too powerful to be used during docking. Also, you cannot turn off-and-on the decelerating rocket, because your spaceship is a used and old one, once you stopped the rocket, it is less certain whether you can turn it on again. In other words, the time you turn off the rocket is the time you stop your spaceship at the right position. After turning on the deceleration rocket, your program can change the mode or stop the rocket at every sec- ond, starting at the very moment the deceleration began. Given x and v , your program have to make a plan of deceleration. The purpose and the priority of your program is as follows: Stop the spaceship exactly at the limit line. If this is possible, print “perfect”. If it is impossible, then stop the spaceship at the position nearest possible to the limit line, but before the line. In this case, print “good d ”, where d is the distance between the limit line and the stopped position. Print three digits after the decimal point. If it is impossible again, decelerate the spaceship to have negative speed, and print “try again”. If all of these three cases are impossible, then the spaceship cannot avoid overrunning the limit line. In this case, print “crash”. Input The first line of the input consists of a single integer c , the number of test cases. Each test case begins with a single integer n (1 ≀ n ≀ 10), the number of deceleration modes. The following line contains n positive integers a 0 , . . . , a n-1 (1 ≀ a i ≀ 100), each denoting the deceleration rate of each mode. The next line contains a single integer q (1 ≀ q ≀ 20), and then q lines follow. Each of them contains two positive integers x and v (1 ≀ x , v ≀ 100) defined in the problem statement. Output For each pair of x and v , print the result in one line. A blank line should be inserted between the test cases. Sample Input 1 3 2 4 6 4 10 100 2 3 10 6 7 6 Output for the Sample Input crash try again good 1.000 perfect
39,173
Problem E: Rooted Tree Game Problem 初期状態ずしお耇数の根付き朚が䞎えられる。これに察しAliceずBobはゲヌムを行う。ゲヌムは2人亀互に行い、先手がAliceで埌手がBobである。タヌンが回っおきたプレむダヌは以䞋の行動を取る。 æ ¹(芪を持たない頂点)を1぀遞択する。この頂点を S ずする。 S を根ずする根付き朚に含たれる頂点を遞択する(ここでは S も遞択可胜)。この頂点を T ずする。 S から T ぞの経路䞊にある頂点を、 S ず T も含めすべお削陀する。たた、削陀された頂点が端点であるような蟺もすべお削陀する。 タヌンが回っおきた時点で頂点がすべお削陀されおいた堎合、そのプレむダヌの負けずなる。 AliceずBobが垞に最適な行動を取る時、䞎えられた初期状態に察し、勝利するプレむダヌを刀定せよ。 以䞋の図は、プレむダヌの行動の䟋を瀺す。 Input 入力は以䞋の圢匏で䞎えられる。 N M p 1 p 2 : p M 1行目に、初期状態の頂点数 N ず蟺の数 M が空癜区切りで䞎えられる。この時、各頂点を衚す番号は1~ N である。次の M 行では、蟺の情報が䞎えられる。このうち i 行目では1぀の敎数 p i が䞎えられる。これは、頂点 p i から頂点 i ぞの蟺があるこずを衚す。蚀い換えるず、頂点 i の芪が頂点 p i であるこずを衚す。 Constraints 入力は以䞋の制玄を満たす。 1 ≀ N ≀ 1000 0 ≀ M < N i < p i ≀ N ( 1 ≀ i ≀ M ) Output 勝利するプレむダヌの名前(AliceたたはBob)を1行に出力せよ。 Sample Input1 6 3 4 4 5 Sample Output1 Alice Sample Input2 6 4 2 5 4 5 Sample Output2 Bob
39,174
H - 怍林 問題文 ずある倧孊の魔法孊郚の生埒であるK君は広倧な屋敷に䜏んでいる 屋敷の裏には林が広がっおいたが所々朚が生えおいない箇所があった このたたでは屋敷からの芋た目が悪いず感じたのでK君は朚が生えおいない箇所に朚を怍えるこずに決めた 魔法䜿いの卵であるK君は䜿い魔を召喚するこずが出来るので怍林の䜜業を䜿い魔にやらせようず考えた 林は H × W 個のセルを持぀長方圢領域ずみなすこずが出来る 各セルは朚が1本生えおいるか1本も生えおいないかのどちらかである K君はこの H × W 個のセルのどれか 1 ぀を指定しそこに䜿い魔を召喚する しかしK君はただ未熟者なので魔力を調節するこずが出来ず䜿い魔を召喚するずきは必ず 5 匹召喚しおしたう さらに悪いこずにK君が召喚する䜿い魔はひねくれもので蚪れたセルに朚が生えおいない堎合はそこに朚を怍えるが蚪れたセルに既に朚が生えおいる堎合はそのセルの朚を消しおしたう 召喚された䜿い魔のうちの 4 匹は指定されたセルを始点ずしお東西南北に散らばり1盎線䞊を進み1぀1぀セルを蚪れおいくこれらの䜿い魔は林の倖に出るず消える 残りの䜿い魔は指定されたセルのみを蚪問しその埌盎ちに消える より正確に蚀えばK君がセル (i, j) に䜿い魔を召喚するず i 行目か或いは j 列目にある H+W-1 個のセルに察し 朚が生えおいなければ朚が怍えられ朚が生えおいれば朚が消されるずいう操䜜が行われる 召喚には倚くの魔力を必芁ずするので出来るだけ少ない召喚回数で林を朚で芆い぀くしたい K君はどのセルに䜿い魔を召喚すれば最小の召喚回数で林を朚で芆い぀くすこずが出来るだろうか 入力圢匏 入力は以䞋の圢匏で䞎えられる H W a 1,1 ... a 1,W ... a H,1 ... a H,W a i,j が 1 ならセル (i,j) に朚が生えおいるこずを意味し, 0 なら生えおいないこずを意味する 出力圢匏 もし林を朚で芆い぀くすこずが䞍可胜なら1行に Impossible を出力せよ そうでなければ召喚回数を最小にするような召喚手順を以䞋の圢匏で H 行に出力せよ b 1,1 ... b 1,W ... b H,1 ... b H,W b i,j は 0 もしくは 1 でなければならず, 1 ならセル (i,j) に䜿い魔を召喚する事を意味し 0 なら召喚しない事を意味する 2回同じ堎所に䜿い魔を召喚しおも意味が無いこずに泚意せよ召喚回数を最小にするような召喚手順が耇数ある堎合はどれを出力しおも良い 制玄 2 ≀ HW ≀ 1000 HW は偶数 a i,j ∈ {0, 1} この問題の刀定には3 点分のテストケヌスのグルヌプが蚭定されおいる このグルヌプに含たれるテストケヌスは䞊蚘の制玄に加えお䞋蚘の制玄も満たす H × W ≀ 20 入出力䟋 入力䟋 1 4 4 0 0 0 1 0 1 1 0 0 1 1 0 1 0 0 0 出力䟋 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 セル (1,1) ずセル (4,4) に䜿い魔を召喚するこずで林を朚で芆い぀くすこずが出来る Writer: 田村和範 Tester: 花田裕䞀朗
39,175
Problem G: Entangled Tree The electronics division in Ishimatsu Company consists of various development departments for electronic devices including disks and storages, network devices, mobile phones, and many others. Each department covers a wide range of products. For example, the department of disks and storages develops internal and external hard disk drives, USB thumb drives, solid-state drives, and so on. This situation brings staff in the product management division difficulty categorizing these numerous products because of their poor understanding of computer devices. One day, a staff member suggested a tree-based diagram named a category diagram in order to make their tasks easier. A category diagram is depicted as follows. Firstly, they prepare one large sheet of paper. Secondly, they write down the names of the development departments on the upper side of the sheet. These names represent the start nodes of the diagram. Each start node is connected to either a single split node or a single end node (these nodes will be mentioned soon later). Then they write down a number of questions that distinguish features of products in the middle, and these questions represent the split nodes of the diagram. Each split node is connected with other split nodes and end nodes, and each line from a split node is labeled with the answer to the question. Finally, they write down all category names on the lower side, which represents the end nodes . The classification of each product is done like the following. They begin with the start node that corresponds to the department developing the product. Visiting some split nodes, they traces the lines down until they reach one of the end nodes labeled with a category name. Then they find the product classified into the resultant category. The visual appearance of a category diagram makes the diagram quite understandable even for non-geek persons. However, product managers are not good at drawing the figures by hand, so most of the diagrams were often messy due to many line crossings. For this reason, they hired you, a talented programmer, to obtain the clean diagrams equivalent to their diagrams. Here, we mean the clean diagrams as those with no line crossings. Your task is to write a program that finds the clean diagrams. For simplicity, we simply ignore the questions of the split nodes, and use integers from 1 to N instead of the category names. Input The input consists of multiple datasets. Each dataset follows the format below: N M Q split node info 1 split node info 2 ... split node info M query 1 query 2 ... query Q The first line of each dataset contains three integers N (1 ≀ N ≀ 100000), M (0 ≀ M ≀ N - 1), and Q (1 ≀ Q ≀ 1000, Q ≀ N ), representing the number of end nodes and split nodes, and the number of queries respectively. Then M lines describing the split nodes follow. Each split node is described in the format below: Y L label 1 label 2 . . . The first two integers, Y (0 ≀ Y ≀ 10 9 ) and L , which indicates the y -coordinate where the split node locates (the smaller is the higher) and the size of a label list. After that, L integer numbers of end node labels which directly or indirectly connected to the split node follow. This is a key information for node connections. A split node A is connected to another split node B if and only if both A and B refer (at least) one identical end node in their label lists, and the y-coordinate of B is the lowest of all split nodes referring identical end nodes and located below A. The split node is connected to the end node if and only if that is the lowest node among all nodes which contain the same label as the end node’s label. The start node is directly connected to the end node, if and only if the end node is connected to none of the split nodes. After the information of the category diagram, Q lines of integers follow. These integers indicate the horizontal positions of the end nodes in the diagram. The leftmost position is numbered 1. The input is terminated by the dataset with N = M = Q = 0, and this dataset should not be processed. Output Your program must print the Q lines, each of which denotes the label of the end node at the position indicated by the queries in the clean diagram. One blank line must follow after the output for each dataset. Sample Input 3 2 3 10 2 1 2 20 2 3 2 1 2 3 5 2 5 10 3 1 2 4 20 3 2 3 5 1 2 3 4 5 4 1 4 10 2 1 4 1 2 3 4 4 3 4 30 2 1 4 20 2 2 4 10 2 3 4 1 2 3 4 4 3 4 10 2 1 4 20 2 2 4 30 2 3 4 1 2 3 4 4 3 4 10 2 1 2 15 2 1 4 20 2 2 3 1 2 3 4 3 2 3 10 2 2 3 20 2 1 2 1 2 3 1 0 1 1 0 0 0 Output for the Sample Input 1 2 3 1 2 3 5 4 1 4 2 3 1 4 2 3 1 2 3 4 1 4 2 3 1 2 3 1
39,176
繰り返す呪文 叀代囜家むワシロを発芋し調査を進めた我々研究者は、぀いにむワシロの䞭心郚にある神殿を発芋した。神殿にはむワシロの神にささげた石版が保管されおいた。石版には、文章ず呪文が䞀぀ず぀、぀の文字列が曞かれおいた。 むワシロでは呪文が文章の䞭に、䜕回珟れるかが重芁な意味を持぀。ただし、呪文に含たれるすべおの文字が順番に、文章の䞭にずびずびで珟れるものも回珟れおいるず考える。䟋えば、文章が"abab" で、呪文が "ab" であれば、連続でないものも含めお "ab" は "abab" の䞭に回珟われおいる ab ab, ab ab , a ba b の通り。 文章ず呪文が䞎えられたずき、呪文が文章の䞭に䜕回珟れるかを出力するプログラムを䜜成せよ。 Input 入力は以䞋の圢匏で䞎えられる。 t b 行目に、石版に曞かれた文章を衚す文字列tが䞎えられる。行目に、石版に曞かれた呪文を衚す文字列bが䞎えられる。どちらの文字列も英小文字だけから成る、長さが1以䞊1000以䞋の文字列である。 Output 呪文が文章の䞭に䜕回珟れるかを行に出力する。ただし、出力すべき倀は非垞に倧きくなりうるので、代わりに 1,000,000,007 で割った䜙りを出力する。 Sample Input 1 abab ab Sample Output 1 3 Sample Input 2 aaaabaaaabaaaabaaaab aaaaa Sample Output 2 4368 Sample Input 3 data structure Sample Output 3 0
39,177
C: 成長する点 - Growing Point - 問題 粘菌コンピュヌタずいうものがある。 ある皮の粘菌には「逌を求め、逌ず逌の最短距離を぀なぐ圢に倉圢する」 ずいう性質がある。 これを利甚し、逌を「入力」、圢を「出力」ずみなしお コンピュヌタずしお利甚するこずができる。 今、二次元平面䞊に1぀の粘菌の拠点ず N 個の逌が存圚する。それぞれの逌には 1 から N たでの異なる番号が䞎えられ、拠点には番号0が䞎えられおいる。 この粘菌はある逌を食べるために、その逌ず最も近い拠点の最短距離を結ぶ管状に 成長し、食べた䜍眮に新たに拠点を圢成する。 新たに圢成した拠点は拠点を圢成する盎前に食べた逌ず同じ番号を持぀。 粘菌は拠点以倖の堎所から成長するこずはできない。 以降では、拠点ず逌を二次元平面䞊の点、管状に成長した粘菌を耇数の線分ずしお考える。 すべおの拠点ず線分からなる構造を粘菌網ず呌ぶ。 粘菌は1぀の逌を食べるために次のような操䜜を繰り返す。 ただ食べおいない逌の䞭で粘菌網に最も近い逌を遞ぶ。そのような逌が耇数存圚する堎合は番号が最も小さい逌を遞ぶ。 遞んだ逌ず最も近い拠点を遞ぶ。そのような拠点が耇数存圚する堎合は、最も拠点の番号が小さいものから取る。 遞んだ拠点ず逌を結ぶ線分を匕く。以降ではこのずき遞んだ逌も拠点ずしお扱う。 この粘菌は生きるために必芁な栄逊を取るのに M 個の逌を食べる必芁がある。 粘菌が M 個の逌を食べるたでに匕いたすべおの線分の長さの合蚈を求めよ。 たた、出力する倀は0.0001以䞋の誀差を含んでいおも良い。 以䞋の図では入力䟋2の粘菌の様子を瀺しおいる。 入力圢匏 N M X Y px_1 py_1 ... px_n py_n 1 行目には逌の数 N  1 \≀ N \≀ 5,000 、食べる逌の個数 M  1 \≀ M \≀ N 、番号0の拠点の座暙 X , Y  −5,000 \≀ X, Y \≀ 5,000 が敎数倀で䞎えられる。続く N 行には番号順に逌の座暙 px_i , py_i  1 \≀ i \≀ N が敎数倀で䞎えられる。 −5,000 \≀ px_i, py_i \≀ 5,000  たた、番号が異なる逌は異なる座暙に存圚し、それぞれの逌ず番号0の拠点の座暙は異なる。 出力圢匏 成長した距離の合蚈を1行で出力せよ。たた、出力する倀は0.0001以䞋の誀差を含んでいおも良い。 入力䟋1 2 2 0 0 3 3 4 0 出力䟋1 7.16227766017 最初は番号1の逌ず拠点の距離が $3\sqrt{2}$ ず番号2の逌ず拠点の距離が 4 なので 番号2の逌が遞ばれる。その埌番号1の逌ず粘菌網ずの距離が 3 になり、番号1の逌が遞ばれる。 入力䟋2 4 4 1 3 3 3 2 1 3 1 1 1 出力䟋2 6.2360679775 図のように逌1、2、4、3の順に粘菌は逌を食べおいく 入力䟋3 16 15 -4077 763 -2480 2841 -2908 -1096 676 -4080 -4988 -2634 3004 -1360 -2272 1773 -4344 -3631 -355 4426 -3740 3634 -3330 2191 -3423 -2999 -3438 2281 4754 -1500 -3440 -3873 -2089 -3419 1426 2793 出力䟋3 25349.9626798834
39,178
Patisserie ACM Amber Claes Maes, a patissier, opened her own shop last month. She decided to submit her work to the International Chocolate Patissier Competition to promote her shop, and she was pursuing a recipe of sweet chocolate bars. After thousands of trials, she finally reached the recipe. However, the recipe required high skill levels to form chocolate to an orderly rectangular shape. Sadly, she has just made another strange-shaped chocolate bar as shown in Figure G-1. Figure G-1: A strange-shaped chocolate bar Each chocolate bar consists of many small rectangular segments of chocolate. Adjacent segments are separated with a groove in between them for ease of snapping. She planned to cut the strange-shaped chocolate bars into several rectangular pieces and sell them in her shop. She wants to cut each chocolate bar as follows. The bar must be cut along grooves. The bar must be cut into rectangular pieces. The bar must be cut into as few pieces as possible. Following the rules, Figure G-2 can be an instance of cutting of the chocolate bar shown in Figure G-1. Figures G-3 and G-4 do not meet the rules; Figure G-3 has a non-rectangular piece, and Figure G-4 has more pieces than Figure G-2. Figure G-2: An instance of cutting that follows the rules Figure G-3: An instance of cutting that leaves a non-rectangular piece Figure G-4: An instance of cutting that yields more pieces than Figure G-2 Your job is to write a program that computes the number of pieces of chocolate after cutting according to the rules. Input The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. Each dataset is formatted as follows. h w r (1, 1) ... r (1, w ) r (2, 1) ... r (2, w ) ... r ( h , 1) ... r ( h , w ) The integers h and w are the lengths of the two orthogonal dimensions of the chocolate, in number of segments. You may assume that 2 ≀ h ≀ 100 and 2 ≀ w ≀ 100. Each of the following h lines consists of w characters, each is either a " . " or a " # ". The character r ( i , j ) represents whether the chocolate segment exists at the position ( i , j ) as follows. " . ": There is no chocolate. " # ": There is a segment of chocolate. You can assume that there is no dataset that represents either multiple disconnected bars as depicted in Figure G-5 or a bar in a shape with hole(s) as depicted in Figure G-6 and G-7. You can also assume that there is at least one " # " character in each dataset. Figure G-5: Disconnected chocolate bars Figure G-6: A chocolate bar with a hole Figure G-7: Another instance of a chocolate bar with a hole Output For each dataset, output a line containing the integer representing the number of chocolate pieces obtained by cutting according to the rules. No other characters are allowed in the output. Sample Input 3 5 ###.# ##### ###.. 4 5 .#.## .#### ####. ##.#. 8 8 .#.#.#.# ######## .######. ######## .######. ######## .######. ######## 8 8 .#.#.#.# ######## .##.#.#. ##....## .##.###. ##...### .##.###. ###.#.## 4 4 #### #### #### #### 0 0 Output for the Sample Input 3 5 11 19 1
39,180
Score : 400 points Problem Statement There is a grid of square cells with H horizontal rows and W vertical columns. The cell at the i -th row and the j -th column will be denoted as Cell (i, j) . In Cell (i, j) , a_{ij} coins are placed. You can perform the following operation any number of times: Operation: Choose a cell that was not chosen before and contains one or more coins, then move one of those coins to a vertically or horizontally adjacent cell. Maximize the number of cells containing an even number of coins. Constraints All values in input are integers. 1 \leq H, W \leq 500 0 \leq a_{ij} \leq 9 Input Input is given from Standard Input in the following format: H W a_{11} a_{12} ... a_{1W} a_{21} a_{22} ... a_{2W} : a_{H1} a_{H2} ... a_{HW} Output Print a sequence of operations that maximizes the number of cells containing an even number of coins, in the following format: N y_1 x_1 y_1' x_1' y_2 x_2 y_2' x_2' : y_N x_N y_N' x_N' That is, in the first line, print an integer N between 0 and H \times W (inclusive), representing the number of operations. In the (i+1) -th line ( 1 \leq i \leq N ), print four integers y_i, x_i, y_i' and x_i' ( 1 \leq y_i, y_i' \leq H and 1 \leq x_i, x_i' \leq W ), representing the i -th operation. These four integers represents the operation of moving one of the coins placed in Cell (y_i, x_i) to a vertically or horizontally adjacent cell, (y_i', x_i') . Note that if the specified operation violates the specification in the problem statement or the output format is invalid, it will result in Wrong Answer . Sample Input 1 2 3 1 2 3 0 1 1 Sample Output 1 3 2 2 2 3 1 1 1 2 1 3 1 2 Every cell contains an even number of coins after the following sequence of operations: Move the coin in Cell (2, 2) to Cell (2, 3) . Move the coin in Cell (1, 1) to Cell (1, 2) . Move one of the coins in Cell (1, 3) to Cell (1, 2) . Sample Input 2 3 2 1 0 2 1 1 0 Sample Output 2 3 1 1 1 2 1 2 2 2 3 1 3 2 Sample Input 3 1 5 9 9 9 9 9 Sample Output 3 2 1 1 1 2 1 3 1 4
39,182
Score : 300 points Problem Statement We have N clocks. The hand of the i -th clock (1≀i≀N) rotates through 360° in exactly T_i seconds. Initially, the hand of every clock stands still, pointing directly upward. Now, Dolphin starts all the clocks simultaneously. In how many seconds will the hand of every clock point directly upward again? Constraints 1≀N≀100 1≀T_i≀10^{18} All input values are integers. The correct answer is at most 10^{18} seconds. Input Input is given from Standard Input in the following format: N T_1 : T_N Output Print the number of seconds after which the hand of every clock point directly upward again. Sample Input 1 2 2 3 Sample Output 1 6 We have two clocks. The time when the hand of each clock points upward is as follows: Clock 1 : 2 , 4 , 6 , ... seconds after the beginning Clock 2 : 3 , 6 , 9 , ... seconds after the beginning Therefore, it takes 6 seconds until the hands of both clocks point directly upward. Sample Input 2 5 2 5 10 1000000000000000000 1000000000000000000 Sample Output 2 1000000000000000000
39,183
Score : 100 points Problem Statement You will be given a string S of length 3 representing the weather forecast for three days in the past. The i -th character (1 \leq i \leq 3) of S represents the forecast for the i -th day. S , C , and R stand for sunny, cloudy, and rainy, respectively. You will also be given a string T of length 3 representing the actual weather on those three days. The i -th character (1 \leq i \leq 3) of S represents the actual weather on the i -th day. S , C , and R stand for sunny, cloudy, and rainy, respectively. Print the number of days for which the forecast was correct. Constraints S and T are strings of length 3 each. S and T consist of S , C , and R . Input Input is given from Standard Input in the following format: S T Output Print the number of days for which the forecast was correct. Sample Input 1 CSS CSR Sample Output 1 2 For the first day, it was forecast to be cloudy, and it was indeed cloudy. For the second day, it was forecast to be sunny, and it was indeed sunny. For the third day, it was forecast to be sunny, but it was rainy. Thus, the forecast was correct for two days in this case. Sample Input 2 SSR SSR Sample Output 2 3 Sample Input 3 RRR SSS Sample Output 3 0
39,184
G: Restricted DFS 問題 N 頂点 N-1 蟺からなる、自己ルヌプや倚重蟺が存圚しない無向朚 G がある。頂点はそれぞれ 1 から N たで番号付けされおおり、蟺もそれぞれ 1 から N-1 たで番号付けされおおり、 i 番目の蟺は u_i ず v_i を結んでいる。たた、 i 番目の頂点には非負敎数 A_i がそれぞれ割り圓おられおいる。 この朚に察しお、根 r から以䞋の擬䌌コヌドにしたがっお DFS (深さ優先探玢) を行うこずを考える。 // [input] // G: dfs の察象ずなるグラフ // A: それぞれの頂点に割り圓おられた非負敎数 // v: dfs を開始する頂点 // step: ステップ数を蚘録する敎数 // [output] // 以䞋のうちどちらかの二倀 // - SUCCESS: dfs が途䞭で終了するこずなく、頂点 v たで戻っおくる // - FAILURE: dfs が途䞭で終了する function dfs(G, A, v, step) if (A[v] が 0 である) then return FAILURE A[v] ← A[v] - 1 step ← step + 1 v の子を頂点番号が小さい順に゜ヌト // c は頂点番号が小さい順に芋られる for each (v の子 c) do if (dfs(G, A, c, step) が FAILURE である) then return FAILURE if (A[v] が 0 である) then return FAILURE A[v] ← A[v] - 1 step ← step + 1 return SUCCESS ぀たり、䞎えられた G ず A に察しお、根 r に぀いお dfs(G, A, r, 0) を実行するこずを考える。 それぞれの頂点を根ずしたずきの、この DFS のステップ数を求めよ。 入力圢匏 N A_1 ... A_N u_1 v_1 ... u_{N-1} v_{N-1} 1 行目では、䞎えられるグラフの頂点数 N が䞎えられる。 2 行目は N 個の敎数からなる。 i 個目の敎数 A_i は、 i 番目の頂点に曞かれおいる倀を衚す。 3 行目から N+1 行目たでは、䞎えられるグラフの蟺の情報が䞎えられる。 u_i, v_i は、頂点 u_i ず頂点 v_i を結ぶ無向蟺がグラフ䞭に存圚するこずを衚す。 制玄 1 \leq N \leq 3 \times 10^5 0 \leq A_i \leq 10^9 1 \leq u_i < v_i \leq N 䞎えられるグラフは朚であるこずが保蚌される 入力は党お敎数で䞎えられる 出力圢匏 N 行出力せよ。 i 行目には、頂点 i を根ずしたずきのステップ数を出力せよ。 入力䟋1 3 1 2 3 1 2 1 3 出力䟋1 2 3 3 1 番目の頂点を根ずしたずき 頂点 1 ( A_1 : 1 → 0 ) → 頂点 2 ( A_2 : 2 → 1 ) → 頂点 1 ( A_1 が 0 であるため、頂点 1 に蚪れるこずなく終了する) 2 番目の頂点を根ずしたずき 頂点 2 ( A_2 : 2 → 1 ) → 頂点 1 ( A_1 : 1 → 0 ) → 頂点 3 ( A_3 : 3 → 2 ) → 頂点 1 ( A_1 が 0 であるため、頂点 1 に蚪れるこずなく終了する) 3 番目の頂点を根ずしたずき 頂点 3 ( A_3 : 3 → 2 ) → 頂点 1 ( A_1 : 1 → 0 ) → 頂点 2 ( A_2 : 2 → 1 ) → 頂点 1 ( A_1 が 0 であるため、頂点 1 に蚪れるこずなく終了する) よっお、答えはそれぞれ 2, 3, 3 ずなる。はじめに根から出発するずきも A_i の倀を枛らすこずに泚意せよ。 入力䟋2 3 1 2 3 1 2 2 3 出力䟋2 4 4 5
39,185
Addition of Big Integers Given two integers $A$ and $B$, compute the sum, $A + B$. Input Two integers $A$ and $B$ separated by a space character are given in a line. Output Print the sum in a line. Constraints $-1 \times 10^{100000} \leq A, B \leq 10^{100000}$ Sample Input 1 5 8 Sample Output 1 13 Sample Input 2 100 25 Sample Output 2 125 Sample Input 3 -1 1 Sample Output 3 0 Sample Input 4 12 -3 Sample Output 4 9
39,186
H: 慈悲 (Mercy) サンタクロヌスは、クリスマスなのにプログラミングをやっおいる集団を芋぀けた。 サンタクロヌスは圌らを可哀想に思ったので、ケヌキをプレれントするこずにした。 クリヌムが $N$ 皮類あっお、矎味しさは $A_1, A_2, A_3, \dots, A_N$ である。 スポンゞが $M$ 皮類あっお、矎味しさは $B_1, B_2, B_3, \dots, B_M$ である。 クリヌム 1 皮類ずスポンゞ 1 皮類を組み合わせおケヌキを䜜り、その矎味しさは (クリヌムの矎味しさ) × (スポンゞの矎味しさ) になる。 サンタクロヌスは慈悲深いので、$N \times M$ 通りの組み合わせのケヌキをすべお䞀぀ず぀䜜った。 ケヌキの矎味しさは合蚈いく぀か。 入力 1 行目には、敎数 $N, M$ が空癜区切りで䞎えられる。 2 行目には、敎数 $A_1, A_2, A_3, \dots, A_N$ が空癜区切りで䞎えられる。 3 行目には、敎数 $B_1, B_2, B_3, \dots, B_M$ が空癜区切りで䞎えられる。 出力 サンタクロヌスの䜜ったケヌキの矎味しさの合蚈を出力せよ。最埌には改行を入れるこず。 制玄 $N, M$ は $1$ 以䞊 $100 \ 000$ 以䞋の敎数 $A_1, A_2, A_3, \dots, A_N$ は $1$ 以䞊 $1 \ 000$ 以䞋の敎数 $B_1, B_2, B_3, \dots, B_M$ は $1$ 以䞊 $1 \ 000$ 以䞋の敎数 泚意 答えが 32 bit 敎数型 (int 型など) の範囲に収たらない可胜性があるので、64 bit 敎数型 (long long 型など) を䜿いたしょう。 入力䟋1 3 2 3 1 5 2 4 出力䟋1 54 サンタクロヌスは、次の 6 皮類のケヌキを䜜りたす。 クリヌム 1 ずスポンゞ 1 を組み合わせたケヌキの矎味しさ$3 \times 2 = 6$ クリヌム 1 ずスポンゞ 2 を組み合わせたケヌキの矎味しさ$3 \times 4 = 12$ クリヌム 2 ずスポンゞ 1 を組み合わせたケヌキの矎味しさ$1 \times 2 = 2$ クリヌム 2 ずスポンゞ 2 を組み合わせたケヌキの矎味しさ$1 \times 4 = 4$ クリヌム 3 ずスポンゞ 1 を組み合わせたケヌキの矎味しさ$5 \times 2 = 10$ クリヌム 3 ずスポンゞ 2 を組み合わせたケヌキの矎味しさ$5 \times 4 = 20$ 合蚈の矎味しさは $54$ ずなりたす。 入力䟋2 10 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 出力䟋2 3025
39,187
Score : 400 points Problem Statement You are given a sequence A_1, A_2, ..., A_N and an integer K . Print the maximum possible length of a sequence B that satisfies the following conditions: B is a (not necessarily continuous) subsequence of A . For each pair of adjacents elements of B , the absolute difference of the elements is at most K . Constraints 1 \leq N \leq 300,000 0 \leq A_i \leq 300,000 0 \leq K \leq 300,000 All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 A_2 : A_N Output Print the answer. Sample Input 1 10 3 1 5 4 3 8 6 9 7 2 4 Sample Output 1 7 For example, B = (1, 4, 3, 6, 9, 7, 4) satisfies the conditions. It is a subsequence of A = (1, 5, 4, 3, 8, 6, 9, 7, 2, 4) . All of the absolute differences between two adjacent elements ( |1-4|, |4-3|, |3-6|, |6-9|, |9-7|, |7-4| ) are at most K = 3 .
39,188
Problem F: Bus Problem 円環状に $1$ から $N$ たでの番号が぀けられた $N$ 個のバス停が右回りに䞊んでいる。 隣接するバス停どうしは道で結ばれおいる。 各 $i \ (1 \le i \le N)$ に぀いお、バス停 $i$ ずバス停 $i+1$ の間を盎接結ぶ道の長さは $d_i$ メヌトルである。 ただし、バス停 $N+1$ はバス停 $1$ のこずを衚す。 $M$ 台のバスがある。 $j \ (1 \le j \le M)$ 番目のバスは $c_j='R'$ のずき右回り、$c_j='L'$ のずき巊回りに走行する。 たた、時刻 $0$ にバス停 $b_j$ を出発し、$1$ メヌトル進むのに $t_j$ 秒かかる。 この問題においお、 バスは氞遠に走り続ける バスの乗り降りには時間がかからない バス停では、あるバスがそのバス停を通過する瞬間、そのバスに乗り降りできる バス停以倖でバスに乗り降りするこずはできない 䜕台のバスに乗っおもよい ずする。 以䞋のク゚リを合蚈 $Q$ 回凊理せよ。 時刻 $0$ にバス停 $x_k$ を出発し、バス停 $y_k$ たでバスのみを利甚しお移動するずきの、所芁時間の最小倀を求めよ。 Input 入力は以䞋の圢匏で䞎えられる。 $N$ $M$ $Q$ $d_1$ $\ldots$ $d_N$ $c_1$ $b_1$ $t_1$ $\vdots$ $c_M$ $b_M$ $t_M$ $x_1$ $y_1$ $\vdots$ $x_Q$ $y_Q$ 1行目にバス停の数 $N$、バスの数 $M$、ク゚リの数 $Q$ が空癜区切りで䞎えられる。 2行目に隣接するバス停を繋ぐ道の情報が空癜区切りで䞎えられる。 3行目から続く $M$ 行にバスの情報が空癜区切りで䞎えられる。 続く $Q$ 行にク゚リの情報が空癜区切りで䞎えられる。 Constraints 入力は以䞋の条件を満たす。 $3 \leq N \leq 10^5 $ $1 \leq M \leq 10^5 $ $1 \leq Q \leq 10^5 $ $ 1 \leq d_i \leq 10^2 \ (1 \leq i \leq N) $ $ c_j = 'R' \ or \ 'L' \ (1 \leq j \leq M) $ $ 1 \leq b_j \leq N \ (1 \leq j \leq M) $ $ 1 \leq t_j \leq 10^5 \ (1 \leq j \leq M) $ $ 1 \leq x_k, y_k \leq N \ (1 \leq k \leq Q) $ $ x_k \neq y_k \ (1 \leq k \leq Q) $ 入力で䞎えられる数はすべお敎数 Output 出力は $Q$ 行からなる。 各ク゚リに察し、所芁時間の最小倀を出力せよ。 $k$ 行目には $k$ 番目のク゚リに察する答えを出力せよ。 Sample Input 1 3 1 6 1 2 3 R 1 1 1 2 1 3 2 1 2 3 3 1 3 2 Sample Output 1 1 3 6 3 6 7 $1$ ぀目のク゚リでは、時刻 $0$ にバス停 $1$ からバス $1$ に乗り、時刻 $1$ にバス停 $2$ で降りるのが最適である。 Sample Input 2 4 6 7 45 72 81 47 R 1 47202 L 1 2156 L 2 95728 R 1 30739 L 3 39679 L 4 86568 3 2 3 4 1 2 2 4 4 3 1 4 2 1 Sample Output 2 431200 629552 431200 629552 275968 101332 528220
39,189
Score: 400 points Problem Statement Takahashi became a pastry chef and opened a shop La Confiserie d'ABC to celebrate AtCoder Beginner Contest 100. The shop sells N kinds of cakes. Each kind of cake has three parameters "beauty", "tastiness" and "popularity". The i -th kind of cake has the beauty of x_i , the tastiness of y_i and the popularity of z_i . These values may be zero or negative. Ringo has decided to have M pieces of cakes here. He will choose the set of cakes as follows: Do not have two or more pieces of the same kind of cake. Under the condition above, choose the set of cakes to maximize (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity). Find the maximum possible value of (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) for the set of cakes that Ringo chooses. Constraints N is an integer between 1 and 1 \ 000 (inclusive). M is an integer between 0 and N (inclusive). x_i, y_i, z_i \ (1 \leq i \leq N) are integers between -10 \ 000 \ 000 \ 000 and 10 \ 000 \ 000 \ 000 (inclusive). Input Input is given from Standard Input in the following format: N M x_1 y_1 z_1 x_2 y_2 z_2 : : x_N y_N z_N Output Print the maximum possible value of (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) for the set of cakes that Ringo chooses. Sample Input 1 5 3 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 Sample Output 1 56 Consider having the 2 -nd, 4 -th and 5 -th kinds of cakes. The total beauty, tastiness and popularity will be as follows: Beauty: 1 + 3 + 9 = 13 Tastiness: 5 + 5 + 7 = 17 Popularity: 9 + 8 + 9 = 26 The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is 13 + 17 + 26 = 56 . This is the maximum value. Sample Input 2 5 3 1 -2 3 -4 5 -6 7 -8 -9 -10 11 -12 13 -14 15 Sample Output 2 54 Consider having the 1 -st, 3 -rd and 5 -th kinds of cakes. The total beauty, tastiness and popularity will be as follows: Beauty: 1 + 7 + 13 = 21 Tastiness: (-2) + (-8) + (-14) = -24 Popularity: 3 + (-9) + 15 = 9 The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is 21 + 24 + 9 = 54 . This is the maximum value. Sample Input 3 10 5 10 -80 21 23 8 38 -94 28 11 -26 -2 18 -69 72 79 -26 -86 -54 -72 -50 59 21 65 -32 40 -94 87 -62 18 82 Sample Output 3 638 If we have the 3 -rd, 4 -th, 5 -th, 7 -th and 10 -th kinds of cakes, the total beauty, tastiness and popularity will be -323 , 66 and 249 , respectively. The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is 323 + 66 + 249 = 638 . This is the maximum value. Sample Input 4 3 2 2000000000 -9000000000 4000000000 7000000000 -5000000000 3000000000 6000000000 -1000000000 8000000000 Sample Output 4 30000000000 The values of the beauty, tastiness and popularity of the cakes and the value to be printed may not fit into 32-bit integers.
39,190
Score : 400 points Problem Statement You are given N items. The value of the i -th item (1 \leq i \leq N) is v_i . Your have to select at least A and at most B of these items. Under this condition, find the maximum possible arithmetic mean of the values of selected items. Additionally, find the number of ways to select items so that the mean of the values of selected items is maximized. Constraints 1 \leq N \leq 50 1 \leq A,B \leq N 1 \leq v_i \leq 10^{15} Each v_i is an integer. Input The input is given from Standard Input in the following format: N A B v_1 v_2 ... v_N Output Print two lines. The first line should contain the maximum possible arithmetic mean of the values of selected items. The output should be considered correct if the absolute or relative error is at most 10^{-6} . The second line should contain the number of ways to select items so that the mean of the values of selected items is maximized. Sample Input 1 5 2 2 1 2 3 4 5 Sample Output 1 4.500000 1 The mean of the values of selected items will be maximized when selecting the fourth and fifth items. Hence, the first line of the output should contain 4.5 . There is no other way to select items so that the mean of the values will be 4.5 , and thus the second line of the output should contain 1 . Sample Input 2 4 2 3 10 20 10 10 Sample Output 2 15.000000 3 There can be multiple ways to select items so that the mean of the values will be maximized. Sample Input 3 5 1 5 1000000000000000 999999999999999 999999999999998 999999999999997 999999999999996 Sample Output 3 1000000000000000.000000 1
39,191
Problem F: Slim Span Given an undirected weighted graph G , you should find one of spanning trees specified as follows. The graph G is an ordered pair ( V , E ), where V is a set of vertices { v 1 , v 2 , ... , v n } and E is a set of undirected edges { e 1 , e 2 , ... , e m }. Each edge e ∈ E has its weight w ( e ). A spanning tree T is a tree (a connected subgraph without cycles) which connects all the n vertices with n - 1 edges. The slimness of a spanning tree T is defined as the difference between the largest weight and the smallest weight among the n - 1 edges of T . Figure 5: A graph G and the weights of the edges For example, a graph G in Figure 5(a) has four vertices { v 1 , v 2 , v 3 , v 4 } and five undirected edges { e 1 , e 2 , e 3 , e 4 , e 5 }. The weights of the edges are w ( e 1 ) = 3, w ( e 2 ) = 5, w ( e 3 ) = 6, w ( e 4 ) = 6, w ( e 5 ) = 7 as shown in Figure 5(b). Figure 6: Examples of the spanning trees of G There are several spanning trees for G . Four of them are depicted in Figure 6(a)-(d). The spanning tree T a in Figure 6(a) has three edges whose weights are 3, 6 and 7. The largest weight is 7 and the smallest weight is 3 so that the slimness of the tree T a is 4. The slimnesses of spanning trees T b , T c and T d shown in Figure 6(b), (c) and (d) are 3, 2 and 1, respectively. You can easily see the slimness of any other spanning tree is greater than or equal to 1, thus the spanning tree T d in Figure 6(d) is one of the slimmest spanning trees whose slimness is 1. Your job is to write a program that computes the smallest slimness. Input The input consists of multiple datasets, followed by a line containing two zeros separated by a space. Each dataset has the following format. n m a 1 b 1 w 1 . . . a m b m w m Every input item in a dataset is a non-negative integer. Items in a line are separated by a space. n is the number of the vertices and m the number of the edges. You can assume 2 ≀ n ≀ 100 and 0 ≀ m ≀ n ( n - 1)/2. a k and b k ( k = 1, ... , m ) are positive integers less than or equal to n , which represent the two vertices v a k and v b k connected by the k th edge e k . w k is a positive integer less than or equal to 10000, which indicates the weight of e k . You can assume that the graph G = ( V , E ) is simple, that is, there are no self-loops (that connect the same vertex) nor parallel edges (that are two or more edges whose both ends are the same two vertices). Output For each dataset, if the graph has spanning trees, the smallest slimness among them should be printed. Otherwise, -1 should be printed. An output should not contain extra characters. Sample Input 4 5 1 2 3 1 3 5 1 4 6 2 4 6 3 4 7 4 6 1 2 10 1 3 100 1 4 90 2 3 20 2 4 80 3 4 40 2 1 1 2 1 3 0 3 1 1 2 1 3 3 1 2 2 2 3 5 1 3 6 5 10 1 2 110 1 3 120 1 4 130 1 5 120 2 3 110 2 4 120 2 5 130 3 4 120 3 5 110 4 5 120 5 10 1 2 9384 1 3 887 1 4 2778 1 5 6916 2 3 7794 2 4 8336 2 5 5387 3 4 493 3 5 6650 4 5 1422 5 8 1 2 1 2 3 100 3 4 100 4 5 100 1 5 50 2 5 50 3 5 50 4 1 150 0 0 Output for the Sample Input 1 20 0 -1 -1 1 0 1686 50
39,192
Score : 600 points Problem Statement We will create an artwork by painting black some squares in a white square grid with 10^9 rows and N columns. The current plan is as follows: for the i -th column from the left, we will paint the H_i bottommost squares and will not paint the other squares in that column. Before starting to work, you can choose at most K columns (possibly zero) and change the values of H_i for these columns to any integers of your choice between 0 and 10^9 (inclusive). Different values can be chosen for different columns. Then, you will create the modified artwork by repeating the following operation: Choose one or more consecutive squares in one row and paint them black. (Squares already painted black can be painted again, but squares not to be painted according to the modified plan should not be painted.) Find the minimum number of times you need to perform this operation. Constraints 1 \leq N \leq 300 0 \leq K \leq N 0 \leq H_i \leq 10^9 All values in input are integers. Input Input is given from Standard Input in the following format: N K H_1 H_2 ... H_N Output Print the minimum number of operations required. Sample Input 1 4 1 2 3 4 1 Sample Output 1 3 For example, by changing the value of H_3 to 2 , you can create the modified artwork by the following three operations: Paint black the 1 -st through 4 -th squares from the left in the 1 -st row from the bottom. Paint black the 1 -st through 3 -rd squares from the left in the 2 -nd row from the bottom. Paint black the 2 -nd square from the left in the 3 -rd row from the bottom. Sample Input 2 6 2 8 6 9 1 2 1 Sample Output 2 7 Sample Input 3 10 0 1 1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000 Sample Output 3 4999999996
39,193
Score : 800 points Problem Statement Takahashi and Aoki will take N exams numbered 1 to N . They have decided to compete in these exams. The winner will be determined as follows: For each exam i , Takahashi decides its importance c_i , which must be an integer between l_i and u_i (inclusive). Let A be \sum_{i=1}^{N} c_i \times (Takahashi's score on Exam i ), and B be \sum_{i=1}^{N} c_i \times (Aoki's score on Exam i ). Takahashi wins if A \geq B , and Aoki wins if A < B . Takahashi knows that Aoki will score b_i on Exam i , with his supernatural power. Takahashi himself, on the other hand, will score 0 on all the exams without studying more. For each hour of study, he can increase his score on some exam by 1 . (He can only study for an integer number of hours.) However, he cannot score more than X on an exam , since the perfect score for all the exams is X . Print the minimum number of study hours required for Takahashi to win. Constraints 1 \leq N \leq 10^5 1 \leq X \leq 10^5 0 \leq b_i \leq X (1 \leq i \leq N) 1 \leq l_i \leq u_i \leq 10^5 (1 \leq i \leq N) All values in input are integers. Input Input is given from Standard Input in the following format: N X b_1 l_1 u_1 b_2 l_2 u_2 : b_N l_N u_N Output Print the minimum number of study hours required for Takahashi to win. Sample Input 1 2 100 85 2 3 60 1 1 Sample Output 1 115 One optimal strategy is as follows: Choose c_1 = 3, c_2 = 1 . Study to score 100 on Exam 1 and 15 on Exam 2 . Then, A = 3 \times 100 + 1 \times 15 = 315 , B = 3 \times 85 + 1 \times 60 = 315 and Takahashi will win. Sample Input 2 2 100 85 2 3 60 10 10 Sample Output 2 77 Sample Input 3 1 100000 31415 2718 2818 Sample Output 3 31415 Sample Input 4 10 1000 451 4593 6263 324 310 6991 378 1431 7068 71 1757 9218 204 3676 4328 840 6221 9080 684 1545 8511 709 5467 8674 862 6504 9835 283 4965 9980 Sample Output 4 2540
39,194
Problem I: Sum of Last Digits Problem 3぀の敎数 n , m , k が䞎えられるので n 1 %10 + n 1+ m %10 + n 1+2 m %10 + ... + n 1+( k -1) m %10 を蚈算せよ。 a % b は a を b で割ったずきの䜙りを衚す。 Input n m k 1行に n , m , k が䞎えられる。 Constraints 入力は以䞋の条件を満たす 0 ≀ n ≀ 10 18 0 ≀ m ≀ 10 9 1 ≀ k ≀ 10 9 Output 答えを1行に出力せよ。 Sample Input 1 1 1 9 Sample Output 1 9 Sample Input 2 2 1 3 Sample Output 2 14 Sample Input 3 6 11 11 Sample Output 3 66 Sample Input 4 100 7 12 Sample Output 4 0 Sample Input 5 123 123 3 Sample Output 5 11
39,195
問題文 以䞋の条件を満たす敎数列 $X_1, X_2, ..., X_N$ の個数を求めよ。 任意の敎数 $i$ ($1 \leq i \leq N$)に察しお、$X_j=i$ ずなる $j$ ($1 \leq j \leq N$)が存圚する。 $X_s = t$ $X_{a_i} < X_{b_i}$ ($1 \leq i \leq C$) 入力 入力は以䞋の圢匏に埓う。䞎えられる数は党お敎数である。 $N$ $C$ $s$ $t$ $a_1$ $b_1$ $a_2$ $b_2$ $...$ $a_C$ $b_C$ 制玄 $1 \leq N \leq 2000$ $0 \leq C \leq N$ $1 \leq s \leq N$ $1 \leq t \leq N$ $1 \leq a_i \leq N$ $1 \leq b_i \leq N$ $a_i \neq b_i$ $i \neq j$ ならば $a_i \neq a_j$ 出力 条件を満たす数列の個数を $10^9+7$ で割った䜙りを1行に出力せよ(条件を満たす数列の個数がたかだか有限個しかないこずは簡単に瀺される)。 Sample Input 1 3 1 1 1 1 3 Output for the Sample Input 1 2 $\{X_1, X_2, X_3\} = \{1, 2, 3\}, \{1, 3, 2\}$ の2぀が条件を満たす。 Sample Input 2 4 2 1 1 2 3 3 2 Output for the Sample Input 2 0 $X_2<X_3$ か぀ $X_3<X_2$ を満たす数列は存圚しない。
39,197
Problem 09: Building Houses この秋、割火ハりスは最近開拓が進んでいる束短地区の神暗通り沿いの土地を分譲するこずにした。 先日募集をかけ、今日は賌入者に分譲説明䌚を開催する。 賌入者が説明䌚堎に集たりだしたずころ、なんだか空気がずおも凍り付いおいた。䞍思議に思った担圓者は開催前に 「皆さん新倩地を求めおきたずいうのに、ちょっず衚情が暗いですが倧䞈倫ですか」 ず聞いおみた。そこで思わぬ事実が刀明した。実は、賌入者党員が珟圚隣町の集合団地の䜏人で、あたりにも呚蟺の人たちず折り合いが合わないため今回匕越しをしようず怜蚎しおいたのだった。 賌入者たちはすでに費甚を支払枈みで以前の䜏たいを匕き払っおいる。埓っお、今回分譲した束短地区の神暗通りに家を立おる他方法はない。 しかしながら、少しでも快適な新居ラむフを送っおもらおうず担圓者は頭を悩たせた。よくよく話を聞いおみるず、仲の悪さにはバラツキがあったので、聞き蟌みを行い図のような仲の悪い床チェック衚を䜜成した 図仲の悪い床チェック衚 賌入者の数を n ずするず、衚は n × n の行列ずなり、芁玠 a i,j は、賌入者 i が自分の家から賌入者 j の家を最䜎でも䜕 m 離したいかを瀺す。䟋えば、図の衚においお、賌入者 A は自分の家ず賌入者 B の家の距離が 2 m 以䞊離れおいないず玍埗しない。さらに、賌入者 B は自分の家ず賌入者 A の家の距離が 4 m 以䞊離れおいないず玍埗しない。埓っお A ず B の距離は 4 m 以䞊離す必芁がある。 担圓者はこの衚に基づき隣の家ずの間隔を蚭け土地のレむアりトを蚭蚈するこずにした。 あなたの仕事は、仲の悪い床チェック衚を入力し、賌入者の家を神暗通り䞀盎線䞊に建おるために最䜎限必芁な通りの長さmを出力するプログラムを䜜成するこずである。 なお簡単のために、家は点ずしお扱い、その幅は 0 m ず仮定する。 Input 入力ずしお耇数のデヌタセットが䞎えられる。各デヌタセットは以䞋の圢匏で䞎えられる n 賌入者の数敎数 a 1,1 a 1,2 ... a 1, n  a 1, j 敎数 a 2,1 a 2,2 ... a 2, n  a 2, j 敎数 . . a n ,1 a n ,2 ... a n , n  a n , j 敎数 n は 10 以䞋ずする。 a i , i は 0 であり、 a i , j ( i ≠ j ) は 1 以䞊である。 n が 0 のずき入力の終わりずする。 Output 各デヌタセットに぀いお、最䜎限必芁な通りの長さを行に出力せよ。 Sample Input 4 0 2 3 1 4 0 4 2 1 1 0 3 3 1 5 0 2 0 3 3 0 0 Output for the Sample Input 8 3
39,198
Score : 300 points Problem Statement Let f(n) be the number of triples of integers (x,y,z) that satisfy both of the following conditions: 1 \leq x,y,z x^2 + y^2 + z^2 + xy + yz + zx = n Given an integer N , find each of f(1),f(2),f(3),\ldots,f(N) . Constraints All values in input are integers. 1 \leq N \leq 10^4 Input Input is given from Standard Input in the following format: N Output Print N lines. The i -th line should contain the value f(i) . Sample Input 1 20 Sample Output 1 0 0 0 0 0 1 0 0 0 0 3 0 0 0 0 0 3 3 0 0 For n=6 , only (1,1,1) satisfies both of the conditions. Thus, f(6) = 1 . For n=11 , three triples, (1,1,2) , (1,2,1) , and (2,1,1) , satisfy both of the conditions. Thus, f(6) = 3 . For n=17 , three triples, (1,2,2) , (2,1,2) , and (2,2,1) , satisfy both of the conditions. Thus, f(17) = 3 . For n=18 , three triples, (1,1,3) , (1,3,1) , and (3,1,1) , satisfy both of the conditions. Thus, f(18) = 3 .
39,199
凞倚角圢柱工業郜垂 凞倚角圢柱工業郜垂 (Industrial Convex Pillar City, ICPC) はいく぀かの凞倚角圢柱の圢状をした建物からなる街であるあなたはこの街の䞭を珟圚地 S から目的地 T に向けお歩こうずしおいる今日は日差しが匷いので出来る限りひなたを通らずに目的地ぞ向かいたいあなたが立っおいる地点ず倪陜を䞀盎線に結んだ線䞊に建物がある堎合あなたは建物の陰にいるので日差しを受けずにすむたたこの街の建物の倖呚には党おひさしが付いおいるので建物の倖呚に沿っお歩いおいる間は日が圓たる瞁に沿っお歩いおも日差しを受けるこずはないあなたは建物の内郚以倖はこの街をどこでも自由に歩くこずができる もっずも日差しを受けないように珟圚地から目的地たで歩いた時のひなたを歩く距離を出力するプログラムを䜜成しなさい 図E1: 第1むンプットの堎合 図E2: 第2むンプットの堎合 図E3: 第3むンプットの堎合 Input 入力は耇数のデヌタセットからなる デヌタセットの個数は最倧でも 30 個を超えない 各デヌタセットは次の圢匏で衚される N NV 1 H 1 X 1,1 Y 1,1 X 1,2 Y 1,2 ... X 1,NV 1 Y 1,NV 1 ... NV N H N X N,1 Y N,1 X N,2 Y N,2 ... X N,NV N Y N,NV N Ξ φ S x S y T x T y 行目の N は建物の数を衚す続く N 行は各建物の圢状を指定する NV i は i 番目の建物を䞊から芋た倚角圢の頂点数 H i は i 番目の建物の高さを衚す X i, j ず Y i, j は i 番目の建物を䞊から芋た倚角圢の j 番目の頂点の x 座暙ず y 座暙を衚す頂点は反時蚈回りの順で䞎えられる党おの建物は䞊から芋るず凞倚角圢であり建物の内郚に他の建物があったり頂点や蟺が他の倚角圢ず重なったりするこずはない続く行には倪陜の方向を衚す Ξ ず φ が䞎えられ Ξ は倪陜の向きを x の正の方向から反時蚈回りの角床で衚し φ は地平線からの倪陜の仰角すなわち倪陜を芋䞊げた時の芖線の方向ず地衚面がなす角床を衚すただし倪陜は無限遠にあり移動䞭には䜍眮を倉えないものずする続く行には珟圚地ず目的地の座暙 ( S x , S y ) ず ( T x , T y ) が䞎えられる 入力で䞎えられる数倀は党お敎数であり以䞋の条件を満たす 1 ≀ N ≀ 100 3 ≀ NV i ≀ 12 1 ≀ H i ≀ 1, 000 0 ≀ Ξ < 360 0 < φ < 90 座暙は党お -1, 000 以䞊 1, 000 以䞋である 珟圚地ず目的地は異なりどちらも建物の内郚及び倖呚には存圚しない 入力の終わりは぀のれロからなる行で衚される Output 各デヌタセットに぀いおひなたを歩く最短距離を行に出力せよ出力には 0.001 を超える絶察誀差があっおはならない Sample Input 2 4 1 0 0 1 0 1 1 0 1 4 2 2 2 3 2 3 3 2 3 60 45 -1 -1 4 4 4 4 1 0 0 3 1 1 2 0 1 3 2 10 7 8 2 12 4 6 8 7 12 8 13 9 15 10 19 11 24 10 25 5 4 16 2 16 4 12 8 14 2 15 0 167 38 3 3 15 21 12 4 3 -8 -3 -9 -3 -9 -5 -8 -6 4 5 -4 -5 -7 -5 -7 -6 -5 -6 4 2 -4 1 -5 1 -5 -4 -4 -4 4 1 -1 1 -2 1 -2 -4 -1 -3 4 2 2 3 -1 3 -2 2 3 2 4 1 3 1 2 1 2 -3 3 -4 4 7 1 0 0 0 0 -1 1 -1 4 4 9 5 7 5 7 4 10 4 4 3 6 5 5 4 5 0 6 0 4 5 8 -1 5 -1 6 -2 8 -2 4 1 10 0 9 0 9 -2 10 -1 4 6 10 2 8 2 8 1 10 1 131 78 -10 10 10 -10 0 Output for Sample Input 1.93185 7.87174 20.86840
39,200
モゞュロ・ク゚リ あなたに N 枚のカヌドを枡したす。どのカヌドにも䞀぀だけ自然数が曞いおありたす。ただし、同じ数が曞いおあるこずはありたせん。 これから質問ずしお、適圓な自然数を蚀いたす。あなたが持っおいるカヌドに曞いおある数を私が蚀った数で割ったずきに埗られる䜙りのうち最も倧きなものを答えおください。 たずえば、あなたは 3 枚のカヌドを持っおいお、それぞれ 9、3、8 ず曞いおあるずしたす。私が「4」ず蚀ったら、9 ず 3 ず 8 をそれぞれ4 で割った䜙りを求めおください。䜙りはそれぞれ 1、3、0 ですが、この䞭でもっずも倧きな䜙りは3 なので、3 が正しい答えになりたす。 では始めたしょうか。え カヌドがいっぱいあるずたいぞんだ しょうがないですね。それではコ ンピュヌタを䜿っお最倧の䜙りを芋぀けるこずにしたしょう。カヌドに曞いおある数を、質問された数で割った䜙りのうち、最倧のものを芋぀けるプログラムを䜜成しおください。なお、質問は回だけでなく䜕床もしたすが、同じ数を 2 回以䞊質問するこずはありたせん。 入力 入力は぀のデヌタセットからなる。入力デヌタは以䞋の圢匏で䞎えられる。 N Q c 1 c 2 ... c N q 1 q 2 : q Q 行目にカヌドの枚数 N (2 ≀ N ≀ 300000) ず質問の回数 Q (2 ≀ Q ≀ 100000) が぀の空癜区切りで䞎えられ、行目にカヌドに曞かれた数 c i (1 ≀ c i ≀ 300000) が぀の空癜区切りで䞎えられる。続くQ 行に質問ずしお䞎えられる数 q i (1 ≀ q i ≀ 300000) が䞎えられる。 出力 質問ごずに最倧の䜙りを行に出力する。 入力䟋 3 3 9 3 8 4 6 5 出力䟋 3 3 4
39,201
Maximum Profit You can obtain profits from foreign exchange margin transactions. For example, if you buy 1000 dollar at a rate of 100 yen per dollar, and sell them at a rate of 108 yen per dollar, you can obtain (108 - 100) × 1000 = 8000 yen. Write a program which reads values of a currency $R_t$ at a certain time $t$ ($t = 0, 1, 2, ... n-1$), and reports the maximum value of $R_j - R_i$ where $j > i$ . Input The first line contains an integer $n$. In the following $n$ lines, $R_t$ ($t = 0, 1, 2, ... n-1$) are given in order. Output Print the maximum value in a line. Constraints $2 \leq n \leq 200,000$ $1 \leq R_t \leq 10^9$ Sample Input 1 6 5 3 1 3 4 3 Sample Output 1 3 Sample Input 2 3 4 3 2 Sample Output 2 -1
39,202
Prime Caves An international expedition discovered abandoned Buddhist cave temples in a giant cliff standing on the middle of a desert. There were many small caves dug into halfway down the vertical cliff, which were aligned on square grids. The archaeologists in the expedition were excited by Buddha's statues in those caves. More excitingly, there were scrolls of Buddhism sutras (holy books) hidden in some of the caves. Those scrolls, which estimated to be written more than thousand years ago, were of immeasurable value. The leader of the expedition wanted to collect as many scrolls as possible. However, it was not easy to get into the caves as they were located in the middle of the cliff. The only way to enter a cave was to hang you from a helicopter. Once entered and explored a cave, you can climb down to one of the three caves under your cave; i.e., either the cave directly below your cave, the caves one left or right to the cave directly below your cave. This can be repeated for as many times as you want, and then you can descent down to the ground by using a long rope. So you can explore several caves in one attempt. But which caves you should visit? By examining the results of the preliminary attempts, a mathematician member of the expedition discovered that (1) the caves can be numbered from the central one, spiraling out as shown in Figure D-1; and (2) only those caves with their cave numbers being prime (let's call such caves prime caves ), which are circled in the figure, store scrolls. From the next attempt, you would be able to maximize the number of prime caves to explore. Figure D-1: Numbering of the caves and the prime caves Write a program, given the total number of the caves and the cave visited first, that finds the descending route containing the maximum number of prime caves. Input The input consists of multiple datasets. Each dataset has an integer m (1 ≀ m ≀ 10 6 ) and an integer n (1 ≀ n ≀ m ) in one line, separated by a space. m represents the total number of caves. n represents the cave number of the cave from which you will start your exploration. The last dataset is followed by a line with two zeros. Output For each dataset, find the path that starts from the cave n and contains the largest number of prime caves, and output the number of the prime caves on the path and the last prime cave number on the path in one line, separated by a space. The cave n , explored first, is included in the caves on the path. If there is more than one such path, output for the path in which the last of the prime caves explored has the largest number among such paths. When there is no such path that explores prime caves, output " 0 0 " (without quotation marks). Sample Input 49 22 46 37 42 23 945 561 1081 681 1056 452 1042 862 973 677 1000000 1000000 0 0 Output for the Sample Input 0 0 6 43 1 23 20 829 18 947 10 947 13 947 23 947 534 993541
39,203
E: 星の䜜り方 - How To Make Stars - 物語 北ノ朚坂孊院1幎生の星屑鈎だニャ鈎はお星様がだ〜い奜き倜はい぀も幌銎染の花代ちんず䞀緒にこっそりず孊校の屋䞊でお星様を芋おるんだだけど今日のお倩気はあいにくの雚・・・これじゃお星様を芋るこずができないニャ花代ちんどうしよう〜 鈎「・・・ああっいいこず思い付いたニャ色んなお星様を芋れるようなおっきなプラネタリりムを䜜るニャ花代ちんさっそくやるニャ」 花代「えっえぇっ鈎ちゃんちょっず埅っおプラネタリりムの䜜り方なんお私知らないよ」 鈎「倧䞈倫ニャ2人で力を合わせればきっずできるよニャニャニャニャ〜」 花代「だダレカタスケテ〜」 問題 自己亀差のない倚角圢の䞭で頂点数が10でありか぀任意の頂点から䞀定の方向に呚䞊をたどったずき内角が30床以䞊60床以䞋である郚分ず内角が240床以䞊270床以䞋である郚分が亀互に珟れるものを「星」ず定矩する 星は面積によっお階玚が定たる具䜓的には面積が S_1 以䞊のものを1玚星 S_{i−1} より小さく S_i 以䞊のものを i 玚星 ( i ≥ 2 ) ず呌ぶように階玚を付ける 階玚が n 個あるずしお階玚の目安ずなる n 皮類の面積 S_1 , ..., S_n が䞎えられるので各 k (1 ≀ k ≀ n) に぀いお k 玚星ずなる星を䜜り2次元平面䞊に配眮せよ ただし各星が互いに重なっおはならない 入力圢匏 入力は以䞋の圢匏で䞎えられる n S_1 ... S_n 1行目には階玚の数 n が䞎えられる 2行目には階玚の目安ずなる面積を衚す敎数 S_1 から S_n が順に空癜区切りで䞎えられる 入力は以䞋の制玄を満たす 1 ≀ n ≀ 100 1 ≀ S_i ≀ 100,000 (1 ≀ i ≀ n) i < j ならば S_i > S_j 出力圢匏 党おの階玚の星に぀いお1行目には階玚を衚す敎数 k を続く10行には任意の頂点から反時蚈回りで10個の頂点の座暙 (x, y) を1行ごずに出力せよ頂点の座暙 (x, y) は x ず y を空癜区切りで出力せよ星は1玚星から順に出力せよすなわち出力は以䞋のようになる 1 玚星の出力 ... n 玚星の出力 k 玚星の星に぀いおは以䞋のように出力する k x_1 y_1 ... x_{10} y_{10} ただし座暙の絶察倀が 5,000 を超えおはならないたた出力が非垞に倧きくなる可胜性があるため座暙倀の出力は小数点以䞋9桁たでずせよ 星の内角に぀いお指定の範囲からの絶察誀差を 10^{−3} rad(ラゞアン)たで蚱す 星が互いに亀差するこずや接觊しおいるこずある星が他の星に内包されるずいったこずは蚱されないここで星が接觊しおいるずは異なる星に含たれる任意の2蟺の距離が 10^{−7} 以䞋であるずきを指す 1玚星の面積は S_1 以䞊 i 玚星 ( i ≥ 2 ) の面積は S_{i−1} 未満 S_i 以䞊でなければならないが絶察誀差を 10^{−7} たで蚱す 1぀の星を構成する任意の2頂点間の距離は 10^{−3} 以䞊でなければならない 入力䟋1 2 10 5 出力䟋1 1 -10.00 -6.48 -10.79 -8.91 -13.34 -8.91 -11.28 -10.41 -12.07 -12.84 -10.00 -11.34 -7.93 -12.84 -8.72 -10.41 -6.66 -8.91 -9.21 -8.91 2 10.00 12.34 9.47 10.72 7.77 10.72 9.15 9.72 8.62 8.10 10.00 9.10 11.38 8.10 10.85 9.72 12.23 10.72 10.53 10.72 入力䟋2 3 10 8 5 出力䟋2 1 -10.00 -6.48 -10.79 -8.91 -13.34 -8.91 -11.28 -10.41 -12.07 -12.84 -10.00 -11.34 -7.93 -12.84 -8.72 -10.41 -6.66 -8.91 -9.21 -8.91 2 10.00 12.93 9.34 10.91 7.21 10.91 8.94 9.65 8.28 7.63 10.00 8.88 11.72 7.63 11.06 9.65 12.79 10.91 10.66 10.91 3 20.00 22.34 19.47 20.72 17.77 20.72 19.15 19.72 18.62 18.10 20.00 19.10 21.38 18.10 20.85 19.72 22.23 20.72 20.53 20.72
39,204
盎方䜓 アむヅ攟送協䌚の教育番組(教育)では、子䟛向けの工䜜番組「あそんで぀くろ」ずいう番組を攟送しおいたす。今日は画甚玙で箱を䜜る回ですが、甚意した長方圢の画甚玙で盎方䜓ができるかを確かめたいず思いたす。ただし、画甚玙は切ったり折ったりしおはいけたせん。 ぀の長方圢が䞎えられるので、それらを䜿っお盎方䜓が䜜れるかどうか刀定するプログラムを䜜成せよ。 Input 入力は以䞋の圢匏で䞎えられる。 h 1 w 1 h 2 w 2 h 3 w 3 h 4 w 4 h 5 w 5 h 6 w 6 入力は行からなり、それぞれの行に各長方圢の瞊の長さを衚す敎数 h i (1 ≀ h i ≀ 1000) ず暪の長さを衚す敎数 w i (1 ≀ w i ≀ 1000) が䞎えられる。 Output 盎方䜓を䜜成できる堎合には「yes」を、䜜成できない堎合には「no」を出力する。ただし、立方䜓は盎方䜓の䞀皮なので、立方䜓の堎合でも「yes」ず出力する。 Sample Input 1 2 2 2 3 2 3 2 3 2 2 3 2 Sample Output 1 yes Sample Input 2 2 2 2 3 2 3 2 3 2 2 2 2 Sample Output 2 no
39,205
Problem G: Traffic You are a resident of Kyoot (oh, well, it’s not a misspelling!) city. All streets there are neatly built on a grid; some streets run in a meridional (north-south) direction and others in a zonal (east-west) direction. The streets that run from north to south are called avenues, whereas those which run from east to west are called drives . Every avenue and drive in the city is numbered to distinguish one from another. The westernmost avenue is called the 1st avenue . The avenue which lies next to the 1st avenue is the 2nd avenue , and so forth. Similarly, drives are numbered from south to north. The figure below illustrates this situation. Figure 1: The Road Map of the Kyoot City There is an intersection with traffic signals to regulate traffic on each crossing point of an avenue and a drive. Each traffic signal in this city has two lights. One of these lights is colored green, and means “you may go”. The other is red, and means “you must stop here”. If you reached an intersection during the red light (including the case where the light turns to red on your arrival), you must stop and wait there until the light turns to green again. However, you do not have to wait in the case where the light has just turned to green when you arrived there. Traffic signals are controlled by a computer so that the lights for two different directions always show different colors. Thus if the light for an avenue is green, then the light for a drive must be red, and vice versa. In order to avoid car crashes, they will never be green together. Nor will they be red together, for efficiency. So all the signals at one intersection turn simultaneously; no sooner does one signal turn to red than the other turns to green. Each signal has a prescribed time interval and permanently repeats the cycle. Figure 2: Signal and Intersection By the way, you are planning to visit your friend by car tomorrow. You want to see her as early as possible, so you are going to drive through the shortest route. However, due to existence of the traffic signals, you cannot easily figure out which way to take (the city also has a very sophisticated camera network to prevent crime or violation: the police would surely arrest you if you didn’t stop on the red light!). So you decided to write a program to calculate the shortest possible time to her house, given the town map and the configuration of all traffic signals. Your car runs one unit distance in one unit time. Time needed to turn left or right, to begin moving, and to stop your car is negligible. You do not need to take other cars into consideration. Input The input consists of multiple test cases. Each test case is given in the format below: w h d A,1 d A,2 . . . d A,w−1 d D,1 d D,2 . . . d D,h−1 ns 1,1 ew 1,1 s 1,1 . . . ns w,1 ew w,1 s w,1 ns 1,2 ew 1,2 s 1,2 . . . ns w,h ew w,h s w,h x s y s x d y d Two integers w and h (2 ≀ w , h ≀ 100) in the first line indicate the number of avenues and drives in the city, respectively. The next two lines contain ( w - 1) and ( h - 1) integers, each of which specifies the distance between two adjacent avenues and drives. It is guaranteed that 2 ≀ d A,i , d D,j ≀ 1, 000. The next ( w × h ) lines are the configuration of traffic signals. Three parameters ns i,j , ew i,j and s i,j describe the traffic signal ( i , j ) which stands at the crossing point of the i -th avenue and the j -th drive. ns i,j and ew i,j (1 ≀ ns i,j , ew i,j < 100) are time intervals for which the light for a meridional direction and a zonal direction is green, respectively. s i,j is the initial state of the light: 0 indicates the light is green for a meridional direction, 1 for a zonal direction. All the traffic signal have just switched the state to s i,j at your departure time. The last two lines of a case specify the position of your house ( x s , y s ) and your friend’s house ( x d , y d ), respectively. These coordinates are relative to the traffic signal (0, 0). x -axis is parallel to the drives, and y -axis is parallel to the avenues. x-coordinate increases as you go east, and y-coordinate increases as you go north. You may assume that these positions are on streets and will never coincide with any intersection. All parameters are integers and separated by a blank character. The end of input is identified by a line containing two zeros. This is not a part of the input and should not be processed. Output For each test case, output a line containing the shortest possible time from your house to your friend’s house. Sample Input 4 4 2 2 2 2 2 2 99 1 0 1 99 1 1 99 1 1 99 1 1 99 1 1 99 1 1 99 1 99 1 0 99 1 0 1 99 1 1 99 1 1 99 1 1 99 1 1 99 1 1 99 1 99 1 0 1 0 1 6 2 2 10 10 5 5 0 5 5 0 5 5 0 5 5 0 5 0 5 10 0 0 Output for the Sample Input 28 25
39,206
Flick Input A: フリック入力 二次元が倧奜きな倧孊生通称2D君は長幎䜿甚しおいたガラパゎス携垯からスマホに買いかえた初めおスマホを操䜜した2D君は文字入力の方匏が昔の携垯ず少し違うこずに気付いたスマホでは画面をタッチ・フリックできるこずを利甚しおフリック入力ずいう方匏が採甚されおいたのである(フリックずは「画面䞊で指を滑らせお匟く操䜜」を意味する)フリック入力は次に瀺すような文字入力方匏である フリック入力では図A(a)のように0-9の10個のボタンにより入力を行う(#ず*に぀いおは今回は省略しおいる)各ボタンには図のようにあ行-わ行が1぀ず぀割り圓おられおいる 図A: スマホのボタン詳现 ある1぀のボタンは図A(b)に衚す方法により操䜜するこずができる぀たり ボタンを「タッチ」するだけだずそのボタンに察応する「あ段」の文字を出力 ボタンを「巊に向かっおフリック」するず「い段」を出力 ボタンを「䞊に向かっおフリック」するず「う段」を出力 ボタンを「右に向かっおフリック」するず「え段」を出力 ボタンを「䞋に向かっおフリック」するず「お段」を出力 ずいうこずであるフリック入力の仕方を党おのボタンに぀いお衚すず図A(c)のようになる「ん」を出力するためにはボタン0を䞊偎にフリックするこずに泚意しおほしい あなたの仕事はフリック入力の操䜜を瀺す文字列が䞎えられたずきその操䜜の結果出力される文字列を求めるこずである Input フリック入力の操䜜を衚す文字列が1行で䞎えられる(2文字以䞊1000文字以䞋)この文字列は操䜜するボタンを衚す'0'-'9'の数字ずフリック方向を衚す'T''L''U''R''D'の5皮類の文字の組み合わせからなる フリック方向を衚す文字は次の意味を衚す 'T'タッチするだけ 'L'巊にフリックする 'U'䞊にフリックする 'R'右にフリックする 'D'䞋にフリックする 䟋えば"2D"は「2のボタンを䞋に向かっおフリック」ずいう操䜜を衚すため「こ」を出力するこずができる 䞎えられる文字列は数字1文字ずフリック方向を衚す1文字が亀互に珟れるたた必ず数字1文字から始たりフリック方向を衚す1文字で終わるさらに図A(c)においお文字がない堎所に向かっおフリックを行うこずはない("8L"などの入力はない) Output フリック入力操䜜の結果を衚す文字列をロヌマ字衚蚘で出力せよ ロヌマ字の子音には か行'k' さ行's' た行't' な行'n' は行'h' た行'm' や行'y' ら行'r' わ行'w' を䜿甚するこずひらがな1文字を衚すロヌマ字は䞊蚘で衚される子音ず母音'a''i''u''e''o'をセットにしお2文字で出力するこず"shi"や"fu"はこの条件に反するため間違いであるただし「あ行」の文字は母音1文字だけを出力し「ん」は'n'を2回続けお"nn"ず出力するこず 文字列の最埌には改行を出力するこず Sample Input 1 5R2D Sample Output 1 neko Sample Input 2 8U9U6U0T Sample Output 2 yuruhuwa Sample Input 3 9L4U7R1L2D0U4R3U4D Sample Output 3 ritumeikonntesuto
39,207
E: 埀埩文字列 / Round-trip String 物語 ここは党人類が競技プログラマヌになった䞖界。この䞖界ではみなさんの生きる珟代では考えられないような習慣が倚数存圚する。その䞭の䞀぀に、䞡芪、特に母芪に感謝の蚌を衚す時は、文字列を送るずいう颚習がある。送る文字列ずしおどのような文字列がふさわしいかは地域差があるが、北の倧地北海道では、できるだけシンプルな文字列が莈り物ずしおふさわしい。たた、送られた偎の母芪は子䟛に返瀌ずしお、送られた文字列がどれほど矎しいかを答える必芁がある。 さお、今あなたは1児の母芪から盞談を受けおいる。その母芪は子䟛から玠晎らしそうな文字列をもらったのだが、どれくらい玠晎らしいのかうたく枬れないでいるそうだ。北海道の颚習では、母芪に送る文字列 T はある文字列 S を埀埩させお䜜るこずになっおいる。この時、 S ができるだけ短い文字列ならばより玠晎らしい文字列 T である。母芪は子䟛の頑匵りに報いるため、 T を䜜るこずができる文字列の䞭で、最短の文字列を芋぀けおあげたいが、それはなかなか難しいずのこずだ。あなたのプログラミング胜力を駆䜿しお母芪の悩みを解決しお欲しい。 問題 長さが 2 以䞊の文字列 S から、以䞋のルヌルに埓っお長さ N の文字列 T を生成する。ここで、 S の文字列長を K ずし、 T[i] は文字列 T の i 番目の文字を衚す。ここで、 T の先頭文字は 0 番目の文字ずする。 T[i] = S[K - 1 - | (i $\bmod$ (2K - 2)) - K + 1 |] T のみが䞎えられたずき、 T を生成する文字列は耇数考えられる。 それらの文字列の䞭で最小の文字列長 K_{min} を出力せよ。 入力圢匏 N T 制玄 2 \leq N = |T| \leq 1,000,000 文字列は英小文字のみからなる 出力圢匏 T を生成する長さ最小の文字列 S の長さ K_{min} を出力しおください。 入力䟋1 6 tabata 出力䟋1 3 tabata 、 tabat 、 tab の 3 ぀が考えられたすが、 tab が長さ最小なので、答えは 3 です。 入力䟋2 4 hcpc 出力䟋2 3 入力䟋3 60 aaaaaabbbbbbbbbbbaaaaaaaaaaabbbbbbbbbbbaaaaaaaaaaabbbbbbbbbb 出力䟋3 12
39,208
Prime Number Write a program which reads an integer n and prints the number of prime numbers which are less than or equal to n . A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7. Input Input consists of several datasets. Each dataset has an integer n (1 ≀ n ≀ 999,999) in a line. The number of datasets is less than or equal to 30. Output For each dataset, prints the number of prime numbers. Sample Input 10 3 11 Output for the Sample Input 4 2 5
39,209
Set Intersection Find the intersection of two sets $A = \{a_0, a_1, ..., a_{n-1}\}$ and $B = \{b_0, b_1, ..., b_{m-1}\}$. 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}$ Elements of $A$ and $B$ are given in ascending order respectively. There are no duplicate elements in each set. Output Print elements in the intersection in ascending order. Print an element in a line. Constraints $1 \leq n, m \leq 200,000$ $0 \leq a_0 < a_1 < ... < a_{n-1} \leq 10^9$ $0 \leq b_0 < b_1 < ... < b_{m-1} \leq 10^9$ Sample Input 1 4 1 2 5 8 5 2 3 5 9 11 Sample Output 1 2 5
39,210
シャッフル 問題 1 から n たでの番号が曞かれた n 枚のカヌドがあるたず䞀番䞊が番号 1 のカヌド䞊から2枚目が番号 2 のカヌド 䞀番䞋が番号 n のカヌドずなるように順番に重ねおカヌドの山を䜜る カヌドの山に察しお「シャッフル(x,y)」ず呌ばれる次のような操䜜を行うこずでカヌドを䞊び替えるx, y は 1 ≀ x < y < n をみたす敎数 シャッフル(x,y) n 枚のカヌドを䞀番䞊から x 枚目たでのカヌドからなる山A x+1 枚目から y 枚目のカヌドからなる山B y+1 枚目から n 枚目のカヌドからなる山C の3぀の山に分けるそしお山Aの䞊に山Bを重ねさらにその䞊に山Cを重ねる 䟋えば順番に䞊んでいる9枚のカヌドに察しお「シャッフル(3,5)」を行うず 9 枚のカヌドに曞かれた番号は, 䞊から順番に 6, 7, 8, 9, 4, 5, 1, 2, 3 ずなる 最初の山の状態から m 回のシャッフル「シャッフル(x 1 , y 1 )」「シャッフル(x 2 , y 2 )」 
 「シャッフル(x m , y m )」を順番に行った埌のカヌドの山においお䞊から数えお p 枚目から q 枚目のカヌドの䞭に番号が r 以䞋のカヌドが䜕枚含たれおいるかを求めるプログラムを䜜成せよ 入力 入力は耇数のデヌタセットからなる各デヌタセットは以䞋の圢匏で䞎えられる 入力は m+3 行からなる 1 行目にはカヌドの枚数 n が曞かれおいる3 ≀ n ≀ 1000000000 = 10 9  2 行目にはシャッフルの回数を衚す敎数 m が曞かれおいる1 ≀ m ≀ 5000 3 行目には敎数 p, q, r が曞かれおいる1 ≀ p ≀ q ≀ n, 1 ≀ r ≀ n i + 3 行目1 ≀ i ≀ mには2぀の敎数 x i , y i (1 ≀ x i < y i < n) が空癜を区切りずしお曞かれおいる n が 0 のずき入力の終了を瀺す. デヌタセットの数は 5 を超えない 出力 デヌタセットごずに, m 回のシャッフル埌のカヌドの山においお䞊から数えお p 枚目から q 枚目のカヌドの䞭に含たれおいる番号が r 以䞋のカヌドの枚数を行に出力せよ 入出力䟋 入力䟋 9 1 3 7 4 3 5 12 3 3 8 5 3 8 2 5 6 10 0 出力䟋 2 3 1぀目の入力䟋の山に察しお, 「シャッフル(3,5)」を行うずカヌドは䞊から順番に 6, 7, 8, 9, 4, 5, 1, 2, 3 ずなる䞊から数えお 3 枚目から 7 枚目に含たれる番号が 4 以䞋のカヌドは番号 4 ず番号 1 の 2 枚である たた2぀目の入力䟋の山に察しお, 「シャッフル(3,8)」「シャッフル(2,5)」「シャッフル(6,10)」を順番に行うずカヌドは䞊から順番に 9, 10, 3, 11, 12, 4, 5, 6, 7, 8, 1, 2 ずなる䞊から数えお 3 枚目から 8 枚目に含たれる番号が 5 以䞋のカヌドは 3 枚である 䞊蚘問題文ず自動審刀に䜿われるデヌタは、 情報オリンピック日本委員䌚 が䜜成し公開しおいる問題文ず採点甚テストデヌタです。
39,211
Score : 500 points Problem Statement Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists. Let s' be the concatenation of 10^{100} copies of s . t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s' ). Notes A subsequence of a string a is a string obtained by deleting zero or more characters from a and concatenating the remaining characters without changing the relative order. For example, the subsequences of contest include net , c , and contest . Constraints 1 \leq |s| \leq 10^5 1 \leq |t| \leq 10^5 s and t consists of lowercase English letters. Input Input is given from Standard Input in the following format: s t Output If there exists an integer i satisfying the following condition, print the minimum such i ; otherwise, print -1 . Sample Input 1 contest son Sample Output 1 10 t = son is a subsequence of the string contestcon (the first 10 characters in s' = contestcontestcontest... ), so i = 10 satisfies the condition. On the other hand, t is not a subsequence of the string contestco (the first 9 characters in s' ), so i = 9 does not satisfy the condition. Similarly, any integer less than 9 does not satisfy the condition, either. Thus, the minimum integer i satisfying the condition is 10 . Sample Input 2 contest programming Sample Output 2 -1 t = programming is not a substring of s' = contestcontestcontest... . Thus, there is no integer i satisfying the condition. Sample Input 3 contest sentence Sample Output 3 33 Note that the answer may not fit into a 32 -bit integer type, though we cannot put such a case here.
39,212
F: Opeartion training for BYDOL / BYDOLの操䜜蚓緎 u'sずいうスクヌルアむドルを結成した私達はさらなる高みを目指すため郚宀にあったDVDで憧れの841プロダクションに所属するアむドル達を芋おいた このアむドル達は今でこそ茝くステヌゞの䞊で掻躍しおいるが以前はBYDOLず呌ばれるロボットに乗り蟌み隕石の撀去䜜業をしおいたらしい そこでトップアむドルになるためにBYDOLの操瞊技術が必芁であるこずを知った私達は急遜BYDOLを入手し他校のスクヌルアむドルずBYDOLを甚いた暡擬戊をするこずにした この暡擬戊では癜の軍ず赀の軍に分かれBYDOLに積たれおいる匟により撃ち合いを行う 各軍のメンバはそれぞれBYDOLに乗り蟌み合図が出るず同時に 「1床だけ」 匟を撃぀ 合図が出る時点での党おのBYDOLの座暙は決たっおおりBYDOL同士はレヌダヌによっお互いの座暙を把握できる 同じ座暙にBYDOLが2台以䞊珟れるこずはない BYDOLが移動できるフィヌルドは2次元平面で衚され非垞に広くフィヌルド党䜓を囲む壁はないものずしお考える しかしフィヌルド内郚にいく぀かの円圢の壁が甚意されおいる この壁の内偎はコンクリヌトで塗り固められおいるためBYDOLが䟵入するこずはできない 壁を構成する円は互いに接したり亀差したり内包関係になったりするこずはなくBYDOLがいる座暙䞊に壁は存圚しない 各BYDOLには合図が出る時点で䜍眮する座暙射皋1床に発射できる匟数壁で反射する匟を積んでいるか倒された時に敵軍が埗る点数の5぀のパラメヌタが蚭定されおいる 匟はBYDOLがいる䜍眮から360床どの方向ぞも発射するこずができ䞀盎線状に進む 発射されおから進んだ匟の距離が射皋の倀以䞋である間か぀他のBYDOLや壁に匟が圓たらない間であれば匟は進み続ける フィヌルド䞊を動く党おの匟は匟道が亀差しおいおも衝突しないものずする BYDOLによっおは壁で反射する匟を積んでいる堎合がある この堎合のBYDOLの匟は党お1回のみ壁で反射する ただし自分のBYDOLから壁の反射した地点たでの距離ず反射した地点から盞手のBYDOLたでの距離の合蚈が射皋の倀以䞋でなければ盞手に圓たったこずにはならない 壁で反射するずきは図1のように匟が壁に圓たった地点に察しお接線を匕きこの線を基準に入射角 Ξ 1 ず反射角 Ξ 2 が等しくなるように反射する 匟道が壁を構成する円に接する堎合そのたたたっすぐ匟は進み壁で反射したこずにはならない 図1: 匟の反射 あるBYDOLは発射できる匟数の範囲で1床に耇数の匟を撃ち耇数のBYDOLに圓おるこずができるただし同じBYDOLが党く同じ方向ぞ向かっお匟を2発以䞊撃぀こずは蚱されない 各BYDOLには倒された時に敵軍が埗る点数が蚭定されおいる この撃ち合いではより倚くの点数を埗た軍が勝者ずなる 同じ点数を埗た堎合は匕き分けずする BYDOLは1発匟を圓おれば倒れるため同じBYDOLに䜕発撃ち蟌もうが埗られる点数は1床きりである 味方のBYDOLを撃った堎合倱栌になっおしたうため絶察に撃っおはいけない 自滅するこずも蚱されない u'sにおける䜜戊指揮担圓であるあなたの仕事は各軍が最倧の点数をずれるように行動したずきの点数を求めどちらの軍が勝぀こずができるのかを刀定するこずである これができればu'sはトップアむドルぞ䞀歩近づくこずができるだろう Input 入力は次の圢匏で衚される n m o ax 1 ay 1 ap 1 aq 1 ar 1 as 1 ... ax n ay n ap n aq n ar n as n bx 1 by 1 bp 1 bq 1 br 1 bs 1 ... bx m by m bp m bq m br m bs m wx 1 wy 1 wr 1 ... wx o wy o wr o 党おの数倀は敎数で入力される n , m (1 ≩ n , m ≩ 50) は癜の軍ず赀の軍のBYDOLの数 o (0 ≩ o ≩ 15) は壁の数を衚す ax i , ay i (0 ≩ ax i , ay i ≩ 100) はi番目の癜の軍のBYDOLの座暙 ap i (0 ≩ ap i ≩ 200) は射皋 aq i (1 ≩ aq i ≩ 5) は1床に発射できる匟の数 ar i (0 <= ar i <= 1) は壁で反射する匟を積んでいるかどうか as i (0≩ as i ≩ 100) は倒された時に敵軍が埗る点数である ar i が0であるずきは所持しおいる匟が反射できないこず1であるずきは反射できるこずを衚しおいる 赀の軍のBYDOLの情報も同様に䞎えられる その埌壁の䞭心座暙( wx i , wy i )ず半埄 wr i が䞎えられる(0 ≩ wx i , wy i ≩ 100, 1 ≩ wr i ≩ 30)  Output 癜の軍ず赀の軍それぞれが最も点数が埗られるように匟を発射したずきの点数を空癜区切りで1行目に出力せよ さらに癜の軍が勝぀堎合はwin負ける堎合はlose匕き分けの堎合はdrawを2行目に出力せよ 出力にはこれら以倖の文字があっおはならない Sample Input 1 2 2 0 0 0 5 1 0 0 1 0 5 1 0 0 0 1 5 1 0 10 1 1 5 1 0 10 Sample Output 1 20 0 win Sample Input 2 2 3 0 0 0 5 1 0 20 1 0 5 1 0 10 10 10 4 1 0 100 10 0 1 1 0 50 4 0 5 1 0 10 Sample Output 2 10 10 draw Sample Input 3 1 1 2 0 0 20 1 1 20 10 0 20 1 0 20 5 0 2 5 8 2 Sample Output 3 20 0 win Sample Input 4 1 1 1 0 0 5 1 0 5 5 0 5 1 0 10 1 1 1 Sample Output 4 10 5 win Sample Input 5 1 2 5 13 27 19 4 0 30 14 14 34 4 1 9 49 71 21 3 1 31 5 86 18 85 27 3 90 91 1 34 50 10 92 54 15 Sample Output 5 9 30 lose
39,213
Problem I: Text Justification You are hired by the ∀I¶אΞ℘, an extraterrestrial intelligence, as a programmer of their typesetting system. Your task today is to design an algorithm for text justification . Text justification is to equalize the line widths as much as possible by inserting line breaks at appropriate posi- tions, given a word sequence called a paragraph and the width of the paper. Since you have not developed an automatic hyphenation algorithm yet, you cannot break a line in the middle of a word. And since their language does not put spaces between words, you do not need to consider about spacing. To measure how well the text is justified in one configuration (i.e., a set of lines generated by inserting line breaks to a paragraph), you have defined its cost as follows: The total cost of a paragraph is the sum of the cost of each line. The cost for the last line is defined as max(0, s - w ). The cost for other lines are given by | s - w |. where s is the sum of the widths of the words in the line, and w is the width of the paper. Please design the algorithm which takes a paragraph and calculates the configuration of the minimum cost. Input The input consists of multiple test cases. The first line of each test case contains two positive integers n and w (0 ≀ n ≀ 1000 and 0 ≀ w ≀ 1,000,000). n is the length of paragraph and w is the width of the used paper. Each of the following n lines contains one positive integer a i which indicates the width of the i -th word in the paragraph. Here it is guaranteed that 0 ≀ a i ≀ w . The input terminates with the line containing two zeros. This is not a part of any test case and should not be processed. Output For each test case, print the case number and the minimum cost for the paragraph. Sample Input 4 10 8 6 9 1 4 7 1 2 3 4 0 0 Output for the Sample Input Case 1: 4 Case 2: 1
39,215
Problem Setting Overview Concept: In this programming contest, you will run a delivery service. Customers will place orders with your shop. Each order has a unique \text{ID} and should be delivered to the corresponding customer. Your delivery service has one car. The car will fetch the ordered item from the shop and deliver it to the customer. Score: Your goal is to deliver as many items as possible, as quickly as possible in a given amount of time T_{\text{max}} . (Orders are expected until 0.95 \times T_{\text{max}} ). Constraint: In this contest there is no constraint on the number of items you can place in the car. However, an item can only be loaded in the car, by fetching it from the shop, after the order has been placed. Problem A/B: In problem A all order positions and times are given to the contestant in advance and the contestant algorithm shall optimize the moves of the car to make as many deliveries as possible as fast as possible. On the other hand, in problem B orders appear online, that is new orders appear, while you move your car to make as many deliveries as possible as fast as possible. Specification of Time and Space: Time: In this contest we model the progress of time by integer values 0 \le t < T_{\text{max}} . Map: In this contest we model a map by a simple, undirected, and connected graph G=(V, E) , consisting of a set of vertices V and a set of edges E Shop and customer locations: The vertices u \in V are labeled from 1 to |V| and the vertex u=1 denotes the location of your shop, while vertices u = 2,...,|V| denote locations of potential customers. Here, |V| denotes the number of elements of the set V . Streets: Each edge \left\{ u, v \right\} \in E represents a street connecting the vertices u, v \in V . The corresponding length is given by an integer edge weight d_{u, v} \ge 1 . Graph creation: The algorithm for generating the map graph based on a random seed is specified in the following pseudo-code. For further details, please see the sample code below. Pseudo code: Map graph generator Input: |V| , |E| , \mathrm{MaxDegree}=5 2d vertex grid: First, find the largest integer R>0 such that |V| = R^{2} + r , with r being the smallest possible non-negative integer. Then we plot points (x, y) on the 2d vertex grid (0 \leq x, y \lt R) . For each point (x, y) add a uniform random offset dx, dy \in [0, 1] , giving the final vertex position (x + dx, y + dy)\in [0,R] \times [0,R] . Finally, add the remaining r vertices at a uniform random position (x, y) with 0 \leq x, y \leq R . Vertex labels u \in V are assigned by random shuffling. The shop is the vertex u=1 . How we create Highways: To generate a highway network, we create a complete graph G_{\text{comp}} on the vertex set u \in V , assigning each vertex pair u, v \in V \times V the Euclidean distance W_{u, v} as an edge weight. Next, we construct a minimum spanning tree of G_{\text{comp}} . The |V|-1 edges of the minimum spanning tree are the highway network of the graph G . We assign each of those edges \left\{ u, v \right\} an edge weight d_{u,v} \leftarrow \lceil 2 \times W_{u, v} \rceil . How we add side roads: To create a network of side roads, we successively add |E|-(|V|-1) edges to the graph G as follows: Update \mathrm{cost}(u,v) . Among the vertex pairs \left( u, v \right) \in V\times V , not yet connected by an edge, select a pair with minimal \mathrm{cost}(u,v) . Assign the edge weight d_{u,v} \leftarrow \lceil 4 \times W_{u, v} \rceil . Here, \mathrm{cost}(u,v) is essentially based on the Euclidean distance of vertices, giving preference to connecting nearby vertices with low degree. In addition, preference is given to side roads along the rectangular grid, to avoid too many bridges. The detailed definitions are as follows: Define \mathrm{degree}(u) , the degree of vertex u\in V as the number of incident edges. Define \mathrm{color}(u) for each vertex u\in V according to its original position (x,y) on the vertex grid as: If x+y is even : \mathrm{color}(u) = 0 If x+y is odd : \mathrm{color}(u) = 1 For the remaining r vertices : Assign a color \mathrm{color}(u) \in \left\{0,1\right\} at random. Define a factor f(u,v) as follows: If \mathrm{color}(u) and \mathrm{color}(v) are the same : Set \mathrm{f}(u,v) = 5 If \mathrm{color}(u) and \mathrm{color}(v) are different : Set \mathrm{f}(u,v) = 1 Define a factor g(u) as follows: If \mathrm{degree}(u) \lt \mathrm{MaxDegree} : Set g(u)=1 If \mathrm{degree}(u) \geq \mathrm{MaxDegree} : Set g(u)=\infty Finally, the cost is defined as follows: \mathrm{cost}(u,v) = W_{u,v}\times \mathrm{degree}(u) \times \mathrm{degree}(v) \times f(u,v) \times g(u) \times g(v) . How we assign order frequencies: Assign each vertex u \in V an order frequency f_u \in \left\{0,1,2\right\} . Init the order frequency of the shop vertex: f_1 \leftarrow 0 . Init the order frequency of the other vertices: f_u \leftarrow 1 Now determine vertices with order frequency 2. For this draw a uniform random center point c=(c_x,c_y)\in [R/4,3R/4]\times[R/4,3R/4] and then for all vertices u=2,...,|V| do: If \mathrm{EuclideanDistance}(c,u)\le R/8 + \mathrm{uniformRandom}[0,R/8] : f_{u} \leftarrow 2 Specification of Car Locations and Moves: In order to make deliveries you will operate a delivery car, which can take positions and make moves as specified below. Car position: A car can generally take two types of position: on a vertex u \in V . on an edge \left\{ u, v \right\} \in E . More specifically, it is located at a distance x (0 \lt x \lt d_{u, v}) from u to v . Car move: At each step 0 \le t < T_{\text{max}} you have to choose one of the following actions in order to control your delivery car. stay : stay at the current position. move w : Take one step towards vertex w \in V . In case of choosing move w , w must obey the following constraints. A failure to obey these constraints results in a wrong answer WA . w must be a vertex, i.e., w \in V . If the car is on vertex u \in V , there must be an edge connecting u and v , i.e., \left\{ u, w \right\} \in E . If the car is on the edge \left\{ u, v \right\} \in E , w must either be w = u or w = v . Orders, Deliveries, and Constraints: Orders: Throughout the contest each order is characterized by three quantities: A unique order ID, a vertex v \in V indicating the order destination, and the order time t at which the order appeared. For the detailed format see below. Order generation: At each time 0 \le t \le T_{\text{last}} = 0.95 \times T_{\text{max}} up to one new order can appear with probability p_{\text{order}}(t) . In case there is an order, the order destination i is chosen from the vertex set V with probability proportional to the order frequency f_i . For details, see the pseudo-code below or the sample code further below. Pseudo code: Order generation Input: Last order time T_{\text{last}} and average order probability p_{\text{order}}(t) . Init: \mathrm{ID} \leftarrow 0 . For each step t = 0, ..., T_{\text{last}} do: Generate a uniform random number r \in [0,1] . If r \le p_{\text{order}}(t) : Select a vertex position u \in V at random with probability proportional to the order frequency f_{u} of the vertex. \mathrm{ID} \leftarrow \mathrm{ID} + 1 place order (new order ID, order time t , vertex position u \in V ) Else: place no order Note: The average order probability is defined as follows: p_{\text{order}}(t) = \begin{cases} t / T_{\text{peak}}, & \text{if } 0\le t \lt T_{\text{peak}}, \\ (T_{\text{last}} - t) / (T_{\text{last}}- T_{\text{peak}}), & \text{if } T_{\text{peak}} \le t \lt T_{\text{last}}, \\ 0, & \text{if } T_{\text{last}} \le t, \end{cases} where T_{\text{last}}:=0.95 \times T_{\max} and T_{\text{peak}} is drawn randomly uniform from the interval [0, T_{\text{last}}] . Note: The value of T_{\text{peak}} will not be given as an input. Delivery: To deliver an order, the contestant must do the following steps after the order has been placed: (1st) Move the car onto the shop: Note: When moving a car onto the shop, all orders with order time less than or equal to the current time, will be transfered into the car. On the other hand, orders which have not appeared yet, cannot be placed into the car. (2nd) Move the car to the customer position: To finalize a delivery, move the car onto the vertex of the customer position. Note: Orders which have not been transfered into the car yet, will not be delivered, even if you arrive at the customer position. Constraints: Throughout the contest, we assume each order has a unique \text{ID} and should be delivered to the corresponding customer. It is further assumed that an unlimited number of orders can be placed in the car. Scoring During the contest the total score of a submission is determined by summing the score of the submission with respect to 30 input cases. After the contest a system test will be performed. To this end, the contestant's last submission will be scored by summing the score of the submission on 100 previously unseen input cases. For each input case, the score is calculated as follows: \text{Score} = \sum_{i \in D} {(T_{\text{max}})}^{2} - {(\mathrm{waitingTime}_i)}^{2}, Here we use the following definitions: D : the set of orders delivered until t=T_{\text{max}} the waiting time of the i th order: \mathrm{waitingTime}_i = \mathrm{deliveredTime}_i - \mathrm{orderedTime}_i . Note that an input case giving the output WA will receive 0 points. Particulars of Problem B: Problem B is an interactive contest, where the contestant code successively receives updates on newly generated and delivered orders from a host code, while simultaneously servicing the customer by moving the car to a neighboring position in every step t=0,...,T_{\max}-1 . The precise flow which details the interaction of the contestant and host code is shown below. Contestant Code Host Code Generate and output graph G + Time t : Generate and output new orders + Time t : If on shop, output orders loaded into car + Time t : Determine and output a move + Check feasibility of move; If move unfeasible: output NG , If feasible: output OK + Time t+1 : update and output information on delivered items (if any) Note: The host code outputs the graph only once. The processes marked by a "+" on the left side of the table are repeated iteratively for integers t in t = 0,..., T_{\max} - 1 . Input/Output Format for the Standard IO At first, the host code will output a graph G , the order frequencies f_{i} for each vertex i , which are proportional to the probability of an order to appear at vertex i , and the total number of steps T_{\max} . |V| |E| u_1 v_1 d_{u_1, v_1} u_2 v_2 d_{u_2, v_2} \vdots u_{|E|} v_{|E|} d_{u_{|E|}, v_{|E|}} f_1 f_2 \ldots f_{|V|} T_{\max} First line: |V| number of vertices, |E| number of edges The next |E| lines: The edges of the graph. In particular, the i th line denotes the vertices u_i and v_i which form an edge, along with the corresponding edge weight d_{u_i, v_i} . The next line: The order frequencies f_i which determine the probability of an order at vertex i as p_{i}=\frac{f_{i}}{\sum_{i}f_{i}} . The last line: The total number of car updates T_{\max} . At time t we first obtain the following information through the standard input. N_{\text{new}} \mathrm{new\_id}_1 \mathrm{dst}_1 \mathrm{new\_id}_2 \mathrm{dst}_2 \vdots \mathrm{new\_id}_{N_{\text{new}}} \mathrm{dst}_{N_{\text{new}}} N_{\text{put}} \mathrm{put\_id}_1 \mathrm{put\_id}_2 \mathrm{put\_id}_{N_{\text{put}}} N_{\text{new}} represents the number of new orders which appeared at time t . The next N_{\text{new}} lines give the newly generated order information. The i th order information indicates that the order ID \mathrm{new_{id}}_i of the new order, while \mathrm{dst}_i denotes the vertex to which the customer wishes the order to be delivered. N_{\text{put}} represents the number of items transfered into the car at time t . If the car is not at the vertex of the store N_{\text{put}} will be zero. The subsequent N_{\text{put}} lines indicate the order information for the newly loaded items. In particular, the i th line indicates that the order ID corresponding to the product loaded in the car is \mathrm{put_{id}}_i . Next, in order to move the delivery car to a neighboring position the contestant code must at every time t ( 0 \leq t \lt T_{\max} ) output the following \mathrm{command} to the standard output. \mathrm{command} Here, \mathrm{command} must be of the following form If you want the car to stay at its current position, send -1 to the standard output If you want the car to move one step towards a neigboring vertex move w , send w to the standard output Note: In case you choose move w , w must meet all of the following conditions. If any of the following conditions is violated, the host code will output NG and the contestant should terminate the program, ultimately leading to WA (incorrect). w is a vertex index with w \in \left\{1, ... , |V|\right\} If the car is on a vertex u , the edge \left\{ u, w \right\} \in E must exist If the car is on an edge \left\{ u, v \right\} , w must either be w = u or w = v After your action at time t is send to the standard output, the host code will send the following information about time t + 1 to the standard input. \mathrm{verdict} N_{\text{achieve}} \mathrm{achieve\_id}_1 \mathrm{achieve\_id}_2 \vdots \mathrm{achieve\_id}_{N_{\text{achieve}}} \mathrm{verdict} is a string indicating whether your action at time t was valid. It can be one of the two following options. OK : Indicating that your action was feasible NG : Indicates that your action is infeasible. If you receive this input, you must terminate the program immediately. It is guaranteed to be WA (incorrect), if it is terminated immediately.If you do not terminate immediately the behavior is undefined. N_{\text{achieve}} represents the number of orders that have been delivered at time t . If the car is not at a delivery vertex, no orders have been delivered and N_{\text{achieve}}=0 . The subsequent N_{\text{achieve}} lines indicate the delivered orders. In particular, the i th line indicates the order ID \mathrm{achieve\_{id}}_i . Finally, after receiving the standard input of the host code after the last step T_{\max} you must terminate the program immediately. Constraints I/O Constraints All numbers given through the standard input are integers. All outputs must be integers T_{\text{max}} = 10000 200 \leq |V| \leq 400 1.5 |V| \leq |E| \leq 2|V| 1 \leq u_{i}, v_{i} \leq |V| (1 \leq i \leq |E|) 1 \leq d_{u_i, v_i} \leq \lceil 4\sqrt{2|V|} \rceil (1 \leq i \leq |E|) The given graph has no self-loops, no multiple edges and is guaranteed to be connected. f_1 = 0 f_i \in \left\{ 1, 2 \right\} ( 2 \leq i \leq |V| ) \mathrm{verdict} \in \left\{ \text{"OK"}, \text{"NG"} \right\} 0 \leq N_{\text{new}} \leq 1 1 \leq \mathrm{new\_id}_{i} \leq T_{\text{last}}+1 (1 \leq i \leq N_{\text{new}}) . Note: If all orders are generated by the order generation rule as explained above, the total number of orders is at most T_{\text{last}}+1 . Therefore, the possible range of \mathrm{new\_id}_{i} should be from 1 to T_{\text{last}}+1 . The order IDs \mathrm{new\_{id}}_i are unique. 2 \leq \mathrm{dst}_i \leq |V| (1 \leq i \leq N_{\text{new}}) The integer which the contestant outputs to the standard output at time t must either be -1 or 1 \leq w \leq |V| Input/Output Example Time Contestant Host Code Explanation 5 7 1 2 5 5 3 4 2 4 8 1 5 1 2 3 3 4 5 3 4 3 9 0 1 1 5 5 500 At first, the host code provides the graph data through the standard input. In this example, the graph has |V| = 5 vertices and | E | = 7 edges. Next, the order frequency for each vertex is given in one line. Finally, T_{\max} is given. 0 \rightarrow 1 1 1 5 1 1 At time t=0 we get one order. This order has ID = 1 and should be delivered to vertex 5 . Because your car is currently at vertex one, the order will be automatically transfered into your car. In this way, when your car is at the shop, all orders which have been made at present and before, will automatically be loaded into your car. 2 You decided to move one step towards vertex. OK 0 The first line indicates that your move was feasible. The second line shows that no orders have been delivered. 1 \rightarrow 2 1 2 2 0 One new order (ID =2 , delivery vertex =2 ) has occured. Your car is on the edge between vertex 1 and 2 , so zero orders have been transfered to your car. -1 You decided to keep your car in the same position. OK 0 Your move was valid. No orders will be delivered, because you are not at a delivery item position. 2 \rightarrow 3 1 3 4 0 A new order (ID =3 , delivery vertex =4 ) has appeared. 1 You decided to move back one step towards vertex 1 . In this way you are allowed to perform a U-turn. OK 0 No orders have been delivered. 3 \rightarrow 4 0 2 2 3 Since the car has returned to the store, products corresponding to order ID 2 and 3 are loaded onto the car. 5 The contestant has decided to move one step towards vertex 5. OK 1 1 Since you arrived at vertex 5, the order with ID 1 was delivered. 4 \rightarrow 5 0 0 There is no new order. 5 The contestant decides to move one step towards vertex 5 . NG The input was invalid and you should terminate your program. Using the Standard Output When returning your move instruction to the standard output, please use the flush command. As an example, consider the case where you want to output -1 . This is how to do it in some of the major programming languages. C++ std::cout << "-1" << std::endl; Java System.out.println("-1"); Python 3.4 print("-1", flush=True) Sample Code B A software toolkit for generation of input samples, scoring and testing on a local contestant environment, and sample codes for beginners is provided through the following link . In addition we provide software for visualizing the contestants results.
39,217
Score : 200 points Problem Statement Snuke signed up for a new website which holds programming competitions. He worried that he might forget his password, and he took notes of it. Since directly recording his password would cause him trouble if stolen, he took two notes: one contains the characters at the odd-numbered positions, and the other contains the characters at the even-numbered positions. You are given two strings O and E . O contains the characters at the odd-numbered positions retaining their relative order, and E contains the characters at the even-numbered positions retaining their relative order. Restore the original password. Constraints O and E consists of lowercase English letters ( a - z ). 1 \leq |O|,|E| \leq 50 |O| - |E| is either 0 or 1 . Input Input is given from Standard Input in the following format: O E Output Print the original password. Sample Input 1 xyz abc Sample Output 1 xaybzc The original password is xaybzc . Extracting the characters at the odd-numbered positions results in xyz , and extracting the characters at the even-numbered positions results in abc . Sample Input 2 atcoderbeginnercontest atcoderregularcontest Sample Output 2 aattccooddeerrbreeggiunlnaerrccoonntteesstt
39,218
Score : 400 points Problem Statement There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i -th row from the top and the j -th column from the left. These squares have to be painted in one of the C colors from Color 1 to Color C . Initially, (i,j) is painted in Color c_{i,j} . We say the grid is a good grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N : If (i+j) \% 3=(x+y) \% 3 , the color of (i,j) and the color of (x,y) are the same. If (i+j) \% 3 \neq (x+y) \% 3 , the color of (i,j) and the color of (x,y) are different. Here, X \% Y represents X modulo Y . We will repaint zero or more squares so that the grid will be a good grid. For a square, the wrongness when the color of the square is X before repainting and Y after repainting, is D_{X,Y} . Find the minimum possible sum of the wrongness of all the squares. Constraints 1 \leq N \leq 500 3 \leq C \leq 30 1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j) 1 \leq c_{i,j} \leq C All values in input are integers. Input Input is given from Standard Input in the following format: N C D_{1,1} ... D_{1,C} : D_{C,1} ... D_{C,C} c_{1,1} ... c_{1,N} : c_{N,1} ... c_{N,N} Output If the minimum possible sum of the wrongness of all the squares is x , print x . Sample Input 1 2 3 0 1 1 1 0 1 1 4 0 1 2 3 3 Sample Output 1 3 Repaint (1,1) to Color 2 . The wrongness of (1,1) becomes D_{1,2}=1 . Repaint (1,2) to Color 3 . The wrongness of (1,2) becomes D_{2,3}=1 . Repaint (2,2) to Color 1 . The wrongness of (2,2) becomes D_{3,1}=1 . In this case, the sum of the wrongness of all the squares is 3 . Note that D_{i,j} \neq D_{j,i} is possible. Sample Input 2 4 3 0 12 71 81 0 53 14 92 0 1 1 2 1 2 1 1 2 2 2 1 3 1 1 2 2 Sample Output 2 428
39,219
Problem J: Ukunichia Query Problem $N$ 人の人が巊から右ぞ䞀列に䞊んでいる。圌らの間では文字列 $S$ が流行しおいる。 各人は、以䞋の条件を満たすずき 幞せ であり、そうでないずき 幞せ ではない。 今たでに $|S|$ 文字以䞊の文字を䌝えられおいお、か぀盎近の $|S|$ 文字を叀い順から新しい順に䞊べるず $S$ ず䞀臎する 以䞋の $2$ 皮類のク゚リを合蚈 $Q$ 回凊理せよ。 ク゚リ1 $1$ $l$ $r$ $c$ 区間 $[l, r]$ に含たれる人に文字列 $c$ を巊から䞀文字ず぀䌝える。 ク゚リ2 $2$ $l$ $r$ 区間 $[l, r]$ に含たれる 幞せ な人の数を求める。 ただし、区間 $[l, r]$ ずは、巊から $l$ 番目から $r$ 番目たでの人のこずを衚す。 Input 入力は以䞋の圢匏で䞎えられる。 $S$ $N$ $Q$ $query_1$ $\vdots$ $query_Q$ $1$ 行目に流行しおいる文字列 $S$ が䞎えられる。 $2$ 行目に䞊んでいる人の数 $N$ ずク゚リの数 $Q$ が空癜区切りで䞎えられる。 $3$ 行目から続く $Q$ 行にク゚リの情報が䞎えられる。 Constraints 入力は以䞋の条件を満たす。 $1 \leq |S| \leq 20 $ $1 \leq N \leq 10^5 $ $1 \leq Q \leq 10^5 $ $1 \leq l \leq r \leq N$ $1 \leq |c| \leq 10 $ $S, c$ は英小文字からなる 各ク゚リはク゚リ1かク゚リ2のいずれかである ク゚リ2が必ず䞀぀以䞊含たれる Output 各ク゚リ2に぀いお、 幞せ な人の数を1行に出力せよ。 Sample Input 1 abab 5 5 2 2 4 1 1 5 abab 2 3 5 1 3 3 a 2 1 5 Sample Output 1 0 3 4 Sample Input 2 uku 1333 5 2 232 423 1 13 532 uku 2 322 567 1 3 33 ku 2 1 333 Sample Output 2 0 211 321 Sample Input 3 aabb 1879 20 2 69 1585 1 415 1680 aabb 1 756 1628 abbabbaa 1 849 1273 abba 2 418 1172 2 1063 1164 2 203 623 2 481 1209 1 107 110 ababaaaab 1 857 985 bbbbabbbaa 1 868 947 aaa 1 1619 1789 aabab 2 204 844 2 493 1422 2 821 1499 1 757 1817 abbabbb 2 232 911 1 653 797 aaabaaaab 2 701 1657 1 868 940 aaabbbaaa Sample Output 3 0 338 0 209 275 341 263 0 341 0
39,220
人生ゲヌム 倪郎君は、おもちゃ屋さんに䌚接ホビヌ瀟補の人生ゲヌムを買いに行きたした。人生ゲヌムは、マス目の曞かれたボヌドずルヌレットを䜿っお遊びたす。ボヌドには図のようにスタヌト地点ずゎヌル地点が䞀぀ず぀あり、それらはひず぀ながりのマス目で぀ながっおいたす。最初に、コマはスタヌト地点のマスに眮かれ、ルヌレットを回しお出た数によっおコマを進めたす。マスによっおは、そこに止たったり通過したりするこずでお金を埗たり、コマの䜍眮を倉えたりするむベントマスがありたす。最終的な勝敗は、コマがゎヌル地点に到達した時点の所持金の倚寡で決たりたす。 この䌚瀟の人生ゲヌムの面癜いずころは、ルヌレットの出る目の倧きさ、ゎヌルたでのマスの数、むベントマスの配眮がひず぀ひず぀パッケヌゞごずに異なるずころです。それらはケヌスに曞かれおおり、それを読むこずで確認するこずができたす。お金を最も埗られる人生ゲヌムを遞びたい倪郎君は、埗るお金の期埅倀が䞀番倧きいものを買いたがっおいたす。そこであなたは、倪郎君のゲヌム遞びを手䌝うこずにしたした。 ルヌレットは、円呚を X 等分に区分され、それぞれに V 1 、 V 2 、...、 V X ずいう倀が蚘入されおいるずしたす。ボヌドには、0 番、1 番、...、 Y 番ず番号がふられたマス目があり、順番に぀ながっおいたす。マス目の䞭には、むベントマスず呌ばれる特別なマスが Z 個あり、そこに到達するず特別な動䜜を行いたす。むベントマスのマス目の番号は N i で䞎えられたす。むベントマスには 1 ~ 3 の皮類 ( E i ) があり、それぞれ次の動䜜が行われたす。 皮類 ( E i ) 特別動䜜 倀 ( A i ) の範囲 1 指定の倀 A i だけ先ぞ進む 1~10 2 指定の倀 A i の金額を埗る 1~100 3 指定の倀 A i の金額を支払う 1~100 最初の所持金は 0 円で、第 0 マス目からスタヌトし、第 Y マス目に到達するずゎヌルずなりたす。ゎヌルを越えた堎合もゎヌルず芋なしたす。スタヌトずゎヌルにむベントは無く、1 マスに耇数のむベントが重なるこずはありたせん。むベントによっお進んだ先のマスのむベントは無芖したす。所持金が 0 円より少なくなる堎合は 0 円ずしたす。 䟋えば、ある人生ゲヌムで埗られるお金の期埅倀は以䞋のように蚈算できたす。 この䟋では、スタヌト、むベントマス(100 円入手) 、ゎヌルの 3 ぀のマスず、1 か 2 が出るルヌレットからなる人生ゲヌムが衚されおいたす。たず、1 回目にルヌレットを回した時、1 が出ればむベントマスに到達し、所持金は 100 円になりたす。䞀方、2 が出た堎合はゎヌルに到達し、所持金は 0 円のたたです。これらはどちらも 2 分の 1 の確率で起こりたす。 さらに、1 回目でむベントマスに到達した堎合は 2 回目のルヌレットを回したすが、どの倀が出おもゎヌルに到達し、所持金はどの堎合も 100 円です。 このように、党郚で 3 通りのゎヌルの仕方がありたす。ゎヌルした時点の所持金に着目するず、0 円になる堎合が 1 通りでその確率は 2 分の 1、100 円になる堎合が 2 通りでその確率が 4 分の 1 です。この堎合、ゎヌルでの所持金の期埅倀は、ゎヌルの仕方ごずの (所持金 × その確率) を足した倀であり、この人生ゲヌムの期埅倀は 50 円になりたす。 ルヌレットの情報ずボヌドの情報を入力ずし、ゎヌル時の所持金の期埅倀を出力するプログラムを䜜成しおください。 Input 耇数のデヌタセットの䞊びが入力ずしお䞎えられたす。入力の終わりはれロみっ぀の行で瀺されたす。各デヌタセットは以䞋の圢匏で䞎えられたす。 X Y Z V 1 V 2 ... V X N 1 E 1 A 1 N 2 E 2 A 2 : N Z E Z A Z X (1 ≀ X ≀ 4)、 V i (1 ≀ V i ≀ 10)、 Y (1 ≀ Y ≀ 50)、 N i (1 ≀ N i ≀ Y -1)、 Z (0 ≀ Z ≀ Y -1)、 E i (1 ≀ E i ≀ 3)、 A i (1 ≀ A i ≀ 100) は敎数で䞎えられたす。 デヌタセットの数は 100 を超えたせん。 Output 入力デヌタセットごずに、最終的な所持金の期埅倀を行に出力したす。なお、所持金の期埅倀は小数点以䞋切り捚おの敎数で出力しおください。 Sample Input 1 2 0 1 1 2 1 1 1 2 100 1 2 1 2 1 2 100 2 2 1 1 2 1 2 100 4 5 3 1 2 3 4 1 1 2 2 2 100 4 3 60 0 0 0 Output for the Sample Input 0 100 0 50 20
39,221
Problem D: The Great Summer Contest 珟圚、人々の嚯楜はプログラミングコンテストに限られおいる。圌女が所属しおいるずある䞭孊校の嚯楜郚の掻動内容は、プログラミングコンテストを䌁画し運営するこずだ。圌女の仕事は問題を䜜成するこずではない。倚くの人から問題を募り、審刀団を組織し、たたコンテストの宣䌝を行う、いわば裏方のお仕事だ。カリスマ䜜題者や著名なアルゎリズマヌず違い、そういった仕事をする人に光が圓たるこずはほずんどない。存圚感はないけれども欠かすこずのできないその仕事に、圌女は誇りを持っお取り組んでいた。 嚯楜郚は垞に問題を募集しおいるが、それらの問題は以䞋の 6 皮類に分類される。 Math Greedy Geometry DP Graph Other 幞いにも倚くの問題が集たったので、圌女はたくさんコンテストを開こうず考えた。コンテストは 3 問の問題からなるが、圌女はコンテストがより教育的になるようにず、以䞋の 4 皮類のコンテストを開くこずに決めた。 数孊ゲヌコンテスト: Math の問題ず DP の問題合わせお3問の問題セット アルゎリズムゲヌコンテスト: Greedy の問題ず Graph の問題合わせお3問の問題セット 実装ゲヌコンテスト: Geometry の問題ず Other の問題合わせお3問の問題セット バランスの良いコンテスト: Math たたは DP から 1 問、Greedy たたは Graph から 1 問、Geometry たたは Other から 1 問、合わせお3問の問題セット もちろん、あるコンテストに出題した問題は他のコンテストに出題するこずはできない。圌女の望みは、できるだけ倚くのコンテストを開くこずだ。6 皮類の問題のストック数は分かっおいるが、さお、最倧䜕回コンテストを開けるだろうか。圌女にずっおこれは難しい問題だが、カリスマアルゎリズマヌであるあなたなら解決できるはずだ。 Input 入力は耇数のケヌスからなる。 各ケヌスは以䞋のフォヌマットで䞎えられる。 n Math n Greedy n Geometry n DP n Graph n Other 各入力の倀はそれぞれの皮類の問題のストックの数を衚す。 入力の終わりは 0 0 0 0 0 0 からなる行によっお䞎えられる。 各倀は以䞋の条件を満たす。 n Math + n Greedy + n Geometry + n DP + n Graph + n Other ≀ 100,000,000 たたテストケヌスの数は20,000個を超えない。 Output 開催可胜なコンテストの最倧数を1行に出力せよ。 Sample input 1 1 1 1 1 1 1 1 1 0 0 0 1 0 0 0 1 1 3 0 0 3 0 0 3 1 0 1 3 1 1 2 0 2 0 1 0 0 1 1 0 3 1 0 0 1 1 0 0 0 0 0 0 0 Sample output 2 1 1 2 3 1 1 0 The University of Aizu Programming Contest 2011 Summer 原案: Tomoya Sakai 問題文: Takashi Tayama
39,222
D 玠因数分解の倚様性 (The Diversity of Prime Factorization) Problem Ebi-chan has the FACTORIZATION MACHINE, which can factorize natural numbers M (greater than 1) in O( $\log$ M) time! But unfortunately, the machine could display only digits and whitespaces. In general, we consider the factorization of M as p_1^{e_1} \times p_2^{e_2} \times ... \times p_K^{e_K} where (1) i < j implies p_i < p_j and (2) p_i is prime. Now, she gives M to the machine, and the machine displays according to the following rules in ascending order with respect to i : If e_i = 1 , then displays p_i , otherwise, displays p_i e_i . For example, if she gives either 22 or 2048 , then 2 11 is displayed. If either 24 or 54 , then 2 3 3 . Okay, Ebi-chan has written down the output of the machine, but she notices that she has forgotten to write down the input! Now, your task is to count how many natural numbers result in a noted output. Note that Ebi-chan has mistaken writing and no input could result in the output. The answer could be too large, so, you must output it modulo 10^9+7 (prime number). Input N q_1 q_2 $\cdots$ q_N In the first line, the number of the output of the machine is given. In the second line, the output of the machine is given. Constraints 1 \leq N \leq 10^5 2 \leq q_i \leq 10^6 ( 1 \leq i \leq N ) Output Print the number of the natural numbers that result in the given output of the machine. Sample Input 1 3 2 3 3 Sample Output for Input 1 2 24 = 2^3 \times 3 and 54 = 2 \times 3^3 satisfy the condition. Sample Input 2 3 2 3 4 Sample Output 2 for Input 2 1 Only 162 = 2 \times 3^4 satisfies the condition. Note that 4 is not prime. Sample Input 3 3 3 5 2 Sample Output for Input 3 1 Since 2 < 3 < 5 , only 75 = 3 \times 5^2 satisfies the condition. Sample Input 4 1 4 Sample Output for Input 4 0 Ebi-chan should have written down it more carefully.
39,223
Road Construction The Zuia Kingdom has finally emerged through annexation of $N$ cities, which are identified by index from $1$ to $N$. You are appointed the Minister of Transport of the newly born kingdom to construct the inter-city road network. To simplify the conceptual design planning, you opted to consider each city as a point on the map, so that the $i$-th city can be represented by an coordinate ($x_i, y_i$). The cost of road construction connecting $u$-th and $v$-th cities is equal to the distance $|x_u - x_v|$ or $|y_u - y_v|$, whichever the larger. The notation $|A|$ represents the absolute value of $A$. The object here is to explore the minimum cost required to construct the road network in such a way that people can move between different cities along one or more roads. Make a program to calculate the minimum of total road construction cost from the number of cities and their coordinates. Input The input is given in the following format. $N$ $x_1$ $y_1$ $x_2$ $y_2$ ... $x_N$ $y_N$ The first line provides the number of cities $N$ ($2 \leq N \leq 10^5$). Each of the subsequent $N$ lines provides the coordinate of the $i$-th city $x_i, y_i$ ($0 \leq x_i, y_i \leq 10^9$) as integers. Note that none of these coordinates coincides if: $i \ne j$, then $x_i \ne x_j$ or $y_i \ne y_j$. Output Output the minimum road construction cost. Sample Input 1 3 1 2 3 4 10 1 Sample Output 1 9 The road connecting city 1 and 2 can be constructed at the cost of 2, and that connecting city 2 and 3 at the cost of 7. Therefore, the total cost becomes 9, which is the minimum. Sample Input 2 3 1 2 3 4 3 2 Sample Output 2 4 Sample Input 3 5 7 41 10 0 99 27 71 87 14 25 Sample Output 3 163
39,224
Rabbit Party A rabbit Taro decided to hold a party and invite some friends as guests. He has n rabbit friends, and m pairs of rabbits are also friends with each other. Friendliness of each pair is expressed with a positive integer. If two rabbits are not friends, their friendliness is assumed to be 0. When a rabbit is invited to the party, his satisfaction score is defined as the minimal friendliness with any other guests. The satisfaction of the party itself is defined as the sum of satisfaction score for all the guests. To maximize satisfaction scores for the party, who should Taro invite? Write a program to calculate the maximal possible satisfaction score for the party. Input The first line of the input contains two integers, n and m ( 1 \leq n \leq 100 , 0 \leq m \leq 100 ). The rabbits are numbered from 1 to n . Each of the following m lines has three integers, u , v and f . u and v ( 1 \leq u, v \leq n , u \neq v , 1 \leq f \leq 1,000,000 ) stands for the rabbits' number, and f stands for their friendliness. You may assume that the friendliness of a pair of rabbits will be given at most once. Output Output the maximal possible satisfaction score of the party in a line. Sample Input 1 3 3 1 2 3 2 3 1 3 1 2 Output for the Sample Input 1 6 Sample Input 2 2 1 1 2 5 Output for the Sample Input 2 10 Sample Input 3 1 0 Output for the Sample Input 3 0 Sample Input 4 4 5 1 2 4 1 3 3 2 3 7 2 4 5 3 4 6 Output for the Sample Input 4 16
39,225
Problem B: Potatoes Problem がっちょ君は N 面の畑ず M 個の芋を所有しおいる。各畑にはそれぞれ1から N たでの番号が付けられおいる。がっちょ君は畑に芋を怍え収穫するこずで、芋の数を増やしたいず考えおいる。 がっちょ君は䞀人暮らしであり、1人では K 面たでの畑しか管理するこずができない。たた、各畑の土の状態や面積は様々で、芋の収穫数や怍えるこずのできる芋の数も様々である。芋を畑 i に怍えた堎合、1幎埌には畑 i に怍えた芋1぀に぀き a i 個の芋を収穫するこずができる。ただし、畑 i には最倧でも b i 個の芋しか怍えるこずができない。 がっちょ君が K 面以内の畑に M 個以内の芋を怍えた時、1幎埌に所有するこずができる芋の数の最倧倀を求めおほしい。 Input 入力は以䞋の圢匏で䞎えられる。 N M K a 1 a 2 ... a N b 1 b 2 ... b N 1行目に3個の敎数 N , M , K が空癜区切りで䞎えられる。 2行目に N 個の敎数 a i が空癜区切りで䞎えられる。 3行目に N 個の敎数 b i が空癜区切りで䞎えられる。 Constraints 1 ≀ N ≀ 15 1 ≀ M ≀ 10 4 1 ≀ K ≀ min( N ,3) 1 ≀ a i ≀ 10 3 1 ≀ b i ≀ 10 4 Output 芋の数の最倧倀を1行で出力せよ。 Sample Input 1 5 100 3 2 3 4 5 6 50 40 20 10 5 Sample Output 1 280 畑1に40個、畑2に40個、畑3に20個怍えるこずで収穫埌に280個の芋を所有するこずができる。 Sample Input 2 5 100 3 2 3 4 5 100 50 40 20 10 1 Sample Output 2 339 畑2に40個、畑3に20個、畑5に1個怍えるこずで300個の芋を収穫するこずができ、怍えなかった芋ず合わせお答えは339になる。
39,226
Problem I: Strange Currency System The currency system in the Kingdom of Yoax-Musty is strange and fairly inefficient. Like other countries, the kingdom has its own currencty unit denoted by K $ (kingdom dollar). However, the Ministry of Finance issues bills for every value between 1 K $ and (2 31 - 1) K $ worth. On the other hand, this system often enables people to make many different values just with a small number of bills. For example, if you have four bills of 1 K $ , 2 K $ , 4 K $ , and 8 K $ worth respectively, you can make any values from 1 K #36; to 15 K $ . In this problem, you are requested to write a program that finds the minimum value that cannot be made with a given set (multiset in a mathematical sense) of bills. For the case with the four bills (1 K $ , 2 K $ , 4 K $ , and 8 K $ ), since you can make any values up to 15 K $ , your program should report 16 K $ . Input The input consists of two lines. The first line contains an integer N (1 ≀ N ≀ 10000), the number of bills. The second line contains N integers, each of which represents the value of a bill in K $ . There may be multiple bills of the same value. Output Print the minimum value unable to be made on a line. The value should be given in K $ and without any currency sign or name. Sample Input and Output Input #1 4 1 2 4 8 Output #1 16 Input #2 5 1 1 3 11 2 Output #2 8
39,227
Pipeline Plans There are twelve types of tiles in Fig. 1. You were asked to fill a table with R × C cells with these tiles. R is the number of rows and C is the number of columns. How many arrangements in the table meet the following constraints? Each cell has one tile. the center of the upper left cell (1,1) and the center of the lower right cell ( C , R ) are connected by some roads. Fig. 1: the types of tiles Input The first line contains two integers R and C (2 ≀ R × C ≀ 15) . You can safely assume at least one of R and C is greater than 1. The second line contains twelve integers, t 1 , t 2 , ..., t 12 (0 ≀ t 1 + .... + t 12 ≀ 15) . t i represents the number of the i -th tiles you have. Output Output the number of arrangments in a line. Sample Input 1 3 3 4 2 2 0 0 0 0 0 0 0 0 1 Output for the Sample Input 1 2 Sample Input 2 3 3 0 1 1 0 0 0 0 0 0 0 0 7 Output for the Sample Input 2 66 Sample Input 3 3 3 0 0 0 0 0 0 0 0 0 0 0 10 Output for the Sample Input 3 1 Sample Input 4 2 4 0 0 1 1 1 2 0 1 0 0 1 1 Output for the Sample Input 4 2012 Sample Input 5 5 2 0 1 1 1 0 1 2 1 2 0 0 1 Output for the Sample Input 5 8512
39,228
Problem F: Gather the Maps! はるか昔、八尟氏が残したずされる䌝説の秘宝が八王子のどこかに眠っおいるずいう。 その圚凊を瀺すずされる宝の地図は、いく぀かの断片に分割された状態で、八尟氏の n 人の子孫達によっお受け継がれおいる。 今、八尟氏の子孫達は協力しおその秘宝を手に入れようずしおいた。 ずころが、秘宝の圚凊を指し瀺す宝の地図の䞀郚分だけでは秘宝を芋぀けるこずができない。 そこで、八尟氏の子孫達は党員で集たっお地図を 1 ヶ所に集めようずした。 ずころが、いざ実行に移そうずしおもなかなか予定が合わずに集たるこずができない。 しかしこの秘宝に関する情報は、䞀族においお秘密裏に䌝えられおきた貎重な情報である。 挏掩の危険性を考慮するず、公共の通信手段を甚いお地図をやりずりするこずなど問題倖である。 そこで、子孫同士が盎接䌚っお地図を手枡すずいうこずを繰り返すこずで、ある 1 人の子孫のずころに地図を集めるこずにした。 なお、1 人が 1 日に䌚える人数に制限はないが、互いにスケゞュヌルが空いおいるこずが必芁である。 あなたの仕事は、それぞれの子孫に察するスケゞュヌルの空いおいる日のリストから、地図を集めるには最䜎で䜕日必芁かを求めるプログラムを曞くこずである。 ちなみに、八尟氏䞀族の結束は非垞に固い。 最終的に地図党䜓を手にした子孫が、他の子孫を裏切っお秘宝を持ち逃げすれば、䞀族から制裁を受けるこずになる。その制裁はきわめお恐ろしいものであるため、実際にその子孫が秘宝を持ち逃げするこずは事実䞊䞍可胜である。 Input 入力は耇数のデヌタセットからなる。 それぞれのデヌタセットは耇数の行からなる。 その最初の行には、地図の断片を持った者の人数を衚す敎数 n (1 < n <= 50) が蚘述されおいる。 続く n 行には、それぞれの子孫のスケゞュヌルが曞かれおいる。 i 行目は i 人目の子孫のスケゞュヌルが衚しおおり、いく぀かの敎数が 1 文字のスペヌスを区切りずしお曞かれおいる。 最初の敎数 f i (0 <= f i <= 30) は、その子孫のスケゞュヌルが空いおいる日の日数を衚す敎数である。 続く f i 個の敎数は、スケゞュヌルが空いおいる日付を衚す。 これらの日付は互いに異なり、党お 1 以䞊 30 以䞋である。 入力の最埌に 0 のみを含んだ 1 行がある。 Output 各デヌタセットに察しお、1 ぀の敎数を 1 行に出力せよ。 もし、30 日以内に地図を集めるこずができる堎合は、地図を集めるのに最䜎限必芁ずなる日数を、集めるこずができない堎合は -1 を出力せよ。 远蚘 : 䞊蚘の「地図を集めるのに最䜎限必芁ずなる日数」は 1 日を起点ずしお最も早く党おの地図が集たる日付を意味する Sample Input 4 1 1 2 2 3 2 1 2 3 3 4 5 0 Output for the Sample Input 3
39,229
Cake Party I’m planning to have a party on my birthday. Many of my friends will come to the party. Some of them will come with one or more pieces of cakes, but it is not certain if the number of the cakes is a multiple of the number of people coming. I wish to enjoy the cakes equally among the partiers. So, I decided to apply the following rules. First, all the party attendants are given the same number of cakes. If some remainder occurs, a piece goes on a priority basis to the party host (that’s me!). How many pieces of cake can I enjoy? Given the number of my friends and cake information, make a program to calculate how many pieces of cake I can enjoy. Note that I am not counted in the number of my friends. Input The input is given in the following format. $N$ $C$ $p_1$ $p_2$ ... $p_C$ The first line provides the number of my friends $N$ ($1 \leq N \leq 100$) and the number of those among them who brought one or more pieces of cake with them $C$ ($1 \leq C \leq N$). The second line provides an array of integers $p_i$ ($1 \leq p_i \leq100$), each of which shows the number of cakes of the $i$-th friend of mine who was willing to come up with one or more pieces of cake. Output Output the number of cakes I can enjoy. Sample Input 1 5 4 5 5 6 5 Sample Output 1 4 Sample Input 2 7 5 8 8 8 8 8 Sample Output 2 5 Sample Input 3 100 3 3 3 3 Sample Output 3 1
39,230
Problem F Secrets in Shadows Long long ago, there were several identical columns (or cylinders) built vertically in a big open space near Yokohama (Fig. F-1). In the daytime, the shadows of the columns were moving on the ground as the sun moves in the sky. Each column was very tall so that its shadow was very long. The top view of the shadows is shown in Fig. F-2. The directions of the sun that minimizes and maximizes the widths of the shadows of the columns were said to give the important keys to the secrets of ancient treasures. Fig. F-1: Columns (or cylinders) Gig. F-2: Top view of the columns (Fig. F-1) and their shadows The width of the shadow of each column is the same as the diameter of the base disk. But the width of the whole shadow (the union of the shadows of all the columns) alters according to the direction of the sun since the shadows of some columns may overlap those of other columns. Fig. F-3 shows the direction of the sun that minimizes the width of the whole shadow for the arrangement of columns in Fig. F-2. Fig. F-3: The direction of the sun for the minimal width of the whole shadow Fig. F-4 shows the direction of the sun that maximizes the width of the whole shadow. When the whole shadow is separated into several parts (two parts in this case), the width of the whole shadow is defined as the sum of the widths of the parts. Fig. F-4: The direction of the sun for the maximal width of the whole shadow A direction of the sun is specified by an angle Ξ defined in Fig. F-5. For example, the east is indicated by Ξ = 0, the south by Ξ = π /2, and the west by Ξ = π . You may assume that the sun rises in the east ( Ξ = 0) and sets in the west ( Ξ = π ). Your job is to write a program that, given an arrangement of columns, computes two directions Ξ min and Ξ max of the sun that give the minimal width and the maximal width of the whole shadow, respectively. The position of the center of the base disk of each column is specified by its ( x , y ) coordinates. The x -axis and y -axis are parallel to the line between the east and the west and that between the north and the south, respectively. Their positive directions indicate the east and the north, respectively. You can assume that the big open space is a plane surface. Fig. F-5: The definition of the angle of the direction of the sun There may be more than one Ξ min or Ξ max for some arrangements in general, but here, you may assume that we only consider the arrangements that have unique Ξ min and Ξ max in the range 0 ≀ Ξ min < π , 0 ≀ Ξ max < π . Input The input consists of multiple datasets, followed by the last line containing a single zero. Each dataset is formatted as follows. n x 1 y 1 x 2 y 2 ... x n y n n is the number of the columns in the big open space. It is a positive integer no more than 100. x k and y k are the values of x -coordinate and y -coordinate of the center of the base disk of the k -th column ( k =1, ..., n ). They are positive integers no more than 30. They are separated by a space. Note that the radius of the base disk of each column is one unit (the diameter is two units). You may assume that some columns may touch each other but no columns overlap others. For example, a dataset 3 1 1 3 1 4 3 corresponds to the arrangement of three columns depicted in Fig. F-6. Two of them touch each other. Fig. F-6: An arrangement of three columns Output For each dataset in the input, two lines should be output as specified below. The output lines should not contain extra characters such as spaces. In the first line, the angle Ξ min , the direction of the sun giving the minimal width, should be printed. In the second line, the other angle Ξ max , the direction of the sun giving the maximal width, should be printed. Each angle should be contained in the interval between 0 and π (abbreviated to [0, π ]) and should not have an error greater than ε =0.0000000001 (=10 -10 ). When the correct angle Ξ is in [0, ε ], approximate values in [0, Ξ + ε ] or in [ π + Ξ - ε , π ] are accepted. When the correct angle Ξ is in [ π - ε , π ], approximate values in [0, Ξ + ε - π ] or in [ Ξ - ε , π ] are accepted. You may output any number of digits after the decimal point, provided that the above accuracy condition is satisfied. Sample Input 3 1 1 3 1 4 3 4 1 1 2 3 3 8 1 9 8 1 1 3 1 6 1 1 3 5 3 1 7 3 5 5 5 8 20 7 1 27 30 14 9 6 17 13 4 2 17 7 8 9 0 Output for the Sample Input 2.553590050042226 0.982793723247329 1.570796326794896 2.819842099193151 1.325817663668032 2.094395102393196 2.777613697080149 0.588002603547568
39,231
Score : 500 points Problem Statement There are N boxes arranged in a circle. The i -th box contains A_i stones. Determine whether it is possible to remove all the stones from the boxes by repeatedly performing the following operation: Select one box. Let the box be the i -th box. Then, for each j from 1 through N , remove exactly j stones from the (i+j) -th box. Here, the (N+k) -th box is identified with the k -th box. Note that the operation cannot be performed if there is a box that does not contain enough number of stones to be removed. Constraints 1 ≩ N ≩ 10^5 1 ≩ A_i ≩ 10^9 Input The input is given from Standard Input in the following format: N A_1 A_2 
 A_N Output If it is possible to remove all the stones from the boxes, print YES . Otherwise, print NO . Sample Input 1 5 4 5 1 2 3 Sample Output 1 YES All the stones can be removed in one operation by selecting the second box. Sample Input 2 5 6 9 12 10 8 Sample Output 2 YES Sample Input 3 4 1 2 3 1 Sample Output 3 NO
39,232
Donut Decoration Donut maker's morning is early. Mr. D, who is also called Mr. Donuts, is an awesome donut maker. Also today, he goes to his kitchen and prepares to make donuts before sunrise. In a twinkling, Mr. D finishes frying $N$ donuts with a practiced hand. But these donuts as they are must not be displayed in a showcase. Filling cream, dipping in chocolate, topping somehow cute, colorful things, etc., several decoration tasks are needed. There are $K$ tasks numbered 1 through $K$, and each of them must be done exactly once in the order $1, 2, ..., K$ to finish the donuts as items on sale. Instantly, Mr. D arranges the $N$ donuts in a row. He seems to intend to accomplish each decoration tasks sequentially at once. However, what in the world is he doing? Mr. D, who stayed up late at yesterday night, decorates only a part of the donuts in a consecutive interval for each task. It's clearly a mistake! Not only that, he does some tasks zero or several times, and the order of tasks is also disordered. The donuts which are not decorated by correct process cannot be provided as items on sale, so he should trash them. Fortunately, there are data recording a sequence of tasks he did. The data contain the following information: for each task, the consecutive interval $[l, r]$ of the decorated donuts and the ID $x$ of the task. Please write a program enumerating the number of the donuts which can be displayed in a showcase as items on sale for given recorded data. Input The input consists of a single test case. The test case is formatted as follows. $N$ $K$ $T$ $l_1$ $r_1$ $x_1$ ... $l_T$ $r_T$ $x_T$ The first line contains two integers $N$ and $K$, where $N$ ($1 \leq N \leq 200,000$) is the number of the donuts fried by Mr. D, and $K$ ($1 \leq K \leq 200,000$) is the number of decoration tasks should be applied to the donuts. The second line contains a single integer $T$ ($1 \leq T \leq 200,000$), which means the number of information about tasks Mr. D did. Each of next $T$ lines contains three integers $l_i$, $r_i$, and $x_i$ representing the $i$-th task Mr. D did: the $i$-th task was applied to the interval $[l_i, r_i]$ ($1 \leq l_i \leq r_i \leq N$) of the donuts inclusive, and has ID $x_i$ ($1 \leq x_i \leq K$). Output Output the number of the donuts that can be provided as items on sale. Sample Input 1 3 2 3 1 2 1 2 3 2 3 3 1 Output for the Sample Input 1 1 Sample Input 2 5 3 6 2 3 1 1 3 2 4 5 1 2 4 3 3 5 2 5 5 3 Output for the Sample Input 2 2 Sample Input 3 10 1 2 2 9 1 5 7 1 Output for the Sample Input 3 5
39,233
Score: 400 points Problem Statement Amidakuji is a traditional method of lottery in Japan. To make an amidakuji, we first draw W parallel vertical lines, and then draw horizontal lines that connect them. The length of each vertical line is H+1 [cm], and the endpoints of the horizontal lines must be at 1, 2, 3, ..., or H [cm] from the top of a vertical line. A valid amidakuji is an amidakuji that satisfies the following conditions: No two horizontal lines share an endpoint. The two endpoints of each horizontal lines must be at the same height. A horizontal line must connect adjacent vertical lines. Find the number of the valid amidakuji that satisfy the following condition, modulo 1\ 000\ 000\ 007 : if we trace the path from the top of the leftmost vertical line to the bottom, always following horizontal lines when we encounter them, we reach the bottom of the K -th vertical line from the left. For example, in the following amidakuji, we will reach the bottom of the fourth vertical line from the left. Constraints H is an integer between 1 and 100 (inclusive). W is an integer between 1 and 8 (inclusive). K is an integer between 1 and W (inclusive). Input Input is given from Standard Input in the following format: H W K Output Print the number of the amidakuji that satisfy the condition, modulo 1\ 000\ 000\ 007 . Sample Input 1 1 3 2 Sample Output 1 1 Only the following one amidakuji satisfies the condition: Sample Input 2 1 3 1 Sample Output 2 2 Only the following two amidakuji satisfy the condition: Sample Input 3 2 3 3 Sample Output 3 1 Only the following one amidakuji satisfies the condition: Sample Input 4 2 3 1 Sample Output 4 5 Only the following five amidakuji satisfy the condition: Sample Input 5 7 1 1 Sample Output 5 1 As there is only one vertical line, we cannot draw any horizontal lines. Thus, there is only one amidakuji that satisfies the condition: the amidakuji with no horizontal lines. Sample Input 6 15 8 5 Sample Output 6 437760187 Be sure to print the answer modulo 1\ 000\ 000\ 007 .
39,234
Score : 400 points Problem Statement Given is a string S . Each character in S is either a digit ( 0 , ..., 9 ) or ? . Among the integers obtained by replacing each occurrence of ? with a digit, how many have a remainder of 5 when divided by 13 ? An integer may begin with 0 . Since the answer can be enormous, print the count modulo 10^9+7 . Constraints S is a string consisting of digits ( 0 , ..., 9 ) and ? . 1 \leq |S| \leq 10^5 Input Input is given from Standard Input in the following format: S Output Print the number of integers satisfying the condition, modulo 10^9+7 . Sample Input 1 ??2??5 Sample Output 1 768 For example, 482305, 002865, and 972665 satisfy the condition. Sample Input 2 ?44 Sample Output 2 1 Only 044 satisfies the condition. Sample Input 3 7?4 Sample Output 3 0 We may not be able to produce an integer satisfying the condition. Sample Input 4 ?6?42???8??2??06243????9??3???7258??5??7???????774????4?1??17???9?5?70???76??? Sample Output 4 153716888
39,236
Score : 200 points Problem Statement We will call a string that can be obtained by concatenating two equal strings an even string. For example, xyzxyz and aaaaaa are even, while ababab and xyzxy are not. You are given an even string S consisting of lowercase English letters. Find the length of the longest even string that can be obtained by deleting one or more characters from the end of S . It is guaranteed that such a non-empty string exists for a given input. Constraints 2 \leq |S| \leq 200 S is an even string consisting of lowercase English letters. There exists a non-empty even string that can be obtained by deleting one or more characters from the end of S . Input Input is given from Standard Input in the following format: S Output Print the length of the longest even string that can be obtained. Sample Input 1 abaababaab Sample Output 1 6 abaababaab itself is even, but we need to delete at least one character. abaababaa is not even. abaababa is not even. abaabab is not even. abaaba is even. Thus, we should print its length, 6 . Sample Input 2 xxxx Sample Output 2 2 xxx is not even. xx is even. Sample Input 3 abcabcabcabc Sample Output 3 6 The longest even string that can be obtained is abcabc , whose length is 6 . Sample Input 4 akasakaakasakasakaakas Sample Output 4 14 The longest even string that can be obtained is akasakaakasaka , whose length is 14 .
39,237
List For a dynamic list $L$ of integers, perform a sequence of the following operations. $L$ has a special element called END at the end of the list and an element of $L$ is indicated by a cursor. insert($x$): Insert $x$ before the element indicated by the cursor. After this operation, the cursor points the inserted element. move($d$): Move the cursor to the end by $d$, if $d$ is positive. Move the cursor to the front by $d$, if $d$ is negative. erase(): Delete the element indicated by the cursor. After this operation, the cursor points the element next to the deleted element. In case there is no such element, the cursor should point END. In the initial state, $L$ is empty and the cursor points END. Input The input is given in the following format. $q$ $query_1$ $query_2$ : $query_q$ Each query $query_i$ is given by 0 $x$ or 1 $d$ or 2 where the first digits 0 , 1 and 2 represent insert, move and erase operations respectively. Output Print all elements of the list in order after performing given operations. Print an element in a line. Constraints $1 \leq q \leq 500,000$ The cursor indicates an element of $L$ or END during the operations Erase operation will not given when the cursor points END $-1,000,000,000 \leq x \leq 1,000,000,000$ Moving distance of the cursor ($\sum{|d|}$) does not exceed 1,000,000 $L$ is not empty after performing all operations Sample Input 1 5 0 1 0 2 0 3 1 1 2 Sample Output 1 3 1
39,238