question_id
stringlengths
6
6
content
stringlengths
1
27.2k
p00444
<H1> おつり </H1> <h2>問題</h2> <p> 太郎君はよくJOI雑貨店で買い物をする. JOI雑貨店には硬貨は500円,100円,50円,10円,5円,1円が十分な数だけあり,いつも最も枚数が少なくなるようなおつりの支払い方をする.太郎君がJOI雑貨店で買い物をしてレジで1000円札を1枚出した時,もらうおつりに含まれる硬貨の枚数を求めるプログラムを作成せよ. </p> <p> 例えば入力例1の場合は下の図に示すように,4を出力しなければならない. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_otsuri"> </center> <br> <h2>入力</h2> <p> 入力は複数のデータセットからなる.各データセットは1行からなり,太郎君が支払う金額(1以上1000未満の整数)が1つだけ書かれている.入力データはゼロ1つの行で終了する. </p> <p> データセットの数は 5 を超えない. </p> <h2>出力</h2> <p> <!--提出する出力ファイルは1行のみである.おつりに含まれる硬貨の枚数を出力せよ.--> データセットごとにおつりに含まれる硬貨の枚数を1行に出力せよ. </p> <h2>入出力例</h2> <h3>入力例</h3> <pre> 380 1 0 </pre> <h3>出力例</h3> <pre> 4 15 </pre> <div class="source"> <p class="source"> 上記問題文と自動審判に使われるデータは、<a href="http://www.ioi-jp.org">情報オリンピック日本委員会</a>が作成し公開している問題文と採点用テストデータです。 </p> </div>
p00151
<H1>Grid</H1> <p> There is a n &times; n grid D where each cell contains either 1 or 0. </p> <p> Your task is to create a program that takes the gird data as input and computes the greatest number of consecutive 1s in either vertical, horizontal, or diagonal direction. </p> <p> For example, the consecutive 1s with greatest number in the figure below is circled by the dashed-line. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_masume"> </center> <p> The size of the grid n is an integer where 2 &le; n &le; 255. </p> <H2>Input</H2> <p> The input is a sequence of datasets. The end of the input is indicated by a line containing one zero. Each dataset is formatted as follows: </p> <pre> n D<sub>11</sub> D<sub>12</sub> ... D<sub>1n</sub> D<sub>21</sub> D<sub>22</sub> ... D<sub>2n</sub> . . D<sub>n1</sub> D<sub>n2</sub> ... D<sub>nn</sub> </pre> <H2>Output</H2> <p> For each dataset, print the greatest number of consecutive 1s. </p> <H2>Sample Input</H2> <pre> 5 00011 00101 01000 10101 00010 8 11000001 10110111 01100111 01111010 11111111 01011010 10100010 10000001 2 01 00 3 000 000 000 0 </pre> <H2>Output for the Sample Input</H2> <pre> 4 8 1 0 </pre>
p00501
<H1>看板 (Signboard) </H1> <br/> <h2> 問題</h2> <p> JOI 君はお店の看板を作ることにした. </p> <p> 文字が等間隔に書かれた古い看板が N 枚ある.JOI 君は古い看板からいくつかの文字を消すことで看板を作る.残った文字列がお店の名前になっていて,しかも残った文字が等間隔に並んでいるようにしたい.看板は 1 枚の古い看板から作らなければならず,古い看板を切ったりつなげたりしてはならない. </p> <p> お店の名前と N 枚の古い看板の情報が与えられた時,JOI 君が作ることができる看板の枚数を求めるプログラムを作成せよ.ただし, 1 枚の古い看板から作ることができる看板が複数考えられる場合も,作ることができる看板は 1 枚であると考える. </p> <h2> 入力</h2> <p> 入力は 2 + N 行からなる. </p> <p> 1 行目には,整数 N <nobr>(1 &leqq; N &leqq; 100)</nobr> が書かれており,古い看板の枚数を表す. </p> <p> 2 行目には,3 文字以上 25 文字以下のアルファベット小文字からなる文字列が書かれており,お店の名前を表す. </p> <p> 続く N 行のうちの i 行目 <nobr>(1 &leqq; i &leqq; N)</nobr> には 1 文字以上 100 文字以下のアルファベット小文字からなる文字列が書かれており,i 枚目の古い看板に書かれている文字列を表す. </p> <h2> 出力</h2> <p> JOI 君が作ることができる看板の枚数を表す整数を 1 行で出力せよ. </p> <h2> 入出力例</h2> <h3>入力例</h3> <pre> 4 bar abracadabra bear bar baraxbara </pre> <h3>出力例</h3> <pre> 3 </pre> <p> お店の名前は bar である. </p> <p> 1 枚目の古い看板には文字列 abracadabra が書かれている. この古い看板から 2 文字目,6 文字目,10 文字目以外を消すことで看板を作ることができる. </p> <p> 2 枚目は,2 文字目を消すと bar という文字列を作ることができるが,これは残った文字が等間隔に並んでいない. </p> <p> 3 枚目は,文字を何も消さなくても看板になっている. </p> <p> 4 枚目の古い看板から看板を作る方法は 2 通りある. 1 つの方法は,1 文字目,2 文字目,3 文字目以外を消すことである. もう 1 つの方法は,6 文字目,7 文字目,8 文字目以外を消すことである. </p> <p> よって,JOI 君は 1 枚目,3 枚目,4 枚目の古い看板から看板を作ることができるので,3 を出力する. </p> <div class="source"> <p class="source"> 問題文と自動審判に使われるデータは、<a href="http://www.ioi-jp.org">情報オリンピック日本委員会</a>が作成し公開している問題文と採点用テストデータです。 </p> </div>
p01740
<p> You have a board with height two and width <var>W</var>. The board is divided into 1x1 cells. Each row of the board is numbered 1 and 2 from top to bottom and each column is numbered 1 to <var>W</var> from left to right. We denote a cell at the <var>i</var>-th row in the <var>j</var>-th column by <var>(i, j)</var>. Initially, some checkers are placed on some cells of the board. Here we assume that each checker looks the same. So we do not distinguish each checker. You are allowed to perform the following operations on the board: </p> <ol> <li> Square rotation: Choose a 2x2 square on the board. Then move checkers in the 2x2 square such that the checkers rotate their position in the square by 90 degrees in either clockwise or counterclockwise direction.</p> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_JAGSummerCamp2013Day4_rotation-game-square" width="480" height="360" /> </li> <li>Triangular rotation: Choose a set of cells on the board that form a triangle. Here, we call a set of cells triangle if it is formed by removing exactly one cell from a 2x2 square. In the similar manner with the square rotation, rotate checkers in the chosen triangle in either clockwise or counterclockwise direction.</p> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_JAGSummerCamp2013Day4_rotation-game-triangle" width="480" height="360" /> </li> </ol> <p> The objective in this problem is to transform the arrangement of the checkers into desired arrangement by performing a sequence of rotation operations. The information of the initial arrangement and the target arrangement are given as the input. For the desired arrangement, one of the following is specified for each cell <var>(i, j)</var>: i. cell <var>(i,j)</var> must contain a checker, ii. cell <var>(i,j)</var> must not contain a checker, or iii. there is no constraint for cell <var>(i, j)</var>. Compute the minimum required number of operations to move the checkers so the arrangement satisfies the constraints and output it. </p> <h3>Input</h3> <p>The input is formatted as follows. </p><pre> <var>W</var> <var>s_{11}s_{11}...s_{1W}</var> <var>s_{21}s_{21}...s_{2W}</var> <var>t_{11}t_{11}...t_{1W}</var> <var>t_{21}t_{21}...t_{2W}</var> </pre> <p>The first line of the input contains an integer <var>W</var> (<var>2 \leq W \leq 2,000</var>), the width of the board. The following two lines contain the information of the initial arrangement. If a cell <var>(i, j)</var> contains a checker in the initial arrangement, <var>s_{ij}</var> is <code>o</code>. Otherwise, <var>s_{ij}</var> is <code>.</code>. Then an empty line follows. The following two lines contain the information of the desired arrangement. Similarly, if a cell <var>(i, j)</var> must contain a checker at last, <var>t_{ij}</var> is <code>o</code>. If a cell <var>(i, j)</var> must not contain a checker at last, <var>t_{ij}</var> is <code>.</code>. If there is no condition for a cell <var>(i, j)</var>, <var>t_{ij}</var> is <code>*</code>. You may assume that a solution always exists. </p> <h3>Output</h3> <p>Output the minimum required number of operations in one line. </p> <h3>Sample Input 1</h3> <pre>3 .oo o.. o.o o.. </pre> <h3>Output for the Sample Input 1</h3> <pre>1 </pre> <h3>Sample Input 2</h3> <pre>5 .o.o. o.o.o o.o.o .o.o. </pre> <h3>Output for the Sample Input 2</h3> <pre>3 </pre> <h3>Sample Input 3</h3> <pre>4 o.o. o.o. .*.o .o.* </pre> <h3>Output for the Sample Input 3</h3> <pre>4 </pre> <h3>Sample Input 4</h3> <pre>20 oooo.....ooo......oo .o..o....o..oooo...o ...o*.o.oo..*o*.*o.. .*.o.o.*o*o*..*.o... </pre> <h3>Output for the Sample Input 4</h3> <pre>25 </pre> <h3>Sample Input 5</h3> <pre>30 ...o...oo.o..o...o.o........o. .o.oo..o.oo...o.....o........o **o.*....*...*.**...**....o... **..o...*...**..o..o.*........ </pre> <h3>Output for the Sample Input 5</h3> <pre>32 </pre>
p00852
<H1><font color="#000">Problem H:</font> The Best Name for Your Baby</H1> <p> In the year 29XX, the government of a small country somewhere on the earth introduced a law restricting first names of the people only to traditional names in their culture, in order to preserve their cultural uniqueness. The linguists of the country specifies a set of rules once every year, and only names conforming to the rules are allowed in that year. In addition, the law also requires each person to use a name of a specific length calculated from one's birth date because otherwise too many people would use the same very popular names. Since the legislation of that law, the common task of the parents of new babies is to find the name that comes first in the alphabetical order among the legitimate names of the given length because names earlier in the alphabetical order have various benefits in their culture. </p> <p> Legitimate names are the strings consisting of only lowercase letters that can be obtained by repeatedly applying the rule set to the initial string "S", a string consisting only of a single uppercase S. </p> <p> Applying the rule set to a string is to choose one of the rules and apply it to the string. Each of the rules has the form <i>A</i> -&gt; <i>&alpha;</i>, where <i>A</i> is an uppercase letter and <i>&alpha;</i> is a string of lowercase and/or uppercase letters. Applying such a rule to a string is to replace an occurrence of the letter <i>A</i> in the string to the string <i>&alpha;</i>. That is, when the string has the form "<i>&beta;A&gamma;</i>", where <i>&beta;</i> and <i>&gamma;</i> are arbitrary (possibly empty) strings of letters, applying the rule rewrites it into the string "<i>&beta;&alpha;&gamma;</i>". If there are two or more occurrences of A in the original string, an arbitrary one of them can be chosen for the replacement. </p> <p> Below is an example set of rules. </p> <pre> S -&gt; aAB (1) A -&gt; (2) A -&gt; Aa (3) B -&gt; AbbA (4) </pre> <p> Applying the rule (1) to "S", "aAB" is obtained. Applying (2) to it results in "aB", as A is replaced by an empty string. Then, the rule (4) can be used to make it "aAbbA". Applying (3) to the first occurrence of A makes it "aAabbA". Applying the rule (2) to the A at the end results in "aAabb". Finally, applying the rule (2) again to the remaining A results in "aabb". As no uppercase letter remains in this string, "aabb" is a legitimate name. </p> <p> We denote such a rewriting process as follows. </p> <pre> (1) (2) (4) (3) (2) (2) S --&gt; aAB --&gt; aB --&gt; aAbbA --&gt; aAabbA --&gt; aAabb --&gt; aabb </pre> <p> Linguists of the country may sometimes define a ridiculous rule set such as follows. </p> <pre> S -&gt; sA (1) A -&gt; aS (2) B -&gt; b (3) </pre> <p> The only possible rewriting sequence with this rule set is: </p> <pre> (1) (2) (1) (2) S --&gt; sA --&gt; saS --&gt; sasA --&gt; ... </pre> <p> which will never terminate. No legitimate names exist in this case. Also, the rule (3) can never be used, as its left hand side, B, does not appear anywhere else. </p> <p> It may happen that no rules are supplied for some uppercase letters appearing in the rewriting steps. In its extreme case, even S might have no rules for it in the set, in which case there are no legitimate names, of course. Poor nameless babies, sigh! </p> <p> Now your job is to write a program that finds the name earliest in the alphabetical order among the legitimate names of the given length conforming to the given set of rules. </p> <H2>Input</H2> <p> The input is a sequence of datasets, followed by a line containing two zeros separated by a space representing the end of the input. Each dataset starts with a line including two integers <i>n</i> and <i>l</i> separated by a space, where <i>n</i> (1 &le; <i>n</i> &le; 50) is the number of rules and <i>l</i> (0 &le; <i>l</i> &le; 20) is the required length of the name. After that line, <i>n</i> lines each representing a rule follow. Each of these lines starts with one of uppercase letters, A to Z, followed by the character "=" (instead of "-&gt;") and then followed by the right hand side of the rule which is a string of letters A to Z and a to z. The length of the string does not exceed 10 and may be zero. There appears no space in the lines representing the rules. </p> <H2>Output</H2> <p> The output consists of the lines showing the answer to each dataset in the same order as the input. Each line is a string of lowercase letters, a to z, which is the first legitimate name conforming to the rules and the length given in the corresponding input dataset. When the given set of rules has no conforming string of the given length, the corresponding line in the output should show a single hyphen, "-". No other characters should be included in the output. </p> <H2>Sample Input</H2> <pre> 4 3 A=a A= S=ASb S=Ab 2 5 S=aSb S= 1 5 S=S 1 0 S=S 1 0 A= 2 0 A= S=AA 4 5 A=aB A=b B=SA S=A 4 20 S=AAAAAAAAAA A=aA A=bA A= 0 0 </pre> <H2>Output for the Sample Input</H2> <pre> abb - - - - aabbb aaaaaaaaaaaaaaaaaaaa </pre>
p03287
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> boxes arranged in a row from left to right. The <var>i</var>-th box from the left contains <var>A_i</var> candies.</p> <p>You will take out the candies from some consecutive boxes and distribute them evenly to <var>M</var> children.</p> <p>Such being the case, find the number of the pairs <var>(l, r)</var> that satisfy the following:</p> <ul> <li><var>l</var> and <var>r</var> are both integers and satisfy <var>1 \leq l \leq r \leq N</var>.</li> <li><var>A_l + A_{l+1} + ... + A_r</var> is a multiple of <var>M</var>.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All values in input are integers.</li> <li><var>1 \leq N \leq 10^5</var></li> <li><var>2 \leq M \leq 10^9</var></li> <li><var>1 \leq A_i \leq 10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the pairs <var>(l, r)</var> that satisfy the conditions.</p> <p>Note that the number may not fit into a <var>32</var>-bit integer type.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 4 1 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>The sum <var>A_l + A_{l+1} + ... + A_r</var> for each pair <var>(l, r)</var> is as follows:</p> <ul> <li>Sum for <var>(1, 1)</var>: <var>4</var></li> <li>Sum for <var>(1, 2)</var>: <var>5</var></li> <li>Sum for <var>(1, 3)</var>: <var>10</var></li> <li>Sum for <var>(2, 2)</var>: <var>1</var></li> <li>Sum for <var>(2, 3)</var>: <var>6</var></li> <li>Sum for <var>(3, 3)</var>: <var>5</var></li> </ul> <p>Among these, three are multiples of <var>2</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>13 17 29 7 5 7 9 51 7 13 8 55 42 9 81 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>10 400000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>25 </pre></section> </div> </span>
p01310
<h1><font color="#000">Problem H:</font> あみだくじ</h1> <p> なつめは大のねこ好きである。なつめは今日も日課としている野良ねこのブラッシングをするために,いつも野良ねこが集まってくる校庭の一角に向かうことにした。 </p> <p> そのスポットに今日は <var>n</var> 匹のねこが集まってきた。なつめは全員をブラッシングしてやりたかったのだが,ちょうどそこに来る直前に突然用事が出来てしまい,時間的に一匹しかブラッシングしてやれないことになってしまった。ブラッシングはどのねこも楽しみにしているので,一匹だけやると他のねこが嫉妬してしまう。そこでねこたちを納得させるために,どのねこをブラッシングするかをあみだくじで決めることにした。 </p> <p> あみだくじは <var>n</var> 本の縦線とあらかじめ引いた <var>m</var> 本の横線からなる。横線はとなりあう2本の縦線をそれらと垂直になるように結ばなければならない。また,横線同士が端点を共有してはいけない。ある1本の縦線の下端には当たりのマークがつけてある。これらの条件を満たすあみだくじの例を図に示す(これらはサンプル入力の例と対応している)。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_amida" alt="サンプル入力の例に挙げられたあみだくじの図" ><br> 図: 問題の条件をみたすあみだくじの例 </center> <p> ねこたちはそれぞれ縦線の上端からどれか1つを選ぶ。そしてあみだくじを辿った結果,当たりをひいたねこが,今日のブラッシングをしてもらう権利を手に入れることができる,というルールである。なつめは早速あみだくじを作成した。縦線・横線・当たりを書き込み,横線が見えないように隠した。 </p> <p> いよいよねこたちに選んでもらおうという時になって,なつめは <var>n</var> 匹のねこの中に,普段は顔を見せない野良ねこのアクタガワがいることに気がついた。アクタガワはもう一ヶ月ほどブラッシングをしておらず,毛がごわごわしている。 </p> <p> なつめは他のねこたちには申し訳ないと思いながらも,ズルをしてアクタガワをブラッシングするように仕組むことにした。まず,ねこたちにそれぞれ普通通り縦線を選んでもらう。全員が選び終わったらなつめはこっそりといくつかの横線を追加し,アクタガワが当たるようにあみだくじを変更するのだ。 </p> <p> しかし,アクタガワが選んだ縦線によってはアクタガワを当たりにするために新たに引かなければならない横線が多すぎて,細工をしている間にねこたちに計画がバレてしまうかもしれない。そこでなつめはまずそれぞれの縦線について,アクタガワがそれを選んだ場合に何本の横線を引けば当たりにすることができるかを求めることにした。 </p> <p> なお,新たに引く横線も,となりあう2本の縦線をそれらと垂直に結ばなければならず,横線同士が端点を共有しないように引かなければならない。 </p> <h2>Input</h2> <p> 入力データは以下の形式で与えられる。 </p> <p> <var>n</var> <var>m</var> <var>k</var><br> <var>h</var><sub>1</sub> <var>x</var><sub>1</sub><br> <var>h</var><sub>2</sub> <var>x</var><sub>2</sub><br> ...<br> <var>h</var><sub><var>m</var></sub> <var>x</var><sub><var>m</var></sub> </p> <p> 入力の一行目には縦線の本数 <var>n</var> (1 &le; <var>n</var> &le; 100000),すでに引かれている横線の本数 <var>m</var> (0 &le; <var>m</var> &le; 100000),当たりの縦線の番号 <var>k</var> (1 &le; <var>k</var> &le; <var>n</var>) が与えられる。ここで,縦線の番号は,左から順に,1, 2, ..., <var>n</var> とする。 </p> <p> 続く <var>m</var> 行には横線の情報が与えられる。各行は一つの横線の情報を表す二つの整数 <var>h</var> (1 &le; <var>h</var> &le; 1000000) 及び <var>x</var> (1 &le; <var>x</var> &le; <var>n</var> - 1) が与えられる。<var>h</var> は縦線の下端からの距離,<var>x</var> は横線によって結ばれる縦線のうち,左側の縦線の番号である。つまり,この横線は <var>x</var> 番目と <var>x</var>+1 番目の縦線を結ぶことになる。 </p> <p> なお、入力では横線は整数高さの位置にしか存在しないが、新たに追加する横棒の位置は必ずしも整数高さでなくてもよい。 </p> <h2>Output</h2> <p> <var>n</var> 行の文字列を出力せよ。 <var>i</var> (1 &le; <var>i</var> &le; <var>n</var>) 行目には,アクタガワが <var>i</var> 番目の縦線を選んだときに,アクタガワを当たりにするために追加する必要がある横線の最小本数を出力せよ。 </p> <h2>Notes on Submission</h2> <p> 上記形式で複数のデータセットが与えられます。入力データの 1 行目にデータセットの数が与えられます。各データセットに対する出力を上記形式で順番に出力するプログラムを作成して下さい。 </p> <h2>Sample Input</h2> <pre> 4 3 2 1 20 1 10 2 5 7 3 70 1 60 4 50 2 40 4 30 1 20 3 10 2 1 0 1 4 2 1 10 1 10 3 </pre> <h2>Output for the Sample Input</h2> <pre> 1 0 1 1 0 1 1 2 0 1 0 1 2 </pre>
p03403
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> sightseeing spots on the <var>x</var>-axis, numbered <var>1, 2, ..., N</var>. Spot <var>i</var> is at the point with coordinate <var>A_i</var>. It costs <var>|a - b|</var> yen (the currency of Japan) to travel from a point with coordinate <var>a</var> to another point with coordinate <var>b</var> along the axis.</p> <p>You planned a trip along the axis. In this plan, you first depart from the point with coordinate <var>0</var>, then visit the <var>N</var> spots in the order they are numbered, and finally return to the point with coordinate <var>0</var>.</p> <p>However, something came up just before the trip, and you no longer have enough time to visit all the <var>N</var> spots, so you decided to choose some <var>i</var> and cancel the visit to Spot <var>i</var>. You will visit the remaining spots as planned in the order they are numbered. You will also depart from and return to the point with coordinate <var>0</var> at the beginning and the end, as planned.</p> <p>For each <var>i = 1, 2, ..., N</var>, find the total cost of travel during the trip when the visit to Spot <var>i</var> is canceled.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 10^5</var></li> <li><var>-5000 \leq A_i \leq 5000</var> (<var>1 \leq i \leq N</var>)</li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>N</var> lines. In the <var>i</var>-th line, print the total cost of travel during the trip when the visit to Spot <var>i</var> is canceled.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 5 -1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>12 8 10 </pre> <p>Spot <var>1</var>, <var>2</var> and <var>3</var> are at the points with coordinates <var>3</var>, <var>5</var> and <var>-1</var>, respectively. For each <var>i</var>, the course of the trip and the total cost of travel when the visit to Spot <var>i</var> is canceled, are as follows:</p> <ul> <li>For <var>i = 1</var>, the course of the trip is <var>0 \rightarrow 5 \rightarrow -1 \rightarrow 0</var> and the total cost of travel is <var>5 + 6 + 1 = 12</var> yen.</li> <li>For <var>i = 2</var>, the course of the trip is <var>0 \rightarrow 3 \rightarrow -1 \rightarrow 0</var> and the total cost of travel is <var>3 + 4 + 1 = 8</var> yen.</li> <li>For <var>i = 3</var>, the course of the trip is <var>0 \rightarrow 3 \rightarrow 5 \rightarrow 0</var> and the total cost of travel is <var>3 + 2 + 5 = 10</var> yen.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 1 1 1 2 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>4 4 4 2 4 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 -679 -2409 -3258 3095 -3291 -4462 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>21630 21630 19932 8924 21630 19288 </pre></section> </div> </span>
p01594
<h2 align="center">Problem D: Exportation in Space</h2> <p>In an era of space travelling, Mr. Jonathan A. Goldmine exports a special material named "Interstellar Condensed Powered Copper". A piece of it consists of several spheres floating in the air. There is strange force affecting between the spheres so that their relative positions of them are not changed. If you move one of them, all the others follow it.</p> <div align="center"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_exportationInSpace"><br> Example of ICPC (left: bare, right: shielded)</div> <p>To transport ICPCs between planets, it needs to be shielded from cosmic rays. It will be broken when they hit by strong cosmic rays. Mr. Goldmine needs to wrap up each ICPC pieces with anti-cosmic-ray shield, a paper-like material which cuts all the cosmic rays. To save cost, he wants to know the minimum area of the shield he needs to wrap an ICPC piece up.</p> <p>Note that he can't put the shield between the spheres of an ICPC because it affects the force between the spheres. So that the only way to shield it is to wrap the whole ICPC piece by shield.</p> <p>Mr. Goldmine asked you, an experienced programmer, to calculate the minimum area of shield for each material.</p> <p>You can assume that each sphere in an ICPC piece is a point, and you can ignore the thickness of the shield.</p> <h2>Input</h2> <p>Each file consists of one test case, which has the information of one ICPC piece. A test case starts with a line with an integer <var>N</var> (4 <= <var>N</var> <= 50), denoting the number of spheres in the piece of the test case. Then <var>N</var> lines follow. The <var>i</var>-th line of them describes the position of the <var>i</var>-th sphere. It has 3 integers <var>x<sub>i</sub></var>, <var>y<sub>i</sub></var> and <var>z<sub>i</sub></var> (0 <= <var>x</var>, <var>y</var>, <var>z</var> <= 100), separated by a space. They denote the <var>x</var>, <var>y</var> and <var>z</var> coordinates of the <var>i</var>-th sphere, respectively.</p> <p>It is guaranteed that there are no data all of whose spheres are on the same plane, and that no two spheres in a data are on the same coordinate.</p> <h2>Output</h2> <!-- <p>For each input, print the area of the shield Mr. Goldmine needs. The output value should be printed with three digits after the decimal point, and should not contain an error greater than 0.001.</p> --> <p>For each input, print the area of the shield Mr. Goldmine needs. The output value should be in a decimal fraction and should not contain an error greater than 0.001.</p> <h2>Sample Input 1</h2> <pre> 4 0 0 0 0 0 1 0 1 0 1 0 0 </pre> <h2>Output for the Sample Input 1</h2> <pre> 2.366 </pre> <h2>Sample Input 2</h2> <pre> 8 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 </pre> <h2>Output for the Sample Input 2</h2> <pre> 6.000 </pre>
p03053
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a grid of squares with <var>H</var> horizontal rows and <var>W</var> vertical columns, where each square is painted white or black. <var>HW</var> characters from <var>A_{11}</var> to <var>A_{HW}</var> represent the colors of the squares. <var>A_{ij}</var> is <code>#</code> if the square at the <var>i</var>-th row from the top and the <var>j</var>-th column from the left is black, and <var>A_{ij}</var> is <code>.</code> if that square is white.</p> <p>We will repeatedly perform the following operation until all the squares are black:</p> <ul> <li>Every white square that shares a side with a black square, becomes black.</li> </ul> <p>Find the number of operations that will be performed. The initial grid has at least one black square.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq H,W \leq 1000</var></li> <li><var>A_{ij}</var> is <code>#</code> or <code>.</code>.</li> <li>The given grid has at least one black square.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A_{11}</var><var>A_{12}</var><var>...</var><var>A_{1W}</var> <var>:</var> <var>A_{H1}</var><var>A_{H2}</var><var>...</var><var>A_{HW}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of operations that will be performed.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 ... .#. ... </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>After one operation, all but the corners of the grid will be black. After one more operation, all the squares will be black.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 6 ..#..# ...... #..#.. ...... .#.... ....#. </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 </pre></section> </div> </span>
p02212
<span class="lang"> <span class="lang-ja"> <h1>A: チーム分け</h1> <div class="part"> <section> <h3>問題文</h3><p>今年の KUPC はチームで参加できることを聞きつけたあなたは、友達に声をかけてチームで出場することにしました。</p> <p>最終的に、あなたを含めて $4$ 人が集まりました。</p> <p>個人の強さはレーティングで表され、$4$ 人のレーティングはそれぞれ $a$, $b$, $c$, $d$ です。</p> <p>あなたたちは $2$ 人ずつの $2$ チームに分かれることにしました。</p> <p>このとき、チームの強さはそのチームを構成する $2$ 人のレーティングの和で定義されます。 また、チーム間の実力差はそれぞれのチームの強さの差の絶対値で定義されます。</p> <p>あなたはチーム間の実力差ができるだけ小さくなるようにチーム分けを行いたいです。</p> <p>うまくチーム分けを行ったときの、チーム間の実力差の最小値を求めてください。</p> </section> </div> <div class="part"> <section> <h3>制約</h3><ul> <li>$1 \leq a, b, c, d \leq 2799$</li> <li>入力はすべて整数である</li> </ul> </section> </div> <hr /> <div class="io-style"> <div class="part"> <section> <h3>入力</h3><p>入力は以下の形式で標準入力から与えられる。</p> <pre>$a$ $b$ $c$ $d$ </pre> </section> </div> <div class="part"> <section> <h3>出力</h3><p>チーム間の実力差の最小値を整数 $1$ 行で出力せよ。</p> </section> </div> </div> <hr /> <div class="part"> <section> <h3>入力例 1</h3><pre>2 1 3 4 </pre> </section> </div> <div class="part"> <section> <h3>出力例 1</h3><pre>0 </pre> <p>$1$ 人目と $3$ 人目、$2$ 人目と $4$ 人目でチームを組むと、チームの強さは共に $5$ となり、実力差は $0$ となります。 明らかにこれが最小です。</p> </section> </div> <hr /> <div class="part"> <section> <h3>入力例 2</h3><pre>64 224 239 1024 </pre> </section> </div> <div class="part"> <section> <h3>出力例 2</h3><pre>625 </pre></section> </div> </span> </span>
p00385
<h1>Disc</h1>  <p> A mysterious device was discovered in an ancient ruin. The device consists of a disc with integers inscribed on both sides, a bundle of cards with an integer written on each of them and a pedestal within which the cards are placed. The disc rotates when the bundle of cards is put into the pedestal. Only one side of the disc is exposed to view at any one time. </p> <p> The disc is radially segmented in equal angles into $K$ sections, and an integer, from $1$ to $K$ in increasing order, is inscribed clockwise on each section on the front side. The sections on the back side share the same boundaries with the front side, and a negative counterpart of that in the section on the front side is inscribed, i.e., if $X$ is inscribed in the front side section, $-X$ is inscribed on the counterpart section on the back side. There is a needle on the periphery of the disc, and it always points to the middle point of the arc that belongs to a section. Hereafter, we say "the disc is set to $X$" if the front side section just below the needle contains an integer $X$. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/PCK2018_disk1" width="480"><br> Fig.1 Disc with 5 sections (i.e., $K = 5$): When the disc is set to $1$ on the front side, it is set to $-1$ on the back side. </center> <p> Investigation of the behavior of the device revealed the following: </p> <p> When the bundle of cards is placed in the pedestal, the device sets the disc to 1.<br> Then, the device reads the cards slice by slice from top downward and performs one of the following actions according to the integer $A$ on the card </p> <ul> <li>If $A$ is a positive number, it rotates the disc clockwise by $|A|$ sections.</li> <li>If $A$ is zero, it turns the disc back around the vertical line defined by the needle and the center of the disc.</li> <li>If $A$ is a negative number, it rotates the disc counterclockwise by $|A|$ sections.</li> </ul> <p> It is provided that $|A|$ is the absolute value of $A$. The final state of the device (i.e., to what section the needle is pointing) varies depending on the initial stacking sequence of the cards. To further investigate the behavior of the device, we swapped two arbitrary cards in the stack and placed the bundle in the pedestal to check what number the disc is set to. We performed this procedure over again and again. </p> <p> Given the information on the device and several commands to swap two cards, make a program to determine the number to which the device is set at the completion of all actions. Note that the stacking sequence of the cards continues to change as a new trial is made. </p> <h2>Input</h2> <p> The input is given in the following format. </p> <pre> $K$ $N$ $Q$ $A_1$ $A_2$ ... $A_N$ $L_1$ $R_1$ $L_2$ $R_2$ $...$ $L_Q$ $R_Q$ </pre> <p> The first line provides the number of sections $K$ ($2 \leq K \leq 10^9$), the slices of cards $N$ ($2 \leq N \leq 100,000$), and the number of commands $Q$ ($1 \leq Q \leq 100,000$). The second line provides an array of $N$ integers inscribed on the sections of the disc. $A_i$ ($-10^9 \leq A_i \leq 10^9$) represents the number written on the $i$-th card from the top. Each of the subsequent $Q$ lines defines the $i$-th command $L_i,R_i$ ($1 \leq L_ i < R_i \leq N$) indicating a swapping of $L_i$-th and $R_i$-th cards from the top followed by placing the card bundle in the device. </p> <h2>Output</h2> <p> For each command, output a line containing the number to which the device is set at the completion of all actions.  </p> <h2>Sample Input 1</h2> <pre> 5 3 1 1 -2 0 2 3 </pre> <h2>Sample Output 1</h2> <pre> -3 </pre> <p> The device moves as follows while it is performing the first command. </p> <pre> 1 0 -2 </pre> <p> When the first command is performed, the stacking sequence of the card bundle, from top to bottom, changes to the sequence shown below. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/PCK2018_disk2" width="480"><br> Fig.2 An illustrative example of how the device works </center> <p> Therefore, the device is set to -3 after all actions have completed. Output this value. </p> <h2>Sample Input 2</h2> <pre> 5 7 5 0 0 3 3 3 3 3 1 2 2 3 3 4 4 5 5 6 </pre> <h2>Sample Output 2</h2> <pre> 1 2 3 4 5 </pre>
p03950
<span class="lang-en"> <p>Score : <var>1600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a rectangle in the <var>xy</var>-plane, with its lower left corner at <var>(0, 0)</var> and its upper right corner at <var>(W, H)</var>. Each of its sides is parallel to the <var>x</var>-axis or <var>y</var>-axis. Initially, the whole region within the rectangle is painted white.</p> <p>Snuke plotted <var>N</var> points into the rectangle. The coordinate of the <var>i</var>-th (<var>1 ≦ i ≦ N</var>) point was <var>(x_i, y_i)</var>.</p> <p>Then, for each <var>1 ≦ i ≦ N</var>, he will paint one of the following four regions black:</p> <ul> <li>the region satisfying <var>x &lt; x_i</var> within the rectangle</li> <li>the region satisfying <var>x &gt; x_i</var> within the rectangle</li> <li>the region satisfying <var>y &lt; y_i</var> within the rectangle</li> <li>the region satisfying <var>y &gt; y_i</var> within the rectangle</li> </ul> <p>Find the longest possible perimeter of the white region of a rectangular shape within the rectangle after he finishes painting.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ W, H ≦ 10^8</var></li> <li><var>1 ≦ N ≦ 3 \times 10^5</var></li> <li><var>0 ≦ x_i ≦ W</var> (<var>1 ≦ i ≦ N</var>)</li> <li><var>0 ≦ y_i ≦ H</var> (<var>1 ≦ i ≦ N</var>)</li> <li><var>W</var>, <var>H</var> (21:32, added), <var>x_i</var> and <var>y_i</var> are integers.</li> <li>If <var>i ≠ j</var>, then <var>x_i ≠ x_j</var> and <var>y_i ≠ y_j</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>W</var> <var>H</var> <var>N</var> <var>x_1</var> <var>y_1</var> <var>x_2</var> <var>y_2</var> <var>:</var> <var>x_N</var> <var>y_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the longest possible perimeter of the white region of a rectangular shape within the rectangle after Snuke finishes painting.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>10 10 4 1 6 4 1 6 9 9 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>32 </pre> <p>In this case, the maximum perimeter of <var>32</var> can be obtained by painting the rectangle as follows:</p> <div style="text-align: center;"> <img alt="842bb3939c9721d978d4e122b0bfff55.png" src="https://atcoder.jp/img/arc063/842bb3939c9721d978d4e122b0bfff55.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 4 5 0 0 1 1 2 2 4 3 5 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>12 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100 100 8 19 33 8 10 52 18 94 2 81 36 88 95 67 83 20 71 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>270 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000000 100000000 1 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>399999994 </pre></section> </div> </span>
p02642
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given is a number sequence <var>A</var> of length <var>N</var>.</p> <p>Find the number of integers <var>i</var> <var>\left(1 \leq i \leq N\right)</var> with the following property:</p> <ul> <li>For every integer <var>j</var> <var>\left(1 \leq j \leq N\right)</var> such that <var>i \neq j </var>, <var>A_j</var> does not divide <var>A_i</var>.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All values in input are integers.</li> <li><var>1 \leq N \leq 2 \times 10^5</var></li> <li><var>1 \leq A_i \leq 10^6</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>A_2</var> <var>\cdots</var> <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 24 11 8 3 16 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>The integers with the property are <var>2</var>, <var>3</var>, and <var>4</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 5 5 5 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Note that there can be multiple equal numbers.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>10 33 18 45 28 8 19 89 86 2 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>5 </pre></section> </div> </span>
p02357
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML"> </script> <H1>Sliding Minimum Element</H1> <p> For a given array $a_1, a_2, a_3, ... , a_N$ of $N$ elements and an integer $L$, find the minimum of each possible sub-arrays with size $L$ and print them from the beginning. For example, for an array $\{1, 7, 7, 4, 8, 1, 6\}$ and $L = 3$, the possible sub-arrays with size $L = 3$ includes $\{1, 7, 7\}$, $\{7, 7, 4\}$, $\{7, 4, 8\}$, $\{4, 8, 1\}$, $\{8, 1, 6\}$ and the minimum of each sub-array is 1, 4, 4, 1, 1 respectively. </p> <h2>Constraints</h2> <ul> <li> $1 \leq N \leq 10^6$ </li> <li> $1 \leq L \leq 10^6$</li> <li> $1 \leq a_i \leq 10^9$</li> <li> $L \leq N$ </li> </ul> <h2>Input</h2> <p>The input is given in the following format.</p> <p> $N$ $L$<br> $a_1$ $a_2$ ... $a_N$<br> </p> <h2>Output</h2> <p> Print a sequence of the minimum in a line. Print a space character between adjacent elements. </p> <h2>Sample Input 1</h2> <pre> 7 3 1 7 7 4 8 1 6 </pre> <h2>Sample Output 1</h2> <pre> 1 4 4 1 1 </pre>
p01982
<h3>一般化うるう年</h3> <!-- begin ja only --> <p>通常,西暦 <i>x</i> 年がうるう年か否かは以下のように定義されている.</p> <ol> <li><i>x</i> が 400 の倍数であれば,うるう年である.</li> <li>そうでないとき,<i>x</i> が 100 の倍数であれば,うるう年ではない.</li> <li>そうでないとき,<i>x</i> が 4 の倍数であれば,うるう年である.</li> <li>そうでないとき,うるう年ではない.</li> </ol> <p>これは次のように一般化できる.ある数列 <i>A<sub>1</sub>, ..., A<sub>n</sub></i> について,西暦 <i>x</i> 年が "一般化うるう年" であるか否かを以下のように定義する.</p> <ol> <li><i>x</i> が <i>A<sub>i</sub></i> の倍数であるような最小の <i>i</i> (<i>1 &le; i &le; n</i>) について,<i>i</i> が奇数であれば一般化うるう年であり,偶数であれば一般化うるう年ではない.</li> <li>そのような <i>i</i> が存在しないとき,<i>n</i> が奇数であれば一般化うるう年ではなく,偶数であれば一般化うるう年である.</li> </ol> <p>例えば <i>A = [400, 100, 4]</i> のとき,この <i>A</i> に対する一般化うるう年は通常のうるう年と等価になる.</p> <p>数列 <i>A<sub>1</sub>, ..., A<sub>n</sub></i> と正の整数 <i>l, r</i> が与えられるので.<i>l &le; x &le; r</i> なる正の整数 <i>x</i> のうち,西暦 <i>x</i> 年が <i>A</i> に対する一般化うるう年であるような <i>x</i> の個数を答えよ.</p> <!-- end ja only --> <h3>Input</h3> <!-- begin ja only --> <p>入力は最大で 50 個のデータセットからなる.各データセットは次の形式で表される.</p> <blockquote><i>n</i> <i>l</i> <i>r</i> <i>A<sub>1</sub></i> <i>A<sub>2</sub></i> <i>...</i> <i>A<sub>n</sub></i></blockquote> <p>整数 <i>n</i> は <i>1 &le; n &le; 50</i> を満たす.整数 <i>l,r</i> は<i>1 &le; l &le; r &le; 4000</i> を満たす.各 <i>i</i> に対し整数 <i>A<sub>i</sub></i> は <i>1 &le; A<sub>i</sub> &le; 4000</i> を満たす.</p> <p>入力の終わりは 3 つのゼロからなる行で表される.</p> <!-- end ja only --> <h3>Output</h3> <!-- begin ja only --> <p>各データセットについて,答えを 1 行で出力せよ.</p> <!-- end ja only --> <h3>Sample Input</h3><pre>3 1988 2014 400 100 4 1 1000 1999 1 2 1111 3333 2 2 6 2000 3000 5 7 11 9 3 13 0 0 0 </pre><h3>Output for the Sample Input</h3><pre>7 1000 2223 785 </pre>
p00690
<h1> A Long Ride on a Railway</h1> <p> Travelling by train is fun and exciting. But more than that indeed. Young challenging boys often tried to purchase the longest single tickets and to single ride the longest routes of various railway systems. Route planning was like solving puzzles. However, once assisted by computers, and supplied with machine readable databases, it is not any more than elaborating or hacking on the depth-first search algorithms.</p> <p> Map of a railway system is essentially displayed in the form of a graph. (See the Figures below.) The vertices (i.e. points) correspond to stations and the edges (i.e. lines) correspond to direct routes between stations. The station names (denoted by positive integers and shown in the upright letters in the Figures) and direct route distances (shown in the slanted letters) are also given.</p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_railway1"> </center> <p> The problem is to find the length of the longest path and to form the corresponding list of stations on the path for each system. The longest of such paths is one that starts from one station and, after travelling many direct routes as far as possible without passing any direct route more than once, arrives at a station which may be the starting station or different one and is longer than any other sequence of direct routes. Any station may be visited any number of times in travelling the path if you like. </p> <h2>Input</h2> <p>The graph of a system is defined by a set of lines of integers of the form: </p> <table> <tr><td><i>ns</i></td><td><i>nl</i></td></tr> <tr><td><i>s</i><sub>1,1</sub></td> <td><i>s</i><sub>1,2</sub></td> <td><i>d</i><sub>1</sub></td></tr> <tr><td><i>s</i><sub>2,1</sub></td> <td><i>s</i><sub>2,2</sub></td> <td><i>d</i><sub>2</sub></td></tr> <tr><td> ... </td></tr> <tr><td><i>s</i><sub><i>nl</i>,1</sub></td> <td><i>s</i><sub><i>nl</i>,2</sub></td> <td><i>d</i><sub><i>nl</i></sub></td></tr> </table> <p> Here 1 <= <i>ns</i> <= 10 is the number of stations (so, station names are 1, 2, ..., <i>ns</i>), and 1 <= <i>nl</i> <= 20 is the number of direct routes in this graph. </p> <p> <i>s</i><sub><i>i</i>,1</sub> and <i>s</i><sub><i>i</i>,2</sub> (<i>i</i> = 1, 2, ..., <i>nl</i>) are station names at the both ends of the <i>i</i>-th direct route. <i>d<sub>i</sub></i> >= 1 (<i>i</i> = 1, 2, ..., <i>nl</i>) is the direct route distance between two different stations <i>s</i><sub><i>i</i>,1</sub> and <i>s</i><sub><i>i</i>,2</sub>. </p> <p> It is also known that there is at most one direct route between any pair of stations, and all direct routes can be travelled in either directions. Each station has at most four direct routes leaving from it.</p> <p> The integers in an input line are separated by at least one white space (i.e. a space character (ASCII code 32) or a tab character (ASCII code 9)), and possibly preceded and followed by a number of white spaces. </p> <p> The whole input of this problem consists of a few number of sets each of which represents one graph (i.e. system) and the input terminates with two integer <tt>0</tt>'s in the line of next <i>ns</i> and <i>nl</i>.</p> <h2>Output</h2> <p> Paths may be travelled from either end; loops can be traced clockwise or anticlockwise. So, the station lists for the longest path for Figure A are multiple like (in lexicographic order):</p> <pre> 2 3 4 5 5 4 3 2 </pre> <p> For Figure B, the station lists for the longest path are also multiple like (again in lexicographic order):</p> <pre> 6 2 5 9 6 7 6 9 5 2 6 7 7 6 2 5 9 6 7 6 9 5 2 6 </pre> <p> Yet there may be the case where the longest paths are not unique. (That is, multiple independent paths happen to have the same longest distance.) To make the answer canonical, it is required to answer the lexicographically least station list of the path(s) for each set. Thus for Figure A, </p> <pre> 2 3 4 5 </pre> <p> and for Figure B, </p> <pre>6 2 5 9 6 7</pre></p> <p> must be reported as the answer station list in a single line. The integers in a station list must be separated by at least one white space. The line of station list must be preceded by a separate line that gives the length of the corresponding path. Every output line may start with white spaces. (See output sample.) </p> <h2>Sample Input</h2> <pre> 6 5 1 3 10 2 3 12 3 4 2 4 5 13 4 6 11 10 10 1 2 11 2 3 25 2 5 81 2 6 82 4 5 58 5 9 68 6 7 80 6 9 67 8 9 44 9 10 21 0 0 </pre> <p> Warning: The station names are given in the lexicographic order here. The contestants should not depend on the order. </p> <h2>Output for the Sample Input</h2> <pre> 27 2 3 4 5 378 6 2 5 9 6 7 </pre>
p03815
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has decided to play with a six-sided die. Each of its six sides shows an integer <var>1</var> through <var>6</var>, and two numbers on opposite sides always add up to <var>7</var>.</p> <p>Snuke will first put the die on the table with an arbitrary side facing upward, then repeatedly perform the following operation:</p> <ul> <li>Operation: Rotate the die <var>90°</var> toward one of the following directions: left, right, front (the die will come closer) and back (the die will go farther). Then, obtain <var>y</var> points where <var>y</var> is the number written in the side facing upward.</li> </ul> <p>For example, let us consider the situation where the side showing <var>1</var> faces upward, the near side shows <var>5</var> and the right side shows <var>4</var>, as illustrated in the figure. If the die is rotated toward the right as shown in the figure, the side showing <var>3</var> will face upward. Besides, the side showing <var>4</var> will face upward if the die is rotated toward the left, the side showing <var>2</var> will face upward if the die is rotated toward the front, and the side showing <var>5</var> will face upward if the die is rotated toward the back.</p> <div style="text-align: center;"> <img alt="864abc2e4a08c26015ffd007a30aab03.png" src="https://atcoder.jp/img/arc068/864abc2e4a08c26015ffd007a30aab03.png"> </img></div> <p>Find the minimum number of operation Snuke needs to perform in order to score at least <var>x</var> points in total.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ x ≦ 10^{15}</var></li> <li><var>x</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>x</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>149696127901 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>27217477801 </pre></section> </div> </span>
p01828
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script type='text/javascript' src='http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script> </script> <h2>M and A</h2> <p> The CEO of the company named $S$ is planning M&amp;A with another company named $T$. M&amp;A is an abbreviation for "Mergers and Acquisitions". The CEO wants to keep the original company name $S$ through the M&amp;A on the plea that both company names are mixed into a new one. </p> <p> The CEO insists that the mixed company name after the M&amp;A is produced as follows. </p> <p> Let $s$ be an arbitrary subsequence of $S$, and $t$ be an arbitrary subsequence of $T$. The new company name must be a string of the same length to $S$ obtained by alternatively lining up the characters in $s$ and $t$. More formally, $s_0 + t_0 + s_1 + t_1 + ...$ or $t_0 + s_0 + t_1 + s_1 + ... $ can be used as the company name after M&amp;A. Here, $s_k$ denotes the $k$-th (0-based) character of string $s$. Please note that the lengths of $s$ and $t$ will be different if the length of $S$ is odd. In this case, the company name after M&amp;A is obtained by $s_0 + t_0 + ... + t_{|S|/2} + s_{|S|/2+1}$ or $t_0 + s_0 + ... + s_{|S|/2} + t_{|S|/2+1}$ ($|S|$ denotes the length of $S$ and "/" denotes integer division). </p> <p> A subsequence of a string is a string which is obtained by erasing zero or more characters from the original string. For example, the strings "abe", "abcde" and "" (the empty string) are all subsequences of the string "abcde". </p> <p> You are a programmer employed by the acquiring company. You are assigned a task to write a program that determines whether it is possible to make $S$, which is the original company name, by mixing the two company names. </p> <h3>Input</h3> <p> The input consists of a single test case. The test case consists of two lines. </p> <p> The first line contains a string $S$ which denotes the name of the company that you belong to. The second line contains a string $T$ which denotes the name of the target company of the planned M&amp;A. The two names $S$ and $T$ are non-empty and of the same length no longer than 1,000 characters, and all the characters used in the names are lowercase English letters. </p> <h3>Output</h3> <p> Print "Yes" in a line if it is possible to make original company name by combining $S$ and $T$. Otherwise, print "No" in a line. </p> <h3>Sample Input 1</h3> <pre> acmicpc tsukuba </pre> <h3>Output for the Sample Input 1</h3> <pre> No </pre> <h3>Sample Input 2</h3> <pre> hoge moen </pre> <h3>Output for the Sample Input 2</h3> <pre> Yes </pre> <h3>Sample Input 3</h3> <pre> abcdefg xacxegx </pre> <h3>Output for the Sample Input 3</h3> <pre> Yes </pre>
p02707
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>A company has <var>N</var> members, who are assigned ID numbers <var>1, ..., N</var>.</p> <p>Every member, except the member numbered <var>1</var>, has exactly one immediate boss with a smaller ID number.</p> <p>When a person <var>X</var> is the immediate boss of a person <var>Y</var>, the person <var>Y</var> is said to be an immediate subordinate of the person <var>X</var>.</p> <p>You are given the information that the immediate boss of the member numbered <var>i</var> is the member numbered <var>A_i</var>. For each member, find how many immediate subordinates it has.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 2 \times 10^5</var></li> <li><var>1 \leq A_i &lt; i</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_2</var> <var>...</var> <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>For each of the members numbered <var>1, 2, ..., N</var>, print the number of immediate subordinates it has, in its own line.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 1 1 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 2 0 0 0 </pre> <p>The member numbered <var>1</var> has two immediate subordinates: the members numbered <var>2</var> and <var>3</var>.</p> <p>The member numbered <var>2</var> has two immediate subordinates: the members numbered <var>4</var> and <var>5</var>.</p> <p>The members numbered <var>3</var>, <var>4</var>, and <var>5</var> do not have immediate subordinates.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 1 1 1 1 1 1 1 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>9 0 0 0 0 0 0 0 0 0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>7 1 2 3 4 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 1 1 1 1 1 0 </pre></section> </div> </span>
p03546
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Joisino the magical girl has decided to turn every single digit that exists on this world into <var>1</var>.</p> <p>Rewriting a digit <var>i</var> with <var>j</var> <var>(0≤i,j≤9)</var> costs <var>c_{i,j}</var> MP (Magic Points).</p> <p>She is now standing before a wall. The wall is divided into <var>HW</var> squares in <var>H</var> rows and <var>W</var> columns, and at least one square contains a digit between <var>0</var> and <var>9</var> (inclusive).</p> <p>You are given <var>A_{i,j}</var> that describes the square at the <var>i</var>-th row from the top and <var>j</var>-th column from the left, as follows:</p> <ul> <li>If <var>A_{i,j}≠-1</var>, the square contains a digit <var>A_{i,j}</var>.</li> <li>If <var>A_{i,j}=-1</var>, the square does not contain a digit.</li> </ul> <p>Find the minimum total amount of MP required to turn every digit on this wall into <var>1</var> in the end.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≤H,W≤200</var></li> <li><var>1≤c_{i,j}≤10^3</var> <var>(i≠j)</var></li> <li><var>c_{i,j}=0</var> <var>(i=j)</var></li> <li><var>-1≤A_{i,j}≤9</var></li> <li>All input values are integers.</li> <li>There is at least one digit on the wall.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>c_{0,0}</var> <var>...</var> <var>c_{0,9}</var> <var>:</var> <var>c_{9,0}</var> <var>...</var> <var>c_{9,9}</var> <var>A_{1,1}</var> <var>...</var> <var>A_{1,W}</var> <var>:</var> <var>A_{H,1}</var> <var>...</var> <var>A_{H,W}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum total amount of MP required to turn every digit on the wall into <var>1</var> in the end.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 4 0 9 9 9 9 9 9 9 9 9 9 0 9 9 9 9 9 9 9 9 9 9 0 9 9 9 9 9 9 9 9 9 9 0 9 9 9 9 9 9 9 9 9 9 0 9 9 9 9 2 9 9 9 9 9 0 9 9 9 9 9 9 9 9 9 9 0 9 9 9 9 9 9 9 9 9 9 0 9 9 9 9 9 9 2 9 9 9 0 9 9 2 9 9 9 9 9 9 9 0 -1 -1 -1 -1 8 1 1 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>12 </pre> <p>To turn a single <var>8</var> into <var>1</var>, it is optimal to first turn <var>8</var> into <var>4</var>, then turn <var>4</var> into <var>9</var>, and finally turn <var>9</var> into <var>1</var>, costing <var>6</var> MP.</p> <p>The wall contains two <var>8</var>s, so the minimum total MP required is <var>6×2=12</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 5 0 999 999 999 999 999 999 999 999 999 999 0 999 999 999 999 999 999 999 999 999 999 0 999 999 999 999 999 999 999 999 999 999 0 999 999 999 999 999 999 999 999 999 999 0 999 999 999 999 999 999 999 999 999 999 0 999 999 999 999 999 999 999 999 999 999 0 999 999 999 999 999 999 999 999 999 999 0 999 999 999 999 999 999 999 999 999 999 0 999 999 999 999 999 999 999 999 999 999 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Note that she may not need to change any digit.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 5 0 4 3 6 2 7 2 5 3 3 4 0 5 3 7 5 3 7 2 7 5 7 0 7 2 9 3 2 9 1 3 6 2 0 2 4 6 4 2 3 3 5 7 4 0 6 9 7 6 7 9 8 5 2 2 0 4 7 6 5 5 4 6 3 2 3 0 5 4 3 3 6 2 3 4 2 4 0 8 9 4 6 5 4 3 5 3 2 0 8 2 1 3 4 5 7 8 6 4 0 3 5 2 6 1 2 5 3 2 1 6 9 2 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>47 </pre></section> </div> </span>
p01081
<h1>Problem J: Char Swap</h1> <h2>Problem</h2> <p> 長さが偶数の文字列<var>S</var>が与えられる。<br> あなたは、文字列<var>S</var>の隣り合った2つの文字を入れ替える操作を何回でも行うことができる。<br> 文字列<var>S</var>を回文にするためには、最小で何回の操作を行う必要があるだろうか?<br> 回文にすることが不可能な場合は-1を出力せよ。 </p> <h2>Input</h2> <p>入力は以下の形式で与えられる。</p> <pre> <var>S</var> </pre> <p> 1行に文字列<var>S</var>が与えられる。 </p> <h2>Constraints</h2> <ul> <li>2 &le; |<var>S</var>| &le; 4 &times; 10<sup>5</sup></li> <li>文字列はすべて英小文字で構成されている</li> <li>文字列の長さは偶数である</li> </ul> <h2>Output</h2> <p> 文字列<var>S</var>を回文にするための最小の操作回数を1行に出力する。回文にすることが不可能な場合は代わりに-1を1行に出力をする。 </p> <h2>Sample Input 1</h2> <pre> acca </pre> <h2>Sample Output 1</h2> <pre> 0 </pre> <h2>Sample Input 2</h2> <pre> acpcacpc </pre> <h2>Sample Output 2</h2> <pre> 3 </pre> <h2>Sample Input 3</h2> <pre> aizu </pre> <h2>Sample Output 3</h2> <pre> -1 </pre>
p03116
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p><font color="red"><strong>Red bold fonts show the difference from C1.</strong></font></p> <p>There is an infinitely large triangular grid, as shown below. Each point with integer coordinates contains a lamp.</p> <p><img alt="" src="https://img.atcoder.jp/wtf19/f617c94527a62ed72fe7db12b6d1f6b0.png"/></p> <p>Initially, only the lamp at <font color="red"><strong><var>(X, Y)</var></strong></font> was on, and all other lamps were off. Then, Snuke performed the following operation zero or more times:</p> <ul> <li>Choose two integers <var>x</var> and <var>y</var>. Toggle (on to off, off to on) the following three lamps: <var>(x, y), (x, y+1), (x+1, y)</var>.</li> </ul> <p>After the operations, <var>N</var> lamps <var>(x_1, y_1), \cdots, (x_N, y_N)</var> are on, and all other lamps are off. Find <font color="red"><strong><var>X</var> and <var>Y</var></strong></font>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><font color="red"><strong><var>1 \leq N \leq 10^4</var></strong></font></li> <li><var>-10^{17} \leq x_i, y_i \leq 10^{17}</var></li> <li><var>(x_i, y_i)</var> are pairwise distinct.</li> <li>The input is consistent with the statement, and you can uniquely determine <font color="red"><strong><var>X</var> and <var>Y</var></strong></font>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>x_1</var> <var>y_1</var> <var>:</var> <var>x_N</var> <var>y_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <font color="red"><strong><var>X</var> and <var>Y</var>, separated by a space</strong></font>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 -2 1 -2 2 0 1 1 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>-1 0 </pre> <p>The following picture shows one possible sequence of operations:</p> <p><img alt="" src="https://img.atcoder.jp/wtf19/cff6dc4d81e995e9300ccbaca5bf85de.png"/></p></section> </div> </span>
p00413
<h1>海苔(のり)</h1> <p>  ヒバラ海の浜辺の集落では海苔の生産が盛んです。海苔はすべて長方形に成型されます。海苔を天日干しするときは、2枚の海苔を同じ敷物の上に乗せて、1枚目の上辺と2枚目の上辺が平行になるように置きます。アルバイトのゲンムさんは平行に置くことはできるのですが、まだ慣れていないため重なった部分ができてしまうことがあります。重なった部分は売り物にならないので、重なっていない部分の海苔の面積を求めておく必要があります。 </p> <p> 同じ敷物の上に置いた2枚の海苔それぞれの左下端の位置と幅と高さが与えられたとき、重なっていない部分の海苔の面積を出力するプログラムを作成せよ。 </p> <h2>入力</h2> <p> 入力は以下の形式で与えられる。 </p> <pre> $x_1$ $y_1$ $w_1$ $h_1$ $x_2$ $y_2$ $w_2$ $h_2$ </pre> <p> 1行目に1枚目の海苔の左下端の座標$x_1,y_1$ ($0 \leq x_1,y_1 \leq 1,000$)と幅と高さ$w_1,h_1$ ($1 \leq w_1,h_1 \leq 1,000$)が与えられる。2行目に2枚目の海苔の左下端の座標$x_2,y_2$ ($0 \leq x_2,y_2 \leq 1,000$)と幅と高さ$w_2,h_2$ ($1 \leq w_2,h_2 \leq 1,000$)が与えられる。入力はすべて整数で与えられる。 </p> <h2>出力</h2> <p> 重なっていない部分の海苔の面積を整数で1行に出力する。 </p> <h2>入出力例</h2> <h3>入力例1</h3> <pre> 0 0 4 5 2 2 3 6 </pre> <h3>出力例1</h3> <pre> 26 </pre> <h3>入力例2</h3> <pre> 1 2 2 1 2 0 2 2 </pre> <h3>出力例2</h3> <pre> 6 </pre> <h3>入力例3</h3> <pre> 0 0 1 1 0 0 1 1 </pre> <h3>出力例3</h3> <pre> 0 </pre>  
p00043
<H1>パズル</H1> <p> 1 〜 9 の数字を 14 個組み合わせて完成させるパズルがあります。与えられた 13 個の数字にもうひとつ数字を付け加えて完成させます。 </p> <p> パズルの完成条件は </p> <ul> <li>同じ数字を2つ組み合わせたものが必ずひとつ必要です。</li> <li>残りの12 個の数字は、3個の数字の組み合わせ4つです。<br> 3個の数字の組み合わせ方は、同じ数字を3つ組み合わせたものか、または3つの連続する数字を組み合わせたものです。ただし、9 1 2 のような並びは連続する数字とは認められません。</li> <li>同じ数字は4 回まで使えます。</li> </ul> <p> 13 個の数字からなる文字列を読み込んで、パズルを完成することができる数字を昇順に全て出力するプログラムを作成してください。なお、1〜9 のどの数字を付け加えてもパズルを完成させることができないときは 0 を出力してください。 </p> <p>例えば与えられた文字列が 3456666777999 の場合<br/> <br/> 「2」があれば、 <span>234 567 666 77 999</span><br/> 「3」があれば、 <span>33 456 666 777 999</span><br/> 「5」があれば、 <span>345 567 666 77 999</span><br/> 「8」があれば、 <span>345 666 678 77 999</span><br/> <br/> というふうに、2 3 5 8 のいずれかの数字が付け加えられるとパズルは完成します。「6」でも整いますが、5 回目の使用になるので、この例では使えないことに注意してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットとして、13 個の数字が1行に与えられます。データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、パズルを完成させることができる数字を昇順に空白区切りで1行に出力します。 </p> <H2>Sample Input</H2> <pre> 3649596966777 6358665788577 9118992346175 9643871425498 7755542764533 1133557799246 </pre> <H2>Output for the Sample Input</H2> <pre> 2 3 5 8 3 4 1 2 3 4 5 6 7 8 9 7 8 9 1 2 3 4 6 7 8 0 </pre>
p02584
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi, who lives on the number line, is now at coordinate <var>X</var>. He will make exactly <var>K</var> moves of distance <var>D</var> in the positive or negative direction.</p> <p>More specifically, in one move, he can go from coordinate <var>x</var> to <var>x + D</var> or <var>x - D</var>.</p> <p>He wants to make <var>K</var> moves so that the absolute value of the coordinate of the destination will be the smallest possible.</p> <p>Find the minimum possible absolute value of the coordinate of the destination.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>-10^{15} \leq X \leq 10^{15}</var></li> <li><var>1 \leq K \leq 10^{15}</var></li> <li><var>1 \leq D \leq 10^{15}</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>X</var> <var>K</var> <var>D</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum possible absolute value of the coordinate of the destination.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>Takahashi is now at coordinate <var>6</var>. It is optimal to make the following moves:</p> <ul> <li>Move from coordinate <var>6</var> to (<var>6 - 4 =</var>) <var>2</var>.</li> <li>Move from coordinate <var>2</var> to (<var>2 - 4 =</var>) <var>-2</var>.</li> </ul> <p>Here, the absolute value of the coordinate of the destination is <var>2</var>, and we cannot make it smaller.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 4 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 </pre> <p>Takahashi is now at coordinate <var>7</var>. It is optimal to make, for example, the following moves:</p> <ul> <li>Move from coordinate <var>7</var> to <var>4</var>.</li> <li>Move from coordinate <var>4</var> to <var>7</var>.</li> <li>Move from coordinate <var>7</var> to <var>4</var>.</li> <li>Move from coordinate <var>4</var> to <var>1</var>.</li> </ul> <p>Here, the absolute value of the coordinate of the destination is <var>1</var>, and we cannot make it smaller.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>10 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>8 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>1000000000000000 1000000000000000 1000000000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>1000000000000000 </pre> <p>The answer can be enormous.</p></section> </div> </span>
p01202
<H1><font color="#000">Problem A:</font> Dance Dance Revolution</H1> <p> <i>Dance Dance Revolution</i> is one of the most popular arcade games in Japan. The rule of this game is very simple. A series of four arrow symbols, <i>up</i>, <i>down</i>, <i>left</i> and <i>right</i>, flows downwards on the screen in time to music. The machine has four panels under your foot, each of which corresponds to one of the four arrows, and you have to make steps on these panels according to the arrows displayed on the screen. The more accurate in timing, the higher score you will mark. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_dancedance"> <p>Figure 1: Layout of Arrow Panels and Screen</p> </center> <p> Each series of arrows is commonly called a score. Difficult scores usually have hundreds of arrows, and newbies will often be scared when seeing those arrows fill up the monitor screen. Conversely, scores with fewer arrows are considered to be easy ones. </p> <p> One day, a friend of yours, who is an enthusiast of this game, asked you a favor. What he wanted is an automated method to see if given scores are <i>natural</i>. A score is considered as natural when there is a sequence of steps that meets all the following conditions: </p> <ul> <li> left-foot steps and right-foot steps appear in turn;</li> <li> the same panel is not stepped on any two consecutive arrows;</li> <li> a player can keep his or her upper body facing forward during a play; and <li> his or her legs never cross each other.</li> </ul> <p> Your task is to write a program that determines whether scores are natural ones. </p> <H2>Input</H2> <p> The first line of the input contains a positive integer. This indicates the number of data sets. </p> <p> Each data set consists of a string written in a line. Each string is made of four letters, “U” for up, “D” for down, “L” for left and “R” for right, and specifies arrows in a score. No string contains more than 100,000 characters. </p> <H2>Output</H2> <p> For each data set, output in a line “Yes” if the score is natural, or “No” otherwise. </p> <H2>Sample Input</H2> <pre> 3 UU RDUL ULDURDULDURDULDURDULDURD </pre> <H2>Output for the Sample Input</H2> <pre> No Yes Yes </pre>
p04000
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a grid with <var>H</var> rows and <var>W</var> columns. At first, all cells were painted white.</p> <p>Snuke painted <var>N</var> of these cells. The <var>i</var>-th ( <var>1 \leq i \leq N</var> ) cell he painted is the cell at the <var>a_i</var>-th row and <var>b_i</var>-th column.</p> <p>Compute the following:</p> <ul> <li>For each integer <var>j</var> ( <var>0 \leq j \leq 9</var> ), how many subrectangles of size <var>3×3</var> of the grid contains exactly <var>j</var> black cells, after Snuke painted <var>N</var> cells?</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>3 \leq H \leq 10^9</var></li> <li><var>3 \leq W \leq 10^9</var></li> <li><var>0 \leq N \leq min(10^5,H×W)</var></li> <li><var>1 \leq a_i \leq H</var> <var>(1 \leq i \leq N)</var></li> <li><var>1 \leq b_i \leq W</var> <var>(1 \leq i \leq N)</var></li> <li><var>(a_i, b_i) \neq (a_j, b_j)</var> <var>(i \neq j)</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>N</var> <var>a_1</var> <var>b_1</var> : <var>a_N</var> <var>b_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>10</var> lines. The <var>(j+1)</var>-th ( <var>0 \leq j \leq 9</var> ) line should contain the number of the subrectangles of size <var>3×3</var> of the grid that contains exactly <var>j</var> black cells.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 5 8 1 1 1 4 1 5 2 3 3 1 3 2 3 4 4 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>0 0 0 2 4 0 0 0 0 0 </pre> <p><img alt="" src="https://atcoder.jp/img/arc061/30326702be007759dce81231012a8353.png"/></p> <p>There are six subrectangles of size <var>3×3</var>. Two of them contain three black cells each, and the remaining four contain four black cells each.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 10 20 1 1 1 4 1 9 2 5 3 10 4 2 4 7 5 9 6 4 6 6 6 7 7 1 7 3 7 7 8 1 8 5 8 10 9 2 10 4 10 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>4 26 22 10 2 0 0 0 0 0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1000000000 1000000000 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>999999996000000004 0 0 0 0 0 0 0 0 0 </pre></section> </div> </span>
p01652
<h2>Problem Statement</h2> <p> One day in a forest, Alice found an old monolith. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ancientCommemorative1"><br/> <br/> </center> <p> She investigated the monolith, and found this was a sentence written in an old language. A sentence consists of glyphs and rectangles which surrounds them. For the above example, there are following glyphs. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ancientCommemorative2"><br/> <br/> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ancientCommemorative3"><br/> <br/> </center> <p> Notice that some glyphs are flipped horizontally in a sentence. </p> <p> She decided to transliterate it using ASCII letters assigning an alphabet to each glyph, and <code>[</code> and <code>]</code> to rectangles. If a sentence contains a flipped glyph, she transliterates it from right to left. For example, she could assign <code>a</code> and <code>b</code> to the above glyphs respectively. Then the above sentence would be transliterated into <code>[[ab][ab]a]</code>. </p> <p> After examining the sentence, Alice figured out that the sentence was organized by the following structure: </p> <ul> <li> A sentence &lt;seq&gt; is a sequence consists of zero or more &lt;term&gt;s. </li> <li> A term &lt;term&gt; is either a glyph or a &lt;box&gt;. A glyph may be flipped.</li> <li> &lt;box&gt; is a rectangle surrounding a &lt;seq&gt;. The height of a box is larger than any glyphs inside.</li> </ul> <p> Now, the sentence written on the monolith is a nonempty &lt;seq&gt;. Each term in the sequence fits in a rectangular bounding box, although the boxes are not explicitly indicated for glyphs. Those bounding boxes for adjacent terms have no overlap to each other. Alice formalized the transliteration rules as follows. </p> <p> Let <var>f</var> be the transliterate function. </p> <p> Each sequence <var>s = t_1 t_2 ... t_m</var> is written either from left to right, or from right to left. However, please note here <var>t_1</var> corresponds to the leftmost term in the sentence, <var>t_2</var> to the 2nd term from the left, and so on. </p> <p> Let's define <var>&#7713;</var> to be the flipped glyph <var>g</var>. A sequence must have been written from right to left, when a sequence contains one or more single-glyph term which is unreadable without flipping, i.e. there exists an integer <var>i</var> where <var>t_i</var> is a single glyph <var>g</var>, and <var>g</var> is not in the glyph dictionary given separately whereas <var>&#7713;</var> is. In such cases <var>f(s)</var> is defined to be <var>f(t_m) f(t_{m-1}) ... f(t_1)</var>, otherwise <var>f(s) = f(t_1) f(t_2) ... f(t_m)</var>. It is guaranteed that all the glyphs in the sequence are flipped if there is one or more glyph which is unreadable without flipping. </p> <p> If the term <var>t_i</var> is a box enclosing a sequence <var>s'</var>, <var>f(t_i) = </var><code>[</code><var> f(s') </var><code>]</code>. If the term <var>t_i</var> is a glyph <var>g</var>, <var>f(t_i)</var> is mapped to an alphabet letter corresponding to the glyph <var>g</var>, or <var>&#7713;</var> if the sequence containing <var>g</var> is written from right to left. </p> <p> Please make a program to transliterate the sentences on the monoliths to help Alice. </p> <h2>Input</h2> <p> The input consists of several datasets. The end of the input is denoted by two zeros separated by a single-space. </p> <p> Each dataset has the following format. </p> <pre> <var>n</var> <var>m</var> <var>glyph_1</var> ... <var>glyph_n</var> <var>string_1</var> ... <var>string_m</var> </pre> <p> <var>n</var> (<var>1\leq n\leq 26</var>) is the number of glyphs and <var>m</var> (<var>1\leq m\leq 10</var>) is the number of monoliths. <var>glyph_i</var> is given by the following format. </p> <pre> <var>c</var> <var>h</var> <var>w</var> <var>b_{11}</var>...<var>b_{1w}</var> ... <var>b_{h1}</var>...<var>b_{hw}</var> </pre> <p> <var>c</var> is a lower-case alphabet that Alice assigned to the glyph. <var>h</var> and <var>w</var> (<var>1 \leq h \leq 15</var>, <var>1 \leq w \leq 15</var>) specify the height and width of the bitmap of the glyph, respectively. The matrix <var>b</var> indicates the bitmap. A white cell is represented by <code>.</code> and a black cell is represented by <code>*</code>. </p> <p> You can assume that all the glyphs are assigned distinct characters. You can assume that every column of the bitmap contains at least one black cell, and the first and last row of the bitmap contains at least one black cell. Every bitmap is different to each other, but it is possible that a flipped bitmap is same to another bitmap. Moreover there may be symmetric bitmaps. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ancientCommemorative4" height="188" width="580"> </center> <p> <var>string_i</var> is given by the following format. </p> <pre> <var>h</var> <var>w</var> <var>b_{11}</var>...<var>b_{1w}</var> ... <var>b_{h1}</var>...<var>b_{hw}</var> </pre> <p> <var>h</var> and <var>w</var> (<var>1 \leq h \leq 100</var>, <var>1 \leq w \leq 1000</var>) is the height and width of the bitmap of the sequence. Similarly to the glyph dictionary, <var>b</var> indicates the bitmap where A white cell is represented by <code>.</code> and a black cell by <code>*</code>. </p> <p> There is no noise: every black cell in the bitmap belongs to either one glyph or one rectangle box. The height of a rectangle is at least 3 and more than the maximum height of the tallest glyph. The width of a rectangle is at least 3. </p> <p> A box must have a margin of 1 pixel around the edge of it. </p> <p> You can assume the glyphs are never arranged vertically. Moreover, you can assume that if two rectangles, or a rectangle and a glyph, are in the same column, then one of them contains the other. For all bounding boxes of glyphs and black cells of rectangles, there is at least one white cell between every two of them. You can assume at least one cell in the bitmap is black. </p> <h2>Output</h2> <p> For each monolith, output the transliterated sentence in one line. After the output for one dataset, output <code>#</code> in one line. </p> <h2>Sample Input</h2> <pre> 2 1 a 11 9 ****..... .*.*..... .*.*..... .*..*.... .**..*... ..**..*.. ..*.*.... ..**.*.*. ..*..*.*. ..*****.* ******.** b 10 6 ....*. ....*. ....*. ....*. ....*. ....*. ....*. ....*. .**..* *****. 19 55 ******************************************************* *.....................................................* *.********************.********************...........* *.*..................*.*..................*...........* *.*.****.............*.*.............****.*.****......* *.*..*.*..........*..*.*..*..........*.*..*..*.*......* *.*..*.*..........*..*.*..*..........*.*..*..*.*......* *.*..*..*.........*..*.*..*.........*..*..*..*..*.....* *.*..**..*........*..*.*..*........*..**..*..**..*....* *.*...**..*.......*..*.*..*.......*..**...*...**..*...* *.*...*.*.........*..*.*..*.........*.*...*...*.*.....* *.*...**.*.*......*..*.*..*......*.*.**...*...**.*.*..* *.*...*..*.*......*..*.*..*......*.*..*...*...*..*.*..* *.*...*****.*..**..*.*.*.*..**..*.*****...*...*****.*.* *.*.******.**.*****..*.*..*****.**.******.*.******.**.* *.*..................*.*..................*...........* *.********************.********************...........* *.....................................................* ******************************************************* 2 3 a 2 3 .*. *** b 3 3 .*. *** .*. 2 3 .*. *** 4 3 *** *.* *.* *** 9 13 ************. *..........*. *.......*..*. *...*..***.*. *..***.....*. *...*......*. *..........*. ************. ............. 3 1 a 2 2 .* ** b 2 1 * * c 3 3 *** *.* *** 11 16 **************** *..............* *....*********.* *....*.......*.* *.*..*.***...*.* *.**.*.*.*.*.*.* *....*.***.*.*.* *....*.......*.* *....*********.* *..............* **************** 0 0 </pre> <h2>Output for the Sample Input</h2> <pre> [[ab][ab]a] # a [] [ba] # [[cb]a] # </pre>
p00940
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script type='text/javascript' src='http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script> </script> <h2>Problem F Deadlock Detection</h2> <p> You are working on an analysis of a system with multiple processes and some kinds of resource (such as memory pages, DMA channels, and I/O ports). Each kind of resource has a certain number of instances. A process has to acquire resource instances for its execution. The number of required instances of a resource kind depends on a process. A process finishes its execution and terminates eventually after all the resource in need are acquired. These resource instances are released then so that other processes can use them. No process releases instances before its termination. Processes keep trying to acquire resource instances in need, one by one, without taking account of behavior of other processes. Since processes run independently of others, they may sometimes become unable to finish their execution because of <i>deadlock</i>. </p> <p> A process has to wait when no more resource instances in need are available until some other processes release ones on their termination. Deadlock is a situation in which two or more processes wait for termination of each other, and, regrettably, forever. This happens with the following scenario: One process A acquires the sole instance of resource X, and another process B acquires the sole instance of another resource Y; after that, A tries to acquire an instance of Y, and B tries to acquire an instance of X. As there are no instances of Y other than one acquired by B, A will never acquire Y before B finishes its execution, while B will never acquire X before A finishes. There may be more complicated deadlock situations involving three or more processes. </p> <p> Your task is, receiving the system's resource allocation time log (from the system's start to a certain time), to determine when the system fell into a <i>deadlock-unavoidable</i> state. Deadlock may usually be avoided by an appropriate allocation order, but deadlock-unavoidable states are those in which some resource allocation has already been made and no allocation order from then on can ever avoid deadlock. </p> <p> Let us consider an example corresponding to Sample Input 1 below. The system has two kinds of resource $R_1$ and $R_2$, and two processes $P_1$ and $P_2$. The system has three instances of $R_1$ and four instances of $R_2$. Process $P_1$ needs three instances of $R_1$ and two instances of $R_2$ to finish its execution, while process $P_2$ needs a single instance of $R_1$ and three instances of $R_2$. The resource allocation time log is given as follows. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ICPCAsia2015_DeadlockDetection"> </center> <br> <p> At time 4, $P_2$ acquired $R_1$ and the number of available instances of $R_1$ became less than $P_1$'s need of $R_1$. Therefore, it became necessary for $P_1$ to wait $P_2$ to terminate and release the instance. However, at time 5, $P_1$ acquired $R_2$ necessary for $P_2$ to finish its execution, and thus it became necessary also for $P_2$ to wait $P_1$; the deadlock became unavoidable at this time. </p> <p> Note that the deadlock was still avoidable at time 4 by finishing $P_2$ first (Sample Input 2). </p> <h3>Input</h3> <p> The input consists of a single test case formatted as follows.<br> <br> $p$ $r$ $t$<br> $l_1$ ... $l_r$<br> $n_{1,1}$ ... $n_{1,r}$<br> ...<br> $n_{p,1}$ ... $n_{p,r}$<br> $P_1$ $R_1$<br> ...<br> $P_t$ $R_t$<br> <br> $p$ is the number of processes, and is an integer between 2 and 300, inclusive. The processes are numbered 1 through $p$. $r$ is the number of resource kinds, and is an integer between 1 and 300, inclusive. The resource kinds are numbered 1 through $r$. $t$ is the length of the time log, and is an integer between 1 and 200,000, inclusive. $l_j$ $(1 \leq j \leq r)$ is the number of initially available instances of the resource kind $j$, and is an integer between 1 and 100, inclusive. $n_{i,j}$ $(1 \leq i \leq p, 1 \leq j \leq r)$ is the number of resource instances of the resource kind $j$ that the process $i$ needs, and is an integer between 0 and $l_j$ , inclusive. For every $i$, at least one of $n_{i,j}$ is non-zero. Each pair of $P_k$ and $R_k$ $(1 \leq k \leq t)$ is a resource allocation log at time $k$ meaning that process $P_k$ acquired an instance of resource $R_k$. </p> <p> You may assume that the time log is consistent: no process acquires unnecessary resource instances; no process acquires instances after its termination; and a process does not acquire any instance of a resource kind when no instance is available. </p> <h3>Output</h3> <p> Print the time when the system fell into a deadlock-unavoidable state. If the system could still avoid deadlock at time $t$, print <span>-1</span>. </p> <h3>Sample Input 1</h3> <pre>2 2 7 3 4 3 2 1 3 1 1 2 2 1 2 2 1 1 2 2 2 1 1</pre> <h3>Sample Output 1</h3> <pre>5</pre> <h3>Sample Input 2</h3> <pre>2 2 5 3 4 3 2 1 3 1 1 2 2 1 2 2 1 2 2</pre> <h3>Sample Output 2</h3> <pre>-1</pre>
p03395
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Aoki is playing with a sequence of numbers <var>a_{1}, a_{2}, ..., a_{N}</var>. Every second, he performs the following operation :</p> <ul> <li>Choose a positive integer <var>k</var>. For each element of the sequence <var>v</var>, Aoki may choose to replace <var>v</var> with its remainder when divided by <var>k</var>, or do nothing with <var>v</var>. The cost of this operation is <var>2^{k}</var> (regardless of how many elements he changes).</li> </ul> <p>Aoki wants to turn the sequence into <var>b_{1}, b_{2}, ..., b_{N}</var> (the order of the elements is important). Determine if it is possible for Aoki to perform this task and if yes, find the minimum cost required.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 50</var></li> <li><var>0 \leq a_{i}, b_{i} \leq 50</var></li> <li>All values in the input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_{1}</var> <var>a_{2}</var> <var>...</var> <var>a_{N}</var> <var>b_{1}</var> <var>b_{2}</var> <var>...</var> <var>b_{N}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum cost required to turn the original sequence into <var>b_{1}, b_{2}, ..., b_{N}</var>. If the task is impossible, output <var>-1</var> instead.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 19 10 14 0 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>160 </pre> <p>Here's a possible sequence of operations :</p> <ul> <li> <p>Choose <var>k = 7</var>. Replace <var>19</var> with <var>5</var>, <var>10</var> with <var>3</var> and do nothing to <var>14</var>. The sequence is now <var>5, 3, 14</var>.</p> </li> <li> <p>Choose <var>k = 5</var>. Replace <var>5</var> with <var>0</var>, do nothing to <var>3</var> and replace <var>14</var> with <var>4</var>. The sequence is now <var>0, 3, 4</var>.</p> </li> </ul> <p>The total cost is <var>2^{7} + 2^{5} = 160</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 19 15 14 0 0 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 </pre> <p>Aoki can just choose <var>k = 1</var> and turn everything into <var>0</var>. The cost is <var>2^{1} = 2</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>2 8 13 5 13 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>-1 </pre> <p>The task is impossible because we can never turn <var>8</var> into <var>5</var> using the given operation.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>4 2 0 1 8 2 0 1 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>0 </pre> <p>Aoki doesn't need to do anything here. The cost is <var>0</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>1 50 13 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>137438953472 </pre> <p>Beware of overflow issues.</p></section> </div> </span>
p03680
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi wants to gain muscle, and decides to work out at AtCoder Gym.</p> <p>The exercise machine at the gym has <var>N</var> buttons, and exactly one of the buttons is lighten up. These buttons are numbered <var>1</var> through <var>N</var>. When Button <var>i</var> is lighten up and you press it, the light is turned off, and then Button <var>a_i</var> will be lighten up. It is possible that <var>i=a_i</var>. When Button <var>i</var> is not lighten up, nothing will happen by pressing it.</p> <p>Initially, Button <var>1</var> is lighten up. Takahashi wants to quit pressing buttons when Button <var>2</var> is lighten up.</p> <p>Determine whether this is possible. If the answer is positive, find the minimum number of times he needs to press buttons.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N ≤ 10^5</var></li> <li><var>1 ≤ a_i ≤ N</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>a_2</var> : <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>-1</var> if it is impossible to lighten up Button <var>2</var>. Otherwise, print the minimum number of times we need to press buttons in order to lighten up Button <var>2</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>Press Button <var>1</var>, then Button <var>3</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 3 4 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>-1 </pre> <p>Pressing Button <var>1</var> lightens up Button <var>3</var>, and vice versa, so Button <var>2</var> will never be lighten up.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 3 3 4 2 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>3 </pre></section> </div> </span>
p02992
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Find the number of sequences of length <var>K</var> consisting of positive integers such that the product of any two adjacent elements is at most <var>N</var>, modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1\leq N\leq 10^9</var></li> <li><s>1</s> <var>2\leq K\leq 100</var> (fixed at 21:33 JST)</li> <li><var>N</var> and <var>K</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of sequences, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>5 </pre> <p><var>(1,1)</var>, <var>(1,2)</var>, <var>(1,3)</var>, <var>(2,1)</var>, and <var>(3,1)</var> satisfy the condition.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>147 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>314159265 35 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>457397712 </pre></section> </div> </span>
p01347
<H1><font color="#000">Problem I: </font>How to Create a Good Game</H1> <p> A video game company called ICPC (International Company for Playing and Competing) is now developing a new arcade game. The new game features a lot of branches. This makes the game enjoyable for everyone, since players can choose their routes in the game depending on their skills. Rookie players can choose an easy route to enjoy the game, and talented players can choose their favorite routes to get high scores. </p> <p> In the game, there are many checkpoints connected by paths. Each path consists of several stages, and completing stages on a path leads players to the next checkpoint. The game ends when players reach a particular checkpoint. At some checkpoints, players can choose which way to go, so routes diverge at that time. Sometimes different routes join together at a checkpoint. The paths between checkpoints are directed, and there is no loop (otherwise, players can play the game forever). In other words, the structure of the game can be viewed as a DAG (directed acyclic graph), when considering paths between checkpoints as directed edges. </p> <p> Recently, the development team completed the beta version of the game and received feedbacks from other teams. They were quite positive overall, but there are some comments that should be taken into consideration. Some testers pointed out that some routes were very short compared to the longest ones. Indeed, in the beta version, the number of stages in one play can vary drastically depending on the routes. Game designers complained many brilliant ideas of theirs were unused in the beta version because of the tight development schedule. They wanted to have more stages included in the final product. </p> <p> However, it’s not easy to add more stages. this is an arcade game – if the playing time was too long, it would bring down the income of the game and owners of arcades would complain. So, the longest route of the final product can’t be longer than that of the beta version. Moreover, the producer of the game didn’t want to change the structure of paths (i.e., how the checkpoints connect to each other), since it would require rewriting the scenario, recording voices, creating new cutscenes, etc. </p> <p> Considering all together, the producer decided to add as many new stages as possible, while keeping the maximum possible number of stages in one play and the structure of paths unchanged. How many new stages can be added to the game? </p> <H2>Input</H2> <p> <i>N M</i><br> <i>x</i><sub>1</sub> <i>y</i><sub>1</sub> <i>s</i><sub>1</sub><br> .<br> .<br> .<br> <i>x</i><sub><i>M</i></sub> <i>y</i><sub><i>M</i></sub> <i>s</i><sub><i>M</i></sub><br> </p> <p> The first line of the input contains two positive integers <i>N</i> and <i>M</i> (2 &le; <i>N</i> &le; 100, 1 &le; <i>M</i> &le; 1000). <i>N</i> indicates the number of checkpoints, including the opening and ending of the game. <i>M</i> indicates the number of paths between checkpoints. </p> <p> The following <i>M</i> lines describe the structure of paths in the beta version of the game. The <i>i</i>-th line contains three integers <i>x<sub>i</sub></i> , <i>y<sub>i</sub></i> and <i>s<sub>i</sub></i> (0 &le; <i>x<sub>i</sub></i> &lt; <i>y<sub>i</sub></i> &le; <i>N</i> - 1, 1 &le; <i>s<sub>i</sub></i> &le; 1000), which describe that there is a path from checkpoint <i>x<sub>i</sub></i> to <i>y<sub>i</sub></i> consists of si stages. As for indices of the checkpoints, note that 0 indicates the opening of the game and <i>N</i> - 1 indicates the ending of the game. You can assume that, for every checkpoint <i>i</i>, there exists a route from the opening to the ending which passes through checkpoint <i>i</i>. You can also assume that no two paths connect the same pair of checkpoints. </p> <H2>Output</H2> <p> Output a line containing the maximum number of new stages that can be added to the game under the following constraints: </p> <ul> <li> You can’t increase the maximum possible number of stages in one play (i.e., the length of the longest route to the ending).</li> <li> You can’t change the structure of paths (i.e., how the checkpoints connect to each other).</li> <li> You can’t delete any stage that already exists in the beta version.</li> </ul> <H2>Sample Input 1</H2> <pre> 3 3 0 1 5 1 2 3 0 2 2 </pre> <H2>Output for the Sample Input 1</H2> <pre> 6 </pre> <H2>Sample Input 2</H2> <pre> 2 1 0 1 10 </pre> <H2>Output for the Sample Input 2</H2> <pre> 0 </pre> <H2>Sample Input 3</H2> <pre> 4 6 0 1 5 0 2 5 0 3 5 1 2 5 1 3 5 2 3 5 </pre> <H2>Output for the Sample Input 3</H2> <pre> 20 </pre>
p01717
<h1>問題 F : Social Monsters</h1> <h2>問題文</h2> <p> 不思議な生き物と人間が互いに助け合って生きている世界. この世界では自分で捕まえたモンスター同士を戦わせる大会が盛んに行われており, 多くの少年少女達が世界一のトレーナーを目指している. </p> <p> バトルでは自分が捕まえたモンスターからパーティを作り,パーティ同士の対戦となる. モンスターのペアには相性があり,相性が非常に悪い場合と普通の場合がある. 相性が非常に悪い場合,そのペアのモンスターを同じパーティに入れることはできない. 相性が普通の場合には,<i>友情度</i>という数値で相性の良さが表現される. バトルでは,パーティ全体の友情度の和が勝負の鍵となる. </p> <p> いま<var>N</var>匹のモンスターから<var>K</var>匹のモンスターからなるパーティを作りたい. <var>M</var>個のモンスターのペア<var>(a_i, &thinsp; b_i)</var>に対して相性が分かっており,整数 <var>c_i</var> で表されている. <var>c_i = 0</var> のとき,<var>a_i</var> 番目のモンスターと <var>b_i</var> 番目のモンスターは相性が非常に悪いことを意味する. <var>c_i &thinsp;&ne;&thinsp; 0</var> のとき,<var>a_i</var> 番目のモンスターと <var>b_i</var> 番目のモンスターは相性が普通であり,その友情度は <var>c_i</var> であることを意味する. 与えられた<var>M</var>個のペア以外は,相性が普通であり,それらの友情度はすべて<var>0</var>である. </p> <p> パーティの友情度の和を最大にする選び方を考えてみよう. </o> <h2>入力形式</h2> <p> 入力は以下の形式で与えられる <pre> <var>N</var> <var>M</var> <var>K</var> <var>a_0</var> <var>b_0</var> <var>c_0</var> <var>...</var> <var>a_{M-1}</var> <var>b_{M-1}</var> <var>c_{M-1}</var> </pre> <h2>出力形式</h2> <p> <var>K</var>匹のモンスターからなるパーティを作ることができない場合は1行に "Impossible" を出力せよ. <br> パーティを作ることができるとき,友情度の和の最大値を出力せよ. </p> <h2>制約</h2> <ul> <li><var>1 &le; K &le; N &le; 2000</var></li> <li><var>0 &le; M &le; N </var></li> <li><var>1 &thinsp; &le; &thinsp; a_i &thinsp; &ne; &thinsp; b_i &thinsp; &le; N</var></li> <li><var>|c_i| &thinsp; &le; 10000</var></li> <li> 各モンスターは <var>a_0, a_1, …, a_{M-1}, b_0, b_1, …, b_{M-1}</var> の中に高々2回しか現れない. <li> <var>i &thinsp; &ne; &thinsp; j &ensp; &rArr; &ensp; \{a_i,&thinsp;b_i\} &thinsp; &ne; &thinsp; \{a_j, &thinsp; b_j\} </var></li> </ul> <h2>入出力例</h2> <h3>入力例 1</h3> <pre> 6 5 4 2 1 1 2 5 2 5 1 1 4 3 2 3 6 -1 </pre> <h3>出力例 1</h3> <pre> 4 </pre> <h3>入力例 2</h3> <pre> 3 1 3 1 2 -10 </pre> <h3>出力例 2</h3> <pre> -10 </pre> <h3>入力例 2</h3> <pre> 6 3 5 1 2 0 2 3 10 3 4 0 </pre> <h3>出力例 2</h3> <pre> Impossible </pre>
p02838
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have <var>N</var> integers. The <var>i</var>-th integer is <var>A_i</var>.</p> <p>Find <var>\sum_{i=1}^{N-1}\sum_{j=i+1}^{N} (A_i \mbox{ XOR } A_j)</var>, modulo <var>(10^9+7)</var>.</p> <details> <summary>What is <var>\mbox{ XOR }</var>?</summary> <p> The XOR of integers <var>A</var> and <var>B</var>, <var>A \mbox{ XOR } B</var>, is defined as follows: <ul> <li>When <var>A \mbox{ XOR } B</var> is written in base two, the digit in the <var>2^k</var>'s place (<var>k \geq 0</var>) is <var>1</var> if either <var>A</var> or <var>B</var>, but not both, has <var>1</var> in the <var>2^k</var>'s place, and <var>0</var> otherwise.</li> </ul> For example, <var>3 \mbox{ XOR } 5 = 6</var>. (In base two: <var>011 \mbox{ XOR } 101 = 110</var>.) </p> </details> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 3 \times 10^5</var></li> <li><var>0 \leq A_i &lt; 2^{60}</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the value <var>\sum_{i=1}^{N-1}\sum_{j=i+1}^{N} (A_i \mbox{ XOR } A_j)</var>, modulo <var>(10^9+7)</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>6 </pre> <p>We have <var>(1\mbox{ XOR } 2)+(1\mbox{ XOR } 3)+(2\mbox{ XOR } 3)=3+2+1=6</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 3 1 4 1 5 9 2 6 5 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>237 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>10 3 14 159 2653 58979 323846 2643383 27950288 419716939 9375105820 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>103715602 </pre> <p>Print the sum modulo <var>(10^9+7)</var>.</p></section> </div> </span>
p00805
<H1><font color="#000">Problem C:</font> Fishnet</H1> <p> A fisherman named Etadokah awoke in a very small island. He could see calm, beautiful and blue sea around the island. The previous night he had encountered a terrible storm and had reached this uninhabited island. Some wrecks of his ship were spread around him. He found a square wood-frame and a long thread among the wrecks. He had to survive in this island until someone came and saved him. </p> <p> In order to catch fish, he began to make a kind of fishnet by cutting the long thread into short threads and fixing them at pegs on the square wood-frame (Figure 1). He wanted to know the sizes of the meshes of the fishnet to see whether he could catch small fish as well as large ones. </p> <p> The wood-frame is perfectly square with four thin edges one meter long.. a bottom edge, a top edge, a left edge, and a right edge. There are <i>n</i> pegs on each edge, and thus there are 4<i>n</i> pegs in total. The positions ofpegs are represented by their (<i>x</i>, <i>y</i>)-coordinates. Those of an example case with <i>n</i> = 2 are depicted in Figures 2 and 3. The position of the <i>i</i>th peg on the bottom edge is represented by (<i>a<sub>i</sub></i>, 0) . That on the top edge, on the left edge and on the right edge are represented by (<i>b<sub>i</sub></i>, 1) , (0, <i>c<sub>i</sub></i>), and (1, <i>d<sub>i</sub></i>), respectively. The long thread is cut into 2<i>n</i> threads with appropriate lengths. The threads are strained between (<i>a<sub>i</sub></i>, 0) and (<i>b<sub>i</sub></i>, 1) , and between (0, <i>c<sub>i</sub></i>) and (1, <i>d<sub>i</sub></i>) (<i>i</i> = 1,..., <i>n</i>) . </p> <p> You should write a program that reports the size of the largest mesh among the (<i>n</i> + 1)<sup>2</sup> meshes of the fishnet made by fixing the threads at the pegs. You may assume that the thread he found is long enough to make the fishnet and that the wood-frame is thin enough for neglecting its thickness. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_fishnet1"> <p>Figure 1. A wood-frame with 8 pegs.</p> </center> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_fishnet2"> <p>Figure 2. Positions of pegs (indicated by small black circles)</p> </center> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_fishnet3"> <p>Figure 3. A fishnet and the largest mesh (shaded)</p> </center> <H2>Input</H2> <p> The input consists of multiple subproblems followed by a line containing a zero that indicates the end of input. Each subproblem is given in the following format. </p> <pre> <i>n</i> <i>a</i><sub>1</sub><i>a</i><sub>2</sub> ... <i>a<sub>n</sub></i> <i>b</i><sub>1</sub><i>b</i><sub>2</sub> ... <i>b<sub>n</sub></i> <i>c</i><sub>1</sub><i>c</i><sub>2</sub> ... <i>c<sub>n</sub></i> <i>d</i><sub>1</sub><i>d</i><sub>2</sub> ... <i>d<sub>n</sub></i> </pre> <p> An integer <i>n</i> followed by a newline is the number of pegs on each edge. <i>a</i><sub>1</sub>,..., <i>a<sub>n</sub></i>, <i>b</i><sub>1</sub>,..., <i>b<sub>n</sub></i>, <i>c</i><sub>1</sub>,..., <i>c<sub>n</sub></i>, <i>d</i><sub>1</sub>,..., <i>d<sub>n</sub></i> are decimal fractions, and they are separated by a space character except that <i>a<sub>n</sub></i>, <i>b<sub>n</sub></i>, <i>c<sub>n</sub></i> and <i>d<sub>n</sub></i> are followed by a new line. Each <i>a<sub>i</sub></i> (<i>i</i> = 1,..., <i>n</i>) indicates the <i>x</i>-coordinate of the <i>i</i>th peg on the bottom edge. Each <i>b<sub>i</sub></i> (<i>i</i> = 1,..., <i>n</i>) indicates the <i>x</i>-coordinate of the <i>i</i>th peg on the top edge. Each <i>c<sub>i</sub></i> (<i>i</i> = 1,..., <i>n</i>) indicates the <i>y</i>-coordinate of the <i>i</i>th peg on the left edge. Each <i>d<sub>i</sub></i> (<i>i</i> = 1,..., <i>n</i>) indicates the <i>y</i>-coordinate of the <i>i</i>th peg on the right edge. The decimal fractions are represented by 7 digits after the decimal point. In addition you may assume that 0 &lt; <i>n</i> &le; 30 , 0 &lt; <i>a</i><sub>1</sub> &lt; <i>a</i><sub>2</sub> &lt; ... &lt; <i>a<sub>n</sub></i> &lt; 1, 0 &lt; <i>b</i><sub>1</sub> &lt; <i>b</i><sub>2</sub> &lt; ... &lt; <i>b<sub>n</sub></i> &lt; 1 , 0 &lt; <i>c</i><sub>1</sub> &lt; <i>c</i><sub>2</sub> &lt; ... &lt; <i>c<sub>n</sub></i> &lt; 1 and 0 &lt; <i>d</i><sub>1</sub> &le; <i>d</i><sub>2</sub> &lt; ... &lt; <i>d<sub>n</sub></i> &lt; 1 . </p> <H2>Output</H2> <p> For each subproblem, the size of the largest mesh should be printed followed by a new line. Each value should be represented by 6 digits after the decimal point, and it may not have an error greater than 0.000001. </p> <H2>Sample Input</H2> <pre> 2 0.2000000 0.6000000 0.3000000 0.8000000 0.3000000 0.5000000 0.5000000 0.6000000 2 0.3333330 0.6666670 0.3333330 0.6666670 0.3333330 0.6666670 0.3333330 0.6666670 4 0.2000000 0.4000000 0.6000000 0.8000000 0.1000000 0.5000000 0.6000000 0.9000000 0.2000000 0.4000000 0.6000000 0.8000000 0.1000000 0.5000000 0.6000000 0.9000000 2 0.5138701 0.9476283 0.1717362 0.1757412 0.3086521 0.7022313 0.2264312 0.5345343 1 0.4000000 0.6000000 0.3000000 0.5000000 0 </pre> <H2>Output for the Sample Input</H2> <pre> 0.215657 0.111112 0.078923 0.279223 0.348958 </pre>
p02091
<h2>D: XORANDORBAN</h2> <h3>Problem Statement</h3> <p>You are given a positive integer <var>N</var>. Your task is to determine a set <var>S</var> of <var>2^N</var> integers satisfying the following conditions:</p> <ul> <li> All the integers in <var>S</var> are at least <var>0 </var> and less than <var>2^{N+1}</var>.</li> <li> All the integers in <var>S</var> are distinct.</li> <li> You are also given three integers <var>X</var>, <var>A</var>, and <var>O</var>, where <var>0 \leq X, A, O &lt; 2^{N+1}</var>. Then, any two integers (<var>a</var>, <var>b</var>) in <var>S</var> must satisfy <var>a</var> <var>{\it xor}</var> <var>b \neq X</var>, <var>a</var> <var>{\it and}</var> <var>b \neq A</var>, <var>a</var> <var>{\it or}</var> <var>b \neq O</var>, where <var>{\it xor}</var>, <var>{\it and}</var>, <var>{\it or}</var> are <i>bitwise xor</i>, <i>bitwise and</i>, <i>bitwise or</i>, respectively. Note that <var>a</var> and <var>b</var> are not necessarily different.</li> </ul> <h3>Input</h3> <pre><var>N</var> <var>X</var> <var>A</var> <var>O</var></pre> <h3>Constraints</h3> <ul> <li> <var>1 \leq N \leq 13</var></li> <li> <var>0 \leq X, A, O &lt; 2^{N+1}</var></li> <li> Inputs consist only of integers.</li> </ul> <h3>Output</h3> <p>If there is no set satisfying the conditions mentioned in the problem statement, output <code>No</code> in a line. Otherwise, output <code>Yes</code> in the first line, and then output <var>2^N</var> integers in such a set in the second line. If there are multiple sets satisfying the conditions, you can output any of them.</p> <h3>Sample Input 1</h3> <pre>2 6 1 5</pre> <h3>Output for Sample Input 1</h3> <pre> Yes 0 3 4 7 </pre> <h3>Sample Input 2</h3> <pre>3 0 5 1</pre> <h3>Output for Sample Input 2</h3> <pre>No</pre> <h3>Sample Input 3</h3> <pre>3 4 2 5</pre> <h3>Output for Sample Input 3</h3> <pre> Yes 1 0 6 15 8 9 14 7 </pre>
p00556
<h1>ぬいぐるみの整理 (Plush Toys)</h1> <h2>問題</h2> <p> ある JOI 関係者は,おもちゃ屋で働いている.今日は,店内にあるぬいぐるみコーナーの整理をすることになった. </p> <p> ぬいぐるみコーナーの棚には,N 個のぬいぐるみが左から右に一列に並べられている.棚は仕切りにより N 個の区画に区切られており,1 つの区画に 1 個のぬいぐるみを置く.このおもちゃ屋は合計 M 種類のぬいぐるみを売っており,それぞれ 1 から M までの番号が付けられている.棚に並べられた N 個のぬいぐるみは,それぞれこの M 種類のうちのいずれかである.また,それぞれの種類のぬいぐるみは,少なくとも 1 個は存在する. </p> <p> 見栄えを良くするため,同じ種類のぬいぐるみが全て連続して棚に置かれるように,ぬいぐるみを並べ替えたい.次のような方法で,ぬいぐるみを並べ替えることにした. </p> <ul> <li>N 個のぬいぐるみのうちいくつかを選び,棚から取り出す.取り出さなかったぬいぐるみの位置は動かさない.</li> <li>取り出したぬいぐるみを,好きな順に棚の空いている区画に戻していく.</li> </ul> <p> 並べ替えた後,同じ種類のぬいぐるみが全て連続して棚に置かれていなければならない. 並べ替えるために取り出すぬいぐるみの個数の最小値を求めるプログラムを作成せよ. </p> <h2>入力</h2> <p> 入力は 1 + N 行からなる. </p> <p> 1 行目には 2 個の整数 N, M (1 ≦ N ≦ 100000, 1 ≦ M ≦ 20) が空白を区切りとして書かれており,ぬいぐるみが N 個あり,種類が M 種類あることを表す. </p> <p> 続く N 行のそれぞれには,1 以上 M 以下の整数が書かれている. N 行のうちの i 行目 (1 ≦ i ≦ N) に書かれた整数は,棚の左から i 番目の区画に置かれたぬいぐるみの種類を表す.各種類について,少なくとも 1 個のぬいぐるみが存在していることが保証される. </p> <h2>出力</h2> <p> 並べ替えるために取り出すぬいぐるみの個数の最小値を 1 行で出力せよ. </p> <h2>入出力例</h2> <h3>入力例 1</h3> <pre> 7 2 1 2 2 2 1 2 1 </pre> <h3>出力例 1</h3> <pre> 2 </pre> <br/> <h3>入力例 2</h3> <pre> 12 4 1 3 2 4 2 1 2 3 1 1 3 4 </pre> <h3>出力例 2</h3> <pre> 7 </pre> <br/> <p> 入力例 1 においては,最初に置かれているぬいぐるみの種類は左から順に 1, 2, 2, 2, 1, 2, 1 である.並べ替えるために取り出すぬいぐるみの個数を最小にするには,左から 1 番目と 6 番目のぬいぐるみを取り出し,左から 1 番目に種類 2 のぬいぐるみを,左から 6 番目に種類 1 のぬいぐるみを配置すればよい.このとき,取り出すぬいぐるみの個数は 2 個である. </p> <br/> <div class="source"> <p class="source"> <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/"><img alt="クリエイティブ・コモンズ・ライセンス" style="border-width:0" src="https://i.creativecommons.org/l/by-sa/4.0/80x15.png"/></a> </p> <p class="source"> <a href="https://www.ioi-jp.org/joi/2016/2017-yo/index.html">情報オリンピック日本委員会作 『第 16 回日本情報オリンピック JOI 2016/2017 予選競技課題』</a> </p> </div>
p00106
<H1>Discounts of Buckwheat</H1> <p> Aizu is famous for its buckwheat. There are many people who make buckwheat noodles by themselves. </p> <p> One day, you went shopping to buy buckwheat flour. You can visit three shops, A, B and C. The amount in a bag and its unit price for each shop is determined by the follows table. Note that it is discounted when you buy buckwheat flour in several bags. </p> <center> <table class="withborder"> <tr><th></th><th> Shop A </th><th> Shop B </th><th> Shop C</th></tr> <tr><th> Amount in a bag </th><td> 200g</td><td> 300g</td><td> 500g</td></tr> <tr><th> Unit price for a bag (nominal cost)</th><td> 380 yen </td><td> 550 yen </td><td> 850 yen</td></tr> <tr><th> Discounted units </th><td> per 5 bags </td><td>per 4 bags </td><td>per 3 bags</td></tr> <tr><th> Discount rate</th><td> reduced by 20 %</td><td> reduced by 15 %</td><td> reduced by 12 %</td></tr> </table> </center> <br/> <p> For example, when you buy 12 bags of flour at shop A, the price is reduced by 20 % for 10 bags, but not for other 2 bags. So, the total amount shall be (380 &times; 10) &times; 0.8 + 380 &times; 2 = 3,800 yen. </p> <p> Write a program which reads the amount of flour, and prints the lowest cost to buy them. Note that you should buy the flour of exactly the same amount as the given input. </p> <H2>Input</H2> <p> The input consists of multiple datasets. For each dataset, an integer <var>a</var> (500 &le; <var>a</var> &le; 5000, <var>a</var> is divisible by 100) which represents the amount of flour is given in a line. </p> <p> The input ends with a line including a zero. Your program should not process for the terminal symbol. The number of datasets does not exceed 50. </p> <H2>Output</H2> <p> For each dataset, print an integer which represents the lowest cost. </p> <H2>Sample Input</H2> <pre> 500 2200 0 </pre> <H2>Output for the Sample Input</H2> <pre> 850 3390 </pre>
p00311
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML"> </script> <H1>魚釣り競争 </H1> <p> 浩と健次郎の兄弟は猪苗代湖に魚釣りをしに来ました。二人は以下のように点数を決め、釣り上げた魚の合計得点で勝負することにしました。 </p> <ul> <li>イワナは1匹 <var>a</var> 点</li> <li>ヤマメは1匹 <var>b</var> 点</li> <li>イワナ10匹ごとに <var>c</var> 点追加</li> <li>ヤマメ20匹ごとに <var>d</var>点追加</li> </ul> <p> 浩と健次郎が釣り上げた魚の数をもとに、どちらが勝ちか、あるいは引き分けか判定するプログラムを作成せよ。 </p> <h2>Input</h2> <p> 入力は以下の形式で与えられる。 </p> <pre> <var>h<sub>1</sub></var> <var>h<sub>2</sub></var> <var>k<sub>1</sub></var> <var>k<sub>2</sub></var> <var>a</var> <var>b</var> <var>c</var> <var>d</var> </pre> <p> 1行目には、浩が釣り上げたイワナの数 <var>h<sub>1</sub></var> (0 &le; <var>h<sub>1</sub></var> &le; 100) とヤマメの数 <var>h<sub>2</sub></var> (0 &le; <var>h<sub>2</sub></var> &le; 100) が与えられる。2行目には、健次郎が釣り上げたイワナの数 <var>k<sub>1</sub></var> (0 &le; <var>k<sub>1</sub></var> &le; 100) とヤマメの数 <var>k<sub>2</sub></var> (0 &le; <var>k<sub>2</sub></var> &le; 100) が与えられる。3行目には、イワナ1匹ごとの点数 <var>a</var>(1 &le; <var>a</var> &le; 100)、ヤマメ1匹ごとの点数 <var>b</var> (1 &le; <var>b</var> &le; 100)、イワナ10匹ごとの追加点数 <var>c</var> (0 &le; <var>c</var> &le; 100)、ヤマメ20匹ごとの追加点数 <var>d</var> (0 &le; <var>d</var> &le; 100) が与えられる。 </p> <h2>Output</h2> <p> 浩が勝ちなら <span>hiroshi</span>、健次郎が勝ちなら <span>kenjiro</span> 、引き分けなら <span>even</span> と1行に出力する。 </p> <h2>Sample Input 1</h2> <pre> 5 1 3 1 1 2 5 5 </pre> <h2>Sample Output 1</h2> <pre> hiroshi </pre> <br/> <h2>Sample Input 2</h2> <pre> 5 1 4 2 1 2 5 5 </pre> <h2>Sample Output 2</h2> <pre> kenjiro </pre> <br/> <h2>Sample Input 3</h2> <pre> 0 20 10 0 1 1 10 0 </pre> <h2>Sample Output 3</h2> <pre> even </pre>
p01853
<h1 id="b-平均値の嘘-lie-with-mean-value">B : 平均値の嘘 / Lie with Mean Value</h1> <h2 id="問題文">問題文</h2> <p>R 大学情報理工学部 <var>1</var> 回生の学生 <var>n</var> 人が, プログラミング演習 1 という講義の期末試験を受験する. 試験は <var>m</var> 点満点である.つまり,<var>1</var> 人の学生の取りうる点数は <var>0</var> 以上 <var>m</var> 以下の整数である.</p> <p>担当の先生は意地が悪いので,平均値と中央値の差が最大になるような点数分布が気になっている.そのような点数の取り方を <var>1</var> つ出力せよ.</p> <p>注 : 平均値とは点数の和を <var>n</var> で割ったものであり,中央値とは点数を昇順に並べたとき,<var>n</var> が奇数なら前から <var>(n+1)</var>/<var>2</var> 番目 (1 &minus; indexed) の点数, 偶数なら前から <var>n</var>/<var>2</var> 番目と <var>n</var>/<var>2+1</var> 番目の点数を足して <var>2</var> で割った点数である.</p> <h2 id="入力">入力</h2> <pre> <var>n \ m</var> </pre> <h2 id="制約">制約</h2> <ul> <li>整数である</li> <li><var>1 &le; n &le; 100</var></li> <li><var>1 &le; m &le; 100</var></li> </ul> <h2 id="出力">出力</h2> <p>答えの <var>n</var> 個の点数の列をスペース区切りで <var>1</var> 行に出力し, 最後に改行を出力せよ.複数通りある場合はどれを出力しても良い.</p> <h2 id="サンプル">サンプル</h2> <h3 id="サンプル入力1">サンプル入力1</h3> <pre> 3 100 </pre> <h3 id="サンプル出力1">サンプル出力1</h3> <pre> 0 0 100 </pre> <p><var>100 \ 100 \ 0</var> 等も正答である.</p> <br> <h3 id="サンプル入力2">サンプル入力2</h3> <pre> 1 100 </pre> <h3 id="サンプル出力2">サンプル出力2</h3> <pre> 50 </pre> <!-- - - - end nicebady - - - -->
p00741
<h1><font color="#000000">Problem B:</font> How Many Islands?</h1> <p> You are given a marine area map that is a mesh of squares, each representing either a land or sea area. Figure B-1 is an example of a map. </p> <p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_2009B2"><br> Figure B-1: A marine area map </center> </p> <p> You can walk from a square land area to another if they are horizontally, vertically, or diagonally adjacent to each other on the map. Two areas are on the same island if and only if you can walk from one to the other possibly through other land areas. The marine area on the map is surrounded by the sea and therefore you cannot go outside of the area on foot. </p> <p> You are requested to write a program that reads the map and counts the number of islands on it. For instance, the map in Figure B-1 includes three islands. </p> <h3>Input</h3> <p> The input consists of a series of datasets, each being in the following format. </p> <blockquote> <i>w h</i><br> <i>c</i><sub>1,1</sub> <i>c</i><sub>1,2</sub> ... <i>c</i><sub>1,<i>w</i></sub><br> <i>c</i><sub>2,1</sub> <i>c</i><sub>2,2</sub> ... <i>c</i><sub>2,<i>w</i></sub><br> ...<br> <i>c</i><sub><i>h</i>,1</sub> <i>c</i><sub><i>h</i>,2</sub> ... <i>c</i><sub><i>h</i>,<i>w</i></sub><br> </blockquote> <p> <i>w</i> and <i>h</i> are positive integers no more than 50 that represent the width and the height of the given map, respectively. In other words, the map consists of <i>w</i>&times;<i>h</i> squares of the same size. <i>w</i> and <i>h</i> are separated by a single space. </p> <p> <i>c</i><sub><i>i</i>, <i>j</i></sub> is either 0 or 1 and delimited by a single space. If <i>c</i><sub><i>i</i>, <i>j</i></sub> = 0, the square that is the <i>i</i>-th from the left and <i>j</i>-th from the top on the map represents a sea area. Otherwise, that is, if c<sub><i>i</i>, <i>j</i></sub> = 1, it represents a land area. </p> <p> The end of the input is indicated by a line containing two zeros separated by a single space. </p> <!-- end en only --> <h3>Output</h3> <!-- begin en only --> <p> For each dataset, output the number of the islands in a line. No extra characters should occur in the output. </p> <!-- end en only --> <h3>Sample Input</h3> <pre> 1 1 0 2 2 0 1 1 0 3 2 1 1 1 1 1 1 5 4 1 0 1 0 0 1 0 0 0 0 1 0 1 0 1 1 0 0 1 0 5 4 1 1 1 0 1 1 0 1 0 1 1 0 1 0 1 1 0 1 1 1 5 5 1 0 1 0 1 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 1 0 1 0 0 </pre> <h3>Output for the Sample Input</h3> <pre> 0 1 1 3 1 9 </pre>
p02286
<H1>Treap</H1> <p> A binary search tree can be unbalanced depending on features of data. For example, if we insert $n$ elements into a binary search tree in ascending order, the tree become a list, leading to long search times. One of strategies is to randomly shuffle the elements to be inserted. However, we should consider to maintain the balanced binary tree where different operations can be performed one by one depending on requirement. </p> <p> We can maintain the balanced binary search tree by assigning a priority randomly selected to each node and by ordering nodes based on the following properties. Here, we assume that all priorities are distinct and also that all keys are distinct. </p> <ul> <li><b>binary-search-tree property.</b> If $v$ is a <b>left child</b> of $u$, then $v.key < u.key$ and if $v$ is a <b>right child</b> of $u$, then $u.key < v.key$</li> <li><b>heap property.</b> If $v$ is a <b>child</b> of $u$, then $v.priority < u.priority$</li> </ul> <p> This combination of properties is why the tree is called Treap (tree + heap). </p> <p> An example of Treap is shown in the following figure. </p> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/ALDS1_8_D_treap"> <p> <b>Insert</b><br> To insert a new element into a Treap, first of all, insert a node which a randomly selected priority value is assigned in the same way for ordinal binary search tree. For example, the following figure shows the Treap after a node with key = 6 and priority = 90 is inserted. </p> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/ALDS1_8_D_treap_insert"> <p> It is clear that this Treap violates the heap property, so we need to modify the structure of the tree by <b>rotate</b> operations. The rotate operation is to change parent-child relation while maintaing the binary-search-tree property. </p> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/ALDS1_8_D_rotate"> <p> The rotate operations can be implemented as follows. </p> <table> <tr> <td width="340"> <pre> rightRotate(Node t) Node s = t.left t.left = s.right s.right = t return s // the new root of subtree </pre> </td> <td width="340"> <pre> leftRotate(Node t) Node s = t.right t.right = s.left s.left = t return s // the new root of subtree </pre> </td> </tr> </table> <p> The following figure shows processes of the rotate operations after the insert operation to maintain the properties. </p> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/ALDS1_8_D_insert_rotate"> <p> The insert operation with rotate operations can be implemented as follows. </p> <pre> insert(Node t, int key, int priority) // search the corresponding place recursively if t == NIL return Node(key, priority) // create a new node when you reach a leaf if key == t.key return t // ignore duplicated keys if key < t.key // move to the left child t.left = insert(t.left, key, priority) // update the pointer to the left child if t.priority < t.left.priority // rotate right if the left child has higher priority t = rightRotate(t) else // move to the right child t.right = insert(t.right, key, priority) // update the pointer to the right child if t.priority < t.right.priority // rotate left if the right child has higher priority t = leftRotate(t) return t </pre> <p> <b>Delete</b><br> To delete a node from the Treap, first of all, the target node should be moved until it becomes a leaf by rotate operations. Then, you can remove the node (the leaf). These processes can be implemented as follows. </p> <pre> delete(Node t, int key) // seach the target recursively if t == NIL return NIL if key < t.key // search the target recursively t.left = delete(t.left, key) else if key > t.key t.right = delete(t.right, key) else return _delete(t, key) return t _delete(Node t, int key) // if t is the target node if t.left == NIL && t.right == NIL // if t is a leaf return NIL else if t.left == NIL // if t has only the right child, then perform left rotate t = leftRotate(t) else if t.right == NIL // if t has only the left child, then perform right rotate t = rightRotate(t) else // if t has both the left and right child if t.left.priority > t.right.priority // pull up the child with higher priority t = rightRotate(t) else t = leftRotate(t) return delete(t, key) </pre> <p> Write a program which performs the following operations to a Treap $T$ based on the above described algorithm. </p> <ul> <li><span>insert </span>($k$, $p$): Insert a node containing $k$ as key and $p$ as priority to $T$.</li> <li><span>find </span>($k$): Report whether $T$ has a node containing $k$.</li> <li><span>delete </span>($k$): Delete a node containing $k$.</li> <li><span>print</span>(): Print the keys of the binary search tree by inorder tree walk and preorder tree walk respectively.</li> </ul> <H2>Input</H2> <p> In the first line, the number of operations $m$ is given. In the following $m$ lines, operations represented by <span>insert </span>$k \; p$, <span>find </span>$k$, <span>delete </span>$k$ or print are given. </p> <H2>Output</H2> <p> For each find($k$) operation, print "<span>yes</span>" if $T$ has a node containing $k$, "<apan>no</span>" if not. </p> <p> In addition, for each print operation, print a list of keys obtained by inorder tree walk and preorder tree walk in a line respectively. Put a space character <u>before each key</u>. </p> <H2>Constraints</H2> <ul> <li>The number of operations $ \leq 200,000$</li> <li>$0 \leq k, p \leq 2,000,000,000$</li> <li>The height of the binary tree does not exceed 50 if you employ the above algorithm</li> <li>The keys in the binary search tree are all different.</li> <li>The priorities in the binary search tree are all different.</li> <li>The size of output $\leq 10$ MB</li> </ul> <H2>Sample Input 1</H2> <pre> 16 insert 35 99 insert 3 80 insert 1 53 insert 14 25 insert 80 76 insert 42 3 insert 86 47 insert 21 12 insert 7 10 insert 6 90 print find 21 find 22 delete 35 delete 99 print </pre> <H2>Sample Output 1</H2> <pre> 1 3 6 7 14 21 35 42 80 86 35 6 3 1 14 7 21 80 42 86 yes no 1 3 6 7 14 21 42 80 86 6 3 1 80 14 7 21 42 86 </pre> <H2>Reference</H2> <ul> <li>Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.</li> <li>Randomized Search Trees, R. Seidel, C.R. Aragon.</li> </ul>
p01500
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script type='text/javascript' src='http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script> </script> <h2>Problem F: Rabbit Jumping </h2> <p> There are $K$ rabbits playing on rocks floating in a river. They got tired of playing on the rocks on which they are standing currently, and they decided to move to other rocks. This seemed an easy task initially, but there are so many constraints that they got totally confused. </p> <p> First of all, by one leap, they can only move to a rock within $R$ meters from the current rock. Also, they can never leap over rocks. That is, when they leap in some direction, they should land on the nearest rock in that direction. Furthermore, since they always want to show off that they are courageous, they will never leap to rocks downriver. Finally, since they never want to admit they have been defeated, they never land on a rock if the rock is already visited by other rabbits. </p> <p> In this situation, is it possible for them to move to their destination rocks? If possible, please minimize the sum of their moving distance. </p> <h3>Input</h3> <p> The first line contains two integers $N$ ($1 \leq N \leq 100$), $K$ ($1 \leq K \leq 3$) and a real number $R$ ($0 \leq R \leq 10^4$), which denote the number of rocks, the number of rabbits, and the maximum distance a rabbit can leap, respectively. The second line contains $K$ numbers $s_1, ..., s_K$ where $s_i$ denote the rock where the $i$-th rabbit is standing. Similarly, the third line contains $K$ numbers $t_1, ..., t_K$ where $t_i$ denote the destination rock for the $i$-th rabbit. $s_1, ..., s_K$ are distinct, and $t_1, ..., t_K$ are distinct. A destination rock of a rabbit is always different from the rock he/she is standing currently. </p> <p> Then the following $N$ lines describe the positions of the rocks. The $i$-th line in this block contains two integers $x_i$ and $y_i$ ($0 \leq x_i, y_i \leq 10,000$), which indicate the coordinates of the $i$-th rock. The river flows along Y-axis, toward the direction where Y-coordinate decreases. No pair of rocks has identical coordinates. </p> <p> You may assume that the answer do not change even if you increase $R$ by up to $10^{-5}$. </p> <h3>Output</h3> <p> If all the rabbits can move to their destination rocks, print the minimum total distance they need to leap. If not, print "-1" (without quotes). Your answer may contain at most $10^{-6}$ of absolute error. </p> <h3>Sample Input</h3> <pre>6 3 1.0 1 2 3 4 5 6 0 0 1 0 2 0 0 1 1 1 2 1</pre> <h3>Output for the Sample Input</h3> <pre>3</pre>
p01150
<H1><font color="#000">Problem B:</font> Eight Princes</H1> <p> Once upon a time in a kingdom far, far away, there lived eight princes. Sadly they were on very bad terms so they began to quarrel every time they met. </p> <p> One day, the princes needed to seat at the same round table as a party was held. Since they were always in bad mood, a quarrel would begin whenever: </p> <ul> <li>A prince took the seat next to another prince.</li> <li>A prince took the seat opposite to that of another prince (this happens only when the table has an even number of seats), since they would give malignant looks each other. </li> </ul> <p> Therefore the seat each prince would seat was needed to be carefully determined in order to avoid their quarrels. You are required to, given the number of the seats, count the number of ways to have all eight princes seat in peace. </p> <H2>Input</H2> <p> Each line in the input contains single integer value N , which represents the number of seats on the round table. A single zero terminates the input. </p> <H2>Output</H2> <p> For each input N , output the number of possible ways to have all princes take their seat without causing any quarrels. Mirrored or rotated placements must be counted as different. </p> <p> You may assume that the output value does not exceed 10<sup>14</sup>. </p> <H2>Sample Input</H2> <pre> 8 16 17 0 </pre> <H2>Output for the Sample Input</H2> <pre> 0 0 685440 </pre>
p03497
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi has <var>N</var> balls. Initially, an integer <var>A_i</var> is written on the <var>i</var>-th ball.</p> <p>He would like to rewrite the integer on some balls so that there are at most <var>K</var> different integers written on the <var>N</var> balls.</p> <p>Find the minimum number of balls that Takahashi needs to rewrite the integers on them.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq K \leq N \leq 200000</var></li> <li><var>1 \leq A_i \leq N</var></li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>A_2</var> ... <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of balls that Takahashi needs to rewrite the integers on them.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 2 1 1 2 2 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 </pre> <p>For example, if we rewrite the integer on the fifth ball to <var>2</var>, there are two different integers written on the balls: <var>1</var> and <var>2</var>. On the other hand, it is not possible to rewrite the integers on zero balls so that there are at most two different integers written on the balls, so we should print <var>1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 4 1 1 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Already in the beginning, there are two different integers written on the balls, so we do not need to rewrite anything.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>10 3 5 1 3 2 4 1 1 2 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>3 </pre></section> </div> </span>
p03182
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Consider a string of length <var>N</var> consisting of <code>0</code> and <code>1</code>. The score for the string is calculated as follows:</p> <ul> <li>For each <var>i</var> (<var>1 \leq i \leq M</var>), <var>a_i</var> is added to the score if the string contains <code>1</code> at least once between the <var>l_i</var>-th and <var>r_i</var>-th characters (inclusive).</li> </ul> <p>Find the maximum possible score of a string.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All values in input are integers.</li> <li><var>1 \leq N \leq 2 × 10^5</var></li> <li><var>1 \leq M \leq 2 × 10^5</var></li> <li><var>1 \leq l_i \leq r_i \leq N</var></li> <li><var>|a_i| \leq 10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>l_1</var> <var>r_1</var> <var>a_1</var> <var>l_2</var> <var>r_2</var> <var>a_2</var> <var>:</var> <var>l_M</var> <var>r_M</var> <var>a_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum possible score of a string.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 3 1 3 10 2 4 -10 3 5 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>20 </pre> <p>The score for <code>10001</code> is <var>a_1 + a_3 = 10 + 10 = 20</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 4 1 3 100 1 1 -10 2 2 -20 3 3 -30 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>90 </pre> <p>The score for <code>100</code> is <var>a_1 + a_2 = 100 + (-10) = 90</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1 1 1 1 -10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> <p>The score for <code>0</code> is <var>0</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>1 5 1 1 1000000000 1 1 1000000000 1 1 1000000000 1 1 1000000000 1 1 1000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>5000000000 </pre> <p>The answer may not fit into a 32-bit integer type.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>6 8 5 5 3 1 1 10 1 6 -8 3 6 5 3 4 9 5 5 -2 1 3 -6 4 6 -7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>10 </pre> <p>For example, the score for <code>101000</code> is <var>a_2 + a_3 + a_4 + a_5 + a_7 = 10 + (-8) + 5 + 9 + (-6) = 10</var>.</p></section> </div> </span>
p03478
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Find the sum of the integers between <var>1</var> and <var>N</var> (inclusive), whose sum of digits written in base <var>10</var> is between <var>A</var> and <var>B</var> (inclusive).</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 10^4</var></li> <li><var>1 \leq A \leq B \leq 36</var></li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the sum of the integers between <var>1</var> and <var>N</var> (inclusive), whose sum of digits written in base <var>10</var> is between <var>A</var> and <var>B</var> (inclusive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>20 2 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>84 </pre> <p>Among the integers not greater than <var>20</var>, the ones whose sums of digits are between <var>2</var> and <var>5</var>, are: <var>2,3,4,5,11,12,13,14</var> and <var>20</var>. We should print the sum of these, <var>84</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>13 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100 4 16 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>4554 </pre></section> </div> </span>
p01445
<H1><font color="#000">Problem I:</font> Mobile Network</H1> <p> The trafic on the Internet is increasing these days due to smartphones. The wireless carriers have to enhance their network infrastructure. </p> <p> The network of a wireless carrier consists of a number of base stations and lines. Each line connects two base stations bi-directionally. The bandwidth of a line increases every year and is given by a polynomial <i>f</i>(<i>x</i>) of the year <i>x</i>. </p> <p> Your task is, given the network structure, to write a program to calculate the maximal bandwidth between the 1-st and <i>N</i>-th base stations as a polynomial of <i>x</i>. </p> <H2>Input</H2> <p> The input consists of multiple datasets. Each dataset has the following format: </p> <p> <i>N M</i><br> <i>u</i><sub>1</sub> <i>v</i><sub>1</sub> <i>p</i><sub>1</sub><br> ...<br> <i>u</i><sub><i>M</i></sub> <i>v</i><sub><i>M</i></sub> <i>p</i><sub><i>M</i></sub><br> </p> <p> The first line of each dataset contains two integers <i>N</i> (2 &le; <i>N</i> &le; 50) and <i>M</i> (0 &le; <i>M</i> &le; 500), which indicates the number of base stations and lines respectively. The following <i>M</i> lines describe the network structure. The <i>i</i>-th of them corresponds to the <i>i</i>-th network line and contains two integers <i>u<sub>i</sub></i> and <i>v<sub>i</sub></i> and a polynomial <i>p<sub>i</sub></i>. <i>u<sub>i</sub></i> and <i>v<sub>i</sub></i> indicate the indices of base stations (1 &le; <i>u<sub>i</sub></i>, <i>v<sub>i</sub></i> &le; <i>N</i>); <i>p<sub>i</sub></i> indicates the network bandwidth. </p> <p> Each polynomial has the form of: </p> <p> <i>a<sub>L</sub>x<sup>L</sup></i> + <i>a<sub>L</sub></i>-1</sub><i>x</i><sup><i>L</i>-1</sup> + ... + <i>a</i><sub>2</sub><i>x</i><sup>2</sup> + <i>a</i><sub>1</sub><i>x</i> + <i>a</i><sub>0</sub> </p> <p> where <i>L</i> (0 &le; <i>L</i> &le; 50) is the degree and <i>a<sub>i</sub></i>'s (0 &le; <i>i</i> &le; <i>L</i>, 0 &le; <i>a<sub>i</sub></i> &le; 100) are the coefficients. In the input, </p> <ul> <li>each term <i>a<sub>i</sub>x<sup>i</sup></i> (for <i>i</i> &ge; 2) is represented as &lt;<i>a<sub>i</sub></i>&gt;<i>x</i>^&lt;<i>i</i>&gt; </li> <li>the linear term (<i>a</i><sub>1</sub><i>x</i>) is represented as &lt;<i>a</i><sub>1</sub>&gt;<i>x</i>; </li> <li> the constant (<i>a</i><sub>0</sub>) is represented just by digits; </li> <li> these terms are given in the strictly decreasing order of the degrees and connected by a plus sign ("<span>+</span>"); </li> <li> just like the standard notations, the &lt;<i>a<sub>i</sub></i>&gt; is omitted if <i>a<sub>i</sub></i> = 1 for non-constant terms; </li> <li> similarly, the entire term is omitted if <i>a<sub>i</sub></i> = 0 for any terms; and </li> <li>the polynomial representations contain no space or characters other than digits, "x", "^", and "+". </li> </ul> <p> For example, 2<i>x</i><sup>2</sup> + 3<i>x</i> + 5 is represented as <span>2x^2+3x+5</span>; 2<i>x</i><sup>3</sup> + <i>x</i> is represented as <span>2x^3+x</span>, not <span>2x^3+0x^2+1x+0</span> or the like. No polynomial is a constant zero, i.e. the one with all the coefficients being zero. </p> <p> The end of input is indicated by a line with two zeros. This line is not part of any dataset. </ul> <H2>Output</H2> <p> For each dataset, print the maximal bandwidth as a polynomial of <i>x</i>. The polynomial should be represented in the same way as the input format except that a constant zero is possible and should be represented by "0" (without quotes). </p> <H2>Sample Input</H2> <pre> 3 3 1 2 x+2 2 3 2x+1 3 1 x+1 2 0 3 2 1 2 x 2 3 2 4 3 1 2 x^3+2x^2+3x+4 2 3 x^2+2x+3 3 4 x+2 0 0 </pre> <H2>Output for the Sample Input</H2> <pre> 2x+3 0 2 x+2 </pre>
p03028
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We will host a rock-paper-scissors tournament with <var>N</var> people. The participants are called Person <var>1</var>, Person <var>2</var>, <var>\ldots</var>, Person <var>N</var>. For any two participants, the result of the match between them is determined in advance. This information is represented by positive integers <var>A_{i,j}</var> ( <var>1 \leq j &lt; i \leq N</var> ) as follows:</p> <ul> <li>If <var>A_{i,j} = 0</var>, Person <var>j</var> defeats Person <var>i</var>.</li> <li>If <var>A_{i,j} = 1</var>, Person <var>i</var> defeats Person <var>j</var>.</li> </ul> <p>The tournament proceeds as follows:</p> <ul> <li>We will arrange the <var>N</var> participants in a row, in the order Person <var>1</var>, Person <var>2</var>, <var>\ldots</var>, Person <var>N</var> from left to right.</li> <li>We will randomly choose two consecutive persons in the row. They will play a match against each other, and we will remove the loser from the row. We will repeat this process <var>N-1</var> times, and the last person remaining will be declared the champion.</li> </ul> <p>Find the number of persons with the possibility of becoming the champion.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 2000</var></li> <li><var>A_{i,j}</var> is <var>0</var> or <var>1</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_{2,1}</var> <var>A_{3,1}</var><var>A_{3,2}</var> <var>:</var> <var>A_{N,1}</var><var>\ldots</var><var>A_{N,N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of persons with the possibility of becoming the champion.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 0 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>Person <var>1</var> defeats Person <var>2</var>, Person <var>2</var> defeats Person <var>3</var> and Person <var>3</var> defeats Person <var>1</var>. If Person <var>1</var> and Person <var>2</var> play the first match, Person <var>3</var> will become the champion. If Person <var>2</var> and Person <var>3</var> play the first match, Person <var>1</var> will become the champion.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 0 11 111 1111 11001 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 </pre></section> </div> </span>
p01015
<script src="./IMAGE/varmath.js" charset="UTF-8"></script> <h1>Problem J: J's Final Problem</h1> <h2>Problem</h2> <p> 不思議なダンジョンとは構造の変化を伴うダンジョンである。不思議なダンジョンは階層が深いものから浅いものまで様々なものがあり、深い階層には凶悪なモンスターが生息していたり、財宝が眠っていたりする。ジェイは不思議なダンジョンを研究している研究者である。ある日、新しいダンジョンを掘っていたところ、とても大きく深いダンジョンに繋がってしまった。ジェイはこのダンジョンを「ジェイの最終問題」と名付けた。このダンジョンの調査が、優秀な冒険者であるあなたに依頼されることになった。冒険者の目的はできるだけ深い層のフロアに到達することである。このダンジョンの特徴を以下に述べる。 </p> <ul> <li>地上からダンジョンの地下1階の部屋への下りの階段は1つしか存在しない。</li> <li>各部屋は1つの上りの階段と2つの下りの階段が存在する。</li> <li>2つの下りの階段のうち片方を右の階段、もう片方を左の階段とする。</li> <li>冒険者が階段を降りた際に、ダンジョンの構造が変化することがある。</li> <li>階段を降りるとは、地下<var>i</var>階のある部屋から地下<var>i</var>+1階のある部屋へ降りることを示す。</li> <li>階段を上った場合はダンジョンに対して何も変化を及ぼさない。</li> <li>地下<var>n</var>階目には階段は存在しない。</li> </ul> <p> 最近、ジェイの最終問題に関する石碑が発見された。 どうやら、各階の階段を降りたときの変化がメモとして記されているようだ。 メモの形式は、 </p> <ul> <var>num1</var> <var>direction1</var> : <var>num2</var> <var>direction2</var> </ul> <p> という形である。 地下<var>num1</var>階のすべての部屋について、direction1の階段を降りている途中で、地下num2階のすべての部屋について、 direction2の階段を降りた先の部屋が消滅し、さらにそこから階段を降りることによって到達できる部屋や階段がすべて消滅する。部屋や階段の消滅に冒険者が巻き込まれた場合、冒険は失敗する。地下<var>i</var>階から地下<var>i</var>+1階へ向かう階段の途中で冒険者が失敗した場合は地下<var>i</var>階まで到達したものとして扱う。 石碑において言及されていない階段は、その階段を降りてもダンジョンの構造に変化を及ぼさないことを示す。 <br/> <br/> 冒険者はこの石碑をヒントにして探索を行うことにした。 </p> <p> ダンジョンの深さ<var>n</var>とメモの情報が<var>m</var>個与えられるので、最大地下何階まで到達できるかを答えなさい。 </p> <h2>Input</h2> <p> 入力は複数のデータセットからなる。</br> </p> <pre> <var>n</var> <var>m</var> <var>memo<sub>1</sub></var> <var>memo<sub>2</sub></var> … <var>memo<sub>m</sub></var> </pre> <p> 最初に<var>n</var>,<var>m</var>が与えられる。 それぞれ、ダンジョンの変化前の最下層の階数、石碑のメモの数を表す。 次に<var>m</var>行にわたりメモの情報 <var>num1</var> <var>direction1</var> : <var>num2</var> <var>direction2</var> が与えられる。 </p> <h2>Constraints</h2> <p>入力は以下の条件を満たす。</p> <ul> <li>入力に含まれる値はすべて整数</li> <li>1 &le; <var>n</var> &le; 10000</li> <li>0 &le; <var>m</var> &le; (<var>n</var>-1)&times;4</li> <li>1 &le; <var>num1</var>,<var>num2</var> &le; <var>n</var>-1</li> <li>direction1,direction2は"left"と"right"のいずれかである。</li> </ul> <h2>Output</h2> <p> 最大地下何階まで降りることができるか1行に出力して答えよ。 </p> <h2>Sample Input1</h2> <pre> 10 2 9 left : 9 left 9 right : 9 right </pre> <h2>Sample Output1</h2> <pre> 9 </pre> <h2>Sample Input2</h2> <pre> 4 5 1 left : 2 right 2 left : 3 right 2 left : 3 left 1 right : 3 left 2 right : 3 right </pre> <h2>Sample Output2</h2> <pre> 3 </pre>
p03881
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><style> #nck { width: 30px; height: auto; } </style> <p>There are two (6-sided) dice: a red die and a blue die. When a red die is rolled, it shows <var>i</var> with probability <var>p_i</var> percents, and when a blue die is rolled, it shows <var>j</var> with probability <var>q_j</var> percents.</p> <p>Petr and tourist are playing the following game. Both players know the probabilistic distributions of the two dice. First, Petr chooses a die in his will (without showing it to tourist), rolls it, and tells tourist the number it shows. Then, tourist guesses the color of the chosen die. If he guesses the color correctly, tourist wins. Otherwise Petr wins.</p> <p>If both players play optimally, what is the probability that tourist wins the game?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>0 ≤ p_i, q_i ≤ 100</var></li> <li><var>p_1 + ... + p_6 = q_1 + ... + q_6 = 100</var></li> <li>All values in the input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>p_1</var> <var>p_2</var> <var>p_3</var> <var>p_4</var> <var>p_5</var> <var>p_6</var> <var>q_1</var> <var>q_2</var> <var>q_3</var> <var>q_4</var> <var>q_5</var> <var>q_6</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the probability that tourist wins. The absolute error or the relative error must be at most <var>10^{-9}</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>25 25 25 25 0 0 0 0 0 0 50 50 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1.000000000000 </pre> <p>tourist can always win the game: If the number is at most <var>4</var>, the color is definitely red. Otherwise the color is definitely blue.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 20 20 10 20 20 20 20 20 10 10 20 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0.550000000000 </pre></section> </div> </span>
p02793
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given are <var>N</var> positive integers <var>A_1,...,A_N</var>.</p> <p>Consider positive integers <var>B_1, ..., B_N</var> that satisfy the following condition.</p> <p>Condition: For any <var>i, j</var> such that <var>1 \leq i &lt; j \leq N</var>, <var>A_i B_i = A_j B_j</var> holds.</p> <p>Find the minimum possible value of <var>B_1 + ... + B_N</var> for such <var>B_1,...,B_N</var>.</p> <p>Since the answer can be enormous, print the sum modulo (<var>10^9 +7</var>).</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 10^4</var></li> <li><var>1 \leq A_i \leq 10^6</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>...</var> <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum possible value of <var>B_1 + ... + B_N</var> for <var>B_1,...,B_N</var> that satisfy the condition, modulo (<var>10^9 +7</var>).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>13 </pre> <p>Let <var>B_1=6</var>, <var>B_2=4</var>, and <var>B_3=3</var>, and the condition will be satisfied.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 12 12 12 12 12 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>5 </pre> <p>We can let all <var>B_i</var> be <var>1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 1000000 999999 999998 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>996989508 </pre> <p>Print the sum modulo <var>(10^9+7)</var>.</p></section> </div> </span>
p00254
<H1>すべての数は6174に通ず</H1> <p> 0 から 9 の数字からなる四桁の数 N に対して以下の操作を行う。 </p> <ol> <li> N の桁それぞれの数値を大きい順に並べた結果得た数を L とする</li> <li> N の桁それぞれの数値を小さい順に並べた結果得た数を S とする</li> <li> 差 L-S を新しい N とする(1回分の操作終了)</li> <li> 新しい N に対して 1. から繰り返す</li> </ol> <p> このとき、全桁が同じ数字(0000, 1111など)である場合を除き、あらゆる四桁の数はいつかは 6174になることが知られている。例えば N = 2012の場合<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1回目 (N = 2012): L = 2210, S = 0122, L-S = 2088<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2回目 (N = 2088): L = 8820, S = 0288, L-S = 8532<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3回目 (N = 8532): L = 8532, S = 2358, L-S = 6174<br/> となり、3 回の操作で 6174 に到達する。 </p> <p> 0 から 9 の数字からなる四桁の数が与えられたとき、何回の操作で 6174 に到達するか計算するプログラムを作成して下さい。 </p> <h2>入力</h2> <p> 入力は複数のデータセットからなる。入力の終わりは 0000 が1つの行で示される。各データセットは以下の形式で与えられる。 </p> <pre> N </pre> <p> データセットは1行であり、N (1 &le; N &le; 9999) は四桁の数を示す。N &lt; 1000 の場合は上の桁は 0 で埋められている。 </p> <p> データセットの数は 10000 を超えない。 </p> <h2>出力</h2> <p> 各データセットごとに何回の操作で 6174 に到達したかを1行に出力する。ただし全桁が同じ数字である数が入力として与えられた場合は NA と出力する。 </p> <h2>入力例</h2> <pre> 6174 2012 3333 0000 </pre> <h2>出力例</h2> <pre> 0 3 NA </pre>
p02269
<H1>Search III</H1> <p> Your task is to write a program of a simple <i>dictionary</i> which implements the following instructions: </p> <ul> <li><b>insert <i>str</i></b>: insert a string <i>str</i> in to the dictionary</li> <li><b>find <i>str</i></b>: if the distionary contains <i>str</i>, then print '<span>yes</span>', otherwise print '<span>no</span>'</li> </ul> </ul> <H2>Input</H2> <p> In the first line <i>n</i>, the number of instructions is given. In the following <i>n</i> lines, <i>n</i> instructions are given in the above mentioned format. </p> <H2>Output</H2> <p> Print <span>yes</span> or <span>no</span> for each find instruction in a line. </p> <H2>Constraints</H2> <ul> <li>A string consists of '<span>A</span>', '<span>C</span>', '<span>G</span>', or '<span>T</span>'</li> <li>1 &le; length of a string &le; 12</li> <li> <i>n</i> &le; 1000000 </li> </ul> <H2>Sample Input 1</H2> <pre> 5 insert A insert T insert C find G find A </pre> <H2>Sample Output 1</H2> <pre> no yes </pre> <H2>Sample Input 2</H2> <pre> 13 insert AAA insert AAC insert AGA insert AGG insert TTT find AAA find CCC find CCC insert CCC find CCC insert T find TTT find T </pre> <H2>Sample Output 2</H2> <pre> yes no no yes yes yes </pre> <H2>Notes</H2> <a href="template/ALDS1_4_C_template.c" target="_blank">Template in C</a>
p01916
<link rel="stylesheet" href="css/description.css" type="text/css" /> <script language="JavaScript" type="text/javascript" src="js/varmath2017.js" charset="UTF-8"></script> <h2>A: アルファベットブロック</h2> <p>名川 若菜 (なかわ わかな) ちゃんは回文が大好きだ。なぜなら、自分の名前もまた回文であるからである。</p> <p>わかなちゃんはアルファベットブロックがいくつか入っているセットを手に入れた。アルファベットブロックというのは、<var>1</var> つのブロックにつき <var>1</var> 文字の英小文字アルファベットが書いてあるブロックのことであり、ブロックの順序を入れ替えて組み合わせることで好きな文字列を作ることができる。わかなちゃんはこのセットで回文を作れないか考えているところだ。</p> <p>アルファベットブロックのセットには、以下の <var>3</var> 種類の操作が可能であり、操作のコストはそれぞれ <var>1</var> である。</p> <ol> <li>アルファベットブロックを <var>1</var> つ追加する (記載されている文字は何であっても良い)</li> <li>いま持っているアルファベットブロック <var>1</var> つを別のブロック <var>1</var> つ (記載されている文字は何であっても良い) に変える</li> <li>いま持っているアルファベットブロックを <var>1</var> つ削除する</li> </ol> <p>アルファベットブロックのセットに何度か操作を行うことによって、並び替えて回文にできるようなセットにしたい。そのような操作のコストの最小値はいくつになるだろうか? わかなちゃんと一緒に考えてみよう。</p> <h3>Input</h3> <p>入力は、最初に持っているアルファベットブロックセットの情報を指す文字列 <var>S</var> の <var>1</var> 行からなる。</p> <p><var>S</var> は英小文字のみで構成され、<var>1 \leq |S| \leq 10^3</var> を満たす。</p> <h3>Output</h3> <p>回文を作成するためのコストの最小値を出力せよ。末尾の改行を忘れないこと。</p> <h3>Sample Input 1</h3> <pre>hcpc</pre> <h3>Sample Output 1</h3> <pre>1</pre> <p>この場合コスト <var>1</var> で回文を作ることができるが、複数の方法が考えられる。例えば、’h’ のブロックを追加すると 'hcpch' が作れるため、コスト <var>1</var> で回文が作れる。また、'h’のブロックを削除すると 'cpc' が作れるため、この方法でもコスト <var>1</var> で回文が作れる。</p> <h3>Sample Input 2</h3> <pre>ritscamp</pre> <h3>Sample Output 2</h3> <pre>4</pre> <h3>Sample Input 3</h3> <pre>nakawawakana</pre> <h3>Sample Output 3</h3> <pre>0</pre> <p>最初から回文を作ることができる場合、コストは <var>0</var> である。</p>
p00604
<H1><font color="#000000">Problem J:</font> Cheating on ICPC</H1> <p> Peter loves any kinds of cheating. A week before ICPC, he broke into Doctor's PC and sneaked a look at all the problems that would be given in ICPC. He solved the problems, printed programs out, and brought into ICPC. Since electronic preparation is strictly prohibited, he had to type these programs again during the contest. </p> <p> Although he believes that he can solve every problems thanks to carefully debugged programs, he still has to find an optimal strategy to make certain of his victory. </p> <p> Teams are ranked by following rules. </p> <ol> <li> Team that solved more problems is ranked higher.</li> <li> In case of tie (solved same number of problems), team that received less Penalty is ranked higher.</li> </ol> <p> Here, Penalty is calculated by these rules. </p> <ol> <li> When the team solves a problem, time that the team spent to solve it (i.e. (time of submission) - (time of beginning of the contest)) are added to penalty.</li> <li> For each submittion that doesn't solve a problem, 20 minutes of Penalty are added. However, if the problem wasn't solved eventually, Penalty for it is not added.</li> </ol> <p> You must find that order of solving will affect result of contest. For example, there are three problem named A, B, and C, which takes 10 minutes, 20 minutes, and 30 minutes to solve, respectively. If you solve A, B, and C in this order, Penalty will be 10 + 30 + 60 = 100 minutes. However, If you do in reverse order, 30 + 50 + 60 = 140 minutes of Penalty will be given. </p> <p> Peter can easily estimate time to need to solve each problem (actually it depends only on length of his program.) You, Peter's teammate, are asked to calculate minimal possible Penalty when he solve all the problems. </p> <H2>Input</H2> <p> Input file consists of multiple datasets. The first line of a dataset is non-negative integer N (0 &le; N &le; 100) which stands for number of problem. Next N Integers P[1], P[2], ..., P[N] (0 &le; P[i] &le; 10800) represents time to solve problems. </p> <p> Input ends with EOF. The number of datasets is less than or equal to 100.<!--when N = 0. You should output nothing in this case.--> </p> <H2>Output</H2> <p> Output minimal possible Penalty, one line for one dataset. </p> <H2>Sample Input</H2> <pre> 3 10 20 30 7 56 26 62 43 25 80 7 </pre> <H2>Output for the Sample Input</H2> <pre> 100 873 </pre>
p02639
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have five variables <var>x_1, x_2, x_3, x_4,</var> and <var>x_5</var>.</p> <p>The variable <var>x_i</var> was initially assigned a value of <var>i</var>.</p> <p>Snuke chose one of these variables and assigned it <var>0</var>.</p> <p>You are given the values of the five variables after this assignment.</p> <p>Find out which variable Snuke assigned <var>0</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>The values of <var>x_1, x_2, x_3, x_4,</var> and <var>x_5</var> given as input are a possible outcome of the assignment by Snuke.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>x_1</var> <var>x_2</var> <var>x_3</var> <var>x_4</var> <var>x_5</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If the variable Snuke assigned <var>0</var> was <var>x_i</var>, print the integer <var>i</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>0 2 3 4 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 </pre> <p>In this case, Snuke assigned <var>0</var> to <var>x_1</var>, so we should print <var>1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 2 0 4 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 </pre></section> </div> </span>
p03301
<span class="lang-en"> <p>Score : <var>2000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> boxes arranged in a row from left to right. The <var>i</var>-th box from the left contains <var>a_i</var> manju (buns stuffed with bean paste). <span style="color:#C06000">Sugim</span> and <span style="color:red">Sigma</span> play a game using these boxes. They alternately perform the following operation. <span style="color:#C06000">Sugim</span> goes first, and the game ends when a total of <var>N</var> operations are performed.</p> <ul> <li>Choose a box that still does not contain a piece and is adjacent to the box chosen in the other player's <strong>last</strong> operation, then put a piece in that box. If there are multiple such boxes, any of them can be chosen.</li> <li>If there is no box that satisfies the condition above, or this is <span style="color:#C06000">Sugim</span>'s first operation, choose any one box that still does not contain a piece, then put a piece in that box.</li> </ul> <p>At the end of the game, each player can have the manju in the boxes in which he put his pieces. They love manju, and each of them is wise enough to perform the optimal moves in order to have the maximum number of manju at the end of the game.</p> <p>Find the number of manju that each player will have at the end of the game.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 300</var> <var>000</var></li> <li><var>1 \leq a_i \leq 1000</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the numbers of <span style="color:#C06000">Sugim</span>'s manju and <span style="color:red">Sigma</span>'s manju at the end of the game, in this order, with a space in between.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 20 100 10 1 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>120 21 </pre> <p>If the two performs the optimal moves, the game proceeds as follows:</p> <ul> <li>First, <span style="color:#C06000">Sugim</span> has to put his piece in the second box from the left.</li> <li>Then, <span style="color:red">Sigma</span> has to put his piece in the leftmost box.</li> <li>Then, <span style="color:#C06000">Sugim</span> puts his piece in the third or fifth box.</li> <li>Then, <span style="color:red">Sigma</span> puts his piece in the fourth box.</li> <li>Finally, <span style="color:#C06000">Sugim</span> puts his piece in the remaining box.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 4 5 1 1 4 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>11 9 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 1 10 100 10 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>102 20 </pre> <!-- {sample example_3} The answer may not be representable as a <var>32</var>-bit integer. --></section> </div> </span>
p01296
<H1><font color="#000">Problem D:</font> Futon</H1> <p> The sales department of Japanese Ancient Giant Corp. is visiting a hot spring resort for their recreational trip. For deepening their friendships, they are staying in one large room of a Japanese-style hotel called a ryokan. </p> <p> In the ryokan, people sleep in Japanese-style beds called futons. They all have put their futons on the floor just as they like. Now they are ready for sleeping but they have one concern: they don’t like to go into their futons with their legs toward heads — this is regarded as a bad custom in Japanese tradition. However, it is not obvious whether they can follow a good custom. You are requested to write a program answering their question, as a talented programmer. </p> <p> Here let's model the situation. The room is considered to be a grid on an <i>xy</i>-plane. As usual, <i>x</i>-axis points toward right and <i>y</i>-axis points toward up. Each futon occupies two adjacent cells. People put their pillows on either of the two cells. Their heads come to the pillows; their foots come to the other cells. If the cell of some person's foot becomes adjacent to the cell of another person's head, regardless their directions, then it is considered as a bad case. Otherwise people are all right. </p> <H2>Input</H2> <p> The input is a sequence of datasets. Each dataset is given in the following format: </p> <p> <i>n</i><br> <i>x</i><sub>1</sub> <i>y</i><sub>1</sub> <i>dir</i><sub>1</sub><br> ...<br> <i>x<sub>n</sub></i> <i>y<sub>n</sub></i> <i>dir<sub>n</sub></i><br> </p> <p> <i>n</i> is the number of futons (1 &le; <i>n</i> &le; 20,000); (<i>x<sub>i</sub></i>, <i>y<sub>i</sub></i>) denotes the coordinates of the left-bottom corner of the <i>i</i>-th futon; <i>dir<sub>i</sub></i> is either '<span>x</span>' or '<span>y</span>' and denotes the direction of the <i>i</i>-th futon, where '<span>x</span>' means the futon is put horizontally and '<span>y</span>' means vertically. All coordinate values are non-negative integers not greater than 10<sup>9</sup> . </p> <p> It is guaranteed that no two futons in the input overlap each other. </p> <p> The input is terminated by a line with a single zero. This is not part of any dataset and thus should not be processed. </p> <H2>Output</H2> <p> For each dataset, print "<span>Yes</span>" in a line if it is possible to avoid a bad case, or "<span>No</span>" otherwise. </p> <H2>Sample Input</H2> <pre> 4 0 0 x 2 0 x 0 1 x 2 1 x 4 1 0 x 0 1 x 2 1 x 1 2 x 4 0 0 x 2 0 y 0 1 y 1 2 x 0 </pre> <H2>Output for the Sample Input</H2> <pre> Yes No Yes </pre>
p03751
<span class="lang-en lang-child hidden-lang"> <div class="part"> Max Score: <var>250</var> Points <br/> <section> <h3>Problem Statement</h3> Mr.X, who the handle name is <var>T</var>, looked at the list which written <var>N</var> handle names, <var>S_1, S_2, ..., S_N</var>. <br/> But he couldn't see some parts of the list. Invisible part is denoted <code>?</code>. <br/> <br/> Please calculate all possible index of the handle name of Mr.X when you sort <var>N+1</var> handle names (<var>S_1, S_2, ..., S_N</var> and <var>T</var>) in lexicographical order. <br/> Note: If there are pair of people with same handle name, either one may come first. <br/> </section> </div> <div class="io-style"> <div class="part"> <section> <h3>Input</h3> The input is given from standard input in the following format.<br/> <pre> <var>N</var> <var>S_1</var> <var>S_2</var> : <var>S_N</var> <var>T</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3> <ul> <li>Calculate the possible index and print in sorted order. The output should be separated with a space. Don't print a space after last number.</li> <li>Put a line break in the end.</li> </ul> </section> <section> <h3>Constraints</h3> <ul> <li><var>1 ≤ N ≤ 10000</var></li> <li><var>1 ≤ |S_i|, |T| ≤ 20</var> (<var>|A|</var> is the length of <var>A</var>.)</li> <li><var>S_i</var> consists from lower-case alphabet and <code>?</code>.</li> <li><var>T</var> consists from lower-case alphabet.</li> </ul> </section> <section> <h3>Scoring</h3> <p>Subtask 1 [ <var>130</var> points ]</p> <ul> <li>There are no <code>?</code>'s.</li> </ul> <p>Subtask 2 [ <var>120</var> points ]</p> <ul> <li>There are no additional constraints.</li> </ul> </section> </div> </div> <div class="part"> <section> <h3>Sample Input 1</h3> <pre> 2 tourist petr e </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3> <pre> 1 </pre> </section> </div> <div class="part"> <section> There are no invisible part, so there are only one possibility. The sorted handle names are <code>e</code>, <code>petr</code>, <code>tourist</code>, so <code>e</code> comes first.<br/> </section> </div> <div class="part"> <h3>Sample Input 2</h3> <pre> 2 ?o?r?s? ?et? e </pre> </div></span>
p02843
<span class="lang-en"> <p>Score: <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>AtCoder Mart sells <var>1000000</var> of each of the six items below:</p> <ul> <li>Riceballs, priced at <var>100</var> yen (the currency of Japan) each</li> <li>Sandwiches, priced at <var>101</var> yen each</li> <li>Cookies, priced at <var>102</var> yen each</li> <li>Cakes, priced at <var>103</var> yen each</li> <li>Candies, priced at <var>104</var> yen each</li> <li>Computers, priced at <var>105</var> yen each</li> </ul> <p>Takahashi wants to buy some of them that cost exactly <var>X</var> yen in total. Determine whether this is possible.<br/> (Ignore consumption tax.)</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3> <ul> <li><var>1 \leq X \leq 100000</var></li> <li><var>X</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3> <p>Input is given from Standard Input in the following format:</p> <pre><var>X</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3> <p>If it is possible to buy some set of items that cost exactly <var>X</var> yen in total, print <code>1</code>; otherwise, print <code>0</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>615 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 </pre> <p>For example, we can buy one of each kind of item, which will cost <var>100+101+102+103+104+105=615</var> yen in total.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>217 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>No set of items costs <var>217</var> yen in total.</p></section> </div> </span>
p00487
<H1> 微生物実験(Bug Party)</H1> <p> あなたはJust Odd Inventions 社を知っているだろうか? この会社の業務は「ただ奇妙な発明(just odd inventions)」をすることである.ここでは略してJOI 社と呼ぶ. </p> <p> JOI 社では多くの微生物を1 つのシャーレに生きたまま閉じ込める研究をしている.調査対象の微生物は<i>N</i> 匹存在し,1, 2, ... , <i>N</i> という番号がついている.各微生物はシャーレに閉じ込められると,foo (fatally odd object) と呼ばれる有害物質を一瞬のうちにシャーレ内に放出する.各微生物が放出するfoo の量は知られている.シャーレに閉じ込められた全微生物が放出したfoo はシャーレ内の各微生物が均等に摂取する.各微生物のfoo 許容量は知られており,この量を超えてfoo を摂取すると微生物は死んでしまう. </p> <p> 微生物<i>i</i> のfoo 放出量は<i>a<sub>i</sub></i> ミリグラム,foo 許容量は<i>b<sub>i</sub></i> ミリグラムである.すなわち,シャーレに微生物<i>i</i><sub>1</sub>, <i>i</i><sub>2</sub>, ... , <i>i<sub>k</sub></i> を閉じ込めたとき,シャーレ内の各微生物はそれぞれ(<i>a</i><sub><i>i</i><sub>1</sub></sub> + <i>a</i><sub><i>i</i><sub>2</sub></sub> + ... + <i>a<sub>i<sub>k</sub></sub></i>)/<i>k</i> ミリグラムのfoo を摂取し,シャーレ内の微生物<i>i</i> は,この摂取量がbi より大きいと死んでしまう. </p> <p> JOI 社からの委託により,あなたは出来るだけ多くの微生物をシャーレに生きたまま閉じ込めなければならない.ただし,微生物の死骸はシャーレ内の環境に悪影響を及ぼすため,シャーレ内のどの微生物もfoo の摂取で死んではならない. </p> <p> なお,JOI 社が「ただ奇妙な発明」をすることでどうやって利益を得ているかは,今もって謎であり,JOI社内でも社長以外の誰も知らない. </p> <h2>課題</h2> <p> 調査対象の微生物数と,各微生物のfoo 放出量とfoo 許容量が与えられたとき,1 つのシャーレに閉じ込めることができる微生物数の最大値を求めるプログラムを作成せよ. </p> <h2>制限</h2> <p> 1 &le; <i>N</i> &le; 300000 = 3 &times; 10<sup>5</sup> &nbsp;&nbsp;&nbsp;調査対象の微生物の数<br> 1 &le; <i>a<sub>i</sub></i> &le; 100000 = 10<sup>5</sup> &nbsp;&nbsp;&nbsp;微生物<i>i</i> のfoo 放出量(ミリグラム)<br> 1 &le; <i>b<sub>i</sub></i> &le; 100000 = 10<sup>5</sup> &nbsp;&nbsp;&nbsp;微生物<i>i</i> のfoo 許容量(ミリグラム) </p> <h2>入力</h2> <p> 標準入力から以下の入力を読み込め. </p> <ul> <li> 1 行目には整数<i>N</i> が書かれており,調査対象の微生物が<i>N</i> 匹存在することを表す.</li> <li> 続くN 行には各微生物の情報が書かれている.<i>i</i> + 1 行目(1 &le; <i>i</i> &le; <i>N</i>) には,正整数 <i>a<sub>i</sub></i>, <i>b<sub>i</sub></i> が空白を区切りとして書かれており,微生物i のfoo 放出量が<i>a<sub>i</sub></i> ミリグラムでfoo 許容量が<i>b<sub>i</sub></i> ミリグラムであることを表す. </ul> <h2>出力</h2> <p> 標準出力に,1 つのシャーレに閉じ込めることができる微生物数の最大値を1 行で出力せよ. </p> <h2>注意</h2> <p> この問題では,扱う整数の範囲が32 ビットに収まらない可能性があることに注意せよ. </p> <h2>採点基準</h2> <p> 採点用データのうち,配点の30% については,<i>N</i> &le; 1000 を満たす. </p> <h2>入出力の例</h2> <h3>入力例</h3> <pre> 6 12 8 5 9 2 4 10 12 6 7 13 9 </pre> <h3>出力例</h3> <pre> 3 </pre> <p> この例では,微生物2, 4, 5 をシャーレに入れれば,放出されるfoo の合計量は5 + 10 + 6 = 21 ミリグラムとなり,それぞれの微生物が摂取するfoo の量は21/3 = 7 ミリグラムとなる.微生物2, 4, 5 のfoo 許容量はそれぞれ9, 12, 7 ミリグラムなので,シャーレ内のどの微生物も死なない.また,4 匹以上の微生物をどの微生物も死なないようにシャーレに入れることはできない. </p> <div class="source"> <p class="source"> 問題文と自動審判に使われるデータは、<a href="http://www.ioi-jp.org">情報オリンピック日本委員会</a>が作成し公開している問題文と採点用テストデータです。 </p> </div>
p02140
<h1>Problem D: Gridgedge</h1> <h2>Problem</h2> <p> 左上が$(0, 0)$、 右下が$(R-1, C-1)$である$R \times C$マスのグリッドがある。あるマス($e$, $f$)にいるとき、そこから$(e+1, f)$, $(e-1, f)$, $(e, f+1)$, $(e, f-1)$, $(e, 0)$, $(e, C-1)$, $(0, f)$, $(R-1, f)$にはコスト$1$で移動できる。ただしグリッドの外に出ることはできない。マス$(a_i, a_j)$から$(b_i, b_j)$へ移動するとき、最短経路のコストと、最短経路の組み合わせの総数を$10^9 + 7$で割った余りを計算せよ。</p> <p>ただし、経路の組み合わせは移動の方法によって区別するものとする。たとえば、現在地が$(100, 1)$ のとき、 $(100, 0)$に最短コストで行くには上記$(e, f-1)$か$(e, 0)$で行くことができるので、$2$通りと数える。</p> <h2>Input</h2> <p>入力は以下の形式で与えられる。</p> <pre> $R$ $C$ $a_i$ $a_j$ $b_i$ $b_j$ </pre> <h2>Constraints</h2> <p>入力は以下の条件を満たす。</p> <ul> <li>$1 \le R, C \le 500$</li> <li>$0 \le a_i, b_i \le R - 1$</li> <li>$0 \le a_j, b_j \le C - 1$</li> <li>与えられる入力は全て整数である。</li> </ul> <h2>Output</h2> <p>マス$(a_i, a_j)$から$(b_i, b_j)$へ移動するとき、最短経路のコストと、最短経路の組み合わせの総数を$10^9+7$で割った余りを空白区切りで$1$行に出力せよ。</p> <h2>Sample Input 1</h2> <pre> 2 2 0 0 1 1 </pre> <h2>Sample Output 1</h2> <pre> 2 8 </pre> <h2>Sample Input 2</h2> <pre> 1 1 0 0 0 0 </pre> <h2>Sample Output 2</h2> <pre> 0 1 </pre> <h2>Sample Input 3</h2> <pre> 1 10 0 0 0 9 </pre> <h2>Sample Output 3</h2> <pre> 1 1 </pre> <h2>Sample Input 4</h2> <pre> 5 5 0 0 4 4 </pre> <h2>Sample Output 4</h2> <pre> 2 2 </pre> <h2>Sample Input 5</h2> <pre> 421 435 196 169 388 9 </pre> <h2>Sample Output 5</h2> <pre> 43 917334776 </pre>
p02455
<h1>Set: Search</h1> <p> For a set $S$ of integers, perform a sequence of the following operations. Note that <u>each value in $S$ must be unique</u>. </p> <ul> <li>insert($x$): Insert $x$ to $S$ and report the number of elements in $S$ after the operation.</li> <li>find($x$): Report the number of $x$ in $S$ (0 or 1).</li> </ul> <h2>Input</h2> <p> The input is given in the following format. </p> <pre> $q$ $query_1$ $query_2$ : $query_q$ </pre> <p> Each query $query_i$ is given by </p> <pre> 0 $x$ </pre> <p>or</p> <pre> 1 $x$ </pre> <p> where the first digits <span>0</span> and <span>1</span> represent insert and find operations respectively. </p> <h2>Output</h2> <p> For each insert operation, print the number of elements in $S$.<br> For each find operation, print the number of specified elements in $S$. </p> <h2>Constraints</h2> <ul> <li>$1 \leq q \leq 200,000$</li> <li>$0 \leq x \leq 1,000,000,000$</li> </ul> <h2>Sample Input 1</h2> <pre> 7 0 1 0 2 0 3 0 2 0 4 1 3 1 10 </pre> <h2>Sample Output 1</h2> <pre> 1 2 3 3 4 1 0 </pre>
p00468
<H1>パーティー</H2> <h2>問題</h2> <p> あなたはクリスマスパーティーに学校内の自分の友達と,自分の友達の友達を招待することにした. あなたの通う学校の生徒数は n 人であり,それぞれの生徒には 1 から n までの番号が割り振られている. あなたの番号は 1 である. あなたの手元には,誰と誰が友達であるかを記したリストがある. このリストをもとに, あなたがクリスマスパーティーに招待する生徒数を求めるプログラムを作成せよ. </p> <h2>入力</h2> <p> 入力は複数のデータセットからなる.各データセットは以下の形式で与えられる. </p> <p> データセットの1 行目には学校の生徒数 n (2 &le; n &le; 500)が, 2 行目にはリストの長さ m (1 &le; m &le; 10000)が書かれている. 入力は全部で 2+m 行からなる.2+i 行目(1 &le; i &le; m)には 2 つの整数 a<sub>i</sub> と b<sub>i</sub> (1 &le; a<sub>i</sub> &lt; b<sub>i</sub> &le; n)が空白区切りで書かれており,番号 a<sub>i</sub> と番号 b<sub>i</sub> の生徒が友達同士であることを表す. 3 行目から 2+m 行目には同じ友達関係を表す行が重複して現れることはない. </p> <p> n, m がともに 0 のとき入力の終了を示す.データセットの数は 5 を超えない. </p> <h2>出力</h2> <p> データセットごとに,あなたがクリスマスパーティーに招待する生徒数を1 行に出力する. </p> <h2>入出力例</h2> <h3>入力例</h3> <pre> 6 5 1 2 1 3 3 4 2 3 4 5 6 5 2 3 3 4 4 5 5 6 2 5 0 0 </pre> <h3>出力例</h3> <pre> 3 0 </pre> <p> 1つ目の入力例 において,あなたの友達は番号 2 と番号 3 の生徒の 2 人である. また,番号 3 と番号 4 の生徒は友達同士であるので, 番号 4 の生徒はあなたの友達の友達である. 番号 5 と番号 6 の生徒はあなたの友達でもなく,あなたの友達の友達でもない. したがって,あなたは番号 2,3,4 の 3 人の生徒をクリスマスパーティーに招待する. </p> <p> 2つ目の入力例 において,あなたには友達はいない. したがって,あなたがクリスマスパーティーに招待する生徒数は 0 人である. </p> <div class="source"> <p class="source"> 上記問題文と自動審判に使われるデータは、<a href="http://www.ioi-jp.org">情報オリンピック日本委員会</a>が作成し公開している問題文と採点用テストデータです。 </p> </div>
p00192
<H1>立体駐車場</H1> <table> <tr> <td style="vertical-align:top"> <p> 街中には駐車場の利用効率を上げるため、立体式やタワー式などの様々な駐車場があります。その中には、ひとつの駐車スペースに図のような「2段式駐車装置」を設置し、2台分の駐車スペースを確保している駐車場もあります。この2段式駐車装置は1台を昇降式のパレット(車を乗せる平らな鉄板)に乗せて上段に駐車させ、もう1台を下段に駐車することができます。 </p> <p> このような2段式駐車装置を用いている駐車場では、上段の車を出し入れするのに、その都度、下段に駐車されている車を出して、退かす必要があるので、必ず管理人さんが駐車している車のカギを預かって、必要に応じて車の出し入れを行います。 </p> </td> <td> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tsurugaParking1"> </td> </tr> </table> <p> 鶴ヶ駐車場もこのような2段式駐車装置を設置している駐車場のひとつですが、人手不足のため、車の運転ができない人が管理人になってしまいました。そのため、一度駐車した車はお客さんが戻るまで動かすことができず、上段になった車は下段の車の持ち主が戻ってからでないと車を出すことができない状態になってしまいました。 </p> <p> 次から次へと駐車しに来る車を手際よくさばかなければならない管理人さんを手伝うため、鶴ヶ駐車場のルールを満たすプログラムを作成してください。 </p> <p><b>鶴ヶ駐車場の設備</b></p> <ul> <li> 駐車スペースは1つ以上あり、全て2段式駐車装置が設置されています。</li> <li> 各駐車スペースには1から順に番号が割り振られています。</li> <li> 初めは駐車場に1台も駐車していないものとします。</li> </ul> <p>鶴ヶ駐車場は以下のようなルールを採用しています。</p> <p><b>車を止める時</b></p> <ul> <li>駐車する車の駐車時間が管理人に知らされます。</li> <li>1台も駐車されていない駐車スペースから先に駐車していきます。</li> <li>1台も駐車されていない駐車スペースがない場合には、空いている駐車スペースに駐車します。 ただし、そのような駐車スペースが複数あるときは以下の手順で駐車します。</li> <ol> <li> 駐車してある車の残り駐車時間が駐車しようとしている車の駐車時間以上のものがある場合、その差が一番小さい駐車スペースに駐車します。</li> <li> 駐車してあるどの車の残り駐車時間も駐車しようとしている車の駐車時間未満である場合、その差が一番小さい駐車スペースに駐車します。</li> </ol> <li>満車(空いている駐車スペースがない)の場合、駐車しようとする車は駐車スペースが空くまで順番に待ちます。空いたと同時に、最初に待っていた車から順に駐車します。 </li> </ul> <p> ※各条件において、該当する駐車スペースが複数ある場合は駐車スペース番号の最も小さいところに駐車することとします。また、同時刻に出庫する車がある場合は、出庫する車がすべて出てから駐車を始め、待っている車がある限り、駐車できるだけの車が同時刻に駐車することとします。 </p> <p><b>車が出る時</b></p> <ul> <li>管理人に知らされた駐車時間を過ぎた車は出庫します。</li> <li>複数の駐車スペースで同時に駐車時間を過ぎた車があった場合、駐車スペース番号の小さい車から先に出庫します。</li> <li>上段に駐車した車の駐車時間が過ぎた場合、下段の車が出庫するまで待たなければなりません。上段の車は下段の車が出庫した後、同時刻に出庫します。</li> </ul> <p> 下図で鶴ヶ駐車場の駐車方法の例を示します。この例では、駐車スペースの数は3で、車 B ~車 Eがすでに駐車してあるとします。そこに駐車時間70分の車 A が来たことを考えます。駐車スペース3にはすでに2台駐車されているので駐車できず、まだ空いている駐車スペース1か駐車スペース2のどちらかに駐車することになります。駐車スペース1に駐車中の車 B の残り駐車時間は50分、駐車スペース2に駐車中の車Cの残り駐車時間は22分で、どちらも車 A の駐車時間より少ないので、車 A の駐車時間との差がより小さい車 B が駐車してある駐車スペース1に駐車します。その結果、先に駐車していた車 B は上段になります。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tsurugaParking2"> </center> <br/> <p> 駐車スペースの数 <var>m</var>、駐車する車の台数 <var>n</var>、各車の駐車時間 <var>t</var> を入力とし、駐車場から出てくる順番に車の整理番号を出力するプログラムを作成してください。ただし、車には入力の順に1からはじまる整数の整理番号が割り振られており、車は10分おきに1台ずつ整理番号順に駐車しにくるものとします。 </p> <H2>Input</H2> <p> 複数のデータセットの並びが入力として与えられます。入力の終わりはゼロふたつの行で示されます。各データセットは以下の形式で与えられます。 </p> <pre> <var>m</var> <var>n</var> <var>t<sub>1</sub></var> <var>t<sub>2</sub></var> : <var>t<sub>n</sub></var> </pre> <p> 1 行目に2段式の駐車装置の数 <var>m</var> (1 &le; <var>m</var> &le; 10) と駐車する車の台数 <var>n</var> (1 &le; <var>n</var> &le; 100) が与えられます。続く <var>n</var> 行に<var>i</var> 台目の車の駐車時間 <var>t<sub>i</sub></var> (1 &le; <var>t<sub>i</sub></var> &le; 120) が与えられます。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセット毎に駐車場から出てくる順番に車の整理番号を1行に出力します。整理番号は空白区切りで出力してください。 </p> <H2>Sample Input</H2> <pre> 3 5 90 52 82 84 70 2 4 10 30 40 60 0 0 </pre> <H2>Output for the Sample Input</H2> <pre> 2 5 1 4 3 1 2 4 3 </pre>
p02005
<h1>Problem B. Colorful Drink</h1> <!-- Time Limit: 2 sec Memory Limit: 512 MB --> <p> In the Jambo Amusement Garden (JAG), you sell colorful drinks consisting of multiple color layers. This colorful drink can be made by pouring multiple colored liquids of different density from the bottom in order. </p> <p> You have already prepared several colored liquids with various colors and densities. You will receive a drink request with specified color layers. The colorful drink that you will serve must satisfy the following conditions. </p> <ul> <li>You cannot use a mixed colored liquid as a layer. Thus, for instance, you cannot create a new liquid with a new color by mixing two or more different colored liquids, nor create a liquid with a density between two or more liquids with the same color by mixing them.</li> <li>Only a colored liquid with strictly less density can be an upper layer of a denser colored liquid in a drink. That is, you can put a layer of a colored liquid with density $x$ directly above the layer of a colored liquid with density $y$ if $x < y$ holds.</li> </ul> <p> Your task is to create a program to determine whether a given request can be fulfilled with the prepared colored liquids under the above conditions or not. </p> <h3>Input</h3> <p> The input consists of a single test case in the format below. </p> <pre> $N$ $C_1$ $D_1$ $\vdots$ $C_N$ $D_N$ $M$ $O_1$ $\vdots$ $O_M$ </pre> <p> The first line consists of an integer $N$ ($1 \leq N \leq 10^5$), which represents the number of the prepared colored liquids. The following $N$ lines consists of $C_i$ and $D_i$ ($1 \leq i \leq N$). $C_i$ is a string consisting of lowercase alphabets and denotes the color of the $i$-th prepared colored liquid. The length of $C_i$ is between $1$ and $20$ inclusive. $D_i$ is an integer and represents the density of the $i$-th prepared colored liquid. The value of $D_i$ is between $1$ and $10^5$ inclusive. The ($N+2$)-nd line consists of an integer $M$ ($1 \leq M \leq 10^5$), which represents the number of color layers of a drink request. The following $M$ lines consists of $O_i$ ($1 \leq i \leq M$). $O_i$ is a string consisting of lowercase alphabets and denotes the color of the $i$-th layer from the top of the drink request. The length of $O_i$ is between $1$ and $20$ inclusive. </p> <h2>Output</h2> <p> If the requested colorful drink can be served by using some of the prepared colored liquids, print '<span>Yes</span>'. Otherwise, print '<span>No</span>'. </p> <h2>Examples</h2> <h2>Sample Input 1</h2> <pre> 2 white 20 black 10 2 black white </pre> <h2>Output for Sample Input 1</h2> <pre> Yes </pre> <h2>Sample Input 2</h2> <pre> 2 white 10 black 10 2 black white </pre> <h2>Output for Sample Input 2</h2> <pre> No </pre> <h2>Sample Input 3</h2> <pre> 2 white 20 black 10 2 black orange </pre> <h2>Output for Sample Input 3</h2> <pre> No </pre> <h2>Sample Input 4</h2> <pre> 3 white 10 red 20 white 30 3 white red white </pre> <h2>Output for Sample Input 4</h2> <pre> Yes </pre> <h2>Sample Input 5</h2> <pre> 4 red 3444 red 3018 red 3098 red 3319 4 red red red red </pre> <h2>Output for Sample Input 5</h2> <pre> Yes </pre>
p00038
<H1>ポーカー</H1> <p> ポーカーの手札データを読み込んで、それぞれについてその役を出力するプログラムを作成してください。ただし、この問題では、以下のルールに従います。 </p> <ul> <li>ポーカーはトランプ 5 枚で行う競技です。</li> <li>同じ数字のカードは 5 枚以上ありません。</li> <li>ジョーカーは無いものとします。</li> <li>以下のポーカーの役だけを考えるものとします。(番号が大きいほど役が高くなります。)</li> </ul> <ol style="margin-left:40px"> <li>役なし(以下に挙げるどれにも当てはまらない)</li> <li>ワンペア(2 枚の同じ数字のカードが1 組ある)</li> <li>ツーペア(2 枚の同じ数字のカードが2 組ある)</li> <li>スリーカード(3 枚の同じ数字のカードが1 組ある)</li> <li>ストレート(5 枚のカードの数字が連続している)<br> ただし、A を含むストレートの場合、A で終わる並びもストレートとします。つまり、A を含むストレート は、A 2 3 4 5  と 10 J Q K A  の2種類です。J Q K A 2 などのように、A をまたぐ並び はストレートではありません。(この場合、「役なし」になります)。 </li> <li>フルハウス(3 枚の同じ数字のカードが1 組と、残りの2 枚が同じ数字のカード)</li> <li>フォーカード(4 枚の同じ数字のカードが1 組ある)</li> </ol> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットは以下の形式で与えられます。 </p> <pre class=exp> 手札1,手札2,手札3,手札4,手札5 </pre> <p> 手札は、トランプのJ(ジャック) を11、Q(クイーン) を12、K(キング) を13、A(エース)を 1、その他はそれぞれの数字で表すこととします。 </p> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、手札によってできる最も高い役をひとつ出力してください。役の表記については出力例に従ってください。 </p> <H2>Sample Input</H2> <pre> 1,2,3,4,1 2,3,2,3,12 12,13,11,12,12 7,6,7,6,7 3,3,2,3,3 6,7,8,9,10 11,12,10,1,13 11,12,13,1,2 </pre> <H2>Output for the Sample Input</H2> <pre> one pair two pair three card full house four card straight straight null </pre> <p> 3 3 2 3 3 という手札であったときは、two pair ではなく four card です。 </p>
p01279
<H1><font color="#000">Problem G:</font> Defend the Bases</H1> <p> A country Gizevom is being under a sneak and fierce attack by their foe. They have to deploy one or more troops to every base immediately in order to defend their country. Otherwise their foe would take all the bases and declare "All your base are belong to us." </p> <p> You are asked to write a program that calculates the minimum time required for deployment, given the present positions and marching speeds of troops and the positions of the bases. </p> <H2>Input</H2> <p> The input consists of multiple datasets. Each dataset has the following format: </p> <p> <i>N M</i><br> <i>x</i><sub>1</sub> <i>y</i><sub>1</sub> <i>v</i><sub>1</sub><br> <i>x</i><sub>2</sub> <i>y</i><sub>2</sub> <i>v</i><sub>2</sub><br> ...<br> <i>x</i><sub><i>N</i></sub> <i>y</i><sub><i>N</i></sub> <i>v</i><sub><i>N</i></sub><br> <i>x'</i><sub>1</sub> <i>y'</i><sub>1</sub><br> <i>x'</i><sub>2</sub> <i>y'</i><sub>2</sub><br> ...<br> <i>x'</i><sub><i>M</i></sub> <i>y'</i><sub><i>M</i></sub><br> </p> <p> <i>N</i> is the number of troops (1 &le; <i>N</i> &le; 100); <i>M</i> is the number of bases (1 &le; <i>M</i> &le; 100); (<i>x<sub>i</sub></i>, <i>y<sub>i</sub></i> ) denotes the present position of <i>i</i>-th troop; <i>v<sub>i</sub></i> is the speed of the <i>i</i>-th troop (1 &le; <i>v<sub>i</sub></i> &le; 100); (<i>x'<sub>j</sub></i>, <i>y'<sub>j</sub></i>) is the position of the <i>j</i>-th base. </p> <p> All the coordinates are integers between 0 and 10000 inclusive. </p> <p> The last dataset is followed by a line containing two zeros. This line is not a part of any dataset and should not be processed. </p> <H2>Output</H2> <p> For each dataset, print the minimum required time in a line. </p> <H2>Sample Input</H2> <pre> 2 2 10 20 1 0 10 1 0 10 10 0 0 0 </pre> <H2>Output for the Sample Input</H2> <pre> 14.14213562 </pre>
p03244
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>A sequence <var>a_1,a_2,... ,a_n</var> is said to be /\/\/\/ when the following conditions are satisfied:</p> <ul> <li>For each <var>i = 1,2,..., n-2</var>, <var>a_i = a_{i+2}</var>.</li> <li>Exactly two different numbers appear in the sequence.</li> </ul> <p>You are given a sequence <var>v_1,v_2,...,v_n</var> whose length is even. We would like to make this sequence /\/\/\/ by replacing some of its elements. Find the minimum number of elements that needs to be replaced.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq n \leq 10^5</var></li> <li><var>n</var> is even.</li> <li><var>1 \leq v_i \leq 10^5</var></li> <li><var>v_i</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>n</var> <var>v_1</var> <var>v_2</var> <var>...</var> <var>v_n</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of elements that needs to be replaced.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 3 1 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 </pre> <p>The sequence <var>3,1,3,2</var> is not /\/\/\/, but we can make it /\/\/\/ by replacing one of its elements: for example, replace the fourth element to make it <var>3,1,3,1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 105 119 105 119 105 119 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>The sequence <var>105,119,105,119,105,119</var> is /\/\/\/.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 1 1 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>2 </pre> <p>The elements of the sequence <var>1,1,1,1</var> are all the same, so it is not /\/\/\/.</p></section> </div> </span>
p01783
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script type='text/javascript' src='http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script> </script> <h2>D - LR</h2> <h3>Problem Statement</h3> <p> JAG Kingdom will hold a contest called ICPC (Interesting Contest for Producing Calculation). </p> <p> At the contest, you are given a string composed of <code>?</code>s and <i>usable characters</i>. You should replace all <code>?</code>s in the string with the usable characters to make the string valid mathematical expression, before submitting it. The usable characters are <code>L</code>, <code>R</code>, <code>(</code>, <code>)</code>, <code>,</code>, <code>0</code>, <code>1</code>, <code>2</code>, <code>3</code>, <code>4</code>, <code>5</code>, <code>6</code>, <code>7</code>, <code>8</code>, and <code>9</code>. </p> <p> For example, suppose that you are given the string "R(??3,??1?78??1?)?", then you can submit "R(123,L(1678,213))" as an example. </p> <p> The submitted string will be scored as follows. </p> <ul> <li>Let $L$ and $R$ be functions defined by $L(x,y)=x$, $R(x,y)=y$, where $x$ and $y$ are non-negative integers. </li> <li>The submitted string will be regarded as a mathematical expression, whose value will be the score. For example, the score of the string "R(123,L(1678,213))" is $R(123,L(1678,213)) = R(123,1678) = 1678$. </li> <li>If the string cannot be evaluated as a mathematical expression about the functions $L$ and $R$, the string will be rejected. For example, "R", "R(3)", "R(3,2", "R(3,2,4)" and "LR(3,2)" are all invalid. </li> <li>And strings that contain numbers with extra leading zeros, will be rejected. For example, "R(04,18)" is invalid, while "R(0,18)" is valid.</li> </ul> <p> The winner of the contest will be the person getting the highest score. Your friend Jagger, who is going to join the contest, wants to be the winner. You are asked by Jagger to make the program finding the possible highest score for the input string. </p> <h3>Input</h3> <p> The input contains one string in a line, whose length $N$ is between $1$ and $50$, inclusive. </p> <p> You can assume that each element in the string is one of <code>L</code>, <code>R</code>, <code>(</code>, <code>)</code>, <code>,</code>, <code>0</code>, <code>1</code>, <code>2</code>, <code>3</code>, <code>4</code>, <code>5</code>, <code>6</code>, <code>7</code>, <code>8</code>, <code>9</code>, or <code>?</code>. </p> <h3>Output</h3> <p> Display the possible highest score in a line for the given string. </p> <p> If it's impossible to get valid strings for the input string, print "invalid" in a line. </p> <h3>Sample Input 1</h3> <pre>R?????,2?)</pre> <h3>Output for the Sample Input 1</h3> <pre>29</pre> <h3>Sample Input 2</h3> <pre>???3??</pre> <h3>Output for the Sample Input 2</h3> <pre>999399</pre> <h3>Sample Input 3</h3> <pre>????,??,???</pre> <h3>Output for the Sample Input 3</h3> <pre>invalid</pre> <h3>Sample Input 4</h3> <pre>?????,??,???</pre> <h3>Output for the Sample Input 4</h3> <pre>99</pre> <h3>Sample Input 5</h3> <pre>L(1111111111111111111111111111111111111111111,2)</pre> <h3>Output for the Sample Input 5</h3> <pre>1111111111111111111111111111111111111111111</pre> <h3>Sample Input 6</h3> <pre>L?1???????????????????????????????????????????????</pre> <h3>Output for the Sample Input 6</h3> <pre>199999999999999999999999999999999999999999999</pre> <h3>Sample Input 7</h3> <pre>L?0???????????????????????????????????????????????</pre> <h3>Output for the Sample Input 7</h3> <pre>0</pre>
p00891
<H1><font color="#000">Problem H:</font> Where's Wally</H1> <p> Do you know the famous series of children's books named "Where's Wally"? Each of the books contains a variety of pictures of hundreds of people. Readers are challenged to find a person called Wally in the crowd. </p> <p> We can consider "Where's Wally" as a kind of pattern matching of two-dimensional graphical images. Wally's figure is to be looked for in the picture. It would be interesting to write a computer program to solve "Where's Wally", but this is not an easy task since Wally in the pictures may be slightly different in his appearances. We give up the idea, and make the problem much easier to solve. You are requested to solve an easier version of the graphical pattern matching problem. </p> <p> An image and a pattern are given. Both are rectangular matrices of bits (in fact, the pattern is always square-shaped). 0 means white, and 1 black. The problem here is to count the number of occurrences of the pattern in the image, i.e. the number of squares in the image exactly matching the pattern. Patterns appearing rotated by any multiples of 90 degrees and/or turned over forming a mirror image should also be taken into account. </p> <H2>Input</H2> <p> The input is a sequence of datasets each in the following format. </p> <p> <i>w h p</i><br> image data<br> pattern data<br> </p> <p> The first line of a dataset consists of three positive integers <i>w</i>, <i>h</i> and <i>p</i>. <i>w</i> is the width of the image and <i>h</i> is the height of the image. Both are counted in numbers of bits. <i>p</i> is the width and height of the pattern. The pattern is always square-shaped. You may assume 1 &le; <i>w</i> &le; 1000, 1 &le; <i>h</i> &le; 1000, and 1 &le; <i>p</i> &le; 100. </p> <p> The following <i>h</i> lines give the image. Each line consists of &lceil;<i>w</i>/6&rceil; (which is equal to &&lfloor;(<i>w</i>+5)/6&rfloor;) characters, and corresponds to a horizontal line of the image. Each of these characters represents six bits on the image line, from left to right, in a variant of the BASE64 encoding format. The encoding rule is given in the following table. The most significant bit of the value in the table corresponds to the leftmost bit in the image. The last character may also represent a few bits beyond the width of the image; these bits should be ignored. </p> <table> <tr> <td>character</td><td>value (six bits)</td> </tr> <tr><td>A-Z</td><td>0-25</td></tr> <tr><td>a-z</td><td>26-51</td></tr> <tr><td>0-9</td><td>52-61</td></tr> <tr><td>+</td><td>62</td></tr> <tr><td>/</td><td>63</td></tr> </table> <p> The last p lines give the pattern. Each line consists of &lceil;<i>p</i>/6&rceil; characters, and is encoded in the same way as the image. </p> <p> A line containing three zeros indicates the end of the input. The total size of the input does not exceed two megabytes. </p> <H2>Output</H2> <p> For each dataset in the input, one line containing the number of matched squares in the image should be output. An output line should not contain extra characters. </p> <p> Two or more matching squares may be mutually overlapping. In such a case, they are counted separately. On the other hand, a single square is never counted twice or more, even if it matches both the original pattern and its rotation, for example. </p> <H2>Sample Input</H2> <pre> 48 3 3 gAY4I4wA gIIgIIgg w4IAYAg4 g g w 153 3 3 kkkkkkkkkkkkkkkkkkkkkkkkkg SSSSSSSSSSSSSSSSSSSSSSSSSQ JJJJJJJJJJJJJJJJJJJJJJJJJI g Q I 1 1 2 A A A 384 3 2 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ BCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/A CDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/AB A A 0 0 0 </pre> <H2>Output for the Sample Input</H2> <pre> 8 51 0 98 </pre>
p01629
<h1>Problem C: ホテル探し</h1> <h2>Problem Statement</h2> <p> 会津合宿に参加予定の高槻さんは、家が貧乏であまりお金を持っていない。彼女は、合宿のためにホテルを探しているが、なるべくお金を節約できるプランにするために、悩み中である。宿泊の合計金額が安くなるホテルの泊まり方を求めて、彼女に教えてあげよう。 </p> <p> 宿泊するホテルの候補数は、N個であり、各ホテルをホテル1、ホテル2、...、ホテルNと呼ぶこととする。彼女は、これからD泊分の予約をこれらのホテルから選ぶことにしている。候補のホテルは全て、1泊ごとに宿泊料金が変動することがあるため、入力では各ホテルごとにD泊分の宿泊費が与えられる。 </p> <p> D泊分の宿泊費の合計が最小になるような、ホテルの泊まり方を出力せよ。ただし、D泊全て同じホテルに泊まる必要はないことに注意してほしい。安さのためであれば、彼女は1泊ごとにホテルの移動をすることも考えている。 </p> <p> もし、そのような宿泊方法が複数あるならば、ホテルの移動回数が最小になるような宿泊方法を出力せよ。ホテルの移動回数とは、x泊目(1 <= x < D)の宿泊ホテルとx+1泊目の宿泊ホテルが異なる場合にこれを1回の移動と考え、D泊分足し合わせた値である。 </p> <p> それでも1通りに定まらない場合は、辞書順で最も小さくなるような宿泊方法を出力すること。2つの宿泊ホテル番号の数列 A = {a1, a2, ..., aD} と B = {b1, b2, ..., bD} があったとき、AがBより辞書順で小さいとは、aiがbiと異なるような最初のiについて、aiがbiより小さいときのことを言う。 </p> <h2>Input</h2> <p> 各データセットは、以下の形式で入力される。 </p> <pre>N D p11 p12 ... p1D p21 p22 ... p2D ... pN1 pN2 ... pND </pre> <p> 入力は、全て整数である。Nは宿泊候補のホテルの数、Dは宿泊日数である。pijは、ホテルiにおける、j泊目の宿泊費である。 </p> <h2>Constraints</h2> <ul> <li>1 <= N <= 50</li> <li>1 <= D <= 50</li> <li>1 <= pij <= 10000</li> </ul> <h2>Output</h2> <p> 各データセットに対して、以下の形式で出力せよ。 </p> <pre>P M stay1 stay2 ... stayD </pre> <p> PはD泊分の最小の宿泊費合計、Mは最小のホテル移動回数である。続いて、問題文に指定された条件で宿泊するホテルを決定し、D泊分のホテルの番号を出力せよ。stayi (1 <= stayi <= N)は、i泊目に宿泊するホテルが、ホテルstayiであることを意味する。 </p> <h2>Sample Input 1</h2> <pre>2 3 3000 6000 3000 4000 5500 4500 </pre> <h2>Output for the Sample Input 1</h2> <pre>11500 2 1 2 1 </pre> <p> 最小の宿泊費合計は、11500円であり、この合計値を満たせるのは、 1泊目にホテル1に泊まり3000円、2泊目にホテル2に泊まり5500円、3泊目にホテル1に泊まり3000円の方法だけである。ここでは、1泊目と2泊目の間、2泊目と3泊目の間、の合計2回ホテルの移動を行っている。 </p> <h2>Sample Input 2</h2> <pre>3 4 5500 5500 5500 5500 5480 5780 5980 5980 5500 5500 5500 5500 </pre> <h2>Output for the Sample Input 2</h2> <pre>21980 1 2 1 1 1 </pre> <p> このサンプルの宿泊費合計の最小は21980円である。合計をこの価格にするための方法は何通りか存在するが、移動回数を最小にするならば、A = {2, 1, 1, 1}、B = {2, 3, 3, 3}の2通りにしぼられる。この2通りの内、辞書順で最小なものはAであるため、これを出力する。 </p>
p03614
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a permutation <var>p_1,p_2,...,p_N</var> consisting of <var>1</var>,<var>2</var>,..,<var>N</var>. You can perform the following operation any number of times (possibly zero):</p> <p>Operation: Swap two <strong>adjacent</strong> elements in the permutation.</p> <p>You want to have <var>p_i ≠ i</var> for all <var>1≤i≤N</var>. Find the minimum required number of operations to achieve this.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≤N≤10^5</var></li> <li><var>p_1,p_2,..,p_N</var> is a permutation of <var>1,2,..,N</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>p_1</var> <var>p_2</var> .. <var>p_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum required number of operations</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 1 4 3 5 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>Swap <var>1</var> and <var>4</var>, then swap <var>1</var> and <var>3</var>. <var>p</var> is now <var>4,3,1,5,2</var> and satisfies the condition. This is the minimum possible number, so the answer is <var>2</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 </pre> <p>Swapping <var>1</var> and <var>2</var> satisfies the condition.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> <p>The condition is already satisfied initially.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>9 1 2 4 9 5 8 7 3 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>3 </pre></section> </div> </span>
p02906
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke's mother gave Snuke an undirected graph consisting of <var>N</var> vertices numbered <var>0</var> to <var>N-1</var> and <var>M</var> edges. This graph was connected and contained no parallel edges or self-loops.</p> <p>One day, Snuke broke this graph. Fortunately, he remembered <var>Q</var> clues about the graph. The <var>i</var>-th clue (<var>0 \leq i \leq Q-1</var>) is represented as integers <var>A_i,B_i,C_i</var> and means the following:</p> <ul> <li>If <var>C_i=0</var>: there was exactly one simple path (a path that never visits the same vertex twice) from Vertex <var>A_i</var> to <var>B_i</var>.</li> <li>If <var>C_i=1</var>: there were two or more simple paths from Vertex <var>A_i</var> to <var>B_i</var>.</li> </ul> <p>Snuke is not sure if his memory is correct, and worried whether there is a graph that matches these <var>Q</var> clues. Determine if there exists a graph that matches Snuke's memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 10^5</var></li> <li><var>N-1 \leq M \leq N \times (N-1)/2</var></li> <li><var>1 \leq Q \leq 10^5</var></li> <li><var>0 \leq A_i,B_i \leq N-1</var></li> <li><var>A_i \neq B_i</var></li> <li><var>0 \leq C_i \leq 1</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>Q</var> <var>A_0</var> <var>B_0</var> <var>C_0</var> <var>A_1</var> <var>B_1</var> <var>C_1</var> <var>\vdots</var> <var>A_{Q-1}</var> <var>B_{Q-1}</var> <var>C_{Q-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a graph that matches Snuke's memory, print <code>Yes</code>; otherwise, print <code>No</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 5 3 0 1 0 1 2 1 2 3 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes </pre> <p>For example, consider a graph with edges <var>(0,1),(1,2),(1,4),(2,3),(2,4)</var>. This graph matches the clues.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 4 3 0 1 0 1 2 1 2 3 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>No </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>10 9 9 7 6 0 4 5 1 9 7 0 2 9 0 2 3 0 4 1 0 8 0 0 9 1 0 3 0 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>No </pre></section> </div> </span>
p01107
<h3><u>Go around the Labyrinth</u></h3> <p> Explorer Taro got a floor plan of a labyrinth. The floor of this labyrinth is in the form of a two-dimensional grid. Each of the cells on the floor plan corresponds to a room and is indicated whether it can be entered or not. The labyrinth has only one entrance located at the northwest corner, which is upper left on the floor plan. There is a treasure chest in each of the rooms at other three corners, southwest, southeast, and northeast. To get a treasure chest, Taro must move onto the room where the treasure chest is placed. </p> <p> Taro starts from the entrance room and repeats moving to one of the enterable adjacent rooms in the four directions, north, south, east, or west. He wants to collect all the three treasure chests and come back to the entrance room. A bad news for Taro is that, the labyrinth is quite dilapidated and even for its enterable rooms except for the entrance room, floors are so fragile that, once passed over, it will collapse and the room becomes not enterable. Determine whether it is possible to collect all the treasure chests and return to the entrance. </p> <h3>Input</h3> <p> The input consists of at most 100 datasets, each in the following format. </p> <pre> <i>N</i> <i>M</i> <i>c</i><sub>1,1</sub>...<i>c</i><sub>1,<i>M</i></sub> ... <i>c</i><sub><i>N</i>,1</sub>...<i>c</i><sub><i>N</i>,<i>M</i></sub> </pre> <p> The first line contains <i>N</i>, which is the number of rooms in the north-south direction, and <i>M</i>, which is the number of rooms in the east-west direction. <i>N</i> and <i>M</i> are integers satisfying 2 &#8804; <i>N</i> &#8804; 50 and 2 &#8804; <i>M</i> &#8804; 50. Each of the following <i>N</i> lines contains a string of length <i>M</i>. The <i>j</i>-th character <i>c</i><sub><i>i,j</i></sub> of the <i>i</i>-th string represents the state of the room (<i>i,j</i>), <i>i</i>-th from the northernmost and <i>j</i>-th from the westernmost; the character is the period ('<tt>.</tt>') if the room is enterable and the number sign ('<tt>#</tt>') if the room is not enterable. The entrance is located at (1,1), and the treasure chests are placed at (<i>N,</i>1), (<i>N,M</i>) and (1,<i>M</i>). All of these four rooms are enterable. Taro cannot go outside the given <i>N</i> × <i>M</i> rooms. </p> <p> The end of the input is indicated by a line containing two zeros. </p> <h3>Output</h3> <p> For each dataset, output <tt>YES</tt> if it is possible to collect all the treasure chests and return to the entrance room, and otherwise, output <tt>NO</tt> in a single line. </p> <h3>Sample Input</h3> <pre>3 3 ... .#. ... 5 5 ..#.. ..... #.... ..... ..... 3 8 ..#..... ........ .....#.. 3 5 ..#.. ..... ..#.. 4 4 .... .... ..## ..#. 0 0 </pre> <h3>Output for the Sample Input</h3> <pre>YES NO YES NO NO </pre>
p03090
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an integer <var>N</var>. Build an undirected graph with <var>N</var> vertices with indices <var>1</var> to <var>N</var> that satisfies the following two conditions:</p> <ul> <li>The graph is simple and connected.</li> <li>There exists an integer <var>S</var> such that, for every vertex, the sum of the indices of the vertices adjacent to that vertex is <var>S</var>.</li> </ul> <p>It can be proved that at least one such graph exists under the constraints of this problem.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All values in input are integers.</li> <li><var>3 \leq N \leq 100</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>In the first line, print the number of edges, <var>M</var>, in the graph you made. In the <var>i</var>-th of the following <var>M</var> lines, print two integers <var>a_i</var> and <var>b_i</var>, representing the endpoints of the <var>i</var>-th edge.</p> <p>The output will be judged correct if the graph satisfies the conditions.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 1 3 2 3 </pre> <ul> <li>For every vertex, the sum of the indices of the vertices adjacent to that vertex is <var>3</var>.</li> </ul></section> </div> </span>
p01557
<h1>Reverse Sort</h1> <p>1 &sim; NまでのN個の数字の順列A<sub>1</sub>, A<sub>2</sub>, ……, A<sub>N</sub>が与えられる。 この順列に対して区間[i, j](1 &le; i &le; j &le; N)の数字の順序を逆順にする操作reverse(i, j)を行うことが出来る。 例えば、[1, 2, 3, 4, 5]に対してreverse(2, 4)を適用すると[1, 4, 3, 2, 5]となる。 最小で何回の操作を行えば順列を昇順にソートされた状態にすることが出来るか計算せよ。 </p> <h2>Input</h2> <p>入力は以下の形式で与えられる。 </p><blockquote> N<br>A<sub>1</sub> A<sub>2</sub> …… A<sub>N</sub><br></blockquote> <h2>Constraints</h2> <ul><li><p>Nは整数である </p></li><li><p>2 &le; N &le; 10 </p></li></ul> <h2>Output</h2> <p>問題の解を1行に出力せよ。 </p> <h2>Sample Input 1</h2> <pre>5 1 4 3 5 2 </pre> <h2>Output for the Sample Input 1</h2> <pre>2 </pre><ul><li><p>reverse(4,5):1 4 3 2 5 </p></li><li><p>reverse(2,4):1 2 3 4 5 </p></li></ul> <p>のようにすればよい。 </p> <h2>Sample Input 2</h2> <pre>5 3 1 5 2 4 </pre> <h2>Output for the Sample Input 2</h2> <pre>4 </pre><p>例えば </p><ul><li><p>reverse(1,2):1 3 5 2 4 </p></li><li><p>reverse(3,5):1 3 4 2 5 </p></li><li><p>reverse(2,4):1 2 4 3 5 </p></li><li><p>reverse(3,4):1 2 3 4 5 </p></li></ul> <p>のようにすればよい。 </p> <h2>Sample Input 3</h2> <pre>3 1 2 3 </pre> <h2>Output for the Sample Input 3</h2> <pre>0 </pre> <h2>Sample Input 4</h2> <pre>10 3 1 5 2 7 4 9 6 10 8 </pre> <h2>Output for the Sample Input 4</h2> <pre>9 </pre>
p03839
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> squares aligned in a row. The <var>i</var>-th square from the left contains an integer <var>a_i</var>.</p> <p>Initially, all the squares are white. Snuke will perform the following operation some number of times:</p> <ul> <li>Select <var>K</var> consecutive squares. Then, paint all of them white, or paint all of them black. Here, the colors of the squares are overwritten.</li> </ul> <p>After Snuke finishes performing the operation, the score will be calculated as the sum of the integers contained in the black squares. Find the maximum possible score.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≤N≤10^5</var></li> <li><var>1≤K≤N</var></li> <li><var>a_i</var> is an integer.</li> <li><var>|a_i|≤10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum possible score.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 3 -10 10 -10 10 -10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>10 </pre> <p>Paint the following squares black: the second, third and fourth squares from the left.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 2 10 -10 -10 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>20 </pre> <p>One possible way to obtain the maximum score is as follows:</p> <ul> <li>Paint the following squares black: the first and second squares from the left.</li> <li>Paint the following squares black: the third and fourth squares from the left.</li> <li>Paint the following squares white: the second and third squares from the left.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1 1 -10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>10 5 5 -4 -5 -8 -4 7 2 -4 0 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>17 </pre></section> </div> </span>
p01804
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]} }); </script> <script type="text/javascript" async src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML"> </script> <!-- begin en only --> <h3><u>Falling Block Puzzle</u></h3> <!-- end en only --> <!-- begin ja only --> <h3><u>ブロック落とし</u></h3> <!-- end ja only --> <!-- end en only --> <!-- begin ja only --> <p>あなたはある落ち物パズルで遊んでいる. このパズルのフィールドは,下図のように各段に立方体のセルが2マス×2マスに並び,段が上に無限に並んでいる形をしている. </p> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_JAGDomestic2015_c-field" height="253" width="114" /> <p>それぞれのセルは,セルにぴったり収まるブロックが1つ存在するか,何もないかのどちらかである. このパズルは以下のように進行する. </p> <ol><li> 初期状態としていくつかのブロックが設置されている. </li><li> 2マス×2マス×2マスに収まるブロックの塊を上から落とす.ただし,落とす前に,ブロックがフィールドからはみ出さないようにして塊を水平方向に平行移動することができる. </li><li> 落としたブロックのうち,ある1つの下面が,すでに置かれているブロックまたはフィールドの底に着いた時点で,すべてのブロックの落下が止まり停止する. </li><li> それぞれの段について,4マス全てが埋まっていればその段のブロックは消滅し,その上にあるブロックが1段ずつ落下する.落下後のそれぞれのブロックの下のセルにブロックがなかったとしても,それ以上落下することはない. </li><li> 2に戻る. </li></ol> <p>初期状態で置かれているブロックと,落とす塊がいくつか与えられるので,与えられる順に全ての塊を落とすことで,最大いくつの段を消すことができるかを求めるプログラムを作れ. </p> <!-- end ja only --> <h3>Input</h3> <!-- begin ja only --> <p>入力は100個以下のデータセットからなる.各データセットは以下の形をしている. </p> <blockquote>(初期状態のブロックの高さ <var>H</var> ) (落とす塊の数 <var>N</var> )<br>(初期状態の1段目)<br>...<br>(初期状態の <var>H</var> 段目)<br>(1個目の落とす塊)<br>...<br>( <var>N</var> 個目の落とす塊)</blockquote> <p>各データセットの1行目には初期状態のブロックの高さ <var>H</var> (1 &le; <var>H</var> &le; 10)と落とす塊の数 <var>N</var> (1 &le; <var>N</var> &le; 3)が指定されている. 続いて初期状態のそれぞれ段の情報が以下の形式で与えられる. </p> <blockquote><var>c</var><sub>11</sub> <var>c</var><sub>12</sub><br><var>c</var><sub>21</sub> <var>c</var><sub>22</sub></blockquote> <p><var>c<sub>ij</sub></var>はそれぞれのセルの情報を表し,'<samp>#</samp>'はブロックが存在することを,'<samp>.</samp>'はブロックがないことを表す.1段目から <var>H</var> 段目までのそれぞれの段について,全てのセルにブロックが存在したり,全てのセルにブロックがなかったりすることはないと仮定して良い. 続いてそれぞれの落とす塊の情報が以下の形式で与えられる. </p> <blockquote><var>b</var><sub>111</sub> <var>b</var><sub>112</sub><br><var>b</var><sub>121</sub> <var>b</var><sub>122</sub><br><var>b</var><sub>211</sub> <var>b</var><sub>212</sub><br><var>b</var><sub>221</sub> <var>b</var><sub>222</sub></blockquote> <p>初期状態の形式と同様に'<samp>#</samp>'はブロックが存在することを,'<samp>.</samp>'はブロックがないことを表す.それぞれの塊には少なくとも1つのブロックが含まれる.塊に含まれるブロックは角や辺のみで接していることがあり,面で繋がっているとは限らない. </p> <p>初期状態・ブロックの塊の入力の添え字の対応は以下の図を参照すること. </p> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_JAGDomestic2015_c-format" height="431" width="205" /> <p>入力の終わりは,2つのゼロからなる1行で示される. </p> <p>なお,下図は,後に示す Sample Input 中の最初のデータセットを表している.このデータセットでは,ブロックの塊を斜めに平行移動した後に落下させることで1つの段を消すことができる. </p> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_JAGDomestic2015_c-sample0" height="377" width="137" /> <!-- end ja only --> <h3>Output</h3> <!-- begin ja only --> <p>各データセットに対して,最大でいくつの段を消すことができるかを1行に出力せよ. </p> <!-- end ja only --> <h3>Sample Input</h3> <pre>1 1 ## #. .. .. #. .. 1 1 .# #. #. .. .. .# 2 2 ## #. ## #. .. .# ## #. #. .. #. .. 1 3 #. .. ## ## ## ## ## ## ## ## ## ## ## ## 10 3 ## #. ## #. ## .# ## .# #. ## #. ## .# ## .# ## .# #. .# #. #. .# #. .# #. .. #. .. #. .. #. .. 10 3 ## .# ## .. ## #. .# .. ## .# .# ## .# ## #. ## ## .# .. .# #. #. .# #. #. #. #. .. .. .. .. .# 0 0</pre> <h3>Output for Sample Input</h3> <pre>1 0 3 6 6 0</pre>
p00716
<h1><font color="#000">Problem A:</font> Ohgas' Fortune</h1> <p> The Ohgas are a prestigious family based on Hachioji. The head of the family, Mr. Nemochi Ohga, a famous wealthy man, wishes to increase his fortune by depositing his money to an operation company. You are asked to help Mr. Ohga maximize his profit by operating the given money during a specified period. </p> <p> From a given list of possible operations, you choose an operation to deposit the given fund to. You commit on the single operation throughout the period and deposit all the fund to it. Each operation specifies an annual interest rate, whether the interest is simple or compound, and an annual operation charge. An annual operation charge is a constant not depending on the balance of the fund. The amount of interest is calculated at the end of every year, by multiplying the balance of the fund under operation by the annual interest rate, and then rounding off its fractional part. For compound interest, it is added to the balance of the fund under operation, and thus becomes a subject of interest for the following years. For simple interest, on the other hand, it is saved somewhere else and does not enter the balance of the fund under operation (i.e. it is not a subject of interest in the following years). An operation charge is then subtracted from the balance of the fund under operation. You may assume here that you can always pay the operation charge (i.e. the balance of the fund under operation is never less than the operation charge). The amount of money you obtain after the specified years of operation is called ``the final amount of fund.'' For simple interest, it is the sum of the balance of the fund under operation at the end of the final year, plus the amount of interest accumulated throughout the period. For compound interest, it is simply the balance of the fund under operation at the end of the final year. </p> <p> Operation companies use C, C++, Java, etc., to perform their calculations, so they pay a special attention to their interest rates. That is, in these companies, an interest rate is always an integral multiple of 0.0001220703125 and between 0.0001220703125 and 0.125 (inclusive). 0.0001220703125 is a decimal representation of 1/8192. Thus, interest rates' being its multiples means that they can be represented with no errors under the double-precision binary representation of floating-point numbers. </p> <p> For example, if you operate 1000000 JPY for five years with an annual, compound interest rate of 0.03125 (3.125 %) and an annual operation charge of 3000 JPY, the balance changes as follows. </p> <table border> <tr align=right rowspan=2><td>The balance of the fund under operation (at the beginning of year)</td> <td>Interest</td><td> The balance of the fund under operation (at the end of year)</td></tr> <tr align=right><td>A</td><td>B = A &times; 0.03125 (and rounding off fractions)</td> <td>A + B - 3000</td></tr> <tr align=right><td>1000000</td><td>31250</td><td>1028250</td></tr> <tr align=right><td>1028250</td><td>32132</td><td>1057382</td></tr> <tr align=right><td>1057382</td><td>33043</td><td>1087425</td></tr> <tr align=right><td>1087425</td><td>33982</td><td>1118407</td></tr> <tr align=right><td>1118407</td><td>34950</td><td>1150357</td></tr> </table> <p> After the five years of operation, the final amount of fund is 1150357 JPY. </p> <p> If the interest is simple with all other parameters being equal, it looks like: </p> <table border> <tr align=right rowspan=2> <td> The balance of the fund under operation (at the beginning of year)</td> <td>Interest</td> <td>The balance of the fund under operation (at the end of year)</td> <td>Cumulative interest </td></tr> <tr align=right> <td>A</td><td>B = A &times; 0.03125 (and rounding off fractions)</td><td>A - 3000</td><td></td> </tr> <tr align=right><td>1000000</td><td>31250</td><td>997000</td><td>31250</td></tr> <tr align=right><td>997000</td><td>31156</td><td>994000</td><td>62406</td></tr> <tr align=right><td>994000</td><td>31062</td><td>991000</td><td>93468</td></tr> <tr align=right><td>991000</td><td>30968</td><td>988000</td><td>124436</td></tr> <tr align=right><td>988000</td><td>30875</td><td>985000</td><td>155311</td></tr> </table border> <p>In this case the final amount of fund is the total of the fund under operation, 985000 JPY, and the cumulative interests, 155311 JPY, which is 1140311 JPY. </p> <h2>Input</h2> <p> The input consists of datasets. The entire input looks like: </p> <blockquote> <i>the number of datasets (=m)</i> <br> <i>1st dataset</i> <br> <i>2nd dataset</i> <br> ... <br> <i>m-th dataset</i> <br> </blockquote> <p> The number of datasets, <i>m</i>, is no more than 100. Each dataset is formatted as follows. </p> <blockquote> <i>the initial amount of the fund for operation</i> <br> <i>the number of years of operation</i> <br> <i>the number of available operations (=n)</i> <br> <i>operation 1</i> <br> <i>operation 2</i> <br> ... <br> <i>operation n</i> <br> </blockquote> <p> The initial amount of the fund for operation, the number of years of operation, and the number of available operations are all positive integers. The first is no more than 100000000, the second no more than 10, and the third no more than 100. </p> <p> Each ``operation'' is formatted as follows. </p> <blockquote> <i>simple-or-compound annual-interest-rate annual-operation-charge</i> </blockquote> <p> where <i>simple-or-compound</i> is a single character of either '0' or '1', with '0' indicating simple interest and '1' compound. <i>annual-interest-rate</i> is represented by a decimal fraction and is an integral multiple of 1/8192. <i>annual-operation-charge</i> is an integer not exceeding 100000. </p> <h2>Output</h2> <p> For each dataset, print a line having a decimal integer indicating the final amount of fund for the best operation. The best operation is the one that yields the maximum final amount among the available operations. Each line should not have any character other than this number. </p> <p> You may assume the final balance never exceeds 1000000000. You may also assume that at least one operation has the final amount of the fund no less than the initial amount of the fund. </p> <h2>Sample Input</h2> <pre> 4 1000000 5 2 0 0.03125 3000 1 0.03125 3000 6620000 7 2 0 0.0732421875 42307 1 0.0740966796875 40942 39677000 4 4 0 0.0709228515625 30754 1 0.00634765625 26165 0 0.03662109375 79468 0 0.0679931640625 10932 10585000 6 4 1 0.0054931640625 59759 1 0.12353515625 56464 0 0.0496826171875 98193 0 0.0887451171875 78966 </pre> <h2>Output for the Sample Input</h2> <pre> 1150357 10559683 50796918 20829397 </pre>
p03993
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> rabbits, numbered <var>1</var> through <var>N</var>.</p> <p>The <var>i</var>-th (<var>1≤i≤N</var>) rabbit likes rabbit <var>a_i</var>. Note that no rabbit can like itself, that is, <var>a_i≠i</var>.</p> <p>For a pair of rabbits <var>i</var> and <var>j</var> (<var>i<j</var>), we call the pair (<var>i,j</var>) a <em>friendly pair</em> if the following condition is met.</p> <ul> <li>Rabbit <var>i</var> likes rabbit <var>j</var> and rabbit <var>j</var> likes rabbit <var>i</var>.</li> </ul> <p>Calculate the number of the friendly pairs.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≤N≤10^5</var></li> <li><var>1≤a_i≤N</var></li> <li><var>a_i≠i</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the friendly pairs.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 2 1 4 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>There are two friendly pairs: <var>(1,2)</var> and <var>(3,4)</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 2 3 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>There are no friendly pairs.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 5 5 5 5 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> <p>There is one friendly pair: <var>(1,5)</var>.</p></section> </div> </span>
p02681
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi wants to be a member of some web service.</p> <p>He tried to register himself with the ID <var>S</var>, which turned out to be already used by another user.</p> <p>Thus, he decides to register using a string obtained by appending one character at the end of <var>S</var> as his ID.</p> <p>He is now trying to register with the ID <var>T</var>. Determine whether this string satisfies the property above.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>S</var> and <var>T</var> are strings consisting of lowercase English letters.</li> <li><var>1 \leq |S| \leq 10</var></li> <li><var>|T| = |S| + 1</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>S</var> <var>T</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If <var>T</var> satisfies the property in Problem Statement, print <code>Yes</code>; otherwise, print <code>No</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>chokudai chokudaiz </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes </pre> <p><code>chokudaiz</code> can be obtained by appending <code>z</code> at the end of <code>chokudai</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>snuke snekee </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>No </pre> <p><code>snekee</code> cannot be obtained by appending one character at the end of <code>snuke</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>a aa </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Yes </pre></section> </div> </span>
p00346
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML"> </script> <H1>静かな町</H1> <p> アイヅ国では、毎年、駅伝大会が開催されています。アイヅ国には <var>N</var> 個の町が点在しており、それぞれ 1 から <var>N</var> までの番号が付いています。いくつかの町の間は、互いに直接行き来できる道でつながっています。また、どの町の間もいくつかの道を辿って行き来ができます。大会のコースは、次のようにして決めます。 </p> <ul> <li> 2つの町のすべての組み合わせについて、最短経路の距離を求める。</li> <li> それらの中で、最短経路の距離が最大になるような2つの町を、スタートの町とゴールの町とする。町の組み合わせが複数ある場合、そのうちのどれか一つを選ぶ。</li> <li> 選ばれたスタートの町とゴールの町の間の最短経路を大会のコースとする。最短経路が複数ある場合、そのうちのどれか一つを選ぶ。</li> </ul> <p> ヤマト国から来たお坊さんのトクイツは、アイヅ国のできるだけ静かな町に新しいお寺を開きたいと考えています。そのため、駅伝大会のコースに使われる可能性がない町を知りたがっています。 </p> <br/> <p> アイヅ国の町を結ぶ道の情報が与えられたとき、駅伝大会のコースに使われる可能性がない町を求めるプログラムを作成せよ。 </p> <h2>Input</h2> <p> 入力は以下の形式で与えられる。 </p> <pre> <var>N</var> <var>R</var> <var>s<sub>1</sub></var> <var>t<sub>1</sub></var> <var>d<sub>1</sub></var> <var>s<sub>2</sub></var> <var>t<sub>2</sub></var> <var>d<sub>2</sub></var> : <var>s<sub>R</sub></var> <var>t<sub>R</sub></var> <var>d<sub>R</sub></var> </pre> <p> 1行目に町の数 <var>N</var> (2 &le; <var>N</var> &le; 1500) と、町の間を直接つなぐ道の数 <var>R</var> (1 &le; <var>R</var> &le; 3000) が与えられる。続く <var>R</var> 行に2つの町を双方向に直接つなぐ道が与えられる。<var>s<sub>i</sub></var> と <var>t<sub>i</sub></var> (1 &le; <var>s<sub>i</sub></var> &lt; <var>t<sub>i</sub></var> &le; <var>N</var>) は <var>i</var> 番目の道がつなぐ2つの町の番号を表し、<var>d<sub>i</sub></var> (1 &le; <var>d<sub>i</sub></var> &le; 1000) はその道の長さを表す。ただし、どの2つの町についても、それらを直接つなぐ道は高々1本とする。 </p> <h2>Output</h2> <p> 与えられた道の情報に対して、駅伝大会のコースになることがない町をすべて出力する。1行目に駅伝大会のコースになることがない町の数 <var>M</var> を出力する。続く <var>M</var> 行に、そのような町の番号を昇順で出力する。 </p> <h2>Sample Input 1</h2> <pre> 4 5 1 2 2 1 3 2 2 3 1 2 4 2 3 4 1 </pre> <h2>Sample Output 1</h2> <pre> 1 2 </pre> <h2>Sample Input 2</h2> <pre> 7 11 1 2 2 1 3 1 2 4 2 2 5 2 2 6 1 3 4 1 3 5 1 3 6 1 4 7 2 5 7 2 6 7 1 </pre> <h2>Sample Output 2</h2> <pre> 3 2 4 5 </pre>
p01941
<h2>D: 優柔不断 - Indecision -</h2> <h3>問題</h3> <p>えびちゃんは最近ギャルゲーにハマっていて、今の目標は二人のヒロインを同時に攻略することです。</p> <p>えびちゃんはいくつかあるイベントを利用してヒロインからの好感度を高めることができますが、各イベントの対象として選べるヒロインはどちらか一人のみです。しかし、えびちゃんは選ばなかったヒロインに対してもフォローを忘れないので、もう片方のヒロインからの好感度もある程度は高めることができます。ただし、イベントには費用がかかるため、必ずしも全てのイベントを行えるわけではありません。</p> <p>さて、優柔不断なえびちゃんは各イベントをどちらのヒロインと行うか(またはどちらとも行わないか)で迷っています。えびちゃんは片方のヒロインからの好感度だけが高くても他方が低ければ意味がないと思っているので、好感度が高くない方のヒロインからの好感度が最大となる選択が理想です。</p> <p>このとき、好感度が高くない方のヒロインからの好感度の最大値を求めてください。すなわち、イベントおよび対象ヒロインの選択の結果、ふたりのヒロインからの好感度をそれぞれ <var>A</var>、<var>B</var> としたときの、min<var>\{A, B\}</var> として考えうる値の最大値を求めてください。ここで、行うことにしたイベントに掛かる費用の合計が予算を超えてはいけません。また、各ヒロインからの好感度の初期値は <var>0</var> です。</p> <h3>入力形式</h3> <pre> <var>N</var> <var>C</var> <var>a_1</var> <var>b_1</var> <var>c_1</var> <var>…</var> <var>a_N</var> <var>b_N</var> <var>c_N</var> </pre> <p><var>1</var> 行目にはイベントの候補の数 <var>N</var> と予算 <var>C</var> が空白区切りで与えられる。</p> <p><var>1+i</var> 行目 <var>(1 \leq i \leq N)</var> には <var>i</var> 番目のイベントについて、それを行うヒロインの好感度の増分 <var>a_i</var> と他方のヒロインの好感度の増分 <var>b_i</var>、およびそれらにかかる費用 <var>c_i</var> が空白区切りで与えられる。</p> <h3>制約</h3> <ul> <li> <var>1 \leq N \leq 100</var></li> <li> <var>1 \leq C \leq 100</var></li> <li> <var>1 \leq b_i &lt; a_i \leq 100</var></li> <li> <var>1 \leq c_i \leq 100</var></li> </ul> <h3>出力形式</h3> <p>問題文中の理想的な選択の下で、好感度が高くない方のヒロインからの好感度の最大値を一行に出力せよ。</p> <h3>入力例1</h3> <pre> 3 4 3 2 2 2 1 1 2 1 1 </pre> <h3>出力例1</h3> <pre>5</pre> <p>最初の二つのイベントを片方のヒロインと、最後のイベントをもう片方のヒロインと行うのが最善です。</p> <h3>入力例2</h3> <pre> 3 3 3 2 2 2 1 1 2 1 1 </pre> <h3>出力例2</h3> <pre>4</pre>
p00653
<H1>Problem G: School of Killifish</H1> <p> Jon is the leader of killifish. Jon have a problem in these days. Jon has a plan to built new town in a pond. Of course new towns should have a school for children. But there are some natural enemies in a pond. Jon thinks that the place of a school should be the safest place in a pond for children. </p> <p> Jon has asked by some construction companies to make construction plans and Jon has <i> q </i> construction plans now. The plan is selected by voting. But for each plan, the safest place for the school should be decided before voting. </p> <p> A pond is expressed by a 2D grid whose size is <i>r*c</i>. The grid has <i>r</i> rows and <i>c</i> columns. The coordinate of the top-left cell is (0,0). The coordinate of the bottom-right cell is expressed as (<i>r</i>-1,<i>c</i>-1). Each cell has an integer which implies a dangerousness. The lower value implies safer than the higher value. <i>q</i> plans are given by four integer <i>r1</i> ,<i>c1</i> ,<i>r2</i> and <i>c2</i> which represent the subgrid. The top-left corner is (<i>r1</i>,<i>c1</i>) and the bottom-right corner is (<i>r2</i>,<i>c2</i>). </p> <p> You have the <i>r*c</i> grid and <i>q</i> plans. Your task is to find the safest point for each plan. You should find the lowest value in the subgrid. </p> <h2>Input</h2> <p> Input consists of multiple datasets.<br> Each dataset is given in the following format. </p> <pre> <i>r</i> <i>c</i> <i>q</i> <i>grid<sub>0,0</sub></i> <i>grid<sub>0,1</sub></i> ... <i>grid<sub>0,c-1</sub></i> ... <i>grid<sub>r-1,0</sub></i> ... <i>grid<sub>r-1,c-1</sub></i> <i>r1</i> <i>c1</i> <i>r2</i> <i>c2</i> ... <i>r1</i> <i>c1</i> <i>r2</i> <i>c2</i> </pre> <p> The first line consists of 3 integers, <i>r</i> ,<i>c</i> , and <i>q</i> . Next <i>r</i> lines have <i>c</i> integers. <i>grid<sub>i,j</sub></i> means a dangerousness of the grid. And you can assume 0 &le; <i>grid<sub>i,j</sub></i> &le; 2<sup>31</sup>-1. After that, <i>q</i> queries, <i>r1</i>, <i>c1</i>, <i>r2</i> and <i>c2</i>, are given. </p> <p> The dataset satisfies following constraints.<br> <i>r*c</i> &le; 10<sup>6</sup><br> <i>q</i> &le; 10<sup>4</sup><br> For each query, you can assume that <i>r1</i>&le; <i>r2</i> and <i>c1</i> &le; <i>c2</i> . If <i> r * c </i> &le; 250000, <i>q</i> &le; 100 is assured. If <i> r * c </i> &gt; 250000, Input consists of only that case. </p> <h2>Output</h2> <p> For each query you should print the lowest value in the subgrid in a line. </p> <H2>Sample input</H2> <pre> 3 3 4 1 2 3 4 2 2 2 1 1 0 0 2 2 0 0 1 1 1 0 1 0 0 1 1 2 1 10 4 1 2 3 4 5 6 7 8 9 10 0 0 0 9 0 0 0 4 0 5 0 9 0 4 0 5 0 0 0 </pre> <h2>Sample output</h2> <pre> 1 1 4 2 1 1 6 5 </pre>
p02394
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script> <H1>Circle in a Rectangle</H1> <p> Write a program which reads a rectangle and a circle, and determines whether the circle is arranged inside the rectangle. As shown in the following figures, the upper right coordinate $(W, H)$ of the rectangle and the central coordinate $(x, y)$ and radius $r$ of the circle are given. </p> <center> <br> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_circle_and_rectangle" title="Circle inside a rectangle" alt="Circle inside a rectangle"> <br> </center> <br> <H2>Input</H2> <p> Five integers $W$, $H$, $x$, $y$ and $r$ separated by a single space are given in a line. </p> <H2>Output</H2> <p> Print "Yes" if the circle is placed inside the rectangle, otherwise "No" in a line. </p> <h2>Constraints</h2> <ul> <li>$ -100 \leq x, y \leq 100$</li> <li>$ 0 < W, H, r \leq 100$</li> </ul> <H2>Sample Input 1</H2> <pre> 5 4 2 2 1 </pre> <H2>Sample Output 1</H2> <pre> Yes </pre> <H2>Sample Input 2</H2> <pre> 5 4 2 4 1 </pre> <H2>Sample Output 2</H2> <pre> No </pre>
p00203
<h1>会津山スキー場の新企画</h1> <p> 会津山スキー場の経営者である油木屋さんは、上級者向けとして障害物やジャンプ台を配置したコースを用意しました。コースにはいろいろな滑り方があり、シーズン中に全てのパターンの滑り方が出来た利用者には、プレゼントを贈ることになっています。 </p> <p> 油木屋さんのために、コースの見取り図をもとに滑り方のパターン数を出力するプログラムを作ってあげましょう。 </p> <!-- <p> しかし、オープン直前になって、油木屋さんは滑り方のパターン数を記録した書類をなくしてしま いました。どうにかして書類を作り直したい油木屋さんは、コースの見取り図を片手にあなたに助け を求めてきました。油木屋さんのために、コースの見取り図をもとに滑り方のパターン数を出力する プログラムを作ってあげましょう。 </p> --> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ski"><br/> </center> <br/> <p> コースは上図のような <var>X &times; Y</var> 個のマスからなるグリッドで表されます。左上を原点とし、x 座標は右に行くにつれて大きくなり、y 座標は下にいくにつれて大きくなるものとします。 </p> <p> 各滑り方のパターンは、最も高いところ(<var>y = 1</var> 、ただし障害物の無いところ) からスタートし、ゴール(<var>y = Y</var>) に向かって進んでいきます。グリッドのマス(<var>x, y</var>) にいる滑走者は、(<var>x &minus; 1, y + 1</var>)、(<var>x, y + 1</var>)、 (<var>x + 1, y + 1</var>) のいずれかに移動することができます。マスには、障害物やジャンプ台があり、障害物のあるマスには進入できず、ジャンプ台があるマスに進入すると(<var>x, y + 2</var>) へ移動します。ただし、いちばん高いマス (<var>y = 1</var> のマス) にはジャンプ台は存在せず、ジャンプ台のあるマスに進入する際には x 座標が同じマスからしか進入できません。コースの上端(<var>y = 1</var>) からスタートし、コースからはずれることなく下端を超えれば(<var>y &ge; Y</var>) 1つの滑り方とみなし、滑り終わります。 </p> <p> コースの情報を入力とし、滑り方の総数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 複数のデータセットの並びが入力として与えられます。入力の終わりはゼロふたつの行で示されます。各データセットは以下の形式で与えられます。 </p> <pre> <var>X</var> <var>Y</var> <var>c<sub>11</sub></var> <var>c<sub>21</sub></var> ... <var>c<sub>X1</sub></var> <var>c<sub>12</sub></var> <var>c<sub>22</sub></var> ... <var>c<sub>X2</sub></var> : <var>c<sub>1Y</sub></var> <var>c<sub>2Y</sub></var> ... <var>c<sub>XY</sub></var> </pre> <p> 1 行目にコースの大きさ<var>X, Y</var> (1 &le; <var>X, Y</var> &le; 15) が与えられます。続く <var>Y</var> 行にコースの情報が与えられます。<var>c<sub>ij</sub></var> (0 , 1 , 2 のいずれか) は<var>x = i, y = j</var> のマスの情報を表す整数で、0 が移動可能なマス、1 が障害物があるマス、2 がジャンプ台があるマスを表します。 </p> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> 入力データセットごとに、コースの滑り方のパターン数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 5 5 0 0 0 0 1 2 1 0 2 0 1 0 0 1 1 0 2 1 2 0 0 1 0 0 0 5 5 0 0 1 0 0 2 1 0 2 0 1 0 0 1 1 0 2 1 2 0 0 1 0 0 0 15 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 </pre> <H2>Output for the Sample Input</H2> <pre> 8 6 52694573 </pre>
p01042
<h1>Problem F: Sum of Numbers</h1> <h2>Problem</h2> <p> <var>a</var>以上<var>b</var>以下の整数が書かれたカードがそれぞれ<var>c</var>枚ずつあります。 これら<var>c(b-a+1)</var>枚のカードの中から<var>d</var>枚のカードを選択したとき、それらのカードに書かれている整数の合計が<var>e</var>になる場合の数を1,000,000,007で割った余りを求めてください。 </p> <h2>Input</h2> <p> 入力は以下の形式で与えられる。 </p> <pre> <var>a</var> <var>b</var> <var>c</var> <var>d</var> <var>e</var> </pre> <p> 1行目に、5つの整数<var>a</var> <var>b</var> <var>c</var> <var>d</var> <var>e</var>が空白区切りで与えられる。 </p> <h2>Constraints</h2> <p> 入力は以下の制約を満たす。 </p> <ul> <li>1 &le; <var>a</var> &le; 1000</li> <li><var>a</var> &le; <var>b</var> &le; 1000</li> <li>1 &le; <var>c</var> &le; 100</li> <li>1 &le; <var>d</var> &le; 1000</li> <li><var>c</var> &le; <var>d</var> &le; <var>c(b-a+1)</var></li> <li>1 &le; <var>e</var> &le; 20000</li> </ul> <h2>Output</h2> <p> 場合の数を1,000,000,007で割った余りを出力せよ。 </p> <h2>Sample Input1</h2> <pre> 2 5 3 3 11 </pre> <h2>Sample Output1</h2> <pre> 3 </pre> <p> {2,2,2,3,3,3,4,4,4,5,5,5}の中から3枚選び合計が11になるのは、 {2,4,5},{3,3,5},{3,4,4}の3通りである。 </p> <h2>Sample Input2</h2> <pre> 1 2 3 4 100 </pre> <h2>Sample Output2</h2> <pre> 0 </pre> <p> {1,1,1,2,2,2}の中から4枚選び合計が100になるのは0通りである。 </p>
p03585
<span class="lang-en"> <p>Score : <var>800</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> lines in the <var>xy</var>-plane. The <var>i</var>-th line is represented by <var>A_ix+B_iy=C_i</var>. Any two lines among the <var>N+2</var> lines, the above <var>N</var> lines plus the <var>x</var>-axis and <var>y</var>-axis, cross each other at exactly one point.</p> <p>For each pair <var>1 \leq i &lt; j \leq N</var>, there is a car at the cross point of the <var>i</var>-th and <var>j</var>-th lines. Even where three or more lines intersect at a point, a car is individually placed for each pair of lines. That is, there will be <var>k(k-1)/2</var> cars placed at the intersection of <var>k</var> lines.</p> <p>Those cars are already very old, and can only be moved parallel to the <var>x</var>-axis or <var>y</var>-axis.</p> <p>Takahashi will hold an exhibition of antique cars at a place on the <var>xy</var>-plane. In order to avoid damaging the half-broken cars too much, he will select the place of the exhibition so that the total distance covered will be minimized when all the cars are moved to the place. If such a place is not uniquely determined, among the places that satisfy the condition above, the place with the minimum <var>x</var>-coordinate will be selected. If the place is still not uniquely determined, among the places that satisfy the two conditions above, the place with the minimum <var>y</var>-coordinate will be selected.</p> <p>Find the place of the exhibition that will be selected.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 4 × 10^4</var></li> <li><var>1 \leq |A_i|,|B_i| \leq 10^4(1 \leq i \leq N)</var></li> <li><var>0 \leq |C_i| \leq 10^4(1 \leq i \leq N)</var></li> <li>No two given lines are parallel.</li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Inputs</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>C_1</var> : <var>A_N</var> <var>B_N</var> <var>C_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Outputs</h3><p>Print the <var>x</var>-coordinate and <var>y</var>-coordinate of the place of the exhibition that will be selected, in this order, with a space in between. The output will be judged as correct when the absolute or relative error is at most <var>10^{-9}</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 2 -1 2 -1 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1.000000000000000 1.000000000000000 </pre> <p><img alt="" src="https://img.atcoder.jp/tenka1-2017/650fff486341589a0388efff446acf0a.png"/></p> <p>There is a car at each place shown by a blue circle in the figure. The place to be selected is shown by a purple circle.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 1 1 2 1 -1 0 3 -1 -2 1 -3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>-1.000000000000000 -1.000000000000000 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>7 1 7 8 -2 4 9 3 -8 -5 9 2 -14 6 7 5 -8 -9 3 3 8 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>-1.722222222222222 1.325000000000000 </pre></section> </div> </span>
p01412
<H1>F: Power of Power</H1> <p> ICPC で良い成績を収めるには修行が欠かせない.うさぎは ICPC で勝ちたいので,今日も修行をすることにした. </p> <p> 今日の修行は,非常に大きな数の計算を行って,計算力をつけて意識を高めようというものである.大きい数が簡単に出てくる演算として,累乗が挙げられる. </p> <p> <i>N</i> 個の非負整数 <i>A</i><sub>1</sub>, <i>A</i><sub>2</sub>, ..., <i>A</i><sub><i>N</i></sub> がある.これらの並べ替え <i>B</i><sub>1</sub>, <i>B</i><sub>2</sub>, ..., <i>B</i><sub><i>N</i></sub> であって,<i>B</i><sub>1</sub><sup><i>B</i><sub>2</sub><sup>...<sup><i>B</i><sub><i>N</i> - 1</sub><sup><i>B</i><sub><i>N</i></sub></sup></sup></sup></sup> が最大になるものを求めたい.うさぎたちにとってはもちろん常識であるが,この計算は右上から順に行う.また,0<sup>0</sup> = 1 であると約束することにする. </p> <p> 最大条件を満たす <i>B</i><sub>1</sub>, <i>B</i><sub>2</sub>, ..., <i>B</i><sub><i>N</i></sub> は複数通りあるかもしれない.そのような場合は,そのうちで辞書順最小の列を選ぶことにしよう. </p> <H2>Input</H2> <pre> <i>N</i> <i>A</i><sub>1</sub> ... <i>A</i><sub><i>N</i></sub> </pre> <p> 1 &le; <i>N</i> &le; 100,0 &le; <i>A</i><sub><i>i</i></sub> &le; 1,000,000,000 を満たす. </p> <H2>Output</H2> <p> 出力は <i>N</i> 行からなる.<i>i</i> 行目には <i>B</i><sub><i>i</i></sub> を出力せよ. </p> <H2>Sample Input 1</H2> <pre> 4 7 5 10 6 </pre> <H2>Sample Output 1</H2> <pre> 5 6 7 10 </pre> <H2>Sample Input 2</H2> <pre> 3 0 0 1000000000 </pre> <H2>Sample Output 2</H2> <pre> 1000000000 0 0 </pre>
p02117
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <h1>Problem G: Picnic</h1> <h2>Problem</h2> <p> 明日はいよいよマイヅ小学校の遠足の日である。マイヅ小学校に通うがっちょ君は、楽しみのあまり明日のお菓子を買い忘れていたことに気づいた。 がっちょ君は遠足を最大限楽しむため、お菓子にもこだわりたい。<br> </p> <p> がっちょ君はお母さんから$X$円のお小遣いをもらってお菓子を買いに行く。ただし、小学校のルールで遠足に持っていくお菓子の合計金額は$Y$円までと決まっており、$Y$円を上回るとお菓子は全て先生に取り上げられてしまう。 </p> <p> がっちょ君の住む学区には$N$個の町があり、各町には1軒の駄菓子屋がある。各町には1から$N$の番号が割り振られており、がっちょ君は町1に住んでいる。 各駄菓子屋ではいくつかのお菓子が売っており、1つあたりの値段、1つ買うごとにがっちょ君が得られる満足度が決まっている。ただし、各お菓子の在庫数には限りがある。 また、がっちょ君は2つの町の間を移動するとき公共交通機関を利用するため、町$i$から町$j$へ直接移動するには$d_{i,j}$円だけお金がかかる。 </p> <p> 最初、がっちょ君は町1にいる。がっちょ君は移動にかかる費用の合計と買ったお菓子の値段の合計の和が$X$円以内で、かつ、買ったお菓子の値段の合計が$Y$円以内になるようにお菓子を買いに行く。最後には町1に到着している必要がある。がっちょ君は買ったお菓子の満足度の合計ができるだけ大きくなるようにしたい。がっちょ君が最適な行動をしたときの満足度の合計を求めよ。 </p> <h2>Input</h2> <p>入力は以下の形式ですべて整数で与えられる。</p> <p> $N$ $X$ $Y$<br> 町1の駄菓子屋の情報<br> 町2の駄菓子屋の情報<br> ... <br> 町$N$の駄菓子屋の情報<br> $d_{1,1}$ $d_{1,2}$ ... $d_{1,N}$<br> $d_{2,1}$ $d_{2,2}$ ... $d_{2,N}$<br> ...<br> $d_{N,1}$ $d_{N,2}$ ... $d_{N,N}$<br> </p> <p> 1行目に町の数$N$と所持金$X$、お菓子を買うために使うことができる上限金額$Y$が空白区切りで与えられる。<br> 2行目からは$N$個の各駄菓子屋の情報が与えられる。<br> 続く$N$行$N$列に町$i$と町$j$を直接行き来するために必要な金額$d_{i,j}$が空白区切りで与えられる。<br> </p> <p>各町の駄菓子屋の情報は以下の形式で与えられる。</p> <p> $K$<br> $a_1$ $b_1$ $c_1$<br> $a_2$ $b_2$ $c_2$<br> ...<br> $a_K$ $b_K$ $c_K$<br> </p> <p> 1行目にその駄菓子屋で売っているお菓子の種類の数$K$が与えられる。続く$K$行にお菓子の1つの値段$a_i$、1つあたりの満足度$b_i$、在庫数$c_i$が空白区切りで与えられる。 </p> <h2>Constraints</h2> <p>入力は以下の条件を満たす。</p> <ul> <li>$1 \leq N \leq 14$</li> <li>$1 \leq X \leq 10000$</li> <li>$1 \leq Y \leq min(1000,X)$</li> <li>$1 \leq K \leq 300$</li> <li>$1 \leq a_i \leq 1000$</li> <li>$1 \leq b_i \leq 1000$</li> <li>$1 \leq c_i \leq 1000$</li> <li>$0 \leq d_{i,j} \leq 10000$</li> <li>$d_{i,i} = 0$</li> </ul> <h2>Output</h2> <p> 満足度の合計の最大値を1行に出力する。 </p> <h2>Sample Input 1</h2> <pre> 1 10 10 3 1 10 1 2 20 2 3 30 3 0 </pre> <h2>Sample Output 1</h2> <pre> 100 </pre> <h2>Sample Input 2</h2> <pre> 2 10 10 3 1 10 1 2 20 2 3 30 3 1 5 200 1 0 2 3 0 </pre> <h2>Sample Output 2</h2> <pre> 200 </pre> <h2>Sample Input 3</h2> <pre> 3 10 10 1 1 1 1 1 3 3 3 1 5 5 5 0 1 0 1 0 0 0 1 0 </pre> <h2>Sample Output 3</h2> <pre> 10 </pre> <h2>Sample Input 4</h2> <pre> 4 59 40 1 7 6 3 1 10 3 9 2 9 8 5 7 6 10 4 8 2 9 1 7 1 7 7 9 1 2 3 0 28 7 26 14 0 10 24 9 6 0 21 9 24 14 0 </pre> <h2>Sample Output 4</h2> <pre> 34 </pre>
p02547
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Tak performed the following action <var>N</var> times: rolling two dice. The result of the <var>i</var>-th roll is <var>D_{i,1}</var> and <var>D_{i,2}</var>.</p> <p>Check if doublets occurred at least three times in a row. Specifically, check if there exists at lease one <var>i</var> such that <var>D_{i,1}=D_{i,2}</var>, <var>D_{i+1,1}=D_{i+1,2}</var> and <var>D_{i+2,1}=D_{i+2,2}</var> hold.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>3 \leq N \leq 100</var></li> <li><var>1\leq D_{i,j} \leq 6</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>D_{1,1}</var> <var>D_{1,2}</var> <var>\vdots</var> <var>D_{N,1}</var> <var>D_{N,2}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <code>Yes</code> if doublets occurred at least three times in a row. Print <code>No</code> otherwise.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 1 2 6 6 4 4 3 3 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes </pre> <p>From the second roll to the fourth roll, three doublets occurred in a row.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 1 1 2 2 3 4 5 5 6 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>No </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 1 1 2 2 3 3 4 4 5 5 6 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Yes </pre></section> </div> </span>
p00080
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML"> </script> <H1>3乗根</H1> <p> $x^3 = q$ の解は漸化式 $x_{n+1} = x_n - \frac{x_{n}^3 - q}{3x_{n}^2}$ を計算していくことで近似的に求めることができます。</p> <p> $x_1$ に正の数 $\frac{q}{2}$ をいれ </p> <p> $x_2 = x_1 - \frac{x_{1}^3 - q}{3x_{1}^2}$、$x_3 = x_2 - \frac{x_{2}^3 - q}{3x_{2}^2}$、… と計算します。</p> <p> この計算をしながら、 </p> <p> $|x^3 - q|$ の値が、十分小さくなったところで、計算をやめ、最後に計算した $x_n$ を $x^3 = q$ の近似解とします。</p> <!-- <p> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_08_1"> の解は漸化式 <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_08_2"> を計算していくことで近似的に求めることができます。</p> <p> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_08_3">に正の数 <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_08_4">をいれ </p> <p> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_08_5"> 、<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_08_6"> 、… と計算します。</p> <p> この計算をしながら、 </p> <p> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_08_7"> の値が、十分小さくなったところで、計算をやめ、最後に計算した <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_08_8">を </p> <p><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_08_9">の近似解とします。</p> --> <p> この方法に従って、入力された正の整数 $q$ に対し、 $q$ の3乗根の近似値を出力するプログラムを作成してください。ただし、「十分小さくなった」という判定は $|x^3 - q| < 0.00001 q$ を用いてください。 </p> <H2>入力</H2> <p>複数のデータセットが与えられる。各データセットに $q$ ($1 \leq q < 2^{31}$)(整数)が一行に与えられる。入力の終わりは -1 である。 </p> <p> データセットの数は 50 を超えない。 </p> <H2>出力</H2> <p>各データセットに対して $x$ (実数)を1行に出力する。出力結果に 0.00001 以下の誤差を含んでもよい。 </p> <H2>Sample Input</H2> <pre> 15 15 -1 </pre> <H2>Output for the Sample Input</H2> <pre> 2.466212 2.466212 </pre>
p03706
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Skenu constructed a building that has <var>N</var> floors. The building has an elevator that stops at every floor.</p> <p>There are buttons to control the elevator, but Skenu thoughtlessly installed only one button on each floor - up or down. This means that, from each floor, one can only go in one direction. If <var>S_i</var> is <code>U</code>, only "up" button is installed on the <var>i</var>-th floor and one can only go up; if <var>S_i</var> is <code>D</code>, only "down" button is installed on the <var>i</var>-th floor and one can only go down.</p> <p>The residents have no choice but to go to their destination floors via other floors if necessary. Find the sum of the following numbers over all ordered pairs of two floors <var>(i,j)</var>: the minimum number of times one needs to take the elevator to get to the <var>j</var>-th floor from the <var>i</var>-th floor.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ |S| ≤ 10^5</var></li> <li><var>S_i</var> is either <code>U</code> or <code>D</code>.</li> <li><var>S_1</var> is <code>U</code>.</li> <li><var>S_{|S|}</var> is <code>D</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>S_1S_2...S_{|S|}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the sum of the following numbers over all ordered pairs of two floors <var>(i,j)</var>: the minimum number of times one needs to take the elevator to get to the <var>j</var>-th floor from the <var>i</var>-th floor.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>UUD </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>7 </pre> <p>From the <var>1</var>-st floor, one can get to the <var>2</var>-nd floor by taking the elevator once.</p> <p>From the <var>1</var>-st floor, one can get to the <var>3</var>-rd floor by taking the elevator once.</p> <p>From the <var>2</var>-nd floor, one can get to the <var>1</var>-st floor by taking the elevator twice.</p> <p>From the <var>2</var>-nd floor, one can get to the <var>3</var>-rd floor by taking the elevator once.</p> <p>From the <var>3</var>-rd floor, one can get to the <var>1</var>-st floor by taking the elevator once.</p> <p>From the <var>3</var>-rd floor, one can get to the <var>2</var>-nd floor by taking the elevator once.</p> <p>The sum of these numbers of times, <var>7</var>, should be printed.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>UUDUUDUD </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>77 </pre></section> </div> </span>
p02814
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given are a sequence <var>A= {a_1,a_2,......a_N}</var> of <var>N</var> positive even numbers, and an integer <var>M</var>.</p> <p>Let a <em>semi-common multiple</em> of <var>A</var> be a positive integer <var>X</var> that satisfies the following condition for every <var>k</var> <var>(1 \leq k \leq N)</var>:</p> <ul> <li>There exists a non-negative integer <var>p</var> such that <var>X= a_k \times (p+0.5)</var>.</li> </ul> <p>Find the number of semi-common multiples of <var>A</var> among the integers between <var>1</var> and <var>M</var> (inclusive).</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 10^5</var></li> <li><var>1 \leq M \leq 10^9</var></li> <li><var>2 \leq a_i \leq 10^9</var></li> <li><var>a_i</var> is an even number.</li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of semi-common multiples of <var>A</var> among the integers between <var>1</var> and <var>M</var> (inclusive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 50 6 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <ul> <li><var>15 = 6 \times 2.5 </var></li> <li><var>15 = 10 \times 1.5 </var></li> <li><var>45 = 6 \times 7.5 </var></li> <li><var>45 = 10 \times 4.5 </var></li> </ul> <p>Thus, <var>15</var> and <var>45</var> are semi-common multiples of <var>A</var>. There are no other semi-common multiples of <var>A</var> between <var>1</var> and <var>50</var>, so the answer is <var>2</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 100 14 22 40 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>The answer can be <var>0</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 1000000000 6 6 2 6 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>166666667 </pre></section> </div> </span>
p00829
<H1><font color="#000">Problem C:</font> Leaky Cryptography</H1> <p> The ACM ICPC judges are very careful about not leaking their problems, and all communications are encrypted. However, one does sometimes make mistakes, like using too weak an encryption scheme. Here is an example of that. </p> <p> The encryption chosen was very simple: encrypt each chunk of the input by flipping some bits according to a shared key. To provide reasonable security, the size of both chunk and key is 32 bits. </p> <p> That is, suppose the input was a sequence of <i>m</i> 32-bit integers. </p> <center> <p> <i>N</i><sub>1</sub> <i>N</i><sub>2</sub> <i>N</i><sub>3</sub> ... <i>N<sub>m</sub></i> </p> </center> <p> After encoding with the key <i>K</i> it becomes the following sequence of <i>m</i> 32-bit integers. </p> <center> <p> (<i>N</i><sub>1</sub> &and; <i>K</i>) (<i>N</i><sub>2</sub> &and; <i>K</i>) (<i>N</i><sub>3</sub> &and; <i>K</i>) ... (<i>N</i><sub><i>m</i></sub> &and; <i>K</i>) </p> </center> <p> where (<i>a</i> &and; <i>b</i>) is the bitwise <i>exclusive or</i> of <i>a</i> and <i>b</i>. </p> <p> Exclusive or is the logical operator which is 1 when only one of its operands is 1, and 0 otherwise. Here is its definition for 1-bit integers. </p> <center> <pre> 0 &oplus; 0 = 0 0 &oplus; 1 = 1 1 &oplus; 0 = 1 1 &oplus; 1 =0 </pre> </center> <p> As you can see, it is identical to addition modulo 2. For two 32-bit integers <i>a</i> and <i>b</i>, their bitwise exclusive or <i>a</i> &and; <i>b</i> is defined as follows, using their binary representations, composed of 0's and 1's. </p> <center> <p> <i>a</i> &and; <i>b</i> = <i>a</i><sub>31</sub> ... <i>a</i><sub>1</sub><i>a</i><sub>0</sub> &and; <i>b</i><sub>31</sub> ... <i>b</i><sub>1</sub><i>b</i><sub>0</sub> = <i>c</i><sub>31</sub> ... <i>c</i><sub>1</sub><i>c</i><sub>0</sub> </p> </center> <p> where </p> <center> <p> <i>c<sub>i</sub></i> = <i>a<sub>i</sub></i> &oplus; <i>b<sub>i</sub></i> (<i>i</i> = 0, 1, ... , 31). </p> </center> <p> For instance, using binary notation, 11010110 &and; 01010101 = 10100011, or using hexadecimal, </p> <pre> d6 &and; 55 = a3. </pre> <p> Since this kind of encryption is notoriously weak to statistical attacks, the message has to be compressed in advance, so that it has no statistical regularity. We suppose that <i>N</i><sub>1</sub> <i>N</i><sub>2</sub> ... <i>N<sub>m</sub></i> is already in compressed form. </p> <p> However, the trouble is that the compression algorithm itself introduces some form of regularity: after every 8 integers of compressed data, it inserts a checksum, the sum of these integers. That is, in the above input, <i>N</i><sub>9</sub> = &sum;<sup>8</sup><sub><i>i</i>=1</sub> <i>N<sub>i</sub></i> = <i>N</i><sub>1</sub> + ... + <i>N</i><sub>8</sub>, where additions are modulo 2<sup>32</sup>. </p> <p> Luckily, you could intercept a communication between the judges. Maybe it contains a problem for the finals! </p> <p> As you are very clever, you have certainly seen that you can easily find the lowest bit of the key, denoted by <i>K</i><sub>0</sub>. On the one hand, if <i>K</i><sub>0</sub> = 1, then after encoding, the lowest bit of &sum;<sup>8</sup><sub><i>i</i>=1</sub> <i>N<sub>i</sub></i> &and; <i>K</i> is unchanged, as <i>K</i><sub>0</sub> is added an even number of times, but the lowest bit of <i>N</i><sub>9</sub> &and; <i>K</i> is changed, so they shall differ. On the other hand, if <i>K</i><sub>0</sub> = 0, then after encoding, the lowest bit of &sum;<sup>8</sup><sub><i>i</i>=1</sub> <i>N<sub>i</sub></i> &and; <i>K</i> shall still be identical to the lowest bit of <i>N</i><sub>9</sub> &and; <i>K</i>, as they do not change. For instance, if the lowest bits after encoding are 1 1 1 1 1 1 1 1 1 then <i>K</i><sub>0</sub> must be 1, but if they are 1 1 1 1 1 1 1 0 1 then <i>K</i><sub>0</sub> must be 0. </p> <p> So far, so good. Can you do better? </p> <p> You should find the key used for encoding. </p> <H2>Input</H2> <p> The input starts with a line containing only a positive integer <i>S</i>, indicating the number of datasets in the input. <i>S</i> is no more than 1000. </p> <p> It is followed by <i>S</i> datasets. Each dataset is composed of nine 32-bit integers corresponding to the first nine chunks of a communication. They are written in hexadecimal notation, using digits ‘0’ to ‘9’ and lowercase letters ‘a’ to ‘f’, and with no leading zeros. They are separated by a space or a newline. Each dataset is ended by a newline. </p> <H2>Output</H2> <p> For each dataset you should output the key used for encoding. Each key shall appear alone on its line, and be written in hexadecimal notation, using digits ‘0’ to ‘9’ and lowercase letters ‘a’ to ‘f’, and with no leading zeros. </p> <H2>Sample Input</H2> <pre> 8 1 1 1 1 1 1 1 1 8 3 2 3 2 3 2 3 2 6 3 4 4 7 7 b a 2 2e e1 13 ce 28 ca 6 ab 46 a6d b08 49e2 6128 f27 8cf2 bc50 7380 7fe1 723b 4eba eb4 a352 fd14 6ac1 eed1 dd06 bb83 392bc ef593c08 847e522f 74c02b9c 26f3a4e1 e2720a01 6fe66007 7a4e96ad 6ee5cef6 3853cd88 60202fb8 757d6d66 9c3a9525 fbcd7983 82b9571c ddc54bab 853e52da 22047c88 e5524401 </pre> <H2>Output for the Sample Input</H2> <pre> 0 2 6 1c6 4924afc7 ffff95c5 546991d 901c4a16 </pre>
p03356
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a permutation of the integers from <var>1</var> through <var>N</var>, <var>p_1</var>, <var>p_2</var>, .., <var>p_N</var>. We also have <var>M</var> pairs of two integers between <var>1</var> and <var>N</var> (inclusive), represented as <var>(x_1,y_1)</var>, <var>(x_2,y_2)</var>, .., <var>(x_M,y_M)</var>. AtCoDeer the deer is going to perform the following operation on <var>p</var> as many times as desired so that the number of <var>i</var> (<var>1</var> <var>≤</var> <var>i</var> <var>≤</var> <var>N</var>) such that <var>p_i = i</var> is maximized:</p> <ul> <li>Choose <var>j</var> such that <var>1</var> <var>≤</var> <var>j</var> <var>≤</var> <var>M</var>, and swap <var>p_{x_j}</var> and <var>p_{y_j}</var>.</li> </ul> <p>Find the maximum possible number of <var>i</var> such that <var>p_i = i</var> after operations.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2</var> <var>≤</var> <var>N</var> <var>≤</var> <var>10^5</var></li> <li><var>1</var> <var>≤</var> <var>M</var> <var>≤</var> <var>10^5</var></li> <li><var>p</var> is a permutation of integers from <var>1</var> through <var>N</var>.</li> <li><var>1</var> <var>≤</var> <var>x_j,y_j</var> <var>≤</var> <var>N</var></li> <li><var>x_j</var> <var>≠</var> <var>y_j</var></li> <li>If <var>i</var> <var>≠</var> <var>j</var>, <var>\{x_i,y_i\}</var> <var>≠</var> <var>\{x_j,y_j\}</var>.</li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>p_1</var> <var>p_2</var> <var>..</var> <var>p_N</var> <var>x_1</var> <var>y_1</var> <var>x_2</var> <var>y_2</var> <var>:</var> <var>x_M</var> <var>y_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum possible number of <var>i</var> such that <var>p_i = i</var> after operations.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 2 5 3 1 4 2 1 3 5 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>If we perform the operation by choosing <var>j=1</var>, <var>p</var> becomes <code>1 3 5 4 2</code>, which is optimal, so the answer is <var>2</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 2 3 2 1 1 2 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 </pre> <p>If we perform the operation by, for example, choosing <var>j=1</var>, <var>j=2</var>, <var>j=1</var> in this order, <var>p</var> becomes <code>1 2 3</code>, which is obviously optimal. Note that we may choose the same <var>j</var> any number of times.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>10 8 5 3 6 8 7 10 9 1 2 4 3 1 4 1 5 9 2 5 6 5 3 5 8 9 7 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>8 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>5 1 1 2 3 4 5 1 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>5 </pre> <p>We do not have to perform the operation. </p></section> </div> </span>