question_id
stringlengths
6
6
content
stringlengths
1
27.2k
p01878
<h2>G: 運命力 - Destiny Draw -</h2> <h3>問題</h3> <p> Mr. D はカードゲームで勝負をすることになった。 このカードゲームでは、 <var>N</var> 枚のカードの山を用いる。 また、山のカードには、上から順に <var>1, 2, 3,</var> ... <var>, N</var> の番号がついている。 </p> <p> D で始まるものすべてを背負う彼に敗北は許されないが、不幸にも Mr. D はカードゲームが不得手である。 そこで、自分が引くカードをコントロールすることで勝利を手にすることにした。 </p> <p> Mr. D は <var>K</var> 種類のシャッフルが可能である。 <var>K</var> 種類のうち <var>i</var> 番目のシャッフルでは上から <var>a_i</var> 枚目から <var>a_i+b_i &minus; 1</var>枚目までのちょうど <var>b_i</var> 枚を引き抜き、上に重ねる。 <var>i</var>番目のシャッフルを<var>1</var> 回するとき、それぞれ <var>t_i</var> 秒を要する。 </p> <p>ちょうど <var>T</var> 秒のシャッフルの後、一番上のカードを <var>C</var> にする方法は何通りあるか。 数が多くなる可能性があるので、 <var>10^9+7</var> で割った余りを出力せよ。</p> <h3>入力形式</h3> <p>入力は次の形式で与えられる。</p> <pre> <var>N</var> <var>K</var> <var>C</var> <var>T</var> <var>a_1</var> <var>b_1</var> <var>t_1</var> <var>a_2</var> <var>b_2</var> <var>t_2</var> ... <var>a_K</var> <var>b_K</var> <var>t_K</var> </pre> <ul> <li><var>N</var> は整数で <var>2 \&le; N \&le; 40</var> を満たす。</li> <li><var>K</var> は整数で <var>1 \&le; K \&le; \frac{N (N+1)}{2}</var> を満たす</li> <li><var>C</var> は整数で <var>1 \&le; C \&le; N</var> を満たす</li> <li><var>T</var> は整数で <var>1 \&le; T \&le; 1,000,000</var> を満たす</li> <li><var>a_i</var> (<var>i = 1, 2,</var> ... <var>, K</var>) は整数で <var>1 \&le; a_i \&le; N</var> を満たす</li> <li><var>b_i</var> (<var>i = 1, 2,</var> ... <var>, K</var>) は整数で <var>1 \&le; b_i \&le; N &minus; a_i + 1</var> を満たす</li> <li><var>a_i = a_j</var> かつ <var>b_i = b_j</var> を満たすとき <var>i = j</var> に限定される</li> <li><var>t_i</var> は整数で <var>1 \&le; t_i \&le; 5</var> を満たす</li> </ul> <h3>出力形式</h3> <p>答えを <var>10^9+7</var> で割った余りを一行で出力せよ。</p> <h3>入力例1</h3> <pre> 4 1 1 6 3 2 3 </pre> <h3>出力例1</h3> <pre>1</pre> <h3>入力例2</h3> <pre> 4 1 1 5 3 2 3 </pre> <h3>出力例2</h3> <pre>0</pre> <h3>入力例3</h3> <pre> 6 2 2 5 1 2 1 2 5 3 </pre> <h3>出力例3</h3> <pre>3</pre> <p>ちょうど <var>5</var> 秒で一番上のカードを <var>2</var> にする方法は以下の <var>3</var> 通りである。</p> <pre> 1→1→2 1→2→1 2→1→1 </pre> <h3>入力例4</h3> <pre> 6 8 3 10 1 4 5 1 3 3 1 6 5 1 2 2 1 1 4 2 5 1 4 3 1 2 1 3 </pre> <h3>出力例4</h3> <pre>3087</pre>
p02757
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi has a string <var>S</var> of length <var>N</var> consisting of digits from <code>0</code> through <code>9</code>.</p> <p>He loves the prime number <var>P</var>. He wants to know how many non-empty (contiguous) substrings of <var>S</var> - there are <var>N \times (N + 1) / 2</var> of them - are divisible by <var>P</var> when regarded as integers written in base ten.</p> <p>Here substrings starting with a <code>0</code> also count, and substrings originated from different positions in <var>S</var> are distinguished, even if they are equal as strings or integers.</p> <p>Compute this count to help Takahashi.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 2 \times 10^5</var></li> <li><var>S</var> consists of digits.</li> <li><var>|S| = N</var></li> <li><var>2 \leq P \leq 10000</var></li> <li><var>P</var> is a prime number.</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>P</var> <var>S</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of non-empty (contiguous) substrings of <var>S</var> that are divisible by <var>P</var> when regarded as an integer written in base ten.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 3 3543 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>6 </pre> <p>Here <var>S</var> = <code>3543</code>. There are ten non-empty (contiguous) substrings of <var>S</var>:</p> <ul> <li> <p><code>3</code>: divisible by <var>3</var>.</p> </li> <li> <p><code>35</code>: not divisible by <var>3</var>.</p> </li> <li> <p><code>354</code>: divisible by <var>3</var>.</p> </li> <li> <p><code>3543</code>: divisible by <var>3</var>.</p> </li> <li> <p><code>5</code>: not divisible by <var>3</var>.</p> </li> <li> <p><code>54</code>: divisible by <var>3</var>.</p> </li> <li> <p><code>543</code>: divisible by <var>3</var>.</p> </li> <li> <p><code>4</code>: not divisible by <var>3</var>.</p> </li> <li> <p><code>43</code>: not divisible by <var>3</var>.</p> </li> <li> <p><code>3</code>: divisible by <var>3</var>.</p> </li> </ul> <p>Six of these are divisible by <var>3</var>, so print <var>6</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 2 2020 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>10 </pre> <p>Here <var>S</var> = <code>2020</code>. There are ten non-empty (contiguous) substrings of <var>S</var>, all of which are divisible by <var>2</var>, so print <var>10</var>.</p> <p>Note that substrings beginning with a <code>0</code> also count.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>20 11 33883322005544116655 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>68 </pre></section> </div> </span>
p00290
<h1>椅子の総数</h1> <p> 選手のみなさん、パソコン甲子園にようこそ。パソコン甲子園の本選は会津大学で行われ、会場内では 1つの机に1チームが割り当てられます。パソコン甲子園は1チーム2人なので、チーム数×2脚の椅子が必要です。大学では、他にも様々なイベントの会場設営で机と椅子を準備する機会がありますが、必要な机と椅子の数も様々です。そこで、あるイベントに対して準備する机の数と、机1つあたりに必要な椅子の数が与えられたとき、必要な椅子の総数を計算するプログラムを作成してください。 </p> <h2>入力</h2> <p> 入力は以下の形式で与えられる。 </p> <pre> <var>d</var> <var>c</var> </pre> <p> 入力は1行であり、必要な机の数 <var>d</var> (1 &le; <var>d</var> &le; 100) と机1つあたりに必要な椅子の数 <var>c</var> (1 &le; <var>c</var> &le; 10) を表す整数が与えられる。 </p> <h2>出力</h2> <p> イベントにおいて必要な椅子の総数を1行に出力する。 </p> <h2>入出力例</h2> <br> <h2>入力例1</h2> <pre> 20 2 </pre> <h2> 出力例1</h2> <pre> 40 </pre> <h2>入力例2</h2> <pre> 1 1 </pre> <h2> 出力例2</h2> <pre> 1 </pre>
p02307
<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>Circumscribed Circle of a Triangle</H1> <p> Write a program which prints the central coordinate ($cx$,$cy$) and the radius $r$ of a circumscribed circle of a triangle which is constructed by three points ($x_1$, $y_1$), ($x_2$, $y_2$) and ($x_3$, $y_3$) on the plane surface. </p> <h2>Input</h2> <p>The input is given in the following format</p> <pre> $x_1$ $y_1$ $x_2$ $y_2$ $x_3$ $y_3$ </pre> <p> All the input are integers. </p> <h2>Constraints</h2> <ul> <li>$-10000 \leq x_i, y_i \leq 10000$</li> <li>The three points are not on the same straight line</li> </ul> <h2>Output</h2> <p> Print $cx$, $cy$ and $r$ separated by a single space in a line. The output val ues should be in a decimal fraction with an error less than 0.000001. </p> <h2>Sample Input 1</h2> <pre> 1 -2 3 2 -2 0 </pre> <h2>Sample Output 1</h2> <pre> 0.62500000000000000000 0.68750000000000000000 2.71353666826155124291 </pre> <h2>Sample Input 2</h2> <pre> 0 3 4 0 0 0 </pre> <h2>Sample Output 2</h2> <pre> 2.00000000000000000000 1.50000000000000000000 2.50000000000000000000 </pre>
p03146
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>A sequence <var>a=\{a_1,a_2,a_3,......\}</var> is determined as follows:</p> <ul> <li> <p>The first term <var>s</var> is given as input.</p> </li> <li> <p>Let <var>f(n)</var> be the following function: <var>f(n) = n/2</var> if <var>n</var> is even, and <var>f(n) = 3n+1</var> if <var>n</var> is odd.</p> </li> <li> <p><var>a_i = s</var> when <var>i = 1</var>, and <var>a_i = f(a_{i-1})</var> when <var>i &gt; 1</var>.</p> </li> </ul> <p>Find the minimum integer <var>m</var> that satisfies the following condition:</p> <ul> <li>There exists an integer <var>n</var> such that <var>a_m = a_n (m &gt; n)</var>.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq s \leq 100</var></li> <li>All values in input are integers.</li> <li>It is guaranteed that all elements in <var>a</var> and the minimum <var>m</var> that satisfies the condition are at most <var>1000000</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> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum integer <var>m</var> that satisfies the condition.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>5 </pre> <p><var>a=\{8,4,2,1,4,2,1,4,2,1,......\}</var>. As <var>a_5=a_2</var>, the answer is <var>5</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>18 </pre> <p><var>a=\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\}</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>54 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>114 </pre></section> </div> </span>
p01481
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <body> <h1> Lucky Dip</h1> <p> Time Limit: 8 sec / Memory Limit: 64 MB </p> <h2> C: 福袋</h2> <p> 福袋,それは年始を祝う商習慣の一つである. 人気の福袋には多くの人が開店前から行列を作り,ターゲットの福袋へダッシュする. この風物詩に魅了されたあなたは,今年もお目当ての福袋を狙い,計画を立てていた. </p> <p> しかし,お客を呼び寄せる側の某百貨店の店長は頭を悩ませていた. どうやらフロアの準備が開店に間に合いそうにない. </p> <p> そこで店長はある一つのアイディアを思いついた. それは開店後,店内を徐々に開放すればなんとか営業できるのではないかというものである. </p> <p> あなたは刻一刻と変化する店内の情報を元に,お目当ての福袋がどの時間帯から入手できるのかを調べたい. </p> <h2> Input</h2> <p> 入力は単一のデータセットで与えられ,EOFで終了する.<br/> <br/> 店内の様子は各行が <i>W</i> 文字からなる <i>H</i> 行の文字列で表される.<br/> <br/> 1 行目<br/> </p> <pre> <i>W H</i> 2 &lt; <i>W</i>, <i>H</i> &lt;= 1000 </pre> <p> <br/> 続く <i>H</i> 行<br/> </p> <pre> 店内の様子 </pre> <ul> <li> .: 床</li> <li> #: 壁</li> <li> <i>t</i>: 目的の福袋</li> </ul> <p> 目的の福袋は店内に必ず1つだけ存在する.<br/> <br/> <i>H</i> + 2 行目<br/> </p> <pre> 営業時間 <i>N</i> (0 &lt;= <i>N</i> &lt;= 1000) </pre> <p> <br/> 続く <i>N</i> 行<br/> </p> <pre> <i>x<sub>1</sub></i> <i>y<sub>1</sub></i> <i>x<sub>2</sub></i> <i>x<sub>2</sub></i> ... <i>x<sub>i</sub></i> <i>y<sub>i</sub></i> ... <i>x<sub>n</sub></i> <i>y<sub>n</sub></i> </pre> <p> (<i>x<sub>i</sub></i>, <i>y<sub>i</sub></i>)は,<i>i</i> 時間目に開放される壁の座標を表す.(0 &lt;= <i>x<sub>i</sub></i> &lt; <i>W</i>, 0 &lt;= <i>y<sub>i</sub></i> &lt; <i>H</i>)<br/> <i>x<sub>i</sub></i> が列,<i>y<sub>i</sub></i> が行である.<br/> 開放される壁の座標の位置が &quot;#&quot; なら &quot;.&quot; に置き換え,&quot;.&quot; なら何もしない.<br/> 目的の福袋が開放されることはない. <br/> (0, 0) が壁になることはない.<br/> </p> <p> またあるマスからの移動は,上下左右の4方向のマスへのみが可能であり,壁の上へ移動することはできないものとする. </p> <h2> Output</h2> <p> 左上(0, 0) から <i>t</i> の座標へ辿りつけるようになる時間 <i>s</i> (-1 &lt;= <i>s</i> &lt;= <i>N</i>)を1行で出力せよ.<br/> 壁を開放せずに辿りつける場合,0 を出力せよ.<br/> 営業時間が終了しても到達できなければ -1 を出力すること.<br/> <br/> </p> <h2> Sample Input 1</h2> <pre> 10 10 .....#.... .....#.... .....#.... ######.... .......... ####...... ....###... t..#..#### ...##..... ....#...## 3 0 3 0 5 4 6 </pre> <h2> Sample Output 1</h2> <pre> 2 </pre> <h2> Sample Input 2</h2> <pre> 10 10 .....#.... .....#.... .....#.... ########## ########## ########## ########## t..#..#### ...##..... ....#...## 3 0 3 0 5 4 6 </pre> <h2> Sample Output 2</h2> <pre> -1 </pre> </body> </html>
p03516
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has come up with the following problem.</p> <blockquote> <p>You are given a sequence <var>d</var> of length <var>N</var>. Find the number of the undirected graphs with <var>N</var> vertices labeled <var>1,2,...,N</var> satisfying the following conditions, modulo <var>10^{9} + 7</var>:</p> <ul> <li>The graph is simple and connected.</li> <li>The degree of Vertex <var>i</var> is <var>d_i</var>.</li> </ul> </blockquote> <p><strong>When <var>2 \leq N, 1 \leq d_i \leq N-1, {\rm Σ} d_i = 2(N-1)</var>,</strong> it can be proved that the answer to the problem is <var>\frac{(N-2)!}{(d_{1} -1)!(d_{2} - 1)! ... (d_{N}-1)!}</var>.</p> <p>Snuke is wondering what the answer is <strong>when <var>3 \leq N, 1 \leq d_i \leq N-1, { \rm Σ} d_i = 2N</var>.</strong> Solve the problem under this condition for him.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>3 \leq N \leq 300</var></li> <li><var>1 \leq d_i \leq N-1</var></li> <li><var>{ \rm Σ} d_i = 2N</var></li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Scores</h3><ul> <li>In the test set worth <var>200</var> points, <var>N \leq 5</var>.</li> <li>In the test set worth another <var>200</var> points, <var>N \leq 18</var>.</li> <li>In the test set worth another <var>300</var> points, <var>N \leq 50</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>d_1</var> <var>d_2</var> <var>...</var> <var>d_{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 1 2 2 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>6 </pre> <ul> <li>There are six graphs as shown below:</li> </ul> <div style="text-align: center;"> <img alt="51367cdb21c64bfb07113b300921d52c.png" src="https://atcoder.jp/img/asaporo2/51367cdb21c64bfb07113b300921d52c.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>16 2 1 3 1 2 1 4 1 1 2 1 1 3 2 4 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>555275958 </pre> <ul> <li>Be sure to find the answer modulo <var>10^{9} + 7</var>.</li> </ul></section> </div> </span>
p02891
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given is a string <var>S</var>. Let <var>T</var> be the concatenation of <var>K</var> copies of <var>S</var>. We can repeatedly perform the following operation: choose a character in <var>T</var> and replace it with a different character. Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in <var>T</var> are different.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq |S| \leq 100</var></li> <li><var>S</var> consists of lowercase English letters.</li> <li><var>1 \leq K \leq 10^9</var></li> <li><var>K</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>S</var> <var>K</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of operations required.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>issii 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p><var>T</var> is <code>issiiissii</code>. For example, we can rewrite it into <code>ispiqisyhi</code>, and now any two adjacent characters are different.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>qq 81 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>81 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>cooooooooonteeeeeeeeeest 999993333 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>8999939997 </pre></section> </div> </span>
p03783
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>AtCoDeer the deer found <var>N</var> rectangle lying on the table, each with height <var>1</var>. If we consider the surface of the desk as a two-dimensional plane, the <var>i</var>-th rectangle <var>i(1≤i≤N)</var> covers the vertical range of <var>[i-1,i]</var> and the horizontal range of <var>[l_i,r_i]</var>, as shown in the following figure:</p> <p><img alt="" src="https://atcoder.jp/img/arc070/46b7dc61fc2016c9b45f10db46c6fce9.png"/></p> <p>AtCoDeer will move these rectangles horizontally so that all the rectangles are connected. For each rectangle, the cost to move it horizontally by a distance of <var>x</var>, is <var>x</var>. Find the minimum cost to achieve connectivity. It can be proved that this value is always an integer under the constraints of the problem.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>1≤N≤10^5</var></li> <li><var>1≤l_i&lt;r_i≤10^9</var></li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li><var>300</var> points will be awarded for passing the test set satisfying <var>1≤N≤400</var> and <var>1≤l_i&lt;r_i≤400</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>l_1</var> <var>r_1</var> <var>l_2</var> <var>r_2</var> : <var>l_N</var> <var>r_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum cost to achieve connectivity.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 3 5 7 1 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The second rectangle should be moved to the left by a distance of <var>2</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 2 5 4 6 1 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>The rectangles are already connected, and thus no move is needed.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 999999999 1000000000 1 2 314 315 500000 500001 999999999 1000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1999999680 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>5 123456 789012 123 456 12 345678901 123456 789012 1 23 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>246433 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>1 1 400 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>0 </pre></section> </div> </span>
p04046
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</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>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
p03279
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> robots and <var>M</var> exits on a number line. The <var>N + M</var> coordinates of these are all integers and all distinct. For each <var>i</var> (<var>1 \leq i \leq N</var>), the coordinate of the <var>i</var>-th robot from the left is <var>x_i</var>. Also, for each <var>j</var> (<var>1 \leq j \leq M</var>), the coordinate of the <var>j</var>-th exit from the left is <var>y_j</var>.</p> <p>Snuke can repeatedly perform the following two kinds of operations in any order to move all the robots simultaneously:</p> <ul> <li>Increment the coordinates of all the robots on the number line by <var>1</var>.</li> <li>Decrement the coordinates of all the robots on the number line by <var>1</var>.</li> </ul> <p>Each robot will disappear from the number line when its position coincides with that of an exit, going through that exit. Snuke will continue performing operations until all the robots disappear.</p> <p>When all the robots disappear, how many combinations of exits can be used by the robots? Find the count modulo <var>10^9 + 7</var>. Here, two combinations of exits are considered different when there is a robot that used different exits in those two combinations.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N, M \leq 10^5</var></li> <li><var>1 \leq x_1 &lt; x_2 &lt; ... &lt; x_N \leq 10^9</var></li> <li><var>1 \leq y_1 &lt; y_2 &lt; ... &lt; y_M \leq 10^9</var></li> <li>All given coordinates are integers.</li> <li>All given coordinates are distinct.</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>x_1</var> <var>x_2</var> <var>...</var> <var>x_N</var> <var>y_1</var> <var>y_2</var> <var>...</var> <var>y_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the combinations of exits that can be used by the robots when all the robots disappear, modulo <var>10^9 + 7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 2 2 3 1 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>The <var>i</var>-th robot from the left will be called Robot <var>i</var>, and the <var>j</var>-th exit from the left will be called Exit <var>j</var>. There are three possible combinations of exits (the exit used by Robot <var>1</var>, the exit used by Robot <var>2</var>) as follows:</p> <ul> <li><var>(</var>Exit <var>1</var><var>,</var> Exit <var>1</var><var>)</var></li> <li><var>(</var>Exit <var>1</var><var>,</var> Exit <var>2</var><var>)</var></li> <li><var>(</var>Exit <var>2</var><var>,</var> Exit <var>2</var><var>)</var></li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 4 2 5 10 1 3 7 13 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>8 </pre> <p>The exit for each robot can be chosen independently, so there are <var>2^3 = 8</var> possible combinations of exits.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 1 1 2 4 5 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> <p>Every robot uses Exit <var>1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>4 5 2 5 7 11 1 3 6 9 13 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>6 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>10 10 4 13 15 18 19 20 21 22 25 27 1 5 11 12 14 16 23 26 29 30 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>22 </pre></section> </div> </span>
p01244
<H1><font color="#000">Problem B:</font> Divisor Function</H1> <p> Teiji is a number theory lover. All numbers are his friends - so long as they are integers. One day, while preparing for the next class as a teaching assistant, he is interested in a number-theoretical function called the <i>divisor function</i>. The divisor function <i>&sigma;</i>(<i>n</i>) is defined as the sum of all positive divisors of <i>n</i>. “How fast does this <i>&sigma;</i>(<i>n</i>) grow?” he asked himself. Apparently, <i>&sigma;</i>(<i>n</i>) grows fast as <i>n</i> increases, but it is hard to estimate the speed of the growth. He decided to calculate the maximum value of <i>&sigma;</i>(<i>n</i>)/<i>n</i> on 1 &le; <i>n</i> &le; <i>k</i>, for various <i>k</i>. </p> <p> While it is easy for small numbers, it is tough to calculate many values of the divisor function by hand. Tired of writing thousands of digits, he decided to solve the problem with the help of a computer. But there is a problem: he is not familiar with computer programming. He asked you, a talented programmer, for help. </p> <p> Please write a program to help him. </p> <H2>Input</H2> <p> The input contains a series of test cases. Each test case is described by a line, which contains a single integer <i>k</i> (1 &le; <i>k</i> &le; 10<sup>15</sup> ). The input ends with a line containing a zero, which should not be processed. </p> <H2>Output</H2> <p> For each test case, output the maximum value of <i>&sigma;</i>(<i>n</i>)/<i>n</i> where 1 &le; <i>n</i> &le; <i>k</i> in a line. Each value should be printed with six digits after the decimal point, and should not contain an absolute error greater than 10<sup>-6</sup>. </p> <H2>Sample Input</H2> <pre> 1 2 3 10 50 0 </pre> <H2>Output for the Sample Input</H2> <pre> 1.000000 1.500000 1.500000 2.000000 2.583333 </pre>
p00906
<H1><font color="#000">Problem C: </font>One-Dimensional Cellular Automaton</H1> <p> There is a one-dimensional cellular automaton consisting of <i>N</i> cells. Cells are numbered from 0 to <i>N</i> &minus; 1. </p> <p> Each cell has a state represented as a non-negative integer less than <i>M</i>. The states of cells evolve through discrete time steps. We denote the state of the i-th cell at time t as <i>S</i>(<i>i</i>, <i>t</i>). The state at time <i>t</i> + 1 is defined by the equation </p> <p style="text-align:center"> <i>S</i>(<i>i</i>, <i>t</i> + 1) = (<i>A</i> &times; <i>S</i>(<i>i</i> &minus; 1, <i>t</i>) + <i>B</i> &times; <i>S</i>(<i>i</i>, <i>t</i>) + <i>C</i> &times; <i>S</i>(<i>i</i> + 1, <i>t</i>)) mod <i>M</i>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(1) </p> <p> where <i>A</i>, <i>B</i> and <i>C</i> are non-negative integer constants. For <i>i</i> &lt; 0 or <i>N</i> &le; <i>i</i>, we define <i>S</i>(<i>i</i>, <i>t</i>) = 0. </p> <p> Given an automaton definition and initial states of cells, your mission is to write a program that computes the states of the cells at a specified time <i>T</i>. </p> <H2>Input</H2> <p> The input is a sequence of datasets. Each dataset is formatted as follows. </p> <p> <i>N M A B C T</i><br/> <i>S</i>(0, 0) <i>S</i>(1, 0) ... <i>S</i>(<i>N</i> &minus; 1, 0)<br/> </p> <p> The first line of a dataset consists of six integers, namely <i>N</i>, <i>M</i>, <i>A</i>, <i>B</i>, <i>C</i> and <i>T</i>. <i>N</i> is the number of cells. <i>M</i> is the modulus in the equation (1). <i>A</i>, <i>B</i> and <i>C</i> are coefficients in the equation (1). Finally, <i>T</i> is the time for which you should compute the states. </p> <p> You may assume that 0 &lt; <i>N</i> &le; 50, 0 &lt; <i>M</i> &le; 1000, 0 &le; <i>A</i>, <i>B</i>, <i>C</i> &lt; <i>M</i> and 0 &le; <i>T</i> &le; 10<sup>9</sup>. </p> <p> The second line consists of <i>N</i> integers, each of which is non-negative and less than <i>M</i>. They represent the states of the cells at time zero. </p> <p> A line containing six zeros indicates the end of the input. </p> <H2>Output</H2> <p> For each dataset, output a line that contains the states of the cells at time <i>T</i>. The format of the output is as follows. </p> <p style="text-align:center"> <i>S</i>(0, <i>T</i>) <i>S</i>(1, <i>T</i>) ... <i>S</i>(<i>N</i> &minus; 1, <i>T</i>) </p> <p> Each state must be represented as an integer and the integers must be separated by a space. </p> <H2>Sample Input</H2> <pre> 5 4 1 3 2 0 0 1 2 0 1 5 7 1 3 2 1 0 1 2 0 1 5 13 1 3 2 11 0 1 2 0 1 5 5 2 0 1 100 0 1 2 0 1 6 6 0 2 3 1000 0 1 2 0 1 4 20 1000 0 2 3 1000000000 0 1 2 0 1 0 1 2 0 1 0 1 2 0 1 0 1 2 0 1 30 2 1 0 1 1000000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 30 2 1 1 1 1000000000 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30 5 2 3 1 1000000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 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> 0 1 2 0 1 2 0 0 4 3 2 12 10 9 11 3 0 4 2 1 0 4 2 0 4 4 0 376 752 0 376 0 376 752 0 376 0 376 752 0 376 0 376 752 0 376 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 3 2 2 2 3 3 1 4 3 1 2 3 0 4 3 3 0 4 2 2 2 2 1 1 2 1 3 0 </pre>
p03629
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>A subsequence of a string <var>S</var> is a string that can be obtained by deleting zero or more characters from <var>S</var> without changing the order of the remaining characters. For example, <code>arc</code>, <code>artistic</code> and (an empty string) are all subsequences of <code>artistic</code>; <code>abc</code> and <code>ci</code> are not.</p> <p>You are given a string <var>A</var> consisting of lowercase English letters. Find the shortest string among the strings consisting of lowercase English letters that are not subsequences of <var>A</var>. If there are more than one such string, find the lexicographically smallest one among them.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq |A| \leq 2 \times 10^5</var></li> <li><var>A</var> consists of lowercase English letters.</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>A</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the lexicographically smallest string among the shortest strings consisting of lowercase English letters that are not subsequences of <var>A</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>atcoderregularcontest </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>b </pre> <p>The string <code>atcoderregularcontest</code> contains <code>a</code> as a subsequence, but not <code>b</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>abcdefghijklmnopqrstuvwxyz </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>aa </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>frqnvhydscshfcgdemurlfrutcpzhopfotpifgepnqjxupnskapziurswqazdwnwbgdhyktfyhqqxpoidfhjdakoxraiedxskywuepzfniuyskxiyjpjlxuqnfgmnjcvtlpnclfkpervxmdbvrbrdn </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>aca </pre></section> </div> </span>
p01614
<h1>B: VOCAL ANDROID</h1> <p>ボーカルアンドロイド (VOCAL ANDROID) とは,メロディと歌詞を入力して音声を合成する作曲ツールである. 2013年3月9日に和歌山県で開催された,ボーカルアンドロイドのライブに行ったあなたは,音楽の素晴らしさに感動した. そこで,あなたは新作ボーカルアンドロイド「立音リプロ (CV.立命太郎)」を購入し,作曲を始めることにした. ところが,メロディは完成したものの作詞で詰まってしまった.</p> <p>使いたいフレーズはいくつか断片的に用意しており,それらを組み合わせて各メロディにあてはめる. 各メロディは,長さが決まっており,必ずこの長さぴったりになるようにフレーズをあてはめる必要がある. また,各フレーズには,そのフレーズを使うと得られるノリの良さを表す得点と,下限の長さと上限の長さが決まっている. フレーズは,長さが整数であれば,下限から上限まで自由に調整できる. ただし,長さを変更しても,得られる点数は変わらない. 図1は,サンプルの1番目の例である.</p> <div align="center"> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE3_Rits_Camp13_Day3_B_B_fig" alt="メロディに対するフレーズの割り当て" /> <p><strong>図1:</strong> メロディに対するフレーズの割り当て</p> </div> <p>あなたの仕事はメロディの長さぴったりにフレーズを組み合わせて,できるだけ得点の総和が高い曲を作ることである. 同じフレーズは何度も使用してよいものとする.</p> <div> <h1>Input</h1> <p>入力は以下の形式で与えられる.</p> <div> <em>n</em><br /> <em>s</em><sub>1</sub> <em>l</em><sub>1</sub> <em>p</em><sub>1</sub><br /> …<br /> <em>s</em><sub>n</sub> <em>l</em><sub>n</sub> <em>p</em><sub>n</sub><br /> <em>m</em><br /> <em>w</em><sub>1</sub><br /> …<br /> <em>w</em><sub>m</sub><br /> </div> <br/> <p>1行目には <em>n</em> ( 1 ≦ <em>n</em> ≦ 393 ) が与えられ, その後 <em>n</em> 行には歌詞に使うフレーズの情報が与えられる. <em>s</em><sub>i</sub>, <em>l</em><sub>i</sub> ( 1 ≦ <em>s</em><sub>i</sub> &lt; <em>l</em><sub>i</sub> ≦ 393 ) は, そのフレーズの長さの下限と上限であり, <em>p</em><sub>i</sub> ( 1 ≦ <em>p</em><sub>i</sub> ≦ 393 ) は,そのフレーズの得点を表す.</p> <p><em>m</em> ( 1 ≦ <em>m</em> ≦ 393 ) は,メロディの数を表し, その後 <em>m</em> 行にメロディの長さ <em>w</em><sub>i</sub> ( 1 ≦ <em>w</em><sub>i</sub> ≦ 393 ) が与えられる.</p> </div> <div> <h1>Output</h1> <p>以下のような形式で各メロディの得点を出力せよ.</p> <div> <em>1</em> 行目 メロディ 1 の最大得点<br /> …<br /> <em>m</em> 行目 メロディ <em>m</em> の最大得点<br /> </div> <br/> <p>ただし,1つでもフレーズをぴったり当てはめられないメロディが存在する場合には,-1のみを出力せよ.</p> </div> <div> <h1>Sample Input 1</h1> <pre> 3 4 5 15 2 3 4 7 8 39 2 6 8 </pre> </div> <div> <h1>Sample Output 1</h1> <pre> 19 39 </pre> </div> <div> <h1>Sample Input 2</h1> <pre> 1 2 3 10 2 3 1 </pre> </div> <div> <h1>Sample Output 2</h1> <pre> -1 </pre> </div>
p02192
<h2>K Average Ranges</h2> <p>数列<var>a_1,a_2,..,a_N</var>が与えられます。</p> <p>この数列に値の平均が<var>K</var>以上の長さ<var>1</var>以上の区間はいくつありますか。</p> <h3>入力</h3> <pre> <var>N K</var> <var>a_1 a_2...a_N</var> </pre> <h3>出力</h3> <p>答えを出力せよ。</p> <h3>制約</h3> <ul> <li><var>1 \leq N \leq 10^5 </var></li> <li><var>1 \leq K \leq 10^9 </var></li> <li><var>1 \leq a_i \leq 10^9</var></li> </ul> <h3>入力例</h3> <pre> 6 6 8 6 9 1 2 1 </pre> <h3>出力例</h3> <pre> 7 </pre>
p00455
<H1>タイムカード </H1> <h2>問題</h2> <p> JOI商事では社員の在社時間をタイムカードで管理している.社員は,出社すると専用の装置を使ってタイムカードに出社時刻を刻印する.勤務を終え退社するときにも,タイムカードに退社時刻を刻印する.時刻は24時間制で扱われる. </p> <p> 防犯上の理由から,社員の出社時刻は7時以降である.また,全ての社員は23時より前に退社する.社員の退社時刻は常に出社時刻より後である. </p> <p> 入力としてJOI商事の3人の社員Aさん,Bさん,Cさんの出社時刻と退社時刻が与えられたとき,それぞれの社員の在社時間を計算するプログラムを作成せよ. </p> <h2>入力</h2> <p> 入力は3行からなる. 1行目にはAさんの出社時刻と退社時刻, 2行目にはBさんの出社時刻と退社時刻, 3行目にはCさんの出社時刻と退社時刻がそれぞれ空白で区切られ書かれている. </p> <p> 時刻はそれぞれ空白で区切られた3つの整数で書かれている. 3つの整数 h, m, s は h 時 m 分 s 秒を表す. 7 &le; h &le; 22, 0 &le; m &le; 59, 0 &le; s &le; 59 である. </p> <h2>出力</h2> <p> 1行目にAさんの在社時間, 2行目にBさんの在社時間, 3行目にCさんの在社時間を出力せよ. </p> <p> 出力する在社時間が h 時間 m 分 s 秒の場合, h, m, s の順に空白で区切って出力せよ. </p> <h2>入出力例</h2> <h3>入力例</h3> <pre> 9 0 0 18 0 0 9 0 1 18 0 0 12 14 52 12 15 30</pre> <h3>出力例</h3> <pre> 9 0 0 8 59 59 0 0 38</pre> <div class="source"> <p class="source"> 上記問題文と自動審判に使われるデータは、<a href="http://www.ioi-jp.org">情報オリンピック日本委員会</a>が作成し公開している問題文と採点用テストデータです。 </p> </div>
p02468
<H1>Power</H1> <br/> <p> For given integers <var>m</var> and <var>n</var>, compute <var>m<sup>n</sup></var> (mod 1,000,000,007). Here, <var>A</var> (mod <var>M</var>) is the remainder when <var>A</var> is divided by <var>M</var>. </p> <H2>Input</H2> <pre> <var>m</var> <var>n</var> </pre> <p> Two integers <var>m</var> and <var>n</var> are given in a line. </p> <H2>Output</H2> <p> Print <var>m<sup>n</sup></var> (mod 1,000,000,007) in a line. </p> <H2>Constraints</H2> <ul> <li>1 &le; <var>m</var> &le; 100</li> <li>1 &le; <var>n</var> &le; 10<sup>9</sup></li> </ul> <H2>Sample Input 1</H2> <pre> 2 3 </pre> <H2>Sample Output 1</H2> <pre> 8 </pre> <br/> <H2>Sample Input 2</H2> <pre> 5 8 </pre> <H2>Sample Output 2</H2> <pre> 390625 </pre>
p00005
<H1>GCD and LCM</H1> <p> Write a program which computes the greatest common divisor (GCD) and the least common multiple (LCM) of given <var>a</var> and <var>b</var>. </p> <H2>Input</H2> <p> Input consists of several data sets. Each data set contains <var>a</var> and <var>b</var> separated by a single space in a line. The input terminates with EOF. </p> <h2>Constraints</h2> <ul> <li> 0 &lt; <var>a, b</var> &le; 2,000,000,000</li> <li> LCM(<var>a, b</var>) &le; 2,000,000,000</li> <li> The number of data sets &le; 50</li> </ul> <H2>Output</H2> <p> For each data set, print GCD and LCM separated by a single space in a line. </p> <H2>Sample Input</H2> <pre> 8 6 50000000 30000000 </pre> <H2>Output for the Sample Input</H2> <pre> 2 24 10000000 150000000 </pre>
p02038
<h1>B: たぬきつね<h1> <h2>問題</h2> <p> 謎の装置 $M$ があり,この装置に Tanuki と Fox を入れると, 装置から一匹の動物が出てくる(以下, Tanuki を $T$ ,Foxを $F$ とする). </p> <p> $M(x, y)$ は,装置 $M$ に $x, y$ の順で動物を入れて出てきた動物を表す. </p> <p> 色々試した結果,以下のことがわかった.<br> <ul> $M(T, T) = T$ <br> $M(T, F) = F$ <br> $M(F, T) = T$ <br> $M(F, F) = T$ <br> </ul> </p> <p> あなたは,一列に並んでいる動物たち $P_1, P_2, ... , P_N$ を以下のように装置に入れていった.<br> $M(....M(M(P_1,P_2), P_3)...., P_N)$ </p> <p> 最後に出てくる動物を答えよ. </p> <h2>制約</h2> <ul> <li>$2 \leq N \leq 10^{5}$</li> <li> $P_i$ は $T$ か $F$ の一文字 </li> </ul> <h2>入力形式</h2> <p> 入力は以下の形式で与えられる. </p> <p> $N$<br> $P_1\ P_2\ ...\ P_N$ </p> <h2>出力</h2> <p>最後に出てくる動物を表す文字 $T$ または $F$ を出力せよ.また,末尾に改行も出力せよ.</p> <h2>サンプル</h2> <h3>サンプル入力 1</h3> <pre> 3 F T T </pre> <h3>サンプル出力 1</h3> <pre> T </pre> <h3>サンプル入力 2</h3> <pre> 7 F T T F T T F </pre> <h3>サンプル出力 2</h3> <pre> F </pre>
p00510
<H1>問題 1 </H1> <br/> <p>  n 分間にわたり, トンネルの入口と出口で, 1分間に通過する車の数を数えたデータがある. そのデータは, 全部で n+2 行からなり, 各行には次の内容が書かれている. </p> <UL> <LI> 第1行目には,正整数 n が書かれており, 調査時間が n 分間であったことを表している.</LI> <LI> 第2行目には,正整数 m が書かれており, 調査開始時におけるトンネル内の車の台数が m であったことを表している.</LI> <LI> 第(2+i)行目( i = 1, 2, ... , n ) には, 調査開始後 (i-1) 分経過した時点から i 分経過するまでの1分間に, 入口を通過した車の台数と出口を通過した車の台数が 1つの空白で区切られて書かれている.</LI> </UL> <p> 調査開始後 j 分経過した時点 ( j=0, 1, 2, ... , n ) におけるトンネル内の車の台数を S<sub>j</sub> とする. S<sub>j</sub> の最大値を出力しなさい. また, トンネル内の車の台数が負になることは考えられないので, S<sub>j</sub> が一度でも負になった場合は, 「エラー」の意味で 0 を出力しなさい. ただし, n は 10000 以下で, トンネルの入口および出口を1分間に通過する車の台数は 100 以下である. </p> <p>  出力ファイルにおいては, 出力の最後の行にも改行コードを入れること. </p> <h2>入出力例</h2> <h3>入力例1</h3> <pre> 3 2 2 3 2 3 4 1 </pre> <h3>出力例1</h3> <pre> 3 </pre> <br> <h3>入力例2</h3> <pre> 3 2 2 3 2 4 4 1 </pre> <h3>出力例2</h3> <pre> 0 </pre> <br> <h3>入力例3</h3> <pre> 3 2 2 3 2 3 1 0 </pre> <h3>出力例3</h3> <pre> 2 </pre> <div class="source"> <p class="source"> 問題文と自動審判に使われるデータは、<a href="http://www.ioi-jp.org">情報オリンピック日本委員会</a>が作成し公開している問題文と採点用テストデータです。 </p> </div>
p00140
<H1>バス路線</H1> <p> 図 1のようなバス路線があります。停留所は 10 箇所で、それぞれ 0〜9 の番号がついています。バスは停留所 0 では折り返しますが、反対側は循環路線になっており、図のように 5→6→7→8→9→5 の順で循環します。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_busLine"> </center> <br/> <p> このバス路線について、乗車する停留所と降車する停留所を入力とし、乗車から降車までに通る停留所の番号を出力するプログラムを作成してください。 </p> <p> ただし、1〜5 の停留所では 2 方向のバスに乗車できますが、より短い経路で降車停留所に到着するバスに乗車することとします。例えば、停留所 4 から停留所 2 へ行く場合、左方向に進むバスに乗り「4→3→2」の経路を通ります。また、一度バスに乗車したら途中下車はしないものとします。同じ停留所を乗車停留所、降車停留所に指定することはありません。 </p> <H2>Input</H2> <p> 複数のデータセットが与えられます。一行目にデータセット数 <var>n</var> (<var>n</var> &le; 20 ) が与えられます。各データセットとして、乗車する停留所番号と降車する停留所番号が空白区切りで1行に与えられます。 </pre> <H2>Output</H2> <p> 各データセットに対して、通過する停留所番号の並びを空白区切りで1行に出力してください。 </p> <H2>Sample Input</H2> <pre> 2 2 4 4 2 </pre> <H2>Output for the Sample Input</H2> <pre> 2 3 4 4 3 2 </pre>
p01301
<H1><font color="#000">Problem I:</font> Crystal Jails</H1> <p> Artistic Crystal Manufacture developed products named Crystal Jails. They are cool ornaments forming a rectangular solid. They consist of colorful crystal cubes. There are bright cores on the center of cubes, which are the origin of the name. The combination of various colored reflections shows fantastic dance of lights. </p> <p> The company wanted to make big sales with Crystal Jails. They thought nice-looking color patterns were the most important factor for attractive products. If they could provide several nice patterns, some customers would buy more than one products. However, they didn't have staff who could design nice patterns. So they hired a temporary designer to decide the patterns. </p> <p> The color pattern for mass production had a technical limitation: all cubes of the same color must be connected. In addition, they needed to send the pattern to the factory as a set of <i>blocks</i>, i.e. shapes formed by cubes of the same color. They requested him to represent the design in this form. </p> <p> After a week of work, he sent various ideas of the color patterns to them. At first, his designs looked nice, but they noticed some patterns couldn’t form a rectangular solid. He was a good designer, but they had not noticed he lacked space geometrical sense. </p> <p> They didn't have time to ask him to revise his design. Although it was acceptable to ignore bad patterns, it also took a lot of time to figure out all bad patterns manually. So the leader of this project decided to ask you, a freelance programmer, for help. </p> <p> Your task is to write a program to judge whether a pattern can form a rectangular solid. Note that blocks can be rotated. </p> <H2>Input</H2> <p> The input consists of multiple datasets. Each dataset is formatted as follows: </p> <p> <i>W D H N</i><br> <i>Block</i><sub>1</sub><br> <i>Block</i><sub>2</sub><br> ...<br> <i>Block</i><sub><i>N</i></sub><br> </p> <p> The first line of a dataset contains four positive integers <i>W</i>, <i>D</i>, <i>H</i> and <i>N</i>. <i>W</i>, <i>D</i> and <i>H</i> indicate the width, depth and height of a Crystal Jail. <i>N</i> indicates the number of colors. </p> <p> The remaining lines describe <i>N</i> colored blocks. Each description is formatted as follows: </p> <p> <i>w d h</i><br> <i>c</i><sub>111</sub> <i>c</i><sub>211</sub> ... <i>c</i><sub><i>w</i>11</sub><br> <i>c</i><sub>121</sub> <i>c</i><sub>221</sub> ... <i>c</i><sub><i>w</i>21</sub><br> ...<br> <i>c</i><sub>1<i>d</i>1</sub> <i>c</i><sub>2<i>d</i>1</sub> ... <i>c</i><sub><i>wd</i>1</sub><br> <i>c</i><sub>112</sub> <i>c</i><sub>212</sub> ... <i>c</i><sub><i>w</i>12</sub><br> <i>c</i><sub>122</sub> <i>c</i><sub>222</sub> ... <i>c</i><sub><i>w</i>22</sub><br> ...<br> <i>c</i><sub>1<i>d</i>2</sub> <i>c</i><sub>2<i>d</i>2</sub> ... <i>c</i><sub><i>wd</i>2</sub><br> .<br> .<br> .<br> <i>c</i><sub>11<i>h</i></sub> <i>c</i><sub>21<i>h</i></sub> ... <i>c</i><sub><i>w</i>1<i>h</i></sub><br> <i>c</i><sub>12<i>h</i></sub> <i>c</i><sub>22<i>h</i></sub> ... <i>c</i><sub><i>w</i>2<i>h</i></sub><br> ...<br> <i>c</i><sub>1<i>dh</i></sub> <i>c</i><sub>2<i>dh</i></sub> ... <i>c</i><sub><i>wdh</i></sub><br> </p> <p> The first line of the description contains three positive integers <i>w</i>, <i>d</i> and <i>h</i>, which indicate the width, depth and height of the block. The following (<i>d</i> + 1) &times; <i>h</i> lines describe the shape of the block. They show the cross-section layout of crystal cubes from bottom to top. On each height, the layout is described as <i>d</i> lines of <i>w</i> characters. Each character <i>c<sub>xyz</sub></i> is either '<span>*</span>' or '<span>.</span>'. '<span>*</span>' indicates there is a crystal cube on that space, and '<span>.</span>' indicates there is not. A blank line follows after each (<i>d</i> &times; <i>w</i>)-matrix. </p> <p> The input is terminated by a line containing four zeros. </p> <p> You can assume the followings. </p> <ul> <li> 1 &le; <i>W</i>, <i>D</i>, <i>H</i>, <i>w</i>, <i>d</i>, <i>h</i> &le; 3.</li> <li> 1 &le; <i>N</i> &le; 27.</li> <li> Each block has at least one crystal cubes and they are connected.</li> <li> The total number of crystal cubes equals to <i>W</i> &times; <i>D</i> &times; <i>H</i>.</li> </ul> <H2>Output</H2> <p> For each dataset, determine whether the given blocks can form a <i>W</i> &times; <i>D</i> &times; <i>H</i> rectangular solid and output "<span>Yes</span>" or "<span>No</span>" in one line. </p> <H2>Sample Input</H2> <pre> 3 3 3 5 3 2 2 *** .*. .*. ... 3 2 1 *** **. 3 1 3 ..* .** **. 3 2 2 ..* ... *** ..* 3 1 3 .** .** *** 3 3 3 2 3 3 3 *** *** *** *** *.* *** *** *** *** 1 1 1 * 3 2 1 2 3 1 1 *** 2 2 1 ** *. 0 0 0 0 </pre> <H2>Output for the Sample Input</H2> <pre> Yes Yes No </pre>
p00843
<H1><font color="#000">Problem H:</font> Bingo</H1> <p> A Bingo game is played by one gamemaster and several players. At the beginning of a game, each player is given a card with <i>M</i> &times; <i>M</i> numbers in a matrix (See Figure 10). </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_bingo1"> </center> <p> As the game proceeds, the gamemaster announces a series of numbers one by one. Each player punches a hole in his card on the announced number, if any. </p> <p> When at least one 'Bingo' is made on the card, the player wins and leaves the game. The 'Bingo' means that all the <i>M</i> numbers in a line are punched vertically, horizontally or diagonally (See Figure 11). </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_bingo2"> </center> <p> The gamemaster continues announcing numbers until all the players make a Bingo. </p> <p> In the ordinary Bingo games, the gamemaster chooses numbers by a random process and has no control on them. But in this problem the gamemaster knows all the cards at the beginning of the game and controls the game by choosing the number sequence to be announced at his will. </p> <p> Specifically, he controls the game to satisfy the following condition. </p> <p> Card<sub><i>i</i></sub> makes a Bingo no later than Card<sub><i>j</i></sub>, for <i>i</i> &lt; <i>j</i>. (*) </p> <p> Figure 12 shows an example of how a game proceeds. The gamemaster cannot announce '5' before '16', because Card<sub>4</sub> makes a Bingo before Card<sub>2</sub> and Card<sub>3</sub>, violating the condition (*). </p> <p> Your job is to write a program which finds the minimum length of such sequence of numbers for the given cards. </p> <H2>Input</H2> <p> The input consists of multiple datasets. The format of each dataset is as follows. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_bingo3"> </center> <p> All data items are integers. <i>P</i> is the number of the cards, namely the number of the players. <i>M</i> is the number of rows and the number of columns of the matrix on each card. <i>N<sup>k</sup><sub>ij</sub></i> means the number written at the position (<i>i</i>, <i>j</i>) on the <i>k</i>-th card. If (<i>i</i>, <i>j</i>) &ne; (<i>p</i>, <i>q</i>), then <i>N<sup>k</sup><sub>ij</sub></i> &ne; <i>N<sup>k</sup><sub>pq</sub></i>. The parameters <i>P</i>, <i>M</i>, and <i>N</i> satisfy the conditions 2 &le; <i>P</i> &le; 4, 3 &le; <i>M</i> &le; 4, and 0 &le; <i>N<sup>k</sup><sub>ij</sub></i> &le; 99. </p> <p> The end of the input is indicated by a line containing two zeros separated by a space. It is not a dataset. </p> <H2>Output</H2> <p> For each dataset, output the minimum length of the sequence of numbers which satisfy the condition (*). Output a zero if there are no such sequences. Output for each dataset must be printed on a separate line. </p> <H2>Sample Input</H2> <pre> 4 3 10 25 11 20 6 2 1 15 23 5 21 3 12 23 17 7 26 2 8 18 4 22 13 27 16 5 11 19 9 24 2 11 5 14 28 16 4 3 12 13 20 24 28 32 15 16 17 12 13 21 25 29 33 16 17 18 12 13 22 26 30 34 17 18 15 12 13 23 27 31 35 18 15 16 4 3 11 12 13 14 15 16 17 18 19 21 22 23 24 25 26 27 28 29 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 47 48 49 4 4 2 6 9 21 15 23 17 31 33 12 25 4 8 24 13 36 22 18 27 26 35 28 3 7 11 20 38 16 5 32 14 29 26 7 16 29 27 3 38 14 18 28 20 32 22 35 11 5 36 13 24 8 4 25 12 33 31 17 23 15 21 9 6 2 0 0 </pre> <H2>Output for the Sample Input</H2> <pre> 5 4 12 0 </pre> <p> For your convenience, sequences satisfying the condition (*) for the first three datasets are shown below. There may be other sequences of the same length satisfying the condition, but no shorter. </p> <pre> 11, 2, 23, 16, 5 15, 16, 17, 18 11, 12, 13, 21, 22, 23, 31, 32, 33, 41, 42, 43 </pre>
p01751
<p> A 君は山手線に乗って目的地にたどり着きたい. </p> <p> A 君は電車に乗ってから <var>a</var> 分起きて <var>b</var> 分寝ることを繰り返す.目的地までは乗車後 <var>c</var> 分であり,このとき起きていれば下車できるが,逆に寝ている途中であれば乗り過ごしてしまう.また,A 君は乗り過ごしてもずっと同じ電車に乗り続け,電車は山手線を一周するのに <var>60</var> 分かかる.したがって,A 君は目的地に <var>60t + c</var> ( <var>t</var> は非負整数) 分後に着くことになる. </p> <p> 何分後にA 君は目的地に到着できるか.到着不可能なときは-1 を出力せよ.ただし,目的地に到着した時が寝ている時間と起きている時間の境目だった場合は降りることができるものとする. </p> <h2>Input</h2> <p> 入力は以下の形式で1 行で与えられる. </p> <pre> <var>a</var> <var>b</var> <var>c</var> </pre> <p> 入力は3 つの整数 <var>a, b, c</var> からなる. </p> <p> <var>a</var> は起きている時間,<var>b</var> は寝ている時間,<var>c</var> は乗車してから目的地までにかかる時間である.また,<var>a, b, c</var> の単位は分である. </p> <h2>Constraints</h2> <ul> <li>0 &lt; <var>a, b, c</var> &lt; 60</li> </ul> <h2>Output</h2> <p> A 君が目的地に到着できる場合はA 君の目的地到着までにかかる時間を出力せよ.それ以外の場合は-1 を出力せよ. </p> <h2>Sample Input 1</h2> <pre> 10 10 5 </pre> <h2>Output for the Sample Input 1</h2> <pre> 5 </pre> <ul> <li> 寝る前に目的地にたどり着く.</li> </ul> <h2>Sample Input 2</h2> <pre> 50 40 51 </pre> <h2>Output for the Sample Input 2</h2> <pre> 111 </pre> <ul> <li> 一回乗り過ごしてしまう.</li> </ul> <h2>Sample Input 3</h2> <pre> 20 20 20 </pre> <h2>Output for the Sample Input 3</h2> <pre> 20 </pre> <ul> <li>もうちょっとで寝るところだったがなんとか目的地で起きていられた.</li> </ul> <h2>Sample Input 4</h2> <pre> 30 30 40 </pre> <h2>Output for the Sample Input 4</h2> <pre> -1 </pre> <ul> <li> 一周の前半では起きているが後半で寝ているということを繰り返す.</li> <li> このため目的地にはたどり着けない.</li> </ul>
p03296
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi lives in another world. There are slimes (creatures) of <var>10000</var> colors in this world. Let us call these colors Color <var>1, 2, ..., 10000</var>.</p> <p>Takahashi has <var>N</var> slimes, and they are standing in a row from left to right. The color of the <var>i</var>-th slime from the left is <var>a_i</var>. If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic.</p> <p>Takahashi can change the color of one slime to any of the <var>10000</var> colors by one spell. How many spells are required so that no slimes will start to combine themselves?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 100</var></li> <li><var>1 \leq a_i \leq N</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 minimum number of spells required.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 1 1 2 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>For example, if we change the color of the second slime from the left to <var>4</var>, and the color of the fourth slime to <var>5</var>, the colors of the slimes will be <var>1, 4, 2, 5, 2</var>, which satisfy the condition.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Although the colors of the first and third slimes are the same, they are not adjacent, so no spell is required.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 1 1 1 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>2 </pre> <p>For example, if we change the colors of the second and fourth slimes from the left to <var>2</var>, the colors of the slimes will be <var>1, 2, 1, 2, 1</var>, which satisfy the condition.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>14 1 2 2 3 3 3 4 4 4 4 1 2 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>4 </pre></section> </div> </span>
p02254
<h1>Huffman Coding</h1> <p>We want to encode a given string $S$ to a binary string. Each alphabet character in $S$ should be mapped to a different variable-length code and the code must not be a prefix of others.</p> <p>Huffman coding is known as one of ways to obtain a code table for such encoding.</p> <p>For example, we consider that appearance frequencycies of each alphabet character in $S$ are as follows:</p> <table border=""> <tr><th></th><th>a</th><th>b</th><th>c</th><th>d</th><th>e</th><th>f</th></tr> <tr><th>freq.</th><td>45</td><td>13</td><td>12</td><td>16</td><td>9</td><td>5</td></tr> <tr><th>code</th><td>0</td><td>101</td><td>100</td><td>111</td><td>1101</td><td>1100</td></tr> </table> <p>We can obtain "Huffman codes" (the third row of the table) using Huffman's algorithm.</p> <p>The algorithm finds a full binary tree called "Huffman tree" as shown in the following figure.</p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/ALDS1_15_D_tree"> </center> <p>To obtain a Huffman tree, for each alphabet character, we first prepare a node which has no parents and a frequency (weight) of the alphabet character. Then, we repeat the following steps:</p> <ol> <li>Choose two nodes, $x$ and $y$, which has no parents and the smallest weights.</li> <li>Create a new node $z$ whose weight is the sum of $x$'s and $y$'s.</li> <li>Add edge linking $z$ to $x$ with label $0$ and to $y$ with label $1$. Then $z$ become their parent.</li> <li>If there is only one node without a parent, which is the root, finish the algorithm. Otherwise, go to step 1.</li> </ol> <p>Finally, we can find a "Huffman code" in a path from the root to leaves.</p> <p>Task</p> <p>Given a string $S$, output the length of a binary string obtained by using Huffman coding for $S$.</p> <h2>Input</h2> <pre> $S$ </pre> <p>A string $S$ is given in a line.</p> <h2>Output</h2> <p>Print the length of a binary string in a line.</p> <h2>Constraints</h2> <ul> <li>$1 \le |S| \le 10^5$</li> <li>$S$ consists of lowercase English letters.</li> </ul> <h2>Sample Input 1</h2> <pre> abca </pre> <h2>Sample Output 1</h2> <pre> 6 </pre> <h2>Sample Input 2</h2> <pre> aaabbcccdeeeffg </pre> <h2>Sample Output 2</h2> <pre> 41 </pre> <h2>Sample Input 3</h2> <pre> z </pre> <h2>Sample Output 3</h2> <pre> 1 </pre>
p00269
<H1>図画工作</H1> <p> イヅア大学附属小学校は日本有数の競技プログラマー養成校として有名です。この学校の教員は幅広い アルゴリズムの知識を持ち、日々それを活用しています。教員であるあなたは、今年は図画工作の授業 を担当することになりました。この授業では、児童全員がそれぞれ1年間で一つの課題を完成させることになっています。授業の概要は以下のとおりです。 </p> <ul> <li> 1年間で授業は D 回(同じ日に2 回以上授業はない)あり、その全てが課題制作に充てられる。</li> <li> 制作する課題は M 種類用意されている。</li> <li> それぞれの児童に、M 種類の中から課題を1つずつ割り当てる。</li> <li> 児童は N 人であり、N 人それぞれに異なる課題が割り当てられる。</li> </ul> <p> 児童は、K 種類ある部品のうちいくつかの種類の部品を使用して課題を完成させます。課題の制作の概要は以下のとおりです。 </p> <ul> <li> 課題ごとに、使用すべき部品の種類と数はあらかじめ決められている。</li> <li> 課題が完成するまでに使用する部品の種類と数は、その課題で使用すべき部品の種類と数に、過不足なく一致していなければならない。</li> <li> 異なる課題でも、使われる部品の種類と数がすべて同じ場合もある。</li> <li> どの課題も、同じ種類の部品は2個までしか使用できない。</li> <li> 部品を使用する順序は課題の完成に影響を与えない。</li> <li> いくつかの部品が入っている袋が事前に P 個用意されている。ただし、異なる袋でも、入っている部品の種類と数がすべて同じ場合もある。</li> <li> 教員は、児童1人につき袋を1つだけ渡すことができる(袋を渡さない児童がいてもよい)。</li> <li> 2人以上の児童に同じ袋を渡すことはできない(反対に、誰にも渡されない袋があってもよい)。</li> <li> 袋を渡された児童は、袋の中に入っている部品をすべて、自分が制作する課題に使わなければならない。</li> </ul> <p> 袋に入っている部品以外で課題に使用する部品は、別途購入する必要があります。部品の購入に関する条件は以下のとおりです。 </p> <ul> <li> 部品は授業の日だけ購入でき、その日にしか使えない。</li> <li> それぞれの課題について、1回の授業でL個までの部品を購入することができる。</li> <li> 部品の種類によって価格が設定されており、購入する日によって価格が変動する。ただし、どの種類も品切れになることはない。</li> </ul> <p> あなたは、このような条件下で、授業にかかる経費をなるべく抑えたいと考えています。そこで、児童全員の部品購入費の合計の最小値を計算するプログラムを作成することにしました。 </p> <h2>入力</h2> <p> 入力は複数のデータセットからなる。入力の終わりはゼロ3つの行で示される。各データセットは以下の形式で与えられる。 </p> <pre> D K L c<sub>1,1</sub> c<sub>1,2</sub> ... c<sub>1,K</sub> c<sub>2,1</sub> c<sub>2,2</sub> ... c<sub>2,K</sub> : c<sub>D,1</sub> c<sub>D,2</sub> ... c<sub>D,K</sub> M N P r<sub>1,1</sub> r<sub>1,2</sub> ... r<sub>1,K</sub> r<sub>2,1</sub> r<sub>2,2</sub> ... r<sub>2,K</sub> : r<sub>M,1</sub> r<sub>M,2</sub> ... r<sub>M,K</sub> t<sub>1,1</sub> t<sub>1,2</sub> ... t<sub>1,K</sub> t<sub>2,1</sub> t<sub>2,2</sub> ... t<sub>2,K</sub> : t<sub>P,1</sub> t<sub>P,2</sub> ... t<sub>P,K</sub> </pre> <p> 各行で与えられる数値は1つの空白で区切られている。 </p> <p> D(1 &le; D &le; 8)は授業の回数、K(1 &le; K &le; 8)は部品の種類の数、L(1 &le; L &le; 8)は1日に購入することができる部品の数を示す。c<sub>d,k</sub> (1 &le; c<sub>d,k</sub> &le; 100)は d 日目の部品k の価格を示す。 </p> <p> M(1 &le; M &le; 200)は課題の種類の数、N(1 &le; N &le; M)は生徒の人数、P(1 &le; P &le; 200)は袋の数を示す。 </p> <p> r<sub>m,k</sub> (0 &le; r<sub>m,k</sub> &le; 2)は課題 m に必要な部品 k の個数、t<sub>p,k</sub> (0 &le; t<sub>p,k</sub> &le; 2)は袋 p に入っている部品 k の個数を示す。 </p> <p> 部品を1つも必要としない課題や、空の袋は与えられないものとする。 </p> <p> データセットの数は20 を超えない。 </p> <h2>出力</h2> <p> データセットごとに、生徒全員の部品購入費の合計の最小値を1行に出力する。N種類の作品を完成させることができない場合は -1 を出力する。 </p> <h2>入力例</h2> <pre> 2 2 1 5 3 2 2 3 3 1 2 0 1 2 2 2 1 1 2 2 1 5 3 2 2 3 2 1 2 0 1 2 2 2 1 1 2 2 2 5 3 2 2 3 2 1 2 0 1 2 2 2 1 1 4 3 1 2 2 1 3 2 2 2 3 3 1 2 2 5 4 3 1 1 0 1 0 1 1 0 2 1 1 2 2 2 2 1 0 1 2 0 2 1 1 1 0 0 0 </pre> <h2>出力例</h2> <pre> -1 9 6 7 </pre>
p00793
<H1><font color="#000">Problem F:</font> Heavenly Jewels</H1> <p> There is a flat island whose shape is a perfect square. On this island, there are three habitants whose names are IC, PC, and ACM Every day, one jewel is dropped from the heaven. Just as the jewel touches the ground, IC, PC, and ACM leave their houses simultaneously, run with the same speed, and then a person who first touched the jewel can get the jewel. Hence, the person whose house is nearest to the location of the jewel is the winner of that day. </p> <p> They always have a quarrel with each other, and therefore their houses are located at distinct places. The locations of their houses are fixed. This jewel falls at a random point on the island, that is, all points on the island have even chance. </p> <p> When three are two or more persons whose houses are simultaneously nearest, the last person in the order of </p> <p> IC, PC, ACM </p> <p> obtains the jewel. </p> <p> Our interest is to know the probability for IC to get the jewel under this situation. </p> <H2>Input</H2> <p> The input describes one problem instance per line. Each line contains the <i>x</i>- and <i>y</i>-coordinates of IC's home, those of PC's, and those of ACM's in this order. Note that the houses of IC, PC and ACM are located at distinct places. The end of the input is indicated by a line with six zeros. </p> <p> The coordinates of the whole island are given by (0, 10000) &times; (0, 10000) and coordinates of houses are given in integer numbers between 1 and 9999, inclusive. It should be noted that the locations of the jewels are arbitrary places on the island and their coordinate values are not necessarily integers. </p> <H2>Output</H2> <p> For each input line, your program should output its sequence number starting from 1, and the probability for the instance. The computed probability values should have errors less than 10<sup>-5</sub>. </p> <p> The sequence number and the probability should be printed on the same line. The two numbers should be separated by a space. </p> <H2>Sample Input</H2> <pre> 2000 2000 8000 8000 9000 9500 2500 2500 7500 2500 2500 7500 0 0 0 0 0 0 </pre> <H2>Output for the Sample Input</H2> <pre> 1 0.50000 2 0.25000 </pre>
p01881
<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>Help the Princess!</h2> <p> The people of a certain kingdom make a revolution against the bad government of the princess. The revolutionary army invaded the royal palace in which the princess lives. The soldiers of the army are exploring the palace to catch the princess. Your job is writing a program to decide that the princess can escape from the royal palace or not. </p> <p> For simplicity, the ground of the palace is a rectangle divided into a grid. There are two kinds of cells in the grid: one is a cell that soldiers and the princess can enter, the other is a cell that soldiers or the princess cannot enter. We call the former an empty cell, the latter a wall. The princess and soldiers are in different empty cells at the beginning. There is only one escape hatch in the grid. If the princess arrives the hatch, then the princess can escape from the palace. There are more than or equal to zero soldiers in the palace. </p> <p> The princess and all soldiers take an action at the same time in each unit time. In other words, the princess and soldiers must decide their action without knowing a next action of the other people. In each unit time, the princess and soldiers can move to a horizontally or vertically adjacent cell, or stay at the current cell. Furthermore the princess and soldiers cannot move out of the ground of the palace. If the princess and one or more soldiers exist in the same cell after their move, then the princess will be caught. It is guaranteed that the princess can reach the escape hatch via only empty cells if all soldiers are removed from the palace. </p> <p> If there is a route for the princess such that soldiers cannot catch the princess even if soldiers make any moves, then the princess can escape the soldiers. Note that if the princess and a soldier arrive the escape hatch at the same time, the princess will be caught. Can the princess escape from the palace? </p> <h3>Input</h3> <p> Each dataset is formatted as follows.<br/> <br/> $H$ $W$<br/> $map_1$<br/> $map_2$<br/> ...<br/> $map_H$<br/> </p> <p> The first line of a dataset contains two positive integers $H$ and $W$ delimited by a space, where $H$ is the height of the grid and $W$ is the width of the grid ($2 \leq H, W \leq 200$). </p> <p> The $i$-th line of the subsequent $H$ lines gives a string $map_i$, which represents situation in the ground of palace. </p> <p> $map_i$ is a string of length $W$, and the $j$-th character of $map_i$ represents the state of the cell of the $i$-th row and the $j$-th column. </p> <p> '@', '\$', '%', '.', and '#' represent the princess, a soldier, the escape hatch, an empty cell, and a wall, respectively. It is guaranteed that there exists only one '@', only one '%', and more than or equal to zero '\$' in the grid. </p> <h3>Output</h3> <p> Output a line containing a word "Yes", if the princess can escape from the palace. Otherwise, output "No". </p> <h3>Sample Input 1</h3> <pre> 2 4 %.@\$ ..\$\$ </pre> <h3>Output for the Sample Input 1</h3> <pre> Yes </pre> <h3>Sample Input 2</h3> <pre> 3 4 .%.. .##. .@\$. </pre> <h3>Output for the Sample Input 2</h3> <pre> Yes </pre> <h3>Sample Input 3</h3> <pre> 2 3 %\$@ ### </pre> <h3>Output for the Sample Input 3</h3> <pre> No </pre> <h3>Sample Input 4</h3> <pre> 2 3 @#\$ .%. </pre> <h3>Output for the Sample Input 4</h3> <pre> No </pre> <h3>Sample Input 5</h3> <pre> 2 2 @% .. </pre> <h3>Output for the Sample Input 5</h3> <pre> Yes </pre>
p02604
<span class="lang-en"> <p>Score: <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>New AtCoder City has an infinite grid of streets, as follows:</p> <ul> <li>At the center of the city stands a clock tower. Let <var>(0, 0)</var> be the coordinates of this point.</li> <li>A straight street, which we will call <em>East-West Main Street</em>, runs east-west and passes the clock tower. It corresponds to the <var>x</var>-axis in the two-dimensional coordinate plane.</li> <li>There are also other infinitely many streets parallel to East-West Main Street, with a distance of <var>1</var> between them. They correspond to the lines <var>\ldots, y = -2, y = -1, y = 1, y = 2, \ldots</var> in the two-dimensional coordinate plane.</li> <li>A straight street, which we will call <em>North-South Main Street</em>, runs north-south and passes the clock tower. It corresponds to the <var>y</var>-axis in the two-dimensional coordinate plane.</li> <li>There are also other infinitely many streets parallel to North-South Main Street, with a distance of <var>1</var> between them. They correspond to the lines <var>\ldots, x = -2, x = -1, x = 1, x = 2, \ldots</var> in the two-dimensional coordinate plane.</li> </ul> <p>There are <var>N</var> residential areas in New AtCoder City. The <var>i</var>-th area is located at the intersection with the coordinates <var>(X_i, Y_i)</var> and has a population of <var>P_i</var>. Each citizen in the city lives in one of these areas.</p> <p><strong>The city currently has only two railroads, stretching infinitely, one along East-West Main Street and the other along North-South Main Street.</strong><br/> M-kun, the mayor, thinks that they are not enough for the commuters, so he decides to choose <var>K</var> streets and build a railroad stretching infinitely along each of those streets.</p> <p>Let the <em>walking distance</em> of each citizen be the distance from his/her residential area to the nearest railroad.<br/> M-kun wants to build railroads so that the sum of the walking distances of all citizens, <var>S</var>, is minimized.</p> <p>For each <var>K = 0, 1, 2, \dots, N</var>, what is the minimum possible value of <var>S</var> after building railroads?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3> <ul> <li><var>1 \leq N \leq 15</var></li> <li><var>-10 \ 000 \leq X_i \leq 10 \ 000</var></li> <li><var>-10 \ 000 \leq Y_i \leq 10 \ 000</var></li> <li><var>1 \leq P_i \leq 1 \ 000 \ 000</var></li> <li>The locations of the <var>N</var> residential areas, <var>(X_i, Y_i)</var>, are all distinct.</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>X_1</var> <var>Y_1</var> <var>P_1</var> <var>X_2</var> <var>Y_2</var> <var>P_2</var> <var>:</var> <var>:</var> <var>:</var> <var>X_N</var> <var>Y_N</var> <var>P_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3> <p>Print the answer in <var>N+1</var> lines.<br/> The <var>i</var>-th line <var>(i = 1, \ldots, N+1)</var> should contain the minimum possible value of <var>S</var> after building railroads for the case <var>K = i-1</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 2 300 3 3 600 1 4 800 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2900 900 0 0 </pre> <p>When <var>K = 0</var>, the residents of Area <var>1</var>, <var>2</var>, and <var>3</var> have to walk the distances of <var>1</var>, <var>3</var>, and <var>1</var>, respectively, to reach a railroad.<br/> Thus, the sum of the walking distances of all citizens, <var>S</var>, is <var>1 \times 300 + 3 \times 600 + 1 \times 800 = 2900</var>.</p> <p>When <var>K = 1</var>, if we build a railroad along the street corresponding to the line <var>y = 4</var> in the coordinate plane, the walking distances of the citizens of Area <var>1</var>, <var>2</var>, and <var>3</var> become <var>1</var>, <var>1</var>, and <var>0</var>, respectively.<br/> Then, <var>S = 1 \times 300 + 1 \times 600 + 0 \times 800 = 900</var>.<br/> We have many other options for where we build the railroad, but none of them makes <var>S</var> less than <var>900</var>.</p> <p>When <var>K = 2</var>, if we build a railroad along the street corresponding to the lines <var>x = 1</var> and <var>x = 3</var> in the coordinate plane, all citizens can reach a railroad with the walking distance of <var>0</var>, so <var>S = 0</var>. We can also have <var>S = 0</var> when <var>K = 3</var>.</p> <p>The figure below shows the optimal way to build railroads for the cases <var>K = 0, 1, 2</var>.<br/> The street painted blue represents the roads along which we build railroads.</p> <p><img height="320" src="https://img.atcoder.jp/m-solutions2020/fc274bed71a4c37706550fa083496d39.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 3 5 400 5 3 700 5 5 1000 5 7 700 7 5 400 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>13800 1600 0 0 0 0 </pre> <p>The figure below shows the optimal way to build railroads for the cases <var>K = 1, 2</var>.</p> <p><img height="350" src="https://img.atcoder.jp/m-solutions2020/7c6b7a31998a1c46fba4c0679b023822.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 2 5 1000 5 2 1100 5 5 1700 -2 -5 900 -5 -2 600 -5 -5 2200 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>26700 13900 3200 1200 0 0 0 </pre> <p>The figure below shows the optimal way to build railroads for the case <var>K = 3</var>.</p> <p><img height="390" src="https://img.atcoder.jp/m-solutions2020/0453fa9c2f02c3bd5d5f9e20d0e8e589.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>8 2 2 286017 3 1 262355 2 -2 213815 1 -3 224435 -2 -2 136860 -3 -1 239338 -2 2 217647 -1 3 141903 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>2576709 1569381 868031 605676 366338 141903 0 0 0 </pre> <p>The figure below shows the optimal way to build railroads for the case <var>K = 4</var>.</p> <p><img height="390" src="https://img.atcoder.jp/m-solutions2020/464ce76d1d7d72638eb372342f8386c5.png"/></p></section> </div> </span>
p00639
<H1><font color="#000000">Problem C:</font> Accelerated Railgun</H1> <p> She catched the thrown coin that draws parabolic curve with her sparkling fingers. She is an ESPer. Yes, she is an electro-master who has the third strongest power among more than one million ESPers in the city. Being flicked by her thumb, the coin is accelerated by electromagnetic force and is shot as Fleming's right-hand rule. Even if she holds back the initial velocity of the coin exceeds three times of the speed of sound. The coin that is shot in such velocity is heated because of air friction and adiabatic compression. As a result coin melts and shines in orange. This is her special ability, called railgun. The strength of railgun can make a hole of two meters in diameter on a concrete wall. </p> <p> She had defeated criminals such as post office robberies and bank robberies in the city with her ability. And today, she decided to destroy a laboratory that is suspected to making some inhumane experiments on human body. Only her railgun can shoot it. </p> <p> The railgun with a coin cannot destroy the laboratory because of lack of power. Since she have found a powered-suit nearby, so she decided to use it as a projectile. However, in this case it is difficult to take sight properly because the suit is much bigger and heavier than coins. Therefore she only can shoot the suit with certain velocity vector from her current position. Depending on the position of the laboratory, her railgun may not hit it and become a firework. </p> <p> Therefore she asked cooperation to the strongest ESPer in the city. He can change direction of a moving object as one of uncountable application of his ESP. Let's consider a 2-dimensional plane where the laboratory is on the origin (0, 0). She shoots a projectile from <i>P</i> = (<i>px</i>, <i>py</i>) with velocity vector <i>V</i> = (<i>vx</i>, <i>vy</i>). His ESP makes a virtual wall of radius <i>R</i> (= 1.0) centered on the origin. When projectile collides with the wall, it is reflected so that incident angle will be equal to reflection angle. </p> <p> Range of railgun is limited to <i>D</i>, because air friction decreases velocity of projectile and heat may melts projectile completely. Under these conditions, please write a program that judges if the railgun hits the laboratory. Size of the laboratory and the suit is ignorablly small. After the railgun is shot, it is allowed to pass through <i>P</i> again. </p> <H2>Input</H2> <p> Input consists of several datasets. </p> <p> The first line of each dataset contains a real number <i>D</i>. </p> <p> Next line contains 4 real numbers, which means <i>px</i>, <i>py</i>, <i>vx</i>, <i>vy</i>, respectively. </p> <p> Input terminates when <i>D</i> = 0. </p> <H2>Output</H2> <p> For each dataset, if the railgun hits, output the distance it moved until hits. Otherwise output '<span>impossible</span>' (without quotes). </p> <p> You can print any number of digits and answer with an error less than 1.0e-6 will be accepted. </p> <H2>Constraints</H2> <ul> <li>Judge data never include dataset where the answer is (<i>D</i> - 1.0e-3) or bigger. </li> <li>0.0001 &le; |V| &le; 0.9999</li> <li>0.0001 &le; |P| &le; 0.9999</li> <li>D &le; 50</li> </ul> <H2>Sample Input</H2> <pre> 10.0 0.5 0.0 -0.2 0.0 1.0 0.1 0.0 0.2 0.2 0 </pre> <H2>Output for the Sample Input</H2> <pre> 0.50000000 impossible </pre>
p03916
<span class="lang-en"> <p>Score : <var>1600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> squares in a row, numbered <var>1</var> through <var>N</var> from left to right. Snuke and Rng are playing a board game using these squares, described below:</p> <ol> <li>First, Snuke writes an integer into each square.</li> <li>Each of the two players possesses one piece. Snuke places his piece onto square <var>1</var>, and Rng places his onto square <var>2</var>.</li> <li>The player whose piece is to the left of the opponent's, moves his piece. The destination must be a square to the right of the square where the piece is currently placed, and must not be a square where the opponent's piece is placed.</li> <li>Repeat step <em>3.</em> When the pieces cannot be moved any more, the game ends.</li> <li>The score of each player is calculated as the sum of the integers written in the squares where the player has placed his piece before the end of the game.</li> </ol> <p>Snuke has already written an integer <var>A_i</var> into square <var>i (1≦i≦N-1)</var>, but not into square <var>N</var> yet. He has decided to calculate for each of <var>M</var> integers <var>X_1,X_2,...,X_M</var>, if he writes it into square <var>N</var> and the game is played, what the value "(Snuke's score) - (Rng's score)" will be. Here, it is assumed that each player moves his piece to maximize the value "(the player's score) - (the opponent's score)".</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>3≦N≦200,000</var></li> <li><var>0≦A_i≦10^6</var></li> <li>The sum of all <var>A_i</var> is at most <var>10^6</var>.</li> <li><var>1≦M≦200,000</var></li> <li><var>0≦X_i≦10^9</var></li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Scores</h3><ul> <li><var>700</var> points will be awarded for passing the test set satisfying <var>M=1</var>.</li> <li>Additional <var>900</var> points will be awarded for passing the test set without additional constraints.</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-1}</var> <var>M</var> <var>X_1</var> <var>X_2</var> <var>:</var> <var>X_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>For each of the <var>M</var> integers <var>X_1, ..., X_M</var>, print the value "(Snuke's score) - (Rng's score)" if it is written into square <var>N</var>, one per line.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 2 7 1 8 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>0 </pre> <p>The game proceeds as follows, where <code>S</code> represents Snuke's piece, and <code>R</code> represents Rng's.</p> <p><img alt="" src="https://atcoder.jp/img/code-festival-2016-final/0c38db3b7902579a8bc2d0798b8dda27.png"/></p> <p>Both player scores <var>10</var>, thus "(Snuke's score) - (Rng's score)" is <var>0</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>9 2 0 1 6 1 1 2 6 5 2016 1 1 2 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2001 6 6 7 7 </pre></section> </div> </span>
p01478
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], skipTags: ["script","noscript","style","textarea","code"], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script> <h2>問題文</h2> <p>$1,2,...,N$ を並べ替えた順列がある。2つの異なる数字 $i$, $j$ を選んで入れ替えることを繰り返してソートされた状態( $1,2,...,N$ の順番に並んだ状態)にしたい。 数字 $i$, $j$ を入れ替える度にコストが $c_{i,j}$ 必要になる。</p> <p>順列 $p$ に対して、 $p$ をソートするのに必要な最小コストを $f(p)$ とする。 $f(p)$ のとりうる最大値を求めよ。</p> <h2>入力</h2> <p>入力は以下のフォーマットに従う。与えられる数は全て整数である。</p> <pre>$N$ $c_{1,1}$ $c_{1,2}$ $...$ $c_{1,N}$ $c_{2,1}$ $c_{2,2}$ $...$ $c_{2,N}$ $...$ $c_{N,1}$ $c_{N,2}$ $...$ $c_{N,N}$</pre> <h2>制約</h2> <ul><li>$2 \leq N \leq 8$</li> <li>$0 \leq c_{i,j} \leq 10^5$</li> <li>$c_{i,j} = c_{j,i}$</li> <li>$c_{i,i} = 0$</li></ul> <h2>出力</h2> <p>最大値を1行に出力せよ。</p> <h2>Sample Input 1</h2> <pre>3 0 1 3 1 0 8 3 8 0</pre> <h2>Output for the Sample Input 1</h2> <pre>5</pre> <p>もっとも必要コストが大きい順列は$\{1,3,2\}$で、</p> <ol><li>1,2を入れ替えて$\{2,3,1\}$とする。</li> <li>1,3を入れ替えて$\{2,1,3\}$とする。</li> <li>1,2を入れ替えて$\{1,2,3\}$とする。</li></ol> <p>以上の操作によりコスト5でソートでき、またこれが最善であることが証明できる。 他の順列もコスト5以下でソートできることを示すことができる。</p>
p03445
<span class="lang-en"> <p>Score : <var>2000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an <var>H \times W</var> grid. The square at the top-left corner will be represented by <var>(0, 0)</var>, and the square at the bottom-right corner will be represented by <var>(H-1, W-1)</var>.</p> <p>Of those squares, <var>N</var> squares <var>(x_1, y_1), (x_2, y_2), ..., (x_N, y_N)</var> are painted black, and the other squares are painted white.</p> <p>Let the shortest distance between white squares <var>A</var> and <var>B</var> be the minimum number of moves required to reach <var>B</var> from <var>A</var> <strong>visiting only white squares</strong>, where one can travel to an adjacent square sharing a side (up, down, left or right) in one move.</p> <p>Since there are <var>H × W - N</var> white squares in total, there are <var>_{(H×W-N)}C_2</var> ways to choose two of the white squares.</p> <p>For each of these <var>_{(H×W-N)}C_2</var> ways, find the shortest distance between the chosen squares, then find the sum of all those distances, modulo <var>1</var> <var>000</var> <var>000</var> <var>007=10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq H, W \leq 10^6</var></li> <li><var>1 \leq N \leq 30</var></li> <li><var>0 \leq x_i \leq H-1</var></li> <li><var>0 \leq y_i \leq W-1</var></li> <li>If <var>i \neq j</var>, then either <var>x_i \neq x_j</var> or <var>y_i \neq y_j</var>.</li> <li>There is at least one white square.</li> <li>For every pair of white squares <var>A</var> and <var>B</var>, it is possible to reach <var>B</var> from <var>A</var> visiting only white squares.</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>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 sum of the shortest distances, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>20 </pre> <p>We have the following grid (<code>.</code>: a white square, <code>#</code>: a black square).</p> <pre>... .#. </pre> <p>Let us assign a letter to each white square as follows:</p> <pre>ABC D#E </pre> <p>Then, we have:</p> <ul> <li>dist(A, B) = <var>1</var></li> <li>dist(A, C) = <var>2</var></li> <li>dist(A, D) = <var>1</var></li> <li>dist(A, E) = <var>3</var></li> <li>dist(B, C) = <var>1</var></li> <li>dist(B, D) = <var>2</var></li> <li>dist(B, E) = <var>2</var></li> <li>dist(C, D) = <var>3</var></li> <li>dist(C, E) = <var>1</var></li> <li>dist(D, E) = <var>4</var></li> </ul> <p>where dist(A, B) is the shortest distance between A and B. The sum of these distances is <var>20</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 3 1 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>16 </pre> <p>Let us assign a letter to each white square as follows:</p> <pre>ABC DE# </pre> <p>Then, we have:</p> <ul> <li>dist(A, B) = <var>1</var></li> <li>dist(A, C) = <var>2</var></li> <li>dist(A, D) = <var>1</var></li> <li>dist(A, E) = <var>2</var></li> <li>dist(B, C) = <var>1</var></li> <li>dist(B, D) = <var>2</var></li> <li>dist(B, E) = <var>1</var></li> <li>dist(C, D) = <var>3</var></li> <li>dist(C, E) = <var>2</var></li> <li>dist(D, E) = <var>1</var></li> </ul> <p>The sum of these distances is <var>16</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 3 1 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>64 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>4 4 4 0 1 1 1 2 1 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>268 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>1000000 1000000 1 0 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>333211937 </pre></section> </div> </span>
p01182
<H1><font color="#000">Problem B:</font> Restaurant</H1> <p> Steve runs a small restaurant in a city. Also, as he is the only cook in his restaurant, he cooks everything ordered by customers. </p> <p> Basically he attends to orders by <i>first-come-first-served</i> principle. He takes some prescribed time to prepare each dish. As a customer can order more than one dish at a time, Steve starts his cooking with the dish that takes the longest time to prepare. In case that there are two or more dishes that take the same amount of time to prepare, he makes these dishes in the sequence as listed in the menu card of his restaurant. Note that he does not care in which order these dishes have been ordered. When he completes all dishes ordered by a customer, he immediately passes these dishes to the waitress and has them served to the customer. Time for serving is negligible. </p> <p> On the other hand, during his cooking for someone’s order, another customer may come and order dishes. For his efficiency, he decided to prepare together multiple dishes of the same if possible. When he starts cooking for new dishes, he looks over the orders accepted by that time (including the order accepted exactly at that time if any), and counts the number of the same dishes he is going to cook next. Time required for cooking is the same no matter how many dishes he prepare at once. Unfortunately, he has only limited capacity in the kitchen, and hence it is sometimes impossible that the requested number of dishes are prepared at once. In such cases, he just prepares as many dishes as possible. </p> <p> Your task is to write a program that simulates the restaurant. Given a list of dishes in the menu card and orders from customers with their accepted times, your program should output a list of times when each customer is served. </p> <H2>Input</H2> <p> The input contains multiple data sets. Each data set is in the format below: </p> <pre> <i>N M</i> <i>Name</i><sub>1</sub> <i>Limit</i><sub>1</sub> <i>Time</i><sub>1</sub> ... <i>Name</i><sub><i>N</i></sub> <i>Limit</i><sub><i>N</i></sub> <i>Time</i><sub><i>N</i></sub> <i>T</i><sub>1</sub> <i>K</i><sub>1</sub> <i>Dish</i><sub>1,1</sub> . . . <i>Dish</i><sub>1,<i>K</i><sub>1</sub></sub> ... <i>T</i><sub><i>M</i></sub> <i>K</i><sub><i>M</i></sub> <i>Dish</i><sub><i>M</i>,1</sub> . . . <i>Dish</i><sub><i>M</i>,<i>K</i><sub><i>M</i></sub></sub> </pre> <p> Here, <i>N</i> (1 &le; <i>N</i> &le; 20) and <i>M</i> (1 &le; <i>M</i> &le; 100) specify the number of entries in the menu card and the number of orders that Steve will receive, respectively; each <i>Name<sub>i</sub></i> is the name of a dish of the <i>i</i>-th entry in the menu card, which consists of up to 20 alphabetical letters; <i>Limit<sub>i</sub></i> (1 &le; <i>Limit<sub>i</sub></i> &le; 10) is the number of dishes he can prepare at the same time; <i>Time<sub>i</sub></i> (1 &le; <i>Time<sub>i</sub></i> &le; 1000) is time required to prepare a dish (or dishes) of the <i>i</i>-th entry; <i>T<sub>j</sub></i> (1 &le; <i>T<sub>j</sub></i> &le; 10000000) is the time when the <i>j</i>-th order is accepted; <i>K<sub>j</sub></i> (1 &le; <i>K<sub>j</sub></i> &le; 10) is the number of dishes in the <i>j</i>-th order; and each <i>Dish<sub>j,k</sub></i> represents a dish in the <i>j</i>-th order. </p> <p> You may assume that every dish in the orders is listed in the menu card, but you should note that each order may contain multiple occurrences of the same dishes. The orders are given in ascending order by <i>T<sub>j</sub></i> , and no two orders are accepted at the same time. </p> <p> The input is terminated with a line that contains two zeros. This is not part of data sets and hence should not be processed. </p> <H2>Output</H2> <p> Your program should produce the output of M -lines for each data set. The i-th line of the output should contain a single integer that indicates the time when the i-th order will be completed and served to the customer. </p> <p> Print a blank line between two successive data sets. </p> <H2>Sample Input</H2> <pre> 5 4 Ramen 3 10 Chahan 5 5 Gyoza 5 10 Rice 1 1 Soup 1 1 5 2 Ramen Gyoza 10 6 Chahan Gyoza Soup Ramen Gyoza Rice 20 1 Chahan 25 1 Ramen 0 0 </pre> <H2>Output for the Sample Input</H2> <pre> 25 42 40 35 </pre>
p01028
<h1>Problem A: Yu-kun Likes an Integer</h1> <h2>Background</h2> <p> 会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらい数字が大好きだ。そんなゆう君は最近、表に0から9までのいずれかの数字の書かかれたプレートを<var>n</var>枚購入して<var>n</var>桁の数を作る遊びに熱中している。 今月もゆう君は貰ったお小遣いで<var>n</var>枚のプレートを買いに行こうと考えていた。 </p> <h2>Problem</h2> <p> プレートは表に書かれている数字によって値段が異なる。 それらを使って作れる<var>n</var>桁の数をできる限り小さくしたい。 ゆう君の所持金額と各プレートの値段、購入するプレートの枚数が与えられるので、そこから作ることのできる数の最小値を求めよ。 </p> <p> 購入するプレートの値段の総和が所持金額を超えなければ自由にプレートを購入できる。 同じ数字の書かれたプレートを複数枚購入しても良い。 </p> <p> プレートは必ず<var>n</var>枚購入しなければならない。<var>n</var>枚購入できない場合は "NA" と出力すること。 </p> <p> 購入後は<var>n</var>枚のプレートを任意の順番で一直線上に並べる。 そうしてできる数字の列を10進数の数とし、その値が最小になるようにしたい。 先頭に1つ以上の0が連続していても良い。 ( 例えば 0019 なら 19 となり, 0000 なら 0 として考える ) </p> <p> 図1は0、1、9のプレートを購入した場合についての説明である。 </p> <center> <table> <tr> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE3_AizuCamp14_Day2_A_00.png" alt="figure1" height="150" witdh="150"> </tr> <tr> <td ALIGN="center">図1 : 0,1,9のプレートを購入した場合</td> </tr> </table> </center> <h2>Hint</h2> <p> この問題を解くに当たって、以下のことを参考にしても良い。 整数値を文字列に変換する方法を示す。 value を文字列として str に代入する。 </p> <h4>Cの場合</h4> <pre> #include&lt;stdio.h&gt; int main(){ int value = 123; // この値を文字列に変換する char str[6]; // この変数に value を文字列にしたものが入る sprintf(str,"%d",value); return 0; } </pre> <h4>C++の場合</h4> <pre> #include&lt;sstream&gt; using namespace std; int main(){ int value = 123; // この値を文字列に変換する string str; // この変数に value を文字列にしたものが入る stringstream ss; ss << value; ss >> str; return 0; } </pre> <h4>JAVAの場合</h4> <pre> class Main { public static void main(String args[]){ int value = 123; // この値を文字列に変換する String str = new Integer(value).toString(); // この変数に value を文字列にしたものが入る } } </pre> <h2>Input</h2> <pre> <var>n</var> <var>m</var> <var>c</var><sub>0</sub> <var>c</var><sub>1</sub> <var>c</var><sub>2</sub> ... <var>c</var><sub>9</sub> </pre> <p> 1行目に2つの整数 <var>n</var> ,<var>m</var> が空白区切りで与えられる。<var>n</var> は購入するプレートの枚数, <var>m</var> はゆう君の所持金額を表す。 </p> <p> 2行目には10個の整数が空白区切りで与えられる。 <var>c<sub>i</sub></var> ( <var>i</var> は0以上9以下 ) は表に <var>i</var> が書かれたプレートの値段を表す。 </p> <h2>Constraints</h2> <p>入力は以下の条件を満たす。</p> <ul> <li>1 &le; <var>n</var> &le; 5</li> <li>0 &le; <var>m</var> &le; 500</li> <li>1 &le; <var>c<sub>i</sub></var> &le; 1000 ( 0 &le; <var>i</var> &le; 9 ) </li> </ul> <h2>Output</h2> <p> <var>n</var>枚のプレートを購入し、それらを任意の順番で並べることでできる数の値の最小値を出力せよ。 </p> <p> 先頭にいくつかの0を含む場合はそのまま出力すること。 ( 例えば答えが 0019 の場合は先頭の0を取り除いて 19 とするのではなく、そのまま 0019 と出力すること ) 所持金額では <var>n</var> 枚プレートを購入できない場合は "NA" と出力すること。 </p> <h2>Sample Input 1</h2> <pre> 1 10 1 2 3 4 5 6 7 8 9 10 </pre> <h2>Sample Output 1</h2> <pre> 0 </pre> <h2>Sample Input 2</h2> <pre> 3 10 8 4 5 3 5 6 9 10 11 2 </pre> <h2>Sample Output 2</h2> <pre> 119 </pre> <h2>Sample Input 3</h2> <pre> 5 30 25 51 32 9 2 1 10 2 5 10 </pre> <h2>Sample Output 3</h2> <pre> 04555 </pre> <h2>Sample Input 4</h2> <pre> 5 100 101 101 101 101 101 101 101 101 101 101 </pre> <h2>Sample Output 4</h2> <pre> NA </pre>
p03015
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a positive integer <var>L</var> in base two. How many pairs of non-negative integers <var>(a, b)</var> satisfy the following conditions?</p> <ul> <li><var>a + b \leq L</var></li> <li><var>a + b = a \mbox{ XOR } b</var></li> </ul> <p>Since there can be extremely many such pairs, print the count modulo <var>10^9 + 7</var>.</p> <p><details> <summary style="display: list-item; outline: none;">What is XOR?</summary></details></p> <p>The XOR of integers <var>A</var> and <var>B</var>, <var>A \mbox{ XOR } B</var>, is defined as follows:</p> <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> <p>For example, <var>3 \mbox{ XOR } 5 = 6</var>. (In base two: <var>011 \mbox{ XOR } 101 = 110</var>.)</p> <p></p></section></div></span>
p03500
<span class="lang-en"> <p>Score : <var>1200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> non-negative integers written on the blackboard: <var>A_1, ..., A_N</var>.</p> <p>Snuke can perform the following two operations at most <var>K</var> times in total in any order:</p> <ul> <li>Operation A: Replace each integer <var>X</var> on the blackboard with <var>X</var> divided by <var>2</var>, rounded down to the nearest integer.</li> <li>Operation B: Replace each integer <var>X</var> on the blackboard with <var>X</var> minus <var>1</var>. This operation cannot be performed if one or more <var>0</var>s are written on the blackboard.</li> </ul> <p>Find the number of the different possible combinations of integers written on the blackboard after Snuke performs the operations, modulo <var>1,000,000,007</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 200</var></li> <li><var>1 \leq A_i \leq 10^{18}</var></li> <li><var>1 \leq K \leq 10^{18}</var></li> <li><var>A_i</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> <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 different possible combinations of integers written on the blackboard after Snuke performs the operations, modulo <var>1,000,000,007</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 2 5 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>6 </pre> <p>There are six possible combinations of integers on the blackboard: <var>(1, 1)</var>, <var>(1, 2)</var>, <var>(2, 3)</var>, <var>(3, 5)</var>, <var>(4, 6)</var> and <var>(5, 7)</var>. For example, <var>(1, 2)</var> can be obtained by performing Operation A and Operation B in this order.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 4 10 13 22 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>20 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1 100 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>11 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>10 123456789012345678 228344079825412349 478465001534875275 398048921164061989 329102208281783917 779698519704384319 617456682030809556 561259383338846380 254083246422083141 458181156833851984 502254767369499613 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>164286011 </pre></section> </div> </span>
p01497
<H1><font color="#000">Problem B: </font>Bubble Puzzle</H1> <p> A browser-based puzzle game called "Bubble Puzzle" is now popular on the Internet. </p> <p> The puzzle is played on a 4 &times; 4 grid, and initially there are several bubbles in the grid squares. Each bubble has a state expressed by a positive integer, and the state changes when the bubble gets stimulated. You can stimulate a bubble by clicking on it, and this will increase the bubble's state by 1. You can also click on an empty square. In this case, a bubble with state 1 is newly created in the square. </p> <p> When the state of a bubble becomes 5 or higher, the bubble blows up and disappears, and small waterdrops start flying in four directions (up, down, left and right). These waterdrops move one square per second. At the moment when a bubble blows up, 4 waterdrops are in the square the bubble was in, and one second later they are in the adjacent squares, and so on. </p> <p> A waterdrop disappears either when it hits a bubble or goes outside the grid. When a waterdrop hits a bubble, the state of the bubble gets increased by 1. Similarly, if a bubble is hit by more than one water drop at the same time, its state is increased by the number of the hitting waterdrops. Please note that waterdrops do not collide with each other. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_bubblePuzzle"> </center> <p> As shown in the figure above, waterdrops created by a blow-up may cause other blow-ups. In other words, one blow-up can trigger a chain reaction. You are not allowed to click on any square while waterdrops are flying (i.e., you have to wait until a chain reaction ends). The goal of this puzzle is to blow up all the bubbles and make all the squares empty as quick as possible. </p> <p> Your mission is to calculate the minimum number of clicks required to solve the puzzle. </p> <H2>Input</H2> <p> The input consists of 4 lines, each contains 4 nonnegative integers smaller than 5. Each integer describes the initial states of bubbles on grid squares. 0 indicates that the corresponding square is empty. </p> <H2>Output</H2> <p> Output the minimum number of clicks to blow up all the bubbles on the grid in a line. If the answer is bigger than 5, output -1 instead. No extra characters should appear in the output. </p> <H2>Sample Input 1</H2> <pre> 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 </pre> <H2>Output for the Sample Input 1</H2> <pre> 1 </pre> <br/> <H2>Sample Input 2</H2> <pre> 2 4 4 1 2 4 4 1 2 4 4 1 2 4 4 1 </pre> <H2>Output for the Sample Input 2</H2> <pre> 5 </pre> <br/> <H2>Sample Input 3</H2> <pre> 2 4 3 4 2 2 4 4 3 3 2 2 2 3 3 3 </pre> <H2>Output for the Sample Input 3</H2> <pre> 3 </pre> <br/>
p03150
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>A string is called a KEYENCE string when it can be changed to <code>keyence</code> by removing its contiguous substring (possibly empty) only once.</p> <p>Given a string <var>S</var> consisting of lowercase English letters, determine if <var>S</var> is a KEYENCE string.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>The length of <var>S</var> is between <var>7</var> and <var>100</var> (inclusive).</li> <li><var>S</var> consists of lowercase English letters.</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> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If <var>S</var> is a KEYENCE string, print <code>YES</code>; otherwise, print <code>NO</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>keyofscience </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>YES </pre> <p><code>keyence</code> is an abbreviation of <code>key of science</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>mpyszsbznf </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>ashlfyha </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>NO </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>keyence </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>YES </pre></section> </div> </span>
p02311
<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>Common Tangent</H1> <p> Find common tangent lines of two circles $c1$ and $c2$. </p> <H2>Input</H2> <p> Center coordinates ($cix$, $ciy$) and radii $cir$ of two circles $c1$ are $c2$ are given in the following format. </p> <p> $c1x \; c1y \; c1r$ <br> $c2x \; c2y \; c2r$ </p> <p> All input values are given in integers. </p> <H2>Output</H2> <p> Print coordinates of the tangent points on circle $c1$ based on the following rules. </p> <ul> <li>Print the coordinate with smaller $x$ first. In case of a tie, print the coordinate with smaller $y$ first. </li> </ul> <p> The output values should be in a decimal fraction with an error less than 0.00001. </p> <H2>Constraints</H2> <ul> <li>$-1,000 \leq c1x, c1y, c2x, c2y \leq 1,000$</li> <li>$1 \leq c1r, c2r \leq 1,000$</li> <li>$c1$ and $c2$ are different</li> </ul> <H2>Sample Input 1</H2> <pre> 1 1 1 6 2 2 </pre> <H2>Sample Output 1</H2> <pre> 0.6153846154 1.9230769231 1.0000000000 0.0000000000 1.4183420913 1.9082895434 1.7355040625 0.3224796874 </pre> <H2>Sample Input 2</H2> <pre> 1 2 1 4 2 2 </pre> <H2>Sample Output 2</H2> <pre> 0.6666666667 1.0571909584 0.6666666667 2.9428090416 2.0000000000 2.0000000000 </pre> <H2>Sample Input 3</H2> <pre> 1 2 1 3 2 2 </pre> <H2>Sample Output 3</H2> <pre> 0.5000000000 1.1339745962 0.5000000000 2.8660254038 </pre> <H2>Sample Input 4</H2> <pre> 0 0 1 1 0 2 </pre> <H2>Sample Output 4</H2> <pre> -1.0000000000 0.0000000000 </pre> <H2>Sample Input 5</H2> <pre> 0 0 1 0 0 2 </pre> <H2>Sample Output 5</H2> <pre> </pre> <p> No output. </p>
p00286
<h1>古代遺跡の謎</h1> <p> 古代遺跡から超大型あみだくじが発見された。学者達はこれをVLA(Very Long Amidakuji)と命名した。分析の結果、VLAは <var>N</var> 本の縦棒を持ついくつかの部品の組み合わせで構成され、多くの場合繰り返しを含んでいることがわかった。例えば、図のVLAは2種類の部品AとBを使った4つの部品から構成されている。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_PCK2013_monument" width="400"> </center> <p> このVLAの上端に左から順番に 1,2,3,4,5 と番号をつけ、それぞれの番号からVLAをたどり(あみだくじのたどり方については問題文最後の補足を参照してください)、下端にたどり着いた場所の縦棒にその番号を振る。すべての番号についてたどり終えた後で、下端に振られた番号を左から読むと 4,2,5,1,3 になる。このように、1,2,3,4,5 という番号の列が 4,2,5,1,3 という列に変換される。 </p> <p> 彼らはVLAによって 1 から <var>N</var> までの番号の列がどのように変換されるのかを知りたいが、手作業でたどるのは大変そうだ。そこでパソコン甲子園出場者である優秀なプログラマーの君たちに、VLAのシミュレーションを依頼した。 </p> <p> VLAはとても長いため、そのままの状態ではプログラマーたちに情報を伝えることができない。そこで学者たちは、部品の種類を英大文字1つで表し、VLAを式で表現することにした。 </p> <p> 部品は同じ位置関係である縦棒が一致するように、左から順番に <span>+<span> 演算子で接続される。たとえば、上からA,B,C,D という順番で連結された部品の列は <span>A+B+C+D</span> という式で表記される。 </p> <p> 部品の列を表す式 <var>seq</var> の <var>m</var> 回の繰り返しは、<var>m</var>(<var>seq</var>) と表記できる。たとえば、<span>A+B</span> の2回の繰り返しは <span>2(A+B)</span> と表せ、これは <span>A+B+A+B</span> と同じである。また、<var>seq</var> が1つの部品 <var>a</var> の場合は、<var>a</var> を囲むかっこを省略しても構わない。<span>4(A+B)</span> と <span>3(X+Y)</span> を、この順番で連結したVLAは <span>4(A+B)+3(X+Y)</span> と表せる。また、かっこは <span>2(4(A+B)+3(X+Y))+10C</span> のように入れ子にしても構わない。 </p> <p> それでは、縦棒の数 <var>N</var>、各部品の情報、複数のVLAの式を入力とし、各式が表すVLAによって1から <var>N</var> までの番号の列がどのように変換されるかを出力するプログラムを作成しなさい。 </p> <p> (※補足:あみだくじのたどり方について)<br> あみだくじのある縦棒の上端から出発して上から下へ進む。ただし、横棒がある地点ではその横棒でつながった別の縦棒に移動する。これを、縦棒の下端にたどり着くまで繰り返す。 </p> <h2>入力</h2> <p> 入力は1つのデータセットからなる。入力データは以下の形式で与えられる。 </p> <pre> <var>N</var> <var>K</var> <var>p<sub>1</sub></var> <var>h<sub>1</sub></var> <var>g</var><sub>1</sub>(1,1) <var>g</var><sub>1</sub>(1,2) .. <var>g</var><sub>1</sub>(1,<var>N</var>-1) <var>g</var><sub>1</sub>(2,1) <var>g</var><sub>1</sub>(2,2) .. <var>g</var><sub>1</sub>(2,<var>N</var>-1) : <var>g</var><sub>1</sub>(<var>h</var><sub>1</sub>-1,1) <var>g</var><sub>1</sub>(<var>h</var><sub>1</sub>-1,2) .. <var>g</var><sub>1</sub>(<var>h</var><sub>1</sub>-1,<var>N</var>-1) <var>p<sub>2</sub></var> <var>h<sub>2</sub></var> <var>g</var><sub>2</sub>(1,1) <var>g</var><sub>2</sub>(1,2) .. <var>g</var><sub>2</sub>(1,<var>N</var>-1) <var>g</var><sub>2</sub>(2,1) <var>g</var><sub>2</sub>(2,2) .. <var>g</var><sub>2</sub>(2,<var>N</var>-1) : <var>g</var><sub>2</sub>(<var>h</var><sub>2</sub>-1,1) <var>g</var><sub>2</sub>(<var>h</var><sub>2</sub>-1,2) .. <var>g</var><sub>2</sub>(<var>h</var><sub>2</sub>-1,<var>N</var>-1) : <var>p<sub>K</sub></var> <var>h<sub>K</sub></var> <var>g<sub>K</sub></var>(1,1) <var>g<sub>K</sub></var>(1,2) .. <var>g<sub>K</sub></var>(1,<var>N</var>-1) <var>g<sub>K</sub></var>(2,1) <var>g<sub>K</sub></var>(2,2) .. <var>g<sub>K</sub></var>(2,<var>N</var>-1) : <var>g<sub>K</sub></var>(<var>h<sub>K</sub></var>-1,1) <var>g<sub>K</sub></var>(<var>h<sub>K</sub></var>-1,2) .. <var>g<sub>K</sub></var>(<var>h<sub>K</sub></var>-1,<var>N</var>-1) <var>E</var> expression<sub>1</sub> expression<sub>2</sub> : expression<var><sub>E</sub></var> </pre> <p> 1行目に縦棒の数を表す整数 <var>N</var> (2 &le; <var>N</var> &le; 12) と部品の種類の数 <var>K</var> (1 &le; <var>K</var> &le; 26)が与えられる。 </p> <p> 2行目以降に <var>K</var> 種類の部品の情報が与えられる。各部品は以下の形式で与えられる。 </p> <ul> <li> 最初の行は部品名を表す1つの英大文字 <var>p<sub>i</sub></var> と部品 <var>i</var> の縦棒の長さを表す整数 <var>h<sub>i</sub></var> (1 &le; <var>h<sub>i</sub> &le; 20)を含む。</li> <li> 続く<var>h<sub>i</sub></var>-1 行で部品 <var>i</var> の横棒の情報が与えられる。<var>g<sub>i</sub></var>(<var>r, c</var>) は部品 <var>i</var> の上端から長さ <var>r</var> の位置において、左から <var>c</var> 番目の縦棒と <var>c</var>+1 番目の縦棒をつなぐ横棒の有無を表す。横棒があるときは <var>g<sub>i</sub></var>(<var>r</var>, <var>c</var>) = 1、横棒がないときは <var>g<sub>i</sub></var>(<var>r</var>, <var>c</var>) = 0である。 </ul> <p> 続く1行に式の数 <var>E</var> (1 &le; <var>E</var> &le; 100) が与えられる。続く <var>E</var> 行にVLAの式を表す文字列 expression<sub><var>i</var></sub>(1文字以上1000文字以下)が与えられる。 </p> <p> 入力は以下の条件を満たす。 </p> <ul> <li> 異なる部品に同じ名前が使われることはない。 </li> <li> 各部品 <var>i</var> について、<var>g<sub>i</sub></var>(<var>r</var>, <var>c</var>)と <var>g<sub>i</sub></var>(<var>r</var>, <var>c</var>+1)が同時に 1 となることはない。</li> <li> 式に含まれる整数は 1 以上 1,000,000,000 以下である。</li> <li> 式には空白は含まれない。</li> </ul> <h2>出力</h2> <p> 式ごとに、VLAによる1からNまでの列の変換結果を1行に出力する。それぞれの数字の間は空白1つで区切る。 </P> <h2>入出力例</h2> <br> <h2>入力例</h2> <pre> 5 2 A 5 1 0 0 1 0 1 0 0 1 0 1 0 0 1 0 1 B 4 1 0 0 1 0 1 0 0 0 0 1 0 2 2(A+B) 1000000000(1000000000A+1000000000B) </pre> <h2>出力例</h2> <pre> 4 2 5 1 3 1 2 3 4 5 </pre>
p02741
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Print the <var>K</var>-th element of the following sequence of length <var>32</var>:</p> <pre>1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51 </pre> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq K \leq 32</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>K</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the <var>K</var>-th element.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The <var>6</var>-th element is <var>2</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>27 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>5 </pre> <p>The <var>27</var>-th element is <var>5</var>.</p></section> </div> </span>
p03853
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is an image with a height of <var>H</var> pixels and a width of <var>W</var> pixels. Each of the pixels is represented by either <code>.</code> or <code>*</code>. The character representing the pixel at the <var>i</var>-th row from the top and the <var>j</var>-th column from the left, is denoted by <var>C_{i,j}</var>.</p> <p>Extend this image vertically so that its height is doubled. That is, print a image with a height of <var>2H</var> pixels and a width of <var>W</var> pixels where the pixel at the <var>i</var>-th row and <var>j</var>-th column is equal to <var>C_{(i+1)/2,j}</var> (the result of division is rounded down).</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦H, W≦100</var></li> <li><var>C_{i,j}</var> is either <code>.</code> or <code>*</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>H</var> <var>W</var> <var>C_{1,1}...C_{1,W}</var> : <var>C_{H,1}...C_{H,W}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the extended image.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 2 *. .* </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>*. *. .* .* </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 4 ***. </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>***. ***. </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>9 20 .....***....***..... ....*...*..*...*.... ...*.....**.....*... ...*.....*......*... ....*.....*....*.... .....**..*...**..... .......*..*.*....... ........**.*........ .........**......... </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>.....***....***..... .....***....***..... ....*...*..*...*.... ....*...*..*...*.... ...*.....**.....*... ...*.....**.....*... ...*.....*......*... ...*.....*......*... ....*.....*....*.... ....*.....*....*.... .....**..*...**..... .....**..*...**..... .......*..*.*....... .......*..*.*....... ........**.*........ ........**.*........ .........**......... .........**......... </pre></section> </div> </span>
p00052
<H1>階乗 II</H1> <p> <var>n! = n &times; (n &minus; 1) &times; (n &minus; 2) &times;</var> ... <var> &times; 3 &times; 2 &times; 1</var><br/> <br/> を <var>n</var> の階乗といいます。例えば、12 の階乗は<br/> <br/> <var>12! = 12 &times; 11 &times; 10 &times; 9 &times; 8 &times; 7 &times; 6 &times; 5 &times; 4 &times; 3 &times; 2 &times; 1 = 479001600</var><br/> <br/> となり、末尾に 0 が 2 つ連続して並んでいます。 </p> <p> 整数 <var>n</var> を入力して、<var>n!</var> の末尾に連続して並んでいる 0 の数を出力するプログラムを作成してください。ただし、<var>n</var> は 20000 以下の正の整数とします。 </p> <H2>Input</H2> <p> 複数のデータが与えられます。各データに <var>n</var> (<var>n</var> &le; 20000) が1行に与えられます。<var>n</var> が 0 の時入力の最後とします。 </p> <p> データの数は 20 を超えません。 </p> <H2>Output</H2> <p> 各データに対して <var>n!</var> の末尾に連続して並んでいる 0 の数を1行に出力して下さい。 </p> <H2>Sample Input</H2> <pre> 2 12 10000 0 </pre> <H2>Output for the Sample Input</H2> <pre> 0 2 2499 </pre>
p02595
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have <var>N</var> points in the two-dimensional plane. The coordinates of the <var>i</var>-th point are <var>(X_i,Y_i)</var>.</p> <p>Among them, we are looking for the points such that the distance from the origin is at most <var>D</var>. How many such points are there?</p> <p>We remind you that the distance between the origin and the point <var>(p, q)</var> can be represented as <var>\sqrt{p^2+q^2}</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 2\times 10^5</var></li> <li><var>0 \leq D \leq 2\times 10^5</var></li> <li><var>|X_i|,|Y_i| \leq 2\times 10^5</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</var> <var>X_1</var> <var>Y_1</var> <var>\vdots</var> <var>X_N</var> <var>Y_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print an integer representing the number of points such that the distance from the origin is at most <var>D</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 5 0 5 -2 4 3 4 4 -4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>The distance between the origin and each of the given points is as follows:</p> <ul> <li><var>\sqrt{0^2+5^2}=5</var></li> <li><var>\sqrt{(-2)^2+4^2}=4.472\ldots</var></li> <li><var>\sqrt{3^2+4^2}=5</var></li> <li><var>\sqrt{4^2+(-4)^2}=5.656\ldots</var></li> </ul> <p>Thus, we have three points such that the distance from the origin is at most <var>5</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>12 3 1 1 1 1 1 1 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>7 </pre> <p>Multiple points may exist at the same coordinates.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>20 100000 14309 -32939 -56855 100340 151364 25430 103789 -113141 147404 -136977 -37006 -30929 188810 -49557 13419 70401 -88280 165170 -196399 137941 -176527 -61904 46659 115261 -153551 114185 98784 -6820 94111 -86268 -30401 61477 -55056 7872 5901 -163796 138819 -185986 -69848 -96669 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>6 </pre></section> </div> </span>
p00402
<h1>集会所</h1>   <p> イヅア村の住民は村に集会所を建てることにしました。この村では東西に一直線に伸びる道路に沿って建物を建てられる地点が決まっています。最も西の地点を0番目の地点として、そこから等間隔に東に向かって1番目、2番目、…と地点番号が与えられています。村人の家と集会所を建てることができるのは、これらの地点だけです。各家には1人以上の村人が住んでいます。どの村人も隣の地点まで1分間で移動できます。 </p> <p> 集会を開くのに便利なように、ある時刻に一斉に全ての村人が自分の家から集会所に向かったとき、全員が集まるのに必要な時間が最小になるような場所に、集会所を建てることにしました。 </p> <p> 家の建っている地点の番号が与えられたときに、全ての村人が集会所に集まるのに必要な時間の最小値を計算するプログラムを作成せよ。ただし、集会所の地点番号が家の地点番号と重複してもよいものとする。 </p> <h2>入力</h2> <p> 入力は以下の形式で与えられる。 </p> <pre> $N$ $x_1$ $x_2$ ... $x_N$ </pre> <p> 1行目に家の数$N$ ($1 \leq N \leq 1000$)が与えられる。2行目に家の建っている地点番号$x_i$ ($0 \leq x_i \leq 2000$)が与えられる。ただし、家の地点番号は全て異なる($i \ne j$について$x_i \ne x_j$である)。 </p> <h2>出力</h2> <p> 集会所に集まるのに必要な最小の時間を分単位で1行に出力する。 </p> <h2>入出力例</h2> <h3>入力例1</h3> <pre> 3 4 0 1 </pre> <h3>出力例1</h3> <pre> 2 </pre> <h3>入力例2</h3> <pre> 2 1 2 </pre> <h3>出力例2</h3> <pre> 1 </pre>
p00951
<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 Three Kingdoms of Bourdelot </h2> <p> You are an archaeologist at the Institute for Cryptic Pedigree Charts, specialized in the study of the ancient Three Kingdoms of Bourdelot. One day, you found a number of documents on the dynasties of the Three Kingdoms of Bourdelot during excavation of an ancient ruin. Since the early days of the Kingdoms of Bourdelot are veiled in mystery and even the names of many kings and queens are lost in history, the documents are expected to reveal the blood relationship of the ancient dynasties. </p> <p> The documents have lines, each of which consists of a pair of names, presumably of ancient royal family members. An example of a document with two lines follows. </p> <pre> Alice Bob Bob Clare </pre> <p> Lines should have been complete sentences, but the documents are heavily damaged and therefore you can read only two names per line. At first, you thought that all of those lines described the true ancestor relations, but you found that would lead to contradiction. Later, you found that some documents should be interpreted negatively in order to understand the ancestor relations without contradiction. Formally, a document should be interpreted either as a <i>positive</i> document or as a <i>negative</i> document. </p> <ul> <li> In a positive document, the person on the left in each line is an ancestor of the person on the right in the line. If the document above is a positive document, it should be interpreted as "Alice is an ancestor of Bob, and Bob is an ancestor of Clare". </li> <li> In a negative document, the person on the left in each line is NOT an ancestor of the person on the right in the line. If the document above is a negative document, it should be interpreted as "Alice is NOT an ancestor of Bob, and Bob is NOT an ancestor of Clare". </li> </ul> <p> A single document can never be a mixture of positive and negative parts. The document above can never read "Alice is an ancestor of Bob, and Bob is NOT an ancestor of Clare". </p> <p> You also found that there can be ancestor-descendant pairs that didn't directly appear in the documents but can be inferred by the following rule: For any persons $x$, $y$ and $z$, if $x$ is an ancestor of $y$ and $y$ is an ancestor of $z$, then $x$ is an ancestor of $z$. For example, if the document above is a positive document, then the ancestor-descendant pair of "Alice Clare" is inferred. </p> <p> You are interested in the ancestry relationship of two royal family members. Unfortunately, the interpretation of the documents, that is, which are positive and which are negative, is unknown. Given a set of documents and two distinct names $p$ and $q$, your task is to find an interpretation of the documents that does not contradict with the hypothesis that $p$ is an ancestor of $q$. An interpretation of the documents contradicts with the hypothesis if and only if there exist persons $x$ and $y$ such that we can infer from the interpretation of the documents and the hypothesis that </p> <ul> <li> $x$ is an ancestor of $y$ and $y$ is an ancestor of $x$, or</li> <li> $x$ is an ancestor of $y$ and $x$ is not an ancestor of $y$.</li> </ul> <p> We are sure that every person mentioned in the documents had a unique single name, i.e., no two persons have the same name and one person is never mentioned with different names. </p> <p> When a person A is an ancestor of another person B, the person A can be a parent, a grandparent, a great-grandparent, or so on, of the person B. Also, there can be persons or ancestor-descendant pairs that do not appear in the documents. For example, for a family tree shown in Figure F.1, there can be a positive document: </p> <pre> A H B H D H F H E I </pre> <p> where persons C and G do not appear, and the ancestor-descendant pairs such as "A E", "D F", and "C I" do not appear. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ICPCAsia2016_Figure_F1"><br/> Figure F.1. A Family Tree </center> <br/> <h3>Input</h3> <p> The input consists of a single test case of the following format. <br/> $p$ $q$<br/> $n$<br/> $c_1$<br/> .<br/> .<br/> .<br/> $c_n$<br/> </p> <p> The first line of a test case consists of two distinct names, $p$ and $q$, separated by a space. The second line of a test case consists of a single integer $n$ that indicates the number of documents. Then the descriptions of $n$ documents follow. </p> <p> The description of the $i$-th document $c_i$ is formatted as follows:<br/> <br/> $m_i$<br/> $x_{i,1}$ $y_{i,1}$<br/> .<br/> .<br/> .<br/> $x_{i,m_i}$ $y_{i,m_i}$ </p> <p> The first line consists of a single integer $m_i$ that indicates the number of pairs of names in the document. Each of the following $m_i$ lines consists of a pair of distinct names $x_{i,j}$ and $y_{i,j}$ ($1 \leq j \leq m_i$), separated by a space. </p> <p> Each name consists of lowercase or uppercase letters and its length is between 1 and 5, inclusive. </p> <p> A test case satisfies the following constraints. </p> <ul> <li> $1 \leq n \leq 1000$.</li> <li> $1 \leq m_i$.</li> <li> $\sum^{n}_{i=1} m_i \leq 100000$, that is, the total number of pairs of names in the documents does not exceed 100000.</li> <li> The number of distinct names that appear in a test case does not exceed 300.</li> </ul> <h3>Output</h3> <p> Output "Yes" (without quotes) in a single line if there exists an interpretation of the documents that does not contradict with the hypothesis that $p$ is an ancestor of $q$. Output "No", otherwise. </p> <h3>Sample Input 1</h3> <pre>Alice Bob 3 2 Alice Bob Bob Clare 2 Bob Clare Clare David 2 Clare David David Alice </pre> <h3>Sample Output 1</h3> <pre>No</pre> <br/> <h3>Sample Input 2</h3> <pre>Alice David 3 2 Alice Bob Bob Clare 2 Bob Clare Clare David 2 Clare David David Alice </pre> <h3>Sample Output 2</h3> <pre>Yes</pre> <br/> <h3>Sample Input 3</h3> <pre>Alice Bob 1 2 Clare David David Clare </pre> <h3>Sample Output 3</h3> <pre>Yes</pre>
p01643
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [['\\(','\\)']] } }); </script> <script type='text/javascript' src='http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script> <h1 class="ndoc-heading1">Problem J: Avant-garde Art</h1> <p class="ndoc-top">ICPC World Finals 6日目</p> <p class="ndoc-top">ロシア構成主義とは、1910年代半ばに始まったソ連における芸術運動である。 R国に長く滞在していたティー氏は、 そのようなものに感化され、 ICPC World Finalsのリハーサル中にも関わらず、 クールなデザインを作ることにした。 ティー氏は語る: 「記号は円と線分だけで充分だ。 線分をいかに美しく交差させるかが重要である。」</p> <h2 class="ndoc-heading2">問題</h2> <p class="ndoc-top">円周に\(n\)個の座標\( 1, 2, …, n \)を等間隔に割り当てる。 各座標からはちょうど1本の線分が出ており、 座標\( i \)の線分は異なる座標\( a_{i} \)に結ばれている。 (逆に、座標\( a_{i} \)の線分は座標\( a_{a_{i}} = i \)に結ばれている。) この状態から高々\( k \)本の線分を(座標・長さに関係なく自由に)円周上に結び直すことが出来る。 互いに交差している線分の集合の最大サイズを答えよ。</p> <h2 class="ndoc-heading2">入力</h2> <pre> n k a<sub>1</sub> a<sub>2</sub> … a<sub>n</sub> </pre> <p>1行目に座標の数\(n\)、結び直すことのできる線分の数\(k\)が空白区切りで与えられる。 2行目に座標\(i\)の線分が結ばれる座標を表す\( a_{i} \)が空白区切りで与えられる。</p> <h2 class="ndoc-heading2">出力</h2> <p class="ndoc-top">高々\(k\)本の線分を結び直した後に 互いに交差している線分の集合の最大サイズを1行に出力せよ。</p> <h2 class="ndoc-heading2">制約</h2> <ul class="ndoc-indent"> <li>\( 2 \leq n \leq 8000 \)</li> <li>\(n\)は偶数</li> <li>\( 0 \leq k \leq \min(n/2, 20) \)</li> <li>\( 1 \leq a_{i} \leq n \)</li> <li>\( a_{i} \not = i \) <ul> <li>線分を自分自身に結ぶことは無い</li> </ul> </li> <li>\( a_{i} \not = a_{j} (i \not = j) \) <ul> <li>同じ座標に2本以上の線分が結ばれることは無い</li> </ul> </li> <li>\( a_{a_{i}} = i \) <ul> <li>\(i\)から\(j\)に線分が結ばれているならば、\(j\)から\(i\)に線分が結ばれている</li> </ul> </li> </ul> <h2 class="ndoc-heading2">入出力例</h2> <h3 class="ndoc-heading3">入力1</h3> <pre> 8 0 7 4 6 2 8 3 1 5 </pre> <h3 class="ndoc-heading3">出力1</h3> <pre> 2 </pre> <p> 互いに交差する線分は下図の赤い線分で表される。 </p> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_uecpc2013_files_art_sample_1" alt="http://k-operafan.info/static/uecpc2013/files/art_sample_1.png" width="400"> <h3 class="ndoc-heading3">入力2</h3> <pre> 8 1 7 4 6 2 8 3 1 5 </pre> <h3 class="ndoc-heading3">出力2</h3> <pre> 3 </pre> <p> 1,7を結ぶ線分を下図のように結び直せば互いに交差する3つの線分が得られる。 </p> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_uecpc2013_files_art_sample_2" alt="http://k-operafan.info/static/uecpc2013/files/art_sample_2.png" width="400"> <h3 class="ndoc-heading3">入力3</h3> <pre> 8 0 5 6 7 8 1 2 3 4 </pre> <h3 class="ndoc-heading3">出力3</h3> <pre> 4 </pre> <p>全ての線分は互いに交差しているため、交差する最大の線分の数は4となる。</p> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_uecpc2013_files_art_sample_3" alt="http://k-operafan.info/static/uecpc2013/files/art_sample_3.png" width="400">
p03384
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi has an ability to generate a tree using a permutation <var>(p_1,p_2,...,p_n)</var> of <var>(1,2,...,n)</var>, in the following process:</p> <p>First, prepare Vertex <var>1</var>, Vertex <var>2</var>, ..., Vertex <var>N</var>. For each <var>i=1,2,...,n</var>, perform the following operation:</p> <ul> <li>If <var>p_i = 1</var>, do nothing.</li> <li>If <var>p_i \neq 1</var>, let <var>j'</var> be the largest <var>j</var> such that <var>p_j &lt; p_i</var>. Span an edge between Vertex <var>i</var> and Vertex <var>j'</var>.</li> </ul> <p>Takahashi is trying to make his favorite tree with this ability. His favorite tree has <var>n</var> vertices from Vertex <var>1</var> through Vertex <var>n</var>, and its <var>i</var>-th edge connects Vertex <var>v_i</var> and <var>w_i</var>. Determine if he can make a tree isomorphic to his favorite tree by using a proper permutation. If he can do so, find the lexicographically smallest such permutation.</p> </section> </div> <div class="part"> <section> <h3>Notes</h3><p>For the definition of isomorphism of trees, see <a href="https://en.wikipedia.org/wiki/Graph_isomorphism">wikipedia</a>. Intuitively, two trees are isomorphic when they are the "same" if we disregard the indices of their vertices.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq n \leq 10^5</var></li> <li><var>1 \leq v_i, w_i \leq n</var></li> <li>The given graph is a tree.</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>w_1</var> <var>v_2</var> <var>w_2</var> <var>:</var> <var>v_{n-1}</var> <var>w_{n-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there is no permutation that can generate a tree isomorphic to Takahashi's favorite tree, print <code>-1</code>. If it exists, print the lexicographically smallest such permutation, with spaces in between.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 1 2 1 3 1 4 1 5 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 4 5 3 6 </pre> <p>If the permutation <var>(1, 2, 4, 5, 3, 6)</var> is used to generate a tree, it looks as follows:</p> <p><img alt="" src="https://img.atcoder.jp/arc095/db000b879402aed649a1516620eb1e21.png"/></p> <p>This is isomorphic to the given graph.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 2 2 3 3 4 1 5 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 2 3 4 5 6 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>15 1 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 5 10 5 11 6 12 6 13 7 14 7 15 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>-1 </pre></section> </div> </span>
p01213
<H1><font color="#000">Problem G:</font> Repeated Subsequences</H1> <p> You are a treasure hunter traveling around the world. Finally, you’ve got an ancient text indicating the place where the treasure was hidden. The ancient text looks like a meaningless string of characters at first glance. Actually, the secret place is embedded as the <i>longest repeated subsequence</i> of the text. </p> <p> Well, then, what is the <i>longest repeated subsequence</i> of a string? First, you split the given string <i>S</i> into two parts <i>F</i> and <i>R</i>. Then, you take the longest common subsequence <i>L</i> of <i>F</i> and <i>R</i> (longest string <i>L</i> that is a subsequence of both <i>F</i> and <i>R</i>). Since there are many possible ways to split <i>S</i> into two parts, there are many possible <i>L</i>'s. The longest repeated subsequence is the longest one among them. For example, the longest repeated subsequence of “ABCABCABAB” is “ABAB”, which is obtained when you split “ABCABCABAB” into “ABCABC” and “ABAB”. </p> <p> Now your task is clear. Please find the longest repeated subsequence and get the hidden treasure! </p> <H2>Input</H2> <p> The input consists of multiple data sets. Each data set comes with a single line that contains one string of up to 300 capital letters. It is guaranteed that there is at least one repeated subsequence in each string. </p> <p> The end of input is indicated by a line that contains “#END”. This line should not be processed. </p> <H2>Output</H2> <p> For each data set, print the longest repeated subsequence on a line. If there are multiple longest subsequence, print any one of them. </p> <H2>Sample Input</H2> <pre> ABCABCABAB ZZZZZZZZZZZZ #END </pre> <H2>Output for the Sample Input</H2> <pre> ABAB ZZZZZZ </pre>
p04011
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a hotel with the following accommodation fee:</p> <ul> <li><var>X</var> yen (the currency of Japan) per night, for the first <var>K</var> nights</li> <li><var>Y</var> yen per night, for the <var>(K+1)</var>-th and subsequent nights</li> </ul> <p>Tak is staying at this hotel for <var>N</var> consecutive nights. Find his total accommodation fee.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N, K \leq 10000</var></li> <li><var>1 \leq Y &lt; X \leq 10000</var></li> <li><var>N,\,K,\,X,\,Y</var> 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>N</var> <var>K</var> <var>X</var> <var>Y</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print Tak's total accommodation fee.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 3 10000 9000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>48000 </pre> <p>The accommodation fee is as follows:</p> <ul> <li><var>10000</var> yen for the <var>1</var>-st night</li> <li><var>10000</var> yen for the <var>2</var>-nd night</li> <li><var>10000</var> yen for the <var>3</var>-rd night</li> <li><var>9000</var> yen for the <var>4</var>-th night</li> <li><var>9000</var> yen for the <var>5</var>-th night</li> </ul> <p>Thus, the total is <var>48000</var> yen.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 3 10000 9000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>20000 </pre></section> </div> </span>
p00814
<H1><font color="#000">Problem D:</font> Life Line</H1> <p> Let's play a new board game ``Life Line''. </p> <p> The number of the players is greater than 1 and less than 10. </p> <p> In this game, the board is a regular triangle in which many small regular triangles are arranged (See Figure l). The edges of each small triangle are of the same length. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_lifeline1"> <p>Figure 1: The board</p> </center> <p> The size of the board is expressed by the number of vertices on the bottom edge of the outer triangle. For example, the size of the board in Figure 1 is 4. </p> <p> At the beginning of the game, each player is assigned his own identification number between 1 and 9, and is given some stones on which his identification number is written. </p> <p> Each player puts his stone in turn on one of the ``empty'' vertices. An ``empty vertex'' is a vertex that has no stone on it. </p> <p> When one player puts his stone on one of the vertices during his turn, some stones might be removed from the board. The player gains points which is equal to the number of the removed stones of others, but loses points which is equal to the number of the removed stones of himself. The points of a player for a single turn is the points he gained minus the points he lost in that turn. </p> <p> The conditions for removing stones are as follows: </p> <ul> <li> The stones on the board are divided into groups. Each group contains a set of stones whose numbers are the same and placed adjacently. That is, if the same numbered stones are placed adjacently, they belong to the same group. </li> <li> If none of the stones in a group is adjacent to at least one ``empty'' vertex, all the stones in that group are removed from the board. </li> </ul> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_lifeline2"> <p>Figure 2: The groups of stones</p> </center> <p>Figure 2 shows an example of the groups of stones.</p> <p> Suppose that the turn of the player `4' comes now. If he puts his stone on the vertex shown in Figure 3a, the conditions will be satisfied to remove some groups of stones (shadowed in Figure 3b). The player gains 6 points, because the 6 stones of others are removed from the board (See Figure 3c). </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_lifeline3"> <table> <tr> <td align="center" width="260"><p>Figure 3a</p></td> <td align="center" width="260"><p>Figure 3b</p></td> <td align="center" width="260"><p>Figure 3c</p></td> </tr> </table> </center> <p> As another example, suppose that the turn of the player `2' comes in Figure 2. If the player puts his stone on the vertex shown in Figure 4a, the conditions will be satisfied to remove some groups of stones (shadowed in Figure 4b). The player gains 4 points, because the 4 stones of others are removed. But, at the same time, he loses 3 points, because his 3 stones are removed. As the result, the player's points of this turn is 4 - 3 = 1 (See Figure 4c). </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_lifeline4"> <table> <tr> <td align="center" width="260"><p>Figure 4a</p></td> <td align="center" width="260"><p>Figure 4b</p></td> <td align="center" width="260"><p>Figure 4c</p></td> </tr> </table> </center> <p> When each player puts all of his stones on the board, the game is over. The total score of a player is the summation of the points of all of his turns. </p> <p> Your job is to write a program that tells you the maximum points a player can get (i.e., the points he gains - the points he loses) in his current turn. <H2>Input</H2> <p> The input consists of multiple data. Each data represents the state of the board of the game still in progress. </p> <p> The format of each data is as follows. </p> <pre> <i>N C</i> <i>S</i><sub>1,1</sub> <i>S</i><sub>2,1</sub> <i>S</i><sub>2,2</sub> <i>S</i><sub>3,1</sub> <i>S</i><sub>3,2</sub> <i>S</i><sub>3,3</sub> ... <i>S</i><sub><i>N</i>,1</sub> ... <i>S</i><sub><i>N</i>,<i>N</i></sub> </pre> <p> <i>N</i> is the size of the board (3 &le; <i>N</i> &le; 10). </p> <p> <i>C</i> is the identification number of the player whose turn comes now (1 &le; <i>C</i> &le; 9) . That is, your program must calculate his points in this turn. </p> <p> <i>S</i><sub><i>i</i>,<i>j</i></sub> is the state of the vertex on the board (0 &le; <i>S</i><sub><i>i</i>,<i>j</i></sub> &le; 9) . If the value of <i>S</i><sub><i>i</i>,<i>j</i></sub> is positive, it means that there is the stone numbered by <i>S</i><sub><i>i</i>,<i>j</i></sub> there. If the value of <i>S</i><sub><i>i</i>,<i>j</i></sub> is 0, it means that the vertex is ``empty''. </p> <p> Two zeros in a line, i.e., 0 0, represents the end of the input. </p> <H2>Output</H2> <p> For each data, the maximum points the player can get in the turn should be output, each in a separate line. </p> <H2>Sample Input</H2> <pre> 4 4 2 2 3 1 0 4 1 1 4 0 4 5 2 2 3 3 0 4 1 1 4 0 4 1 2 2 3 3 0 4 1 1 4 0 4 1 1 1 1 1 1 1 1 1 1 0 4 2 1 1 1 1 1 1 1 1 1 0 4 1 0 2 2 5 0 7 0 5 7 0 4 2 0 0 3 1 0 4 0 1 0 4 4 3 0 3 3 3 2 3 0 3 0 3 4 2 0 3 3 3 2 3 0 3 0 3 6 1 1 1 2 1 1 0 6 7 6 8 0 7 6 8 2 6 6 7 2 2 0 5 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 3 3 3 2 4 3 2 4 4 0 3 3 3 3 0 3 0 0 </pre> <H2>Output for the Sample Input</H2> <pre> 6 5 1 -10 8 -1 0 1 -1 5 0 5 </pre>
p02829
<span class="lang-en"> <p>Score: <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>Takahashi is solving quizzes. He has easily solved all but the last one.</p> <p>The last quiz has three choices: <var>1</var>, <var>2</var>, and <var>3</var>.</p> <p>With his supernatural power, Takahashi has found out that the choices <var>A</var> and <var>B</var> are both wrong.</p> <p>Print the correct choice for this problem.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3> <ul> <li>Each of the numbers <var>A</var> and <var>B</var> is <var>1</var>, <var>2</var>, or <var>3</var>.</li> <li><var>A</var> and <var>B</var> are different.</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>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3> <p>Print the correct choice.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>When we know <var>3</var> and <var>1</var> are both wrong, the correct choice is <var>2</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 </pre></section> </div> </span>
p01706
<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> <meta http-equiv="X-UA-Compatible" CONTENT="IE=EmulateIE7" /><style type="text/css">blockquote { font-family: Menlo, Monaco, "Courier New", monospace; color: #333333; display: block; padding: 8.5px; margin: 0 0 9px; font-size: 12px; line-height: 18px; background-color: #f5f5f5; border: 1px solid #ccc; border: 1px solid rgba(0, 0, 0, 0.15); -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; white-space: pre; white-space: pre-wrap; word-break: break-all; word-wrap: break-word; }</style><div class="part"><h3>Problem Statement</h3><p>JAG Kingdom is a strange kingdom such that its $N$ cities are connected only by one-way roads. The $N$ cities are numbered $1$ through $N$. ICPC (International Characteristic Product Corporation) transports its products from the factory at the city $S$ to the storehouse at the city $T$ in JAG Kingdom every day. For efficiency, ICPC uses multiple trucks at once. Each truck starts from $S$ and reaches $T$ on the one-way road network, passing through some cities (or directly). In order to reduce risks of traffic jams and accidents, no pair of trucks takes the same road. </p> <p>Now, ICPC wants to improve the efficiency of daily transports, while ICPC operates daily transports by as many trucks as possible under the above constraint. JAG Kingdom, whose finances are massively affected by ICPC, considers to change the direction of one-way roads in order to increase the number of trucks for daily transports of ICPC. Because reversal of many roads causes confusion, JAG Kingdom decides to reverse at most a single road. </p> <p>If there is no road such that reversal of the road can improve the transport efficiency, JAG Kingdom need not reverse any roads. Check whether reversal of a single road can improve the current maximum number of trucks for daily transports. And if so, calculate the maximum number of trucks which take disjoint sets of roads when a one-way road can be reversed, and the number of roads which can be chosen as the road to be reversed to realize the maximum. </p> </div><div class="part"><h3>Input</h3> <p>The input consists of multiple datasets. The number of dataset is no more than $100$. </p> <p>Each dataset is formatted as follows. </p> <blockquote style="font-size:14pt">$N$ $M$ $S$ $T$<br>$a_1$ $b_1$<br>$a_2$ $b_2$<br>:<br>:<br>$a_M$ $b_M$</blockquote> <p>The first line of each dataset contains four integers: the number of cities $N$ ($2 \le N \le 1{,}000$), the number of roads $M$ ($1 \le M \le 10{,}000$), the city with the factory $S$ and the city with the storehouse $T$ ($1 \le S, T \le N$, $S \neq T$). </p> <p>The following $M$ lines describe the information of the roads. The $i$-th line of them contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le N$, $a_i \neq b_i$), meaning that the $i$-th road is directed from $a_i$ to $b_i$. </p> <p>The end of input is indicated by a line containing four zeros. </p> </div><div class="part"><h3>Output</h3> <p>For each dataset, output two integers separated by a single space in a line as follows: If reversal of a single road improves the current maximum number of trucks for daily transports, the first output integer is the new maximum after reversal of a road, and the second output integer is the number of roads which can be chosen as the road to be reversed to realize the new maximum. Otherwise, i.e. if the current maximum cannot be increased by any reversal of a road, the first output integer is the current maximum and the second output integer is $0$. </p> </div><div class="part"><h3>Sample Input</h3> <pre>4 4 1 4 1 2 3 1 4 2 3 4 7 8 1 7 1 2 1 3 2 4 3 4 4 5 4 6 5 7 7 6 6 4 5 2 1 2 1 3 4 5 5 6 10 21 9 10 9 1 9 2 9 3 9 4 10 1 10 2 10 3 10 4 1 5 2 5 2 6 3 6 3 7 4 7 4 8 1 8 5 10 6 10 7 10 10 8 10 9 2 15 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 0 0 0 0</pre> </div><div class="part"><h3>Output for the Sample Input</h3> <pre>1 2 2 1 0 0 4 6 15 0</pre> </div>
p02983
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given two non-negative integers <var>L</var> and <var>R</var>. We will choose two integers <var>i</var> and <var>j</var> such that <var>L \leq i &lt; j \leq R</var>. Find the minimum possible value of <var>(i \times j) \mbox{ mod } 2019</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All values in input are integers.</li> <li><var>0 \leq L &lt; R \leq 2 \times 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>L</var> <var>R</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum possible value of <var>(i \times j) \mbox{ mod } 2019</var> when <var>i</var> and <var>j</var> are chosen under the given condition.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2020 2040 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>When <var>(i, j) = (2020, 2021)</var>, <var>(i \times j) \mbox{ mod } 2019 = 2</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>20 </pre> <p>We have only one choice: <var>(i, j) = (4, 5)</var>.</p></section> </div> </span>
p03691
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> turkeys. We number them from <var>1</var> through <var>N</var>.</p> <p><var>M</var> men will visit here one by one. The <var>i</var>-th man to visit will take the following action:</p> <ul> <li>If both turkeys <var>x_i</var> and <var>y_i</var> are alive: selects one of them with equal probability, then eats it.</li> <li>If either turkey <var>x_i</var> or <var>y_i</var> is alive (but not both): eats the alive one.</li> <li>If neither turkey <var>x_i</var> nor <var>y_i</var> is alive: does nothing.</li> </ul> <p>Find the number of pairs <var>(i,\ j)</var> (<var>1 ≤ i &lt; j ≤ N</var>) such that the following condition is held:</p> <ul> <li>The probability of both turkeys <var>i</var> and <var>j</var> being alive after all the men took actions, is greater than <var>0</var>.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N ≤ 400</var></li> <li><var>1 ≤ M ≤ 10^5</var></li> <li><var>1 ≤ x_i &lt; y_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>M</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 number of pairs <var>(i,\ j)</var> (<var>1 ≤ i &lt; j ≤ N</var>) such that the condition is held.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p><var>(i,\ j) = (1,\ 3), (2,\ 3)</var> satisfy the condition.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 3 1 2 3 4 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 </pre> <p><var>(i,\ j) = (1,\ 4)</var> satisfies the condition. Both turkeys <var>1</var> and <var>4</var> are alive if:</p> <ul> <li>The first man eats turkey <var>2</var>.</li> <li>The second man eats turkey <var>3</var>.</li> <li>The third man does nothing.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 2 1 2 1 2 </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 10 8 9 2 8 4 6 4 9 7 8 2 8 1 8 3 4 3 4 2 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>5 </pre></section> </div> </span>
p01356
<H1>Problem H: Nearest Station</H1> <p> うさぎがある電車のチケットを<i>n</i> 枚持っている. チケットにはそれぞれ0 から<i>n</i> − 1 までの番号がついていて, <i>k</i> 番のチケットを使うと, <i>p</i>&sdot;<i>a<sup>k</sup></i> + <i>q</i>&sdot;<i>b<sup>k</sup></i> 駅進むことができる. </p> <p> うさぎは今いる駅から<i>m</i> 駅進んだ駅にあるニンジン食べ放題の店に行きたいが, なるべく歩く距離を短くしたい. 駅は等間隔に並んでいる. チケットを電車の上り線で進むことのみに用いるとき, うさぎは最小何駅分の徒歩で店に着けるか. </p> <H2>Input</H2> <p> 1 &le; <i>n</i>, <i>m</i>, <i>a</i>, <i>b</i>, <i>p</i>, <i>q</i> &le; 1 000 000 000 000 (整数) </p> <H2>Output</H2> <p> うさぎは最小何駅分の徒歩で店に着けるか, その数を一行に出力せよ. </p> <H2>Sample Input 1</H2> <pre> 6 200 2 3 4 5 </pre> <H2>Sample Output 1</H2> <pre> 1 </pre> <H2>Sample Input 2</H2> <pre> 6 1 2 3 4 5 </pre> <H2>Sample Output 2</H2> <pre> 1 </pre>
p00117
<H1>大工の褒美</H1> <p> ある日、殿様は一人の大工に、「台風や地震が来たときに町人が避難できる、頑丈で大きな建物を造りなさい。」と命じました。しかし、その頑丈で大きな建物を完成させるには、大きな太い柱が必要です。町にそんな大きな柱はありません。そこで、大工は遠くの山里まで大きな柱を調達しに行くことになりました(大工は町から里山に行って、町に戻ってくる必要があります)。 </p> <p> 大工の褒美は、殿様から受け取ったお金から柱の代金と交通費を差し引いた余りです。下の地図のように、山里に行くには、いろいろな町を通るたくさんの街道があり、2つの町をつなぐ街道はそれぞれ交通費が違います。大工の褒美を最大にするにはどのように街道をたどり調達すればよいでしょうか。最大となる大工の褒美を出力するプログラムを作成してください。ただし、町の数を <var>n</var> とすれば、各町は 1 から <var>n</var> までの整数で識別されます。2 つの町を直接つなぐ街道は 2 本以上ありません。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_carpenter"> </center> <p> ※ 矢印上の数字は、その方向に行くための交通費を示します。 </p> <H2>Input</H2> <p> 入力は以下の形式で与えられます。 </p> <pre> <var>n</var> <var>m</var> <var>a<sub>1</sub></var>,<var>b<sub>1</sub></var>,<var>c<sub>1</sub></var>, <var>d<sub>1</sub></var> <var>a<sub>2</sub></var>,<var>b<sub>2</sub></var>,<var>c<sub>2</sub></var>, <var>d<sub>2</sub></var> : <var>a<sub>m</sub></var>,<var>b<sub>m</sub></var>,<var>c<sub>m</sub></var>, <var>d<sub>m</sub></var> <var>s</var>,<var>g</var>,<var>V</var>,<var>P</var> </pre> <p> 1 行目に町の総数 <var>n</var>( <var>n</var> &le; 20)、2 行目に街道の総数 <var>m</var> (<var>m</var> &le; 100) が与えられます。続く <var>m</var> 行に <var>i</var> 番目の街道の情報 <var>a<sub>i</sub></var>, <var>b<sub>i</sub></var>, <var>c<sub>i</sub></var>, <var>d<sub>i</sub></var> (1 &le; <var>a<sub>i</sub></var>, <var>b<sub>i</sub></var> &le; <var>n</var>, 0 &le; <var>c<sub>i</sub></var>, <var>d<sub>i</sub></var> &le; 1,000) が与えられます。<var>a<sub>i</sub></var>, <var>b<sub>i</sub></var> は街道 <var>i</var> がつないでいる町の番号、 <var>c<sub>i</sub></var> は <var>a<sub>i</sub></var> から <var>b<sub>i</sub></var> への交通費、 <var>d<sub>i</sub></var> は <var>b<sub>i</sub></var> から <var>a<sub>i</sub></var> への交通費を表します。 </p> <p> 最終行に大工が出発する町の番号 <var>s</var>、柱のある山里の番号 <var>g</var>、殿様から大工が受け取ったお金 <var>V</var>、柱の代金 <var>P</var> が与えられます。 </p> <H2>Output</H2> <p> 大工の褒美(整数)を1行に出力してください。 </p> <H2>Sample Input</H2> <pre> 6 8 1,2,2,2 1,3,4,3 1,4,4,2 2,5,3,2 3,4,4,2 3,6,1,2 4,6,1,1 5,6,1,2 2,4,50,30 </pre> <H2>Output for the Sample Input</H2> <pre> 11 </pre>
p02080
<h1>C: Cyclic String</h1> <h2>問題文</h2> <p>文字列 $S = s_0 s_1 \ldots s_{n-1}$ が文字列 $T = t_0 t_1 \ldots t_{m-1}$ に巡回的に含まれるとは、 ある $k$ があって、全ての $i$ $(0 \le i \lt n)$ について $s_i = t_{(i+k) \bmod m}$ が成立する事を言います。</p> <p>$N$ 個の文字列 $S_1, \dots, S_N$ が与えられます。 以下の条件を満たす文字列の集合 $\{C_1, \dots, C_k\}$ を考えます($k$ は任意):</p> <ul> <li>各$i$ $(1 \leq i \leq N)$について、$S_i$は少なくとも1つの$C_j$に巡回的に含まれる。$(1 \leq j \leq k)$</li> </ul> <p>$|C_1| + |C_2| + \cdots + |C_k|$の最小値を求めてください。</p> <h2>制約</h2> <ul> <li>$1 \leq N \leq 100$</li> <li>$1 \leq |S_i| \leq 1000$</li> <li>$S_i$ の各文字は<strong>英小文字</strong>または<strong>英大文字</strong>である</li> <li>任意の$i,j$ $(i \ne j)$について、$S_i$ は $S_j$ の連続する部分文字列ではない</li> </ul> <h2>入力</h2> <p>入力は標準入力から以下の形式で与えられます。</p> <pre>$N$ $S_1$ $S_2$ $\vdots$ $S_N$</pre> <h2>出力</h2> <p>答えを一行で出力してください。</p> <h2>入出力例</h2> <h3>入力例1</h3> <pre>3 dea xxx abcd </pre> <h3>出力例1</h3> <pre>6 </pre> <p>例えば、文字列の集合 {"abcde", "x"} は問題文の条件を満たし、文字列の長さの合計は $6$ です。</p> <h3>入力例2</h3> <pre>1 xy </pre> <h3>出力例2</h3> <pre>2 </pre> <h3>入力例3</h3> <pre>3 AAaaBB aaBBAA AaB </pre> <h3>出力例3</h3> <pre>9 </pre>
p00547
<h1>屋台 (Food stalls)</h1> <h2>問題</h2> <p> JOI 君の住む IOI 市は,南北方向に H 区画,東西方向に W 区画の長方形の形をしており,H × W 個の区画に区切られている.北から i 番目,西から j 番目の区画を (i, j) と表す.現在,IOI 市で開催されている国際的なプログラミングコンテストを記念して,大規模なお祭りが開催されている.いくつかの区画には屋台が出ており,それぞれ違う種類のお菓子を販売している. 区画 (1, 1), 区画 (H, W) およびそれらに東西南北に隣接する区画には屋台はない. </p> <p> JOI 君は区画 (1, 1) から区画 (H, W) に移動する.移動時間を短くするため,JOI 君は東あるいは南のみに移動を行う.JOI 君はお菓子が好きなので,区画に入るごとに順に次の行動をとる. </p> <ol> <li> 現在の区画にまだ買っていないお菓子を販売している屋台が出ている場合,その屋台でお菓子を購入する. </li> <li> 現在の区画の東西南北に隣接する区画にまだ買っていないお菓子を販売している屋台が出ている場合,それらの屋台のうち 1 つを除く全ての屋台から販売員を呼んで,お菓子を購入する. </li> </ol> <p> JOI 君は同じ種類のお菓子を複数回購入することはない. </p> <p> IOI 市の大きさ,屋台の位置と,それぞれの屋台のお菓子の値段が与えられたとき,JOI 君が区画 (1, 1) から区画 (H, W) に移動する間に購入するお菓子の総額の最小値を求めよ. </p> <h2> 入力</h2> <p> 入力は 1 + H 行からなる. </p> <p> 1 行目には,2 つの整数 H, W (3 ≦ H ≦ 1000, 3 ≦ W ≦ 1000) が空白を区切りとして書かれている.これは,IOI 市が H × W 個の区画に区切られていることを表す. </p> <p> 続く H 行にはそれぞれ W 文字からなる文字列が書かれており,IOI 市のそれぞれの区画の情報を表す.H 行のうちの i 行目の左から j 番目の文字 (1 ≦ i ≦ H, 1 ≦ j ≦ W) は,区画 (i, j) に屋台がない場合には '.' (ピリオド) である.屋台がある場合には '1', '2', ..., '9' のいずれかであり,その屋台で販売しているお菓子の値段を表す. </p> <p> 与えられる 5 つの入力データのうち,入力 1 では,屋台のある区画の数は 20 以下である. </p> <h2> 出力</h2> <p> JOI 君が区画 (1, 1) から区画 (H, W) に移動する間に購入するお菓子の総額の最小値を 1 行で出力せよ. </p> <h2> 入出力例</h2> <h3>入力例 1</h3> <pre> 5 5 ..483 .59.9 3.866 79... 4.8.. </pre> <h3>出力例 1</h3> <pre> 20 </pre> <h3>入力例 2</h3> <pre> 12 10 ..498522.4 .633527629 54.4621596 634.213458 1924518685 7739539767 276155.3.6 87716372.2 .858877595 7998739511 3438.5852. 568.9319.. </pre> <h3>出力例 2</h3> <pre> 63 </pre> <p> 入出力例 1 においては,区画 (1, 1),区画 (2, 1),区画 (3, 1),区画 (3, 2),区画 (4, 2),区画 (4, 3),区画 (4, 4),区画 (4, 5),区画 (5, 5) の順番に移動して,区画 (3, 1),区画 (3, 3),区画 (4, 2) の屋台で販売しているお菓子を購入すると,購入するお菓子の総額が最小となる. </p> <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/2015/2016-yo/index.html">情報オリンピック日本委員会作 『第 15 回日本情報オリンピック JOI 2015/2016 予選競技課題』</a> </p> </div>
p01585
<H1><font color="#000">Problem E:</font> Mickle's Beam</H1> <p> Major Mickle is commanded to protect a secret base which is being attacked by <i>N</i> tanks. Mickle has an ultimate laser rifle called <i>Mickle's beam</i>. Any object shot by this beam will be immediately destroyed, and there is no way for protection. Furthermore, the beam keeps going through even after destroying objects. The beam is such a powerful and horrible weapon, but there is one drawback: the beam consumes a huge amount of energy. For this reason, the number of shots should be always minimized. </p> <p> Your job is to write a program that calculates the minimum number of shots required to destroy all the enemy tanks. </p> <p> You can assume the following: </p> <ul> <li> the base is located on the origin (0, 0);</li> <li> each tank has a rectangular shape with edges parallel to the <i>x</i>- and <i>y</i>-axes;</li> <li> no tank may touch or include the origin;</li> <li> no two tanks share the same point;</li> <li> the width of beam is negligible; and</li> <li> a beam destroys every tank it touches or crosses.</li> </ul> <H2>Input</H2> <p> The input consists of an integer sequence. </p> <p> The first integer indicates <i>N</i> (<i>N</i> &le; 2,000). Each of the following <i>N</i> lines contains four integers which indicates the <i>x</i>- and <i>y</i>-coordinates of the lower-left corner and the upper-right corner of a tank respectively. You can assume that every coordinate may not exceed 10,000 in absolute value. </p> <H2>Output</H2> <p> Output the minimum number of shots of the beam. </p> <H2>Sample Input and Output</H2> <H2>Input #1</H2> <pre> 4 2 -1 3 1 -1 2 1 3 -3 -1 -2 1 -1 -3 1 -2 </pre> <H2>Output #1</H2> <pre> 4 </pre> <br/> <H2>Input #2</H2> <pre> 2 1 0 2 1 3 -1 4 0 </pre> <H2>Output #2</H2> <pre> 1 </pre> <br/>
p03042
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You have a digit sequence <var>S</var> of length <var>4</var>. You are wondering which of the following formats <var>S</var> is in:</p> <ul> <li>YYMM format: the last two digits of the year and the two-digit representation of the month (example: <code>01</code> for January), concatenated in this order</li> <li>MMYY format: the two-digit representation of the month and the last two digits of the year, concatenated in this order</li> </ul> <p>If <var>S</var> is valid in only YYMM format, print <code>YYMM</code>; if <var>S</var> is valid in only MMYY format, print <code>MMYY</code>; if <var>S</var> is valid in both formats, print <code>AMBIGUOUS</code>; if <var>S</var> is valid in neither format, print <code>NA</code>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>S</var> is a digit sequence of length <var>4</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> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the specified string: <code>YYMM</code>, <code>MMYY</code>, <code>AMBIGUOUS</code> or <code>NA</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>1905 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>YYMM </pre> <p>May <var>XX19</var> is a valid date, but <var>19</var> is not valid as a month. Thus, this string is only valid in YYMM format.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>0112 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>AMBIGUOUS </pre> <p>Both December <var>XX01</var> and January <var>XX12</var> are valid dates. Thus, this string is valid in both formats.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1700 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>NA </pre> <p>Neither <var>0</var> nor <var>17</var> is valid as a month. Thus, this string is valid in neither format.</p></section> </div> </span>
p03412
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given two integer sequences, each of length <var>N</var>: <var>a_1, ..., a_N</var> and <var>b_1, ..., b_N</var>.</p> <p>There are <var>N^2</var> ways to choose two integers <var>i</var> and <var>j</var> such that <var>1 \leq i, j \leq N</var>. For each of these <var>N^2</var> pairs, we will compute <var>a_i + b_j</var> and write it on a sheet of paper. That is, we will write <var>N^2</var> integers in total.</p> <p>Compute the XOR of these <var>N^2</var> integers.</p> <p><details> <summary style="display:list-item">Definition of XOR</summary></details></p> <p>The XOR of integers <var>c_1, c_2, ..., c_m</var> is defined as follows:</p> <ul> <li>Let the XOR be <var>X</var>. In the binary representation of <var>X</var>, the digit in the <var>2^k</var>'s place (<var>0 \leq k</var>; <var>k</var> is an integer) is <var>1</var> if there are an odd number of integers among <var>c_1, c_2, ...c_m</var> whose binary representation has <var>1</var> in the <var>2^k</var>'s place, and <var>0</var> if that number is even.</li> </ul> <p>For example, let us compute the XOR of <var>3</var> and <var>5</var>. The binary representation of <var>3</var> is <var>011</var>, and the binary representation of <var>5</var> is <var>101</var>, thus the XOR has the binary representation <var>110</var>, that is, the XOR is <var>6</var>.</p> <p></p></section></div></span>
p00394
<!--<h1> Salary Calculation</h1>--> <h1>Payroll</h1> <p> PCK company has $N$ salaried staff and they have specific ID numbers that run from $0$ to $N-1$. The company provides $N$ types of work that are also identified by ID numbers from $0$ to $N-1$. The time required to complete work $j$ is $t_j$. </p> <p> To get each of the working staff to learn as many types of work as possible, the company shifts work assignment in the following fashion. </p> <ul> <li> On the $d$-th day of a shift (the first day is counted as $0$th day), work $j$ is assigned to the staff $(j + d) \% N$, where the notation $p \% q$ indicates the remainder when $p$ is divided by $q$. </li> </ul> <p> Salaries in the PCK company are paid on a daily basis based on the hourly rate. When a staff whose hourly rate is $a$ carries out work $j$, his/her salary will amount to $a \times t_j$. The initial hourly rate for a staff $i$ is $s_i$ yen. To boost staff’s motivation, the company employs a unique scheme for raising hourly rates. The promotion scheme table of the company contains a list of factors $f_k$ ($0 \leq k \leq M-1$), and the hourly rate of a staff is raised based on the following rule. </p> <ul> <li> At the completion of the $d$-th day’s work (the first day is counted as the $0$th day), the hourly rate of the staff with ID number $d \% N$ is raised by $f_{(d \% M)}$ yen. The salary raise takes effect after the day’s pay has been made. </li> </ul> <p> As the person in charge of accounting in PCK company, you have to calculate the total amount of pay for $D$ days from the $0$-th day. </p> <p> Make a program to work out the total amount of salary given the following information: initial hourly rate of each staff, hours required to complete a job, a table of promotion coefficients, and the number of days. Because the total may become excessively large, report the remainder when the total salary amount is divided by $1,000,000,007(= 10^9 + 7)$. </p> <h2>Input</h2> <p> The input is given in the following format. </p> <pre> $N$ $M$ $D$ $s_0$ $s_1$ $...$ $s_{N-1}$ $t_0$ $t_1$ $...$ $t_{N-1}$ $f_0$ $f_1$ $...$ $f_{M-1}$ </pre> <p> The first line provides the number of working staff and types of work $N$ ($1 \leq N \leq 100$), the number of factors $M$ ($1 \leq M \leq 100$)and the number of days $D$ ($1 \leq D \leq 10^{15}$). Note that $M + N$ is equal to or less than 100. The second line provides the initial hourly rate for each staff $s_i$ ($1 \leq s_i \leq 10^8$) as integers. The third line provides the hours $t_j$ ($1 \leq t_j \leq 10^8$) required to complete each type of job as integers. The fourth line lists the factors $f_k$ ($1 \leq f_k \leq 10^8$) in the promotion scheme table. </p> <h2>Output</h2> <p> Output the total amount of the salary. </p> <h2>Sample Input 1</h2> <pre> 3 2 2 3 2 1 1 2 3 1 2 </pre> <h2>Sample Output 1</h2> <pre> 26 </pre> <h2>Sample Input 2</h2> <pre> 3 2 5 3 2 1 1 2 3 1 2 </pre> <h2>Sample Output 2</h2> <pre> 91 </pre>
p02653
<span class="lang-en"> <p>Score : <var>800</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has a string <var>x</var> of length <var>N</var>. Initially, every character in <var>x</var> is <code>0</code>.</p> <p>Snuke can do the following two operations any number of times in any order:</p> <ul> <li>Choose <var>A</var> consecutive characters in <var>x</var> and replace each of them with <code>0</code>.</li> <li>Choose <var>B</var> consecutive characters in <var>x</var> and replace each of them with <code>1</code>.</li> </ul> <p>Find the number of different strings that <var>x</var> can be after Snuke finishes doing operations. This count can be enormous, so compute it modulo <var>(10^9+7)</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 5000</var></li> <li><var>1 \leq A,B \leq N</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</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of different strings that <var>x</var> can be after Snuke finishes doing operations, modulo <var>(10^9+7)</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>11 </pre> <p>For example, <var>x</var> can be <code>0011</code> or <code>1111</code> in the end, but cannot be <code>0110</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>533 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1000 100 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>828178524 </pre></section> </div> </span>
p03941
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><style> #nck { width: 30px; height: auto; } </style> <p>In the country there are <var>N</var> cities numbered <var>1</var> through <var>N</var>, which are connected by <var>N-1</var> bidirectional roads. In terms of graph theory, there is a unique simple path connecting every pair of cities. That is, the <var>N</var> cities form a tree. Besides, if we view city <var>1</var> as the root of this tree, the tree will be a full binary tree (a tree is a full binary tree if and only if every non-leaf vertex in the tree has exactly two children). Roads are numbered <var>1</var> through <var>N-1</var>. The <var>i</var>-th road connects city <var>i+1</var> and city <var>a_i</var>. When you pass through road <var>i</var>, the toll you should pay is <var>v_i</var> (if <var>v_i</var> is <var>0</var>, it indicates the road does not require a toll).</p> <p>A company in city <var>1</var> will give each employee a family travel and cover a large part of the tolls incurred. Each employee can design the travel by themselves. That is, which cities he/she wants to visit in each day and which city to stay at each night. However, there are some constraints. More details are as follows:</p> <ul> <li> <p>The travel must start and end at city <var>1</var>. If there are <var>m</var> leaves in the tree formed by the cities, then the number of days in the travel must be <var>m+1</var>. At the end of each day, except for the last day, the employee must stay at some hotel in a leaf city. During the whole travel, the employee must stay at all leaf cities exactly once.</p> </li> <li> <p>During the whole travel, all roads of the country must be passed through exactly twice.</p> </li> <li> <p>The amount that the employee must pay for tolls by him/herself is the maximum total toll incurred in a single day during the travel, except the first day and the last day. The remaining tolls will be covered by the company.</p> </li> </ul> <p>Shik, as an employee of this company, has only one hope for his travel. He hopes that the amount he must pay for tolls by himself will be as small as possible. Please help Shik to design the travel which satisfies his hope.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 &lt; N &lt; 131,072</var></li> <li><var>1 \leq a_i \leq i</var> for all <var>i</var></li> <li><var>0 \leq v_i \leq 131,072</var></li> <li><var>v_i</var> is an integer</li> <li>The given tree is a full binary tree</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>v_1</var> <var>a_2</var> <var>v_2</var> <var>:</var> <var>a_{N-1}</var> <var>v_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print an integer denoting the minimum amount Shik must pay by himself for tolls incurred during the travel.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>7 1 1 1 1 2 1 2 1 3 1 3 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>There are <var>4</var> leaves in the tree formed by the cities (cities <var>4</var>, <var>5</var>, <var>6</var>, and <var>7</var>), so the travel must be <var>5</var> days long. There are <var>4! = 24</var> possible arrangements of the leaf cities to stay during the travel, but some of them are invalid. For example, <var>(4,5,6,7)</var> and <var>(6,7,5,4)</var> are valid orders, but <var>(5,6,7,4)</var> is invalid because the route passes <var>4</var> times through the road connecting city <var>1</var> and city <var>2</var>. The figure below shows the routes of the travel for these arrangements.</p> <div style="text-align: center;"> <img alt="04b39e0341af562ba20ba2d49c6f2b69.jpg" src="https://atcoder.jp/img/agc007/04b39e0341af562ba20ba2d49c6f2b69.jpg"> </img></div> <p>For all valid orders, day <var>3</var> is the day with the maximum total incurred toll of <var>4</var>, passing through <var>4</var> roads.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>9 1 2 1 2 3 2 3 2 5 2 5 2 7 2 7 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 </pre> <p>The following figure shows the route of the travel for one possible arrangement of the stayed cities for the solution. Note that the tolls incurred in the first day and the last day are always covered by the company.</p> <div style="text-align: center;"> <img alt="92271892911b34032766803fa9c9e159.jpg" src="https://atcoder.jp/img/agc007/92271892911b34032766803fa9c9e159.jpg"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>15 1 2 1 5 3 3 4 3 4 3 6 5 5 4 5 3 6 3 3 2 11 5 11 3 13 2 13 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>15 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>3 1 0 1 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>0 </pre></section> </div> </span>
p02203
<h2>競り (Auction)</h2> <p>square1001君はとある競りを鑑賞していました。</p> <p>競りとは、買い手が多数で品数に制限があるとき、高値を付けたものに買う権利を与える仕組みの取引です。<sub>(新明解国語辞典第七版より)</sub></p> <p>ここでの競りのルールは以下の通りです。</p> <br> <p>1. 次の 2. ~ 6. を品物が尽きるまで繰り返す</p> <p>2. 新たな品物を出し、客に見せる</p> <p>3. 客のうちの 1 人が好きな値段を品物につける</p> <p>4. 客のうちの 1 人が現在品物についている値段<strong>よりも</strong>高い値段をつける</p> <p>5. 4. の行為を行う客がいなくなるまで 4. を繰り返す</p> <p>6. 品物に一番高い値段を付けた客がそれを落札する</p> <br> <p>square1001君はこの競り中に品物につけられた値段を時間順に全て記録しました。</p> <p>その記録によると、$N$ 回品物に値段がつけられ、つけられた値段は最初から順に $A_1, A_2, A_3, \dots, A_N$ です。</p> <p>E869120君はこの競りで何個の品物が出品されたか気になっています。</p> <p>square1001君が作ったリストから、この競りで出品された品物の個数としてありうる最小値と最大値を求めてください。</p> <h3>入力</h3> <p>入力は以下の形式で標準入力から与えられる。</p> <pre> $N$ $A_1$ $A_2$ $A_3$ $\cdots$ $A_N$ </pre> <h3>出力</h3> <p>この競りで出品された品物の個数としてありうる最小値と最大値をこの順で改行区切りで出力してください。</p> <p>ただし、最後には改行を入れること。</p> <h3>制約</h3> <ul> <li>$1 \leq N \leq 100000 \ (= 10^5)$</li> <li>$1 \leq A_i \leq 1000000000 \ (= 10^9)$</li> <li>入力は全て整数である。</li> </ul> <h3>入力例1</h3> <pre> 5 8 6 9 1 20 </pre> <h3>出力例1</h3> <pre> 3 5 </pre> <p>3 つの品物が順に、値段 8、値段 9、値段 20 で落札されたとき、品物の個数は最小値 3 となります。</p> <h3>入力例2</h3> <pre> 6 3 3 4 3 3 4 </pre> <h3>出力例2</h3> <pre> 4 6 </pre> <h3>入力例3</h3> <pre> 8 5 5 4 4 3 3 2 2 </pre> <h3>出力例3</h3> <pre> 8 8 </pre>
p02716
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given is an integer sequence <var>A_1, ..., A_N</var> of length <var>N</var>.</p> <p>We will choose exactly <var>\left\lfloor \frac{N}{2} \right\rfloor</var> elements from this sequence so that no two adjacent elements are chosen.</p> <p>Find the maximum possible sum of the chosen elements.</p> <p>Here <var>\lfloor x \rfloor</var> denotes the greatest integer not greater than <var>x</var>.</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>|A_i|\leq 10^9</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 maximum possible sum of the chosen elements.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 1 2 3 4 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>12 </pre> <p>Choosing <var>2</var>, <var>4</var>, and <var>6</var> makes the sum <var>12</var>, which is the maximum possible value.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 -1000 -100 -10 0 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Choosing <var>-10</var> and <var>10</var> makes the sum <var>0</var>, which is the maximum possible value.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>10 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>5000000000 </pre> <p>Watch out for overflow.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>27 18 -28 18 28 -45 90 -45 23 -53 60 28 -74 -71 35 -26 -62 49 -77 57 24 -70 -93 69 -99 59 57 -49 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>295 </pre></section> </div> </span>
p01839
<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-AMS_HTML"> </script> <h2>A - 阿吽の呼吸</h2> <h3>Problem Statement</h3> <p> 時は進んで 2060 年,共に 70 歳を迎える前田さんと後藤さんは長い付き合いの友人であり,大学時代にACM-ICPCで共に戦った仲間でもある. </p> <p> 二人は今でもよく一緒にお茶を飲みつつ,競技プログラミングの話で盛り上がっている. </p> <p> 二人で一緒にお茶を飲む時,前田さんが 1 回<code>A</code>と言うと,その発言の後に後藤さんがちょうど 1 回<code>Un</code>と返事をする習慣がいつのまにか出来た. </p> <p> しかし最近後藤さんは物忘れや勘違いをすることが多く,前田さんが<code>A</code>と言っても,後藤さんはたまに<code>Un</code>の返事を忘れたり,余計に返事をしたりする. </p> <p> ついこの間も前田さんと後藤さんはお茶を飲みながら,二人のお気に入りのデータ構造について話し込んでいたようだ. </p> <p> この時の会話の中から,<code>A</code>で表される前田さんの発言と,<code>Un</code>で表される後藤さんの返事のみからなる記録が時系列で与えられたとき,後藤さんが習慣通りに反応したとみなすことが出来るかチェックしてほしい. </p> <p> 注意点として,前田さんの発言に対し,後藤さんの返事が多少遅れても,後藤さんは習慣通りに反応したとみなせる場合がある,ということが挙げられる. 例えば,前田さんが2回連続して<code>A</code>と言った後,後藤さんが 2 回連続して<code>Un</code>と返事をして会話が終了した場合は,後藤さんが習慣通りの返事をしたとみなされる (Sample Input 2 参照). </p> <p>また,会話が終了した時点で,前田さんが<code>A</code>と言った回数と,後藤さんが<code>Un</code>と返事した回数が一致しても,後藤さんが習慣通りに返事をしたとはみなされない場合もあるので注意すること. 例えば,前田さんが1回<code>A</code>と言った後,後藤さんが 2 回連続して<code>Un</code>と返事し,その後で前田さんが 1 回<code>A</code>と言って会話が終了した場合は,後藤さんが習慣通りの返事をしたとはみなされない (Sample Input 3 参照). </p> <h3>Input</h3> <p> 入力は以下の形式で与えられる. </p> <p> $N$<br> $S_1$<br> $S_2$<br> $…$<br> $S_N$ </p> <p> 最初の行はひとつの整数からなる. $N$ は,記録に含まれる前田さんが<code>A</code>と発言した回数と後藤さんが<code>Un</code>と返事した回数の合計を表し,$1 \leq N \leq 100$ を満たす. その後 $N$ 行に,文字列 $S_i$ が続き,各 $S_i (1 \leq i \leq N)$ は<code>A</code>か<code>Un</code>のどちらかに一致する.ここで<code>A</code>は前田さんの発言,<code>Un</code>は後藤さんの返事を表す. $i$ の小さい順に $S_i$ が記録されたものとする. 前田さんと後藤さんが同時に発言することは無かったとする. </p> <h3>Output</h3> <p> 後藤さんが習慣通りに反応したとみなすことが出来れば<code>YES</code>,出来なければ<code>NO</code>を1行で出力すること. </p> <h3>Sample Input 1</h3> <pre>4 A Un A Un</pre> <h3>Output for the Sample Input 1</h3> <pre>YES</pre> <h3>Sample Input 2</h3> <pre>4 A A Un Un</pre> <h3>Output for the Sample Input 2</h3> <pre>YES</pre> <h3>Sample Input 3</h3> <pre>4 A Un Un A</pre> <h3>Output for the Sample Input 3</h3> <pre>NO</pre> <h3>Sample Input 4</h3> <pre>1 Un</pre> <h3>Output for the Sample Input 4</h3> <pre>NO</pre>
p03804
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an image <var>A</var> composed of <var>N</var> rows and <var>N</var> columns of pixels, and a template image <var>B</var> composed of <var>M</var> rows and <var>M</var> columns of pixels.<br/> A pixel is the smallest element of an image, and in this problem it is a square of size <var>1×1</var>.<br/> Also, the given images are binary images, and the color of each pixel is either white or black. </p> <p>In the input, every pixel is represented by a character: <code>.</code> corresponds to a white pixel, and <code>#</code> corresponds to a black pixel.<br/> The image <var>A</var> is given as <var>N</var> strings <var>A_1,...,A_N</var>.<br/> The <var>j</var>-th character in the string <var>A_i</var> corresponds to the pixel at the <var>i</var>-th row and <var>j</var>-th column of the image <var>A</var> <var>(1≦i,j≦N)</var>.<br/> Similarly, the template image <var>B</var> is given as <var>M</var> strings <var>B_1,...,B_M</var>.<br/> The <var>j</var>-th character in the string <var>B_i</var> corresponds to the pixel at the <var>i</var>-th row and <var>j</var>-th column of the template image <var>B</var> <var>(1≦i,j≦M)</var>. </p> <p>Determine whether the template image <var>B</var> is contained in the image <var>A</var> when only parallel shifts can be applied to the images. </p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦M≦N≦50</var> </li> <li><var>A_i</var> is a string of length <var>N</var> consisting of <code>#</code> and <code>.</code>.</li> <li><var>B_i</var> is a string of length <var>M</var> consisting of <code>#</code> and <code>.</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>N</var> <var>M</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_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <code>Yes</code> if the template image <var>B</var> is contained in the image <var>A</var>. Print <code>No</code> otherwise.</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>Yes </pre> <p>The template image <var>B</var> is identical to the upper-left <var>2 × 2</var> subimage and the lower-right <var>2 × 2</var> subimage of <var>A</var>. Thus, the output should be <code>Yes</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 1 .... .... .... .... # </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>No </pre> <p>The template image <var>B</var>, composed of a black pixel, is not contained in the image <var>A</var> composed of white pixels.</p></section> </div> </span>
p02346
<H1>Range Sum Query</H1> <p> Write a program which manipulates a sequence <var>A</var> = {<var>a<sub>1</sub>, a<sub>2</sub>, . . . , a<sub>n</sub></var>} with the following operations: </p> <ul> <li><var>add(i, x)</var>: add <var>x</var> to <var>a<sub>i</sub></var>.</li> <li><var>getSum(s, t)</var>: print the sum of <var>a<sub>s</sub>, a<sub>s+1</sub>,...,a<sub>t</sub></var>.</li> </ul> <p> Note that the initial values of <var>a<sub>i</sub></var> (<var>i = 1, 2, . . . , n</var>) are 0. </p> <H2>Input</H2> <pre> <var>n</var> <var>q</var> <var>com<sub>1</sub></var> <var>x<sub>1</sub></var> <var>y<sub>1</sub></var> <var>com<sub>2</sub></var> <var>x<sub>2</sub></var> <var>y<sub>2</sub></var> ... <var>com<sub>q</sub></var> <var>x<sub>q</sub></var> <var>y<sub>q</sub></var> </pre> <p> In the first line, <var>n</var> (the number of elements in <var>A</var>) and <var>q</var> (the number of queries) are given. Then, <var>q</var> queries are given where <var>com</var> represents the type of queries. '0' denotes <var>add(x<sub>i</sub>, y<sub>i</sub>)</var> and '1' denotes <var>getSum(x<sub>i</sub>, y<sub>i</sub>)</var>. </p> <H2>Output</H2> <p> For each <var>getSum</var> operation, print the sum in a line. </p> <H2>Constraints</H2> <ul> <li> <var>1 &le; n &le; 100000</var> </li> <li> <var>1 &le; q &le; 100000</var> </li> <li> If <var>com<sub>i</sub></var> is 0, then <var>1 &le; x<sub>i</sub> &le; n</var>, <var>0 &le; y<sub>i</sub> &le; 1000</var>. </li> <li> If <var>com<sub>i</sub></var> is 1, then <var>1 &le; x<sub>i</sub> &le; n</var>, <var>1 &le; y<sub>i</sub> &le; n</var>. </li> </ul> <H2>Sample Input 1</H2> <pre> 3 5 0 1 1 0 2 2 0 3 3 1 1 2 1 2 2 </pre> <H2>Sample Output 1</H2> <pre> 3 2 </pre>
p00681
<h1>Wall Breaker KND</h1> <h2>Problem</h2> <p>KND君は会津大学に在籍する学生プログラマである。彼はプログラマーであるとともに格闘家でもある。そんな彼は甘党であることで知られてるが、特に生クリームが大好物である。生クリームを食べればコンクリートの壁を数回壊すことができるようになるほどである。そのパワーに興味をもった彼の隣人は、生クリームたっぷりの新商品、「マトクリーム」を用いて実験をすることにした。 </p> <p>その実験は、まず迷路を用意しKND君をそこに閉じ込める。その迷路には鍵がかかった扉、扉を開けるための鍵、マトクリームがそれぞれ1つずつ別のマスに存在する。彼は落ちている鍵を拾い、扉の鍵を開けることで外に脱出することが可能である。鍵を持っていなくても扉があるマスに移動することは可能である。また、マトクリームを食べることで迷路の壁を<var> N </var>回だけ壊すことができるようになる。ただし外壁を壊して迷路の外に出ることはできない。さて、彼は最小何回のマス移動で迷路を脱出できるだろうか。</p> <p>サンプル2つの迷路と最短の脱出経路は次の図で示される。SはKND君の初期位置、Mはマトクリーム、Kは鍵、Dは扉を表す。<br> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ACPC2012Day3_F_1"><br> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ACPC2012Day3_F_2"> </p> <h2>Input</h2> <p> 入力は複数のテストケースからなる。 ひとつのテストケースは以下の形式で与えられる。 入力の終了を2つのゼロを含む行で示す。 </p> <pre> <var>(迷路の入力)</var> <var>x<sub>s</sub></var> <var>y<sub>s</sub></var> <var>x<sub>m</sub></var> <var>y<sub>m</sub></var> <var>x<sub>k</sub></var> <var>y<sub>k</sub></var> <var>x<sub>d</sub></var> <var>y<sub>d</sub></var> <var>N</var> </pre> <p>ここで、</p> <ul> <li>迷路の入力:<br />まず迷路の幅と高さを表す2つの整数<var> W </var>,<var> H </var>が来る。次に 2 *<var> H </var>- 1 行の入力がある。このうちの奇数行目は横に隣合うマスの間の壁の有無を表す。最初に空白が1つあり、<var> W </var>- 1 個の1か0が来る。1は壁があること、0は壁がないことを表す。偶数行目は縦に隣合うマスの間の壁の有無を表す。こちらは<var> W </var>個の1か0が来る。同様に1は壁があること、0は壁がないことを表す。</li> <li><var>x<sub>s</sub></var> <var>y<sub>s</sub></var>:主人公の初期位置のマスの座標</li> <li><var>x<sub>m</sub></var> <var>y<sub>m</sub></var>:マトクリームのマスの座標</li> <li><var>x<sub>k</sub></var> <var>y<sub>k</sub></var>:鍵のマスの座標</li> <li><var>x<sub>d</sub></var> <var>y<sub>d</sub></var>:扉のマスの座標</li> <li><var>N</var>:マトクリームをとった場合に迷路の壁を壊せる回数</li> </ul> <h2>Constraints</h2> <p>入力は以下の条件を満たす。</p> <ul> <li>入力はすべて整数。</li> <li>2 &le; <var>W</var>, <var>H</var> &le; 20</li> <li>0 &le; <var>x<sub>s</sub></var>, <var>x<sub>m</sub></var>, <var>x<sub>k</sub></var>, <var>x<sub>d</sub></var> &lt; <var>W</var></li> <li>0 &le; <var>y<sub>s</sub></var>, <var>y<sub>m</sub></var>, <var>y<sub>k</sub></var>, <var>y<sub>d</sub></var> &lt; <var>H</var></li> <li>0 &le; <var>N</var> &le; 100</li> <li>主人公の初期位置のマスの座標、マトクリームのマスの座標、鍵のマスの座標、扉のマスの座標は互いに別の座標である。</li> <li>扉に入ることが不可能なデータセットはない。</li> </ul> <h2>Output</h2> <p>各テストケースにつきKND君が迷路から脱出する最小のマス移動回数を一行に出力せよ。</p> <h2>Sample Input</h2> <pre> 3 3 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 1 1 2 2 1 5 5 0 1 0 0 1 0 0 1 0 1 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1 0 0 0 0 0 0 0 0 1 1 0 0 1 0 4 4 0 1 2 0 0 </pre> <h2>Sample Output</h2> <pre> 4 15 </pre>
p01993
<h2>E: 均衡な辺削除 (Balanced Edge Deletion)</h2> <h3>問題</h3> <p> <var>N</var> 頂点 <var>M</var> 辺の重み付き単純無向グラフ <var>G</var> が与えられる。 頂点には <var>1</var> から <var>N</var>、 辺には <var>1</var> から <var>M</var> の番号がつけられている。 <var>i</var> 番目の辺は頂点 <var>u_i</var> と <var>v_i</var> を結んでおり、そのコストは <var>w_i</var> である。 </p> <p>このグラフに対して、以下の操作を <var>1</var> 度だけ行うことを考える。</p> <ul> <li> <var>G</var> の辺集合から辺を <var>1</var> つ選択し、その辺を削除する。</li> </ul> <p> 上の操作によってグラフが分割されることがあるので、操作後のグラフを <var>A</var>, <var>B</var> と表記することとする。(分割されない場合、<var>B</var> は空グラフであるとする。) <var>W(X)</var> をグラフ <var>X</var> 内にある辺のコストの総和 (グラフに辺が存在しない場合 <var>W(X)=0</var>) とし、$\mathrm{cost}$<var>(A,B)=|W(A)−W(B)|</var> と定義する。$\mathrm{cost}$<var>(A,B)</var> が最小になるような辺 <var>(u,v)</var> を求めよ。複数存在する場合は、<var>u</var> が最小であるものを答えよ。それでも複数存在する場合は、<var>v</var> が最小であるものを答えよ。 </p> <h3>入力形式</h3> <p>入力は以下の形式で与えられます。</p> <pre> <var>N</var> <var>M</var> <var>u_1</var> <var>v_1</var> <var>w_1</var> <var>...</var> <var>u_M</var> <var>v_M</var> <var>w_M</var> </pre> <h3>制約</h3> <ul> <li> <var>2 \leq N \leq 10^5</var></li> <li> <var>1 \leq M \leq 10^5</var></li> <li> <var>1 \leq u_i &lt; v_i \leq N</var></li> <li> <var> i \neq j</var> ならば <var>u_i \neq u_j</var> または <var>v_i \neq v_j</var></li> <li> <var>1 \leq w_i \leq 10^9</var></li> </ul> <p>与えられるグラフは連結である。</p> <h3>出力形式</h3> <p>答えとなる辺を以下のように空白区切りで一行に出力せよ。</p> <pre><var>u</var> <var>v</var></pre> <ul> <li> <var>u, v</var> は整数</li> <li> <var>1 \leq u, v \leq N</var></li> </ul> <h3>入力例1</h3> <pre> 5 4 1 2 1 2 3 10 3 4 5 4 5 1 </pre> <h3>出力例1</h3> <pre>2 3</pre> <h3>入力例2</h3> <pre> 10 11 1 2 1 2 3 10 1 5 2 3 4 7 3 5 9 5 6 8 6 7 5 6 8 3 7 8 12 7 9 1 9 10 8 </pre> <h3>出力例2</h3> <pre>5 6</pre>
p03107
<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> cubes stacked vertically on a desk.</p> <p>You are given a string <var>S</var> of length <var>N</var>. The color of the <var>i</var>-th cube from the bottom is red if the <var>i</var>-th character in <var>S</var> is <code>0</code>, and blue if that character is <code>1</code>.</p> <p>You can perform the following operation any number of times: choose a red cube and a blue cube that are adjacent, and remove them. Here, the cubes that were stacked on the removed cubes will fall down onto the object below them.</p> <p>At most how many cubes can be removed?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 10^5</var></li> <li><var>|S| = N</var></li> <li>Each character in <var>S</var> is <code>0</code> or <code>1</code>.</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> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum number of cubes that can be removed.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>0011 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>All four cubes can be removed, by performing the operation as follows:</p> <ul> <li>Remove the second and third cubes from the bottom. Then, the fourth cube drops onto the first cube.</li> <li>Remove the first and second cubes from the bottom.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>11011010001011 </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>0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre></section> </div> </span>
p03557
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>The season for Snuke Festival has come again this year. First of all, Ringo will perform a ritual to summon Snuke. For the ritual, he needs an altar, which consists of three parts, one in each of the three categories: upper, middle and lower.</p> <p>He has <var>N</var> parts for each of the three categories. The size of the <var>i</var>-th upper part is <var>A_i</var>, the size of the <var>i</var>-th middle part is <var>B_i</var>, and the size of the <var>i</var>-th lower part is <var>C_i</var>.</p> <p>To build an altar, the size of the middle part must be strictly greater than that of the upper part, and the size of the lower part must be strictly greater than that of the middle part. On the other hand, any three parts that satisfy these conditions can be combined to form an altar.</p> <p>How many different altars can Ringo build? Here, two altars are considered different when at least one of the three parts used is different.</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 A_i \leq 10^9(1\leq i\leq N)</var></li> <li><var>1 \leq B_i \leq 10^9(1\leq i\leq N)</var></li> <li><var>1 \leq C_i \leq 10^9(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>...</var> <var>A_N</var> <var>B_1</var> <var>...</var> <var>B_N</var> <var>C_1</var> <var>...</var> <var>C_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of different altars that Ringo can build.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 1 5 2 4 3 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>The following three altars can be built:</p> <ul> <li>Upper: <var>1</var>-st part, Middle: <var>1</var>-st part, Lower: <var>1</var>-st part</li> <li>Upper: <var>1</var>-st part, Middle: <var>1</var>-st part, Lower: <var>2</var>-nd part</li> <li>Upper: <var>1</var>-st part, Middle: <var>2</var>-nd part, Lower: <var>2</var>-nd part</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 1 1 1 2 2 2 3 3 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>27 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 3 14 159 2 6 53 58 9 79 323 84 6 2643 383 2 79 50 288 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>87 </pre></section> </div> </span>
p01090
<h2>Bridge Construction Planning</h2> <p> There is a city consisting of many small islands, and the citizens live in these islands. Citizens feel inconvenience in requiring ferry rides between these islands. The city mayor decided to build bridges connecting all the islands. </p> <p> The city has two construction companies, A and B. The mayor requested these companies for proposals, and obtained proposals in the form: "Company A (or B) can build a bridge between islands <i>u</i> and <i>v</i> in <i>w</i> hundred million yen." </p> <p> The mayor wants to accept some of these proposals to make a plan with the lowest budget. However, if the mayor accepts too many proposals of one company, the other may go bankrupt, which is not desirable for the city with only two construction companies. However, on the other hand, to avoid criticism on wasteful construction, the mayor can only accept the minimum number (i.e., <i>n</i> &#8722; 1) of bridges for connecting all the islands. Thus, the mayor made a decision that exactly <i>k</i> proposals by the company A and exactly <i>n</i> &#8722; 1 &#8722; <i>k</i> proposals by the company B should be accepted. </p> <p> Your task is to write a program that computes the cost of the plan with the lowest budget that satisfies the constraints. Here, the cost of a plan means the sum of all the costs mentioned in the accepted proposals. </p> <h3>Input</h3> <p> The input consists of multiple datasets. The number of datasets is at most 30. Each dataset is in the following format. </p> <blockquote> <i>n</i> <i>m</i> <i>k</i><br> <i>u</i><sub>1</sub> <i>v</i><sub>1</sub> <i>w</i><sub>1</sub> <i>l</i><sub>1</sub><br> ... <br> <i>u<sub>m</sub></i> <i>v<sub>m</sub></i> <i>w<sub>m</sub></i> <i>l<sub>m</sub></i><br> </blockquote> <p> The first line contains three integers <i>n</i>, <i>m</i>, and <i>k</i>, where <i>n</i> is the number of islands, <i>m</i> is the total number of proposals, and <i>k</i> is the number of proposals that are to be ordered to company A (2 &#8804; <i>n</i> &#8804; 200, 1 &#8804; <i>m</i> &#8804; 600, and 0 &#8804; <i>k</i> &#8804; <i>n</i>&#8722;1). Islands are identified by integers, 1 through <i>n</i>. The following <i>m</i> lines denote the proposals each of which is described with three integers <i>u<sub>i</sub></i>, <i>v<sub>i</sub></i>, <i>w<sub>i</sub></i> and one character <i>l<sub>i</sub></i>, where <i>u<sub>i</sub></i> and <i>v<sub>i</sub></i> denote two bridged islands, <i>w<sub>i</sub></i> is the cost of the bridge (in hundred million yen), and <i>l<sub>i</sub></i> is the name of the company that submits this proposal (1 &#8804; <i>u<sub>i</sub></i> &#8804; <i>n</i>, 1 &#8804; <i>v<sub>i</sub></i> &#8804; <i>n</i>, 1 &#8804; <i>w<sub>i</sub></i> &#8804; 100, and <i>l<sub>i</sub></i> = 'A' or 'B'). You can assume that each bridge connects distinct islands, i.e., <i>u<sub>i</sub></i> &#8800; <i>v<sub>i</sub></i>, and each company gives at most one proposal for each pair of islands, i.e., {<i>u<sub>i</sub></i>, <i>v<sub>i</sub></i>} &#8800; {<i>u<sub>j</sub></i>, <i>v<sub>j</sub></i>} if <i>i</i> &#8800; <i>j</i> and <i>l<sub>i</sub></i> = <i>l<sub>j</sub></i>. </p> <p> The end of the input is indicated by a line with three zeros separated by single spaces. </p> <h3>Output</h3> <p> For each dataset, output a single line containing a single integer that denotes the cost (in hundred million yen) of the plan with the lowest budget. If there are no plans that satisfy the constraints, output &#8722;1. </p> <h3>Sample Input</h3> <pre>4 5 2 1 2 2 A 1 3 2 A 1 4 2 A 2 3 1 B 3 4 1 B 5 8 2 1 2 1 A 2 3 1 A 3 4 3 A 4 5 3 A 1 2 5 B 2 3 5 B 3 4 8 B 4 5 8 B 5 5 1 1 2 1 A 2 3 1 A 3 4 1 A 4 5 1 B 3 5 1 B 4 5 3 1 2 2 A 2 4 3 B 3 4 4 B 2 3 5 A 3 1 6 A 0 0 0 </pre> <h3>Output for the Sample Input</h3> <pre>5 16 -1 -1 </pre>
p03520
<span class="lang-en"> <p>Score : <var>800</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke found a record of a tree with <var>N</var> vertices in ancient ruins. The findings are as follows:</p> <ul> <li>The vertices of the tree were numbered <var>1,2,...,N</var>, and the edges were numbered <var>1,2,...,N-1</var>.</li> <li>Edge <var>i</var> connected Vertex <var>a_i</var> and <var>b_i</var>.</li> <li>The length of each edge was an integer between <var>1</var> and <var>10^{18}</var> (inclusive).</li> <li>The sum of the shortest distances from Vertex <var>i</var> to Vertex <var>1,...,N</var> was <var>s_i</var>.</li> </ul> <p>From the information above, restore the length of each edge. <strong>The input guarantees that it is possible to determine the lengths of the edges consistently with the record.</strong> Furthermore, it can be proved that the length of each edge is uniquely determined in such a case.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 10^{5}</var></li> <li><var>1 \leq a_i,b_i \leq N</var></li> <li><var>1 \leq s_i \leq 10^{18}</var></li> <li>The given graph is a tree.</li> <li>All input values are integers.</li> <li>It is possible to consistently restore the lengths of the edges.</li> <li>In the restored graph, the length of each edge is an integer between <var>1</var> and <var>10^{18}</var> (inclusive).</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Scores</h3><ul> <li>In the test set worth <var>300</var> points, <var>a_i = i, b_i = i+1</var>.</li> <li>In the test set worth <var>200</var> points, <var>N \geq 3, a_i = 1, b_i = i+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_1</var> <var>b_1</var> <var>:</var> <var>a_{N-1}</var> <var>b_{N-1}</var> <var>s_1</var> <var>s_2</var> <var>...</var> <var>s_{N}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>N-1</var> lines. The <var>i</var>-th line must contain the length of Edge <var>i</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 1 2 2 3 3 4 8 6 6 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 </pre> <ul> <li>The given record corresponds to the tree shown below:</li> </ul> <div style="text-align: center;"> <img alt="010664dd33d69063a99075c0f7a391f8.png" src="https://atcoder.jp/img/asaporo2/010664dd33d69063a99075c0f7a391f8.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 1 2 1 3 1 4 1 5 10 13 16 19 22 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 2 3 4 </pre> <ul> <li>The given record corresponds to the tree shown below:</li> </ul> <div style="text-align: center;"> <img alt="41891e0c5dc01850fd29636b200f7f49.png" src="https://atcoder.jp/img/asaporo2/41891e0c5dc01850fd29636b200f7f49.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>15 9 10 9 15 15 4 4 13 13 2 13 11 2 14 13 6 11 1 1 12 12 3 12 7 2 5 14 8 1154 890 2240 883 2047 2076 1590 1104 1726 1791 1091 1226 841 1000 901 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>5 75 2 6 7 50 10 95 9 8 78 28 89 8 </pre></section> </div> </span>
p03170
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a set <var>A = \{ a_1, a_2, \ldots, a_N \}</var> consisting of <var>N</var> positive integers. Taro and Jiro will play the following game against each other.</p> <p>Initially, we have a pile consisting of <var>K</var> stones. The two players perform the following operation alternately, starting from Taro:</p> <ul> <li>Choose an element <var>x</var> in <var>A</var>, and remove exactly <var>x</var> stones from the pile.</li> </ul> <p>A player loses when he becomes unable to play. Assuming that both players play optimally, determine the winner.</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 100</var></li> <li><var>1 \leq K \leq 10^5</var></li> <li><var>1 \leq a_1 &lt; a_2 &lt; \cdots &lt; a_N \leq K</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>K</var> <var>a_1</var> <var>a_2</var> <var>\ldots</var> <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If Taro will win, print <code>First</code>; if Jiro will win, print <code>Second</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 4 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>First </pre> <p>If Taro removes three stones, Jiro cannot make a move. Thus, Taro wins.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 5 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>Second </pre> <p>Whatever Taro does in his operation, Jiro wins, as follows:</p> <ul> <li>If Taro removes two stones, Jiro can remove three stones to make Taro unable to make a move.</li> <li>If Taro removes three stones, Jiro can remove two stones to make Taro unable to make a move.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>2 7 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>First </pre> <p>Taro should remove two stones. Then, whatever Jiro does in his operation, Taro wins, as follows:</p> <ul> <li>If Jiro removes two stones, Taro can remove three stones to make Jiro unable to make a move.</li> <li>If Jiro removes three stones, Taro can remove two stones to make Jiro unable to make a move.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>3 20 1 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>Second </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>3 21 1 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>First </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 6</h3><pre>1 100000 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 6</h3><pre>Second </pre></section> </div> </span>
p02331
<!--<h1>写像12相 その1:ボールに区別あり・箱に区別あり・入れ方に制限なし</h1>--> <h1>Balls and Boxes 1</h1> <table border=""> <tr><th>Balls</th><th>Boxes</th><th>Any way</th><th>At most one ball</th><th>At least one ball</th></tr> <tr><th>Distinguishable</th><th>Distinguishable</th><td style="background-color:#aff">1</td><td>2</td><td>3</td></tr> <tr><th>Indistinguishable</th><th>Distinguishable</th><td>4</td><td>5</td><td>6</td></tr> <tr><th>Distinguishable</th><th>Indistinguishable</th><td>7</td><td>8</td><td>9</td></tr> <tr><th>Indistinguishable</th><th>Indistinguishable</th><td>10</td><td>11</td><td>12</td></tr> </table> <h2>Problem</h2> <p>You have $n$ balls and $k$ boxes. You want to put these balls into the boxes.</p> <p>Find the number of ways to put the balls under the following conditions:</p> <ul> <li>Each ball is distinguished from the other.</li> <li>Each box is distinguished from the other.</li> <li>Each ball can go into only one box and no one remains outside of the boxes.</li> <li>Each box can contain an arbitrary number of balls (including zero).</li> </ul> <p>Note that you must print this count modulo $10^9+7$.</p> <h2>Input</h2> <pre> $n$ $k$ </pre> <p>The first line will contain two integers $n$ and $k$.</p> <h2>Output</h2> <p>Print the number of ways modulo $10^9+7$ in a line.</p> <h2>Constraints</h2> <ul> <li>$1 \le n \le 1000$</li> <li>$1 \le k \le 1000$</li> </ul> <h2>Sample Input 1</h2> <pre> 2 3 </pre> <h2>Sample Output 1</h2> <pre> 9 </pre> <h2>Sample Input 2</h2> <pre> 10 5 </pre> <h2>Sample Output 2</h2> <pre> 9765625 </pre> <h2>Sample Input 3</h2> <pre> 100 100 </pre> <h2>Sample Output 3</h2> <pre> 424090053 </pre>
p02761
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>If there is an integer not less than <var>0</var> satisfying the following conditions, print the smallest such integer; otherwise, print <code>-1</code>.</p> <ul> <li>The integer has exactly <var>N</var> digits in base ten. (We assume <var>0</var> to be a <var>1</var>-digit integer. For other integers, leading zeros are not allowed.)</li> <li>The <var>s_i</var>-th digit from the left is <var>c_i</var>. <var>\left(i = 1, 2, \cdots, M\right)</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 3</var></li> <li><var>0 \leq M \leq 5</var></li> <li><var>1 \leq s_i \leq N</var></li> <li><var>0 \leq c_i \leq 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>s_1</var> <var>c_1</var> <var>\vdots</var> <var>s_M</var> <var>c_M</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>3 3 1 7 3 2 1 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>702 </pre> <p><var>702</var> satisfies the conditions - its <var>1</var>-st and <var>3</var>-rd digits are <code>7</code> and <code>2</code>, respectively - while no non-negative integer less than <var>702</var> satisfies them.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 2 2 1 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>-1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 1 1 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>-1 </pre></section> </div> </span>
p03873
<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>Snuke received <var>N</var> intervals as a birthday present. The <var>i</var>-th interval was <var>[-L_i, R_i]</var>. It is guaranteed that both <var>L_i</var> and <var>R_i</var> are positive. In other words, the origin is strictly inside each interval.</p> <p>Snuke doesn't like overlapping intervals, so he decided to move some intervals. For any positive integer <var>d</var>, if he pays <var>d</var> dollars, he can choose one of the intervals and move it by the distance of <var>d</var>. That is, if the chosen segment is <var>[a, b]</var>, he can change it to either <var>[a+d, b+d]</var> or <var>[a-d, b-d]</var>.</p> <p>He can repeat this type of operation arbitrary number of times. After the operations, the intervals must be pairwise disjoint (however, they may touch at a point). Formally, for any two intervals, the length of the intersection must be zero.</p> <p>Compute the minimum cost required to achieve his goal.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 5000</var></li> <li><var>1 ≤ L_i, R_i ≤ 10^9</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>N</var> <var>L_1</var> <var>R_1</var> : <var>L_N</var> <var>R_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum cost required to achieve his goal.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 2 7 2 5 4 1 7 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>22 </pre> <p>One optimal solution is as follows:</p> <ul> <li>Move the interval <var>[-2, 7]</var> to <var>[6, 15]</var> with <var>8</var> dollars</li> <li>Move the interval <var>[-2, 5]</var> to <var>[-1, 6]</var> with <var>1</var> dollars</li> <li>Move the interval <var>[-4, 1]</var> to <var>[-6, -1]</var> with <var>2</var> dollars</li> <li>Move the interval <var>[-7, 5]</var> to <var>[-18, -6]</var> with <var>11</var> dollars</li> </ul> <p>The total cost is <var>8 + 1 + 2 + 11 = 22</var> dollars.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>20 97 2 75 25 82 84 17 56 32 2 28 37 57 39 18 11 79 6 40 68 68 16 40 63 93 49 91 10 55 68 31 80 57 18 34 28 76 55 21 80 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>7337 </pre></section> </div> </span>
p00249
<H1>アリの巣箱の形状</H1> <p> 求悟君は夏休みの自由研究としてアリの巣の観察を行うことにしました。彼のおじいちゃんが孫のために用意してくれた観察用の透明ケースはとてもユニークで、図1のような形をしています。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_0253_1" title="アリの巣" alt="アリの巣"><br> 図1<br> </center> <br> <p> このケースは、2枚の合同な凸多角形 s1、s2 と何枚かの長方形で構成されています。s1、s2 以外のいずれかの長方形の面を床に接するように置きます。観察を始めた求悟君は、入っている土の量が同じにもかかわらず、底面の選び方によって土の高さが変わることに気付きました(図2)。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_0253_2" title="アリの巣" alt="アリの巣"><br> 図2<br> </center> <br> <p> 求悟君は、ケースの置き方によって、どのくらい高くなるか知りたくなりました。凸多角形 s1 の形、s1 と s2 間の距離 <var>d</var>、土の体積 <var>V</var> を入力とし、土の高さの最大値を出力するプログラムを作成してください。土は、ケースの底面を床に置くとすぐに水平になるものとします。透明ケースの s1 の形は、二次元の座標平面上の <var>n</var> 個の頂点で与えられます。これらの頂点は、反時計回りの順番で入力されます。<var>n</var> は 3 以上 100 以下で、<var>d</var> と <var>V</var> はそれぞれ 1 以上 10,000 以下の整数とします。また、多角形の頂点の座標 <var>(x, y)</var> の <var>x</var>、<var>y</var> はそれぞれ -1,000 以上 1,000 以下の整数とします。<var>V</var>はケースの体積を超えません。 </p> <!--土の高さの最大値は小数点以下第7位を四捨五入して、小数点第6位まで出力してください。--> <h2>入力</h2> <p> 複数のデータセットの並びが入力として与えられます。入力の終わりはゼロみっつの行で示されます。各データセットは以下のとおりです。 </p> <p> 1 行目 <var>n</var> <var>d</var> <var>V</var>(整数 整数 整数;半角空白区切り)<br> 2 行目 第 1 の頂点の座標 <var>x</var> <var>y</var>(整数 整数;半角空白区切り)<br> 3 行目 第 2 の頂点の座標<br> :<br> <var>n+1</var> 行目 第 <var>n</var> の頂点の座標<br> </p> <p> データセットの数は 20 を超えません。 </p> <h2>出力</h2> <p> 入力データセットごとに、土の高さの最大値を出力します。答えには 0.00001 を越える誤差があってはいけません。 </p> <h2>入力例</h2> <pre> 4 1 1 0 0 1 0 1 2 0 2 0 0 0 </pre> <h2>出力例</h2> <pre> 1.000000 </pre>
p02274
<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>The Number of Inversions</H1> <br> <p> For a given sequence $A = \{a_0, a_1, ... a_{n-1}\}$, the number of pairs $(i, j)$ where $a_i > a_j$ and $i < j$, is called the number of inversions. The number of inversions is equal to the number of swaps of Bubble Sort defined in the following program: </p> <pre> bubbleSort(A) cnt = 0 // the number of inversions for i = 0 to A.length-1 for j = A.length-1 downto i+1 if A[j] < A[j-1] swap(A[j], A[j-1]) cnt++ return cnt </pre> <p> For the given sequence $A$, print the number of inversions of $A$. Note that you should not use the above program, which brings Time Limit Exceeded. </p> <H2>Input</H2> <p> In the first line, an integer $n$, the number of elements in $A$, is given. In the second line, the elements $a_i$ ($i = 0, 1, .. n-1$) are given separated by space characters. </p> <H2>output</H2> <p> Print the number of inversions in a line. </p> <H2>Constraints</H2> <ul> <li>$ 1 \leq n \leq 200,000$</li> <li>$ 0 \leq a_i \leq 10^9$</li> <li>$a_i$ are all different</li> </ul> <H2>Sample Input 1</H2> <pre> 5 3 5 2 1 4 </pre> <H2>Sample Output 1</H2> <pre> 6 </pre> <H2>Sample Input 2</H2> <pre> 3 3 1 2 </pre> <H2>Sample Output 2</H2> <pre> 2 </pre>
p00619
<H1><font color="#000000">Problem E:</font> Kuru-Kuru Robot</H1> <p> ロボット工学の研究者であるアシモフ博士は、新しいロボットの開発に成功した。このロボットは、床に張り巡らせた特殊なワイヤーに沿って自由に動くことができる。ロボットは常に現在いるワイヤーに平行な方向を向いて前進する。 </p> <p> すばらしいことに、このロボットはワイヤー上を移動するためにその距離にかかわらずエネルギーを消費しない。しかし、ワイヤーの端点やワイヤーの交点で向きを変えるためにはその点を中心に回転する必要があり、その回転角度だけエネルギーを消費する。 </p> <p> このロボットをスタート地点からゴール地点まで動かしたい。例えば、下図においてロボットを start に東向き(下図 <i>x</i>軸の正の向き)で配置し goal まで動かしてみよう。start から交点 A 交点 B を経由して goal にたどり着く経路と、交点 A 交点 C を経由する経路があるが、前者の方が移動距離が長いものの回転角度が少ないためエネルギー消費量は少ない。 </p> <br> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_kurukuruRobot"> </center> <br> <p> あなたの仕事は、床にあるワイヤーの位置、スタート地点、ゴール地点を読み込み、スタート地点からゴール地点へ移動するために必要な回転角度の合計の最小値を報告するプログラムを作成することである。 </p> <p> ロボットはスタート地点においてどのような向きに配置してもよく、ゴール地点ではどの方向を向いていてもよい。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなる。各データセットの形式は以下のとおり: </p> <pre> <i>n</i> <i>x</i>1<sub>1</sub> <i>y</i>1<sub>1</sub> <i>x</i>2<sub>1</sub> <i>y</i>2<sub>1</sub> <i>x</i>1<sub>2</sub> <i>y</i>1<sub>2</sub> <i>x</i>2<sub>2</sub> <i>y</i>2<sub>2</sub> . . . <i>x</i>1<sub><i>n</i></sub> <i>y</i>1<sub><i>n</i></sub> <i>x</i>2<sub><i>n</i></sub> <i>y</i>2<sub><i>n</i></sub> <i>sx</i> <i>sy</i> <i>gx</i> <i>gy</i> </pre> <p> <i>n</i> はワイヤーの数を示す整数である。<i>x</i>1<sub><i>i</i></sub> <i>y</i>1<sub><i>i</i></sub> は <i>i</i> 番目のワイヤーの一方の端点の座標、<i>x</i>2<sub><i>i</i></sub> <i>y</i>2<sub><i>i</i></sub> は <i>i</i> 番目のワイヤーのもう一方の端点の座標を示す。 </p> <p> <i>sx</i> <i>sy</i>、<i>gx</i> <i>gy</i> はそれぞれスタート地点とゴール地点の座標を示す。 </p> <p> 与えられる座標はすべて整数値であり、<i>x</i>軸 <i>y</i>軸の値は -1000 以上 1000 以下である。また、スタート地点とゴール地点は与えられるワイヤーの端点上にあると仮定してよい。 </p> <p> <i>n</i> &le; 50 であると仮定してよい。 </p> <p> <i>n</i> が 0 のとき入力の終了を示す。 </p> <H2>Output</H2> <p> 各データセットに対して、回転角度の合計の最小値を1行に出力せよ。ロボットがゴール地点にたどり着けない場合は "-1" と出力せよ。出力は 0.00001 以下の誤差を含んでもよい。 </p> <H2>Sample Input</H2> <pre> 8 1 3 11 3 5 3 13 11 10 10 17 10 11 2 11 6 10 5 14 5 13 6 13 2 17 10 17 3 13 3 19 3 1 3 19 3 6 1 3 11 3 5 3 13 11 10 10 17 10 11 2 11 6 10 5 14 5 13 3 19 3 1 3 19 3 2 0 0 7 0 3 0 7 3 0 0 7 3 0 </pre> <H2>Output for the Sample Input</H2> <pre> 270.0000 -1 36.86989765 </pre>
p02624
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>For a positive integer <var>X</var>, let <var>f(X)</var> be the number of positive divisors of <var>X</var>.</p> <p>Given a positive integer <var>N</var>, find <var>\sum_{K=1}^N K\times f(K)</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 10^7</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>Print the value <var>\sum_{K=1}^N K\times f(K)</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>23 </pre> <p>We have <var>f(1)=1</var>, <var>f(2)=2</var>, <var>f(3)=2</var>, and <var>f(4)=3</var>, so the answer is <var>1\times 1 + 2\times 2 + 3\times 2 + 4\times 3 =23</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>26879 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>10000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>838627288460105 </pre> <p>Watch out for overflows.</p></section> </div> </span>
p03936
<span class="lang-en lang-child hidden-lang"> <div id="task-statement"> <div class="part"> Max Score: $1500$ Points <br/> <section> <h3>Problem Statement</h3> There is a $50 \times 50$ field. The cell at $i$-th row and $j$-th column is denoted $(i, j)$ (0-indexed). <br/> One day, square1001 puts $250$ bombs in this field. In each cell, there is 1 or 0 bomb. <br/> You want to know where the bombs are by asking some questions. <br/> <div align="left" class="img-nocaption"> <img src="https://atcoder.jp/img/s8pc-3/f963d1cafbb260ddc067f68c6292487c.png" width="300"/> </div> <br/> You can ask this question many times. <br/> <ul class="simple"> <li>You can ask the number of bombs in the rectangular area in which upper-left cell is $(a, b)$ and lower-right cell is $(c, d)$.</li> </ul> <br/> Please find all bombs' positions asking as few questions as possible. <br/> <br/> Note: This problem Input/Output type is special. Please read Input/Output. If you want to use <code>scanf/printf</code>, you have to write <code>fflush(stdout)</code>.<br/> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input and output</h3> The first line input is following this format:<br/> <blockquote>$H \ W \ N \ K$ </blockquote> <ul class="simple"> <li>$H, W$ is field size. In this problem, $H = 50$ and $W = 50$.</li> <li>$N$ is the number of bombs in the field. In this problem, $N = 250$.</li> <li>$K$ is modulo. This integer uses to answer the state of the field.</li> </ul> <br/> After this input, You can ask some questions, following this output format: <br/> <blockquote>$? \ a \ b \ c \ d$ </blockquote> It means that you want to ask the number of bombs in the rectangular area which upper-left cell is $(a, b)$ and lower-right cell is $(c, d)$.<br/> <br/> You can know the value of the question, from input in this format:<br/> <blockquote>$p$ </blockquote> $p$ is the result of your question. <br/> <br/> Finally, you have to output the answer following this format:<br/> <blockquote>$! \ ans$ </blockquote> The value of $ans$ is $\sum_{i=0}^{H-1} \sum_{j=0}^{W-1} r_{i, j} 2^{iW + j}$ mod $K$. Note: $r_{i, j}$ is the number of bombs in cell $(i, j)$.<br/> </section> </div> <div class="part"> <section> <h3>Constraints</h3> <ul> <li>$H, W = 50$</li> <li>$N = 250$</li> <li>$1,000,000,000 \le K \le 1,000,010,000$ ($K$ is random)</li> </ul> </section> <section> <h3>Score</h3> There are 5 testcases in the judge. <br/> The $score$ in each testcase is defined by following formula: <br/> <ul> <li>$Q$ is the number of questions.</li> <li>If $Q &gt; 2500$, $score = 0$ (Wrong Answer).</li> <li>If $930 \le Q \le 2500$, $score = max(\lfloor 125 - 3.2\sqrt{Q - 920} \rfloor, 40)$.</li> <li>If $880 \le Q &lt; 930$, $score = \lfloor 288 - 22\sqrt{Q - 870} \rfloor$.</li> <li>If $Q &lt; 880$, $score = min(2860 - 3Q, 300)$.</li> </ul> </section> </div> </div> <div class="part"> <section> <h3>Sample Input・Output</h3> In the case of the following arrangement, this input / output is conceivable.<br/> This case is $H,W=4$, so this example is not include in testcases.<br/> <pre> H=4, W=4, N=4 1001 0000 0010 0100 </pre> Example of Input・Output<br/> <table class="table-striped table-bordered table-condensed"> <tr align="center"> <th>Input from program</th> <th>Output</th> </tr> <tr align="center"> <td>4 4 4 1000000007</td> <td> </td> </tr> <tr align="center"> <td> </td> <td>? 0 0 0 1</td> </tr> <tr align="center"> <td>1</td> <td> </td> </tr> <tr align="center"> <td> </td> <td>? 0 1 0 2</td> </tr> <tr align="center"> <td>0</td> <td> </td> </tr> <tr align="center"> <td> </td> <td>? 0 3 1 3</td> </tr> <tr align="center"> <td>1</td> <td> </td> </tr> <tr align="center"> <td> </td> <td>? 2 1 3 2</td> </tr> <tr align="center"> <td>2</td> <td> </td> </tr> <tr align="center"> <td> </td> <td>? 2 2 2 2</td> </tr> <tr align="center"> <td>1</td> <td> </td> </tr> <tr align="center"> <td> </td> <td>! 9225</td> </tr> </table> These question is not always meaningful. <br/> </section> </div> Writer: E869120<br/> </div> </span>
p03465
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given <var>N</var> integers <var>A_1</var>, <var>A_2</var>, ..., <var>A_N</var>.</p> <p>Consider the sums of all non-empty subsequences of <var>A</var>. There are <var>2^N - 1</var> such sums, an odd number.</p> <p>Let the list of these sums in non-decreasing order be <var>S_1</var>, <var>S_2</var>, ..., <var>S_{2^N - 1}</var>.</p> <p>Find the median of this list, <var>S_{2^{N-1}}</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 2000</var></li> <li><var>1 \leq A_i \leq 2000</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 the median of the sorted list of the sums of all non-empty subsequences of <var>A</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>In this case, <var>S = (1, 1, 2, 2, 3, 3, 4)</var>. Its median is <var>S_4 = 2</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 58 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>58 </pre> <p>In this case, <var>S = (58)</var>.</p></section> </div> </span>
p01458
<script src="./IMAGE/varmath.js" charset="UTF-8"></script> <H1>Kth Sentence</H1> <p> A student, Kita_masa, is taking an English examination. In this examination, he has to write a sentence of length <var>m</var>. </p> <p> Since he completely forgot the English grammar, he decided to consider all sentences of length <var>m</var> constructed by concatenating the words he knows and write the <var>K</var>-th sentence among the candidates sorted in lexicographic order. He believes that it must be the correct sentence because <var>K</var> is today's lucky number for him. </p> <p> Each word may be used multiple times (or it's also fine not to use it at all) and the sentence does not contain any extra character between words. Two sentences are considered different if the order of the words are different even if the concatenation resulted in the same string. </p> <H2>Input</H2> <p> The first line contains three integers <var>n</var> (<var>1 \leq n \leq 100</var>), <var>m</var> (<var>1 \leq m \leq 2000</var>) and <var>K</var> (<var>1 \leq K \leq 10^{18}</var>), separated by a single space. Each of the following <var>n</var> lines contains a word that Kita_masa knows. The length of each word is between 1 and 200, inclusive, and the words contain only lowercase letters. You may assume all the words given are distinct. </p> <H2>Output</H2> <p> Print the <var>K</var>-th (1-based) sentence of length <var>m</var> in lexicographic order. If there is no such a sentence, print &quot;-&quot;. </p> <H2>Sample Input 1</H2> <pre> 2 10 2 hello world </pre> <H2>Output for the Sample Input 1</H2> <pre> helloworld </pre> <H2>Sample Input 2</H2> <pre> 3 3 6 a aa b </pre> <H2>Output for the Sample Input 2</H2> <pre> aba </pre> <H2>Sample Input 3</H2> <pre> 2 59 1000000000000000000 a b </pre> <H2>Output for the Sample Input 3</H2> <pre> - </pre>
p03035
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi, who is <var>A</var> years old, is riding a Ferris wheel.</p> <p>It costs <var>B</var> yen (<var>B</var> is an even number) to ride the Ferris wheel if you are <var>13</var> years old or older, but children between <var>6</var> and <var>12</var> years old (inclusive) can ride it for half the cost, and children who are <var>5</var> years old or younger are free of charge. (Yen is the currency of Japan.)</p> <p>Find the cost of the Ferris wheel for Takahashi.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>0 ≤ A ≤ 100</var></li> <li><var>2 ≤ B ≤ 1000</var></li> <li><var>B</var> is an even number.</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>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the cost of the Ferris wheel for Takahashi.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>30 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>100 </pre> <p>Takahashi is <var>30</var> years old now, and the cost of the Ferris wheel is <var>100</var> yen.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>12 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>50 </pre> <p>Takahashi is <var>12</var> years old, and the cost of the Ferris wheel is the half of <var>100</var> yen, that is, <var>50</var> yen.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>0 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> <p>Takahashi is <var>0</var> years old, and he can ride the Ferris wheel for free.</p></section> </div> </span>
p01008
<script src="./IMAGE/varmath.js" charset="UTF-8"></script> <h1>Problem C: Last One</h1> <h2>Problem</h2> <p> あなたは、集合好きな友人Aと一風変わったゲームをしてみる事にした。 </p> <p> n個の要素からなる重複が許される非負の整数の集合Sが与えられる。集合Sに含まれる各要素は非負の<var>p<sub>i</sub></var>進数である。集合Sに対して 以下のStep1~3を1つのターンとして、あなたと相手の二人が交互にターンを繰り返す。各ターンではこの番号の順に操作を行う。 </p> <p> Step 1: 集合Sに含まれる全ての要素を一度取り除き、その取り除いた各要素を2進数に変換して、0を消して1が1つ以上連続する部分に分割し、その分割された部分を全て集合Sに加える。<br/><br/> Step 2: このとき集合Sが空になっていたならば、現在このターンをプレイしているプレイヤーは負ける。</br><br/> Step 3: 集合Sに含まれる要素aを1つ選び、そのaから 1 &le; x &le; a を満たす任意の整数xを引く。</br> <br/> </p> <p> 下の図は次の入力が与えられたときの最初の1ターンの進行の仕方のうちの1つの例である。 </p> <pre> 4 10 3 2 11 16 5 16 AE </pre> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE3_ACPC2013Aizu_aizuicpc_C" width="700"><br/> <p> 最初はあなたのターンだ。与えられた入力に対して両者が最善を尽くすとき、果たしてあなたに勝ち目はあるのだろうか。 </p> <h2>Input</h2> <p> 入力は次の形式で与えられる。 </p> <pre> <var>n</var> <var>p<sub>1</sub></var> <var>m<sub>1</sub></var> <var>p<sub>2</sub></var> <var>m<sub>2</sub></var> ... <var>p<sub>i</sub></var> <var>m<sub>i</sub></var> ... <var>p<sub>n</sub></var> <var>m<sub>n</sub></var> </pre> <p> 集合の要素である<var>p<sub>i</sub></var>進数<var>m<sub>i</sub></var>が<var>n</var>個与えられる。集合の要素の数<var>n</var>は10<sup>5</sup>を超えない。また、2≦<var>p<sub>i<sub></var>≦62を満たす。 <var>p<sub>i</sub></var>進数<var>m<sub>i</sub></var>は通常の数字である0~9に加え、アルファベット52文字を使用する。 その順番は012...789ABC...XYZabc...xyzを昇順とし、<var>i</var>番目までの文字を使うものとする。また、<var>p<sub>i</sub></var>進数<var>m<sub>i</sub></var>は10進数で2<sup>18</sup>-1を超えない. </p> <h2>Output</h2> <p> 与えられた入力に対しあなたが勝てるならwin、負けるならloseを出力せよ。 </p> <h2>Sample Input 1</h2> <pre> 1 10 1 </pre> <h2>Sample Output 1</h2> <pre> win </pre> <h2>Sample Input 2</h2> <pre> 2 10 1 10 1 </pre> <h2>Sample Output 2</h2> <pre> lose </pre> <h2>Sample Input 3</h2> <pre> 3 25 53AI 43 3BI0 62 pn6 </pre> <h2>Sample Output 3</h2> <pre> win </pre>
p00530
<h2>バブルソート(Bubble Sort)</h2> <p> バブルソートとは,列をソートするアルゴリズムの 1 つである.長さ <var>N</var> の数列 <var>A</var> を昇順にソートしたいとしよう.バブルソートは,隣り合う 2 つの数で大小関係が崩れているものがあれば,それらの位置を交換する.これを,数列を前から順に走査しながら行う.すなわち,<var>A<sub>i</sub></var> &gt; <var>A<sub>i+1</sub></var> となっている場所があれば,その 2 数を交換するということを,<var>i</var> = 1, 2, ... , <var>N</var> − 1 に対してこの順で行うのが 1 回の走査である.この走査を <var>N</var> − 1 回繰り返すことで,数列を昇順にソートできることが知られている. </p> <p> 数列 <var>A</var> のバブルソートによる交換回数とは,数列 <var>A</var> に上記のアルゴリズムを適用した時に,整数の交換が行われる回数である.(バブルソートとして知られるアルゴリズム及び実装には,ループの順番や範囲,及び終了条件など,細かな差異がある場合がある.ただし,同じ数列に適用した際の整数の交換回数はそれらの差異により変化しないことが知られている.) </p> <p> 例えば,以下のプログラムは長さ <span>n</span> の整数の配列 <span>a</span> をバブルソートによりソートする関数を C 言語で記述したものである. </p> <pre> void bubble_sort(int *a, int n) { int i, j; for (i = 0; i < n - 1; ++i) { for (j = 0; j < n - 1; ++j) { if (a[j] > a[j + 1]) { /* 以下3 行が1 回の整数の交換に相当*/ int x = a[j]; a[j] = a[j + 1]; a[j + 1] = x; } } } } </pre> <h3>課題</h3> <p> 長さ <var>N</var> の数列 <var>A</var> が与えられる.数列 <var>A</var> の任意の場所の 2 つの整数を 1 回だけ交換した数列 <var>A'</var> を作るとする.数列 <var>A'</var> のバブルソートによる交換回数の最小値を求めるプログラムを作成せよ.(最初に交換する2つの整数は必ずしも隣り合っている必要はないことに注意せよ.) </p> <h3>制限</h3> <ul> <li>1 &le; <var>N</var> &le; 100 000 &nbsp;&nbsp;&nbsp;&nbsp; 数列 <var>A</var> の長さ</li> <li>1 &le; <var>A<sub>i</sub></var> &le; 1 000 000 000 &nbsp;&nbsp;&nbsp;&nbsp;数列 <var>A</var> に含まれる数字の大きさ</li> </ul> <h3>入力</h3> <p> 標準入力から以下のデータを読み込め. </p> <ul> <li> 1 行目には,整数 <var>N</var> が書かれている.<var>N</var> は数列 <var>A</var> の長さを表す.</li> <li> 続く <var>N</var> 行のうちの <var>i</var> 行目(1 &le; <var>i</var> &le; <var>N</var>) には,整数 <var>A<sub>i</sub></var> が書かれている.これは数列 <var>A</var> の <var>i</var> 番目の整数を表す.</li> </ul> <h3>出力</h3> <p> 標準出力に,数列 <var>A'</var> のバブルソートによる交換回数の最小値を表す整数を 1 行で出力せよ. </p> <h3>採点基準</h3> <ul> <li>採点用データのうち,配点の10%分については,<var>N</var> &le; 1 000 を満たし,任意の <var>i, j</var> (1 &le; <var>i</var> &lt; <var>j</var> &le; <var>N</var>) について <var>A<sub>i</sub></var> &ne; <var>A<sub>j</sub></var> を満たす.</li> <li>採点用データのうち,配点の30%分については,<var>N</var> &le; 5 000 を満たし,任意の <var>i, j</var> (1 &le; <var>i</var> &lt; <var>j</var> &le; <var>N</var>) について <var>A<sub>i</sub></var> &ne; <var>A<sub>j</sub></var> を満たす.</li> <li>採点用データのうち,配点の80%分については,任意の <var>i, j</var> (1 &le; <var>i</var> &lt; <var>j</var> &le; <var>N</var>) について <var>A<sub>i</sub></var> &ne; <var>A<sub>j</sub></var> を満たす.</li> </ul> <h3> 入出力例 </h3> <h3>入力例 1</h3> <pre> 5 10 3 6 8 1 </pre> <h3> 出力例 1</h3> <pre> 0 </pre> <p> 数列 <var>A</var> の最初の 10 と最後の 1 を交換することにすると,数列 <var>A'</var> はソート済みの列となり,バブルソートの交換回数は 0 となる. </p> <br> <h3>入力例 2</h3> <pre> 5 3 1 7 9 5 </pre> <h3>出力例 2</h3> <pre> 2 </pre> <p> 数列 <var>A</var> の 3 番目の 7 と最後の 5 を交換することで,数列 <var>A'</var> は 3,1,5,9,7 となる.<var>A'</var> のバブルソートによる交換回数は 2 である. </p> <br> <h3>入力例 3</h3> <pre> 3 1 2 3 </pre> <h3>出力例 3</h3> <pre> 1 </pre> <p> 最初から数列 <var>A</var> がソートされている場合でも,数列 <var>A'</var> を作る際に交換を行わなければならない. </p> <div class="source"> <p class="source"> 問題文と自動審判に使われるデータは、<a href="http://www.ioi-jp.org">情報オリンピック日本委員会</a>が作成し公開している問題文と採点用テストデータです。 </p> </div>
p00160
<H1>宅配料金</H1> <p> ある宅配業者の宅配料金は大きさと重量で下表のように料金が設定されています。 </p> <center> <table> <tr> <td width="80"></td> <td width="100">A サイズ</td> <td width="100">B サイズ</td> <td width="100">C サイズ</td> <td width="100">D サイズ</td> <td width="100">E サイズ</td> <td width="100">F サイズ</td> </tr> <tr> <td>大きさ</td> <td>60cm以内</td> <td>80cm以内</td> <td>100cm以内</td> <td>120cm以内</td> <td>140cm以内</td> <td>160cm以内</td> </tr> <td>重さ</td> <td>2kg以内</td> <td>5kg以内</td> <td>10kg以内</td> <td>15kg以内</td> <td>20kg以内</td> <td>25kg以内</td> </tr> <tr> <td>料金</td> <td>600円</td> <td>800円</td> <td>1000円</td> <td>1200円</td> <td>1400円</td> <td>1600円</td> </tr> </table> </center> <br/> <p> 大きさは三辺(縦、横、高さ)の和です。例えば大きさが120cmで、重さが15kg以内の荷物はDサイズ(1,200円)となります。大きさが120cm以内でも、重さが15kgを超え、20kg以内ならばEサイズとなります。 </p> <p> 1日に持ち込まれた荷物の情報を入力とし、料金の総計を出力するプログラムを作成してください。なお、Fサイズを超えた荷物は対象外とし料金の総計には含みません。 </p> <H2>Input</H2> <p> 複数のデータセットの並びが入力として与えられます。入力の終わりはゼロひとつの行で示されます。 各データセットは以下の形式で与えられます。 </p> <pre> <var>n</var> <var>x<sub>1</sub></var> <var>y<sub>1</sub></var> <var>h<sub>1</sub></var> <var>w<sub>1</sub></var> <var>x<sub>2</sub></var> <var>y<sub>2</sub></var> <var>h<sub>2</sub></var> <var>w<sub>2</sub></var> : <var>x<sub>n</sub></var> <var>y<sub>n</sub></var> <var>h<sub>n</sub></var> <var>w<sub>n</sub></var> </pre> <p> 1行目に荷物の個数<var>n</var> (1 &le; <var>n</var> &le; 10000)、続く<var>n</var> 行に <var>i</var> 番目の荷物の縦の長さ <var>x<sub>i</sub></var>、横の長さ <var>y<sub>i</sub></var>、高さ <var>h<sub>i</sub></var>、重さ <var>w<sub>i</sub></var> (1 &le; <var>x<sub>i</sub></var>, <var>y<sub>i</sub></var>, <var>h<sub>i</sub></var>, <var>w<sub>i</sub></var> &le; 200) が空白区切りで1行に与えられます。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに荷物の料金の総計を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 2 50 25 5 5 80 60 10 30 3 10 15 25 24 5 8 12 5 30 30 30 18 0 </pre> <H2>Output for the Sample Input</H2> <pre> 800 3800 </pre>
p01321
<h1><font color="#000">Problem A:</font> 期末試験!</h1> <p> 私立桜が丘女子高等学校に通う平沢唯さんは明後日までに進路希望を出さなければならないのだが、困ったことに進路についてまだ何も決めていなかった。 友人の和に相談したところ、和の第一志望はK大と知り、自分もK大に入れるかどうか進路指導の先生に相談にいった。 </p> <p> 相談された進路指導の先生であるあなたは唯さんがK大に入れるかどうかを予想するため唯さんの期末試験の成績を参考にすることにした。 しかし、唯さんは試験の山が当たるかどうかによって大きく成績が左右されるため、過去の期末試験の中で一番良かった時の点数と一番悪かった時の点数を調べることにした。 過去の期末試験のデータは5教科の各点数のみしか残っておらず、試験の合計点数は残っていなかった。 したがってあなたの仕事は各試験の点数データを入力として、過去の期末試験の中で一番良かった時の点数と一番悪かった時の点数を出力するプログラムを書くことである。 </p> <h2>Input</h2> <pre> n s<sub>11</sub> s<sub>12</sub> s<sub>13</sub> s<sub>14</sub> s<sub>15</sub> ... s<sub>n1</sub> s<sub>n2</sub> s<sub>n3</sub> s<sub>n4</sub> s<sub>n5</sub> </pre> <p> n は今までに受けた試験の回数である。試験の回数は1以上100以下である。 続く n 行には各試験における5教科の点数が与えられる。点数の範囲は0以上100以下の整数である。 </p> <h2>Output</h2> <p> 最高点 最低点を、一行に出力しなさい。最高点と最低点の間は1文字の空白で区切り、これら以外の文字を含んではならない。 </p> <h2>Notes on Test Cases</h2> <p> 上記入力形式で複数のデータセットが与えられます。各データセットに対して上記出力形式で出力を行うプログラムを作成して下さい。 </p> <p> n が 0 のとき入力の終わりを示します。 </p> <!-- <h2>Sample Input 1</h2> <pre> 3 49 50 87 78 41 27 61 100 45 84 28 88 40 95 66 </pre> <h2>Output for Sample Input 1</h2> <pre>317 305 </pre> <h2>Sample Input 2</h2> <pre>2 100 100 100 100 100 0 0 0 0 0 </pre> <h2>Output for Sample Input 2</h2> <pre>500 0 </pre> <h2>Sample Input 3</h2> <pre>1 89 90 85 93 82 </pre> <h2>Output for Sample Input 3</h2> <pre>439 439 </pre> --> <h2>Sample Input</h2> <pre> 3 49 50 87 78 41 27 61 100 45 84 28 88 40 95 66 2 100 100 100 100 100 0 0 0 0 0 1 89 90 85 93 82 0 </pre> <h2>Output for Sample Input</h2> <pre> 317 305 500 0 439 439 </pre>
p00863
<H1><font color="#000">Problem J:</font> The Teacher’s Side of Math</H1> <p> One of the tasks students routinely carry out in their mathematics classes is to solve a polynomial equation. It is, given a polynomial, say <i>X</i><sup>2</sup> - 4<i>X</i> + 1, to find its roots (2 &plusmn; &radic;3). </p> <p> If the students’ task is to find the roots of a given polynomial, the teacher’s task is then to find a polynomial that has a given root. Ms. Galsone is an enthusiastic mathematics teacher who is bored with finding solutions of quadratic equations that are as simple as <i>a</i> + <i>b</i>&radic;<i>c</i>. She wanted to make higher-degree equations whose solutions are a little more complicated. As usual in problems in mathematics classes, she wants to maintain all coefficients to be integers and keep the degree of the polynomial as small as possible (provided it has the specified root). Please help her by writing a program that carries out the task of the teacher’s side. </p> <p> You are given a number t of the form: </p> <center> <p> t = <sup><i>m</i></sup>&radic;<i>a</i>+<sup><i>n</i></sup>&radic;<i>b</i> </p> </center> <p> where <i>a</i> and <i>b</i> are distinct prime numbers, and <i>m</i> and <i>n</i> are integers greater than 1. </p> <p> In this problem, you are asked to find t's <i>minimal polynomial on integers</i>, which is the polynomial <i>F</i>(<i>X</i>) = <i>a<sub>d</sub>X<sup>d</sup></i> + <i>a</i><sub><i>d</i>-1</sub><i>X</i><sup><i>d</i>-1</sup> + ... + <i>a</i><sub>1</sub><i>X</i> + <i>a</i><sub>0</sub> satisfying the following conditions. </p> <ol> <li> Coefficients <i>a</i><sub>0</sub>, ... , <i>a<sub>d</sub></i> are integers and <i>a<sub>d</sub></i> &gt; 0.</li> <li> <i>F</i>(<i>t</i>) = 0.</li> <li> The degree <i>d</i> is minimum among polynomials satisfying the above two conditions.</li> <li> <i>F</i>(<i>X</i>) is primitive. That is, coefficients <i>a</i><sub>0</sub>, ... , <i>a<sub>d</sub></i> have no common divisors greater than one.</li> </ol> <p> For example, the minimal polynomial of &radic;3+ &radic;2 on integers is <i>F</i>(<i>X</i>) = <i>X</i><sup>4</sup> - 10<i>X</i><sup>2</sup> + 1. Verifying <i>F</i>(<i>t</i>) = 0 is as simple as the following (<i>&alpha;</i> = 3, <i>&beta;</i> = 2). </p> <p> <i>F</i>(<i>t</i>) = (<i>&alpha;</i> + <i>&beta;</i>)<sup>4</sup> - 10(<i>&alpha;</i> + <i>&beta;</i>)<sup>2</sup> + 1 </p> <p> = (<i>&alpha;</i><sup>4</sup> + 4<i>&alpha</i><sup>3</sup><i>&beta;</i> + 6<i>&alpha;</i><sup>2</sup><i>&beta;</i><sup>2</sup> + 4<i>&alpha;&beta;</i><sup>3</sup> + <i>&beta;</i><sup>4</sup> ) - 10(<i>&alpha;</i><sup>2</sup> + 2<i>&alpha;&beta;</i> + <i>&beta;</i><sup>2</sup>) + 1 </p> <p> = 9 + 12<i>&alpha;&beta;</i> + 36 + 8<i>&alpha;&beta;</i> + 4 - 10(3 + 2<i>&alpha;&beta;</i> + 2) + 1 </p> <p> = (9 + 36 + 4 - 50 + 1) + (12 + 8 - 20)<i>&alpha;&beta;</i> </p> <p> = 0 </p> <p> Verifying that the degree of <i>F</i>(<i>t</i>) is in fact minimum is a bit more difficult. Fortunately, under the condition given in this problem, which is that <i>a</i> and <i>b</i> are distinct prime numbers and <i>m</i> and <i>n</i> greater than one, the degree of the minimal polynomial is always <i>mn</i>. Moreover, it is always <i>monic</i>. That is, the coefficient of its highest-order term (<i>a<sub>d</sub></i>) is one. </p> <H2>Input</H2> <p> The input consists of multiple datasets, each in the following format. </p> <center><p> <i> a m b n</i> </p></center> <p> This line represents <sup><i>m</i></sup>&radic;<i>a</i> + <sup><i>n</i></sup>&radic;<i>b</i>. The last dataset is followed by a single line consisting of four zeros. Numbers in a single line are separated by a single space. </p> <p> Every dataset satisfies the following conditions. </p> <ol> <li> <sup><i>m</i></sup>&radic;<i>a</i> + <sup><i>n</i></sup>&radic;<i>b</i> &le; 4.</li> <li> <i>mn</i> &le; 20.</li> <li> The coefficients of the answer <i>a</i><sub>0</sub>, ... , <i>a<sub>d</sub></i> are between (-2<sup>31</sup> + 1) and (2<sup>31</sup> - 1), inclusive. </ol> <H2>Output</H2> <p> For each dataset, output the coefficients of its minimal polynomial on integers <i>F</i>(<i>X</i>) = <i>a<sub>d</sub>X<sup>d</sup></i> + <i>a</i><sub><i>d</i>-1</sub><i>X</i><sup><i>d</i>-1</sup> + ... + <i>a</i><sub>1</sub><i>X</i> + <i>a</i><sub>0</sub>, in the following format. </p> <center><p> <i>a<sub>d</sub></i> <i>a</i><sub><i>d</i>-1</sub> ... <i>a</i><sub>1</sub> <i>a</i><sub>0</sub> </p> </center> <p> Non-negative integers must be printed without a sign (+ or -). Numbers in a single line must be separated by a single space and no other characters or extra spaces may appear in the output. </p> <H2>Sample Input</H2> <pre> 3 2 2 2 3 2 2 3 2 2 3 4 31 4 2 3 3 2 2 7 0 0 0 0 </pre> <H2>Output for the Sample Input</H2> <pre> 1 0 -10 0 1 1 0 -9 -4 27 -36 -23 1 0 -8 0 18 0 -104 0 1 1 0 0 -8 -93 0 24 -2976 2883 -32 -3720 -23064 -29775 1 0 -21 0 189 0 -945 -4 2835 -252 -5103 -1260 5103 -756 -2183 </pre>
p01771
<h1 id="f-tree-木">F : Tree / 木</h1> <h2 id="問題文">問題文</h2> <p>大きさ <var>N</var> の根付き木がある。 各頂点には <var>0</var> から <var>N&minus;1</var> までの番号が付けられ、根が <var>0</var>である。 任意の辺の両端の頂点について、割り当てられた整数は根に近いほうが小さい。初めは全ての辺の重みは <var>0</var> である。</p> <p>この木に対する <var>Q</var> 個のクエリを順に処理せよ。クエリには以下の <var>2</var> 種類がある。</p> <ul> <li>頂点 <var>u</var>, <var>v</var> の距離(パスに含まれる辺の重みの和)を求める。</li> <li>頂点 <var>v</var> の子孫(自身を含まない)に接続する辺の重みを全て <var>x</var> 増加させる。</li> </ul> <h2 id="入力">入力</h2> <p>入力は以下の形式からなる。</p> <pre><var>N</var> <var>Q</var> <var>a_0</var> <var>b_0</var> <var>…</var> <var>a_{N&minus;2}</var> <var>b_{N&minus;2}</var> <var>q_0</var> <var>…</var> <var>q_{Q&minus;1}</var> </pre> <p><var>a_i,b_i</var> は <var>i</var> 番目の辺が結ぶ頂点である。 <var>q_i</var> は <var>i</var> 番目のクエリを表す <var>3</var> つの整数であり、次のうちいずれかである。</p> <ul> <li><var>0 \ u_i \ v_i</var> : 頂点 <var>u_i, v_i</var> の距離を求める。</li> <li><var>1 \ v_i \ x_i</var> : 頂点 <var>v_i</var> の子孫に接続する辺の重みを全て <var>x_i</var> 増加させる。</li> </ul> <h2 id="制約">制約</h2> <ul> <li>入力はすべて整数である</li> <li><var>2 \&le; N \&le; 150\,000</var></li> <li><var>1 \&le; Q \&le; 150\,000</var></li> <li><var>0 \&le; a_{i},b_{i},u_{i},v_{i} \&le; N&minus;1</var></li> <li><var>a_{i} &lt; b_{i}</var></li> <li><var>0 \&le; x_{i} \&le; 300</var></li> </ul> <h2 id="出力">出力</h2> <p>距離を求めるクエリのたびに、結果を1行で出力せよ。</p> <h2 id="注意">注意</h2> <p>この問題では入出力が非常に大きくなりうるので高速な関数を使用してほしい。</p> <h2 id="サンプル">サンプル</h2> <h3 id="サンプル入力1">サンプル入力1</h3> <pre>9 6 0 1 0 2 0 3 1 4 1 5 4 6 5 7 5 8 1 0 1 1 1 2 0 6 3 1 5 5 1 8 4 0 4 3 </pre> <h3 id="サンプル出力1">サンプル出力1</h3> <pre>8 5</pre> <p>図のようになる。</p> <div class="figure"> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_RUPC2015_F1" /> </div> <h3 id="サンプル入力2">サンプル入力2</h3> <pre>13 7 0 1 1 2 2 3 1 4 3 5 1 6 2 7 0 8 7 9 5 10 8 11 1 12 1 2 143 0 8 7 0 10 6 1 1 42 1 6 37 0 3 6 1 6 38 </pre> <h3 id="サンプル出力2">サンプル出力2</h3> <pre>143 429 269 </pre>
p01264
<H1><font color="#000">Problem E:</font> Magical Island</H1> <p> This is a story in the epoch of magic. A clan of magicians lived in an artificial island built by magic power. </p> <p> One day, a crisis erupted on the island. An Empire ACM (Atlas Country of Magic) required unconditional surrender to them, otherwise an imperial force attacked by magical missiles to the island. However, they were so proud that they did not surrender to the ACM, and built a system to generate magical shield to protect the clan from the threat of magical missiles. In this system, a crystal with different elements was put on each corner of the island: the world consisted of four elements, namely Fire, Water, Air and Earth. Each crystal generated magical shield with the element of the crystal by receiving magicians’ magic power; it shielded the island from magical missiles of the same element: any magical missile consists of one of the four elements. Magic shield covered a circular area; the crystal should be located on the center the circular area. The crystal required <i>R</i><sup>2</sup> magic power to shield a circular area of radius <i>R</i>. However, there was one restriction. Magicians should send exactly the same amount of magic power to all crystals, otherwise the island was lost because of losing a balance between four elements. </p> <p> They decided to live in an area which is shielded from any magical missile. Your job is to write a program to calculate minimum amount of magic power to secure enough area for them to live in. </p> <H2>Input</H2> <p> The input consists of multiple datasets. Each dataset is a single line containing three integers <i>W</i>, <i>H</i> and <i>S</i>, separated by a single space. The line containing three zeros separated by a single space indicates the end of the input. </p> <p> <i>W</i> and <i>H</i> are width and depth of the island, respectively. <i>S</i> is the area magicians needed to live in. You may assume that 0 &lt; <i>W</i>, <i>H</i> &le; 100 and 0 &lt; <i>S</i> &le; <i>W</i> &times; <i>H</i>. </p> <H2>Output</H2> <p> For each dataset, output a separate line containing the total minimum necessary magic power. The value may contain an error less than or equal to 0.001. You may print any number of digits after the decimal point. </p> <H2>Sample Input</H2> <pre> 1 1 1 10 15 5 15 10 100 0 0 0 </pre> <H2>Output for the Sample Input</H2> <pre> 8.000 409.479 861.420 </pre>
p03259
<span class="lang-en"> <p>Score : <var>1900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has found two trees <var>A</var> and <var>B</var>, each with <var>N</var> vertices numbered <var>1</var> to <var>N</var>. The <var>i</var>-th edge of <var>A</var> connects Vertex <var>a_i</var> and <var>b_i</var>, and the <var>j</var>-th edge of <var>B</var> connects Vertex <var>c_j</var> and <var>d_j</var>. Also, all vertices of <var>A</var> are initially painted white.</p> <p>Snuke would like to perform the following operation on <var>A</var> zero or more times so that <var>A</var> coincides with <var>B</var>:</p> <ul> <li>Choose a leaf vertex that is painted white. (Let this vertex be <var>v</var>.)</li> <li>Remove the edge incident to <var>v</var>, and add a new edge that connects <var>v</var> to another vertex.</li> <li>Paint <var>v</var> black.</li> </ul> <p>Determine if <var>A</var> can be made to coincide with <var>B</var>, ignoring color. If the answer is yes, find the minimum number of operations required.</p> <p>You are given <var>T</var> cases of this kind. Find the answer for each of them.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq T \leq 20</var></li> <li><var>3 \leq N \leq 50</var></li> <li><var>1 \leq a_i,b_i,c_i,d_i \leq N</var></li> <li>All given graphs are trees.</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>T</var> <var>case_1</var> <var>:</var> <var>case_{T}</var> </pre> <p>Each case is given in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>b_1</var> <var>:</var> <var>a_{N-1}</var> <var>b_{N-1}</var> <var>c_1</var> <var>d_1</var> <var>:</var> <var>c_{N-1}</var> <var>d_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>For each case, if <var>A</var> can be made to coincide with <var>B</var> ignoring color, print the minimum number of operations required, and print <code>-1</code> if it cannot.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 2 2 3 1 3 3 2 6 1 2 2 3 3 4 4 5 5 6 1 2 2 4 4 3 3 5 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 -1 </pre> <ul> <li>The graph in each case is shown below.</li> <li>In case <var>1</var>, <var>A</var> can be made to coincide with <var>B</var> by choosing Vertex <var>1</var>, removing the edge connecting <var>1</var> and <var>2</var>, and adding an edge connecting <var>1</var> and <var>3</var>. Note that Vertex <var>2</var> is not a leaf vertex and thus cannot be chosen.</li> </ul> <div style="text-align: center;"> <img alt="3f020b4a4e883680357cc55adb571fbc.png" src="https://img.atcoder.jp/agc027/3f020b4a4e883680357cc55adb571fbc.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 8 2 7 4 8 8 6 7 1 7 3 5 7 7 8 4 2 5 2 1 2 8 1 3 2 2 6 2 7 4 1 2 2 3 3 4 3 4 2 1 3 2 9 5 3 4 3 9 3 6 8 2 3 1 3 3 8 1 7 4 1 2 8 9 6 3 6 3 5 1 8 9 7 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 0 7 </pre> <ul> <li><var>A</var> may coincide with <var>B</var> from the beginning.</li> </ul></section> </div> </span>
p00926
<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 C: Shopping </h2> <p> Your friend will enjoy shopping. She will walk through a mall along a straight street, where $N$ individual shops (numbered from 1 to $N$) are aligned at regular intervals. Each shop has one door and is located at the one side of the street. The distances between the doors of the adjacent shops are the same length, i.e. a unit length. Starting shopping at the entrance of the mall, she visits shops in order to purchase goods. She has to go to the exit of the mall after shopping. </p> <p> She requires some restrictions on visiting order of shops. Each of the restrictions indicates that she shall visit a shop before visiting another shop. For example, when she wants to buy a nice dress before choosing heels, she shall visit a boutique before visiting a shoe store. When the boutique is farther than the shoe store, she must pass the shoe store before visiting the boutique, and go back to the shoe store after visiting the boutique. </p> <p> If only the order of the visiting shops satisfies all the restrictions, she can visit other shops in any order she likes. </p> <p> Write a program to determine the minimum required walking length for her to move from the entrance to the exit. </p> <p> Assume that the position of the door of the shop numbered $k$ is $k$ units far from the entrance, where the position of the exit is $N + 1$ units far from the entrance. </p> <h3>Input</h3> <p> The input consists of a single test case.<br> <br> $N$ $m$<br> $c_1$ $d_1$<br> .<br> .<br> .<br> $c_m$ $d_m$<br> <br> The first line contains two integers $N$ and $m$, where $N$ ($1 \leq N \leq 1000$) is the number of shops, and $m$ ($0 \leq m \leq 500$) is the number of restrictions. Each of the next $m$ lines contains two integers $c_i$ and $d_i$ ($1 \leq c_i < d_i \leq N$) indicating the $i$-th restriction on the visiting order, where she must visit the shop numbered $c_i$ after she visits the shop numbered $d_i$ ($i = 1, . . . , m$). </p> <p> There are no pair of $j$ and $k$ that satisfy $c_j = c_k$ and $d_j = d_k$. </p> <h3>Output</h3> <p> Output the minimum required walking length for her to move from the entrance to the exit. You should omit the length of her walk in the insides of shops. </p> <h3>Sample Input 1</h3> <pre>10 3 3 7 8 9 2 5</pre> <h3>Sample Output 1</h3> <pre>23</pre> <h3>Sample Input 2</h3> <pre>10 3 8 9 6 7 2 4</pre> <h3>Sample Output 2</h3> <pre>19</pre> <h3>Sample Input 3</h3> <pre>10 0</pre> <h3>Sample Output 3</h3> <pre>11</pre> <h3>Sample Input 4</h3> <pre>10 6 6 7 4 5 2 5 6 9 3 5 6 8</pre> <h3>Sample Output 4</h3> <pre>23</pre> <h3>Sample Input 5</h3> <pre>1000 8 3 4 6 1000 5 1000 7 1000 8 1000 4 1000 9 1000 1 2</pre> <h3>Sample Output 5</h3> <pre>2997</pre>