id
stringlengths
6
6
slug_name
stringlengths
1
105
pretty_content
stringlengths
1
27.2k
test_cases
sequencelengths
1
1
p03756
square869120Contest #4 - Find the Route!
<span class="lang-en lang-child hidden-lang"> <div class="part"> Max Score: <var>1150</var> Points <br/> <font color="red">Task statement was updated.</font> <section> <h3>Problem Statement</h3> There is a grid which size is $H \times W$. the upper-left cell is $(1,1)$ and the lower-right cell is $(H,W)$.<br/> There is $N$ arrows. Arrow which start point is $(a_i,b_i)$ of direction is $c_i$, and size is $d_i$. ($d_i$ may be negative)<br/> It is guaranteed that there are no two arrow which start point is same.<br/> <br/> Sothe want to move from cell $(sx,sy)$ to cell $(gx,gy)$ with arrows.<br/> But it may not possible to move to goal in initial grid.<br/> So, Snuke decided to change some arrows.<br/> <br/> <div align="left" class="img-nocaption"> <img src="https://atcoder.jp/img/s8pc-4/5eff69da8ebd635a501515b7f86a47e6.png" width="500"/> </div> Sothe can change each arrow as follows:<br/> <ul> <li>He can't change the start point of this arrow.</li> <li>It costs $e_i$ if he change the direction of this arrow.</li> <li>It costs $f \times |d_i-G|$ if he change <var>d_i</var> to $G$.</li> </ul> <div align="left" class="img-nocaption"> <img src="https://atcoder.jp/img/s8pc-4/532f821bbe6594a29e622d7a81cbcad7.png" width="700"/> </div><br/> He can't add or erase arrows.<br/> <br/> Please calculate the minimum cost that he can move to $(gx,gy)$.<br/><h3> If he can't move to goal, please output '-1'.</h3> Note: Arrows are directed, and he can't turn in the middle of the arrow.<br/> </section> </div> <div class="io-style"> <div class="part"> <section> <h3>Input</h3> The input is given from standard input in the following format. <br/> <br/> <pre> <var>H</var> <var>W</var> <var>N</var> <var>f</var> <var>sx</var> <var>sy</var> <var>gx</var> <var>gy</var> <var>a_1</var> <var>b_1</var> <var>c_1</var> <var>d_1</var> <var>e_1</var> <var>a_2</var> <var>b_2</var> <var>c_2</var> <var>d_2</var> <var>e_2</var> : : : <var>a_N</var> <var>b_N</var> <var>c_N</var> <var>d_N</var> <var>e_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3> <ul> <li>Please output a single integer: The minimum cost to clear this puzzle.</li> <li>If you can't achieve the objective, print <code>-1</code>.</li> <li>Print <code>\n</code> (line break) in the end.</li> </ul> </section> <section> <h3>Constraints</h3> <ul> <li>$1 \le H,W \le 100000$</li> <li>$1 \le N \le 70000$</li> <li>$1 \le f,e_i \le 1000000$</li> <li>$1 \le d_i \le 100000$</li> <li>$1 \le a_i,sx,tx \le H$</li> <li>$1 \le b_i,sy,ty \le W$</li> <li>$c_i$ is <code>N</code>, <code>E</code>, <code>S</code>, or <code>W</code>, which means North, East, South, West.</li> </ul> </section> <section> <h3>Subtasks</h3> <p>Subtask 1 [ <var>190</var> points ]</p> <ul> <li>$H=1$</li> <li>$W \le 600$</li> </ul> <p>Subtask 2 [ <var>170</var> points ]</p> <ul> <li>$H,W \le 80$</li> </ul> <p>Subtask 3 [ <var>360</var> points ]</p> <ul> <li>$H,W \le 600$</li> </ul> <p>Subtask 4 [ <var>430</var> points ]</p> <ul> <li>There is no additional constraints.</li> </ul> </section> </div> </div> <div class="part"> <section> <h3>Sample Input 1</h3> <pre> 4 4 2 2 1 1 2 2 1 1 E 1 1 1 2 E 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3> <pre> 4 </pre> </section> </div> <div align="left" class="img-nocaption"> <img src="https://atcoder.jp/img/s8pc-4/6c5665931a6f8044048238e0a68bf17c.png" width="500"/> </div> <div class="part"> <section> <h3>Sample Input 2</h3> <pre> 1 4 2 10 1 1 1 4 1 1 E 1 4 1 3 W 1 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3> <pre> 14 </pre> </section> </div> <div class="part"> <section> <h3>Sample Input 3</h3> <pre> 1 8 4 9 1 3 1 6 1 1 E 7 2 1 8 W 7 5 1 3 W 2 5 1 6 E 2 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3> <pre> 14 </pre> </section> </div> <div class="part"> <h3>Sample Input 4</h3> <pre> 5 5 7 10 1 2 4 5 1 2 E 2 6 2 3 S 2 7 3 1 N 1 8 3 2 W 1 10 4 1 E 4 12 5 5 N 3 13 5 1 E 2 14 </pre> </div></span>
[ [ "4 4 2 2\n1 1 2 2\n1 1 E 1 1\n1 2 E 2 2\n", "4 4 2 2\n1 1 2 2\n1 1 E 1 1\n1 2 E 2 2\n" ] ]
p03757
square869120Contest #4 - Get the Salary of Atcoder
<span class="lang-en lang-child hidden-lang"> <div id="task-statement"> Max Score: <var>1450</var> Points <br/> <section> <h3>Problem Statement</h3> There are <var>N</var> workers in Atcoder company. Each worker is numbered <var>0</var> through <var>N - 1</var>, and the boss for worker <var>i</var> is <var>p_i</var> like a tree structure and the salary is currently <var>a_i</var>. (<var>p_i &lt; i</var>, especially <var>p_0 = -1</var> because worker <var>0</var> is a president) <br/> In atcoder, the boss of boss of boss of ... (repeated <var>k</var> times) worker <var>i</var> called "<var>k</var>-th upper boss", and "<var>k</var>-th lower subordinate" called for vice versa. <br/> <br/> You have to process <var>Q</var> queries for Atcoder: <br/> <ul> <li>Query 1: You are given <var>v_i, d_i, x_i</var>. Increase the salary of worker <var>v_i</var>, and all <var>j</var>-th (<var>1 ≤ j ≤ d_i</var>) lower subordinates by <var>x_i</var>.</li> <li>Query 2: You are given <var>v_i, d_i</var>. Calculate the sum of salary of worker <var>v_i</var> and all <var>j</var>-th (<var>1 ≤ j ≤ d_i</var>) lower subordinates.</li> <li>Query 3: You are given <var>pr_i, ar_i</var>. Now Atcoder has a new worker <var>c</var>! (<var>c</var> is the current number of workers) The boss is <var>pr_i</var>, and the first salary is <var>ar_i</var>.</li> </ul> Process all queries!!! <br/> </section> </div> <div class="io-style"> <div class="part"> <section> <h3>Input Format</h3> Let the <var>i</var>-th query <var>query_i</var>, the input format is following:<br/> <pre> <var>N</var> <var>Q</var> <var>p_0</var> <var>a_0</var> <var>p_1</var> <var>a_1</var> : : <var>p_{N - 1}</var> <var>a_{N - 1}</var> <var>query_0</var> <var>query_1</var> : : <var>query_{Q - 1}</var> </pre> THe format of <var>query_i</var> is one of the three format:<br/> <pre> 1 <var>v_i</var> <var>d_i</var> <var>x_i</var> </pre> <pre> 2 <var>v_i</var> <var>d_i</var> </pre> <pre> 3 <var>pr_i</var> <var>ar_i</var> </pre> </section> </div> <div class="part"> <section> <h3>Output Format</h3> Print the result in one line for each query 2.<br/> </section> <section> <h3>Constraints</h3> <ul> <li><var>N ≤ 400000</var></li> <li><var>Q ≤ 50000</var></li> <li><var>p_i &lt; i</var> for all valid <var>i</var>.</li> <li>In each question 1 or 2, worker <var>v_i</var> exists.</li> <li><var>d_i ≤ 400000</var></li> <li><var>0 ≤ a_i, x_i ≤ 1000</var></li> </ul> </section> <section> <h3>Scoring</h3> Subtask 1 [<var>170</var> points] <br/> <ul> <li><var>N, Q ≤ 5000</var></li> </ul> Subtask 2 [<var>310</var> points] <br/> <ul> <li><var>p_i + 1 = i</var> for all valid <var>i</var>.</li> </ul> Subtask 3 [<var>380</var> points] <br/> <ul> <li>There are no query 3.</li> </ul> Subtask 4 [<var>590</var> points] <br/> <ul> <li>There are no additional constraints.</li> </ul> </section> </div> <div class="part"> <section> <h3>Sample Input 1</h3> <pre> 6 7 -1 6 0 5 0 4 2 3 2 2 1 1 2 0 1 1 0 2 1 2 2 1 3 3 3 2 0 3 3 3 4 2 1 1 </pre> </section> <section> <h3>Sample Output 1</h3> <pre> 15 12 30 8 </pre> </section> </div> </div> <div class="part"> <section> <h3>Sample Input 2</h3> <pre> 7 9 -1 1 0 5 0 7 0 8 1 3 4 1 5 1 2 1 1 2 1 2 1 1 2 3 1 4 1 1 2 3 1 2 0 2 3 6 1 3 7 11 2 0 15 </pre> </section> <section> <h3>Sample Output 2</h3> <pre> 8 9 8 31 49 </pre> </section> </div> <!-- 入出力例の説明もできれば書く --> </span>
[ [ "6 7\n-1 6\n0 5\n0 4\n2 3\n2 2\n1 1\n2 0 1\n1 0 2 1\n2 2 1\n3 3 3\n2 0 3\n3 3 4\n2 1 1\n", "6 7\n-1 6\n0 5\n0 4\n2 3\n2 2\n1 1\n2 0 1\n1 0 2 1\n2 2 1\n3 3 3\n2 0 3\n3 3 4\n2 1 1\n" ] ]
p03758
square869120Contest #4 - Huge Kingdom: Atcoder
<span class="lang-en lang-child hidden-lang"> <div class="part"> Max Score: $1500$ Points <br/> <section> <h3>Problem Statement</h3> Huge kingdom: $Atcoder$ contains $N$ towns and $N-1$ roads. This kingdom is connected.<br/> <br/> You have to detect the kingdom's road.<br/> In order to detect, You can ask this form of questions.<br/> <ul> <li>You can output a string $S$. Length of $S$ must be $N$ and The character of string $S$ must be represented by '$0$' or '$1$'.</li> <li>Computer paint the towns with white or black according to the character string you output.</li> <li>If $i$-th character of $S$ is '0', computer paint town $i$ white.</li> <li>If $i$-th character of $S$ is '1', computer paint town $i$ black.</li> <li>Please consider an undirected graph $G$.</li> <li>For each edge, computer do the following processing: If both ends painted black, computer adds this edge to $G$.</li> <li>Computer returns value $x$: sum of "diameter of tree"$^2$ about each connected components.</li> </ul> For example, when $S$="11001111" and the kingdom's structure is this, computer returns 10.<br/> <div align="left" class="img-nocaption"> <img src="https://atcoder.jp/img/s8pc-4/18f55c17e4feefd6794b69a1b91d5e6f.png" width="500"/> </div> Please detect the structure of $Atcoder$ as few questions as possible.<br/> </section> </div> <div class="io-style"> <div class="part"> <section> <h3>Input, Output</h3> This is a reactive problem.<br/> The first input is as follows:<br/> <br/> <blockquote> $N$ </blockquote> <ul class="simple"> <li>$N$ is number of towns in $Atcoder$.</li> </ul> <br/> Next, you can ask questions.<br/> The question must be in the form as follows:<br/> <blockquote> ? $S$ </blockquote> $S$ is a string. The mean of $S$ written in the problem statement. Length of $S$ must be $N$.<br/> <br/> The answer of question is as follows:<br/> <blockquote> $x$ </blockquote> The mean of $x$ written in the problem statement.<br/> <br/> Finally, your program must output as follows:<br/> <blockquote> ! $(a_1,b_1)$ $(a_2,b_2)$ $(a_3,b_3)$ … $(a_{N-1},b_{N-1})$ </blockquote> This output means your program detects all roads of $Atcoder$.<br/> $(a_i,b_i)$ means there is a road between $a_i$ and $b_i$.<br/> <br/> You can output roads any order, but you must output the answer on a single line.<br/> </section> </div> </div> <div class="part"> <section> <h3>Constraints</h3> <ul> <li>$N$=200</li> <li>$Atcoder$ contains $N-1$ roads and this kingdom is connected.</li> <li>All cases were made randomly.</li> </ul> </section> </div> <div class="part"> <section> <h3>Scoring</h3> Let the number of questions be $L$.<br/> <br/> <ul> <li>If $L &gt; 20000$, $score$ = $0$ points</li> <li>If $18000 &lt; L ≦ 20000$, $score$ = $200$ points</li> <li>If $5000 &lt; L ≦ 18000$, $score$ = $650-L/40$ points</li> <li>If $4000 &lt; L ≦ 5000$, $score$ = $800-L/20$ points</li> <li>If $2000 &lt; L ≦ 4000$, $score$ = $1400-L/5$ points</li> <li>If $1200 &lt; L ≦ 2000$, $score$ = $1500-L/4$ points</li> <li>If $700 &lt; L ≦ 1200$, $score$ = $1850-L/2$ points</li> <li>If $L ≦ 700$, $score$ = $1500$ points</li> </ul> <br/> There is $5$ cases, so points of each case is $score/5$.<br/> </section> </div> <div class="part"> <section> <h3>Sample Input</h3> This case is $N=4$. This case is not include because this is not $N=200$.<br/> <pre> N=4 0 1 1 2 1 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output</h3> Sample interaction is as follows:<br/> <table class="table-striped table-bordered table-condensed"> <tr align="center"> <th>Input from computer</th> <th>Output</th> </tr> <tr align="center"> <td>4</td> <td> </td> </tr> <tr align="center"> <td> </td> <td>? 1111</td> </tr> <tr align="center"> <td>4</td> <td> </td> </tr> <tr align="center"> <td> </td> <td>? 1101</td> </tr> <tr align="center"> <td>4</td> <td> </td> </tr> <tr align="center"> <td> </td> <td>? 1001</td> </tr> <tr align="center"> <td>0</td> <td> </td> </tr> <tr align="center"> <td> </td> <td>? 1100</td> </tr> <tr align="center"> <td>1</td> <td> </td> </tr> <tr align="center"> <td> </td> <td>? 1011</td> </tr> <tr align="center"> <td>0</td> <td> </td> </tr> <tr align="center"> <td> </td> <td>! (0,1) (1,2) (1,3)</td> </tr> </table> <br/> In this sample, structure of $Atcoder$ is as follows:<br/> <div align="left" class="img-nocaption"> <img src="https://atcoder.jp/img/s8pc-4/8d26e2d0a3fd5e4cc24efe8d21296341.png" width="500"/> </div> <br/> This question is not always meaningful.<br/> </section> </div> </span>
[ [ "", "" ] ]
p03759
AtCoder Beginner Contest 058 - ι⊥l
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Three poles stand evenly spaced along a line. Their heights are <var>a</var>, <var>b</var> and <var>c</var> meters, from left to right. We will call the arrangement of the poles <em>beautiful</em> if the tops of the poles lie on the same line, that is, <var>b-a = c-b</var>.</p> <p>Determine whether the arrangement of the poles is beautiful.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq a,b,c \leq 100</var></li> <li><var>a</var>, <var>b</var> and <var>c</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> <var>c</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <code>YES</code> if the arrangement of the poles is beautiful; print <code>NO</code> otherwise.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 4 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>YES </pre> <p>Since <var>4-2 = 6-4</var>, this arrangement of poles is beautiful.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 5 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>NO </pre> <p>Since <var>5-2 \neq 6-5</var>, this arrangement of poles is not beautiful.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>YES </pre> <p>Since <var>1-2 = 2-3</var>, this arrangement of poles is beautiful.</p></section> </div> </span>
[ [ "2 4 6\n", "2 4 6\n" ] ]
p03760
AtCoder Beginner Contest 058 - ∵∴∵
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke signed up for a new website which holds programming competitions. He worried that he might forget his password, and he took notes of it. Since directly recording his password would cause him trouble if stolen, he took two notes: one contains the characters at the odd-numbered positions, and the other contains the characters at the even-numbered positions.</p> <p>You are given two strings <var>O</var> and <var>E</var>. <var>O</var> contains the characters at the odd-numbered positions retaining their relative order, and <var>E</var> contains the characters at the even-numbered positions retaining their relative order. Restore the original password.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>O</var> and <var>E</var> consists of lowercase English letters (<code>a</code> - <code>z</code>).</li> <li><var>1 \leq |O|,|E| \leq 50</var></li> <li><var>|O| - |E|</var> is either <var>0</var> or <var>1</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>O</var> <var>E</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the original password.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>xyz abc </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>xaybzc </pre> <p>The original password is <code>xaybzc</code>. Extracting the characters at the odd-numbered positions results in <code>xyz</code>, and extracting the characters at the even-numbered positions results in <code>abc</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>atcoderbeginnercontest atcoderregularcontest </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>aattccooddeerrbreeggiunlnaerrccoonntteesstt </pre></section> </div> </span>
[ [ "xyz\nabc\n", "xyz\nabc\n" ] ]
p03761
AtCoder Beginner Contest 058 - Dubious Document
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke loves "paper cutting": he cuts out characters from a newspaper headline and rearranges them to form another string.</p> <p>He will receive a headline which contains one of the strings <var>S_1,...,S_n</var> tomorrow. He is excited and already thinking of what string he will create. Since he does not know the string on the headline yet, he is interested in strings that can be created regardless of which string the headline contains.</p> <p>Find the longest string that can be created regardless of which string among <var>S_1,...,S_n</var> the headline contains. If there are multiple such strings, find the lexicographically smallest one among them.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq n \leq 50</var></li> <li><var>1 \leq |S_i| \leq 50</var> for every <var>i = 1, ..., n</var>.</li> <li><var>S_i</var> consists of lowercase English letters (<code>a</code> - <code>z</code>) for every <var>i = 1, ..., n</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>n</var> <var>S_1</var> <var>...</var> <var>S_n</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the lexicographically smallest string among the longest strings that satisfy the condition. If the answer is an empty string, print an empty line.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 cbaa daacc acacac </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>aac </pre> <p>The strings that can be created from each of <code>cbaa</code>, <code>daacc</code> and <code>acacac</code>, are <code>aa</code>, <code>aac</code>, <code>aca</code>, <code>caa</code> and so forth. Among them, <code>aac</code>, <code>aca</code> and <code>caa</code> are the longest, and the lexicographically smallest of these three is <code>aac</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 a aa b </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre> </pre> <p>The answer is an empty string.</p></section> </div> </span>
[ [ "3\ncbaa\ndaacc\nacacac\n", "3\ncbaa\ndaacc\nacacac\n" ] ]
p03762
AtCoder Beginner Contest 058 - ###
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>On a two-dimensional plane, there are <var>m</var> lines drawn parallel to the <var>x</var> axis, and <var>n</var> lines drawn parallel to the <var>y</var> axis. Among the lines parallel to the <var>x</var> axis, the <var>i</var>-th from the bottom is represented by <var>y = y_i</var>. Similarly, among the lines parallel to the <var>y</var> axis, the <var>i</var>-th from the left is represented by <var>x = x_i</var>.</p> <p>For every rectangle that is formed by these lines, find its area, and print the total area modulo <var>10^9+7</var>.</p> <p>That is, for every quadruple <var>(i,j,k,l)</var> satisfying <var>1\leq i &lt; j\leq n</var> and <var>1\leq k &lt; l\leq m</var>, find the area of the rectangle formed by the lines <var>x=x_i</var>, <var>x=x_j</var>, <var>y=y_k</var> and <var>y=y_l</var>, and print the sum of these areas modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq n,m \leq 10^5</var></li> <li><var>-10^9 \leq x_1 &lt; ... &lt; x_n \leq 10^9</var></li> <li><var>-10^9 \leq y_1 &lt; ... &lt; y_m \leq 10^9</var></li> <li><var>x_i</var> and <var>y_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>x_1</var> <var>x_2</var> <var>...</var> <var>x_n</var> <var>y_1</var> <var>y_2</var> <var>...</var> <var>y_m</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the total area of the rectangles, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 1 3 4 1 3 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>60 </pre> <p>The following figure illustrates this input:</p> <p><img alt="sample1-1" src="https://atcoder.jp/img/arc071/aec4d5cc2e5c73dbee455be237a649a5.png"/></p> <p>The total area of the nine rectangles A, B, ..., I shown in the following figure, is <var>60</var>.</p> <p><img alt="sample1-2" src="https://atcoder.jp/img/arc071/f0771c0f7e68af2b00e7513186f585ff.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 -790013317 -192321079 95834122 418379342 586260100 802780784 -253230108 193944314 363756450 712662868 735867677 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>835067060 </pre></section> </div> </span>
[ [ "3 3\n1 3 4\n1 3 6\n", "3 3\n1 3 4\n1 3 6\n" ] ]
p03763
AtCoder Regular Contest 071 - Dubious Document
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke loves "paper cutting": he cuts out characters from a newspaper headline and rearranges them to form another string.</p> <p>He will receive a headline which contains one of the strings <var>S_1,...,S_n</var> tomorrow. He is excited and already thinking of what string he will create. Since he does not know the string on the headline yet, he is interested in strings that can be created regardless of which string the headline contains.</p> <p>Find the longest string that can be created regardless of which string among <var>S_1,...,S_n</var> the headline contains. If there are multiple such strings, find the lexicographically smallest one among them.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq n \leq 50</var></li> <li><var>1 \leq |S_i| \leq 50</var> for every <var>i = 1, ..., n</var>.</li> <li><var>S_i</var> consists of lowercase English letters (<code>a</code> - <code>z</code>) for every <var>i = 1, ..., n</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>n</var> <var>S_1</var> <var>...</var> <var>S_n</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the lexicographically smallest string among the longest strings that satisfy the condition. If the answer is an empty string, print an empty line.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 cbaa daacc acacac </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>aac </pre> <p>The strings that can be created from each of <code>cbaa</code>, <code>daacc</code> and <code>acacac</code>, are <code>aa</code>, <code>aac</code>, <code>aca</code>, <code>caa</code> and so forth. Among them, <code>aac</code>, <code>aca</code> and <code>caa</code> are the longest, and the lexicographically smallest of these three is <code>aac</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 a aa b </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre> </pre> <p>The answer is an empty string.</p></section> </div> </span>
[ [ "3\ncbaa\ndaacc\nacacac\n", "3\ncbaa\ndaacc\nacacac\n" ] ]
p03764
AtCoder Regular Contest 071 - ###
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>On a two-dimensional plane, there are <var>m</var> lines drawn parallel to the <var>x</var> axis, and <var>n</var> lines drawn parallel to the <var>y</var> axis. Among the lines parallel to the <var>x</var> axis, the <var>i</var>-th from the bottom is represented by <var>y = y_i</var>. Similarly, among the lines parallel to the <var>y</var> axis, the <var>i</var>-th from the left is represented by <var>x = x_i</var>.</p> <p>For every rectangle that is formed by these lines, find its area, and print the total area modulo <var>10^9+7</var>.</p> <p>That is, for every quadruple <var>(i,j,k,l)</var> satisfying <var>1\leq i &lt; j\leq n</var> and <var>1\leq k &lt; l\leq m</var>, find the area of the rectangle formed by the lines <var>x=x_i</var>, <var>x=x_j</var>, <var>y=y_k</var> and <var>y=y_l</var>, and print the sum of these areas modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq n,m \leq 10^5</var></li> <li><var>-10^9 \leq x_1 &lt; ... &lt; x_n \leq 10^9</var></li> <li><var>-10^9 \leq y_1 &lt; ... &lt; y_m \leq 10^9</var></li> <li><var>x_i</var> and <var>y_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>x_1</var> <var>x_2</var> <var>...</var> <var>x_n</var> <var>y_1</var> <var>y_2</var> <var>...</var> <var>y_m</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the total area of the rectangles, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 1 3 4 1 3 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>60 </pre> <p>The following figure illustrates this input:</p> <p><img alt="sample1-1" src="https://atcoder.jp/img/arc071/aec4d5cc2e5c73dbee455be237a649a5.png"/></p> <p>The total area of the nine rectangles A, B, ..., I shown in the following figure, is <var>60</var>.</p> <p><img alt="sample1-2" src="https://atcoder.jp/img/arc071/f0771c0f7e68af2b00e7513186f585ff.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 5 -790013317 -192321079 95834122 418379342 586260100 802780784 -253230108 193944314 363756450 712662868 735867677 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>835067060 </pre></section> </div> </span>
[ [ "3 3\n1 3 4\n1 3 6\n", "3 3\n1 3 4\n1 3 6\n" ] ]
p03765
AtCoder Regular Contest 071 - TrBBnsformBBtion
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Let us consider the following operations on a string consisting of <code>A</code> and <code>B</code>:</p> <ol> <li>Select a character in a string. If it is <code>A</code>, replace it with <code>BB</code>. If it is <code>B</code>, replace with <code>AA</code>.</li> <li>Select a substring that is equal to either <code>AAA</code> or <code>BBB</code>, and delete it from the string.</li> </ol> <p>For example, if the first operation is performed on <code>ABA</code> and the first character is selected, the string becomes <code>BBBA</code>. If the second operation is performed on <code>BBBAAAA</code> and the fourth through sixth characters are selected, the string becomes <code>BBBA</code>.</p> <p>These operations can be performed any number of times, in any order.</p> <p>You are given two string <var>S</var> and <var>T</var>, and <var>q</var> queries <var>a_i, b_i, c_i, d_i</var>. For each query, determine whether <var>S_{a_i} S_{{a_i}+1} ... S_{b_i}</var>, a substring of <var>S</var>, can be made into <var>T_{c_i} T_{{c_i}+1} ... T_{d_i}</var>, a substring of <var>T</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq |S|, |T| \leq 10^5</var></li> <li><var>S</var> and <var>T</var> consist of letters <code>A</code> and <code>B</code>.</li> <li><var>1 \leq q \leq 10^5</var></li> <li><var>1 \leq a_i \leq b_i \leq |S|</var></li> <li><var>1 \leq c_i \leq d_i \leq |T|</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>S</var> <var>T</var> <var>q</var> <var>a_1</var> <var>b_1</var> <var>c_1</var> <var>d_1</var> <var>...</var> <var>a_q</var> <var>b_q</var> <var>c_q</var> <var>d_q</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>q</var> lines. The <var>i</var>-th line should contain the response to the <var>i</var>-th query. If <var>S_{a_i} S_{{a_i}+1} ... S_{b_i}</var> can be made into <var>T_{c_i} T_{{c_i}+1} ... T_{d_i}</var>, print <code>YES</code>. Otherwise, print <code>NO</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>BBBAAAABA BBBBA 4 7 9 2 5 7 9 1 4 1 7 2 5 1 7 2 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>YES NO YES NO </pre> <p>The first query asks whether the string <code>ABA</code> can be made into <code>BBBA</code>. As explained in the problem statement, it can be done by the first operation.</p> <p>The second query asks whether <code>ABA</code> can be made into <code>BBBB</code>, and the fourth query asks whether <code>BBBAAAA</code> can be made into <code>BBB</code>. Neither is possible.</p> <p>The third query asks whether the string <code>BBBAAAA</code> can be made into <code>BBBA</code>. As explained in the problem statement, it can be done by the second operation.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>AAAAABBBBAAABBBBAAAA BBBBAAABBBBBBAAAAABB 10 2 15 2 13 2 13 6 16 1 13 2 20 4 20 3 20 1 18 9 19 2 14 1 11 3 20 3 15 6 16 1 17 4 18 8 20 7 20 3 14 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>YES YES YES YES YES YES NO NO NO NO </pre></section> </div> </span>
[ [ "BBBAAAABA\nBBBBA\n4\n7 9 2 5\n7 9 1 4\n1 7 2 5\n1 7 2 4\n", "BBBAAAABA\nBBBBA\n4\n7 9 2 5\n7 9 1 4\n1 7 2 5\n1 7 2 4\n" ] ]
p03766
AtCoder Regular Contest 071 - Infinite Sequence
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>How many infinite sequences <var>a_1, a_2, ...</var> consisting of {<var>{1, ... ,n}</var>} satisfy the following conditions?</p> <ul> <li>The <var>n</var>-th and subsequent elements are all equal. That is, if <var>n \leq i,j</var>, <var>a_i = a_j</var>.</li> <li>For every integer <var>i</var>, the <var>a_i</var> elements immediately following the <var>i</var>-th element are all equal. That is, if <var>i &lt; j &lt; k\leq i+a_i</var>, <var>a_j = a_k</var>.</li> </ul> <p>Find the count modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \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 how many sequences satisfy the conditions, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>The four sequences that satisfy the conditions are:</p> <ul> <li><var>1, 1, 1, ...</var></li> <li><var>1, 2, 2, ...</var></li> <li><var>2, 1, 1, ...</var></li> <li><var>2, 2, 2, ...</var></li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>654321 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>968545283 </pre></section> </div> </span>
[ [ "2\n", "2\n" ] ]
p03767
AtCoder Grand Contest 012 - AtCoder Group Contest
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>3N</var> participants in <em>AtCoder Group Contest</em>. The <em>strength</em> of the <var>i</var>-th participant is represented by an integer <var>a_i</var>. They will form <var>N</var> teams, each consisting of three participants. No participant may belong to multiple teams.</p> <p>The strength of a team is defined as the second largest strength among its members. For example, a team of participants of strength <var>1</var>, <var>5</var>, <var>2</var> has a strength <var>2</var>, and a team of three participants of strength <var>3</var>, <var>2</var>, <var>3</var> has a strength <var>3</var>.</p> <p>Find the maximum possible sum of the strengths of <var>N</var> teams.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 10^5</var></li> <li><var>1 ≤ a_i ≤ 10^{9}</var></li> <li><var>a_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>a_1</var> <var>a_2</var> <var>...</var> <var>a_{3N}</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 5 2 8 5 1 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>10 </pre> <p>The following is one formation of teams that maximizes the sum of the strengths of teams:</p> <ul> <li>Team <var>1</var>: consists of the first, fourth and fifth participants.</li> <li>Team <var>2</var>: consists of the second, third and sixth participants.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>10000000000 </pre> <p>The sum of the strengths can be quite large.</p></section> </div> </span>
[ [ "2\n5 2 8 5 1 5\n", "2\n5 2 8 5 1 5\n" ] ]
p03768
AtCoder Grand Contest 012 - Splatter Painting
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Squid loves painting vertices in graphs.</p> <p>There is a simple undirected graph consisting of <var>N</var> vertices numbered <var>1</var> through <var>N</var>, and <var>M</var> edges. Initially, all the vertices are painted in color <var>0</var>. The <var>i</var>-th edge bidirectionally connects two vertices <var>a_i</var> and <var>b_i</var>. The length of every edge is <var>1</var>.</p> <p>Squid performed <var>Q</var> operations on this graph. In the <var>i</var>-th operation, he repaints all the vertices within a distance of <var>d_i</var> from vertex <var>v_i</var>, in color <var>c_i</var>.</p> <p>Find the color of each vertex after the <var>Q</var> operations.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N,M,Q ≤ 10^5</var></li> <li><var>1 ≤ a_i,b_i,v_i ≤ N</var></li> <li><var>a_i ≠ b_i</var></li> <li><var>0 ≤ d_i ≤ 10</var></li> <li><var>1 ≤ c_i ≤10^5</var></li> <li><var>d_i</var> and <var>c_i</var> are all integers.</li> <li>There are no self-loops or multiple edges in the given graph.</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li><var>200</var> points will be awarded for passing the testset satisfying <var>1 ≤ N,M,Q ≤ 2{,}000</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>:</var> <var>a_{M}</var> <var>b_{M}</var> <var>Q</var> <var>v_1</var> <var>d_1</var> <var>c_1</var> <var>:</var> <var>v_{Q}</var> <var>d_{Q}</var> <var>c_{Q}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer in <var>N</var> lines. In the <var>i</var>-th line, print the color of vertex <var>i</var> after the <var>Q</var> operations.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>7 7 1 2 1 3 1 4 4 5 5 6 5 7 2 3 2 6 1 1 1 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 2 2 2 2 1 0 </pre> <p>Initially, each vertex is painted in color <var>0</var>. In the first operation, vertices <var>5</var> and <var>6</var> are repainted in color <var>1</var>. In the second operation, vertices <var>1</var>, <var>2</var>, <var>3</var>, <var>4</var> and <var>5</var> are repainted in color <var>2</var>. <div style="text-align: center;"> <img alt="2ab7e180230b159d42d35ea7e555b3b0.png" src="https://atcoder.jp/img/agc012/2ab7e180230b159d42d35ea7e555b3b0.png"> </img></div></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>14 10 1 4 5 7 7 11 4 10 14 7 14 3 6 14 8 11 5 13 8 3 8 8 6 2 9 7 85 6 9 3 6 7 5 10 3 1 12 9 4 9 6 6 8 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 0 3 1 5 5 3 3 6 1 3 4 5 3 </pre> <p>The given graph may not be connected.</p></section> </div> </span>
[ [ "7 7\n1 2\n1 3\n1 4\n4 5\n5 6\n5 7\n2 3\n2\n6 1 1\n1 2 2\n", "7 7\n1 2\n1 3\n1 4\n4 5\n5 6\n5 7\n2 3\n2\n6 1 1\n1 2 2\n" ] ]
p03769
AtCoder Grand Contest 012 - Tautonym Puzzle
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We will call a string <var>x</var> <em>good</em> if it satisfies the following condition:</p> <ul> <li>Condition: <var>x</var> can be represented as a concatenation of two copies of another string <var>y</var> of length at least <var>1</var>.</li> </ul> <p>For example, <code>aa</code> and <code>bubobubo</code> are good; an empty string, <code>a</code>, <code>abcabcabc</code> and <code>abba</code> are not good.</p> <p>Eagle and Owl created a puzzle on good strings. Find one string <var>s</var> that satisfies the following conditions. It can be proved that such a string always exists under the constraints in this problem.</p> <ul> <li><var>1 ≤ |s| ≤ 200</var></li> <li>Each character of <var>s</var> is one of the <var>100</var> characters represented by the integers <var>1</var> through <var>100</var>.</li> <li>Among the <var>2^{|s|}</var> subsequences of <var>s</var>, exactly <var>N</var> are good strings.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 10^{12}</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>Input is given from Standard Input in the following format:</p> <pre><var>N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>In the first line, print <var>|s|</var>, the length of <var>s</var>. In the second line, print the elements in <var>s</var> in order, with spaces in between. Any string that satisfies the above conditions will be accepted.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 1 1 1 1 </pre> <p>There are two good strings that appear as subsequences of <var>s</var>: <var>(1,1)</var> and <var>(1,1,1,1)</var>. There are six occurrences of <var>(1,1)</var> and one occurrence of <var>(1,1,1,1)</var>, for a total of seven.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>299 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>23 32 11 11 73 45 8 11 83 83 8 45 32 32 10 100 73 32 83 45 73 32 11 10 </pre></section> </div> </span>
[ [ "7\n", "7\n" ] ]
p03770
AtCoder Grand Contest 012 - Colorful Balls
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke arranged <var>N</var> colorful balls in a row. The <var>i</var>-th ball from the left has color <var>c_i</var> and weight <var>w_i</var>.</p> <p>He can rearrange the balls by performing the following two operations any number of times, in any order:</p> <ul> <li>Operation <var>1</var>: Select two balls with the same color. If the total weight of these balls is at most <var>X</var>, swap the positions of these balls.</li> <li>Operation <var>2</var>: Select two balls with different colors. If the total weight of these balls is at most <var>Y</var>, swap the positions of these balls.</li> </ul> <p>How many different sequences of colors of balls can be obtained? Find the count modulo <var>10^9 + 7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 2 × 10^5</var></li> <li><var>1 ≤ X, Y ≤ 10^9</var></li> <li><var>1 ≤ c_i ≤ N</var></li> <li><var>1 ≤ w_i ≤ 10^9</var></li> <li><var>X, Y, c_i, w_i</var> are all 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>Y</var> <var>c_1</var> <var>w_1</var> <var>:</var> <var>c_N</var> <var>w_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 7 3 3 2 4 3 2 1 4 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <ul> <li>The sequence of colors <var>(2,4,3,4)</var> can be obtained by swapping the positions of the first and third balls by operation <var>2</var>.</li> <li>It is also possible to swap the positions of the second and fourth balls by operation <var>1</var>, but it does not affect the sequence of colors.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 1 1 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>21 77 68 16 73 16 99 19 66 2 87 2 16 7 17 10 36 10 68 2 38 10 74 13 55 21 21 3 7 12 41 13 88 18 6 2 12 13 87 1 9 2 27 13 15 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>129729600 </pre></section> </div> </span>
[ [ "4 7 3\n3 2\n4 3\n2 1\n4 4\n", "4 7 3\n3 2\n4 3\n2 1\n4 4\n" ] ]
p03771
AtCoder Grand Contest 012 - Camel and Oases
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> oases on a number line. The coordinate of the <var>i</var>-th oases from the left is <var>x_i</var>.</p> <p>Camel hopes to visit all these oases. Initially, the volume of the hump on his back is <var>V</var>. When the volume of the hump is <var>v</var>, water of volume at most <var>v</var> can be stored. Water is only supplied at oases. He can get as much water as he can store at a oasis, and the same oasis can be used any number of times.</p> <p>Camel can travel on the line by either walking or jumping:</p> <ul> <li>Walking over a distance of <var>d</var> costs water of volume <var>d</var> from the hump. A walk that leads to a negative amount of stored water cannot be done.</li> <li>Let <var>v</var> be the amount of water stored at the moment. When <var>v&gt;0</var>, Camel can jump to any point on the line of his choice. After this move, the volume of the hump becomes <var>v/2</var> (rounded down to the nearest integer), and the amount of stored water becomes <var>0</var>.</li> </ul> <p>For each of the oases, determine whether it is possible to start from that oasis and visit all the oases.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N,V ≤ 2 × 10^5</var></li> <li><var>-10^9 ≤ x_1 &lt; x_2 &lt; ... &lt; x_N ≤ 10^9</var></li> <li><var>V</var> and <var>x_i</var> are all 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>V</var> <var>x_1</var> <var>x_2</var> <var>...</var> <var>x_{N}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>N</var> lines. The <var>i</var>-th line should contain <code>Possible</code> if it is possible to start from the <var>i</var>-th oasis and visit all the oases, and <code>Impossible</code> otherwise.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 1 3 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Possible Possible Possible </pre> <p>It is possible to start from the first oasis and visit all the oases, as follows:</p> <ul> <li>Walk from the first oasis to the second oasis. The amount of stored water becomes <var>0</var>.</li> <li>Get water at the second oasis. The amount of stored water becomes <var>2</var>.</li> <li>Jump from the second oasis to the third oasis. The amount of stored water becomes <var>0</var>, and the volume of the hump becomes <var>1</var>.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 2 -10 -4 -2 0 2 4 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>Impossible Possible Possible Possible Possible Possible Impossible </pre> <p>A oasis may be visited any number of times.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>16 19 -49 -48 -33 -30 -21 -14 0 15 19 23 44 52 80 81 82 84 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Possible Possible Possible Possible Possible Possible Possible Possible Possible Possible Possible Possible Impossible Impossible Impossible Impossible </pre></section> </div> </span>
[ [ "3 2\n1 3 6\n", "3 2\n1 3 6\n" ] ]
p03772
AtCoder Grand Contest 012 - Prefix Median
<span class="lang-en"> <p>Score : <var>2000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke received an integer sequence <var>a</var> of length <var>2N-1</var>.</p> <p>He arbitrarily permuted the elements in <var>a</var>, then used it to construct a new integer sequence <var>b</var> of length <var>N</var>, as follows:</p> <ul> <li><var>b_1 =</var> the median of <var>(a_1)</var></li> <li><var>b_2 =</var> the median of <var>(a_1, a_2, a_3)</var></li> <li><var>b_3 =</var> the median of <var>(a_1, a_2, a_3, a_4, a_5)</var></li> <li><var>:</var></li> <li><var>b_N =</var> the median of <var>(a_1, a_2, a_3, ..., a_{2N-1})</var></li> </ul> <p>How many different sequences can be obtained as <var>b</var>? Find the count modulo <var>10^{9} + 7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 50</var></li> <li><var>1 ≤ a_i ≤ 2N-1</var></li> <li><var>a_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>a_1</var> <var>a_2</var> <var>...</var> <var>a_{2N-1}</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 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>There are three sequences that can be obtained as <var>b</var>: <var>(1,2)</var>, <var>(2,2)</var> and <var>(3,2)</var>. Each of these can be constructed from <var>(1,2,3)</var>, <var>(2,1,3)</var> and <var>(3,1,2)</var>, respectively.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 1 3 2 3 2 4 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>16 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>15 1 5 9 11 1 19 17 18 20 1 14 3 3 8 19 15 16 29 10 2 4 13 6 12 7 15 16 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>911828634 </pre> <p>Print the count modulo <var>10^{9} + 7</var>.</p></section> </div> </span>
[ [ "2\n1 3 2\n", "2\n1 3 2\n" ] ]
p03773
AtCoder Beginner Contest 057 - Remaining Time
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Dolphin loves programming contests. Today, he will take part in a contest in AtCoder.<br/> In this country, 24-hour clock is used. For example, <var>9:00</var> p.m. is referred to as "<var>21</var> o'clock".<br/> The current time is <var>A</var> o'clock, and a contest will begin in exactly <var>B</var> hours. When will the contest begin? Answer in 24-hour time.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>0 \leq A,B \leq 23</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>The input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the hour of the starting time of the contest in 24-hour time.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>9 12 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>21 </pre> <p>In this input, the current time is <var>9</var> o'clock, and <var>12</var> hours later it will be <var>21</var> o'clock in 24-hour time.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>19 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>19 </pre> <p>The contest has just started.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>23 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> <p>The contest will begin at <var>1</var> o'clock the next day.</p></section> </div> </span>
[ [ "9 12\n", "9 12\n" ] ]
p03774
AtCoder Beginner Contest 057 - Checkpoints
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> students and <var>M</var> checkpoints on the <var>xy</var>-plane.<br/> The coordinates of the <var>i</var>-th student <var>(1 \leq i \leq N)</var> is <var>(a_i,b_i)</var>, and the coordinates of the checkpoint numbered <var>j</var> <var>(1 \leq j \leq M)</var> is <var>(c_j,d_j)</var>.<br/> When the teacher gives a signal, each student has to go to the nearest checkpoint measured in <em>Manhattan distance</em>. <br/> The Manhattan distance between two points <var>(x_1,y_1)</var> and <var>(x_2,y_2)</var> is <var>|x_1-x_2|+|y_1-y_2|</var>.<br/> Here, <var>|x|</var> denotes the absolute value of <var>x</var>.<br/> If there are multiple nearest checkpoints for a student, he/she will select the checkpoint with the smallest index.<br/> Which checkpoint will each student go to?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N,M \leq 50</var></li> <li><var>-10^8 \leq a_i,b_i,c_j,d_j \leq 10^8</var></li> <li>All input values are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>a_1</var> <var>b_1</var> <var>:</var> <var>a_N</var> <var>b_N</var> <var>c_1</var> <var>d_1</var> <var>:</var> <var>c_M</var> <var>d_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>N</var> lines.<br/> The <var>i</var>-th line <var>(1 \leq i \leq N)</var> should contain the index of the checkpoint for the <var>i</var>-th student to go.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 2 2 0 0 0 -1 0 1 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 1 </pre> <p>The Manhattan distance between the first student and each checkpoint is:</p> <ul> <li>For checkpoint <var>1</var>: <var>|2-(-1)|+|0-0|=3</var></li> <li>For checkpoint <var>2</var>: <var>|2-1|+|0-0|=1</var></li> </ul> <p>The nearest checkpoint is checkpoint <var>2</var>. Thus, the first line in the output should contain <var>2</var>.</p> <p>The Manhattan distance between the second student and each checkpoint is:</p> <ul> <li>For checkpoint <var>1</var>: <var>|0-(-1)|+|0-0|=1</var></li> <li>For checkpoint <var>2</var>: <var>|0-1|+|0-0|=1</var></li> </ul> <p>When there are multiple nearest checkpoints, the student will go to the checkpoint with the smallest index. Thus, the second line in the output should contain <var>1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 4 10 10 -10 -10 3 3 1 2 2 3 3 5 3 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 1 2 </pre> <p>There can be multiple checkpoints at the same coordinates.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 5 -100000000 -100000000 -100000000 100000000 100000000 -100000000 100000000 100000000 0 0 0 0 100000000 100000000 100000000 -100000000 -100000000 100000000 -100000000 -100000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>5 4 3 2 1 </pre></section> </div> </span>
[ [ "2 2\n2 0\n0 0\n-1 0\n1 0\n", "2 2\n2 0\n0 0\n-1 0\n1 0\n" ] ]
p03775
AtCoder Beginner Contest 057 - Digits in Multiplication
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an integer <var>N</var>.<br/> For two positive integers <var>A</var> and <var>B</var>, we will define <var>F(A,B)</var> as the larger of the following: the number of digits in the decimal notation of <var>A</var>, and the number of digits in the decimal notation of <var>B</var>.<br/> For example, <var>F(3,11) = 2</var> since <var>3</var> has one digit and <var>11</var> has two digits.<br/> Find the minimum value of <var>F(A,B)</var> as <var>(A,B)</var> ranges over all pairs of positive integers such that <var>N = A \times B</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 10^{10}</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>The input is given from Standard Input in the following format:</p> <pre><var>N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum value of <var>F(A,B)</var> as <var>(A,B)</var> ranges over all pairs of positive integers such that <var>N = A \times B</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>10000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p><var>F(A,B)</var> has a minimum value of <var>3</var> at <var>(A,B)=(100,100)</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1000003 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>7 </pre> <p>There are two pairs <var>(A,B)</var> that satisfy the condition: <var>(1,1000003)</var> and <var>(1000003,1)</var>. For these pairs, <var>F(1,1000003)=F(1000003,1)=7</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>9876543210 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>6 </pre></section> </div> </span>
[ [ "10000\n", "10000\n" ] ]
p03776
AtCoder Beginner Contest 057 - Maximum Average Sets
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given <var>N</var> items.<br/> The <em>value</em> of the <var>i</var>-th item <var>(1 \leq i \leq N)</var> is <var>v_i</var>.<br/> Your have to select at least <var>A</var> and at most <var>B</var> of these items.<br/> Under this condition, find the maximum possible arithmetic mean of the values of selected items.<br/> Additionally, find the number of ways to select items so that the mean of the values of selected items is maximized. </p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 50</var></li> <li><var>1 \leq A,B \leq N</var></li> <li><var>1 \leq v_i \leq 10^{15}</var></li> <li>Each <var>v_i</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A</var> <var>B</var> <var>v_1</var> <var>v_2</var> ... <var>v_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print two lines.<br/> The first line should contain the maximum possible arithmetic mean of the values of selected items. The output should be considered correct if the absolute or relative error is at most <var>10^{-6}</var>.<br/> The second line should contain the number of ways to select items so that the mean of the values of selected items is maximized.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 2 2 1 2 3 4 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4.500000 1 </pre> <p>The mean of the values of selected items will be maximized when selecting the fourth and fifth items. Hence, the first line of the output should contain <var>4.5</var>.<br/> There is no other way to select items so that the mean of the values will be <var>4.5</var>, and thus the second line of the output should contain <var>1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 2 3 10 20 10 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>15.000000 3 </pre> <p>There can be multiple ways to select items so that the mean of the values will be maximized.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 1 5 1000000000000000 999999999999999 999999999999998 999999999999997 999999999999996 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1000000000000000.000000 1 </pre></section> </div> </span>
[ [ "5 2 2\n1 2 3 4 5\n", "5 2 2\n1 2 3 4 5\n" ] ]
p03777
AtCoder Beginner Contest 056 - HonestOrDishonest
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Two deer, AtCoDeer and TopCoDeer, are playing a game called <em>Honest or Dishonest</em>. In this game, an honest player always tells the truth, and an dishonest player always tell lies. You are given two characters <var>a</var> and <var>b</var> as the input. Each of them is either <code>H</code> or <code>D</code>, and carries the following information:</p> <p>If <var>a</var>=<code>H</code>, AtCoDeer is honest; if <var>a</var>=<code>D</code>, AtCoDeer is dishonest. If <var>b</var>=<code>H</code>, AtCoDeer is saying that TopCoDeer is honest; if <var>b</var>=<code>D</code>, AtCoDeer is saying that TopCoDeer is dishonest.</p> <p>Given this information, determine whether TopCoDeer is honest.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>a</var>=<code>H</code> or <var>a</var>=<code>D</code>.</li> <li><var>b</var>=<code>H</code> or <var>b</var>=<code>D</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>a</var> <var>b</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If TopCoDeer is honest, print <code>H</code>. If he is dishonest, print <code>D</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>H H </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>H </pre> <p>In this input, AtCoDeer is honest. Hence, as he says, TopCoDeer is honest.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>D H </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>D </pre> <p>In this input, AtCoDeer is dishonest. Hence, contrary to what he says, TopCoDeer is dishonest.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>D D </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>H </pre></section> </div> </span>
[ [ "H H\n", "H H\n" ] ]
p03778
AtCoder Beginner Contest 056 - NarrowRectanglesEasy
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>AtCoDeer the deer found two rectangles lying on the table, each with height <var>1</var> and width <var>W</var>. If we consider the surface of the desk as a two-dimensional plane, the first rectangle covers the vertical range of <var>[0,1]</var> and the horizontal range of <var>[a,a+W]</var>, and the second rectangle covers the vertical range of <var>[1,2]</var> and the horizontal range of <var>[b,b+W]</var>, as shown in the following figure:</p> <p><img alt="" src="https://atcoder.jp/img/abc056/5c3a0ed9a07aa0992011c11ffbc9441b.png"/></p> <p>AtCoDeer will move the second rectangle horizontally so that it connects with the first rectangle. Find the minimum distance it needs to be moved.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>1≤W≤10^5</var></li> <li><var>1≤a,b≤10^5</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>W</var> <var>a</var> <var>b</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum distance the second rectangle needs to be moved.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 </pre> <p>This input corresponds to the figure in the statement. In this case, the second rectangle should be moved to the left by a distance of <var>1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 1 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>The rectangles are already connected, and thus no move is needed.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 10 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>4 </pre></section> </div> </span>
[ [ "3 2 6\n", "3 2 6\n" ] ]
p03779
AtCoder Beginner Contest 056 - Go Home
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a kangaroo at coordinate <var>0</var> on an infinite number line that runs from left to right, at time <var>0</var>. During the period between time <var>i-1</var> and time <var>i</var>, the kangaroo can either stay at his position, or perform a jump of length exactly <var>i</var> to the left or to the right. That is, if his coordinate at time <var>i-1</var> is <var>x</var>, he can be at coordinate <var>x-i</var>, <var>x</var> or <var>x+i</var> at time <var>i</var>. The kangaroo's nest is at coordinate <var>X</var>, and he wants to travel to coordinate <var>X</var> as fast as possible. Find the earliest possible time to reach coordinate <var>X</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>X</var> is an integer.</li> <li><var>1≤X≤10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>X</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the earliest possible time for the kangaroo to reach coordinate <var>X</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>The kangaroo can reach his nest at time <var>3</var> by jumping to the right three times, which is the earliest possible time.</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>2 </pre> <p>He can reach his nest at time <var>2</var> by staying at his position during the first second, and jumping to the right at the next second.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>11 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>5 </pre></section> </div> </span>
[ [ "6\n", "6\n" ] ]
p03780
AtCoder Beginner Contest 056 - No Need
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>AtCoDeer the deer has <var>N</var> cards with positive integers written on them. The number on the <var>i</var>-th card <var>(1≤i≤N)</var> is <var>a_i</var>. Because he loves big numbers, he calls a subset of the cards <em>good</em> when the sum of the numbers written on the cards in the subset, is <var>K</var> or greater.</p> <p>Then, for each card <var>i</var>, he judges whether it is <em>unnecessary</em> or not, as follows:</p> <ul> <li>If, for any good subset of the cards containing card <var>i</var>, the set that can be obtained by eliminating card <var>i</var> from the subset is also good, card <var>i</var> is unnecessary.</li> <li>Otherwise, card <var>i</var> is NOT unnecessary.</li> </ul> <p>Find the number of the unnecessary cards. Here, he judges each card independently, and he does not throw away cards that turn out to be unnecessary.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>1≤N≤5000</var></li> <li><var>1≤K≤5000</var></li> <li><var>1≤a_i≤10^9 (1≤i≤N)</var></li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li><var>300</var> points will be awarded for passing the test set satisfying <var>N,K≤400</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>a_1</var> <var>a_2</var> ... <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the unnecessary cards.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 6 1 4 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 </pre> <p>There are two good sets: {<var>2,3</var>} and {<var>1,2,3</var>}.</p> <p>Card <var>1</var> is only contained in {<var>1,2,3</var>}, and this set without card <var>1</var>, {<var>2,3</var>}, is also good. Thus, card <var>1</var> is unnecessary.</p> <p>For card <var>2</var>, a good set {<var>2,3</var>} without card <var>2</var>, {<var>3</var>}, is not good. Thus, card <var>2</var> is NOT unnecessary.</p> <p>Neither is card <var>3</var> for a similar reason, hence the answer is <var>1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 400 3 1 4 1 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>5 </pre> <p>In this case, there is no good set. Therefore, all the cards are unnecessary.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 20 10 4 3 10 25 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>3 </pre></section> </div> </span>
[ [ "3 6\n1 4 3\n", "3 6\n1 4 3\n" ] ]
p03781
AtCoder Regular Contest 070 - Go Home
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a kangaroo at coordinate <var>0</var> on an infinite number line that runs from left to right, at time <var>0</var>. During the period between time <var>i-1</var> and time <var>i</var>, the kangaroo can either stay at his position, or perform a jump of length exactly <var>i</var> to the left or to the right. That is, if his coordinate at time <var>i-1</var> is <var>x</var>, he can be at coordinate <var>x-i</var>, <var>x</var> or <var>x+i</var> at time <var>i</var>. The kangaroo's nest is at coordinate <var>X</var>, and he wants to travel to coordinate <var>X</var> as fast as possible. Find the earliest possible time to reach coordinate <var>X</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>X</var> is an integer.</li> <li><var>1≤X≤10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>X</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the earliest possible time for the kangaroo to reach coordinate <var>X</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>The kangaroo can reach his nest at time <var>3</var> by jumping to the right three times, which is the earliest possible time.</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>2 </pre> <p>He can reach his nest at time <var>2</var> by staying at his position during the first second, and jumping to the right at the next second.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>11 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>5 </pre></section> </div> </span>
[ [ "6\n", "6\n" ] ]
p03782
AtCoder Regular Contest 070 - No Need
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>AtCoDeer the deer has <var>N</var> cards with positive integers written on them. The number on the <var>i</var>-th card <var>(1≤i≤N)</var> is <var>a_i</var>. Because he loves big numbers, he calls a subset of the cards <em>good</em> when the sum of the numbers written on the cards in the subset, is <var>K</var> or greater.</p> <p>Then, for each card <var>i</var>, he judges whether it is <em>unnecessary</em> or not, as follows:</p> <ul> <li>If, for any good subset of the cards containing card <var>i</var>, the set that can be obtained by eliminating card <var>i</var> from the subset is also good, card <var>i</var> is unnecessary.</li> <li>Otherwise, card <var>i</var> is NOT unnecessary.</li> </ul> <p>Find the number of the unnecessary cards. Here, he judges each card independently, and he does not throw away cards that turn out to be unnecessary.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>1≤N≤5000</var></li> <li><var>1≤K≤5000</var></li> <li><var>1≤a_i≤10^9 (1≤i≤N)</var></li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li><var>300</var> points will be awarded for passing the test set satisfying <var>N,K≤400</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>a_1</var> <var>a_2</var> ... <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the unnecessary cards.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 6 1 4 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 </pre> <p>There are two good sets: {<var>2,3</var>} and {<var>1,2,3</var>}.</p> <p>Card <var>1</var> is only contained in {<var>1,2,3</var>}, and this set without card <var>1</var>, {<var>2,3</var>}, is also good. Thus, card <var>1</var> is unnecessary.</p> <p>For card <var>2</var>, a good set {<var>2,3</var>} without card <var>2</var>, {<var>3</var>}, is not good. Thus, card <var>2</var> is NOT unnecessary.</p> <p>Neither is card <var>3</var> for a similar reason, hence the answer is <var>1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 400 3 1 4 1 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>5 </pre> <p>In this case, there is no good set. Therefore, all the cards are unnecessary.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 20 10 4 3 10 25 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>3 </pre></section> </div> </span>
[ [ "3 6\n1 4 3\n", "3 6\n1 4 3\n" ] ]
p03783
AtCoder Regular Contest 070 - NarrowRectangles
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>AtCoDeer the deer found <var>N</var> rectangle lying on the table, each with height <var>1</var>. If we consider the surface of the desk as a two-dimensional plane, the <var>i</var>-th rectangle <var>i(1≤i≤N)</var> covers the vertical range of <var>[i-1,i]</var> and the horizontal range of <var>[l_i,r_i]</var>, as shown in the following figure:</p> <p><img alt="" src="https://atcoder.jp/img/arc070/46b7dc61fc2016c9b45f10db46c6fce9.png"/></p> <p>AtCoDeer will move these rectangles horizontally so that all the rectangles are connected. For each rectangle, the cost to move it horizontally by a distance of <var>x</var>, is <var>x</var>. Find the minimum cost to achieve connectivity. It can be proved that this value is always an integer under the constraints of the problem.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>1≤N≤10^5</var></li> <li><var>1≤l_i&lt;r_i≤10^9</var></li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li><var>300</var> points will be awarded for passing the test set satisfying <var>1≤N≤400</var> and <var>1≤l_i&lt;r_i≤400</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>l_1</var> <var>r_1</var> <var>l_2</var> <var>r_2</var> : <var>l_N</var> <var>r_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum cost to achieve connectivity.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 3 5 7 1 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The second rectangle should be moved to the left by a distance of <var>2</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 2 5 4 6 1 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>The rectangles are already connected, and thus no move is needed.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 999999999 1000000000 1 2 314 315 500000 500001 999999999 1000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1999999680 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>5 123456 789012 123 456 12 345678901 123456 789012 1 23 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>246433 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>1 1 400 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>0 </pre></section> </div> </span>
[ [ "3\n1 3\n5 7\n1 3\n", "3\n1 3\n5 7\n1 3\n" ] ]
p03784
AtCoder Regular Contest 070 - HonestOrUnkind
<span class="lang-en"> <p>Score : <var>1300</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>AtCoDeer the deer came across <var>N</var> people. For convenience, the people are numbered <var>0</var> through <var>N-1</var>. Among them, <var>A</var> are <em>honest</em> and the remaining <var>B(=N-A)</var> are <em>unkind</em>. All of these <var>N</var> people know who are honest and who are unkind, but AtCoDeer only knows that there are <var>A</var> honest and <var>B</var> unkind people. He is trying to identify all of the honest people by asking questions to these <var>N</var> people. For one question, AtCoDeer selects <var>a</var> and <var>b</var> <var>(0≤a,b≤N-1)</var>, and asks person <var>a</var> the following question: "Is person <var>b</var> honest?"</p> <p>An honest person will always answer correctly by "Yes" or "No". An unkind person, however, will answer by selecting "Yes" or "No" <strong>arbitrarily</strong>. That is, the algorithm used by an unkind person may not be simple one such as always lying or giving random fifty-fifty answers.</p> <p>AtCoDeer can ask at most <var>2N</var> questions. He will ask questions one by one, and the responses to the previous questions can be used when deciding the next question to ask.</p> <p>Identify all of the honest people. If it is impossible (more formally, if, for any strategy of asking <var>2N</var> questions, there exists a strategy for unkind people to answer the questions so that there are two or more possible sets of the honest people), report that fact.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≤A,B≤2000</var></li> </ul> </section> </div> <div class="part"> <section> <h3>Input and Output</h3><p>First, <var>A</var> and <var>B</var> are given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> </pre> <p>If identifying the honest people is impossible, the program must immediately print the following output and terminate itself:</p> <pre>Impossible </pre> <p>Otherwise, the program shall ask questions. Each question must be written to Standard Output in the following format:</p> <pre>? <var>a</var> <var>b</var> </pre> <p>Here, <var>a</var> and <var>b</var> must be integers between <var>0</var> and <var>N-1</var> (inclusive). The response to the question will be given from Standard Input in the following format:</p> <pre><var>ans</var> </pre> <p>Here, <var>ans</var> is either <code>Y</code> or <code>N</code>. <code>Y</code> represents "Yes"; <code>N</code> represents "No".</p> <p>Finally, the answer must be written to Standard Output in the following format:</p> <pre>! <var>s_0s_1...s_{N-1}</var> </pre> <p>Here, <var>s_i</var> must be <code>1</code> if person <var>i</var> is honest, and <code>0</code> if person <var>i</var> is unkind.</p> </section> </div> <div class="part"> <section> <h3>Judgement</h3><ul> <li><font color="red"><strong>After each output, you must flush Standard Output.</strong></font> Otherwise you may get <code>TLE</code>.</li> <li>After you print the answer, the program must be terminated immediately. Otherwise, the behavior of the judge is undefined.</li> <li>When your output is invalid or incorrect, the behavior of the judge is undefined (it does not necessarily give <code>WA</code>).</li> </ul> </section> </div> <div class="part"> <section> <h3>Samples</h3><p>In the following sample, <var>A = 2</var>, <var>B = 1</var>, and the answer is <code>101</code>.</p> <table class="table table-bordered"> <thead> <tr> <th align="left">Input</th> <th align="left">Output</th> </tr> </thead> <tbody> <tr> <td align="left"><var>2</var> <var>1</var></td> <td align="left"></td> </tr> <tr> <td align="left"></td> <td align="left"><var>?</var> <var>0</var> <var>1</var></td> </tr> <tr> <td align="left"><var>N</var></td> <td align="left"></td> </tr> <tr> <td align="left"></td> <td align="left"><var>?</var> <var>0</var> <var>2</var></td> </tr> <tr> <td align="left"><var>Y</var></td> <td align="left"></td> </tr> <tr> <td align="left"></td> <td align="left"><var>?</var> <var>1</var> <var>0</var></td> </tr> <tr> <td align="left"><var>Y</var></td> <td align="left"></td> </tr> <tr> <td align="left"></td> <td align="left"><var>?</var> <var>2</var> <var>0</var></td> </tr> <tr> <td align="left"><var>Y</var></td> <td align="left"></td> </tr> <tr> <td align="left"></td> <td align="left"><var>?</var> <var>2</var> <var>2</var></td> </tr> <tr> <td align="left"><var>Y</var></td> <td align="left"></td> </tr> <tr> <td align="left"></td> <td align="left"><var>!</var> <var>101</var></td> </tr> </tbody> </table> <p>In the following sample, <var>A = 1</var>, <var>B = 2</var>, and the answer is <code>Impossible</code>.</p> <table class="table table-bordered"> <thead> <tr> <th align="left">Input</th> <th align="left">Output</th> </tr> </thead> <tbody> <tr> <td align="left"><var>1</var> <var>2</var></td> <td align="left"></td> </tr> <tr> <td align="left"></td> <td align="left"><var>Impossible</var></td> </tr> </tbody> </table></section> </div> </span>
[ [ "", "" ] ]
p03785
AtCoder Grand Contest 011 - Airport Bus
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Every day, <var>N</var> passengers arrive at Takahashi Airport. The <var>i</var>-th passenger arrives at time <var>T_i</var>.</p> <p>Every passenger arrived at Takahashi airport travels to the city by bus. Each bus can accommodate up to <var>C</var> passengers. Naturally, a passenger cannot take a bus that departs earlier than the airplane arrives at the airport. Also, a passenger will get angry if he/she is still unable to take a bus <var>K</var> units of time after the arrival of the airplane. For that reason, it is necessary to arrange buses so that the <var>i</var>-th passenger can take a bus departing at time between <var>T_i</var> and <var>T_i + K</var> (inclusive).</p> <p>When setting the departure times for buses under this condition, find the minimum required number of buses. Here, the departure time for each bus does not need to be an integer, and there may be multiple buses that depart at the same time.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 100000</var></li> <li><var>1 \leq C \leq 10^9</var></li> <li><var>1 \leq K \leq 10^9</var></li> <li><var>1 \leq T_i \leq 10^9</var></li> <li><var>C</var>, <var>K</var> and <var>T_i</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>C</var> <var>K</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 required number of buses.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 3 5 1 2 3 6 12 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>For example, the following three buses are enough:</p> <ul> <li>A bus departing at time <var>4.5</var>, that carries the passengers arriving at time <var>2</var> and <var>3</var>.</li> <li>A bus departing at time <var>6</var>, that carries the passengers arriving at time <var>1</var> and <var>6</var>.</li> <li>A bus departing at time <var>12</var>, that carries the passenger arriving at time <var>12</var>.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>6 3 3 7 6 2 8 10 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 </pre></section> </div> </span>
[ [ "5 3 5\n1\n2\n3\n6\n12\n", "5 3 5\n1\n2\n3\n6\n12\n" ] ]
p03786
AtCoder Grand Contest 011 - Colorful Creatures
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke found <var>N</var> strange creatures. Each creature has a fixed color and size. The color and size of the <var>i</var>-th creature are represented by <var>i</var> and <var>A_i</var>, respectively.</p> <p>Every creature can absorb another creature whose size is at most twice the size of itself. When a creature of size <var>A</var> and color <var>B</var> absorbs another creature of size <var>C</var> and color <var>D</var> (<var>C \leq 2 \times A</var>), they will merge into one creature of size <var>A+C</var> and color <var>B</var>. Here, depending on the sizes of two creatures, it is possible that both of them can absorb the other.</p> <p>Snuke has been watching these creatures merge over and over and ultimately become one creature. Find the number of the possible colors of this creature.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 100000</var></li> <li><var>1 \leq A_i \leq 10^9</var></li> <li><var>A_i</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>A_2</var> … <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the possible colors of the last remaining creature after the <var>N</var> creatures repeatedly merge and ultimately become one creature.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 1 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The possible colors of the last remaining creature are colors <var>1</var> and <var>3</var>. For example, when the creature of color <var>3</var> absorbs the creature of color <var>2</var>, then the creature of color <var>1</var> absorbs the creature of color <var>3</var>, the color of the last remaining creature will be color <var>1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 1 1 1 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>5 </pre> <p>There may be multiple creatures of the same size.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 40 1 30 2 7 20 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>4 </pre></section> </div> </span>
[ [ "3\n3 1 4\n", "3\n3 1 4\n" ] ]
p03787
AtCoder Grand Contest 011 - Squared Graph
<span class="lang-en"> <p>Score : <var>800</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi has received an undirected graph with <var>N</var> vertices, numbered <var>1</var>, <var>2</var>, ..., <var>N</var>. The edges in this graph are represented by <var>(u_i, v_i)</var>. There are no self-loops and multiple edges in this graph.</p> <p>Based on this graph, Takahashi is now constructing a new graph with <var>N^2</var> vertices, where each vertex is labeled with a pair of integers <var>(a, b)</var> (<var>1 \leq a \leq N</var>, <var>1 \leq b \leq N</var>). The edges in this new graph are generated by the following rule:</p> <ul> <li>Span an edge between vertices <var>(a, b)</var> and <var>(a', b')</var> if and only if both of the following two edges exist in the original graph: an edge between vertices <var>a</var> and <var>a'</var>, and an edge between vertices <var>b</var> and <var>b'</var>.</li> </ul> <p>How many connected components are there in this new graph?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 \leq N \leq 100,000</var></li> <li><var>0 \leq M \leq 200,000</var></li> <li><var>1 \leq u_i &lt; v_i \leq N</var></li> <li>There exists no pair of distinct integers <var>i</var> and <var>j</var> such that <var>u_i = u_j</var> and <var>v_i = v_j</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>u_1</var> <var>v_1</var> <var>u_2</var> <var>v_2</var> : <var>u_M</var> <var>v_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the connected components in the graph constructed by Takahashi.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>7 </pre> <p>The graph constructed by Takahashi is as follows.</p> <p><img alt="" src="https://atcoder.jp/img/agc011/6d34a4ddeba67b2286c00acda56abbcc.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 5 1 2 3 4 3 5 4 5 2 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>18 </pre></section> </div> </span>
[ [ "3 1\n1 2\n", "3 1\n1 2\n" ] ]
p03788
AtCoder Grand Contest 011 - Half Reflector
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Takahashi has a lot of peculiar devices. These cylindrical devices receive balls from left and right. Each device is in one of the two states A and B, and for each state, the device operates as follows:</p> <ul> <li>When a device in state A receives a ball from either side (left or right), the device throws out the ball from the same side, then immediately goes into state B.</li> <li>When a device in state B receives a ball from either side, the device throws out the ball from the other side, then immediately goes into state A.</li> </ul> <p>The transition of the state of a device happens momentarily and always completes before it receives another ball.</p> <p>Takahashi built a contraption by concatenating <var>N</var> of these devices. In this contraption,</p> <ul> <li>A ball that was thrown out from the right side of the <var>i</var>-th device from the left (<var>1 \leq i \leq N-1</var>) immediately enters the <var>(i+1)</var>-th device from the left side.</li> <li>A ball that was thrown out from the left side of the <var>i</var>-th device from the left (<var>2 \leq i \leq N</var>) immediately enters the <var>(i-1)</var>-th device from the right side.</li> </ul> <p>The initial state of the <var>i</var>-th device from the left is represented by the <var>i</var>-th character in a string <var>S</var>. From this situation, Takahashi performed the following <var>K</var> times: put a ball into the leftmost device from the left side, then wait until the ball comes out of the contraption from either end. Here, it can be proved that the ball always comes out of the contraption after a finite time. Find the state of each device after <var>K</var> balls are processed.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 200,000</var></li> <li><var>1 \leq K \leq 10^9</var></li> <li><var>|S|=N</var></li> <li>Each character in <var>S</var> is either <code>A</code> or <code>B</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>S</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print a string that represents the state of each device after <var>K</var> balls are processed. The string must be <var>N</var> characters long, and the <var>i</var>-th character must correspond to the state of the <var>i</var>-th device from the left.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 1 ABAAA </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>BBAAA </pre> <p>In this input, we put a ball into the leftmost device from the left side, then it is returned from the same place.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 2 ABAAA </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>ABBBA </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 123456789 AABB </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>BABA </pre></section> </div> </span>
[ [ "5 1\nABAAA\n", "5 1\nABAAA\n" ] ]
p03789
AtCoder Grand Contest 011 - Increasing Numbers
<span class="lang-en"> <p>Score : <var>1300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We will call a non-negative integer <em>increasing</em> if, for any two adjacent digits in its decimal representation, the digit to the right is greater than or equal to the digit to the left. For example, <var>1558</var>, <var>11</var>, <var>3</var> and <var>0</var> are all increasing; <var>10</var> and <var>20170312</var> are not.</p> <p>Snuke has an integer <var>N</var>. Find the minimum number of increasing integers that can represent <var>N</var> as their sum.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 10^{500000}</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of increasing integers that can represent <var>N</var> as their sum.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>80 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>One possible representation is <var>80 = 77 + 3</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>123456789 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 </pre> <p><var>123456789</var> in itself is increasing, and thus it can be represented as the sum of one increasing integer.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>20170312 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>4 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>7204647845201772120166980358816078279571541735614841625060678056933503 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>31 </pre></section> </div> </span>
[ [ "80\n", "80\n" ] ]
p03790
AtCoder Grand Contest 011 - Train Service Planning
<span class="lang-en"> <p>Score : <var>1700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a railroad in Takahashi Kingdom. The railroad consists of <var>N</var> sections, numbered <var>1</var>, <var>2</var>, ..., <var>N</var>, and <var>N+1</var> stations, numbered <var>0</var>, <var>1</var>, ..., <var>N</var>. Section <var>i</var> directly connects the stations <var>i-1</var> and <var>i</var>. A train takes exactly <var>A_i</var> minutes to run through section <var>i</var>, regardless of direction. Each of the <var>N</var> sections is either single-tracked over the whole length, or double-tracked over the whole length. If <var>B_i = 1</var>, section <var>i</var> is single-tracked; if <var>B_i = 2</var>, section <var>i</var> is double-tracked. Two trains running in opposite directions can cross each other on a double-tracked section, but not on a single-tracked section. Trains can also cross each other at a station.</p> <p>Snuke is creating the timetable for this railroad. In this timetable, the trains on the railroad run every <var>K</var> minutes, as shown in the following figure. Here, bold lines represent the positions of trains running on the railroad. (See Sample 1 for clarification.)</p> <p><img alt="" src="https://atcoder.jp/img/agc011/a5c221ce77ab6ee8aee48e75a4e5c969.png"/></p> <p>When creating such a timetable, find the minimum sum of the amount of time required for a train to depart station <var>0</var> and reach station <var>N</var>, and the amount of time required for a train to depart station <var>N</var> and reach station <var>0</var>. It can be proved that, if there exists a timetable satisfying the conditions in this problem, this minimum sum is always an integer.</p> <p>Formally, the times at which trains arrive and depart must satisfy the following:</p> <ul> <li>Each train either departs station <var>0</var> and is bound for station <var>N</var>, or departs station <var>N</var> and is bound for station <var>0</var>.</li> <li>Each train takes exactly <var>A_i</var> minutes to run through section <var>i</var>. For example, if a train bound for station <var>N</var> departs station <var>i-1</var> at time <var>t</var>, the train arrives at station <var>i</var> exactly at time <var>t+A_i</var>.</li> <li>Assume that a train bound for station <var>N</var> arrives at a station at time <var>s</var>, and departs the station at time <var>t</var>. Then, the next train bound for station <var>N</var> arrives at the station at time <var>s+K</var>, and departs the station at time <var>t+K</var>. Additionally, the previous train bound for station <var>N</var> arrives at the station at time <var>s-K</var>, and departs the station at time <var>t-K</var>. This must also be true for trains bound for station <var>0</var>.</li> <li>Trains running in opposite directions must not be running on the same single-tracked section (except the stations at both ends) at the same time.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 \leq N \leq 100000</var></li> <li><var>1 \leq K \leq 10^9</var></li> <li><var>1 \leq A_i \leq 10^9</var></li> <li><var>A_i</var> is an integer.</li> <li><var>B_i</var> is either <var>1</var> or <var>2</var>.</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li>In the test set worth <var>500</var> points, all the sections are single-tracked. That is, <var>B_i = 1</var>.</li> <li>In the test set worth another <var>500</var> points, <var>N \leq 200</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>A_1</var> <var>B_1</var> <var>A_2</var> <var>B_2</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print an integer representing the minimum sum of the amount of time required for a train to depart station <var>0</var> and reach station <var>N</var>, and the amount of time required for a train to depart station <var>N</var> and reach station <var>0</var>. If it is impossible to create a timetable satisfying the conditions, print <var>-1</var> instead.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 10 4 1 3 1 4 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>26 </pre> <p>For example, the sum of the amount of time in question will be <var>26</var> minutes in the following timetable:</p> <p><img alt="" src="https://atcoder.jp/img/agc011/a5c221ce77ab6ee8aee48e75a4e5c969.png"/></p> <p>In this timetable, the train represented by the red line departs station <var>0</var> at time <var>0</var>, arrives at station <var>1</var> at time <var>4</var>, departs station <var>1</var> at time <var>5</var>, arrives at station <var>2</var> at time <var>8</var>, and so on.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 10 10 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>-1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 4 1 1 1 1 1 1 1 1 1 1 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>12 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>20 987654321 129662684 2 162021979 1 458437539 1 319670097 2 202863355 1 112218745 1 348732033 1 323036578 1 382398703 1 55854389 1 283445191 1 151300613 1 693338042 2 191178308 2 386707193 1 204580036 1 335134457 1 122253639 1 824646518 2 902554792 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>14829091348 </pre></section> </div> </span>
[ [ "3 10\n4 1\n3 1\n4 1\n", "3 10\n4 1\n3 1\n4 1\n" ] ]
p03791
Mujin Programming Challenge 2017 - Robot Racing
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are developing frog-shaped robots, and decided to race them against each other.</p> <p>First, you placed <var>N</var> robots onto a number line. These robots are numbered <var>1</var> through <var>N</var>. The current coordinate of robot <var>i</var> is <var>x_i</var>. Here, all <var>x_i</var> are integers, and <var>0 &lt; x_1 &lt; x_2 &lt; ... &lt; x_N</var>.</p> <p>You will repeatedly perform the following operation:</p> <ul> <li>Select a robot on the number line. Let the coordinate of the robot be <var>x</var>. Select the destination coordinate, either <var>x-1</var> or <var>x-2</var>, that is not occupied by another robot. The robot now jumps to the selected coordinate.</li> </ul> <p>When the coordinate of a robot becomes <var>0</var> or less, the robot is considered finished and will be removed from the number line immediately. You will repeat the operation until all the robots finish the race.</p> <p>Depending on your choice in the operation, the <var>N</var> robots can finish the race in different orders. In how many different orders can the <var>N</var> robots finish the race? Find the answer modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N ≤ 10^5</var></li> <li><var>x_i</var> is an integer.</li> <li><var>0 &lt; x_1 &lt; x_2 &lt; ... &lt; x_N ≤ 10^9</var></li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li>In a test set worth <var>500</var> points, <var>N ≤ 8</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>x_1</var> <var>x_2</var> <var>...</var> <var>x_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different orders in which the <var>N</var> robots can finish the race, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>There are four different orders in which the three robots can finish the race:</p> <ul> <li><var>(</var>Robot <var>1 →</var> Robot <var>2 →</var> Robot <var>3)</var></li> <li><var>(</var>Robot <var>1 →</var> Robot <var>3 →</var> Robot <var>2)</var></li> <li><var>(</var>Robot <var>2 →</var> Robot <var>1 →</var> Robot <var>3)</var></li> <li><var>(</var>Robot <var>2 →</var> Robot <var>3 →</var> Robot <var>1)</var></li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 2 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 </pre> <p>There are six different orders in which the three robots can finish the race:</p> <ul> <li><var>(</var>Robot <var>1 →</var> Robot <var>2 →</var> Robot <var>3)</var></li> <li><var>(</var>Robot <var>1 →</var> Robot <var>3 →</var> Robot <var>2)</var></li> <li><var>(</var>Robot <var>2 →</var> Robot <var>1 →</var> Robot <var>3)</var></li> <li><var>(</var>Robot <var>2 →</var> Robot <var>3 →</var> Robot <var>1)</var></li> <li><var>(</var>Robot <var>3 →</var> Robot <var>1 →</var> Robot <var>2)</var></li> <li><var>(</var>Robot <var>3 →</var> Robot <var>2 →</var> Robot <var>1)</var></li> </ul> <p>For example, the order <var>(</var>Robot <var>3 →</var> Robot <var>2 →</var> Robot <var>1)</var> can be achieved as shown in the figure below:</p> <div style="text-align: center;"> <img alt="a55aed48a00614569d4844f39807e2fb.png" src="https://atcoder.jp/img/mujin/a55aed48a00614569d4844f39807e2fb.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>8 1 2 3 5 7 11 13 17 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>10080 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>13 4 6 8 9 10 12 14 15 16 18 20 21 22 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>311014372 </pre> <p>Remember to print the answer modulo <var>10^9+7</var>. This case is not included in the test set for the partial score.</p></section> </div> </span>
[ [ "3\n1 2 3\n", "3\n1 2 3\n" ] ]
p03792
Mujin Programming Challenge 2017 - Row to Column
<span class="lang-en"> <p>Score : <var>1300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a square-shaped grid with <var>N</var> vertical rows and <var>N</var> horizontal columns. We will denote the square at the <var>i</var>-th row from the top and the <var>j</var>-th column from the left as <var>(i,\ j)</var>.</p> <p>Initially, each square is either white or black. The initial color of the grid is given to you as characters <var>a_{ij}</var>, arranged in a square shape. If the square <var>(i,\ j)</var> is white, <var>a_{ij}</var> is <code>.</code>. If it is black, <var>a_{ij}</var> is <code>#</code>.</p> <p>You are developing a robot that repaints the grid. It can repeatedly perform the following operation:</p> <ul> <li>Select two integers <var>i</var>, <var>j</var> (<var>1 ≤ i,\ j ≤ N</var>). Memorize the colors of the squares <var>(i,\ 1)</var>, <var>(i,\ 2)</var>, <var>...</var>, <var>(i,\ N)</var> as <var>c_1</var>, <var>c_2</var>, <var>...</var>, <var>c_N</var>, respectively. Then, repaint the squares <var>(1,\ j)</var>, <var>(2,\ j)</var>, <var>...</var>, <var>(N,\ j)</var> with the colors <var>c_1</var>, <var>c_2</var>, <var>...</var>, <var>c_N</var>, respectively.</li> </ul> <p>Your objective is to turn all the squares black. Determine whether it is possible, and find the minimum necessary number of operations to achieve it if the answer is positive.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N ≤ 500</var></li> <li><var>a_{ij}</var> is either <code>.</code> or <code>#</code>.</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li>In a test set worth <var>300</var> points, <var>N ≤ 3</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_{11}</var><var>...</var><var>a_{1N}</var> <var>:</var> <var>a_{N1}</var><var>...</var><var>a_{NN}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If it is possible to turn all the squares black, print the minimum necessary number of operations to achieve the objective. If it is impossible, print <code>-1</code> instead.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 #. .# </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>For example, perform the operation as follows:</p> <ul> <li>Select <var>i = 1</var>, <var>j = 2</var>.</li> <li>Select <var>i = 1</var>, <var>j = 1</var>.</li> <li>Select <var>i = 1</var>, <var>j = 2</var>.</li> </ul> <p>The transition of the colors of the squares is shown in the figure below:</p> <div style="text-align: center;"> <img alt="6a0314bb2b1073694a7ef5a062e77b13.png" src="https://atcoder.jp/img/mujin/6a0314bb2b1073694a7ef5a062e77b13.png"> </img></div> </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> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>2 ## ## </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>3 .#. ### .#. </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>2 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>3 ... .#. ... </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>5 </pre></section> </div> </span>
[ [ "2\n#.\n.#\n", "2\n#.\n.#\n" ] ]
p03793
Mujin Programming Challenge 2017 - Robot and String
<span class="lang-en"> <p>Score : <var>1300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are developing a robot that processes strings. When the robot is given a string <var>t</var> consisting of lowercase English letters, it processes the string by following the procedure below:</p> <ol> <li>Let <var>i</var> be the smallest index such that <var>t_i = t_{i + 1}</var>. If such an index does not exist, terminate the procedure.</li> <li>If <var>t_i</var> is <code>z</code>, remove <var>t_i</var> and <var>t_{i + 1}</var> from <var>t</var>. Otherwise, let <var>c</var> be the next letter of <var>t_i</var> in the English alphabet, and replace <var>t_i</var> and <var>t_{i + 1}</var> together with <var>c</var>, reducing the length of <var>t</var> by <var>1</var>.</li> <li>Go back to step 1.</li> </ol> <p>For example, when the robot is given the string <code>axxxxza</code>, it will be processed as follows: <code>axxxxza</code> → <code>ayxxza</code> → <code>ayyza</code> → <code>azza</code> → <code>aa</code> → <code>b</code>.</p> <p>You are given a string <var>s</var> consisting of lowercase English letters. Answer <var>Q</var> queries. The <var>i</var>-th query is as follows:</p> <ul> <li>Assume that the robot is given a substring of <var>s</var> that runs from the <var>l_i</var>-th character and up to the <var>r_i</var>-th character (inclusive). Will the string be empty after processing?</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ |s| ≤ 5 × 10^5</var></li> <li><var>s</var> consists of lowercase English letters.</li> <li><var>1 ≤ Q ≤ 10^5</var></li> <li><var>1 ≤ l_i ≤ r_i ≤ |s|</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>s</var> <var>Q</var> <var>l_1</var> <var>r_1</var> <var>l_2</var> <var>r_2</var> <var>:</var> <var>l_Q</var> <var>r_Q</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>Q</var> lines. The <var>i</var>-th line should contain the answer to the <var>i</var>-th query: <code>Yes</code> or <code>No</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>axxxxza 2 1 7 2 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>No Yes </pre> <ul> <li>Regarding the first query, the string will be processed as follows: <code>axxxxza</code> → <code>ayxxza</code> → <code>ayyza</code> → <code>azza</code> → <code>aa</code> → <code>b</code>.</li> <li>Regarding the second query, the string will be processed as follows: <code>xxxxz</code> → <code>yxxz</code> → <code>yyz</code> → <code>zz</code> → <code></code>.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>aabcdefghijklmnopqrstuvwxyz 1 1 27 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>Yes </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>yzyyyzyzyyyz 8 1 6 7 12 1 12 6 11 1 1 1 3 4 9 3 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Yes Yes Yes Yes No No No No </pre></section> </div> </span>
[ [ "axxxxza\n2\n1 7\n2 6\n", "axxxxza\n2\n1 7\n2 6\n" ] ]
p03794
Mujin Programming Challenge 2017 - Oriented Tree
<span class="lang-en"> <p>Score : <var>1800</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a tree <var>T</var> with <var>N</var> vertices, numbered <var>1</var> through <var>N</var>. For each <var>1 ≤ i ≤ N - 1</var>, the <var>i</var>-th edge connects vertices <var>a_i</var> and <var>b_i</var>.</p> <p>Snuke is constructing a directed graph <var>T'</var> by arbitrarily assigning direction to each edge in <var>T</var>. (There are <var>2^{N - 1}</var> different ways to construct <var>T'</var>.)</p> <p>For a fixed <var>T'</var>, we will define <var>d(s,\ t)</var> for each <var>1 ≤ s,\ t ≤ N</var>, as follows:</p> <ul> <li><var>d(s,\ t) = </var>(The number of edges that must be traversed against the assigned direction when traveling from vertex <var>s</var> to vertex <var>t</var>)</li> </ul> <p>In particular, <var>d(s,\ s) = 0</var> for each <var>1 ≤ s ≤ N</var>. Also note that, in general, <var>d(s,\ t) ≠ d(t,\ s)</var>.</p> <p>We will further define <var>D</var> as the following:</p> <div style="text-align: center;"> <img alt="3d2f3f88e8fa23f065c04cd175c14ebf.png" src="https://atcoder.jp/img/mujin/3d2f3f88e8fa23f065c04cd175c14ebf.png"> </img></div> <p>Snuke is constructing <var>T'</var> so that <var>D</var> will be the minimum possible value. How many different ways are there to construct <var>T'</var> so that <var>D</var> will be the minimum possible value, modulo <var>10^9 + 7</var>?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N ≤ 1000</var></li> <li><var>1 ≤ a_i,\ b_i ≤ N</var></li> <li>The given graph is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>b_1</var> <var>a_2</var> <var>b_2</var> <var>:</var> <var>a_{N - 1}</var> <var>b_{N - 1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different ways to construct <var>T'</var> so that <var>D</var> will be the minimum possible value, modulo <var>10^9 + 7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 1 2 1 3 1 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The minimum possible value for <var>D</var> is <var>1</var>. There are two ways to construct <var>T'</var> to achieve this value, as shown in the following figure:</p> <p><img alt="" src="https://atcoder.jp/img/mujin/de49901ddf69d8565fde5b6870afb595.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 1 2 2 3 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>6 </pre> <p>The minimum possible value for <var>D</var> is <var>2</var>. There are six ways to construct <var>T'</var> to achieve this value, as shown in the following figure:</p> <p><img alt="" src="https://atcoder.jp/img/mujin/dcb377e8c7fe15d6dd0cb815dc57c41a.png"/></p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 1 2 1 3 1 4 2 5 2 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>14 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>10 2 4 2 5 8 3 10 7 1 6 2 8 9 5 8 6 10 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>102 </pre></section> </div> </span>
[ [ "4\n1 2\n1 3\n1 4\n", "4\n1 2\n1 3\n1 4\n" ] ]
p03795
AtCoder Beginner Contest 055 - Restaurant
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has a favorite restaurant.</p> <p>The price of any meal served at the restaurant is <var>800</var> yen (the currency of Japan), and each time a customer orders <var>15</var> meals, the restaurant pays <var>200</var> yen back to the customer.</p> <p>So far, Snuke has ordered <var>N</var> meals at the restaurant. Let the amount of money Snuke has paid to the restaurant be <var>x</var> yen, and let the amount of money the restaurant has paid back to Snuke be <var>y</var> yen. Find <var>x-y</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 100</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>20 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>15800 </pre> <p>So far, Snuke has paid <var>16000</var> yen, and the restaurant has paid back <var>200</var> yen. Thus, the answer is <var>15800</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>60 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>47200 </pre> <p>Snuke has paid <var>48000</var> yen for <var>60</var> meals, and the restaurant has paid back <var>800</var> yen.</p></section> </div> </span>
[ [ "20\n", "20\n" ] ]
p03796
AtCoder Beginner Contest 055 - Training Camp
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke loves working out. He is now exercising <var>N</var> times.</p> <p>Before he starts exercising, his <em>power</em> is <var>1</var>. After he exercises for the <var>i</var>-th time, his power gets multiplied by <var>i</var>.</p> <p>Find Snuke's power after he exercises <var>N</var> times. Since the answer can be extremely large, print the answer modulo <var>10^{9}+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 10^{5}</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer 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>6 </pre> <ul> <li>After Snuke exercises for the first time, his power gets multiplied by <var>1</var> and becomes <var>1</var>.</li> <li>After Snuke exercises for the second time, his power gets multiplied by <var>2</var> and becomes <var>2</var>.</li> <li>After Snuke exercises for the third time, his power gets multiplied by <var>3</var> and becomes <var>6</var>.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3628800 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>457992974 </pre> <p>Print the answer modulo <var>10^{9}+7</var>.</p></section> </div> </span>
[ [ "3\n", "3\n" ] ]
p03797
AtCoder Beginner Contest 055 - Scc Puzzle
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke loves puzzles.</p> <p>Today, he is working on a puzzle using <code>S</code>- and <code>c</code>-shaped pieces. In this puzzle, you can combine two <code>c</code>-shaped pieces into one <code>S</code>-shaped piece, as shown in the figure below:</p> <div style="text-align: center;"> <img alt="9b0bd546db9f28b4093d417b8f274124.png" src="https://atcoder.jp/img/arc069/9b0bd546db9f28b4093d417b8f274124.png"> </img></div> <p>Snuke decided to create as many <code>Scc</code> groups as possible by putting together one <code>S</code>-shaped piece and two <code>c</code>-shaped pieces.</p> <p>Find the maximum number of <code>Scc</code> groups that can be created when Snuke has <var>N</var> <code>S</code>-shaped pieces and <var>M</var> <code>c</code>-shaped pieces.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N,M ≤ 10^{12}</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>Two <code>Scc</code> groups can be created as follows:</p> <ul> <li>Combine two <code>c</code>-shaped pieces into one <code>S</code>-shaped piece</li> <li>Create two <code>Scc</code> groups, each from one <code>S</code>-shaped piece and two <code>c</code>-shaped pieces</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>12345 678901 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>175897 </pre></section> </div> </span>
[ [ "1 6\n", "1 6\n" ] ]
p03798
AtCoder Beginner Contest 055 - Menagerie
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke, who loves animals, built a zoo.</p> <p>There are <var>N</var> animals in this zoo. They are conveniently numbered <var>1</var> through <var>N</var>, and arranged in a circle. The animal numbered <var>i (2≤i≤N-1)</var> is adjacent to the animals numbered <var>i-1</var> and <var>i+1</var>. Also, the animal numbered <var>1</var> is adjacent to the animals numbered <var>2</var> and <var>N</var>, and the animal numbered <var>N</var> is adjacent to the animals numbered <var>N-1</var> and <var>1</var>.</p> <p>There are two kinds of animals in this zoo: honest sheep that only speak the truth, and lying wolves that only tell lies.</p> <p>Snuke cannot tell the difference between these two species, and asked each animal the following question: "Are your neighbors of the same species?" The animal numbered <var>i</var> answered <var>s_i</var>. Here, if <var>s_i</var> is <code>o</code>, the animal said that the two neighboring animals are of the same species, and if <var>s_i</var> is <code>x</code>, the animal said that the two neighboring animals are of different species.</p> <p>More formally, a sheep answered <code>o</code> if the two neighboring animals are both sheep or both wolves, and answered <code>x</code> otherwise. Similarly, a wolf answered <code>x</code> if the two neighboring animals are both sheep or both wolves, and answered <code>o</code> otherwise.</p> <p>Snuke is wondering whether there is a valid assignment of species to the animals that is consistent with these responses. If there is such an assignment, show one such assignment. Otherwise, print <code>-1</code>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>3 ≤ N ≤ 10^{5}</var></li> <li><var>s</var> is a string of length <var>N</var> consisting of <code>o</code> and <code>x</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>s</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there does not exist an valid assignment that is consistent with <var>s</var>, print <code>-1</code>. Otherwise, print an string <var>t</var> in the following format. The output is considered correct if the assignment described by <var>t</var> is consistent with <var>s</var>.</p> <ul> <li><var>t</var> is a string of length <var>N</var> consisting of <code>S</code> and <code>W</code>.</li> <li>If <var>t_i</var> is <code>S</code>, it indicates that the animal numbered <var>i</var> is a sheep. If <var>t_i</var> is <code>W</code>, it indicates that the animal numbered <var>i</var> is a wolf.</li> </ul> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 ooxoox </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>SSSWWS </pre> <p>For example, if the animals numbered <var>1</var>, <var>2</var>, <var>3</var>, <var>4</var>, <var>5</var> and <var>6</var> are respectively a sheep, sheep, sheep, wolf, wolf, and sheep, it is consistent with their responses. Besides, there is another valid assignment of species: a wolf, sheep, wolf, sheep, wolf and wolf.</p> <p>Let us remind you: if the neiboring animals are of the same species, a sheep answers <code>o</code> and a wolf answers <code>x</code>. If the neiboring animals are of different species, a sheep answers <code>x</code> and a wolf answers <code>o</code>.</p> <div style="text-align: center;"> <img alt="b34c052fc21c42d2def9b98d6dccd05c.png" src="https://atcoder.jp/img/arc069/b34c052fc21c42d2def9b98d6dccd05c.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 oox </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>-1 </pre> <p>Print <code>-1</code> if there is no valid assignment of species.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>10 oxooxoxoox </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>SSWWSSSWWS </pre></section> </div> </span>
[ [ "6\nooxoox\n", "6\nooxoox\n" ] ]
p03799
AtCoder Regular Contest 069 - Scc Puzzle
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke loves puzzles.</p> <p>Today, he is working on a puzzle using <code>S</code>- and <code>c</code>-shaped pieces. In this puzzle, you can combine two <code>c</code>-shaped pieces into one <code>S</code>-shaped piece, as shown in the figure below:</p> <div style="text-align: center;"> <img alt="9b0bd546db9f28b4093d417b8f274124.png" src="https://atcoder.jp/img/arc069/9b0bd546db9f28b4093d417b8f274124.png"> </img></div> <p>Snuke decided to create as many <code>Scc</code> groups as possible by putting together one <code>S</code>-shaped piece and two <code>c</code>-shaped pieces.</p> <p>Find the maximum number of <code>Scc</code> groups that can be created when Snuke has <var>N</var> <code>S</code>-shaped pieces and <var>M</var> <code>c</code>-shaped pieces.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N,M ≤ 10^{12}</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>1 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>Two <code>Scc</code> groups can be created as follows:</p> <ul> <li>Combine two <code>c</code>-shaped pieces into one <code>S</code>-shaped piece</li> <li>Create two <code>Scc</code> groups, each from one <code>S</code>-shaped piece and two <code>c</code>-shaped pieces</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>12345 678901 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>175897 </pre></section> </div> </span>
[ [ "1 6\n", "1 6\n" ] ]
p03800
AtCoder Regular Contest 069 - Menagerie
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke, who loves animals, built a zoo.</p> <p>There are <var>N</var> animals in this zoo. They are conveniently numbered <var>1</var> through <var>N</var>, and arranged in a circle. The animal numbered <var>i (2≤i≤N-1)</var> is adjacent to the animals numbered <var>i-1</var> and <var>i+1</var>. Also, the animal numbered <var>1</var> is adjacent to the animals numbered <var>2</var> and <var>N</var>, and the animal numbered <var>N</var> is adjacent to the animals numbered <var>N-1</var> and <var>1</var>.</p> <p>There are two kinds of animals in this zoo: honest sheep that only speak the truth, and lying wolves that only tell lies.</p> <p>Snuke cannot tell the difference between these two species, and asked each animal the following question: "Are your neighbors of the same species?" The animal numbered <var>i</var> answered <var>s_i</var>. Here, if <var>s_i</var> is <code>o</code>, the animal said that the two neighboring animals are of the same species, and if <var>s_i</var> is <code>x</code>, the animal said that the two neighboring animals are of different species.</p> <p>More formally, a sheep answered <code>o</code> if the two neighboring animals are both sheep or both wolves, and answered <code>x</code> otherwise. Similarly, a wolf answered <code>x</code> if the two neighboring animals are both sheep or both wolves, and answered <code>o</code> otherwise.</p> <p>Snuke is wondering whether there is a valid assignment of species to the animals that is consistent with these responses. If there is such an assignment, show one such assignment. Otherwise, print <code>-1</code>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>3 ≤ N ≤ 10^{5}</var></li> <li><var>s</var> is a string of length <var>N</var> consisting of <code>o</code> and <code>x</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>s</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there does not exist an valid assignment that is consistent with <var>s</var>, print <code>-1</code>. Otherwise, print an string <var>t</var> in the following format. The output is considered correct if the assignment described by <var>t</var> is consistent with <var>s</var>.</p> <ul> <li><var>t</var> is a string of length <var>N</var> consisting of <code>S</code> and <code>W</code>.</li> <li>If <var>t_i</var> is <code>S</code>, it indicates that the animal numbered <var>i</var> is a sheep. If <var>t_i</var> is <code>W</code>, it indicates that the animal numbered <var>i</var> is a wolf.</li> </ul> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>6 ooxoox </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>SSSWWS </pre> <p>For example, if the animals numbered <var>1</var>, <var>2</var>, <var>3</var>, <var>4</var>, <var>5</var> and <var>6</var> are respectively a sheep, sheep, sheep, wolf, wolf, and sheep, it is consistent with their responses. Besides, there is another valid assignment of species: a wolf, sheep, wolf, sheep, wolf and wolf.</p> <p>Let us remind you: if the neiboring animals are of the same species, a sheep answers <code>o</code> and a wolf answers <code>x</code>. If the neiboring animals are of different species, a sheep answers <code>x</code> and a wolf answers <code>o</code>.</p> <div style="text-align: center;"> <img alt="b34c052fc21c42d2def9b98d6dccd05c.png" src="https://atcoder.jp/img/arc069/b34c052fc21c42d2def9b98d6dccd05c.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 oox </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>-1 </pre> <p>Print <code>-1</code> if there is no valid assignment of species.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>10 oxooxoxoox </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>SSWWSSSWWS </pre></section> </div> </span>
[ [ "6\nooxoox\n", "6\nooxoox\n" ] ]
p03801
AtCoder Regular Contest 069 - Frequency
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke loves constructing integer sequences.</p> <p>There are <var>N</var> piles of stones, numbered <var>1</var> through <var>N</var>. The pile numbered <var>i</var> consists of <var>a_i</var> stones.</p> <p>Snuke will construct an integer sequence <var>s</var> of length <var>Σa_i</var>, as follows:</p> <ol> <li>Among the piles with the largest number of stones remaining, let <var>x</var> be the index of the pile with the smallest index. Append <var>x</var> to the end of <var>s</var>.</li> <li>Select a pile with one or more stones remaining, and remove a stone from that pile.</li> <li>If there is a pile with one or more stones remaining, go back to step 1. Otherwise, terminate the process.</li> </ol> <p>We are interested in the lexicographically smallest sequence that can be constructed. For each of the integers <var>1,2,3,...,N</var>, how many times does it occur in the lexicographically smallest sequence?</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 10^{5}</var></li> <li><var>1 ≤ a_i ≤ 10^{9}</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_{N}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>N</var> lines. The <var>i</var>-th line should contain the number of the occurrences of the integer <var>i</var> in the lexicographically smallest sequence that can be constructed.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 1 </pre> <p>The lexicographically smallest sequence is constructed as follows:</p> <ul> <li>Since the pile with the largest number of stones remaining is pile <var>2</var>, append <var>2</var> to the end of <var>s</var>. Then, remove a stone from pile <var>2</var>.</li> <li>Since the piles with the largest number of stones remaining are pile <var>1</var> and <var>2</var>, append <var>1</var> to the end of <var>s</var> (we take the smallest index). Then, remove a stone from pile <var>2</var>.</li> <li>Since the pile with the largest number of stones remaining is pile <var>1</var>, append <var>1</var> to the end of <var>s</var>. Then, remove a stone from pile <var>1</var>.</li> </ul> <p>The resulting sequence is <var>(2,1,1)</var>. In this sequence, <var>1</var> occurs twice, and <var>2</var> occurs once.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 1 2 1 3 2 4 2 5 8 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>10 7 0 4 0 3 0 2 3 0 </pre></section> </div> </span>
[ [ "2\n1 2\n", "2\n1 2\n" ] ]
p03802
AtCoder Regular Contest 069 - Flags
<span class="lang-en"> <p>Score : <var>1200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke loves flags.</p> <p>Snuke is placing <var>N</var> flags on a line.</p> <p>The <var>i</var>-th flag can be placed at either coordinate <var>x_i</var> or coordinate <var>y_i</var>.</p> <p>Snuke thinks that the flags look nicer when the smallest distance between two of them, <var>d</var>, is larger. Find the maximum possible value of <var>d</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N ≤ 10^{4}</var></li> <li><var>1 ≤ x_i, y_i ≤ 10^{9}</var></li> <li><var>x_i</var> and <var>y_i</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>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 the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 3 2 5 1 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>The optimal solution is to place the first flag at coordinate <var>1</var>, the second flag at coordinate <var>5</var> and the third flag at coordinate <var>9</var>. The smallest distance between two of the flags is <var>4</var> in this case.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 2 2 2 2 2 2 2 2 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>There can be more than one flag at the same position.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>22 93 6440 78 6647 862 11 8306 9689 798 99 801 521 188 206 6079 971 4559 209 50 94 92 6270 5403 560 803 83 1855 99 42 504 75 484 629 11 92 122 3359 37 28 16 648 14 11 269 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>17 </pre></section> </div> </span>
[ [ "3\n1 3\n2 5\n1 9\n", "3\n1 3\n2 5\n1 9\n" ] ]
p03803
AtCoder Beginner Contest 054 - One Card Poker
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Alice and Bob are playing <em>One Card Poker</em>.<br/> One Card Poker is a two-player game using playing cards. </p> <p>Each card in this game shows an integer between <code>1</code> and <code>13</code>, inclusive.<br/> The <em>strength</em> of a card is determined by the number written on it, as follows: </p> <p>Weak <code>2</code> <var>&lt;</var> <code>3</code> <var>&lt;</var> <code>4</code> <var>&lt;</var> <code>5</code> <var>&lt;</var> <code>6</code> <var>&lt;</var> <code>7</code> <var>&lt;</var> <code>8</code> <var>&lt;</var> <code>9</code> <var>&lt;</var> <code>10</code> <var>&lt;</var> <code>11</code> <var>&lt;</var> <code>12</code> <var>&lt;</var> <code>13</code> <var>&lt;</var> <code>1</code> Strong </p> <p>One Card Poker is played as follows: </p> <ol> <li>Each player picks one card from the deck. The chosen card becomes the player's hand.</li> <li>The players reveal their hands to each other. The player with the stronger card wins the game.<br/> If their cards are equally strong, the game is drawn. </li> </ol> <p>You are watching Alice and Bob playing the game, and can see their hands.<br/> The number written on Alice's card is <var>A</var>, and the number written on Bob's card is <var>B</var>.<br/> Write a program to determine the outcome of the game. </p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦A≦13</var> </li> <li><var>1≦B≦13</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>The input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <code>Alice</code> if Alice will win. Print <code>Bob</code> if Bob will win. Print <code>Draw</code> if the game will be drawn.</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>Alice </pre> <p><code>8</code> is written on Alice's card, and <code>6</code> is written on Bob's card. Alice has the stronger card, and thus the output should be <code>Alice</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>Draw </pre> <p>Since their cards have the same number, the game will be drawn.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>13 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Bob </pre></section> </div> </span>
[ [ "8 6\n", "8 6\n" ] ]
p03804
AtCoder Beginner Contest 054 - Template Matching
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an image <var>A</var> composed of <var>N</var> rows and <var>N</var> columns of pixels, and a template image <var>B</var> composed of <var>M</var> rows and <var>M</var> columns of pixels.<br/> A pixel is the smallest element of an image, and in this problem it is a square of size <var>1×1</var>.<br/> Also, the given images are binary images, and the color of each pixel is either white or black. </p> <p>In the input, every pixel is represented by a character: <code>.</code> corresponds to a white pixel, and <code>#</code> corresponds to a black pixel.<br/> The image <var>A</var> is given as <var>N</var> strings <var>A_1,...,A_N</var>.<br/> The <var>j</var>-th character in the string <var>A_i</var> corresponds to the pixel at the <var>i</var>-th row and <var>j</var>-th column of the image <var>A</var> <var>(1≦i,j≦N)</var>.<br/> Similarly, the template image <var>B</var> is given as <var>M</var> strings <var>B_1,...,B_M</var>.<br/> The <var>j</var>-th character in the string <var>B_i</var> corresponds to the pixel at the <var>i</var>-th row and <var>j</var>-th column of the template image <var>B</var> <var>(1≦i,j≦M)</var>. </p> <p>Determine whether the template image <var>B</var> is contained in the image <var>A</var> when only parallel shifts can be applied to the images. </p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦M≦N≦50</var> </li> <li><var>A_i</var> is a string of length <var>N</var> consisting of <code>#</code> and <code>.</code>.</li> <li><var>B_i</var> is a string of length <var>M</var> consisting of <code>#</code> and <code>.</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>:</var> <var>A_N</var> <var>B_1</var> <var>B_2</var> <var>:</var> <var>B_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <code>Yes</code> if the template image <var>B</var> is contained in the image <var>A</var>. Print <code>No</code> otherwise.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 #.# .#. #.# #. .# </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes </pre> <p>The template image <var>B</var> is identical to the upper-left <var>2 × 2</var> subimage and the lower-right <var>2 × 2</var> subimage of <var>A</var>. Thus, the output should be <code>Yes</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 1 .... .... .... .... # </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>No </pre> <p>The template image <var>B</var>, composed of a black pixel, is not contained in the image <var>A</var> composed of white pixels.</p></section> </div> </span>
[ [ "3 2\n#.#\n.#.\n#.#\n#.\n.#\n", "3 2\n#.#\n.#.\n#.#\n#.\n.#\n" ] ]
p03805
AtCoder Beginner Contest 054 - One-stroke Path
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an undirected unweighted graph with <var>N</var> vertices and <var>M</var> edges that contains neither self-loops nor double edges.<br/> Here, a <em>self-loop</em> is an edge where <var>a_i = b_i (1≤i≤M)</var>, and <em>double edges</em> are two edges where <var>(a_i,b_i)=(a_j,b_j)</var> or <var>(a_i,b_i)=(b_j,a_j) (1≤i&lt;j≤M)</var>.<br/> How many different paths start from vertex <var>1</var> and visit all the vertices exactly once?<br/> Here, the endpoints of a path are considered visited. </p> <p>For example, let us assume that the following undirected graph shown in Figure 1 is given.</p> <div style="text-align: center;"> <img src="https://atcoder.jp/img/5013/888b2f55d46f66125a4ac25cd8cfc19a.png"/> <p>Figure 1: an example of an undirected graph</p> </div> <p>The following path shown in Figure 2 satisfies the condition.</p> <div style="text-align: center;"> <img src="https://atcoder.jp/img/5013/694eda4639f3f4608c9f0b38af1633d3.png"/> <p>Figure 2: an example of a path that satisfies the condition</p> </div> <p>However, the following path shown in Figure 3 does not satisfy the condition, because it does not visit all the vertices.</p> <div style="text-align: center;"> <img src="https://atcoder.jp/img/5013/4739baf6665ab2832ea424b1cc404ee1.png"/> <p>Figure 3: an example of a path that does not satisfy the condition</p> </div> <p>Neither the following path shown in Figure 4, because it does not start from vertex <var>1</var>.</p> <div style="text-align: center;"> <img src="https://atcoder.jp/img/5013/7ad401c30e069a98a34c8cfec70ec278.png"/> <p>Figure 4: another example of a path that does not satisfy the condition</p> </div> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≦N≦8</var> </li> <li><var>0≦M≦N(N-1)/2</var> </li> <li><var>1≦a_i&lt;b_i≦N</var> </li> <li>The given graph contains neither self-loops nor double edges.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>a_1</var> <var>b_1</var> <var>a_2</var> <var>b_2</var> <var>:</var> <var>a_M</var> <var>b_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different paths that start from vertex <var>1</var> and visit all the vertices exactly once.</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>2 </pre> <p>The given graph is shown in the following figure:</p> <div style="text-align: center;"> <img alt="43c0ac53de20d989d100bf60b3cd05fa.png" src="https://atcoder.jp/img/5013/43c0ac53de20d989d100bf60b3cd05fa.png"/> </div> <p>The following two paths satisfy the condition:</p> <div style="text-align: center;"> <img alt="c4a27b591d364fa479314e3261b85071.png" src="https://atcoder.jp/img/5013/c4a27b591d364fa479314e3261b85071.png"/> </div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 7 1 3 2 7 3 4 4 5 4 6 5 6 6 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 </pre> <p>This test case is the same as the one described in the problem statement.</p></section> </div> </span>
[ [ "3 3\n1 2\n1 3\n2 3\n", "3 3\n1 2\n1 3\n2 3\n" ] ]
p03806
AtCoder Beginner Contest 054 - Mixing Experiment
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Dolphin is planning to generate a small amount of a certain chemical substance C.<br/> In order to generate the substance C, he must prepare a solution which is a mixture of two substances A and B in the ratio of <var>M_a:M_b</var>.<br/> He does not have any stock of chemicals, however, so he will purchase some chemicals at a local pharmacy.<br/> The pharmacy sells <var>N</var> kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock.<br/> The package of chemical <var>i</var> contains <var>a_i</var> grams of the substance A and <var>b_i</var> grams of the substance B, and is sold for <var>c_i</var> yen (the currency of Japan).<br/> Dolphin will purchase some of these packages. For some reason, he must use all contents of the purchased packages to generate the substance C.<br/> Find the minimum amount of money required to generate the substance C.<br/> If it is not possible to generate the substance C by purchasing any combination of packages at the pharmacy, report that fact. </p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦40</var> </li> <li><var>1≦a_i,b_i≦10</var> </li> <li><var>1≦c_i≦100</var> </li> <li><var>1≦M_a,M_b≦10</var> </li> <li><var>gcd(M_a,M_b)=1</var></li> <li><var>a_i</var>, <var>b_i</var>, <var>c_i</var>, <var>M_a</var> and <var>M_b</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M_a</var> <var>M_b</var> <var>a_1</var> <var>b_1</var> <var>c_1</var> <var>a_2</var> <var>b_2</var> <var>c_2</var> <var>:</var> <var>a_N</var> <var>b_N</var> <var>c_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C, print <code>-1</code> instead.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 1 1 2 1 2 1 2 3 3 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>The amount of money spent will be minimized by purchasing the packages of chemicals <var>1</var> and <var>2</var>.<br/> In this case, the mixture of the purchased chemicals will contain <var>3</var> grams of the substance A and <var>3</var> grams of the substance B, which are in the desired ratio: <var>3:3=1:1</var>.<br/> The total price of these packages is <var>3</var> yen. </p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 1 10 10 10 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>-1 </pre> <p>The ratio <var>1:10</var> of the two substances A and B cannot be satisfied by purchasing any combination of the packages. Thus, the output should be <code>-1</code>.</p></section> </div> </span>
[ [ "3 1 1\n1 2 1\n2 1 2\n3 3 10\n", "3 1 1\n1 2 1\n2 1 2\n3 3 10\n" ] ]
p03807
AtCoder Grand Contest 010 - Addition
<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> integers written on a blackboard. The <var>i</var>-th integer is <var>A_i</var>.</p> <p>Takahashi will repeatedly perform the following operation on these numbers:</p> <ul> <li>Select a pair of integers, <var>A_i</var> and <var>A_j</var>, that have the same parity (that is, both are even or both are odd) and erase them.</li> <li>Then, write a new integer on the blackboard that is equal to the sum of those integers, <var>A_i+A_j</var>.</li> </ul> <p>Determine whether it is possible to have only one integer on the blackboard.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≦ N ≦ 10^5</var></li> <li><var>1 ≦ A_i ≦ 10^9</var></li> <li><var>A_i</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>A_2</var> … <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If it is possible to have only one integer on the blackboard, 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 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>YES </pre> <p>It is possible to have only one integer on the blackboard, as follows:</p> <ul> <li>Erase <var>1</var> and <var>3</var> from the blackboard, then write <var>4</var>. Now, there are two integers on the blackboard: <var>2</var> and <var>4</var>.</li> <li>Erase <var>2</var> and <var>4</var> from the blackboard, then write <var>6</var>. Now, there is only one integer on the blackboard: <var>6</var>.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 1 2 3 4 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>NO </pre></section> </div> </span>
[ [ "3\n1 2 3\n", "3\n1 2 3\n" ] ]
p03808
AtCoder Grand Contest 010 - Boxes
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> boxes arranged in a circle. The <var>i</var>-th box contains <var>A_i</var> stones.</p> <p>Determine whether it is possible to remove all the stones from the boxes by repeatedly performing the following operation:</p> <ul> <li>Select one box. Let the box be the <var>i</var>-th box. Then, for each <var>j</var> from <var>1</var> through <var>N</var>, remove exactly <var>j</var> stones from the <var>(i+j)</var>-th box. Here, the <var>(N+k)</var>-th box is identified with the <var>k</var>-th box.</li> </ul> <p>Note that the operation cannot be performed if there is a box that does not contain enough number of stones to be removed.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ N ≦ 10^5</var></li> <li><var>1 ≦ A_i ≦ 10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>A_2</var> … <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If it is possible to remove all the stones from the boxes, print <code>YES</code>. Otherwise, print <code>NO</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 4 5 1 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>YES </pre> <p>All the stones can be removed in one operation by selecting the second box.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 6 9 12 10 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>YES </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 1 2 3 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>NO </pre></section> </div> </span>
[ [ "5\n4 5 1 2 3\n", "5\n4 5 1 2 3\n" ] ]
p03809
AtCoder Grand Contest 010 - Cleaning
<span class="lang-en"> <p>Score : <var>700</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 of the <var>N-1</var> edges connects vertices <var>a_i</var> and <var>b_i</var>.</p> <p>Currently, there are <var>A_i</var> stones placed on vertex <var>i</var>. Determine whether it is possible to remove all the stones from the vertices by repeatedly performing the following operation:</p> <ul> <li>Select a pair of different leaves. Then, remove exactly one stone from every vertex on the path between those two vertices. Here, a <em>leaf</em> is a vertex of the tree whose degree is <var>1</var>, and the selected leaves themselves are also considered as vertices on the path connecting them.</li> </ul> <p>Note that the operation cannot be performed if there is a vertex with no stone on the path.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≦ N ≦ 10^5</var></li> <li><var>1 ≦ a_i,b_i ≦ N</var></li> <li><var>0 ≦ A_i ≦ 10^9</var></li> <li>The given graph is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>A_2</var> … <var>A_N</var> <var>a_1</var> <var>b_1</var> : <var>a_{N-1}</var> <var>b_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If it is possible to remove all the stones from the vertices, print <code>YES</code>. Otherwise, print <code>NO</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 1 2 1 1 2 2 4 5 2 3 2 1 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>YES </pre> <p>All the stones can be removed, as follows:</p> <ul> <li>Select vertices <var>4</var> and <var>5</var>. Then, there is one stone remaining on each vertex except <var>4</var>.</li> <li>Select vertices <var>1</var> and <var>5</var>. Then, there is no stone on any vertex.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 1 2 1 1 2 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>NO </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 3 2 2 2 2 2 1 2 2 3 1 4 1 5 4 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>YES </pre></section> </div> </span>
[ [ "5\n1 2 1 1 2\n2 4\n5 2\n3 2\n1 3\n", "5\n1 2 1 1 2\n2 4\n5 2\n3 2\n1 3\n" ] ]
p03810
AtCoder Grand Contest 010 - Decrementing
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> integers written on a blackboard. The <var>i</var>-th integer is <var>A_i</var>, and the greatest common divisor of these integers is <var>1</var>.</p> <p>Takahashi and Aoki will play a game using these integers. In this game, starting from Takahashi the two player alternately perform the following operation:</p> <ul> <li>Select one integer on the blackboard that is not less than <var>2</var>, and subtract <var>1</var> from the integer.</li> <li>Then, divide all the integers on the black board by <var>g</var>, where <var>g</var> is the greatest common divisor of the integers written on the blackboard.</li> </ul> <p>The player who is left with only <var>1</var>s on the blackboard and thus cannot perform the operation, loses the game. Assuming that both players play optimally, determine the winner of the game.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ N ≦ 10^5</var></li> <li><var>1 ≦ A_i ≦ 10^9</var></li> <li>The greatest common divisor of the integers from <var>A_1</var> through <var>A_N</var> is <var>1</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>A_2</var> … <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If Takahashi will win, print <code>First</code>. If Aoki will win, print <code>Second</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 6 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>First </pre> <p>Takahashi, the first player, can win as follows:</p> <ul> <li>Takahashi subtracts <var>1</var> from <var>7</var>. Then, the integers become: <var>(1,2,2)</var>.</li> <li>Aoki subtracts <var>1</var> from <var>2</var>. Then, the integers become: <var>(1,1,2)</var>.</li> <li>Takahashi subtracts <var>1</var> from <var>2</var>. Then, the integers become: <var>(1,1,1)</var>.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 1 2 4 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>First </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 7 8 8 8 8 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>Second </pre></section> </div> </span>
[ [ "3\n3 6 7\n", "3\n3 6 7\n" ] ]
p03811
AtCoder Grand Contest 010 - Rearranging
<span class="lang-en"> <p>Score : <var>1600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> integers written on a blackboard. The <var>i</var>-th integer is <var>A_i</var>.</p> <p>Takahashi and Aoki will arrange these integers in a row, as follows:</p> <ul> <li>First, Takahashi will arrange the integers as he wishes.</li> <li>Then, Aoki will repeatedly swap two adjacent integers that are coprime, as many times as he wishes.</li> </ul> <p>We will assume that Takahashi acts optimally so that the eventual sequence will be lexicographically as small as possible, and we will also assume that Aoki acts optimally so that the eventual sequence will be lexicographically as large as possible. Find the eventual sequence that will be produced.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ N ≦ 2000</var></li> <li><var>1 ≦ A_i ≦ 10^8</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>A_2</var> … <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the eventual sequence that will be produced, in a line.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 1 2 3 4 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>5 3 2 4 1 </pre> <p>If Takahashi arranges the given integers in the order <var>(1,2,3,4,5)</var>, they will become <var>(5,3,2,4,1)</var> after Aoki optimally manipulates them.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 2 3 4 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>2 4 6 3 </pre></section> </div> </span>
[ [ "5\n1 2 3 4 5\n", "5\n1 2 3 4 5\n" ] ]
p03812
AtCoder Grand Contest 010 - Tree Game
<span class="lang-en"> <p>Score : <var>1600</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 of the <var>N-1</var> edges connects vertices <var>a_i</var> and <var>b_i</var>.</p> <p>Currently, there are <var>A_i</var> stones placed on vertex <var>i</var>. Takahashi and Aoki will play a game using this tree.</p> <p>First, Takahashi will select a vertex and place a piece on it. Then, starting from Takahashi, they will alternately perform the following operation:</p> <ul> <li>Remove one stone from the vertex currently occupied by the piece.</li> <li>Then, move the piece to a vertex that is adjacent to the currently occupied vertex.</li> </ul> <p>The player who is left with no stone on the vertex occupied by the piece and thus cannot perform the operation, loses the game. Find all the vertices <var>v</var> such that Takahashi can place the piece on <var>v</var> at the beginning and win the game.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≦ N ≦ 3000</var></li> <li><var>1 ≦ a_i,b_i ≦ N</var></li> <li><var>0 ≦ A_i ≦ 10^9</var></li> <li>The given graph is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>A_2</var> … <var>A_N</var> <var>a_1</var> <var>b_1</var> : <var>a_{N-1}</var> <var>b_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the indices of the vertices <var>v</var> such that Takahashi can place the piece on <var>v</var> at the beginning and win the game, in a line, in ascending order.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 2 3 1 2 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>The following is one possible progress of the game when Takahashi places the piece on vertex <var>2</var>:</p> <ul> <li>Takahashi moves the piece to vertex <var>1</var>. The number of the stones on each vertex is now: <var>(1,1,3)</var>.</li> <li>Aoki moves the piece to vertex <var>2</var>. The number of the stones on each vertex is now: <var>(0,1,3)</var>.</li> <li>Takahashi moves the piece to vertex <var>1</var>. The number of the stones on each vertex is now: <var>(0,0,3)</var>.</li> <li>Aoki cannot take a stone from the vertex, and thus Takahashi wins.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 5 4 1 2 3 1 2 1 3 2 4 2 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 2 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 1 1 1 1 2 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre> </pre> <p>Note that the correct output may be an empty line.</p></section> </div> </span>
[ [ "3\n1 2 3\n1 2\n2 3\n", "3\n1 2 3\n1 2\n2 3\n" ] ]
p03813
AtCoder Beginner Contest 053 - ABC/ARC
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Smeke has decided to participate in AtCoder Beginner Contest (ABC) if his current rating is less than <var>1200</var>, and participate in AtCoder Regular Contest (ARC) otherwise.</p> <p>You are given Smeke's current rating, <var>x</var>. Print <code>ABC</code> if Smeke will participate in ABC, and print <code>ARC</code> otherwise.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ x ≦ 3{,}000</var></li> <li><var>x</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>x</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>1000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>ABC </pre> <p>Smeke's current rating is less than <var>1200</var>, thus the output should be <code>ABC</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>ARC </pre> <p>Smeke's current rating is not less than <var>1200</var>, thus the output should be <code>ARC</code>.</p></section> </div> </span>
[ [ "1000\n", "1000\n" ] ]
p03814
AtCoder Beginner Contest 053 - A to Z String
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has decided to construct a string that starts with <code>A</code> and ends with <code>Z</code>, by taking out a substring of a string <var>s</var> (that is, a consecutive part of <var>s</var>).</p> <p>Find the greatest length of the string Snuke can construct. Here, the test set guarantees that there always exists a substring of <var>s</var> that starts with <code>A</code> and ends with <code>Z</code>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ |s| ≦ 200{,}000</var></li> <li><var>s</var> consists of uppercase English letters.</li> <li>There exists a substring of <var>s</var> that starts with <code>A</code> and ends with <code>Z</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>s</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>QWERTYASDFZXCV </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>5 </pre> <p>By taking out the seventh through eleventh characters, it is possible to construct <code>ASDFZ</code>, which starts with <code>A</code> and ends with <code>Z</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>ZABCZ </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>4 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>HASFJGHOGAKZZFEGA </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>12 </pre></section> </div> </span>
[ [ "QWERTYASDFZXCV\n", "QWERTYASDFZXCV\n" ] ]
p03815
AtCoder Beginner Contest 053 - X: Yet Another Die Game
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has decided to play with a six-sided die. Each of its six sides shows an integer <var>1</var> through <var>6</var>, and two numbers on opposite sides always add up to <var>7</var>.</p> <p>Snuke will first put the die on the table with an arbitrary side facing upward, then repeatedly perform the following operation:</p> <ul> <li>Operation: Rotate the die <var>90°</var> toward one of the following directions: left, right, front (the die will come closer) and back (the die will go farther). Then, obtain <var>y</var> points where <var>y</var> is the number written in the side facing upward.</li> </ul> <p>For example, let us consider the situation where the side showing <var>1</var> faces upward, the near side shows <var>5</var> and the right side shows <var>4</var>, as illustrated in the figure. If the die is rotated toward the right as shown in the figure, the side showing <var>3</var> will face upward. Besides, the side showing <var>4</var> will face upward if the die is rotated toward the left, the side showing <var>2</var> will face upward if the die is rotated toward the front, and the side showing <var>5</var> will face upward if the die is rotated toward the back.</p> <div style="text-align: center;"> <img alt="864abc2e4a08c26015ffd007a30aab03.png" src="https://atcoder.jp/img/arc068/864abc2e4a08c26015ffd007a30aab03.png"> </img></div> <p>Find the minimum number of operation Snuke needs to perform in order to score at least <var>x</var> points in total.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ x ≦ 10^{15}</var></li> <li><var>x</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>x</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>149696127901 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>27217477801 </pre></section> </div> </span>
[ [ "7\n", "7\n" ] ]
p03816
AtCoder Beginner Contest 053 - Card Eater
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has decided to play a game using cards. He has a deck consisting of <var>N</var> cards. On the <var>i</var>-th card from the top, an integer <var>A_i</var> is written.</p> <p>He will perform the operation described below zero or more times, so that the values written on the remaining cards will be pairwise distinct. Find the maximum possible number of remaining cards. Here, <var>N</var> is odd, which guarantees that at least one card can be kept.</p> <p>Operation: Take out three arbitrary cards from the deck. Among those three cards, eat two: one with the largest value, and another with the smallest value. Then, return the remaining one card to the deck.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>3 ≦ N ≦ 10^{5}</var></li> <li><var>N</var> is odd.</li> <li><var>1 ≦ A_i ≦ 10^{5}</var></li> <li><var>A_i</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>A_2</var> <var>A_3</var> ... <var>A_{N}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 1 2 1 3 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>One optimal solution is to perform the operation once, taking out two cards with <var>1</var> and one card with <var>2</var>. One card with <var>1</var> and another with <var>2</var> will be eaten, and the remaining card with <var>1</var> will be returned to deck. Then, the values written on the remaining cards in the deck will be pairwise distinct: <var>1</var>, <var>3</var> and <var>7</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>15 1 3 5 2 1 3 2 8 8 6 2 6 11 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>7 </pre></section> </div> </span>
[ [ "5\n1 2 1 3 7\n", "5\n1 2 1 3 7\n" ] ]
p03817
AtCoder Regular Contest 068 - X: Yet Another Die Game
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has decided to play with a six-sided die. Each of its six sides shows an integer <var>1</var> through <var>6</var>, and two numbers on opposite sides always add up to <var>7</var>.</p> <p>Snuke will first put the die on the table with an arbitrary side facing upward, then repeatedly perform the following operation:</p> <ul> <li>Operation: Rotate the die <var>90°</var> toward one of the following directions: left, right, front (the die will come closer) and back (the die will go farther). Then, obtain <var>y</var> points where <var>y</var> is the number written in the side facing upward.</li> </ul> <p>For example, let us consider the situation where the side showing <var>1</var> faces upward, the near side shows <var>5</var> and the right side shows <var>4</var>, as illustrated in the figure. If the die is rotated toward the right as shown in the figure, the side showing <var>3</var> will face upward. Besides, the side showing <var>4</var> will face upward if the die is rotated toward the left, the side showing <var>2</var> will face upward if the die is rotated toward the front, and the side showing <var>5</var> will face upward if the die is rotated toward the back.</p> <div style="text-align: center;"> <img alt="864abc2e4a08c26015ffd007a30aab03.png" src="https://atcoder.jp/img/arc068/864abc2e4a08c26015ffd007a30aab03.png"> </img></div> <p>Find the minimum number of operation Snuke needs to perform in order to score at least <var>x</var> points in total.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ x ≦ 10^{15}</var></li> <li><var>x</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>x</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>149696127901 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>27217477801 </pre></section> </div> </span>
[ [ "7\n", "7\n" ] ]
p03818
AtCoder Regular Contest 068 - Card Eater
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has decided to play a game using cards. He has a deck consisting of <var>N</var> cards. On the <var>i</var>-th card from the top, an integer <var>A_i</var> is written.</p> <p>He will perform the operation described below zero or more times, so that the values written on the remaining cards will be pairwise distinct. Find the maximum possible number of remaining cards. Here, <var>N</var> is odd, which guarantees that at least one card can be kept.</p> <p>Operation: Take out three arbitrary cards from the deck. Among those three cards, eat two: one with the largest value, and another with the smallest value. Then, return the remaining one card to the deck.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>3 ≦ N ≦ 10^{5}</var></li> <li><var>N</var> is odd.</li> <li><var>1 ≦ A_i ≦ 10^{5}</var></li> <li><var>A_i</var> is an integer.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>A_2</var> <var>A_3</var> ... <var>A_{N}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 1 2 1 3 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>One optimal solution is to perform the operation once, taking out two cards with <var>1</var> and one card with <var>2</var>. One card with <var>1</var> and another with <var>2</var> will be eaten, and the remaining card with <var>1</var> will be returned to deck. Then, the values written on the remaining cards in the deck will be pairwise distinct: <var>1</var>, <var>3</var> and <var>7</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>15 1 3 5 2 1 3 2 8 8 6 2 6 11 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>7 </pre></section> </div> </span>
[ [ "5\n1 2 1 3 7\n", "5\n1 2 1 3 7\n" ] ]
p03819
AtCoder Regular Contest 068 - Snuke Line
<span class="lang-en"> <p>Score : <var>700</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has decided to play a game, where the player runs a railway company. There are <var>M+1</var> stations on Snuke Line, numbered <var>0</var> through <var>M</var>. A train on Snuke Line stops at station <var>0</var> and every <var>d</var>-th station thereafter, where <var>d</var> is a predetermined constant for each train. For example, if <var>d = 3</var>, the train stops at station <var>0</var>, <var>3</var>, <var>6</var>, <var>9</var>, and so forth.</p> <p>There are <var>N</var> kinds of souvenirs sold in areas around Snuke Line. The <var>i</var>-th kind of souvenirs can be purchased when the train stops at one of the following stations: stations <var>l_i</var>, <var>l_i+1</var>, <var>l_i+2</var>, <var>...</var>, <var>r_i</var>.</p> <p>There are <var>M</var> values of <var>d</var>, the interval between two stops, for trains on Snuke Line: <var>1</var>, <var>2</var>, <var>3</var>, <var>...</var>, <var>M</var>. For each of these <var>M</var> values, find the number of the kinds of souvenirs that can be purchased if one takes a train with that value of <var>d</var> at station <var>0</var>. Here, assume that it is not allowed to change trains.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ N ≦ 3 × 10^{5}</var></li> <li><var>1 ≦ M ≦ 10^{5}</var></li> <li><var>1 ≦ l_i ≦ r_i ≦ M</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>l_1</var> <var>r_1</var> <var>:</var> <var>l_{N}</var> <var>r_{N}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer in <var>M</var> lines. The <var>i</var>-th line should contain the maximum number of the kinds of souvenirs that can be purchased if one takes a train stopping every <var>i</var>-th station.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 1 2 2 3 3 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 2 2 </pre> <ul> <li>If one takes a train stopping every station, three kinds of souvenirs can be purchased: kind <var>1</var>, <var>2</var> and <var>3</var>.</li> <li>If one takes a train stopping every second station, two kinds of souvenirs can be purchased: kind <var>1</var> and <var>2</var>.</li> <li>If one takes a train stopping every third station, two kinds of souvenirs can be purchased: kind <var>2</var> and <var>3</var>.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 9 1 7 5 9 5 7 5 9 1 1 6 8 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>7 6 6 5 4 5 5 3 2 </pre></section> </div> </span>
[ [ "3 3\n1 2\n2 3\n3 3\n", "3 3\n1 2\n2 3\n3 3\n" ] ]
p03820
AtCoder Regular Contest 068 - Solitaire
<span class="lang-en"> <p>Score : <var>1200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has decided to play with <var>N</var> cards and a deque (that is, a double-ended queue). Each card shows an integer from <var>1</var> through <var>N</var>, and the deque is initially empty.</p> <p>Snuke will insert the cards at the beginning or the end of the deque one at a time, in order from <var>1</var> to <var>N</var>. Then, he will perform the following action <var>N</var> times: take out the card from the beginning or the end of the deque and eat it.</p> <p>Afterwards, we will construct an integer sequence by arranging the integers written on the eaten cards, in the order they are eaten. Among the sequences that can be obtained in this way, find the number of the sequences such that the <var>K</var>-th element is <var>1</var>. Print the answer modulo <var>10^{9} + 7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ K ≦ N ≦ 2{,}000</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the answer modulo <var>10^{9} + 7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 </pre> <p>There is one sequence satisfying the condition: <var>1,2</var>. One possible way to obtain this sequence is the following:</p> <ul> <li>Insert both cards, <var>1</var> and <var>2</var>, at the end of the deque.</li> <li>Eat the card at the beginning of the deque twice.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>17 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>262144 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>2000 1000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>674286644 </pre></section> </div> </span>
[ [ "2 1\n", "2 1\n" ] ]
p03821
AtCoder Grand Contest 009 - Multiple Array
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are an integer sequence <var>A_1,...,A_N</var> consisting of <var>N</var> terms, and <var>N</var> buttons. When the <var>i</var>-th <var>(1 ≦ i ≦ N)</var> button is pressed, the values of the <var>i</var> terms from the first through the <var>i</var>-th are all incremented by <var>1</var>.</p> <p>There is also another integer sequence <var>B_1,...,B_N</var>. Takahashi will push the buttons some number of times so that for every <var>i</var>, <var>A_i</var> will be a multiple of <var>B_i</var>.</p> <p>Find the minimum number of times Takahashi will press the buttons.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>1 ≦ N ≦ 10^5</var></li> <li><var>0 ≦ A_i ≦ 10^9(1 ≦ i ≦ N)</var></li> <li><var>1 ≦ B_i ≦ 10^9(1 ≦ i ≦ N)</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>B_1</var> : <var>A_N</var> <var>B_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print an integer representing the minimum number of times Takahashi will press the buttons.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 5 2 7 9 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>7 </pre> <p>Press the first button twice, the second button twice and the third button three times.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 3 1 4 1 5 9 2 6 5 3 5 8 9 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>22 </pre></section> </div> </span>
[ [ "3\n3 5\n2 7\n9 4\n", "3\n3 5\n2 7\n9 4\n" ] ]
p03822
AtCoder Grand Contest 009 - Tournament
<span class="lang-en"> <p>Score : <var>800</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p><var>N</var> contestants participated in a competition. The total of <var>N-1</var> matches were played in a knockout tournament. For some reasons, the tournament may not be "fair" for all the contestants. That is, the number of the matches that must be played in order to win the championship may be different for each contestant. The structure of the tournament is formally described at the end of this statement.</p> <p>After each match, there were always one winner and one loser. The last contestant standing was declared the champion.</p> <div style="text-align: center;"> <img src="https://atcoder.jp/img/agc009/783f7be9c88350e31963edba8a958879.png"> <p>Figure: an example of a tournament</p> </img></div> <p>For convenience, the contestants were numbered <var>1</var> through <var>N</var>. The contestant numbered <var>1</var> was the champion, and the contestant numbered <var>i(2 ≦ i ≦ N)</var> was defeated in a match against the contestant numbered <var>a_i</var>.</p> <p>We will define the <em>depth</em> of the tournament as the maximum number of the matches that must be played in order to win the championship over all the contestants.</p> <p>Find the minimum possible depth of the tournament.</p> <p>The formal description of the structure of the tournament is as follows. In the <var>i</var>-th match, one of the following played against each other:</p> <ul> <li>Two predetermined contestants</li> <li>One predetermined contestant and the winner of the <var>j</var>-th match, where <var>j(j&lt;i)</var> was predetermined</li> <li>The winner of the <var>j</var>-th match and the winner of the <var>k</var>-th match, where <var>j</var> and <var>k</var> <var>(j,k&lt;i, j ≠ k)</var> were predetermined</li> </ul> <p>Such structure is valid structure of the tournament, if and only if no contestant who has already been defeated in a match will never play in a match, regardless of the outcome of the matches.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≦ N ≦ 10^5</var></li> <li><var>1 ≦ a_i ≦ N(2 ≦ i ≦ N)</var></li> <li>It is guaranteed that the input is consistent (that is, there exists at least one tournament that matches the given information).</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_2</var> : <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum possible depth of the tournament.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 1 1 2 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>The following tournament and the result of the matches are consistent with the given information:</p> <ul> <li>In the first match, contestants <var>4</var> and <var>5</var> played against each other, and contestant <var>4</var> won.</li> <li>In the second match, contestants <var>2</var> and <var>4</var> played against each other, and contestant <var>2</var> won.</li> <li>In the third match, contestants <var>1</var> and <var>3</var> played against each other, and contestant <var>1</var> won.</li> <li>In the fourth match, contestants <var>1</var> and <var>2</var> played against each other, and contestant <var>1</var> won.</li> </ul> <div style="text-align: center;"> <img alt="783f7be9c88350e31963edba8a958879.png" src="https://atcoder.jp/img/agc009/783f7be9c88350e31963edba8a958879.png"> </img></div> <p>The depth of this tournament is <var>3</var>, since contestant <var>5</var> must play three matches in order to win the championship. There is no tournament with depth <var>2</var> or less that is consistent with the given information, thus the output should be <var>3</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 1 2 1 3 1 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>4 4 4 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>3 </pre></section> </div> </span>
[ [ "5\n1\n1\n2\n4\n", "5\n1\n1\n2\n4\n" ] ]
p03823
AtCoder Grand Contest 009 - Division into Two
<span class="lang-en"> <p>Score : <var>1100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a set consisting of <var>N</var> distinct integers. The <var>i</var>-th smallest element in this set is <var>S_i</var>. We want to divide this set into two sets, <var>X</var> and <var>Y</var>, such that:</p> <ul> <li>The absolute difference of any two distinct elements in <var>X</var> is <var>A</var> or greater.</li> <li>The absolute difference of any two distinct elements in <var>Y</var> is <var>B</var> or greater.</li> </ul> <p>How many ways are there to perform such division, modulo <var>10^9 + 7</var>? Note that one of <var>X</var> and <var>Y</var> may be empty.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>1 ≦ N ≦ 10^5</var></li> <li><var>1 ≦ A , B ≦ 10^{18}</var></li> <li><var>0 ≦ S_i ≦ 10^{18}(1 ≦ i ≦ N)</var></li> <li><var>S_i &lt; S_{i+1}(1 ≦ i ≦ N - 1)</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A</var> <var>B</var> <var>S_1</var> : <var>S_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different divisions under the conditions, modulo <var>10^9 + 7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 3 7 1 3 6 9 12 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>5 </pre> <p>There are five ways to perform division:</p> <ul> <li><var>X=</var>{<var>1,6,9,12</var>}, <var>Y=</var>{<var>3</var>}</li> <li><var>X=</var>{<var>1,6,9</var>}, <var>Y=</var>{<var>3,12</var>}</li> <li><var>X=</var>{<var>3,6,9,12</var>}, <var>Y=</var>{<var>1</var>}</li> <li><var>X=</var>{<var>3,6,9</var>}, <var>Y=</var>{<var>1,12</var>}</li> <li><var>X=</var>{<var>3,6,12</var>}, <var>Y=</var>{<var>1,9</var>}</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 5 3 0 2 4 7 8 11 15 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>4 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>8 2 9 3 4 5 13 15 22 26 32 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>13 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>3 3 4 5 6 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>0 </pre></section> </div> </span>
[ [ "5 3 7\n1\n3\n6\n9\n12\n", "5 3 7\n1\n3\n6\n9\n12\n" ] ]
p03824
AtCoder Grand Contest 009 - Uninity
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We will recursively define <em>uninity</em> of a tree, as follows: (<em>Uni</em> is a Japanese word for sea urchins.)</p> <ul> <li>A tree consisting of one vertex is a tree of uninity <var>0</var>.</li> <li>Suppose there are zero or more trees of uninity <var>k</var>, and a vertex <var>v</var>. If a vertex is selected from each tree of uninity <var>k</var> and connected to <var>v</var> with an edge, the resulting tree is a tree of uninity <var>k+1</var>.</li> </ul> <p>It can be shown that a tree of uninity <var>k</var> is also a tree of uninity <var>k+1,k+2,...</var>, and so forth.</p> <p>You are given a tree consisting of <var>N</var> vertices. The vertices of the tree are numbered <var>1</var> through <var>N</var>, and the <var>i</var>-th of the <var>N-1</var> edges connects vertices <var>a_i</var> and <var>b_i</var>.</p> <p>Find the minimum <var>k</var> such that the given tree is a tree of uninity <var>k</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≦ N ≦ 10^5</var></li> <li><var>1 ≦ a_i, b_i ≦ N(1 ≦ i ≦ N-1)</var></li> <li>The given graph is a tree.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>b_1</var> : <var>a_{N-1}</var> <var>b_{N-1}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum <var>k</var> such that the given tree is a tree of uninity <var>k</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>7 1 2 2 3 2 4 4 6 6 7 7 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>A tree of uninity <var>1</var> consisting of vertices <var>1</var>, <var>2</var>, <var>3</var> and <var>4</var> can be constructed from the following: a tree of uninity <var>0</var> consisting of vertex <var>1</var>, a tree of uninity <var>0</var> consisting of vertex <var>3</var>, a tree of uninity <var>0</var> consisting of vertex <var>4</var>, and vertex <var>2</var>.</p> <p>A tree of uninity <var>1</var> consisting of vertices <var>5</var> and <var>7</var> can be constructed from the following: a tree of uninity <var>1</var> consisting of vertex <var>5</var>, and vertex <var>7</var>.</p> <p>A tree of uninity <var>2</var> consisting of vertices <var>1</var>, <var>2</var>, <var>3</var>, <var>4</var>, <var>5</var>, <var>6</var> and <var>7</var> can be constructed from the following: a tree of uninity <var>1</var> consisting of vertex <var>1</var>, <var>2</var>, <var>3</var> and <var>4</var>, a tree of uninity <var>1</var> consisting of vertex <var>5</var> and <var>7</var>, and vertex <var>6</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>12 1 2 2 3 2 4 4 5 5 6 6 7 7 8 5 9 9 10 10 11 11 12 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3 </pre></section> </div> </span>
[ [ "7\n1 2\n2 3\n2 4\n4 6\n6 7\n7 5\n", "7\n1 2\n2 3\n2 4\n4 6\n6 7\n7 5\n" ] ]
p03825
AtCoder Grand Contest 009 - Eternal Average
<span class="lang-en"> <p>Score : <var>1600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> zeros and <var>M</var> ones written on a blackboard. Starting from this state, we will repeat the following operation: select <var>K</var> of the rational numbers written on the blackboard and erase them, then write a new number on the blackboard that is equal to the arithmetic mean of those <var>K</var> numbers. Here, assume that <var>N + M - 1</var> is divisible by <var>K - 1</var>.</p> <p>Then, if we repeat this operation until it is no longer applicable, there will be eventually one rational number left on the blackboard.</p> <p>Find the number of the different possible values taken by this rational number, modulo <var>10^9 + 7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≦ N, M ≦ 2000</var></li> <li><var>2 ≦ K ≦ 2000</var></li> <li><var>N + M - 1</var> is divisible by <var>K - 1</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>K</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the different possible values taken by the rational number that will be eventually left on the blackboard, modulo <var>10^9 + 7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 2 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>5 </pre> <p>There are five possible values for the number that will be eventually left on the blackboard: <var>\frac{1}{4}</var>, <var>\frac{3}{8}</var>, <var>\frac{1}{2}</var>, <var>\frac{5}{8}</var> and <var>\frac{3}{4}</var>.</p> <p>For example, <var>\frac{3}{8}</var> can be eventually left if we:</p> <ul> <li>Erase <var>0</var> and <var>1</var>, then write <var>\frac{1}{2}</var>.</li> <li>Erase <var>\frac{1}{2}</var> and <var>1</var>, then write <var>\frac{3}{4}</var>.</li> <li>Erase <var>0</var> and <var>\frac{3}{4}</var>, then write <var>\frac{3}{8}</var>.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 4 3 </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>150 150 14 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>937426930 </pre></section> </div> </span>
[ [ "2 2 2\n", "2 2 2\n" ] ]
p03826
AtCoder Beginner Contest 052 - Two Rectangles
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are two rectangles. The lengths of the vertical sides of the first rectangle are <var>A</var>, and the lengths of the horizontal sides of the first rectangle are <var>B</var>. The lengths of the vertical sides of the second rectangle are <var>C</var>, and the lengths of the horizontal sides of the second rectangle are <var>D</var>.</p> <p>Print the area of the rectangle with the larger area. If the two rectangles have equal areas, print that area.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>1≤A≤10^4</var></li> <li><var>1≤B≤10^4</var></li> <li><var>1≤C≤10^4</var></li> <li><var>1≤D≤10^4</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>B</var> <var>C</var> <var>D</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the area of the rectangle with the larger area. If the two rectangles have equal areas, print that area.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 5 2 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>15 </pre> <p>The first rectangle has an area of <var>3×5=15</var>, and the second rectangle has an area of <var>2×7=14</var>. Thus, the output should be <var>15</var>, the larger area.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>100 600 200 300 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>60000 </pre></section> </div> </span>
[ [ "3 5 2 7\n", "3 5 2 7\n" ] ]
p03827
AtCoder Beginner Contest 052 - Increment Decrement
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You have an integer variable <var>x</var>. Initially, <var>x=0</var>.</p> <p>Some person gave you a string <var>S</var> of length <var>N</var>, and using the string you performed the following operation <var>N</var> times. In the <var>i</var>-th operation, you incremented the value of <var>x</var> by <var>1</var> if <var>S_i=</var><code>I</code>, and decremented the value of <var>x</var> by <var>1</var> if <var>S_i=</var><code>D</code>.</p> <p>Find the maximum value taken by <var>x</var> during the operations (including before the first operation, and after the last operation).</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≤N≤100</var></li> <li><var>|S|=N</var></li> <li>No characters except <code>I</code> and <code>D</code> occur in <var>S</var>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>S</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum value taken by <var>x</var> during the operations.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 IIDID </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>After each operation, the value of <var>x</var> becomes <var>1</var>, <var>2</var>, <var>1</var>, <var>2</var> and <var>1</var>, respectively. Thus, the output should be <var>2</var>, the maximum value.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 DDIDDII </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>The initial value <var>x=0</var> is the maximum value taken by <var>x</var>, thus the output should be <var>0</var>.</p></section> </div> </span>
[ [ "5\nIIDID\n", "5\nIIDID\n" ] ]
p03828
AtCoder Beginner Contest 052 - Factors of Factorial
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an integer <var>N</var>. Find the number of the positive divisors of <var>N!</var>, modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≤N≤10^3</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the positive divisors of <var>N!</var>, 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>4 </pre> <p>There are four divisors of <var>3!</var> <var>=6</var>: <var>1</var>, <var>2</var>, <var>3</var> and <var>6</var>. Thus, the output should be <var>4</var>.</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>30 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>972926972 </pre></section> </div> </span>
[ [ "3\n", "3\n" ] ]
p03829
AtCoder Beginner Contest 052 - Walk and Teleport
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> towns on a line running east-west. The towns are numbered <var>1</var> through <var>N</var>, in order from west to east. Each point on the line has a one-dimensional coordinate, and a point that is farther east has a greater coordinate value. The coordinate of town <var>i</var> is <var>X_i</var>.</p> <p>You are now at town <var>1</var>, and you want to visit all the other towns. You have two ways to travel:</p> <ul> <li> <p>Walk on the line. Your <em>fatigue level</em> increases by <var>A</var> each time you travel a distance of <var>1</var>, regardless of direction.</p> </li> <li> <p>Teleport to any location of your choice. Your fatigue level increases by <var>B</var>, regardless of the distance covered.</p> </li> </ul> <p>Find the minimum possible total increase of your fatigue level when you visit all the towns in these two ways.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>2≤N≤10^5</var></li> <li><var>1≤X_i≤10^9</var></li> <li>For all <var>i(1≤i≤N-1)</var>, <var>X_i&lt;X_{i+1}</var>.</li> <li><var>1≤A≤10^9</var></li> <li><var>1≤B≤10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A</var> <var>B</var> <var>X_1</var> <var>X_2</var> <var>...</var> <var>X_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum possible total increase of your fatigue level when you visit all the towns.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 2 5 1 2 5 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>11 </pre> <p>From town <var>1</var>, walk a distance of <var>1</var> to town <var>2</var>, then teleport to town <var>3</var>, then walk a distance of <var>2</var> to town <var>4</var>. The total increase of your fatigue level in this case is <var>2×1+5+2×2=11</var>, which is the minimum possible value.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 1 100 40 43 45 105 108 115 124 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>84 </pre> <p>From town <var>1</var>, walk all the way to town <var>7</var>. The total increase of your fatigue level in this case is <var>84</var>, which is the minimum possible value.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>7 1 2 24 35 40 68 72 99 103 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>12 </pre> <p>Visit all the towns in any order by teleporting six times. The total increase of your fatigue level in this case is <var>12</var>, which is the minimum possible value.</p></section> </div> </span>
[ [ "4 2 5\n1 2 5 7\n", "4 2 5\n1 2 5 7\n" ] ]
p03830
AtCoder Regular Contest 067 - Factors of Factorial
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an integer <var>N</var>. Find the number of the positive divisors of <var>N!</var>, modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≤N≤10^3</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the positive divisors of <var>N!</var>, 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>4 </pre> <p>There are four divisors of <var>3!</var> <var>=6</var>: <var>1</var>, <var>2</var>, <var>3</var> and <var>6</var>. Thus, the output should be <var>4</var>.</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>30 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>972926972 </pre></section> </div> </span>
[ [ "3\n", "3\n" ] ]
p03831
AtCoder Regular Contest 067 - Walk and Teleport
<span class="lang-en"> <p>Score : <var>500</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> towns on a line running east-west. The towns are numbered <var>1</var> through <var>N</var>, in order from west to east. Each point on the line has a one-dimensional coordinate, and a point that is farther east has a greater coordinate value. The coordinate of town <var>i</var> is <var>X_i</var>.</p> <p>You are now at town <var>1</var>, and you want to visit all the other towns. You have two ways to travel:</p> <ul> <li> <p>Walk on the line. Your <em>fatigue level</em> increases by <var>A</var> each time you travel a distance of <var>1</var>, regardless of direction.</p> </li> <li> <p>Teleport to any location of your choice. Your fatigue level increases by <var>B</var>, regardless of the distance covered.</p> </li> </ul> <p>Find the minimum possible total increase of your fatigue level when you visit all the towns in these two ways.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>2≤N≤10^5</var></li> <li><var>1≤X_i≤10^9</var></li> <li>For all <var>i(1≤i≤N-1)</var>, <var>X_i&lt;X_{i+1}</var>.</li> <li><var>1≤A≤10^9</var></li> <li><var>1≤B≤10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A</var> <var>B</var> <var>X_1</var> <var>X_2</var> <var>...</var> <var>X_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum possible total increase of your fatigue level when you visit all the towns.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 2 5 1 2 5 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>11 </pre> <p>From town <var>1</var>, walk a distance of <var>1</var> to town <var>2</var>, then teleport to town <var>3</var>, then walk a distance of <var>2</var> to town <var>4</var>. The total increase of your fatigue level in this case is <var>2×1+5+2×2=11</var>, which is the minimum possible value.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 1 100 40 43 45 105 108 115 124 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>84 </pre> <p>From town <var>1</var>, walk all the way to town <var>7</var>. The total increase of your fatigue level in this case is <var>84</var>, which is the minimum possible value.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>7 1 2 24 35 40 68 72 99 103 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>12 </pre> <p>Visit all the towns in any order by teleporting six times. The total increase of your fatigue level in this case is <var>12</var>, which is the minimum possible value.</p></section> </div> </span>
[ [ "4 2 5\n1 2 5 7\n", "4 2 5\n1 2 5 7\n" ] ]
p03832
AtCoder Regular Contest 067 - Grouping
<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> people, conveniently numbered <var>1</var> through <var>N</var>. We want to divide them into some number of groups, under the following two conditions:</p> <ul> <li> <p>Every group contains between <var>A</var> and <var>B</var> people, inclusive.</p> </li> <li> <p>Let <var>F_i</var> be the number of the groups containing exactly <var>i</var> people. Then, for all <var>i</var>, either <var>F_i=0</var> or <var>C≤F_i≤D</var> holds.</p> </li> </ul> <p>Find the number of these ways to divide the people into groups. Here, two ways to divide them into groups is considered different if and only if there exists two people such that they belong to the same group in exactly one of the two ways. Since the number of these ways can be extremely large, print the count modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≤N≤10^3</var></li> <li><var>1≤A≤B≤N</var></li> <li><var>1≤C≤D≤N</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A</var> <var>B</var> <var>C</var> <var>D</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways to divide the people into groups under the conditions, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 3 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>There are four ways to divide the people:</p> <ul> <li><var>(1,2),(3)</var></li> <li><var>(1,3),(2)</var></li> <li><var>(2,3),(1)</var></li> <li><var>(1,2,3)</var></li> </ul> <p>The following way to divide the people does not count: <var>(1),(2),(3)</var>. This is because it only satisfies the first condition and not the second.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 2 3 1 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>105 </pre> <p>The only ways to divide the people under the conditions are the ones where there are two groups of two people, and one group of three people. There are <var>105</var> such ways.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1000 1 1000 1 1000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>465231251 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>10 3 4 2 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>0 </pre> <p>The answer can be <var>0</var>.</p></section> </div> </span>
[ [ "3 1 3 1 2\n", "3 1 3 1 2\n" ] ]
p03833
AtCoder Regular Contest 067 - Yakiniku Restaurants
<span class="lang-en"> <p>Score : <var>1000</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> barbecue restaurants along a street. The restaurants are numbered <var>1</var> through <var>N</var> from west to east, and the distance between restaurant <var>i</var> and restaurant <var>i+1</var> is <var>A_i</var>.</p> <p>Joisino has <var>M</var> tickets, numbered <var>1</var> through <var>M</var>. Every barbecue restaurant offers barbecue meals in exchange for these tickets. Restaurant <var>i</var> offers a meal of <em>deliciousness</em> <var>B_{i,j}</var> in exchange for ticket <var>j</var>. Each ticket can only be used once, but any number of tickets can be used at a restaurant.</p> <p>Joisino wants to have <var>M</var> barbecue meals by starting from a restaurant of her choice, then repeatedly traveling to another barbecue restaurant and using unused tickets at the restaurant at her current location. Her eventual <em>happiness</em> is calculated by the following formula: "(The total deliciousness of the meals eaten) - (The total distance traveled)". Find her maximum possible eventual happiness.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>2≤N≤5×10^3</var></li> <li><var>1≤M≤200</var></li> <li><var>1≤A_i≤10^9</var></li> <li><var>1≤B_{i,j}≤10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_{N-1}</var> <var>B_{1,1}</var> <var>B_{1,2}</var> <var>...</var> <var>B_{1,M}</var> <var>B_{2,1}</var> <var>B_{2,2}</var> <var>...</var> <var>B_{2,M}</var> <var>:</var> <var>B_{N,1}</var> <var>B_{N,2}</var> <var>...</var> <var>B_{N,M}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print Joisino's maximum possible eventual happiness.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 4 1 4 2 2 5 1 1 3 3 2 2 2 5 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>11 </pre> <p>The eventual happiness can be maximized by the following strategy: start from restaurant <var>1</var> and use tickets <var>1</var> and <var>3</var>, then move to restaurant <var>2</var> and use tickets <var>2</var> and <var>4</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 3 1 2 3 4 10 1 1 1 1 1 1 10 1 1 1 1 1 1 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>20 </pre></section> </div> </span>
[ [ "3 4\n1 4\n2 2 5 1\n1 3 3 2\n2 2 5 1\n", "3 4\n1 4\n2 2 5 1\n1 3 3 2\n2 2 5 1\n" ] ]
p03834
AtCoder Beginner Contest 051 - Haiku
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>As a New Year's gift, Dolphin received a string <var>s</var> of length <var>19</var>.<br/> The string <var>s</var> has the following format: <code>[five lowercase English letters],[seven lowercase English letters],[five lowercase English letters]</code>.<br/> Dolphin wants to convert the comma-separated string <var>s</var> into a space-separated string.<br/> Write a program to perform the conversion for him. </p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>The length of <var>s</var> is <var>19</var>.</li> <li>The sixth and fourteenth characters in <var>s</var> are <code>,</code>.</li> <li>The other characters in <var>s</var> are lowercase English letters.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>s</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the string after the conversion.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>happy,newyear,enjoy </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>happy newyear enjoy </pre> <p>Replace all the commas in <code>happy,newyear,enjoy</code> with spaces to obtain <code>happy newyear enjoy</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>haiku,atcoder,tasks </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>haiku atcoder tasks </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>abcde,fghihgf,edcba </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>abcde fghihgf edcba </pre></section> </div> </span>
[ [ "happy,newyear,enjoy\n", "happy,newyear,enjoy\n" ] ]
p03835
AtCoder Beginner Contest 051 - Sum of Three Integers
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given two integers <var>K</var> and <var>S</var>.<br/> Three variable <var>X, Y</var> and <var>Z</var> takes integer values satisfying <var>0≤X,Y,Z≤K</var>.<br/> How many different assignments of values to <var>X, Y</var> and <var>Z</var> are there such that <var>X + Y + Z = S</var>? </p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≤K≤2500</var> </li> <li><var>0≤S≤3K</var> </li> <li><var>K</var> and <var>S</var> are integers. </li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>K</var> <var>S</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the triples of <var>X, Y</var> and <var>Z</var> that satisfy the condition.</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>6 </pre> <p>There are six triples of <var>X, Y</var> and <var>Z</var> that satisfy the condition:</p> <ul> <li><var>X = 0, Y = 0, Z = 2</var> </li> <li><var>X = 0, Y = 2, Z = 0</var> </li> <li><var>X = 2, Y = 0, Z = 0</var> </li> <li><var>X = 0, Y = 1, Z = 1</var> </li> <li><var>X = 1, Y = 0, Z = 1</var> </li> <li><var>X = 1, Y = 1, Z = 0</var></li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 15 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 </pre> <p>The maximum value of <var>X + Y + Z</var> is <var>15</var>, achieved by one triple of <var>X, Y</var> and <var>Z</var>.</p></section> </div> </span>
[ [ "2 2\n", "2 2\n" ] ]
p03836
AtCoder Beginner Contest 051 - Back and Forth
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Dolphin resides in two-dimensional Cartesian plane, with the positive <var>x</var>-axis pointing right and the positive <var>y</var>-axis pointing up.<br/> Currently, he is located at the point <var>(sx,sy)</var>. In each second, he can move up, down, left or right by a distance of <var>1</var>.<br/> Here, both the <var>x</var>- and <var>y</var>-coordinates before and after each movement must be integers.<br/> He will first visit the point <var>(tx,ty)</var> where <var>sx &lt; tx</var> and <var>sy &lt; ty</var>, then go back to the point <var>(sx,sy)</var>, then visit the point <var>(tx,ty)</var> again, and lastly go back to the point <var>(sx,sy)</var>.<br/> Here, during the whole travel, he is not allowed to pass through the same point more than once, except the points <var>(sx,sy)</var> and <var>(tx,ty)</var>.<br/> Under this condition, find a shortest path for him. </p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>-1000 ≤ sx &lt; tx ≤ 1000</var> </li> <li><var>-1000 ≤ sy &lt; ty ≤ 1000</var> </li> <li><var>sx,sy,tx</var> and <var>ty</var> are integers.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>sx</var> <var>sy</var> <var>tx</var> <var>ty</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print a string <var>S</var> that represents a shortest path for Dolphin.<br/> The <var>i</var>-th character in <var>S</var> should correspond to his <var>i</var>-th movement.<br/> The directions of the movements should be indicated by the following characters: </p> <ul> <li><code>U</code>: Up</li> <li><code>D</code>: Down</li> <li><code>L</code>: Left</li> <li><code>R</code>: Right</li> </ul> <p>If there exist multiple shortest paths under the condition, print any of them.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>0 0 1 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>UURDDLLUUURRDRDDDLLU </pre> <p>One possible shortest path is:</p> <ul> <li>Going from <var>(sx,sy)</var> to <var>(tx,ty)</var> for the first time: <var>(0,0)</var> → <var>(0,1)</var> → <var>(0,2)</var> → <var>(1,2)</var></li> <li>Going from <var>(tx,ty)</var> to <var>(sx,sy)</var> for the first time: <var>(1,2)</var> → <var>(1,1)</var> → <var>(1,0)</var> → <var>(0,0)</var></li> <li>Going from <var>(sx,sy)</var> to <var>(tx,ty)</var> for the second time: <var>(0,0)</var> → <var>(-1,0)</var> → <var>(-1,1)</var> → <var>(-1,2)</var> → <var>(-1,3)</var> → <var>(0,3)</var> → <var>(1,3)</var> → <var>(1,2)</var></li> <li>Going from <var>(tx,ty)</var> to <var>(sx,sy)</var> for the second time: <var>(1,2)</var> → <var>(2,2)</var> → <var>(2,1)</var> → <var>(2,0)</var> → <var>(2,-1)</var> → <var>(1,-1)</var> → <var>(0,-1)</var> → <var>(0,0)</var></li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>-2 -2 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>UURRURRDDDLLDLLULUUURRURRDDDLLDL </pre></section> </div> </span>
[ [ "0 0 1 2\n", "0 0 1 2\n" ] ]
p03837
AtCoder Beginner Contest 051 - Candidates of No Shortest Paths
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an undirected connected weighted graph with <var>N</var> vertices and <var>M</var> edges that contains neither self-loops nor double edges.<br/> The <var>i</var>-th <var>(1≤i≤M)</var> edge connects vertex <var>a_i</var> and vertex <var>b_i</var> with a distance of <var>c_i</var>.<br/> Here, a <em>self-loop</em> is an edge where <var>a_i = b_i (1≤i≤M)</var>, and <em>double edges</em> are two edges where <var>(a_i,b_i)=(a_j,b_j)</var> or <var>(a_i,b_i)=(b_j,a_j) (1≤i&lt;j≤M)</var>.<br/> A <em>connected graph</em> is a graph where there is a path between every pair of different vertices.<br/> Find the number of the edges that are not contained in any shortest path between any pair of different vertices. </p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2≤N≤100</var> </li> <li><var>N-1≤M≤min(N(N-1)/2,1000)</var> </li> <li><var>1≤a_i,b_i≤N</var> </li> <li><var>1≤c_i≤1000</var></li> <li><var>c_i</var> is an integer.</li> <li>The given graph contains neither self-loops nor double edges.</li> <li>The given graph is connected.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>M</var> <var>a_1</var> <var>b_1</var> <var>c_1</var> <var>a_2</var> <var>b_2</var> <var>c_2</var> <var>:</var> <var>a_M</var> <var>b_M</var> <var>c_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the edges in the graph that are not contained in any shortest path between any pair of different vertices.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 3 1 2 1 1 3 1 2 3 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 </pre> <p>In the given graph, the shortest paths between all pairs of different vertices are as follows:</p> <ul> <li>The shortest path from vertex <var>1</var> to vertex <var>2</var> is: vertex <var>1</var> → vertex <var>2</var>, with the length of <var>1</var>.</li> <li>The shortest path from vertex <var>1</var> to vertex <var>3</var> is: vertex <var>1</var> → vertex <var>3</var>, with the length of <var>1</var>.</li> <li>The shortest path from vertex <var>2</var> to vertex <var>1</var> is: vertex <var>2</var> → vertex <var>1</var>, with the length of <var>1</var>.</li> <li>The shortest path from vertex <var>2</var> to vertex <var>3</var> is: vertex <var>2</var> → vertex <var>1</var> → vertex <var>3</var>, with the length of <var>2</var>.</li> <li>The shortest path from vertex <var>3</var> to vertex <var>1</var> is: vertex <var>3</var> → vertex <var>1</var>, with the length of <var>1</var>.</li> <li>The shortest path from vertex <var>3</var> to vertex <var>2</var> is: vertex <var>3</var> → vertex <var>1</var> → vertex <var>2</var>, with the length of <var>2</var>.</li> </ul> <p>Thus, the only edge that is not contained in any shortest path, is the edge of length <var>3</var> connecting vertex <var>2</var> and vertex <var>3</var>, hence the output should be <var>1</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>3 2 1 2 1 2 3 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Every edge is contained in some shortest path between some pair of different vertices.</p></section> </div> </span>
[ [ "3 3\n1 2 1\n1 3 1\n2 3 3\n", "3 3\n1 2 1\n1 3 1\n2 3 3\n" ] ]
p03838
AtCoder Grand Contest 008 - Simple Calculator
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Snuke has a calculator. It has a display and two buttons.</p> <p>Initially, the display shows an integer <var>x</var>. Snuke wants to change this value into another integer <var>y</var>, by pressing the following two buttons some number of times in arbitrary order:</p> <ul> <li>Button A: When pressed, the value on the display is incremented by <var>1</var>.</li> <li>Button B: When pressed, the sign of the value on the display is reversed.</li> </ul> <p>Find the minimum number of times Snuke needs to press the buttons to achieve his objective. It can be shown that the objective is always achievable regardless of the values of the integers <var>x</var> and <var>y</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>x</var> and <var>y</var> are integers.</li> <li><var>|x|, |y| ≤ 10^9</var></li> <li><var>x</var> and <var>y</var> are different.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>x</var> <var>y</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the minimum number of times Snuke needs to press the buttons to achieve his objective.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>10 20 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>10 </pre> <p>Press button A ten times.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 -10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 </pre> <p>Press button B once.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>-10 -20 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>12 </pre> <p>Press the buttons as follows:</p> <ul> <li>Press button B once.</li> <li>Press button A ten times.</li> <li>Press button B once.</li> </ul></section> </div> </span>
[ [ "10 20\n", "10 20\n" ] ]
p03839
AtCoder Grand Contest 008 - Contiguous Repainting
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There are <var>N</var> squares aligned in a row. The <var>i</var>-th square from the left contains an integer <var>a_i</var>.</p> <p>Initially, all the squares are white. Snuke will perform the following operation some number of times:</p> <ul> <li>Select <var>K</var> consecutive squares. Then, paint all of them white, or paint all of them black. Here, the colors of the squares are overwritten.</li> </ul> <p>After Snuke finishes performing the operation, the score will be calculated as the sum of the integers contained in the black squares. Find the maximum possible score.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≤N≤10^5</var></li> <li><var>1≤K≤N</var></li> <li><var>a_i</var> is an integer.</li> <li><var>|a_i|≤10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum possible score.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 3 -10 10 -10 10 -10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>10 </pre> <p>Paint the following squares black: the second, third and fourth squares from the left.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 2 10 -10 -10 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>20 </pre> <p>One possible way to obtain the maximum score is as follows:</p> <ul> <li>Paint the following squares black: the first and second squares from the left.</li> <li>Paint the following squares black: the third and fourth squares from the left.</li> <li>Paint the following squares white: the second and third squares from the left.</li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1 1 -10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>10 5 5 -4 -5 -8 -4 7 2 -4 0 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>17 </pre></section> </div> </span>
[ [ "5 3\n-10 10 -10 10 -10\n", "5 3\n-10 10 -10 10 -10\n" ] ]
p03840
AtCoder Grand Contest 008 - Tetromino Tiling
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>A <em>tetromino</em> is a figure formed by joining four squares edge to edge. We will refer to the following seven kinds of tetromino as I-, O-, T-, J-, L-, S- and Z-tetrominos, respectively:</p> <div style="text-align: center;"> <img alt="a60bcb8e9e8f22e3af51049eda063392.png" src="https://atcoder.jp/img/agc008/a60bcb8e9e8f22e3af51049eda063392.png"> </img></div> <p>Snuke has many tetrominos. The number of I-, O-, T-, J-, L-, S- and Z-tetrominos in his possession are <var>a_I</var>, <var>a_O</var>, <var>a_T</var>, <var>a_J</var>, <var>a_L</var>, <var>a_S</var> and <var>a_Z</var>, respectively. Snuke will join <var>K</var> of his tetrominos to form a rectangle that is two squares tall and <var>2K</var> squares wide. Here, the following rules must be followed:</p> <ul> <li>When placing each tetromino, rotation is allowed, but reflection is not.</li> <li>Each square in the rectangle must be covered by exactly one tetromino.</li> <li>No part of each tetromino may be outside the rectangle.</li> </ul> <p>Snuke wants to form as large a rectangle as possible. Find the maximum possible value of <var>K</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>0≤a_I,a_O,a_T,a_J,a_L,a_S,a_Z≤10^9</var></li> <li><var>a_I+a_O+a_T+a_J+a_L+a_S+a_Z≥1</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>a_I</var> <var>a_O</var> <var>a_T</var> <var>a_J</var> <var>a_L</var> <var>a_S</var> <var>a_Z</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum possible value of <var>K</var>. If no rectangle can be formed, print <code>0</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 1 1 0 0 0 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>One possible way to form the largest rectangle is shown in the following figure:</p> <div style="text-align: center;"> <img alt="45515ed2a1dd5e41c5e4ca1f39323d8e.png" src="https://atcoder.jp/img/agc008/45515ed2a1dd5e41c5e4ca1f39323d8e.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>0 0 10 0 0 0 0 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>No rectangle can be formed.</p></section> </div> </span>
[ [ "2 1 1 0 0 0 0\n", "2 1 1 0 0 0 0\n" ] ]
p03841
AtCoder Grand Contest 008 - K-th K
<span class="lang-en"> <p>Score : <var>800</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an integer sequence <var>x</var> of length <var>N</var>. Determine if there exists an integer sequence <var>a</var> that satisfies all of the following conditions, and if it exists, construct an instance of <var>a</var>.</p> <ul> <li><var>a</var> is <var>N^2</var> in length, containing <var>N</var> copies of each of the integers <var>1</var>, <var>2</var>, <var>...</var>, <var>N</var>.</li> <li>For each <var>1 ≤ i ≤ N</var>, the <var>i</var>-th occurrence of the integer <var>i</var> from the left in <var>a</var> is the <var>x_i</var>-th element of <var>a</var> from the left.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 500</var></li> <li><var>1 ≤ x_i ≤ N^2</var></li> <li>All <var>x_i</var> are distinct.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>x_1</var> <var>x_2</var> <var>...</var> <var>x_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If there does not exist an integer sequence <var>a</var> that satisfies all the conditions, print <code>No</code>. If there does exist such an sequence <var>a</var>, print <code>Yes</code> in the first line, then print an instance of <var>a</var> in the second line, with spaces inbetween.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 5 9 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>Yes 1 1 1 2 2 2 3 3 3 </pre> <p>For example, the second occurrence of the integer <var>2</var> from the left in <var>a</var> in the output is the fifth element of <var>a</var> from the left. Similarly, the condition is satisfied for the integers <var>1</var> and <var>3</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 4 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>No </pre></section> </div> </span>
[ [ "3\n1 5 9\n", "3\n1 5 9\n" ] ]
p03842
AtCoder Grand Contest 008 - Next or Nextnext
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given an integer sequence <var>a</var> of length <var>N</var>. How many permutations <var>p</var> of the integers <var>1</var> through <var>N</var> satisfy the following condition?</p> <ul> <li>For each <var>1 ≤ i ≤ N</var>, at least one of the following holds: <var>p_i = a_i</var> and <var>p_{p_i} = a_i</var>.</li> </ul> <p>Find the count modulo <var>10^9 + 7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1 ≤ N ≤ 10^5</var></li> <li><var>a_i</var> is an integer.</li> <li><var>1 ≤ a_i ≤ N</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the permutations <var>p</var> that satisfy the condition, modulo <var>10^9 + 7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 1 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>The following four permutations satisfy the condition:</p> <ul> <li><var>(1, 2, 3)</var></li> <li><var>(1, 3, 2)</var></li> <li><var>(3, 2, 1)</var></li> <li><var>(2, 1, 3)</var></li> </ul> <p>For example, <var>(1, 3, 2)</var> satisfies the condition because <var>p_1 = 1</var>, <var>p_{p_2} = 2</var> and <var>p_{p_3} = 3</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>2 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 </pre> <p>The following one permutation satisfies the condition:</p> <ul> <li><var>(2, 1)</var></li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>3 2 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>2 </pre> <p>The following two permutations satisfy the condition:</p> <ul> <li><var>(2, 3, 1)</var></li> <li><var>(3, 1, 2)</var></li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>3 1 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>0 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 5</h3><pre>13 2 1 4 3 6 7 5 9 10 8 8 9 11 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 5</h3><pre>6 </pre></section> </div> </span>
[ [ "3\n1 2 3\n", "3\n1 2 3\n" ] ]
p03843
AtCoder Grand Contest 008 - Black Radius
<span class="lang-en"> <p>Score : <var>1900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is a tree with <var>N</var> vertices. The vertices are numbered <var>1</var> through <var>N</var>. For each <var>1 ≤ i ≤ N - 1</var>, the <var>i</var>-th edge connects vertices <var>a_i</var> and <var>b_i</var>. The lengths of all the edges are <var>1</var>.</p> <p>Snuke likes some of the vertices. The information on his favorite vertices are given to you as a string <var>s</var> of length <var>N</var>. For each <var>1 ≤ i ≤ N</var>, <var>s_i</var> is <code>1</code> if Snuke likes vertex <var>i</var>, and <code>0</code> if he does not like vertex <var>i</var>.</p> <p>Initially, all the vertices are white. Snuke will perform the following operation exactly once:</p> <ul> <li>Select a vertex <var>v</var> that he likes, and a non-negative integer <var>d</var>. Then, paint all the vertices black whose distances from <var>v</var> are at most <var>d</var>.</li> </ul> <p>Find the number of the possible combinations of colors of the vertices after the operation.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≤ N ≤ 2×10^5</var></li> <li><var>1 ≤ a_i, b_i ≤ N</var></li> <li>The given graph is a tree.</li> <li><var>|s| = N</var></li> <li><var>s</var> consists of <code>0</code> and <code>1</code>.</li> <li><var>s</var> contains at least one occurrence of <code>1</code>.</li> </ul> </section> </div> <div class="part"> <section> <h3>Partial Score</h3><ul> <li>In the test set worth <var>1300</var> points, <var>s</var> consists only of <code>1</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>a_1</var> <var>b_1</var> <var>a_2</var> <var>b_2</var> <var>:</var> <var>a_{N - 1}</var> <var>b_{N - 1}</var> <var>s</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the possible combinations of colors of the vertices after the operation.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 1 2 1 3 1 4 1100 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>The following four combinations of colors of the vertices are possible:</p> <div style="text-align: center;"> <img alt="334d566ec1f4f38d23cd580044f1cd07.png" src="https://atcoder.jp/img/agc008/334d566ec1f4f38d23cd580044f1cd07.png"> </img></div> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 1 2 1 3 1 4 4 5 11111 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>11 </pre> <p>This case satisfies the additional constraint for the partial score.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>6 1 2 1 3 1 4 2 5 2 6 100011 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>8 </pre></section> </div> </span>
[ [ "4\n1 2\n1 3\n1 4\n1100\n", "4\n1 2\n1 3\n1 4\n1100\n" ] ]
p03844
AtCoder Beginner Contest 050 - Addition and Subtraction Easy
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Joisino wants to evaluate the formula "<var>A</var> <var>op</var> <var>B</var>". Here, <var>A</var> and <var>B</var> are integers, and the binary operator <var>op</var> is either <code>+</code> or <code>-</code>. Your task is to evaluate the formula instead of her.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦A,B≦10^9</var></li> <li><var>op</var> is either <code>+</code> or <code>-</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>A</var> <var>op</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Evaluate the formula and print the result.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>1 + 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>3 </pre> <p>Since <var>1 + 2 = 3</var>, the output should be <var>3</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 - 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>-2 </pre></section> </div> </span>
[ [ "1 + 2\n", "1 + 2\n" ] ]
p03845
AtCoder Beginner Contest 050 - Contest with Drinks Easy
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Joisino is about to compete in the final round of a certain programming competition. In this contest, there are <var>N</var> problems, numbered <var>1</var> through <var>N</var>. Joisino knows that it takes her <var>T_i</var> seconds to solve problem <var>i(1≦i≦N)</var>.</p> <p>Also, there are <var>M</var> kinds of drinks offered to the contestants, numbered <var>1</var> through <var>M</var>. If Joisino takes drink <var>i(1≦i≦M)</var>, her brain will be stimulated and the time it takes for her to solve problem <var>P_i</var> will become <var>X_i</var> seconds. It does not affect the time to solve the other problems.</p> <p>A contestant is allowed to take exactly one of the drinks before the start of the contest. For each drink, Joisino wants to know how many seconds it takes her to solve all the problems if she takes that drink. Here, assume that the time it takes her to solve all the problems is equal to the sum of the time it takes for her to solve individual problems. Your task is to write a program to calculate it instead of her.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>1≦N≦100</var></li> <li><var>1≦T_i≦10^5</var></li> <li><var>1≦M≦100</var></li> <li><var>1≦P_i≦N</var></li> <li><var>1≦X_i≦10^5</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>T_1</var> <var>T_2</var> <var>...</var> <var>T_N</var> <var>M</var> <var>P_1</var> <var>X_1</var> <var>P_2</var> <var>X_2</var> <var>:</var> <var>P_M</var> <var>X_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>For each drink, calculate how many seconds it takes Joisino to solve all the problems if she takes that drink, and print the results, one per line.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 2 1 4 2 1 1 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>6 9 </pre> <p>If Joisino takes drink <var>1</var>, the time it takes her to solve each problem will be <var>1</var>, <var>1</var> and <var>4</var> seconds, respectively, totaling <var>6</var> seconds.</p> <p>If Joisino takes drink <var>2</var>, the time it takes her to solve each problem will be <var>2</var>, <var>3</var> and <var>4</var> seconds, respectively, totaling <var>9</var> seconds.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 7 2 3 8 5 3 4 2 1 7 4 13 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>19 25 30 </pre></section> </div> </span>
[ [ "3\n2 1 4\n2\n1 1\n2 3\n", "3\n2 1 4\n2\n1 1\n2 3\n" ] ]
p03846
AtCoder Beginner Contest 050 - Lining Up
<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, conveniently numbered <var>1</var> through <var>N</var>. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person. According to their reports, the difference above for person <var>i</var> is <var>A_i</var>.</p> <p>Based on these reports, find the number of the possible orders in which they were standing. Since it can be extremely large, print the answer modulo <var>10^9+7</var>. Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print <var>0</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>0≦A_i≦N-1</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the possible orders in which they were standing, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 2 4 4 0 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>There are four possible orders, as follows:</p> <ul> <li><var>2,1,4,5,3</var></li> <li><var>2,5,4,1,3</var></li> <li><var>3,1,4,5,2</var></li> <li><var>3,5,4,1,2</var></li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 6 4 0 2 4 0 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Any order would be inconsistent with the reports, thus the answer is <var>0</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>8 7 5 1 1 7 3 5 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>16 </pre></section> </div> </span>
[ [ "5\n2 4 4 0 2\n", "5\n2 4 4 0 2\n" ] ]
p03847
AtCoder Beginner Contest 050 - Xor Sum
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a positive integer <var>N</var>. Find the number of the pairs of integers <var>u</var> and <var>v</var> <var>(0≦u,v≦N)</var> such that there exist two non-negative integers <var>a</var> and <var>b</var> satisfying <var>a</var> <var>xor</var> <var>b=u</var> and <var>a+b=v</var>. Here, <var>xor</var> denotes the bitwise exclusive OR. Since it can be extremely large, compute the answer modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^{18}</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the possible pairs of integers <var>u</var> and <var>v</var>, 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>5 </pre> <p>The five possible pairs of <var>u</var> and <var>v</var> are:</p> <ul> <li> <p><var>u=0,v=0</var> (Let <var>a=0,b=0</var>, then <var>0</var> <var>xor</var> <var>0=0</var>, <var>0+0=0</var>.)</p> </li> <li> <p><var>u=0,v=2</var> (Let <var>a=1,b=1</var>, then <var>1</var> <var>xor</var> <var>1=0</var>, <var>1+1=2</var>.)</p> </li> <li> <p><var>u=1,v=1</var> (Let <var>a=1,b=0</var>, then <var>1</var> <var>xor</var> <var>0=1</var>, <var>1+0=1</var>.)</p> </li> <li> <p><var>u=2,v=2</var> (Let <var>a=2,b=0</var>, then <var>2</var> <var>xor</var> <var>0=2</var>, <var>2+0=2</var>.)</p> </li> <li> <p><var>u=3,v=3</var> (Let <var>a=3,b=0</var>, then <var>3</var> <var>xor</var> <var>0=3</var>, <var>3+0=3</var>.)</p> </li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1422 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>52277 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1000000000000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>787014179 </pre></section> </div> </span>
[ [ "3\n", "3\n" ] ]
p03848
AtCoder Regular Contest 066 - Lining Up
<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, conveniently numbered <var>1</var> through <var>N</var>. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person. According to their reports, the difference above for person <var>i</var> is <var>A_i</var>.</p> <p>Based on these reports, find the number of the possible orders in which they were standing. Since it can be extremely large, print the answer modulo <var>10^9+7</var>. Note that the reports may be incorrect and thus there may be no consistent order. In such a case, print <var>0</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>0≦A_i≦N-1</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the possible orders in which they were standing, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 2 4 4 0 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>4 </pre> <p>There are four possible orders, as follows:</p> <ul> <li><var>2,1,4,5,3</var></li> <li><var>2,5,4,1,3</var></li> <li><var>3,1,4,5,2</var></li> <li><var>3,5,4,1,2</var></li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>7 6 4 0 2 4 0 2 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>0 </pre> <p>Any order would be inconsistent with the reports, thus the answer is <var>0</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>8 7 5 1 1 7 3 5 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>16 </pre></section> </div> </span>
[ [ "5\n2 4 4 0 2\n", "5\n2 4 4 0 2\n" ] ]
p03849
AtCoder Regular Contest 066 - Xor Sum
<span class="lang-en"> <p>Score : <var>600</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a positive integer <var>N</var>. Find the number of the pairs of integers <var>u</var> and <var>v</var> <var>(0≦u,v≦N)</var> such that there exist two non-negative integers <var>a</var> and <var>b</var> satisfying <var>a</var> <var>xor</var> <var>b=u</var> and <var>a+b=v</var>. Here, <var>xor</var> denotes the bitwise exclusive OR. Since it can be extremely large, compute the answer modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^{18}</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of the possible pairs of integers <var>u</var> and <var>v</var>, 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>5 </pre> <p>The five possible pairs of <var>u</var> and <var>v</var> are:</p> <ul> <li> <p><var>u=0,v=0</var> (Let <var>a=0,b=0</var>, then <var>0</var> <var>xor</var> <var>0=0</var>, <var>0+0=0</var>.)</p> </li> <li> <p><var>u=0,v=2</var> (Let <var>a=1,b=1</var>, then <var>1</var> <var>xor</var> <var>1=0</var>, <var>1+1=2</var>.)</p> </li> <li> <p><var>u=1,v=1</var> (Let <var>a=1,b=0</var>, then <var>1</var> <var>xor</var> <var>0=1</var>, <var>1+0=1</var>.)</p> </li> <li> <p><var>u=2,v=2</var> (Let <var>a=2,b=0</var>, then <var>2</var> <var>xor</var> <var>0=2</var>, <var>2+0=2</var>.)</p> </li> <li> <p><var>u=3,v=3</var> (Let <var>a=3,b=0</var>, then <var>3</var> <var>xor</var> <var>0=3</var>, <var>3+0=3</var>.)</p> </li> </ul> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1422 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>52277 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>1000000000000000000 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>787014179 </pre></section> </div> </span>
[ [ "3\n", "3\n" ] ]
p03850
AtCoder Regular Contest 066 - Addition and Subtraction Hard
<span class="lang-en"> <p>Score : <var>900</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Joisino has a formula consisting of <var>N</var> terms: <var>A_1</var> <var>op_1</var> <var>A_2</var> <var>...</var> <var>op_{N-1}</var> <var>A_N</var>. Here, <var>A_i</var> is an integer, and <var>op_i</var> is an binary operator either <code>+</code> or <code>-</code>. Because Joisino loves large numbers, she wants to maximize the evaluated value of the formula by inserting an arbitrary number of pairs of parentheses (possibly zero) into the formula. Opening parentheses can only be inserted immediately before an integer, and closing parentheses can only be inserted immediately after an integer. It is allowed to insert any number of parentheses at a position. Your task is to write a program to find the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦N≦10^5</var></li> <li><var>1≦A_i≦10^9</var></li> <li><var>op_i</var> is either <code>+</code> or <code>-</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>A_1</var> <var>op_1</var> <var>A_2</var> <var>...</var> <var>op_{N-1}</var> <var>A_N</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the maximum possible evaluated value of the formula after inserting an arbitrary number of pairs of parentheses.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>3 5 - 1 - 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>7 </pre> <p>The maximum possible value is: <var>5 - (1 - 3) = 7</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>5 1 - 2 + 3 - 4 + 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>5 </pre> <p>The maximum possible value is: <var>1 - (2 + 3 - 4) + 5 = 5</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>5 1 - 20 - 13 + 14 - 5 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>13 </pre> <p>The maximum possible value is: <var>1 - (20 - (13 + 14) - 5) = 13</var>.</p></section> </div> </span>
[ [ "3\n5 - 1 - 3\n", "3\n5 - 1 - 3\n" ] ]
p03851
AtCoder Regular Contest 066 - Contest with Drinks Hard
<span class="lang-en"> <p>Score : <var>1400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Joisino is about to compete in the final round of a certain programming competition. In this contest, there are <var>N</var> problems, numbered <var>1</var> through <var>N</var>. Joisino knows that it takes her <var>T_i</var> seconds to solve problem <var>i(1≦i≦N)</var>.</p> <p>In this contest, a contestant will first select some number of problems to solve. Then, the contestant will solve the selected problems. After that, the score of the contestant will be calculated as follows:</p> <ul> <li>(The score) <var>=</var> (The number of the pairs of integers <var>L</var> and <var>R</var> <var>(1≦L≦R≦N)</var> such that for every <var>i</var> satisfying <var>L≦i≦R</var>, problem <var>i</var> is solved) - (The total number of seconds it takes for the contestant to solve the selected problems)</li> </ul> <p>Note that a contestant is allowed to choose to solve zero problems, in which case the score will be <var>0</var>.</p> <p>Also, there are <var>M</var> kinds of drinks offered to the contestants, numbered <var>1</var> through <var>M</var>. If Joisino takes drink <var>i(1≦i≦M)</var>, her brain will be stimulated and the time it takes for her to solve problem <var>P_i</var> will become <var>X_i</var> seconds. Here, <var>X_i</var> may be greater than the length of time originally required to solve problem <var>P_i</var>. Taking drink <var>i</var> does not affect the time required to solve the other problems.</p> <p>A contestant is allowed to take exactly one of the drinks before the start of the contest. For each drink, Joisino wants to know the maximum score that can be obtained in the contest if she takes that drink. Your task is to write a program to calculate it instead of her.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li>All input values are integers.</li> <li><var>1≦N≦3*10^5</var></li> <li><var>1≦T_i≦10^9</var></li> <li>(The sum of <var>T_i</var>) <var>≦10^{12}</var></li> <li><var>1≦M≦3*10^5</var></li> <li><var>1≦P_i≦N</var></li> <li><var>1≦X_i≦10^9</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>T_1</var> <var>T_2</var> <var>...</var> <var>T_N</var> <var>M</var> <var>P_1</var> <var>X_1</var> <var>P_2</var> <var>X_2</var> <var>:</var> <var>P_M</var> <var>X_M</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>For each drink, print the maximum score that can be obtained if Joisino takes that drink, in order, one per line.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>5 1 1 4 1 1 2 3 2 3 10 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>9 2 </pre> <p>If she takes drink <var>1</var>, the maximum score can be obtained by solving all the problems.</p> <p>If she takes drink <var>2</var>, the maximum score can be obtained by solving the problems <var>1,2,4</var> and <var>5</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>12 1 2 1 3 4 1 2 1 12 3 12 12 10 9 3 11 1 5 35 6 15 12 1 1 9 4 3 10 2 5 1 7 6 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>34 35 5 11 35 17 25 26 28 21 </pre></section> </div> </span>
[ [ "5\n1 1 4 1 1\n2\n3 2\n3 10\n", "5\n1 1 4 1 1\n2\n3 2\n3 10\n" ] ]
p03852
AtCoder Beginner Contest 049 - UOIAUAI
<span class="lang-en"> <p>Score : <var>100</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>Given a lowercase English letter <var>c</var>, determine whether it is a vowel. Here, there are five vowels in the English alphabet: <code>a</code>, <code>e</code>, <code>i</code>, <code>o</code> and <code>u</code>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>c</var> is a lowercase English letter.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>c</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If <var>c</var> is a vowel, print <code>vowel</code>. Otherwise, print <code>consonant</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>a </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>vowel </pre> <p>Since <code>a</code> is a vowel, print <code>vowel</code>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>z </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>consonant </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>s </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>consonant </pre></section> </div> </span>
[ [ "a\n", "a\n" ] ]
p03853
AtCoder Beginner Contest 049 - Thin
<span class="lang-en"> <p>Score : <var>200</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>There is an image with a height of <var>H</var> pixels and a width of <var>W</var> pixels. Each of the pixels is represented by either <code>.</code> or <code>*</code>. The character representing the pixel at the <var>i</var>-th row from the top and the <var>j</var>-th column from the left, is denoted by <var>C_{i,j}</var>.</p> <p>Extend this image vertically so that its height is doubled. That is, print a image with a height of <var>2H</var> pixels and a width of <var>W</var> pixels where the pixel at the <var>i</var>-th row and <var>j</var>-th column is equal to <var>C_{(i+1)/2,j}</var> (the result of division is rounded down).</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦H, W≦100</var></li> <li><var>C_{i,j}</var> is either <code>.</code> or <code>*</code>.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>C_{1,1}...C_{1,W}</var> : <var>C_{H,1}...C_{H,W}</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the extended image.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 2 *. .* </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>*. *. .* .* </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>1 4 ***. </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>***. ***. </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>9 20 .....***....***..... ....*...*..*...*.... ...*.....**.....*... ...*.....*......*... ....*.....*....*.... .....**..*...**..... .......*..*.*....... ........**.*........ .........**......... </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>.....***....***..... .....***....***..... ....*...*..*...*.... ....*...*..*...*.... ...*.....**.....*... ...*.....**.....*... ...*.....*......*... ...*.....*......*... ....*.....*....*.... ....*.....*....*.... .....**..*...**..... .....**..*...**..... .......*..*.*....... .......*..*.*....... ........**.*........ ........**.*........ .........**......... .........**......... </pre></section> </div> </span>
[ [ "2 2\n*.\n.*\n", "2 2\n*.\n.*\n" ] ]
p03854
AtCoder Beginner Contest 049 - Daydream
<span class="lang-en"> <p>Score : <var>300</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>You are given a string <var>S</var> consisting of lowercase English letters. Another string <var>T</var> is initially empty. Determine whether it is possible to obtain <var>S = T</var> by performing the following operation an arbitrary number of times:</p> <ul> <li>Append one of the following at the end of <var>T</var>: <code>dream</code>, <code>dreamer</code>, <code>erase</code> and <code>eraser</code>.</li> </ul> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>1≦|S|≦10^5</var></li> <li><var>S</var> consists of lowercase English letters.</li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>S</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>If it is possible to obtain <var>S = T</var>, print <code>YES</code>. Otherwise, print <code>NO</code>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>erasedream </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>YES </pre> <p>Append <code>erase</code> and <code>dream</code> at the end of <var>T</var> in this order, to obtain <var>S = T</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>dreameraser </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>YES </pre> <p>Append <code>dream</code> and <code>eraser</code> at the end of <var>T</var> in this order, to obtain <var>S = T</var>.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>dreamerer </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>NO </pre></section> </div> </span>
[ [ "erasedream\n", "erasedream\n" ] ]
p03855
AtCoder Beginner Contest 049 - Connectivity
<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> cities. There are also <var>K</var> roads and <var>L</var> railways, extending between the cities. The <var>i</var>-th road bidirectionally connects the <var>p_i</var>-th and <var>q_i</var>-th cities, and the <var>i</var>-th railway bidirectionally connects the <var>r_i</var>-th and <var>s_i</var>-th cities. No two roads connect the same pair of cities. Similarly, no two railways connect the same pair of cities.</p> <p>We will say city <var>A</var> and <var>B</var> are <em>connected by roads</em> if city <var>B</var> is reachable from city <var>A</var> by traversing some number of roads. Here, any city is considered to be connected to itself by roads. We will also define <em>connectivity by railways</em> similarly.</p> <p>For each city, find the number of the cities connected to that city by both roads and railways.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var>2 ≦ N ≦ 2*10^5</var></li> <li><var>1 ≦ K, L≦ 10^5</var></li> <li><var>1 ≦ p_i, q_i, r_i, s_i ≦ N</var></li> <li><var>p_i &lt; q_i</var></li> <li><var>r_i &lt; s_i</var></li> <li>When <var>i ≠ j</var>, <var>(p_i, q_i) ≠ (p_j, q_j)</var></li> <li>When <var>i ≠ j</var>, <var>(r_i, s_i) ≠ (r_j, s_j)</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>N</var> <var>K</var> <var>L</var> <var>p_1</var> <var>q_1</var> : <var>p_K</var> <var>q_K</var> <var>r_1</var> <var>s_1</var> : <var>r_L</var> <var>s_L</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print <var>N</var> integers. The <var>i</var>-th of them should represent the number of the cities connected to the <var>i</var>-th city by both roads and railways.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>4 3 1 1 2 2 3 3 4 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>1 2 2 1 </pre> <p>All the four cities are connected to each other by roads.</p> <p>By railways, only the second and third cities are connected. Thus, the answers for the cities are <var>1, 2, 2</var> and <var>1</var>, respectively.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>4 2 2 1 2 2 3 1 4 2 3 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>1 2 2 1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>7 4 4 1 2 2 3 2 5 6 7 3 5 4 5 3 4 6 7 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 1 2 1 2 2 2 </pre></section> </div> </span>
[ [ "4 3 1\n1 2\n2 3\n3 4\n2 3\n", "4 3 1\n1 2\n2 3\n3 4\n2 3\n" ] ]