id
stringlengths
6
6
slug_name
stringlengths
1
105
pretty_content
stringlengths
1
27.2k
test_cases
sequencelengths
1
1
p03356
AtCoder Regular Contest 097 - Equals
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a permutation of the integers from <var>1</var> through <var>N</var>, <var>p_1</var>, <var>p_2</var>, .., <var>p_N</var>. We also have <var>M</var> pairs of two integers between <var>1</var> and <var>N</var> (inclusive), represented as <var>(x_1,y_1)</var>, <var>(x_2,y_2)</var>, .., <var>(x_M,y_M)</var>. AtCoDeer the deer is going to perform the following operation on <var>p</var> as many times as desired so that the number of <var>i</var> (<var>1</var> <var>≤</var> <var>i</var> <var>≤</var> <var>N</var>) such that <var>p_i = i</var> is maximized:</p> <ul> <li>Choose <var>j</var> such that <var>1</var> <var>≤</var> <var>j</var> <var>≤</var> <var>M</var>, and swap <var>p_{x_j}</var> and <var>p_{y_j}</var>.</li> </ul> <p>Find the maximum possible number of <var>i</var> such that <var>p_i = i</var> after operations.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2</var> <var>≤</var> <var>N</var> <var>≤</var> <var>10^5</var></li> <li><var>1</var> <var>≤</var> <var>M</var> <var>≤</var> <var>10^5</var></li> <li><var>p</var> is a permutation of integers from <var>1</var> through <var>N</var>.</li> <li><var>1</var> <var>≤</var> <var>x_j,y_j</var> <var>≤</var> <var>N</var></li> <li><var>x_j</var> <var>≠</var> <var>y_j</var></li> <li>If <var>i</var> <var>≠</var> <var>j</var>, <var>\{x_i,y_i\}</var> <var>≠</var> <var>\{x_j,y_j\}</var>.</li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>p_1</var> <var>p_2</var> <var>..</var> <var>p_N</var> <var>x_1</var> <var>y_1</var> <var>x_2</var> <var>y_2</var> <var>:</var> <var>x_M</var> <var>y_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum possible number of <var>i</var> such that <var>p_i = i</var> after operations.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 2 5 3 1 4 2 1 3 5 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>If we perform the operation by choosing <var>j=1</var>, <var>p</var> becomes <code>1 3 5 4 2</code>, which is optimal, so the answer is <var>2</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 2 3 2 1 1 2 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 </pre> <p>If we perform the operation by, for example, choosing <var>j=1</var>, <var>j=2</var>, <var>j=1</var> in this order, <var>p</var> becomes <code>1 2 3</code>, which is obviously optimal. Note that we may choose the same <var>j</var> any number of times.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>10 8 5 3 6 8 7 10 9 1 2 4 3 1 4 1 5 9 2 5 6 5 3 5 8 9 7 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>8 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>5 1 1 2 3 4 5 1 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>5 </pre> <p>We do not have to perform the operation. </p></section> </div> </span>
[ [ "5 2\n5 3 1 4 2\n1 3\n5 4\n", "5 2\n5 3 1 4 2\n1 3\n5 4\n" ] ]
p03357
AtCoder Regular Contest 097 - Sorted and Sorted
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>2N</var> balls, <var>N</var> white and <var>N</var> black, arranged in a row. The integers from <var>1</var> through <var>N</var> are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the <var>i</var>-th ball from the left (<var>1</var> <var>≤</var> <var>i</var> <var>≤</var> <var>2N</var>) is <var>a_i</var>, and the color of this ball is represented by a letter <var>c_i</var>. <var>c_i</var> <var>=</var> <code>W</code> represents the ball is white; <var>c_i</var> <var>=</var> <code>B</code> represents the ball is black.</p> <p>Takahashi the human wants to achieve the following objective:</p> <ul> <li>For every pair of integers <var>(i,j)</var> such that <var>1</var> <var>≤</var> <var>i</var> <var>&lt;</var> <var>j</var> <var>≤</var> <var>N</var>, the white ball with <var>i</var> written on it is to the left of the white ball with <var>j</var> written on it.</li> <li>For every pair of integers <var>(i,j)</var> such that <var>1</var> <var>≤</var> <var>i</var> <var>&lt;</var> <var>j</var> <var>≤</var> <var>N</var>, the black ball with <var>i</var> written on it is to the left of the black ball with <var>j</var> written on it.</li> </ul> <p>In order to achieve this, he can perform the following operation:</p> <ul> <li>Swap two adjacent balls.</li> </ul> <p>Find the minimum number of operations required to achieve the objective.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1</var> <var>≤</var> <var>N</var> <var>≤</var> <var>2000</var></li> <li><var>1</var> <var>≤</var> <var>a_i</var> <var>≤</var> <var>N</var></li> <li><var>c_i</var> <var>=</var> <code>W</code> or <var>c_i</var> <var>=</var> <code>B</code>.</li> <li>If <var>i</var> <var>≠</var> <var>j</var>, <var>(a_i,c_i)</var> <var>≠</var> <var>(a_j,c_j)</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>c_1</var> <var>a_1</var> <var>c_2</var> <var>a_2</var> <var>:</var> <var>c_{2N}</var> <var>a_{2N}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of operations required to achieve the objective.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 B 1 W 2 B 3 W 1 W 3 B 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>The objective can be achieved in four operations, for example, as follows:</p> <ul> <li>Swap the black <var>3</var> and white <var>1</var>.</li> <li>Swap the white <var>1</var> and white <var>2</var>.</li> <li>Swap the black <var>3</var> and white <var>3</var>.</li> <li>Swap the black <var>3</var> and black <var>2</var>.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 B 4 W 4 B 3 W 3 B 2 W 2 B 1 W 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>18 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>9 W 3 B 1 B 4 W 1 B 5 W 9 W 2 B 6 W 5 B 3 W 8 B 9 W 7 B 2 B 8 W 4 W 6 B 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>41 </pre></section> </div> </span>
[ [ "3\nB 1\nW 2\nB 3\nW 1\nW 3\nB 2\n", "3\nB 1\nW 2\nB 3\nW 1\nW 3\nB 2\n" ] ]
p03358
AtCoder Regular Contest 097 - Monochrome Cat
<span class="lang-en"> <p>Score : <var>800</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>. The <var>i</var>-th edge connects Vertex <var>x_i</var> and <var>y_i</var>. Each vertex is painted white or black. The initial color of Vertex <var>i</var> is represented by a letter <var>c_i</var>. <var>c_i</var> <var>=</var> <code>W</code> represents the vertex is white; <var>c_i</var> <var>=</var> <code>B</code> represents the vertex is black.</p> <p>A cat will walk along this tree. More specifically, she performs one of the following in one second repeatedly:</p> <ul> <li>Choose a vertex that is adjacent to the vertex where she is currently, and move to that vertex. Then, invert the color of the destination vertex.</li> <li>Invert the color of the vertex where she is currently.</li> </ul> <p>The cat's objective is to paint all the vertices black. She may start and end performing actions at any vertex. At least how many seconds does it takes for the cat to achieve her objective?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1</var> <var>≤</var> <var>N</var> <var>≤</var> <var>10^5</var></li> <li><var>1</var> <var>≤</var> <var>x_i,y_i</var> <var>≤</var> <var>N</var> (<var>1</var> <var>≤</var> <var>i</var> <var>≤</var> <var>N-1</var>)</li> <li>The given graph is a tree.</li> <li><var>c_i</var> <var>=</var> <code>W</code> or <var>c_i</var> <var>=</var> <code>B</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>x_1</var> <var>y_1</var> <var>x_2</var> <var>y_2</var> <var>:</var> <var>x_{N-1}</var> <var>y_{N-1}</var> <var>c_1c_2..c_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of seconds required to achieve the objective.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 1 2 2 3 2 4 4 5 WBBWW </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>5 </pre> <p>The objective can be achieved in five seconds, for example, as follows:</p> <ul> <li>Start at Vertex <var>1</var>. Change the color of Vertex <var>1</var> to black.</li> <li>Move to Vertex <var>2</var>, then change the color of Vertex <var>2</var> to white.</li> <li>Change the color of Vertex <var>2</var> to black.</li> <li>Move to Vertex <var>4</var>, then change the color of Vertex <var>4</var> to black.</li> <li>Move to Vertex <var>5</var>, then change the color of Vertex <var>5</var> to black.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 3 1 4 5 2 6 6 1 3 4 WWBWBB </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>7 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1 B </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>20 2 19 5 13 6 4 15 6 12 19 13 19 3 11 8 3 3 20 16 13 7 14 3 17 7 8 10 20 11 9 8 18 8 2 10 1 6 13 WBWBWBBWWWBBWWBBBBBW </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>21 </pre></section> </div> </span>
[ [ "5\n1 2\n2 3\n2 4\n4 5\nWBBWW\n", "5\n1 2\n2 3\n2 4\n4 5\nWBBWW\n" ] ]
p03359
AtCoder Beginner Contest 096 - Day of Takahashi
<span class="lang-en"> <p>Score: <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>In AtCoder Kingdom, Gregorian calendar is used, and dates are written in the "year-month-day" order, or the "month-day" order without the year.<br/> For example, May <var>3</var>, <var>2018</var> is written as <var>2018</var>-<var>5</var>-<var>3</var>, or <var>5</var>-<var>3</var> without the year. </p> <p>In this country, a date is called <em>Takahashi</em> when the month and the day are equal as numbers. For example, <var>5</var>-<var>5</var> is Takahashi.<br/> How many days from <var>2018</var>-<var>1</var>-<var>1</var> through <var>2018</var>-<var>a</var>-<var>b</var> are Takahashi?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3> <ul> <li><var>a</var> is an integer between <var>1</var> and <var>12</var> (inclusive).</li> <li><var>b</var> is an integer between <var>1</var> and <var>31</var> (inclusive).</li> <li><var>2018</var>-<var>a</var>-<var>b</var> is a valid date in Gregorian calendar.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3> <p>Input is given from Standard Input in the following format:</p> <pre><var>a</var> <var>b</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3> <p>Print the number of days from <var>2018</var>-<var>1</var>-<var>1</var> through <var>2018</var>-<var>a</var>-<var>b</var> that are Takahashi.</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>5 </pre> <p>There are five days that are Takahashi: <var>1</var>-<var>1</var>, <var>2</var>-<var>2</var>, <var>3</var>-<var>3</var>, <var>4</var>-<var>4</var> and <var>5</var>-<var>5</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 </pre> <p>There is only one day that is Takahashi: <var>1</var>-<var>1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>11 30 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>11 </pre> <p>There are eleven days that are Takahashi: <var>1</var>-<var>1</var>, <var>2</var>-<var>2</var>, <var>3</var>-<var>3</var>, <var>4</var>-<var>4</var>, <var>5</var>-<var>5</var>, <var>6</var>-<var>6</var>, <var>7</var>-<var>7</var>, <var>8</var>-<var>8</var>, <var>9</var>-<var>9</var>, <var>10</var>-<var>10</var> and <var>11</var>-<var>11</var>.</p></section> </div> </span>
[ [ "5 5\n", "5 5\n" ] ]
p03360
AtCoder Beginner Contest 096 - Maximum Sum
<span class="lang-en"> <p>Score: <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>There are three positive integers <var>A</var>, <var>B</var> and <var>C</var> written on a blackboard. E869120 performs the following operation <var>K</var> times:</p> <ul> <li>Choose one integer written on the blackboard and let the chosen integer be <var>n</var>. Replace the chosen integer with <var>2n</var>.</li> </ul> <p>What is the largest possible sum of the integers written on the blackboard after <var>K</var> operations?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3> <ul> <li><var>A, B</var> and <var>C</var> are integers between <var>1</var> and <var>50</var> (inclusive).</li> <li><var>K</var> is an integer between <var>1</var> and <var>10</var> (inclusive).</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3> <p>Input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> <var>C</var> <var>K</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3> <p>Print the largest possible sum of the integers written on the blackboard after <var>K</var> operations by E869220.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 3 11 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>30 </pre> <p>In this sample, <var>5, 3, 11</var> are initially written on the blackboard, and E869120 can perform the operation once.<br/> There are three choices: </p> <ol> <li>Double <var>5</var>: The integers written on the board after the operation are <var>10, 3, 11</var>.</li> <li>Double <var>3</var>: The integers written on the board after the operation are <var>5, 6, 11</var>.</li> <li>Double <var>11</var>: The integers written on the board after the operation are <var>5, 3, 22</var>.</li> </ol> <p>If he chooses 3., the sum of the integers written on the board afterwards is <var>5 + 3 + 22 = 30</var>, which is the largest among 1. through 3. </p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 3 4 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>22 </pre> <p>E869120 can perform the operation twice. The sum of the integers eventually written on the blackboard is maximized as follows: </p> <ul> <li>First, double <var>4</var>. The integers written on the board are now <var>3, 3, 8</var>. </li> <li>Next, double <var>8</var>. The integers written on the board are now <var>3, 3, 16</var>. </li> </ul> <p>Then, the sum of the integers eventually written on the blackboard is <var>3 + 3 + 16 = 22</var>.</p></section> </div> </span>
[ [ "5 3 11\n1\n", "5 3 11\n1\n" ] ]
p03361
AtCoder Beginner Contest 096 - Grid Repainting 2
<span class="lang-en"> <p>Score: <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>We have a canvas divided into a grid with <var>H</var> rows and <var>W</var> columns. The square at the <var>i</var>-th row from the top and the <var>j</var>-th column from the left is represented as <var>(i, j)</var>.<br/> Initially, all the squares are white. square1001 wants to draw a picture with black paint. His specific objective is to make Square <var>(i, j)</var> black when <var>s_{i, j}=</var> <code>#</code>, and to make Square <var>(i, j)</var> white when <var>s_{i, j}=</var> <code>.</code>.<br/> However, since he is not a good painter, he can only choose two squares that are horizontally or vertically adjacent and paint those squares black, for some number of times (possibly zero). He may choose squares that are already painted black, in which case the color of those squares remain black.<br/> Determine if square1001 can achieve his objective. </p> </section> </div> <div class="part"> <section> <h3>Constraints</h3> <ul> <li><var>H</var> is an integer between <var>1</var> and <var>50</var> (inclusive).</li> <li><var>W</var> is an integer between <var>1</var> and <var>50</var> (inclusive).</li> <li>For every <var>(i, j)</var> <var>(1 \leq i \leq H, 1 \leq j \leq W)</var>, <var>s_{i, j}</var> is <code>#</code> or <code>.</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3> <p>Input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>s_{1, 1} s_{1, 2} s_{1, 3} ... s_{1, W}</var> <var>s_{2, 1} s_{2, 2} s_{2, 3} ... s_{2, W}</var> <var>:</var> <var>:</var> <var>s_{H, 1} s_{H, 2} s_{H, 3} ... s_{H, W}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3> <p>If square1001 can achieve his objective, print <code>Yes</code>; if he cannot, print <code>No</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 .#. ### .#. </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes </pre> <p>One possible way to achieve the objective is shown in the figure below. Here, the squares being painted are marked by stars.</p> <p><img alt=" " src="https://img.atcoder.jp/abc096/18f94b6627ec5dc8aa4f6d99ae1c8fca.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 5 #.#.# .#.#. #.#.# .#.#. #.#.# </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>No </pre> <p>square1001 cannot achieve his objective here.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>11 11 ...#####... .##.....##. #..##.##..# #..##.##..# #.........# #...###...# .#########. .#.#.#.#.#. ##.#.#.#.## ..##.#.##.. .##..#..##. </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Yes </pre></section> </div> </span>
[ [ "3 3\n.#.\n###\n.#.\n", "3 3\n.#.\n###\n.#.\n" ] ]
p03362
AtCoder Beginner Contest 096 - Five Five Everywhere
<span class="lang-en"> <p>Score: <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>Print a sequence <var>a_1, a_2, ..., a_N</var> whose length is <var>N</var> that satisfies the following conditions:</p> <ul> <li><var>a_i</var> <var>(1 \leq i \leq N)</var> is a prime number at most <var>55</var> <var>555</var>.</li> <li>The values of <var>a_1, a_2, ..., a_N</var> are all different.</li> <li>In every choice of five different integers from <var>a_1, a_2, ..., a_N</var>, the sum of those integers is a composite number.</li> </ul> <p>If there are multiple such sequences, printing any of them is accepted.</p> </section> </div> <div class="part"> <section> <h3>Notes</h3> <p>An integer <var>N</var> not less than <var>2</var> is called a prime number if it cannot be divided evenly by any integers except <var>1</var> and <var>N</var>, and called a composite number otherwise.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3> <ul> <li><var>N</var> is an integer between <var>5</var> and <var>55</var> (inclusive).</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3> <p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3> <p>Print <var>N</var> numbers <var>a_1, a_2, a_3, ..., a_N</var> in a line, with spaces in between.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 5 7 11 31 </pre> <p>Let us see if this output actually satisfies the conditions.<br/> First, <var>3</var>, <var>5</var>, <var>7</var>, <var>11</var> and <var>31</var> are all different, and all of them are prime numbers.<br/> The only way to choose five among them is to choose all of them, whose sum is <var>a_1+a_2+a_3+a_4+a_5=57</var>, which is a composite number.<br/> There are also other possible outputs, such as <code>2 3 5 7 13</code>, <code>11 13 17 19 31</code> and <code>7 11 5 31 3</code>. </p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 3 5 7 11 13 </pre> <ul> <li><var>2</var>, <var>3</var>, <var>5</var>, <var>7</var>, <var>11</var>, <var>13</var> are all different prime numbers.</li> <li><var>2+3+5+7+11=28</var> is a composite number.</li> <li><var>2+3+5+7+13=30</var> is a composite number.</li> <li><var>2+3+5+11+13=34</var> is a composite number.</li> <li><var>2+3+7+11+13=36</var> is a composite number.</li> <li><var>2+5+7+11+13=38</var> is a composite number.</li> <li><var>3+5+7+11+13=39</var> is a composite number.</li> </ul> <p>Thus, the sequence <code>2 3 5 7 11 13</code> satisfies the conditions. </p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>2 5 7 13 19 37 67 79 </pre></section> </div> </span>
[ [ "5\n", "5\n" ] ]
p03363
AtCoder Grand Contest 023 - Zero-Sum Ranges
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have an integer sequence <var>A</var>, whose length is <var>N</var>.</p> <p>Find the number of the non-empty <strong>contiguous</strong> subsequences of <var>A</var> whose sums are <var>0</var>. Note that we are counting <strong>the ways to take out subsequences</strong>. That is, even if the contents of some two subsequences are the same, they are counted individually if they are taken from different positions.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 2 \times 10^5</var></li> <li><var>-10^9 \leq A_i \leq 10^9</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Find the number of the non-empty contiguous subsequences of <var>A</var> whose sum is <var>0</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 1 3 -4 2 2 -2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>There are three contiguous subsequences whose sums are <var>0</var>: <var>(1,3,-4)</var>, <var>(-4,2,2)</var> and <var>(2,-2)</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 1 -1 1 -1 1 -1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>12 </pre> <p>In this case, some subsequences that have the same contents but are taken from different positions are counted individually. For example, three occurrences of <var>(1, -1)</var> are counted.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 1 -2 3 -4 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> <p>There are no contiguous subsequences whose sums are <var>0</var>.</p></section> </div> </span>
[ [ "6\n1 3 -4 2 2 -2\n", "6\n1 3 -4 2 2 -2\n" ] ]
p03364
AtCoder Grand Contest 023 - Find Symmetries
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has two boards, each divided into a grid with <var>N</var> rows and <var>N</var> columns. For both of these boards, the square at the <var>i</var>-th row from the top and the <var>j</var>-th column from the left is called Square <var>(i,j)</var>.</p> <p>There is a lowercase English letter written in each square on the first board. The letter written in Square <var>(i,j)</var> is <var>S_{i,j}</var>. On the second board, nothing is written yet.</p> <p>Snuke will write letters on the second board, as follows:</p> <ul> <li>First, choose two integers <var>A</var> and <var>B</var> ( <var>0 \leq A, B &lt; N</var> ).</li> <li>Write one letter in each square on the second board. Specifically, write the letter written in Square <var>( i+A, j+B )</var> on the first board into Square <var>(i,j)</var> on the second board. Here, the <var>k</var>-th row is also represented as the <var>(N+k)</var>-th row, and the <var>k</var>-th column is also represented as the <var>(N+k)</var>-th column.</li> </ul> <p>After this operation, the second board is called a <em>good</em> board when, for every <var>i</var> and <var>j</var> ( <var>1 \leq i, j \leq N</var> ), the letter in Square <var>(i,j)</var> and the letter in Square <var>(j,i)</var> are equal.</p> <p>Find the number of the ways to choose integers <var>A</var> and <var>B</var> ( <var>0 \leq A, B &lt; N</var> ) such that the second board is a good board.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 300</var></li> <li><var>S_{i,j}</var> ( <var>1 \leq i, j \leq N</var> ) is a lowercase English letter.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>S_{1,1}S_{1,2}..S_{1,N}</var> <var>S_{2,1}S_{2,2}..S_{2,N}</var> <var>:</var> <var>S_{N,1}S_{N,2}..S_{N,N}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the ways to choose integers <var>A</var> and <var>B</var> ( <var>0 \leq A, B &lt; N</var> ) such that the second board is a good board.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 ab ca </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>For each pair of <var>A</var> and <var>B</var>, the second board will look as shown below:</p> <p><img alt="" src="https://img.atcoder.jp/agc023/2414e26dc3abb6dd7bfa0c800bb4af0c.png"/></p> <p>The second board is a good board when <var>(A,B) = (0,1)</var> or <var>(A,B) = (1,0)</var>, thus the answer is <var>2</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 aaaa aaaa aaaa aaaa </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>16 </pre> <p>Every possible choice of <var>A</var> and <var>B</var> makes the second board good.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 abcde fghij klmno pqrst uvwxy </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> <p>No possible choice of <var>A</var> and <var>B</var> makes the second board good.</p></section> </div> </span>
[ [ "2\nab\nca\n", "2\nab\nca\n" ] ]
p03365
AtCoder Grand Contest 023 - Painting Machines
<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> squares lining up in a row, numbered <var>1</var> through <var>N</var> from left to right. Initially, all squares are white. We also have <var>N-1</var> painting machines, numbered <var>1</var> through <var>N-1</var>. When operated, Machine <var>i</var> paints Square <var>i</var> and <var>i+1</var> black.</p> <p>Snuke will operate these machines one by one. The order in which he operates them is represented by a permutation of <var>(1, 2, ..., N-1)</var>, <var>P</var>, which means that the <var>i</var>-th operated machine is Machine <var>P_i</var>.</p> <p>Here, the <em>score</em> of a permutation <var>P</var> is defined as the number of machines that are operated before all the squares are painted black for the first time, when the machines are operated in the order specified by <var>P</var>. Snuke has not decided what permutation <var>P</var> to use, but he is interested in the scores of possible permutations. Find the sum of the scores over all possible permutations for him. Since this can be extremely large, compute the sum modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 10^6</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the sum of the scores over all possible permutations, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>16 </pre> <p>There are six possible permutations as <var>P</var>. Among them, <var>P = (1, 3, 2)</var> and <var>P = (3, 1, 2)</var> have a score of <var>2</var>, and the others have a score of <var>3</var>. Thus, the answer is <var>2 \times 2 + 3 \times 4 = 16</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 </pre> <p>There is only one possible permutation: <var>P = (1)</var>, which has a score of <var>1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>84 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>341429644 </pre></section> </div> </span>
[ [ "4\n", "4\n" ] ]
p03366
AtCoder Grand Contest 023 - Go Home
<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> apartments along a number line, numbered <var>1</var> through <var>N</var>. Apartment <var>i</var> is located at coordinate <var>X_i</var>. Also, the office of AtCoder Inc. is located at coordinate <var>S</var>. Every employee of AtCoder lives in one of the <var>N</var> apartments. There are <var>P_i</var> employees who are living in Apartment <var>i</var>.</p> <p>All employees of AtCoder are now leaving the office all together. Since they are tired from work, they would like to get home by the company's bus. AtCoder owns only one bus, but it can accommodate all the employees. This bus will leave coordinate <var>S</var> with all the employees and move according to the following rule:</p> <ul> <li> <p>Everyone on the bus casts a vote on which direction the bus should proceed, positive or negative. (The bus is autonomous and has no driver.) Each person has one vote, and abstaining from voting is not allowed. Then, the bus moves a distance of <var>1</var> in the direction with the greater number of votes. If a tie occurs, the bus moves in the negative direction. If there is an apartment at the coordinate of the bus after the move, all the employees who live there get off.</p> </li> <li> <p>Repeat the operation above as long as there is one or more employees on the bus.</p> </li> </ul> <p>For a specific example, see Sample Input 1.</p> <p>The bus takes one seconds to travel a distance of <var>1</var>. The time required to vote and get off the bus is ignorable.</p> <p>Every employee will vote so that he himself/she herself can get off the bus at the earliest possible time. Strictly speaking, when a vote is taking place, each employee see which direction results in the earlier arrival at his/her apartment, assuming that all the employees follow the same strategy in the future. Based on this information, each employee makes the optimal choice, but if either direction results in the arrival at the same time, he/she casts a vote to the negative direction.</p> <p>Find the time the bus will take from departure to arrival at the last employees' apartment. It can be proved that, given the positions of the apartments, the numbers of employees living in each apartment and the initial position of the bus, the future movement of the bus is uniquely determined, and the process will end in a finite time.</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 S \leq 10^9</var></li> <li><var>1 \leq X_1 &lt; X_2 &lt; ... &lt; X_N \leq 10^9</var></li> <li><var>X_i \neq S</var> ( <var>1 \leq i \leq N</var> )</li> <li><var>1 \leq P_i \leq 10^9</var> ( <var>1 \leq i \leq N</var> )</li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>S</var> <var>X_1</var> <var>P_1</var> <var>X_2</var> <var>P_2</var> <var>:</var> <var>X_N</var> <var>P_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of seconds the bus will take from departure to arrival at the last employees' apartment.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 1 3 3 4 4 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>Assume that the bus moves in the negative direction first. Then, the coordinate of the bus changes from <var>2</var> to <var>1</var>, and the employees living in Apartment <var>1</var> get off. The movement of the bus after that is obvious: it just continues moving in the positive direction. Thus, if the bus moves in the negative direction first, the coordinate of the bus changes as <var>2 → 1 → 2 → 3 → 4</var> from departure. The time it takes to get home for the employees living in Apartment <var>1</var>, <var>2</var>, <var>3</var> are <var>1</var>, <var>3</var>, <var>4</var> seconds, respectively.</p> <p>Next, assume that the bus moves in the positive direction first. Then, the coordinate of the bus changes from <var>2</var> to <var>3</var>, and the employees living in Apartment <var>2</var> get off. Afterwards, the bus starts heading to Apartment <var>1</var>, because there are more employees living in Apartment <var>1</var> than in Apartment <var>3</var>. Then, after arriving at Apartment <var>1</var>, the bus heads to Apartment <var>3</var>. Thus, if the bus moves in the positive direction first, the coordinate of the bus changes as <var>2 → 3 → 2 → 1 → 2 → 3 → 4</var> from departure. The time it takes to get home for the employees living in Apartment <var>1</var>, <var>2</var>, <var>3</var> are <var>3</var>, <var>1</var>, <var>6</var> seconds, respectively.</p> <p>Therefore, in the beginning, the employees living in Apartment <var>1</var> or <var>3</var> will try to move the bus in the negative direction. On the other hand, the employees living in Apartment <var>2</var> will try to move the bus in the positive direction in the beginning. There are a total of <var>3 + 2 = 5</var> employees living in Apartment <var>1</var> and <var>3</var> combined, which is more than those living in Apartment <var>2</var>, which is <var>4</var>. Thus, the bus will move in the negative direction first, and the coordinate of the bus will change as <var>2 → 1 → 2 → 3 → 4</var> from departure.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 4 1 10 2 1000 3 100000 5 1000000 6 10000 7 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>21 </pre> <p>Since the numbers of employees living in different apartments are literally off by a digit, the bus consistently head to the apartment where the most number of employees on the bus lives.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>15 409904902 94198000 15017 117995501 7656764 275583856 313263626 284496300 356635175 324233841 607 360631781 148 472103717 5224 497641071 34695 522945827 816241 554305668 32 623788284 22832 667409501 124410641 876731548 12078 904557302 291749534 918215789 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>2397671583 </pre></section> </div> </span>
[ [ "3 2\n1 3\n3 4\n4 2\n", "3 2\n1 3\n3 4\n4 2\n" ] ]
p03367
AtCoder Grand Contest 023 - Inversions
<span class="lang-en"> <p>Score : <var>1700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has an integer sequence <var>A</var> whose length is <var>N</var>. He likes permutations of <var>(1, 2, ..., N)</var>, <var>P</var>, that satisfy the following condition:</p> <ul> <li><var>P_i \leq A_i</var> for all <var>i</var> ( <var>1 \leq i \leq N</var> ).</li> </ul> <p>Snuke is interested in the inversion numbers of such permutations. Find the sum of the inversion numbers over all permutations that satisfy the condition. Since this can be extremely large, compute the sum modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Notes</h3><p>The <em>inversion number</em> of a sequence <var>Z</var> whose length <var>N</var> is the number of pairs of integers <var>i</var> and <var>j</var> ( <var>1 \leq i &lt; j \leq N</var> ) such that <var>Z_i &gt; Z_j</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 2 \times 10^5</var></li> <li><var>1 \leq A_i \leq N</var> ( <var>1 \leq i \leq N</var> )</li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the sum of the inversion numbers over all permutations that satisfy the condition.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 3 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>There are four permutations that satisfy the condition: <var>(1,2,3)</var>, <var>(1,3,2)</var>, <var>(2,1,3)</var> and <var>(2,3,1)</var>. The inversion numbers of these permutations are <var>0</var>, <var>1</var>, <var>1</var> and <var>2</var>, respectively, for a total of <var>4</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 4 2 5 1 6 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>7 </pre> <p>Only one permutation <var>(4,2,5,1,6,3)</var> satisfies the condition. The inversion number of this permutation is <var>7</var>, so the answer is <var>7</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 4 4 4 4 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> <p>No permutation satisfies the condition.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>30 22 30 15 20 10 29 11 29 28 11 26 10 18 28 22 5 29 16 24 24 27 10 21 30 29 19 28 27 18 23 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>848414012 </pre></section> </div> </span>
[ [ "3\n2 3 3\n", "3\n2 3 3\n" ] ]
p03368
AtCoder Grand Contest 023 - 01 on Tree
<span class="lang-en"> <p>Score : <var>1700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has a rooted tree with <var>N</var> vertices, numbered <var>1</var> through <var>N</var>. Vertex <var>1</var> is the root of the tree, and the parent of Vertex <var>i</var> ( <var>2\leq i \leq N</var> ) is Vertex <var>P_i</var> ( <var>P_i &lt; i</var> ). There is a number, <var>0</var> or <var>1</var>, written on each vertex. The number written on Vertex <var>i</var> is <var>V_i</var>.</p> <p>Snuke would like to arrange the vertices of this tree in a horizontal row. Here, for every vertex, there should be no ancestor of that vertex to the right of that vertex.</p> <p>After arranging the vertices, let <var>X</var> be the sequence obtained by reading the numbers written on the vertices from left to right in the arrangement. Snuke would like to minimize the inversion number of <var>X</var>. Find the minimum possible inversion number of <var>X</var>.</p> </section> </div> <div class="part"> <section> <h3>Notes</h3><p>The <em>inversion number</em> of a sequence <var>Z</var> whose length <var>N</var> is the number of pairs of integers <var>i</var> and <var>j</var> ( <var>1 \leq i &lt; j \leq N</var> ) such that <var>Z_i &gt; Z_j</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 2 \times 10^5</var></li> <li><var>1 \leq P_i &lt; i</var> ( <var>2 \leq i \leq N</var> )</li> <li><var>0 \leq V_i \leq 1</var> ( <var>1 \leq i \leq N</var> )</li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>P_2</var> <var>P_3</var> <var>...</var> <var>P_N</var> <var>V_1</var> <var>V_2</var> <var>...</var> <var>V_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum possible inversion number of <var>X</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 1 1 2 3 3 0 1 1 0 0 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>When the vertices are arranged in the order <var>1, 3, 5, 6, 2, 4</var>, <var>X</var> will be <var>(0, 1, 0, 0, 1, 0)</var>, whose inversion number is <var>4</var>. It is impossible to have fewer inversions, so the answer is <var>4</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p><var>X = (0)</var>, whose inversion number is <var>0</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>15 1 2 3 2 5 6 2 2 9 10 1 12 13 12 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>31 </pre></section> </div> </span>
[ [ "6\n1 1 2 3 3\n0 1 1 0 0 0\n", "6\n1 1 2 3 3\n0 1 1 0 0 0\n" ] ]
p03369
AtCoder Beginner Contest 095 - Something on It
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>In "Takahashi-ya", a ramen restaurant, a bowl of ramen costs <var>700</var> yen (the currency of Japan), plus <var>100</var> yen for each kind of topping (boiled egg, sliced pork, green onions).</p> <p>A customer ordered a bowl of ramen and told which toppings to put on his ramen to a clerk. The clerk took a memo of the order as a string <var>S</var>. <var>S</var> is three characters long, and if the first character in <var>S</var> is <code>o</code>, it means the ramen should be topped with boiled egg; if that character is <code>x</code>, it means the ramen should not be topped with boiled egg. Similarly, the second and third characters in <var>S</var> mean the presence or absence of sliced pork and green onions on top of the ramen.</p> <p>Write a program that, when <var>S</var> is given, prints the price of the corresponding bowl of ramen.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>S</var> is a string of length <var>3</var>.</li> <li>Each character in <var>S</var> is <code>o</code> or <code>x</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>S</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the price of the bowl of ramen corresponding to <var>S</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>oxo </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>900 </pre> <p>The price of a ramen topped with two kinds of toppings, boiled egg and green onions, is <var>700 + 100 \times 2 = 900</var> yen.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>ooo </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1000 </pre> <p>The price of a ramen topped with all three kinds of toppings is <var>700 + 100 \times 3 = 1000</var> yen.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>xxx </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>700 </pre> <p>The price of a ramen without any toppings is <var>700</var> yen.</p></section> </div> </span>
[ [ "oxo\n", "oxo\n" ] ]
p03370
AtCoder Beginner Contest 095 - Bitter Alchemy
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Akaki, a patissier, can make <var>N</var> kinds of doughnut using only a certain powder called "Okashi no Moto" (literally "material of pastry", simply called Moto below) as ingredient. These doughnuts are called Doughnut <var>1</var>, Doughnut <var>2</var>, <var>...,</var> Doughnut <var>N</var>. In order to make one Doughnut <var>i</var> <var>(1 ≤ i ≤ N)</var>, she needs to consume <var>m_i</var> grams of Moto. She cannot make a non-integer number of doughnuts, such as <var>0.5</var> doughnuts.</p> <p>Now, she has <var>X</var> grams of Moto. She decides to make as many doughnuts as possible for a party tonight. However, since the tastes of the guests differ, she will obey the following condition:</p> <ul> <li>For each of the <var>N</var> kinds of doughnuts, make at least one doughnut of that kind.</li> </ul> <p>At most how many doughnuts can be made here? She does not necessarily need to consume all of her Moto. Also, under the constraints of this problem, it is always possible to obey the condition.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N ≤ 100</var></li> <li><var>1 ≤ m_i ≤ 1000</var></li> <li><var>m_1 + m_2 + ... + m_N ≤ X ≤ 10^5</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>X</var> <var>m_1</var> <var>m_2</var> <var>:</var> <var>m_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum number of doughnuts that can be made under the condition.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1000 120 100 140 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>9 </pre> <p>She has <var>1000</var> grams of Moto and can make three kinds of doughnuts. If she makes one doughnut for each of the three kinds, she consumes <var>120 + 100 + 140 = 360</var> grams of Moto. From the <var>640</var> grams of Moto that remains here, she can make additional six Doughnuts <var>2</var>. This is how she can made a total of nine doughnuts, which is the maximum.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 360 90 90 90 90 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>4 </pre> <p>Making one doughnut for each of the four kinds consumes all of her Moto.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 3000 150 130 150 130 110 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>26 </pre></section> </div> </span>
[ [ "3 1000\n120\n100\n140\n", "3 1000\n120\n100\n140\n" ] ]
p03371
AtCoder Beginner Contest 095 - Half and Half
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>"Pizza At", a fast food chain, offers three kinds of pizza: "A-pizza", "B-pizza" and "AB-pizza". A-pizza and B-pizza are completely different pizzas, and AB-pizza is one half of A-pizza and one half of B-pizza combined together. The prices of one A-pizza, B-pizza and AB-pizza are <var>A</var> yen, <var>B</var> yen and <var>C</var> yen (yen is the currency of Japan), respectively.</p> <p>Nakahashi needs to prepare <var>X</var> A-pizzas and <var>Y</var> B-pizzas for a party tonight. He can only obtain these pizzas by directly buying A-pizzas and B-pizzas, or buying two AB-pizzas and then rearrange them into one A-pizza and one B-pizza. At least how much money does he need for this? It is fine to have more pizzas than necessary by rearranging pizzas.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ A, B, C ≤ 5000</var></li> <li><var>1 ≤ X, Y ≤ 10^5</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> <var>C</var> <var>X</var> <var>Y</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum amount of money required to prepare <var>X</var> A-pizzas and <var>Y</var> B-pizzas.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>1500 2000 1600 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>7900 </pre> <p>It is optimal to buy four AB-pizzas and rearrange them into two A-pizzas and two B-pizzas, then buy additional one A-pizza.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1500 2000 1900 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>8500 </pre> <p>It is optimal to directly buy three A-pizzas and two B-pizzas.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1500 2000 500 90000 100000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>100000000 </pre> <p>It is optimal to buy <var>200000</var> AB-pizzas and rearrange them into <var>100000</var> A-pizzas and <var>100000</var> B-pizzas. We will have <var>10000</var> more A-pizzas than necessary, but that is fine.</p></section> </div> </span>
[ [ "1500 2000 1600 3 2\n", "1500 2000 1600 3 2\n" ] ]
p03372
AtCoder Beginner Contest 095 - Static Sushi
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>"Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is <var>C</var> meters. Customers cannot go inside the counter.</p> <p>Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are <var>N</var> pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the <var>i</var>-th sushi is placed, is <var>x_i</var> meters. Also, the <var>i</var>-th sushi has a nutritive value of <var>v_i</var> kilocalories.</p> <p>Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes <var>1</var> kilocalories per meter.</p> <p>Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 10^5</var></li> <li><var>2 ≤ C ≤ 10^{14}</var></li> <li><var>1 ≤ x_1 &lt; x_2 &lt; ... &lt; x_N &lt; C</var></li> <li><var>1 ≤ v_i ≤ 10^9</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <div class="part"> <section> <h3>Subscores</h3><ul> <li><var>300</var> points will be awarded for passing the test set satisfying <var>N ≤ 100</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>C</var> <var>x_1</var> <var>v_1</var> <var>x_2</var> <var>v_2</var> <var>:</var> <var>x_N</var> <var>v_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If Nakahashi can take in at most <var>c</var> kilocalories on balance before he leaves the restaurant, print <var>c</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 20 2 80 9 120 16 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>191 </pre> <p>There are three sushi on the counter with a circumference of <var>20</var> meters. If he walks two meters clockwise from the initial place, he can eat a sushi of <var>80</var> kilocalories. If he walks seven more meters clockwise, he can eat a sushi of <var>120</var> kilocalories. If he leaves now, the total nutrition taken in is <var>200</var> kilocalories, and the total energy consumed is <var>9</var> kilocalories, thus he can take in <var>191</var> kilocalories on balance, which is the largest possible value.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 20 2 80 9 1 16 120 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>192 </pre> <p>The second and third sushi have been swapped. Again, if he walks two meters clockwise from the initial place, he can eat a sushi of <var>80</var> kilocalories. If he walks six more meters counterclockwise this time, he can eat a sushi of <var>120</var> kilocalories. If he leaves now, the total nutrition taken in is <var>200</var> kilocalories, and the total energy consumed is <var>8</var> kilocalories, thus he can take in <var>192</var> kilocalories on balance, which is the largest possible value.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1 100000000000000 50000000000000 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> <p>Even though the only sushi is so far that it does not fit into a <var>32</var>-bit integer, its nutritive value is low, thus he should immediately leave without doing anything.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>15 10000000000 400000000 1000000000 800000000 1000000000 1900000000 1000000000 2400000000 1000000000 2900000000 1000000000 3300000000 1000000000 3700000000 1000000000 3800000000 1000000000 4000000000 1000000000 4100000000 1000000000 5200000000 1000000000 6600000000 1000000000 8000000000 1000000000 9300000000 1000000000 9700000000 1000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>6500000000 </pre> <p>All these sample inputs above are included in the test set for the partial score.</p></section> </div> </span>
[ [ "3 20\n2 80\n9 120\n16 1\n", "3 20\n2 80\n9 120\n16 1\n" ] ]
p03373
AtCoder Regular Contest 096 - Half and Half
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>"Pizza At", a fast food chain, offers three kinds of pizza: "A-pizza", "B-pizza" and "AB-pizza". A-pizza and B-pizza are completely different pizzas, and AB-pizza is one half of A-pizza and one half of B-pizza combined together. The prices of one A-pizza, B-pizza and AB-pizza are <var>A</var> yen, <var>B</var> yen and <var>C</var> yen (yen is the currency of Japan), respectively.</p> <p>Nakahashi needs to prepare <var>X</var> A-pizzas and <var>Y</var> B-pizzas for a party tonight. He can only obtain these pizzas by directly buying A-pizzas and B-pizzas, or buying two AB-pizzas and then rearrange them into one A-pizza and one B-pizza. At least how much money does he need for this? It is fine to have more pizzas than necessary by rearranging pizzas.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ A, B, C ≤ 5000</var></li> <li><var>1 ≤ X, Y ≤ 10^5</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> <var>C</var> <var>X</var> <var>Y</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum amount of money required to prepare <var>X</var> A-pizzas and <var>Y</var> B-pizzas.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>1500 2000 1600 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>7900 </pre> <p>It is optimal to buy four AB-pizzas and rearrange them into two A-pizzas and two B-pizzas, then buy additional one A-pizza.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1500 2000 1900 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>8500 </pre> <p>It is optimal to directly buy three A-pizzas and two B-pizzas.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1500 2000 500 90000 100000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>100000000 </pre> <p>It is optimal to buy <var>200000</var> AB-pizzas and rearrange them into <var>100000</var> A-pizzas and <var>100000</var> B-pizzas. We will have <var>10000</var> more A-pizzas than necessary, but that is fine.</p></section> </div> </span>
[ [ "1500 2000 1600 3 2\n", "1500 2000 1600 3 2\n" ] ]
p03374
AtCoder Regular Contest 096 - Static Sushi
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>"Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is <var>C</var> meters. Customers cannot go inside the counter.</p> <p>Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are <var>N</var> pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the <var>i</var>-th sushi is placed, is <var>x_i</var> meters. Also, the <var>i</var>-th sushi has a nutritive value of <var>v_i</var> kilocalories.</p> <p>Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes <var>1</var> kilocalories per meter.</p> <p>Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 10^5</var></li> <li><var>2 ≤ C ≤ 10^{14}</var></li> <li><var>1 ≤ x_1 &lt; x_2 &lt; ... &lt; x_N &lt; C</var></li> <li><var>1 ≤ v_i ≤ 10^9</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <div class="part"> <section> <h3>Subscores</h3><ul> <li><var>300</var> points will be awarded for passing the test set satisfying <var>N ≤ 100</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>C</var> <var>x_1</var> <var>v_1</var> <var>x_2</var> <var>v_2</var> <var>:</var> <var>x_N</var> <var>v_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If Nakahashi can take in at most <var>c</var> kilocalories on balance before he leaves the restaurant, print <var>c</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 20 2 80 9 120 16 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>191 </pre> <p>There are three sushi on the counter with a circumference of <var>20</var> meters. If he walks two meters clockwise from the initial place, he can eat a sushi of <var>80</var> kilocalories. If he walks seven more meters clockwise, he can eat a sushi of <var>120</var> kilocalories. If he leaves now, the total nutrition taken in is <var>200</var> kilocalories, and the total energy consumed is <var>9</var> kilocalories, thus he can take in <var>191</var> kilocalories on balance, which is the largest possible value.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 20 2 80 9 1 16 120 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>192 </pre> <p>The second and third sushi have been swapped. Again, if he walks two meters clockwise from the initial place, he can eat a sushi of <var>80</var> kilocalories. If he walks six more meters counterclockwise this time, he can eat a sushi of <var>120</var> kilocalories. If he leaves now, the total nutrition taken in is <var>200</var> kilocalories, and the total energy consumed is <var>8</var> kilocalories, thus he can take in <var>192</var> kilocalories on balance, which is the largest possible value.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1 100000000000000 50000000000000 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> <p>Even though the only sushi is so far that it does not fit into a <var>32</var>-bit integer, its nutritive value is low, thus he should immediately leave without doing anything.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>15 10000000000 400000000 1000000000 800000000 1000000000 1900000000 1000000000 2400000000 1000000000 2900000000 1000000000 3300000000 1000000000 3700000000 1000000000 3800000000 1000000000 4000000000 1000000000 4100000000 1000000000 5200000000 1000000000 6600000000 1000000000 8000000000 1000000000 9300000000 1000000000 9700000000 1000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>6500000000 </pre> <p>All these sample inputs above are included in the test set for the partial score.</p></section> </div> </span>
[ [ "3 20\n2 80\n9 120\n16 1\n", "3 20\n2 80\n9 120\n16 1\n" ] ]
p03375
AtCoder Regular Contest 096 - Everything on It
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>In "Takahashi-ya", a ramen restaurant, basically they have one menu: "ramen", but <var>N</var> kinds of toppings are also offered. When a customer orders a bowl of ramen, for each kind of topping, he/she can choose whether to put it on top of his/her ramen or not. There is no limit on the number of toppings, and it is allowed to have all kinds of toppings or no topping at all. That is, considering the combination of the toppings, <var>2^N</var> types of ramen can be ordered.</p> <p>Akaki entered Takahashi-ya. She is thinking of ordering some bowls of ramen that satisfy both of the following two conditions:</p> <ul> <li>Do not order multiple bowls of ramen with the exactly same set of toppings.</li> <li>Each of the <var>N</var> kinds of toppings is on two or more bowls of ramen ordered.</li> </ul> <p>You are given <var>N</var> and a prime number <var>M</var>. Find the number of the sets of bowls of ramen that satisfy these conditions, disregarding order, modulo <var>M</var>. Since she is in extreme hunger, ordering any number of bowls of ramen is fine.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 3000</var></li> <li><var>10^8 \leq M \leq 10^9 + 9</var></li> <li><var>N</var> is an integer.</li> <li><var>M</var> is a prime number.</li> </ul> </section> </div> <div class="part"> <section> <h3>Subscores</h3><ul> <li><var>600</var> points will be awarded for passing the test set satisfying <var>N ≤ 50</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the sets of bowls of ramen that satisfy the conditions, disregarding order, modulo <var>M</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 1000000007 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>Let the two kinds of toppings be A and B. Four types of ramen can be ordered: "no toppings", "with A", "with B" and "with A, B". There are two sets of ramen that satisfy the conditions:</p> <ul> <li>The following three ramen: "with A", "with B", "with A, B".</li> <li>Four ramen, one for each type.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 1000000009 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>118 </pre> <p>Let the three kinds of toppings be A, B and C. In addition to the four types of ramen above, four more types of ramen can be ordered, where C is added to the above four. There are <var>118</var> sets of ramen that satisfy the conditions, and here are some of them:</p> <ul> <li>The following three ramen: "with A, B", "with A, C", "with B, C".</li> <li>The following five ramen: "no toppings", "with A", "with A, B", "with B, C", "with A, B, C".</li> <li>Eight ramen, one for each type.</li> </ul> <p>Note that the set of the following three does not satisfy the condition: "'with A', 'with B', 'with A, B'", because C is not on any of them.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>50 111111113 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1456748 </pre> <p>Remember to print the number of the sets modulo <var>M</var>. Note that these three sample inputs above are included in the test set for the partial score.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>3000 123456791 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>16369789 </pre> <p>This sample input is not included in the test set for the partial score.</p></section> </div> </span>
[ [ "2 1000000007\n", "2 1000000007\n" ] ]
p03376
AtCoder Regular Contest 096 - Sweet Alchemy
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Akaki, a patissier, can make <var>N</var> kinds of doughnut using only a certain powder called "Okashi no Moto" (literally "material of pastry", simply called Moto below) as ingredient. These doughnuts are called Doughnut <var>1</var>, Doughnut <var>2</var>, <var>...,</var> Doughnut <var>N</var>. In order to make one Doughnut <var>i</var> <var>(1 ≤ i ≤ N)</var>, she needs to consume <var>m_i</var> grams of Moto. She cannot make a non-integer number of doughnuts, such as <var>0.5</var> doughnuts.</p> <p>The recipes of these doughnuts are developed by repeated modifications from the recipe of Doughnut <var>1</var>. Specifically, the recipe of Doughnut <var>i</var> <var>(2 ≤ i ≤ N)</var> is a direct modification of the recipe of Doughnut <var>p_i</var> <var>(1 ≤ p_i &lt; i)</var>.</p> <p>Now, she has <var>X</var> grams of Moto. She decides to make as many doughnuts as possible for a party tonight. However, since the tastes of the guests differ, she will obey the following condition:</p> <ul> <li>Let <var>c_i</var> be the number of Doughnut <var>i</var> <var>(1 ≤ i ≤ N)</var> that she makes. For each integer <var>i</var> such that <var>2 ≤ i ≤ N</var>, <var>c_{p_i} ≤ c_i ≤ c_{p_i} + D</var> must hold. Here, <var>D</var> is a predetermined value.</li> </ul> <p>At most how many doughnuts can be made here? She does not necessarily need to consume all of her Moto.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N ≤ 50</var></li> <li><var>1 ≤ X ≤ 10^9</var></li> <li><var>0 ≤ D ≤ 10^9</var></li> <li><var>1 ≤ m_i ≤ 10^9</var> <var>(1 ≤ i ≤ N)</var></li> <li><var>1 ≤ p_i &lt; i</var> <var>(2 ≤ i ≤ N)</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>X</var> <var>D</var> <var>m_1</var> <var>m_2</var> <var>p_2</var> <var>:</var> <var>m_N</var> <var>p_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum number of doughnuts that can be made under the condition.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 100 1 15 10 1 20 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>7 </pre> <p>She has <var>100</var> grams of Moto, can make three kinds of doughnuts, and the conditions that must hold are <var>c_1 ≤ c_2 ≤ c_1 + 1</var> and <var>c_1 ≤ c_3 ≤ c_1 + 1</var>. It is optimal to make two Doughnuts <var>1</var>, three Doughnuts <var>2</var> and two Doughnuts <var>3</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 100 10 15 10 1 20 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>10 </pre> <p>The amount of Moto and the recipes of the doughnuts are not changed from Sample Input 1, but the last conditions are relaxed. In this case, it is optimal to make just ten Doughnuts <var>2</var>. As seen here, she does not necessarily need to make all kinds of doughnuts.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 1000000000 1000000 123 159 1 111 1 135 3 147 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>7496296 </pre></section> </div> </span>
[ [ "3 100 1\n15\n10 1\n20 1\n", "3 100 1\n15\n10 1\n20 1\n" ] ]
p03377
AtCoder Beginner Contest 094 - Cats and Dogs
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are a total of <var>A + B</var> cats and dogs. Among them, <var>A</var> are known to be cats, but the remaining <var>B</var> are not known to be either cats or dogs.</p> <p>Determine if it is possible that there are exactly <var>X</var> cats among these <var>A + B</var> animals.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq A \leq 100</var></li> <li><var>1 \leq B \leq 100</var></li> <li><var>1 \leq X \leq 200</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> <var>X</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If it is possible that there are exactly <var>X</var> cats, print <code>YES</code>; if it is impossible, print <code>NO</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 5 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>YES </pre> <p>If there are one cat and four dogs among the <var>B = 5</var> animals, there are <var>X = 4</var> cats in total.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 2 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>NO </pre> <p>Even if all of the <var>B = 2</var> animals are cats, there are less than <var>X = 6</var> cats in total.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>NO </pre> <p>Even if all of the <var>B = 3</var> animals are dogs, there are more than <var>X = 2</var> cats in total.</p></section> </div> </span>
[ [ "3 5 4\n", "3 5 4\n" ] ]
p03378
AtCoder Beginner Contest 094 - Toll Gates
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N + 1</var> squares arranged in a row, numbered <var>0, 1, ..., N</var> from left to right.</p> <p>Initially, you are in Square <var>X</var>. You can freely travel between adjacent squares. Your goal is to reach Square <var>0</var> or Square <var>N</var>. However, for each <var>i = 1, 2, ..., M</var>, there is a toll gate in Square <var>A_i</var>, and traveling to Square <var>A_i</var> incurs a cost of <var>1</var>. It is guaranteed that there is no toll gate in Square <var>0</var>, Square <var>X</var> and Square <var>N</var>.</p> <p>Find the minimum cost incurred before reaching the goal.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 100</var></li> <li><var>1 \leq M \leq 100</var></li> <li><var>1 \leq X \leq N - 1</var></li> <li><var>1 \leq A_1 &lt; A_2 &lt; ... &lt; A_M \leq N</var></li> <li><var>A_i \neq X</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>X</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum cost incurred before reaching the goal.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 3 3 1 2 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 </pre> <p>The optimal solution is as follows:</p> <ul> <li>First, travel from Square <var>3</var> to Square <var>4</var>. Here, there is a toll gate in Square <var>4</var>, so the cost of <var>1</var> is incurred.</li> <li>Then, travel from Square <var>4</var> to Square <var>5</var>. This time, no cost is incurred.</li> <li>Now, we are in Square <var>5</var> and we have reached the goal.</li> </ul> <p>In this case, the total cost incurred is <var>1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 3 2 4 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>We may be able to reach the goal at no cost.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>10 7 5 1 2 3 4 6 8 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>3 </pre></section> </div> </span>
[ [ "5 3 3\n1 2 4\n", "5 3 3\n1 2 4\n" ] ]
p03379
AtCoder Beginner Contest 094 - Many Medians
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>When <var>l</var> is an odd number, the median of <var>l</var> numbers <var>a_1, a_2, ..., a_l</var> is the <var>(\frac{l+1}{2})</var>-th largest value among <var>a_1, a_2, ..., a_l</var>.</p> <p>You are given <var>N</var> numbers <var>X_1, X_2, ..., X_N</var>, where <var>N</var> is an even number. For each <var>i = 1, 2, ..., N</var>, let the median of <var>X_1, X_2, ..., X_N</var> excluding <var>X_i</var>, that is, the median of <var>X_1, X_2, ..., X_{i-1}, X_{i+1}, ..., X_N</var> be <var>B_i</var>.</p> <p>Find <var>B_i</var> for each <var>i = 1, 2, ..., N</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 200000</var></li> <li><var>N</var> is even.</li> <li><var>1 \leq X_i \leq 10^9</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>X_1</var> <var>X_2</var> ... <var>X_N</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 <var>B_i</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 2 4 4 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 3 3 4 </pre> <ul> <li>Since the median of <var>X_2, X_3, X_4</var> is <var>4</var>, <var>B_1 = 4</var>.</li> <li>Since the median of <var>X_1, X_3, X_4</var> is <var>3</var>, <var>B_2 = 3</var>.</li> <li>Since the median of <var>X_1, X_2, X_4</var> is <var>3</var>, <var>B_3 = 3</var>.</li> <li>Since the median of <var>X_1, X_2, X_3</var> is <var>4</var>, <var>B_4 = 4</var>.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 5 5 4 4 3 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>4 4 4 4 4 4 </pre></section> </div> </span>
[ [ "4\n2 4 4 3\n", "4\n2 4 4 3\n" ] ]
p03380
AtCoder Beginner Contest 094 - Binomial Coefficients
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Let <var>{\rm comb}(n,r)</var> be the number of ways to choose <var>r</var> objects from among <var>n</var> objects, disregarding order. From <var>n</var> non-negative integers <var>a_1, a_2, ..., a_n</var>, select two numbers <var>a_i &gt; a_j</var> so that <var>{\rm comb}(a_i,a_j)</var> is maximized. If there are multiple pairs that maximize the value, any of them is accepted.</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 a_i \leq 10^9</var></li> <li><var>a_1,a_2,...,a_n</var> are pairwise distinct.</li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>n</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_n</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>a_i</var> and <var>a_j</var> that you selected, with a space in between.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 6 9 4 2 11 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>11 6 </pre> <p><var>\rm{comb}(a_i,a_j)</var> for each possible selection is as follows:</p> <ul> <li><var>\rm{comb}(4,2)=6</var> </li> <li><var>\rm{comb}(6,2)=15</var> </li> <li><var>\rm{comb}(6,4)=15</var> </li> <li><var>\rm{comb}(9,2)=36</var> </li> <li><var>\rm{comb}(9,4)=126</var> </li> <li><var>\rm{comb}(9,6)=84</var> </li> <li><var>\rm{comb}(11,2)=55</var> </li> <li><var>\rm{comb}(11,4)=330</var> </li> <li><var>\rm{comb}(11,6)=462</var> </li> <li><var>\rm{comb}(11,9)=55</var></li> </ul> <p>Thus, we should print <var>11</var> and <var>6</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 100 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>100 0 </pre></section> </div> </span>
[ [ "5\n6 9 4 2 11\n", "5\n6 9 4 2 11\n" ] ]
p03381
AtCoder Regular Contest 095 - Many Medians
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>When <var>l</var> is an odd number, the median of <var>l</var> numbers <var>a_1, a_2, ..., a_l</var> is the <var>(\frac{l+1}{2})</var>-th largest value among <var>a_1, a_2, ..., a_l</var>.</p> <p>You are given <var>N</var> numbers <var>X_1, X_2, ..., X_N</var>, where <var>N</var> is an even number. For each <var>i = 1, 2, ..., N</var>, let the median of <var>X_1, X_2, ..., X_N</var> excluding <var>X_i</var>, that is, the median of <var>X_1, X_2, ..., X_{i-1}, X_{i+1}, ..., X_N</var> be <var>B_i</var>.</p> <p>Find <var>B_i</var> for each <var>i = 1, 2, ..., N</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 200000</var></li> <li><var>N</var> is even.</li> <li><var>1 \leq X_i \leq 10^9</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>X_1</var> <var>X_2</var> ... <var>X_N</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 <var>B_i</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 2 4 4 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 3 3 4 </pre> <ul> <li>Since the median of <var>X_2, X_3, X_4</var> is <var>4</var>, <var>B_1 = 4</var>.</li> <li>Since the median of <var>X_1, X_3, X_4</var> is <var>3</var>, <var>B_2 = 3</var>.</li> <li>Since the median of <var>X_1, X_2, X_4</var> is <var>3</var>, <var>B_3 = 3</var>.</li> <li>Since the median of <var>X_1, X_2, X_3</var> is <var>4</var>, <var>B_4 = 4</var>.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 5 5 4 4 3 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>4 4 4 4 4 4 </pre></section> </div> </span>
[ [ "4\n2 4 4 3\n", "4\n2 4 4 3\n" ] ]
p03382
AtCoder Regular Contest 095 - Binomial Coefficients
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Let <var>{\rm comb}(n,r)</var> be the number of ways to choose <var>r</var> objects from among <var>n</var> objects, disregarding order. From <var>n</var> non-negative integers <var>a_1, a_2, ..., a_n</var>, select two numbers <var>a_i &gt; a_j</var> so that <var>{\rm comb}(a_i,a_j)</var> is maximized. If there are multiple pairs that maximize the value, any of them is accepted.</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 a_i \leq 10^9</var></li> <li><var>a_1,a_2,...,a_n</var> are pairwise distinct.</li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>n</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_n</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>a_i</var> and <var>a_j</var> that you selected, with a space in between.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 6 9 4 2 11 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>11 6 </pre> <p><var>\rm{comb}(a_i,a_j)</var> for each possible selection is as follows:</p> <ul> <li><var>\rm{comb}(4,2)=6</var> </li> <li><var>\rm{comb}(6,2)=15</var> </li> <li><var>\rm{comb}(6,4)=15</var> </li> <li><var>\rm{comb}(9,2)=36</var> </li> <li><var>\rm{comb}(9,4)=126</var> </li> <li><var>\rm{comb}(9,6)=84</var> </li> <li><var>\rm{comb}(11,2)=55</var> </li> <li><var>\rm{comb}(11,4)=330</var> </li> <li><var>\rm{comb}(11,6)=462</var> </li> <li><var>\rm{comb}(11,9)=55</var></li> </ul> <p>Thus, we should print <var>11</var> and <var>6</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 100 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>100 0 </pre></section> </div> </span>
[ [ "5\n6 9 4 2 11\n", "5\n6 9 4 2 11\n" ] ]
p03383
AtCoder Regular Contest 095 - Symmetric Grid
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is an <var>H \times W</var> grid (<var>H</var> vertical, <var>W</var> horizontal), where each square contains a lowercase English letter. Specifically, the letter in the square at the <var>i</var>-th row and <var>j</var>-th column is equal to the <var>j</var>-th character in the string <var>S_i</var>.</p> <p>Snuke can apply the following operation to this grid any number of times:</p> <ul> <li>Choose two different rows and swap them. Or, choose two different columns and swap them.</li> </ul> <p>Snuke wants this grid to be <em>symmetric</em>. That is, for any <var>1 \leq i \leq H</var> and <var>1 \leq j \leq W</var>, the letter in the square at the <var>i</var>-th row and <var>j</var>-th column and the letter in the square at the <var>(H + 1 - i)</var>-th row and <var>(W + 1 - j)</var>-th column should be equal.</p> <p>Determine if Snuke can achieve this objective.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq H \leq 12</var></li> <li><var>1 \leq W \leq 12</var></li> <li><var>|S_i| = W</var></li> <li><var>S_i</var> consists of lowercase English letters.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>S_1</var> <var>S_2</var> <var>:</var> <var>S_H</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If Snuke can make the grid symmetric, print <code>YES</code>; if he cannot, print <code>NO</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 arc rac </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>YES </pre> <p>If the second and third columns from the left are swapped, the grid becomes symmetric, as shown in the image below:</p> <p><img alt="" src="https://img.atcoder.jp/arc095/2b61caf45e7c5c7311e3944f3418b0bb.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 7 atcoder regular contest </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>12 12 bimonigaloaf faurwlkbleht dexwimqxzxbb lxdgyoifcxid ydxiliocfdgx nfoabgilamoi ibxbdqmzxxwe pqirylfrcrnf wtehfkllbura yfrnpflcrirq wvcclwgiubrk lkbrwgwuiccv </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>YES </pre></section> </div> </span>
[ [ "2 3\narc\nrac\n", "2 3\narc\nrac\n" ] ]
p03384
AtCoder Regular Contest 095 - Permutation Tree
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi has an ability to generate a tree using a permutation <var>(p_1,p_2,...,p_n)</var> of <var>(1,2,...,n)</var>, in the following process:</p> <p>First, prepare Vertex <var>1</var>, Vertex <var>2</var>, ..., Vertex <var>N</var>. For each <var>i=1,2,...,n</var>, perform the following operation:</p> <ul> <li>If <var>p_i = 1</var>, do nothing.</li> <li>If <var>p_i \neq 1</var>, let <var>j'</var> be the largest <var>j</var> such that <var>p_j &lt; p_i</var>. Span an edge between Vertex <var>i</var> and Vertex <var>j'</var>.</li> </ul> <p>Takahashi is trying to make his favorite tree with this ability. His favorite tree has <var>n</var> vertices from Vertex <var>1</var> through Vertex <var>n</var>, and its <var>i</var>-th edge connects Vertex <var>v_i</var> and <var>w_i</var>. Determine if he can make a tree isomorphic to his favorite tree by using a proper permutation. If he can do so, find the lexicographically smallest such permutation.</p> </section> </div> <div class="part"> <section> <h3>Notes</h3><p>For the definition of isomorphism of trees, see <a href="https://en.wikipedia.org/wiki/Graph_isomorphism">wikipedia</a>. Intuitively, two trees are isomorphic when they are the "same" if we disregard the indices of their vertices.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq n \leq 10^5</var></li> <li><var>1 \leq v_i, w_i \leq n</var></li> <li>The given graph is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>n</var> <var>v_1</var> <var>w_1</var> <var>v_2</var> <var>w_2</var> <var>:</var> <var>v_{n-1}</var> <var>w_{n-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there is no permutation that can generate a tree isomorphic to Takahashi's favorite tree, print <code>-1</code>. If it exists, print the lexicographically smallest such permutation, with spaces in between.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 1 2 1 3 1 4 1 5 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 4 5 3 6 </pre> <p>If the permutation <var>(1, 2, 4, 5, 3, 6)</var> is used to generate a tree, it looks as follows:</p> <p><img alt="" src="https://img.atcoder.jp/arc095/db000b879402aed649a1516620eb1e21.png"/></p> <p>This is isomorphic to the given graph.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 1 2 2 3 3 4 1 5 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 2 3 4 5 6 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>15 1 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 5 10 5 11 6 12 6 13 7 14 7 15 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>-1 </pre></section> </div> </span>
[ [ "6\n1 2\n1 3\n1 4\n1 5\n5 6\n", "6\n1 2\n1 3\n1 4\n1 5\n5 6\n" ] ]
p03385
AtCoder Beginner Contest 093 - abc of ABC
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a string <var>S</var> of length <var>3</var> consisting of <code>a</code>, <code>b</code> and <code>c</code>. Determine if <var>S</var> can be obtained by permuting <code>abc</code>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>|S|=3</var></li> <li><var>S</var> consists of <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>Input is given from Standard Input in the following format:</p> <pre><var>S</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If <var>S</var> can be obtained by permuting <code>abc</code>, print <code>Yes</code>; otherwise, print <code>No</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>bac </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes </pre> <p>Swapping the first and second characters in <code>bac</code> results in <code>abc</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>bab </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>abc </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Yes </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>aaa </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>No </pre></section> </div> </span>
[ [ "bac\n", "bac\n" ] ]
p03386
AtCoder Beginner Contest 093 - Small and Large Integers
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Print all the integers that satisfies the following in ascending order:</p> <ul> <li>Among the integers between <var>A</var> and <var>B</var> (inclusive), it is either within the <var>K</var> smallest integers or within the <var>K</var> largest integers.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq A \leq B \leq 10^9</var></li> <li><var>1 \leq K \leq 100</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> <var>K</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print all the integers that satisfies the condition above in ascending order.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 8 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 4 7 8 </pre> <ul> <li><var>3</var> is the first smallest integer among the integers between <var>3</var> and <var>8</var>.</li> <li><var>4</var> is the second smallest integer among the integers between <var>3</var> and <var>8</var>.</li> <li><var>7</var> is the second largest integer among the integers between <var>3</var> and <var>8</var>.</li> <li><var>8</var> is the first largest integer among the integers between <var>3</var> and <var>8</var>.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 8 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>4 5 6 7 8 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>2 9 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>2 3 4 5 6 7 8 9 </pre></section> </div> </span>
[ [ "3 8 2\n", "3 8 2\n" ] ]
p03387
AtCoder Beginner Contest 093 - Same Integers
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given three integers <var>A</var>, <var>B</var> and <var>C</var>. Find the minimum number of operations required to make <var>A</var>, <var>B</var> and <var>C</var> all equal by repeatedly performing the following two kinds of operations in any order:</p> <ul> <li>Choose two among <var>A</var>, <var>B</var> and <var>C</var>, then increase both by <var>1</var>.</li> <li>Choose one among <var>A</var>, <var>B</var> and <var>C</var>, then increase it by <var>2</var>.</li> </ul> <p>It can be proved that we can always make <var>A</var>, <var>B</var> and <var>C</var> all equal by repeatedly performing these operations.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>0 \leq A,B,C \leq 50</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> <var>C</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of operations required to make <var>A</var>, <var>B</var> and <var>C</var> all equal.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 5 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We can make <var>A</var>, <var>B</var> and <var>C</var> all equal by the following operations:</p> <ul> <li>Increase <var>A</var> and <var>C</var> by <var>1</var>. Now, <var>A</var>, <var>B</var>, <var>C</var> are <var>3</var>, <var>5</var>, <var>5</var>, respectively.</li> <li>Increase <var>A</var> by <var>2</var>. Now, <var>A</var>, <var>B</var>, <var>C</var> are <var>5</var>, <var>5</var>, <var>5</var>, respectively.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 6 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>5 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>31 41 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>23 </pre></section> </div> </span>
[ [ "2 5 4\n", "2 5 4\n" ] ]
p03388
AtCoder Beginner Contest 093 - Worst Case
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p><var>10^{10^{10}}</var> participants, including Takahashi, competed in two programming contests. In each contest, all participants had distinct ranks from first through <var>10^{10^{10}}</var>-th.</p> <p>The <em>score</em> of a participant is the product of his/her ranks in the two contests.</p> <p>Process the following <var>Q</var> queries:</p> <ul> <li>In the <var>i</var>-th query, you are given two positive integers <var>A_i</var> and <var>B_i</var>. Assuming that Takahashi was ranked <var>A_i</var>-th in the first contest and <var>B_i</var>-th in the second contest, find the maximum possible number of participants whose scores are smaller than Takahashi's.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq Q \leq 100</var></li> <li><var>1\leq A_i,B_i\leq 10^9(1\leq i\leq Q)</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>Q</var> <var>A_1</var> <var>B_1</var> <var>:</var> <var>A_Q</var> <var>B_Q</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>For each query, print the maximum possible number of participants whose scores are smaller than Takahashi's.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>8 1 4 10 5 3 3 4 11 8 9 22 40 8 36 314159265 358979323 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 12 4 11 14 57 31 671644785 </pre> <p>Let us denote a participant who was ranked <var>x</var>-th in the first contest and <var>y</var>-th in the second contest as <var>(x,y)</var>.</p> <p>In the first query, <var>(2,1)</var> is a possible candidate of a participant whose score is smaller than Takahashi's. There are never two or more participants whose scores are smaller than Takahashi's, so we should print <var>1</var>.</p></section> </div> </span>
[ [ "8\n1 4\n10 5\n3 3\n4 11\n8 9\n22 40\n8 36\n314159265 358979323\n", "8\n1 4\n10 5\n3 3\n4 11\n8 9\n22 40\n8 36\n314159265 358979323\n" ] ]
p03389
AtCoder Regular Contest 094 - Same Integers
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given three integers <var>A</var>, <var>B</var> and <var>C</var>. Find the minimum number of operations required to make <var>A</var>, <var>B</var> and <var>C</var> all equal by repeatedly performing the following two kinds of operations in any order:</p> <ul> <li>Choose two among <var>A</var>, <var>B</var> and <var>C</var>, then increase both by <var>1</var>.</li> <li>Choose one among <var>A</var>, <var>B</var> and <var>C</var>, then increase it by <var>2</var>.</li> </ul> <p>It can be proved that we can always make <var>A</var>, <var>B</var> and <var>C</var> all equal by repeatedly performing these operations.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>0 \leq A,B,C \leq 50</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> <var>C</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of operations required to make <var>A</var>, <var>B</var> and <var>C</var> all equal.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 5 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We can make <var>A</var>, <var>B</var> and <var>C</var> all equal by the following operations:</p> <ul> <li>Increase <var>A</var> and <var>C</var> by <var>1</var>. Now, <var>A</var>, <var>B</var>, <var>C</var> are <var>3</var>, <var>5</var>, <var>5</var>, respectively.</li> <li>Increase <var>A</var> by <var>2</var>. Now, <var>A</var>, <var>B</var>, <var>C</var> are <var>5</var>, <var>5</var>, <var>5</var>, respectively.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 6 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>5 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>31 41 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>23 </pre></section> </div> </span>
[ [ "2 5 4\n", "2 5 4\n" ] ]
p03390
AtCoder Regular Contest 094 - Worst Case
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p><var>10^{10^{10}}</var> participants, including Takahashi, competed in two programming contests. In each contest, all participants had distinct ranks from first through <var>10^{10^{10}}</var>-th.</p> <p>The <em>score</em> of a participant is the product of his/her ranks in the two contests.</p> <p>Process the following <var>Q</var> queries:</p> <ul> <li>In the <var>i</var>-th query, you are given two positive integers <var>A_i</var> and <var>B_i</var>. Assuming that Takahashi was ranked <var>A_i</var>-th in the first contest and <var>B_i</var>-th in the second contest, find the maximum possible number of participants whose scores are smaller than Takahashi's.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq Q \leq 100</var></li> <li><var>1\leq A_i,B_i\leq 10^9(1\leq i\leq Q)</var></li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>Q</var> <var>A_1</var> <var>B_1</var> <var>:</var> <var>A_Q</var> <var>B_Q</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>For each query, print the maximum possible number of participants whose scores are smaller than Takahashi's.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>8 1 4 10 5 3 3 4 11 8 9 22 40 8 36 314159265 358979323 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 12 4 11 14 57 31 671644785 </pre> <p>Let us denote a participant who was ranked <var>x</var>-th in the first contest and <var>y</var>-th in the second contest as <var>(x,y)</var>.</p> <p>In the first query, <var>(2,1)</var> is a possible candidate of a participant whose score is smaller than Takahashi's. There are never two or more participants whose scores are smaller than Takahashi's, so we should print <var>1</var>.</p></section> </div> </span>
[ [ "8\n1 4\n10 5\n3 3\n4 11\n8 9\n22 40\n8 36\n314159265 358979323\n", "8\n1 4\n10 5\n3 3\n4 11\n8 9\n22 40\n8 36\n314159265 358979323\n" ] ]
p03391
AtCoder Regular Contest 094 - Tozan and Gezan
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given sequences <var>A</var> and <var>B</var> consisting of non-negative integers. The lengths of both <var>A</var> and <var>B</var> are <var>N</var>, and the sums of the elements in <var>A</var> and <var>B</var> are equal. The <var>i</var>-th element in <var>A</var> is <var>A_i</var>, and the <var>i</var>-th element in <var>B</var> is <var>B_i</var>.</p> <p>Tozan and Gezan repeats the following sequence of operations:</p> <ul> <li>If <var>A</var> and <var>B</var> are equal sequences, terminate the process.</li> <li>Otherwise, first Tozan chooses a positive element in <var>A</var> and decrease it by <var>1</var>.</li> <li>Then, Gezan chooses a positive element in <var>B</var> and decrease it by <var>1</var>.</li> <li>Then, give one candy to Takahashi, their pet.</li> </ul> <p>Tozan wants the number of candies given to Takahashi until the process is terminated to be as large as possible, while Gezan wants it to be as small as possible. Find the number of candies given to Takahashi when both of them perform the operations optimally.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 2 × 10^5</var></li> <li><var>0 \leq A_i,B_i \leq 10^9(1\leq i\leq N)</var></li> <li>The sums of the elements in <var>A</var> and <var>B</var> are equal.</li> <li>All values in input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> <var>:</var> <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of candies given to Takahashi when both Tozan and Gezan perform the operations optimally.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 1 2 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>When both Tozan and Gezan perform the operations optimally, the process will proceed as follows:</p> <ul> <li>Tozan decreases <var>A_1</var> by <var>1</var>.</li> <li>Gezan decreases <var>B_1</var> by <var>1</var>.</li> <li>One candy is given to Takahashi.</li> <li>Tozan decreases <var>A_2</var> by <var>1</var>.</li> <li>Gezan decreases <var>B_1</var> by <var>1</var>.</li> <li>One candy is given to Takahashi.</li> <li>As <var>A</var> and <var>B</var> are equal, the process is terminated.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 8 3 0 1 4 8 </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>1 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre></section> </div> </span>
[ [ "2\n1 2\n3 2\n", "2\n1 2\n3 2\n" ] ]
p03392
AtCoder Regular Contest 094 - Normalization
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a string <var>S</var> consisting of <code>a</code>,<code>b</code> and <code>c</code>. Find the number of strings that can be possibly obtained by repeatedly performing the following operation zero or more times, modulo <var>998244353</var>:</p> <ul> <li>Choose an integer <var>i</var> such that <var>1\leq i\leq |S|-1</var> and the <var>i</var>-th and <var>(i+1)</var>-th characters in <var>S</var> are different. Replace each of the <var>i</var>-th and <var>(i+1)</var>-th characters in <var>S</var> with the character that differs from both of them (among <code>a</code>, <code>b</code> and <code>c</code>).</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq |S| \leq 2 × 10^5</var></li> <li><var>S</var> consists of <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>Input is given from Standard Input in the following format:</p> <pre><var>S</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of strings that can be possibly obtained by repeatedly performing the operation, modulo <var>998244353</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>abc </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p><code>abc</code>, <code>aaa</code> and <code>ccc</code> can be obtained.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>abbac </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>65 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>babacabac </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>6310 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>ababacbcacbacacbcbbcbbacbaccacbacbacba </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>148010497 </pre></section> </div> </span>
[ [ "abc\n", "abc\n" ] ]
p03393
AtCoder Grand Contest 022 - Diverse Word
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Gotou just received a dictionary. However, he doesn't recognize the language used in the dictionary. He did some analysis on the dictionary and realizes that the dictionary contains all possible <strong>diverse</strong> words in lexicographical order.</p> <p>A word is called <strong>diverse</strong> if and only if it is a nonempty string of English lowercase letters and all letters in the word are distinct. For example, <code>atcoder</code>, <code>zscoder</code> and <code>agc</code> are diverse words while <code>gotou</code> and <code>connect</code> aren't diverse words.</p> <p>Given a diverse word <var>S</var>, determine the next word that appears after <var>S</var> in the dictionary, i.e. the lexicographically smallest diverse word that is lexicographically larger than <var>S</var>, or determine that it doesn't exist.</p> <p>Let <var>X = x_{1}x_{2}...x_{n}</var> and <var>Y = y_{1}y_{2}...y_{m}</var> be two distinct strings. <var>X</var> is lexicographically larger than <var>Y</var> if and only if <var>Y</var> is a prefix of <var>X</var> or <var>x_{j} &gt; y_{j}</var> where <var>j</var> is the smallest integer such that <var>x_{j} \neq y_{j}</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq |S| \leq 26</var></li> <li><var>S</var> is a diverse word.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>S</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the next word that appears after <var>S</var> in the dictionary, or <code>-1</code> if it doesn't exist.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>atcoder </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>atcoderb </pre> <p><code>atcoderb</code> is the lexicographically smallest diverse word that is lexicographically larger than <code>atcoder</code>. Note that <code>atcoderb</code> is lexicographically smaller than <code>b</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>abc </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>abcd </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>zyxwvutsrqponmlkjihgfedcba </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>-1 </pre> <p>This is the lexicographically largest diverse word, so the answer is <code>-1</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>abcdefghijklmnopqrstuvwzyx </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>abcdefghijklmnopqrstuvx </pre></section> </div> </span>
[ [ "atcoder\n", "atcoder\n" ] ]
p03394
AtCoder Grand Contest 022 - GCD Sequence
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Nagase is a top student in high school. One day, she's analyzing some properties of special sets of positive integers. </p> <p>She thinks that a set <var>S = \{a_{1}, a_{2}, ..., a_{N}\}</var> of <strong>distinct</strong> positive integers is called <strong>special</strong> if for all <var>1 \leq i \leq N</var>, the gcd (greatest common divisor) of <var>a_{i}</var> and the sum of the remaining elements of <var>S</var> is <strong>not</strong> <var>1</var>.</p> <p>Nagase wants to find a <strong>special</strong> set of size <var>N</var>. However, this task is too easy, so she decided to ramp up the difficulty. Nagase challenges you to find a <strong>special</strong> set of size <var>N</var> such that the gcd of all elements are <var>1</var> and the elements of the set does not exceed <var>30000</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>3 \leq N \leq 20000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Output <var>N</var> space-separated integers, denoting the elements of the set <var>S</var>. <var>S</var> must satisfy the following conditions :</p> <ul> <li>The elements must be <strong>distinct</strong> positive integers not exceeding <var>30000</var>.</li> <li>The gcd of all elements of <var>S</var> is <var>1</var>, i.e. there does not exist an integer <var>d &gt; 1</var> that divides all elements of <var>S</var>.</li> <li><var>S</var> is a <strong>special</strong> set.</li> </ul> <p>If there are multiple solutions, you may output any of them. The elements of <var>S</var> may be printed in any order. It is guaranteed that at least one solution exist under the given contraints.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 5 63 </pre> <p><var>\{2, 5, 63\}</var> is special because <var>gcd(2, 5 + 63) = 2, gcd(5, 2 + 63) = 5, gcd(63, 2 + 5) = 7</var>. Also, <var>gcd(2, 5, 63) = 1</var>. Thus, this set satisfies all the criteria.</p> <p>Note that <var>\{2, 4, 6\}</var> is not a valid solution because <var>gcd(2, 4, 6) = 2 &gt; 1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 5 20 63 </pre> <p><var>\{2, 5, 20, 63\}</var> is special because <var>gcd(2, 5 + 20 + 63) = 2, gcd(5, 2 + 20 + 63) = 5, gcd(20, 2 + 5 + 63) = 10, gcd(63, 2 + 5 + 20) = 9</var>. Also, <var>gcd(2, 5, 20, 63) = 1</var>. Thus, this set satisfies all the criteria.</p></section> </div> </span>
[ [ "3\n", "3\n" ] ]
p03395
AtCoder Grand Contest 022 - Remainder Game
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Aoki is playing with a sequence of numbers <var>a_{1}, a_{2}, ..., a_{N}</var>. Every second, he performs the following operation :</p> <ul> <li>Choose a positive integer <var>k</var>. For each element of the sequence <var>v</var>, Aoki may choose to replace <var>v</var> with its remainder when divided by <var>k</var>, or do nothing with <var>v</var>. The cost of this operation is <var>2^{k}</var> (regardless of how many elements he changes).</li> </ul> <p>Aoki wants to turn the sequence into <var>b_{1}, b_{2}, ..., b_{N}</var> (the order of the elements is important). Determine if it is possible for Aoki to perform this task and if yes, find the minimum cost required.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 50</var></li> <li><var>0 \leq a_{i}, b_{i} \leq 50</var></li> <li>All values in the input are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_{1}</var> <var>a_{2}</var> <var>...</var> <var>a_{N}</var> <var>b_{1}</var> <var>b_{2}</var> <var>...</var> <var>b_{N}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum cost required to turn the original sequence into <var>b_{1}, b_{2}, ..., b_{N}</var>. If the task is impossible, output <var>-1</var> instead.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 19 10 14 0 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>160 </pre> <p>Here's a possible sequence of operations :</p> <ul> <li> <p>Choose <var>k = 7</var>. Replace <var>19</var> with <var>5</var>, <var>10</var> with <var>3</var> and do nothing to <var>14</var>. The sequence is now <var>5, 3, 14</var>.</p> </li> <li> <p>Choose <var>k = 5</var>. Replace <var>5</var> with <var>0</var>, do nothing to <var>3</var> and replace <var>14</var> with <var>4</var>. The sequence is now <var>0, 3, 4</var>.</p> </li> </ul> <p>The total cost is <var>2^{7} + 2^{5} = 160</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 19 15 14 0 0 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 </pre> <p>Aoki can just choose <var>k = 1</var> and turn everything into <var>0</var>. The cost is <var>2^{1} = 2</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>2 8 13 5 13 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>-1 </pre> <p>The task is impossible because we can never turn <var>8</var> into <var>5</var> using the given operation.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>4 2 0 1 8 2 0 1 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>0 </pre> <p>Aoki doesn't need to do anything here. The cost is <var>0</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>1 50 13 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>137438953472 </pre> <p>Beware of overflow issues.</p></section> </div> </span>
[ [ "3\n19 10 14\n0 3 4\n", "3\n19 10 14\n0 3 4\n" ] ]
p03396
AtCoder Grand Contest 022 - Shopping
<span class="lang-en"> <p>Score : <var>1600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Yui loves shopping. She lives in Yamaboshi City and there is a train service in the city. The city can be modelled as a very long number line. Yui's house is at coordinate <var>0</var>.</p> <p>There are <var>N</var> shopping centres in the city, located at coordinates <var>x_{1}, x_{2}, ..., x_{N}</var> respectively. There are <var>N + 2</var> train stations, one located at coordinate <var>0</var>, one located at coordinate <var>L</var>, and one located at each shopping centre.</p> <p>At time <var>0</var>, the train departs from position <var>0</var> to the positive direction. The train travels at a constant speed of <var>1</var> unit per second. At time <var>L</var>, the train will reach the last station, the station at coordinate <var>L</var>. The train immediately moves in the opposite direction at the same speed. At time <var>2L</var>, the train will reach the station at coordinate <var>0</var> and it immediately moves in the opposite direction again. The process repeats indefinitely.</p> <p>When the train arrives at a station where Yui is located, Yui can board or leave the train immediately. At time <var>0</var>, Yui is at the station at coordinate <var>0</var>. </p> <p>Yui wants to go shopping in all <var>N</var> shopping centres, in any order, and return home after she finishes her shopping. She needs to shop for <var>t_{i}</var> seconds in the shopping centre at coordinate <var>x_{i}</var>. <strong>She must finish her shopping in one shopping centre before moving to the next shopping centre.</strong> Yui can immediately start shopping when she reaches a station with a shopping centre and she can immediately board the train when she finishes shopping.</p> <p>Yui wants to spend the minimum amount of time to finish her shopping. Can you help her determine the minimum number of seconds required to complete her shopping?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 300000</var></li> <li><var>1 \leq L \leq 10^{9}</var></li> <li><var>0 &lt; x_{1} &lt; x_{2} &lt; ... &lt; x_{N} &lt; L</var></li> <li><var>1 \leq t_{i} \leq 10^{9}</var></li> <li>All values in the input are integers.</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li><var>1000</var> points will be awarded for passing the testset satisfying <var>1 \leq N \leq 3000</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>L</var> <var>x_{1}</var> <var>x_{2}</var> <var>...</var> <var>x_{N}</var> <var>t_{1}</var> <var>t_{2}</var> <var>...</var> <var>t_{N}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum time (in seconds) required for Yui to finish shopping at all <var>N</var> shopping centres and return home.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 10 5 8 10 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>40 </pre> <p>Here's one possible way for Yui to finish her shopping :</p> <ul> <li> <p>Travel to the station at coordinate <var>8</var> with the train. She arrives at coordinate <var>8</var> at time <var>8</var>.</p> </li> <li> <p>Shop in the shopping centre at coordinate <var>8</var>. She finishes her shopping at time <var>12</var>.</p> </li> <li> <p>The train arrives at coordinate <var>8</var> at time <var>12</var>. She boards the train which is currently moving in the negative direction.</p> </li> <li> <p>She arrives at coordinate <var>5</var> at time <var>15</var>. She finishes her shopping at time <var>25</var>.</p> </li> <li> <p>The train arrives at coordinate <var>5</var> at time <var>25</var>. She boards the train which is currently moving in the positive direction.</p> </li> <li> <p>She arrives at coordinate <var>L = 10</var> at time <var>30</var>. The train starts moving in the negative direction immediately.</p> </li> <li> <p>She arrives at coordinate <var>0</var> at time <var>40</var>, ending the trip.</p> </li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 10 5 8 10 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>60 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 100 10 19 28 47 68 200 200 200 200 200 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1200 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>8 1000000000 2018 123456 1719128 1929183 9129198 10100101 77777777 120182018 99999999 1000000000 1000000000 11291341 1 200 1 123812831 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>14000000000 </pre> <p>Beware of overflow issues.</p></section> </div> </span>
[ [ "2 10\n5 8\n10 4\n", "2 10\n5 8\n10 4\n" ] ]
p03397
AtCoder Grand Contest 022 - Median Replace
<span class="lang-en"> <p>Score : <var>1600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Taichi thinks a binary string <var>X</var> of odd length <var>N</var> is <strong>beautiful</strong> if it is possible to apply the following operation <var>\frac{N-1}{2}</var> times so that the only character of the resulting string is <code>1</code> :</p> <ul> <li>Choose three <strong>consecutive</strong> bits of <var>X</var> and replace them by their median. For example, we can turn <code>00110</code> into <code>010</code> by applying the operation to the middle three bits.</li> </ul> <p>Taichi has a string <var>S</var> consisting of characters <code>0</code>, <code>1</code> and <code>?</code>. Taichi wants to know the number of ways to replace the question marks with <code>1</code> or <code>0</code> so that the resulting string is beautiful, modulo <var>10^{9} + 7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq |S| \leq 300000</var></li> <li><var>|S|</var> is odd.</li> <li>All characters of <var>S</var> are either <code>0</code>, <code>1</code> or <code>?</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>S</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways to replace the question marks so that the resulting string is beautiful, modulo <var>10^{9} + 7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>1??00 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>There are <var>4</var> ways to replace the question marks with <code>0</code> or <code>1</code> :</p> <ul> <li> <p><code>11100</code> : This string is beautiful because we can first perform the operation on the last <var>3</var> bits to get <code>110</code> and then on the only <var>3</var> bits to get <code>1</code>.</p> </li> <li> <p><code>11000</code> : This string is beautiful because we can first perform the operation on the last <var>3</var> bits to get <code>110</code> and then on the only <var>3</var> bits to get <code>1</code>.</p> </li> <li> <p><code>10100</code> : This string is not beautiful because there is no sequence of operations such that the final string is <code>1</code>.</p> </li> <li> <p><code>10000</code> : This string is not beautiful because there is no sequence of operations such that the final string is <code>1</code>.</p> </li> </ul> <p>Thus, there are <var>2</var> ways to form a beautiful string.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>? </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 </pre> <p>In this case, <code>1</code> is the only beautiful string.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>?0101???10???00?1???????????????0????????????1????0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>402589311 </pre> <p>Remember to output your answer modulo <var>10^{9} + 7</var>.</p></section> </div> </span>
[ [ "1??00\n", "1??00\n" ] ]
p03398
AtCoder Grand Contest 022 - Checkers
<span class="lang-en"> <p>Score : <var>1600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Let <var>X = 10^{100}</var>. Inaba has <var>N</var> checker pieces on the number line, where the <var>i</var>-th checker piece is at coordinate <var>X^{i}</var> for all <var>1 \leq i \leq N</var>.</p> <p>Every second, Inaba chooses two checker pieces, <var>A</var> and <var>B</var>, and move <var>A</var> to the symmetric point of its current position with respect to <var>B</var>. After that, <var>B</var> is removed. (It is possible that <var>A</var> and <var>B</var> occupy the same position, and it is also possible for <var>A</var> to occupy the same position as another checker piece after the move).</p> <p>After <var>N - 1</var> seconds, only one checker piece will remain. Find the number of distinct possible positions of that checker piece, modulo <var>10^{9} + 7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 50</var></li> <li><var>N</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of distinct possible positions of the final checker piece, modulo <var>10^{9} + 7</var>.</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>12 </pre> <p>There are <var>3</var> checker pieces, positioned at <var>10^{100}, 10^{200}, 10^{300}</var> respectively. Call them <var>A, B, C</var> respectively.</p> <p>Here are two (of the <var>12</var>) possible sequence of moves :</p> <ul> <li> <p>Let <var>A</var> jump over <var>B</var> in the first second, and let <var>A</var> jump over <var>C</var> in the second second. The final position of <var>A</var> is <var>2 \times 10^{300} - 2 \times 10^{200} + 10^{100}</var>.</p> </li> <li> <p>Let <var>C</var> jump over <var>A</var> in the first second, and let <var>B</var> jump over <var>C</var> in the second second. The final position of <var>B</var> is <var>-2 \times 10^{300} - 10^{200} + 4 \times 10^{100}</var>.</p> </li> </ul> <p>There are a total of <var>3 \times 2 \times 2 = 12</var> possible sequence of moves, and the final piece are in different positions in all of them.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>84 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>22 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>487772376 </pre> <p>Remember to output your answer modulo <var>10^{9} + 7</var>.</p></section> </div> </span>
[ [ "3\n", "3\n" ] ]
p03399
AtCoder Beginner Contest 092 - Traveling Budget
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You planned a trip using trains and buses. The train fare will be <var>A</var> yen (the currency of Japan) if you buy ordinary tickets along the way, and <var>B</var> yen if you buy an unlimited ticket. Similarly, the bus fare will be <var>C</var> yen if you buy ordinary tickets along the way, and <var>D</var> yen if you buy an unlimited ticket.</p> <p>Find the minimum total fare when the optimal choices are made for trains and buses.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq A \leq 1</var> <var>000</var></li> <li><var>1 \leq B \leq 1</var> <var>000</var></li> <li><var>1 \leq C \leq 1</var> <var>000</var></li> <li><var>1 \leq D \leq 1</var> <var>000</var></li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> <var>C</var> <var>D</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum total fare.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>600 300 220 420 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>520 </pre> <p>The train fare will be <var>600</var> yen if you buy ordinary tickets, and <var>300</var> yen if you buy an unlimited ticket. Thus, the optimal choice for trains is to buy an unlimited ticket for <var>300</var> yen. On the other hand, the optimal choice for buses is to buy ordinary tickets for <var>220</var> yen.</p> <p>Therefore, the minimum total fare is <var>300 + 220 = 520</var> yen.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>555 555 400 200 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>755 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>549 817 715 603 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1152 </pre></section> </div> </span>
[ [ "600\n300\n220\n420\n", "600\n300\n220\n420\n" ] ]
p03400
AtCoder Beginner Contest 092 - Chocolate
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Some number of chocolate pieces were prepared for a training camp. The camp had <var>N</var> participants and lasted for <var>D</var> days. The <var>i</var>-th participant (<var>1 \leq i \leq N</var>) ate one chocolate piece on each of the following days in the camp: the <var>1</var>-st day, the <var>(A_i + 1)</var>-th day, the <var>(2A_i + 1)</var>-th day, and so on. As a result, there were <var>X</var> chocolate pieces remaining at the end of the camp. During the camp, nobody except the participants ate chocolate pieces.</p> <p>Find the number of chocolate pieces prepared at the beginning of the camp.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 100</var></li> <li><var>1 \leq D \leq 100</var></li> <li><var>1 \leq X \leq 100</var></li> <li><var>1 \leq A_i \leq 100</var> (<var>1 \leq i \leq N</var>)</li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>D</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 number of chocolate pieces prepared at the beginning of the camp.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 7 1 2 5 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>8 </pre> <p>The camp has <var>3</var> participants and lasts for <var>7</var> days. Each participant eats chocolate pieces as follows:</p> <ul> <li>The first participant eats one chocolate piece on Day <var>1</var>, <var>3</var>, <var>5</var> and <var>7</var>, for a total of four.</li> <li>The second participant eats one chocolate piece on Day <var>1</var> and <var>6</var>, for a total of two.</li> <li>The third participant eats one chocolate piece only on Day <var>1</var>, for a total of one.</li> </ul> <p>Since the number of pieces remaining at the end of the camp is one, the number of pieces prepared at the beginning of the camp is <var>1 + 4 + 2 + 1 = 8</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 8 20 1 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>29 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 30 44 26 18 81 18 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>56 </pre></section> </div> </span>
[ [ "3\n7 1\n2\n5\n10\n", "3\n7 1\n2\n5\n10\n" ] ]
p03401
AtCoder Beginner Contest 092 - Traveling Plan
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> sightseeing spots on the <var>x</var>-axis, numbered <var>1, 2, ..., N</var>. Spot <var>i</var> is at the point with coordinate <var>A_i</var>. It costs <var>|a - b|</var> yen (the currency of Japan) to travel from a point with coordinate <var>a</var> to another point with coordinate <var>b</var> along the axis.</p> <p>You planned a trip along the axis. In this plan, you first depart from the point with coordinate <var>0</var>, then visit the <var>N</var> spots in the order they are numbered, and finally return to the point with coordinate <var>0</var>.</p> <p>However, something came up just before the trip, and you no longer have enough time to visit all the <var>N</var> spots, so you decided to choose some <var>i</var> and cancel the visit to Spot <var>i</var>. You will visit the remaining spots as planned in the order they are numbered. You will also depart from and return to the point with coordinate <var>0</var> at the beginning and the end, as planned.</p> <p>For each <var>i = 1, 2, ..., N</var>, find the total cost of travel during the trip when the visit to Spot <var>i</var> is canceled.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 10^5</var></li> <li><var>-5000 \leq A_i \leq 5000</var> (<var>1 \leq i \leq N</var>)</li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>N</var> lines. In the <var>i</var>-th line, print the total cost of travel during the trip when the visit to Spot <var>i</var> is canceled.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 5 -1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>12 8 10 </pre> <p>Spot <var>1</var>, <var>2</var> and <var>3</var> are at the points with coordinates <var>3</var>, <var>5</var> and <var>-1</var>, respectively. For each <var>i</var>, the course of the trip and the total cost of travel when the visit to Spot <var>i</var> is canceled, are as follows:</p> <ul> <li>For <var>i = 1</var>, the course of the trip is <var>0 \rightarrow 5 \rightarrow -1 \rightarrow 0</var> and the total cost of travel is <var>5 + 6 + 1 = 12</var> yen.</li> <li>For <var>i = 2</var>, the course of the trip is <var>0 \rightarrow 3 \rightarrow -1 \rightarrow 0</var> and the total cost of travel is <var>3 + 4 + 1 = 8</var> yen.</li> <li>For <var>i = 3</var>, the course of the trip is <var>0 \rightarrow 3 \rightarrow 5 \rightarrow 0</var> and the total cost of travel is <var>3 + 2 + 5 = 10</var> yen.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 1 1 1 2 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>4 4 4 2 4 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 -679 -2409 -3258 3095 -3291 -4462 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>21630 21630 19932 8924 21630 19288 </pre></section> </div> </span>
[ [ "3\n3 5 -1\n", "3\n3 5 -1\n" ] ]
p03402
AtCoder Beginner Contest 092 - Grid Components
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given two integers <var>A</var> and <var>B</var>.</p> <p>Print a grid where each square is painted white or black that satisfies the following conditions, in the format specified in Output section:</p> <ul> <li>Let the size of the grid be <var>h \times w</var> (<var>h</var> vertical, <var>w</var> horizontal). Both <var>h</var> and <var>w</var> are at most <var>100</var>.</li> <li>The set of the squares painted white is divided into exactly <var>A</var> connected components.</li> <li>The set of the squares painted black is divided into exactly <var>B</var> connected components.</li> </ul> <p>It can be proved that there always exist one or more solutions under the conditions specified in Constraints section. If there are multiple solutions, any of them may be printed.</p> </section> </div> <div class="part"> <section> <h3>Notes</h3><p>Two squares painted white, <var>c_1</var> and <var>c_2</var>, are called connected when the square <var>c_2</var> can be reached from the square <var>c_1</var> passing only white squares by repeatedly moving up, down, left or right to an adjacent square.</p> <p>A set of squares painted white, <var>S</var>, forms a connected component when the following conditions are met:</p> <ul> <li>Any two squares in <var>S</var> are connected.</li> <li>No pair of a square painted white that is not included in <var>S</var> and a square included in <var>S</var> is connected.</li> </ul> <p>A connected component of squares painted black is defined similarly.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq A \leq 500</var></li> <li><var>1 \leq B \leq 500</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Output should be in the following format:</p> <ul> <li>In the first line, print integers <var>h</var> and <var>w</var> representing the size of the grid you constructed, with a space in between.</li> <li>Then, print <var>h</var> more lines. The <var>i</var>-th (<var>1 \leq i \leq h</var>) of these lines should contain a string <var>s_i</var> as follows:<ul> <li>If the square at the <var>i</var>-th row and <var>j</var>-th column (<var>1 \leq j \leq w</var>) in the grid is painted white, the <var>j</var>-th character in <var>s_i</var> should be <code>.</code>.</li> <li>If the square at the <var>i</var>-th row and <var>j</var>-th column (<var>1 \leq j \leq w</var>) in the grid is painted black, the <var>j</var>-th character in <var>s_i</var> should be <code>#</code>.</li> </ul> </li> </ul> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 3 ##. ..# #.# </pre> <p>This output corresponds to the grid below:</p> <div style="text-align: center;"> <img alt="2701558bf42f7c088abad927b419472a.png" src="https://img.atcoder.jp/arc093/2701558bf42f7c088abad927b419472a.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 5 #.#.# .#.#. #.#.# </pre> </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>4 2 .. #. ## ## </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>3 14 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>8 18 .................. .................. ....##.......####. ....#.#.....#..... ...#...#....#..... ..#.###.#...#..... .#.......#..#..... #.........#..####. </pre></section> </div> </span>
[ [ "2 3\n", "2 3\n" ] ]
p03403
AtCoder Regular Contest 093 - Traveling Plan
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> sightseeing spots on the <var>x</var>-axis, numbered <var>1, 2, ..., N</var>. Spot <var>i</var> is at the point with coordinate <var>A_i</var>. It costs <var>|a - b|</var> yen (the currency of Japan) to travel from a point with coordinate <var>a</var> to another point with coordinate <var>b</var> along the axis.</p> <p>You planned a trip along the axis. In this plan, you first depart from the point with coordinate <var>0</var>, then visit the <var>N</var> spots in the order they are numbered, and finally return to the point with coordinate <var>0</var>.</p> <p>However, something came up just before the trip, and you no longer have enough time to visit all the <var>N</var> spots, so you decided to choose some <var>i</var> and cancel the visit to Spot <var>i</var>. You will visit the remaining spots as planned in the order they are numbered. You will also depart from and return to the point with coordinate <var>0</var> at the beginning and the end, as planned.</p> <p>For each <var>i = 1, 2, ..., N</var>, find the total cost of travel during the trip when the visit to Spot <var>i</var> is canceled.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 10^5</var></li> <li><var>-5000 \leq A_i \leq 5000</var> (<var>1 \leq i \leq N</var>)</li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>N</var> lines. In the <var>i</var>-th line, print the total cost of travel during the trip when the visit to Spot <var>i</var> is canceled.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 5 -1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>12 8 10 </pre> <p>Spot <var>1</var>, <var>2</var> and <var>3</var> are at the points with coordinates <var>3</var>, <var>5</var> and <var>-1</var>, respectively. For each <var>i</var>, the course of the trip and the total cost of travel when the visit to Spot <var>i</var> is canceled, are as follows:</p> <ul> <li>For <var>i = 1</var>, the course of the trip is <var>0 \rightarrow 5 \rightarrow -1 \rightarrow 0</var> and the total cost of travel is <var>5 + 6 + 1 = 12</var> yen.</li> <li>For <var>i = 2</var>, the course of the trip is <var>0 \rightarrow 3 \rightarrow -1 \rightarrow 0</var> and the total cost of travel is <var>3 + 4 + 1 = 8</var> yen.</li> <li>For <var>i = 3</var>, the course of the trip is <var>0 \rightarrow 3 \rightarrow 5 \rightarrow 0</var> and the total cost of travel is <var>3 + 2 + 5 = 10</var> yen.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 1 1 1 2 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>4 4 4 2 4 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 -679 -2409 -3258 3095 -3291 -4462 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>21630 21630 19932 8924 21630 19288 </pre></section> </div> </span>
[ [ "3\n3 5 -1\n", "3\n3 5 -1\n" ] ]
p03404
AtCoder Regular Contest 093 - Grid Components
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given two integers <var>A</var> and <var>B</var>.</p> <p>Print a grid where each square is painted white or black that satisfies the following conditions, in the format specified in Output section:</p> <ul> <li>Let the size of the grid be <var>h \times w</var> (<var>h</var> vertical, <var>w</var> horizontal). Both <var>h</var> and <var>w</var> are at most <var>100</var>.</li> <li>The set of the squares painted white is divided into exactly <var>A</var> connected components.</li> <li>The set of the squares painted black is divided into exactly <var>B</var> connected components.</li> </ul> <p>It can be proved that there always exist one or more solutions under the conditions specified in Constraints section. If there are multiple solutions, any of them may be printed.</p> </section> </div> <div class="part"> <section> <h3>Notes</h3><p>Two squares painted white, <var>c_1</var> and <var>c_2</var>, are called connected when the square <var>c_2</var> can be reached from the square <var>c_1</var> passing only white squares by repeatedly moving up, down, left or right to an adjacent square.</p> <p>A set of squares painted white, <var>S</var>, forms a connected component when the following conditions are met:</p> <ul> <li>Any two squares in <var>S</var> are connected.</li> <li>No pair of a square painted white that is not included in <var>S</var> and a square included in <var>S</var> is connected.</li> </ul> <p>A connected component of squares painted black is defined similarly.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq A \leq 500</var></li> <li><var>1 \leq B \leq 500</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Output should be in the following format:</p> <ul> <li>In the first line, print integers <var>h</var> and <var>w</var> representing the size of the grid you constructed, with a space in between.</li> <li>Then, print <var>h</var> more lines. The <var>i</var>-th (<var>1 \leq i \leq h</var>) of these lines should contain a string <var>s_i</var> as follows:<ul> <li>If the square at the <var>i</var>-th row and <var>j</var>-th column (<var>1 \leq j \leq w</var>) in the grid is painted white, the <var>j</var>-th character in <var>s_i</var> should be <code>.</code>.</li> <li>If the square at the <var>i</var>-th row and <var>j</var>-th column (<var>1 \leq j \leq w</var>) in the grid is painted black, the <var>j</var>-th character in <var>s_i</var> should be <code>#</code>.</li> </ul> </li> </ul> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 3 ##. ..# #.# </pre> <p>This output corresponds to the grid below:</p> <div style="text-align: center;"> <img alt="2701558bf42f7c088abad927b419472a.png" src="https://img.atcoder.jp/arc093/2701558bf42f7c088abad927b419472a.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 5 #.#.# .#.#. #.#.# </pre> </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>4 2 .. #. ## ## </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>3 14 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>8 18 .................. .................. ....##.......####. ....#.#.....#..... ...#...#....#..... ..#.###.#...#..... .#.......#..#..... #.........#..####. </pre></section> </div> </span>
[ [ "2 3\n", "2 3\n" ] ]
p03405
AtCoder Regular Contest 093 - Bichrome Spanning Tree
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have an undirected weighted graph with <var>N</var> vertices and <var>M</var> edges. The <var>i</var>-th edge in the graph connects Vertex <var>U_i</var> and Vertex <var>V_i</var>, and has a weight of <var>W_i</var>. Additionally, you are given an integer <var>X</var>.</p> <p>Find the number of ways to paint each edge in this graph either white or black such that the following condition is met, modulo <var>10^9 + 7</var>:</p> <ul> <li>The graph has a spanning tree that contains both an edge painted white and an edge painted black. Furthermore, among such spanning trees, the one with the smallest weight has a weight of <var>X</var>.</li> </ul> <p>Here, the weight of a spanning tree is the sum of the weights of the edges contained in the spanning tree.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 1</var> <var>000</var></li> <li><var>1 \leq M \leq 2</var> <var>000</var></li> <li><var>1 \leq U_i, V_i \leq N</var> (<var>1 \leq i \leq M</var>)</li> <li><var>1 \leq W_i \leq 10^9</var> (<var>1 \leq i \leq M</var>)</li> <li>If <var>i \neq j</var>, then <var>(U_i, V_i) \neq (U_j, V_j)</var> and <var>(U_i, V_i) \neq (V_j, U_j)</var>.</li> <li><var>U_i \neq V_i</var> (<var>1 \leq i \leq M</var>)</li> <li>The given graph is connected.</li> <li><var>1 \leq X \leq 10^{12}</var></li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>X</var> <var>U_1</var> <var>V_1</var> <var>W_1</var> <var>U_2</var> <var>V_2</var> <var>W_2</var> <var>:</var> <var>U_M</var> <var>V_M</var> <var>W_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 2 1 2 1 2 3 1 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>6 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 3 3 1 2 1 2 3 1 3 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 4 1 1 2 3 1 3 3 2 4 6 2 5 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>8 10 49 4 6 10 8 4 11 5 8 9 1 8 10 3 8 128773450 7 8 10 4 2 4 3 4 1 3 1 13 5 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>4 </pre></section> </div> </span>
[ [ "3 3\n2\n1 2 1\n2 3 1\n3 1 1\n", "3 3\n2\n1 2 1\n2 3 1\n3 1 1\n" ] ]
p03406
AtCoder Regular Contest 093 - Dark Horse
<span class="lang-en"> <p>Score : <var>1100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>2^N</var> players, numbered <var>1, 2, ..., 2^N</var>. They decided to hold a tournament.</p> <p>The tournament proceeds as follows:</p> <ul> <li>Choose a permutation of <var>1, 2, ..., 2^N</var>: <var>p_1, p_2, ..., p_{2^N}</var>.</li> <li>The players stand in a row in the order of Player <var>p_1</var>, Player <var>p_2</var>, <var>...</var>, Player <var>p_{2^N}</var>.</li> <li>Repeat the following until there is only one player remaining in the row:<ul> <li>Play the following matches: the first player in the row versus the second player in the row, the third player versus the fourth player, and so on. The players who lose leave the row. The players who win stand in a row again, preserving the relative order of the players.</li> </ul> </li> <li>The last player who remains in the row is the champion.</li> </ul> <p>It is known that, the result of the match between two players can be written as follows, using <var>M</var> integers <var>A_1, A_2, ..., A_M</var> given as input:</p> <ul> <li>When <var>y = A_i</var> for some <var>i</var>, the winner of the match between Player <var>1</var> and Player <var>y</var> (<var>2 \leq y \leq 2^N</var>) will be Player <var>y</var>.</li> <li>When <var>y \neq A_i</var> for every <var>i</var>, the winner of the match between Player <var>1</var> and Player <var>y</var> (<var>2 \leq y \leq 2^N</var>) will be Player <var>1</var>.</li> <li>When <var>2 \leq x &lt; y \leq 2^N</var>, the winner of the match between Player <var>x</var> and Player <var>y</var> will be Player <var>x</var>.</li> </ul> <p>The champion of this tournament depends only on the permutation <var>p_1, p_2, ..., p_{2^N}</var> chosen at the beginning. Find the number of permutation <var>p_1, p_2, ..., p_{2^N}</var> chosen at the beginning of the tournament that would result in Player <var>1</var> becoming the champion, modulo <var>10^9 + 7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 16</var></li> <li><var>0 \leq M \leq 16</var></li> <li><var>2 \leq A_i \leq 2^N</var> (<var>1 \leq i \leq M</var>)</li> <li><var>A_i &lt; A_{i + 1}</var> (<var>1 \leq i &lt; M</var>)</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 1 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>8 </pre> <p>Examples of <var>p</var> that satisfy the condition are: <var>[1, 4, 2, 3]</var> and <var>[3, 2, 1, 4]</var>. Examples of <var>p</var> that do not satisfy the condition are: <var>[1, 2, 3, 4]</var> and <var>[1, 3, 2, 4]</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 3 2 4 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>40320 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>3 3 3 4 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>2688 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>16 16 5489 5490 5491 5492 5493 5494 5495 5497 18993 18995 18997 18999 19000 19001 19002 19003 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>816646464 </pre></section> </div> </span>
[ [ "2 1\n3\n", "2 1\n3\n" ] ]
p03407
AtCoder Beginner Contest 091 - Two Coins
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>An elementary school student Takahashi has come to a variety store.</p> <p>He has two coins, <var>A</var>-yen and <var>B</var>-yen coins (yen is the currency of Japan), and wants to buy a toy that costs <var>C</var> yen. Can he buy it?</p> <p>Note that he lives in Takahashi Kingdom, and may have coins that do not exist in Japan.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>1 \leq A, B \leq 500</var></li> <li><var>1 \leq C \leq 1000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> <var>C</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If Takahashi can buy the toy, print <code>Yes</code>; if he cannot, print <code>No</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>50 100 120 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes </pre> <p>He has <var>50 + 100 = 150</var> yen, so he can buy the <var>120</var>-yen toy.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>500 100 1000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>No </pre> <p>He has <var>500 + 100 = 600</var> yen, but he cannot buy the <var>1000</var>-yen toy.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>19 123 143 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>No </pre> <p>There are <var>19</var>-yen and <var>123</var>-yen coins in Takahashi Kingdom, which are rather hard to use.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>19 123 142 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>Yes </pre></section> </div> </span>
[ [ "50 100 120\n", "50 100 120\n" ] ]
p03408
AtCoder Beginner Contest 091 - Two Colors Card Game
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi has <var>N</var> blue cards and <var>M</var> red cards. A string is written on each card. The string written on the <var>i</var>-th blue card is <var>s_i</var>, and the string written on the <var>i</var>-th red card is <var>t_i</var>.</p> <p>Takahashi will now announce a string, and then check every card. Each time he finds a blue card with the string announced by him, he will earn <var>1</var> yen (the currency of Japan); each time he finds a red card with that string, he will lose <var>1</var> yen.</p> <p>Here, we only consider the case where the string announced by Takahashi and the string on the card are exactly the same. For example, if he announces <code>atcoder</code>, he will not earn money even if there are blue cards with <code>atcoderr</code>, <code>atcode</code>, <code>btcoder</code>, and so on. (On the other hand, he will not lose money even if there are red cards with such strings, either.)</p> <p>At most how much can he earn on balance?</p> <p>Note that the same string may be written on multiple cards.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>N</var> and <var>M</var> are integers.</li> <li><var>1 \leq N, M \leq 100</var></li> <li><var>s_1, s_2, ..., s_N, t_1, t_2, ..., t_M</var> are all strings of lengths between <var>1</var> and <var>10</var> (inclusive) consisting of lowercase English letters.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>s_1</var> <var>s_2</var> <var>:</var> <var>s_N</var> <var>M</var> <var>t_1</var> <var>t_2</var> <var>:</var> <var>t_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If Takahashi can earn at most <var>X</var> yen on balance, print <var>X</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 apple orange apple 1 grape </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>He can earn <var>2</var> yen by announcing <code>apple</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 apple orange apple 5 apple apple apple apple apple </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 </pre> <p>If he announces <code>apple</code>, he will lose <var>3</var> yen. If he announces <code>orange</code>, he can earn <var>1</var> yen.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1 voldemort 10 voldemort voldemort voldemort voldemort voldemort voldemort voldemort voldemort voldemort voldemort </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> <p>If he announces <code>voldemort</code>, he will lose <var>9</var> yen. If he announces <code>orange</code>, for example, he can avoid losing a yen.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>6 red red blue yellow yellow red 5 red red yellow green blue </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>1 </pre></section> </div> </span>
[ [ "3\napple\norange\napple\n1\ngrape\n", "3\napple\norange\napple\n1\ngrape\n" ] ]
p03409
AtCoder Beginner Contest 091 - 2D Plane 2N Points
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>On a two-dimensional plane, there are <var>N</var> red points and <var>N</var> blue points. The coordinates of the <var>i</var>-th red point are <var>(a_i, b_i)</var>, and the coordinates of the <var>i</var>-th blue point are <var>(c_i, d_i)</var>.</p> <p>A red point and a blue point can form a <em>friendly pair</em> when, the <var>x</var>-coordinate of the red point is smaller than that of the blue point, and the <var>y</var>-coordinate of the red point is also smaller than that of the blue point.</p> <p>At most how many friendly pairs can you form? Note that a point cannot belong to multiple pairs.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>1 \leq N \leq 100</var></li> <li><var>0 \leq a_i, b_i, c_i, d_i &lt; 2N</var></li> <li><var>a_1, a_2, ..., a_N, c_1, c_2, ..., c_N</var> are all different.</li> <li><var>b_1, b_2, ..., b_N, d_1, d_2, ..., d_N</var> are all different.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>b_1</var> <var>a_2</var> <var>b_2</var> <var>:</var> <var>a_N</var> <var>b_N</var> <var>c_1</var> <var>d_1</var> <var>c_2</var> <var>d_2</var> <var>:</var> <var>c_N</var> <var>d_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum number of friendly pairs.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 0 3 1 1 3 4 2 0 4 5 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>For example, you can pair <var>(2, 0)</var> and <var>(4, 2)</var>, then <var>(3, 1)</var> and <var>(5, 5)</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 0 0 1 1 5 2 2 3 3 4 4 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 </pre> <p>For example, you can pair <var>(0, 0)</var> and <var>(2, 3)</var>, then <var>(1, 1)</var> and <var>(3, 4)</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>2 2 2 3 3 0 0 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> <p>It is possible that no pair can be formed.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>5 0 0 7 3 2 2 4 8 1 6 8 5 6 9 5 4 9 1 3 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>5 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>5 0 0 1 1 5 5 6 6 7 7 2 2 3 3 4 4 8 8 9 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>4 </pre></section> </div> </span>
[ [ "3\n2 0\n3 1\n1 3\n4 2\n0 4\n5 5\n", "3\n2 0\n3 1\n1 3\n4 2\n0 4\n5 5\n" ] ]
p03410
AtCoder Beginner Contest 091 - Two Sequences
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given two integer sequences, each of length <var>N</var>: <var>a_1, ..., a_N</var> and <var>b_1, ..., b_N</var>.</p> <p>There are <var>N^2</var> ways to choose two integers <var>i</var> and <var>j</var> such that <var>1 \leq i, j \leq N</var>. For each of these <var>N^2</var> pairs, we will compute <var>a_i + b_j</var> and write it on a sheet of paper. That is, we will write <var>N^2</var> integers in total.</p> <p>Compute the XOR of these <var>N^2</var> integers.</p> <p><details> <summary style="display:list-item">Definition of XOR</summary></details></p> <p>The XOR of integers <var>c_1, c_2, ..., c_m</var> is defined as follows:</p> <ul> <li>Let the XOR be <var>X</var>. In the binary representation of <var>X</var>, the digit in the <var>2^k</var>'s place (<var>0 \leq k</var>; <var>k</var> is an integer) is <var>1</var> if there are an odd number of integers among <var>c_1, c_2, ...c_m</var> whose binary representation has <var>1</var> in the <var>2^k</var>'s place, and <var>0</var> if that number is even.</li> </ul> <p>For example, let us compute the XOR of <var>3</var> and <var>5</var>. The binary representation of <var>3</var> is <var>011</var>, and the binary representation of <var>5</var> is <var>101</var>, thus the XOR has the binary representation <var>110</var>, that is, the XOR is <var>6</var>.</p> <p></p></section></div></span>
[ [ "", "" ] ]
p03411
AtCoder Regular Contest 092 - 2D Plane 2N Points
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>On a two-dimensional plane, there are <var>N</var> red points and <var>N</var> blue points. The coordinates of the <var>i</var>-th red point are <var>(a_i, b_i)</var>, and the coordinates of the <var>i</var>-th blue point are <var>(c_i, d_i)</var>.</p> <p>A red point and a blue point can form a <em>friendly pair</em> when, the <var>x</var>-coordinate of the red point is smaller than that of the blue point, and the <var>y</var>-coordinate of the red point is also smaller than that of the blue point.</p> <p>At most how many friendly pairs can you form? Note that a point cannot belong to multiple pairs.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>1 \leq N \leq 100</var></li> <li><var>0 \leq a_i, b_i, c_i, d_i &lt; 2N</var></li> <li><var>a_1, a_2, ..., a_N, c_1, c_2, ..., c_N</var> are all different.</li> <li><var>b_1, b_2, ..., b_N, d_1, d_2, ..., d_N</var> are all different.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>b_1</var> <var>a_2</var> <var>b_2</var> <var>:</var> <var>a_N</var> <var>b_N</var> <var>c_1</var> <var>d_1</var> <var>c_2</var> <var>d_2</var> <var>:</var> <var>c_N</var> <var>d_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum number of friendly pairs.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 0 3 1 1 3 4 2 0 4 5 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>For example, you can pair <var>(2, 0)</var> and <var>(4, 2)</var>, then <var>(3, 1)</var> and <var>(5, 5)</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 0 0 1 1 5 2 2 3 3 4 4 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 </pre> <p>For example, you can pair <var>(0, 0)</var> and <var>(2, 3)</var>, then <var>(1, 1)</var> and <var>(3, 4)</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>2 2 2 3 3 0 0 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> <p>It is possible that no pair can be formed.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>5 0 0 7 3 2 2 4 8 1 6 8 5 6 9 5 4 9 1 3 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>5 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>5 0 0 1 1 5 5 6 6 7 7 2 2 3 3 4 4 8 8 9 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>4 </pre></section> </div> </span>
[ [ "3\n2 0\n3 1\n1 3\n4 2\n0 4\n5 5\n", "3\n2 0\n3 1\n1 3\n4 2\n0 4\n5 5\n" ] ]
p03412
AtCoder Regular Contest 092 - Two Sequences
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given two integer sequences, each of length <var>N</var>: <var>a_1, ..., a_N</var> and <var>b_1, ..., b_N</var>.</p> <p>There are <var>N^2</var> ways to choose two integers <var>i</var> and <var>j</var> such that <var>1 \leq i, j \leq N</var>. For each of these <var>N^2</var> pairs, we will compute <var>a_i + b_j</var> and write it on a sheet of paper. That is, we will write <var>N^2</var> integers in total.</p> <p>Compute the XOR of these <var>N^2</var> integers.</p> <p><details> <summary style="display:list-item">Definition of XOR</summary></details></p> <p>The XOR of integers <var>c_1, c_2, ..., c_m</var> is defined as follows:</p> <ul> <li>Let the XOR be <var>X</var>. In the binary representation of <var>X</var>, the digit in the <var>2^k</var>'s place (<var>0 \leq k</var>; <var>k</var> is an integer) is <var>1</var> if there are an odd number of integers among <var>c_1, c_2, ...c_m</var> whose binary representation has <var>1</var> in the <var>2^k</var>'s place, and <var>0</var> if that number is even.</li> </ul> <p>For example, let us compute the XOR of <var>3</var> and <var>5</var>. The binary representation of <var>3</var> is <var>011</var>, and the binary representation of <var>5</var> is <var>101</var>, thus the XOR has the binary representation <var>110</var>, that is, the XOR is <var>6</var>.</p> <p></p></section></div></span>
[ [ "", "" ] ]
p03413
AtCoder Regular Contest 092 - Both Sides Merger
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You have an integer sequence of length <var>N</var>: <var>a_1, a_2, ..., a_N</var>.</p> <p>You repeatedly perform the following operation until the length of the sequence becomes <var>1</var>:</p> <ul> <li>First, choose an element of the sequence.</li> <li>If that element is at either end of the sequence, delete the element.</li> <li>If that element is not at either end of the sequence, replace the element with the sum of the two elements that are adjacent to it. Then, delete those two elements.</li> </ul> <p>You would like to maximize the final element that remains in the sequence.</p> <p>Find the maximum possible value of the final element, and the way to achieve it.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>2 \leq N \leq 1000</var></li> <li><var>|a_i| \leq 10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><ul> <li>In the first line, print the maximum possible value of the final element in the sequence.</li> <li>In the second line, print the number of operations that you perform.</li> <li>In the <var>(2+i)</var>-th line, if the element chosen in the <var>i</var>-th operation is the <var>x</var>-th element from the left in the sequence at that moment, print <var>x</var>.</li> <li>If there are multiple ways to achieve the maximum value of the final element, any of them may be printed.</li> </ul> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 1 4 3 7 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>11 3 1 4 2 </pre> <p>The sequence would change as follows:</p> <ul> <li>After the first operation: <var>4, 3, 7, 5</var></li> <li>After the second operation: <var>4, 3, 7</var></li> <li>After the third operation: <var>11(4+7)</var></li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 100 100 -1 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>200 2 3 1 </pre> <ul> <li>After the first operation: <var>100, 200(100+100)</var></li> <li>After the second operation: <var>200</var></li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 -1 -2 -3 1 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>4 3 2 1 2 </pre> <ul> <li>After the first operation: <var>-4, 1, 2, 3</var></li> <li>After the second operation: <var>1, 2, 3</var></li> <li>After the third operation: <var>4</var></li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>9 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>5000000000 4 2 2 2 2 </pre></section> </div> </span>
[ [ "5\n1 4 3 7 5\n", "5\n1 4 3 7 5\n" ] ]
p03414
AtCoder Regular Contest 092 - Two Faced Edges
<span class="lang-en"> <p>Score : <var>1100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a directed graph with <var>N</var> vertices and <var>M</var> edges. The vertices are numbered <var>1, 2, ..., N</var>, and the edges are numbered <var>1, 2, ..., M</var>. Edge <var>i</var> points from Vertex <var>a_i</var> to Vertex <var>b_i</var>.</p> <p>For each edge, determine whether the reversion of that edge would change the number of the strongly connected components in the graph.</p> <p>Here, the reversion of Edge <var>i</var> means deleting Edge <var>i</var> and then adding a new edge that points from Vertex <var>b_i</var> to Vertex <var>a_i</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 1000</var></li> <li><var>1 \leq M \leq 200,000</var></li> <li><var>1 \leq a_i, b_i \leq N</var></li> <li><var>a_i \neq b_i</var></li> <li>If <var>i \neq j</var>, then <var>a_i \neq a_j</var> or <var>b_i \neq b_j</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>a_1</var> <var>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 <var>M</var> lines. In the <var>i</var>-th line, if the reversion of Edge <var>i</var> would change the number of the strongly connected components in the graph, print <code>diff</code>; if it would not, print <code>same</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 1 2 1 3 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>same diff same </pre> <p>The number of the strongly connected components is <var>3</var> without reversion of edges, but it will become <var>1</var> if Edge <var>2</var> is reversed.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 2 1 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>diff diff </pre> <p>Reversion of an edge may result in multiple edges in the graph.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 9 3 2 3 1 4 1 4 2 3 5 5 3 3 4 1 2 2 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>same same same same same diff diff diff diff </pre></section> </div> </span>
[ [ "3 3\n1 2\n1 3\n2 3\n", "3 3\n1 2\n1 3\n2 3\n" ] ]
p03415
AtCoder Beginner Contest 090 - Diagonal String
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a <var>3×3</var> square grid, where each square contains a lowercase English letters. The letter in the square at the <var>i</var>-th row from the top and <var>j</var>-th column from the left is <var>c_{ij}</var>.</p> <p>Print the string of length <var>3</var> that can be obtained by concatenating the letters in the squares on the diagonal connecting the top-left and bottom-right corner of the grid, from the top-left to bottom-right.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>Input consists of lowercase English letters.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>c_{11}c_{12}c_{13}</var> <var>c_{21}c_{22}c_{23}</var> <var>c_{31}c_{32}c_{33}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the string of length <var>3</var> that can be obtained by concatenating the letters on the diagonal connecting the top-left and bottom-right corner of the grid, from the top-left to bottom-right.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>ant obe rec </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>abc </pre> <p>The letters in the squares on the diagonal connecting the top-left and bottom-right corner of the grid are <code>a</code>, <code>b</code> and <code>c</code> from top-right to bottom-left. Concatenate these letters and print <code>abc</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>edu cat ion </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>ean </pre></section> </div> </span>
[ [ "ant\nobe\nrec\n", "ant\nobe\nrec\n" ] ]
p03416
AtCoder Beginner Contest 090 - Palindromic Numbers
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Find the number of <em>palindromic numbers</em> among the integers between <var>A</var> and <var>B</var> (inclusive). Here, a palindromic number is a positive integer whose string representation in base <var>10</var> (without leading zeros) reads the same forward and backward.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>10000 \leq A \leq B \leq 99999</var></li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of <em>palindromic numbers</em> among the integers between <var>A</var> and <var>B</var> (inclusive).</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>11009 11332 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>There are four integers that satisfy the conditions: <var>11011</var>, <var>11111</var>, <var>11211</var> and <var>11311</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>31415 92653 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>612 </pre></section> </div> </span>
[ [ "11009 11332\n", "11009 11332\n" ] ]
p03417
AtCoder Beginner Contest 090 - Flip Flip and Flip......
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive <var>N</var> rows and <var>M</var> columns, and a card is placed in each square in this region. The front and back sides of these cards can be distinguished, and initially every card faces up.</p> <p>We will perform the following operation once for each square contains a card:</p> <ul> <li>For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square.</li> </ul> <p>It can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed. Find the number of cards that face down after all the operations.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N,M \leq 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>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of cards that face down after all the operations.</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>0 </pre> <p>We will flip every card in any of the four operations. Thus, after all the operations, all cards face up.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>5 </pre> <p>After all the operations, all cards except at both ends face down.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>314 1592 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>496080 </pre></section> </div> </span>
[ [ "2 2\n", "2 2\n" ] ]
p03418
AtCoder Beginner Contest 090 - Remainder Reminder
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi had a pair of two positive integers not exceeding <var>N</var>, <var>(a,b)</var>, which he has forgotten. He remembers that the remainder of <var>a</var> divided by <var>b</var> was greater than or equal to <var>K</var>. Find the number of possible pairs that he may have had.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 10^5</var></li> <li><var>0 \leq K \leq N-1</var></li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of possible pairs that he may have had.</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>7 </pre> <p>There are seven possible pairs: <var>(2,3),(5,3),(2,4),(3,4),(2,5),(3,5)</var> and <var>(4,5)</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 0 </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>31415 9265 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>287927211 </pre></section> </div> </span>
[ [ "5 2\n", "5 2\n" ] ]
p03419
AtCoder Regular Contest 091 - Flip Flip and Flip......
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive <var>N</var> rows and <var>M</var> columns, and a card is placed in each square in this region. The front and back sides of these cards can be distinguished, and initially every card faces up.</p> <p>We will perform the following operation once for each square contains a card:</p> <ul> <li>For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square.</li> </ul> <p>It can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed. Find the number of cards that face down after all the operations.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N,M \leq 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>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of cards that face down after all the operations.</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>0 </pre> <p>We will flip every card in any of the four operations. Thus, after all the operations, all cards face up.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>5 </pre> <p>After all the operations, all cards except at both ends face down.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>314 1592 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>496080 </pre></section> </div> </span>
[ [ "2 2\n", "2 2\n" ] ]
p03420
AtCoder Regular Contest 091 - Remainder Reminder
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi had a pair of two positive integers not exceeding <var>N</var>, <var>(a,b)</var>, which he has forgotten. He remembers that the remainder of <var>a</var> divided by <var>b</var> was greater than or equal to <var>K</var>. Find the number of possible pairs that he may have had.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 10^5</var></li> <li><var>0 \leq K \leq N-1</var></li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of possible pairs that he may have had.</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>7 </pre> <p>There are seven possible pairs: <var>(2,3),(5,3),(2,4),(3,4),(2,5),(3,5)</var> and <var>(4,5)</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 0 </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>31415 9265 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>287927211 </pre></section> </div> </span>
[ [ "5 2\n", "5 2\n" ] ]
p03421
AtCoder Regular Contest 091 - LISDL
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Determine if there exists a sequence obtained by permuting <var>1,2,...,N</var> that satisfies the following conditions:</p> <ul> <li>The length of its longest increasing subsequence is <var>A</var>.</li> <li>The length of its longest decreasing subsequence is <var>B</var>.</li> </ul> <p>If it exists, construct one such sequence.</p> </section> </div> <div class="part"> <section> <h3>Notes</h3><p>A subsequence of a sequence <var>P</var> is a sequence that can be obtained by extracting some of the elements in <var>P</var> without changing the order.</p> <p>A longest increasing subsequence of a sequence <var>P</var> is a sequence with the maximum length among the subsequences of <var>P</var> that are monotonically increasing.</p> <p>Similarly, a longest decreasing subsequence of a sequence <var>P</var> is a sequence with the maximum length among the subsequences of <var>P</var> that are monotonically decreasing.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N,A,B \leq 3\times 10^5</var></li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there are no sequences that satisfy the conditions, print <code>-1</code>.</p> <p>Otherwise, print <var>N</var> integers. The <var>i</var>-th integer should be the <var>i</var>-th element of the sequence that you constructed.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 4 1 5 3 </pre> <p>One longest increasing subsequence of this sequence is <var>{2,4,5}</var>, and one longest decreasing subsequence of it is <var>{4,3}</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 7 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 2 3 4 5 6 7 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>300000 300000 300000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>-1 </pre></section> </div> </span>
[ [ "5 3 2\n", "5 3 2\n" ] ]
p03422
AtCoder Regular Contest 091 - Strange Nim
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi and Aoki are playing a stone-taking game. Initially, there are <var>N</var> piles of stones, and the <var>i</var>-th pile contains <var>A_i</var> stones and has an associated integer <var>K_i</var>.</p> <p>Starting from Takahashi, Takahashi and Aoki take alternate turns to perform the following operation:</p> <ul> <li>Choose a pile. If the <var>i</var>-th pile is selected and there are <var>X</var> stones left in the pile, remove some number of stones between <var>1</var> and <var>floor(X/K_i)</var> (inclusive) from the pile.</li> </ul> <p>The player who first becomes unable to perform the operation loses the game. Assuming that both players play optimally, determine the winner of the game. Here, <var>floor(x)</var> represents the largest integer not greater than <var>x</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 200</var></li> <li><var>1 \leq A_i,K_i \leq 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>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>K_1</var> <var>:</var> <var>A_N</var> <var>K_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If Takahashi will win, print <code>Takahashi</code>; if Aoki will win, print <code>Aoki</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 5 2 3 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Aoki </pre> <p>Initially, from the first pile at most <var>floor(5/2)=2</var> stones can be removed at a time, and from the second pile at most <var>floor(3/3)=1</var> stone can be removed at a time.</p> <ul> <li>If Takahashi first takes two stones from the first pile, from the first pile at most <var>floor(3/2)=1</var> stone can now be removed at a time, and from the second pile at most <var>floor(3/3)=1</var> stone can be removed at a time.</li> <li>Then, if Aoki takes one stone from the second pile, from the first pile at most <var>floor(3/2)=1</var> stone can be removed at a time, and from the second pile no more stones can be removed (since <var>floor(2/3)=0</var>).</li> <li>Then, if Takahashi takes one stone from the first pile, from the first pile at most <var>floor(2/2)=1</var> stone can now be removed at a time, and from the second pile no more stones can be removed.</li> <li>Then, if Aoki takes one stone from the first pile, from the first pile at most <var>floor(1/2)=0</var> stones can now be removed at a time, and from the second pile no more stones can be removed.</li> </ul> <p>No more operation can be performed, thus Aoki wins. If Takahashi plays differently, Aoki can also win by play accordingly.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 3 2 4 3 5 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>Takahashi </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 28 3 16 4 19 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Aoki </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>4 3141 59 26535 897 93 23 8462 64 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>Takahashi </pre></section> </div> </span>
[ [ "2\n5 2\n3 3\n", "2\n5 2\n3 3\n" ] ]
p03423
AtCoder Beginner Contest 089 - Grouping 2
<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> students in a school.</p> <p>We will divide these students into some groups, and in each group they will discuss some themes.</p> <p>You think that groups consisting of two or less students cannot have an effective discussion, so you want to have as many groups consisting of three or more students as possible.</p> <p>Divide the students so that the number of groups consisting of three or more students is maximized.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 1000</var></li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If you can form at most <var>x</var> groups consisting of three or more students, print <var>x</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>For example, you can form a group of three students and another of five students.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Sometimes you cannot form any group consisting of three or more students, regardless of how you divide the students.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>3 </pre></section> </div> </span>
[ [ "8\n", "8\n" ] ]
p03424
AtCoder Beginner Contest 089 - Hina Arare
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>In Japan, people make offerings called <em>hina arare</em>, colorful crackers, on March <var>3</var>.</p> <p>We have a bag that contains <var>N</var> hina arare. (From here, we call them arare.)</p> <p>It is known that the bag either contains arare in three colors: pink, white and green, or contains arare in four colors: pink, white, green and yellow.</p> <p>We have taken out the arare in the bag one by one, and the color of the <var>i</var>-th arare was <var>S_i</var>, where colors are represented as follows - pink: <code>P</code>, white: <code>W</code>, green: <code>G</code>, yellow: <code>Y</code>.</p> <p>If the number of colors of the arare in the bag was three, print <code>Three</code>; if the number of colors was four, print <code>Four</code>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 100</var></li> <li><var>S_i</var> is <code>P</code>, <code>W</code>, <code>G</code> or <code>Y</code>.</li> <li>There always exist <var>i</var>, <var>j</var> and <var>k</var> such that <var>S_i=</var><code>P</code>, <var>S_j=</var><code>W</code> and <var>S_k=</var><code>G</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>S_1</var> <var>S_2</var> <var>...</var> <var>S_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If the number of colors of the arare in the bag was three, print <code>Three</code>; if the number of colors was four, print <code>Four</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 G W Y P Y W </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Four </pre> <p>The bag contained arare in four colors, so you should print <code>Four</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>9 G W W G P W P G G </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>Three </pre> <p>The bag contained arare in three colors, so you should print <code>Three</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>8 P Y W G Y W Y Y </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Four </pre></section> </div> </span>
[ [ "6\nG W Y P Y W\n", "6\nG W Y P Y W\n" ] ]
p03425
AtCoder Beginner Contest 089 - March
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> people. The name of the <var>i</var>-th person is <var>S_i</var>.</p> <p>We would like to choose three people so that the following conditions are met:</p> <ul> <li>The name of every chosen person begins with <code>M</code>, <code>A</code>, <code>R</code>, <code>C</code> or <code>H</code>.</li> <li>There are no multiple people whose names begin with the same letter.</li> </ul> <p>How many such ways are there to choose three people, disregarding order?</p> <p>Note that the answer may not fit into a <var>32</var>-bit integer type.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 10^5</var></li> <li><var>S_i</var> consists of uppercase English letters.</li> <li><var>1 \leq |S_i| \leq 10</var></li> <li><var>S_i \neq S_j (i \neq j)</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>S_1</var> <var>:</var> <var>S_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there are <var>x</var> ways to choose three people so that the given conditions are met, print <var>x</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 MASHIKE RUMOI OBIRA HABORO HOROKANAI </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We can choose three people with the following names:</p> <ul> <li> <p><code>MASHIKE</code>, <code>RUMOI</code>, <code>HABORO</code></p> </li> <li> <p><code>MASHIKE</code>, <code>RUMOI</code>, <code>HOROKANAI</code></p> </li> </ul> <p>Thus, we have two ways.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 ZZ ZZZ Z ZZZZZZZZZZ </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Note that there may be no ways to choose three people so that the given conditions are met.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 CHOKUDAI RNG MAKOTO AOKI RINGO </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>7 </pre></section> </div> </span>
[ [ "5\nMASHIKE\nRUMOI\nOBIRA\nHABORO\nHOROKANAI\n", "5\nMASHIKE\nRUMOI\nOBIRA\nHABORO\nHOROKANAI\n" ] ]
p03426
AtCoder Beginner Contest 089 - Practical Skill Test
<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. The square at the <var>i</var>-th row and the <var>j</var>-th column will be called Square <var>(i,j)</var>.</p> <p>The integers from <var>1</var> through <var>H×W</var> are written throughout the grid, and the integer written in Square <var>(i,j)</var> is <var>A_{i,j}</var>.</p> <p>You, a magical girl, can teleport a piece placed on Square <var>(i,j)</var> to Square <var>(x,y)</var> by consuming <var>|x-i|+|y-j|</var> magic points.</p> <p>You now have to take <var>Q</var> practical tests of your ability as a magical girl.</p> <p>The <var>i</var>-th test will be conducted as follows:</p> <ul> <li> <p>Initially, a piece is placed on the square where the integer <var>L_i</var> is written.</p> </li> <li> <p>Let <var>x</var> be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer <var>x+D</var> is written, as long as <var>x</var> is not <var>R_i</var>. The test ends when <var>x=R_i</var>.</p> </li> <li> <p>Here, it is guaranteed that <var>R_i-L_i</var> is a multiple of <var>D</var>.</p> </li> </ul> <p>For each test, find the sum of magic points consumed during that test.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq H,W \leq 300</var></li> <li><var>1 \leq D \leq H×W</var></li> <li><var>1 \leq A_{i,j} \leq H×W</var></li> <li><var>A_{i,j} \neq A_{x,y} ((i,j) \neq (x,y))</var></li> <li><var>1 \leq Q \leq 10^5</var></li> <li><var>1 \leq L_i \leq R_i \leq H×W</var></li> <li><var>(R_i-L_i)</var> is a multiple of <var>D</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>D</var> <var>A_{1,1}</var> <var>A_{1,2}</var> <var>...</var> <var>A_{1,W}</var> <var>:</var> <var>A_{H,1}</var> <var>A_{H,2}</var> <var>...</var> <var>A_{H,W}</var> <var>Q</var> <var>L_1</var> <var>R_1</var> <var>:</var> <var>L_Q</var> <var>R_Q</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>For each test, print the sum of magic points consumed during that test.</p> <p>Output should be in the order the tests are conducted.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 2 1 4 3 2 5 7 8 9 6 1 4 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>5 </pre> <ul> <li> <p><var>4</var> is written in Square <var>(1,2)</var>.</p> </li> <li> <p><var>6</var> is written in Square <var>(3,3)</var>.</p> </li> <li> <p><var>8</var> is written in Square <var>(3,1)</var>.</p> </li> </ul> <p>Thus, the sum of magic points consumed during the first test is <var>(|3-1|+|3-2|)+(|3-3|+|1-3|)=5</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 2 3 3 7 1 4 5 2 6 8 2 2 2 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 0 </pre> <p>Note that there may be a test where the piece is not moved at all, and there may be multiple identical tests.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 5 4 13 25 7 15 17 16 22 20 2 9 14 11 12 1 19 10 6 23 8 18 3 21 5 24 4 3 13 13 2 10 13 13 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 5 0 </pre></section> </div> </span>
[ [ "3 3 2\n1 4 3\n2 5 7\n8 9 6\n1\n4 8\n", "3 3 2\n1 4 3\n2 5 7\n8 9 6\n1\n4 8\n" ] ]
p03427
AtCoder Grand Contest 021 - Digit Sum 2
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Find the maximum possible sum of the digits (in base <var>10</var>) of a positive integer not greater than <var>N</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1\leq N \leq 10^{16}</var></li> <li><var>N</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum possible sum of the digits (in base <var>10</var>) of a positive integer not greater than <var>N</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>18 </pre> <p>For example, the sum of the digits in <var>99</var> is <var>18</var>, which turns out to be the maximum value.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>9995 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>35 </pre> <p>For example, the sum of the digits in <var>9989</var> is <var>35</var>, which turns out to be the maximum value.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3141592653589793 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>137 </pre></section> </div> </span>
[ [ "100\n", "100\n" ] ]
p03428
AtCoder Grand Contest 021 - Holes
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> holes in a two-dimensional plane. The coordinates of the <var>i</var>-th hole are <var>(x_i,y_i)</var>.</p> <p>Let <var>R=10^{10^{10^{10}}}</var>. Ringo performs the following operation:</p> <ul> <li>Randomly choose a point from the interior of a circle of radius <var>R</var> centered at the origin, and put Snuke there. Snuke will move to the hole with the smallest Euclidean distance from the point, and fall into that hole. If there are multiple such holes, the hole with the smallest index will be chosen.</li> </ul> <p>For every <var>i</var> <var>(1 \leq i \leq N)</var>, find the probability that Snuke falls into the <var>i</var>-th hole.</p> <p>Here, the operation of randomly choosing a point from the interior of a circle of radius <var>R</var> is defined as follows:</p> <ul> <li>Pick two real numbers <var>x</var> and <var>y</var> independently according to uniform distribution on <var>[-R,R]</var>.</li> <li>If <var>x^2+y^2\leq R^2</var>, the point <var>(x,y)</var> is chosen. Otherwise, repeat picking the real numbers <var>x,y</var> until the condition is met.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 100</var></li> <li><var>|x_i|,|y_i| \leq 10^6(1\leq i\leq N)</var></li> <li>All given points are pairwise distinct.</li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>x_1</var> <var>y_1</var> <var>:</var> <var>x_N</var> <var>y_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>N</var> real numbers. The <var>i</var>-th real number must represent the probability that Snuke falls into the <var>i</var>-th hole.</p> <p>The output will be judged correct when, for all output values, the absolute or relative error is at most <var>10^{-5}</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 0 0 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>0.5 0.5 </pre> <p>If Ringo put Snuke in the region <var>x+y\leq 1</var>, Snuke will fall into the first hole. The probability of this happening is very close to <var>0.5</var>. Otherwise, Snuke will fall into the second hole, the probability of which happening is also very close to <var>0.5</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 0 0 2 8 4 5 2 6 3 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0.43160120892732328768 0.03480224363653196956 0.13880483535586193855 0.00000000000000000000 0.39479171208028279727 </pre></section> </div> </span>
[ [ "2\n0 0\n1 1\n", "2\n0 0\n1 1\n" ] ]
p03429
AtCoder Grand Contest 021 - Tiling
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi has an <var>N \times M</var> grid, with <var>N</var> horizontal rows and <var>M</var> vertical columns. Determine if we can place <var>A</var> <var>1 \times 2</var> tiles (<var>1</var> vertical, <var>2</var> horizontal) and <var>B</var> <var>2 \times 1</var> tiles (<var>2</var> vertical, <var>1</var> horizontal) satisfying the following conditions, and construct one arrangement of the tiles if it is possible:</p> <ul> <li>All the tiles must be placed on the grid.</li> <li>Tiles must not stick out of the grid, and no two different tiles may intersect.</li> <li>Neither the grid nor the tiles may be rotated.</li> <li>Every tile completely covers exactly two squares.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N,M \leq 1000</var></li> <li><var>0 \leq A,B \leq 500000</var></li> <li><var>N</var>, <var>M</var>, <var>A</var> and <var>B</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If it is impossible to place all the tiles, print <code>NO</code>. Otherwise, print the following:</p> <pre>YES <var>c_{11}...c_{1M}</var> <var>:</var> <var>c_{N1}...c_{NM}</var> </pre> <p>Here, <var>c_{ij}</var> must be one of the following characters: <code>.</code>, <code>&lt;</code>, <code>&gt;</code>, <code>^</code> and <code>v</code>. Represent an arrangement by using each of these characters as follows:</p> <ul> <li>When <var>c_{ij}</var> is <code>.</code>, it indicates that the square at the <var>i</var>-th row and <var>j</var>-th column is empty;</li> <li>When <var>c_{ij}</var> is <code>&lt;</code>, it indicates that the square at the <var>i</var>-th row and <var>j</var>-th column is covered by the left half of a <var>1 \times 2</var> tile;</li> <li>When <var>c_{ij}</var> is <code>&gt;</code>, it indicates that the square at the <var>i</var>-th row and <var>j</var>-th column is covered by the right half of a <var>1 \times 2</var> tile;</li> <li>When <var>c_{ij}</var> is <code>^</code>, it indicates that the square at the <var>i</var>-th row and <var>j</var>-th column is covered by the top half of a <var>2 \times 1</var> tile;</li> <li>When <var>c_{ij}</var> is <code>v</code>, it indicates that the square at the <var>i</var>-th row and <var>j</var>-th column is covered by the bottom half of a <var>2 \times 1</var> tile.</li> </ul> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 4 4 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>YES &lt;&gt;&lt;&gt; ^&lt;&gt;^ v&lt;&gt;v </pre> <p>This is one example of a way to place four <var>1 \times 2</var> tiles and three <var>2 \times 1</var> tiles on a <var>3 \times 4</var> grid.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 5 5 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>YES &lt;&gt;..^ ^.&lt;&gt;v v&lt;&gt;.^ &lt;&gt;&lt;&gt;v </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>7 9 20 20 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>NO </pre></section> </div> </span>
[ [ "3 4 4 2\n", "3 4 4 2\n" ] ]
p03430
AtCoder Grand Contest 021 - Reversed LCS
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi has decided to give a string to his mother.</p> <p>The <em>value</em> of a string <var>T</var> is the length of the longest common subsequence of <var>T</var> and <var>T'</var>, where <var>T'</var> is the string obtained by reversing <var>T</var>. That is, the value is the longest length of the following two strings that are equal: a subsequence of <var>T</var> (possibly non-contiguous), and a subsequence of <var>T'</var> (possibly non-contiguous).</p> <p>Takahashi has a string <var>S</var>. He wants to give her mother a string of the highest possible value, so he would like to change at most <var>K</var> characters in <var>S</var> to any other characters in order to obtain a string of the highest possible value. Find the highest possible value achievable.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq |S| \leq 300</var></li> <li><var>0 \leq K \leq |S|</var></li> <li><var>S</var> consists of lowercase English letters.</li> <li><var>K</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>S</var> <var>K</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the highest possible value achievable.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>abcabcabc 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>7 </pre> <p>Changing the first character to <code>c</code> results in <code>cbcabcabc</code>. Let this tring be <var>T</var>, then one longest common subsequence of <var>T</var> and <var>T'</var> is <code>cbabcbc</code>, whose length is <var>7</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>atcodergrandcontest 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>15 </pre></section> </div> </span>
[ [ "abcabcabc\n1\n", "abcabcabc\n1\n" ] ]
p03431
AtCoder Grand Contest 021 - Ball Eat Chameleons
<span class="lang-en"> <p>Score : <var>1200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>In Republic of AtCoder, Snuke Chameleons (Family: Chamaeleonidae, Genus: Bartaberia) are very popular pets. Ringo keeps <var>N</var> Snuke Chameleons in a cage.</p> <p>A Snuke Chameleon that has not eaten anything is blue. It changes its color according to the following rules:</p> <ul> <li>A Snuke Chameleon that is blue will change its color to red when the number of red balls it has eaten becomes strictly larger than the number of blue balls it has eaten.</li> <li>A Snuke Chameleon that is red will change its color to blue when the number of blue balls it has eaten becomes strictly larger than the number of red balls it has eaten.</li> </ul> <p>Initially, every Snuke Chameleon had not eaten anything. Ringo fed them by repeating the following process <var>K</var> times:</p> <ul> <li>Grab either a red ball or a blue ball.</li> <li>Throw that ball into the cage. Then, one of the chameleons eats it.</li> </ul> <p>After Ringo threw in <var>K</var> balls, all the chameleons were red. We are interested in the possible ways Ringo could have thrown in <var>K</var> balls. How many such ways are there? Find the count modulo <var>998244353</var>. Here, two ways to throw in balls are considered different when there exists <var>i</var> such that the color of the ball that are thrown in the <var>i</var>-th throw is different.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N,K \leq 5 \times 10^5</var></li> <li><var>N</var> and <var>K</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the possible ways Ringo could have thrown in <var>K</var> balls, modulo <var>998244353</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>7 </pre> <p>We will use <code>R</code> to represent a red ball, and <code>B</code> to represent a blue ball. There are seven ways to throw in balls that satisfy the condition: <code>BRRR</code>, <code>RBRB</code>, <code>RBRR</code>, <code>RRBB</code>, <code>RRBR</code>, <code>RRRB</code> and <code>RRRR</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>57 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>8 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>8 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>46 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>123456 234567 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>857617983 </pre></section> </div> </span>
[ [ "2 4\n", "2 4\n" ] ]
p03432
AtCoder Grand Contest 021 - Trinity
<span class="lang-en"> <p>Score : <var>1800</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have an <var>N \times M</var> grid. The square at the <var>i</var>-th row and <var>j</var>-th column will be denoted as <var>(i,j)</var>. Particularly, the top-left square will be denoted as <var>(1,1)</var>, and the bottom-right square will be denoted as <var>(N,M)</var>. Takahashi painted some of the squares (possibly zero) black, and painted the other squares white.</p> <p>We will define an integer sequence <var>A</var> of length <var>N</var>, and two integer sequences <var>B</var> and <var>C</var> of length <var>M</var> each, as follows:</p> <ul> <li><var>A_i(1\leq i\leq N)</var> is the minimum <var>j</var> such that <var>(i,j)</var> is painted black, or <var>M+1</var> if it does not exist.</li> <li><var>B_i(1\leq i\leq M)</var> is the minimum <var>k</var> such that <var>(k,i)</var> is painted black, or <var>N+1</var> if it does not exist.</li> <li><var>C_i(1\leq i\leq M)</var> is the maximum <var>k</var> such that <var>(k,i)</var> is painted black, or <var>0</var> if it does not exist.</li> </ul> <p>How many triples <var>(A,B,C)</var> can occur? Find the count modulo <var>998244353</var>.</p> </section> </div> <div class="part"> <section> <h3>Notice</h3><p><font color="Red">In this problem, the length of your source code must be at most <var>20000</var> B. Note that we will invalidate submissions that exceed the maximum length.</font></p> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 8000</var></li> <li><var>1 \leq M \leq 200</var></li> <li><var>N</var> and <var>M</var> are integers.</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li><var>1500</var> points will be awarded for passing the test set satisfying <var>N\leq 300</var>.</li> </ul> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of triples <var>(A,B,C)</var>, modulo <var>998244353</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>64 </pre> <p>Since <var>N=2</var>, given <var>B_i</var> and <var>C_i</var>, we can uniquely determine the arrangement of black squares in each column. For each <var>i</var>, there are four possible pairs <var>(B_i,C_i)</var>: <var>(1,1)</var>, <var>(1,2)</var>, <var>(2,2)</var> and <var>(3,0)</var>. Thus, the answer is <var>4^M=64</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2588 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>17 13 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>229876268 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>5000 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>57613837 </pre></section> </div> </span>
[ [ "2 3\n", "2 3\n" ] ]
p03433
AtCoder Beginner Contest 088 - Infinite Coins
<span class="lang-en"> <p>Score: <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>E869120 has <var>A</var> <var>1</var>-yen coins and infinitely many <var>500</var>-yen coins.<br/> Determine if he can pay exactly <var>N</var> yen using only these coins.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3> <ul> <li><var>N</var> is an integer between <var>1</var> and <var>10000</var> (inclusive).</li> <li><var>A</var> is an integer between <var>0</var> and <var>1000</var> (inclusive).</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3> <p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3> <p>If E869120 can pay exactly <var>N</var> yen using only his <var>1</var>-yen and <var>500</var>-yen coins, print <code>Yes</code>; otherwise, print <code>No</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2018 218 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes </pre> <p>We can pay <var>2018</var> yen with four <var>500</var>-yen coins and <var>18</var> <var>1</var>-yen coins, so the answer is <code>Yes</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2763 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>No </pre> <p>When we have no <var>1</var>-yen coins, we can only pay a multiple of <var>500</var> yen using only <var>500</var>-yen coins. Since <var>2763</var> is not a multiple of <var>500</var>, we cannot pay this amount.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>37 514 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Yes </pre></section> </div> </span>
[ [ "2018\n218\n", "2018\n218\n" ] ]
p03434
AtCoder Beginner Contest 088 - Card Game for Two
<span class="lang-en"> <p>Score: <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>We have <var>N</var> cards. A number <var>a_i</var> is written on the <var>i</var>-th card.<br/> Alice and Bob will play a game using these cards. In this game, Alice and Bob alternately take one card. Alice goes first.<br/> The game ends when all the cards are taken by the two players, and the score of each player is the sum of the numbers written on the cards he/she has taken. When both players take the optimal strategy to maximize their scores, find Alice's score minus Bob's score.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3> <ul> <li><var>N</var> is an integer between <var>1</var> and <var>100</var> (inclusive).</li> <li><var>a_i \ (1 \leq i \leq N)</var> is an integer between <var>1</var> and <var>100</var> (inclusive).</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3> <p>Input is given from Standard Input in the following format: </p> <pre><var>N</var> <var>a_1</var> <var>a_2</var> <var>a_3</var> <var>...</var> <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3> <p>Print Alice's score minus Bob's score when both players take the optimal strategy to maximize their scores.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>First, Alice will take the card with <var>3</var>. Then, Bob will take the card with <var>1</var>. The difference of their scores will be <var>3</var> - <var>1</var> = <var>2</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 2 7 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>5 </pre> <p>First, Alice will take the card with <var>7</var>. Then, Bob will take the card with <var>4</var>. Lastly, Alice will take the card with <var>2</var>. The difference of their scores will be <var>7</var> - <var>4</var> + <var>2</var> = <var>5</var>. The difference of their scores will be <var>3</var> - <var>1</var> = <var>2</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 20 18 2 18 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>18 </pre></section> </div> </span>
[ [ "2\n3 1\n", "2\n3 1\n" ] ]
p03435
AtCoder Beginner Contest 088 - Takahashi's Information
<span class="lang-en"> <p>Score: <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3> <p>We have a <var>3 \times 3</var> grid. A number <var>c_{i, j}</var> is written in the square <var>(i, j)</var>, where <var>(i, j)</var> denotes the square at the <var>i</var>-th row from the top and the <var>j</var>-th column from the left.<br/> According to Takahashi, there are six integers <var>a_1, a_2, a_3, b_1, b_2, b_3</var> whose values are fixed, and the number written in the square <var>(i, j)</var> is equal to <var>a_i + b_j</var>.<br/> Determine if he is correct. </p> </section> </div> <div class="part"> <section> <h3>Constraints</h3> <ul> <li><var>c_{i, j} \ (1 \leq i \leq 3, 1 \leq j \leq 3)</var> is an integer between <var>0</var> and <var>100</var> (inclusive).</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3> <p>Input is given from Standard Input in the following format:</p> <pre><var>c_{1,1}</var> <var>c_{1,2}</var> <var>c_{1,3}</var> <var>c_{2,1}</var> <var>c_{2,2}</var> <var>c_{2,3}</var> <var>c_{3,1}</var> <var>c_{3,2}</var> <var>c_{3,3}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3> <p>If Takahashi's statement is correct, print <code>Yes</code>; otherwise, print <code>No</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>1 0 1 2 1 2 1 0 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes </pre> <p>Takahashi is correct, since there are possible sets of integers such as: <var>a_1=0,a_2=1,a_3=0,b_1=1,b_2=0,b_3=1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 2 2 2 1 2 2 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>No </pre> <p>Takahashi is incorrect in this case.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>0 8 8 0 8 8 0 8 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Yes </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>1 8 6 2 9 7 0 7 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>No </pre></section> </div> </span>
[ [ "1 0 1\n2 1 2\n1 0 1\n", "1 0 1\n2 1 2\n1 0 1\n" ] ]
p03436
AtCoder Beginner Contest 088 - Grid Repainting
<span class="lang-en"> <p>Score: <var>400</var> points</p> <div class="part"> <section> <h3>Problem statement</h3> <p>We have an <var>H \times W</var> grid whose squares are painted black or white. The square at the <var>i</var>-th row from the top and the <var>j</var>-th column from the left is denoted as <var>(i, j)</var>.<br/> Snuke would like to play the following game on this grid. At the beginning of the game, there is a character called Kenus at square <var>(1, 1)</var>. The player repeatedly moves Kenus up, down, left or right by one square. The game is completed when Kenus reaches square <var>(H, W)</var> passing only white squares.<br/> Before Snuke starts the game, he can change the color of some of the white squares to black. However, he cannot change the color of square <var>(1, 1)</var> and <var>(H, W)</var>. Also, changes of color must all be carried out before the beginning of the game.<br/> When the game is completed, Snuke's score will be the number of times he changed the color of a square before the beginning of the game. Find the maximum possible score that Snuke can achieve, or print <var>-1</var> if the game cannot be completed, that is, Kenus can never reach square <var>(H, W)</var> regardless of how Snuke changes the color of the squares. </p> <p>The color of the squares are given to you as characters <var>s_{i, j}</var>. If square <var>(i, j)</var> is initially painted by white, <var>s_{i, j}</var> is <code>.</code>; if square <var>(i, j)</var> is initially painted by black, <var>s_{i, j}</var> is <code>#</code>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3> <ul> <li><var>H</var> is an integer between <var>2</var> and <var>50</var> (inclusive).</li> <li><var>W</var> is an integer between <var>2</var> and <var>50</var> (inclusive).</li> <li><var>s_{i, j}</var> is <code>.</code> or <code>#</code> <var>(1 \leq i \leq H, 1 \leq j \leq W)</var>.</li> <li><var>s_{1, 1}</var> and <var>s_{H, W}</var> are <code>.</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3> <p>Input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>s_{1, 1}s_{1, 2}s_{1, 3} ... s_{1, W}</var> <var>s_{2, 1}s_{2, 2}s_{2, 3} ... s_{2, W}</var> <var>:</var> <var>:</var> <var>s_{H, 1}s_{H, 2}s_{H, 3} ... s_{H, W}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3> <p>Print the maximum possible score that Snuke can achieve, or print <var>-1</var> if the game cannot be completed.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 ..# #.. ... </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The score <var>2</var> can be achieved by changing the color of squares as follows:</p> <p><img alt="Explanation of Sample 1" src="https://img.atcoder.jp/abc088/bc944898899615e35f898654b68cd517.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 37 ..................................... ...#...####...####..###...###...###.. ..#.#..#...#.##....#...#.#...#.#...#. ..#.#..#...#.#.....#...#.#...#.#...#. .#...#.#..##.#.....#...#.#.###.#.###. .#####.####..#.....#...#..##....##... .#...#.#...#.#.....#...#.#...#.#...#. .#...#.#...#.##....#...#.#...#.#...#. .#...#.####...####..###...###...###.. ..................................... </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>209 </pre></section> </div> </span>
[ [ "3 3\n..#\n#..\n...\n", "3 3\n..#\n#..\n...\n" ] ]
p03437
AtCoder Petrozavodsk Contest 001 - Two Integers
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given positive integers <var>X</var> and <var>Y</var>. If there exists a positive integer not greater than <var>10^{18}</var> that is a multiple of <var>X</var> but not a multiple of <var>Y</var>, choose one such integer and print it. If it does not exist, print <var>-1</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ X,Y ≤ 10^9</var></li> <li><var>X</var> and <var>Y</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>X</var> <var>Y</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print a positive integer not greater than <var>10^{18}</var> that is a multiple of <var>X</var> but not a multiple of <var>Y</var>, or print <var>-1</var> if it does not exist.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>8 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>16 </pre> <p>For example, <var>16</var> is a multiple of <var>8</var> but not a multiple of <var>6</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>-1 </pre> <p>A multiple of <var>3</var> is a multiple of <var>3</var>.</p></section> </div> </span>
[ [ "8 6\n", "8 6\n" ] ]
p03438
AtCoder Petrozavodsk Contest 001 - Two Arrays
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given two integer sequences of length <var>N</var>: <var>a_1,a_2,..,a_N</var> and <var>b_1,b_2,..,b_N</var>. Determine if we can repeat the following operation zero or more times so that the sequences <var>a</var> and <var>b</var> become equal.</p> <p>Operation: Choose two integers <var>i</var> and <var>j</var> (possibly the same) between <var>1</var> and <var>N</var> (inclusive), then perform the following two actions <strong>simultaneously</strong>:</p> <ul> <li>Add <var>2</var> to <var>a_i</var>.</li> <li>Add <var>1</var> to <var>b_j</var>.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 10</var> <var>000</var></li> <li><var>0 ≤ a_i,b_i ≤ 10^9</var> (<var>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>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>a_2</var> <var>..</var> <var>a_N</var> <var>b_1</var> <var>b_2</var> <var>..</var> <var>b_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If we can repeat the operation zero or more times so that the sequences <var>a</var> and <var>b</var> become equal, print <code>Yes</code>; otherwise, print <code>No</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 2 3 5 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes </pre> <p>For example, we can perform three operations as follows to do our job:</p> <ul> <li>First operation: <var>i=1</var> and <var>j=2</var>. Now we have <var>a = \{3,2,3\}</var>, <var>b = \{5,3,2\}</var>.</li> <li>Second operation: <var>i=1</var> and <var>j=2</var>. Now we have <var>a = \{5,2,3\}</var>, <var>b = \{5,4,2\}</var>.</li> <li>Third operation: <var>i=2</var> and <var>j=3</var>. Now we have <var>a = \{5,4,3\}</var>, <var>b = \{5,4,3\}</var>.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 3 1 4 1 5 2 7 1 8 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>5 2 7 1 8 2 3 1 4 1 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>No </pre></section> </div> </span>
[ [ "3\n1 2 3\n5 2 2\n", "3\n1 2 3\n5 2 2\n" ] ]
p03439
AtCoder Petrozavodsk Contest 001 - Vacant Seat
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p><font color="red"><strong>This is an interactive task.</strong></font></p> <p>Let <var>N</var> be an odd number at least <var>3</var>.</p> <p>There are <var>N</var> seats arranged in a circle. The seats are numbered <var>0</var> through <var>N-1</var>. For each <var>i</var> (<var>0 ≤ i ≤ N - 2</var>), Seat <var>i</var> and Seat <var>i + 1</var> are adjacent. Also, Seat <var>N - 1</var> and Seat <var>0</var> are adjacent.</p> <p>Each seat is either vacant, or oppupied by a man or a woman. However, no two adjacent seats are occupied by two people of the same sex. It can be shown that there is at least one empty seat where <var>N</var> is an odd number at least <var>3</var>.</p> <p>You are given <var>N</var>, but the states of the seats are not given. Your objective is to correctly guess the ID number of any one of the empty seats. To do so, you can repeatedly send the following query:</p> <ul> <li>Choose an integer <var>i</var> (<var>0 ≤ i ≤ N - 1</var>). If Seat <var>i</var> is empty, the problem is solved. Otherwise, you are notified of the sex of the person in Seat <var>i</var>.</li> </ul> <p>Guess the ID number of an empty seat by sending at most <var>20</var> queries.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>N</var> is an odd number.</li> <li><var>3 ≤ N ≤ 99</var> <var>999</var></li> </ul> </section> </div> <div class="part"> <section> <h3>Input and Output</h3><p>First, <var>N</var> is given from Standard Input in the following format:</p> <pre><var>N</var> </pre> <p>Then, you should send queries. A query should be printed to Standart Output in the following format. Print a newline at the end.</p> <pre><var>i</var> </pre> <p>The response to the query is given from Standard Input in the following format:</p> <pre><var>s</var> </pre> <p>Here, <var>s</var> is <code>Vacant</code>, <code>Male</code> or <code>Female</code>. Each of these means that Seat <var>i</var> is empty, occupied by a man and occupied by a woman, respectively.</p> </section> </div> <div class="part"> <section> <h3>Notice</h3><ul> <li>Flush Standard Output each time you print something. Failure to do so may result in <code>TLE</code>.</li> <li>Immediately terminate the program when <var>s</var> is <code>Vacant</code>. Otherwise, the verdict is indeterminate.</li> <li>The verdict is indeterminate if more than <var>20</var> queries or ill-formatted queries are sent.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input / Output 1</h3> <p>In this sample, <var>N = 3</var>, and Seat <var>0</var>, <var>1</var>, <var>2</var> are occupied by a man, occupied by a woman and vacant, respectively.</p> <table class="table-striped table-bordered table-condensed"> <thead> <tr><th width="200">Input</th><th width="200">Output</th></tr> </thead> <tbody> <tr><td>3</td><td></td></tr> <tr><td></td><td>0</td></tr> <tr><td>Male</td><td></td></tr> <tr><td></td><td>1</td></tr> <tr><td>Female</td><td></td></tr> <tr><td></td><td>2</td></tr> <tr><td>Vacant</td><td></td></tr> </tbody> </table> </section> </div> </span>
[ [ "", "" ] ]
p03440
AtCoder Petrozavodsk Contest 001 - Forest
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a forest with <var>N</var> vertices and <var>M</var> edges. The vertices are numbered <var>0</var> through <var>N-1</var>. The edges are given in the format <var>(x_i,y_i)</var>, which means that Vertex <var>x_i</var> and <var>y_i</var> are connected by an edge.</p> <p>Each vertex <var>i</var> has a value <var>a_i</var>. You want to add edges in the given forest so that the forest becomes connected. To add an edge, you choose two different vertices <var>i</var> and <var>j</var>, then span an edge between <var>i</var> and <var>j</var>. This operation costs <var>a_i + a_j</var> dollars, and afterward neither Vertex <var>i</var> nor <var>j</var> can be selected again.</p> <p>Find the minimum total cost required to make the forest connected, or print <code>Impossible</code> if it is impossible.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 100,000</var></li> <li><var>0 ≤ M ≤ N-1</var></li> <li><var>1 ≤ a_i ≤ 10^9</var></li> <li><var>0 ≤ x_i,y_i ≤ N-1</var></li> <li>The given graph is a forest.</li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>a_0</var> <var>a_1</var> <var>..</var> <var>a_{N-1}</var> <var>x_1</var> <var>y_1</var> <var>x_2</var> <var>y_2</var> <var>:</var> <var>x_M</var> <var>y_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum total cost required to make the forest connected, or print <code>Impossible</code> if it is impossible.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>7 5 1 2 3 4 5 6 7 3 0 4 0 1 2 1 3 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>7 </pre> <p>If we connect vertices <var>0</var> and <var>5</var>, the graph becomes connected, for the cost of <var>1 + 6 = 7</var> dollars.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 0 3 1 4 1 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>Impossible </pre> <p>We can't make the graph connected.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1 0 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> <p>The graph is already connected, so we do not need to add any edges.</p></section> </div> </span>
[ [ "7 5\n1 2 3 4 5 6 7\n3 0\n4 0\n1 2\n1 3\n5 6\n", "7 5\n1 2 3 4 5 6 7\n3 0\n4 0\n1 2\n1 3\n5 6\n" ] ]
p03441
AtCoder Petrozavodsk Contest 001 - Antennas on Tree
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a tree with <var>N</var> vertices. The vertices are numbered <var>0</var> through <var>N - 1</var>, and the <var>i</var>-th edge (<var>0 ≤ i &lt; N - 1</var>) comnnects Vertex <var>a_i</var> and <var>b_i</var>. For each pair of vertices <var>u</var> and <var>v</var> (<var>0 ≤ u, v &lt; N</var>), we define the distance <var>d(u, v)</var> as the number of edges in the path <var>u</var>-<var>v</var>.</p> <p>It is expected that one of the vertices will be invaded by aliens from outer space. Snuke wants to immediately identify that vertex when the invasion happens. To do so, he has decided to install an antenna on some vertices.</p> <p>First, he decides the number of antennas, <var>K</var> (<var>1 ≤ K ≤ N</var>). Then, he chooses <var>K</var> different vertices, <var>x_0</var>, <var>x_1</var>, ..., <var>x_{K - 1}</var>, on which he installs Antenna <var>0</var>, <var>1</var>, ..., <var>K - 1</var>, respectively. If Vertex <var>v</var> is invaded by aliens, Antenna <var>k</var> (<var>0 ≤ k &lt; K</var>) will output the distance <var>d(x_k, v)</var>. Based on these <var>K</var> outputs, Snuke will identify the vertex that is invaded. Thus, in order to identify the invaded vertex no matter which one is invaded, the following condition must hold:</p> <ul> <li>For each vertex <var>u</var> (<var>0 ≤ u &lt; N</var>), consider the vector <var>(d(x_0, u), ..., d(x_{K - 1}, u))</var>. These <var>N</var> vectors are distinct.</li> </ul> <p>Find the minumum value of <var>K</var>, the number of antennas, when the condition is satisfied.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N ≤ 10^5</var></li> <li><var>0 ≤ a_i, b_i &lt; N</var></li> <li>The given graph is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_0</var> <var>b_0</var> <var>a_1</var> <var>b_1</var> <var>:</var> <var>a_{N - 2}</var> <var>b_{N - 2}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minumum value of <var>K</var>, the number of antennas, when the condition is satisfied.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 0 1 0 2 0 3 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>For example, install an antenna on Vertex <var>1</var> and <var>3</var>. Then, the following five vectors are distinct:</p> <ul> <li><var>(d(1, 0), d(3, 0)) = (1, 1)</var></li> <li><var>(d(1, 1), d(3, 1)) = (0, 2)</var></li> <li><var>(d(1, 2), d(3, 2)) = (2, 2)</var></li> <li><var>(d(1, 3), d(3, 3)) = (2, 0)</var></li> <li><var>(d(1, 4), d(3, 4)) = (3, 1)</var></li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 0 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 </pre> <p>For example, install an antenna on Vertex <var>0</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>10 2 8 6 0 4 1 7 6 2 3 8 6 6 9 2 4 5 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>3 </pre> <p>For example, install an antenna on Vertex <var>0</var>, <var>4</var>, <var>9</var>.</p></section> </div> </span>
[ [ "5\n0 1\n0 2\n0 3\n3 4\n", "5\n0 1\n0 2\n0 3\n3 4\n" ] ]
p03442
AtCoder Petrozavodsk Contest 001 - XOR Tree
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a tree with <var>N</var> vertices. The vertices are numbered <var>0</var> through <var>N-1</var>, and the edges are numbered <var>1</var> through <var>N-1</var>. Edge <var>i</var> connects Vertex <var>x_i</var> and <var>y_i</var>, and has a value <var>a_i</var>. You can perform the following operation any number of times:</p> <ul> <li>Choose a simple path and a non-negative integer <var>x</var>, then for each edge <var>e</var> that belongs to the path, change <var>a_e</var> by executing <var>a_e ← a_e ⊕ x</var> (⊕ denotes XOR).</li> </ul> <p>Your objective is to have <var>a_e = 0</var> for all edges <var>e</var>. Find the minimum number of operations required to achieve it.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N ≤ 10^5</var></li> <li><var>0 ≤ x_i,y_i ≤ N-1</var></li> <li><var>0 ≤ a_i ≤ 15</var></li> <li>The given graph is a tree.</li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>x_1</var> <var>y_1</var> <var>a_1</var> <var>x_2</var> <var>y_2</var> <var>a_2</var> <var>:</var> <var>x_{N-1}</var> <var>y_{N-1}</var> <var>a_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Find the minimum number of operations required to achieve the objective.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 0 1 1 0 2 3 0 3 6 3 4 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>The objective can be achieved in three operations, as follows:</p> <ul> <li>First, choose the path connecting Vertex <var>1, 2</var>, and <var>x = 1</var>.</li> <li>Then, choose the path connecting Vertex <var>2, 3</var>, and <var>x = 2</var>.</li> <li>Lastly, choose the path connecting Vertex <var>0, 4</var>, and <var>x = 4</var>.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 1 0 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre></section> </div> </span>
[ [ "5\n0 1 1\n0 2 3\n0 3 6\n3 4 4\n", "5\n0 1 1\n0 2 3\n0 3 6\n3 4 4\n" ] ]
p03443
AtCoder Petrozavodsk Contest 001 - Colorful Doors
<span class="lang-en"> <p>Score : <var>2000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a bridge that connects the left and right banks of a river. There are <var>2 N</var> doors placed at different positions on this bridge, painted in some colors. The colors of the doors are represented by integers from <var>1</var> through <var>N</var>. For each <var>k</var> (<var>1 \leq k \leq N</var>), there are exactly two doors painted in Color <var>k</var>.</p> <p>Snuke decides to cross the bridge from the left bank to the right bank. He will keep on walking to the right, but the following event will happen while doing so:</p> <ul> <li>At the moment Snuke touches a door painted in Color <var>k</var> (<var>1 \leq k \leq N</var>), he teleports to the right side of the other door painted in Color <var>k</var>.</li> </ul> <p>It can be shown that he will eventually get to the right bank.</p> <p>For each <var>i</var> (<var>1 \leq i \leq 2 N - 1</var>), the section between the <var>i</var>-th and <var>(i + 1)</var>-th doors from the left will be referred to as Section <var>i</var>. After crossing the bridge, Snuke recorded whether or not he walked through Section <var>i</var>, for each <var>i</var> (<var>1 \leq i \leq 2 N - 1</var>). This record is given to you as a string <var>s</var> of length <var>2 N - 1</var>. For each <var>i</var> (<var>1 \leq i \leq 2 N - 1</var>), if Snuke walked through Section <var>i</var>, the <var>i</var>-th character in <var>s</var> is <code>1</code>; otherwise, the <var>i</var>-th character is <code>0</code>.</p> <div style="text-align: center;"> <img src="https://img.atcoder.jp/cookie/970b981380ffad7745008433034c0885.png"> <p>Figure: A possible arrangement of doors for Sample Input 3</p> </img></div> <p>Determine if there exists an arrangement of doors that is consistent with the record. If it exists, construct one such arrangement.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 10^5</var></li> <li><var>|s| = 2 N - 1</var></li> <li><var>s</var> consists of <code>0</code> and <code>1</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>s</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there is no arrangement of doors that is consistent with the record, print <code>No</code>. If there exists such an arrangement, print <code>Yes</code> in the first line, then print one such arrangement in the second line, in the following format:</p> <pre><var>c_1</var> <var>c_2</var> <var>...</var> <var>c_{2 N}</var> </pre> <p>Here, for each <var>i</var> (<var>1 \leq i \leq 2 N</var>), <var>c_i</var> is the color of the <var>i</var>-th door from the left.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 010 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes 1 1 2 2 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 001 </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>3 10110 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Yes 1 3 2 1 2 3 </pre> <p>The figure below is identical to the one in the statement.</p> <p><img alt="" src="https://img.atcoder.jp/cookie/970b981380ffad7745008433034c0885.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>3 10101 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>No </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>6 00111011100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>Yes 1 6 1 2 3 4 4 2 3 5 6 5 </pre></section> </div> </span>
[ [ "2\n010\n", "2\n010\n" ] ]
p03444
AtCoder Petrozavodsk Contest 001 - Generalized Insertion Sort
<span class="lang-en"> <p>Score : <var>2000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a rooted tree with <var>N</var> vertices. The vertices are numbered <var>0, 1, ..., N-1</var>. The root is Vertex <var>0</var>, and the parent of Vertex <var>i</var> <var>(i = 1, 2, ..., N-1)</var> is Vertex <var>p_i</var>.</p> <p>Initially, an integer <var>a_i</var> is written in Vertex <var>i</var>. Here, <var>(a_0, a_1, ..., a_{N-1})</var> is a permutation of <var>(0, 1, ..., N-1)</var>.</p> <p>You can execute the following operation at most <var>25</var> <var>000</var> times. Do it so that the value written in Vertex <var>i</var> becomes <var>i</var>.</p> <ul> <li>Choose a vertex and call it <var>v</var>. Consider the path connecting Vertex <var>0</var> and <var>v</var>.</li> <li>Rotate the values written on the path. That is, For each edge <var>(i, p_i)</var> along the path, replace the value written in Vertex <var>p_i</var> with the value written in Vertex <var>i</var> (just before this operation), and replace the value of <var>v</var> with the value written in Vertex <var>0</var> (just before this operation).</li> <li>You may choose Vertex <var>0</var>, in which case the operation does nothing.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 2000</var></li> <li><var>0 \leq p_i \leq i-1</var></li> <li><var>(a_0, a_1, ..., a_{N-1})</var> is a permutation of <var>(0, 1, ..., N-1)</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>p_1</var> <var>p_2</var> ... <var>p_{N-1}</var> <var>a_0</var> <var>a_1</var> ... <var>a_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>In the first line, print the number of operations, <var>Q</var>. In the second through <var>(Q+1)</var>-th lines, print the chosen vertices in order.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 0 1 2 3 2 4 0 1 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 3 4 </pre> <ul> <li>After the first operation, the values written in Vertex <var>0, 1, .., 4</var> are <var>4, 0, 1, 2, 3</var>.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 0 1 2 2 4 3 1 2 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 4 3 1 </pre> <ul> <li>After the first operation, the values written in Vertex <var>0, 1, .., 4</var> are <var>3, 1, 0, 2, 4</var>.</li> <li>After the second operation, the values written in Vertex <var>0, 1, .., 4</var> are <var>1, 0, 2, 3, 4</var>.</li> </ul></section> </div> </span>
[ [ "5\n0 1 2 3\n2 4 0 1 3\n", "5\n0 1 2 3\n2 4 0 1 3\n" ] ]
p03445
AtCoder Petrozavodsk Contest 001 - Simple APSP Problem
<span class="lang-en"> <p>Score : <var>2000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an <var>H \times W</var> grid. The square at the top-left corner will be represented by <var>(0, 0)</var>, and the square at the bottom-right corner will be represented by <var>(H-1, W-1)</var>.</p> <p>Of those squares, <var>N</var> squares <var>(x_1, y_1), (x_2, y_2), ..., (x_N, y_N)</var> are painted black, and the other squares are painted white.</p> <p>Let the shortest distance between white squares <var>A</var> and <var>B</var> be the minimum number of moves required to reach <var>B</var> from <var>A</var> <strong>visiting only white squares</strong>, where one can travel to an adjacent square sharing a side (up, down, left or right) in one move.</p> <p>Since there are <var>H × W - N</var> white squares in total, there are <var>_{(H×W-N)}C_2</var> ways to choose two of the white squares.</p> <p>For each of these <var>_{(H×W-N)}C_2</var> ways, find the shortest distance between the chosen squares, then find the sum of all those distances, modulo <var>1</var> <var>000</var> <var>000</var> <var>007=10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq H, W \leq 10^6</var></li> <li><var>1 \leq N \leq 30</var></li> <li><var>0 \leq x_i \leq H-1</var></li> <li><var>0 \leq y_i \leq W-1</var></li> <li>If <var>i \neq j</var>, then either <var>x_i \neq x_j</var> or <var>y_i \neq y_j</var>.</li> <li>There is at least one white square.</li> <li>For every pair of white squares <var>A</var> and <var>B</var>, it is possible to reach <var>B</var> from <var>A</var> visiting only white squares.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>N</var> <var>x_1</var> <var>y_1</var> <var>x_2</var> <var>y_2</var> <var>:</var> <var>x_N</var> <var>y_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the sum of the shortest distances, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>20 </pre> <p>We have the following grid (<code>.</code>: a white square, <code>#</code>: a black square).</p> <pre>... .#. </pre> <p>Let us assign a letter to each white square as follows:</p> <pre>ABC D#E </pre> <p>Then, we have:</p> <ul> <li>dist(A, B) = <var>1</var></li> <li>dist(A, C) = <var>2</var></li> <li>dist(A, D) = <var>1</var></li> <li>dist(A, E) = <var>3</var></li> <li>dist(B, C) = <var>1</var></li> <li>dist(B, D) = <var>2</var></li> <li>dist(B, E) = <var>2</var></li> <li>dist(C, D) = <var>3</var></li> <li>dist(C, E) = <var>1</var></li> <li>dist(D, E) = <var>4</var></li> </ul> <p>where dist(A, B) is the shortest distance between A and B. The sum of these distances is <var>20</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 3 1 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>16 </pre> <p>Let us assign a letter to each white square as follows:</p> <pre>ABC DE# </pre> <p>Then, we have:</p> <ul> <li>dist(A, B) = <var>1</var></li> <li>dist(A, C) = <var>2</var></li> <li>dist(A, D) = <var>1</var></li> <li>dist(A, E) = <var>2</var></li> <li>dist(B, C) = <var>1</var></li> <li>dist(B, D) = <var>2</var></li> <li>dist(B, E) = <var>1</var></li> <li>dist(C, D) = <var>3</var></li> <li>dist(C, E) = <var>2</var></li> <li>dist(D, E) = <var>1</var></li> </ul> <p>The sum of these distances is <var>16</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 3 1 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>64 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>4 4 4 0 1 1 1 2 1 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>268 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>1000000 1000000 1 0 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>333211937 </pre></section> </div> </span>
[ [ "2 3\n1\n1 1\n", "2 3\n1\n1 1\n" ] ]
p03446
AtCoder Petrozavodsk Contest 001 - Rectangles
<span class="lang-en"> <p>Score : <var>2100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a rectangular parallelepiped of dimensions <var>A×B×C</var>, divided into <var>1×1×1</var> small cubes. The small cubes have coordinates from <var>(0, 0, 0)</var> through <var>(A-1, B-1, C-1)</var>.</p> <p>Let <var>p</var>, <var>q</var> and <var>r</var> be integers. Consider the following set of <var>abc</var> small cubes:</p> <p><var>\{(\ (p + i)</var> mod <var>A</var>, <var>(q + j)</var> mod <var>B</var>, <var>(r + k)</var> mod <var>C\ )</var> <var>|</var> <var>i</var>, <var>j</var> and <var>k</var> are integers satisfying <var>0</var> <var>≤</var> <var>i</var> <var>&lt;</var> <var>a</var>, <var>0</var> <var>≤</var> <var>j</var> <var>&lt;</var> <var>b</var>, <var>0</var> <var>≤</var> <var>k</var> <var>&lt;</var> <var>c</var> <var>\}</var></p> <p>A set of small cubes that can be expressed in the above format using some integers <var>p</var>, <var>q</var> and <var>r</var>, is called a <em>torus cuboid</em> of size <var>a×b×c</var>.</p> <p>Find the number of the sets of torus cuboids of size <var>a×b×c</var> that satisfy the following condition, modulo <var>10^9+7</var>:</p> <ul> <li>No two torus cuboids in the set have intersection.</li> <li>The union of all torus cuboids in the set is the whole rectangular parallelepiped of dimensions <var>A×B×C</var>.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1</var> <var>≤</var> <var>a</var> <var>&lt;</var> <var>A</var> <var>≤</var> <var>100</var></li> <li><var>1</var> <var>≤</var> <var>b</var> <var>&lt;</var> <var>B</var> <var>≤</var> <var>100</var></li> <li><var>1</var> <var>≤</var> <var>c</var> <var>&lt;</var> <var>C</var> <var>≤</var> <var>100</var></li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>a</var> <var>b</var> <var>c</var> <var>A</var> <var>B</var> <var>C</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the sets of torus cuboids of size <var>a×b×c</var> that satisfy the condition, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>1 1 1 2 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 2 2 4 4 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>744 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>2 3 4 6 7 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>2 3 4 98 99 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>471975164 </pre></section> </div> </span>
[ [ "1 1 1 2 2 2\n", "1 1 1 2 2 2\n" ] ]
p03447
AtCoder Beginner Contest 087 - Buying Sweets
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You went shopping to buy cakes and donuts with <var>X</var> yen (the currency of Japan).</p> <p>First, you bought one cake for <var>A</var> yen at a cake shop. Then, you bought as many donuts as possible for <var>B</var> yen each, at a donut shop.</p> <p>How much do you have left after shopping?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq A, B \leq 1</var> <var>000</var></li> <li><var>A + B \leq X \leq 10</var> <var>000</var></li> <li><var>X</var>, <var>A</var> and <var>B</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>X</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the amount you have left after shopping.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>1234 150 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>84 </pre> <p>You have <var>1234 - 150 = 1084</var> yen left after buying a cake. With this amount, you can buy <var>10</var> donuts, after which you have <var>84</var> yen left.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1000 108 108 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>28 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>579 123 456 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>7477 549 593 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>405 </pre></section> </div> </span>
[ [ "1234\n150\n100\n", "1234\n150\n100\n" ] ]
p03448
AtCoder Beginner Contest 087 - Coins
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You have <var>A</var> <var>500</var>-yen coins, <var>B</var> <var>100</var>-yen coins and <var>C</var> <var>50</var>-yen coins (yen is the currency of Japan). In how many ways can we select some of these coins so that they are <var>X</var> yen in total?</p> <p>Coins of the same kind cannot be distinguished. Two ways to select coins are distinguished when, for some kind of coin, the numbers of that coin are different.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>0 \leq A, B, C \leq 50</var></li> <li><var>A + B + C \geq 1</var></li> <li><var>50 \leq X \leq 20</var> <var>000</var></li> <li><var>A</var>, <var>B</var> and <var>C</var> are integers.</li> <li><var>X</var> is a multiple of <var>50</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> <var>C</var> <var>X</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways to select coins.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 2 2 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>There are two ways to satisfy the condition:</p> <ul> <li>Select zero <var>500</var>-yen coins, one <var>100</var>-yen coin and zero <var>50</var>-yen coins.</li> <li>Select zero <var>500</var>-yen coins, zero <var>100</var>-yen coins and two <var>50</var>-yen coins.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 1 0 150 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Note that the total must be exactly <var>X</var> yen.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>30 40 50 6000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>213 </pre></section> </div> </span>
[ [ "2\n2\n2\n100\n", "2\n2\n2\n100\n" ] ]
p03449
AtCoder Beginner Contest 087 - Candies
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a <var>2 \times N</var> grid. We will denote the square at the <var>i</var>-th row and <var>j</var>-th column (<var>1 \leq i \leq 2</var>, <var>1 \leq j \leq N</var>) as <var>(i, j)</var>.</p> <p>You are initially in the top-left square, <var>(1, 1)</var>. You will travel to the bottom-right square, <var>(2, N)</var>, by repeatedly moving right or down.</p> <p>The square <var>(i, j)</var> contains <var>A_{i, j}</var> candies. You will collect all the candies you visit during the travel. The top-left and bottom-right squares also contain candies, and you will also collect them.</p> <p>At most how many candies can you collect when you choose the best way to travel?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 100</var></li> <li><var>1 \leq A_{i, j} \leq 100</var> (<var>1 \leq i \leq 2</var>, <var>1 \leq j \leq N</var>)</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_{1, 1}</var> <var>A_{1, 2}</var> <var>...</var> <var>A_{1, N}</var> <var>A_{2, 1}</var> <var>A_{2, 2}</var> <var>...</var> <var>A_{2, N}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum number of candies that can be collected.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 3 2 2 4 1 1 2 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>14 </pre> <p>The number of collected candies will be maximized when you:</p> <ul> <li>move right three times, then move down once, then move right once.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 1 1 1 1 1 1 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>5 </pre> <p>You will always collect the same number of candies, regardless of how you travel.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>7 3 3 4 5 4 5 3 5 3 4 4 2 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>29 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>1 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>5 </pre></section> </div> </span>
[ [ "5\n3 2 2 4 1\n1 2 2 2 1\n", "5\n3 2 2 4 1\n1 2 2 2 1\n" ] ]
p03450
AtCoder Beginner Contest 087 - People on a Line
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> people standing on the <var>x</var>-axis. Let the coordinate of Person <var>i</var> be <var>x_i</var>. For every <var>i</var>, <var>x_i</var> is an integer between <var>0</var> and <var>10^9</var> (inclusive). It is possible that more than one person is standing at the same coordinate.</p> <p>You will given <var>M</var> pieces of information regarding the positions of these people. The <var>i</var>-th piece of information has the form <var>(L_i, R_i, D_i)</var>. This means that Person <var>R_i</var> is to the right of Person <var>L_i</var> by <var>D_i</var> units of distance, that is, <var>x_{R_i} - x_{L_i} = D_i</var> holds.</p> <p>It turns out that some of these <var>M</var> pieces of information may be incorrect. Determine if there exists a set of values <var>(x_1, x_2, ..., x_N)</var> that is consistent with the given pieces of information.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 100</var> <var>000</var></li> <li><var>0 \leq M \leq 200</var> <var>000</var></li> <li><var>1 \leq L_i, R_i \leq N</var> (<var>1 \leq i \leq M</var>)</li> <li><var>0 \leq D_i \leq 10</var> <var>000</var> (<var>1 \leq i \leq M</var>)</li> <li><var>L_i \neq R_i</var> (<var>1 \leq i \leq M</var>)</li> <li>If <var>i \neq j</var>, then <var>(L_i, R_i) \neq (L_j, R_j)</var> and <var>(L_i, R_i) \neq (R_j, L_j)</var>.</li> <li><var>D_i</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>L_1</var> <var>R_1</var> <var>D_1</var> <var>L_2</var> <var>R_2</var> <var>D_2</var> <var>:</var> <var>L_M</var> <var>R_M</var> <var>D_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a set of values <var>(x_1, x_2, ..., x_N)</var> that is consistent with all given pieces of information, print <code>Yes</code>; if it does not exist, print <code>No</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 1 2 1 2 3 1 1 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes </pre> <p>Some possible sets of values <var>(x_1, x_2, x_3)</var> are <var>(0, 1, 2)</var> and <var>(101, 102, 103)</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 3 1 2 1 2 3 1 1 3 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>No </pre> <p>If the first two pieces of information are correct, <var>x_3 - x_1 = 2</var> holds, which is contradictory to the last piece of information.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 3 2 1 1 2 3 5 3 4 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Yes </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>10 3 8 7 100 7 9 100 9 8 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>No </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>100 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>Yes </pre></section> </div> </span>
[ [ "3 3\n1 2 1\n2 3 1\n1 3 2\n", "3 3\n1 2 1\n2 3 1\n1 3 2\n" ] ]
p03451
AtCoder Regular Contest 090 - Candies
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a <var>2 \times N</var> grid. We will denote the square at the <var>i</var>-th row and <var>j</var>-th column (<var>1 \leq i \leq 2</var>, <var>1 \leq j \leq N</var>) as <var>(i, j)</var>.</p> <p>You are initially in the top-left square, <var>(1, 1)</var>. You will travel to the bottom-right square, <var>(2, N)</var>, by repeatedly moving right or down.</p> <p>The square <var>(i, j)</var> contains <var>A_{i, j}</var> candies. You will collect all the candies you visit during the travel. The top-left and bottom-right squares also contain candies, and you will also collect them.</p> <p>At most how many candies can you collect when you choose the best way to travel?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 100</var></li> <li><var>1 \leq A_{i, j} \leq 100</var> (<var>1 \leq i \leq 2</var>, <var>1 \leq j \leq N</var>)</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_{1, 1}</var> <var>A_{1, 2}</var> <var>...</var> <var>A_{1, N}</var> <var>A_{2, 1}</var> <var>A_{2, 2}</var> <var>...</var> <var>A_{2, N}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum number of candies that can be collected.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 3 2 2 4 1 1 2 2 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>14 </pre> <p>The number of collected candies will be maximized when you:</p> <ul> <li>move right three times, then move down once, then move right once.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 1 1 1 1 1 1 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>5 </pre> <p>You will always collect the same number of candies, regardless of how you travel.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>7 3 3 4 5 4 5 3 5 3 4 4 2 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>29 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>1 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>5 </pre></section> </div> </span>
[ [ "5\n3 2 2 4 1\n1 2 2 2 1\n", "5\n3 2 2 4 1\n1 2 2 2 1\n" ] ]
p03452
AtCoder Regular Contest 090 - People on a Line
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> people standing on the <var>x</var>-axis. Let the coordinate of Person <var>i</var> be <var>x_i</var>. For every <var>i</var>, <var>x_i</var> is an integer between <var>0</var> and <var>10^9</var> (inclusive). It is possible that more than one person is standing at the same coordinate.</p> <p>You will given <var>M</var> pieces of information regarding the positions of these people. The <var>i</var>-th piece of information has the form <var>(L_i, R_i, D_i)</var>. This means that Person <var>R_i</var> is to the right of Person <var>L_i</var> by <var>D_i</var> units of distance, that is, <var>x_{R_i} - x_{L_i} = D_i</var> holds.</p> <p>It turns out that some of these <var>M</var> pieces of information may be incorrect. Determine if there exists a set of values <var>(x_1, x_2, ..., x_N)</var> that is consistent with the given pieces of information.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 100</var> <var>000</var></li> <li><var>0 \leq M \leq 200</var> <var>000</var></li> <li><var>1 \leq L_i, R_i \leq N</var> (<var>1 \leq i \leq M</var>)</li> <li><var>0 \leq D_i \leq 10</var> <var>000</var> (<var>1 \leq i \leq M</var>)</li> <li><var>L_i \neq R_i</var> (<var>1 \leq i \leq M</var>)</li> <li>If <var>i \neq j</var>, then <var>(L_i, R_i) \neq (L_j, R_j)</var> and <var>(L_i, R_i) \neq (R_j, L_j)</var>.</li> <li><var>D_i</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>L_1</var> <var>R_1</var> <var>D_1</var> <var>L_2</var> <var>R_2</var> <var>D_2</var> <var>:</var> <var>L_M</var> <var>R_M</var> <var>D_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there exists a set of values <var>(x_1, x_2, ..., x_N)</var> that is consistent with all given pieces of information, print <code>Yes</code>; if it does not exist, print <code>No</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 1 2 1 2 3 1 1 3 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes </pre> <p>Some possible sets of values <var>(x_1, x_2, x_3)</var> are <var>(0, 1, 2)</var> and <var>(101, 102, 103)</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 3 1 2 1 2 3 1 1 3 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>No </pre> <p>If the first two pieces of information are correct, <var>x_3 - x_1 = 2</var> holds, which is contradictory to the last piece of information.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 3 2 1 1 2 3 5 3 4 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Yes </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>10 3 8 7 100 7 9 100 9 8 100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>No </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>100 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>Yes </pre></section> </div> </span>
[ [ "3 3\n1 2 1\n2 3 1\n1 3 2\n", "3 3\n1 2 1\n2 3 1\n1 3 2\n" ] ]
p03453
AtCoder Regular Contest 090 - Avoiding Collision
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a graph with <var>N</var> vertices and <var>M</var> edges, and there are two people on the graph: Takahashi and Aoki.</p> <p>The <var>i</var>-th edge connects Vertex <var>U_i</var> and Vertex <var>V_i</var>. The time it takes to traverse this edge is <var>D_i</var> minutes, regardless of direction and who traverses the edge (Takahashi or Aoki).</p> <p>Takahashi departs Vertex <var>S</var> and Aoki departs Vertex <var>T</var> at the same time. Takahashi travels to Vertex <var>T</var> and Aoki travels to Vertex <var>S</var>, both in the shortest time possible. Find the number of the pairs of ways for Takahashi and Aoki to choose their shortest paths such that they never meet (at a vertex or on an edge) during the travel, modulo <var>10^9 + 7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 100</var> <var>000</var></li> <li><var>1 \leq M \leq 200</var> <var>000</var></li> <li><var>1 \leq S, T \leq N</var></li> <li><var>S \neq T</var></li> <li><var>1 \leq U_i, V_i \leq N</var> (<var>1 \leq i \leq M</var>)</li> <li><var>1 \leq D_i \leq 10^9</var> (<var>1 \leq i \leq M</var>)</li> <li>If <var>i \neq j</var>, then <var>(U_i, V_i) \neq (U_j, V_j)</var> and <var>(U_i, V_i) \neq (V_j, U_j)</var>.</li> <li><var>U_i \neq V_i</var> (<var>1 \leq i \leq M</var>)</li> <li><var>D_i</var> are integers.</li> <li>The given graph is connected.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>S</var> <var>T</var> <var>U_1</var> <var>V_1</var> <var>D_1</var> <var>U_2</var> <var>V_2</var> <var>D_2</var> <var>:</var> <var>U_M</var> <var>V_M</var> <var>D_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 4 1 3 1 2 1 2 3 1 3 4 1 4 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>There are two ways to choose shortest paths that satisfies the condition:</p> <ul> <li>Takahashi chooses the path <var>1 \rightarrow 2 \rightarrow 3</var>, and Aoki chooses the path <var>3 \rightarrow 4 \rightarrow 1</var>.</li> <li>Takahashi chooses the path <var>1 \rightarrow 4 \rightarrow 3</var>, and Aoki chooses the path <var>3 \rightarrow 2 \rightarrow 1</var>.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 3 1 3 1 2 1 2 3 1 3 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 3 1 3 1 2 1 2 3 1 3 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>2 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>8 13 4 2 7 3 9 6 2 3 1 6 4 7 6 9 3 8 9 1 2 2 2 8 12 8 6 9 2 5 5 4 2 18 5 3 7 5 1 515371567 4 8 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>6 </pre></section> </div> </span>
[ [ "4 4\n1 3\n1 2 1\n2 3 1\n3 4 1\n4 1 1\n", "4 4\n1 3\n1 2 1\n2 3 1\n3 4 1\n4 1 1\n" ] ]
p03454
AtCoder Regular Contest 090 - Number of Digits
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>For a positive integer <var>n</var>, let us define <var>f(n)</var> as the number of digits in base <var>10</var>.</p> <p>You are given an integer <var>S</var>. Count the number of the pairs of positive integers <var>(l, r)</var> (<var>l \leq r</var>) such that <var>f(l) + f(l + 1) + ... + f(r) = S</var>, and find the count modulo <var>10^9 + 7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq S \leq 10^8</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>S</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>9 </pre> <p>There are nine pairs <var>(l, r)</var> that satisfies the condition: <var>(1, 1)</var>, <var>(2, 2)</var>, <var>...</var>, <var>(9, 9)</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>98 </pre> <p>There are <var>98</var> pairs <var>(l, r)</var> that satisfies the condition, such as <var>(1, 2)</var> and <var>(33, 33)</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>123 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>460191684 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>36018 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>966522825 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>1000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>184984484 </pre></section> </div> </span>
[ [ "1\n", "1\n" ] ]
p03455
AtCoder Beginner Contest 086 - Product
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>AtCoDeer the deer found two positive integers, <var>a</var> and <var>b</var>. Determine whether the product of <var>a</var> and <var>b</var> is even or odd.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1</var> <var>≤</var> <var>a,b</var> <var>≤</var> <var>10000</var></li> <li><var>a</var> and <var>b</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>a</var> <var>b</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If the product is odd, print <code>Odd</code>; if it is even, print <code>Even</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Even </pre> <p>As <var>3 × 4 = 12</var> is even, print <code>Even</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 21 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>Odd </pre> <p>As <var>1 × 21 = 21</var> is odd, print <code>Odd</code>.</p></section> </div> </span>
[ [ "3 4\n", "3 4\n" ] ]