question_id
stringlengths
6
6
content
stringlengths
1
27.2k
p01315
<h1><font color="#000">Problem B:</font> Moonlight Farm</h1> <!-- <h3><U>ムーンライト牧場</U></h3> --> <p>あなたは大人気webゲーム「ムーンライト牧場」に熱中している.このゲームの目的は,畑で作物を育て,それらを売却して収入を得て,その収入によって牧場を大きくしていくことである.</p> <p>あなたは速く畑を大きくしたいと考えた.そこで手始めにゲーム中で育てることのできる作物を,時間あたりの収入効率にもとづいて並べることにした.</p> <p>作物を育てるには種を買わなければならない.ここで作物 <i>i</i> の種の名前は <i>L<sub>i</sub></i> ,値段は <i>P<sub>i</sub></i> でそれぞれ与えられる.畑に種を植えると時間 <i>A<sub>i</sub></i> 後に芽が出る.芽が出てから時間 <i>B<sub>i</sub></i> 後に若葉が出る.若葉が出てから時間 <i>C<sub>i</sub></i> 後に葉が茂る.葉が茂ってから時間 <i>D<sub>i</sub></i> 後に花が咲く.花が咲いてから時間 <i>E<sub>i</sub></i> 後に実が成る.一つの種から <i>F<sub>i</sub></i> 個の実が成り,それらの実は一つあたり<i>S<sub>i</sub></i> の価格で売れる.一部の作物は多期作であり,計 <i>M<sub>i</sub></i> 回実をつける.単期作の場合は <i>M<sub>i</sub></i> = 1で表される.多期作の作物は,<i>M<sub>i</sub></i> 回目の実が成るまでは,実が成ったあと葉に戻る.ある種に対する収入は,その種から成った全ての実を売った金額から,種の値段を引いた値である.また,その種の収入効率は,その収入を,種を植えてから全ての実が成り終わるまでの時間で割った値である.</p> <p>あなたの仕事は,入力として与えられる作物の情報に対して,それらを収入効率の降順に並べ替えて出力するプログラムを書くことである.</p> <h2>Input</h2> <p>入力はデータセットの列であり,各データセットは次のような形式で与えられる.</p> <blockquote><i>N</i><br /> <i>L<sub>1</sub></i> <i>P<sub>1</sub></i> <i>A<sub>1</sub></i> <i>B<sub>1</sub></i> <i>C<sub>1</sub></i> <i>D<sub>1</sub></i> <i>E<sub>1</sub></i> <i>F<sub>1</sub></i> <i>S<sub>1</sub></i> <i>M<sub>1</sub></i><br /> <i>L<sub>2</sub></i> <i>P<sub>2</sub></i> <i>A<sub>2</sub></i> <i>B<sub>2</sub></i> <i>C<sub>2</sub></i> <i>D<sub>2</sub></i> <i>E<sub>2</sub></i> <i>F<sub>2</sub></i> <i>S<sub>2</sub></i> <i>M<sub>2</sub></i><br /> ...<br /> <i>L<sub>N</sub></i> <i>P<sub>N</sub></i> <i>A<sub>N</sub></i> <i>B<sub>N</sub></i> <i>C<sub>N</sub></i> <i>D<sub>N</sub></i> <i>E<sub>N</sub></i> <i>F<sub>N</sub></i> <i>S<sub>N</sub></i> <i>M<sub>N</sub></i><br /> </blockquote> <p>一行目はデータセットに含まれる作物の数 <i>N</i> である (1 &le; N &le; 50).</p> <p>これに続く <i>N</i> 行には,各行に一つの作物の情報が含まれる.各変数の意味は問題文中で述べた通りである.作物の名前 <i>L<sub>i</sub></i> はアルファベット小文字のみからなる20文字以内の文字列であり,また 1 &le; <i>P<sub>i</sub></i> , <i>A<sub>i</sub></i> , <i>B<sub>i</sub></i> , <i>C<sub>i</sub></i> , <i>D<sub>i</sub></i> , <i>E<sub>i</sub></i> , <i>F<sub>i</sub></i> , <i>S<sub>i</sub></i> &le; 100, 1 &le; <i>M<sub>i</sub></i> &le; 5である.一つのケース内に同一の名前を持つ作物は存在しないと仮定して良い.</p> <p>入力の終りはゼロを一つだけ含む行で表される.</p> <h2>Output</h2> <p>各データセットについて,各行に一つ,作物の名前を収入効率の降順に出力せよ.収入効率が同じ作物に対しては,それらの名前を辞書順の昇順に出力せよ.</p> <p>各データセットの出力の後には&ldquo;#&rdquo;のみからなる1行を出力すること.</p> <h2>Sample Input</h2> <pre> 5 apple 1 1 1 1 1 1 1 10 1 banana 1 2 2 2 2 2 1 10 1 carrot 1 2 2 2 2 2 1 10 2 durian 1 3 3 3 3 3 1 10 1 eggplant 1 3 3 3 3 3 1 100 1 4 enoki 1 3 3 3 3 3 1 10 1 tomato 1 3 3 3 3 3 1 10 1 potato 1 3 3 3 3 3 1 10 1 onion 1 3 3 3 3 3 1 10 1 3 a 10 1 1 1 1 1 1 10 1 b 10 2 2 2 2 2 2 10 1 c 10 2 2 2 2 2 2 10 1 0 </pre> <h2>Output for the Sample Input</h2> <pre> eggplant apple carrot banana durian # enoki onion potato tomato # b c a # </pre>
p00912
<H1><font color="#000">Problem I: </font>Beautiful Spacing</H1> <p> Text is a sequence of words, and a word consists of characters. Your task is to put words into a grid with <i>W</i> columns and sufficiently many lines. For the beauty of the layout, the following conditions have to be satisfied. </p> <ol> <li>The words in the text must be placed keeping their original order. The following figures show correct and incorrect layout examples for a 4 word text "This is a pen" into 11 columns.<br/><br/> <center> <table> <tr> <td><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_beautifulSpacing1"><br/></td> <td><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_beautifulSpacing2"><br/></td> </tr> <tr> <td><p>Figure I.1: A good layout.</p></td> <td><p>Figure I.2: BAD | Do not reorder words.</p></td> </tr> </table><br/> </center> </li> <li> Between two words adjacent in the same line, you must place at least one space character. You sometimes have to put more than one space in order to meet other conditions.<br/><br/> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_beautifulSpacing3"><br/> <p> Figure I.3: BAD | Words must be separated by spaces. </p> </center> <br/> </li> <li> A word must occupy the same number of consecutive columns as the number of characters in it. You cannot break a single word into two or more by breaking it into lines or by inserting spaces.<br/><br/> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_beautifulSpacing4"><br/> <p> Figure I.4: BAD | Characters in a single word must be contiguous. </p> </center> <br/> </li> <li> The text must be justified to the both sides. That is, the first word of a line must startfrom the first column of the line, and except the last line, the last word of a line must end at the last column.<br/><br/> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_beautifulSpacing5"><br/> <p> Figure I.5: BAD | Lines must be justi ed to both the left and the right sides. </p> </center> </li> </ol> <p> The text is the most beautifully laid out when there is no unnecessarily long spaces. For instance, the layout in Figure I.6 has at most 2 contiguous spaces, which is more beautiful than that in Figure I.1, having 3 contiguous spaces. Given an input text and the number of columns, please find a layout such that the length of the longest contiguous spaces between words is minimum. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_beautifulSpacing6" style="align:center"> <br/> <p> Figure I.6: A good and the most beautiful layout. </p> </center> <H2>Input</H2> <p> The input consists of multiple datasets, each in the following format. </p> <p> &nbsp;&nbsp;&nbsp;&nbsp;<i>W N</i><br/> &nbsp;&nbsp;&nbsp;&nbsp;<i>x</i><sub>1</sub> <i>x</i><sub>2</sub> ... <i>x<sub>N</sub></i><br/> </p> <p> <i>W</i>, <i>N</i>, and <i>x<sub>i</sub></i> are all integers. <i>W</i> is the number of columns (3 &le; <i>W</i> &le; 80,000). <i>N</i> is the number of words (2 &le; <i>N</i> &le; 50,000). <i>x<sub>i</sub></i> is the number of characters in the <i>i</i>-th word (1 &le; <i>x<sub>i</sub></i> &le; (<i>W</i>&minus;1)/2 ). Note that the upper bound on <i>x<sub>i</sub></i> assures that there always exists a layout satisfying the conditions. </p> <p> The last dataset is followed by a line containing two zeros. </p> <H2>Output</H2> <p> For each dataset, print the smallest possible number of the longest contiguous spaces between words. </p> <H2>Sample Input</H2> <pre> 11 4 4 2 1 3 5 7 1 1 1 2 2 1 2 11 7 3 1 3 1 3 3 4 100 3 30 30 39 30 3 2 5 3 0 0 </pre> <H2>Output for the Sample Input</H2> <pre> 2 1 2 40 1 </pre>
p01600
<h2>Problem J: Tree Construction</h2> <p>Consider a two-dimensional space with a set of points (<var>x<sub>i</sub></var>, <var>y<sub>i</sub></var>) that satisfy <var>x<sub>i</sub></var> < <var>x<sub>j</sub></var> and <var>y<sub>i</sub></var> > <var>y<sub>j</sub></var> for all <var>i < j</var>. We want to have them all connected by a directed tree whose edges go toward either right (<var>x</var> positive) or upward (<var>y</var> positive). The figure below shows an example tree.</p> <div align="center"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_treeConstruction"><br> Figure 1: An example tree</div> <p>Write a program that finds a tree connecting all given points with the shortest total length of edges.</p> <h2>Input</h2> <p>The input begins with a line that contains an integer <var>n</var> (1 <= <var>n</var> <= 1000), the number of points. Then <var>n</var> lines follow. The <var>i</var>-th line contains two integers <var>x<sub>i</sub></var> and <var>y<sub>i</sub></var> (0 <= <var>x<sub>i</sub></var>, <var>y<sub>i</sub></var> <= 10000), which give the coordinates of the <var>i</var>-th point.</p> <h2>Output</h2> <p>Print the total length of edges in a line.</p> <h2>Sample Input 1</h2> <pre> 5 1 5 2 4 3 3 4 2 5 1 </pre> <h2>Output for the Sample Input 1</h2> <pre>12</pre> <h2>Sample Input 2</h2> <pre> 1 10000 0 </pre> <h2>Output for the Sample Input 2</h2> <pre>0</pre>
p01250
<H1><font color="#000">Problem A:</font> Pi is Three</H1> <p> <i>&pi;</i> (spelled pi in English) is a mathematical constant representing the circumference of a circle whose di- ameter is one unit length. The name <i>&pi;</i> is said to come from the first letter of the Greek words &pi;&epsilon;&rho;&iota;&phi;&#941;&rho;&epsilon;&iota;&alpha; (meaning periphery) and &pi;&epsilon;&rho;&#943;&mu;&epsilon;&tau;&rho;&omicron;&#962; (perimeter). </p> <p> Recently, the government of some country decided to allow use of 3, rather than 3.14, as the approximate value of π in school (although the decision was eventually withdrawn probably due to the blame of many people). This decision is very surprising, since this approximation is far less accurate than those obtained before the common era. </p> <p> Ancient mathematicians tried to approximate the value of <i>&pi;</i> without calculators. A typical method was to calculate the perimeter of inscribed and circumscribed regular polygons of the circle. For example, Archimedes (287–212 B.C.) proved that 223/71 &lt; <i>&pi;</i> &lt; 22/7 using 96-sided polygons, where 223/71 and 22/7 were both accurate to two fractional digits (3.14). The resultant approximation would be more accurate as the number of vertices of the regular polygons increased. </p> <p> As you see in the previous paragraph, <i>&pi;</i> was approximated by fractions rather than decimal numbers in the older ages. In this problem, you are requested to represent <i>&pi;</i> as a fraction with the smallest possible denominator such that the representing value is not different by more than the given allowed error. If more than one fraction meets this criterion, the fraction giving better approximation is preferred. </p> <H2>Input</H2> <p> The input consists of multiple datasets. Each dataset has a real number <i>R</i> (0 &lt; <i>R</i> &le; 1) representing the allowed difference between the fraction and <i>&pi;</i>. The value may have up to seven digits after the decimal point. The input is terminated by a line containing 0.0, which must not be processed. </p> <H2>Output</H2> <p> For each dataset, output the fraction which meets the criteria in a line. The numerator and denominator of the fraction should be separated by a slash as shown in the sample output, and those numbers must be integers. </p> <H2>Sample Input</H2> <pre> 0.15 0.05 0.0 </pre> <H2>Output for the Sample Input</H2> <pre> 3/1 19/6 </pre>
p02885
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>The window of Takahashi's room has a width of <var>A</var>. There are two curtains hung over the window, each of which has a horizontal length of <var>B</var>. (Vertically, the curtains are long enough to cover the whole window.)</p> <p>We will close the window so as to minimize the total horizontal length of the uncovered part of the window. Find the total horizontal length of the uncovered parts of the window then.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq A \leq 100</var></li> <li><var>1 \leq B \leq 100</var></li> <li><var>A</var> and <var>B</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>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the total horizontal length of the uncovered parts of the window.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>12 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>We have a window with a horizontal length of <var>12</var>, and two curtains, each of length <var>4</var>, that cover both ends of the window, for example. The uncovered part has a horizontal length of <var>4</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>20 15 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>If the window is completely covered, print <var>0</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>20 30 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> <p>Each curtain may be longer than the window.</p></section> </div> </span>
p04052
<span class="lang-en"> <p>Score : <var>2000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a permutation <var>P_1 ... P_N</var> of the set {<var>1</var>, <var>2</var>, ..., <var>N</var>}.</p> <p>You can apply the following operation to this permutation, any number of times (possibly zero):</p> <ul> <li>Choose two indices <var>i,j (1 ≦ i &lt; j ≦ N)</var>, such that <var>j - i ≧ K</var> and <var>|P_i - P_j| = 1</var>. Then, swap the values of <var>P_i</var> and <var>P_j</var>.</li> </ul> <p>Among all permutations that can be obtained by applying this operation to the given permutation, find the lexicographically smallest one.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦500,000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>P</var> is a permutation of the set {<var>1</var>, <var>2</var>, ..., <var>N</var>}.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>P_1</var> <var>P_2</var> <var>...</var> <var>P_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the lexicographically smallest permutation that can be obtained.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 2 4 2 3 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 1 4 3 </pre> <p>One possible way to obtain the lexicographically smallest permutation is shown below:</p> <ul> <li><var>4 2 3 1</var></li> <li><var>4 1 3 2</var></li> <li><var>3 1 4 2</var></li> <li><var>2 1 4 3</var></li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 1 5 4 3 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 2 3 4 5 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>8 3 4 5 7 8 3 1 2 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 2 6 7 5 3 4 8 </pre></section> </div> </span>
p03797
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke loves puzzles.</p> <p>Today, he is working on a puzzle using <code>S</code>- and <code>c</code>-shaped pieces. In this puzzle, you can combine two <code>c</code>-shaped pieces into one <code>S</code>-shaped piece, as shown in the figure below:</p> <div style="text-align: center;"> <img alt="9b0bd546db9f28b4093d417b8f274124.png" src="https://atcoder.jp/img/arc069/9b0bd546db9f28b4093d417b8f274124.png"> </img></div> <p>Snuke decided to create as many <code>Scc</code> groups as possible by putting together one <code>S</code>-shaped piece and two <code>c</code>-shaped pieces.</p> <p>Find the maximum number of <code>Scc</code> groups that can be created when Snuke has <var>N</var> <code>S</code>-shaped pieces and <var>M</var> <code>c</code>-shaped pieces.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N,M ≤ 10^{12}</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>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>1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>Two <code>Scc</code> groups can be created as follows:</p> <ul> <li>Combine two <code>c</code>-shaped pieces into one <code>S</code>-shaped piece</li> <li>Create two <code>Scc</code> groups, each from one <code>S</code>-shaped piece and two <code>c</code>-shaped pieces</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>12345 678901 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>175897 </pre></section> </div> </span>
p00011
<H1>Drawing Lots</H1> <p> Let's play Amidakuji. </p> <p> In the following example, there are five vertical lines and four horizontal lines. The horizontal lines can intersect (jump across) the vertical lines. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_amida1"> </center> <br> <p> In the starting points (top of the figure), numbers are assigned to vertical lines in ascending order from left to right. At the first step, 2 and 4 are swaped by the first horizontal line which connects second and fourth vertical lines (we call this operation (2, 4)). Likewise, we perform (3, 5), (1, 2) and (3, 4), then obtain "4 1 2 5 3" in the bottom. </p> <p> Your task is to write a program which reads the number of vertical lines <var>w</var> and configurations of horizontal lines and prints the final state of the Amidakuji. In the starting pints, numbers 1, 2, 3, ..., <var>w</var> are assigne to the vertical lines from left to right. </p> <H2>Input</H2> <pre> <var>w</var> <var>n</var> <var>a<sub>1</sub></var>,<var>b<sub>1</sub></var> <var>a<sub>2</sub></var>,<var>b<sub>2</sub></var> . . <var>a<sub>n</sub></var>,<var>b<sub>n</sub></var> </pre> <p> <var>w</var> (<var>w</var> &le; 30) is the number of vertical lines. <var>n</var> (<var>n</var> &le; 30) is the number of horizontal lines. A pair of two integers <var>a<sub>i</sub></var> and <var>b<sub>i</sub></var> delimited by a comma represents the <var>i</var>-th horizontal line. </p> <H2>Output</H2> <p> The number which should be under the 1st (leftmost) vertical line<br> The number which should be under the 2nd vertical line<br> :<br> The number which should be under the <var>w</var>-th vertical line<br> </p> <H2>Sample Input</H2> <pre> 5 4 2,4 3,5 1,2 3,4 </pre> <H2>Output for the Sample Input</H2> <pre> 4 1 2 5 3 </pre> <!-- <H2>Hint</H2> <a href="IMAGE1/lots.gif">Try it.</a> -->
p00441
<H1>最古の遺跡 </H1> <h2>問題</h2> <p> 昔, そこには集落があり, 多くの人が暮らしていた. 人々は形も大きさも様々な建物を建てた. だが, それらの建造物は既に失われ, 文献と, 遺跡から見つかった柱だけが建造物の位置を知る手がかりだった. </p> <p> 文献には神殿の記述がある. 神殿は上から見ると正確に正方形になっており, その四隅には柱があった. 神殿がどの向きを向いていたかはわからない. また, 辺上や内部に柱があったかどうかもわからない. 考古学者たちは, 遺跡から見つかった柱の中で, 正方形になっているもののうち, 面積が最大のものが神殿に違いないと考えた. </p> <p> 柱の位置の座標が与えられるので, 4 本の柱でできる正方形のうち面積が最大のものを探し, その面積を出力するプログラムを書け. なお, 正方形の辺は座標軸に平行とは限らないことに注意せよ. </p> <h2>例</h2> <p> 下の図の例では, 10 本の柱があり, 座標 (4, 2), (5, 2), (5, 3), (4, 3) にある 4 本と座標 (1, 1), (4, 0), (5, 3), (2, 4) にある 4 本が正方形をなしている. 面積が最大の正方形は後者で, その面積は 10 である. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_oldestSite"> </center> <br> <h2>入力</h2> <p> <!--入力ファイルのファイル名は input.txt である.--> 入力は複数のデータセットからなる.各データセットは以下の形式で与えられる.入力はゼロ1つを含む行で終了する. </p> <p> 1 行目には, 遺跡から見つかった柱の本数 n が書かれている. </p> <p> 2 行目から n + 1 行目までの n 行の各々には, 柱の x 座標と y 座標が空白区切りで書かれている. </p> <p> 1 本の柱が 2 度以上現れることはない. </p> <p> n は 1 &le; n &le; 3000 を満たす整数であり, 柱の x 座標と y 座標は 0 以上 5000 以下の整数である. </p> <p> 採点用データのうち, 配点の 30% 分は 1 &le; n &le; 100 を満たし, 配点の 60% 分は1 &le; n &le; 500 を満たす. </p> <p> データセットの数は 10 を超えない. </p> <h2>出力</h2> <!-- <p> 出力ファイルのファイル名は output.txt である. </p> --> <p> <!-- 出力ファイルには--> データセットごとに 1 個の整数を出力する. 4 本の柱からなる正方形が存在する場合は, そのような正方形のうち面積が最大のものの面積を出力し, そのような正方形が存在しない場合は 0 を出力せよ. </p> <h2>入出力例</h2> <h3>入力例</h3> <pre> 10 9 4 4 3 1 1 4 2 2 4 5 8 4 0 5 3 0 5 5 2 10 9 4 4 3 1 1 4 2 2 4 5 8 4 0 5 3 0 5 5 2 0 </pre> <h3>出力例</h3> <pre> 10 10 </pre> <div class="source"> <p class="source"> 上記問題文と自動審判に使われるデータは、<a href="http://www.ioi-jp.org">情報オリンピック日本委員会</a>が作成し公開している問題文と採点用テストデータです。 </p> </div>
p02186
<h2>E: 総和の切り取り</h2> <h3>問題</h3> <p> えびちゃんは数列 <var>(a_1, a_2, ..., a_n)</var> を持っています。 えびちゃんは(空でもよい)部分配列の総和の最大値が気になるので、それを求めてください。 ここで、部分配列とは連続する部分列を指します。なお、空列の総和は <var>0</var> とします。 </p> <p>さらに、えびちゃんがこの数列を <var>q</var> 回書き換えるので、各書き換えの直後にこの値を求めてください。</p> <h3>入力形式</h3> <pre> <var>n</var> <var>q</var> <var>a_1</var> <var>a_2</var> ... <var>a_n</var> <var>k_1</var> <var>x_1</var> ... <var>k_q</var> <var>x_q</var> </pre> <p> 各 <var>k_j</var>, <var>x_j</var> (<var>1 \leq j \leq q</var>) は、<var>a_{k_j}</var> を <var>x_j</var> に書き換えることを意味します。 各書き換えは独立ではなく、その後の処理において配列は書き換えられたままであることに注意してください。 </p> <h3>制約</h3> <ul> <li><var>1\leq n\leq 10^5</var></li> <li><var>1\leq q\leq 10^5</var></li> <li><var>1\leq k_j\leq n</var></li> <li><var>|a_i|, |x_j| \leq 10^9</var></li> </ul> <h3>出力形式</h3> <p><var>q+1</var> 行出力してください。<var>1</var> 行目には書き換えを行う前の、<var>1+i</var> 行目には <var>i</var> 回目の書き換えの直後の部分配列の総和の最大値を出力してください。</p> <h3>入力例1</h3> <pre> 5 2 1 2 -3 4 -5 3 3 2 -6 </pre> <h3>出力例1</h3> <pre> 4 10 7 </pre> <p>部分配列 <var>(a_l, …, a_r)</var> を <var>a[l, r]</var> と表すことにすると、書き換え前は <var>a[1, 4]</var> および <var>a[4, 4]</var> の総和が <var>4</var> で、これが最大です。</p> <p><var>1</var> 回目の書き換えの後、数列は <var>(1, 2, 3, 4, -5)</var> となり、総和の最大値は <var>a[1, 4]</var> の <var>10</var> です。</p> <p><var>2</var> 回目の書き換えの後、数列は <var>(1, -6, 3, 4, -5)</var> となり、総和の最大値は <var>a[3, 4]</var> の <var>7</var> です。</p> <h3>入力例2</h3> <pre> 3 1 -1 -2 -3 1 1 </pre> <h3>出力例2</h3> <pre> 0 1 </pre> <p>空列の総和は <var>0</var> であり、書き換え前はそれが最大です。</p>
p00695
<H1> Get a Rectangular Field </H1> <P> Karnaugh is a poor farmer who has a very small field. He wants to reclaim wasteland in the kingdom to get a new field. But the king who loves regular shape made a rule that a settler can only get a rectangular land as his field. Thus, Karnaugh should get the largest rectangular land suitable for reclamation. </P> <P> The map of the wasteland is represented with a 5 x 5 grid as shown in the following figures, where '1' represents a place suitable for reclamation and '0' represents a sandy place. Your task is to find the largest rectangle consisting only of 1's in the map, and to report the size of the rectangle. The size of a rectangle is defined by the number of 1's in it. Note that there may be two or more largest rectangles of the same size. </P> <pre> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_sample1s"> Figure 1. The size of the largest rectangles is 4. There are two largest rectangles (shaded). <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_sample2s"> Figure 2. The size of the largest rectangle is 3. There is one largest rectangle (shaded). </pre> <P> <H2>Input</H2> <p> The input consists of maps preceded by an integer <I>m</I> indicating the number of input maps. </p> <dir> <pre> <I>m</I> <I>map</I><SUB>1</SUB> <I>map</I><SUB>2</SUB> <I>map</I><SUB>3</SUB> ... <I>map</I><SUB><I>m</I></SUB> </pre> </dir> <p> Two maps are separated by an empty line.<br> Each <I>map</I><SUB><I>k</I></SUB> is in the following format: </p> <dir> <pre> <I> p p p p p </I> <I> p p p p p </I> <I> p p p p p </I> <I> p p p p p </I> <I> p p p p p </I> </pre> </dir> <p> Each <I>p</I> is a character either '1' or '0'. Two <I>p</I>'s in the same line are separated by a space character. Here, the character '1' shows a place suitable for reclamation and '0' shows a sandy place. Each map contains at least one '1'. </p> <H2>Output</H2> <p> The size of the largest rectangle(s) for each input map should be shown each in a separate line. </p> <H2>Sample Input</H2> <pre> 3 1 1 0 1 0 0 1 1 1 1 1 0 1 0 1 0 1 1 1 0 0 1 1 0 0 0 1 0 1 1 0 1 0 1 0 0 0 1 0 0 0 0 1 1 0 1 0 1 0 0 1 1 1 1 0 0 1 1 1 0 0 1 1 0 1 0 1 1 1 0 0 0 0 0 1 </pre> <H2>Output for the Sample Input</H2> <PRE> 4 3 8 </PRE>
p01987
<h3>カジノ</h3> <!-- begin ja only --> <p>あなたの勤めている会社では新たなカジノゲームの開発をしている.今日は最近提案されたゲームについて考察してみることにした.</p> <p>このゲームでは <i>N</i> 個のサイコロを同時に振って,出た目の合計が得点となる.プレイヤーは得点を大きくすることを目標としてプレーする.もし出た目が気に入らなければサイコロを振り直すことができるが,その時は <i>N</i> 個のサイコロ全てを同時に振り直さなければならない.また,サイコロを振ることのできる回数は最大で <i>M</i> 回である.十分に大きい目が出たと思ったのであれば,<i>M</i> 回より少ない回数でゲームを終えても良く,最後に振ったサイコロの目の合計が得点となる.</p> <p>適切な賭け金を設定するためには,得点の期待値を求めなければならない.<i>N</i> と <i>M</i> が与えられた時,最適な戦略をとった場合に得られる得点の期待値を求めるのが今日のあなたの仕事である.</p> <p>ただし,このゲームで用いるサイコロには 1 から 6 までの目があり,それぞれの目が出る確率は同じである.</p> <!-- end ja only --> <h3>Input</h3> <!-- begin ja only --> <p>入力は最大で 100 個のデータセットからなる.各データセットは次の形式で表される.</p> <blockquote><i>N</i> <i>M</i></blockquote> <p>整数 <i>N</i>,<i>M</i> は <i>1 &le; N &le; 20</i>,<i>1 &le; M &le; 10<sup>10</sup></i> を満たす.</p> <p>入力の終わりは 2 つのゼロからなる行で表される.</p> <!-- end ja only --> <h3>Output</h3> <!-- begin ja only --> <p>各データセットについて,最適な戦略をとった時の期待値を一行に出力せよ.出力には <i>10<sup>-2</sup></i> を超える絶対誤差があってはならない.この問題は計算誤差が大きくなる解法が想定されるため,精度のよい型を用いることが推奨される.</p> <!-- end ja only --> <h3>Sample Input</h3><pre>1 2 2 1 2 2 0 0 </pre><h3>Output for the Sample Input</h3><pre>4.25 7 7.9722222222 </pre>
p02352
<H1>RMQ and RAQ</H1> <p> Write a program which manipulates a sequence $A$ = {$a_0, a_1, ..., a_{n-1}$} with the following operations: </p> <ul> <li> $add(s, t, x)$ : add $x$ to $a_s, a_{s+1}, ..., a_t$.</li> <li> $find(s, t)$ : report the minimum value in $a_s, a_{s+1}, ..., a_t$.</li> </ul> <p> Note that the initial values of $a_i ( i = 0, 1, ..., n-1 )$ are 0. </p> <h2>Input</h2> <pre> $n$ $q$ $query_1$ $query_2$ : $query_q$ </pre> <p> In the first line, $n$ (the number of elements in $A$) and $q$ (the number of queries) are given. Then, $i$th query $query_i$ is given in the following format: </p> <pre> 0 $s$ $t$ $x$ </pre> <p>or</p> <pre> 1 $s$ $t$ </pre> <p> The first digit represents the type of the query. '0' denotes $add(s, t, x)$ and '1' denotes $find(s, t)$. </p> <h2>Output</h2> <p> For each $find$ query, print the minimum value. </p> <h2>Constraints</h2> <ul> <li>$1 &le; n &le; 100000$</li> <li>$1 &le; q &le; 100000$</li> <li>$0 &le; s &le; t &lt; n$</li> <li>$-1000 &le; x &le; 1000$</li> </ul> <h2>Sample Input 1</h2> <pre> 6 7 0 1 3 1 0 2 4 -2 1 0 5 1 0 1 0 3 5 3 1 3 4 1 0 5 </pre> <h2>Sample Output 1</h2> <pre> -2 0 1 -1 </pre>
p02702
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given is a string <var>S</var> consisting of digits from <code>1</code> through <code>9</code>.</p> <p>Find the number of pairs of integers <var>(i,j)</var> (<var>1 ≤ i ≤ j ≤ |S|</var>) that satisfy the following condition:</p> <p>Condition: In base ten, the <var>i</var>-th through <var>j</var>-th characters of <var>S</var> form an integer that is a multiple of <var>2019</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ |S| ≤ 200000</var></li> <li><var>S</var> is a string consisting of digits from <code>1</code> through <code>9</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 number of pairs of integers <var>(i,j)</var> (<var>1 ≤ i ≤ j ≤ |S|</var>) that satisfy the condition.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>1817181712114 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>Three pairs - <var>(1,5)</var>, <var>(5,9)</var>, and <var>(9,13)</var> - satisfy the condition.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>14282668646 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>2119 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> <p>No pairs satisfy the condition.</p></section> </div> </span>
p03810
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> integers written on a blackboard. The <var>i</var>-th integer is <var>A_i</var>, and the greatest common divisor of these integers is <var>1</var>.</p> <p>Takahashi and Aoki will play a game using these integers. In this game, starting from Takahashi the two player alternately perform the following operation:</p> <ul> <li>Select one integer on the blackboard that is not less than <var>2</var>, and subtract <var>1</var> from the integer.</li> <li>Then, divide all the integers on the black board by <var>g</var>, where <var>g</var> is the greatest common divisor of the integers written on the blackboard.</li> </ul> <p>The player who is left with only <var>1</var>s on the blackboard and thus cannot perform the operation, loses the game. Assuming that both players play optimally, determine the winner of the game.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ N ≦ 10^5</var></li> <li><var>1 ≦ A_i ≦ 10^9</var></li> <li>The greatest common divisor of the integers from <var>A_1</var> through <var>A_N</var> is <var>1</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>A_2</var> … <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If Takahashi will win, print <code>First</code>. If Aoki will win, print <code>Second</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 6 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>First </pre> <p>Takahashi, the first player, can win as follows:</p> <ul> <li>Takahashi subtracts <var>1</var> from <var>7</var>. Then, the integers become: <var>(1,2,2)</var>.</li> <li>Aoki subtracts <var>1</var> from <var>2</var>. Then, the integers become: <var>(1,1,2)</var>.</li> <li>Takahashi subtracts <var>1</var> from <var>2</var>. Then, the integers become: <var>(1,1,1)</var>.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 1 2 4 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>First </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 7 8 8 8 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Second </pre></section> </div> </span>
p01084
<h1>Problem M: Dial</h1> <h2>Problem</h2> <p> ダイヤル式の南京錠がある。この南京錠には、5つのダイヤルと、決定ボタンが用意されている。 </p> <p> 5つあるダイヤルを回転させて16進数で5ケタの整数を作り、決定ボタンを押して入力すると、入力した値とパスワードが一致していれば開く仕組みになっている。5つのダイヤルそれぞれには<br><br> 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,0,1,2, ...<br><br> の順で文字が現れるように書かれている。 </p> <p> 持ち主はパスワードを忘れてしまった。しかし、パスワードの5ケタのうち、1つのケタだけが偶数で残り4つは奇数であったことは覚えている。 ただし、偶数とは0,2,4,6,8,a,c,eであり、奇数とは1,3,5,7,9,b,d,fである。 </p> <p> またこの鍵は不良品で、以下の条件でパスワードと異なる数字を入力しても開いてしまうことが分かっている。本来、入力した整数とパスワードが5つのケタすべてについて一致していなければ鍵は開かない。しかし、パスワードの偶数となっているケタが<var>i</var>ケタ目だった場合、<var>i</var>ケタ目の一致判定を行うとき、その偶数の前後にある奇数が入力されていた場合でも<var>i</var>ケタ目は一致していると誤判定されてしまうのだ。 </p> <p> 例えば、パスワードが"57677"の南京錠ならば、'6'の前後には'5'と'7'の奇数があるため"57677"のほかに、"57577"や"57777"を入力したときでも開いてしまう。 同様に、パスワードが"130bd"の南京錠ならば、'0'の前後には'1'と'f'の奇数があるため"130bd"のほかに、"13fbd"や"131bd"を入力したときでも開いてしまう。 </p> <p> 今、パスワードの候補が<var>N</var>個あり、それらのうちどれか1つが正解のパスワードであることが分かっている。しかたがないので持ち主は、鍵が開くまで何度も整数を入力して試し続けることにした。持ち主はできるだけ決定ボタンを押す回数を少なくしたい。 </p> <p> 持ち主が最適な戦略をとったとき、最大で何回決定ボタンを押さなければならないだろうか? その回数を出力せよ。 また、実際に最大となるときに入力する整数の集合を昇順に出力せよ。 答えが一意に定まらない場合は、辞書順で最小となるものを出力せよ。 </p> <h2>Input</h2> <p>入力は以下の形式で与えられる。</p> <pre><var>N</var> <var>password<sub>1</sub></var> <var>password<sub>2</sub></var> ... <var>password<sub>N</sub></var></pre> <p> 1行目に<var>N</var>が与えられる。<br> 2行目から<var>N</var>+1行目の<var>i</var>行目には、パスワードの候補を表す5ケタの16進数を表す文字列<var>password<sub>i</sub></var>が与えられる。 </p> <h2>Constraints</h2> <ul> <li>1 &le; <var>N</var> &le; 6000</li> <li><var>password<sub>i</sub></var> &ne; <var>password<sub>j</sub></var> ( <var>i</var> &lt; <var>j</var> )</li> <li>文字列は、0から9の数字または英小文字aからfの5文字で構成され、そのうち1文字は偶数であり、他の4文字は奇数である</li> </ul> <h2>Output</h2> <p> 鍵が開くまでに最適な戦略をとったときの決定ボタンを押す回数の最大値を1行に出力せよ。 続いて、最大となるときに入力する整数の集合を昇順に出力せよ。 もし、答えが一意に定まらない場合は、辞書順で最小となるものを出力せよ。 </p> <h2>Sample Input 1</h2> <pre> 1 0111f </pre> <h2>Sample Output 1</h2> <pre> 1 0111f </pre> <h2>Sample Input 2</h2> <pre> 3 57567 57587 0d351 </pre> <h2>Sample Output 2</h2> <pre> 2 0d351 57577 </pre> <h2>Sample Input 3</h2> <pre> 6 1811b 1a11b f3b1e f3b1c b0df3 bedf3 </pre> <h2>Sample Output 3</h2> <pre> 3 1911b bfdf3 f3b1d </pre>
p03543
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We call a <var>4</var>-digit integer with three or more consecutive same digits, such as <var>1118</var>, <strong>good</strong>.</p> <p>You are given a <var>4</var>-digit integer <var>N</var>. Answer the question: Is <var>N</var> <strong>good</strong>?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1000 ≤ N ≤ 9999</var></li> <li><var>N</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If <var>N</var> is <strong>good</strong>, print <code>Yes</code>; otherwise, print <code>No</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>1118 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes </pre> <p><var>N</var> is <strong>good</strong>, since it contains three consecutive <var>1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7777 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>Yes </pre> <p>An integer is also <strong>good</strong> when all the digits are the same.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1234 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>No </pre></section> </div> </span>
p03113
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke participated in a magic show.</p> <p>A magician prepared <var>N</var> identical-looking boxes. He put a treasure in one of the boxes, closed the boxes, shuffled them, and numbered them <var>1</var> through <var>N</var>.</p> <p>Since the boxes are shuffled, now Snuke has no idea which box contains the treasure. Snuke wins the game if he opens a box containing the treasure. You may think that if Snuke opens all boxes at once, he can always win the game. However, there are some tricks:</p> <ul> <li>Snuke must open the boxes one by one. After he opens a box and checks the content of the box, he must close the box before opening the next box.</li> <li>He is only allowed to open Box <var>i</var> at most <var>a_i</var> times.</li> <li>The magician may secretly move the treasure from a closed box to another closed box, using some magic trick. For simplicity, assume that the magician never moves the treasure while Snuke is opening some box. However, he can move it at any other time (before Snuke opens the first box, or between he closes some box and opens the next box).</li> <li>The magician can perform the magic trick at most <var>K</var> times.</li> </ul> <p>Can Snuke always win the game, regardless of the initial position of the treasure and the movements of the magician?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 50</var></li> <li><var>1 \leq K \leq 50</var></li> <li><var>1 \leq a_i \leq 100</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>a_1</var> <var>a_2</var> <var>\cdots</var> <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If the answer is no, print a single <code>-1</code>.</p> <p>Otherwise, print one possible move of Snuke in the following format:</p> <pre><var>Q</var> <var>x_1</var> <var>x_2</var> <var>\cdots</var> <var>x_Q</var> </pre> <p>It means that he opens boxes <var>x_1, x_2, \cdots, x_Q</var> in this order.</p> <p>In case there are multiple possible solutions, you can output any.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 1 5 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>7 1 1 2 1 2 2 1 </pre> <p>If Snuke opens the boxes <var>7</var> times in the order <var>1 \rightarrow 1 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 2 \rightarrow 1</var>, he can always find the treasure regardless of its initial position and the movements of the magician.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 50 5 10 15 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>-1 </pre></section> </div> </span>
p03406
<span class="lang-en"> <p>Score : <var>1100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>2^N</var> players, numbered <var>1, 2, ..., 2^N</var>. They decided to hold a tournament.</p> <p>The tournament proceeds as follows:</p> <ul> <li>Choose a permutation of <var>1, 2, ..., 2^N</var>: <var>p_1, p_2, ..., p_{2^N}</var>.</li> <li>The players stand in a row in the order of Player <var>p_1</var>, Player <var>p_2</var>, <var>...</var>, Player <var>p_{2^N}</var>.</li> <li>Repeat the following until there is only one player remaining in the row:<ul> <li>Play the following matches: the first player in the row versus the second player in the row, the third player versus the fourth player, and so on. The players who lose leave the row. The players who win stand in a row again, preserving the relative order of the players.</li> </ul> </li> <li>The last player who remains in the row is the champion.</li> </ul> <p>It is known that, the result of the match between two players can be written as follows, using <var>M</var> integers <var>A_1, A_2, ..., A_M</var> given as input:</p> <ul> <li>When <var>y = A_i</var> for some <var>i</var>, the winner of the match between Player <var>1</var> and Player <var>y</var> (<var>2 \leq y \leq 2^N</var>) will be Player <var>y</var>.</li> <li>When <var>y \neq A_i</var> for every <var>i</var>, the winner of the match between Player <var>1</var> and Player <var>y</var> (<var>2 \leq y \leq 2^N</var>) will be Player <var>1</var>.</li> <li>When <var>2 \leq x &lt; y \leq 2^N</var>, the winner of the match between Player <var>x</var> and Player <var>y</var> will be Player <var>x</var>.</li> </ul> <p>The champion of this tournament depends only on the permutation <var>p_1, p_2, ..., p_{2^N}</var> chosen at the beginning. Find the number of permutation <var>p_1, p_2, ..., p_{2^N}</var> chosen at the beginning of the tournament that would result in Player <var>1</var> becoming the champion, modulo <var>10^9 + 7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 16</var></li> <li><var>0 \leq M \leq 16</var></li> <li><var>2 \leq A_i \leq 2^N</var> (<var>1 \leq i \leq M</var>)</li> <li><var>A_i &lt; A_{i + 1}</var> (<var>1 \leq i &lt; M</var>)</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_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>2 1 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>8 </pre> <p>Examples of <var>p</var> that satisfy the condition are: <var>[1, 4, 2, 3]</var> and <var>[3, 2, 1, 4]</var>. Examples of <var>p</var> that do not satisfy the condition are: <var>[1, 2, 3, 4]</var> and <var>[1, 3, 2, 4]</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 3 2 4 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>40320 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>3 3 3 4 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>2688 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>16 16 5489 5490 5491 5492 5493 5494 5495 5497 18993 18995 18997 18999 19000 19001 19002 19003 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>816646464 </pre></section> </div> </span>
p03056
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p><strong>Note the unusual memory limit.</strong></p> <p>For a rectangular grid where each square is painted white or black, we define its <strong>complexity</strong> as follows:</p> <ul> <li>If all the squares are black or all the squares are white, the complexity is <var>0</var>.</li> <li>Otherwise, divide the grid into two subgrids by a line parallel to one of the sides of the grid, and let <var>c_1</var> and <var>c_2</var> be the complexities of the subgrids. There can be multiple ways to perform the division, and let <var>m</var> be the minimum value of <var>\max(c_1, c_2)</var> in those divisions. The complexity of the grid is <var>m+1</var>.</li> </ul> <p>You are given a grid with <var>H</var> horizontal rows and <var>W</var> vertical columns where each square is painted white or black. <var>HW</var> characters from <var>A_{11}</var> to <var>A_{HW}</var> represent the colors of the squares. <var>A_{ij}</var> is <code>#</code> if the square at the <var>i</var>-th row from the top and the <var>j</var>-th column from the left is black, and <var>A_{ij}</var> is <code>.</code> if that square is white.</p> <p>Find the complexity of the given grid.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq H,W \leq 185</var></li> <li><var>A_{ij}</var> is <code>#</code> or <code>.</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>H</var> <var>W</var> <var>A_{11}</var><var>A_{12}</var><var>...</var><var>A_{1W}</var> <var>:</var> <var>A_{H1}</var><var>A_{H2}</var><var>...</var><var>A_{HW}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the complexity of the given grid.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 ... .## .## </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>Let us divide the grid by the boundary line between the first and second columns. The subgrid consisting of the first column has the complexity of <var>0</var>, and the subgrid consisting of the second and third columns has the complexity of <var>1</var>, so the whole grid has the complexity of at most <var>2</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 7 .####.# #....#. #....#. #....#. .####.# #....## </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>4 </pre></section> </div> </span>
p01591
<h2>Problem A: Approximate Circle</h2> <p>Consider a set of <var>n</var> points (<var>x<sub>1</sub></var>, <var>y<sub>1</sub></var>), ..., (<var>x<sub>n</sub></var>,<var>y<sub>n</sub></var>) on a Cartesian space. Your task is to write a program for regression to a circle <var>x<sup>2</sup></var> + <var>y<sup>2</sup></var> + <var>a</var><var>x</var> + <var>b</var><var>y</var> + <var>c</var> = 0. In other words, your program should find a circle that minimizes the error. Here the error is measured by the sum over square distances between each point and the circle, which is given by:</p> <div align="center"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_approximateCircle" /></div> <h2>Input</h2> <p>The input begins with a line containing one integer <var>n</var> (3 <= <var>n</var> <= 40,000). Then <var>n</var> lines follow. The <var>i</var>-th line contains two integers <var>x<sub>i</sub></var> and <var>y<sub>i</sub></var> (0 <= <var>x<sub>i</sub></var>, <var>y<sub>i</sub></var> <= 1,000), which denote the coordinates of the <var>i</var>-th point.</p> <p>You can assume there are no cases in which all the points lie on a straight line.</p> <h2>Output</h2> <!-- <p>Print three integers <var>a</var>, <var>b</var> and <var>c</var>, separated by space, to show the regressed function. The output values should be printed with three digits after the decimal point, and should not contain an error greater than 0.001.</p> --> <p>Print three integers <var>a</var>, <var>b</var> and <var>c</var>, separated by space, to show the regressed function. The output values should be in a decimal fraction and should not contain an error greater than 0.001.</p> <h2>Sample Input 1</h2> <pre> 4 0 0 100 0 100 100 0 100 </pre> <h2>Output for the Sample Input 1</h2> <pre> -100.000 -100.000 0.000 </pre> <h2>Sample Input 2</h2> <pre> 3 0 0 10 0 5 100 </pre> <h2>Output for the Sample Input 2</h2> <pre> -10.000 -99.750 0.000 </pre>
p02217
<span class="lang"> <span class="lang-ja"> <h1>F: ボタンの木</h1> <div class="part"> <section> <h3>問題文</h3><p>$N$ 頂点 $N-1$ 辺からなる木があり、$i$ 番目の辺は $u_i$ 番目の頂点と $v_i$ 番目の頂点を接続しています。</p> <p>各頂点にはボタンが咲いており、$i$ 番目の頂点に咲いているボタンをボタン $i$ と呼ぶことにします。</p> <p>はじめ、ボタン $i$ の美しさは $a_i$ です。</p> <p>ボタン $i$ を押すたびに、ボタン $i$ の美しさを隣接するボタンに $1$ ずつ分け与えます。</p> <p>すなわち、$i$ 番目の頂点に $c_1, ..., c_k$ 番目の頂点が隣接している場合、ボタン $i$ の美しさが $k$ 減少し、ボタン $c_1, ..., c_k$ の美しさがそれぞれ $1$ ずつ増加します。</p> <p>このとき、負の美しさのボタンを押しても良いし、ボタンを押した結果あるボタンの美しさが負になっても良いです。</p> <p>あなたの目的はボタン $1, 2, ..., N$ の美しさをそれぞれ $b_1, ..., b_N$ にすることです。</p> <p>最小で合計何回ボタンを押せば目的を達成できるでしょうか。</p> </section> </div> <div class="part"> <section> <h3>制約</h3><ul> <li>入力はすべて整数である</li> <li>$1 \leq N \leq 10^5$</li> <li>$1 \leq u_i, v_i \leq N$</li> <li>$-1000 \leq a_i, b_i \leq 1000$</li> <li>$\sum_i a_i = \sum_i b_i$</li> <li>与えられるグラフは木である</li> <li>与えられる入力において目的は必ず達成できる</li> </ul> </section> </div> <hr /> <div class="io-style"> <div class="part"> <section> <h3>入力</h3><p>入力は以下の形式で標準入力から与えられる。</p> <pre>$N$ $u_1$ $v_1$ $\vdots$ $u_{N-1}$ $v_{N-1}$ $a_1$ $a_2$ $...$ $a_N$ $b_1$ $b_2$ $...$ $b_N$ </pre> </section> </div> <div class="part"> <section> <h3>出力</h3><p>目的を達成するためにボタンを押す合計回数の最小値を出力せよ。</p> </section> </div> </div> <hr /> <div class="part"> <section> <h3>入力例1</h3><pre>4 1 2 1 3 3 4 0 3 2 0 -3 4 5 -1 </pre> </section> </div> <div class="part"> <section> <h3>出力例1</h3><pre>4 </pre> <p>次のように合計 $4$ 回ボタンを押すことで目的を達成でき、このときが最小です。</p> <ul> <li>ボタン $1$ を $2$ 回押します。ボタン $1, 2, 3, 4$ の美しさがそれぞれ $-4, 5, 4, 0$ に変化します。</li> <li>ボタン $2$ を $1$ 回押します。美しさがそれぞれ $-3, 4, 4, 0$ に変化します。</li> <li>ボタン $4$ を $1$ 回押します。美しさがそれぞれ $-3, 4, 5, -1$ に変化します。</li> </ul> </section> </div> <hr /> <div class="part"> <section> <h3>入力例2</h3><pre>5 1 2 1 3 3 4 3 5 -9 5 7 1 8 -7 4 5 1 9 </pre> </section> </div> <div class="part"> <section> <h3>出力例2</h3><pre>3 </pre></section> </div> </span> </span>
p02647
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have <var>N</var> bulbs arranged on a number line, numbered <var>1</var> to <var>N</var> from left to right. Bulb <var>i</var> is at coordinate <var>i</var>.</p> <p>Each bulb has a non-negative integer parameter called intensity. When there is a bulb of intensity <var>d</var> at coordinate <var>x</var>, the bulb illuminates the segment from coordinate <var>x-d-0.5</var> to <var>x+d+0.5</var>. Initially, the intensity of Bulb <var>i</var> is <var>A_i</var>. We will now do the following operation <var>K</var> times in a row:</p> <ul> <li>For each integer <var>i</var> between <var>1</var> and <var>N</var> (inclusive), let <var>B_i</var> be the number of bulbs illuminating coordinate <var>i</var>. Then, change the intensity of each bulb <var>i</var> to <var>B_i</var>.</li> </ul> <p>Find the intensity of each bulb after the <var>K</var> operations.</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>1 \leq K \leq 2 \times 10^5</var></li> <li><var>0 \leq A_i \leq 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>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>Print the intensity <var>A{'}_i</var> of each bulb <var>i</var> after the <var>K</var> operations to Standard Output in the following format:</p> <pre><var>A{'}_1</var> <var>A{'}_2</var> <var>\ldots</var> <var>A{'}_N</var> </pre> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 1 1 0 0 1 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 2 1 2 </pre> <p>Initially, only Bulb <var>1</var> illuminates coordinate <var>1</var>, so the intensity of Bulb <var>1</var> becomes <var>1</var> after the operation. Similarly, the bulbs initially illuminating coordinate <var>2</var> are Bulb <var>1</var> and <var>2</var>, so the intensity of Bulb <var>2</var> becomes <var>2</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 2 1 0 0 1 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 3 4 4 3 </pre></section> </div> </span>
p01968
<h2>B: 階層的計算機 (Hierarchical Calculator)</h2> <h3>Problem</h3> <p>Ebi-chan has <var>N</var> formulae: <var>y = a_i x</var> for <var>i =1, ..., N</var> (inclusive). Now she considers a subsequence of indices with length <var>k</var>: <var>s_1</var>, <var>s_2</var>, <var>...</var>, <var>s_k</var>. At first, let <var>x_0</var> be <var>1</var> and evaluate <var>s_1</var>-th formulae with <var>x = x_0</var>. Next, let <var>x_1</var> be the output of <var>s_1</var> and evaluate <var>s_2</var>-th formulae with <var>x = x_1</var>, and so on.</p> <p>She wants to maximize the final output of the procedure, <var>x_{s_k}</var>. If there are many candidates, she wants the """shortest one""". If there are still many candidates, she wants the """lexicographically smallest one""".</p> <p>Sequence <var>s</var> is lexicographically smaller than sequence <var>t</var>, if and only if either of the following conditions hold:</p> <ul> <li> there exists <var>m &lt; |s|</var> such that <var>s_i = t_i</var> for <var>i</var> in <var>1</var> to <var>m</var> (inclusive) and <var>s_{m+1} &lt; t_{m+1}</var>, or</li> <li> <var>s_i = t_i</var> for <var>i</var> in <var>1</var> to <var>|s|</var> (inclusive) and <var>|s| &lt; |t|</var>,</li> </ul> <p>where <var>|s|</var> is the length of the <var>s</var>.</p> <h3>Input</h3> <pre> <var>N</var> <var>a_1</var> <var>a_2</var> $\cdots$ <var>a_N</var> </pre> <h3>Constraints</h3> <ul> <li> <var>1 \leq N \leq 60</li> <li> <var>-2 \leq a_i \leq 2</var> for <var>i=1, ...,N</var> (inclusive)</li> <li> Every input is given as the integer.</li> </ul> <h3>Output</h3> <p>Output <var>k+1</var> lines. First line, the length of the sequence, <var>k</var>. Following <var>k</var> lines, the index of the <var>i</var>-th element of the subsequence, <var>s_i</var> (one element per line).</p> <h3>Sample Input 1</h3> <pre> 4 2 0 -2 1 </pre> <h3>Sample Output for Input 1</h3> <pre> 1 1 </pre> <p>She evaluates the first one and gets the maximum value <var>2</var>.</p> <h3>Sample Input 2</h3> <pre> 3 2 -2 -2 </pre> <h3>Sample Output for Input 2</h3> <pre> 3 1 2 3 </pre> <p>She evaluates all of them and gets the maximum value <var>8</var>.</p> <h3>Sample Input 3</h3> <pre> 2 -1 0 </pre> <h3>Sample Output for Input 3</h3> <pre>0</pre> <p>She evaluates none of them and gets the maximum value <var>0</var>. Empty sequence is the shorter and lexicographically smaller than any other sequences.</p> <h3>Sample Input 4</h3> <pre> 5 -1 2 1 -2 -1 </pre> <h3>Sample Output for Input 4</h3> <pre> 3 1 2 4 </pre> <p>She evaluates $\langle$<var> 1, 2, 4 </var>$\rangle$ ones and gets the maximum value <var>4</var>. Note that $\langle$<var> 2, 4, 5 </var>$\rangle$ is not lexicographically smallest one.</p>
p03955
<span class="lang-en"> <p>Score : <var>1500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a grid with <var>3</var> rows and <var>N</var> columns. The cell at the <var>i</var>-th row and <var>j</var>-th column is denoted (<var>i</var>, <var>j</var>). Initially, each cell (<var>i</var>, <var>j</var>) contains the integer <var>i+3j-3</var>.</p> <div style="text-align: center;"> <img src="https://atcoder.jp/img/agc006/0322a46da4f0298345d1574383f0d83e.png"> <p>A grid with <var>N=5</var> columns</p> </img></div> <p>Snuke can perform the following operation any number of times:</p> <ul> <li>Choose a <var>3×3</var> subrectangle of the grid. The placement of integers within the subrectangle is now rotated by <var>180°</var>.</li> </ul> <div style="text-align: center;"> <img src="https://atcoder.jp/img/agc006/a18e112da218b4ecd3c2b3fdfcf15d94.png"> <p>An example sequence of operations (each chosen subrectangle is colored blue)</p> </img></div> <p>Snuke's objective is to manipulate the grid so that each cell (<var>i</var>, <var>j</var>) contains the integer <var>a_{i,j}</var>. Determine whether it is achievable.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>5≤N≤10^5</var></li> <li><var>1≤a_{i,j}≤3N</var></li> <li>All <var>a_{i,j}</var> are distinct.</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,1}</var> <var>a_{1,2}</var> <var>...</var> <var>a_{1,N}</var> <var>a_{2,1}</var> <var>a_{2,2}</var> <var>...</var> <var>a_{2,N}</var> <var>a_{3,1}</var> <var>a_{3,2}</var> <var>...</var> <var>a_{3,N}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If Snuke's objective is achievable, print <code>Yes</code>. Otherwise, print <code>No</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 9 6 15 12 1 8 5 14 11 2 7 4 13 10 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes </pre> <p>This case corresponds to the figure in the problem statement.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 </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>5 1 4 7 10 13 2 5 8 11 14 3 6 9 12 15 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Yes </pre> <p>The objective is already achieved with the initial placement of the integers.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>6 15 10 3 4 9 16 14 11 2 5 8 17 13 12 1 6 7 18 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>Yes </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>7 21 12 1 16 13 6 7 20 11 2 17 14 5 8 19 10 3 18 15 4 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>No </pre></section> </div> </span>
p00380
<h1>Bozosort</h1>   <p> Bozosort, as well as Bogosort, is a very inefficient sort algorithm. It is a random number-based algorithm that sorts sequence elements following the steps as below: </p> <ol> <li> Randomly select two elements and swap them.</li> <li> Verify if all the elements are sorted in increasing order.</li> <li> Finish if sorted, else return to 1.</li> </ol> <p> To analyze Bozosort, you have decided to simulate the process using several predetermined pairs of elements. </p> <p> You are given several commands to swap two elements. Make a program to evaluate how many times you have to run the command before the sequence is aligned in increasing order. </p> <h2>Input</h2> <p> The input is given in the following format. </p> <pre> $N$ $a_1$ $a_2$ ... $a_N$ $Q$ $x_1$ $y_1$ $x_2$ $y_2$ $...$ $x_Q$ $y_Q$ </pre> <p> The first line provides the number of sequence elements $N$ ($2 \leq N \leq 300,000$). The second line provides an array of integers $a_i$ ($1 \leq a_i \leq 10^9$) that constitutes the sequence. Each of the subsequent $Q$ lines provides a pair of integers $x_i,y_i$ ($1 \leq x_i,y_i \leq N$) that represent the $i$-th command, which swaps the two elements indicated by $x_i$ and $y_i$ ($x_i \ne y_i$). </p> <h2>Output</h2> <p> If neat alignment in increasing order is realized the first time after execution of multiple commands, output at what time it was. Output <span>0</span> if the initial sequence is aligned in increasing order, and <span>-1</span> if exhaustive execution still failed to attain the goal. </p> <h2>Sample Input 1</h2> <pre> 6 9 7 5 6 3 1 3 1 6 2 5 3 4 </pre> <h2>Sample Output 1</h2> <pre> 2 </pre> <h2>Sample Input 2</h2> <pre> 4 4 3 2 1 2 1 2 3 4 </pre> <h2>Sample Output 2</h2> <pre> -1 </pre> <h2>Sample Input 3</h2> <pre> 5 1 1 1 2 2 1 1 2 </pre> <h2>Sample Input 3</h2> <pre> 0 </pre>
p01342
<H1><font color="#000">Problem D: </font>Dungeon Wall</H1> <p> In 2337, people are bored at daily life and have crazy desire of having extraordinary experience. These days,“Dungeon Adventure” is one of the hottest attractions, where brave adventurers risk their lives to kill the evil monsters and save the world. </p> <p> You are a manager of one of such dungeons. Recently you have been receiving a lot of complaints from soldiers who frequently enter your dungeon; they say your dungeon is too easy and they do not want to enter again. You are considering making your dungeon more difficult by increasing the distance between the entrance and the exit of your dungeon so that more monsters can approach the adventurers. </p> <p> The shape of your dungeon is a rectangle whose width is <i>W</i> and whose height is <i>H</i>. The dungeon is a grid which consists of <i>W</i> &times; <i>H</i> square rooms of identical size 1 &times; 1. The southwest corner of the dungeon has the coordinate (0; 0), and the northeast corner has (<i>W</i>, <i>H</i>). The outside of the dungeon is surrounded by walls, and there may be some more walls inside the dungeon in order to prevent adventurers from going directly to the exit. Each wall in the dungeon is parallel to either <i>x</i>-axis or <i>y</i>-axis, and both ends of each wall are at integer coordinates. An adventurer can move from one room to another room if they are adjacent vertically or horizontally and have no wall between. </p> <p> You would like to add some walls to make the shortest path between the entrance and the exit longer. However, severe financial situation only allows you to build at most only one wall of a unit length. Note that a new wall must also follow the constraints stated above. Furthermore, you need to guarantee that there is at least one path from the entrance to the exit. </p> <p> You are wondering where you should put a new wall to maximize the minimum number of steps between the entrance and the exit. Can you figure it out? </p> <H2>Input</H2> <p> <i>W H N</i><br> <i>sx</i><sub>0</sub> <i>sy</i><sub>0</sub> <i>dx</i><sub>0</sub> <i>dy</i><sub>0</sub><br> <i>sx</i><sub>1</sub> <i>sy</i><sub>1</sub> <i>dx</i><sub>1</sub> <i>dy</i><sub>1</sub><br> .<br> .<br> .<br> <i>ix iy</i><br> <i>ox oy</i><br> <!--0 0 0<br>--> </p> <p> The first line contains three integers: <i>W</i>, <i>H</i> and <i>N</i> (1 &le; <i>W</i>, <i>H</i> &le; 50, 0 &le; <i>N</i> &le; 1000). They are separated with a space. </p> <p> The next <i>N</i> lines contain the specifications of <i>N</i> walls inside the dungeon. Each wall specification is a line containing four integers. The <i>i</i>-th specification contains <i>sx<sub>i</sub></i>, <i>sy<sub>i</sub></i>, <i>dx<sub>i</sub></i> and <i>dy<sub>i</sub></i>, separated with a space. These integers give the position of the <i>i</i>-th wall: it spans from (<i>sx<sub>i</sub></i>, <i>sy<sub>i</sub></i>) to (<i>dx<sub>i</sub></i>, <i>dy<sub>i</sub></i>). </p> <p> The last two lines contain information about the locations of the entrance and the exit. The first line contains two integers <i>ix</i> and <i>iy</i>, and the second line contains two integers <i>ox</i> and <i>oy</i>. The entrance is located at the room whose south-west corner is (<i>ix</i>, <i>iy</i>), and the exit is located at the room whose southwest corner is (<i>ox</i>, <i>oy</i>). </p> <H2>Output</H2> <p> Print the maximum possible increase in the minimum number of required steps between the entrance and the exit just by adding one wall. If there is no good place to build a new wall, print zero. </p> <H2>Sample Input 1</H2> <pre> 3 4 4 1 0 1 1 1 3 1 4 1 2 2 2 2 1 2 3 0 0 1 0 </pre> <H2>Output for the Sample Input 1</H2> <pre> 6 </pre> <H2>Sample Input 2</H2> <pre> 50 2 0 0 0 49 0 </pre> <H2>Output for the Sample Input 2</H2> <pre> 2 </pre> <H2>Sample Input 3</H2> <pre> 50 2 0 0 0 49 1 </pre> <H2>Output for the Sample Input 3</H2> <pre> 0 </pre>
p02997
<span class="lang-en"> <p>Score: <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>Does there exist an undirected graph with <var>N</var> vertices satisfying the following conditions?</p> <ul> <li>The graph is simple and connected.</li> <li>The vertices are numbered <var>1, 2, ..., N</var>.</li> <li>Let <var>M</var> be the number of edges in the graph. The edges are numbered <var>1, 2, ..., M</var>, the length of each edge is <var>1</var>, and Edge <var>i</var> connects Vertex <var>u_i</var> and Vertex <var>v_i</var>.</li> <li>There are exactly <var>K</var> pairs of vertices <var>(i,\ j)\ (i &lt; j)</var> such that the shortest distance between them is <var>2</var>.</li> </ul> <p>If there exists such a graph, construct an example.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All values in input are integers.</li> <li><var>2 \leq N \leq 100</var></li> <li><var>0 \leq K \leq \frac{N(N - 1)}{2}</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> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there does not exist an undirected graph with <var>N</var> vertices satisfying the conditions, print <code>-1</code>.</p> <p>If there exists such a graph, print an example in the following format (refer to Problem Statement for what the symbols stand for):</p> <pre><var>M</var> <var>u_1</var> <var>v_1</var> <var>:</var> <var>u_M</var> <var>v_M</var> </pre> <p>If there exist multiple graphs satisfying the conditions, any of them will be accepted.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>5 4 3 1 2 3 1 4 5 2 3 </pre> <p>This graph has three pairs of vertices such that the shortest distance between them is <var>2</var>: <var>(1,\ 4)</var>, <var>(2,\ 4)</var>, and <var>(3,\ 5)</var>. Thus, the condition is satisfied.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>-1 </pre> <p>There is no graph satisfying the conditions.</p></section> </div> </span>
p03685
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is playing a puzzle game. In this game, you are given a rectangular board of dimensions <var>R × C</var>, filled with numbers. Each integer <var>i</var> from <var>1</var> through <var>N</var> is written twice, at the coordinates <var>(x_{i,1},y_{i,1})</var> and <var>(x_{i,2},y_{i,2})</var>.</p> <p>The objective is to draw a curve connecting the pair of points where the same integer is written, for every integer from <var>1</var> through <var>N</var>. Here, the curves may not go outside the board or cross each other.</p> <p>Determine whether this is possible.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ R,C ≤ 10^8</var></li> <li><var>1 ≤ N ≤ 10^5</var></li> <li><var>0 ≤ x_{i,1},x_{i,2} ≤ R(1 ≤ i ≤ N)</var></li> <li><var>0 ≤ y_{i,1},y_{i,2} ≤ C(1 ≤ i ≤ N)</var></li> <li>All given points are distinct.</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>R</var> <var>C</var> <var>N</var> <var>x_{1,1}</var> <var>y_{1,1}</var> <var>x_{1,2}</var> <var>y_{1,2}</var> : <var>x_{N,1}</var> <var>y_{N,1}</var> <var>x_{N,2}</var> <var>y_{N,2}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <code>YES</code> if the objective is achievable; print <code>NO</code> otherwise.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 2 3 0 1 3 1 1 1 4 1 2 0 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>YES </pre> <p><img alt="" src="https://atcoder.jp/img/arc076/hogehogehoge.png"/></p> <p>The above figure shows a possible solution.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 2 4 0 0 2 2 2 0 0 1 0 2 1 2 1 1 2 1 </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>5 5 7 0 0 2 4 2 3 4 5 3 5 5 2 5 5 5 4 0 3 5 1 2 2 4 4 0 5 4 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>YES </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>1 1 2 0 0 1 1 1 0 0 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>NO </pre></section> </div> </span>
p00800
<H1><font color="#000">Problem E:</font> The Devil of Gravity</H1> <p> Haven't you ever thought that programs written in Java, C++, Pascal, or any other modern computer languages look rather sparse? Although most editors provide sufficient screen space for at least 80 characters or so in a line, the average number of significant characters occurring in a line is just a fraction. Today, people usually prefer readability of programs to efficient use of screen real estate. </p> <p> Dr. Faust, a radical computer scientist, believes that editors for real programmers shall be more space efficient. He has been doing research on saving space and invented various techniques for many years, but he has reached the point where no more essential improvements will be expected with his own ideas. </p> <p> After long thought, he has finally decided to take the ultimate but forbidden approach. He does not hesitate to sacrifice anything for his ambition, and asks a devil to give him supernatural intellectual powers in exchange with his soul. With the transcendental knowledge and ability, the devil provides new algorithms and data structures for space efficient implementations of editors. </p> <p> The editor implemented with those evil techniques is beyond human imaginations and behaves somehow strange. The mighty devil Dr. Faust asks happens to be the devil of gravity. The editor under its control saves space with magical magnetic and gravitational forces. </p> <p> Your mission is to defeat Dr. Faust by re-implementing this strange editor without any help of the devil. At first glance, the editor looks like an ordinary text editor. It presents texts in two-dimensional layouts and accepts editing commands including those of cursor movements and character insertions and deletions. A text handled by the devil's editor, however, is partitioned into text segments, each of which is a horizontal block of non-blank characters. In the following figure, for instance, four text segments "<span>abcdef</span>", "<span>ghijkl</span>", "<span>mnop</span>", "<span>qrstuvw</span>" are present and the first two are placed in the same row. </p> <pre> abcdef ghijkl mnop qrstuvw </pre> <p> The editor has the following unique features. </p> <ol> <li> A text segment without any supporting segments in the row immediately below it falls by the evil gravitational force.</li> <li> Text segments in the same row and contiguous to each other are concatenated by the evil magnetic force.</li> </ol> <p> For instance, if characters in the segment "<span>mnop</span>" in the previous example are deleted, the two segments on top of it fall and we have the following. </p> <pre> abcdef qrstuvw ghijkl </pre> <p> After that, if "<span>x</span>" is added at the tail (i.e., the right next of the rightmost column) of the segment "<span>qrstuvw</span>", the two segments in the bottom row are concatenated. </p> <pre> abcdef qrstuvwxghijkl </pre> <p> Now we have two text segments in this figure. By this way, the editor saves screen space but demands the users' extraordinary intellectual power. </p> <p> In general, after a command execution, the following rules are applied, where <i>S</i> is a text segment, left(<i>S</i>) and right(<i>S</i>) are the leftmost and rightmost columns of <i>S</i>, respectively, and row(<i>S</i>) is the row number of <i>S</i>. </p> <ol> <li> If the columns from left(<i>S</i>) to right(<i>S</i>) in the row numbered row(<i>S</i>)-1 (i.e., the row just below <i>S</i>) are empty (i.e., any characters of any text segments do not exist there), <i>S</i> is pulled down to row(<i>S</i>)-1 vertically, preserving its column position. If the same ranges in row(<i>S</i>)-2, row(<i>S</i>)-3, and so on are also empty, <i>S</i> is further pulled down again and again. This process terminates sooner or later since the editor has the ultimate bottom, the row whose number is zero.</li> <li> If two text segments <i>S</i> and <i>T</i> are in the same row and right(<i>S</i>)+1 = left(<i>T</i>), that is, <i>T</i> starts at the right next column of the rightmost character of <i>S</i>, <i>S</i> and <i>T</i> are automatically concatenated to form a single text segment, which starts at left(<i>S</i>) and ends at right(<i>T</i>). Of course, the new segment is still in the original row.</li> </ol> <p> Note that any text segment has at least one character. Note also that the first rule is applied prior to any application of the second rule. This means that no concatenation may occur while falling segments exist. For instance, consider the following case. </p> <pre> dddddddd cccccccc bbbb aaa </pre> <p> If the last character of the text segment "<span>bbbb</span>" is deleted, the concatenation rule is not applied until the two segments "<span>cccccccc</span>" and "<span>dddddddd</span>" stop falling. This means that "<span>bbb<span>" and "<span>cccccccc</span>" are not concatenated. </p> <p> The devil's editor has a cursor and it is always in a single text segment, which we call the current segment. The cursor is at some character position of the current segment or otherwise at its tail. Note that the cursor cannot be a support. For instance, in the previous example, even if the cursor is at the last character of "<span>bbbb</span>" and it stays at the same position after the deletion, it cannot support "<span>cccccccc</span>" and "<span>dddddddd</span>" any more by solely itself and thus those two segments shall fall. Finally, the cursor is at the leftmost "<span>d</span>" </p> <p> The editor accepts the following commands, each represented by a single character. </p> <ul> <li> F: Move the cursor forward (i.e., to the right) by one column in the current segment. If the cursor is at the tail of the current segment and thus it cannot move any more within the segment, an error occurs.</li> <li> B: Move the cursor backward (i.e., to the left) by one column in the current segment. If the cursor is at the leftmost position of the current segment, an error occurs.</li> <li> P: Move the cursor upward by one row. The column position of the cursor does not change. If the new position would be out of the legal cursor range of any existing text segment, an error occurs.</li> <li> N: Move the cursor downward by one row. The column position of the cursor does not change. If the cursor is in the bottom row or the new position is out of the legal cursor range of any existing text segment, an error occurs.</li> <li> D: Delete the character at the cursor position. If the cursor is at the tail of the current segment and so no character is there, an error occurs. If the cursor is at some character, it is deleted and the current segment becomes shorter by one character. Neither the beginning (i.e., leftmost) column of the current segment nor the column position of the cursor changes. However, if the current segment becomes empty, it is removed and the cursor falls by one row. If the new position would be out of the legal cursor range of any existing text segment, an error occurs.<br> Note that after executing this command, some text segments may lose their supports and be pulled down toward the hell. If the current segment falls, the cursor also falls with it.</li> <li> C: Create a new segment of length one immediately above the current cursor position. It consists of a copy of the character under the cursor. If the cursor is at the tail, an error occurs. Also if the the position of the new segment is already occupied by another segment, an error occurs. After executing the command, the created segment becomes the new current segment and the column position of the cursor advances to the right by one column.</li> <li> Lowercase and numeric characters (`<span>a</span>' to `<span>z</span>' and `<span>0</span>' to `<span>9</span>'): Insert the character at the current cursor position. The current segment becomes longer by one character. The cursor moves forward by one column. The beginning column of the current segment does not change.</li> </ul> <p> Once an error occurs, the entire editing session terminates abnormally. </p> <H2>Input</H2> <p> The first line of the input contains an integer that represents the number of editing sessions. Each of the following lines contains a character sequence, where the first character is the initial character and the rest represents a command sequence processed during a session. Each session starts with a single segment consisting of the initial character in the bottom row. The initial cursor position is at the tail of the segment. The editor processes each command represented by a character one by one in the manner described above. </p> <p> You may assume that each command line is non-empty and its length is at most one hundred. A command sequence ends with a newline. </p> <H2>Output</H2> <p> For each editing session specified by the input, if it terminates without errors, your program should print the current segment at the completion of the session in a line. If an error occurs during the session, just print "<span>ERROR</span>" in capital letters in a line. </p> <H2>Sample Input</H2> <pre> 3 12BC3BC4BNBBDD5 aaaBCNBBBCb aaaBBCbNBC </pre> <H2>Output for the Sample Input</H2> <pre> 15234 aba ERROR </pre>
p01712
<h1>問題 A : Koto Distance</h1> <h2>問題文</h2> <p> Koto は言わずと知れた碁盤の目の街である. この街は東西方向に <var>W</var> メートル,南北方向に <var>H</var> メートルに伸びる長方形の領域によってできている. この街の西端から <var>x</var> メートル,南端から <var>y</var> メートルの点は <var>(x,&thinsp; y) </var> と記される. ここの街に住む人は古くから伝わる独自の文化を重んじており,その一つにKoto距離という変わった距離尺度がある. 2つの点 <var>(x_1, y_1)</var>,<var>(x_2, y_2)</var> の Koto 距離は,min<var>(|x_1 - x_2|,&thinsp; |y_1 - y_2|)</var> によって定義される. </p> <p> 最近この街全体に Wifi を使えるようにする計画が立ち上がった. 現在の計画では,親機を <var>N</var> 個作ることになっている. <var>i</var> 番目の親機は点 <var>(x_i, &thinsp;y_i)</var> に設置され,Koto距離が <var>w_i</var> 以下の領域に対して Wifi を提供する. </p> <p> 親機を計画どおり建てた場合に,街の内部及び境界上に Wifi を提供できるかどうかを判定せよ. </p> <p> <font color=gray>なお,Koto距離は一般に三角不等式を満たさないため,距離の公理を満たさないということはここだけの秘密である.</font> </p> <h2>入力形式</h2> <p> 入力は以下の形式で与えられる. <pre> <var>N</var> <var>W</var> <var>H</var> <var>x_1</var> <var>y_1</var> <var>w_1</var> <var>...</var> <var>x_N</var> <var>y_N</var> <var>w_N</var> </pre> <h2>出力形式</h2> <p> 街の内部および境界上に Wifi を提供できるなら “Yes” を,そうでない場合は “No” を出力せよ. </p> <h2>制約</h2> <ul> <li><var>1 &le; N &le; 10^5</var></li> <li><var>1 &le; W &le; 10^5</var></li> <li><var>1 &le; H &le; 10^5</var></li> <li><var>0 &le; x_i &le; W</var></li> <li><var>0 &le; y_i &le; H</var></li> <li><var>1 &le; w_i &le; 10^5</var></li> <li>同一座標に親機は複数存在しない</li> </ul> <h2>入出力例</h2> <h3>入力例 1</h3> <pre> 3 9 9 2 2 2 5 5 2 8 8 2 </pre> <h3>出力例 1</h3> <pre> Yes </pre> <p>2番目の親機が提供するWifiの範囲は以下の通りである.</p> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_JAGSummer2013_Koto-Sample0"></img> <h3>入力例 2</h3> <pre> 2 7 7 2 2 1 6 6 1 </pre> <h3>出力例 2</h3> <pre> No </pre> <h3>入力例 3</h3> <pre> 3 10 20 5 10 5 2 10 1 8 10 1 </pre> <h3>出力例 3</h3> <pre> Yes </pre>
p00553
<h1>電子レンジ (Microwave)</h1> <h2>問題</h2> <p> JOI 君は食事の準備のため,A ℃の肉を電子レンジで B ℃まで温めようとしている. 肉は温度が 0 ℃未満のとき凍っている. また,温度が 0 ℃より高いとき凍っていない. 温度がちょうど 0 ℃のときの肉の状態は,凍っている場合と,凍っていない場合の両方があり得る. </p> <p> JOI 君は,肉の加熱にかかる時間は以下のようになると仮定して,肉を温めるのにかかる時間を見積もることにした. </p> <ul> <li>肉が凍っていて,その温度が 0 ℃より小さいとき: C 秒で 1 ℃温まる. <li>肉が凍っていて,その温度がちょうど 0 ℃のとき: D 秒で肉が解凍され,凍っていない状態になる. <li>肉が凍っていないとき: E 秒で 1 ℃温まる. </ul> <p> この見積もりにおいて,肉を B ℃にするのに何秒かかるかを求めよ. </p> <h2>入力</h2> <p> 入力は 5 行からなり,1 行に 1 個ずつ整数が書かれている. </p> <p> 1 行目には,もともとの肉の温度 A が書かれている.<br> 2 行目には,目的の温度 B が書かれている.<br> 3 行目には,凍った肉を 1 ℃温めるのにかかる時間 C が書かれている.<br> 4 行目には,凍った肉を解凍するのにかかる時間 D が書かれている.<br> 5 行目には,凍っていない肉を 1 ℃温めるのにかかる時間 E が書かれている. </p> <p> もともとの温度 A は -100 以上 100 以下,目的の温度 B は 1 以上 100 以下であり,A &ne; 0 および A &lt; B を満たす. </p> <p> 温めるのにかかる時間 C, D, E はすべて 1 以上 100 以下である. </p> <h2>出力</h2> <p> 肉を B ℃にするのにかかる秒数を 1 行で出力せよ. </p> <h2>入出力例</h2> <h3>入力例 1</h3> <pre> -10 20 5 10 3 </pre> <h3>出力例 1</h3> <pre> 120 </pre> <br/> <h3>入力例 2</h3> <pre> 35 92 31 50 11 </pre> <h3>出力例 2</h3> <pre> 627 </pre> <br/> <p> 入出力例 1 では,もともとの肉は -10 ℃で凍っている.かかる時間は以下のようになる. </p> <ul> <li>-10 ℃から 0 ℃まで温めるのに 5 × 10 = 50 秒. <li>0 ℃の肉を解凍するのに 10 秒. <li>0 ℃から 20 ℃まで温めるのに 3 × 20 = 60 秒. </ul> <p> したがって,かかる時間の合計は 120 秒である. </p> <p> 入出力例 2 では,もともとの肉は凍っていない.したがって,肉を 35 ℃から 92 ℃まで温めるのにかかる時間は 627 秒である. </p> <br/> <div class="source"> <p class="source"> <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/"><img alt="クリエイティブ・コモンズ・ライセンス" style="border-width:0" src="https://i.creativecommons.org/l/by-sa/4.0/80x15.png"/></a> </p> <p class="source"> <a href="https://www.ioi-jp.org/joi/2016/2017-yo/index.html">情報オリンピック日本委員会作 『第 16 回日本情報オリンピック JOI 2016/2017 予選競技課題』</a> </p> </div>
p02094
<h2>G: Toss Cut Tree</h2> <h3>Problem Statement</h3> <p>You are given two trees <var>T, U</var>. Each tree has <var>N</var> vertices that are numbered <var>1</var> through <var>N</var>. The <var>i</var>-th edge (<var>1 \leq i \leq N-1</var>) of tree <var>T</var> connects vertices <var>a_i</var> and <var>b_i</var>. The <var>i</var>-th edge (<var>1 \leq i \leq N-1</var>) of tree <var>U</var> connects vertices <var>c_i</var> and <var>d_i</var>.</p> <p>The operation described below will be performed <var>N</var> times.</p> <ul> <li> On the <var>i</var>-th operation, flip a fair coin.</li> </ul> <ul> <ul> <li> If it lands heads up, remove vertex <var>i</var> and all edges directly connecting to it from tree <var>T</var>.</li> <li> If it lands tails up, remove vertex <var>i</var> and all edges directly connecting to it from tree <var>U</var>. </li> </ul> </ul> <p> After <var>N</var> operations, each tree will be divided into several connected components. Let <var>X</var> and <var>Y</var> be the number of connected components originated from tree <var>T</var> and <var>U</var>, respectively. Your task is to compute the expected value of <var>X \times Y</var>. More specifically, your program must output an integer <var>R</var> described below. First, you can prove <var>X \times Y</var> is a rational number. Let <var>P</var> and <var>Q</var> be coprime integers that satisfies <var>\frac{P}{Q} = X \times Y</var>. <var>R</var> is the only integer that satisfies <var>R \times Q</var> $\equiv$ <var>P</var> $ \bmod \;$ <var> 998244353, 0 \leq R &lt; 998244353</var>. </p> <h3>Input</h3> <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> <h3>Constraints</h3> <ul> <li> <var>1 \leq N \leq 2 \times 10^5</var></li> <li> <var>1 \leq a_i, b_i, c_i, d_i \leq N</var></li> <li> The given two graphs are trees.</li> <li> All values in the input are integers.</li> </ul> <h3>Output</h3> <p>Print <var>R</var> described above in a single line.</p> <h3>Sample Input 1</h3> <pre> 3 1 2 2 3 1 2 3 1 </pre> <h3>Output for Sample Input 1</h3> <pre>1</pre> <p>A coin is flipped <var>3</var> times. Therefore, there are <var>2^3 = 8</var> outcomes in total.</p> <p>Suppose that the outcome was (tail, head, tail) in this order. Vertex <var>2</var> of tree <var>T</var> and vertex <var>1</var>, <var>3</var> of tree <var>U</var> are removed along with all edges connecting to them. <var>T</var>, <var>U</var> are decomposed to <var>2</var>, <var>1</var> connected components, respectively. Thus, the value of <var>XY</var> is <var>2 \times 1 = 2</var> in this case.</p> <p>It is possible to calculate <var>XY</var> for the other cases in the same way and the expected value of <var>XY</var> is <var>1</var>.</p> <h3>Sample Input 2</h3> <pre> 4 1 2 1 4 2 3 1 4 4 2 2 3 </pre> <h3>Output for Sample Input 2</h3> <pre>374341634</pre> <p>The expected number of <var>XY</var> is <var>\frac{13}{8}</var>.</p>
p00103
<H1>Baseball Simulation</H1> <p> Ichiro likes baseball and has decided to write a program which simulates baseball.</p> <p> The program reads events in an inning and prints score in that inning. There are only three events as follows: </p> <p>Single hit</p> <ul> <li>put a runner on the first base.</li> <li>the runner in the first base advances to the second base and the runner in the second base advances to the third base.</li> <li>the runner in the third base advances to the home base (and go out of base) and a point is added to the score.</li> </ul> <p>Home run</p> <ul> <li>all the runners on base advance to the home base.</li> <li>points are added to the score by an amount equal to the number of the runners plus one.</li> </ul> <p>Out</p> <ul> <li>The number of outs is increased by 1.</li> <li>The runners and the score remain stationary.</li> <li>The inning ends with three-out.</li> </ul> <p> Ichiro decided to represent these events using "HIT", "HOMERUN" and "OUT", respectively. </p> <p>Write a program which reads events in an inning and prints score in that inning. You can assume that the number of events is less than or equal to 100. </p> <H2>Input</H2> <p> The input consists of several datasets. In the first line, the number of datasets <i>n</i> is given. Each dataset consists of a list of events (strings) in an inning. </p> <H2>Output</H2> <p> For each dataset, prints the score in the corresponding inning. </p> <H2>Sample Input</H2> <pre> 2 HIT OUT HOMERUN HIT HIT HOMERUN HIT OUT HIT HIT HIT HIT OUT HIT HIT OUT HIT OUT OUT </pre> <H2>Output for the Sample Input</H2> <pre> 7 0 </pre>
p00416
<h1>2つの多角形</h1>   <p> ヤエさんは3本以上の真っすぐな棒を繋げて多角形を作り、恋人のジョウ君にプレゼントすることにしました。棒はそれらの端点でのみ繋げることができます。端点はすべて同じ平面上に置きます。一つの接合点で繋がる棒は2本だけで、それらがなす角度は自由に決めることができます。 </p> <p> ヤエさんは、自分が持っている棒をすべて使って大きさが同じくらいの2つの多角形を作ろうとしています。 </p> <p> 与えられたすべての棒を使って、2つの多角形を、それらの周囲の長さの差の絶対値が最小となるように作る。各棒の長さが与えられたとき、2つの多角形の周囲の長さの差の絶対値を求めるプログラムを作成せよ。2つの多角形を作成できない場合はそのことを報告せよ。ただし、長さが$a_1 \leq a_2 \leq ... \leq a_m$である$m$ ($3 \leq m$)本の棒で、$a_m <a_1+a_2+ ... +a_{m-1}$を満たすとき、これら$m$本の棒でひとつの多角形を作成できるものとする。 </p> <h2>入力</h2> <p> 入力は以下の形式で与えられる。 </p> <pre> $N$ $a_1$ $a_2$ ... $a_N$ </pre> <p> 1行目に棒の数$N$ ($6 \leq N \leq 40$)が与えられる。2行目に各棒の長さ$a_i$ ($1 \leq a_i \leq 100$)が与えられる。 </p> <h2>出力</h2> <p> 与えられたすべての棒を使って2つの多角形を作れる場合は、長さの差の絶対値の最小値を1行に出力する。2つの多角形を作れない場合は「-1」を1行に出力する。 </p> <h2>入出力例</h2> <h3>入力例1</h3> <pre> 6 2 2 3 2 2 2 </pre> <h3>出力例1</h3> <pre> 1 </pre> <h3>入力例2</h3> <pre> 7 2 2 3 2 2 2 1 </pre> <h3>出力例2</h3> <pre> 0 </pre> <h3>入力例3</h3> <pre> 6 1 1 1 1 2 4 </pre> <h3>出力例3</h3> <pre> -1 </pre>
p02581
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have <var>3N</var> cards arranged in a row from left to right, where each card has an integer between <var>1</var> and <var>N</var> (inclusive) written on it. The integer written on the <var>i</var>-th card from the left is <var>A_i</var>.</p> <p>You will do the following operation <var>N-1</var> times:</p> <ul> <li>Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain <var>1</var> point.</li> </ul> <p>After these <var>N-1</var> operations, if the integers written on the remaining three cards are all equal, you will gain <var>1</var> additional point.</p> <p>Find the maximum number of points you can gain.</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 N</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>A_2</var> <var>\cdots</var> <var>A_{3N}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum number of points you can gain.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 1 2 1 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>Let us rearrange the five leftmost cards so that the integers written on the six cards will be <var>2\ 2\ 2\ 1\ 1\ 1</var> from left to right.</p> <p>Then, remove the three leftmost cards, all of which have the same integer <var>2</var>, gaining <var>1</var> point.</p> <p>Now, the integers written on the remaining cards are <var>1\ 1\ 1</var>.</p> <p>Since these three cards have the same integer <var>1</var>, we gain <var>1</var> more point.</p> <p>In this way, we can gain <var>2</var> points - which is the maximum possible.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 1 1 2 2 3 3 3 2 1 </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 2 2 2 3 3 3 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>3 </pre></section> </div> </span>
p00046
<H1>標高差</H1> <p> 今まで登ったことのある山の標高を記録したデータがあります。このデータを読み込んで、一番高い山と一番低い山の標高差を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は以下の形式で与えられます。 </p> <pre class="exp"> 山の高さ ... ... </pre> <p> 山の高さが複数行に渡って与えられます。入力される値はすべて 0 以上 1,000,000 以下の実数です。入力される山の高さの数は 50 以下です。 </p> <H2>Output</H2> <p> 一番高い山と一番低い山の標高差を実数で出力する。出力は0.01以下の誤差を含んでもよい。 </p> <H2>Sample Input</H2> <pre> 3776.0 1819.0 645.2 2004.1 1208.6 </pre> <H2>Output for the Sample Input</H2> <pre> 3130.8 </pre>
p04005
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a rectangular parallelepiped of size <var>A×B×C</var>, built with blocks of size <var>1×1×1</var>. Snuke will paint each of the <var>A×B×C</var> blocks either red or blue, so that:</p> <ul> <li>There is at least one red block and at least one blue block.</li> <li>The union of all red blocks forms a rectangular parallelepiped.</li> <li>The union of all blue blocks forms a rectangular parallelepiped.</li> </ul> <p>Snuke wants to minimize the difference between the number of red blocks and the number of blue blocks. Find the minimum possible difference.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≤A,B,C≤10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> <var>C</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum possible difference between the number of red blocks and the number of blue blocks.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>9 </pre> <p>For example, Snuke can paint the blocks as shown in the diagram below. There are <var>9</var> red blocks and <var>18</var> blue blocks, thus the difference is <var>9</var>.</p> <p><img src="/img/agc/004/gatbantar/A_1.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 2 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>For example, Snuke can paint the blocks as shown in the diagram below. There are <var>8</var> red blocks and <var>8</var> blue blocks, thus the difference is <var>0</var>.</p> <p><img src="/img/agc/004/gatbantar/A_2.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 3 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>15 </pre> <p>For example, Snuke can paint the blocks as shown in the diagram below. There are <var>45</var> red blocks and <var>30</var> blue blocks, thus the difference is <var>9</var>.</p> <p><img src="/img/agc/004/gatbantar/A_3.png"/></p></section> </div> </span>
p01207
<H1><font color="#000">Problem A:</font> Hit and Blow</H1> <p> <i>Hit and blow</i> is a popular code-breaking game played by two people, one codemaker and one codebreaker. The objective of this game is that the codebreaker guesses correctly a secret number the codemaker makes in his or her mind. </p> <p> The game is played as follows. The codemaker first chooses a secret number that consists of four different digits, which may contain a leading zero. Next, the codebreaker makes the first attempt to guess the secret number. The guessed number needs to be legal (i.e. consist of four different digits). The codemaker then tells the numbers of <i>hits</i> and <i>blows</i> to the codebreaker. Hits are the matching digits on their right positions, and blows are those on different positions. For example, if the secret number is 4321 and the guessed is 2401, there is one hit and two blows where 1 is a hit and 2 and 4 are blows. After being told, the codebreaker makes the second attempt, then the codemaker tells the numbers of hits and blows, then the codebreaker makes the third attempt, and so forth. The game ends when the codebreaker gives the correct number. </p> <p> Your task in this problem is to write a program that determines, given the situation, whether the codebreaker can logically guess the secret number within the next two attempts. Your program will be given the four-digit numbers the codebreaker has guessed, and the responses the codemaker has made to those numbers, and then should make output according to the following rules: </p> <ul> <li> if only one secret number is possible, print the secret number;</li> <li> if more than one secret number is possible, but there are one or more critical numbers, print the <i>smallest</i> one;</li> <li> otherwise, print “????” (four question symbols).</li> </ul> <p> Here, critical numbers mean those such that, after being given the number of hits and blows for them on the next attempt, the codebreaker can determine the secret number uniquely. </p> <H2>Input</H2> <p> The input consists of multiple data sets. Each data set is given in the following format: </p> <pre> <i>N</i> <i>four-digit-number</i><sub>1</sub> <i>n-hits</i><sub>1</sub> <i>n-blows</i><sub>1</sub> ... <i>four-digit-number<sub>N</sub> n-hits<sub>N</sub> n-blows<sub>N</sub></i> </pre> <p> <i>N</i> is the number of attempts that has been made. <i>four-digit-number<sub>i</sub></i> is the four-digit number guessed on the <i>i</i>-th attempt, and <i>n-hits<sub>i</sub></i> and <i>n-blows<sub>i</sub></i> are the numbers of hits and blows for that number, respectively. It is guaranteed that there is at least one possible secret number in each data set. </p> <p> The end of input is indicated by a line that contains a zero. This line should not be processed. </p> <H2>Output</H2> <p> For each data set, print a four-digit number or “????” on a line, according to the rules stated above. </p> <H2>Sample Input</H2> <pre> 2 1234 3 0 1245 3 0 1 1234 0 0 2 0123 0 4 1230 0 4 0 </pre> <H2>Output for the Sample Input</H2> <pre> 1235 ???? 0132 </pre>
p03390
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p><var>10^{10^{10}}</var> participants, including Takahashi, competed in two programming contests. In each contest, all participants had distinct ranks from first through <var>10^{10^{10}}</var>-th.</p> <p>The <em>score</em> of a participant is the product of his/her ranks in the two contests.</p> <p>Process the following <var>Q</var> queries:</p> <ul> <li>In the <var>i</var>-th query, you are given two positive integers <var>A_i</var> and <var>B_i</var>. Assuming that Takahashi was ranked <var>A_i</var>-th in the first contest and <var>B_i</var>-th in the second contest, find the maximum possible number of participants whose scores are smaller than Takahashi's.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq Q \leq 100</var></li> <li><var>1\leq A_i,B_i\leq 10^9(1\leq i\leq Q)</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>Q</var> <var>A_1</var> <var>B_1</var> <var>:</var> <var>A_Q</var> <var>B_Q</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>For each query, print the maximum possible number of participants whose scores are smaller than Takahashi's.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>8 1 4 10 5 3 3 4 11 8 9 22 40 8 36 314159265 358979323 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 12 4 11 14 57 31 671644785 </pre> <p>Let us denote a participant who was ranked <var>x</var>-th in the first contest and <var>y</var>-th in the second contest as <var>(x,y)</var>.</p> <p>In the first query, <var>(2,1)</var> is a possible candidate of a participant whose score is smaller than Takahashi's. There are never two or more participants whose scores are smaller than Takahashi's, so we should print <var>1</var>.</p></section> </div> </span>
p00945
<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 K Min-Max Distance Game</h2> <p> Alice and Bob are playing the following game. Initially, $n$ stones are placed in a straight line on a table. Alice and Bob take turns alternately. In each turn, the player picks one of the stones and removes it. The game continues until the number of stones on the straight line becomes two. The two stones are called <i>result stones</i>. Alice's objective is to make the result stones as distant as possible, while Bob's is to make them closer. </p> <p> You are given the coordinates of the stones and the player's name who takes the first turn. Assuming that both Alice and Bob do their best, compute and output the distance between the result stones at the end of the game. </p> <h3>Input</h3> <p> The input consists of a single test case with the following format.<br> <br> $n$ $f$<br> $x_1$ $x_2$ ... $x_n$<br> <br> $n$ is the number of stones $(3 \leq n \leq 10^5)$. $f$ is the name of the first player, either Alice or Bob. For each $i$, $x_i$ is an integer that represents the distance of the $i$-th stone from the edge of the table. It is guaranteed that $0 \leq x_1 < x_2 < ... < x_n \leq 10^9$ holds. </p> <h3>Output</h3> <p> Output the distance between the result stones in one line. </p> <h3>Sample Input 1</h3> <pre>5 Alice 10 20 30 40 50</pre> <h3>Sample Output 1</h3> <pre>30</pre> <h3>Sample Input 2</h3> <pre>5 Bob 2 3 5 7 11</pre> <h3>Sample Output 2</h3> <pre>2</pre>
p02978
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a stack of <var>N</var> cards, each of which has a non-negative integer written on it. The integer written on the <var>i</var>-th card from the top is <var>A_i</var>.</p> <p>Snuke will repeat the following operation until two cards remain:</p> <ul> <li>Choose three consecutive cards from the stack.</li> <li>Eat the middle card of the three.</li> <li>For each of the other two cards, replace the integer written on it by the sum of that integer and the integer written on the card eaten.</li> <li>Return the two cards to the original position in the stack, without swapping them.</li> </ul> <p>Find the minimum possible sum of the integers written on the last two cards remaining.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 18</var></li> <li><var>0 \leq A_i \leq 10^9 (1\leq 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 possible sum of the integers written on the last two cards remaining.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 3 1 4 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>16 </pre> <p>We can minimize the sum of the integers written on the last two cards remaining by doing as follows:</p> <ul> <li>Initially, the integers written on the cards are <var>3</var>, <var>1</var>, <var>4</var>, and <var>2</var> from top to bottom.</li> <li>Choose the first, second, and third card from the top. Eat the second card with <var>1</var> written on it, add <var>1</var> to each of the other two cards, and return them to the original position in the stack. The integers written on the cards are now <var>4</var>, <var>5</var>, and <var>2</var> from top to bottom.</li> <li>Choose the first, second, and third card from the top. Eat the second card with <var>5</var> written on it, add <var>5</var> to each of the other two cards, and return them to the original position in the stack. The integers written on the cards are now <var>9</var> and <var>7</var> from top to bottom.</li> <li>The sum of the integers written on the last two cards remaining is <var>16</var>.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 2 4 1 6 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>51 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>10 3 1 4 1 5 9 2 6 5 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>115 </pre></section> </div> </span>
p01657
<h2>B - ライオン</h2> <p> あなたは動物園に来てライオンを見ている。 この動物園には<var>n</var>個のライオンのオリがある。 あなたはライオンが全部で何頭いるのか知りたくなったのだが、 一人で数えるのは大変だ。 そこで近くにいた動物園の来園者<var>m</var>人に協力してもらうことにした。 </p> <p> ライオンのオリは、それぞれ<var>1</var>番から<var>n</var>番までの数字がついており、 順番に並んでいる。 一つのオリには<var>0</var>頭以上<var>x</var>頭以下のライオンがいる。 </p> <p> <var>m</var>人の来園者で<var>i</var>番目の人<var>(1 &le; i &le; m)</var>には <var>l_i</var>番から <var>r_i</var>番までの 両端を含むすべてのオリのライオンの数の総和を数えてもらった。 </p> <p> この情報を元に、<var>1</var>から<var>n</var> 各オリの中のライオンの数を求めることにした。 </p> <h2>入力形式</h2> <p> 入力は以下の形式で与えられる <pre> <var>n</var> <var>x</var> <var>m</var> <var>l_1</var> <var>r_1</var> <var>s_1</var> <var>...</var> <var>l_m</var> <var>r_m</var> <var>s_m</var> </pre> <p> 1行目は3つの整数<var>n</var> <var>x</var> <var>m</var>で、 それぞれオリの数、一つのオリの中のライオンの最大の数、 数えるのに協力した人の数である。 2行目から続く<var>m</var>行は各行3つの整数で、 それぞれ<var>i</var>番目の人が数えた結果 <var>l_i</var> <var>r_i</var><var>s_i</var>である。 <var>l_i</var>番から<var>r_i</var>番までのオリのライオンを数えたら 合計<var>s_i</var>頭いたことを表している。 </p> <h2>出力形式</h2> <p> 数えてもらった結果が全て正しいとして、 <var>1</var>から<var>n</var>の 各オリの中のライオンの数を空白区切りの<var>n</var>個の整数で出力せよ。 そのような答が複数あるときは、 ライオンの数の合計が最大になるものをどれでも良いので一つ出力せよ。 条件を満たすライオンの配置が一つもないときは<var>-1</var>を出力せよ。 </p> <h2>制約</h2> <ul> <li><var>1 &le; n &le; 6</var></li> <li><var>1 &le; x &le; 10</var></li> <li><var>1 &le; m &le; 10</var></li> <li><var>1 &le; l_i &le; r_i &le; n</var></li> <li><var>0 &le; s_i &le; 60</var></li> <li>入力値はすべて整数である。</li> </ul> <h2>入出力例</h2> <h3>入力例 1</h3> <pre> 3 5 1 1 2 5 </pre> <h3>出力例 1</h3> <pre> 2 3 5 </pre> <p> その他"4 1 5"や"0 5 5"なども正しい出力である。 </p> <h3>入力例 2</h3> <pre> 4 5 2 1 3 15 3 4 3 </pre> <h3>出力例 2</h3> <pre> -1 </pre> <p> 条件を満たすライオンの配置が一つもないときは<var>-1</var>を出力せよ。 </p>
p01440
<H1><font color="#000">Problem D:</font> Revenge of Champernowne Constant</H1> <p> Champernowne constant is an irrational number. Its decimal representation starts with "0.", followed by concatenation of all positive integers in the increasing order. </p> <p> You will be given a sequence <i>S</i> which consists of decimal digits. Your task is to write a program which computes the position of the first occurrence of S in Champernowne constant after the decimal point. </p> <H2>Input</H2> <p> The input has multiple test cases. Each line of the input has one digit sequence. The input is terminated by a line consisting only of <span>#</span>. </p> <p> It is guaranteed that each sequence has at least one digit and its length is less than or equal to 100. </p> <H2>Output</H2> <p> For each sequence, output one decimal integer described above. You can assume each output value is less than 10<sup>16</sup>. </p> <H2>Sample Input</H2> <pre> 45678 67891011 21 314159265358979 # </pre> <H2>Output for the Sample Input</H2> <pre> 4 6 15 2012778692735799 </pre>
p03187
<span class="lang-en"> <p>Score : <var>800</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi Lake has a perimeter of <var>L</var>. On the circumference of the lake, there is a residence of the lake's owner, Takahashi. Each point on the circumference of the lake has a coordinate between <var>0</var> and <var>L</var> (including <var>0</var> but not <var>L</var>), which is the distance from the Takahashi's residence, measured counter-clockwise.</p> <p>There are <var>N</var> trees around the lake; the coordinate of the <var>i</var>-th tree is <var>X_i</var>. There is no tree at coordinate <var>0</var>, the location of Takahashi's residence.</p> <p>Starting at his residence, Takahashi will repeat the following action:</p> <ul> <li>If all trees are burnt, terminate the process.</li> <li>Specify a direction: clockwise or counter-clockwise.</li> <li>Walk around the lake in the specified direction, until the coordinate of a tree that is not yet burnt is reached for the first time.</li> <li>When the coordinate with the tree is reached, burn that tree, stay at the position and go back to the first step.</li> </ul> <p>Find the longest possible total distance Takahashi walks during the process.</p> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><p>A partial score can be obtained in this problem:</p> <ul> <li><var>300</var> points will be awarded for passing the input satisfying <var>N \leq 2000</var>.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq L \leq 10^9</var></li> <li><var>1 \leq N \leq 2\times 10^5</var></li> <li><var>1 \leq X_1 &lt; ... &lt; X_N \leq L-1</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>L</var> <var>N</var> <var>X_1</var> <var>:</var> <var>X_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the longest possible total distance Takahashi walks during the process.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>10 3 2 7 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>15 </pre> <p>Takahashi walks the distance of <var>15</var> if the process goes as follows:</p> <ul> <li>Walk a distance of <var>2</var> counter-clockwise, burn the tree at the coordinate <var>2</var> and stay there.</li> <li>Walk a distance of <var>5</var> counter-clockwise, burn the tree at the coordinate <var>7</var> and stay there.</li> <li>Walk a distance of <var>8</var> clockwise, burn the tree at the coordinate <var>9</var> and stay there.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 6 1 2 3 6 7 9 </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>314159265 7 21662711 77271666 89022761 156626166 160332356 166902656 298992265 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1204124749 </pre></section> </div> </span>
p01010
<script src="./IMAGE/varmath.js" charset="UTF-8"></script> <h1>Problem E: Light Source</h1> <h2>Problem</h2> <!-- <p> <var>n</var>個の円状の光源がある。ある光源が光を受けた場合、その光源の中心点から扇状に光が放出される。光源が光を受けるとは、光となる扇形が光源となる円を完全に覆うことをいう。また、光源を覆う事によって光は遮られず、扇形に覆われていない円の部分が存在する場合、光を受けたと見なされない。光を受けた光源から放出される光の強さは、受けた光の強さと同じになる。円を全て囲うように受けた光が複数存在する場合、放出される光の強さはそれらの合計となる。ただし、放出される光の強さには上限があり、上限を超えた場合、光の強さは上限と同値になる。 </p> --> <p> 二次元平面上に<var>n</var>個の円状の光源が配置されている。ある光源が光を受けると、下図のようにその光源の中心点から扇状に光が放出される。 <br/> <br/> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE3_ACPC2013Aizu_aizuicpc_shape"><br/> 光源の中心点は(<var>x</var>,<var>y</var>)、半径は<var>r</var>で表される。 光となる扇形は角<var>&beta;</var>を中心に-<var>&theta;</var>/2, +<var>&theta;</var>/2の方向へ対称に広がり、その半径は<var>&alpha;</var>である。 </p> <p> 光源の円が扇型の光に完全に覆われるとき、光源はその光を受けたとみなされる。光源から放出される光の強さは、受けた光の強さの合計となる。ただし、放出される光の強さには上限があり、上限を超えた場合、光の強さは上限値と同じになる。なお、ある光や光源によって光が遮られることはない。 </p> <p> ある地点に存在する光源を目的光源とする。目的光源では光が放出されず、受ける光の強さに制限はない。 あなたは座標(0,0)から、ある強さの扇形の光を、任意の方向に1度だけ放出することができる。目的光源が受ける光の強さの合計の最大値を求めるプログラムを作成せよ。ただし、全ての光源において、放出された光が元の光源に戻るような入力データは与えられないと仮定してよい。また、この問題では、領域の境界からの距離が0.000001以内の場合、領域上とみなすことにする。 </p> <h2>Input</h2> <pre> <var>n</var> <var>&theta;<sub>0</sub></var> <var>&alpha;<sub>0</sub></var> <var>power</var> <var>x<sub>1</sub></var> <var>y<sub>1</sub></var> <var>r<sub>1</sub></var> <var>&theta;<sub>1</sub></var> <var>&alpha;<sub>1</sub></var> <var>&beta;<sub>1</sub></var> <var>maxpower<sub>1</sub></var> . . <var>x<sub>n</sub></var> <var>y<sub>n</sub></var> <var>r<sub>n</sub></var> <var>&theta;<sub>n</sub></var> <var>&alpha;<sub>n</sub></var> <var>&beta;<sub>n</sub></var> <var>maxpower<sub>n</sub></var> <var>x<sub>n+1</sub></var> <var>y<sub>n+1</sub></var> <var>r<sub>n+1</sub></var> </pre> <p> 1行目には目的光源を除く光源の数<var>n</var>が与えられる。<br /> 2行目に最初に光を放出する座標(0,0)の情報、3行目からn+2行目に目的でない光源の情報、n+3行目に目的光源の情報がそれぞれ空白区切りで与えられる。<br /> 各情報に対して、<var>x<sub>i</sub></var>,<var>y<sub>i</sub></var>は光源の中心点の座標、<var>r<sub>i</sub></var>は光源の半径、<var>&theta;<sub>i</sub></var>は光が広がる角度、<var>&alpha;<sub>i</sub></var>は光の半径、<var>&beta;<sub>i</sub></var>は光が向かう方向を表す。2行目の<var>power</var>は最初に放出される光の強さ、3行目からn+2行目の<var>maxpower<sub>i</sub></var>は放出される光の上限を表す。<br /> 光の方向<var>&beta;<sub>i</sub></var>はx軸の正の向きから反時計回りに回転し、各角度の単位は度とする。<br /> </p> <h2>Constraints</h2> <p>入力は以下の条件を満たす。</p> <ul> <li>入力は全て整数からなる</li> <li>1 &le; <var>n</var> &le; 100</li> <li>-1000 &le; <var>x<sub>i</sub></var>,<var>y<sub>i</sub></var> &le; 1000</li> <li>1 &le; <var>r<sub>i</sub></var> &le; 1000</li> <li>1 &le; <var>&theta;<sub>i</sub></var> &le; 180</li> <li>1 &le; <var>&alpha;<sub>i</sub></var> &le; 1000</li> <li>0 &le; <var>&beta;<sub>i</sub></var> &lt; 360</li> <li>0 &le; <var>power</var>,<var>maxpower<sub>i</sub></var> &le; 1000</li> </ul> <h2>Output</h2> <p>目的光源を全て囲うような光の強さの合計の最大値を1行に出力せよ。</p> <h2>Sample Input 1</h2> <pre> 1 90 10 20 3 3 1 90 10 315 10 6 0 1 </pre> <h2>Sample Output 1</h2> <pre> 30 </pre> <h2>Sample Input 2</h2> <pre> 2 90 10 10 -3 0 1 45 10 90 10 -6 0 2 30 3 180 10 -9 0 1 </pre> <h2>Sample Output 2</h2> <pre> 10 </pre>
p00251
<H1>10問解いたら何点取れる?</H1> <p> 選手の皆さん、パソコン甲子園にようこそ。パソコン甲子園は今年で10周年になりますが、出題される問題数や合計得点は年によって異なります。各問題には難易度に応じて得点が決められています。問題数が10問で、それぞれの問題の得点が与えられるとき、それらの合計を出力するプログラムを作成して下さい。 </p> <h2>入力</h2> <p> 入力は以下の形式で与えられる。 </p> <pre> s<sub>1</sub> s<sub>2</sub> . . s<sub>10</sub> </pre> <p> 入力は10行からなり、i 行目に問題 i の得点を表す整数 s<sub>i</sub> (0&le; s<sub>i</sub> &le; 100)が与えられる。 </p> <h2>出力</h2> <p> 合計得点を1行に出力する。 </p> <h2>入力例1</h2> <pre> 1 2 3 4 5 6 7 8 9 10 </pre> <h2>出力例1</h2> <pre> 55 </pre> <br/> <h2>入力例2</h2> <pre> 4 4 8 8 8 10 10 12 16 20 </pre> <h2>出力例2</h2> <pre> 100 </pre>
p02796
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>In a factory, there are <var>N</var> robots placed on a number line. Robot <var>i</var> is placed at coordinate <var>X_i</var> and can extend its arms of length <var>L_i</var> in both directions, positive and negative.</p> <p>We want to remove zero or more robots so that the movable ranges of arms of no two remaining robots intersect. Here, for each <var>i</var> (<var>1 \leq i \leq N</var>), the movable range of arms of Robot <var>i</var> is the part of the number line between the coordinates <var>X_i - L_i</var> and <var>X_i + L_i</var>, excluding the endpoints.</p> <p>Find the maximum number of robots that we can keep.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 100,000</var></li> <li><var>0 \leq X_i \leq 10^9</var> (<var>1 \leq i \leq N</var>)</li> <li><var>1 \leq L_i \leq 10^9</var> (<var>1 \leq i \leq N</var>)</li> <li>If <var>i \neq j</var>, <var>X_i \neq X_j</var>.</li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>X_1</var> <var>L_1</var> <var>X_2</var> <var>L_2</var> <var>\vdots</var> <var>X_N</var> <var>L_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum number of robots that we can keep.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 2 4 4 3 9 3 100 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>By removing Robot <var>2</var>, we can keep the other three robots.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 8 20 1 10 </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>5 10 1 2 1 4 1 6 1 8 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>5 </pre></section> </div> </span>
p03884
<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>Welcome to CODE FESTIVAL 2016! In order to celebrate this contest, find a string <var>s</var> that satisfies the following conditions:</p> <ul> <li>The length of <var>s</var> is between <var>1</var> and <var>5000</var>, inclusive.</li> <li><var>s</var> consists of uppercase letters.</li> <li><var>s</var> contains exactly <var>K</var> occurrences of the string "FESTIVAL" as a subsequence. In other words, there are exactly <var>K</var> tuples of integers <var>(i_0, i_1, ..., i_7)</var> such that <var>0 ≤ i_0 &lt; i_1 &lt; ... &lt; i_7 ≤ |s|-1</var> and <var>s[i_0]='F', s[i_1]='E', ..., s[i_7]='L'</var>.</li> </ul> <p>It can be proved that under the given constraints, the solution always exists. In case there are multiple possible solutions, you can output any.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ K ≤ 10^{18}</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>K</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print a string that satisfies the conditions.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>FESSSSSSSTIVAL </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>256 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>FFEESSTTIIVVAALL </pre></section> </div> </span>
p00601
<H1><font color="#000000">Problem G:</font> Dominating Set</H1> <p> What you have in your hands is a map of Aizu-Wakamatsu City. The lines on this map represent streets and the dots are street corners. Lion Dor Company is going to build food stores at some street corners where people can go to buy food. It is unnecessary and expensive to build a food store on every corner. Their plan is to build the food stores so that people could get food either right there on the corner where they live, or at the very most, have to walk down only one street to find a corner where there was a food store. Now, all they have to do is figure out where to put the food store. </p> <p> The problem can be simplified as follows. Let <i>G</i> = (<i>V, E</i>) be unweighted undirected graph. A dominating set <i>D</i> of <i>G</i> is a subset of <i>V</i> such that every vertex in <i>V</i> - <i>D</i> is adjacent to at least one vertex in <i>D</i>. A minimum dominating set is a dominating set of minimum number of vertices for <i>G</i>. In the example graph given below, the black vertices are the members of the minimum dominating sets. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_dominatingset"> </center> <br/> <p> Design and implement an algorithm for the minimum domminating set problem. </p> <H2>Input</H2> <p> Input has several test cases. Each test case consists of the number of nodes <i>n</i> and the number of edges <i>m</i> in the first line. In the following <i>m</i> lines, edges are given by a pair of nodes connected by the edge. The graph nodess are named with the numbers 0, 1,..., <i>n</i> - 1 respectively. </p> <p> The input terminate with a line containing two 0. The number of test cases is less than 20. </p> <h2>Constrants</h2> <ul> <li> <i>n</i> &le; 30</li> <li> <i>m</i> &le; 50</li> </ul> <H2>Output</H2> <p> Output the size of minimum dominating set for each test case. </p> <H2>Sample Input</H2> <pre> 5 4 0 1 0 4 1 2 3 4 5 4 0 1 0 4 1 2 3 4 0 0 </pre> <H2>Output for the Sample Input</H2> <pre> 2 2 </pre>
p01913
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]} }); </script> <script type="text/javascript" async src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML"> </script> <h1>I: Islands Survival</h1> <h2>問題</h2> <p> $N$ 頂点 $M$ 辺の単純連結無向グラフがある. 頂点には $1, 2, \dots, N$ と番号がつけられている. 辺には $1, 2, \dots, M$ と番号がつけられており,辺 $i$ は頂点 $a_i$ と $b_i$ をつないでいる. また,辺 $i$ は時刻 $t_i$ に消える.どの辺も通過するために単位時間がかかる. あなたは最初,時刻 0 において頂点 1 にいる. あなたは最適に行動することで,時刻 $T$ までに得られるスコアを最大化したい. スコアは最初 0 であり,イベントは以下に従って起きる. </p> <ol> <li>今の時刻 $t'$ が $t' \geq T$ を満たすならば終了する.そうでなければ 2. へ.</li> <li>$t_i = t'$ なるすべての辺 $i$ が消える.</li> <li>あなたがいる頂点を $v$ とするとき,頂点 $v$ を含む連結成分に含まれる頂点数を $x$ とすると,スコアに $x$ が加算される.</li> <li>あなたは頂点 $v$ と隣接している頂点のいずれかに移動するか,頂点 $v$ に留まる.ただし前者の場合,既に消えた辺を使うことはできない.</li> <li>$t' \gets t' + 1$ として 1. へ.</li> </ol> <p> 得られるスコアの最大値を求めよ. </p> <h2>制約</h2> <ul> <li>$2 \le N \le 10^5$</li> <li>$N - 1 \le M \le \min(2 \times 10^5, N(N - 1) / 2)$</li> <li>$1 \le T \le 10^5$</li> <li>$1 \le a_i , b_i \le N$</li> <li>$1 \le t_i \le T$</li> <li>与えられるグラフは単純かつ連結.</li> </ul> <h2>入力形式</h2> <p> 入力は以下の形式で与えられる. </p> <p> $N \ M \ T$<br> $a_1 \ b_1 \ t_1$<br> $\vdots$<br> $a_M \ b_M \ t_M$<br> </p> <h2>出力</h2> <p> スコアの最大値を出力せよ.また,末尾に改行も出力せよ. </p> <h2>サンプル</h2> <h3>サンプル入力1</h3> <pre> 5 4 2 1 2 2 2 3 1 1 4 2 4 5 1 </pre> <h3>サンプル出力1</h3> <pre> 8 </pre> <p> 時刻 0 に 5,時刻 1 に 3 のスコアを得られる. </p> <p> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_RitsCamp17Day1_RUPC2017_Tree_SurvivalTestCase" width="30%" height="30%"> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_RitsCamp17Day1_RUPC2017_Tree_SurvivalTestCase" width="30%" height="30%"> </p> <h3>サンプル入力2</h3> <pre> 4 4 3 1 2 2 2 3 1 3 4 3 4 1 1 </pre> <h3>サンプル出力2</h3> <pre> 8 </pre> <p> 時刻 0 に 4,時刻 1 に 2,時刻 2 に 2 のスコアを得られる. </p>
p02329
<H1>Coin Combination Problem</H1> <p> You have 4 bags A, B, C and D each of which includes <var>N</var> coins (there are totally <var>4N</var> coins). Values of the coins in each bag are <var>a<sub>i</sub></var>, <var>b<sub>i</sub></var>, <var>c<sub>i</sub></var> and <var>d<sub>i</sub></var> respectively. </p> <p> Find the number of combinations that result when you choose one coin from each bag (totally <var>4</var> coins) in such a way that the total value of the coins is <var>V</var>. You should distinguish the coins in a bag. </p> <h2>Constraints</h2> <ul> <li> 1 &le; <var>N</var> &le; 1000 </li> <li> 1 &le; <var>a<sub>i</sub></var>, <var>b<sub>i</sub></var>, <var>c<sub>i</sub></var>, <var>d<sub>i</sub></var> &le; 10<sup>16</sup></li> <li> 1 &le; <var>V</var> &le; 10<sup>16</sup></li> <li> All input values are given in integers</li> </ul> <H2>Input</H2> <p> The input is given in the following format. </p> <pre> <var>N</var> <var>V</var> <var>a<sub>1</sub></var> <var>a<sub>2</sub></var> ... <var>a<sub>N</sub></var> <var>b<sub>1</sub></var> <var>b<sub>2</sub></var> ... <var>b<sub>N</sub></var> <var>c<sub>1</sub></var> <var>c<sub>2</sub></var> ... <var>c<sub>N</sub></var> <var>d<sub>1</sub></var> <var>d<sub>2</sub></var> ... <var>d<sub>N</sub></var> </pre> <H2>Output</H2> <p> Print the number of combinations in a line. </p> <H2>Sample Input 1</H2> <pre> 3 14 3 1 2 4 8 2 1 2 3 7 3 2 </pre> <H2>Sample Output 1</H2> <pre> 9 </pre> <br/> <H2>Sample Input 2</H2> <pre> 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 </pre> <H2>Sample Output 2</H2> <pre> 625 </pre>
p00314
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML"> </script> <H1>プログラミングコンテスト</H1> <p> 今年も白虎大学でプログラミングコンテストが開催されることになりました。コンテストではいくつかの問題が出題され、それぞれ難易度に応じた得点が割り当てられています。 </p> <p> 実行委員会は、解いた問題の数とそれらの得点の両方を考慮し、次のルールに基づいて各チームのスコアを計算することにしました。 </p> <p> 「あるチームが正解した問題のうち、得点が <var>A</var> 以上であるものが <var>A</var> 問以上あることを満たすような最大の <var>A</var> を、そのチームのスコアとする」 </p> <p> あるチームが正解した問題の数と、それらの問題の得点から、チームのスコアを計算するプログラムを作成せよ。 </p> <h2>Input</h2> <p> 入力は以下の形式で与えられる。 </p> <pre> <var>N</var> <var>p<sub>1</sub></var> <var>p<sub>2</sub></var> ... <var>p<sub>N</sub></var> </pre> <p> 1行目にチームが正解した問題の数 <var>N</var> (1 &le; <var>N</var> &le; 100) が与えられる。2行目に正解した各問題の得点 <var>p<sub>i</sub></var>(1 &le; <var>p<sub>i</sub></var> &le; 100) が与えられる。 </p> <h2>Output</h2> <p> チームのスコアを1行に出力する。 </p> <h2>Sample Input 1</h2> <pre> 7 5 4 3 10 2 4 1 </pre> <h2>Sample Output 1</h2> <pre> 4 </pre> <p> 得点が 4 以上の問題を 4 問以上正解しているので、スコアは 4 となる。 </p> <br/> <h2>Sample Input 2</h2> <pre> 3 1 1 100 </pre> <h2>Sample Output 2</h2> <pre> 1 </pre> <p> 得点が 1 以上の問題を 1 問以上正解しているので、スコアは 1 となる。 </p> <br/> <h2>Sample Input 3</h2> <pre> 4 11 15 58 1 </pre> <h2>Sample Output 3</h2> <pre> 3 </pre> <p> 得点が 3 以上の問題を 3 問以上正解しているので、スコアは 3 となる。 </p>
p02283
<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>Binary Search Tree I</H1> <p> Search trees are data structures that support dynamic set operations including insert, search, delete and so on. Thus a search tree can be used both as a dictionary and as a priority queue. </p> <p> Binary search tree is one of fundamental search trees. The keys in a binary search tree are always stored in such a way as to satisfy the following binary search tree property: </p> <ul> <li> Let $x$ be a node in a binary search tree. If $y$ is a node in the left subtree of $x$, then $y.key \leq x.key$. If $y$ is a node in the right subtree of $x$, then $x.key \leq y.key$. </li> </ul> <p> The following figure shows an example of the binary search tree. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ALDS1_8_A.png"> </center> <br> <p> For example, keys of nodes which belong to the left sub-tree of the node containing 80 are less than or equal to 80, and keys of nodes which belong to the right sub-tree are more than or equal to 80. The binary search tree property allows us to print out all the keys in the tree in sorted order by an inorder tree walk. </p> <p> A binary search tree should be implemented in such a way that the binary search tree property continues to hold after modifications by insertions and deletions. A binary search tree can be represented by a linked data structure in which each node is an object. In addition to a key field and satellite data, each node contains fields <i>left</i>, <i>right</i>, and <i>p</i> that point to the nodes corresponding to its left child, its right child, and its parent, respectively. </p> <p> To insert a new value $v$ into a binary search tree $T$, we can use the procedure insert as shown in the following pseudo code. The insert procedure is passed a node $z$ for which $z.key = v$, $z.left = NIL$, and $z.right = NIL$. The procedure modifies $T$ and some of the fields of $z$ in such a way that $z$ is inserted into an appropriate position in the tree. </p> <pre> 1 insert(T, z) 2 y = NIL // parent of x 3 x = 'the root of T' 4 while x &ne; NIL 5 y = x // set the parent 6 if z.key &lt; x.key 7 x = x.left // move to the left child 8 else 9 x = x.right // move to the right child 10 z.p = y 11 12 if y == NIL // T is empty 13 'the root of T' = z 14 else if z.key &lt; y.key 15 y.left = z // z is the left child of y 16 else 17 y.right = z // z is the right child of y </pre> <p> Write a program which performs the following operations to a binary search tree $T$. </p> <ul> <li><span>insert </span> $k$: Insert a node containing $k$ as key into $T$.</li> <li><span>print</span>: Print the keys of the binary search tree by inorder tree walk and preorder tree walk respectively.</li> </ul> <p> You should use the above pseudo code to implement the insert operation. $T$ is empty at the initial state. </p> <H2>Input</H2> <p> In the first line, the number of operations $m$ is given. In the following $m$ lines, operations represented by <span>insert </span>$k$ or <span>print</span> are given. </p> <H2>Output</H2> <p> For each <span>print</span> operation, print a list of keys obtained by inorder tree walk and preorder tree walk in a line respectively. Put a space character <u>before each key</u>. </p> <H2>Constraints</H2> <ul> <li>The number of operations $\leq 500,000$</li> <li>The number of print operations $\leq 10$.</li> <li>$-2,000,000,000 \leq key \leq 2,000,000,000$</li> <li>The height of the binary tree does not exceed 100 if you employ the above pseudo code.</li> <li>The keys in the binary search tree are all different.</li> </ul> <H2>Sample Input 1</H2> <pre> 8 insert 30 insert 88 insert 12 insert 1 insert 20 insert 17 insert 25 print </pre> <H2>Sample Output 1</H2> <pre> 1 12 17 20 25 30 88 30 12 1 20 17 25 88 </pre> <H2>Reference</H2> <p> Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press. </p>
p02779
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given is a sequence of integers <var>A_1, A_2, ..., A_N</var>. If its elements are pairwise distinct, print <code>YES</code>; otherwise, print <code>NO</code>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N ≤ 200000</var></li> <li><var>1 ≤ A_i ≤ 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>If the elements of the sequence are pairwise distinct, print <code>YES</code>; otherwise, print <code>NO</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 2 6 1 4 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>YES </pre> <p>The elements are pairwise distinct.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 4 1 3 1 6 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>NO </pre> <p>The second and fourth elements are identical.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>2 10000000 10000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>NO </pre></section> </div> </span>
p00744
<h1><font color="#000000">Problem E:</font> Cards</h1> <p> There are many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards. </p> <p> A blue card and a red card can be paired when both of the numbers printed on them have a common divisor greater than 1. There may be more than one red card that can be paired with one blue card. Also, there may be more than one blue card that can be paired with one red card. When a blue card and a red card are chosen and paired, these two cards are removed from the whole cards on the table. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_2009E1" width=300 ><br> Figure E-1: Four blue cards and three red cards </center> <p> For example, in Figure E-1, there are four blue cards and three red cards. Numbers 2, 6, 6 and 15 are printed on the faces of the four blue cards, and 2, 3 and 35 are printed on those of the three red cards. Here, you can make pairs of blue cards and red cards as follows. First, the blue card with number 2 on it and the red card with 2 are paired and removed. Second, one of the two blue cards with 6 and the red card with 3 are paired and removed. Finally, the blue card with 15 and the red card with 35 are paired and removed. Thus the number of removed pairs is three. </p> <p> Note that the total number of the pairs depends on the way of choosing cards to be paired. The blue card with 15 and the red card with 3 might be paired and removed at the beginning. In this case, there are only one more pair that can be removed and the total number of the removed pairs is two. </p> <p> Your job is to find the largest number of pairs that can be removed from the given set of cards on the table. </p> <h3>Input</h3> <p> The input is a sequence of datasets. The number of the datasets is less than or equal to 100. Each dataset is formatted as follows. </p> <p> <blockquote> <i>m</i> <i>n</i> <br> <i>b</i><sub>1</sub> ... <i>b</i><sub><i>k</i></sub> ... <i>b</i><sub><i>m</i></sub> <br> <i>r</i><sub>1</sub> ... <i>r</i><sub><i>k</i></sub> ... <i>r</i><sub><i>n</i></sub> <br> </blockquote> </p> <p> The integers <i>m</i> and <i>n</i> are the number of blue cards and that of red cards, respectively. You may assume 1 &le; <i>m</i> &le; 500 and 1&le; <i>n</i> &le; 500. <i>b</i><sub><i>k</i></sub> (1 &le; <i>k</i> &le; <i>m</i>) and <i>r</i><sub><i>k</i></sub> (1 &le; <i>k</i> &le; <i>n</i>) are numbers printed on the blue cards and the red cards respectively, that are integers greater than or equal to 2 and less than 10000000 (=10<sup>7</sup>). The input integers are separated by a space or a newline. Each of <i>b</i><sub><i>m</i></sub> and <i>r</i><sub><i>n</i></sub> is followed by a newline. There are no other characters in the dataset. </p> <p> The end of the input is indicated by a line containing two zeros separated by a space. </p> <h3>Output</h3> <p> For each dataset, output a line containing an integer that indicates the maximum of the number of the pairs. </p> <h3>Sample Input</h3> <pre> 4 3 2 6 6 15 2 3 5 2 3 4 9 8 16 32 4 2 4 9 11 13 5 7 5 5 2 3 5 1001 1001 7 11 13 30 30 10 10 2 3 5 7 9 11 13 15 17 29 4 6 10 14 18 22 26 30 34 38 20 20 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 100 100 195 144 903 63 137 513 44 626 75 473 876 421 568 519 755 840 374 368 570 872 363 650 155 265 64 26 426 391 15 421 373 984 564 54 823 477 565 866 879 638 117 755 835 683 52 369 302 424 513 870 75 874 299 228 140 361 30 342 750 819 761 123 804 325 952 405 578 517 49 457 932 941 988 767 624 41 912 702 241 426 351 92 300 648 318 216 785 347 556 535 166 318 434 746 419 386 928 996 680 975 231 390 916 220 933 319 37 846 797 54 272 924 145 348 350 239 563 135 362 119 446 305 213 879 51 631 43 755 405 499 509 412 887 203 408 821 298 443 445 96 274 715 796 417 839 147 654 402 280 17 298 725 98 287 382 923 694 201 679 99 699 188 288 364 389 694 185 464 138 406 558 188 897 354 603 737 277 35 139 556 826 213 59 922 499 217 846 193 416 525 69 115 489 355 256 654 49 439 118 961 0 0 </pre> <h3>Output for the Sample Input</h3> <pre> 3 1 0 4 9 18 85 </pre>
p01856
<h1 id="e-台風-typhoon">E : 台風 / Typhoon</h1> <h2 id="問題文">問題文</h2> <p>南北方向に <var>H</var> ,東西方向に <var>W</var> の大きさの町がある. 町には一辺の長さが <var>1</var> の正方形の区画に隙間なく整備されており,全ての区画に <var>1</var> 軒ずつ家が建っている.</p> <p>この町のある区画の上空で台風が発生し,被害を与えた後,ある区画の上空で温帯低気圧に変化した. 変化した後は被害を与えない. 下図のように,台風は高さ <var>3</var>, 幅 <var>3</var> の正方形であり, ★のついたマスを中心と呼ぶことにする.台風は <var>8</var> 近傍に区画単位で移動する. つまり,台風の中心は辺または頂点を共有する区画(現在の区画を含む)に移るように,全体を伴って移動する. ただし,町の外に台風がはみ出ることはなく, 台風の中心は,下の図の網掛けのように,北から <var>0</var> 番目と <var>H &minus; 1</var> 番目,西から <var>0</var> 番目と <var>W &minus; 1</var> 番目の区間は通らないように移動する. </p> <div class="figure"> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_RUPC2015ACPC2015_E" /> </div> <p>家は台風が一度上空に来ると,以下のように被害の程度が変化する.</p> <blockquote> <p>損壊ナシ → 一部損壊 → 半壊 → 全壊 → 跡形ナシ</p> </blockquote> <p>だが,幸い跡形ナシとなった家は無かったようだ. 各家の被害の状況が与えられるので,台風が発生した地点と,温帯低気圧に変化した地点を求めよ. ただし,発生した区画を北から <var>s_i</var> 番目,西から <var>s_j</var> 番目, 温帯低気圧に変化した区画を北から <var>t_i</var> 番目,西から <var>t_j</var> 番目とすると, <var>2</var> つの地点は <var>10000 t_i + t_j</var> <var>&le;</var> <var>10000 s_i + s_j</var> を満たすように定まる.</p> <h2 id="入力">入力</h2> <pre> <var>H \ W</var> <var>D_{11} \ … \ D_{1W}</var> <var>D_{21} \ … \ D_{2W}</var> <var>…</var> <var>D_{H1} \ … \ D_{HW}</var> </pre> <p><var>D_{ij}</var> は北から <var>i</var> 番目, 西から <var>j</var> 番目の家の被害の程度を以下のように表す整数である.</p> <ul> <li><var>0</var> : 損壊ナシ</li> <li><var>1</var> : 一部損壊</li> <li><var>2</var> : 半壊</li> <li><var>3</var> : 全壊</li> </ul> <h2 id="制約">制約</h2> <ul> <li>整数である</li> <li>入力は答えが一意に定まるようなもののみ与えられる</li> <li><var>3 &le; H,W &le; 500</var></li> <li><var>0 &le; D_{ij} &le; 3</var></li> </ul> <h2 id="出力">出力</h2> <p>答えを以下のように <var>1</var> 行で出力せよ.</p> <pre> <var>s_i \ s_j \ t_i \ t_j</var> </pre> <h2 id="サンプル">サンプル</h2> <h3 id="サンプル入力1">サンプル入力1</h3> <pre> 7 5 0 0 0 0 0 0 1 1 1 0 0 2 2 2 0 0 3 3 3 0 0 2 2 2 0 0 1 1 1 0 0 0 0 0 0 </pre> <h3 id="サンプル出力1">サンプル出力1</h3> <pre> 4 2 2 2 </pre> <h3 id="サンプル入力2">サンプル入力2</h3> <pre> 6 6 0 0 0 1 1 1 0 0 0 2 2 2 0 0 1 3 3 2 1 2 3 3 2 1 1 2 3 2 1 0 1 2 2 1 0 0 </pre> <h3 id="サンプル出力2">サンプル出力2</h3> <pre> 4 1 1 4 </pre> <h3 id="サンプル入力3">サンプル入力3</h3> <pre> 4 4 2 2 2 0 2 2 2 0 2 2 2 0 0 0 0 0 </pre> <h3 id="サンプル出力3">サンプル出力3</h3> <pre> 1 1 1 1 </pre> <!-- - - - end nicebady - - - -->
p01505
<script src="./IMAGE/varmath.js" charset="UTF-8"></script> <H1>Billiards Sorting</H1> <p> Rotation is one of several popular pocket billiards games. It uses 15 balls numbered from 1 to 15, and set them up as illustrated in the following figure at the beginning of a game. (Note: the ball order is modified from real-world Rotation rules for simplicity of the problem.) </p> <pre> [ 1] [ 2][ 3] [ 4][ 5][ 6] [ 7][ 8][ 9][10] [11][12][13][14][15] </pre> <p> You are an engineer developing an automatic billiards machine. For the first step you had to build a machine that sets up the initial condition. This project went well, and finally made up a machine that could arrange the balls in the triangular shape. However, unfortunately, it could not place the balls in the correct order. </p> <p> So now you are trying to build another machine that fixes the order of balls by swapping them. To cut off the cost, it is only allowed to swap the ball #1 with its neighboring balls that are not in the same row. For example, in the case below, only the following pairs can be swapped: (1,2), (1,3), (1,8), and (1,9). </p> <pre> [ 5] [ 2][ 3] [ 4][ 1][ 6] [ 7][ 8][ 9][10] [11][12][13][14][15] </pre> <p> Write a program that calculates the minimum number of swaps required. </p> <H2>Input</H2> <p> The first line of each test case has an integer <var>N</var> (<var>1 \leq N \leq 5</var>), which is the number of the rows. </p> <p> The following <var>N</var> lines describe how the balls are arranged by the first machine; the <var>i</var>-th of them consists of exactly <var>i</var> integers, which are the ball numbers. </p> <p> The input terminates when <var>N</var> = 0. Your program must not output for this case. </p> <H2>Output</H2> <p> For each test case, print its case number and the minimum number of swaps. </p> <p> You can assume that any arrangement can be fixed by not more than 45 swaps. </p> <H2>Sample Input</H2> <pre> 2 3 2 1 4 9 2 4 8 5 3 7 1 6 10 0 </pre> <H2>Output for the Sample Input</H2> <pre> Case 1: 1 Case 2: 13 </pre>
p03538
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Select any integer <var>N</var> between <var>1000</var> and <var>2000</var> (inclusive), and any integer <var>K</var> not less than <var>1</var>, then solve the problem below.</p> <h4>Problem</h4> <p>We have <var>N</var> sheets of paper. Write <var>K</var> integers on each of them to satisfy the following conditions:</p> <ul> <li>Each integer written must be between <var>1</var> and <var>N</var> (inclusive).</li> <li>The <var>K</var> integers written on the same sheet must be all different.</li> <li>Each of the integers between <var>1</var> and <var>N</var> must be written on exactly <var>K</var> sheets.</li> <li>For any two sheet, there is exactly one integer that appears on both.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>There is no input in this problem.</p> </section> </div> <div class="part"> <section> <h3>Output</h3><p>In the first line, print <var>N</var> and <var>K</var> separated by a space.</p> <p>In the subsequent <var>N</var> lines, print your solution. The <var>i</var>-th of these lines must contain the <var>K</var> integers written on the <var>i</var>-th sheet, with spaces in between.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Output</h3><pre>3 2 1 2 2 3 3 1 </pre> <p>This is an example of a solution for <var>N = 3</var> and <var>K = 2</var>.</p> <p>Note that this output will be judged as incorrect, since the constraint on <var>N</var> is not satisfied.</p></section> </div> </span>
p03492
<span class="lang-en"> <p>Score : <var>800</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a tree with <var>N</var> vertices. The vertices are numbered <var>1</var> through <var>N</var>. The <var>i</var>-th edge (<var>1 \leq i \leq N - 1</var>) connects Vertex <var>x_i</var> and <var>y_i</var>. For vertices <var>v</var> and <var>w</var> (<var>1 \leq v, w \leq N</var>), we will define the distance between <var>v</var> and <var>w</var> <var>d(v, w)</var> as "the number of edges contained in the path <var>v</var>-<var>w</var>".</p> <p>A squirrel lives in each vertex of the tree. They are planning to move, as follows. First, they will freely choose a permutation of <var>(1, 2, ..., N)</var>, <var>p = (p_1, p_2, ..., p_N)</var>. Then, for each <var>1 \leq i \leq N</var>, the squirrel that lived in Vertex <var>i</var> will move to Vertex <var>p_i</var>.</p> <p>Since they like long travels, they have decided to maximize the total distance they traveled during the process. That is, they will choose <var>p</var> so that <var>d(1, p_1) + d(2, p_2) + ... + d(N, p_N)</var> will be maximized. How many such ways are there to choose <var>p</var>, modulo <var>10^9 + 7</var>?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 5,000</var></li> <li><var>1 \leq x_i, y_i \leq N</var></li> <li>The input 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>x_1</var> <var>y_1</var> <var>x_2</var> <var>y_2</var> <var>:</var> <var>x_{N - 1}</var> <var>y_{N - 1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the ways to choose <var>p</var> so that the condition is satisfied, modulo <var>10^9 + 7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 2 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>The maximum possible distance traveled by squirrels is <var>4</var>. There are three choices of <var>p</var> that achieve it, as follows:</p> <ul> <li><var>(2, 3, 1)</var></li> <li><var>(3, 1, 2)</var></li> <li><var>(3, 2, 1)</var></li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 1 2 1 3 1 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>11 </pre> <p>The maximum possible distance traveled by squirrels is <var>6</var>. For example, <var>p = (2, 1, 4, 3)</var> achieves it.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 1 2 1 3 1 4 2 5 2 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>36 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>7 1 2 6 3 4 5 1 7 1 5 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>396 </pre></section> </div> </span>
p01155
<H1><font color="#000">Problem A:</font> Ruins</H1> <p> In 1936, a dictator Hiedler who aimed at world domination had a deep obsession with <i>the Lost Ark</i>. A person with this ark would gain mystic power according to legend. To break the ambition of the dictator, ACM (the Alliance of Crusaders against Mazis) entrusted a secret task to an archeologist Indiana Johns. Indiana stepped into a vast and harsh desert to find the ark. </p> <p> Indiana finally found an underground treasure house at a ruin in the desert. The treasure house seems storing the ark. However, the door to the treasure house has a special lock, and it is not easy to open the door. </p> <p> To open the door, he should solve a problem raised by two positive integers <i>a</i> and <i>b</i> inscribed on the door. The problem requires him to find the minimum sum of squares of differences for all pairs of two integers adjacent in a sorted sequence that contains four positive integers <i>a</i><sub>1</sub>, <i>a</i><sub>2</sub> , <i>b</i><sub>1</sub> and <i>b</i><sub>2</sub> such that <i>a</i> = <i>a</i><sub>1</sub><i>a</i><sub>2</sub> and <i>b</i> = <i>b</i><sub>1</sub><i>b</i><sub>2</sub>. Note that these four integers does not need to be different. For instance, suppose 33 and 40 are inscribed on the door, he would have a sequence 3, 5, 8, 11 as 33 and 40 can be decomposed into 3 &times; 11 and 5 &times 8 respectively. This sequence gives the sum (5 - 3)<sup>2</sup> + (8 - 5)<sup>2</sup> + (11 - 8)<sup>2</sup> = 22, which is the smallest sum among all possible sorted sequences. This example is included as the first data set in the sample input. </p> <p> Once Indiana fails to give the correct solution, he will suffer a calamity by a curse. On the other hand, he needs to solve the problem as soon as possible, since many pawns under Hiedler are also searching for the ark, and they might find the same treasure house. He will be immediately killed if this situation happens. So he decided to solve the problem by your computer program. </p> <p> Your task is to write a program to solve the problem presented above. </p> <H2>Input</H2> <p> The input consists of a series of data sets. Each data set is given by a line that contains two positive integers not greater than 10,000. </p> <p> The end of the input is represented by a pair of zeros, which should not be processed. </p> <H2>Output</H2> <p> For each data set, print a single integer that represents the minimum square sum in a line. No extra space or text should appear. </p> <H2>Sample Input</H2> <pre> 33 40 57 144 0 0 </pre> <H2>Output for the Sample Input</H2> <pre> 22 94 </pre>
p03168
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Let <var>N</var> be a positive odd number.</p> <p>There are <var>N</var> coins, numbered <var>1, 2, \ldots, N</var>. For each <var>i</var> (<var>1 \leq i \leq N</var>), when Coin <var>i</var> is tossed, it comes up heads with probability <var>p_i</var> and tails with probability <var>1 - p_i</var>.</p> <p>Taro has tossed all the <var>N</var> coins. Find the probability of having more heads than tails.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>N</var> is an odd number.</li> <li><var>1 \leq N \leq 2999</var></li> <li><var>p_i</var> is a real number and has two decimal places.</li> <li><var>0 &lt; p_i &lt; 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>p_1</var> <var>p_2</var> <var>\ldots</var> <var>p_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the probability of having more heads than tails. The output is considered correct when the absolute error is not greater than <var>10^{-9}</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 0.30 0.60 0.80 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>0.612 </pre> <p>The probability of each case where we have more heads than tails is as follows:</p> <ul> <li>The probability of having <var>(Coin 1, Coin 2, Coin 3) = (Head, Head, Head)</var> is <var>0.3 × 0.6 × 0.8 = 0.144</var>;</li> <li>The probability of having <var>(Coin 1, Coin 2, Coin 3) = (Tail, Head, Head)</var> is <var>0.7 × 0.6 × 0.8 = 0.336</var>;</li> <li>The probability of having <var>(Coin 1, Coin 2, Coin 3) = (Head, Tail, Head)</var> is <var>0.3 × 0.4 × 0.8 = 0.096</var>;</li> <li>The probability of having <var>(Coin 1, Coin 2, Coin 3) = (Head, Head, Tail)</var> is <var>0.3 × 0.6 × 0.2 = 0.036</var>.</li> </ul> <p>Thus, the probability of having more heads than tails is <var>0.144 + 0.336 + 0.096 + 0.036 = 0.612</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 0.50 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0.5 </pre> <p>Outputs such as <code>0.500</code>, <code>0.500000001</code> and <code>0.499999999</code> are also considered correct.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 0.42 0.01 0.42 0.99 0.42 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0.3821815872 </pre></section> </div> </span>
p00197
<h1>最大公約数-ユークリッドの互除法</h1> <p> 最大公約数は、コンピュータ上で扱う数学には欠かせない要素です。最大公約 数を使うことで、計算の効率が大きく変動することもあります。最大公約数を求めるアルゴリズムのひとつが「ユークリッドの互除法」です。その処理 の流れを以下に示します。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_gcd"><br/> </center> <br/> <p> 例えば 1071 と 1029 の場合、1071 を <var>X</var> 、1029 を <var>Y</var> に代入して、<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1071 &divide; 1029 の余りは 42 、<var>X</var> に 42 を代入して <var>X</var> と <var>Y</var> を入れ替える。 (1 ステップ)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1029 &divide; 42 の余りは 21 、<var>X</var> に 21 を代入して <var>X</var> と <var>Y</var> を入れ替える。 (2 ステップ)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 42 &divide; 21 の余りは 0 、<var>X</var> に 0 を代入して <var>X</var> と <var>Y</var> を入れ替える。 (3 ステップ)<br> <var>Y</var> が 0 になったので、この時の <var>X</var> が最大公約数となる。よって最大公約数は 21 。 </p> <p> このように、たったの 3 ステップで 1071 と 1029 の最大公約数を求めることが出来ました。ユークリッドの互除法は約数を出して比較していく方法に比べ、圧倒的に早く結果を出してくれます。 </p> <p> 2つの整数を入力とし、ユークリッドの互除法を用いて最大公約数を求め、その最大公約数と計算にかかったステップ数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 複数のデータセットの並びが入力として与えられます。入力の終わりはゼロふたつの行で示されます。 各データセットとして2つの整数 <var>a, b</var> (2 &le; <var>a, b</var> &le; 2<sup>31</sup>-1) が1行に与えられます。 </p> <p> データセットの数は 1000 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、入力された2つの整数の最大公約数と、計算にかかったユークリッドの互除法のステップ数を空白区切りで1行に出力します。 </p> <H2>Sample Input</H2> <pre> 1071 1029 5 5 0 0 </pre> <H2>Output for the Sample Input</H2> <pre> 21 3 5 1 </pre>
p02450
<h1>Permutation Enumeration</h1> <p> For given an integer $n$, print all permutations of $\{1, 2, ..., n\}$ in lexicographic order. </p> <h2>Input</h2> <p> An integer $n$ is given in a line. </p> <h2>Output</h2> <p> Print each permutation in a line in order. Separate adjacency elements by a space character. </p> <h2>Constraints</h2> <ul> <li>$1 \leq n \leq 9$</li> </ul> <h2>Sample Input 1</h2> <pre> 2 </pre> <h2>Sample Output 1</h2> <pre> 1 2 2 1 </pre> <h2>Sample Input 2</h2> <pre> 3 </pre> <h2>Sample Output 2</h2> <pre> 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 </pre>
p02000
<h1>E: 凸凹数列</h1> <h2>問題</h2> <p> 長さ $N$ の数列 $A$ が与えられる。$A$ の $i$ 項目 は $A_i$ である。 あなたは、この数列に対して以下の操作を行うことができる。 </p> <p> <ul> <li>$1 \leq i \leq N - 1$ なる整数 i を選ぶ。 $A_i$ の値と $A_{i + 1}$ の値を入れ替える。</li> </ul> </p> <p> $A$ を凸凹数列にするために必要な操作の最小回数を求めよ。 </p> <p> 以下の条件を満たす長さ $N$ の数列を凸凹数列と定義する。 <ul> <li>$1 < i < N$ を満たす任意の $i$ について、 $A_{i + 1}, A_{i - 1} > A_i$ または $A_{i + 1}, A_{i - 1} < A_i$ を満たす。<br> 直感的に言えば、 $1,\ 10,\ 2,\ 30,\ \dots (10,\ 1,\ 30,\ 2,\ \dots )$ のような、増加、減少、増加…(減少、増加、減少…)を繰り返す数列である。</li> </ul> </p> <h2>制約</h2> <ul> <li>入力値は全て整数である。</li> <li>$3 \leq N \leq 10^5$</li> <li>$i \neq j$ ならば $A_i \neq A_j$</li> <li>$-10^9 \leq A_i \leq 10^9$</li> </ul> <h2>入力形式</h2> <p> 入力は以下の形式で与えられる。 </p> <p> $N$<br> $A_1 \dots A_N$<br> </p> <h2>出力</h2> <p>凸凹数列にするために必要な操作の最小回数を出力せよ。また、末尾に改行も出力せよ。 </p> <h2>サンプル</h2> <h3>サンプル入力 1</h3> <pre> 5 1 2 3 4 5 </pre> <h3>サンプル出力 1</h3> <pre> 2 </pre> <p>$2$ と $3$ 、 $4$ と $5$ に操作を行えば、 $1\ 3\ 2\ 5\ 4$ という凸凹数列になる。</p> <h3>サンプル入力 2</h3> <pre> 3 1 2 3 </pre> <h3>サンプル出力 2</h3> <pre> 1 </pre> <p>$1$ と $2$ に操作を行う、あるいは $2$ と $3$ に操作を行うことで、凸凹数列になる。</p> <h3>サンプル入力 3</h3> <pre> 12 5 9 1 38 100 -23 4 16 -2 -10 -17 8 </pre> <h3>サンプル出力 3</h3> <pre> 2 </pre>
p00894
<H1><font color="#000">Problem A: </font> Gift from the Goddess of Programming </H1> <p> The goddess of programming is reviewing a thick logbook, which is a yearly record of visitors to her holy altar of programming. The logbook also records her visits at the altar. </p> <p> The altar attracts programmers from all over the world because one visitor is chosen every year and endowed with a gift of miracle programming power by the goddess. The endowed programmer is chosen from those programmers who spent the longest time at the altar during the goddess's presence. There have been enthusiastic visitors who spent very long time at the altar but failed to receive the gift because the goddess was absent during their visits. </p> <p> Now, your mission is to write a program that finds how long the programmer to be endowed stayed at the altar during the goddess's presence. </p> <H2>Input</H2> <p> The input is a sequence of datasets. The number of datasets is less than 100. Each dataset is formatted as follows. </p> <p> <i>n<br> M</i><sub>1</sub><i>/D</i><sub>1</sub><i> h</i><sub>1</sub>:<i>m</i><sub>1</sub><i>e</i><sub>1</sub><i> p</i><sub>1</sub><br> <i>M</i><sub>2</sub>/D</i><sub>2</sub><i> h</i><sub>2</sub>:<i>m</i><sub>2</sub><i>e</i><sub>2</sub><i> p</i><sub>2</sub><br> .<br> .<br> .<br> <i>M</i><sub>n</sub>/D</i><sub>n</sub><i> h</i><sub>n</sub>:<i>m</i><sub>n</sub><i>e</i><sub>n</sub><i> p</i><sub>n</sub><br> </p> <p> The first line of a dataset contains a positive even integer, n &le; 1000, which denotes the number of lines of the logbook. This line is followed by <i>n</i> lines of space-separated data, where <i>M</i><sub>i</sub><i>/D</i><sub>i</sub> identifies the month and the day of the visit, <i>h</i><sub>i</sub>:<i>m</i><sub>i</sub> represents the time of either the entrance to or exit from the altar, <i>e</i><sub>i</sub> is either I for entrance, or O for exit, and <i>p</i><sub>i</sub> identifies the visitor. </p> <p> All the lines in the logbook are formatted in a fixed-column format. Both the month and the day in the month are represented by two digits. Therefore April 1 is represented by 04/01 and not by 4/1. The time is described in the 24-hour system, taking two digits for the hour, followed by a colon and two digits for minutes, 09:13 for instance and not like 9:13. A programmer is identified by an ID, a unique number using three digits. The same format is used to indicate entrance and exit of the goddess, whose ID is 000. </p> <p> All the lines in the logbook are sorted in ascending order with respect to date and time. Because the altar is closed at midnight, the altar is emptied at 00:00. You may assume that each time in the input is between 00:01 and 23:59, inclusive. </p> <p> A programmer may leave the altar just after entering it. In this case, the entrance and exit time are the same and the length of such a visit is considered 0 minute. You may assume for such entrance and exit records, the line that corresponds to the entrance appears earlier in the input than the line that corresponds to the exit. You may assume that at least one programmer appears in the logbook. </p> <p> The end of the input is indicated by a line containing a single zero. </p> <H2>Output</H2> <p> For each dataset, output the total sum of the blessed time of the endowed programmer. The blessed time of a programmer is the length of his/her stay at the altar during the presence of the goddess. The endowed programmer is the one whose total blessed time is the longest among all the programmers. The output should be represented in minutes. Note that the goddess of programming is not a programmer. </p> <H2>Sample Input</H2> <pre> 14 04/21 09:00 I 000 04/21 09:00 I 001 04/21 09:15 I 002 04/21 09:30 O 001 04/21 09:45 O 000 04/21 10:00 O 002 04/28 09:00 I 003 04/28 09:15 I 000 04/28 09:30 I 004 04/28 09:45 O 004 04/28 10:00 O 000 04/28 10:15 O 003 04/29 20:00 I 002 04/29 21:30 O 002 20 06/01 09:00 I 001 06/01 09:15 I 002 06/01 09:15 I 003 06/01 09:30 O 002 06/01 10:00 I 000 06/01 10:15 O 001 06/01 10:30 I 002 06/01 10:45 O 002 06/01 11:00 I 001 06/01 11:15 O 000 06/01 11:30 I 002 06/01 11:45 O 001 06/01 12:00 O 002 06/01 12:15 I 000 06/01 12:30 I 002 06/01 12:45 O 000 06/01 13:00 I 000 06/01 13:15 O 000 06/01 13:30 O 002 06/01 13:45 O 003 0 </pre> <H2>Output for the Sample Input</H2> <pre> 45 120 </pre>
p01786
<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>G - Proportional Representation</h2> <h3>Problem Statement</h3> <p> An election selecting the members of the parliament in JAG Kingdom was held. The only system adopted in this country is the party-list proportional representation. In this system, each citizen votes for a political party, and the number of seats a party wins will be proportional to the number of votes it receives. Since the total number of seats in the parliament is an integer, of course, it is often impossible to allocate seats exactly proportionaly. In JAG Kingdom, the following method, known as the D'Hondt method, is used to determine the number of seats for each party. </p> <p> Assume that every party has an unlimited supply of candidates and the candidates of each party are ordered in some way. To the $y$-th candidate of a party which received $x$ votes, assign the value $\dfrac{x}{y}$. Then all the candidates are sorted in the decreasing order of their assigned values. The first $T$ candidates are considered to win, where $T$ is the total number of seats, and the number of seats a party win is the number of its winning candidates. </p> <p> The table below shows an example with three parties. The first party received $40$ votes, the second $60$ votes, and the third $30$ votes. If the total number of seats is $T = 9$, the first party will win $3$ seats, the second $4$ seats, and the third $2$ seats. </p> <center><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_Proportional_Representation" /></center> <p> When selecting winning candidates, ties are broken by lottery; any tied candidates will have a chance to win. For instance, in the example above, if $T = 5$ then two candidates tie for the value $20$ and there are two possible outcomes: </p> <ul> <li> The first party wins $2$ seats, the second $2$ seats, and the third $1$ seat. </li> <li> The first party wins $1$ seat, the second $3$ seats, and the third $1$ seat. </li> </ul> <p>You have just heard the results of the election on TV. Knowing the total number of valid votes and the number of seats each party won, you wonder how many votes each party received. </p> <p> Given $N$, $M$, and $S_i$ ($1 \le i \le M$), denoting the total number of valid votes, the number of parties, and the number of seats the $i$-th party won, respectively, your task is to determine for each party the minimum and the maximum possible number of votes it received. Note that for some cases there might be no such situation with the given $N$, $M$, and $S_i$. </p> <h3>Input</h3> <p> The first line of the input contains two integers $N$ ($1 \le N \le 10^9$) and $M$ ($1 \le M \le 30{,}000$), where $N$ is the total number of valid votes and $M$ is the number of parties. $M$ lines follow, the $i$-th of which contains a single integer $S_i$ ($0 \le S_i \le 30{,}000$), representing the number of seats the $i$-th party won. You can assume that there exists $i$ with $S_i \ne 0$. </p> <h3>Output</h3> <p> If there is no situation with the given $N$, $M$, and $S_i$, display the word "impossible". Otherwise, output $M$ lines, each containing two integers. The first integer and the second integer in the $i$-th line should be the minimum and the maximum possible number of votes the $i$-th party received, respectively. </p> <h3>Sample Input 1</h3> <pre>10 2 2 1</pre> <h3>Output for the Sample Input 1</h3> <pre>5 7 3 5</pre> <h3>Sample Input 2</h3> <pre>5 6 0 2 0 2 6 0</pre> <h3>Output for the Sample Input 2</h3> <pre>0 0 1 1 0 0 1 1 3 3 0 0</pre> <h3>Sample Input 3</h3> <pre>2000 5 200 201 202 203 204</pre> <h3>Output for the Sample Input 3</h3> <pre>396 397 397 399 399 401 401 403 403 404</pre> <h3>Sample Input 4</h3> <pre>15 10 1 1 1 1 1 1 1 1 1 13</pre> <h3>Output for the Sample Input 4</h3> <pre>impossible</pre> <h3>Sample Input 5</h3> <pre>1000000000 9 12507 16653 26746 21516 29090 10215 28375 21379 18494</pre> <h3>Output for the Sample Input 5</h3> <pre>67611619 67619582 90024490 90033301 144586260 144597136 116313392 116323198 157257695 157269050 55221291 55228786 153392475 153403684 115572783 115582561 99976756 99985943</pre>
p03241
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given integers <var>N</var> and <var>M</var>.</p> <p>Consider a sequence <var>a</var> of length <var>N</var> consisting of positive integers such that <var>a_1 + a_2 + ... + a_N</var> = <var>M</var>. Find the maximum possible value of the greatest common divisor of <var>a_1, a_2, ..., a_N</var>.</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 10^5</var></li> <li><var>N \leq M \leq 10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum possible value of the greatest common divisor of a sequence <var>a_1, a_2, ..., a_N</var> that satisfies the condition.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 14 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>Consider the sequence <var>(a_1, a_2, a_3) = (2, 4, 8)</var>. Their greatest common divisor is <var>2</var>, and this is the maximum value.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 123 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 1000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>10000 </pre></section> </div> </span>
p02903
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a square grid with <var>H</var> rows and <var>W</var> columns. Snuke wants to write <var>0</var> or <var>1</var> in each of the squares. Here, all of the following conditions have to be satisfied:</p> <ul> <li>For every row, the smaller of the following is <var>A</var>: the number of <var>0</var>s contained in the row, and the number of <var>1</var>s contained in the row. (If these two numbers are equal, “the smaller” should be read as “either”.)</li> <li>For every column, the smaller of the following is <var>B</var>: the number of <var>0</var>s contained in the column, and the number of <var>1</var>s contained in the column.</li> </ul> <p>Determine if these conditions can be satisfied by writing <var>0</var> or <var>1</var> in each of the squares. If the answer is yes, show one way to fill the squares so that the conditions are satisfied.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq H,W \leq 1000</var></li> <li><var>0 \leq A</var></li> <li><var>2 \times A \leq W</var></li> <li><var>0 \leq B</var></li> <li><var>2 \times B \leq H</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>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If the conditions cannot be satisfied by writing <var>0</var> or <var>1</var> in each of the squares, print <var>-1</var>.</p> <p>If the conditions can be satisfied, print one way to fill the squares so that the conditions are satisfied, in the following format:</p> <pre><var>s_{11}s_{12}\cdots s_{1W}</var> <var>s_{21}s_{22}\cdots s_{2W}</var> <var>\vdots</var> <var>s_{H1}s_{H2}\cdots s_{HW}</var> </pre> <p>Here <var>s_{ij}</var> is the digit written in the square at the <var>i</var>-th row from the top and the <var>j</var>-th column from the left in the grid.</p> <p>If multiple solutions exist, printing any of them will be accepted.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>100 010 001 </pre> <p>Every row contains two <var>0</var>s and one <var>1</var>, so the first condition is satisfied. Also, every column contains two <var>0</var>s and one <var>1</var>, so the second condition is satisfied.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 5 2 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>01010 </pre></section> </div> </span>
p03611
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an integer sequence of length <var>N</var>, <var>a_1,a_2,...,a_N</var>.</p> <p>For each <var>1≤i≤N</var>, you have three choices: add <var>1</var> to <var>a_i</var>, subtract <var>1</var> from <var>a_i</var> or do nothing.</p> <p>After these operations, you select an integer <var>X</var> and count the number of <var>i</var> such that <var>a_i=X</var>.</p> <p>Maximize this count by making optimal choices.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≤N≤10^5</var></li> <li><var>0≤a_i&lt;10^5 (1≤i≤N)</var></li> <li><var>a_i</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>a_2</var> .. <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum possible number of <var>i</var> such that <var>a_i=X</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>7 3 1 4 1 5 9 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>For example, turn the sequence into <var>2,2,3,2,6,9,2</var> and select <var>X=2</var> to obtain <var>4</var>, the maximum possible count.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 0 1 2 3 4 5 6 7 8 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre></section> </div> </span>
p03304
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Let us define the <em>beauty</em> of a sequence <var>(a_1,... ,a_n)</var> as the number of pairs of two adjacent elements in it whose absolute differences are <var>d</var>. For example, when <var>d=1</var>, the beauty of the sequence <var>(3, 2, 3, 10, 9)</var> is <var>3</var>.</p> <p>There are a total of <var>n^m</var> sequences of length <var>m</var> where each element is an integer between <var>1</var> and <var>n</var> (inclusive). Find the beauty of each of these <var>n^m</var> sequences, and print the average of those values.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>0 \leq d &lt; n \leq 10^9</var></li> <li><var>2 \leq m \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>m</var> <var>d</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the average of the beauties of the sequences of length <var>m</var> where each element is an integer between <var>1</var> and <var>n</var>. The output will be judged correct if the absolute or relative error is at most <var>10^{-6}</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1.0000000000 </pre> <p>The beauty of <var>(1,1,1)</var> is <var>0</var>.<br/> The beauty of <var>(1,1,2)</var> is <var>1</var>.<br/> The beauty of <var>(1,2,1)</var> is <var>2</var>.<br/> The beauty of <var>(1,2,2)</var> is <var>1</var>.<br/> The beauty of <var>(2,1,1)</var> is <var>1</var>.<br/> The beauty of <var>(2,1,2)</var> is <var>2</var>.<br/> The beauty of <var>(2,2,1)</var> is <var>1</var>.<br/> The beauty of <var>(2,2,2)</var> is <var>0</var>.<br/> The answer is the average of these values: <var>(0+1+2+1+1+2+1+0)/8=1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1000000000 180707 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0.0001807060 </pre></section> </div> </span>
p01339
<H1><font color="#000">Problem A: </font>Alien's Counting</H1> <p> Natsuki and her friends were taken to the space by an alien and made friends with a lot of aliens. During the space travel, she discovered that aliens’ hands were often very different from humans’. Generally speaking, in a kind of aliens, there are <i>N</i> fingers and <i>M</i> bend rules on a hand. Each bend rule describes that a finger A always bends when a finger B bends. However, this rule does not always imply that the finger B bends when the finger A bends. </p> <p> When she were counting numbers with the fingers, she was anxious how many numbers her alien friends can count with the fingers. However, because some friends had too complicated rule sets, she could not calculate those. Would you write a program for her? </p> <H2>Input</H2> <p> <i>N M</i><br> <i>S</i><sub>1</sub> <i>D</i><sub>1</sub><br> <i>S</i><sub>2</sub> <i>D</i><sub>2</sub><br> .<br> .<br> .<br> <i>S</i><sub><i>M</i></sub> <i>D</i><sub><i>M</I></sub><br> </p> <p> The first line contains two integers <i>N</i> and <i>M</i> (1 &le; <i>N</i> &le; 1000, 0 &le; <i>M</i> &le; 1000) in this order. The following <i>M</i> lines mean bend rules. Each line contains two integers <i>S<sub>i</sub></i> and <i>D<sub>i</sub></i> in this order, which mean that the finger <i>D<sub>i</sub></i> always bends when the finger <i>S<sub>i</sub></i> bends. Any finger appears at most once in <i>S</i>. </p> <H2>Output</H2> <p> Calculate how many numbers her alien friends can count with the fingers. Print the answer modulo 1000000007 in a line. </p> <H2>Sample Input 1</H2> <pre> 5 4 2 3 3 4 4 3 5 4 </pre> <H2>Output for the Sample Input 1</H2> <pre> 10 </pre> <H2>Sample Input 2</H2> <pre> 5 5 1 2 2 3 3 4 4 5 5 1 </pre> <H2>Output for the Sample Input 2</H2> <pre> 2 </pre> <H2>Sample Input 3</H2> <pre> 5 0 </pre> <H2>Output for the Sample Input 3</H2> <pre> 32 </pre>
p02846
<span class="lang-en"> <p>Score: <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>Takahashi and Aoki are training for long-distance races in an infinitely long straight course running from west to east.</p> <p>They start simultaneously at the same point and moves as follows <strong>towards the east</strong>:</p> <ul> <li>Takahashi runs <var>A_1</var> meters per minute for the first <var>T_1</var> minutes, then runs at <var>A_2</var> meters per minute for the subsequent <var>T_2</var> minutes, and alternates between these two modes forever.</li> <li>Aoki runs <var>B_1</var> meters per minute for the first <var>T_1</var> minutes, then runs at <var>B_2</var> meters per minute for the subsequent <var>T_2</var> minutes, and alternates between these two modes forever.</li> </ul> <p>How many times will Takahashi and Aoki meet each other, that is, come to the same point? We do not count the start of the run. If they meet infinitely many times, report that fact.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3> <ul> <li><var>1 \leq T_i \leq 100000</var></li> <li><var>1 \leq A_i \leq 10^{10}</var></li> <li><var>1 \leq B_i \leq 10^{10}</var></li> <li><var>A_1 \neq B_1</var></li> <li><var>A_2 \neq B_2</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>T_1</var> <var>T_2</var> <var>A_1</var> <var>A_2</var> <var>B_1</var> <var>B_2</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3> <p>Print the number of times Takahashi and Aoki will meet each other.<br/> If they meet infinitely many times, print <code>infinity</code> instead. </p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>1 2 10 10 12 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 </pre> <p>They will meet just once, <var>\frac{4}{3}</var> minutes after they start, at <var>\frac{40}{3}</var> meters from where they start.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>100 1 101 101 102 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>infinity </pre> <p>They will meet <var>101, 202, 303, 404, 505, 606, ...</var> minutes after they start, that is, they will meet infinitely many times.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>12000 15700 3390000000 3810000000 5550000000 2130000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>113 </pre> <p>The values in input may not fit into a <var>32</var>-bit integer type.</p></section> </div> </span>
p03754
<span class="lang-en lang-child hidden-lang"> <div class="part"> Max Score: $800$ Points <br/> <section> <h3>Problem Statement</h3> There is an undirected connected graph with $N$ vertices and $N-1$ edges. The <var>i</var>-th edge connects <var>u_i</var> and <var>v_i</var>.<br/> <br/> E869120 the coder moves in the graph as follows:<br/> <ul> <li>He move to adjacent vertex, but he can't a visit vertex two or more times.</li> <li>He ends move when there is no way to move.</li> <li>Otherwise, he moves randomly. (equal probability) If he has $p$ way to move this turn, he choose each vertex with $1/p$ probability.</li> </ul> <div align="left" class="img-nocaption"> <img src="https://atcoder.jp/img/s8pc-4/5973dad11843e9098d9225dd431c9084.png" width="500"/> </div> Calculate the expected value of the number of turns, when E869120 starts from vertex <var>i</var>, for all <var>i (1 ≤ i ≤ N)</var>. <br/> </section> </div> <div class="io-style"> <div class="part"> <section> <h3>Input</h3> The input is given from standard input in the following format. <br/> <br/> <pre> <var>N</var> <var>u_1</var> <var>v_1</var> <var>u_2</var> <var>v_2</var> : <var>u_{N-1}</var> <var>v_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3> In <var>i</var>-th <var>(1 ≤ i ≤ N)</var> line, print the expected vaule of the number of turns E869120 moves.<br/> The relative error or absolute error of output should be within <var>10^{-6}</var>. <br/> </section> <section> <h3>Constraints</h3> <ul> <li>$1 \le N \le 150,000$</li> <li>The graph is connected.</li> </ul> </section> <section> <h3>Subtasks</h3> <p>Subtask 1 [ $190$ points ]</p> <ul> <li>There is no vertex which degree is more than 2.</li> <li>This means the graph looks like a list.</li> </ul> <br/> <p>Subtask 2 [ $220$ points ]</p> <ul> <li><var>1 ≤ N ≤ 1000</var>.</li> </ul> <br/> <p>Subtask 3 [ $390$ points ]</p> <ul> <li>There are no additional constraints.</li> </ul> <br/> </section> </div> </div> <div class="part"> <section> <h3>Sample Input 1</h3> <pre> 4 1 2 2 3 2 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3> <pre> 2.0 1.0 2.0 2.0 </pre> </section> </div> <div class="part"> <h3>Sample Input 2</h3> <pre> 4 1 2 2 4 4 3 </pre> </div></span>
p01769
<h1 id="d-hopping-hearts-こころぴょんぴょん">D : Hopping Hearts / こころぴょんぴょん</h1> <h2 id="問題文">問題文</h2> <p><var>N</var> 羽のうさぎが長さ <var>L&minus;1</var> の平均台の上にいる。 <var>i</var> 番目のうさぎの初期位置は整数 <var>x_i</var>であり、 <var>0 &le; x_{i} \lt x_{i+1} &le; L&minus;1</var> を満たす。座標は右に進むほど大きくなる。任意の <var>i</var> 番目のうさぎは、任意の回数だけ、ちょうど距離 <var>a_i</var> だけ右方向にジャンプ(すなわち、<var>x_i</var> から <var>x_i+a_i</var> へ移動)することができる。ただし別のうさぎを飛び越えること、<var>&minus;1</var> 以下または <var>L</var> 以上の位置に入ることはできない。また、同時にジャンプしていられるのは高々 <var>1</var> 羽であり、ある座標に存在できるうさぎの数も高々 </var>1</var> 羽である。</p> <p>初期状態から始めてジャンプを任意の回数繰り返したあとの <var>x_{0}, …, x_{N&minus;1}</var> の状態として、あり得るものは何通りあるか。<var>1\,000\,000\,007</var> で割った余りで求めよ。</p> <h2 id="入力">入力</h2> <p>入力は以下の形式で与えられる。</p> <pre><var>N</var> <var>L</var> <var>x_{0}</var> <var>…</var> <var>x_{N&minus;1}</var> <var>a_{0}</var> <var>…</var> <var>a_{N&minus;1}</var> </pre> <h2 id="制約">制約</h2> <ul> <li>入力はすべて整数である</li> <li><var>1 \&le; N \&le; 5\,000</var></li> <li><var>N \&le; L \&le; 5\,000</var></li> <li><var>0 \&le; x_{i} \lt x_{i+1} \&le; L&minus;1</var></li> <li><var>0 \&le; a_{i} \&le; L&minus;1</var></li> </ul> <h2 id="出力">出力</h2> <p>答えを1行で出力せよ。</p> <h2 id="サンプル">サンプル</h2> <h3 id="サンプル入力1">サンプル入力1</h3> <pre>1 3 0 1 </pre> <h3 id="サンプル出力1">サンプル出力1</h3> <pre>3 </pre> <p>1/0でうさぎがいる/いないを表現すれば、100, 010, 001 の <var>3</var> 通りである。</p> <h3 id="サンプル入力2">サンプル入力2</h3> <pre>2 4 0 1 1 2 </pre> <h3 id="サンプル出力2">サンプル出力2</h3> <pre>4 </pre> <p>1100, 1001, 0101, 0011 の <var>4</var> 通りである。</p> <h3 id="サンプル入力3">サンプル入力3</h3> <pre>10 50 0 1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1 </pre> <h3 id="サンプル出力3">サンプル出力3</h3> <pre>272278100 </pre> <p>二項係数 <var>C(50,10) = 10\,272\,278\,170</var> であり、それを <var>1\,000\,000\,007</var> で割ったあまりは <var>272\,278\,100</var> である。</p>
p01293
<H1><font color="#000">Problem A:</font> Whist</H1> <p> <i>Whist</i> is a game played by four players with a standard deck of playing cards. The players seat around a table, namely, in north, east, south, and west. This game is played in a team-play basis: the players seating opposite to each other become a team. In other words, they make two teams we could call the north-south team and the east-west team. </p> <p> Remember that the standard deck consists of 52 cards each of which has a rank and a suit. The rank indicates the strength of the card and is one of the following: 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king, and ace (from the lowest to the highest). The suit refers to the type of symbols printed on the card, namely, spades, hearts, diamonds, and clubs. The deck contains exactly one card for every possible pair of a rank and a suit, thus 52 cards. </p> <p> One of the four players (called a dealer) shuffles the deck and deals out all the cards face down, one by one, clockwise from the player left to him or her. Each player should have thirteen cards. Then the last dealt card, which belongs to the dealer, is turned face up. The suit of this card is called trumps and has a special meaning as mentioned below. </p> <p> A deal of this game consists of thirteen tricks. The objective for each team is winning more tricks than another team. The player left to the dealer leads the first trick by playing one of the cards in his or her hand. Then the other players make their plays in the clockwise order. They have to play a card of the suit led if they have one; they can play any card otherwise. The trick is won by the player with the highest card of the suit led if no one plays a trump, or with the highest trump otherwise. The winner of this trick leads the next trick, and the remaining part of the deal is played similarly. After the thirteen tricks have been played, the team winning more tricks gains a score, one point per trick in excess of six. </p> <p> Your task is to write a program that determines the winning team and their score for given plays of a deal. </p> <H2>Input</H2> <p> The input is a sequence of datasets. Each dataset corresponds to a single deal and has the following format: </p> <p> <i>Trump</i><br> <i>Card</i><sub>N,1</sub> <i>Card</i><sub>N,2</sub> ... <i>Card</i><sub>N,13</sub><br> <i>Card</i><sub>E,1</sub> <i>Card</i><sub>E,2</sub> ... <i>Card</i><sub>E,13</sub><br> <i>Card</i><sub>S,1</sub> <i>Card</i><sub>S,2</sub> ... <i>Card</i><sub>S,13</sub><br> <i>Card</i><sub>W,1</sub> <i>Card</i><sub>W,2</sub> ... <i>Card</i><sub>W,13</sub><br> </p> <p> <i>Trump</i> indicates the trump suit. <i>Card</i><sub>N,<i>i</i></sub>, <i>Card</i><sub>E,<i>i</i></sub>, <i>Card</i><sub>S,<i>i</i></sub>, and <i>Card</i><sub>W,<i>i</i></sub> denote the card played in the <i>i</i>-th trick by the north, east, south, and west players respectively. Each card is represented by two characters; the first and second character indicates the rank and the suit respectively. </p> <p> The rank is represented by one of the following characters: ‘<span>2</span>’, ‘<span>3</span>’, ‘<span>4</span>’, ‘<span>5</span>’, ‘<span>6</span>’, ‘<span>7</span>’, ‘<span>8</span>’, ‘<span>9</span>’, ‘<span>T</span>’ (10), ‘<span>J</span>’ (jack), ‘<span>Q</span>’ (queen), ‘<span>K</span>’ (king), and ‘<span>A</span>’ (ace). The suit is represented by one of the following characters: ‘<span>S</span>’ (spades), ‘<span>H</span>’ (hearts), ‘<span>D</span>’ (diamonds), and ‘<span>C</span>’ (clubs). </p> <p> You should assume the cards have been dealt out by the west player. Thus the first trick is led by the north player. Also, the input does not contain any illegal plays. </p> <p> The input is terminated by a line with “<span>#</span>”. This is not part of any dataset and thus should not be processed. </p> <H2>Output</H2> <p> For each dataset, print the winner and their score of the deal in a line, with a single space between them as a separator. The winner should be either “<span>NS</span>” (the north-south team) or “<span>EW</span>” (the east-west team). No extra character or whitespace should appear in the output. </p> <H2>Sample Input</H2> <pre> H 4C 8H QS 5D JD KS 8S AH 6H 7H 3S 7S 6D TC JC JS KD AC QC QD 2H QH 3H 3C 7C 4D 6C 9C AS TD 5H 6S 5S KH TH AD 9S 8D 2D 8C 5C 2S 7D KC 4S TS JH 4H 9H 2C 9D 3D D 8D 9D 9S QS 4H 5H JD JS 9H 6S TH 6H QH QD 9C 5S 7S 7H AC 2D KD 6C 3D 8C TC 7C 5D QC 3S 4S 3H 3C 6D KS JC AS 5C 8H TS 4D 4C 8S 2S 2H KC TD JH 2C AH 7D AD KH # </pre> <H2>Output for the Sample Input</H2> <pre> EW 1 EW 2 </pre> <p> The winners of the tricks in the first dataset are as follows (in the order of tricks): east, north, south, east, south, north, west, north, east, west, east, east, north. </p>
p00528
<h2>現代的な屋敷(Modern Mansion)</h2> <p> あなたは,ある大きな屋敷に迷い込んでしまった.この屋敷は正方形の部屋が東西南北に格子状に,東西方向に <var>M</var> 列,南北方向に <var>N</var> 行の合計 <var>M &times; N</var> 個並んだ構造をしている.西から <var>x</var> 列目(1 &le; <var>x</var> &le; <var>M</var>),南から <var>y</var> 行目(1 &le; <var>y</var> &le; <var>N</var>) にある部屋を(<var>x</var>, <var>y</var>) で表す. </p> <p> 東西南北に隣り合う 2 部屋の間は,壁の中央にある扉により結ばれている.それぞれの扉は,閉じていて通行不可能な状態か,開いていて通行可能な状態のいずれかにある.扉が開いているとき,それらの部屋の中央間を移動するのに 1 分間かかる.また,いくつかの部屋の中央にはスイッチがあり,スイッチを 1 分間押し続けると,屋敷内のすべての扉の開閉の状態が切り替わる. </p> <p> 今,東西に隣り合う 2 部屋を結ぶすべての扉は閉じていて,南北に隣り合う 2 部屋を結ぶすべての扉は開いている.あなたは今部屋 (1, 1) の中央にいて,部屋 (<var>M</var>, <var>N</var>) の中央まで最短時間で移動したい. </p> <h3>課題</h3> <p> 屋敷の大きさ <var>M</var>, <var>N</var> および,スイッチのある <var>K</var> 個の部屋の位置 (<var>X<sub>1</sub></var>, <var>Y<sub>1</sub></var>), (<var>X<sub>2</sub></var>, <var>Y<sub>2</sub></var>),..., (<var>X<sub>K</sub></var>, <var>Y<sub>K</sub></var>) が与えられる.東西に隣り合う 2 部屋を結ぶすべての扉は閉じていて,南北に隣り合う 2 部屋を結ぶすべての扉は開いている状態から始めて,部屋 (1, 1) の中央から部屋 (<var>M</var>, <var>N</var>) の中央まで移動するのに最短で何分かかるかを求めるプログラムを作成せよ.ただし,部屋 (<var>M</var>, <var>N</var>) に辿り着くことができないときはそれを指摘せよ. </p> <h3>制限</h3> <ul> <li>2 &le; <var>M</var> &le; 100 000 &nbsp;&nbsp;&nbsp;&nbsp; 屋敷の東西方向の部屋の個数</li> <li>2 &le; <var>N</var> &le; 100 000 &nbsp;&nbsp;&nbsp;&nbsp;屋敷の南北方向の部屋の個数</li> <li>1 &le; <var>K</var> &le; 200 000 &nbsp;&nbsp;&nbsp;&nbsp;スイッチのある部屋の個数</li> <li>1 &le; <var>X<sub>i</sub></var> &le; <var>M</var> &nbsp;&nbsp;&nbsp;&nbsp;スイッチのある部屋の東西方向の位置</li> <li>1 &le; <var>Y<sub>i</sub></var> &le; <var>N</var> &nbsp;&nbsp;&nbsp;&nbsp;スイッチのある部屋の南北方向の位置</li> </ul> <h3>入力</h3> <p> 標準入力から以下のデータを読み込め. </p> <ul> <li> 1 行目には,整数 <var>M, N, K</var> が空白を区切りとして書かれている.<var>M</var> は屋敷の東西方向の部屋の個数,<var>N</var> は屋敷の南北方向の部屋の個数,<var>K</var> はスイッチのある部屋の個数を表す.</li> <li> 続く <var>K</var> 行のうちの <var>i</var> 行目(1 &le; <var>i</var> &le; <var>K</var>) には,整数 <var>X<sub>i</sub></var>, <var>Y<sub>i</sub></var> が空白を区切りとして書かれている.これは,部屋(<var>X<sub>i</sub></var>, <var>Y<sub>i</sub></var>) の中央にスイッチがあることを表す.<var>K</var> 個の組(<var>X<sub>1</sub></var>, <var>Y<sub>1</sub></var>), (<var>X<sub>2</sub></var>, <var>Y<sub>2</sub></var>),..., (<var>X<sub>K</sub></var>, <var>Y<sub>K</sub></var>) は互いに異なる.</li> </ul> <h3>出力</h3> <p> 標準出力に,移動に最短で何分かかるかを表す整数を 1 行で出力せよ.ただし,部屋(<var>M</var>, <var>N</var>) に辿り着くことができないときは代わりに整数 -1 を出力せよ. </p> <h3>採点基準</h3> <ul> <li>採点用データのうち,配点の20%分については,<var>M</var> &le; 1 000, <var>N</var> &le; 1 000 を満たす.</li> <li>採点用データのうち,配点の30%分については,<var>K</var> &le; 2 000 を満たす.</li> <li>採点用データのうち,配点の50%分については,これら 2 つの条件の少なくとも一方を満たす.また,これら 2 つの条件の両方を満たすような採点用データはない.</li> </ul> <h3>入出力例</h3> <h3>入力例 1</h3> <pre> 3 2 1 1 2 </pre> <h3> 出力例 1</h3> <pre> 4 </pre> <p> この例では,以下の行動によって 4 分間で部屋(1, 1) の中央から部屋(3, 2) の中央へ移動することができ,これが最短である. </p> <ol> <li> 部屋(1, 2) の中央へ移動する.</li> <li> 部屋(1, 2) の中央のスイッチを押す.</li> <li> 部屋(2, 2) の中央へ移動する.</li> <li> 部屋(3, 2) の中央へ移動する.</li> </ol> <p> このときの屋敷の様子が以下の図に表されている.図では右方向が東,上方向が北であり,×印はあなたの位置,○印はスイッチを表す. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_JOI2012_2012_ho_3_1"> </center> <br> <br> <h3>入力例 2</h3> <pre> 3 2 1 2 1 </pre> <h3>出力例 2</h3> <pre> -1 </pre> <p> この例では,あなたは部屋(3, 2) に辿り着くことができない. </p> <br> <h3>入力例 3</h3> <pre> 8 9 15 3 1 3 2 3 7 3 8 1 1 4 5 4 3 5 6 5 8 6 3 6 2 7 5 8 9 8 6 8 5 </pre> <h3>出力例 3</h3> <pre> 25 </pre> <p> この例では,最初の屋敷の様子は以下の図のようになっている.部屋(1, 1) や部屋(<var>M</var>, <var>N</var>) の中央にスイッチがある可能性もあることに注意せよ. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_JOI2012_2012_ho_3_2"> </center> <br> <div class="source"> <p class="source"> 問題文と自動審判に使われるデータは、<a href="http://www.ioi-jp.org">情報オリンピック日本委員会</a>が作成し公開している問題文と採点用テストデータです。 </p> </div>
p00178
<H1>テトリス</H1> <p> テトリスは、落ちてくるブロックを盤面上に並べて消すゲームです。ここでは、それを少しアレンジしたゲームを考えましょう。 </p> <p> このゲームの盤面の大きさは横 5 コマで、出現するブロックがすべて入るだけの高さがあります。落ちてくるブロックは直線状で、横向き、縦向きの 2 種類があり、長さは 1 から 5 までの 5 種類です。 </p> <p> 以下に例を示します。Step(イ) からStep(ホ)までの図はブロックが落ちて消えていく様子を表したものです。 Step(イ)から順にStep(ロ)、Step(ハ)と順に進んでいきます。 </p> <p> ブロックを落とすときに、Step(イ)のようにブロックのどこかが下に積んであるブロックに引っかかったときには、Step(ロ)のように落ちたブロックはその場所で止まります。また、ブロックを落とした結果、盤面の横一行の全てのコマにブロックが詰まった場合には、Step(ニ)で示されるように、その行のブロックが消えます。この後、消えた行の上にあるブロックが、そのままの形で1行下にしずみます(Step(ホ))。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_pck200806_1"> <br/><br/> </center> <p> 1 ゲームは 1000 個以内のブロックが順に落ちてくることとします。例えば、落ちるブロックの長さが順番に横向き 4 コマ, 横向き 3 コマ, 縦向き 2 コマ, 縦向き 3 コマで、落ちる場所が左端から 1 コマ目、1 コマ目、4 コマ目、5 コマ目であった場合には、下図のStep(イ)~(ト)のように落ち、最後に残るブロックは 2 コマになります。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_pck200806_2"> <br/><br/> </center> <p> 順番に落ちてくるブロックの情報を入力とし、全てのブロックが落ちた時に残るコマ数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 複数のデータセットの並びが入力として与えられます。入力の終わりはゼロひとつの行で示されます。各データセットは以下の形式で与えられます。 </p> <pre> <var>n</var> <var>d<sub>1</sub></var> <var>p<sub>1</sub></var> <var>q<sub>1</sub></var> <var>d<sub>2</sub></var> <var>p<sub>2</sub></var> <var>q<sub>2</sub></var> : <var>d<sub>n</sub></var> <var>p<sub>n</sub></var> <var>q<sub>n</sub></var> </pre> <p> 1行目にブロックの数 <var>n</var> (1 &le; <var>n</var> &le; 1000) が与えられます。続く <var>n</var> 行に <var>i</var> 個目のブロックの向き <var>d<sub>i</sub></var> (1 または2)、ブロックの長さ <var>p<sub>i</sub></var> (1 &le; <var>p<sub>i</sub></var> &le; 5)、ブロックの位置 <var>q<sub>i</sub></var> が与えられます。ブロックの向き <var>d<sub>i</sub></var> は 1 が横向きを、2 が縦向きを表します。ブロックの位置 <var>q<sub>i</sub></var> は盤面上の左端から1 ~ 5 までの整数とし、横向きのブロックの場合は左端のコマの落ちる位置とします。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセット毎に最後に残るブロックの占めるコマ数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 4 1 4 1 1 3 1 2 2 4 2 3 5 1 1 5 1 7 2 2 2 1 4 1 2 1 3 1 4 1 1 1 1 2 5 5 1 4 2 0 </pre> <H2>Output for the Sample Input</H2> <pre> 2 0 6 </pre>
p02145
<h1>Problem I: Shiritori</h1> <h2>Problem</h2> <p> ここに文字列のリストがある。息抜きに、しりとりをして遊ぶことにしよう。しりとりは、以下のルールで行われる。 </p> <ol> <li>まず最初に、リストの中から好きな文字列を一つ選び、その文字列をリストから除外する。</li> <li>続いて、一つ前に選んだ文字列の最後の一文字が、最初の一文字である文字列をリストから一つ選ぶ。</li> <li>選んだ文字列をリストから除外する。</li> </ol> <p> この後、2., 3.を交互に繰り返すことになる。</p> <p> さて、このしりとりを2.でリストから選べる文字列がなくなるまで続けたとしよう。 このときに、最後に選んだ文字列の「最後の一文字」としてあり得る文字をすべて列挙したい。 </p> <h2>Input</h2> <pre> $N$ $s_1$ $s_2$ $s_3$ : $s_N$ </pre> <p>一行目に、文字列の数$N$が与えられる。<br> 続く$N$行に、文字列のリストが与えられる。 <br> </p> <h2>Constraints</h2> <p>入力は以下の条件を満たす。</p> <ul> <li>$1 \le N \lt 10^4$</li> <li>$1 \le |s_i| \lt 100$</li> <li>$s_i \neq s_j (i \neq j)$</li> <li>文字列には、英小文字のみ含まれる</li> </ul> <h2>Output</h2> <p>最後に選んだ文字列の「最後の一文字」としてあり得る文字をaからzの順で辞書順で昇順に出力せよ。</p> <h2>Sample Input 1</h2> <pre> 5 you ate my hum toast </pre> <h2>Sample Output 1</h2> <pre> e t u </pre> <p>この時、以下のような場合がアウトプット例となる。</p> <ul> <li>ate(1つの文字列のみ) </li> <li>toast</li> <li>hum - my - you</li> </ul> <p>また、m,yで終わるパターンは存在しない。</p> <h2>Sample Input 2</h2> <pre> 7 she sells sea shells by the seashore </pre> <h2>Sample Output 2</h2> <pre> a e y </pre>
p00482
<H1>JOI 旗 (JOI Flag)</H1> <h2>問題</h2> <p> 情報オリンピック日本委員会では,今年の日本情報オリンピック (JOI) を宣伝するために, JOI のロゴをモチーフにした旗を作ることになった.旗は「良い旗」でなければならない.「良い旗」とは,アルファベットの J, O, I のいずれかの文字を,縦 M 行,横 N 列の長方形状に並べたもので, J, O, I が以下の図のように (すなわち,J の右隣に O が,その J の下隣に I が) 並んでいる箇所が旗の少なくとも 1 か所にあるものである. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_0559_1"> </center> <p> 以下の図に,「良い旗」の例を 2 つ示す. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_0559_2"> </center> <p> 以下の図に,「良い旗」ではない例を 2 つ示す. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_0559_3"> </center> <p> いま M, N の値,および旗の一部の場所について J, O, I のどの文字にするかが決まっており,入力としてその情報が与えられる.考えられる「良い旗」は何通りあるかを計算し,その数を 100000 (=10<sup>5</sup>) で割った余りを出力するプログラムを作成せよ. </p> <h2>入力</h2> <p> 入力は 1 + M 行からなる. </p> <p> 1 行目には旗の大きさを表す 2 つの整数 M, N (2 &le; M &le; 20, 2 &le; N &le; 20) が空白で区切られて書かれている. </p> <p> 1 + i 行目 (1 &le; i &le; M) には,N 文字からなる文字列が書かれている.各文字は J, O, I, ? のいずれかであり, j 文字目 (1 &le; j &le; N) が J, O, I のいずれかである場合は i 行 j 列の場所の文字がそれぞれ J, O, I に決まっていること, ? である場合はまだ決まっていないことを表す. </p> <h2>出力</h2> <p> 考えられる「良い旗」の個数を 100000 (=10<sup>5</sup>) で割った余りを 1 行で出力せよ. </p> <h2>入出力例</h2> <h3>入力例 1</h3> <pre> 2 3 ??O IIJ </pre> <h3>出力例 1</h3> <pre> 4 </pre> <p> 入力例 1 においては,以下の図の 4 通りの「良い旗」が考えられる. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_0559_4"> </center> <h3>入力例 2</h3> <pre> 2 2 ?? ?? </pre> <h3>出力例 2</h3> <pre> 3 </pre> <br> <h3>入力例 3</h3> <pre> 3 3 ??I ??? O?J </pre> <h3>出力例 3</h3> <pre> 53 </pre> <br> <h3>入力例 4</h3> <pre> 5 4 JOI? ???? ???? ???? ?JOI </pre> <h3>出力例 4</h3> <pre> 28218 </pre> <p> 入力例 4 においては,「良い旗」は 2428218 通り考えられるので,それを 100000 (=10<sup>5</sup>) で割った余りである 28218 を出力する. </p> <div class="source"> <p class="source"> 問題文と自動審判に使われるデータは、<a href="http://www.ioi-jp.org">情報オリンピック日本委員会</a>が作成し公開している問題文と採点用テストデータです。 </p> </div>
p02391
<H1>Small, Large, or Equal</H1> <p> Write a program which prints small/large/equal relation of given two integers <var>a</var> and <var>b</var>. </p> <H2>Input</H2> <p> Two integers <var>a</var> and <var>b</var> separated by a single space are given in a line. </p> <H2>Output</H2> <p> For given two integers <var>a</var> and <var>b</var>, print </p> <pre> a < b </pre> <p> if <var>a</var> is less than <var>b</var>, </p> <pre> a > b </pre> <p> if <var>a</var> is greater than <var>b</var>, and </p> <pre> a == b </pre> <p> if <var>a</var> equals to <var>b</var>. </p> <h2>Constraints</h2> <ul> <li> -1000 &le; <var>a</var>, <var>b</var> &le; 1000</li> </ul> <H2>Sample Input 1</H2> <pre> 1 2 </pre> <H2>Sample Output 1</H2> <pre> a < b </pre> <H2>Sample Input 2</H2> <pre> 4 3 </pre> <H2>Sample Output 2</H2> <pre> a > b </pre> <H2>Sample Input 3</H2> <pre> 5 5 </pre> <H2>Sample Output 3</H2> <pre> a == b </pre>
p00656
<h1>Problem J: BD Shelf</h1> <p> Remember the boy called Jack. He likes anime, especially programs broadcasting in midnight or early morning. In his room, there is a big shelf and it is kept organized well. And there are anime BD/DVDs which he watched or gave up because of overlapping of broadcasting time with other animes. His money is tight, but he succeeded to get <i>W</i> hundreds dollars because of an unexpected event. And he came up with enhancing the content of his BD shelf. </p> <p> He is troubled about the usage of the unexpected income. He has a good judge for anime to choose the anime he can enjoy because he is a great anime lover. Therefore, first of all, he listed up expectable animes and assigned each anime a value that represents "a magnitude of expectation". Moreover Jack tagged a mysterious string M for each anime. However Jack did not tell you the meaning of the string M. </p> <p> Your task is to write a program that reads a string <i>X</i> and that maximizes the sum of magnitudes of expectation when Jack bought BD/DVD boxes of anime with the budget of <i>W</i> hundreds dollars. Here, Jack must buy anime's BD/DVD box that have a string <i>X</i> as a sub-string of the string <i>M</i> assigned the anime. If he can not buy any boxes, output -1. </p> <p> A sequence of string <i>X</i> is given in order in the input as query. Assume two strings <i>X<sub>1</sub></i> and <i>X<sub>2</sub></i> are given as string <i>X</i>, and <i>X<sub>1</sub></i> is the prefix of <i>X<sub>2</sub></i>. If <i>X<sub>1</sub></i> is given in the input preceding <i>X<sub>2</sub></i>, you must not buy an anime which has the minimum magnitude of expectation among animes extracted by <i>X<sub>1</sub></i> in the calculation for <i>X<sub>2</sub></i>. And vice versa (<i>X<sub>1</sub></i> is the prefix of <i>X<sub>2</sub></i> and if <i>X<sub>2</sub></i> is given in the input preceding <i>X<sub>1</sub></i>, ...) . For example, If three strings "a", "abc" and "ab" are given in this order in the input, In the processing for string "abc", you must not extract an anime which has the minimum magnitude of expectation among animes extracted by the processing of the string "a". In the processing for string "ab", you must not extract an anime has minimum magnitude of expectation among animes extracted by the processing of the string "abc" and "a". </p> <h2>Input</h2> <p> Input consists of multiple datasets.<br> A dataset is given in the following format. <p> <pre> <i>N</i> <i>W</i> <i>string<sub>1</sub></i> <i>expectation<sub>1</sub></i> <i>price<sub>1</sub></i> <i>string<sub>2</sub></i> <i>expectation<sub>2</sub></i> <i>price<sub>2</sub></i> ... <i>string<sub>N</sub></i> <i>expectation<sub>N</sub></i> <i>price<sub>N</sub></i> <i>Q</i> <i>query<sub>1</sub></i> <i>query<sub>2</sub></i> ... <i>query<sub>Q</sub></i> </pre> <p> <i>N</i> is an integer that represents the number of anime. Following <i>N</i> lines contains the information of each anime.<br> <i>string<sub>i</sub></i>(1&le;<i>i</i>&le;<i>N</i>) is a string consists of lowercase letters that represents string <i>M</i> assigned <i>i</i>-th anime by Jack. No two characters in this string are same.<br> <i>expectation<sub>i</sub></i>(1&le;<i>i</i>&le;<i>N</i>) is a natural number that represents a magnitude of expectation of the <i>i</i>-th anime.<br> <i>price<sub>i</sub></i>(1&le;<i>i</i>&le;<i>N</i>) is a natural number that represents a price(cost) in hundreds to buy the <i>i</i>-th anime. <i>Q</i> is an integer that represents the number of queries.<br> <i>query<sub>i</sub></i>(1&le;<i>i</i>&le;<i>Q</i>) is a string consists of lowercase letters. This is a string <i>X</i> described in the problem statement.<br> <i>N</i>=<i>W</i>=0 shows the end of the input. </p> <h2>Constraints</h2> <p> There are 4 test cases. This is given by the following order.<br> In the first testcase, 4 datasets satisfy <i>N</i>&le;10 and <i>Q</i>&le;10, 6 datasets satisfy <i>N</i>&le;5000 and <i>Q</i>&le;5000.<br> In the 2nd, 3rd and 4th testcase, each satisfies <i>N</i>&le;20000 and <i>Q</i>&le;20000.<br> The time limit for this problem is the limit for each testcase.<br> 1&le;<i>N</i>&le;20000<br> 1&le;<i>W</i>&le;20<br> 1&le;(the length of <i>string<sub>i</sub></i>(1&le;<i>i</i>&le;<i>N</i>))&le;10<br> 1&le;<i>price<sub>i</sub></i>(1&le;<i>i</i>&le;<i>N</i>)&le;20<br> 1&le;<i>expectation<sub>i</sub></i>(1&le;<i>i</i>&le;<i>N</i>)&le;7000000<br> 1&le;<i>Q</i>&le;20000<br> 1&le;(the length of <i>query<sub>i</sub></i>(1&le;<i>i</i>&le;<i>Q</i>))&le;5<br> It is assured that all values for expectation are different. </p> <h2>Output</h2> <p> For each query, output an integer <i>S</i> on a line that represents the maximum sum of magnitudes of expectation in the following format. <pre> <i>S</i> </pre> </p> <h2>Sample Input</h2> <pre> 3 4 abc 1 1 abc 10 1 abc 100 1 3 a abc ab 3 4 ab 1 1 ab 10 1 abc 100 1 3 abc ab a 3 4 abc 1 1 abc 10 1 abc 100 1 3 ab ab ab 8 20 abcdef 100 2 bcdef 200 1 cfghj 300 3 ksjirto 400 6 ksitoew 500 2 qwertyl 600 2 kjhbvc 700 2 edfghucb 800 1 10 ks cd hj e a g h j i a 0 0 </pre> <h2>Output for the Sample Input</h2> <pre> 111 110 100 100 11 10 111 110 100 900 300 300 2200 100 1100 1500 1400 900 -1 </pre>
p01944
<h2>G: ほぼ無限グリコ - Almost Infinite Glico</h2> <h3>問題</h3> <p><var>N</var> 個のマスが円環状に並んでいるフィールドがあります。 <var>i</var> 番目 <var>(1 \leq i \leq N-1)</var> のマスの <var>1</var> つ先は、 <var>i+1</var> 番目のマスになります。ただし、 <var>i = N</var> の場合、その <var>1</var> つ先は <var>1</var> 番目のマスになります。</p> <p>あなたが最初にいるマスは <var>1</var> 番目のマスです。そこから以下のルールに従って、じゃんけんを <var>K</var> 回続けて行います。このとき、あなたと相手は特殊なじゃんけんを会得しているので、じゃんけんの勝ち方が <var>M</var> 通りあります。</p> <ul> <li> 相手とじゃんけんを行い、あなたが相手に勝ったとき、今出した手、すなわち勝ち方 <var>i</var> に応じたマス数 <var>p_i</var> だけ進みます。負けたときはその場に留まります。</li> </ul> <p><var>K</var> 回のじゃんけんを終えたあとに、あなたが <var>i</var> 番目のマスにいる場合の数を <var>1,000,000,007</var> で割った余りを、それぞれのマスについて求めてください。ここで <var>2</var> つの場合が異なるとは、 <var>K</var> 回のじゃんけんの各回のうち負けた回が一度でも異なる、または勝ち方が異なる回が一度でも存在することを言います。勝ち方も区別することに注意してください。また、それぞれの勝ち方について、発生する可能性が全くないものは存在しないこととします。</p> <h3>入力形式</h3> <p>入力は <var>1 + M</var> 行与えられる。</p> <pre> <var>N</var> <var>M</var> <var>K</var> <var>p_1</var> <var>...</var> <var>p_M</var> </pre> <p> <var>1</var> 行目では、マスの数 <var>N</var> 、 じゃんけんの勝ち方の数 <var>M</var> 、 じゃんけんを行う回数 <var>K</var> が与えられる。 続いて <var>M</var> 行与えられる。 <var>i+1</var> 行目では、 <var>i</var> 番目の勝ち方をした時に進めるマスの数 <var>p_i</var> が与えられる。 </p> <h3>制約</h3> <ul> <li> <var>1 \leq N \leq 8 \times 10^2</var></li> <li> <var>1 \leq M \leq 10^4</var></li> <li> <var>1 \leq K \leq 10^9</var></li> <li> <var>1 \leq p_i \leq 10^9</var></li> </ul> <h3>出力形式</h3> <p>出力は <var>N</var> 行からなる。</p> <p><var>i</var> 行目では、 <var>K</var> 回のじゃんけんを終えたあとにあなたが <var>i</var> 番目のマスにいる場合の数を出力せよ。</p> <h3>入力例1</h3> <pre> 10 3 2 3 6 6 </pre> <h3>出力例1</h3> <pre> 1 0 4 2 0 0 5 0 0 4 </pre> <p>一般的なグリコです。この入力において、じゃんけんは <var>2</var> 回行われます。</p> <p>例えば、 <var>2</var> 回じゃんけんをした後に <var>1</var> 番目のマスにいるためには、全てのじゃんけんで負けなければいけません。これ以外に <var>1</var> 番目のマスで終わる組み合わせは存在しないため、 <var>1</var> 通りです。</p> <p><var>2</var> 回じゃんけんをした後に <var>4</var> 番目のマスにいるためには、</p> <ul> <li><var>1</var> 回目のじゃんけんで <var>1</var> 番目の勝ち方で勝つ → <var>2</var> 回目のじゃんけんで負ける</li> <li><var>1</var> 回目のじゃんけんで負ける → <var>2</var> 回目のじゃんけんで <var>1</var> 番目の勝ち方で勝つ</li> </ul> <p>の <var>2</var> 通りがあります。順番が異なるならば、異なる場合として扱われることに注意してください。</p> <h3>入力例2</h3> <pre> 5 1 103 4 </pre> <h3>出力例2</h3> <pre> 355272559 885073297 355272559 607675915 607675915 </pre> <p><var>1,000,000,007</var> で割った余りを出力することに注意してください。</p>
p03979
<span class="lang-en"> <p>Score : <var>150</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p> There are some goats on a grid with <var>H</var> rows and <var>W</var> columns. Alice wants to put some fences at some cells where goats do not exist so that no goat can get outside the grid. Goats can move in the four directions, that is, up, down, right and left. Goats can not move onto the cells where fences are placed. If a goat exists at one of the outermost cells in the grid, it can move outside. Goats do not move until all fences are placed. Find the minimum number of fences to be placed. </p> </section> </div> <div class="part"> <section> <h3>Constraints</h3> <ul> <li> <var>1 \leq H \leq 100</var> </li> <li> <var>1 \leq W \leq 100</var> </li> <li> There is at least one goat on the given grid. </li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3> <p>The input is given from the Standart Input in the following format:</p> <pre> <var>H</var> <var>W</var> <var>S_1</var> : <var>S_H</var> </pre> <p> The <var>j</var>-th <var>(1 \leq j \leq W)</var> character in <var>S_i</var> <var>(1 \leq i \leq H)</var> represents whether a goat exists at the cell located at row <var>i</var> and column <var>j</var>. Character <code>.</code> represents there is no goat, and <code>X</code> represents there is one goat at the cell. </p> </section></div></div></span>
p00206
<H1>旅行はいつ?</H1> <p> あなたは友人と旅行に行きたいと考えています。ところが、浪費癖のある友人はなかなか旅行費用を貯めることができません。友人が今の生活を続けていると、旅行に行くのはいつになってしまうか分かりません。そこで、早く旅行に行きたいあなたは、友人が計画的に貯蓄することを助けるプログラムを作成することにしました。 </p> <p> 友人のある月のお小遣いを <var>M</var> 円、その月に使うお金を <var>N</var> 円とすると、その月は (<var>M</var> - <var>N</var>) 円貯蓄されます。毎月の収支情報 <var>M</var> 、 <var>N</var> を入力とし、貯蓄額が旅行費用 <var>L</var> に達するのにかかる月数を出力するプログラムを作成してください。ただし、12 ヶ月を過ぎても貯蓄額が旅行費用に達しなかった場合はNA と出力してください。 </p> <H2>Input</H2> <p> 複数のデータセットの並びが入力として与えられます。 入力の終わりはゼロひとつの行で示されます。 各データセットは以下の形式で与えられます。 </p> <pre> <var>L</var> <var>M<sub>1</sub></var> <var>N<sub>1</sub></var> <var>M<sub>2</sub></var> <var>N<sub>2</sub></var> : <var>M<sub>12</sub></var> <var>N<sub>12</sub></var> </pre> <p> 1 行目に旅行費用 <var>L</var> (1 &le; <var>L</var> &le; 1000000, 整数) が与えられます。続く 12 行に、<var>i</var> 月目の収支情報 <var>M<sub>i</sub></var>, <var>N<sub>i</sub></var> (0 &le; <var>M<sub>i</sub>, N<sub>i</sub></var> &le; 100000, <var>N<sub>i</sub></var> &le; <var>M<sub>i</sub></var>, 整数) が与えられます。 </p> <p> データセットの数は 1000 を超えません。 </p> <H2>Output</H2> <p> 入力データセットごとに、貯蓄額が旅行費用に達するのにかかる月数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 10000 5000 3150 5000 5000 0 0 5000 1050 5000 3980 5000 210 5000 5000 5000 5000 0 0 5000 2100 5000 2100 5000 2100 29170 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 70831 0 </pre> <H2>Output for the Sample Input</H2> <pre> 6 NA </pre>
p03580
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p><var>N</var> cells are arranged in a row. Some of them may contain tokens. You are given a string <var>s</var> that consists of <code>0</code>s and <code>1</code>s. If the <var>i</var>-th character of <var>s</var> is <code>1</code>, the <var>i</var>-th cell (from left) contains a token. Otherwise, it doesn't contain a token.</p> <p>Snuke wants to perform the following operation as many times as possible. In each operation, he chooses three consecutive cells. Let's call the cells <var>X, Y, Z</var> from left to right. In order for the operation to be valid, both <var>X</var> and <var>Z</var> must contain tokens and <var>Y</var> must not contain a token. Then, he removes these two tokens and puts a new token on <var>Y</var>.</p> <p>How many operations can he perform if he performs operations in the optimal way?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 500,000</var></li> <li><var>|s| = N</var></li> <li>Each character in <var>s</var> is either <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>N</var> <var>s</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>7 1010101 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>For example, he can perform two operations in the following way:</p> <ul> <li>Perform an operation on the last three cells. Now the string that represents tokens becomes <code>1010010</code>.</li> <li>Perform an operation on the first three cells. Now the string that represents tokens becomes <code>0100010</code>.</li> </ul> <p>Note that the choice of operations matters. For example, if he chooses three cells in the middle first, he can perform no more operations.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>50 10101000010011011110001001111110000101010111100110 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>10 </pre></section> </div> </span>
p01047
<h1>Problem K: Manhattan Warp Machine 2</h1> <h2>Problem</h2> <p> とある宇宙では、3次元の格子点上に星が存在し、宇宙人達はマンハッタンワープ装置という装置を使い、星間を移動している。<br> このワープ装置には、<var>N</var>個のボタンが付いており、ボタン<var>i</var>を押すと、現在いる星からのマンハッタン距離が<var>d<sub>i</sub></var>である任意の星にワープすることができる。<br> ワープ装置は改良され、コストがかからずに移動出来る。<br> 今、(0, 0, 0)の星にいるある宇宙人がある<var>M</var>個の星を訪れたいと考えている。<br> <var>M</var>個の星を全て訪れるのに最短で何回ワープをする必要があるか答えよ。<br> もし、1つでも訪れることが不可能な星がある場合は-1を出力せよ。<br> <var>M</var>個の星は好きな順番に訪れることが出来、全て訪れた後に(0, 0, 0)に戻って来る必要は無い。<br> 3次元上の全ての格子点には必ず星が存在する。<br> 点(<var>x<sub>1</sub></var>, <var>y<sub>1</sub></var>, <var>z<sub>1</sub></var>)と点(<var>x<sub>2</sub></var>, <var>y<sub>2</sub></var>, <var>z<sub>2</sub></var>)間のマンハッタン距離は| <var>x<sub>1</sub></var> - <var>x<sub>2</sub></var> | + | <var>y<sub>1</sub></var> - <var>y<sub>2</sub></var> | + | <var>z<sub>1</sub></var> - <var>z<sub>2</sub></var> |で表される。 </p> <h2>Input</h2> <pre> <var>N</var> <var>M</var> <var>d<sub>1</sub></var> ... <var>d<sub>N</sub></var> <var>x<sub>1</sub></var> <var>y<sub>1</sub></var> <var>z<sub>1</sub></var> ... <var>x<sub>M</sub></var> <var>y<sub>M</sub></var> <var>z<sub>M</sub></var> </pre> <p> 入力は全て整数で与えられる。<br> 1行目に<var>N</var>, <var>M</var>が与えられる。<br> 2行目に移動可能なマンハッタン距離<var>d<sub>i</sub></var>が空白区切りで与えられる。<br> 3行目以降の<var>M</var>行に、<var>M</var>個の訪れたい星の格子点の座標(<var>x<sub>i</sub></var>, <var>y<sub>i</sub></var>, <var>z<sub>i</sub></var>)が1行ずつ空白区切りで与えられる。<br> </p> <h2>Constraints</h2> <p> 入力は以下の条件を満たす。 </p> <ul> <li>1 &le; <var>N</var> &le; 15</li> <li>1 &le; <var>M</var> &le; 15</li> <li>1 &le; <var>d<sub>i</sub></var> &le; 3×10<sup>5</sup></li> <li>-10<sup>5</sup> &le; <var>x<sub>i</sub></var>, <var>y<sub>i</sub></var>, <var>z<sub>i</sub></var> &le; 10<sup>5</sup></li> <li>与えられるマンハッタン距離<var>d<sub>i</sub></var>は全て異なる。</li> <li>与えられる格子点の座標は全て異なる。</li> </ul> <h2>Output</h2> <p> <var>M</var>個の星全てを訪れるのにかかる最短ワープ回数を1行に出力せよ。 訪れることが不可能な星がある場合は-1を出力せよ。 </p> <h2>Sample Input 1</h2> <pre> 2 1 1 2 5 0 0 </pre> <h2>Sample Output 1</h2> <pre> 3 </pre> <h2>Sample Input 2</h2> <pre> 2 2 9 3 3 0 0 3 9 0 </pre> <h2>Sample Output 2</h2> <pre> 2 </pre> <h2>Sample Input 3</h2> <pre> 1 1 4 3 0 0 </pre> <h2>Sample Output 3</h2> <pre> -1 </pre>
p01417
<script src="./IMAGE/varmath.js" charset="UTF-8"></script> <H1>Calender Colors</H1> <p> Taro is a member of a programming contest circle. In this circle, the members manage their schedules in the system called <i>Great Web Calender</i>. </p> <p> Taro has just added some of his friends to his calendar so that he can browse their schedule on his calendar. Then he noticed that the system currently displays all the schedules in only one color, mixing the schedules for all his friends. This is difficult to manage because it's hard to tell whose schedule each entry is. </p> <p> But actually this calender system has the feature to change the color of schedule entries, based on the person who has the entries. So Taro wants to use that feature to distinguish their plans by color. </p> <p> Given the colors Taro can use and the number of members, your task is to calculate the subset of colors to color all schedule entries. The colors are given by &quot;Lab color space&quot;. </p> <p> In Lab color space, the distance between two colors is defined by the square sum of the difference of each element. Taro has to pick up the subset of colors that maximizes the sum of distances of all color pairs in the set. </p> <H2>Input</H2> <p> The input is like the following style. </p> <p> <var>N</var> <var>M</var><br/> <var>L_{0}</var> <var>a_{0}</var> <var>b_{0}</var><br/> <var>L_{1}</var> <var>a_{1}</var> <var>b_{1}</var><br/> <var>...</var><br/> <var>L_{N-1}</var> <var>a_{N-1}</var> <var>b_{N-1}</var><br/> </p> <p> The first line contains two integers <var>N</var> and <var>M</var> (<var>0 \leq M \leq N \leq 20</var>), where <var>N</var> is the number of colors in the input, and <var>M</var> is the number of friends Taro wants to select the colors for. Each of the following <var>N</var> lines contains three decimal integers <var>L</var>(<var>0.0 \leq L \leq 100.0</var>), <var>a</var>(<var>-134.0 \leq a \leq 220.0</var>) and <var>b</var>(<var>-140.0 \leq b \leq 122.0</var>) which represents a single color in Lab color space. </p> <H2>Output</H2> <p> Output the maximal of the total distance. The output should not contain an error greater than <var>10^{-5}</var>. </p> <H2>Sample Input 1</H2> <pre> 3 2 0 0 0 10 10 10 100 100 100 </pre> <H2>Output for the Sample Input 1</H2> <pre> 30000.00000000000000000000 </pre> <H2>Sample Input 2</H2> <pre> 5 3 12.0 15.0 9.0 10.0 -3.0 2.2 3.5 6.8 9.0 2.1 4.4 5.9 1.2 4.0 -5.4 </pre> <H2>Output for the Sample Input 2</H2> <pre> 1003.44000000000005456968 </pre> <H2>Sample Input 3</H2> <pre> 2 1 1.0 1.0 1.0 0.0 0.0 0.0 </pre> <H2>Output for the Sample Input 3</H2> <pre> 0.00000000000000000000 </pre>
p01102
<h3><u>Almost Identical Programs</u></h3> <p> The programming contest named <i>Concours de Programmation Comtemporaine Interuniversitaire</i> (CPCI) has a judging system similar to that of ICPC; contestants have to submit correct outputs for two different inputs to be accepted as a correct solution. Each of the submissions should include the program that generated the output. A pair of submissions is judged to be a correct solution when, in addition to the correctness of the outputs, they include an identical program. </p> <p> Many contestants, however, do not stop including a different version of their programs in their second submissions, after modifying a single string literal in their programs representing the input file name, attempting to process different input. The organizers of CPCI are exploring the possibility of showing a special error message for such <em>close</em> submissions, indicating contestants what's wrong with such submissions. Your task is to detect such close submissions. </p> <h3>Input</h3> <p> The input consists of at most 100 datasets, each in the following format. </p> <pre> <i>s</i><sub>1</sub> <i>s</i><sub>2</sub> </pre> <p> Each of <i>s</i><sub>1</sub> and <i>s</i><sub>2</sub> is a string written in a line, with the length between 1 and 200, inclusive. They are the first and the second submitted programs respectively. A program consists of lowercase letters (<tt>a</tt>, <tt>b</tt>, ..., <tt>z</tt>), uppercase letters (<tt>A</tt>, <tt>B</tt>, ..., <tt>Z</tt>), digits (<tt>0</tt>, <tt>1</tt>, ..., <tt>9</tt>), double quotes (<tt>"</tt>), and semicolons (<tt>;</tt>). When double quotes occur in a program, there are always even number of them. </p> <p> The end of the input is indicated by a line containing one '<tt>.</tt>' (period). </p> <h3>Output</h3> <p> For each dataset, print the judge result in a line. </p> <p> If the given two programs are identical, print <tt>IDENTICAL</tt>. If two programs differ with only one corresponding string literal, print <tt>CLOSE</tt>. Otherwise, print <tt>DIFFERENT</tt>. A string literal is a possibly empty sequence of characters between an odd-numbered occurrence of a double quote and the next occurrence of a double quote. </p> <h3>Sample Input</h3> <pre>print"hello";print123 print"hello";print123 read"B1input";solve;output; read"B2";solve;output; read"C1";solve;output"C1ans"; read"C2";solve;output"C2ans"; """""""" """42""""" slow"program" fast"code" "super"fast"program" "super"faster"program" X"" X I"S""CREAM" I"CE""CREAM" 11"22"11 1"33"111 . </pre> <h3>Output for the Sample Input</h3> <pre>IDENTICAL CLOSE DIFFERENT CLOSE DIFFERENT DIFFERENT DIFFERENT CLOSE DIFFERENT </pre>
p01552
<h1>問題名 YAML</h1> <p>YAML (YAML Ain't Markup Language)とは、オブジェクトを文字列で表現する形式の一つです。 </p> <p>YAMLのサブセットで表されたオブジェクトと、プロパティを指定するクエリが与えられるので、指定されたプロパティの値を答えてください。 </p> <h3>YAMLのサブセット</h3> <p>YAML のサブセットは、次のような拡張 BNF 記法で表される構文規則に従います。 </p><pre>yaml: mapping(0) mapping(n): mapping-item(n) | mapping-item(n) mapping(n) mapping-item(n): indent(n) key ':' ' ' string '\n' | indent(n) key ':' '\n' mapping(m) (ただしm&gt;n) key: [a-z0-9]+ (※英字小文字または数字からなる1文字以上の文字列) string: [a-z0-9 ]+ (※英字小文字または数字またはスペースからなる1文字以上の文字列) indent(0): &quot;&quot; (※空文字列) indent(n+1): ' ' indent(n) (※スペースをn+1個並べた文字列) </pre> <p>'\n'は改行文字を表します。 </p> <p><var>mapping(n)</var> はオブジェクトを表し、<var>mapping(n)</var> に含まれる <var>mapping-item(n)</var> は、オブジェクトに含まれるプロパティを表します。 </p> <p><code>mapping-item(n): indent(n) key ':' ' ' string '\n'</code> は、 <var>key</var> で表されるプロパティの値がstringで表される文字列であることを示します。 </p> <p><code>mapping-item(n): indent(n) key ':' '\n' mapping(m)</code> は、<var>key</var> で表されるプロパティの値がmapping(m)で表されるオブジェクトであることを示します。 </p> <p>1つのオブジェクトに、2つ以上、同じ key を持つプロパティが含まれることはありません。 </p> <h3>プロパティを指定するクエリの形式</h3> <p>プロパティを指定するクエリは、 </p><pre>.key_1.key_2(..省略..).key_n </pre> <p>のように、'<code>.</code>'と <var>key</var> が交互に現れる形で与えられ、これは「 yaml で与えられたオブジェクトの、<var>key<sub>1</sub></var> という <var>key</var> を持つプロパティの値であるオブジェクトの、 <var>key<sub>2</sub></var> という <var>key</var> を持つプロパティの値であるオブジェクトの、(..省略..) <var>key<sub>n</sub></var> という <var>key</var> を持つプロパティ」を表します。 </p> <p>なお、あるi(<var>1 &le; i &le; n - 1</var>)について、.key_1.key_2.(..省略..).key_iまでで表されるプロパティの値がオブジェクトでない、またはオブジェクトであるがkey_i+1というプロパティを含んでいない場合、.key_1.key_2(..省略..).key_n で表されるようなプロパティは存在しないとみなします。 </p> <h2>Input</h2> <blockquote> .key_1.key_2(...).key_n<br>yaml<br></blockquote> <p><var>1 &le; n &le; 20</var> </p> <p>入力全体に含まれる文字数 <var>&le; 50,000</var> </p> <h2>Output</h2> <p>プロパティの値を 1 行で出力してください。 </p> <p>指定されたプロパティが存在しない場合は <code>no such property</code>, プロパティの値がオブジェクトの場合は <code>object</code>, プロパティの値が文字列の場合は <code>string &quot;&lt;文字列の内容&gt;&quot;</code> と出力してください。 </p> <h2>Sample Input 1</h2> <pre>.tweets.1 name: shimeji id: shimejitan tweets: 1: shimejilove 2: azupero </pre> <h2>Output for the Sample Input 1</h2> <pre>string &quot;shimejilove&quot; </pre> <h2>Sample Input 2</h2> <pre>.a a: sample case </pre> <h2>Output for the Sample Input 2</h2> <pre>string &quot;sample case&quot; </pre> <h2>Sample Input 3</h2> <pre>.my.obj my: str: string value obj: a: a b: b c: c </pre> <h2>Output for the Sample Input 3</h2> <pre>object </pre> <h2>Sample Input 4</h2> <pre>.str.inner str: inner </pre> <h2>Output for the Sample Input 4</h2> <pre>no such property </pre> <h2>Sample Input 5</h2> <pre>.no.such.property object: prop1: str prop2: str </pre> <h2>Output for the Sample Input 5</h2> <pre>no such property </pre>
p03095
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a string <var>S</var> of length <var>N</var>. Among its subsequences, count the ones such that all characters are different, modulo <var>10^9+7</var>. Two subsequences are considered different if their characters come from different positions in the string, even if they are the same as strings.</p> <p>Here, a subsequence of a string is a concatenation of <strong>one or more</strong> characters from the string without changing the order.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 100000</var></li> <li><var>S</var> consists of lowercase English letters.</li> <li><var>|S|=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>S</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the subsequences such that all characters are different, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 abcd </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>15 </pre> <p>Since all characters in <var>S</var> itself are different, all its subsequences satisfy the condition.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 baa </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>5 </pre> <p>The answer is five: <code>b</code>, two occurrences of <code>a</code>, two occurrences of <code>ba</code>. Note that we do not count <code>baa</code>, since it contains two <code>a</code>s.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 abcab </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>17 </pre></section> </div> </span>
p00713
<H1> <font color="#000">Problem D:</font> Circle and Points </H1> <P> You are given <i>N</i> points in the <i>xy</i>-plane. You have a circle of radius one and move it on the <i>xy</i>-plane, so as to enclose as many of the points as possible. Find how many points can be simultaneously enclosed at the maximum. A point is considered <em>enclosed</em> by a circle when it is inside or on the circle. </P> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_points"/> <p>Fig 1. Circle and Points</p> </center> <H2>Input</H2> <P> The input consists of a series of data sets, followed by a single line only containing a single character '0', which indicates the end of the input. Each data set begins with a line containing an integer <i>N</i>, which indicates the number of points in the data set. It is followed by <i>N</i> lines describing the coordinates of the points. Each of the <i>N</i> lines has two decimal fractions <i>X</i> and <i>Y</i>, describing the <i>x</i>- and <i>y</i>-coordinates of a point, respectively. They are given with five digits after the decimal point. </P> <P> You may assume 1 &lt;= <i>N</i> &lt;= 300, 0.0 &lt;= <i>X</i> &lt;= 10.0, and 0.0 &lt;= <i>Y</i> &lt;= 10.0. No two points are closer than 0.0001. No two points in a data set are approximately at a distance of 2.0. More precisely, for any two points in a data set, the distance <i>d</i> between the two never satisfies 1.9999 &lt;= <i>d</i> &lt;= 2.0001. Finally, no three points in a data set are simultaneously very close to a single circle of radius one. More precisely, let P<sub>1</sub>, P<sub>2</sub>, and P<sub>3</sub> be any three points in a data set, and <i>d</i><sub>1</sub>, <i>d</i><sub>2</sub>, and <i>d</i><sub>3</sub> the distances from an arbitrarily selected point in the <i>xy</i>-plane to each of them respectively. Then it never simultaneously holds that 0.9999 &lt;= <i>d</i><sub><i>i</i></sub> &lt;= 1.0001 (<i>i</i> = 1, 2, 3). </P> <H2>Output</H2> <P> For each data set, print a single line containing the maximum number of points in the data set that can be simultaneously enclosed by a circle of radius one. No other characters including leading and trailing spaces should be printed. </P> <H2>Sample Input</H2> <PRE> 3 6.47634 7.69628 5.16828 4.79915 6.69533 6.20378 6 7.15296 4.08328 6.50827 2.69466 5.91219 3.86661 5.29853 4.16097 6.10838 3.46039 6.34060 2.41599 8 7.90650 4.01746 4.10998 4.18354 4.67289 4.01887 6.33885 4.28388 4.98106 3.82728 5.12379 5.16473 7.84664 4.67693 4.02776 3.87990 20 6.65128 5.47490 6.42743 6.26189 6.35864 4.61611 6.59020 4.54228 4.43967 5.70059 4.38226 5.70536 5.50755 6.18163 7.41971 6.13668 6.71936 3.04496 5.61832 4.23857 5.99424 4.29328 5.60961 4.32998 6.82242 5.79683 5.44693 3.82724 6.70906 3.65736 7.89087 5.68000 6.23300 4.59530 5.92401 4.92329 6.24168 3.81389 6.22671 3.62210 0 </PRE> <H2>Output for the Sample Input</H2> <PRE> 2 5 5 11 </PRE>
p01801
<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 L Wall Making Game</h2> <p> The game <i>Wall Making Game</i>, a two-player board game, is all the rage. </p> <p> This game is played on an $H \times W$ board. Each cell of the board is one of <i>empty</i>, <i>marked</i>, or <i>wall</i>. At the beginning of the game, there is no wall on the board. </p> <p> In this game, two players alternately move as follows: </p> <ol> <li> A player chooses one of the empty cells (not marked and not wall). If the player can't choose a cell, he loses.</li> <li> Towards each of the four directions (upper, lower, left, and right) from the chosen cell, the player changes cells (including the chosen cell) to walls until the player first reaches a wall or the outside of the board. </li> </ol> <p> Note that marked cells cannot be chosen in step 1, but they can be changed to walls in step 2. </p> <p> Fig.1 shows an example of a move in which a player chooses the cell at the third row and the fourth column. </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_WallMakingGame"><br> Fig.1: An example of a move in Wall Making Game. </center> <p> Your task is to write a program that determines which player wins the game if the two players play optimally from a given initial board. </p> <h3>Input</h3> <p> The first line of the input consists of two integers $H$ and $W$ $(1 \leq H, W \leq 20)$, where $H$ and $W$ are the height and the width of the board respectively. The following $H$ lines represent the initial board. Each of the $H$ lines consists of $W$ characters. </p> <p> The $j$-th character of the $i$-th line is '<span>.</span>' if the cell at the $j$-th column of the $i$-th row is empty, or '<span>X</span>' if the cell is marked. </p> <h3>Output</h3> <p> Print "<span>First</span>" (without the quotes) in a line if the first player wins the given game. Otherwise, print "<span>Second</span>" (also without the quotes) in a line. </p> <h3>Sample Input 1</h3> <pre>2 2 .. ..</pre> <h3>Output for the Sample Input 1</h3> <pre>Second</pre> <h3>Sample Input 2</h3> <pre>2 2 X. ..</pre> <h3>Output for the Sample Input 2</h3> <pre>First</pre> <h3>Sample Input 3</h3> <pre>4 5 X.... ...X. ..... .....</pre> <h3>Output for the Sample Input 3</h3> <pre>First</pre>
p00343
<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>必勝7並べ </H1> <p> トランプを使ったゲームに「7並べ」があります。ここではそれを簡単にしたゲームを考えます。1から13の番号がそれぞれ書かれた13枚のカードを使って7並べをします。対戦は、2者だけで次のようにゲームを進めます。 </p> <ol> <li> 「場」に7のカードを置きます。</li> <li> 2者には、残りのカードがランダムに6枚ずつ配布されます。</li> <li> 先手の手持ちのカードのうち、場にあるカードの番号と連続する番号のカードがあれば、そのうちの1枚を場に置きます。プレイヤーはカードが置ける場合には必ず置かなければいけません。無いときに限り、カードを出さずに相手の番になります。</li> <li> 後手も同じ要領で、手持ちのカードを場に置きます。</li> <li> 手順3と4を繰り返して、一方の手持ちのカードがなくなるまで続けます。先に手持ちのカードをすべて場に置けた方が勝者となります。</li> </ol> <br/> <p> 先手のカードの番号が与えられたとき、後手がどのようにカードを出してきても、先手が勝つ手順が少なくとも一つあるかを判定して出力するプログラムを作成せよ。 </p> <h2>Input</h2> <p> 入力は以下の形式で与えられる。 </p> <pre> <var>N</var> <var>game<sub>1</sub></var> <var>game<sub>2</sub></var> : <var>game<sub>N</sub></var> </pre> <p> 1行目には、ゲームを行う回数 <var>N</var> (1 &le; <var>N</var> &le; 100) が与えられる。続く <var>N</var> 行に、<var>i</var> 回目のゲームの情報 <var>game<sub>i</sub></var> が与えられる。各 <var>game<sub>i</sub></var> は、以下の形式で与えられる。 </p> <pre> <var>f<sub>1</sub></var> <var>f<sub>2</sub></var> <var>f<sub>3</sub></var> <var>f<sub>4</sub></var> <var>f<sub>5</sub></var> <var>f<sub>6</sub></var> </pre> <p> <var>f<sub>j</sub></var> (1 &le; <var>f<sub>j</sub></var> &le; 13, <var>f<sub>j</sub></var> &ne; 7) は先手に配られるカードの番号である。ただし、同じ行に番号が重複して現れることはない(<var>j</var> &ne; <var>k</var> について <var>f<sub>j</sub></var> &ne; <var>f<sub>k</sub></var>)。 </p> <h2>Output</h2> <p> 各ゲームについて、後手がどのようにカードを出してきても、先手が勝つ手順が少なくとも一つある場合「yes」、そうでない場合「no」と1行に出力する。 </p> <h2>Sample Input 1</h2> <pre> 5 1 2 3 4 5 6 1 3 5 6 8 4 1 2 3 4 5 8 1 2 4 5 10 11 1 2 3 6 9 11 </pre> <h2>Sample Output 1</h2> <pre> yes yes no yes no </pre>
p02684
<span class="lang-en"> <p>Score: <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>The Kingdom of Takahashi has <var>N</var> towns, numbered <var>1</var> through <var>N</var>.</p> <p>There is one teleporter in each town. The teleporter in Town <var>i</var> <var>(1 \leq i \leq N)</var> sends you to Town <var>A_i</var>.</p> <p>Takahashi, the king, loves the positive integer <var>K</var>. The selfish king wonders what town he will be in if he starts at Town <var>1</var> and uses a teleporter exactly <var>K</var> times from there.</p> <p>Help the king by writing a program that answers this question.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3> <ul> <li><var>2 \leq N \leq 2 \times 10^5</var></li> <li><var>1 \leq A_i \leq N</var></li> <li><var>1 \leq K \leq 10^{18}</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>\dots</var> <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3> <p>Print the integer representing the town the king will be in if he starts at Town <var>1</var> and uses a teleporter exactly <var>K</var> times from there.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 5 3 2 4 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>If we start at Town <var>1</var> and use the teleporter <var>5</var> times, our travel will be as follows: <var>1 \to 3 \to 4 \to 1 \to 3 \to 4</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 727202214173249351 6 5 2 5 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 </pre></section> </div> </span>
p03996
<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> arrays. The length of each array is <var>M</var> and initially each array contains integers <var>(1,2,...,M)</var> in this order.</p> <p>Mr. Takahashi has decided to perform <var>Q</var> operations on those <var>N</var> arrays. For the <var>i</var>-th (<var>1≤i≤Q</var>) time, he performs the following operation.</p> <ul> <li>Choose an arbitrary array from the <var>N</var> arrays and move the integer <var>a_i</var> (<var>1≤a_i≤M</var>) to the front of that array. For example, after performing the operation on <var>a_i=2</var> and the array <var>(5,4,3,2,1)</var>, this array becomes <var>(2,5,4,3,1)</var>.</li> </ul> <p>Mr. Takahashi wants to make <var>N</var> arrays exactly the same after performing the <var>Q</var> operations. Determine if it is possible or not.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≤N≤10^5</var></li> <li><var>2≤M≤10^5</var></li> <li><var>1≤Q≤10^5</var></li> <li><var>1≤a_i≤M</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>M</var> <var>Q</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_Q</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <code>Yes</code> if it is possible to make <var>N</var> arrays exactly the same after performing the <var>Q</var> operations. Otherwise, print <code>No</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 2 3 2 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes </pre> <p>You can perform the operations as follows.</p> <p><img src="/img/other/code_festival_2016_quala/gbanjthabot/E_0.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 2 3 2 1 2 </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>2 3 3 3 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Yes </pre> <p>You can perform the operations as follows.</p> <p><img src="/img/other/code_festival_2016_quala/gbanjthabot/E_2.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>3 3 6 1 2 2 3 3 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>No </pre></section> </div> </span>
p00969
<h2>Arithmetic Progressions</h2> <p> An arithmetic progression is a sequence of numbers $a_1, a_2, ..., a_k$ where the difference of consecutive members $a_{i+1} - a_i$ is a constant ($1 \leq i \leq k-1$). For example, the sequence 5, 8, 11, 14, 17 is an arithmetic progression of length 5 with the common difference 3. </p> <p> In this problem, you are requested to find the longest arithmetic progression which can be formed selecting some numbers from a given set of numbers. For example, if the given set of numbers is {0, 1, 3, 5, 6, 9}, you can form arithmetic progressions such as 0, 3, 6, 9 with the common difference 3, or 9, 5, 1 with the common difference -4. In this case, the progressions 0, 3, 6, 9 and 9, 6, 3, 0 are the longest. </p> <h3>Input</h3> <p> The input consists of a single test case of the following format. </p> <pre> $n$ $v_1$ $v_2$ ... $v_n$ </pre> <p> $n$ is the number of elements of the set, which is an integer satisfying $2 \leq n \leq 5000$. Each $v_i$ ($1 \leq i \leq n$) is an element of the set, which is an integer satisfying $0 \leq v_i \leq 10^9$. $v_i$'s are all different, i.e., $v_i \ne v_j$ if $i \ne j$. </p> <h3>Output</h3> <p> Output the length of the longest arithmetic progressions which can be formed selecting some numbers from the given set of numbers. </p> <h3>Sample Input 1</h3> <pre> 6 0 1 3 5 6 9 </pre> <h3>Sample Output 1</h3> <pre> 4 </pre> <h3>Sample Input 2</h3> <pre> 7 1 4 7 3 2 6 5 </pre> </h3>Sample Output 2</h3> <pre> 7 </pre> <h3>Sample Input 3</h3> <pre> 5 1 2 4 8 16 </pre> <h3>Sample Output 3</h3> <pre> 2 </pre>
p02954
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given is a string <var>S</var> consisting of <code>L</code> and <code>R</code>.</p> <p>Let <var>N</var> be the length of <var>S</var>. There are <var>N</var> squares arranged from left to right, and the <var>i</var>-th character of <var>S</var> from the left is written on the <var>i</var>-th square from the left.</p> <p>The character written on the leftmost square is always <code>R</code>, and the character written on the rightmost square is always <code>L</code>.</p> <p>Initially, one child is standing on each square.</p> <p>Each child will perform the move below <var>10^{100}</var> times:</p> <ul> <li>Move one square in the direction specified by the character written in the square on which the child is standing. <code>L</code> denotes left, and <code>R</code> denotes right.</li> </ul> <p>Find the number of children standing on each square after the children performed the moves.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>S</var> is a string of length between <var>2</var> and <var>10^5</var> (inclusive).</li> <li>Each character of <var>S</var> is <code>L</code> or <code>R</code>.</li> <li>The first and last characters of <var>S</var> are <code>R</code> and <code>L</code>, respectively.</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 number of children standing on each square after the children performed the moves, in order from left to right.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>RRLRL </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>0 1 2 1 1 </pre> <ul> <li>After each child performed one move, the number of children standing on each square is <var>0, 2, 1, 1, 1</var> from left to right.</li> <li>After each child performed two moves, the number of children standing on each square is <var>0, 1, 2, 1, 1</var> from left to right.</li> <li>After each child performed <var>10^{100}</var> moves, the number of children standing on each square is <var>0, 1, 2, 1, 1</var> from left to right.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>RRLLLLRLRRLL </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 3 3 0 0 0 1 1 0 2 2 0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>RRRLLRLLRRRLLLLL </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 0 3 2 0 2 1 0 0 0 4 4 0 0 0 0 </pre></section> </div> </span>
p03646
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a sequence of length <var>N</var> consisting of non-negative integers. Consider performing the following operation on this sequence until the largest element in this sequence becomes <var>N-1</var> or smaller.</p> <ul> <li>Determine the largest element in the sequence (if there is more than one, choose one). Decrease the value of this element by <var>N</var>, and increase each of the other elements by <var>1</var>.</li> </ul> <p>It can be proved that the largest element in the sequence becomes <var>N-1</var> or smaller after a finite number of operations.</p> <p>You are given an integer <var>K</var>. Find an integer sequence <var>a_i</var> such that the number of times we will perform the above operation is exactly <var>K</var>. It can be shown that there is always such a sequence under the constraints on input and output in this problem.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>0 ≤ K ≤ 50 \times 10^{16}</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>K</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print a solution in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>a_2</var> ... <var>a_N</var> </pre> <p>Here, <var>2 ≤ N ≤ 50</var> and <var>0 ≤ a_i ≤ 10^{16} + 1000</var> must hold.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 3 3 3 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 1 0 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>2 2 2 </pre> <p>The operation will be performed twice: [2, 2] -&gt; [0, 3] -&gt; [1, 1].</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>7 27 0 0 0 0 0 0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>1234567894848 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>10 1000 193 256 777 0 1 1192 1234567891011 48 425 </pre></section> </div> </span>
p01381
<div> <h1 class="title">問題 F : 全域木</h1> <p>今,G○○gle Code Jam の地区大会が始まろうとしている. 左の席に座っている男の ID は wata と言うらしい. 東京大学時代の記憶に,似たような ID の仲間が居た覚えがある. しかし,僕の仲間は一人残さず美少女だったはずだ.</p> <p>僕の記憶の中の wata は,マトロイドが好きだった. 特に,マトロイド交差が大好きで, 様々なマトロイド達を交差させることに一種の興奮すら覚えると言う少し変わった奴だった.</p> <p>マトロイドの理論の力を使えば, 与えられたグラフ上で辺を共有しない複数の全域木を求めることはとても簡単な問題だと言っていた気がする. しかし,特別なグラフに関しては, マトロイドのアルゴリズムを直接に適用するよりも高速なアルゴリズムがあるのではないか?</p> <div> <h1>問題</h1> <p><span style="font-size:110%;font-family:times new roman;"><i>N</i></span> 個の頂点からなる完全グラフにおいて, 辺を共有しない全域木を <span style="font-size:110%;font-family:times new roman;"><i>K</i></span> 個作成せよ.</p> <p>完全グラフとは,全ての相異なる 2 頂点間に 1 本の辺を持つグラフである. 下図は,4 頂点の完全グラフの例である.</p> <div> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_2264_1" alt="4 頂点の完全グラフ" /> <p class="caption">4 頂点の完全グラフ.</p> </div> <p>全域木とは,元のグラフの全ての頂点と一部の辺からなる木のことである. 木とは,連結かつ閉路を持たないグラフのことである. 下図は,4 頂点の完全グラフにおける,辺を共有しない 2 つの全域木である.</p> <div> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_2264_2" alt="4 頂点の完全グラフの全域木の例" /> <p class="caption">4 頂点の完全グラフの全域木の例.</p> </div> <div> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_2264_3" alt="4 頂点の完全グラフの全域木であり,前の例と辺を共有しないもの" /> <p class="caption">4 頂点の完全グラフの全域木であり,前の例と辺を共有しないもの.</p> </div> </div> <div> <h1>入力</h1> <p>入力は 1 行からなり, 2 つの整数 <span style="font-size:110%;font-family:times new roman;"><i>N</i>,&nbsp;<i>K</i></span> が書かれている.</p> </div> <div> <h1>出力</h1> <p>条件を満たす <span style="font-size:110%;font-family:times new roman;"><i>K</i></span> 個の全域木を作ることができない時,-1 とだけ出力せよ.</p> <p>条件を満たす <span style="font-size:110%;font-family:times new roman;"><i>K</i></span> 個の全域木を作ることができる時, <span style="font-size:110%;font-family:times new roman;"><i>K</i></span> 個の全域木を改行で区切り出力せよ. 1 つの全域木は <span style="font-size:110%;font-family:times new roman;"><i>N</i>&nbsp;-&nbsp;1</span> 行で表される. その <span style="font-size:110%;font-family:times new roman;"><i>i</i></span> 行目には,その全域木の辺 <span style="font-size:110%;font-family:times new roman;"><i>i</i></span> が結ぶ 2 つの頂点を表す 2 つの整数をスペースで区切り出力する. ここで,頂点は <span style="font-size:110%;font-family:times new roman;">1</span> から <span style="font-size:110%;font-family:times new roman;"><i>N</i></span> までの整数で表すものとする.</p> </div> <div> <h1>制約</h1> <ul class="simple"> <li><span style="font-size:110%;font-family:times new roman;">2&nbsp;&le;&nbsp;<i>N</i>&nbsp;&le;&nbsp;10000</span></li> <li><span style="font-size:110%;font-family:times new roman;">1&nbsp;&le;&nbsp;<i>K</i>&nbsp;&le;&nbsp;100</span></li> </ul> </div> <div> <h1>部分点</h1> <p>この問題の判定には,20 点分のテストケースのグループが設定されている. このグループに含まれるテストケースの入力は以下を満たす.</p> <ul class="simple"> <li><span style="font-size:110%;font-family:times new roman;">1&nbsp;&le;&nbsp;<i>N</i>&nbsp;&le;&nbsp;8</span></li> </ul> </div> <div> <h1>入出力例</h1> <div> <h2>入出力例 1</h2> <p>入力例 1:</p> <pre class="literal-block"> 4 2 </pre> <p>入力例 1 に対する出力の例:</p> <pre class="literal-block"> 1 2 1 4 2 3 1 3 2 4 3 4 </pre> <p>この入出力例は問題文中の図と対応している.</p> </div> <div> <h2>入出力例 2</h2> <p>入力例 2:</p> <pre class="literal-block"> 4 3 </pre> <p>入力例 2 に対する出力の例:</p> <pre class="literal-block"> -1 </pre> </div> </div> </div>