id
stringlengths
6
6
slug_name
stringlengths
1
105
pretty_content
stringlengths
1
27.2k
test_cases
sequencelengths
1
1
p03956
AtCoder Grand Contest 006 - Blackout
<span class="lang-en"> <p>Score : <var>1700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a grid with <var>N</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>).</p> <p>Initially, <var>M</var> of the cells are painted black, and all other cells are white. Specifically, the cells (<var>a_1</var>, <var>b_1</var>), (<var>a_2</var>, <var>b_2</var>), <var>...</var>, (<var>a_M</var>, <var>b_M</var>) are painted black.</p> <p>Snuke will try to paint as many white cells black as possible, according to the following rule:</p> <ul> <li>If two cells (<var>x</var>, <var>y</var>) and (<var>y</var>, <var>z</var>) are both black and a cell (<var>z</var>, <var>x</var>) is white for integers <var>1≤x,y,z≤N</var>, paint the cell (<var>z</var>, <var>x</var>) black.</li> </ul> <p>Find the number of black cells when no more white cells can be painted black.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≤N≤10^5</var></li> <li><var>1≤M≤10^5</var></li> <li><var>1≤a_i,b_i≤N</var></li> <li>All pairs (<var>a_i</var>, <var>b_i</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>M</var> <var>a_1</var> <var>b_1</var> <var>a_2</var> <var>b_2</var> <var>:</var> <var>a_M</var> <var>b_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of black cells when no more white cells can be painted black.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 1 2 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>It is possible to paint one white cell black, as follows:</p> <ul> <li>Since cells (<var>1</var>, <var>2</var>) and (<var>2</var>, <var>3</var>) are both black and a cell (<var>3</var>, <var>1</var>) is white, paint the cell (<var>3</var>, <var>1</var>) black.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 2 1 1 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>4 </pre> <p>It is possible to paint two white cells black, as follows:</p> <ul> <li>Since cells (<var>1</var>, <var>1</var>) and (<var>1</var>, <var>2</var>) are both black and a cell (<var>2</var>, <var>1</var>) is white, paint the cell (<var>2</var>, <var>1</var>) black.</li> <li>Since cells (<var>2</var>, <var>1</var>) and (<var>1</var>, <var>2</var>) are both black and a cell (<var>2</var>, <var>2</var>) is white, paint the cell (<var>2</var>, <var>2</var>) black.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 3 1 2 1 3 4 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>3 </pre> <p>No white cells can be painted black.</p></section> </div> </span>
[ [ "3 2\n1 2\n2 3\n", "3 2\n1 2\n2 3\n" ] ]
p03957
CODE FESTIVAL 2016 qual C - CF
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>This contest is <code>CODEFESTIVAL</code>, which can be shortened to the string <code>CF</code> by deleting some characters. </p> <p>Mr. Takahashi, full of curiosity, wondered if he could obtain <code>CF</code> from other strings in the same way. </p> <p>You are given a string <var>s</var> consisting of uppercase English letters. Determine whether the string <code>CF</code> can be obtained from the string <var>s</var> by deleting some characters.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ |s| ≤ 100</var></li> <li>All characters in <var>s</var> are uppercase English letters (<code>A</code>-<code>Z</code>).</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>s</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <code>Yes</code> if the string <code>CF</code> can be obtained from the string <var>s</var> by deleting some characters. Otherwise print <code>No</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>CODEFESTIVAL </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes </pre> <p><code>CF</code> is obtained by deleting characters other than the first character <code>C</code> and the fifth character <code>F</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>FESTIVALCODE </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>No </pre> <p><code>FC</code> can be obtained but <code>CF</code> cannot be obtained because you cannot change the order of the characters.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>CF </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Yes </pre> <p>It is also possible not to delete any characters.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>FCF </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>Yes </pre> <p><code>CF</code> is obtained by deleting the first character.</p></section> </div> </span>
[ [ "CODEFESTIVAL\n", "CODEFESTIVAL\n" ] ]
p03958
CODE FESTIVAL 2016 qual C - K Cakes
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>K</var> pieces of cakes. Mr. Takahashi would like to eat one cake per day, taking <var>K</var> days to eat them all.</p> <p>There are <var>T</var> types of cake, and the number of the cakes of type <var>i</var> (<var>1 ≤ i ≤ T</var>) is <var>a_i</var>. </p> <p>Eating the same type of cake two days in a row would be no fun, so Mr. Takahashi would like to decide the order for eating cakes that minimizes the number of days on which he has to eat the same type of cake as the day before. </p> <p>Compute the minimum number of days on which the same type of cake as the previous day will be eaten.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ K ≤ 10000</var></li> <li><var>1 ≤ T ≤ 100</var></li> <li><var>1 ≤ a_i ≤ 100</var></li> <li><var>a_1 + a_2 + ... + a_T = K</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> <var>T</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_T</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of days on which the same type of cake as the previous day will be eaten.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>7 3 3 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>0 </pre> <p>For example, if Mr. Takahashi eats cakes in the order of <var>2, 1, 2, 3, 1, 3, 1</var>, he can avoid eating the same type of cake as the previous day.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 3 1 4 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 </pre> <p>There are <var>6</var> cakes. For example, if Mr. Takahashi eats cakes in the order of <var>2, 3, 2, 2, 1, 2</var>, he has to eat the same type of cake (i.e., type <var>2</var>) as the previous day only on the fourth day. Since this is the minimum number, the answer is <var>1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100 1 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>99 </pre> <p>Since Mr. Takahashi has only one type of cake, he has no choice but to eat the same type of cake as the previous day from the second day and after.</p></section> </div> </span>
[ [ "7 3\n3 2 2\n", "7 3\n3 2 2\n" ] ]
p03959
CODE FESTIVAL 2016 qual C - Two Alpinists
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Mountaineers Mr. Takahashi and Mr. Aoki recently trekked across a certain famous mountain range. The mountain range consists of <var>N</var> mountains, extending from west to east in a straight line as Mt. <var>1</var>, Mt. <var>2</var>, ..., Mt. <var>N</var>. Mr. Takahashi traversed the range from the west and Mr. Aoki from the east. </p> <p>The height of Mt. <var>i</var> is <var>h_i</var>, but they have forgotten the value of each <var>h_i</var>. Instead, for each <var>i</var> (<var>1 ≤ i ≤ N</var>), they recorded the maximum height of the mountains climbed up to the time they reached the peak of Mt. <var>i</var> (including Mt. <var>i</var>). Mr. Takahashi's record is <var>T_i</var> and Mr. Aoki's record is <var>A_i</var>. </p> <p>We know that the height of each mountain <var>h_i</var> is a positive integer. Compute the number of the possible sequences of the mountains' heights, modulo <var>10^9 + 7</var>.</p> <p>Note that the records may be incorrect and thus there may be no possible sequence of the mountains' heights. In such a case, output <var>0</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 10^5</var></li> <li><var>1 ≤ T_i ≤ 10^9</var></li> <li><var>1 ≤ A_i ≤ 10^9</var></li> <li><var>T_i ≤ T_{i+1}</var> (<var>1 ≤ i ≤ N - 1</var>)</li> <li><var>A_i ≥ A_{i+1}</var> (<var>1 ≤ i ≤ N - 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>T_1</var> <var>T_2</var> <var>...</var> <var>T_N</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of possible sequences of the mountains' heights, modulo <var>10^9 + 7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 1 3 3 3 3 3 3 2 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>The possible sequences of the mountains' heights are:</p> <ul> <li><var>1, 3, 2, 2, 2</var> </li> <li><var>1, 3, 2, 1, 2</var> </li> <li><var>1, 3, 1, 2, 2</var> </li> <li><var>1, 3, 1, 1, 2</var> </li> </ul> <p>for a total of four sequences.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 1 1 1 2 2 3 2 1 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>The records are contradictory, since Mr. Takahashi recorded <var>2</var> as the highest peak after climbing all the mountains but Mr. Aoki recorded <var>3</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>10 1 3776 3776 8848 8848 8848 8848 8848 8848 8848 8848 8848 8848 8848 8848 8848 8848 8848 3776 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>884111967 </pre> <p>Don't forget to compute the number modulo <var>10^9 + 7</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>1 17 17 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>1 </pre> <p>Some mountain ranges consist of only one mountain.</p></section> </div> </span>
[ [ "5\n1 3 3 3 3\n3 3 2 2 2\n", "5\n1 3 3 3 3\n3 3 2 2 2\n" ] ]
p03960
CODE FESTIVAL 2016 qual C - Friction
<span class="lang-en"> <p>Score : <var>800</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Mr. Takahashi has in his room an art object with <var>H</var> rows and <var>W</var> columns, made up of <var>H \times W</var> blocks. Each block has a color represented by a lowercase English letter (<code>a</code>-<code>z</code>). The color of the block at the <var>i</var>-th row and <var>j</var>-th column is <var>c_{i,j}</var>. </p> <p>Mr. Takahashi would like to dismantle the object, finding it a bit kitschy for his tastes. The dismantling is processed by repeating the following operation: </p> <ul> <li>Choose one of the <var>W</var> columns and push down that column one row. The block at the bottom of that column disappears.</li> </ul> <p>Each time the operation is performed, a cost is incurred. Since blocks of the same color have a property to draw each other together, the cost of the operation is the number of the pairs of blocks <var>(p, q)</var> such that:</p> <ul> <li>The block <var>p</var> is in the selected column.</li> <li>The two blocks <var>p</var> and <var>q</var> are horizontally adjacent (before pushing down the column).</li> <li>The two blocks <var>p</var> and <var>q</var> have the same color.</li> </ul> <p>Mr. Takahashi dismantles the object by repeating the operation <var>H \times W</var> times to get rid of all the blocks. Compute the minimum total cost to dismantle the object.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ H ≤ 300</var></li> <li><var>2 ≤ W ≤ 300</var></li> <li>All characters <var>c_{i,j}</var> are lowercase English letters (<code>a</code>-<code>z</code>).</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li>In test cases worth <var>300</var> points, <var>W = 3</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>c_{1,1}c_{1,2}</var>..<var>c_{1,W}</var> <var>c_{2,1}c_{2,2}</var>..<var>c_{2,W}</var> <var>:</var> <var>c_{H,1}c_{H,2}</var>..<var>c_{H,W}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum total cost to dismantle the object.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 rrr brg </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>For example, the total cost of <var>2</var> can be achieved by performing the operation as follows and this is the minimum value.</p> <div style="text-align: center;"> <img alt="ccb25ed6f1df9367829b68523e1deff4.png" src="https://atcoder.jp/img/code-festival-2016-qualc/ccb25ed6f1df9367829b68523e1deff4.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 3 xya xya ayz ayz xaz xaz </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>The total cost of <var>0</var> can be achieved by first pushing down all blocks of the middle column, then all of the left column, and all of the right column.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 2 ay xa xy ay </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> <p>The total cost of <var>0</var> can be achieved by the following operations:</p> <ul> <li>pushing down the right column one row;</li> <li>pushing down the left column one row;</li> <li>pushing down all of the right column;</li> <li>and pushing down all of the left column.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>5 5 aaaaa abbba ababa abbba aaaaa </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>24 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>7 10 xxxxxxxxxx ccccxxffff cxxcxxfxxx cxxxxxffff cxxcxxfxxx ccccxxfxxx xxxxxxxxxx </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>130 </pre></section> </div> </span>
[ [ "2 3\nrrr\nbrg\n", "2 3\nrrr\nbrg\n" ] ]
p03961
CODE FESTIVAL 2016 qual C - Encyclopedia of Permutations
<span class="lang-en"> <p>Score : <var>1200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>One day Mr. Takahashi picked up a dictionary containing all of the <var>N</var>! permutations of integers <var>1</var> through <var>N</var>. The dictionary has <var>N</var>! pages, and page <var>i</var> (<var>1 ≤ i ≤ N!</var>) contains the <var>i</var>-th permutation in the lexicographical order.</p> <p>Mr. Takahashi wanted to look up a certain permutation of length <var>N</var> in this dictionary, but he forgot some part of it.</p> <p>His memory of the permutation is described by a sequence <var>P_1, P_2, ..., P_N</var>. If <var>P_i = 0</var>, it means that he forgot the <var>i</var>-th element of the permutation; otherwise, it means that he remembered the <var>i</var>-th element of the permutation and it is <var>P_i</var>.</p> <p>He decided to look up all the possible permutations in the dictionary. Compute the sum of the page numbers of the pages he has to check, modulo <var>10^9 + 7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 500000</var></li> <li><var>0 ≤ P_i ≤ N</var></li> <li><var>P_i ≠ P_j</var> if <var>i ≠ j</var> (<var>1 ≤ i, j ≤ N</var>), <var>P_i ≠ 0</var> and <var>P_j ≠ 0</var>.</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li>In test cases worth <var>500</var> points, <var>1 ≤ N ≤ 3000</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>P_1</var> <var>P_2</var> <var>...</var> <var>P_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the sum of the page numbers of the pages he has to check, as modulo <var>10^9 + 7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 0 2 3 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>23 </pre> <p>The possible permutations are [<var>1</var>, <var>2</var>, <var>3</var>, <var>4</var>] and [<var>4</var>, <var>2</var>, <var>3</var>, <var>1</var>]. Since the former is on page <var>1</var> and the latter is on page <var>22</var>, the answer is <var>23</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 0 0 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>21 </pre> <p>Since all permutations of length <var>3</var> are possible, the answer is <var>1 + 2 + 3 + 4 + 5 + 6 = 21</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 1 2 3 5 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>2 </pre> <p>Mr. Takahashi remembered all the elements of the permutation.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>1 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>1 </pre> <p>The dictionary consists of one page.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>10 0 3 0 0 1 0 4 0 0 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>953330050 </pre></section> </div> </span>
[ [ "4\n0 2 3 0\n", "4\n0 2 3 0\n" ] ]
p03962
AtCoder Beginner Contest 046 - AtCoDeer and Paint Cans
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>AtCoDeer the deer recently bought three paint cans. The color of the one he bought two days ago is <var>a</var>, the color of the one he bought yesterday is <var>b</var>, and the color of the one he bought today is <var>c</var>. Here, the color of each paint can is represented by an integer between <var>1</var> and <var>100</var>, inclusive.</p> <p>Since he is forgetful, he might have bought more than one paint can in the same color. Count the number of different kinds of colors of these paint cans and tell him.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦a,b,c≦100</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 number of different kinds of colors of the paint cans.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>Three different colors: <var>1</var>, <var>3</var>, and <var>4</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 3 33 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 </pre> <p>Two different colors: <var>3</var> and <var>33</var>.</p></section> </div> </span>
[ [ "3 1 4\n", "3 1 4\n" ] ]
p03963
AtCoder Beginner Contest 046 - Painting Balls with AtCoDeer
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> balls placed in a row. AtCoDeer the deer is painting each of these in one of the <var>K</var> colors of his paint cans. For aesthetic reasons, any two adjacent balls must be painted in different colors.</p> <p>Find the number of the possible ways to paint the balls.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦1000</var></li> <li><var>2≦K≦1000</var></li> <li>The correct answer is at most <var>2^{31}-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>K</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the possible ways to paint the balls.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We will denote the colors by <var>0</var> and <var>1</var>. There are two possible ways: we can either paint the left ball in color <var>0</var> and the right ball in color <var>1</var>, or paint the left in color <var>1</var> and the right in color <var>0</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>10 </pre> <p>Since there is only one ball, we can use any of the ten colors to paint it. Thus, the answer is ten.</p></section> </div> </span>
[ [ "2 2\n", "2 2\n" ] ]
p03964
AtCoder Beginner Contest 046 - AtCoDeer and Election Report
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>AtCoDeer the deer is seeing a quick report of election results on TV. Two candidates are standing for the election: Takahashi and Aoki. The report shows the ratio of the current numbers of votes the two candidates have obtained, but not the actual numbers of votes. AtCoDeer has checked the report <var>N</var> times, and when he checked it for the <var>i</var>-th <var>(1≦i≦N)</var> time, the ratio was <var>T_i:A_i</var>. It is known that each candidate had at least one vote when he checked the report for the first time.</p> <p>Find the minimum possible total number of votes obtained by the two candidates when he checked the report for the <var>N</var>-th time. It can be assumed that the number of votes obtained by each candidate never decreases.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦1000</var></li> <li><var>1≦T_i,A_i≦1000 (1≦i≦N)</var></li> <li><var>T_i</var> and <var>A_i</var> <var>(1≦i≦N)</var> are coprime.</li> <li>It is guaranteed that the correct answer is at most <var>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>N</var> <var>T_1</var> <var>A_1</var> <var>T_2</var> <var>A_2</var> <var>:</var> <var>T_N</var> <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum possible total number of votes obtained by Takahashi and Aoki when AtCoDeer checked the report for the <var>N</var>-th time.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 3 1 1 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>10 </pre> <p>When the numbers of votes obtained by the two candidates change as <var>2,3 → 3,3 → 6,4</var>, the total number of votes at the end is <var>10</var>, which is the minimum possible number.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 1 1 1 1 1 5 1 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>101 </pre> <p>It is possible that neither candidate obtained a vote between the moment when he checked the report, and the moment when he checked it for the next time.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 3 10 48 17 31 199 231 23 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>6930 </pre></section> </div> </span>
[ [ "3\n2 3\n1 1\n3 2\n", "3\n2 3\n1 1\n3 2\n" ] ]
p03965
AtCoder Beginner Contest 046 - AtCoDeer and Rock-Paper
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>AtCoDeer the deer and his friend TopCoDeer is playing a game. The game consists of <var>N</var> turns. In each turn, each player plays one of the two <em>gestures</em>, <em>Rock</em> and <em>Paper</em>, as in Rock-paper-scissors, under the following condition:</p> <p>(※) After each turn, (the number of times the player has played Paper)<var>≦</var>(the number of times the player has played Rock).</p> <p>Each player's score is calculated by (the number of turns where the player wins) <var>-</var> (the number of turns where the player loses), where the outcome of each turn is determined by the rules of Rock-paper-scissors.</p> <p><em>(For those who are not familiar with Rock-paper-scissors: If one player plays Rock and the other plays Paper, the latter player will win and the former player will lose. If both players play the same gesture, the round is a tie and neither player will win nor lose.)</em></p> <p>With his supernatural power, AtCoDeer was able to foresee the gesture that TopCoDeer will play in each of the <var>N</var> turns, before the game starts. Plan AtCoDeer's gesture in each turn to maximize AtCoDeer's score.</p> <p>The gesture that TopCoDeer will play in each turn is given by a string <var>s</var>. If the <var>i</var>-th <var>(1≦i≦N)</var> character in <var>s</var> is <code>g</code>, TopCoDeer will play Rock in the <var>i</var>-th turn. Similarly, if the <var>i</var>-th <var>(1≦i≦N)</var> character of <var>s</var> in <code>p</code>, TopCoDeer will play Paper in the <var>i</var>-th turn.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>N=|s|</var></li> <li>Each character in <var>s</var> is <code>g</code> or <code>p</code>.</li> <li>The gestures represented by <var>s</var> satisfy the condition (※).</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>s</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the AtCoDeer's maximum possible score.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>gpg </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>0 </pre> <p>Playing the same gesture as the opponent in each turn results in the score of <var>0</var>, which is the maximum possible score.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>ggppgggpgg </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 </pre> <p>For example, consider playing gestures in the following order: Rock, Paper, Rock, Paper, Rock, Rock, Paper, Paper, Rock, Paper. This strategy earns three victories and suffers one defeat, resulting in the score of <var>2</var>, which is the maximum possible score.</p></section> </div> </span>
[ [ "gpg\n", "gpg\n" ] ]
p03966
AtCoder Regular Contest 062 - AtCoDeer and Election Report
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>AtCoDeer the deer is seeing a quick report of election results on TV. Two candidates are standing for the election: Takahashi and Aoki. The report shows the ratio of the current numbers of votes the two candidates have obtained, but not the actual numbers of votes. AtCoDeer has checked the report <var>N</var> times, and when he checked it for the <var>i</var>-th <var>(1≦i≦N)</var> time, the ratio was <var>T_i:A_i</var>. It is known that each candidate had at least one vote when he checked the report for the first time.</p> <p>Find the minimum possible total number of votes obtained by the two candidates when he checked the report for the <var>N</var>-th time. It can be assumed that the number of votes obtained by each candidate never decreases.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦1000</var></li> <li><var>1≦T_i,A_i≦1000 (1≦i≦N)</var></li> <li><var>T_i</var> and <var>A_i</var> <var>(1≦i≦N)</var> are coprime.</li> <li>It is guaranteed that the correct answer is at most <var>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>N</var> <var>T_1</var> <var>A_1</var> <var>T_2</var> <var>A_2</var> <var>:</var> <var>T_N</var> <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum possible total number of votes obtained by Takahashi and Aoki when AtCoDeer checked the report for the <var>N</var>-th time.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 3 1 1 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>10 </pre> <p>When the numbers of votes obtained by the two candidates change as <var>2,3 → 3,3 → 6,4</var>, the total number of votes at the end is <var>10</var>, which is the minimum possible number.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 1 1 1 1 1 5 1 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>101 </pre> <p>It is possible that neither candidate obtained a vote between the moment when he checked the report, and the moment when he checked it for the next time.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 3 10 48 17 31 199 231 23 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>6930 </pre></section> </div> </span>
[ [ "3\n2 3\n1 1\n3 2\n", "3\n2 3\n1 1\n3 2\n" ] ]
p03967
AtCoder Regular Contest 062 - AtCoDeer and Rock-Paper
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>AtCoDeer the deer and his friend TopCoDeer is playing a game. The game consists of <var>N</var> turns. In each turn, each player plays one of the two <em>gestures</em>, <em>Rock</em> and <em>Paper</em>, as in Rock-paper-scissors, under the following condition:</p> <p>(※) After each turn, (the number of times the player has played Paper)<var>≦</var>(the number of times the player has played Rock).</p> <p>Each player's score is calculated by (the number of turns where the player wins) <var>-</var> (the number of turns where the player loses), where the outcome of each turn is determined by the rules of Rock-paper-scissors.</p> <p><em>(For those who are not familiar with Rock-paper-scissors: If one player plays Rock and the other plays Paper, the latter player will win and the former player will lose. If both players play the same gesture, the round is a tie and neither player will win nor lose.)</em></p> <p>With his supernatural power, AtCoDeer was able to foresee the gesture that TopCoDeer will play in each of the <var>N</var> turns, before the game starts. Plan AtCoDeer's gesture in each turn to maximize AtCoDeer's score.</p> <p>The gesture that TopCoDeer will play in each turn is given by a string <var>s</var>. If the <var>i</var>-th <var>(1≦i≦N)</var> character in <var>s</var> is <code>g</code>, TopCoDeer will play Rock in the <var>i</var>-th turn. Similarly, if the <var>i</var>-th <var>(1≦i≦N)</var> character of <var>s</var> in <code>p</code>, TopCoDeer will play Paper in the <var>i</var>-th turn.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>N=|s|</var></li> <li>Each character in <var>s</var> is <code>g</code> or <code>p</code>.</li> <li>The gestures represented by <var>s</var> satisfy the condition (※).</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>s</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the AtCoDeer's maximum possible score.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>gpg </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>0 </pre> <p>Playing the same gesture as the opponent in each turn results in the score of <var>0</var>, which is the maximum possible score.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>ggppgggpgg </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 </pre> <p>For example, consider playing gestures in the following order: Rock, Paper, Rock, Paper, Rock, Rock, Paper, Paper, Rock, Paper. This strategy earns three victories and suffers one defeat, resulting in the score of <var>2</var>, which is the maximum possible score.</p></section> </div> </span>
[ [ "gpg\n", "gpg\n" ] ]
p03968
AtCoder Regular Contest 062 - Building Cubes with AtCoDeer
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>AtCoDeer the deer has <var>N</var> square tiles. The tiles are numbered <var>1</var> through <var>N</var>, and the number given to each tile is written on one side of the tile. Also, each corner of each tile is painted in one of the <var>1000</var> colors, which are represented by the integers <var>0</var> between <var>999</var>. The top-left, top-right, bottom-right and bottom-left corner of the tile with the number <var>i</var> are painted in color <var>C_{i,0}, C_{i,1}, C_{i,2}</var> and <var>C_{i,3}</var>, respectively, when seen in the direction of the number written on the tile (See Figure <var>1</var>).</p> <div style="text-align: center;"> <img src="https://atcoder.jp/img/arc062/b8ec940254d280135500dab6d00d4370.png"> <p>Figure <var>1</var>: The correspondence between the colors of a tile and the input</p> </img></div> <p>AtCoDeer is constructing a cube using six of these tiles, under the following conditions:</p> <ul> <li>For each tile, the side with the number must face outward.</li> <li>For each vertex of the cube, the three corners of the tiles that forms it must all be painted in the same color.</li> </ul> <p>Help him by finding the number of the different cubes that can be constructed under the conditions. Since each tile has a number written on it, two cubes are considered different if the set of the used tiles are different, or the tiles are used in different directions, even if the formation of the colors are the same. (Each tile can be used in one of the four directions, obtained by <var>90°</var> rotations.) Two cubes are considered the same only if rotating one in the three dimensional space can obtain an exact copy of the other, including the directions of the tiles.</p> <div style="text-align: center;"> <img src="https://atcoder.jp/img/arc062/8c7552f20698dab0aad52bba476fe6d7.png"> <p>Figure <var>2</var>: The four directions of a tile</p> </img></div> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>6≦N≦400</var></li> <li><var>0≦C_{i,j}≦999 (1≦i≦N , 0≦j≦3)</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>C_{1,0}</var> <var>C_{1,1}</var> <var>C_{1,2}</var> <var>C_{1,3}</var> <var>C_{2,0}</var> <var>C_{2,1}</var> <var>C_{2,2}</var> <var>C_{2,3}</var> <var>:</var> <var>C_{N,0}</var> <var>C_{N,1}</var> <var>C_{N,2}</var> <var>C_{N,3}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different cubes that can be constructed under the conditions.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 0 1 2 3 0 4 6 1 1 6 7 2 2 7 5 3 6 4 5 7 4 0 3 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 </pre> <p>The cube below can be constructed.</p> <p><img alt="" src="https://atcoder.jp/img/arc062/094fbca5395bfaaea28c98c51230693b.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>8 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 1 0 0 1 1 0 1 0 1 1 0 0 1 1 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>144 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>122880 </pre></section> </div> </span>
[ [ "6\n0 1 2 3\n0 4 6 1\n1 6 7 2\n2 7 5 3\n6 4 5 7\n4 0 3 5\n", "6\n0 1 2 3\n0 4 6 1\n1 6 7 2\n2 7 5 3\n6 4 5 7\n4 0 3 5\n" ] ]
p03969
AtCoder Regular Contest 062 - Painting Graphs with AtCoDeer
<span class="lang-en"> <p>Score : <var>1300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>One day, AtCoDeer the deer found a simple graph (that is, a graph without self-loops and multiple edges) with <var>N</var> vertices and <var>M</var> edges, and brought it home. The vertices are numbered <var>1</var> through <var>N</var> and mutually distinguishable, and the edges are represented by <var>(a_i,b_i) (1≦i≦M)</var>.</p> <p>He is painting each edge in the graph in one of the <var>K</var> colors of his paint cans. As he has enough supply of paint, the same color can be used to paint more than one edge.</p> <p>The graph is made of a special material, and has a strange property. He can choose a simple cycle (that is, a cycle with no repeated vertex), and perform a circular shift of the colors along the chosen cycle. More formally, let <var>e_1</var>, <var>e_2</var>, <var>...</var>, <var>e_a</var> be the edges along a cycle in order, then he can perform the following simultaneously: paint <var>e_2</var> in the current color of <var>e_1</var>, paint <var>e_3</var> in the current color of <var>e_2</var>, <var>...</var>, paint <var>e_a</var> in the current color of <var>e_{a-1}</var>, and paint <var>e_1</var> in the current color of <var>e_{a}</var>.</p> <div style="text-align: center;"> <img src="https://atcoder.jp/img/arc062/5fd1cd3003603ab14774f9342ccf2290.png"> <p>Figure <var>1</var>: An example of a circular shift</p> </img></div> <p>Two ways to paint the edges, <var>A</var> and <var>B</var>, are considered the same if <var>A</var> can be transformed into <var>B</var> by performing a finite number of circular shifts. Find the number of ways to paint the edges. Since this number can be extremely large, print the answer modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦50</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦K≦100</var></li> <li><var>1≦a_i,b_i≦N (1≦i≦M)</var></li> <li>The graph has neither self-loops nor multiple edges.</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>K</var> <var>a_1</var> <var>b_1</var> <var>a_2</var> <var>b_2</var> <var>:</var> <var>a_M</var> <var>b_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways to paint the edges, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 4 2 1 2 2 3 3 1 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>8 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 2 3 1 2 4 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>9 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>11 12 48 3 1 8 2 4 9 5 4 1 6 2 9 8 3 10 8 4 10 8 6 11 7 1 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>569519295 </pre></section> </div> </span>
[ [ "4 4 2\n1 2\n2 3\n3 1\n3 4\n", "4 4 2\n1 2\n2 3\n3 1\n3 4\n" ] ]
p03970
CODE FESTIVAL 2016 qual B - Signboard
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>CODE FESTIVAL 2016 is going to be held. For the occasion, Mr. Takahashi decided to make a signboard.</p> <p>He intended to write <code>CODEFESTIVAL2016</code> on it, but he mistakenly wrote a different string <var>S</var>. Fortunately, the string he wrote was the correct length.</p> <p>So Mr. Takahashi decided to perform an operation that replaces a certain character with another in the minimum number of iterations, changing the string to <code>CODEFESTIVAL2016</code>.</p> <p>Find the minimum number of iterations for the rewrite operation.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>S</var> is <var>16</var> characters long.</li> <li><var>S</var> consists of uppercase and lowercase alphabet letters and numerals.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Inputs are provided from Standard Input in the following form.</p> <pre><var>S</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Output an integer representing the minimum number of iterations needed for the rewrite operation.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>C0DEFESTIVAL2O16 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The second character <code>0</code> must be changed to <code>O</code> and the <var>14</var>th character <code>O</code> changed to <code>0</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>FESTIVAL2016CODE </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>16 </pre></section> </div> </span>
[ [ "C0DEFESTIVAL2O16\n", "C0DEFESTIVAL2O16\n" ] ]
p03971
CODE FESTIVAL 2016 qual B - Qualification simulator
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> participants in the CODE FESTIVAL 2016 Qualification contests. The participants are either students in Japan, students from overseas, or neither of these.</p> <p>Only Japanese students or overseas students can pass the Qualification contests. The students pass when they satisfy the conditions listed below, from the top rank down. Participants who are not students cannot pass the Qualification contests.</p> <ul> <li>A Japanese student passes the Qualification contests if the number of the participants who have already definitively passed is currently fewer than <var>A+B</var>.</li> <li>An overseas student passes the Qualification contests if the number of the participants who have already definitively passed is currently fewer than <var>A+B</var> and the student ranks <var>B</var>-th or above among all overseas students.</li> </ul> <p>A string <var>S</var> is assigned indicating attributes of all participants. If the <var>i</var>-th character of string <var>S</var> is <code>a</code>, this means the participant ranked <var>i</var>-th in the Qualification contests is a Japanese student; <code>b</code> means the participant ranked <var>i</var>-th is an overseas student; and <code>c</code> means the participant ranked <var>i</var>-th is neither of these.</p> <p>Write a program that outputs for all the participants in descending rank either <code>Yes</code> if they passed the Qualification contests or <code>No</code> if they did not pass.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N,A,B≦100000</var></li> <li><var>A+B≦N</var></li> <li><var>S</var> is <var>N</var> characters long.</li> <li><var>S</var> consists only of the letters <code>a</code>, <code>b</code> and <code>c</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Inputs are provided from Standard Input in the following form.</p> <pre><var>N</var> <var>A</var> <var>B</var> <var>S</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Output <var>N</var> lines. On the <var>i</var>-th line, output <code>Yes</code> if the <var>i</var>-th participant passed the Qualification contests or <code>No</code> if that participant did not pass.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>10 2 3 abccabaabb </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes Yes No No Yes Yes Yes No No No </pre> <p>The first, second, fifth, sixth, and seventh participants pass the Qualification contests.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>12 5 2 cabbabaacaba </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>No Yes Yes Yes Yes No Yes Yes No Yes No No </pre> <p>The sixth participant is third among overseas students and thus does not pass the Qualification contests.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 2 2 ccccc </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>No No No No No </pre></section> </div> </span>
[ [ "10 2 3\nabccabaabb\n", "10 2 3\nabccabaabb\n" ] ]
p03972
CODE FESTIVAL 2016 qual B - Gr-idian MST
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>On an <var>xy</var> plane, in an area satisfying <var>0 ≤ x ≤ W, 0 ≤ y ≤ H</var>, there is one house at each and every point where both <var>x</var> and <var>y</var> are integers.</p> <p>There are unpaved roads between every pair of points for which either the <var>x</var> coordinates are equal and the difference between the <var>y</var> coordinates is <var>1</var>, or the <var>y</var> coordinates are equal and the difference between the <var>x</var> coordinates is <var>1</var>.</p> <p>The cost of paving a road between houses on coordinates <var>(i,j)</var> and <var>(i+1,j)</var> is <var>p_i</var> for any value of <var>j</var>, while the cost of paving a road between houses on coordinates <var>(i,j)</var> and <var>(i,j+1)</var> is <var>q_j</var> for any value of <var>i</var>.</p> <p>Mr. Takahashi wants to pave some of these roads and be able to travel between any two houses on paved roads only. Find the solution with the minimum total cost.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ W,H ≦ 10^5</var></li> <li><var>1 ≦ p_i ≦ 10^8(0 ≦ i ≦ W-1)</var></li> <li><var>1 ≦ q_j ≦ 10^8(0 ≦ j ≦ H-1)</var></li> <li><var>p_i (0 ≦ i ≦ W−1)</var> is an integer.</li> <li><var>q_j (0 ≦ j ≦ H−1)</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Inputs are provided from Standard Input in the following form.</p> <pre><var>W</var> <var>H</var> <var>p_0</var> : <var>p_{W-1}</var> <var>q_0</var> : <var>q_{H-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Output an integer representing the minimum total cost.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 2 3 5 2 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>29 </pre> <p>It is enough to pave the following eight roads.</p> <ul> <li>Road connecting houses at <var>(0,0)</var> and <var>(0,1)</var></li> <li>Road connecting houses at <var>(0,1)</var> and <var>(1,1)</var></li> <li>Road connecting houses at <var>(0,2)</var> and <var>(1,2)</var></li> <li>Road connecting houses at <var>(1,0)</var> and <var>(1,1)</var></li> <li>Road connecting houses at <var>(1,0)</var> and <var>(2,0)</var></li> <li>Road connecting houses at <var>(1,1)</var> and <var>(1,2)</var></li> <li>Road connecting houses at <var>(1,2)</var> and <var>(2,2)</var></li> <li>Road connecting houses at <var>(2,0)</var> and <var>(2,1)</var></li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 3 2 4 8 1 2 9 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>60 </pre></section> </div> </span>
[ [ "2 2\n3\n5\n2\n7\n", "2 2\n3\n5\n2\n7\n" ] ]
p03973
CODE FESTIVAL 2016 qual B - Greedy customers
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p><var>N</var> people are waiting in a single line in front of the Takahashi Store. The cash on hand of the <var>i</var>-th person from the front of the line is a positive integer <var>A_i</var>.</p> <p>Mr. Takahashi, the shop owner, has decided on the following scheme: He picks a product, sets a positive integer <var>P</var> indicating its price, and shows this product to customers in order, starting from the front of the line. This step is repeated as described below.</p> <p>At each step, when a product is shown to a customer, if price <var>P</var> is equal to or less than the cash held by that customer at the time, the customer buys the product and Mr. Takahashi ends the current step. That is, the cash held by the first customer in line with cash equal to or greater than <var>P</var> decreases by <var>P</var>, and the next step begins.</p> <p>Mr. Takahashi can set the value of positive integer <var>P</var> independently at each step.</p> <p>He would like to sell as many products as possible. However, if a customer were to end up with <var>0</var> cash on hand after a purchase, that person would not have the fare to go home. Customers not being able to go home would be a problem for Mr. Takahashi, so he does not want anyone to end up with <var>0</var> cash.</p> <p>Help out Mr. Takahashi by writing a program that determines the maximum number of products he can sell, when the initial cash in possession of each customer is given.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ | N | ≦ 100000</var></li> <li><var>1 ≦ A_i ≦ 10^9(1 ≦ i ≦ N)</var></li> <li>All inputs are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Inputs are provided from Standard Inputs in the following form.</p> <pre><var>N</var> <var>A_1</var> : <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Output an integer representing the maximum number of products Mr. Takahashi can sell.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 2 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>As values of <var>P</var>, select in order <var>1, 4, 1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>15 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>18 </pre></section> </div> </span>
[ [ "3\n3\n2\n5\n", "3\n3\n2\n5\n" ] ]
p03974
CODE FESTIVAL 2016 qual B - Lexicographical disorder
<span class="lang-en"> <p>Score : <var>1100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> strings of lowercase alphabet only. The <var>i</var>-th string is <var>S_i</var>. Every string is unique.</p> <p>Provide answers for the <var>Q</var> queries below. The <var>i</var>-th query has the following format:</p> <p>Query: An integer <var>k_i</var> and a string <var>p_{i,1}p_{i,2}...p_{i,26}</var> that results from permuting {<code>a</code><var>,</var><code>b</code><var>,...,</var><code>z</code>} are given. Output the sequence of the string <var>S_{k_i}</var> among the <var>N</var> strings in lexicographical order when the literal sequence is <var>p_{i,1}&lt;p_{i,2}&lt;...&lt;p_{i,26}</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ N,Q ≦ 100000</var></li> <li><var>1 ≦ |S_i| (1 ≦ i ≦ N)</var></li> <li><var>S_i (1 ≦ i ≦ N)</var> is a string of lowercase alphabet.</li> <li>The sum of <var>|S_i|</var> is no more than <var>400000</var>.</li> <li>Every <var>S_i</var> is unique.</li> <li><var>1 ≦ k_i ≦ N (1 ≦ i ≦ Q)</var></li> <li>For all <var>1 ≦ i ≦ Q</var>, <var>p_{i,1}p_{i,2}...p_{i,26}</var> is a permutation of <code>abcd...z</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Inputs are provided from standard inputs in the following form.</p> <pre><var>N</var> <var>S_1</var> : <var>S_N</var> <var>Q</var> <var>k_1</var> <var>p_{1,1}p_{1,2}...p_{1,26}</var> : <var>k_Q</var> <var>p_{Q,1}p_{Q,2}...p_{Q,26}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Output <var>Q</var> lines.</p> <p>On line <var>i</var>, for the <var>i</var>-th query, output an integer indicating the sequence of the string <var>S_{k_i}</var> among the <var>N</var> strings in lexicographical order.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 aa abbaa abbba aaab aaaaaba 5 1 abcdefghijklmnopqrstuvwxyz 2 bacdefghijklmnopqrstuvwxyz 3 abcdefghijklmnopqrstuvwxyz 4 bacdefghijklmnopqrstuvwxyz 5 abcdefghijklmnopqrstuvwxyz </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 5 4 2 </pre> <p>When the literal sequence is <code>a</code> <var>&lt;</var> <code>b</code>, sorting the input strings in lexicographical order yields <code>aa</code>, <code>aaaaaba</code>, <code>aaab</code>, <code>abbaa</code>, <code>abbba</code>. The answers to queries <var>1</var>, <var>3</var>, and <var>5</var> are thus <var>1</var>, <var>5</var>, and <var>2</var>, respectively.</p> <p>When the literal sequence is <code>b</code> <var>&lt;</var> <code>a</code>, sorting the input strings in lexicographical order yields <code>abbba</code>, <code>abbaa</code>, <code>aa</code>, <code>aaab</code>, <code>aaaaaba</code>. The answers to queries <var>2</var> and <var>4</var> are thus <var>2</var> and <var>4</var>, respectively.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>8 abrakatabra abadaba abracadabra atcoder grand contest ababa a 6 3 abcdefghijklmnopqrstuvwxyz 6 qwertyuiopasdfghjklzxcvbnm 8 poiuytrewqlkjhgfdsamnbvcxz 2 qazwsxedcrfvtgbyhnujmikolp 1 plokmijnuhbygvtfcrdxeszwaq 4 mnbvcxzasdfghjklpoiuytrewq </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>4 8 2 3 4 7 </pre></section> </div> </span>
[ [ "5\naa\nabbaa\nabbba\naaab\naaaaaba\n5\n1 abcdefghijklmnopqrstuvwxyz\n2 bacdefghijklmnopqrstuvwxyz\n3 abcdefghijklmnopqrstuvwxyz\n4 bacdefghijklmnopqrstuvwxyz\n5 abcdefghijklmnopqrstuvwxyz\n", "5\naa\nabbaa\nabbba\naaab\naaaaaba\n5\n1 abcdefghijklmnopqrstuvwxyz\n2 bacdefghijklmnopqrstuvwxyz\n3 abcdefghijklmnopqrstuvwxyz\n4 bacdefghijklmnopqrstuvwxyz\n5 abcdefghijklmnopqrstuvwxyz\n" ] ]
p03975
Kyoto University Programming Contest 2016 - A Barricade
<span class="lang-en"> <p>Score : <var>66</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p> Summer vacation ended at last and the second semester has begun. You, a Kyoto University student, came to university and heard a rumor that somebody will barricade the entrance of your classroom. The barricade will be built just before the start of the <var>A</var>-th class and removed by Kyoto University students just before the start of the <var>B</var>-th class. All the classes conducted when the barricade is blocking the entrance will be cancelled and you will not be able to attend them. Today you take <var>N</var> classes and class <var>i</var> is conducted in the <var>t_i</var>-th period. You take at most one class in each period. Find the number of classes you can attend. </p> </section> </div> <div class="part"> <section> <h3>Constraints</h3> <ul> <li><var>1 \leq N \leq 1000</var></li> <li><var>1 \leq A &lt; B \leq 10^9 </var></li> <li><var>1 \leq t_i \leq 10^9</var></li> <li>All <var>t_i</var> values are distinct.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3> <p><var>N</var>, <var>A</var> and <var>B</var> are given on the first line and <var>t_i</var> is given on the <var>(i+1)</var>-th line. </p> <pre> <var>N</var> <var>A</var> <var>B</var> <var>t<sub>1</sub></var> : <var>t<sub>N</sub></var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3> <p>Print the number of classes you can attend. </p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3> <pre> 5 5 9 4 3 6 9 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3> <pre> 4 </pre> <p>You can not only attend the third class. </p> </section> </div> <div class="part"> <section> <h3>Sample Input 2</h3> <pre> 5 4 9 5 6 7 8 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3> <pre> 1 </pre> <p>You can only attend the fifth class. </p> </section> </div> <div class="part"> <section> <h3>Sample Input 3</h3> <pre> 4 3 6 9 6 8 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3> <pre> 4 </pre> <p>You can attend all the classes. </p> </section> </div> <div class="part"> <section> <h3>Sample Input 4</h3> <pre> 2 1 2 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3> <pre> 1 </pre> <p>You can not attend the first class, but can attend the second. </p> </section> </div> </span>
[ [ "5 5 9\n4\n3\n6\n9\n1\n", "5 5 9\n4\n3\n6\n9\n1\n" ] ]
p03976
Kyoto University Programming Contest 2016 - Problem Committee
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <blockquote> <p> Kyoto University Programming Contest is a programming contest voluntarily held by some Kyoto University students. This contest is abbreviated as <b>K</b>yoto <b>U</b>niversity <b>P</b>rogramming <b>C</b>ontest and called KUPC. </p> <p> source: <a href="http://www.kupc.jp/index.html"><i>Kyoto University Programming Contest Information</i></a> </p> </blockquote> <p> The problem-preparing committee met to hold this year's KUPC and <var>N</var> problems were proposed there. The problems are numbered from <var>1</var> to <var>N</var> and the name of <var>i</var>-th problem is <var>P_i</var>. However, since they proposed too many problems, they decided to divide them into some sets for several contests. </p> <p> They decided to divide this year's KUPC into several KUPCs by dividing problems under the following conditions. <ul> <li> One KUPC provides <var>K</var> problems. </li> <li> Each problem appears at most once among all the KUPCs. </li> <li> All the first letters of the problem names in one KUPC must be different. </li> </ul> </p> <p> You, one of the committee members, want to hold as many KUPCs as possible. Write a program to find the maximum number of KUPCs that can be held this year. </p> <h3>Constraints</h3> <ul> <li> <var>1 \leq N \leq 10^4</var></li> <li> <var>1 \leq K \leq 26</var></li> <li> <var>1 \leq |P_i| \leq 10</var></li> <li> All characters in <var>P_i</var> are capital letters.</li> </ul> <p> Note that, for each <var>i</var> and <var>j</var> <var>(1 \leq i &lt; j \leq N)</var>, <var>P_i \neq P_j</var> are not necessarily satisfied. </p> </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_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3> <p> Print the maximum number of KUPCs that can be held on one line. </p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3> <pre> 9 3 APPLE ANT ATCODER BLOCK BULL BOSS CAT DOG EGG </pre> <p>For example, three KUPCs can be held by dividing the problems as follows. </p> <ul> <li> First: <code>APPLE</code>, <code>BLOCK</code>, <code>CAT</code></li> <li> Second: <code>ANT</code>, <code>BULL</code>, <code>DOG</code></li> <li> Third: <code>ATCODER</code>, <code>BOSS</code>, <code>EGG</code></li> </ul> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3> <pre> 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Input 2</h3> <pre> 3 2 KU KYOUDAI KYOTOUNIV </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3> <pre> 0 </pre> <p>No KUPC can be held.</p> </section> </div> </span>
[ [ "9 3\nAPPLE\nANT\nATCODER\nBLOCK\nBULL\nBOSS\nCAT\nDOG\nEGG\n", "9 3\nAPPLE\nANT\nATCODER\nBLOCK\nBULL\nBOSS\nCAT\nDOG\nEGG\n" ] ]
p03977
Kyoto University Programming Contest 2016 - Cookie Breeding Machine
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"><section> <h3>Problem Statement</h3> <p>A professor invented Cookie Breeding Machine for his students who like cookies very much. </p> <p>When one cookie with the taste of <var>x</var> is put into the machine and a non-negative integer <var>y</var> less than or equal to <var>127</var> is input on the machine, it consumes the cookie and generates two cookies with the taste of <var>y</var> and (<var>x</var> XOR <var>y</var>). </p> <p>Here, XOR represents <a href="https://en.wikipedia.org/wiki/Exclusive_or">Bitwise Exclusive OR</a>. </p> <p>At first, there is only one cookie and the taste of it is <var>D</var> . </p> <p>Find the maximum value of the sum of the taste of the exactly <var>N</var> cookies generated after the following operation is conducted <var>N-1</var> times. </p> <ol> <li>Put one of the cookies into the machine. </li> <li>Input a non-negative integer less than or equal to <var>127</var> on the machine. </li> </ol> </section></div> <div class="part"><section> <h3>Constraints</h3> <ul> <li><var>1 \leq T \leq 1000</var></li> <li><var>1 \leq N_t \leq 1000</var> <var>(1 \leq t \leq T)</var></li> <li><var>1 \leq D_t \leq 127</var> <var>(1 \leq t \leq T)</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>T</var> <var>N_1</var> <var>D_1</var> : <var>N_T</var> <var>D_T</var> </pre> <p> The input consists of multiple test cases. An Integer <var>T</var> that represents the number of test cases is given on line <var>1</var>. <br/> Each test case is given on the next <var>T</var> lines. <br/> In the <var>t</var>-th test case (<var> 1 \leq t \leq T </var>), <var>N_t</var> that represents the number of cookies generated through the operations and <var>D_t</var> that represents the taste of the initial cookie are given separated by space. <br/> </p> </section></div> <div class="part"><section> <h3>Output</h3> <p>For each test case, print the maximum value of the sum of the taste of the <var>N</var> cookies generated through the operations on one line. </p> </section></div> </div> <hr/> <div class="part"><section> <h3>Sample Input 1</h3> <pre> 3 3 1 4 108 1 10 </pre> </section></div> <div class="part"><section> <h3>Sample Output 1</h3> <pre> 255 400 10 </pre> <p> On the 1st test case, if the machine is used as follows, three cookies with the taste of <var>61</var>, <var>95</var> and <var>99</var> are generated. Since the sum of these values is maximum among all possible ways, the answer is <var>255</var>. <ol> <li>Put the cookie with the taste of <var>1</var> and input an integer <var>60</var> on the machine, lose the cookie and get two cookies with the taste of <var>60</var> and <var>61</var>. </li> <li>Put the cookie with the taste of <var>60</var> and input an integer <var>99</var> on the machine, lose the cookie and get two cookies with the taste of <var>99</var> and <var>95</var>. </li> </ol> </p> <p> On the 3rd test case, the machine may not be used. </p> </section></div> </span>
[ [ "3\n3 1\n4 108\n1 10\n", "3\n3 1\n4 108\n1 10\n" ] ]
p03978
Kyoto University Programming Contest 2016 - Long Blackboard
<span class="lang-en"> <p>Score : <var>150</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p> There is a long blackboard with <var>2</var> rows and <var>N</var> columns in a classroom of Kyoto University. This blackboard is so long that it is impossible to tell which cells are already used and which unused. </p> <p> Recently, a blackboard retrieval device was installed at the classroom. To use this device, you type a search query that forms a rectangle with 2 rows and any length of columns, where each cell is used or unused. When you input a query, the decive answers whether the rectangle that corresponds to the query exists in the blackboard. Here, for a rectangle that corresponds to a search query, if two integer <var>i, j</var> ( <var>i &lt; j</var> ) exist and the rectangle equals to the partial blackboard between column <var>i</var> and <var>j</var> , the rectangle is called a sub-blackboard of the blackboard. </p> <p> You are currently preparing for a presentation at this classroom. To make the presentation go well, you decided to write a program to detect the status of the whole blackboard using the retrieval device. Since it takes time to use the device, you want to use it as few times as possible. </p> <p> The status of the whole blackboard is already determined at the beginning and does not change while you are using the device. </p> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3> <p>The first input is given in the following format:</p> <pre> <var>N</var> </pre> <p> <var>N</var> <var>(1 \leq N \leq 100)</var> is an integer that represents the length of the blackboard. </p> <p> After this input value, your program must print search queries. A search query has the following format. </p> <pre> <var>s_1</var> <var>s_2</var> </pre> <p> Here, <var>s_1</var> represents the upper part of the blackboard and <var>s_2</var> represents the lower. <code>#</code> in <var>s_1</var> and <var>s_2</var> represents the cell is already used and <code>.</code> represents the cell is still unused. The lengths of <var>s_1</var> and <var>s_2</var> are arbitrary, but they must be the same. Make sure to insert a line break at the end of the lines. </p> <p> Every time your program prints a search query, a string that represents the search result of the device is returned in the followin format. </p> <pre> <var>r</var> </pre> <p> <var>r</var> is either <code>T</code> or <code>F</code> . The meaning of each character is as follows. </p> <ul> <li> <code>T</code> represents that the sub-blackboard that corresponds to the search query exists in the blackboard. </li> <li> <code>F</code> represents that the sub-blackboard that corresponds to the search query does not exist in the blackboard. </li> </ul> <p> If the search query equals to the whole blackboard or the number of the search queries exceeds the limit, string <code>end</code> is given instead of <var>r</var> . Once you receive this string, exit your program immediately. If your program prints the whole blackboard as a search query before exceedin the limit, it is judged as <em>Accepted</em>. Note that the search query that represents the whole blackboard is also counted as the number of search queries. </p> </section> </div> </div> <p> Note that the output needs to be flushed every time the output is printed. For example, In C/C++, search query <code>s1</code>, <code>s2</code> can be printed as follows. </p> <pre class="prettyprint">printf("%s\n%s\n", s1, s2); fflush(stdout);</pre> <p> Make sure your program receive all the input from the device. Otherwise, the result may be <em>Time Limit Exceeded</em> . </p> <hr/> <div class="part"> <section> <h3>Query Limit</h3> <p> The maximun number of search queries is <var>420</var>. If the number of queries exceeds the limit, the result will be <em>Query Limit Exceeded</em> . </p> <hr/> </section> </div> <div class="part"> <section> <h3>Sample Input and Output</h3> <p>The following is an example where <var>N=3</var> and the blackboard is as follows. </p> <pre> .#. ... </pre> <p>Note that your program does not know the state of the blackboard. </p> <pre> <table class="table table-striped table-bordered table-condensed"> <tr> <th>Output of your program</th> <th>Input to your program</th> <th>Explanation</th> </tr> <tr> <td></td> <td>3</td> <td>The length of the blackboard is given</td> </tr> <tr> <td>..<br/>##</td> <td></td> <td>Output a search query</td> </tr> <tr> <td></td> <td>F</td> <td>The sub-blackboard does not exist</td> </tr> <tr> <td>.<br/>.</td> <td></td> <td>Output a search query</td> </tr> <tr> <td></td> <td>T</td> <td>The sub-blackboard exists</td> </tr> <tr> <td>..<br/>..</td> <td></td> <td>Output a search query</td> </tr> <tr> <td></td> <td>F</td> <td>The sub-blackboard does not exist</td> </tr> <tr> <td>.#<br/>..</td> <td></td> <td>Output a search query</td> </tr> <tr> <td></td> <td>T</td> <td>The sub-blackboard exists</td> </tr> <tr> <td>.#.<br/>...</td> <td></td> <td>Output a search query</td> </tr> <tr> <td></td> <td>end</td> <td>Exit your program because the above sub-blackboard equals to the whole blackboard. </td> </tr> </table> </pre> </section> </div> </span>
[ [ "", "" ] ]
p03979
Kyoto University Programming Contest 2016 - Fences
<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>
[ [ "", "" ] ]
p03980
Kyoto University Programming Contest 2016 - Speed Solving
<span class="lang-en"> <p>Score : <var>150</var> points</p> <div id="task-statement"> <script src="http://atcoder.jp/js/varmath.js"></script> <div class="part"> <section> <h3>Problem Statement</h3> <p> Gorillas in Kyoto University are good at math. They are currently trying to solve problems to find the value of an expression that contains two functions, <code>_</code> , <code>^</code>. Each of these functions takes two input values. <code>_</code> function returns the smaller of the two input values and <code>^</code> function returns the larger. Gorillas know that integers in the expression are non-negative and less than or equal to <var>99</var>, but can not find out the length of the expression until they read a terminal symbol <code>?</code> that represents the end of the expression. The number of characters included in each expression is less than or equal to 1000, but they do not even know this fact. Ai, a smart gorilla, noticed that she may be able to know the value of the expression even if they don't read the whole expression. </p> <p> For example, </p> <p>Assume you read the following sentence from the left.</p> <pre>^(41,3)?</pre> <p>When you read the sixth character, that is, when you read the following expression,</p> <pre>^(41,3</pre> <p>you can tell the second input value of the funcion is whether <var>3</var> or an integer between <var>30</var> and <var>39</var>, and the value turns out <var>41</var>. </p> <p> Since Ai wants to solve problems earlier than other gorillas, she decided to solve the problems such that she reads as fewer characters as possible from the left. For each expression, Find the value of the expression and the minimum number of characters Ai needs to read to know the value. </p> </section> </div> <div class="part"> <section> <h3>Constraints</h3> <ul> <li><var>1 \leq Q \leq 200</var></li> <li>The number of characters each expression contains is less than or equal to <var>1000</var>. </li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3> <p>The input consists of multiple test cases and is given from Standard Input in the following format:</p> <pre> <var>Q</var> <var>statement_1</var> ... <var>statement_Q</var> </pre> </section> </div> </div> <p>Each <var>statement_i</var> <var>(1 \leq i \leq Q)</var> is given in the following BNF format.</p> <pre> <var>&lt;statement&gt;</var> ::= <var>&lt;expression&gt;</var> <code>?</code> <var>&lt;expression&gt;</var> ::= (<code>^</code> | <code>_</code>) <code>(</code> <var>&lt;expression&gt;</var> <code>,</code> <var>&lt;expression&gt;</var> <code>)</code> | <var>&lt;number&gt; </var> <var>&lt;number&gt;</var> :: = <code>0</code> | <code>1</code> | <code>2</code> | ... | <code>98</code> | <code>99</code> </pre> <div class="io-style"> <div class="part"> <section> <h3>Output</h3> <p> Output consists of <var>Q</var> lines. On line <var>i</var> <var>(1 \leq i \leq Q)</var>, print the value of the expression and the number of character Ai needs to read for the test case <var>i</var> separated by space. </p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3> <pre> 4 _(4,51)? ^(99,_(3,67))? _(0,87)? 3? </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3> <pre> 4 5 99 4 0 3 3 2 </pre> <ul> <li>For the first test case, when you read the fifth character, that is, when you read <code>_(4,5</code>, you will know the value is <var>4</var>. </li> <li>For the second test case, when you read the fourth character, that is, when you read <code>^(99</code>, you will know the value is <var>99</var>. </li> <li>For the third test case, when you read the third character, that is, when you read <code>_(0</code>, you will know the value is <var>0</var>. </li> <li>For the fourth test case, you will not know the value is <var>3</var> untill you read the terminal symbol.</li> </ul> </section> </div> <div class="part"> <section> <h3>Sample Input 2</h3> <pre> 7 _(23,^(_(22,40),4))? _(0,99)? ^(99,_(^(19,2),5))? _(^(43,20),^(30,29))? ^(_(20,3),_(50,41))? ^(_(20,3),_(3,41))? ^(_(20,3),_(4,41))? </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3> <pre> 22 18 0 3 99 4 30 17 41 17 3 14 4 15 </pre> </section> </div> </div> </span>
[ [ "4\n_(4,51)?\n^(99,_(3,67))?\n_(0,87)?\n3?\n", "4\n_(4,51)?\n^(99,_(3,67))?\n_(0,87)?\n3?\n" ] ]
p03981
Kyoto University Programming Contest 2016 - Exam
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p> You are going to take the entrance examination of Kyoto University tomorrow and have decided to memorize a set of strings <var>S</var> that is expected to appear in the examination. Since it is really tough to memorize <var>S</var> as it is, you have decided to memorize a single string <var>T</var> that efficiently contains all the strings in <var>S</var>. </p> You have already confirmed the following conditions for <var>S</var> and <var>T</var> are satisfied. <ul> <li>Every string in <var>S</var> is a <strong>consecutive</strong> subsequence<sup>(1)</sup> in <var>T</var> . </li> <li>For every pair of strings <var>x</var>, <var>y</var> <var>(x \neq y)</var> in <var>S</var> , <var>x</var> is not a subsequence<sup>(2)</sup> of <var>y</var>. </li> </ul> <p> Note that (1) is ''consecutive subsequence'', while (2) is ''subsequence''. </p> <p> The next day, you opened the problem booklet at the examination and found that you can get a full score only if you remember <var>S</var>. However, You have forgot how to reconstruct <var>S</var> from <var>T</var> . Since all you remember is that <var>T</var> satisfies the above conditions, first you have decided to find the maximum possible number of elements in <var>S</var> . </p> </section> </div> <div class="part"> <section> <h3>Constraints</h3> <ul> <li><var>1 \leq |T| \leq 10^5</var></li> <li><var>T</var> consists of only lowercase letters. </li> </ul> <h3>Partial points</h3> <ul> <li><var>30</var> points will be awarded for passing the test set satisfying the condition: <var>1 \leq |T| \leq 50</var> .</li> <li>Another <var>30</var> points will be awarded for passing the test set satisfying the condition: <var>1 \leq |T| \leq 10^3</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>T</var></pre> <p>The input only consists of <var>T</var> on one line. </p> </section> </div> <div class="part"> <section> <h3>Output</h3> <p>Print the maximum number of elements in<var>S</var> .</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3> <pre>abcabc</pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3> <pre>3</pre> In this case, <code>ab</code>,<code>ca</code> and <code>bc</code> are an example of the optimum answers. </section> </div> <div class="part"> <section> <h3>Sample Input 2</h3> <pre>abracadabra</pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3> <pre>7</pre> </section> </div> <div class="part"> <section> <h3>Sample Input 3</h3> <pre>abcbabbcabbc</pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3> <pre>8</pre> </section> </div> <div class="part"> <section> <h3>Sample Input 4</h3> <pre>bbcacbcbcabbabacccbbcacbaaababbacabaaccbccabcaabba</pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3> <pre>44</pre> </section> </div> </span>
[ [ "abcabc\n", "abcabc\n" ] ]
p03982
Kyoto University Programming Contest 2016 - WAAAAAAAAAAAAALL
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"><section> <h3>Problem Statement</h3> <p> Kyoto University decided to build a straight wall on the west side of the university to protect against gorillas that attack the university from the west every night. Since it is difficult to protect the university at some points along the wall where gorillas attack violently, reinforcement materials are also built at those points. Although the number of the materials is limited, the state-of-the-art technology can make a prediction about the points where gorillas will attack next and the number of gorillas that will attack at each point. The materials are moved along the wall everyday according to the prediction. You, a smart student majoring in computer science, are called to find the way to move the materials more efficiently. </p> <p> Theare are <var>N</var> points where reinforcement materials can be build along the straight wall. They are numbered <var>1</var> through <var>N</var>. Because of the protection against the last attack of gorillas, <var>A_i</var> materials are build at point <var>i</var> (<var>1 \leq i \leq N</var>). For the next attack, the materials need to be rearranged such that at least <var>B_i</var> materials are built at point <var>i</var> (<var>1 \leq i \leq N</var>). It costs <var>|i - j|</var> to move <var>1</var> material from point <var>i</var> to point <var>j</var>. Find the minimum total cost required to satisfy the condition by moving materials. You do not need to consider the attack after the next. </p> </section></div> <div class="part"><section> <h3>Constraints</h3> <ul> <li><var>1 \leq N \leq 10^5</var></li> <li><var>A_i \geq 1</var></li> <li><var>B_i \geq 1</var></li> <li><var>A_1 + A_2 + ... + A_N \leq 10^{12}</var></li> <li><var>B_1 + B_2 + ... + B_N \leq A_1 + A_2 + ... + A_N</var></li> <li>There is at least one way to satisfy the condition. </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> <var>B_1</var> <var>B_2</var> ... <var>B_N</var></pre> </section></div> <div class="part"><section> <h3>Output</h3> <p>Print the minimum total cost required to satisfy the condition. </p> <h3>Partial Scores</h3> <p> <var>30</var> points will be awarded for passing the test set satisfying the following: <ul> <li><var>N \leq 100</var></li> <li><var>A_1 + A_2 + ... + A_N \leq 400</var></li> </ul> </p> <p> Another <var>30</var> points will be awarded for passing the test set satisfying the following: <ul> <li><var>N \leq 10^3</var></li> </ul> </p> <p> Another <var>140</var> points will be awarded for passing the test set without addtional constraints and you can get <var>200</var> points in total. </p> </section></div> </div> <hr/> <div class="part"><section> <h3>Sample Input 1</h3> <pre>2 1 5 3 1</pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3> <pre>2</pre> <p>It costs least to move <var>2</var> materials from point <var>2</var> to point <var>1</var>. </p> </section></div> <div class="part"><section> <h3>Sample Input 2</h3> <pre>5 1 2 3 4 5 3 3 1 1 1</pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3> <pre>6</pre> </section></div> <div class="part"><section> <h3>Sample Input 3</h3> <pre>27 46 3 4 2 10 2 5 2 6 7 20 13 9 49 3 8 4 3 19 9 3 5 4 13 9 5 7 10 2 5 6 2 6 3 2 2 5 3 11 13 2 2 7 7 3 9 5 13 4 17 2 2 2 4</pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3> <pre>48</pre> <p>The input of this test case satisfies both the first and second additional constraints. </p> </section></div> <div class="part"><section> <h3>Sample Input 4</h3> <pre>18 3878348 423911 8031742 1035156 24256 10344593 19379 3867285 4481365 1475384 1959412 1383457 164869 4633165 6674637 9732852 10459147 2810788 1236501 770807 4003004 131688 1965412 266841 3980782 565060 816313 192940 541896 250801 217586 3806049 1220252 1161079 31168 2008961</pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3> <pre>6302172</pre> <p>The input of this test case satisfies the second additional constraint. </p> </section></div> <div class="part"><section> <h3>Sample Input 5</h3> <pre>2 1 99999999999 1234567891 1</pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3> <pre>1234567890</pre> <p>The input and output values may exceed the range of 32-bit integer. </p> </section></div> </span>
[ [ "2\n1 5\n3 1\n", "2\n1 5\n3 1\n" ] ]
p03983
Kyoto University Programming Contest 2016 - Handing out leaflets
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p> Eli- <var>1</var> started a part-time job handing out leaflets for <var>N</var> seconds. Eli- <var>1</var> wants to hand out as many leaflets as possible with her special ability, Cloning. Eli- <var>gen</var> can perform two kinds of actions below. </p> <ul> <li> Clone herself and generate Eli- <var>(gen + 1)</var> . (one Eli- <var>gen</var> (cloning) and one Eli- <var>(gen + 1)</var> (cloned) exist as a result of Eli-<var>gen</var> 's cloning.) This action takes <var>gen \times C</var> ( <var>C</var> is a coefficient related to cloning. ) seconds. </li> <li> Hand out one leaflet. This action takes one second regardress of the generation ( <var>=gen</var> ). </li> </ul> <p> They can not hand out leaflets while cloning. Given <var>N</var> and <var>C</var> , find the maximum number of leaflets Eli- <var>1</var> and her clones can hand out in total modulo <var>1000000007</var> (<var>= 10^9 + 7</var>). </p> </section> </div> <div class="part"> <section> <h3>Constraints</h3> <ul> <li> <var>1 \leq Q \leq 100000 = 10^5</var> </li> <li> <var>1 \leq N_q \leq 100000 = 10^5</var> </li> <li> <var>1 \leq C_q \leq 20000 = 2 \times 10^4</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>Q</var> <var>N_1</var> <var>C_1</var> : <var>N_Q</var> <var>C_Q</var> </pre> <p>The input consists of multiple test cases. On line <var>1</var> , <var>Q</var> that represents the number of test cases is given. Each test case is given on the next <var>Q</var> lines. For the test case <var>q</var> ( <var>1 \leq q \leq Q</var> ) , <var>N_q</var> and <var>C_q</var> are given separated by a single space. <var>N_q</var> and <var>C_q</var> represent the working time and the coefficient related to cloning for test case <var>q</var> respectively. </p> </section> </div> <div class="part"> <section> <h3>Output</h3> <p> For each test case, Print the maximum number of leaflets Eli- <var>1</var> and her clones can hand out modulo <var>1000000007</var> ( <var>= 10^9 + 7</var> ). </p></section> </div> <div class="part"> <section> <h3>Partial Scores</h3> <p> <var>30</var> points will be awarded for passing the test set satisfying the condition: <var>Q = 1</var> . </p> <p>Another <var>270</var> points will be awarded for passing the test set without addtional constraints and you can get <var>300</var> points in total. </p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3> <pre> 2 20 8 20 12 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3> <pre> 24 20 </pre> <p> For the first test case, while only <var>20</var> leaflets can be handed out without cloning, <var>24</var> leaflets can be handed out by cloning first and two people handing out<var>12</var> leaflets each. </p> <p> For the second test case, since two people can only hand out <var>8</var> leaflets each if Eli- <var>1</var> clones, she should hand out <var>20</var> leaflets without cloning. </p> </section> </div> <div class="part"> <section> <h3>Sample Input 2</h3> <pre> 1 20 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3> <pre> 67 </pre> <p>One way of handing out 67 leaflets is like the following image. Each black line means cloning, and each red line means handing out. </p> <div style="text-align: center;"> <img src="/img/other/kupc2016/sushi/sample2.png"/> </div> <p> This case satisfies the constraint of the partial score. </p> </section> </div> <div class="part"> <section> <h3>Sample Input 3</h3> <pre> 1 200 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3> <pre> 148322100 </pre> <p> Note that the value modulo <var>1000000007</var> ( <var>10^9 + 7</var> ) must be printed. </p> <p> This case satisfies the constraint of the partial score. </p> </section> </div> </span>
[ [ "2\n20 8\n20 12\n", "2\n20 8\n20 12\n" ] ]
p03984
Kyoto University Programming Contest 2016 - Coloring
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p> Mikan's birthday is coming soon. Since Mikan likes graphs very much, Aroma decided to give her a undirected graph this year too. </p> <p> Aroma bought a connected undirected graph, which consists of <var>n</var> vertices and <var>n</var> edges. The vertices are numbered from 1 to n and for each <var>i</var><var>(1 \leq i \leq n)</var>, vertex <var>i</var> and vartex <var>a_i</var> are connected with an undirected edge. Aroma thinks it is not interesting only to give the ready-made graph and decided to paint it. Count the number of ways to paint the vertices of the purchased graph in <var>k</var> colors modulo <var>10^9 + 7</var>. <strong>Two ways are considered to be the same if the graph painted in one way can be identical to the graph painted in the other way by permutating the numbers of the vertices. </strong> </p> </section> </div> <div class="part"> <section> <h3>Constraints</h3> <ul> <li> <var>1 \leq n \leq 10^5</var> </li> <li> <var>1 \leq k \leq 10^5</var> </li> <li> <var>1 \leq a_i \leq n</var> <var>(1 \leq i \leq n)</var> </li> <li> The given graph is connected</li> <li> The given graph contains no self-loops or multiple edges. </li></ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3> <p>Each data set is given in the following format from the standard input.</p> <pre> <var>n</var> <var>k</var> <var>a_1</var> : <var>a_n</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3> <p>Output the number of ways to paint the vertices of the given graph in k colors modulo <var>10^9 + 7</var> in one line. </p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3> <pre> 4 2 2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3> <pre> 12 </pre> </section> </div> <div class="part"> <section> <h3>Sample Input 2</h3> <pre> 4 4 2 3 4 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3> <pre> 55 </pre> </section> </div> <div class="part"> <section> <h3>Sample Input 3</h3> <pre> 10 5 2 3 4 1 1 1 2 3 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3> <pre> 926250 </pre> </section> </div> </span>
[ [ "4 2\n2\n3\n1\n1\n", "4 2\n2\n3\n1\n1\n" ] ]
p03985
Kyoto University Programming Contest 2016 - Hundred Eyes Monster
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Story</h3> <p>I am a Summoner who can invoke monsters. I can embody the pictures of monsters drawn on sketchbooks. <p>I recently have received a request from Primate Research Institute of Kyoto University to invoke "Hundred Eyes Monster". <p>What an great honor it is to contribute to the prosperity of the primate research of Kyoto University! I decided to paint an extremely high-quarity monster to the best of my ability. </p> <p>I have already painted the forehead and mouth and this time I am going to paint as many eyes as possible between the forehead and mouth. Well...I cannot wait to see the complete painting. <h3>Problem Statement</h3> <p> Two circles <var>A</var>, <var>B</var> are given on a two-dimensional plane. The coordinate of the center and radius of circle <var>A</var> is (<var>x_A</var>, <var>y_A</var>) and <var>r_A</var> respectively. The coordinate of the center and radius of circle <var>B</var> is (<var>x_B</var>, <var>y_B</var>) and <var>r_B</var> respectively. These two circles have no intersection inside. Here, we consider a set of circles <var>S</var> that satisfies the following conditions. </p> <ul> <li>Each circle in <var>S</var> touches both <var>A</var> and <var>B</var>, and does not have common points with them inside the circle. </li> <li>Any two different circles in <var>S</var> have no common points inside them. </li></ul> <p> Write a program that finds the maximum number of elements in <var>S</var>. </p> </p></p></p></section> </div> <div class="part"> <section> <h3>Constraints</h3> <ul> <li><var>1 \leq T \leq 50000</var></li> <li><var>-10^5 \leq x_A \leq 10^5</var></li> <li><var>-10^5 \leq y_A \leq 10^5</var></li> <li><var>-10^5 \leq x_B \leq 10^5</var></li> <li><var>-10^5 \leq y_B \leq 10^5</var></li> <li><var>1 \leq r_A \leq 10^5</var></li> <li><var>1 \leq r_B \leq 10^5</var></li> <li><var>{r_A}^2 + {r_B}^2 &lt; (x_A - x_B)^2 + (y_A - y_B)^2</var></li> <li>All values given as input are integers.</li> <li>It is guaranteed that, when the radiuses of <var>A</var> and <var>B</var> change by <var>0.0005</var>, the answer does not change.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3> <p>The input consists of multiple test cases and is given from Standard Input in the following format: <pre> <var>T</var> <var>testcase_1</var> : <var>testcase_T</var> </pre> Each test case is given with the following format. <pre> <var>x_A</var> <var>y_A</var> <var>r_A</var> <var>x_B</var> <var>y_B</var> <var>r_B</var> </pre> </p></section> </div> <div class="part"> <section> <h3>Output</h3> <p> The output consists of <var>T</var> lines. On line <var>i</var> (<var>1 \leq i \leq T</var>), putput the maximum number of elements in <var>S</var> in <var>i</var>-th test case. </p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3> <pre> 4 0 -3 2 0 3 2 0 0 9 8 8 2 0 0 9 10 10 5 0 0 707 1000 1000 707 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3> <pre> 3 10 21 180 </pre> You can arrange circles ,for example, like the following images in Sample 1, 2. <br/> <div style="text-align: center;"> <img src="/img/other/kupc2016/sushi/img00.png"/> <img src="/img/other/kupc2016/sushi/img01.png"/> </div> </section> </div> </span>
[ [ "4\n0 -3 2 0 3 2\n0 0 9 8 8 2\n0 0 9 10 10 5\n0 0 707 1000 1000 707\n", "4\n0 -3 2 0 3 2\n0 0 9 8 8 2\n0 0 9 10 10 5\n0 0 707 1000 1000 707\n" ] ]
p03986
AtCoder Grand Contest 005 - STring
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a string <var>X</var>, which has an even number of characters. Half the characters are <code>S</code>, and the other half are <code>T</code>.</p> <p>Takahashi, who hates the string <code>ST</code>, will perform the following operation <var>10^{10000}</var> times:</p> <ul> <li>Among the occurrences of <code>ST</code> in <var>X</var> as (contiguous) substrings, remove the leftmost one. If there is no occurrence, do nothing.</li> </ul> <p>Find the eventual length of <var>X</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≦ |X| ≦ 200,000</var></li> <li>The length of <var>X</var> is even.</li> <li>Half the characters in <var>X</var> are <code>S</code>, and the other half are <code>T</code>.</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Scores</h3><ul> <li>In test cases worth <var>200</var> points, <var>|X| ≦ 200</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>X</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the eventual length of <var>X</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>TSTTSS </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>In the <var>1</var>-st operation, the <var>2</var>-nd and <var>3</var>-rd characters of <code>TSTTSS</code> are removed. <var>X</var> becomes <code>TTSS</code>, and since it does not contain <code>ST</code> anymore, nothing is done in the remaining <var>10^{10000}-1</var> operations. Thus, the answer is <var>4</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>SSTTST </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p><var>X</var> will eventually become an empty string: <code>SSTTST</code> ⇒ <code>STST</code> ⇒ <code>ST</code> ⇒ ``.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>TSSTTTSS </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>4 </pre> <p><var>X</var> will become: <code>TSSTTTSS</code> ⇒ <code>TSTTSS</code> ⇒ <code>TTSS</code>.</p></section> </div> </span>
[ [ "TSTTSS\n", "TSTTSS\n" ] ]
p03987
AtCoder Grand Contest 005 - Minimum Sum
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>One day, Snuke was given a permutation of length <var>N</var>, <var>a_1, a_2, ..., a_N</var>, from his friend.</p> <p>Find the following:</p> <p><img src="https://atcoder.jp/img/agc005/520049e1a049bb9810b398b35d7dcb7f.png" style="height: 50px;"/></p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ N ≦ 200,000</var></li> <li><var>(a_1, a_2, ..., a_N)</var> is a permutation of <var>(1, 2, ..., N)</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>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 answer.</p> <p>Note that the answer may not fit into a 32-bit integer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 1 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>9 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 1 3 2 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>19 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>8 5 4 8 1 2 6 7 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>85 </pre></section> </div> </span>
[ [ "3\n2 1 3\n", "3\n2 1 3\n" ] ]
p03988
AtCoder Grand Contest 005 - Tree Restoring
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Aoki loves numerical sequences and trees.</p> <p>One day, Takahashi gave him an integer sequence of length <var>N</var>, <var>a_1, a_2, ..., a_N</var>, which made him want to construct a tree.</p> <p>Aoki wants to construct a tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>, such that for each <var>i = 1,2,...,N</var>, the distance between vertex <var>i</var> and the farthest vertex from it is <var>a_i</var>, assuming that the length of each edge is <var>1</var>.</p> <p>Determine whether such a tree exists.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≦ N ≦ 100</var></li> <li><var>1 ≦ a_i ≦ N-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>...</var> <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a tree that satisfies the condition, print <code>Possible</code>. Otherwise, print <code>Impossible</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 3 2 2 3 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Possible </pre> <p><img alt="" src="https://atcoder.jp/img/agc005/cda0380bb5cd1b9502cfceaf2526d91e.png"/></p> <p>The diagram above shows an example of a tree that satisfies the conditions. The red arrows show paths from each vertex to the farthest vertex from it.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 1 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>Impossible </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>10 1 2 2 2 2 2 2 2 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Possible </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>10 1 1 2 2 2 2 2 2 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>Impossible </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>6 1 1 1 1 1 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>Impossible </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 6</h3><pre>5 4 3 2 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 6</h3><pre>Possible </pre></section> </div> </span>
[ [ "5\n3 2 2 3 3\n", "5\n3 2 2 3 3\n" ] ]
p03989
AtCoder Grand Contest 005 - ~K Perm Counting
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke loves permutations. He is making a permutation of length <var>N</var>.</p> <p>Since he hates the integer <var>K</var>, his permutation will satisfy the following:</p> <ul> <li>Let the permutation be <var>a_1, a_2, ..., a_N</var>. For each <var>i = 1,2,...,N</var>, <var>|a_i - i| \neq K</var>.</li> </ul> <p>Among the <var>N!</var> permutations of length <var>N</var>, how many satisfies this condition?</p> <p>Since the answer may be extremely large, find the answer modulo <var>924844033</var>(prime).</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≦ N ≦ 2000</var></li> <li><var>1 ≦ K ≦ N-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>K</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer modulo <var>924844033</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p><var>2</var> permutations <var>(1, 2, 3)</var>, <var>(3, 2, 1)</var> satisfy the condition.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>5 </pre> <p><var>5</var> permutations <var>(1, 2, 3, 4)</var>, <var>(1, 4, 3, 2)</var>, <var>(3, 2, 1, 4)</var>, <var>(3, 4, 1, 2)</var>, <var>(4, 2, 3, 1)</var> satisfy the condition.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>9 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>4 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>14 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>425 48 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>756765083 </pre></section> </div> </span>
[ [ "3 1\n", "3 1\n" ] ]
p03990
AtCoder Grand Contest 005 - Sugigma: The Showdown
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Sigma and Sugim are playing a game.</p> <p>The game is played on a graph with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. The graph has <var>N-1</var> red edges and <var>N-1</var> blue edges, and the <var>N-1</var> edges in each color forms a tree. The red edges are represented by pairs of integers <var>(a_i, b_i)</var>, and the blue edges are represented by pairs of integers <var>(c_i, d_i)</var>.</p> <p>Each player has his own piece. Initially, Sigma's piece is at vertex <var>X</var>, and Sugim's piece is at vertex <var>Y</var>.</p> <p>The game is played in turns, where turns are numbered starting from turn <var>1</var>. Sigma takes turns <var>1, 3, 5, ...</var>, and Sugim takes turns <var>2, 4, 6, ...</var>.</p> <p>In each turn, the current player either moves his piece, or does nothing. Here, Sigma can only move his piece to a vertex that is directly connected to the current vertex by a red edge. Similarly, Sugim can only move his piece to a vertex that is directly connected to the current vertex by a blue edge.</p> <p>When the two pieces come to the same vertex, the game ends immediately. If the game ends just after the operation in turn <var>i</var>, let <var>i</var> be the total number of turns in the game.</p> <p>Sigma's objective is to make the total number of turns as large as possible, while Sugim's objective is to make it as small as possible.</p> <p>Determine whether the game will end in a finite number of turns, assuming both players plays optimally to achieve their respective objectives. If the answer is positive, find the number of turns in the game.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≦ N ≦ 200,000</var></li> <li><var>1 ≦ X, Y ≦ N</var></li> <li><var>X \neq Y</var></li> <li><var>1 ≦ a_i, b_i, c_i, d_i ≦ N</var></li> <li>The <var>N-1</var> edges in each color (red and blue) forms a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>X</var> <var>Y</var> <var>a_1</var> <var>b_1</var> <var>a_2</var> <var>b_2</var> <var>:</var> <var>a_{N-1}</var> <var>b_{N-1}</var> <var>c_1</var> <var>d_1</var> <var>c_2</var> <var>d_2</var> <var>:</var> <var>c_{N-1}</var> <var>d_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If the game will end in a finite number of turns, print the number of turns. Otherwise, print <code>-1</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 1 2 1 2 1 3 1 4 2 1 2 3 1 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p><img alt="" src="https://atcoder.jp/img/agc005/0f55f48518cb9031ee9f1cc30f686228.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 3 1 1 2 2 3 1 2 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>4 </pre> <p><img alt="" src="https://atcoder.jp/img/agc005/df982a9959ce46d5d5f63800f8972bff.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 1 2 1 2 3 4 2 4 1 2 3 4 1 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>2 </pre> <p><img alt="" src="https://atcoder.jp/img/agc005/11ce9a2283a853025b7075769439d745.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>4 2 1 1 2 3 4 2 4 1 2 3 4 1 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>-1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>5 1 2 1 2 1 3 1 4 4 5 2 1 1 3 1 5 5 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>6 </pre></section> </div> </span>
[ [ "4 1 2\n1 2\n1 3\n1 4\n2 1\n2 3\n1 4\n", "4 1 2\n1 2\n1 3\n1 4\n2 1\n2 3\n1 4\n" ] ]
p03991
AtCoder Grand Contest 005 - Many Easy Problems
<span class="lang-en"> <p>Score : <var>1900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><style> #nck { width: 30px; height: auto; } </style> <p>One day, Takahashi was given the following problem from Aoki:</p> <ul> <li>You are given a tree with <var>N</var> vertices and an integer <var>K</var>. The vertices are numbered <var>1</var> through <var>N</var>. The edges are represented by pairs of integers <var>(a_i, b_i)</var>.</li> <li>For a set <var>S</var> of vertices in the tree, let <var>f(S)</var> be the minimum number of the vertices in a subtree of the given tree that contains all vertices in <var>S</var>.</li> <li>There are <img id="nck" src="https://atcoder.jp/img/agc005/ea29e9345ef75e7d965febb790a5aad1.png"/> ways to choose <var>K</var> vertices from the trees. For each of them, let <var>S</var> be the set of the chosen vertices, and find the sum of <var>f(S)</var> over all <img id="nck" src="https://atcoder.jp/img/agc005/ea29e9345ef75e7d965febb790a5aad1.png"/> ways.</li> <li>Since the answer may be extremely large, print it modulo <var>924844033</var>(prime).</li> </ul> <p>Since it was too easy for him, he decided to solve this problem for all <var>K = 1,2,...,N</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≦ N ≦ 200,000</var></li> <li><var>1 ≦ a_i, b_i ≦ N</var></li> <li>The given graph is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>b_1</var> <var>a_2</var> <var>b_2</var> <var>:</var> <var>a_{N-1}</var> <var>b_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>N</var> lines. The <var>i</var>-th line should contain the answer to the problem where <var>K=i</var>, modulo <var>924844033</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 7 3 </pre> <p><img alt="" src="https://atcoder.jp/img/agc005/44e2fd5d5e0fe66d1d238ee502639e4e.png"/></p> <p>The diagram above illustrates the case where <var>K=2</var>. The chosen vertices are colored pink, and the subtrees with the minimum number of vertices are enclosed by red lines.</p> </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>4 15 13 4 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>7 1 2 2 3 2 4 4 5 4 6 6 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>7 67 150 179 122 45 7 </pre></section> </div> </span>
[ [ "3\n1 2\n2 3\n", "3\n1 2\n2 3\n" ] ]
p03992
CODE FESTIVAL 2016 qual A - CODEFESTIVAL 2016
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>This contest is <code>CODE FESTIVAL</code>. However, Mr. Takahashi always writes it <code>CODEFESTIVAL</code>, omitting the single space between <code>CODE</code> and <code>FESTIVAL</code>.</p> <p>So he has decided to make a program that puts the single space he omitted.</p> <p>You are given a string <var>s</var> with <var>12</var> letters. Output the string putting a single space between the first <var>4</var> letters and last <var>8</var> letters in the string <var>s</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>s</var> contains exactly <var>12</var> letters.</li> <li>All letters in <var>s</var> are uppercase English letters.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>s</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the string putting a single space between the first <var>4</var> letters and last <var>8</var> letters in the string <var>s</var>. Put a line break at the end.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>CODEFESTIVAL </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>CODE FESTIVAL </pre> <p>Putting a single space between the first <var>4</var> letters and last <var>8</var> letters in <code>CODEFESTIVAL</code> makes it <code>CODE FESTIVAL</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>POSTGRADUATE </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>POST GRADUATE </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>ABCDEFGHIJKL </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>ABCD EFGHIJKL </pre></section> </div> </span>
[ [ "CODEFESTIVAL\n", "CODEFESTIVAL\n" ] ]
p03993
CODE FESTIVAL 2016 qual A - Friendly Rabbits
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> rabbits, numbered <var>1</var> through <var>N</var>.</p> <p>The <var>i</var>-th (<var>1≤i≤N</var>) rabbit likes rabbit <var>a_i</var>. Note that no rabbit can like itself, that is, <var>a_i≠i</var>.</p> <p>For a pair of rabbits <var>i</var> and <var>j</var> (<var>i<j</var>), we call the pair (<var>i,j</var>) a <em>friendly pair</em> if the following condition is met.</p> <ul> <li>Rabbit <var>i</var> likes rabbit <var>j</var> and rabbit <var>j</var> likes rabbit <var>i</var>.</li> </ul> <p>Calculate the number of the friendly pairs.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≤N≤10^5</var></li> <li><var>1≤a_i≤N</var></li> <li><var>a_i≠i</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the friendly pairs.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 2 1 4 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>There are two friendly pairs: <var>(1,2)</var> and <var>(3,4)</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 2 3 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>There are no friendly pairs.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 5 5 5 5 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> <p>There is one friendly pair: <var>(1,5)</var>.</p></section> </div> </span>
[ [ "4\n2 1 4 3\n", "4\n2 1 4 3\n" ] ]
p03994
CODE FESTIVAL 2016 qual A - Next Letter
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Mr. Takahashi has a string <var>s</var> consisting of lowercase English letters. He repeats the following operation on <var>s</var> exactly <var>K</var> times.</p> <ul> <li>Choose an arbitrary letter on <var>s</var> and change that letter to the next alphabet. Note that the next letter of <code>z</code> is <code>a</code>.</li> </ul> <p>For example, if you perform an operation for the second letter on <code>aaz</code>, <code>aaz</code> becomes <code>abz</code>. If you then perform an operation for the third letter on <code>abz</code>, <code>abz</code> becomes <code>aba</code>.</p> <p>Mr. Takahashi wants to have the lexicographically smallest string after performing exactly <var>K</var> operations on <var>s</var>. Find the such string.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≤|s|≤10^5</var></li> <li>All letters in <var>s</var> are lowercase English letters.</li> <li><var>1≤K≤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>s</var> <var>K</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the lexicographically smallest string after performing exactly <var>K</var> operations on <var>s</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>xyz 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>aya </pre> <p>For example, you can perform the following operations: <code>xyz</code>, <code>yyz</code>, <code>zyz</code>, <code>ayz</code>, <code>aya</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>a 25 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>z </pre> <p>You have to perform exactly <var>K</var> operations.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>codefestival 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>aaaafeaaivap </pre></section> </div> </span>
[ [ "xyz\n4\n", "xyz\n4\n" ] ]
p03995
CODE FESTIVAL 2016 qual A - Grid and Integers
<span class="lang-en"> <p>Score : <var>800</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a grid with <var>R</var> rows and <var>C</var> columns. We call the cell in the <var>r</var>-th row and <var>c</var>-th column (<var>r,c</var>).</p> <p>Mr. Takahashi wrote non-negative integers into <var>N</var> of the cells, that is, he wrote a non-negative integer <var>a_i</var> into (<var>r_i,c_i</var>) for each <var>i</var> (<var>1≤i≤N</var>). After that he fell asleep.</p> <p>Mr. Aoki found the grid and tries to surprise Mr. Takahashi by writing integers into all remaining cells. The grid must meet the following conditions to really surprise Mr. Takahashi.</p> <ul> <li>Condition <var>1</var>: Each cell contains a non-negative integer.</li> <li>Condition <var>2</var>: For any <var>2×2</var> square formed by cells on the grid, the sum of the top left and bottom right integers must always equal to the sum of the top right and bottom left integers.</li> </ul> <p>Determine whether it is possible to meet those conditions by properly writing integers into all remaining cells.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≤R,C≤10^5</var></li> <li><var>1≤N≤10^5</var></li> <li><var>1≤r_i≤R</var></li> <li><var>1≤c_i≤C</var></li> <li><var>(r_i,c_i) ≠ (r_j,c_j)</var> (<var>i≠j</var>)</li> <li><var>a_i</var> is an integer.</li> <li><var>0≤a_i≤10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>R</var> <var>C</var> <var>N</var> <var>r_1</var> <var>c_1</var> <var>a_1</var> <var>r_2</var> <var>c_2</var> <var>a_2</var> <var>:</var> <var>r_N</var> <var>c_N</var> <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <code>Yes</code> if it is possible to meet the conditions by properly writing integers into all remaining cells. Otherwise, print <code>No</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 2 3 1 1 0 1 2 10 2 1 20 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes </pre> <p>You can write integers as follows.</p> <p><img src="/img/other/code_festival_2016_quala/gbanjthabot/D_0.png" width="300"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 3 5 1 1 0 1 2 10 1 3 20 2 1 30 2 3 40 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>No </pre> <p>There are two <var>2×2</var> squares on the grid, formed by the following cells:</p> <ul> <li>Cells <var>(1,1)</var>, <var>(1,2)</var>, <var>(2,1)</var> and <var>(2,2)</var></li> <li>Cells <var>(1,2)</var>, <var>(1,3)</var>, <var>(2,2)</var> and <var>(2,3)</var></li> </ul> <p>You have to write <var>40</var> into the empty cell to meet the condition on the left square, but then it does not satisfy the condition on the right square.</p> <p><img src="/img/other/code_festival_2016_quala/gbanjthabot/D_1.png" width="168"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>2 2 3 1 1 20 1 2 10 2 1 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>No </pre> <p>You have to write <var>-10</var> into the empty cell to meet condition <var>2</var>, but then it does not satisfy condition <var>1</var>.</p> <p><img src="/img/other/code_festival_2016_quala/gbanjthabot/D_2.png" width="120"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>3 3 4 1 1 0 1 3 10 3 1 10 3 3 20 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>Yes </pre> <p>You can write integers as follows.</p> <p><img src="/img/other/code_festival_2016_quala/gbanjthabot/D_3.png" width="396"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>2 2 4 1 1 0 1 2 10 2 1 30 2 2 20 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>No </pre> <p>All cells already contain a integer and condition <var>2</var> is not satisfied.</p> <p><img src="/img/other/code_festival_2016_quala/gbanjthabot/D_4.png" width="120"/></p></section> </div> </span>
[ [ "2 2\n3\n1 1 0\n1 2 10\n2 1 20\n", "2 2\n3\n1 1 0\n1 2 10\n2 1 20\n" ] ]
p03996
CODE FESTIVAL 2016 qual A - LRU Puzzle
<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>
[ [ "2 2\n3\n2 1 2\n", "2 2\n3\n2 1 2\n" ] ]
p03997
AtCoder Beginner Contest 045 - Trapezoids
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a trapezoid. The lengths of its upper base, lower base, and height are <var>a</var>, <var>b</var>, and <var>h</var>, respectively.</p> <div style="text-align: center;"> <img src="https://atcoder.jp/img/arc061/1158e37155d46a42e90f31566478e6da.png"> <p>An example of a trapezoid</p> </img></div> <p>Find the area of this trapezoid.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦a≦100</var></li> <li><var>1≦b≦100</var></li> <li><var>1≦h≦100</var></li> <li>All input values are integers.</li> <li><var>h</var> is even.</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>h</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the area of the given trapezoid. It is guaranteed that the area is an integer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 4 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>7 </pre> <p>When the lengths of the upper base, lower base, and height are <var>3</var>, <var>4</var>, and <var>2</var>, respectively, the area of the trapezoid is <var>(3+4)×2/2 = 7</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 4 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>16 </pre> <p>In this case, a parallelogram is given, which is also a trapezoid.</p></section> </div> </span>
[ [ "3\n4\n2\n", "3\n4\n2\n" ] ]
p03998
AtCoder Beginner Contest 045 - Card Game for Three (ABC Edit)
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Alice, Bob and Charlie are playing <em>Card Game for Three</em>, as below:</p> <ul> <li>At first, each of the three players has a deck consisting of some number of cards. Each card has a letter <code>a</code>, <code>b</code> or <code>c</code> written on it. The orders of the cards in the decks cannot be rearranged.</li> <li>The players take turns. Alice goes first.</li> <li>If the current player's deck contains at least one card, discard the top card in the deck. Then, the player whose name begins with the letter on the discarded card, takes the next turn. (For example, if the card says <code>a</code>, Alice takes the next turn.)</li> <li>If the current player's deck is empty, the game ends and the current player wins the game.</li> </ul> <p>You are given the initial decks of the players. More specifically, you are given three strings <var>S_A</var>, <var>S_B</var> and <var>S_C</var>. The <var>i</var>-th <var>(1≦i≦|S_A|)</var> letter in <var>S_A</var> is the letter on the <var>i</var>-th card in Alice's initial deck. <var>S_B</var> and <var>S_C</var> describes Bob's and Charlie's initial decks in the same way.</p> <p>Determine the winner of the game.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦|S_A|≦100</var></li> <li><var>1≦|S_B|≦100</var></li> <li><var>1≦|S_C|≦100</var></li> <li>Each letter in <var>S_A</var>, <var>S_B</var>, <var>S_C</var> is <code>a</code>, <code>b</code> or <code>c</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>S_A</var> <var>S_B</var> <var>S_C</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If Alice will win, print <code>A</code>. If Bob will win, print <code>B</code>. If Charlie will win, print <code>C</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>aca accc ca </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>A </pre> <p>The game will progress as below:</p> <ul> <li>Alice discards the top card in her deck, <code>a</code>. Alice takes the next turn.</li> <li>Alice discards the top card in her deck, <code>c</code>. Charlie takes the next turn.</li> <li>Charlie discards the top card in his deck, <code>c</code>. Charlie takes the next turn.</li> <li>Charlie discards the top card in his deck, <code>a</code>. Alice takes the next turn.</li> <li>Alice discards the top card in her deck, <code>a</code>. Alice takes the next turn.</li> <li>Alice's deck is empty. The game ends and Alice wins the game.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>abcb aacb bccc </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>C </pre></section> </div> </span>
[ [ "aca\naccc\nca\n", "aca\naccc\nca\n" ] ]
p03999
AtCoder Beginner Contest 045 - Many Formulas
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a string <var>S</var> consisting of digits between <code>1</code> and <code>9</code>, inclusive. You can insert the letter <code>+</code> into some of the positions (possibly none) between two letters in this string. Here, <code>+</code> must not occur consecutively after insertion.</p> <p>All strings that can be obtained in this way can be evaluated as formulas.</p> <p>Evaluate all possible formulas, and print the sum of the results.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq |S| \leq 10</var></li> <li>All letters in <var>S</var> are digits between <code>1</code> and <code>9</code>, inclusive.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>S</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the sum of the evaluated value over all possible formulas.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>125 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>176 </pre> <p>There are <var>4</var> formulas that can be obtained: <code>125</code>, <code>1+25</code>, <code>12+5</code> and <code>1+2+5</code>. When each formula is evaluated,</p> <ul> <li><var>125</var></li> <li><var>1+25=26</var></li> <li><var>12+5=17</var></li> <li><var>1+2+5=8</var></li> </ul> <p>Thus, the sum is <var>125+26+17+8=176</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>9999999999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>12656242944 </pre></section> </div> </span>
[ [ "125\n", "125\n" ] ]
p04000
AtCoder Beginner Contest 045 - Snuke's Coloring
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a grid with <var>H</var> rows and <var>W</var> columns. At first, all cells were painted white.</p> <p>Snuke painted <var>N</var> of these cells. The <var>i</var>-th ( <var>1 \leq i \leq N</var> ) cell he painted is the cell at the <var>a_i</var>-th row and <var>b_i</var>-th column.</p> <p>Compute the following:</p> <ul> <li>For each integer <var>j</var> ( <var>0 \leq j \leq 9</var> ), how many subrectangles of size <var>3×3</var> of the grid contains exactly <var>j</var> black cells, after Snuke painted <var>N</var> cells?</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>3 \leq H \leq 10^9</var></li> <li><var>3 \leq W \leq 10^9</var></li> <li><var>0 \leq N \leq min(10^5,H×W)</var></li> <li><var>1 \leq a_i \leq H</var> <var>(1 \leq i \leq N)</var></li> <li><var>1 \leq b_i \leq W</var> <var>(1 \leq i \leq N)</var></li> <li><var>(a_i, b_i) \neq (a_j, b_j)</var> <var>(i \neq j)</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>N</var> <var>a_1</var> <var>b_1</var> : <var>a_N</var> <var>b_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>10</var> lines. The <var>(j+1)</var>-th ( <var>0 \leq j \leq 9</var> ) line should contain the number of the subrectangles of size <var>3×3</var> of the grid that contains exactly <var>j</var> black cells.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 5 8 1 1 1 4 1 5 2 3 3 1 3 2 3 4 4 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>0 0 0 2 4 0 0 0 0 0 </pre> <p><img alt="" src="https://atcoder.jp/img/arc061/30326702be007759dce81231012a8353.png"/></p> <p>There are six subrectangles of size <var>3×3</var>. Two of them contain three black cells each, and the remaining four contain four black cells each.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 10 20 1 1 1 4 1 9 2 5 3 10 4 2 4 7 5 9 6 4 6 6 6 7 7 1 7 3 7 7 8 1 8 5 8 10 9 2 10 4 10 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>4 26 22 10 2 0 0 0 0 0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1000000000 1000000000 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>999999996000000004 0 0 0 0 0 0 0 0 0 </pre></section> </div> </span>
[ [ "4 5 8\n1 1\n1 4\n1 5\n2 3\n3 1\n3 2\n3 4\n4 4\n", "4 5 8\n1 1\n1 4\n1 5\n2 3\n3 1\n3 2\n3 4\n4 4\n" ] ]
p04001
AtCoder Regular Contest 061 - Many Formulas
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a string <var>S</var> consisting of digits between <code>1</code> and <code>9</code>, inclusive. You can insert the letter <code>+</code> into some of the positions (possibly none) between two letters in this string. Here, <code>+</code> must not occur consecutively after insertion.</p> <p>All strings that can be obtained in this way can be evaluated as formulas.</p> <p>Evaluate all possible formulas, and print the sum of the results.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq |S| \leq 10</var></li> <li>All letters in <var>S</var> are digits between <code>1</code> and <code>9</code>, inclusive.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>S</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the sum of the evaluated value over all possible formulas.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>125 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>176 </pre> <p>There are <var>4</var> formulas that can be obtained: <code>125</code>, <code>1+25</code>, <code>12+5</code> and <code>1+2+5</code>. When each formula is evaluated,</p> <ul> <li><var>125</var></li> <li><var>1+25=26</var></li> <li><var>12+5=17</var></li> <li><var>1+2+5=8</var></li> </ul> <p>Thus, the sum is <var>125+26+17+8=176</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>9999999999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>12656242944 </pre></section> </div> </span>
[ [ "125\n", "125\n" ] ]
p04002
AtCoder Regular Contest 061 - Snuke's Coloring
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a grid with <var>H</var> rows and <var>W</var> columns. At first, all cells were painted white.</p> <p>Snuke painted <var>N</var> of these cells. The <var>i</var>-th ( <var>1 \leq i \leq N</var> ) cell he painted is the cell at the <var>a_i</var>-th row and <var>b_i</var>-th column.</p> <p>Compute the following:</p> <ul> <li>For each integer <var>j</var> ( <var>0 \leq j \leq 9</var> ), how many subrectangles of size <var>3×3</var> of the grid contains exactly <var>j</var> black cells, after Snuke painted <var>N</var> cells?</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>3 \leq H \leq 10^9</var></li> <li><var>3 \leq W \leq 10^9</var></li> <li><var>0 \leq N \leq min(10^5,H×W)</var></li> <li><var>1 \leq a_i \leq H</var> <var>(1 \leq i \leq N)</var></li> <li><var>1 \leq b_i \leq W</var> <var>(1 \leq i \leq N)</var></li> <li><var>(a_i, b_i) \neq (a_j, b_j)</var> <var>(i \neq j)</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>N</var> <var>a_1</var> <var>b_1</var> : <var>a_N</var> <var>b_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>10</var> lines. The <var>(j+1)</var>-th ( <var>0 \leq j \leq 9</var> ) line should contain the number of the subrectangles of size <var>3×3</var> of the grid that contains exactly <var>j</var> black cells.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 5 8 1 1 1 4 1 5 2 3 3 1 3 2 3 4 4 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>0 0 0 2 4 0 0 0 0 0 </pre> <p><img alt="" src="https://atcoder.jp/img/arc061/30326702be007759dce81231012a8353.png"/></p> <p>There are six subrectangles of size <var>3×3</var>. Two of them contain three black cells each, and the remaining four contain four black cells each.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 10 20 1 1 1 4 1 9 2 5 3 10 4 2 4 7 5 9 6 4 6 6 6 7 7 1 7 3 7 7 8 1 8 5 8 10 9 2 10 4 10 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>4 26 22 10 2 0 0 0 0 0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1000000000 1000000000 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>999999996000000004 0 0 0 0 0 0 0 0 0 </pre></section> </div> </span>
[ [ "4 5 8\n1 1\n1 4\n1 5\n2 3\n3 1\n3 2\n3 4\n4 4\n", "4 5 8\n1 1\n1 4\n1 5\n2 3\n3 1\n3 2\n3 4\n4 4\n" ] ]
p04003
AtCoder Regular Contest 061 - Snuke's Subway Trip
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke's town has a subway system, consisting of <var>N</var> stations and <var>M</var> railway lines. The stations are numbered <var>1</var> through <var>N</var>. Each line is operated by a company. Each company has an identification number.</p> <p>The <var>i</var>-th ( <var>1 \leq i \leq M</var> ) line connects station <var>p_i</var> and <var>q_i</var> bidirectionally. There is no intermediate station. This line is operated by company <var>c_i</var>.</p> <p>You can change trains at a station where multiple lines are available.</p> <p>The fare system used in this subway system is a bit strange. When a passenger only uses lines that are operated by the same company, the fare is <var>1</var> yen (the currency of Japan). Whenever a passenger changes to a line that is operated by a different company from the current line, the passenger is charged an additional fare of <var>1</var> yen. In a case where a passenger who changed from some company A's line to another company's line changes to company A's line again, the additional fare is incurred again.</p> <p>Snuke is now at station <var>1</var> and wants to travel to station <var>N</var> by subway. Find the minimum required fare.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 10^5</var></li> <li><var>0 \leq M \leq 2×10^5</var></li> <li><var>1 \leq p_i \leq N</var> <var>(1 \leq i \leq M)</var></li> <li><var>1 \leq q_i \leq N</var> <var>(1 \leq i \leq M)</var></li> <li><var>1 \leq c_i \leq 10^6</var> <var>(1 \leq i \leq M)</var></li> <li><var>p_i \neq q_i</var> <var>(1 \leq i \leq 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>p_1</var> <var>q_1</var> <var>c_1</var> : <var>p_M</var> <var>q_M</var> <var>c_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum required fare. If it is impossible to get to station <var>N</var> by subway, print <code>-1</code> instead.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 1 2 1 2 3 1 3 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 </pre> <p>Use company <var>1</var>'s lines: <var>1</var> → <var>2</var> → <var>3</var>. The fare is <var>1</var> yen.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>8 11 1 3 1 1 4 2 2 3 1 2 5 1 3 4 3 3 6 3 3 7 3 4 8 4 5 6 1 6 7 5 7 8 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 </pre> <p>First, use company <var>1</var>'s lines: <var>1</var> → <var>3</var> → <var>2</var> → <var>5</var> → <var>6</var>. Then, use company <var>5</var>'s lines: <var>6</var> → <var>7</var> → <var>8</var>. The fare is <var>2</var> yen.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>2 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>-1 </pre></section> </div> </span>
[ [ "3 3\n1 2 1\n2 3 1\n3 1 2\n", "3 3\n1 2 1\n2 3 1\n3 1 2\n" ] ]
p04004
AtCoder Regular Contest 061 - Card Game for Three
<span class="lang-en"> <p>Score : <var>1100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Alice, Bob and Charlie are playing <em>Card Game for Three</em>, as below:</p> <ul> <li>At first, each of the three players has a deck consisting of some number of cards. Alice's deck has <var>N</var> cards, Bob's deck has <var>M</var> cards, and Charlie's deck has <var>K</var> cards. Each card has a letter <code>a</code>, <code>b</code> or <code>c</code> written on it. The orders of the cards in the decks cannot be rearranged.</li> <li>The players take turns. Alice goes first.</li> <li>If the current player's deck contains at least one card, discard the top card in the deck. Then, the player whose name begins with the letter on the discarded card, takes the next turn. (For example, if the card says <code>a</code>, Alice takes the next turn.)</li> <li>If the current player's deck is empty, the game ends and the current player wins the game.</li> </ul> <p>There are <var>3^{N+M+K}</var> possible patters of the three player's initial decks. Among these patterns, how many will lead to Alice's victory?</p> <p>Since the answer can be large, print the count modulo <var>1\,000\,000\,007 (=10^9+7)</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 3×10^5</var></li> <li><var>1 \leq M \leq 3×10^5</var></li> <li><var>1 \leq K \leq 3×10^5</var></li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Scores</h3><ul> <li><var>500</var> points will be awarded for passing the test set satisfying the following: <var>1 \leq N \leq 1000</var>, <var>1 \leq M \leq 1000</var>, <var>1 \leq K \leq 1000</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>K</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer modulo <var>1\,000\,000\,007 (=10^9+7)</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>1 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>17 </pre> <ul> <li>If Alice's card is <code>a</code>, then Alice will win regardless of Bob's and Charlie's card. There are <var>3×3=9</var> such patterns.</li> <li>If Alice's card is <code>b</code>, Alice will only win when Bob's card is <code>a</code>, or when Bob's card is <code>c</code> and Charlie's card is <code>a</code>. There are <var>3+1=4</var> such patterns.</li> <li>If Alice's card is <code>c</code>, Alice will only win when Charlie's card is <code>a</code>, or when Charlie's card is <code>b</code> and Bob's card is <code>a</code>. There are <var>3+1=4</var> such patterns.</li> </ul> <p>Thus, there are total of <var>9+4+4=17</var> patterns that will lead to Alice's victory.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1227 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1000 1000 1000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>261790852 </pre></section> </div> </span>
[ [ "1 1 1\n", "1 1 1\n" ] ]
p04005
AtCoder Grand Contest 004 - Divide a Cuboid
<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>
[ [ "3 3 3\n", "3 3 3\n" ] ]
p04006
AtCoder Grand Contest 004 - Colorful Slimes
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke lives in another world, where slimes are real creatures and kept by some people. Slimes come in <var>N</var> colors. Those colors are conveniently numbered <var>1</var> through <var>N</var>. Snuke currently has no slime. His objective is to have slimes of all the colors together.</p> <p>Snuke can perform the following two actions:</p> <ul> <li> <p>Select a color <var>i</var> (<var>1≤i≤N</var>), such that he does not currently have a slime in color <var>i</var>, and catch a slime in color <var>i</var>. This action takes him <var>a_i</var> seconds.</p> </li> <li> <p>Cast a spell, which changes the color of all the slimes that he currently has. The color of a slime in color <var>i</var> (<var>1≤i≤N-1</var>) will become color <var>i+1</var>, and the color of a slime in color <var>N</var> will become color <var>1</var>. This action takes him <var>x</var> seconds.</p> </li> </ul> <p>Find the minimum time that Snuke needs to have slimes in all <var>N</var> colors.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≤N≤2,000</var></li> <li><var>a_i</var> are integers.</li> <li><var>1≤a_i≤10^9</var></li> <li><var>x</var> is an integer.</li> <li><var>1≤x≤10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>x</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>Find the minimum time that Snuke needs to have slimes in all <var>N</var> colors.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 10 1 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>12 </pre> <p>Snuke can act as follows:</p> <ul> <li>Catch a slime in color <var>1</var>. This takes <var>1</var> second.</li> <li>Cast the spell. The color of the slime changes: <var>1</var> → <var>2</var>. This takes <var>10</var> seconds.</li> <li>Catch a slime in color <var>1</var>. This takes <var>1</var> second.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 10 100 1 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>23 </pre> <p>Snuke can act as follows:</p> <ul> <li>Catch a slime in color <var>2</var>. This takes <var>1</var> second.</li> <li>Cast the spell. The color of the slime changes: <var>2</var> → <var>3</var>. This takes <var>10</var> seconds.</li> <li>Catch a slime in color <var>2</var>. This takes <var>1</var> second.</li> <li>Cast the soell. The color of each slime changes: <var>3</var> → <var>1</var>, <var>2</var> → <var>3</var>. This takes <var>10</var> seconds.</li> <li>Catch a slime in color <var>2</var>. This takes <var>1</var> second.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 10 1 2 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>10 </pre> <p>Snuke can act as follows:</p> <ul> <li>Catch a slime in color <var>1</var>. This takes <var>1</var> second.</li> <li>Catch a slime in color <var>2</var>. This takes <var>2</var> seconds.</li> <li>Catch a slime in color <var>3</var>. This takes <var>3</var> seconds.</li> <li>Catch a slime in color <var>4</var>. This takes <var>4</var> seconds.</li> </ul></section> </div> </span>
[ [ "2 10\n1 100\n", "2 10\n1 100\n" ] ]
p04007
AtCoder Grand Contest 004 - AND Grid
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke and Ciel went to a strange stationery store. Each of them got a transparent graph paper with <var>H</var> rows and <var>W</var> columns.</p> <p>Snuke painted some of the cells red in his paper. Here, the cells painted red were <em>4-connected</em>, that is, it was possible to traverse from any red cell to any other red cell, by moving to vertically or horizontally adjacent red cells only.</p> <p>Ciel painted some of the cells blue in her paper. Here, the cells painted blue were 4-connected.</p> <p>Afterwards, they precisely overlaid the two sheets in the same direction. Then, the intersection of the red cells and the blue cells appeared purple.</p> <p>You are given a matrix of letters <var>a_{ij}</var> (<var>1≤i≤H</var>, <var>1≤j≤W</var>) that describes the positions of the purple cells. If the cell at the <var>i</var>-th row and <var>j</var>-th column is purple, then <var>a_{ij}</var> is <code>#</code>, otherwise <var>a_{ij}</var> is <code>.</code>. Here, it is guaranteed that <strong>no outermost cell is purple</strong>. That is, if <var>i=1, H</var> or <var>j = 1, W</var>, then <var>a_{ij}</var> is <code>.</code>.</p> <p>Find a pair of the set of the positions of the red cells and the blue cells that is consistent with the situation described. It can be shown that a solution always exists.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>3≤H,W≤500</var></li> <li><var>a_{ij}</var> is <code>#</code> or <code>.</code>.</li> <li>If <var>i=1,H</var> or <var>j=1,W</var>, then <var>a_{ij}</var> is <code>.</code>.</li> <li>At least one of <var>a_{ij}</var> is <code>#</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>a_{11}</var><var>...</var><var>a_{1W}</var> <var>:</var> <var>a_{H1}</var><var>...</var><var>a_{HW}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print a pair of the set of the positions of the red cells and the blue cells that is consistent with the situation, as follows:</p> <ul> <li>The first <var>H</var> lines should describe the positions of the red cells.</li> <li>The following <var>1</var> line should be empty.</li> <li>The following <var>H</var> lines should describe the positions of the blue cells.</li> </ul> <p>The description of the positions of the red or blue cells should follow the format of the description of the positions of the purple cells.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 5 ..... .#.#. ..... .#.#. ..... </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>..... ##### #.... ##### ..... .###. .#.#. .#.#. .#.#. ..... </pre> <p>One possible pair of the set of the positions of the red cells and the blue cells is as follows:</p> <p><img src="/img/agc/004/gatbantar/C_1.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 13 ............. .###.###.###. .#.#.#...#... .###.#...#... .#.#.#.#.#... .#.#.###.###. ............. </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>............. .###########. .###.###.###. .###.###.###. .###.###.###. .###.###.###. ............. ............. .###.###.###. .#.#.#...#... .###.#...#... .#.#.#.#.#... .#.#########. ............. </pre> <p>One possible pair of the set of the positions of the red cells and the blue cells is as follows:</p> <p><img src="/img/agc/004/gatbantar/C_2.png"/></p></section> </div> </span>
[ [ "5 5\n.....\n.#.#.\n.....\n.#.#.\n.....\n", "5 5\n.....\n.#.#.\n.....\n.#.#.\n.....\n" ] ]
p04008
AtCoder Grand Contest 004 - Teleporter
<span class="lang-en"> <p>Score : <var>800</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> towns in Snuke Kingdom, conveniently numbered <var>1</var> through <var>N</var>. Town <var>1</var> is the capital.</p> <p>Each town in the kingdom has a <em>Teleporter</em>, a facility that instantly transports a person to another place. The destination of the Teleporter of town <var>i</var> is town <var>a_i</var> (<var>1≤a_i≤N</var>). It is guaranteed that <strong>one can get to the capital from any town by using the Teleporters some number of times</strong>.</p> <p>King Snuke loves the integer <var>K</var>. The selfish king wants to change the destination of the Teleporters so that the following holds:</p> <ul> <li>Starting from any town, one will be at the capital after using the Teleporters exactly <var>K</var> times in total.</li> </ul> <p>Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≤N≤10^5</var></li> <li><var>1≤a_i≤N</var></li> <li>One can get to the capital from any town by using the Teleporters some number of times.</li> <li><var>1≤K≤10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 2 3 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>Change the destinations of the Teleporters to <var>a = (1,1,1)</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 2 1 1 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>There is no need to change the destinations of the Teleporters, since the king's desire is already satisfied.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>8 2 4 1 2 3 1 2 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>3 </pre> <p>For example, change the destinations of the Teleporters to <var>a = (1,1,2,1,1,2,2,4)</var>.</p></section> </div> </span>
[ [ "3 1\n2 3 1\n", "3 1\n2 3 1\n" ] ]
p04009
AtCoder Grand Contest 004 - Salvage Robots
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a grid with <var>H</var> rows and <var>W</var> columns. The state of the cell at the <var>i</var>-th (<var>1≤i≤H</var>) row and <var>j</var>-th (<var>1≤j≤W</var>) column is represented by a letter <var>a_{ij}</var>, as follows:</p> <ul> <li><code>.</code> : This cell is empty.</li> <li><code>o</code> : This cell contains a robot.</li> <li><code>E</code> : This cell contains the exit. <code>E</code> occurs exactly once in the whole grid.</li> </ul> <p>Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:</p> <ul> <li>Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.</li> </ul> <p>Find the maximum number of robots that can be salvaged.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≤H,W≤100</var></li> <li><var>a_{ij}</var> is <code>.</code>, <code>o</code> or <code>E</code>.</li> <li><code>E</code> occurs exactly once in the whole grid.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>a_{11}</var><var>...</var><var>a_{1W}</var> <var>:</var> <var>a_{H1}</var><var>...</var><var>a_{HW}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum number of robots that can be salvaged.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 o.o .Eo ooo </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>For example, select left, up, right.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 2 E. .. </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 4 o... o... oooE </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>5 </pre> <p>Select right, right, right, down, down.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>5 11 ooo.ooo.ooo o.o.o...o.. ooo.oE..o.. o.o.o.o.o.. o.o.ooo.ooo </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>12 </pre></section> </div> </span>
[ [ "3 3\no.o\n.Eo\nooo\n", "3 3\no.o\n.Eo\nooo\n" ] ]
p04010
AtCoder Grand Contest 004 - Namori
<span class="lang-en"> <p>Score : <var>2200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an undirected graph with <var>N</var> vertices and <var>M</var> edges. Here, <var>N-1≤M≤N</var> holds and the graph is connected. There are no self-loops or multiple edges in this graph.</p> <p>The vertices are numbered <var>1</var> through <var>N</var>, and the edges are numbered <var>1</var> through <var>M</var>. Edge <var>i</var> connects vertices <var>a_i</var> and <var>b_i</var>.</p> <p>The color of each vertex can be either white or black. Initially, all the vertices are white. Snuke is trying to turn all the vertices black by performing the following operation some number of times:</p> <ul> <li>Select a pair of adjacent vertices with the same color, and invert the colors of those vertices. That is, if the vertices are both white, then turn them black, and vice versa.</li> </ul> <p>Determine if it is possible to turn all the vertices black. If the answer is positive, find the minimum number of times the operation needs to be performed in order to achieve the objective.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≤N≤10^5</var></li> <li><var>N-1≤M≤N</var></li> <li><var>1≤a_i,b_i≤N</var></li> <li>There are no self-loops or multiple edges in the given graph.</li> <li>The given graph is connected.</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li>In the test set worth <var>1500</var> points, <var>M=N-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>M</var> <var>a_1</var> <var>b_1</var> <var>a_2</var> <var>b_2</var> <var>:</var> <var>a_M</var> <var>b_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If it is possible to turn all the vertices black, print the minimum number of times the operation needs to be performed in order to achieve the objective. Otherwise, print <code>-1</code> instead.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 5 1 2 1 3 1 4 2 5 2 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>5 </pre> <p>For example, it is possible to perform the operations as shown in the following diagram:</p> <p><img src="/img/agc/004/gatbantar/F_1.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 2 1 2 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>-1 </pre> <p>It is not possible to turn all the vertices black.</p> <p><img src="/img/agc/004/gatbantar/F_2.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 4 1 2 2 3 3 4 4 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>2 </pre> <p>This case is not included in the test set for the partial score.</p> <p><img src="/img/agc/004/gatbantar/F_3.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>6 6 1 2 2 3 3 1 1 4 1 5 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>7 </pre> <p>This case is not included in the test set for the partial score.</p> <p><img src="/img/agc/004/gatbantar/F_4.png"/></p></section> </div> </span>
[ [ "6 5\n1 2\n1 3\n1 4\n2 5\n2 6\n", "6 5\n1 2\n1 3\n1 4\n2 5\n2 6\n" ] ]
p04011
AtCoder Beginner Contest 044 - Tak and Hotels (ABC Edit)
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a hotel with the following accommodation fee:</p> <ul> <li><var>X</var> yen (the currency of Japan) per night, for the first <var>K</var> nights</li> <li><var>Y</var> yen per night, for the <var>(K+1)</var>-th and subsequent nights</li> </ul> <p>Tak is staying at this hotel for <var>N</var> consecutive nights. Find his total accommodation fee.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N, K \leq 10000</var></li> <li><var>1 \leq Y &lt; X \leq 10000</var></li> <li><var>N,\,K,\,X,\,Y</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>X</var> <var>Y</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print Tak's total accommodation fee.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 3 10000 9000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>48000 </pre> <p>The accommodation fee is as follows:</p> <ul> <li><var>10000</var> yen for the <var>1</var>-st night</li> <li><var>10000</var> yen for the <var>2</var>-nd night</li> <li><var>10000</var> yen for the <var>3</var>-rd night</li> <li><var>9000</var> yen for the <var>4</var>-th night</li> <li><var>9000</var> yen for the <var>5</var>-th night</li> </ul> <p>Thus, the total is <var>48000</var> yen.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 3 10000 9000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>20000 </pre></section> </div> </span>
[ [ "5\n3\n10000\n9000\n", "5\n3\n10000\n9000\n" ] ]
p04012
AtCoder Beginner Contest 044 - Beautiful Strings
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Let <var>w</var> be a string consisting of lowercase letters. We will call <var>w</var> <em>beautiful</em> if the following condition is satisfied:</p> <ul> <li>Each lowercase letter of the English alphabet occurs even number of times in <var>w</var>.</li> </ul> <p>You are given the string <var>w</var>. Determine if <var>w</var> is beautiful.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq |w| \leq 100</var></li> <li><var>w</var> consists of lowercase letters (<code>a</code>-<code>z</code>).</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>w</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <code>Yes</code> if <var>w</var> is beautiful. Print <code>No</code> otherwise.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>abaccaba </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes </pre> <p><code>a</code> occurs four times, <code>b</code> occurs twice, <code>c</code> occurs twice and the other letters occur zero times.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>hthth </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>No </pre></section> </div> </span>
[ [ "abaccaba\n", "abaccaba\n" ] ]
p04013
AtCoder Beginner Contest 044 - Tak and Cards
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Tak has <var>N</var> cards. On the <var>i</var>-th <var>(1 \leq i \leq N)</var> card is written an integer <var>x_i</var>. He is selecting one or more cards from these <var>N</var> cards, so that the average of the integers written on the selected cards is exactly <var>A</var>. In how many ways can he make his selection?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 50</var></li> <li><var>1 \leq A \leq 50</var></li> <li><var>1 \leq x_i \leq 50</var></li> <li><var>N,\,A,\,x_i</var> are integers.</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li><var>200</var> points will be awarded for passing the test set satisfying <var>1 \leq N \leq 16</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</var> <var>x_1</var> <var>x_2</var> <var>...</var> <var>x_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways to select cards such that the average of the written integers is exactly <var>A</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 8 7 9 8 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>5 </pre> <ul> <li>The following are the <var>5</var> ways to select cards such that the average is <var>8</var>:<ul> <li>Select the <var>3</var>-rd card.</li> <li>Select the <var>1</var>-st and <var>2</var>-nd cards.</li> <li>Select the <var>1</var>-st and <var>4</var>-th cards.</li> <li>Select the <var>1</var>-st, <var>2</var>-nd and <var>3</var>-rd cards.</li> <li>Select the <var>1</var>-st, <var>3</var>-rd and <var>4</var>-th cards.</li> </ul> </li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 8 6 6 9 </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>8 5 3 6 2 8 7 6 5 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>19 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>33 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>8589934591 </pre> <ul> <li>The answer may not fit into a <var>32</var>-bit integer.</li> </ul></section> </div> </span>
[ [ "4 8\n7 9 8 9\n", "4 8\n7 9 8 9\n" ] ]
p04014
AtCoder Beginner Contest 044 - Digit Sum
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>For integers <var>b (b \geq 2)</var> and <var>n (n \geq 1)</var>, let the function <var>f(b,n)</var> be defined as follows:</p> <ul> <li><var>f(b,n) = n</var>, when <var>n &lt; b</var></li> <li><var>f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b)</var>, when <var>n \geq b</var></li> </ul> <p>Here, <var>{\rm floor}(n / b)</var> denotes the largest integer not exceeding <var>n / b</var>, and <var>n \ {\rm mod} \ b</var> denotes the remainder of <var>n</var> divided by <var>b</var>.</p> <p>Less formally, <var>f(b,n)</var> is equal to the sum of the digits of <var>n</var> written in base <var>b</var>. For example, the following hold:</p> <ul> <li><var>f(10,\,87654)=8+7+6+5+4=30</var></li> <li><var>f(100,\,87654)=8+76+54=138</var></li> </ul> <p>You are given integers <var>n</var> and <var>s</var>. Determine if there exists an integer <var>b (b \geq 2)</var> such that <var>f(b,n)=s</var>. If the answer is positive, also find the smallest such <var>b</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq n \leq 10^{11}</var></li> <li><var>1 \leq s \leq 10^{11}</var></li> <li><var>n,\,s</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>n</var> <var>s</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists an integer <var>b (b \geq 2)</var> such that <var>f(b,n)=s</var>, print the smallest such <var>b</var>. If such <var>b</var> does not exist, print <code>-1</code> instead.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>87654 30 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>10 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>87654 138 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>100 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>87654 45678 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>-1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>31415926535 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>31415926535 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>1 31415926535 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>-1 </pre></section> </div> </span>
[ [ "87654\n30\n", "87654\n30\n" ] ]
p04015
AtCoder Regular Contest 060 - Tak and Cards
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Tak has <var>N</var> cards. On the <var>i</var>-th <var>(1 \leq i \leq N)</var> card is written an integer <var>x_i</var>. He is selecting one or more cards from these <var>N</var> cards, so that the average of the integers written on the selected cards is exactly <var>A</var>. In how many ways can he make his selection?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 50</var></li> <li><var>1 \leq A \leq 50</var></li> <li><var>1 \leq x_i \leq 50</var></li> <li><var>N,\,A,\,x_i</var> are integers.</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li><var>200</var> points will be awarded for passing the test set satisfying <var>1 \leq N \leq 16</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</var> <var>x_1</var> <var>x_2</var> <var>...</var> <var>x_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways to select cards such that the average of the written integers is exactly <var>A</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 8 7 9 8 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>5 </pre> <ul> <li>The following are the <var>5</var> ways to select cards such that the average is <var>8</var>:<ul> <li>Select the <var>3</var>-rd card.</li> <li>Select the <var>1</var>-st and <var>2</var>-nd cards.</li> <li>Select the <var>1</var>-st and <var>4</var>-th cards.</li> <li>Select the <var>1</var>-st, <var>2</var>-nd and <var>3</var>-rd cards.</li> <li>Select the <var>1</var>-st, <var>3</var>-rd and <var>4</var>-th cards.</li> </ul> </li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 8 6 6 9 </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>8 5 3 6 2 8 7 6 5 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>19 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>33 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>8589934591 </pre> <ul> <li>The answer may not fit into a <var>32</var>-bit integer.</li> </ul></section> </div> </span>
[ [ "4 8\n7 9 8 9\n", "4 8\n7 9 8 9\n" ] ]
p04016
AtCoder Regular Contest 060 - Digit Sum
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>For integers <var>b (b \geq 2)</var> and <var>n (n \geq 1)</var>, let the function <var>f(b,n)</var> be defined as follows:</p> <ul> <li><var>f(b,n) = n</var>, when <var>n &lt; b</var></li> <li><var>f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b)</var>, when <var>n \geq b</var></li> </ul> <p>Here, <var>{\rm floor}(n / b)</var> denotes the largest integer not exceeding <var>n / b</var>, and <var>n \ {\rm mod} \ b</var> denotes the remainder of <var>n</var> divided by <var>b</var>.</p> <p>Less formally, <var>f(b,n)</var> is equal to the sum of the digits of <var>n</var> written in base <var>b</var>. For example, the following hold:</p> <ul> <li><var>f(10,\,87654)=8+7+6+5+4=30</var></li> <li><var>f(100,\,87654)=8+76+54=138</var></li> </ul> <p>You are given integers <var>n</var> and <var>s</var>. Determine if there exists an integer <var>b (b \geq 2)</var> such that <var>f(b,n)=s</var>. If the answer is positive, also find the smallest such <var>b</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq n \leq 10^{11}</var></li> <li><var>1 \leq s \leq 10^{11}</var></li> <li><var>n,\,s</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>n</var> <var>s</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists an integer <var>b (b \geq 2)</var> such that <var>f(b,n)=s</var>, print the smallest such <var>b</var>. If such <var>b</var> does not exist, print <code>-1</code> instead.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>87654 30 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>10 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>87654 138 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>100 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>87654 45678 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>-1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>31415926535 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>31415926535 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>1 31415926535 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>-1 </pre></section> </div> </span>
[ [ "87654\n30\n", "87654\n30\n" ] ]
p04017
AtCoder Regular Contest 060 - Tak and Hotels
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p><var>N</var> hotels are located on a straight line. The coordinate of the <var>i</var>-th hotel <var>(1 \leq i \leq N)</var> is <var>x_i</var>.</p> <p>Tak the traveler has the following two personal principles:</p> <ul> <li>He never travels a distance of more than <var>L</var> in a single day.</li> <li>He never sleeps in the open. That is, he must stay at a hotel at the end of a day.</li> </ul> <p>You are given <var>Q</var> queries. The <var>j</var>-th <var>(1 \leq j \leq Q)</var> query is described by two distinct integers <var>a_j</var> and <var>b_j</var>. For each query, find the minimum number of days that Tak needs to travel from the <var>a_j</var>-th hotel to the <var>b_j</var>-th hotel following his principles. It is guaranteed that he can always travel from the <var>a_j</var>-th hotel to the <var>b_j</var>-th hotel, in any given input.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 10^5</var></li> <li><var>1 \leq L \leq 10^9</var></li> <li><var>1 \leq Q \leq 10^5</var></li> <li><var>1 \leq x_i &lt; x_2 &lt; ... &lt; x_N \leq 10^9</var></li> <li><var>x_{i+1} - x_i \leq L</var></li> <li><var>1 \leq a_j,b_j \leq N</var></li> <li><var>a_j \neq b_j</var></li> <li><var>N,\,L,\,Q,\,x_i,\,a_j,\,b_j</var> are integers.</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li><var>200</var> points will be awarded for passing the test set satisfying <var>N \leq 10^3</var> and <var>Q \leq 10^3</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>x_1</var> <var>x_2</var> <var>...</var> <var>x_N</var> <var>L</var> <var>Q</var> <var>a_1</var> <var>b_1</var> <var>a_2</var> <var>b_2</var> : <var>a_Q</var> <var>b_Q</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>Q</var> lines. The <var>j</var>-th line <var>(1 \leq j \leq Q)</var> should contain the minimum number of days that Tak needs to travel from the <var>a_j</var>-th hotel to the <var>b_j</var>-th hotel.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>9 1 3 6 13 15 18 19 29 31 10 4 1 8 7 3 6 7 8 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 2 1 2 </pre> <p>For the <var>1</var>-st query, he can travel from the <var>1</var>-st hotel to the <var>8</var>-th hotel in <var>4</var> days, as follows:</p> <ul> <li>Day <var>1</var>: Travel from the <var>1</var>-st hotel to the <var>2</var>-nd hotel. The distance traveled is <var>2</var>.</li> <li>Day <var>2</var>: Travel from the <var>2</var>-nd hotel to the <var>4</var>-th hotel. The distance traveled is <var>10</var>.</li> <li>Day <var>3</var>: Travel from the <var>4</var>-th hotel to the <var>7</var>-th hotel. The distance traveled is <var>6</var>.</li> <li>Day <var>4</var>: Travel from the <var>7</var>-th hotel to the <var>8</var>-th hotel. The distance traveled is <var>10</var>.</li> </ul></section> </div> </span>
[ [ "9\n1 3 6 13 15 18 19 29 31\n10\n4\n1 8\n7 3\n6 7\n8 5\n", "9\n1 3 6 13 15 18 19 29 31\n10\n4\n1 8\n7 3\n6 7\n8 5\n" ] ]
p04018
AtCoder Regular Contest 060 - Best Representation
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Let <var>x</var> be a string of length at least <var>1</var>. We will call <var>x</var> a <em>good</em> string, if for any string <var>y</var> and any integer <var>k (k \geq 2)</var>, the string obtained by concatenating <var>k</var> copies of <var>y</var> is different from <var>x</var>. For example, <code>a</code>, <code>bbc</code> and <code>cdcdc</code> are good strings, while <code>aa</code>, <code>bbbb</code> and <code>cdcdcd</code> are not.</p> <p>Let <var>w</var> be a string of length at least <var>1</var>. For a sequence <var>F=(\,f_1,\,f_2,\,...,\,f_m)</var> consisting of <var>m</var> elements, we will call <var>F</var> a <em>good representation</em> of <var>w</var>, if the following conditions are both satisfied:</p> <ul> <li>For any <var>i \, (1 \leq i \leq m)</var>, <var>f_i</var> is a good string.</li> <li>The string obtained by concatenating <var>f_1,\,f_2,\,...,\,f_m</var> in this order, is <var>w</var>.</li> </ul> <p>For example, when <var>w=</var><code>aabb</code>, there are five good representations of <var>w</var>:</p> <ul> <li><var>(</var><code>aabb</code><var>)</var></li> <li><var>(</var><code>a</code><var>,</var><code>abb</code><var>)</var></li> <li><var>(</var><code>aab</code><var>,</var><code>b</code><var>)</var></li> <li><var>(</var><code>a</code><var>,</var><code>ab</code><var>,</var><code>b</code><var>)</var></li> <li><var>(</var><code>a</code><var>,</var><code>a</code><var>,</var><code>b</code><var>,</var><code>b</code><var>)</var></li> </ul> <p>Among the good representations of <var>w</var>, the ones with the smallest number of elements are called the <em>best representations</em> of <var>w</var>. For example, there are only one best representation of <var>w=</var><code>aabb</code>: <var>(</var><code>aabb</code><var>)</var>.</p> <p>You are given a string <var>w</var>. Find the following:</p> <ul> <li>the number of elements of a best representation of <var>w</var></li> <li>the number of the best representations of <var>w</var>, modulo <var>1000000007 \, (=10^9+7)</var></li> </ul> <p>(It is guaranteed that a good representation of <var>w</var> always exists.)</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq |w| \leq 500000 \, (=5 \times 10^5)</var></li> <li><var>w</var> consists of lowercase letters (<code>a</code>-<code>z</code>).</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li><var>400</var> points will be awarded for passing the test set satisfying <var>1 \leq |w| \leq 4000</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>w</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>2</var> lines.</p> <ul> <li>In the first line, print the number of elements of a best representation of <var>w</var>.</li> <li>In the second line, print the number of the best representations of <var>w</var>, modulo <var>1000000007</var>.</li> </ul> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>aab </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>bcbc </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 3 </pre> <ul> <li>In this case, there are <var>3</var> best representations of <var>2</var> elements:<ul> <li><var>(</var><code>b</code><var>,</var><code>cbc</code><var>)</var></li> <li><var>(</var><code>bc</code><var>,</var><code>bc</code><var>)</var></li> <li><var>(</var><code>bcb</code><var>,</var><code>c</code><var>)</var></li> </ul> </li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>ddd </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>3 1 </pre></section> </div> </span>
[ [ "aab\n", "aab\n" ] ]
p04019
AtCoder Grand Contest 003 - Wanna go back home
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke lives on an infinite two-dimensional plane. He is going on an <var>N</var>-day trip. At the beginning of Day <var>1</var>, he is at home. His plan is described in a string <var>S</var> of length <var>N</var>. On Day <var>i(1 ≦ i ≦ N)</var>, he will travel a positive distance in the following direction:</p> <ul> <li>North if the <var>i</var>-th letter of <var>S</var> is <code>N</code></li> <li>West if the <var>i</var>-th letter of <var>S</var> is <code>W</code></li> <li>South if the <var>i</var>-th letter of <var>S</var> is <code>S</code></li> <li>East if the <var>i</var>-th letter of <var>S</var> is <code>E</code></li> </ul> <p>He has not decided each day's travel distance. Determine whether it is possible to set each day's travel distance so that he will be back at home at the end of Day <var>N</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ | S | ≦ 1000</var></li> <li><var>S</var> consists of the letters <code>N</code>, <code>W</code>, <code>S</code>, <code>E</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>S</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <code>Yes</code> if it is possible to set each day's travel distance so that he will be back at home at the end of Day <var>N</var>. Otherwise, print <code>No</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>SENW </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes </pre> <p>If Snuke travels a distance of <var>1</var> on each day, he will be back at home at the end of day <var>4</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>NSNNSNSN </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>Yes </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>NNEW </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>No </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>W </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>No </pre></section> </div> </span>
[ [ "SENW\n", "SENW\n" ] ]
p04020
AtCoder Grand Contest 003 - Simplified mahjong
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has a large collection of cards. Each card has an integer between <var>1</var> and <var>N</var>, inclusive, written on it. He has <var>A_i</var> cards with an integer <var>i</var>.</p> <p>Two cards can form a pair if the absolute value of the difference of the integers written on them is at most <var>1</var>.</p> <p>Snuke wants to create the maximum number of pairs from his cards, on the condition that no card should be used in multiple pairs. Find the maximum number of pairs that he can create.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ N ≦ 10^5</var></li> <li><var>0 ≦ A_i ≦ 10^9 (1 ≦ i ≦ N)</var></li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> : <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum number of pairs that Snuke can create.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 4 0 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>For example, Snuke can create the following four pairs: <var>(1,1),(1,1),(3,4),(3,4)</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>8 2 0 1 6 0 8 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>9 </pre></section> </div> </span>
[ [ "4\n4\n0\n3\n2\n", "4\n4\n0\n3\n2\n" ] ]
p04021
AtCoder Grand Contest 003 - BBuBBBlesort!
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got an integer sequence of length <var>N</var> from his mother, as a birthday present. The <var>i</var>-th <var>(1 ≦ i ≦ N)</var> element of the sequence is <var>a_i</var>. The elements are pairwise distinct. He is sorting this sequence in increasing order. With supernatural power, he can perform the following two operations on the sequence in any order:</p> <ul> <li>Operation <var>1</var>: choose <var>2</var> consecutive elements, then reverse the order of those elements.</li> <li>Operation <var>2</var>: choose <var>3</var> consecutive elements, then reverse the order of those elements.</li> </ul> <p>Snuke likes Operation <var>2</var>, but not Operation <var>1</var>. Find the minimum number of Operation <var>1</var> that he has to perform in order to sort the sequence in increasing order.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ N ≦ 10^5</var></li> <li><var>0 ≦ A_i ≦ 10^9</var></li> <li>If <var>i ≠ j</var>, then <var>A_i ≠ A_j</var>.</li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> : <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of times Operation <var>1</var> that Snuke has to perform.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 2 4 3 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 </pre> <p>The given sequence can be sorted as follows:</p> <ul> <li>First, reverse the order of the last three elements. The sequence is now: <var>2,1,3,4</var>.</li> <li>Then, reverse the order of the first two elements. The sequence is now: <var>1,2,3,4</var>.</li> </ul> <p>In this sequence of operations, Operation <var>1</var> is performed once. It is not possible to sort the sequence with less number of Operation <var>1</var>, thus the answer is <var>1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 10 8 5 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre></section> </div> </span>
[ [ "4\n2\n4\n3\n1\n", "4\n2\n4\n3\n1\n" ] ]
p04022
AtCoder Grand Contest 003 - Anticube
<span class="lang-en"> <p>Score : <var>1100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got positive integers <var>s_1,...,s_N</var> from his mother, as a birthday present. There may be duplicate elements.</p> <p>He will circle some of these <var>N</var> integers. Since he dislikes cubic numbers, he wants to ensure that if both <var>s_i</var> and <var>s_j (i ≠ j)</var> are circled, the product <var>s_is_j</var> is <em>not</em> cubic. For example, when <var>s_1=1,s_2=1,s_3=2,s_4=4</var>, it is not possible to circle both <var>s_1</var> and <var>s_2</var> at the same time. It is not possible to circle both <var>s_3</var> and <var>s_4</var> at the same time, either.</p> <p>Find the maximum number of integers that Snuke can circle.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ N ≦ 10^5</var></li> <li><var>1 ≦ s_i ≦ 10^{10}</var></li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>s_1</var> : <var>s_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum number of integers that Snuke can circle.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>8 1 2 3 4 5 6 7 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>6 </pre> <p>Snuke can circle <var>1,2,3,5,6,7</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 2 4 8 16 32 64 </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>10 1 10 100 1000000007 10000000000 1000000009 999999999 999 999 999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>9 </pre></section> </div> </span>
[ [ "8\n1\n2\n3\n4\n5\n6\n7\n8\n", "8\n1\n2\n3\n4\n5\n6\n7\n8\n" ] ]
p04023
AtCoder Grand Contest 003 - Sequential operations on Sequence
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got an integer sequence from his mother, as a birthday present. The sequence has <var>N</var> elements, and the <var>i</var>-th of them is <var>i</var>. Snuke performs the following <var>Q</var> operations on this sequence. The <var>i</var>-th operation, described by a parameter <var>q_i</var>, is as follows:</p> <ul> <li>Take the first <var>q_i</var> elements from the sequence obtained by concatenating infinitely many copy of the current sequence, then replace the current sequence with those <var>q_i</var> elements.</li> </ul> <p>After these <var>Q</var> operations, find how many times each of the integers <var>1</var> through <var>N</var> appears in the final sequence.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ N ≦ 10^5</var></li> <li><var>0 ≦ Q ≦ 10^5</var></li> <li><var>1 ≦ q_i ≦ 10^{18}</var></li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>Q</var> <var>q_1</var> : <var>q_Q</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>N</var> lines. The <var>i</var>-th line <var>(1 ≦ i ≦ N)</var> should contain the number of times integer <var>i</var> appears in the final sequence after the <var>Q</var> operations.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 3 6 4 11 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 3 3 2 0 </pre> <p>After the first operation, the sequence becomes: <var>1,2,3,4,5,1</var>.</p> <p>After the second operation, the sequence becomes: <var>1,2,3,4</var>.</p> <p>After the third operation, the sequence becomes: <var>1,2,3,4,1,2,3,4,1,2,3</var>.</p> <p>In this sequence, integers <var>1,2,3,4,5</var> appear <var>3,3,3,2,0</var> times, respectively.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 10 9 13 18 8 10 10 9 19 22 27 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>7 4 4 3 3 2 2 2 0 0 </pre></section> </div> </span>
[ [ "5 3\n6\n4\n11\n", "5 3\n6\n4\n11\n" ] ]
p04024
AtCoder Grand Contest 003 - Fraction of Fractal
<span class="lang-en"> <p>Score : <var>1700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a grid from his mother, as a birthday present. The grid has <var>H</var> rows and <var>W</var> columns. Each cell is painted black or white. All black cells are <em>4-connected</em>, that is, it is possible to traverse from any black cell to any other black cell by just visiting black cells, where it is only allowed to move horizontally or vertically.</p> <p>The color of the cell at the <var>i</var>-th row and <var>j</var>-th column <var>(1 ≦ i ≦ H, 1 ≦ j ≦ W)</var> is represented by a character <var>s_{ij}</var>. If <var>s_{ij}</var> is <code>#</code>, the cell is painted black. If <var>s_{ij}</var> is <code>.</code>, the cell is painted white. At least one cell is painted black.</p> <p>We will define <em>fractals</em> as follows. The fractal of <em>level</em> <var>0</var> is a <var>1 × 1</var> grid with a black cell. The fractal of level <var>k+1</var> is obtained by arranging smaller grids in <var>H</var> rows and <var>W</var> columns, following the pattern of the Snuke's grid. At a position that corresponds to a black cell in the Snuke's grid, a copy of the fractal of level <var>k</var> is placed. At a position that corresponds to a white cell in the Snuke's grid, a grid whose cells are all white, with the same dimensions as the fractal of level <var>k</var>, is placed.</p> <p>You are given the description of the Snuke's grid, and an integer <var>K</var>. Find the number of connected components of black cells in the fractal of level <var>K</var>, modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ H,W ≦ 1000</var></li> <li><var>0 ≦ K ≦ 10^{18}</var></li> <li>Each <var>s_{ij}</var> is either <code>#</code> or <code>.</code>.</li> <li>All black cells in the given grid are 4-connected.</li> <li>There is at least one black cell in 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 Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>K</var> <var>s_{11} .. s_{1W}</var> : <var>s_{H1} .. s_{HW}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of connected components of black cells in the fractal of level <var>K</var>, modulo <var>10^9+7</var>.</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>20 </pre> <p>The fractal of level <var>K=3</var> in this case is shown below. There are <var>20</var> connected components of black cells.</p> <pre> .............#............. ............###............ ............#.#............ ..........#..#..#.......... .........#########......... .........#.##.##.#......... ..........#.....#.......... .........###...###......... .........#.#...#.#......... ....#........#........#.... ...###......###......###... ...#.#......#.#......#.#... .#..#..#..#..#..#..#..#..#. ########################### #.##.##.##.##.##.##.##.##.# .#.....#..#.....#..#.....#. ###...######...######...### #.#...#.##.#...#.##.#...#.# ....#.................#.... ...###...............###... ...#.#...............#.#... .#..#..#...........#..#..#. #########.........######### #.##.##.#.........#.##.##.# .#.....#...........#.....#. ###...###.........###...### #.#...#.#.........#.#...#.# </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 3 3 ### #.# ### </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>11 15 1000000000000000000 .....#......... ....###........ ....####....... ...######...... ...#######..... ..##.###.##.... ..##########... .###.....####.. .####...######. ############### #.##..##..##..# </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>301811921 </pre></section> </div> </span>
[ [ "3 3 3\n.#.\n###\n#.#\n", "3 3 3\n.#.\n###\n#.#\n" ] ]
p04025
AtCoder Regular Contest 059 - Be Together
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Evi has <var>N</var> integers <var>a_1,a_2,..,a_N</var>. His objective is to have <var>N</var> equal <strong>integers</strong> by transforming some of them.</p> <p>He may transform each integer at most once. Transforming an integer <var>x</var> into another integer <var>y</var> costs him <var>(x-y)^2</var> dollars. Even if <var>a_i=a_j (i≠j)</var>, he has to pay the cost separately for transforming each of them (See Sample 2).</p> <p>Find the minimum total cost to achieve his objective.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦100</var></li> <li><var>-100≦a_i≦100</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>Print the minimum total cost to achieve Evi's objective.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 4 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>8 </pre> <p>Transforming the both into <var>6</var>s will cost <var>(4-6)^2+(8-6)^2=8</var> dollars, which is the minimum.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 1 1 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 </pre> <p>Transforming the all into <var>2</var>s will cost <var>(1-2)^2+(1-2)^2+(3-2)^2=3</var> dollars. Note that Evi has to pay <var>(1-2)^2</var> dollar separately for transforming each of the two <var>1</var>s.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 4 2 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>5 </pre> <p>Leaving the <var>4</var> as it is and transforming the <var>2</var> and the <var>5</var> into <var>4</var>s will achieve the total cost of <var>(2-4)^2+(5-4)^2=5</var> dollars, which is the minimum.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>4 -100 -100 -100 -100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>0 </pre> <p>Without transforming anything, Evi's objective is already achieved. Thus, the necessary cost is <var>0</var>.</p></section> </div> </span>
[ [ "2\n4 8\n", "2\n4 8\n" ] ]
p04026
AtCoder Regular Contest 059 - Unbalanced
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given a string <var>t</var>, we will call it <em>unbalanced</em> if and only if the length of <var>t</var> is at least <var>2</var>, and more than half of the letters in <var>t</var> are the same. For example, both <code>voodoo</code> and <code>melee</code> are unbalanced, while neither <code>noon</code> nor <code>a</code> is.</p> <p>You are given a string <var>s</var> consisting of lowercase letters. Determine if there exists a (contiguous) substring of <var>s</var> that is unbalanced. If the answer is positive, show a position where such a substring occurs in <var>s</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≦ |s| ≦ 10^5</var></li> <li><var>s</var> consists of lowercase letters.</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li><var>200</var> points will be awarded for passing the test set satisfying <var>2 ≦ N ≦ 100</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>s</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists no unbalanced substring of <var>s</var>, print <code>-1 -1</code>.</p> <p>If there exists an unbalanced substring of <var>s</var>, let one such substring be <var>s_a s_{a+1} ... s_{b}</var> <var>(1 ≦ a &lt; b ≦ |s|)</var>, and print <code><var>a</var> <var>b</var></code>. If there exists more than one such substring, any of them will be accepted.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>needed </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 5 </pre> <p>The string <var>s_2 s_3 s_4 s_5</var> <var>=</var> <code>eede</code> is unbalanced. There are also other unbalanced substrings. For example, the output <code>2 6</code> will also be accepted.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>atcoder </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>-1 -1 </pre> <p>The string <code>atcoder</code> contains no unbalanced substring.</p></section> </div> </span>
[ [ "needed\n", "needed\n" ] ]
p04027
AtCoder Regular Contest 059 - Children and Candies
<span class="lang-en"> <p>Score : <var>800</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p><strong>12:17 (UTC): The sample input 1 and 2 were swapped. The error is now fixed. We are very sorry for your inconvenience.</strong></p> <p>There are <var>N</var> children in AtCoder Kindergarten, conveniently numbered <var>1</var> through <var>N</var>. Mr. Evi will distribute <var>C</var> indistinguishable candies to the children.</p> <p>If child <var>i</var> is given <var>a</var> candies, the child's <em>happiness</em> will become <var>x_i^a</var>, where <var>x_i</var> is the child's <em>excitement level</em>. The <em>activity level</em> of the kindergarten is the <strong>product</strong> of the happiness of all the <var>N</var> children.</p> <p>For each possible way to distribute <var>C</var> candies to the children by giving zero or more candies to each child, calculate the activity level of the kindergarten. Then, calculate the sum over all possible way to distribute <var>C</var> candies. This sum can be seen as a function of the children's excitement levels <var>x_1,..,x_N</var>, thus we call it <var>f(x_1,..,x_N)</var>.</p> <p>You are given integers <var>A_i,B_i (1≦i≦N)</var>. Find <img alt="" src="http://arc059.contest.atcoder.jp/img/arc/059/E_sigmaf.gif"> modulo <var>10^9+7</var>.</img></p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦400</var></li> <li><var>1≦C≦400</var></li> <li><var>1≦A_i≦B_i≦400 (1≦i≦N)</var></li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li><var>400</var> points will be awarded for passing the test set satisfying <var>A_i=B_i (1≦i≦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>C</var> <var>A_1</var> <var>A_2</var> ... <var>A_N</var> <var>B_1</var> <var>B_2</var> ... <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the value of <img alt="" src="http://arc059.contest.atcoder.jp/img/arc/059/E_sigmaf.gif"> modulo <var>10^9+7</var>.</img></p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>This case is included in the test set for the partial score, since <var>A_i=B_i</var>. We only have to consider the sum of the activity level of the kindergarten where the excitement level of both child <var>1</var> and child <var>2</var> are <var>1</var> (<var>f(1,1)</var>).</p> <ul> <li>If child <var>1</var> is given <var>0</var> candy, and child <var>2</var> is given <var>3</var> candies, the activity level of the kindergarten is <var>1^0*1^3=1</var>.</li> <li>If child <var>1</var> is given <var>1</var> candy, and child <var>2</var> is given <var>2</var> candies, the activity level of the kindergarten is <var>1^1*1^2=1</var>.</li> <li>If child <var>1</var> is given <var>2</var> candies, and child <var>2</var> is given <var>1</var> candy, the activity level of the kindergarten is <var>1^2*1^1=1</var>.</li> <li>If child <var>1</var> is given <var>3</var> candies, and child <var>2</var> is given <var>0</var> candy, the activity level of the kindergarten is <var>1^3*1^0=1</var>.</li> </ul> <p>Thus, <var>f(1,1)=1+1+1+1=4</var>, and the sum over all <var>f</var> is also <var>4</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 2 1 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>14 </pre> <p>Since there is only one child, child <var>1</var>'s happiness itself will be the activity level of the kindergarten. Since the only possible way to distribute <var>2</var> candies is to give both candies to child <var>1</var>, the activity level in this case will become the value of <var>f</var>.</p> <ul> <li>When the excitement level of child <var>1</var> is <var>1</var>, <var>f(1)=1^2=1</var>.</li> <li>When the excitement level of child <var>1</var> is <var>2</var>, <var>f(2)=2^2=4</var>.</li> <li>When the excitement level of child <var>1</var> is <var>3</var>, <var>f(3)=3^2=9</var>.</li> </ul> <p>Thus, the answer is <var>1+4+9=14</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>2 3 1 1 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>66 </pre> <p>Since it can be seen that <var>f(1,1)=4 , f(1,2)=15 , f(2,1)=15 , f(2,2)=32</var>, the answer is <var>4+15+15+32=66</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>4 8 3 1 4 1 3 1 4 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>421749 </pre> <p>This case is included in the test set for the partial score.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>3 100 7 6 5 9 9 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>139123417 </pre></section> </div> </span>
[ [ "2 3\n1 1\n1 1\n", "2 3\n1 1\n1 1\n" ] ]
p04028
AtCoder Regular Contest 059 - Unhappy Hacking
<span class="lang-en"> <p>Score : <var>800</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Sig has built his own keyboard. Designed for ultimate simplicity, this keyboard only has <var>3</var> keys on it: the <code>0</code> key, the <code>1</code> key and the backspace key.</p> <p>To begin with, he is using a plain text editor with this keyboard. This editor always displays one string (possibly empty). Just after the editor is launched, this string is empty. When each key on the keyboard is pressed, the following changes occur to the string:</p> <ul> <li>The <code>0</code> key: a letter <code>0</code> will be inserted to the right of the string.</li> <li>The <code>1</code> key: a letter <code>1</code> will be inserted to the right of the string.</li> <li>The backspace key: if the string is empty, nothing happens. Otherwise, the rightmost letter of the string is deleted.</li> </ul> <p>Sig has launched the editor, and pressed these keys <var>N</var> times in total. As a result, the editor displays a string <var>s</var>. Find the number of such ways to press the keys, modulo <var>10^9 + 7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ N ≦ 5000</var></li> <li><var>1 ≦ |s| ≦ N</var></li> <li><var>s</var> consists of the letters <code>0</code> and <code>1</code>.</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li><var>400</var> points will be awarded for passing the test set satisfying <var>1 ≦ N ≦ 300</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>s</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the ways to press the keys <var>N</var> times in total such that the editor displays the string <var>s</var> in the end, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>5 </pre> <p>We will denote the backspace key by <code>B</code>. The following <var>5</var> ways to press the keys will cause the editor to display the string <code>0</code> in the end: <code>00B</code>, <code>01B</code>, <code>0B0</code>, <code>1B0</code>, <code>BB0</code>. In the last way, nothing will happen when the backspace key is pressed.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>300 1100100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>519054663 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5000 01000001011101000100001101101111011001000110010101110010000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>500886057 </pre></section> </div> </span>
[ [ "3\n0\n", "3\n0\n" ] ]
p04029
AtCoder Beginner Contest 043 - Children and Candies (ABC Edit)
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> children in AtCoder Kindergarten. Mr. Evi will arrange the children in a line, then give <var>1</var> candy to the first child in the line, <var>2</var> candies to the second child, ..., <var>N</var> candies to the <var>N</var>-th child. How many candies will be necessary in total?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦100</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> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the necessary number of candies in total.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>6 </pre> <p>The answer is <var>1+2+3=6</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>55 </pre> <p>The sum of the integers from <var>1</var> to <var>10</var> is <var>55</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> <p>Only one child. The answer is <var>1</var> in this case.</p></section> </div> </span>
[ [ "3\n", "3\n" ] ]
p04030
AtCoder Beginner Contest 043 - Unhappy Hacking (ABC Edit)
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Sig has built his own keyboard. Designed for ultimate simplicity, this keyboard only has <var>3</var> keys on it: the <code>0</code> key, the <code>1</code> key and the backspace key.</p> <p>To begin with, he is using a plain text editor with this keyboard. This editor always displays one string (possibly empty). Just after the editor is launched, this string is empty. When each key on the keyboard is pressed, the following changes occur to the string:</p> <ul> <li>The <code>0</code> key: a letter <code>0</code> will be inserted to the right of the string.</li> <li>The <code>1</code> key: a letter <code>1</code> will be inserted to the right of the string.</li> <li>The backspace key: if the string is empty, nothing happens. Otherwise, the rightmost letter of the string is deleted.</li> </ul> <p>Sig has launched the editor, and pressed these keys several times. You are given a string <var>s</var>, which is a record of his keystrokes in order. In this string, the letter <code>0</code> stands for the <code>0</code> key, the letter <code>1</code> stands for the <code>1</code> key and the letter <code>B</code> stands for the backspace key. What string is displayed in the editor now?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ |s| ≦ 10</var> (<var>|s|</var> denotes the length of <var>s</var>)</li> <li><var>s</var> consists of the letters <code>0</code>, <code>1</code> and <code>B</code>.</li> <li>The correct answer is not an empty string.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>s</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the string displayed in the editor in the end.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>01B0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>00 </pre> <p>Each time the key is pressed, the string in the editor will change as follows: <code>0</code>, <code>01</code>, <code>0</code>, <code>00</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>0BB1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 </pre> <p>Each time the key is pressed, the string in the editor will change as follows: <code>0</code>, <code>(empty)</code>, <code>(empty)</code>, <code>1</code>.</p></section> </div> </span>
[ [ "01B0\n", "01B0\n" ] ]
p04031
AtCoder Beginner Contest 043 - Be Together
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Evi has <var>N</var> integers <var>a_1,a_2,..,a_N</var>. His objective is to have <var>N</var> equal <strong>integers</strong> by transforming some of them.</p> <p>He may transform each integer at most once. Transforming an integer <var>x</var> into another integer <var>y</var> costs him <var>(x-y)^2</var> dollars. Even if <var>a_i=a_j (i≠j)</var>, he has to pay the cost separately for transforming each of them (See Sample 2).</p> <p>Find the minimum total cost to achieve his objective.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦100</var></li> <li><var>-100≦a_i≦100</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>Print the minimum total cost to achieve Evi's objective.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 4 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>8 </pre> <p>Transforming the both into <var>6</var>s will cost <var>(4-6)^2+(8-6)^2=8</var> dollars, which is the minimum.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 1 1 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 </pre> <p>Transforming the all into <var>2</var>s will cost <var>(1-2)^2+(1-2)^2+(3-2)^2=3</var> dollars. Note that Evi has to pay <var>(1-2)^2</var> dollar separately for transforming each of the two <var>1</var>s.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 4 2 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>5 </pre> <p>Leaving the <var>4</var> as it is and transforming the <var>2</var> and the <var>5</var> into <var>4</var>s will achieve the total cost of <var>(2-4)^2+(5-4)^2=5</var> dollars, which is the minimum.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>4 -100 -100 -100 -100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>0 </pre> <p>Without transforming anything, Evi's objective is already achieved. Thus, the necessary cost is <var>0</var>.</p></section> </div> </span>
[ [ "2\n4 8\n", "2\n4 8\n" ] ]
p04032
AtCoder Beginner Contest 043 - Unbalanced
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given a string <var>t</var>, we will call it <em>unbalanced</em> if and only if the length of <var>t</var> is at least <var>2</var>, and more than half of the letters in <var>t</var> are the same. For example, both <code>voodoo</code> and <code>melee</code> are unbalanced, while neither <code>noon</code> nor <code>a</code> is.</p> <p>You are given a string <var>s</var> consisting of lowercase letters. Determine if there exists a (contiguous) substring of <var>s</var> that is unbalanced. If the answer is positive, show a position where such a substring occurs in <var>s</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≦ |s| ≦ 10^5</var></li> <li><var>s</var> consists of lowercase letters.</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li><var>200</var> points will be awarded for passing the test set satisfying <var>2 ≦ N ≦ 100</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>s</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists no unbalanced substring of <var>s</var>, print <code>-1 -1</code>.</p> <p>If there exists an unbalanced substring of <var>s</var>, let one such substring be <var>s_a s_{a+1} ... s_{b}</var> <var>(1 ≦ a &lt; b ≦ |s|)</var>, and print <code><var>a</var> <var>b</var></code>. If there exists more than one such substring, any of them will be accepted.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>needed </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 5 </pre> <p>The string <var>s_2 s_3 s_4 s_5</var> <var>=</var> <code>eede</code> is unbalanced. There are also other unbalanced substrings. For example, the output <code>2 6</code> will also be accepted.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>atcoder </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>-1 -1 </pre> <p>The string <code>atcoder</code> contains no unbalanced substring.</p></section> </div> </span>
[ [ "needed\n", "needed\n" ] ]
p04033
AtCoder Grand Contest 002 - Range Product
<span class="lang-en"> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given two integers <var>a</var> and <var>b</var> (<var>a≤b</var>). Determine if the product of the integers <var>a</var>, <var>a+1</var>, <var>…</var>, <var>b</var> is positive, negative or zero.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>a</var> and <var>b</var> are integers.</li> <li><var>-10^9≤a≤b≤10^9</var></li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li>In test cases worth <var>100</var> points, <var>-10≤a≤b≤10</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> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If the product is positive, print <code>Positive</code>. If it is negative, print <code>Negative</code>. If it is zero, print <code>Zero</code>.</p> </section> </div> </div> <hr> <div class="part"> <section> <h3>Sample Input 1</h3><pre>1 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Positive </pre> <p><var>1×2×3=6</var> is positive.</p> </section> </div> <hr> <div class="part"> <section> <h3>Sample Input 2</h3><pre>-3 -1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>Negative </pre> <p><var>(-3)×(-2)×(-1)=-6</var> is negative.</p> </section> </div> <hr> <div class="part"> <section> <h3>Sample Input 3</h3><pre>-1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Zero </pre> <p><var>(-1)×0×1=0</var>.</p></section> </div> </hr></hr></hr></hr></span>
[ [ "1 3\n", "1 3\n" ] ]
p04034
AtCoder Grand Contest 002 - Box and Ball
<span class="lang-en"> <div class="part"> <section> <h3>Problem Statement</h3><p>We have <var>N</var> boxes, numbered <var>1</var> through <var>N</var>. At first, box <var>1</var> contains one red ball, and each of the other boxes contains one white ball.</p> <p>Snuke will perform the following <var>M</var> operations, one by one. In the <var>i</var>-th operation, he randomly picks one ball from box <var>x_i</var>, then he puts it into box <var>y_i</var>.</p> <p>Find the number of boxes that may contain the red ball after all operations are performed.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≤N≤10^5</var></li> <li><var>1≤M≤10^5</var></li> <li><var>1≤x_i,y_i≤N</var></li> <li><var>x_i≠y_i</var></li> <li>Just before the <var>i</var>-th operation is performed, box <var>x_i</var> contains at least <var>1</var> ball.</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>x_1</var> <var>y_1</var> <var>:</var> <var>x_M</var> <var>y_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of boxes that may contain the red ball after all operations are performed.</p> </section> </div> </div> <hr> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 1 2 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>Just after the first operation, box <var>1</var> is empty, box <var>2</var> contains one red ball and one white ball, and box <var>3</var> contains one white ball.</p> <p>Now, consider the second operation. If Snuke picks the red ball from box <var>2</var>, the red ball will go into box <var>3</var>. If he picks the white ball instead, the red ball will stay in box <var>2</var>. Thus, the number of boxes that may contain the red ball after all operations, is <var>2</var>.</p> </section> </div> <hr> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 3 1 2 2 3 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 </pre> <p>All balls will go into box <var>3</var>.</p> </section> </div> <hr> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 4 1 2 2 3 4 1 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>3 </pre></section> </div> </hr></hr></hr></hr></span>
[ [ "3 2\n1 2\n2 3\n", "3 2\n1 2\n2 3\n" ] ]
p04035
AtCoder Grand Contest 002 - Knot Puzzle
<span class="lang-en"> <div class="part"> <section> <h3>Problem Statement</h3><p>We have <var>N</var> pieces of ropes, numbered <var>1</var> through <var>N</var>. The length of piece <var>i</var> is <var>a_i</var>.</p> <p>At first, for each <var>i (1≤i≤N-1)</var>, piece <var>i</var> and piece <var>i+1</var> are tied at the ends, forming one long rope with <var>N-1</var> knots. Snuke will try to untie all of the knots by performing the following operation repeatedly:</p> <ul> <li>Choose a (connected) rope with a total length of at least <var>L</var>, then untie one of its knots.</li> </ul> <p>Is it possible to untie all of the <var>N-1</var> knots by properly applying this operation? If the answer is positive, find one possible order to untie the knots.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≤N≤10^5</var></li> <li><var>1≤L≤10^9</var></li> <li><var>1≤a_i≤10^9</var></li> <li>All input values are integers.</li> </ul> </section> </div> <hr> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>L</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>If it is not possible to untie all of the <var>N-1</var> knots, print <code>Impossible</code>.</p> <p>If it is possible to untie all of the knots, print <code>Possible</code>, then print another <var>N-1</var> lines that describe a possible order to untie the knots. The <var>j</var>-th of those <var>N-1</var> lines should contain the index of the knot that is untied in the <var>j</var>-th operation. Here, the index of the knot connecting piece <var>i</var> and piece <var>i+1</var> is <var>i</var>.</p> <p>If there is more than one solution, output any.</p> </section> </div> </div> <hr> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 50 30 20 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Possible 2 1 </pre> <p>If the knot <var>1</var> is untied first, the knot <var>2</var> will become impossible to untie.</p> </section> </div> <hr> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 21 10 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>Impossible </pre> </section> </div> <hr> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 50 10 20 30 40 50 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Possible 1 2 3 4 </pre> <p>Another example of a possible solution is <var>3</var>, <var>4</var>, <var>1</var>, <var>2</var>.</p></section> </div> </hr></hr></hr></hr></span>
[ [ "3 50\n30 20 10\n", "3 50\n30 20 10\n" ] ]
p04036
AtCoder Grand Contest 002 - Stamp Rally
<span class="lang-en"> <div class="part"> <section> <h3>Problem Statement</h3><p>We have an undirected graph with <var>N</var> vertices and <var>M</var> edges. The vertices are numbered <var>1</var> through <var>N</var>, and the edges are numbered <var>1</var> through <var>M</var>. Edge <var>i</var> connects vertices <var>a_i</var> and <var>b_i</var>. The graph is connected.</p> <p>On this graph, <var>Q</var> pairs of brothers are participating in an activity called <em>Stamp Rally</em>. The Stamp Rally for the <var>i</var>-th pair will be as follows:</p> <ul> <li>One brother starts from vertex <var>x_i</var>, and the other starts from vertex <var>y_i</var>.</li> <li>The two explore the graph along the edges to visit <var>z_i</var> vertices in total, including the starting vertices. Here, a vertex is counted only once, even if it is visited multiple times, or visited by both brothers.</li> <li>The score is defined as the largest index of the edges traversed by either of them. Their objective is to minimize this value.</li> </ul> <p>Find the minimum possible score for each pair.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>3≤N≤10^5</var></li> <li><var>N−1≤M≤10^5</var></li> <li><var>1≤a_i&lt;b_i≤N</var></li> <li>The given graph is connected.</li> <li><var>1≤Q≤10^5</var></li> <li><var>1≤x_j&lt;y_j≤N</var></li> <li><var>3≤z_j≤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>M</var> <var>a_1</var> <var>b_1</var> <var>a_2</var> <var>b_2</var> <var>:</var> <var>a_M</var> <var>b_M</var> <var>Q</var> <var>x_1</var> <var>y_1</var> <var>z_1</var> <var>x_2</var> <var>y_2</var> <var>z_2</var> <var>:</var> <var>x_Q</var> <var>y_Q</var> <var>z_Q</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>Q</var> lines. The <var>i</var>-th line should contain the minimum possible score for the <var>i</var>-th pair of brothers.</p> </section> </div> </div> <hr> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 6 2 3 4 5 1 2 1 3 1 4 1 5 6 2 4 3 2 4 4 2 4 5 1 3 3 1 3 4 1 3 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 3 1 5 5 </pre></section> </div> </hr></hr></span>
[ [ "5 6\n2 3\n4 5\n1 2\n1 3\n1 4\n1 5\n6\n2 4 3\n2 4 4\n2 4 5\n1 3 3\n1 3 4\n1 3 5\n", "5 6\n2 3\n4 5\n1 2\n1 3\n1 4\n1 5\n6\n2 4 3\n2 4 4\n2 4 5\n1 3 3\n1 3 4\n1 3 5\n" ] ]
p04037
AtCoder Grand Contest 002 - Candy Piles
<span class="lang-en"> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> piles of candies on the table. The piles are numbered <var>1</var> through <var>N</var>. At first, pile <var>i</var> contains <var>a_i</var> candies.</p> <p>Snuke and Ciel are playing a game. They take alternating turns. Snuke goes first. In each turn, the current player must perform one of the following two operations:</p> <ol> <li>Choose a pile with the largest number of candies remaining, then eat all candies of that pile.</li> <li>From each pile with one or more candies remaining, eat one candy.</li> </ol> <p>The player who eats the last candy on the table, loses the game. Determine which player will win if both players play the game optimally.</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> </ul> </section> </div> <hr> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>a_2</var> <var>…</var> <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If Snuke will win, print <code>First</code>. If Ciel will win, print <code>Second</code>.</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>First </pre> <p>At the beginning of the game, pile <var>2</var> contains the most candies. If Snuke eats all candies of this pile, Ciel has no choice but to eat the last candy.</p> </section> </div> <hr> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>First </pre> <p>If Snuke eats one candy from each pile, Ciel is again left with the last candy.</p> </section> </div> <hr> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 1 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Second </pre></section> </div> </hr></hr></hr></hr></span>
[ [ "2\n1 3\n", "2\n1 3\n" ] ]
p04038
AtCoder Grand Contest 002 - Leftmost Ball
<span class="lang-en"> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke loves colorful balls. He has a total of <var>N×K</var> balls, <var>K</var> in each of his favorite <var>N</var> colors. The colors are numbered <var>1</var> through <var>N</var>.</p> <p>He will arrange all of the balls in a row from left to right, in arbitrary order. Then, for each of the <var>N</var> colors, he will paint the leftmost ball of that color into color <var>0</var>, a color different from any of the <var>N</var> original colors.</p> <p>After painting, how many sequences of the colors of the balls are possible? Find this number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≤N,K≤2,000</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> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the possible sequences of the colors of the balls after painting, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>The following <var>4</var> sequences are possible:</p> <ul> <li><var>(0,1,0,2)</var></li> <li><var>(0,0,1,2)</var></li> <li><var>(0,2,0,1)</var></li> <li><var>(0,0,2,1)</var></li> </ul> </section> </div> <hr> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 </pre> <p>The following <var>1</var> sequence is possible:</p> <ul> <li><var>(0,0,0)</var></li> </ul> </section> </div> <hr> <div class="part"> <section> <h3>Sample Input 3</h3><pre>2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>14 </pre> </section> </div> <hr> <div class="part"> <section> <h3>Sample Input 4</h3><pre>2000 2000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>546381702 </pre></section> </div> </hr></hr></hr></hr></hr></span>
[ [ "2 2\n", "2 2\n" ] ]
p04039
AtCoder Regular Contest 058 - Iroha's Obsession
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Iroha is very particular about numbers. There are <var>K</var> digits that she dislikes: <var>D_1, D_2, ..., D_K</var>.</p> <p>She is shopping, and now paying at the cashier. Her total is <var>N</var> yen (the currency of Japan), thus she has to hand at least <var>N</var> yen to the cashier (and possibly receive the change).</p> <p>However, as mentioned before, she is very particular about numbers. When she hands money to the cashier, the decimal notation of the amount must not contain any digits that she dislikes. Under this condition, she will hand the minimum amount of money.</p> <p>Find the amount of money that she will hand to the cashier.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ N &lt; 10000</var></li> <li><var> 1 ≦ K &lt; 10</var></li> <li><var> 0 ≦ D_1 &lt; D_2 &lt; … &lt; D_K≦9</var></li> <li><var>\{D_1,D_2,...,D_K\} ≠ \{1,2,3,4,5,6,7,8,9\}</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>D_1</var> <var>D_2</var> … <var>D_K</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the amount of money that Iroha will hand to the cashier.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>1000 8 1 3 4 5 6 7 8 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2000 </pre> <p>She dislikes all digits except <var>0</var> and <var>2</var>.</p> <p>The smallest integer equal to or greater than <var>N=1000</var> whose decimal notation contains only <var>0</var> and <var>2</var>, is <var>2000</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>9999 1 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>9999 </pre></section> </div> </span>
[ [ "1000 8\n1 3 4 5 6 7 8 9\n", "1000 8\n1 3 4 5 6 7 8 9\n" ] ]
p04040
AtCoder Regular Contest 058 - Iroha and a Grid
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
[ [ "2 3 1 1\n", "2 3 1 1\n" ] ]
p04041
AtCoder Regular Contest 058 - Iroha and Haiku
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p><em>Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order.</em></p> <p>Iroha is looking for <em><var>X,Y,Z</var>-Haiku</em> (defined below) in integer sequences.</p> <p>Consider all integer sequences of length <var>N</var> whose elements are between <var>1</var> and <var>10</var>, inclusive. Out of those <var>10^N</var> sequences, how many contain an <var>X,Y,Z</var>-Haiku?</p> <p>Here, an integer sequence <var>a_0, a_1, ..., a_{N-1}</var> is said to <em>contain an <var>X,Y,Z</var>-Haiku</em> if and only if there exist four indices <var>x, y, z, w (0 ≦ x &lt; y &lt; z &lt; w ≦ N)</var> such that all of the following are satisfied:</p> <ul> <li><var>a_x + a_{x+1} + ... + a_{y-1} = X</var></li> <li><var>a_y + a_{y+1} + ... + a_{z-1} = Y</var></li> <li><var>a_z + a_{z+1} + ... + a_{w-1} = Z</var></li> </ul> <p>Since the answer can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>3 ≦ N ≦ 40</var></li> <li><var>1 ≦ X ≦ 5</var></li> <li><var>1 ≦ Y ≦ 7</var></li> <li><var>1 ≦ Z ≦ 5</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>X</var> <var>Y</var> <var>Z</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the sequences that contain an <var>X,Y,Z</var>-Haiku, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 5 7 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 </pre> <p>Here, the only sequence that contains a <var>5,7,5</var>-Haiku is <var>[5, 7, 5]</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 5 7 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>34 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>37 4 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>863912418 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>40 5 7 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>562805100 </pre></section> </div> </span>
[ [ "3 5 7 5\n", "3 5 7 5\n" ] ]
p04042
AtCoder Regular Contest 058 - Iroha Loves Strings
<span class="lang-en"> <p>Score : <var>1500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Iroha has a sequence of <var>N</var> strings <var>s_1, s_2, ..., s_N</var>.</p> <p>She will choose some (possibly all) strings from the sequence, then concatenate those strings retaining the relative order, to produce a long string.</p> <p>Among all strings of length <var>K</var> that she can produce in this way, find the lexicographically smallest one.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ N ≦ 2000</var></li> <li><var>1 ≦ K ≦ 10^4</var></li> <li>For each <var>i</var>, <var>1 ≦ |s_i| ≦ K</var>.</li> <li><var>|s_1| + |s_2| + ... + |s_N| ≦ 10^6</var></li> <li>For each <var>i</var>, <var>s_i</var> consists of lowercase letters.</li> <li>There exists at least one string of length <var>K</var> that Iroha can produce.</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>s_1</var> <var>s_2</var> : <var>s_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the lexicographically smallest string of length <var>K</var> that Iroha can produce.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 7 at coder codar </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>atcodar </pre> <p><code>at</code> and <code>codar</code> should be chosen.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 7 coder codar at </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>codarat </pre> <p><code>codar</code> and <code>at</code> should be chosen.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 13 kyuri namida zzzzzzz aaaaaa </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>namidazzzzzzz </pre> <p><code>namida</code> and <code>zzzzzzz</code> should be chosen.</p></section> </div> </span>
[ [ "3 7\nat\ncoder\ncodar\n", "3 7\nat\ncoder\ncodar\n" ] ]
p04043
AtCoder Beginner Contest 042 - Iroha and Haiku (ABC Edition)
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Iroha loves <em>Haiku</em>. Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with <var>5</var>, <var>7</var> and <var>5</var> syllables, in this order.</p> <p>To create a Haiku, Iroha has come up with three different phrases. These phrases have <var>A</var>, <var>B</var> and <var>C</var> syllables, respectively. Determine whether she can construct a Haiku by using each of the phrases once, in some order.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦A,B,C≦10</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>If it is possible to construct a Haiku by using each of the phrases once, print <code>YES</code> (case-sensitive). Otherwise, print <code>NO</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 5 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>YES </pre> <p>Using three phrases of length <var>5</var>, <var>5</var> and <var>7</var>, it is possible to construct a Haiku.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 7 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>NO </pre></section> </div> </span>
[ [ "5 5 7\n", "5 5 7\n" ] ]
p04044
AtCoder Beginner Contest 042 - Iroha Loves Strings (ABC Edition)
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Iroha has a sequence of <var>N</var> strings <var>S_1, S_2, ..., S_N</var>. The length of each string is <var>L</var>.</p> <p>She will concatenate all of the strings in some order, to produce a long string.</p> <p>Among all strings that she can produce in this way, find the lexicographically smallest one.</p> <p>Here, a string <var>s=s_1s_2s_3</var>...<var>s_n</var> is <em>lexicographically smaller</em> than another string <var>t=t_1t_2t_3</var>...<var>t_m</var> if and only if one of the following holds: <ul> <li>There exists an index <var>i(1≦i≦min(n,m))</var>, such that <var>s_j = t_j</var> for all indices <var>j(1≦j&lt;i)</var>, and <var>s_i&lt;t_i</var>.</li> <li><var>s_i = t_i</var> for all integers <var>i(1≦i≦min(n,m))</var>, and <var>n&lt;m</var>.</li> </ul></p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ N, L ≦ 100</var></li> <li>For each <var>i</var>, the length of <var>S_i</var> equals <var>L</var>.</li> <li>For each <var>i</var>, <var>S_i</var> consists of lowercase letters.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>L</var> <var>S_1</var> <var>S_2</var> : <var>S_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the lexicographically smallest string that Iroha can produce.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 dxx axx cxx </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>axxcxxdxx </pre> <p>The following order should be used: <code>axx</code>, <code>cxx</code>, <code>dxx</code>.</p></section> </div> </span>
[ [ "3 3\ndxx\naxx\ncxx\n", "3 3\ndxx\naxx\ncxx\n" ] ]
p04045
AtCoder Beginner Contest 042 - Iroha's Obsession
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Iroha is very particular about numbers. There are <var>K</var> digits that she dislikes: <var>D_1, D_2, ..., D_K</var>.</p> <p>She is shopping, and now paying at the cashier. Her total is <var>N</var> yen (the currency of Japan), thus she has to hand at least <var>N</var> yen to the cashier (and possibly receive the change).</p> <p>However, as mentioned before, she is very particular about numbers. When she hands money to the cashier, the decimal notation of the amount must not contain any digits that she dislikes. Under this condition, she will hand the minimum amount of money.</p> <p>Find the amount of money that she will hand to the cashier.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ N &lt; 10000</var></li> <li><var> 1 ≦ K &lt; 10</var></li> <li><var> 0 ≦ D_1 &lt; D_2 &lt; … &lt; D_K≦9</var></li> <li><var>\{D_1,D_2,...,D_K\} ≠ \{1,2,3,4,5,6,7,8,9\}</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>D_1</var> <var>D_2</var> … <var>D_K</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the amount of money that Iroha will hand to the cashier.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>1000 8 1 3 4 5 6 7 8 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2000 </pre> <p>She dislikes all digits except <var>0</var> and <var>2</var>.</p> <p>The smallest integer equal to or greater than <var>N=1000</var> whose decimal notation contains only <var>0</var> and <var>2</var>, is <var>2000</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>9999 1 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>9999 </pre></section> </div> </span>
[ [ "1000 8\n1 3 4 5 6 7 8 9\n", "1000 8\n1 3 4 5 6 7 8 9\n" ] ]
p04046
AtCoder Beginner Contest 042 - Iroha and a Grid
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
[ [ "2 3 1 1\n", "2 3 1 1\n" ] ]
p04047
AtCoder Grand Contest 001 - BBQ Easy
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having a barbeque party.</p> <p>At the party, he will make <var>N</var> servings of <em>Skewer Meal</em>.</p> <div style="text-align: center;"> <img src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/abbq.png"> <p>Example of a serving of Skewer Meal</p> </img></div> <p>He has a stock of <var>2N</var> skewers, all of which will be used in Skewer Meal. The length of the <var>i</var>-th skewer is <var>L_i</var>. Also, he has an infinite supply of ingredients.</p> <p>To make a serving of Skewer Meal, he picks <var>2</var> skewers and threads ingredients onto those skewers. Let the length of the shorter skewer be <var>x</var>, then the serving can hold the maximum of <var>x</var> ingredients.</p> <p>What is the maximum total number of ingredients that his <var>N</var> servings of Skewer Meal can hold, if he uses the skewers optimally?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦100</var></li> <li><var>1≦L_i≦100</var></li> <li>For each <var>i</var>, <var>L_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>L_1</var> <var>L_2</var> <var>...</var> <var>L_{2N}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum total number of ingredients that Snuke's <var>N</var> servings of Skewer Meal can hold.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 1 3 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>If he makes a serving using the first and third skewers, and another using the second and fourth skewers, each serving will hold <var>1</var> and <var>2</var> ingredients, for the total of <var>3</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 100 1 2 3 14 15 58 58 58 29 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>135 </pre></section> </div> </span>
[ [ "2\n1 3 1 2\n", "2\n1 3 1 2\n" ] ]
p04048
AtCoder Grand Contest 001 - Mysterious Light
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is conducting an optical experiment using mirrors and his new invention, the rifle of <em>Mysterious Light</em>.</p> <p>Three mirrors of length <var>N</var> are set so that they form an equilateral triangle. Let the vertices of the triangle be <var>a, b</var> and <var>c</var>.</p> <p>Inside the triangle, the rifle is placed at the point <var>p</var> on segment <var>ab</var> such that <var>ap = X</var>. (The size of the rifle is negligible.) Now, the rifle is about to fire a ray of Mysterious Light in the direction of <var>bc</var>.</p> <p>The ray of Mysterious Light will travel in a straight line, and will be reflected by mirrors, in the same ways as "ordinary" light. There is one major difference, though: it will be also reflected by its own trajectory as if it is a mirror! When the ray comes back to the rifle, the ray will be absorbed.</p> <p>The following image shows the ray's trajectory where <var>N = 5</var> and <var>X = 2</var>.</p> <div style="text-align: center;"> <img alt="btriangle.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/btriangle.png"> </img></div> <p>It can be shown that the ray eventually comes back to the rifle and is absorbed, regardless of the values of <var>N</var> and <var>X</var>. Find the total length of the ray's trajectory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦10^{12}</var></li> <li><var>1≦X≦N-1</var></li> <li><var>N</var> and <var>X</var> are integers.</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Points</h3><ul> <li><var>300</var> points will be awarded for passing the test set satisfying <var>N≦1000</var>.</li> <li>Another <var>200</var> points will be awarded for passing the test set without additional constraints.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>X</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the total length of the ray's trajectory.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>12 </pre> <p>Refer to the image in the Problem Statement section. The total length of the trajectory is <var>2+3+2+2+1+1+1 = 12</var>.</p></section> </div> </span>
[ [ "5 2\n", "5 2\n" ] ]
p04049
AtCoder Grand Contest 001 - Shorten Diameter
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the number of edges on the simple path from <var>u</var> to <var>v</var>. The diameter of a tree is the maximum among the distances between any two vertices. We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p> <p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p> <p>You want to remove zero or more vertices from the tree, so that the resulting tree is good. When a vertex is removed, all incident edges will also be removed. The resulting graph must be connected.</p> <p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦2000</var></li> <li><var>1≦K≦N-1</var></li> <li><var>1≦A_i≦N, 1≦B_i≦N</var></li> <li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_{N-1}</var> <var>B_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 2 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p> <div style="text-align: center;"> <img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 1 2 3 2 4 2 1 6 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Since the given tree is already good, you do not need to remove any vertex.</p></section> </div> </span>
[ [ "6 2\n1 2\n3 2\n4 2\n1 6\n5 6\n", "6 2\n1 2\n3 2\n4 2\n1 6\n5 6\n" ] ]
p04050
AtCoder Grand Contest 001 - Arrays and Palindrome
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke got a present from his mother on his birthday. The present was a pair of two sequences <var>a</var> and <var>b</var>, consisting of positive integers. They satisfied all of the following properties:</p> <ul> <li>The sum of all elements of <var>a</var> is <var>N</var>.</li> <li>The sum of all elements of <var>b</var> is <var>N</var>.</li> <li>Any string of length <var>N</var> that satisfies the following two conditions (1) and (2) will also satisfy the condition (3).</li> <ul style="list-style:none;"> <li><b>(1)</b> Any of the following forms a palindrome: the first <var>a_1</var> letters, the following <var>a_2</var> letters, the following <var>a_3</var> letters and so on.</li> <li><b>(2)</b> Any of the following forms a palindrome: the first <var>b_1</var> letters, the following <var>b_2</var> letters, the following <var>b_3</var> letters and so on.</li> <li><b>(3)</b> All <var>N</var> letters are the same.</li> </ul> </ul> <p>He was happy, until one day he lost both of the sequences. Now, he only remembers that the sequence <var>a</var> was a permutation of another sequence <var>A</var> of length <var>M</var>.</p> <p>To bring him happiness again, his mother has decided to give him another pair of sequences <var>a</var> and <var>b</var> that satisfies his favorite properties and is consistent with his memory.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦A_i≦10^5</var></li> <li>The sum of all <var>A_i</var> equals <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>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>If there exists a pair of sequences <var>a</var> and <var>b</var> that satisfies the properties and is consistent with Snuke's memory, print three lines. The first line must contain the sequence <var>a</var>, the second line must contain the length of the sequence <var>b</var>, and the third line must contain the sequence <var>b</var>.</p> <p>If such a pair does not exist (because Snuke's memory is wrong or some other reason), print a single line containing the word <code>Impossible</code> (case-sensitive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 1 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 3 1 2 3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>55 10 1 2 3 4 5 6 7 8 9 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Impossible </pre></section> </div> </span>
[ [ "3 2\n2 1\n", "3 2\n2 1\n" ] ]
p04051
AtCoder Grand Contest 001 - BBQ Hard
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke is having another barbeque party.</p> <p>This time, he will make one serving of <em>Skewer Meal</em>.</p> <p>He has a stock of <var>N</var> <em>Skewer Meal Packs</em>. The <var>i</var>-th Skewer Meal Pack contains one skewer, <var>A_i</var> pieces of beef and <var>B_i</var> pieces of green pepper. All skewers in these packs are different and distinguishable, while all pieces of beef and all pieces of green pepper are, respectively, indistinguishable.</p> <p>To make a Skewer Meal, he chooses two of his Skewer Meal Packs, and takes out all of the contents from the chosen packs, that is, two skewers and some pieces of beef or green pepper. (Remaining Skewer Meal Packs will not be used.) Then, all those pieces of food are threaded onto both skewers, one by one, in any order.</p> <p>(See the image in the Sample section for better understanding.)</p> <p>In how many different ways can he make a Skewer Meal? Two ways of making a Skewer Meal is different if and only if the sets of the used skewers are different, or the orders of the pieces of food are different. Since this number can be extremely large, find it modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦200,000</var></li> <li><var>1≦A_i≦2000, 1≦B_i≦2000</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>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways Snuke can make a serving of Skewer Meal, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 1 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>The <var>26</var> ways of making a Skewer Meal are shown below. Gray bars represent skewers, each with a number denoting the Skewer Meal Set that contained the skewer. Brown and green rectangles represent pieces of beef and green pepper, respectively.</p> <div style="text-align: center;"> <img alt="ebbq.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ebbq.png"> </img></div></section> </div> </span>
[ [ "3\n1 1\n1 1\n2 1\n", "3\n1 1\n1 1\n2 1\n" ] ]
p04052
AtCoder Grand Contest 001 - Wide Swap
<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>
[ [ "4 2\n4 2 3 1\n", "4 2\n4 2 3 1\n" ] ]