id
stringlengths 6
6
| slug_name
stringlengths 1
105
| pretty_content
stringlengths 1
27.2k
| test_cases
sequencelengths 1
1
|
---|---|---|---|
p03256 | AtCoder Grand Contest 027 - ABland Yard | <span class="lang-en">
<p>Score : <var>900</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given an undirected graph consisting of <var>N</var> vertices and <var>M</var> edges.
The vertices are numbered <var>1</var> to <var>N</var>, and the edges are numbered <var>1</var> to <var>M</var>.
In addition, each vertex has a label, <code>A</code> or <code>B</code>. The label of Vertex <var>i</var> is <var>s_i</var>.
Edge <var>i</var> bidirectionally connects vertex <var>a_i</var> and <var>b_i</var>.</p>
<p>The phantom thief Nusook likes to choose some vertex as the startpoint and traverse an edge zero or more times.
Today, he will make a string after traveling as above, by placing the labels of the visited vertices in the order visited, beginning from the startpoint.</p>
<p>For example, in a graph where Vertex <var>1</var> has the label <code>A</code> and Vertex <var>2</var> has the label <code>B</code>, if Nusook travels along the path <var>1 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 2</var>, the resulting string is <code>ABABB</code>.</p>
<p>Determine if Nusook can make all strings consisting of <code>A</code> and <code>B</code>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq N \leq 2 \times 10^{5}</var></li>
<li><var>1 \leq M \leq 2 \times 10^{5}</var></li>
<li><var>|s| = N</var></li>
<li><var>s_i</var> is <code>A</code> or <code>B</code>.</li>
<li><var>1 \leq a_i, b_i \leq N</var></li>
<li>The given graph may NOT be simple or connected.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>M</var>
<var>s</var>
<var>a_1</var> <var>b_1</var>
<var>:</var>
<var>a_{M}</var> <var>b_{M}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If Nusook can make all strings consisting of <code>A</code> and <code>B</code>, print <code>Yes</code>; otherwise, print <code>No</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 3
AB
1 1
1 2
2 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<ul>
<li>Since Nusook can visit Vertex <var>1</var> and Vertex <var>2</var> in any way he likes, he can make all strings consisting of <code>A</code> and <code>B</code>.</li>
</ul>
<div style="text-align: center;">
<img alt="77e96cf8e213d606ddd8f3c3f8315d32.png" src="https://img.atcoder.jp/agc027/77e96cf8e213d606ddd8f3c3f8315d32.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4 3
ABAB
1 2
2 3
3 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>No
</pre>
<ul>
<li>For example, Nusook cannot make <code>BB</code>.</li>
<li>The given graph may not be connected.</li>
</ul>
<div style="text-align: center;">
<img alt="1ab1411cb9d6ee023d14ca4e77c4b584.png" src="https://img.atcoder.jp/agc027/1ab1411cb9d6ee023d14ca4e77c4b584.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>13 23
ABAAAABBBBAAB
7 1
10 6
1 11
2 10
2 8
2 11
11 12
8 3
7 12
11 2
13 13
11 9
4 1
9 7
9 6
8 13
8 6
4 10
8 7
4 3
2 1
8 12
6 9
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>Yes
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>13 17
BBABBBAABABBA
7 1
7 9
11 12
3 9
11 9
2 1
11 5
12 11
10 8
1 11
1 8
7 7
9 10
8 8
8 12
6 2
13 11
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>No
</pre></section>
</div>
</span> | [
[
"2 3\nAB\n1 1\n1 2\n2 2\n",
"2 3\nAB\n1 1\n1 2\n2 2\n"
]
] |
p03257 | AtCoder Grand Contest 027 - Modulo Matrix | <span class="lang-en">
<p>Score : <var>1100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given an integer <var>N</var>.</p>
<p>Construct any one <var>N</var>-by-<var>N</var> matrix <var>a</var> that satisfies the conditions below. It can be proved that a solution always exists under the constraints of this problem.</p>
<ul>
<li><var>1 \leq a_{i,j} \leq 10^{15}</var></li>
<li><var>a_{i,j}</var> are pairwise distinct integers.</li>
<li>There exists a positive integer <var>m</var> such that the following holds: Let <var>x</var> and <var>y</var> be two elements of the matrix that are vertically or horizontally adjacent. Then, <var>{\rm max}(x,y)</var> <var>{\rm mod}</var> <var>{\rm min}(x,y)</var> is always <var>m</var>.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq N \leq 500</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print your solution in the following format:</p>
<pre><var>a_{1,1}</var> <var>...</var> <var>a_{1,N}</var>
<var>:</var>
<var>a_{N,1}</var> <var>...</var> <var>a_{N,N}</var>
</pre>
</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 7
23 10
</pre>
<ul>
<li>For any two elements <var>x</var> and <var>y</var> that are vertically or horizontally adjacent, <var>{\rm max}(x,y)</var> <var>{\rm mod}</var> <var>{\rm min}(x,y)</var> is always <var>3</var>.</li>
</ul></section>
</div>
</span> | [
[
"2\n",
"2\n"
]
] |
p03258 | AtCoder Grand Contest 027 - ABBreviate | <span class="lang-en">
<p>Score : <var>1300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There is a string <var>s</var> consisting of <code>a</code> and <code>b</code>.
Snuke can perform the following two kinds of operation any number of times in any order:</p>
<ul>
<li>Choose an occurrence of <code>aa</code> as a substring, and replace it with <code>b</code>.</li>
<li>Choose an occurrence of <code>bb</code> as a substring, and replace it with <code>a</code>.</li>
</ul>
<p>How many strings <var>s</var> can be obtained by this sequence of operations?
Find the count modulo <var>10^9 + 7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq |s| \leq 10^5</var></li>
<li><var>s</var> consists of <code>a</code> and <code>b</code>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>s</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of strings <var>s</var> that can be obtained, modulo <var>10^9 + 7</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>aaaa
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>6
</pre>
<p>Six strings can be obtained:</p>
<ul>
<li><code>aaaa</code></li>
<li><code>aab</code></li>
<li><code>aba</code></li>
<li><code>baa</code></li>
<li><code>bb</code></li>
<li><code>a</code></li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>aabb
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>5
</pre>
<p>Five strings can be obtained:</p>
<ul>
<li><code>aabb</code></li>
<li><code>aaa</code></li>
<li><code>bbb</code></li>
<li><code>ab</code></li>
<li><code>ba</code></li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>ababababa
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>1
</pre>
<p>Snuke cannot perform any operation.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>babbabaaba
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>35
</pre></section>
</div>
</span> | [
[
"aaaa\n",
"aaaa\n"
]
] |
p03259 | AtCoder Grand Contest 027 - Grafting | <span class="lang-en">
<p>Score : <var>1900</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Snuke has found two trees <var>A</var> and <var>B</var>, each with <var>N</var> vertices numbered <var>1</var> to <var>N</var>.
The <var>i</var>-th edge of <var>A</var> connects Vertex <var>a_i</var> and <var>b_i</var>, and the <var>j</var>-th edge of <var>B</var> connects Vertex <var>c_j</var> and <var>d_j</var>.
Also, all vertices of <var>A</var> are initially painted white.</p>
<p>Snuke would like to perform the following operation on <var>A</var> zero or more times so that <var>A</var> coincides with <var>B</var>:</p>
<ul>
<li>Choose a leaf vertex that is painted white. (Let this vertex be <var>v</var>.)</li>
<li>Remove the edge incident to <var>v</var>, and add a new edge that connects <var>v</var> to another vertex.</li>
<li>Paint <var>v</var> black.</li>
</ul>
<p>Determine if <var>A</var> can be made to coincide with <var>B</var>, ignoring color. If the answer is yes, find the minimum number of operations required.</p>
<p>You are given <var>T</var> cases of this kind. Find the answer for each of them.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq T \leq 20</var></li>
<li><var>3 \leq N \leq 50</var></li>
<li><var>1 \leq a_i,b_i,c_i,d_i \leq N</var></li>
<li>All given graphs are trees.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>T</var>
<var>case_1</var>
<var>:</var>
<var>case_{T}</var>
</pre>
<p>Each case is given in the following format:</p>
<pre><var>N</var>
<var>a_1</var> <var>b_1</var>
<var>:</var>
<var>a_{N-1}</var> <var>b_{N-1}</var>
<var>c_1</var> <var>d_1</var>
<var>:</var>
<var>c_{N-1}</var> <var>d_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>For each case, if <var>A</var> can be made to coincide with <var>B</var> ignoring color, print the minimum number of operations required, and print <code>-1</code> if it cannot.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2
3
1 2
2 3
1 3
3 2
6
1 2
2 3
3 4
4 5
5 6
1 2
2 4
4 3
3 5
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
-1
</pre>
<ul>
<li>The graph in each case is shown below.</li>
<li>In case <var>1</var>, <var>A</var> can be made to coincide with <var>B</var> by choosing Vertex <var>1</var>, removing the edge connecting <var>1</var> and <var>2</var>, and adding an edge connecting <var>1</var> and <var>3</var>. Note that Vertex <var>2</var> is not a leaf vertex and thus cannot be chosen.</li>
</ul>
<div style="text-align: center;">
<img alt="3f020b4a4e883680357cc55adb571fbc.png" src="https://img.atcoder.jp/agc027/3f020b4a4e883680357cc55adb571fbc.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
8
2 7
4 8
8 6
7 1
7 3
5 7
7 8
4 2
5 2
1 2
8 1
3 2
2 6
2 7
4
1 2
2 3
3 4
3 4
2 1
3 2
9
5 3
4 3
9 3
6 8
2 3
1 3
3 8
1 7
4 1
2 8
9 6
3 6
3 5
1 8
9 7
1 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>6
0
7
</pre>
<ul>
<li><var>A</var> may coincide with <var>B</var> from the beginning.</li>
</ul></section>
</div>
</span> | [
[
"2\n3\n1 2\n2 3\n1 3\n3 2\n6\n1 2\n2 3\n3 4\n4 5\n5 6\n1 2\n2 4\n4 3\n3 5\n5 6\n",
"2\n3\n1 2\n2 3\n1 3\n3 2\n6\n1 2\n2 3\n3 4\n4 5\n5 6\n1 2\n2 4\n4 3\n3 5\n5 6\n"
]
] |
p03260 | AtCoder Beginner Contest 109 - ABC333 | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given integers <var>A</var> and <var>B</var>, each between <var>1</var> and <var>3</var> (inclusive).</p>
<p>Determine if there is an integer <var>C</var> between <var>1</var> and <var>3</var> (inclusive) such that <var>A \times B \times C</var> is an odd number.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>All values in input are integers.</li>
<li><var>1 \leq A, B \leq 3</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>A</var> <var>B</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If there is an integer <var>C</var> between <var>1</var> and <var>3</var> that satisfies the condition, 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
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<p>Let <var>C = 3</var>. Then, <var>A \times B \times C = 3 \times 1 \times 3 = 9</var>, which is an odd number.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>1 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>No
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>2 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>No
</pre></section>
</div>
</span> | [
[
"3 1\n",
"3 1\n"
]
] |
p03261 | AtCoder Beginner Contest 109 - Shiritori | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Takahashi is practicing <em>shiritori</em> alone again today.</p>
<p>Shiritori is a game as follows:</p>
<ul>
<li>In the first turn, a player announces any one word.</li>
<li>In the subsequent turns, a player announces a word that satisfies the following conditions:<ul>
<li>That word is not announced before.</li>
<li>The first character of that word is the same as the last character of the last word announced.</li>
</ul>
</li>
</ul>
<p>In this game, he is practicing to announce as many words as possible in ten seconds.</p>
<p>You are given the number of words Takahashi announced, <var>N</var>, and the <var>i</var>-th word he announced, <var>W_i</var>, for each <var>i</var>. Determine if the rules of shiritori was observed, that is, every word announced by him satisfied the conditions.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>N</var> is an integer satisfying <var>2 \leq N \leq 100</var>.</li>
<li><var>W_i</var> is a string of length between <var>1</var> and <var>10</var> (inclusive) consisting of lowercase English letters.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>W_1</var>
<var>W_2</var>
<var>:</var>
<var>W_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If every word announced by Takahashi satisfied the conditions, print <code>Yes</code>; otherwise, print <code>No</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4
hoge
english
hoge
enigma
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>No
</pre>
<p>As <code>hoge</code> is announced multiple times, the rules of shiritori was not observed.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>9
basic
c
cpp
php
python
nadesico
ocaml
lua
assembly
</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>8
a
aa
aaa
aaaa
aaaaa
aaaaaa
aaa
aaaaaaa
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>No
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>3
abc
arc
agc
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>No
</pre></section>
</div>
</span> | [
[
"4\nhoge\nenglish\nhoge\nenigma\n",
"4\nhoge\nenglish\nhoge\nenigma\n"
]
] |
p03262 | AtCoder Beginner Contest 109 - Skip | <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> cities on a number line. The <var>i</var>-th city is located at coordinate <var>x_i</var>.</p>
<p>Your objective is to visit all these cities at least once.</p>
<p>In order to do so, you will first set a positive integer <var>D</var>.</p>
<p>Then, you will depart from coordinate <var>X</var> and perform Move <var>1</var> and Move <var>2</var> below, as many times as you like:</p>
<ul>
<li>Move <var>1</var>: travel from coordinate <var>y</var> to coordinate <var>y + D</var>.</li>
<li>Move <var>2</var>: travel from coordinate <var>y</var> to coordinate <var>y - D</var>.</li>
</ul>
<p>Find the maximum value of <var>D</var> that enables you to visit all the cities.</p>
<p>Here, to visit a city is to travel to the coordinate where that city is located.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>All values in input are integers.</li>
<li><var>1 \leq N \leq 10^5</var></li>
<li><var>1 \leq X \leq 10^9</var></li>
<li><var>1 \leq x_i \leq 10^9</var></li>
<li><var>x_i</var> are all different.</li>
<li><var>x_1, x_2, ..., x_N \neq X</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>X</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 maximum value of <var>D</var> that enables you to visit all the cities.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 3
1 7 11
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>Setting <var>D = 2</var> enables you to visit all the cities as follows, and this is the maximum value of such <var>D</var>.</p>
<ul>
<li>Perform Move <var>2</var> to travel to coordinate <var>1</var>.</li>
<li>Perform Move <var>1</var> to travel to coordinate <var>3</var>.</li>
<li>Perform Move <var>1</var> to travel to coordinate <var>5</var>.</li>
<li>Perform Move <var>1</var> to travel to coordinate <var>7</var>.</li>
<li>Perform Move <var>1</var> to travel to coordinate <var>9</var>.</li>
<li>Perform Move <var>1</var> to travel to coordinate <var>11</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 81
33 105 57
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>24
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1 1
1000000000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>999999999
</pre></section>
</div>
</span> | [
[
"3 3\n1 7 11\n",
"3 3\n1 7 11\n"
]
] |
p03263 | AtCoder Beginner Contest 109 - Make Them Even | <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There is a grid of square cells with <var>H</var> horizontal rows and <var>W</var> vertical columns. The cell at the <var>i</var>-th row and the <var>j</var>-th column will be denoted as Cell <var>(i, j)</var>.</p>
<p>In Cell <var>(i, j)</var>, <var>a_{ij}</var> coins are placed.</p>
<p>You can perform the following operation any number of times:</p>
<p>Operation: Choose a cell that was not chosen before and contains one or more coins, then move one of those coins to a vertically or horizontally adjacent cell.</p>
<p>Maximize the number of cells containing an even number of coins.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>All values in input are integers.</li>
<li><var>1 \leq H, W \leq 500</var></li>
<li><var>0 \leq a_{ij} \leq 9</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>H</var> <var>W</var>
<var>a_{11}</var> <var>a_{12}</var> <var>...</var> <var>a_{1W}</var>
<var>a_{21}</var> <var>a_{22}</var> <var>...</var> <var>a_{2W}</var>
<var>:</var>
<var>a_{H1}</var> <var>a_{H2}</var> <var>...</var> <var>a_{HW}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print a sequence of operations that maximizes the number of cells containing an even number of coins, in the following format:</p>
<pre><var>N</var>
<var>y_1</var> <var>x_1</var> <var>y_1'</var> <var>x_1'</var>
<var>y_2</var> <var>x_2</var> <var>y_2'</var> <var>x_2'</var>
<var>:</var>
<var>y_N</var> <var>x_N</var> <var>y_N'</var> <var>x_N'</var>
</pre>
<p>That is, in the first line, print an integer <var>N</var> between <var>0</var> and <var>H \times W</var> (inclusive), representing the number of operations.</p>
<p>In the <var>(i+1)</var>-th line (<var>1 \leq i \leq N</var>), print four integers <var>y_i, x_i, y_i'</var> and <var>x_i'</var> (<var>1 \leq y_i, y_i' \leq H</var> and <var>1 \leq x_i, x_i' \leq W</var>), representing the <var>i</var>-th operation. These four integers represents the operation of moving one of the coins placed in Cell <var>(y_i, x_i)</var> to a vertically or horizontally adjacent cell, <var>(y_i', x_i')</var>.</p>
<p>Note that if the specified operation violates the specification in the problem statement or the output format is invalid, it will result in <em>Wrong Answer</em>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 3
1 2 3
0 1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
2 2 2 3
1 1 1 2
1 3 1 2
</pre>
<p>Every cell contains an even number of coins after the following sequence of operations:</p>
<ul>
<li>Move the coin in Cell <var>(2, 2)</var> to Cell <var>(2, 3)</var>.</li>
<li>Move the coin in Cell <var>(1, 1)</var> to Cell <var>(1, 2)</var>.</li>
<li>Move one of the coins in Cell <var>(1, 3)</var> to Cell <var>(1, 2)</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 2
1 0
2 1
1 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>3
1 1 1 2
1 2 2 2
3 1 3 2
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1 5
9 9 9 9 9
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>2
1 1 1 2
1 3 1 4
</pre></section>
</div>
</span> | [
[
"2 3\n1 2 3\n0 1 1\n",
"2 3\n1 2 3\n0 1 1\n"
]
] |
p03264 | AtCoder Beginner Contest 108 - Pair | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Find the number of ways to choose a pair of an even number and an odd number from the positive integers between <var>1</var> and <var>K</var> (inclusive). The order does not matter.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2\leq K\leq 100</var></li>
<li><var>K</var> is an integer.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>K</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of ways to choose a pair of an even number and an odd number from the positive integers between <var>1</var> and <var>K</var> (inclusive).</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>Two pairs can be chosen: <var>(2,1)</var> and <var>(2,3)</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>9
</pre>
</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>30
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>50
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>625
</pre></section>
</div>
</span> | [
[
"3\n",
"3\n"
]
] |
p03265 | AtCoder Beginner Contest 108 - Ruined Square | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There is a square in the <var>xy</var>-plane. The coordinates of its four vertices are <var>(x_1,y_1),(x_2,y_2),(x_3,y_3)</var> and <var>(x_4,y_4)</var> in counter-clockwise order.
(Assume that the positive <var>x</var>-axis points right, and the positive <var>y</var>-axis points up.)</p>
<p>Takahashi remembers <var>(x_1,y_1)</var> and <var>(x_2,y_2)</var>, but he has forgot <var>(x_3,y_3)</var> and <var>(x_4,y_4)</var>.</p>
<p>Given <var>x_1,x_2,y_1,y_2</var>, restore <var>x_3,y_3,x_4,y_4</var>. It can be shown that <var>x_3,y_3,x_4</var> and <var>y_4</var> uniquely exist and have integer values.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>|x_1|,|y_1|,|x_2|,|y_2| \leq 100</var></li>
<li><var>(x_1,y_1)</var> β <var>(x_2,y_2)</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>x_1</var> <var>y_1</var> <var>x_2</var> <var>y_2</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <var>x_3,y_3,x_4</var> and <var>y_4</var> as integers, in this order.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>0 0 0 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>-1 1 -1 0
</pre>
<p><var>(0,0),(0,1),(-1,1),(-1,0)</var> is the four vertices of a square in counter-clockwise order.
Note that <var>(x_3,y_3)=(1,1),(x_4,y_4)=(1,0)</var> is not accepted, as the vertices are in clockwise order.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2 3 6 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>3 10 -1 7
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>31 -41 -59 26
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>-126 -64 -36 -131
</pre></section>
</div>
</span> | [
[
"0 0 0 1\n",
"0 0 0 1\n"
]
] |
p03266 | AtCoder Beginner Contest 108 - Triangular Relationship | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given integers <var>N</var> and <var>K</var>. Find the number of triples <var>(a,b,c)</var> of positive integers not greater than <var>N</var> such that <var>a+b,b+c</var> and <var>c+a</var> are all multiples of <var>K</var>.
The order of <var>a,b,c</var> does matter, and some of them can be the same.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N,K \leq 2\times 10^5</var></li>
<li><var>N</var> and <var>K</var> are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of triples <var>(a,b,c)</var> of positive integers not greater than <var>N</var> such that <var>a+b,b+c</var> and <var>c+a</var> are all multiples of <var>K</var>.</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>9
</pre>
<p><var>(1,1,1),(1,1,3),(1,3,1),(1,3,3),(2,2,2),(3,1,1),(3,1,3),(3,3,1)</var> and <var>(3,3,3)</var> satisfy the condition.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>31415 9265
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>27
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>35897 932
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>114191
</pre></section>
</div>
</span> | [
[
"3 2\n",
"3 2\n"
]
] |
p03267 | AtCoder Beginner Contest 108 - All Your Paths are Different Lengths | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given an integer <var>L</var>. Construct a directed graph that satisfies the conditions below. The graph may contain multiple edges between the same pair of vertices. It can be proved that such a graph always exists.</p>
<ul>
<li>The number of vertices, <var>N</var>, is at most <var>20</var>. The vertices are given ID numbers from <var>1</var> to <var>N</var>.</li>
<li>The number of edges, <var>M</var>, is at most <var>60</var>. Each edge has an integer length between <var>0</var> and <var>10^6</var> (inclusive).</li>
<li>Every edge is directed from the vertex with the smaller ID to the vertex with the larger ID. That is, <var>1,2,...,N</var> is one possible topological order of the vertices.</li>
<li>There are exactly <var>L</var> different paths from Vertex <var>1</var> to Vertex <var>N</var>. The lengths of these paths are all different, and they are integers between <var>0</var> and <var>L-1</var>.</li>
</ul>
<p>Here, the length of a path is the sum of the lengths of the edges contained in that path, and two paths are considered different when the sets of the edges contained in those paths are different.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq L \leq 10^6</var></li>
<li><var>L</var> is an integer.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>L</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>In the first line, print <var>N</var> and <var>M</var>, the number of the vertices and edges in your graph.
In the <var>i</var>-th of the following <var>M</var> lines, print three integers <var>u_i,v_i</var> and <var>w_i</var>, representing the starting vertex, the ending vertex and the length of the <var>i</var>-th edge.
If there are multiple solutions, any of them will be accepted.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>8 10
1 2 0
2 3 0
3 4 0
1 5 0
2 6 0
3 7 0
4 8 0
5 6 1
6 7 1
7 8 1
</pre>
<p>In the graph represented by the sample output, there are four paths from Vertex <var>1</var> to <var>N=8</var>:</p>
<ul>
<li><var>1</var> β <var>2</var> β <var>3</var> β <var>4</var> β <var>8</var> with length <var>0</var></li>
<li><var>1</var> β <var>2</var> β <var>3</var> β <var>7</var> β <var>8</var> with length <var>1</var></li>
<li><var>1</var> β <var>2</var> β <var>6</var> β <var>7</var> β <var>8</var> with length <var>2</var></li>
<li><var>1</var> β <var>5</var> β <var>6</var> β <var>7</var> β <var>8</var> with length <var>3</var></li>
</ul>
<p>There are other possible solutions.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>5 7
1 2 0
2 3 1
3 4 0
4 5 0
2 4 0
1 3 3
3 5 1
</pre></section>
</div>
</span> | [
[
"4\n",
"4\n"
]
] |
p03268 | AtCoder Regular Contest 102 - Triangular Relationship | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given integers <var>N</var> and <var>K</var>. Find the number of triples <var>(a,b,c)</var> of positive integers not greater than <var>N</var> such that <var>a+b,b+c</var> and <var>c+a</var> are all multiples of <var>K</var>.
The order of <var>a,b,c</var> does matter, and some of them can be the same.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N,K \leq 2\times 10^5</var></li>
<li><var>N</var> and <var>K</var> are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of triples <var>(a,b,c)</var> of positive integers not greater than <var>N</var> such that <var>a+b,b+c</var> and <var>c+a</var> are all multiples of <var>K</var>.</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>9
</pre>
<p><var>(1,1,1),(1,1,3),(1,3,1),(1,3,3),(2,2,2),(3,1,1),(3,1,3),(3,3,1)</var> and <var>(3,3,3)</var> satisfy the condition.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>31415 9265
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>27
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>35897 932
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>114191
</pre></section>
</div>
</span> | [
[
"3 2\n",
"3 2\n"
]
] |
p03269 | AtCoder Regular Contest 102 - All Your Paths are Different Lengths | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given an integer <var>L</var>. Construct a directed graph that satisfies the conditions below. The graph may contain multiple edges between the same pair of vertices. It can be proved that such a graph always exists.</p>
<ul>
<li>The number of vertices, <var>N</var>, is at most <var>20</var>. The vertices are given ID numbers from <var>1</var> to <var>N</var>.</li>
<li>The number of edges, <var>M</var>, is at most <var>60</var>. Each edge has an integer length between <var>0</var> and <var>10^6</var> (inclusive).</li>
<li>Every edge is directed from the vertex with the smaller ID to the vertex with the larger ID. That is, <var>1,2,...,N</var> is one possible topological order of the vertices.</li>
<li>There are exactly <var>L</var> different paths from Vertex <var>1</var> to Vertex <var>N</var>. The lengths of these paths are all different, and they are integers between <var>0</var> and <var>L-1</var>.</li>
</ul>
<p>Here, the length of a path is the sum of the lengths of the edges contained in that path, and two paths are considered different when the sets of the edges contained in those paths are different.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq L \leq 10^6</var></li>
<li><var>L</var> is an integer.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>L</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>In the first line, print <var>N</var> and <var>M</var>, the number of the vertices and edges in your graph.
In the <var>i</var>-th of the following <var>M</var> lines, print three integers <var>u_i,v_i</var> and <var>w_i</var>, representing the starting vertex, the ending vertex and the length of the <var>i</var>-th edge.
If there are multiple solutions, any of them will be accepted.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>8 10
1 2 0
2 3 0
3 4 0
1 5 0
2 6 0
3 7 0
4 8 0
5 6 1
6 7 1
7 8 1
</pre>
<p>In the graph represented by the sample output, there are four paths from Vertex <var>1</var> to <var>N=8</var>:</p>
<ul>
<li><var>1</var> β <var>2</var> β <var>3</var> β <var>4</var> β <var>8</var> with length <var>0</var></li>
<li><var>1</var> β <var>2</var> β <var>3</var> β <var>7</var> β <var>8</var> with length <var>1</var></li>
<li><var>1</var> β <var>2</var> β <var>6</var> β <var>7</var> β <var>8</var> with length <var>2</var></li>
<li><var>1</var> β <var>5</var> β <var>6</var> β <var>7</var> β <var>8</var> with length <var>3</var></li>
</ul>
<p>There are other possible solutions.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>5 7
1 2 0
2 3 1
3 4 0
4 5 0
2 4 0
1 3 3
3 5 1
</pre></section>
</div>
</span> | [
[
"4\n",
"4\n"
]
] |
p03270 | AtCoder Regular Contest 102 - Stop. Otherwise... | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Takahashi throws <var>N</var> dice, each having <var>K</var> sides with all integers from <var>1</var> to <var>K</var>. The dice are NOT pairwise distinguishable.
For each <var>i=2,3,...,2K</var>, find the following value modulo <var>998244353</var>:</p>
<ul>
<li>The number of combinations of <var>N</var> sides shown by the dice such that the sum of no two different sides is <var>i</var>.</li>
</ul>
<p>Note that the dice are NOT distinguishable, that is, two combinations are considered different when there exists an integer <var>k</var> such that the number of dice showing <var>k</var> is different in those two.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq K \leq 2000</var></li>
<li><var>2 \leq N \leq 2000</var></li>
<li><var>K</var> and <var>N</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>K</var> <var>N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <var>2K-1</var> integers. The <var>t</var>-th of them <var>(1\leq t\leq 2K-1)</var> should be the answer for <var>i=t+1</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>7
7
4
7
7
</pre>
<ul>
<li>For <var>i=2</var>, the combinations <var>(1,2,2),(1,2,3),(1,3,3),(2,2,2),(2,2,3),(2,3,3),(3,3,3)</var> satisfy the condition, so the answer is <var>7</var>.</li>
<li>For <var>i=3</var>, the combinations <var>(1,1,1),(1,1,3),(1,3,3),(2,2,2),(2,2,3),(2,3,3),(3,3,3)</var> satisfy the condition, so the answer is <var>7</var>.</li>
<li>For <var>i=4</var>, the combinations <var>(1,1,1),(1,1,2),(2,3,3),(3,3,3)</var> satisfy the condition, so the answer is <var>4</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>36
36
20
20
20
36
36
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>6 1000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>149393349
149393349
668669001
668669001
4000002
4000002
4000002
668669001
668669001
149393349
149393349
</pre></section>
</div>
</span> | [
[
"3 3\n",
"3 3\n"
]
] |
p03271 | AtCoder Regular Contest 102 - Revenge of BBuBBBlesort! | <span class="lang-en">
<p>Score : <var>1200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a permutation of <var>1,2,...,N</var>: <var>p_1,p_2,...,p_N</var>. Determine if the state where <var>p_i=i</var> for every <var>i</var> can be reached by performing the following operation any number of times:</p>
<ul>
<li>Choose three elements <var>p_{i-1},p_{i},p_{i+1}</var> (<var>2\leq i\leq N-1</var>) such that <var>p_{i-1}>p_{i}>p_{i+1}</var> and reverse the order of these three.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>3 \leq N \leq 3 Γ 10^5</var></li>
<li><var>p_1,p_2,...,p_N</var> is a permutation of <var>1,2,...,N</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>p_1</var>
<var>:</var>
<var>p_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If the state where <var>p_i=i</var> for every <var>i</var> can be reached by performing the operation, 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
5
2
1
4
3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<p>The state where <var>p_i=i</var> for every <var>i</var> can be reached as follows:</p>
<ul>
<li>Reverse the order of <var>p_1,p_2,p_3</var>. The sequence <var>p</var> becomes <var>1,2,5,4,3</var>.</li>
<li>Reverse the order of <var>p_3,p_4,p_5</var>. The sequence <var>p</var> becomes <var>1,2,3,4,5</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4
3
2
4
1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>No
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>7
3
2
1
6
5
4
7
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>Yes
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>6
5
3
4
1
2
6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>No
</pre></section>
</div>
</span> | [
[
"5\n5\n2\n1\n4\n3\n",
"5\n5\n2\n1\n4\n3\n"
]
] |
p03272 | AtCoder Beginner Contest 107 - Train | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There is an <var>N</var>-car train.</p>
<p>You are given an integer <var>i</var>. Find the value of <var>j</var> such that the following statement is true: "the <var>i</var>-th car from the front of the train is the <var>j</var>-th car from the back."</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 100</var></li>
<li><var>1 \leq i \leq N</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>i</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 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p>The second car from the front of a <var>4</var>-car train is the third car from the back.</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>1
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>15 11
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>5
</pre></section>
</div>
</span> | [
[
"4 2\n",
"4 2\n"
]
] |
p03273 | AtCoder Beginner Contest 107 - Grid Compression | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There is a grid of squares with <var>H</var> horizontal rows and <var>W</var> vertical columns.
The square at the <var>i</var>-th row from the top and the <var>j</var>-th column from the left is represented as <var>(i, j)</var>.
Each square is black or white.
The color of the square is given as an <var>H</var>-by-<var>W</var> matrix <var>(a_{i, j})</var>.
If <var>a_{i, j}</var> is <code>.</code>, the square <var>(i, j)</var> is white; if <var>a_{i, j}</var> is <code>#</code>, the square <var>(i, j)</var> is black.</p>
<p>Snuke is compressing this grid.
He will do so by repeatedly performing the following operation while there is a row or column that consists only of white squares:</p>
<ul>
<li>Operation: choose any one row or column that consists only of white squares, remove it and delete the space between the rows or columns.</li>
</ul>
<p>It can be shown that the final state of the grid is uniquely determined regardless of what row or column is chosen in each operation.
Find the final state of the grid.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq H, W \leq 100</var></li>
<li><var>a_{i, j}</var> is <code>.</code> or <code>#</code>.</li>
<li>There is at least one black square in the whole grid.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>H</var> <var>W</var>
<var>a_{1, 1}...a_{1, W}</var>
<var>:</var>
<var>a_{H, 1}...a_{H, W}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the final state of the grid in the same format as input (without the numbers of rows and columns); see the samples for clarity.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4 4
##.#
....
##.#
.#.#
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>###
###
.##
</pre>
<p>The second row and the third column in the original grid will be removed.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 3
#..
.#.
..#
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>#..
.#.
..#
</pre>
<p>As there is no row or column that consists only of white squares, no operation will be performed.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>4 5
.....
.....
..#..
.....
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>#
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>7 6
......
....#.
.#....
..#...
..#...
......
.#..#.
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>..#
#..
.#.
.#.
#.#
</pre></section>
</div>
</span> | [
[
"4 4\n##.#\n....\n##.#\n.#.#\n",
"4 4\n##.#\n....\n##.#\n.#.#\n"
]
] |
p03274 | AtCoder Beginner Contest 107 - Candles | <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> candles placed on a number line.
The <var>i</var>-th candle from the left is placed on coordinate <var>x_i</var>.
Here, <var>x_1 < x_2 < ... < x_N</var> holds.</p>
<p>Initially, no candles are burning.
Snuke decides to light <var>K</var> of the <var>N</var> candles.</p>
<p>Now, he is at coordinate <var>0</var>.
He can move left and right along the line with speed <var>1</var>.
He can also light a candle when he is at the same position as the candle, in negligible time.</p>
<p>Find the minimum time required to light <var>K</var> candles.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 10^5</var></li>
<li><var>1 \leq K \leq N</var></li>
<li><var>x_i</var> is an integer.</li>
<li><var>|x_i| \leq 10^8</var></li>
<li><var>x_1 < x_2 < ... < x_N</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>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 time required to light <var>K</var> candles.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5 3
-30 -10 10 20 50
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>40
</pre>
<p>He should move and light candles as follows:</p>
<ul>
<li>Move from coordinate <var>0</var> to <var>-10</var>.</li>
<li>Light the second candle from the left.</li>
<li>Move from coordinate <var>-10</var> to <var>10</var>.</li>
<li>Light the third candle from the left.</li>
<li>Move from coordinate <var>10</var> to <var>20</var>.</li>
<li>Light the fourth candle from the left.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 2
10 20 30
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>20
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1 1
0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0
</pre>
<ul>
<li>There may be a candle placed at coordinate <var>0</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>8 5
-9 -7 -4 -3 1 2 3 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>10
</pre></section>
</div>
</span> | [
[
"5 3\n-30 -10 10 20 50\n",
"5 3\n-30 -10 10 20 50\n"
]
] |
p03275 | AtCoder Beginner Contest 107 - Median of Medians | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We will define the <strong>median</strong> of a sequence <var>b</var> of length <var>M</var>, as follows:</p>
<ul>
<li>Let <var>b'</var> be the sequence obtained by sorting <var>b</var> in non-decreasing order. Then, the value of the <var>(M / 2 + 1)</var>-th element of <var>b'</var> is the median of <var>b</var>. Here, <var>/</var> is integer division, rounding down.</li>
</ul>
<p>For example, the median of <var>(10, 30, 20)</var> is <var>20</var>; the median of <var>(10, 30, 20, 40)</var> is <var>30</var>; the median of <var>(10, 10, 10, 20, 30)</var> is <var>10</var>.</p>
<p>Snuke comes up with the following problem.</p>
<p>You are given a sequence <var>a</var> of length <var>N</var>.
For each pair <var>(l, r)</var> (<var>1 \leq l \leq r \leq N</var>), let <var>m_{l, r}</var> be the median of the contiguous subsequence <var>(a_l, a_{l + 1}, ..., a_r)</var> of <var>a</var>.
We will list <var>m_{l, r}</var> for all pairs <var>(l, r)</var> to create a new sequence <var>m</var>.
Find the median of <var>m</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 10^5</var></li>
<li><var>a_i</var> is an integer.</li>
<li><var>1 \leq a_i \leq 10^9</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the median of <var>m</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
10 30 20
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>30
</pre>
<p>The median of each contiguous subsequence of <var>a</var> is as follows:</p>
<ul>
<li>The median of <var>(10)</var> is <var>10</var>.</li>
<li>The median of <var>(30)</var> is <var>30</var>.</li>
<li>The median of <var>(20)</var> is <var>20</var>.</li>
<li>The median of <var>(10, 30)</var> is <var>30</var>.</li>
<li>The median of <var>(30, 20)</var> is <var>30</var>.</li>
<li>The median of <var>(10, 30, 20)</var> is <var>20</var>.</li>
</ul>
<p>Thus, <var>m = (10, 30, 20, 30, 30, 20)</var> and the median of <var>m</var> is <var>30</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>1
10
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>10
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>10
5 9 5 9 8 9 3 5 4 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>8
</pre></section>
</div>
</span> | [
[
"3\n10 30 20\n",
"3\n10 30 20\n"
]
] |
p03276 | AtCoder Regular Contest 101 - Candles | <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> candles placed on a number line.
The <var>i</var>-th candle from the left is placed on coordinate <var>x_i</var>.
Here, <var>x_1 < x_2 < ... < x_N</var> holds.</p>
<p>Initially, no candles are burning.
Snuke decides to light <var>K</var> of the <var>N</var> candles.</p>
<p>Now, he is at coordinate <var>0</var>.
He can move left and right along the line with speed <var>1</var>.
He can also light a candle when he is at the same position as the candle, in negligible time.</p>
<p>Find the minimum time required to light <var>K</var> candles.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 10^5</var></li>
<li><var>1 \leq K \leq N</var></li>
<li><var>x_i</var> is an integer.</li>
<li><var>|x_i| \leq 10^8</var></li>
<li><var>x_1 < x_2 < ... < x_N</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>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 time required to light <var>K</var> candles.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5 3
-30 -10 10 20 50
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>40
</pre>
<p>He should move and light candles as follows:</p>
<ul>
<li>Move from coordinate <var>0</var> to <var>-10</var>.</li>
<li>Light the second candle from the left.</li>
<li>Move from coordinate <var>-10</var> to <var>10</var>.</li>
<li>Light the third candle from the left.</li>
<li>Move from coordinate <var>10</var> to <var>20</var>.</li>
<li>Light the fourth candle from the left.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 2
10 20 30
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>20
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1 1
0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0
</pre>
<ul>
<li>There may be a candle placed at coordinate <var>0</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>8 5
-9 -7 -4 -3 1 2 3 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>10
</pre></section>
</div>
</span> | [
[
"5 3\n-30 -10 10 20 50\n",
"5 3\n-30 -10 10 20 50\n"
]
] |
p03277 | AtCoder Regular Contest 101 - Median of Medians | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We will define the <strong>median</strong> of a sequence <var>b</var> of length <var>M</var>, as follows:</p>
<ul>
<li>Let <var>b'</var> be the sequence obtained by sorting <var>b</var> in non-decreasing order. Then, the value of the <var>(M / 2 + 1)</var>-th element of <var>b'</var> is the median of <var>b</var>. Here, <var>/</var> is integer division, rounding down.</li>
</ul>
<p>For example, the median of <var>(10, 30, 20)</var> is <var>20</var>; the median of <var>(10, 30, 20, 40)</var> is <var>30</var>; the median of <var>(10, 10, 10, 20, 30)</var> is <var>10</var>.</p>
<p>Snuke comes up with the following problem.</p>
<p>You are given a sequence <var>a</var> of length <var>N</var>.
For each pair <var>(l, r)</var> (<var>1 \leq l \leq r \leq N</var>), let <var>m_{l, r}</var> be the median of the contiguous subsequence <var>(a_l, a_{l + 1}, ..., a_r)</var> of <var>a</var>.
We will list <var>m_{l, r}</var> for all pairs <var>(l, r)</var> to create a new sequence <var>m</var>.
Find the median of <var>m</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 10^5</var></li>
<li><var>a_i</var> is an integer.</li>
<li><var>1 \leq a_i \leq 10^9</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the median of <var>m</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
10 30 20
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>30
</pre>
<p>The median of each contiguous subsequence of <var>a</var> is as follows:</p>
<ul>
<li>The median of <var>(10)</var> is <var>10</var>.</li>
<li>The median of <var>(30)</var> is <var>30</var>.</li>
<li>The median of <var>(20)</var> is <var>20</var>.</li>
<li>The median of <var>(10, 30)</var> is <var>30</var>.</li>
<li>The median of <var>(30, 20)</var> is <var>30</var>.</li>
<li>The median of <var>(10, 30, 20)</var> is <var>20</var>.</li>
</ul>
<p>Thus, <var>m = (10, 30, 20, 30, 30, 20)</var> and the median of <var>m</var> is <var>30</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>1
10
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>10
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>10
5 9 5 9 8 9 3 5 4 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>8
</pre></section>
</div>
</span> | [
[
"3\n10 30 20\n",
"3\n10 30 20\n"
]
] |
p03278 | AtCoder Regular Contest 101 - Ribbons on Tree | <span class="lang-en">
<p>Score : <var>900</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Let <var>N</var> be an even number.</p>
<p>There is a tree with <var>N</var> vertices.
The vertices are numbered <var>1, 2, ..., N</var>.
For each <var>i</var> (<var>1 \leq i \leq N - 1</var>), the <var>i</var>-th edge connects Vertex <var>x_i</var> and <var>y_i</var>.</p>
<p>Snuke would like to decorate the tree with ribbons, as follows.</p>
<p>First, he will divide the <var>N</var> vertices into <var>N / 2</var> pairs.
Here, each vertex must belong to exactly one pair.
Then, for each pair <var>(u, v)</var>, put a ribbon through all the edges contained in the shortest path between <var>u</var> and <var>v</var>.</p>
<p>Snuke is trying to divide the vertices into pairs so that the following condition is satisfied: "for every edge, there is at least one ribbon going through it."
How many ways are there to divide the vertices into pairs, satisfying this condition?
Find the count modulo <var>10^9 + 7</var>.
Here, two ways to divide the vertices into pairs are considered different when there is a pair that is contained in one of the two ways but not in the other.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>N</var> is an even number.</li>
<li><var>2 \leq N \leq 5000</var></li>
<li><var>1 \leq x_i, y_i \leq N</var></li>
<li>The given graph is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>x_1</var> <var>y_1</var>
<var>x_2</var> <var>y_2</var>
<var>:</var>
<var>x_{N - 1}</var> <var>y_{N - 1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of the ways to divide the vertices into pairs, satisfying the condition, modulo <var>10^9 + 7</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4
1 2
2 3
3 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>There are three possible ways to divide the vertices into pairs, as shown below, and two satisfy the condition: the middle one and the right one.</p>
<p><img src="https://img.atcoder.jp/arc101/2d7584d2e0736f746aa9d54e1bf31e28.png"/></p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4
1 2
1 3
1 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>3
</pre>
<p>There are three possible ways to divide the vertices into pairs, as shown below, and all of them satisfy the condition.</p>
<p><img src="https://img.atcoder.jp/arc101/2de530ed2e64d0161ee6b989d1946261.png"/></p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>6
1 2
1 3
3 4
1 5
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>10
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>10
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>672
</pre></section>
</div>
</span> | [
[
"4\n1 2\n2 3\n3 4\n",
"4\n1 2\n2 3\n3 4\n"
]
] |
p03279 | AtCoder Regular Contest 101 - Robots and Exits | <span class="lang-en">
<p>Score : <var>900</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There are <var>N</var> robots and <var>M</var> exits on a number line.
The <var>N + M</var> coordinates of these are all integers and all distinct.
For each <var>i</var> (<var>1 \leq i \leq N</var>), the coordinate of the <var>i</var>-th robot from the left is <var>x_i</var>.
Also, for each <var>j</var> (<var>1 \leq j \leq M</var>), the coordinate of the <var>j</var>-th exit from the left is <var>y_j</var>.</p>
<p>Snuke can repeatedly perform the following two kinds of operations in any order to move all the robots simultaneously:</p>
<ul>
<li>Increment the coordinates of all the robots on the number line by <var>1</var>.</li>
<li>Decrement the coordinates of all the robots on the number line by <var>1</var>.</li>
</ul>
<p>Each robot will disappear from the number line when its position coincides with that of an exit, going through that exit.
Snuke will continue performing operations until all the robots disappear.</p>
<p>When all the robots disappear, how many combinations of exits can be used by the robots?
Find the count modulo <var>10^9 + 7</var>.
Here, two combinations of exits are considered different when there is a robot that used different exits in those two combinations.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N, M \leq 10^5</var></li>
<li><var>1 \leq x_1 < x_2 < ... < x_N \leq 10^9</var></li>
<li><var>1 \leq y_1 < y_2 < ... < y_M \leq 10^9</var></li>
<li>All given coordinates are integers.</li>
<li>All given coordinates are distinct.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>M</var>
<var>x_1</var> <var>x_2</var> <var>...</var> <var>x_N</var>
<var>y_1</var> <var>y_2</var> <var>...</var> <var>y_M</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of the combinations of exits that can be used by the robots when all the robots disappear, modulo <var>10^9 + 7</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 2
2 3
1 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p>The <var>i</var>-th robot from the left will be called Robot <var>i</var>, and the <var>j</var>-th exit from the left will be called Exit <var>j</var>.
There are three possible combinations of exits (the exit used by Robot <var>1</var>, the exit used by Robot <var>2</var>) as follows:</p>
<ul>
<li><var>(</var>Exit <var>1</var><var>,</var> Exit <var>1</var><var>)</var></li>
<li><var>(</var>Exit <var>1</var><var>,</var> Exit <var>2</var><var>)</var></li>
<li><var>(</var>Exit <var>2</var><var>,</var> Exit <var>2</var><var>)</var></li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 4
2 5 10
1 3 7 13
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>8
</pre>
<p>The exit for each robot can be chosen independently, so there are <var>2^3 = 8</var> possible combinations of exits.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>4 1
1 2 4 5
3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>1
</pre>
<p>Every robot uses Exit <var>1</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>4 5
2 5 7 11
1 3 6 9 13
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>6
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 5</h3><pre>10 10
4 13 15 18 19 20 21 22 25 27
1 5 11 12 14 16 23 26 29 30
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 5</h3><pre>22
</pre></section>
</div>
</span> | [
[
"2 2\n2 3\n1 4\n",
"2 2\n2 3\n1 4\n"
]
] |
p03280 | AtCoder Beginner Contest 106 - Garden | <span class="lang-en">
<p>ο»ΏScore: <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3>
<p>There is a farm whose length and width are <var>A</var> yard and <var>B</var> yard, respectively. A farmer, John, made a vertical road and a horizontal road inside the farm from one border to another, as shown below: (The gray part represents the roads.)</p>
<p><img alt=" " src="https://img.atcoder.jp/ghi/27d063746b460f1132b6a99aa535a562.png"/></p>
<p>What is the area of this yard excluding the roads? Find it.</p>
</section>
</div>
<div class="part">
<section>
<h3>Note</h3>
<p>It can be proved that the positions of the roads do not affect the area.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3>
<ul>
<li><var>A</var> is an integer between <var>2</var> and <var>100</var> (inclusive).</li>
<li><var>B</var> is an integer between <var>2</var> and <var>100</var> (inclusive).</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3>
<p>Input is given from Standard Input in the following format:</p>
<pre><var>A</var> <var>B</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3>
<p>Print the area of this yard excluding the roads (in square yards).</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>1
</pre>
<p>In this case, the area is <var>1</var> square yard.</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>24
</pre>
<p>In this case, the area is <var>24</var> square yards. </p></section>
</div>
</span> | [
[
"2 2\n",
"2 2\n"
]
] |
p03281 | AtCoder Beginner Contest 106 - 105 | <span class="lang-en">
<p>Score: <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3>
<p>The number <var>105</var> is quite special - it is odd but still it has eight divisors.
Now, your task is this: how many odd numbers with exactly eight positive divisors are there between <var>1</var> and <var>N</var> (inclusive)?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3>
<ul>
<li><var>N</var> is an integer between <var>1</var> and <var>200</var> (inclusive).</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3>
<p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3>
<p>Print the count.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>105
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
</pre>
<p>Among the numbers between <var>1</var> and <var>105</var>, the only number that is odd and has exactly eight divisors is <var>105</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>7
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p><var>1</var> has one divisor. <var>3</var>, <var>5</var> and <var>7</var> are all prime and have two divisors. Thus, there is no number that satisfies the condition.</p></section>
</div>
</span> | [
[
"105\n",
"105\n"
]
] |
p03282 | AtCoder Beginner Contest 106 - To Infinity | <span class="lang-en">
<p>ο»ΏScore: <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3>
<p>Mr. Infinity has a string <var>S</var> consisting of digits from <code>1</code> to <code>9</code>. Each time the date changes, this string changes as follows:</p>
<ul>
<li>Each occurrence of <code>2</code> in <var>S</var> is replaced with <code>22</code>. Similarly, each <code>3</code> becomes <code>333</code>, <code>4</code> becomes <code>4444</code>, <code>5</code> becomes <code>55555</code>, <code>6</code> becomes <code>666666</code>, <code>7</code> becomes <code>7777777</code>, <code>8</code> becomes <code>88888888</code> and <code>9</code> becomes <code>999999999</code>. <code>1</code> remains as <code>1</code>.</li>
</ul>
<p>For example, if <var>S</var> is <code>1324</code>, it becomes <code>1333224444</code> the next day, and it becomes <code>133333333322224444444444444444</code> the day after next.
You are interested in what the string looks like after <var>5 \times 10^{15}</var> days. What is the <var>K</var>-th character from the left in the string after <var>5 \times 10^{15}</var> days?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3>
<ul>
<li><var>S</var> is a string of length between <var>1</var> and <var>100</var> (inclusive).</li>
<li><var>K</var> is an integer between <var>1</var> and <var>10^{18}</var> (inclusive).</li>
<li>The length of the string after <var>5 \times 10^{15}</var> days is at least <var>K</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3>
<p>Input is given from Standard Input in the following format:</p>
<pre><var>S</var>
<var>K</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3>
<p>Print the <var>K</var>-th character from the left in Mr. Infinity's string after <var>5 \times 10^{15}</var> days.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>1214
4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The string <var>S</var> changes as follows: </p>
<ul>
<li>Now: <code>1214</code></li>
<li>After one day: <code>12214444</code></li>
<li>After two days: <code>1222214444444444444444</code></li>
<li>After three days: <code>12222222214444444444444444444444444444444444444444444444444444444444444444</code></li>
</ul>
<p>The first five characters in the string after <var>5 \times 10^{15}</var> days is <code>12222</code>. As <var>K=4</var>, we should print the fourth character, <code>2</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
157
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>3
</pre>
<p>The initial string is <code>3</code>. The string after <var>5 \times 10^{15}</var> days consists only of <code>3</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>299792458
9460730472580800
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>2
</pre></section>
</div>
</span> | [
[
"1214\n4\n",
"1214\n4\n"
]
] |
p03283 | AtCoder Beginner Contest 106 - AtCoder Express 2 | <span class="lang-en">
<p>Score: <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3>
<p>In Takahashi Kingdom, there is a east-west railroad and <var>N</var> cities along it, numbered <var>1</var>, <var>2</var>, <var>3</var>, ..., <var>N</var> from west to east.
A company called <em>AtCoder Express</em> possesses <var>M</var> trains, and the train <var>i</var> runs from City <var>L_i</var> to City <var>R_i</var> (it is possible that <var>L_i = R_i</var>).
Takahashi the king is interested in the following <var>Q</var> matters:</p>
<ul>
<li>The number of the trains that runs <strong>strictly within</strong> the section from City <var>p_i</var> to City <var>q_i</var>, that is, the number of trains <var>j</var> such that <var>p_i \leq L_j</var> and <var>R_j \leq q_i</var>.</li>
</ul>
<p>Although he is genius, this is too much data to process by himself. Find the answer for each of these <var>Q</var> queries to help him.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3>
<ul>
<li><var>N</var> is an integer between <var>1</var> and <var>500</var> (inclusive).</li>
<li><var>M</var> is an integer between <var>1</var> and <var>200 \ 000</var> (inclusive).</li>
<li><var>Q</var> is an integer between <var>1</var> and <var>100 \ 000</var> (inclusive).</li>
<li><var>1 \leq L_i \leq R_i \leq N</var> <var>(1 \leq i \leq M)</var></li>
<li><var>1 \leq p_i \leq q_i \leq N</var> <var>(1 \leq i \leq Q)</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>Q</var>
<var>L_1</var> <var>R_1</var>
<var>L_2</var> <var>R_2</var>
<var>:</var>
<var>L_M</var> <var>R_M</var>
<var>p_1</var> <var>q_1</var>
<var>p_2</var> <var>q_2</var>
<var>:</var>
<var>p_Q</var> <var>q_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 number of the trains that runs <strong>strictly within</strong> the section from City <var>p_i</var> to City <var>q_i</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 3 1
1 1
1 2
2 2
1 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p>As all the trains runs within the section from City <var>1</var> to City <var>2</var>, the answer to the only query is <var>3</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>10 3 2
1 5
2 8
7 10
1 7
3 10
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1
1
</pre>
<p>The first query is on the section from City <var>1</var> to <var>7</var>. There is only one train that runs strictly within that section: Train <var>1</var>.
The second query is on the section from City <var>3</var> to <var>10</var>. There is only one train that runs strictly within that section: Train <var>3</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>10 10 10
1 6
2 9
4 5
4 7
4 7
5 8
6 6
6 7
7 9
10 10
1 8
1 9
1 10
2 8
2 9
2 10
3 8
3 9
3 10
1 10
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>7
9
10
6
8
9
6
7
8
10
</pre></section>
</div>
</span> | [
[
"2 3 1\n1 1\n1 2\n2 2\n1 2\n",
"2 3 1\n1 1\n1 2\n2 2\n1 2\n"
]
] |
p03284 | AtCoder Beginner Contest 105 - AtCoder Crackers | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Takahashi has decided to distribute <var>N</var> AtCoder Crackers to <var>K</var> users of as evenly as possible.
When all the crackers are distributed, find the minimum possible (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N,K \leq 100</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum possible (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>7 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
</pre>
<p>When the users receive two, two and three crackers, respectively, the (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user, is <var>1</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>100 10
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>The crackers can be distributed evenly.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0
</pre></section>
</div>
</span> | [
[
"7 3\n",
"7 3\n"
]
] |
p03285 | AtCoder Beginner Contest 105 - Cakes and Donuts | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p><em>La Confiserie d'ABC</em> sells cakes at <var>4</var> dollars each and doughnuts at <var>7</var> dollars each.
Determine if there is a way to buy some of them for exactly <var>N</var> dollars. You can buy two or more doughnuts and two or more cakes, and you can also choose to buy zero doughnuts or zero cakes.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>N</var> is an integer between <var>1</var> and <var>100</var>, inclusive.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If there is a way to buy some cakes and some doughnuts for exactly <var>N</var> dollars, print <code>Yes</code>; otherwise, print <code>No</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>11
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<p>If you buy one cake and one doughnut, the total will be <var>4 + 7 = 11</var> dollars.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>40
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>Yes
</pre>
<p>If you buy ten cakes, the total will be <var>4 \times 10 = 40</var> dollars.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>No
</pre>
<p>The prices of cakes (<var>4</var> dollars) and doughnuts (<var>7</var> dollars) are both higher than <var>3</var> dollars, so there is no such way.</p></section>
</div>
</span> | [
[
"11\n",
"11\n"
]
] |
p03286 | AtCoder Beginner Contest 105 - Base -2 Number | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an integer <var>N</var>, find the base <var>-2</var> representation of <var>N</var>.</p>
<p>Here, <var>S</var> is the base <var>-2</var> representation of <var>N</var> when the following are all satisfied:</p>
<ul>
<li><var>S</var> is a string consisting of <code>0</code> and <code>1</code>.</li>
<li>Unless <var>S =</var> <code>0</code>, the initial character of <var>S</var> is <code>1</code>.</li>
<li>Let <var>S = S_k S_{k-1} ... S_0</var>, then <var>S_0 \times (-2)^0 + S_1 \times (-2)^1 + ... + S_k \times (-2)^k = N</var>.</li>
</ul>
<p>It can be proved that, for any integer <var>M</var>, the base <var>-2</var> representation of <var>M</var> is uniquely determined.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>Every value in input is integer.</li>
<li><var>-10^9 \leq N \leq 10^9</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the base <var>-2</var> representation of <var>N</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>-9
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1011
</pre>
<p>As <var>(-2)^0 + (-2)^1 + (-2)^3 = 1 + (-2) + (-8) = -9</var>, <code>1011</code> is the base <var>-2</var> representation of <var>-9</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>11000101011001101110100010101
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0
</pre></section>
</div>
</span> | [
[
"-9\n",
"-9\n"
]
] |
p03287 | AtCoder Beginner Contest 105 - Candy Distribution | <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> boxes arranged in a row from left to right. The <var>i</var>-th box from the left contains <var>A_i</var> candies.</p>
<p>You will take out the candies from some consecutive boxes and distribute them evenly to <var>M</var> children.</p>
<p>Such being the case, find the number of the pairs <var>(l, r)</var> that satisfy the following:</p>
<ul>
<li><var>l</var> and <var>r</var> are both integers and satisfy <var>1 \leq l \leq r \leq N</var>.</li>
<li><var>A_l + A_{l+1} + ... + A_r</var> is a multiple of <var>M</var>.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>All values in input are integers.</li>
<li><var>1 \leq N \leq 10^5</var></li>
<li><var>2 \leq M \leq 10^9</var></li>
<li><var>1 \leq A_i \leq 10^9</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>M</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 pairs <var>(l, r)</var> that satisfy the conditions.</p>
<p>Note that the number may not fit into a <var>32</var>-bit integer type.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 2
4 1 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p>The sum <var>A_l + A_{l+1} + ... + A_r</var> for each pair <var>(l, r)</var> is as follows:</p>
<ul>
<li>Sum for <var>(1, 1)</var>: <var>4</var></li>
<li>Sum for <var>(1, 2)</var>: <var>5</var></li>
<li>Sum for <var>(1, 3)</var>: <var>10</var></li>
<li>Sum for <var>(2, 2)</var>: <var>1</var></li>
<li>Sum for <var>(2, 3)</var>: <var>6</var></li>
<li>Sum for <var>(3, 3)</var>: <var>5</var></li>
</ul>
<p>Among these, three are multiples of <var>2</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>13 17
29 7 5 7 9 51 7 13 8 55 42 9 81
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>6
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>10 400000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>25
</pre></section>
</div>
</span> | [
[
"3 2\n4 1 5\n",
"3 2\n4 1 5\n"
]
] |
p03288 | AtCoder Beginner Contest 104 - Rated for Me | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>A programming competition site <em>AtCode</em> regularly holds programming contests.</p>
<p>The next contest on AtCode is called ABC, which is rated for contestants with ratings less than <var>1200</var>.</p>
<p>The contest after the ABC is called ARC, which is rated for contestants with ratings less than <var>2800</var>.</p>
<p>The contest after the ARC is called AGC, which is rated for all contestants.</p>
<p>Takahashi's rating on AtCode is <var>R</var>. What is the next contest rated for him?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>0 β€ R β€ 4208</var></li>
<li><var>R</var> is an integer.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>R</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the name of the next contest rated for Takahashi (<code>ABC</code>, <code>ARC</code> or <code>AGC</code>).</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>1199
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>ABC
</pre>
<p><var>1199</var> is less than <var>1200</var>, so ABC will be rated.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>1200
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>ARC
</pre>
<p><var>1200</var> is not less than <var>1200</var> and ABC will be unrated, but it is less than <var>2800</var> and ARC will be rated.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>4208
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>AGC
</pre></section>
</div>
</span> | [
[
"1199\n",
"1199\n"
]
] |
p03289 | AtCoder Beginner Contest 104 - AcCepted | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a string <var>S</var>. Each character of <var>S</var> is uppercase or lowercase English letter.
Determine if <var>S</var> satisfies all of the following conditions:</p>
<ul>
<li>The initial character of <var>S</var> is an uppercase <code>A</code>.</li>
<li>There is exactly one occurrence of <code>C</code> between the third character from the beginning and the second to last character (inclusive).</li>
<li>All letters except the <code>A</code> and <code>C</code> mentioned above are lowercase.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>4 β€ |S| β€ 10</var> (<var>|S|</var> is the length of the string <var>S</var>.)</li>
<li>Each character of <var>S</var> is uppercase or lowercase English letter.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>S</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If <var>S</var> satisfies all of the conditions in the problem statement, print <code>AC</code>; otherwise, print <code>WA</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>AtCoder
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>AC
</pre>
<p>The first letter is <code>A</code>, the third letter is <code>C</code> and the remaining letters are all lowercase, so all the conditions are satisfied.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>ACoder
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>WA
</pre>
<p>The second letter should not be <code>C</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>AcycliC
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>WA
</pre>
<p>The last letter should not be <code>C</code>, either.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>AtCoCo
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>WA
</pre>
<p>There should not be two or more occurrences of <code>C</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 5</h3><pre>Atcoder
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 5</h3><pre>WA
</pre>
<p>The number of <code>C</code> should not be zero, either.</p></section>
</div>
</span> | [
[
"AtCoder\n",
"AtCoder\n"
]
] |
p03290 | AtCoder Beginner Contest 104 - All Green | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>A programming competition site <em>AtCode</em> provides algorithmic problems.
Each problem is allocated a score based on its difficulty.
Currently, for each integer <var>i</var> between <var>1</var> and <var>D</var> (inclusive), there are <var>p_i</var> problems with a score of <var>100i</var> points.
These <var>p_1 + β¦ + p_D</var> problems are all of the problems available on AtCode.</p>
<p>A user of AtCode has a value called <em>total score</em>.
The total score of a user is the sum of the following two elements:</p>
<ul>
<li>Base score: the sum of the scores of all problems solved by the user.</li>
<li>Perfect bonuses: when a user solves all problems with a score of <var>100i</var> points, he/she earns the perfect bonus of <var>c_i</var> points, aside from the base score <var>(1 β€ i β€ D)</var>.</li>
</ul>
<p>Takahashi, who is the new user of AtCode, has not solved any problem.
His objective is to have a total score of <var>G</var> or more points.
At least how many problems does he need to solve for this objective?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 β€ D β€ 10</var></li>
<li><var>1 β€ p_i β€ 100</var></li>
<li><var>100 β€ c_i β€ 10^6</var></li>
<li><var>100 β€ G</var></li>
<li>All values in input are integers.</li>
<li><var>c_i</var> and <var>G</var> are all multiples of <var>100</var>.</li>
<li>It is possible to have a total score of <var>G</var> or more points.</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>D</var> <var>G</var>
<var>p_1</var> <var>c_1</var>
<var>:</var>
<var>p_D</var> <var>c_D</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of problems that needs to be solved in order to have a total score of <var>G</var> or more points. Note that this objective is always achievable (see Constraints).</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 700
3 500
5 800
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p>In this case, there are three problems each with <var>100</var> points and five problems each with <var>200</var> points. The perfect bonus for solving all the <var>100</var>-point problems is <var>500</var> points, and the perfect bonus for solving all the <var>200</var>-point problems is <var>800</var> points. Takahashi's objective is to have a total score of <var>700</var> points or more.</p>
<p>One way to achieve this objective is to solve four <var>200</var>-point problems and earn a base score of <var>800</var> points. However, if we solve three <var>100</var>-point problems, we can earn the perfect bonus of <var>500</var> points in addition to the base score of <var>300</var> points, for a total score of <var>800</var> points, and we can achieve the objective with fewer problems.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2 2000
3 500
5 800
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>7
</pre>
<p>This case is similar to Sample Input 1, but the Takahashi's objective this time is <var>2000</var> points or more. In this case, we inevitably need to solve all five <var>200</var>-point problems, and by solving two <var>100</var>-point problems additionally we have the total score of <var>2000</var> points.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>2 400
3 500
5 800
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>2
</pre>
<p>This case is again similar to Sample Input 1, but the Takahashi's objective this time is <var>400</var> points or more. In this case, we only need to solve two <var>200</var>-point problems to achieve the objective.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>5 25000
20 1000
40 1000
50 1000
30 1000
1 1000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>66
</pre>
<p>There is only one <var>500</var>-point problem, but the perfect bonus can be earned even in such a case. </p></section>
</div>
</span> | [
[
"2 700\n3 500\n5 800\n",
"2 700\n3 500\n5 800\n"
]
] |
p03291 | AtCoder Beginner Contest 104 - We Love ABC | <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>The <em>ABC number</em> of a string <var>T</var> is the number of triples of integers <var>(i, j, k)</var> that satisfy all of the following conditions:</p>
<ul>
<li><var>1 β€ i < j < k β€ |T|</var> (<var>|T|</var> is the length of <var>T</var>.)</li>
<li><var>T_i =</var> <code>A</code> (<var>T_i</var> is the <var>i</var>-th character of <var>T</var> from the beginning.)</li>
<li><var>T_j =</var> <code>B</code></li>
<li><var>T_k =</var> <code>C</code></li>
</ul>
<p>For example, when <var>T =</var> <code>ABCBC</code>, there are three triples of integers <var>(i, j, k)</var> that satisfy the conditions: <var>(1, 2, 3), (1, 2, 5), (1, 4, 5)</var>. Thus, the ABC number of <var>T</var> is <var>3</var>.</p>
<p>You are given a string <var>S</var>. Each character of <var>S</var> is <code>A</code>, <code>B</code>, <code>C</code> or <code>?</code>.</p>
<p>Let <var>Q</var> be the number of occurrences of <code>?</code> in <var>S</var>. We can make <var>3^Q</var> strings by replacing each occurrence of <code>?</code> in <var>S</var> with <code>A</code>, <code>B</code> or <code>C</code>. Find the sum of the ABC numbers of all these strings.</p>
<p>This sum can be extremely large, so print the sum modulo <var>10^9 + 7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>3 β€ |S| β€ 10^5</var></li>
<li>Each character of <var>S</var> is <code>A</code>, <code>B</code>, <code>C</code> or <code>?</code>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>S</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the sum of the ABC numbers of all the <var>3^Q</var> strings, modulo <var>10^9 + 7</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>A??C
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>8
</pre>
<p>In this case, <var>Q = 2</var>, and we can make <var>3^Q = 9</var> strings by by replacing each occurrence of <code>?</code> with <code>A</code>, <code>B</code> or <code>C</code>. The ABC number of each of these strings is as follows:</p>
<ul>
<li><code>AAAC</code>: <var>0</var></li>
<li><code>AABC</code>: <var>2</var></li>
<li><code>AACC</code>: <var>0</var></li>
<li><code>ABAC</code>: <var>1</var></li>
<li><code>ABBC</code>: <var>2</var></li>
<li><code>ABCC</code>: <var>2</var></li>
<li><code>ACAC</code>: <var>0</var></li>
<li><code>ACBC</code>: <var>1</var></li>
<li><code>ACCC</code>: <var>0</var></li>
</ul>
<p>The sum of these is <var>0 + 2 + 0 + 1 + 2 + 2 + 0 + 1 + 0 = 8</var>, so we print <var>8</var> modulo <var>10^9 + 7</var>, that is, <var>8</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>ABCBC
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>3
</pre>
<p>When <var>Q = 0</var>, we print the ABC number of <var>S</var> itself, modulo <var>10^9 + 7</var>. This string is the same as the one given as an example in the problem statement, and its ABC number is <var>3</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>????C?????B??????A???????
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>979596887
</pre>
<p>In this case, the sum of the ABC numbers of all the <var>3^Q</var> strings is <var>2291979612924</var>, and we should print this number modulo <var>10^9 + 7</var>, that is, <var>979596887</var>.</p></section>
</div>
</span> | [
[
"A??C\n",
"A??C\n"
]
] |
p03292 | AtCoder Beginner Contest 103 - Task Scheduling Problem | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You have three tasks, all of which need to be completed.</p>
<p>First, you can complete any one task at cost <var>0</var>.</p>
<p>Then, just after completing the <var>i</var>-th task, you can complete the <var>j</var>-th task at cost <var>|A_j - A_i|</var>.</p>
<p>Here, <var>|x|</var> denotes the absolute value of <var>x</var>.</p>
<p>Find the minimum total cost required to complete all the task.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>All values in input are integers.</li>
<li><var>1 \leq A_1, A_2, A_3 \leq 100</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>A_1</var> <var>A_2</var> <var>A_3</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum total cost required to complete all the task.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>1 6 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>5
</pre>
<p>When the tasks are completed in the following order, the total cost will be <var>5</var>, which is the minimum:</p>
<ul>
<li>Complete the first task at cost <var>0</var>.</li>
<li>Complete the third task at cost <var>2</var>.</li>
<li>Complete the second task at cost <var>3</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>11 5 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>6
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>100 100 100
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0
</pre></section>
</div>
</span> | [
[
"1 6 3\n",
"1 6 3\n"
]
] |
p03293 | AtCoder Beginner Contest 103 - String Rotation | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given string <var>S</var> and <var>T</var> consisting of lowercase English letters.</p>
<p>Determine if <var>S</var> equals <var>T</var> after <em>rotation</em>.</p>
<p>That is, determine if <var>S</var> equals <var>T</var> after the following operation is performed some number of times:</p>
<p>Operation: Let <var>S = S_1 S_2 ... S_{|S|}</var>. Change <var>S</var> to <var>S_{|S|} S_1 S_2 ... S_{|S|-1}</var>.</p>
<p>Here, <var>|X|</var> denotes the length of the string <var>X</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq |S| \leq 100</var></li>
<li><var>|S| = |T|</var></li>
<li><var>S</var> and <var>T</var> consist of lowercase English letters.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>S</var>
<var>T</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If <var>S</var> equals <var>T</var> after <em>rotation</em>, print <code>Yes</code>; if it does not, print <code>No</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>kyoto
tokyo
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<ul>
<li>In the first operation, <code>kyoto</code> becomes <code>okyot</code>.</li>
<li>In the second operation, <code>okyot</code> becomes <code>tokyo</code>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>abc
arc
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>No
</pre>
<p><code>abc</code> does not equal <code>arc</code> after any number of operations.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>aaaaaaaaaaaaaaab
aaaaaaaaaaaaaaab
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>Yes
</pre></section>
</div>
</span> | [
[
"kyoto\ntokyo\n",
"kyoto\ntokyo\n"
]
] |
p03294 | AtCoder Beginner Contest 103 - Modulo Summation | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given <var>N</var> positive integers <var>a_1, a_2, ..., a_N</var>.</p>
<p>For a non-negative integer <var>m</var>, let <var>f(m) = (m\ mod\ a_1) + (m\ mod\ a_2) + ... + (m\ mod\ a_N)</var>.</p>
<p>Here, <var>X\ mod\ Y</var> denotes the remainder of the division of <var>X</var> by <var>Y</var>.</p>
<p>Find the maximum value of <var>f</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>All values in input are integers.</li>
<li><var>2 \leq N \leq 3000</var></li>
<li><var>2 \leq a_i \leq 10^5</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum value of <var>f</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
3 4 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>10
</pre>
<p><var>f(11) = (11\ mod\ 3) + (11\ mod\ 4) + (11\ mod\ 6) = 10</var> is the maximum value of <var>f</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5
7 46 11 20 11
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>90
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>7
994 518 941 851 647 2 581
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>4527
</pre></section>
</div>
</span> | [
[
"3\n3 4 6\n",
"3\n3 4 6\n"
]
] |
p03295 | AtCoder Beginner Contest 103 - Islands War | <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> islands lining up from west to east, connected by <var>N-1</var> bridges.</p>
<p>The <var>i</var>-th bridge connects the <var>i</var>-th island from the west and the <var>(i+1)</var>-th island from the west.</p>
<p>One day, disputes took place between some islands, and there were <var>M</var> requests from the inhabitants of the islands:</p>
<p>Request <var>i</var>: A dispute took place between the <var>a_i</var>-th island from the west and the <var>b_i</var>-th island from the west. Please make traveling between these islands with bridges impossible.</p>
<p>You decided to remove some bridges to meet all these <var>M</var> requests.</p>
<p>Find the minimum number of bridges that must be removed.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>All values in input are integers.</li>
<li><var>2 \leq N \leq 10^5</var></li>
<li><var>1 \leq M \leq 10^5</var></li>
<li><var>1 \leq a_i < b_i \leq N</var></li>
<li>All pairs <var>(a_i, b_i)</var> are distinct.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>M</var>
<var>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 minimum number of bridges that must be removed.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5 2
1 4
2 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
</pre>
<p>The requests can be met by removing the bridge connecting the second and third islands from the west.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>9 5
1 8
2 7
3 5
4 6
7 9
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>2
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>5 10
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>4
</pre></section>
</div>
</span> | [
[
"5 2\n1 4\n2 5\n",
"5 2\n1 4\n2 5\n"
]
] |
p03296 | AtCoder Grand Contest 026 - Colorful Slimes 2 | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Takahashi lives in another world. There are slimes (creatures) of <var>10000</var> colors in this world. Let us call these colors Color <var>1, 2, ..., 10000</var>.</p>
<p>Takahashi has <var>N</var> slimes, and they are standing in a row from left to right. The color of the <var>i</var>-th slime from the left is <var>a_i</var>.
If two slimes of the same color are adjacent, they will start to combine themselves. Because Takahashi likes smaller slimes, he has decided to change the colors of some of the slimes with his magic.</p>
<p>Takahashi can change the color of one slime to any of the <var>10000</var> colors by one spell.
How many spells are required so that no slimes will start to combine themselves?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq N \leq 100</var></li>
<li><var>1 \leq a_i \leq N</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of spells required.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5
1 1 2 2 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>For example, if we change the color of the second slime from the left to <var>4</var>, and the color of the fourth slime to <var>5</var>, the colors of the slimes will be <var>1, 4, 2, 5, 2</var>, which satisfy the condition.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
1 2 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Although the colors of the first and third slimes are the same, they are not adjacent, so no spell is required.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>5
1 1 1 1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>2
</pre>
<p>For example, if we change the colors of the second and fourth slimes from the left to <var>2</var>, the colors of the slimes will be <var>1, 2, 1, 2, 1</var>, which satisfy the condition.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>14
1 2 2 3 3 3 4 4 4 4 1 2 3 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>4
</pre></section>
</div>
</span> | [
[
"5\n1 1 2 2 2\n",
"5\n1 1 2 2 2\n"
]
] |
p03297 | AtCoder Grand Contest 026 - rng_10s | <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Ringo Mart, a convenience store, sells apple juice.</p>
<p>On the opening day of Ringo Mart, there were <var>A</var> cans of juice in stock in the morning.
Snuke buys <var>B</var> cans of juice here every day in the daytime.
Then, the manager checks the number of cans of juice remaining in stock every night.
If there are <var>C</var> or less cans, <var>D</var> new cans will be added to the stock by the next morning.</p>
<p>Determine if Snuke can buy juice indefinitely, that is, there is always <var>B</var> or more cans of juice in stock when he attempts to buy them.
Nobody besides Snuke buy juice at this store.</p>
<p>Note that each test case in this problem consists of <var>T</var> queries.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq T \leq 300</var></li>
<li><var>1 \leq A, B, C, D \leq 10^{18}</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>T</var>
<var>A_1</var> <var>B_1</var> <var>C_1</var> <var>D_1</var>
<var>A_2</var> <var>B_2</var> <var>C_2</var> <var>D_2</var>
<var>:</var>
<var>A_T</var> <var>B_T</var> <var>C_T</var> <var>D_T</var>
</pre>
<p>In the <var>i</var>-th query, <var>A = A_i, B = B_i, C = C_i, D = D_i</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <var>T</var> lines. The <var>i</var>-th line should contain <code>Yes</code> if Snuke can buy apple juice indefinitely in the <var>i</var>-th query; <code>No</code> otherwise.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>14
9 7 5 9
9 7 6 9
14 10 7 12
14 10 8 12
14 10 9 12
14 10 7 11
14 10 8 11
14 10 9 11
9 10 5 10
10 10 5 10
11 10 5 10
16 10 5 10
1000000000000000000 17 14 999999999999999985
1000000000000000000 17 15 999999999999999985
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>No
Yes
No
Yes
Yes
No
No
Yes
No
Yes
Yes
No
No
Yes
</pre>
<p>In the first query, the number of cans of juice in stock changes as follows: (D represents daytime and N represents night.)</p>
<p><var>9</var>
<ruby><rb>β</rb><rt>D</rt></ruby> <var>2</var>
<ruby><rb>β</rb><rt>N</rt></ruby> <var>11</var>
<ruby><rb>β</rb><rt>D</rt></ruby> <var>4</var>
<ruby><rb>β</rb><rt>N</rt></ruby> <var>13</var>
<ruby><rb>β</rb><rt>D</rt></ruby> <var>6</var>
<ruby><rb>β</rb><rt>N</rt></ruby> <var>6</var>
<ruby><rb>β</rb><rt>D</rt></ruby> x</p>
<p>In the second query, the number of cans of juice in stock changes as follows:</p>
<p><var>9</var>
<ruby><rb>β</rb><rt>D</rt></ruby> <var>2</var>
<ruby><rb>β</rb><rt>N</rt></ruby> <var>11</var>
<ruby><rb>β</rb><rt>D</rt></ruby> <var>4</var>
<ruby><rb>β</rb><rt>N</rt></ruby> <var>13</var>
<ruby><rb>β</rb><rt>D</rt></ruby> <var>6</var>
<ruby><rb>β</rb><rt>N</rt></ruby> <var>15</var>
<ruby><rb>β</rb><rt>D</rt></ruby> <var>8</var>
<ruby><rb>β</rb><rt>N</rt></ruby> <var>8</var>
<ruby><rb>β</rb><rt>D</rt></ruby> <var>1</var>
<ruby><rb>β</rb><rt>N</rt></ruby> <var>10</var>
<ruby><rb>β</rb><rt>D</rt></ruby> <var>3</var>
<ruby><rb>β</rb><rt>N</rt></ruby> <var>12</var>
<ruby><rb>β</rb><rt>D</rt></ruby> <var>5</var>
<ruby><rb>β</rb><rt>N</rt></ruby> <var>14</var>
<ruby><rb>β</rb><rt>D</rt></ruby> <var>7</var>
<ruby><rb>β</rb><rt>N</rt></ruby> <var>7</var>
<ruby><rb>β</rb><rt>D</rt></ruby> <var>0</var>
<ruby><rb>β</rb><rt>N</rt></ruby> <var>9</var>
<ruby><rb>β</rb><rt>D</rt></ruby> <var>2</var>
<ruby><rb>β</rb><rt>N</rt></ruby> <var>11</var>
<ruby><rb>β</rb><rt>D</rt></ruby> β¦</p>
<p>and so on, thus Snuke can buy juice indefinitely.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>24
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>No
No
No
No
No
No
Yes
Yes
No
No
No
No
Yes
Yes
Yes
No
No
No
Yes
Yes
Yes
No
No
No
</pre></section>
</div>
</span> | [
[
"14\n9 7 5 9\n9 7 6 9\n14 10 7 12\n14 10 8 12\n14 10 9 12\n14 10 7 11\n14 10 8 11\n14 10 9 11\n9 10 5 10\n10 10 5 10\n11 10 5 10\n16 10 5 10\n1000000000000000000 17 14 999999999999999985\n1000000000000000000 17 15 999999999999999985\n",
"14\n9 7 5 9\n9 7 6 9\n14 10 7 12\n14 10 8 12\n14 10 9 12\n14 10 7 11\n14 10 8 11\n14 10 9 11\n9 10 5 10\n10 10 5 10\n11 10 5 10\n16 10 5 10\n1000000000000000000 17 14 999999999999999985\n1000000000000000000 17 15 999999999999999985\n"
]
] |
p03298 | AtCoder Grand Contest 026 - String Coloring | <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a string <var>S</var> of length <var>2N</var> consisting of lowercase English letters.</p>
<p>There are <var>2^{2N}</var> ways to color each character in <var>S</var> red or blue. Among these ways, how many satisfy the following condition?</p>
<ul>
<li>The string obtained by reading the characters painted red <strong>from left to right</strong> is equal to the string obtained by reading the characters painted blue <strong>from right to left</strong>.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 18</var></li>
<li>The length of <var>S</var> is <var>2N</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>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>S</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of ways to paint the string that satisfy the condition.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4
cabaacba
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>4
</pre>
<p>There are four ways to paint the string, as follows:</p>
<ul>
<li><span style="color:red">c</span><span style="color:blue">a</span><span style="color:blue">b</span><span style="color:blue">a</span><span style="color:red">a</span><span style="color:blue">c</span><span style="color:red">b</span><span style="color:red">a</span></li>
<li><span style="color:red">c</span><span style="color:blue">a</span><span style="color:blue">b</span><span style="color:red">a</span><span style="color:blue">a</span><span style="color:blue">c</span><span style="color:red">b</span><span style="color:red">a</span></li>
<li><span style="color:blue">c</span><span style="color:red">a</span><span style="color:red">b</span><span style="color:red">a</span><span style="color:blue">a</span><span style="color:red">c</span><span style="color:blue">b</span><span style="color:blue">a</span></li>
<li><span style="color:blue">c</span><span style="color:red">a</span><span style="color:red">b</span><span style="color:blue">a</span><span style="color:red">a</span><span style="color:red">c</span><span style="color:blue">b</span><span style="color:blue">a</span></li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>11
mippiisssisssiipsspiim
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>504
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>4
abcdefgh
</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>18
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>9075135300
</pre>
<p>The answer may not be representable as a <var>32</var>-bit integer.</p></section>
</div>
</span> | [
[
"4\ncabaacba\n",
"4\ncabaacba\n"
]
] |
p03299 | AtCoder Grand Contest 026 - Histogram Coloring | <span class="lang-en">
<p>Score : <var>1100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Let us consider a grid of squares with <var>10^9</var> rows and <var>N</var> columns. Let <var>(i, j)</var> be the square at the <var>i</var>-th column <var>(1 \leq i \leq N)</var> from the left and <var>j</var>-th row <var>(1 \leq j \leq 10^9)</var> from the bottom.</p>
<p>Snuke has cut out some part of the grid so that, for each <var>i = 1, 2, ..., N</var>, the bottom-most <var>h_i</var> squares are remaining in the <var>i</var>-th column from the left.
Now, he will paint the remaining squares in red and blue.
Find the number of the ways to paint the squares so that the following condition is satisfied:</p>
<ul>
<li>Every remaining square is painted either red or blue.</li>
<li>For all <var>1 \leq i \leq N-1</var> and <var>1 \leq j \leq min(h_i, h_{i+1})-1</var>, there are exactly two squares painted red and two squares painted blue among the following four squares: <var>(i, j), (i, j+1), (i+1, j)</var> and <var>(i+1, j+1)</var>.</li>
</ul>
<p>Since the number of 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 \leq N \leq 100</var></li>
<li><var>1 \leq h_i \leq 10^9</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>h_1</var> <var>h_2</var> <var>...</var> <var>h_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of the ways to paint the squares, modulo <var>10^9+7</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>9
2 3 5 4 1 2 4 2 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>12800
</pre>
<p>One of the ways to paint the squares is shown below:</p>
<pre>
<span style="color:blue">#</span>
<span style="color:blue">#</span><span style="color:red">#</span> <span style="color:blue">#</span>
<span style="color:red">#</span><span style="color:blue">#</span><span style="color:red">#</span> <span style="color:blue">#</span>
<span style="color:blue">#</span><span style="color:red">#</span><span style="color:blue">#</span><span style="color:red">#</span> <span style="color:blue">#</span><span style="color:blue">#</span><span style="color:red">#</span>
<span style="color:red">#</span><span style="color:blue">#</span><span style="color:red">#</span><span style="color:blue">#</span><span style="color:red">#</span><span style="color:red">#</span><span style="color:red">#</span><span style="color:blue">#</span><span style="color:blue">#</span>
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2
2 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>6
</pre>
<p>There are six ways to paint the squares, as follows:</p>
<pre>
<span style="color:blue">#</span><span style="color:blue">#</span> <span style="color:blue">#</span><span style="color:red">#</span> <span style="color:red">#</span><span style="color:red">#</span> <span style="color:red">#</span><span style="color:blue">#</span> <span style="color:blue">#</span><span style="color:red">#</span> <span style="color:red">#</span><span style="color:blue">#</span>
<span style="color:red">#</span><span style="color:red">#</span> <span style="color:blue">#</span><span style="color:red">#</span> <span style="color:blue">#</span><span style="color:blue">#</span> <span style="color:red">#</span><span style="color:blue">#</span> <span style="color:red">#</span><span style="color:blue">#</span> <span style="color:blue">#</span><span style="color:red">#</span>
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>5
2 1 2 1 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>256
</pre>
<p>Every way to paint the squares satisfies the condition.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>9
27 18 28 18 28 45 90 45 23
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>844733013
</pre>
<p>Remember to print the number of ways modulo <var>10^9 + 7</var>.</p></section>
</div>
</span> | [
[
"9\n2 3 5 4 1 2 4 2 1\n",
"9\n2 3 5 4 1 2 4 2 1\n"
]
] |
p03300 | AtCoder Grand Contest 026 - Synchronized Subsequence | <span class="lang-en">
<p>Score : <var>1600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a string <var>S</var> of length <var>2N</var>, containing <var>N</var> occurrences of <code>a</code> and <var>N</var> occurrences of <code>b</code>.</p>
<p>You will choose some of the characters in <var>S</var>. Here, for each <var>i = 1,2,...,N</var>, it is not allowed to choose exactly one of the following two: the <var>i</var>-th occurrence of <code>a</code> and the <var>i</var>-th occurrence of <code>b</code>. (That is, you can only choose both or neither.) Then, you will concatenate the chosen characters (without changing the order).</p>
<p>Find the lexicographically largest string that can be obtained in this way.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 3000</var></li>
<li><var>S</var> is a string of length <var>2N</var> containing <var>N</var> occurrences of <code>a</code> and <var>N</var> occurrences of <code>b</code>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>S</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the lexicographically largest string that satisfies the condition.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
aababb
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>abab
</pre>
<p>A subsequence of <var>T</var> obtained from taking the first, third, fourth and sixth characters in <var>S</var>, satisfies the condition.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
bbabaa
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>bbabaa
</pre>
<p>You can choose all the characters.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>6
bbbaabbabaaa
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>bbbabaaa
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>9
abbbaababaababbaba
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>bbaababababa
</pre></section>
</div>
</span> | [
[
"3\naababb\n",
"3\naababb\n"
]
] |
p03301 | AtCoder Grand Contest 026 - Manju Game | <span class="lang-en">
<p>Score : <var>2000</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There are <var>N</var> boxes arranged in a row from left to right. The <var>i</var>-th box from the left contains <var>a_i</var> manju (buns stuffed with bean paste).
<span style="color:#C06000">Sugim</span> and <span style="color:red">Sigma</span> play a game using these boxes.
They alternately perform the following operation. <span style="color:#C06000">Sugim</span> goes first, and the game ends when a total of <var>N</var> operations are performed.</p>
<ul>
<li>Choose a box that still does not contain a piece and is adjacent to the box chosen in the other player's <strong>last</strong> operation, then put a piece in that box. If there are multiple such boxes, any of them can be chosen.</li>
<li>If there is no box that satisfies the condition above, or this is <span style="color:#C06000">Sugim</span>'s first operation, choose any one box that still does not contain a piece, then put a piece in that box.</li>
</ul>
<p>At the end of the game, each player can have the manju in the boxes in which he put his pieces.
They love manju, and each of them is wise enough to perform the optimal moves in order to have the maximum number of manju at the end of the game.</p>
<p>Find the number of manju that each player will have at the end of the game.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq N \leq 300</var> <var>000</var></li>
<li><var>1 \leq a_i \leq 1000</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the numbers of <span style="color:#C06000">Sugim</span>'s manju and <span style="color:red">Sigma</span>'s manju at the end of the game, in this order, with a space in between.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5
20 100 10 1 10
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>120 21
</pre>
<p>If the two performs the optimal moves, the game proceeds as follows:</p>
<ul>
<li>First, <span style="color:#C06000">Sugim</span> has to put his piece in the second box from the left.</li>
<li>Then, <span style="color:red">Sigma</span> has to put his piece in the leftmost box.</li>
<li>Then, <span style="color:#C06000">Sugim</span> puts his piece in the third or fifth box.</li>
<li>Then, <span style="color:red">Sigma</span> puts his piece in the fourth box.</li>
<li>Finally, <span style="color:#C06000">Sugim</span> puts his piece in the remaining box.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6
4 5 1 1 4 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>11 9
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>5
1 10 100 10 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>102 20
</pre>
<!-- {sample example_3}
The answer may not be representable as a <var>32</var>-bit integer. --></section>
</div>
</span> | [
[
"5\n20 100 10 1 10\n",
"5\n20 100 10 1 10\n"
]
] |
p03302 | SoundHound Inc. Programming Contest 2018 -Masters Tournament- - F | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given two integers <var>a</var> and <var>b</var>.
Determine if <var>a+b=15</var> or <var>a\times b=15</var> or neither holds.</p>
<p>Note that <var>a+b=15</var> and <var>a\times b=15</var> do not hold at the same time.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq a,b \leq 15</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>a</var> <var>b</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If <var>a+b=15</var>, print <code>+</code>;
if <var>a\times b=15</var>, print <code>*</code>;
if neither holds, print <code>x</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4 11
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>+
</pre>
<p><var>4+11=15</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>*
</pre>
<p><var>3\times 5=15</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>x
</pre>
<p><var>1+1=2</var> and <var>1\times 1=1</var>, neither of which is <var>15</var>.</p></section>
</div>
</span> | [
[
"4 11\n",
"4 11\n"
]
] |
p03303 | SoundHound Inc. Programming Contest 2018 -Masters Tournament- - Acrostic | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a string <var>S</var> consisting of lowercase English letters.
We will write down this string, starting a new line after every <var>w</var> letters. Print the string obtained by concatenating the letters at the beginnings of these lines from top to bottom.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq w \leq |S| \leq 1000</var></li>
<li><var>S</var> consists of lowercase English letters.</li>
<li><var>w</var> is an integer.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>S</var>
<var>w</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the desired string in one line.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>abcdefgh
3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>adg
</pre>
<p>When we write down <code>abcdefgh</code>, starting a new line after every three letters, we get the following:</p>
<p>abc<br/>
def<br/>
gh</p>
<p>Concatenating the letters at the beginnings of these lines, we obtain <code>adg</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>lllll
1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>lllll
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>souuundhound
2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>suudon
</pre></section>
</div>
</span> | [
[
"abcdefgh\n3\n",
"abcdefgh\n3\n"
]
] |
p03304 | SoundHound Inc. Programming Contest 2018 -Masters Tournament- - Ordinary Beauty | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Let us define the <em>beauty</em> of a sequence <var>(a_1,... ,a_n)</var> as the number of pairs of two adjacent elements in it whose absolute differences are <var>d</var>.
For example, when <var>d=1</var>, the beauty of the sequence <var>(3, 2, 3, 10, 9)</var> is <var>3</var>.</p>
<p>There are a total of <var>n^m</var> sequences of length <var>m</var> where each element is an integer between <var>1</var> and <var>n</var> (inclusive).
Find the beauty of each of these <var>n^m</var> sequences, and print the average of those values.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>0 \leq d < n \leq 10^9</var></li>
<li><var>2 \leq m \leq 10^9</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>n</var> <var>m</var> <var>d</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the average of the beauties of the sequences of length <var>m</var> where each element is an integer between <var>1</var> and <var>n</var>.
The output will be judged correct if the absolute or relative error is at most <var>10^{-6}</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 3 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1.0000000000
</pre>
<p>The beauty of <var>(1,1,1)</var> is <var>0</var>.<br/>
The beauty of <var>(1,1,2)</var> is <var>1</var>.<br/>
The beauty of <var>(1,2,1)</var> is <var>2</var>.<br/>
The beauty of <var>(1,2,2)</var> is <var>1</var>.<br/>
The beauty of <var>(2,1,1)</var> is <var>1</var>.<br/>
The beauty of <var>(2,1,2)</var> is <var>2</var>.<br/>
The beauty of <var>(2,2,1)</var> is <var>1</var>.<br/>
The beauty of <var>(2,2,2)</var> is <var>0</var>.<br/>
The answer is the average of these values: <var>(0+1+2+1+1+2+1+0)/8=1</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>1000000000 180707 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0.0001807060
</pre></section>
</div>
</span> | [
[
"2 3 1\n",
"2 3 1\n"
]
] |
p03305 | SoundHound Inc. Programming Contest 2018 -Masters Tournament- - Saving Snuuk | <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Kenkoooo is planning a trip in Republic of Snuke.
In this country, there are <var>n</var> cities and <var>m</var> trains running.
The cities are numbered <var>1</var> through <var>n</var>, and the <var>i</var>-th train connects City <var>u_i</var> and <var>v_i</var> bidirectionally.
Any city can be reached from any city by changing trains.</p>
<p>Two currencies are used in the country: yen and snuuk.
Any train fare can be paid by both yen and snuuk.
The fare of the <var>i</var>-th train is <var>a_i</var> yen if paid in yen, and <var>b_i</var> snuuk if paid in snuuk.</p>
<p>In a city with a money exchange office, you can change <var>1</var> yen into <var>1</var> snuuk.
However, when you do a money exchange, you have to change all your yen into snuuk.
That is, if Kenkoooo does a money exchange when he has <var>X</var> yen, he will then have <var>X</var> snuuk.
Currently, there is a money exchange office in every city, but the office in City <var>i</var> will shut down in <var>i</var> years and can never be used in and after that year.</p>
<p>Kenkoooo is planning to depart City <var>s</var> with <var>10^{15}</var> yen in his pocket and head for City <var>t</var>, and change his yen into snuuk in some city while traveling.
It is acceptable to do the exchange in City <var>s</var> or City <var>t</var>.</p>
<p>Kenkoooo would like to have as much snuuk as possible when he reaches City <var>t</var> by making the optimal choices for the route to travel and the city to do the exchange.
For each <var>i=0,...,n-1</var>, find the maximum amount of snuuk that Kenkoooo has when he reaches City <var>t</var> if he goes on a trip from City <var>s</var> to City <var>t</var> after <var>i</var> years.
You can assume that the trip finishes within the year.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq n \leq 10^5</var></li>
<li><var>1 \leq m \leq 10^5</var></li>
<li><var>1 \leq s,t \leq n</var></li>
<li><var>s \neq t</var></li>
<li><var>1 \leq u_i < v_i \leq n</var></li>
<li><var>1 \leq a_i,b_i \leq 10^9</var></li>
<li>If <var>i\neq j</var>, then <var>u_i \neq u_j </var> or <var>v_i \neq v_j</var>.</li>
<li>Any city can be reached from any city by changing trains.</li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>n</var> <var>m</var> <var>s</var> <var>t</var>
<var>u_1</var> <var>v_1</var> <var>a_1</var> <var>b_1</var>
<var>:</var>
<var>u_m</var> <var>v_m</var> <var>a_m</var> <var>b_m</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <var>n</var> lines.
In the <var>i</var>-th line, print the maximum amount of snuuk that Kenkoooo has when he reaches City <var>t</var> if he goes on a trip from City <var>s</var> to City <var>t</var> after <var>i-1</var> years.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4 3 2 3
1 4 1 100
1 2 1 10
1 3 20 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>999999999999998
999999999999989
999999999999979
999999999999897
</pre>
<p>After <var>0</var> years, it is optimal to do the exchange in City <var>1</var>.<br/>
After <var>1</var> years, it is optimal to do the exchange in City <var>2</var>.<br/>
Note that City <var>1</var> can still be visited even after the exchange office is closed.
Also note that, if it was allowed to keep <var>1</var> yen when do the exchange in City <var>2</var> and change the remaining yen into snuuk, we could reach City <var>3</var> with <var>999999999999998</var> snuuk, but this is NOT allowed.<br/>
After <var>2</var> years, it is optimal to do the exchange in City <var>3</var>.<br/>
After <var>3</var> years, it is optimal to do the exchange in City <var>4</var>.
Note that the same train can be used multiple times.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>8 12 3 8
2 8 685087149 857180777
6 7 298270585 209942236
2 4 346080035 234079976
2 5 131857300 22507157
4 8 30723332 173476334
2 6 480845267 448565596
1 4 181424400 548830121
4 5 57429995 195056405
7 8 160277628 479932440
1 6 475692952 203530153
3 5 336869679 160714712
2 7 389775999 199123879
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
</pre></section>
</div>
</span> | [
[
"4 3 2 3\n1 4 1 100\n1 2 1 10\n1 3 20 1\n",
"4 3 2 3\n1 4 1 100\n1 2 1 10\n1 3 20 1\n"
]
] |
p03306 | SoundHound Inc. Programming Contest 2018 -Masters Tournament- - + Graph | <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Kenkoooo found a simple connected graph.
The vertices are numbered <var>1</var> through <var>n</var>.
The <var>i</var>-th edge connects Vertex <var>u_i</var> and <var>v_i</var>, and has a fixed integer <var>s_i</var>.</p>
<p>Kenkoooo is trying to write a <em>positive integer</em> in each vertex so that the following condition is satisfied:</p>
<ul>
<li>For every edge <var>i</var>, the sum of the positive integers written in Vertex <var>u_i</var> and <var>v_i</var> is equal to <var>s_i</var>.</li>
</ul>
<p>Find the number of such ways to write positive integers in the vertices.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq n \leq 10^5</var></li>
<li><var>1 \leq m \leq 10^5</var></li>
<li><var>1 \leq u_i < v_i \leq n</var></li>
<li><var>2 \leq s_i \leq 10^9</var></li>
<li>If <var>i\neq j</var>, then <var>u_i \neq u_j </var> or <var>v_i \neq v_j</var>.</li>
<li>The graph is connected.</li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>n</var> <var>m</var>
<var>u_1</var> <var>v_1</var> <var>s_1</var>
<var>:</var>
<var>u_m</var> <var>v_m</var> <var>s_m</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of ways to write positive integers in the vertices so that the condition is satisfied.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 3
1 2 3
2 3 5
1 3 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
</pre>
<p>The condition will be satisfied if we write <var>1,2</var> and <var>3</var> in vertices <var>1,2</var> and <var>3</var>, respectively.
There is no other way to satisfy the condition, so the answer is <var>1</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4 3
1 2 6
2 3 7
3 4 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>3
</pre>
<p>Let <var>a,b,c</var> and <var>d</var> be the numbers to write in vertices <var>1,2,3</var> and <var>4</var>, respectively.
There are three quadruples <var>(a,b,c,d)</var> that satisfy the condition:</p>
<ul>
<li><var>(a,b,c,d)=(1,5,2,3)</var></li>
<li><var>(a,b,c,d)=(2,4,3,2)</var></li>
<li><var>(a,b,c,d)=(3,3,4,1)</var></li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>8 7
1 2 1000000000
2 3 2
3 4 1000000000
4 5 2
5 6 1000000000
6 7 2
7 8 1000000000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0
</pre></section>
</div>
</span> | [
[
"3 3\n1 2 3\n2 3 5\n1 3 4\n",
"3 3\n1 2 3\n2 3 5\n1 3 4\n"
]
] |
p03307 | AtCoder Beginner Contest 102 - Multiple of 2 and N | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a positive integer <var>N</var>.
Find the minimum positive integer divisible by both <var>2</var> and <var>N</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 10^9</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum positive integer divisible by both <var>2</var> and <var>N</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>
<p><var>6</var> is divisible by both <var>2</var> and <var>3</var>.
Also, there is no positive integer less than <var>6</var> that is divisible by both <var>2</var> and <var>3</var>.
Thus, the answer is <var>6</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>10
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>10
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>999999999
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>1999999998
</pre></section>
</div>
</span> | [
[
"3\n",
"3\n"
]
] |
p03308 | AtCoder Beginner Contest 102 - Maximum Difference | <span class="lang-en">
<p>Score : <var>200</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>.
Find the maximum absolute difference of two elements (with different indices) in <var>A</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq N \leq 100</var></li>
<li><var>1 \leq A_i \leq 10^9</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum absolute difference of two elements (with different indices) in <var>A</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4
1 4 6 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>5
</pre>
<p>The maximum absolute difference of two elements is <var>A_3-A_1=6-1=5</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2
1000000000 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>999999999
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>5
1 1 1 1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0
</pre></section>
</div>
</span> | [
[
"4\n1 4 6 3\n",
"4\n1 4 6 3\n"
]
] |
p03309 | AtCoder Beginner Contest 102 - Linear Approximation | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Snuke has an integer sequence <var>A</var> of length <var>N</var>.</p>
<p>He will freely choose an integer <var>b</var>.
Here, he will get sad if <var>A_i</var> and <var>b+i</var> are far from each other.
More specifically, the <em>sadness</em> of Snuke is calculated as follows:</p>
<ul>
<li><var>abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))</var></li>
</ul>
<p>Here, <var>abs(x)</var> is a function that returns the absolute value of <var>x</var>.</p>
<p>Find the minimum possible sadness of Snuke.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 2 \times 10^5</var></li>
<li><var>1 \leq A_i \leq 10^9</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum possible sadness of Snuke.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5
2 2 3 5 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>If we choose <var>b=0</var>, the sadness of Snuke would be <var>abs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2</var>.
Any choice of <var>b</var> does not make the sadness of Snuke less than <var>2</var>, so the answer is <var>2</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>9
1 2 3 4 5 6 7 8 9
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>6
6 5 4 3 2 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>18
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>7
1 1 1 1 2 3 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>6
</pre></section>
</div>
</span> | [
[
"5\n2 2 3 5 5\n",
"5\n2 2 3 5 5\n"
]
] |
p03310 | AtCoder Beginner Contest 102 - Equal Cut | <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Snuke has an integer sequence <var>A</var> of length <var>N</var>.</p>
<p>He will make three cuts in <var>A</var> and divide it into four (non-empty) contiguous subsequences <var>B, C, D</var> and <var>E</var>.
The positions of the cuts can be freely chosen.</p>
<p>Let <var>P,Q,R,S</var> be the sums of the elements in <var>B,C,D,E</var>, respectively.
Snuke is happier when the absolute difference of the maximum and the minimum among <var>P,Q,R,S</var> is smaller.
Find the minimum possible absolute difference of the maximum and the minimum among <var>P,Q,R,S</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>4 \leq N \leq 2 \times 10^5</var></li>
<li><var>1 \leq A_i \leq 10^9</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Find the minimum possible absolute difference of the maximum and the minimum among <var>P,Q,R,S</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5
3 2 4 1 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>If we divide <var>A</var> as <var>B,C,D,E=(3),(2),(4),(1,2)</var>, then <var>P=3,Q=2,R=4,S=1+2=3</var>.
Here, the maximum and the minimum among <var>P,Q,R,S</var> are <var>4</var> and <var>2</var>, with the absolute difference of <var>2</var>.
We cannot make the absolute difference of the maximum and the minimum less than <var>2</var>, so the answer is <var>2</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>10
10 71 84 33 6 47 23 25 52 64
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>36
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>7
1 2 3 1000000000 4 5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>999999994
</pre></section>
</div>
</span> | [
[
"5\n3 2 4 1 2\n",
"5\n3 2 4 1 2\n"
]
] |
p03311 | AtCoder Regular Contest 100 - Linear Approximation | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Snuke has an integer sequence <var>A</var> of length <var>N</var>.</p>
<p>He will freely choose an integer <var>b</var>.
Here, he will get sad if <var>A_i</var> and <var>b+i</var> are far from each other.
More specifically, the <em>sadness</em> of Snuke is calculated as follows:</p>
<ul>
<li><var>abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))</var></li>
</ul>
<p>Here, <var>abs(x)</var> is a function that returns the absolute value of <var>x</var>.</p>
<p>Find the minimum possible sadness of Snuke.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 2 \times 10^5</var></li>
<li><var>1 \leq A_i \leq 10^9</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum possible sadness of Snuke.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5
2 2 3 5 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>If we choose <var>b=0</var>, the sadness of Snuke would be <var>abs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2</var>.
Any choice of <var>b</var> does not make the sadness of Snuke less than <var>2</var>, so the answer is <var>2</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>9
1 2 3 4 5 6 7 8 9
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>6
6 5 4 3 2 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>18
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>7
1 1 1 1 2 3 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>6
</pre></section>
</div>
</span> | [
[
"5\n2 2 3 5 5\n",
"5\n2 2 3 5 5\n"
]
] |
p03312 | AtCoder Regular Contest 100 - Equal Cut | <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Snuke has an integer sequence <var>A</var> of length <var>N</var>.</p>
<p>He will make three cuts in <var>A</var> and divide it into four (non-empty) contiguous subsequences <var>B, C, D</var> and <var>E</var>.
The positions of the cuts can be freely chosen.</p>
<p>Let <var>P,Q,R,S</var> be the sums of the elements in <var>B,C,D,E</var>, respectively.
Snuke is happier when the absolute difference of the maximum and the minimum among <var>P,Q,R,S</var> is smaller.
Find the minimum possible absolute difference of the maximum and the minimum among <var>P,Q,R,S</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>4 \leq N \leq 2 \times 10^5</var></li>
<li><var>1 \leq A_i \leq 10^9</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Find the minimum possible absolute difference of the maximum and the minimum among <var>P,Q,R,S</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5
3 2 4 1 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>If we divide <var>A</var> as <var>B,C,D,E=(3),(2),(4),(1,2)</var>, then <var>P=3,Q=2,R=4,S=1+2=3</var>.
Here, the maximum and the minimum among <var>P,Q,R,S</var> are <var>4</var> and <var>2</var>, with the absolute difference of <var>2</var>.
We cannot make the absolute difference of the maximum and the minimum less than <var>2</var>, so the answer is <var>2</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>10
10 71 84 33 6 47 23 25 52 64
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>36
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>7
1 2 3 1000000000 4 5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>999999994
</pre></section>
</div>
</span> | [
[
"5\n3 2 4 1 2\n",
"5\n3 2 4 1 2\n"
]
] |
p03313 | AtCoder Regular Contest 100 - Or Plus Max | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There is an integer sequence of length <var>2^N</var>: <var>A_0, A_1, ..., A_{2^N-1}</var>. (Note that the sequence is <var>0</var>-indexed.)</p>
<p>For every integer <var>K</var> satisfying <var>1 \leq K \leq 2^N-1</var>, solve the following problem:</p>
<ul>
<li>Let <var>i</var> and <var>j</var> be integers. Find the maximum value of <var>A_i + A_j</var> where <var>0 \leq i < j \leq 2^N-1</var> and <var>(i</var> <var>or</var> <var>j) \leq K</var>.
Here, <var>or</var> denotes the bitwise OR.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 18</var></li>
<li><var>1 \leq A_i \leq 10^9</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>A_0</var> <var>A_1</var> <var>...</var> <var>A_{2^N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <var>2^N-1</var> lines.
In the <var>i</var>-th line, print the answer of the problem above for <var>K=i</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2
1 2 3 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
4
5
</pre>
<p>For <var>K=1</var>, the only possible pair of <var>i</var> and <var>j</var> is <var>(i,j)=(0,1)</var>, so the answer is <var>A_0+A_1=1+2=3</var>.</p>
<p>For <var>K=2</var>, the possible pairs of <var>i</var> and <var>j</var> are <var>(i,j)=(0,1),(0,2)</var>.
When <var>(i,j)=(0,2)</var>, <var>A_i+A_j=1+3=4</var>. This is the maximum value, so the answer is <var>4</var>.</p>
<p>For <var>K=3</var>, the possible pairs of <var>i</var> and <var>j</var> are <var>(i,j)=(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)</var> .
When <var>(i,j)=(1,2)</var>, <var>A_i+A_j=2+3=5</var>. This is the maximum value, so the answer is <var>5</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
10 71 84 33 6 47 23 25
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>81
94
155
155
155
155
155
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>4
75 26 45 72 81 47 97 97 2 2 25 82 84 17 56 32
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>101
120
147
156
156
178
194
194
194
194
194
194
194
194
194
</pre></section>
</div>
</span> | [
[
"2\n1 2 3 1\n",
"2\n1 2 3 1\n"
]
] |
p03314 | AtCoder Regular Contest 100 - Colorful Sequences | <span class="lang-en">
<p>Score : <var>1100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given integers <var>N, K</var>, and an integer sequence <var>A</var> of length <var>M</var>.</p>
<p>An integer sequence where each element is between <var>1</var> and <var>K</var> (inclusive) is said to be <em>colorful</em> when there exists a contiguous subsequence of length <var>K</var> of the sequence that contains one occurrence of each integer between <var>1</var> and <var>K</var> (inclusive).</p>
<p>For every colorful integer sequence of length <var>N</var>, count the number of the contiguous subsequences of that sequence which coincide with <var>A</var>, then find the sum of all the counts.
Here, the answer can be extremely large, so find the sum modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 25000</var></li>
<li><var>1 \leq K \leq 400</var></li>
<li><var>1 \leq M \leq N</var></li>
<li><var>1 \leq A_i \leq K</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var> <var>M</var>
<var>A_1</var> <var>A_2</var> <var>...</var> <var>A_M</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>For every colorful integer sequence of length <var>N</var>, count the number of the contiguous subsequences of that sequence which coincide with <var>A</var>, then print the sum of all the counts modulo <var>10^9+7</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 2 1
1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>9
</pre>
<p>There are six colorful sequences of length <var>3</var>: <var>(1,1,2)</var>, <var>(1,2,1)</var>, <var>(1,2,2)</var>, <var>(2,1,1)</var>, <var>(2,1,2)</var> and <var>(2,2,1)</var>.
The numbers of the contiguous subsequences of these sequences that coincide with <var>A=(1)</var> are <var>2</var>, <var>2</var>, <var>1</var>, <var>2</var>, <var>1</var> and <var>1</var>, respectively.
Thus, the answer is their sum, <var>9</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4 2 2
1 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>12
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>7 4 5
1 2 3 1 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>17
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>5 4 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>10 3 5
1 1 2 3 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 5</h3><pre>1458
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 6</h3><pre>25000 400 4
3 7 31 127
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 6</h3><pre>923966268
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 7</h3><pre>9954 310 12
267 193 278 294 6 63 86 166 157 193 168 43
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 7</h3><pre>979180369
</pre></section>
</div>
</span> | [
[
"3 2 1\n1\n",
"3 2 1\n1\n"
]
] |
p03315 | AtCoder Beginner Contest 101 - Eating Symbols Easy | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There is always an integer in Takahashi's mind.</p>
<p>Initially, the integer in Takahashi's mind is <var>0</var>. Takahashi is now going to eat four symbols, each of which is <code>+</code> or <code>-</code>. When he eats <code>+</code>, the integer in his mind increases by <var>1</var>; when he eats <code>-</code>, the integer in his mind decreases by <var>1</var>.</p>
<p>The symbols Takahashi is going to eat are given to you as a string <var>S</var>. The <var>i</var>-th character in <var>S</var> is the <var>i</var>-th symbol for him to eat.</p>
<p>Find the integer in Takahashi's mind after he eats all the symbols.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>The length of <var>S</var> is <var>4</var>.</li>
<li>Each character in <var>S</var> is <code>+</code> or <code>-</code>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>S</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the integer in Takahashi's mind after he eats all the symbols.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>+-++
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<ul>
<li>Initially, the integer in Takahashi's mind is <var>0</var>.</li>
<li>The first integer for him to eat is <code>+</code>. After eating it, the integer in his mind becomes <var>1</var>.</li>
<li>The second integer to eat is <code>-</code>. After eating it, the integer in his mind becomes <var>0</var>.</li>
<li>The third integer to eat is <code>+</code>. After eating it, the integer in his mind becomes <var>1</var>.</li>
<li>The fourth integer to eat is <code>+</code>. After eating it, the integer in his mind becomes <var>2</var>.</li>
</ul>
<p>Thus, the integer in Takahashi's mind after he eats all the symbols is <var>2</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>-+--
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>-2
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>----
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>-4
</pre></section>
</div>
</span> | [
[
"+-++\n",
"+-++\n"
]
] |
p03316 | AtCoder Beginner Contest 101 - Digit Sums | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Let <var>S(n)</var> denote the sum of the digits in the decimal notation of <var>n</var>.
For example, <var>S(101) = 1 + 0 + 1 = 2</var>.</p>
<p>Given an integer <var>N</var>, determine if <var>S(N)</var> divides <var>N</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 10^9</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If <var>S(N)</var> divides <var>N</var>, print <code>Yes</code>; if it does not, print <code>No</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>12
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<p>In this input, <var>N=12</var>.
As <var>S(12) = 1 + 2 = 3</var>, <var>S(N)</var> divides <var>N</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>101
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>No
</pre>
<p>As <var>S(101) = 1 + 0 + 1 = 2</var>, <var>S(N)</var> does not divide <var>N</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>999999999
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>Yes
</pre></section>
</div>
</span> | [
[
"12\n",
"12\n"
]
] |
p03317 | AtCoder Beginner Contest 101 - Minimization | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There is a sequence of length <var>N</var>: <var>A_1, A_2, ..., A_N</var>. Initially, this sequence is a permutation of <var>1, 2, ..., N</var>.</p>
<p>On this sequence, Snuke can perform the following operation:</p>
<ul>
<li>Choose <var>K</var> consecutive elements in the sequence. Then, replace the value of each chosen element with the minimum value among the chosen elements.</li>
</ul>
<p>Snuke would like to make all the elements in this sequence equal by repeating the operation above some number of times.
Find the minimum number of operations required.
It can be proved that, Under the constraints of this problem, this objective is always achievable.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq K \leq N \leq 100000</var></li>
<li><var>A_1, A_2, ..., A_N</var> is a permutation of <var>1, 2, ..., N</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of operations required.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4 3
2 3 1 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>One optimal strategy is as follows:</p>
<ul>
<li>
<p>In the first operation, choose the first, second and third elements. The sequence <var>A</var> becomes <var>1, 1, 1, 4</var>.</p>
</li>
<li>
<p>In the second operation, choose the second, third and fourth elements. The sequence <var>A</var> becomes <var>1, 1, 1, 1</var>.</p>
</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 3
1 2 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>8 3
7 3 1 8 4 6 2 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>4
</pre></section>
</div>
</span> | [
[
"4 3\n2 3 1 4\n",
"4 3\n2 3 1 4\n"
]
] |
p03318 | AtCoder Beginner Contest 101 - Snuke Numbers | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Let <var>S(n)</var> denote the sum of the digits in the decimal notation of <var>n</var>.
For example, <var>S(123) = 1 + 2 + 3 = 6</var>.</p>
<p>We will call an integer <var>n</var> a <strong>Snuke number</strong> when, for all positive integers <var>m</var> such that <var>m > n</var>, <var>\frac{n}{S(n)} \leq \frac{m}{S(m)}</var> holds.</p>
<p>Given an integer <var>K</var>, list the <var>K</var> smallest Snuke numbers.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq K</var></li>
<li>The <var>K</var>-th smallest Snuke number is not greater than <var>10^{15}</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>K</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <var>K</var> lines. The <var>i</var>-th line should contain the <var>i</var>-th smallest Snuke number.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>10
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
2
3
4
5
6
7
8
9
19
</pre></section>
</div>
</span> | [
[
"10\n",
"10\n"
]
] |
p03319 | AtCoder Regular Contest 099 - Minimization | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There is a sequence of length <var>N</var>: <var>A_1, A_2, ..., A_N</var>. Initially, this sequence is a permutation of <var>1, 2, ..., N</var>.</p>
<p>On this sequence, Snuke can perform the following operation:</p>
<ul>
<li>Choose <var>K</var> consecutive elements in the sequence. Then, replace the value of each chosen element with the minimum value among the chosen elements.</li>
</ul>
<p>Snuke would like to make all the elements in this sequence equal by repeating the operation above some number of times.
Find the minimum number of operations required.
It can be proved that, Under the constraints of this problem, this objective is always achievable.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq K \leq N \leq 100000</var></li>
<li><var>A_1, A_2, ..., A_N</var> is a permutation of <var>1, 2, ..., N</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of operations required.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4 3
2 3 1 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>One optimal strategy is as follows:</p>
<ul>
<li>
<p>In the first operation, choose the first, second and third elements. The sequence <var>A</var> becomes <var>1, 1, 1, 4</var>.</p>
</li>
<li>
<p>In the second operation, choose the second, third and fourth elements. The sequence <var>A</var> becomes <var>1, 1, 1, 1</var>.</p>
</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 3
1 2 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>8 3
7 3 1 8 4 6 2 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>4
</pre></section>
</div>
</span> | [
[
"4 3\n2 3 1 4\n",
"4 3\n2 3 1 4\n"
]
] |
p03320 | AtCoder Regular Contest 099 - Snuke Numbers | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Let <var>S(n)</var> denote the sum of the digits in the decimal notation of <var>n</var>.
For example, <var>S(123) = 1 + 2 + 3 = 6</var>.</p>
<p>We will call an integer <var>n</var> a <strong>Snuke number</strong> when, for all positive integers <var>m</var> such that <var>m > n</var>, <var>\frac{n}{S(n)} \leq \frac{m}{S(m)}</var> holds.</p>
<p>Given an integer <var>K</var>, list the <var>K</var> smallest Snuke numbers.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq K</var></li>
<li>The <var>K</var>-th smallest Snuke number is not greater than <var>10^{15}</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>K</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <var>K</var> lines. The <var>i</var>-th line should contain the <var>i</var>-th smallest Snuke number.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>10
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
2
3
4
5
6
7
8
9
19
</pre></section>
</div>
</span> | [
[
"10\n",
"10\n"
]
] |
p03321 | AtCoder Regular Contest 099 - Independence | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>In the State of Takahashi in AtCoderian Federation, there are <var>N</var> cities, numbered <var>1, 2, ..., N</var>.
<var>M</var> bidirectional roads connect these cities.
The <var>i</var>-th road connects City <var>A_i</var> and City <var>B_i</var>.
Every road connects two distinct cities.
Also, for any two cities, there is at most one road that <strong>directly</strong> connects them.</p>
<p>One day, it was decided that the State of Takahashi would be divided into two states, Taka and Hashi.
After the division, each city in Takahashi would belong to either Taka or Hashi.
It is acceptable for all the cities to belong Taka, or for all the cities to belong Hashi.
Here, the following condition should be satisfied:</p>
<ul>
<li>Any two cities in the same state, Taka or Hashi, are directly connected by a road.</li>
</ul>
<p>Find the minimum possible number of roads whose endpoint cities belong to the same state.
If it is impossible to divide the cities into Taka and Hashi so that the condition is satisfied, print <code>-1</code>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq N \leq 700</var></li>
<li><var>0 \leq M \leq N(N-1)/2</var></li>
<li><var>1 \leq A_i \leq N</var></li>
<li><var>1 \leq B_i \leq N</var></li>
<li><var>A_i \neq B_i</var></li>
<li>If <var>i \neq j</var>, at least one of the following holds: <var>A_i \neq A_j</var> and <var>B_i \neq B_j</var>.</li>
<li>If <var>i \neq j</var>, at least one of the following holds: <var>A_i \neq B_j</var> and <var>B_i \neq A_j</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>M</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
<var>:</var>
<var>A_M</var> <var>B_M</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the answer.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5 5
1 2
1 3
3 4
3 5
4 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>4
</pre>
<p>For example, if the cities <var>1, 2</var> belong to Taka and the cities <var>3, 4, 5</var> belong to Hashi, the condition is satisfied.
Here, the number of roads whose endpoint cities belong to the same state, is <var>4</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5 1
1 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>-1
</pre>
<p>In this sample, the condition cannot be satisfied regardless of which cities belong to each state.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>4 3
1 2
1 3
2 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>3
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>10 39
7 2
7 1
5 6
5 8
9 10
2 8
8 7
3 10
10 1
8 10
2 3
7 4
3 9
4 10
3 4
6 1
6 7
9 5
9 7
6 9
9 4
4 6
7 5
8 3
2 5
9 2
10 7
8 6
8 9
7 3
5 3
4 5
6 3
2 10
5 10
4 2
6 2
8 4
10 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>21
</pre></section>
</div>
</span> | [
[
"5 5\n1 2\n1 3\n3 4\n3 5\n4 5\n",
"5 5\n1 2\n1 3\n3 4\n3 5\n4 5\n"
]
] |
p03322 | AtCoder Regular Contest 099 - Eating Symbols Hard | <span class="lang-en">
<p>Score : <var>1200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>In Takahashi's mind, there is always an integer sequence of length <var>2 \times 10^9 + 1</var>: <var>A = (A_{-10^9}, A_{-10^9 + 1}, ..., A_{10^9 - 1}, A_{10^9})</var> and an integer <var>P</var>.</p>
<p>Initially, all the elements in the sequence <var>A</var> in Takahashi's mind are <var>0</var>, and the value of the integer <var>P</var> is <var>0</var>.</p>
<p>When Takahashi eats symbols <code>+</code>, <code>-</code>, <code>></code> and <code><</code>, the sequence <var>A</var> and the integer <var>P</var> will change as follows:</p>
<ul>
<li>When he eats <code>+</code>, the value of <var>A_P</var> increases by <var>1</var>;</li>
<li>When he eats <code>-</code>, the value of <var>A_P</var> decreases by <var>1</var>;</li>
<li>When he eats <code>></code>, the value of <var>P</var> increases by <var>1</var>;</li>
<li>When he eats <code><</code>, the value of <var>P</var> decreases by <var>1</var>.</li>
</ul>
<p>Takahashi has a string <var>S</var> of length <var>N</var>. Each character in <var>S</var> is one of the symbols <code>+</code>, <code>-</code>, <code>></code> and <code><</code>.
He chose a pair of integers <var>(i, j)</var> such that <var>1 \leq i \leq j \leq N</var> and ate the symbols that are the <var>i</var>-th, <var>(i+1)</var>-th, <var>...</var>, <var>j</var>-th characters in <var>S</var>, in this order.
We heard that, after he finished eating, the sequence <var>A</var> became the same as if he had eaten all the symbols in <var>S</var> from first to last.
How many such possible pairs <var>(i, j)</var> are there?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 250000</var></li>
<li><var>|S| = N</var></li>
<li>Each character in <var>S</var> is <code>+</code>, <code>-</code>, <code>></code> or <code><</code>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>S</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the answer.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5
+>+<-
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p>If Takahashi eats all the symbols in <var>S</var>, <var>A_1 = 1</var> and all other elements would be <var>0</var>.
The pairs <var>(i, j)</var> that leads to the same sequence <var>A</var> are as follows:</p>
<ul>
<li><var>(1, 5)</var></li>
<li><var>(2, 3)</var></li>
<li><var>(2, 4)</var></li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5
+>+-<
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>5
</pre>
<p>Note that the value of <var>P</var> may be different from the value when Takahashi eats all the symbols in <var>S</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>48
-+><<><><><>>>+-<<>->>><<><<-+<>><+<<>+><-+->><<
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>475
</pre></section>
</div>
</span> | [
[
"5\n+>+<-\n",
"5\n+>+<-\n"
]
] |
p03323 | AtCoder Beginner Contest 100 - Happy Birthday! | <span class="lang-en">
<p>Score: <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3>
<p>E869120's and square1001's <var>16</var>-th birthday is coming soon.<br/>
Takahashi from AtCoder Kingdom gave them a round cake cut into <var>16</var> equal fan-shaped pieces.</p>
<p>E869120 and square1001 were just about to eat <var>A</var> and <var>B</var> of those pieces, respectively,<br/>
when they found a note attached to the cake saying that "the same person should not take two adjacent pieces of cake".</p>
<p>Can both of them obey the instruction in the note and take desired numbers of pieces of cake?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3>
<ul>
<li><var>A</var> and <var>B</var> are integers between <var>1</var> and <var>16</var> (inclusive).</li>
<li><var>A+B</var> is at most <var>16</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3>
<p>Input is given from Standard Input in the following format:</p>
<pre><var>A</var> <var>B</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3>
<p>If both E869120 and square1001 can obey the instruction in the note and take desired numbers of pieces of cake, print <code>Yay!</code>; otherwise, print <code>:(</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yay!
</pre>
<p>Both of them can take desired number of pieces as follows:
<img alt=" " src="https://img.atcoder.jp/abc100/e87fa456a900ac9ae36671ae8bd5eeea.png"/></p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>8 8
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>Yay!
</pre>
<p>Both of them can take desired number of pieces as follows:
<img alt=" " src="https://img.atcoder.jp/abc100/a7989ac033e6ba86e14078864c20d9c5.png"/></p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>11 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>:(
</pre>
<p>In this case, there is no way for them to take desired number of pieces, unfortunately.</p></section>
</div>
</span> | [
[
"5 4\n",
"5 4\n"
]
] |
p03324 | AtCoder Beginner Contest 100 - Ringo's Favorite Numbers | <span class="lang-en">
<p>Score: <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3>
<p>Today, the memorable AtCoder Beginner Contest 100 takes place. On this occasion, Takahashi would like to give an integer to Ringo.<br/>
As the name of the contest is AtCoder Beginner Contest 100, Ringo would be happy if he is given a positive integer that can be divided by <var>100</var> <strong>exactly</strong> <var>D</var> times.</p>
<p>Find the <var>N</var>-th smallest integer that would make Ringo happy.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3>
<ul>
<li><var>D</var> is <var>0</var>, <var>1</var> or <var>2</var>.</li>
<li><var>N</var> is an integer between <var>1</var> and <var>100</var> (inclusive).</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3>
<p>Input is given from Standard Input in the following format:</p>
<pre><var>D</var> <var>N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3>
<p>Print the <var>N</var>-th smallest integer that can be divided by <var>100</var> exactly <var>D</var> times.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>0 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>5
</pre>
<p>The integers that can be divided by <var>100</var> exactly <var>0</var> times (that is, not divisible by <var>100</var>) are as follows: <var>1</var>, <var>2</var>, <var>3</var>, <var>4</var>, <var>5</var>, <var>6</var>, <var>7</var>, ...<br/>
Thus, the <var>5</var>-th smallest integer that would make Ringo happy is <var>5</var>. </p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>1 11
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1100
</pre>
<p>The integers that can be divided by <var>100</var> exactly once are as follows: <var>100</var>, <var>200</var>, <var>300</var>, <var>400</var>, <var>500</var>, <var>600</var>, <var>700</var>, <var>800</var>, <var>900</var>, <var>1 \ 000</var>, <var>1 \ 100</var>, ...<br/>
Thus, the integer we are seeking is <var>1 \ 100</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>2 85
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>850000
</pre>
<p>The integers that can be divided by <var>100</var> exactly twice are as follows: <var>10 \ 000</var>, <var>20 \ 000</var>, <var>30 \ 000</var>, ...<br/>
Thus, the integer we are seeking is <var>850 \ 000</var>.</p></section>
</div>
</span> | [
[
"0 5\n",
"0 5\n"
]
] |
p03325 | AtCoder Beginner Contest 100 - *3 or /2 | <span class="lang-en">
<p>Score: <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3>
<p>As AtCoder Beginner Contest 100 is taking place, the office of AtCoder, Inc. is decorated with a sequence of length <var>N</var>, <var>a = </var>{<var>a_1, a_2, a_3, ..., a_N</var>}.<br/>
Snuke, an employee, would like to play with this sequence.</p>
<p>Specifically, he would like to repeat the following operation as many times as possible:</p>
<pre>For every <var>i</var> satisfying <var>1 \leq i \leq N</var>, perform one of the following: "divide <var>a_i</var> by <var>2</var>" and "multiply <var>a_i</var> by <var>3</var>".
Here, choosing "multiply <var>a_i</var> by <var>3</var>" for every <var>i</var> is not allowed, and the value of <var>a_i</var> after the operation must be an integer.
</pre>
<p>At most how many operations can be performed?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3>
<ul>
<li><var>N</var> is an integer between <var>1</var> and <var>10 \ 000</var> (inclusive).</li>
<li><var>a_i</var> is an integer between <var>1</var> and <var>1 \ 000 \ 000 \ 000</var> (inclusive).</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3>
<p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>a_1</var> <var>a_2</var> <var>a_3</var> <var>...</var> <var>a_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3>
<p>Print the maximum number of operations that Snuke can perform.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
5 2 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p>The sequence is initially <var>{5, 2, 4}</var>. Three operations can be performed as follows:</p>
<ul>
<li>First, multiply <var>a_1</var> by <var>3</var>, multiply <var>a_2</var> by <var>3</var> and divide <var>a_3</var> by <var>2</var>. The sequence is now <var>{15, 6, 2}</var>.</li>
<li>Next, multiply <var>a_1</var> by <var>3</var>, divide <var>a_2</var> by <var>2</var> and multiply <var>a_3</var> by <var>3</var>. The sequence is now <var>{45, 3, 6}</var>.</li>
<li>Finally, multiply <var>a_1</var> by <var>3</var>, multiply <var>a_2</var> by <var>3</var> and divide <var>a_3</var> by <var>2</var>. The sequence is now <var>{135, 9, 3}</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4
631 577 243 199
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>No operation can be performed since all the elements are odd. Thus, the answer is <var>0</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>10
2184 2126 1721 1800 1024 2528 3360 1945 1280 1776
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>39
</pre></section>
</div>
</span> | [
[
"3\n5 2 4\n",
"3\n5 2 4\n"
]
] |
p03326 | AtCoder Beginner Contest 100 - Patisserie ABC | <span class="lang-en">
<p>Score: <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3>
<p>Takahashi became a pastry chef and opened a shop <em>La Confiserie d'ABC</em> to celebrate AtCoder Beginner Contest 100.</p>
<p>The shop sells <var>N</var> kinds of cakes.<br/>
Each kind of cake has three parameters "beauty", "tastiness" and "popularity". The <var>i</var>-th kind of cake has the beauty of <var>x_i</var>, the tastiness of <var>y_i</var> and the popularity of <var>z_i</var>.<br/>
These values may be zero or negative.</p>
<p>Ringo has decided to have <var>M</var> pieces of cakes here. He will choose the set of cakes as follows:</p>
<ul>
<li>Do not have two or more pieces of the same kind of cake.</li>
<li>Under the condition above, choose the set of cakes to maximize (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity).</li>
</ul>
<p>Find the maximum possible value of (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) for the set of cakes that Ringo chooses.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3>
<ul>
<li><var>N</var> is an integer between <var>1</var> and <var>1 \ 000</var> (inclusive).</li>
<li><var>M</var> is an integer between <var>0</var> and <var>N</var> (inclusive).</li>
<li><var>x_i, y_i, z_i \ (1 \leq i \leq N)</var> are integers between <var>-10 \ 000 \ 000 \ 000</var> and <var>10 \ 000 \ 000 \ 000</var> (inclusive).</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3>
<p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>M</var>
<var>x_1</var> <var>y_1</var> <var>z_1</var>
<var>x_2</var> <var>y_2</var> <var>z_2</var>
<var>:</var> <var>:</var>
<var>x_N</var> <var>y_N</var> <var>z_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3>
<p>Print the maximum possible value of (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) for the set of cakes that Ringo chooses.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5 3
3 1 4
1 5 9
2 6 5
3 5 8
9 7 9
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>56
</pre>
<p>Consider having the <var>2</var>-nd, <var>4</var>-th and <var>5</var>-th kinds of cakes. The total beauty, tastiness and popularity will be as follows:</p>
<ul>
<li>Beauty: <var>1 + 3 + 9 = 13</var></li>
<li>Tastiness: <var>5 + 5 + 7 = 17</var></li>
<li>Popularity: <var>9 + 8 + 9 = 26</var></li>
</ul>
<p>The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is <var>13 + 17 + 26 = 56</var>. This is the maximum value.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5 3
1 -2 3
-4 5 -6
7 -8 -9
-10 11 -12
13 -14 15
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>54
</pre>
<p>Consider having the <var>1</var>-st, <var>3</var>-rd and <var>5</var>-th kinds of cakes. The total beauty, tastiness and popularity will be as follows:</p>
<ul>
<li>Beauty: <var>1 + 7 + 13 = 21</var></li>
<li>Tastiness: <var>(-2) + (-8) + (-14) = -24</var></li>
<li>Popularity: <var>3 + (-9) + 15 = 9</var></li>
</ul>
<p>The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is <var>21 + 24 + 9 = 54</var>. This is the maximum value.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>10 5
10 -80 21
23 8 38
-94 28 11
-26 -2 18
-69 72 79
-26 -86 -54
-72 -50 59
21 65 -32
40 -94 87
-62 18 82
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>638
</pre>
<p>If we have the <var>3</var>-rd, <var>4</var>-th, <var>5</var>-th, <var>7</var>-th and <var>10</var>-th kinds of cakes, the total beauty, tastiness and popularity will be <var>-323</var>, <var>66</var> and <var>249</var>, respectively.<br/>
The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is <var>323 + 66 + 249 = 638</var>. This is the maximum value.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>3 2
2000000000 -9000000000 4000000000
7000000000 -5000000000 3000000000
6000000000 -1000000000 8000000000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>30000000000
</pre>
<p>The values of the beauty, tastiness and popularity of the cakes and the value to be printed may not fit into 32-bit integers.</p></section>
</div>
</span> | [
[
"5 3\n3 1 4\n1 5 9\n2 6 5\n3 5 8\n9 7 9\n",
"5 3\n3 1 4\n1 5 9\n2 6 5\n3 5 8\n9 7 9\n"
]
] |
p03327 | AtCoder Beginner Contest 099 - ABD | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Decades have passed since the beginning of AtCoder Beginner Contest.</p>
<p>The contests are labeled as <code>ABC001</code>, <code>ABC002</code>, <var>...</var> from the first round, but after the <var>999</var>-th round <code>ABC999</code>, a problem occurred: how the future rounds should be labeled?</p>
<p>In the end, the labels for the rounds from the <var>1000</var>-th to the <var>1998</var>-th are decided: <code>ABD001</code>, <code>ABD002</code>, <var>...</var>, <code>ABD999</code>.</p>
<p>You are given an integer <var>N</var> between <var>1</var> and <var>1998</var> (inclusive). Print the first three characters of the label of the <var>N</var>-th round of AtCoder Beginner Contest.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 1998</var></li>
<li><var>N</var> is an integer.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the first three characters of the label of the <var>N</var>-th round of AtCoder Beginner Contest.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>999
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>ABC
</pre>
<p>The <var>999</var>-th round of AtCoder Beginner Contest is labeled as <code>ABC999</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>1000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>ABD
</pre>
<p>The <var>1000</var>-th round of AtCoder Beginner Contest is labeled as <code>ABD001</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1481
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>ABD
</pre>
<p>The <var>1481</var>-th round of AtCoder Beginner Contest is labeled as <code>ABD482</code>.</p></section>
</div>
</span> | [
[
"999\n",
"999\n"
]
] |
p03328 | AtCoder Beginner Contest 099 - Stone Monument | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>In some village, there are <var>999</var> towers that are <var>1,(1+2),(1+2+3),...,(1+2+3+...+999)</var> meters high from west to east, at intervals of <var>1</var> meter.</p>
<p>It had been snowing for a while before it finally stopped. For some two adjacent towers located <var>1</var> meter apart, we measured the lengths of the parts of those towers that are not covered with snow, and the results are <var>a</var> meters for the west tower, and <var>b</var> meters for the east tower.</p>
<p>Assuming that the depth of snow cover and the altitude are the same everywhere in the village, find the amount of the snow cover.</p>
<p>Assume also that the depth of the snow cover is always at least <var>1</var> meter.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq a < b < 499500(=1+2+3+...+999)</var></li>
<li>All values in input are integers.</li>
<li>There is no input that contradicts the assumption.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>a</var> <var>b</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If the depth of the snow cover is <var>x</var> meters, print <var>x</var> as an integer.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>8 13
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The heights of the two towers are <var>10</var> meters and <var>15</var> meters, respectively.
Thus, we can see that the depth of the snow cover is <var>2</var> meters.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>54 65
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1
</pre></section>
</div>
</span> | [
[
"8 13\n",
"8 13\n"
]
] |
p03329 | AtCoder Beginner Contest 099 - Strange Bank | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation:</p>
<ul>
<li>
<p><var>1</var> yen (the currency of Japan)</p>
</li>
<li>
<p><var>6</var> yen, <var>6^2(=36)</var> yen, <var>6^3(=216)</var> yen, ...</p>
</li>
<li>
<p><var>9</var> yen, <var>9^2(=81)</var> yen, <var>9^3(=729)</var> yen, ...</p>
</li>
</ul>
<p>At least how many operations are required to withdraw exactly <var>N</var> yen in total?</p>
<p>It is not allowed to re-deposit the money you withdrew.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 100000</var></li>
<li><var>N</var> is an integer.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If at least <var>x</var> operations are required to withdraw exactly <var>N</var> yen in total, print <var>x</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>127
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>4
</pre>
<p>By withdrawing <var>1</var> yen, <var>9</var> yen, <var>36(=6^2)</var> yen and <var>81(=9^2)</var> yen, we can withdraw <var>127</var> yen in four operations.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>3
</pre>
<p>By withdrawing <var>1</var> yen three times, we can withdraw <var>3</var> yen in three operations.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>44852
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>16
</pre></section>
</div>
</span> | [
[
"127\n",
"127\n"
]
] |
p03330 | AtCoder Beginner Contest 099 - Good Grid | <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There is a grid with <var>N</var> rows and <var>N</var> columns of squares. Let <var>(i,j)</var> be the square at the <var>i</var>-th row from the top and the <var>j</var>-th column from the left.</p>
<p>These squares have to be painted in one of the <var>C</var> colors from Color <var>1</var> to Color <var>C</var>. Initially, <var>(i,j)</var> is painted in Color <var>c_{i,j}</var>.</p>
<p>We say the grid is a <em>good</em> grid when the following condition is met for all <var>i,j,x,y</var> satisfying <var>1 \leq i,j,x,y \leq N</var>:</p>
<ul>
<li>If <var>(i+j) \% 3=(x+y) \% 3</var>, the color of <var>(i,j)</var> and the color of <var>(x,y)</var> are the same.</li>
<li>If <var>(i+j) \% 3 \neq (x+y) \% 3</var>, the color of <var>(i,j)</var> and the color of <var>(x,y)</var> are different.</li>
</ul>
<p>Here, <var>X \% Y</var> represents <var>X</var> modulo <var>Y</var>.</p>
<p>We will repaint zero or more squares so that the grid will be a good grid.</p>
<p>For a square, the <em>wrongness</em> when the color of the square is <var>X</var> before repainting and <var>Y</var> after repainting, is <var>D_{X,Y}</var>.</p>
<p>Find the minimum possible sum of the wrongness of all the squares.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 500</var></li>
<li><var>3 \leq C \leq 30</var></li>
<li><var>1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j)</var></li>
<li><var>1 \leq c_{i,j} \leq C</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>C</var>
<var>D_{1,1}</var> <var>...</var> <var>D_{1,C}</var>
<var>:</var>
<var>D_{C,1}</var> <var>...</var> <var>D_{C,C}</var>
<var>c_{1,1}</var> <var>...</var> <var>c_{1,N}</var>
<var>:</var>
<var>c_{N,1}</var> <var>...</var> <var>c_{N,N}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If the minimum possible sum of the wrongness of all the squares is <var>x</var>, print <var>x</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 3
0 1 1
1 0 1
1 4 0
1 2
3 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<ul>
<li>Repaint <var>(1,1)</var> to Color <var>2</var>. The wrongness of <var>(1,1)</var> becomes <var>D_{1,2}=1</var>.</li>
<li>Repaint <var>(1,2)</var> to Color <var>3</var>. The wrongness of <var>(1,2)</var> becomes <var>D_{2,3}=1</var>.</li>
<li>Repaint <var>(2,2)</var> to Color <var>1</var>. The wrongness of <var>(2,2)</var> becomes <var>D_{3,1}=1</var>.</li>
</ul>
<p>In this case, the sum of the wrongness of all the squares is <var>3</var>.</p>
<p>Note that <var>D_{i,j} \neq D_{j,i}</var> is possible.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4 3
0 12 71
81 0 53
14 92 0
1 1 2 1
2 1 1 2
2 2 1 3
1 1 2 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>428
</pre></section>
</div>
</span> | [
[
"2 3\n0 1 1\n1 0 1\n1 4 0\n1 2\n3 3\n",
"2 3\n0 1 1\n1 0 1\n1 4 0\n1 2\n3 3\n"
]
] |
p03331 | AtCoder Grand Contest 025 - Digits Sum | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Takahashi has two positive integers <var>A</var> and <var>B</var>.</p>
<p>It is known that <var>A</var> plus <var>B</var> equals <var>N</var>.
Find the minimum possible value of "the sum of the digits of <var>A</var>" plus "the sum of the digits of <var>B</var>" (in base <var>10</var>).</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 β€ N β€ 10^5</var></li>
<li><var>N</var> is an integer.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum possible value of "the sum of the digits of <var>A</var>" plus "the sum of the digits of <var>B</var>".</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>15
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>6
</pre>
<p>When <var>A=2</var> and <var>B=13</var>, the sums of their digits are <var>2</var> and <var>4</var>, which minimizes the value in question.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>100000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>10
</pre></section>
</div>
</span> | [
[
"15\n",
"15\n"
]
] |
p03332 | AtCoder Grand Contest 025 - RGB Coloring | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Takahashi has a tower which is divided into <var>N</var> layers.
Initially, all the layers are uncolored. Takahashi is going to paint some of the layers in red, green or blue to make a beautiful tower.
He defines the <em>beauty of the tower</em> as follows:</p>
<ul>
<li>The beauty of the tower is the sum of the scores of the <var>N</var> layers, where the score of a layer is <var>A</var> if the layer is painted red, <var>A+B</var> if the layer is painted green, <var>B</var> if the layer is painted blue, and <var>0</var> if the layer is uncolored.</li>
</ul>
<p>Here, <var>A</var> and <var>B</var> are positive integer constants given beforehand. Also note that a layer may not be painted in two or more colors.</p>
<p>Takahashi is planning to paint the tower so that the beauty of the tower becomes exactly <var>K</var>.
How many such ways are there to paint the tower? Find the count modulo <var>998244353</var>.
Two ways to paint the tower are considered different when there exists a layer that is painted in different colors, or a layer that is painted in some color in one of the ways and not in the other.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 β€ N β€ 3Γ10^5</var></li>
<li><var>1 β€ A,B β€ 3Γ10^5</var></li>
<li><var>0 β€ K β€ 18Γ10^{10}</var></li>
<li>All values in the input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>A</var> <var>B</var> <var>K</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of the ways to paint tiles, modulo <var>998244353</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4 1 2 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>40
</pre>
<p>In this case, a red layer worth <var>1</var> points, a green layer worth <var>3</var> points and the blue layer worth <var>2</var> points. The beauty of the tower is <var>5</var> when we have one of the following sets of painted layers:</p>
<ul>
<li><var>1</var> green, <var>1</var> blue</li>
<li><var>1</var> red, <var>2</var> blues</li>
<li><var>2</var> reds, <var>1</var> green</li>
<li><var>3</var> reds, <var>1</var> blue</li>
</ul>
<p>The total number of the ways to produce them is <var>40</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2 5 6 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1
</pre>
<p>The beauty of the tower is <var>0</var> only when all the layers are uncolored. Thus, the answer is <var>1</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>90081 33447 90629 6391049189
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>577742975
</pre></section>
</div>
</span> | [
[
"4 1 2 5\n",
"4 1 2 5\n"
]
] |
p03333 | AtCoder Grand Contest 025 - Interval Game | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Takahashi and Aoki will play a game with a number line and some segments.
Takahashi is standing on the number line and he is initially at coordinate <var>0</var>.
Aoki has <var>N</var> segments. The <var>i</var>-th segment is <var>[L_i,R_i]</var>, that is, a segment consisting of points with coordinates between <var>L_i</var> and <var>R_i</var> (inclusive).</p>
<p>The game has <var>N</var> steps. The <var>i</var>-th step proceeds as follows:</p>
<ul>
<li>First, Aoki chooses a segment that is still not chosen yet from the <var>N</var> segments and tells it to Takahashi.</li>
<li>Then, Takahashi walks along the number line to some point within the segment chosen by Aoki this time.</li>
</ul>
<p>After <var>N</var> steps are performed, Takahashi will return to coordinate <var>0</var> and the game ends.</p>
<p>Let <var>K</var> be the total distance traveled by Takahashi throughout the game. Aoki will choose segments so that <var>K</var> will be as large as possible, and Takahashi walks along the line so that <var>K</var> will be as small as possible. What will be the value of <var>K</var> in the end?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 β€ N β€ 10^5</var></li>
<li><var>-10^5 β€ L_i < R_i β€ 10^5</var></li>
<li><var>L_i</var> and <var>R_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>L_1</var> <var>R_1</var>
:
<var>L_N</var> <var>R_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the total distance traveled by Takahashi throughout the game when Takahashi and Aoki acts as above.
It is guaranteed that <var>K</var> is always an integer when <var>L_i,R_i</var> are integers.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
-5 1
3 7
-4 -2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>10
</pre>
<p>One possible sequence of actions of Takahashi and Aoki is as follows:</p>
<ul>
<li>Aoki chooses the first segment. Takahashi moves from coordinate <var>0</var> to <var>-4</var>, covering a distance of <var>4</var>.</li>
<li>Aoki chooses the third segment. Takahashi stays at coordinate <var>-4</var>.</li>
<li>Aoki chooses the second segment. Takahashi moves from coordinate <var>-4</var> to <var>3</var>, covering a distance of <var>7</var>.</li>
<li>Takahashi moves from coordinate <var>3</var> to <var>0</var>, covering a distance of <var>3</var>.</li>
</ul>
<p>The distance covered by Takahashi here is <var>14</var> (because Takahashi didn't move optimally).
It turns out that if both players move optimally, the distance covered by Takahashi will be <var>10</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
1 2
3 4
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>12
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>5
-2 0
-2 0
7 8
9 10
-2 -1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>34
</pre></section>
</div>
</span> | [
[
"3\n-5 1\n3 7\n-4 -2\n",
"3\n-5 1\n3 7\n-4 -2\n"
]
] |
p03334 | AtCoder Grand Contest 025 - Choosing Points | <span class="lang-en">
<p>Score : <var>800</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Takahashi is doing a research on sets of points in a plane.
Takahashi thinks a set <var>S</var> of points in a coordinate plane is a <em>good set</em> when <var>S</var> satisfies both of the following conditions:</p>
<ul>
<li>The distance between any two points in <var>S</var> is not <var>\sqrt{D_1}</var>.</li>
<li>The distance between any two points in <var>S</var> is not <var>\sqrt{D_2}</var>.</li>
</ul>
<p>Here, <var>D_1</var> and <var>D_2</var> are positive integer constants that Takahashi specified.</p>
<p>Let <var>X</var> be a set of points <var>(i,j)</var> on a coordinate plane where <var>i</var> and <var>j</var> are integers and satisfy <var>0 β€ i,j < 2N</var>.</p>
<p>Takahashi has proved that, for any choice of <var>D_1</var> and <var>D_2</var>, there exists a way to choose <var>N^2</var> points from <var>X</var> so that the chosen points form a good set.
However, he does not know the specific way to choose such points to form a good set.
Find a subset of <var>X</var> whose size is <var>N^2</var> that forms a good set.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 β€ N β€ 300</var></li>
<li><var>1 β€ D_1 β€ 2Γ10^5</var></li>
<li><var>1 β€ D_2 β€ 2Γ10^5</var></li>
<li>All values in the input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>D_1</var> <var>D_2</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <var>N^2</var> distinct points that satisfy the condition in the following format:</p>
<pre><var>x_1</var> <var>y_1</var>
<var>x_2</var> <var>y_2</var>
:
<var>x_{N^2}</var> <var>y_{N^2}</var>
</pre>
<p>Here, <var>(x_i,y_i)</var> represents the <var>i</var>-th chosen point.
<var>0 β€ x_i,y_i < 2N</var> must hold, and they must be integers.
The chosen points may be printed in any order.
In case there are multiple possible solutions, you can output any.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 1 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>0 0
0 2
2 0
2 2
</pre>
<p>Among these points, the distance between <var>2</var> points is either <var>2</var> or <var>2\sqrt{2}</var>, thus it satisfies the condition.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 1 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0 0
0 2
0 4
1 1
1 3
1 5
2 0
2 2
2 4
</pre></section>
</div>
</span> | [
[
"2 1 2\n",
"2 1 2\n"
]
] |
p03335 | AtCoder Grand Contest 025 - Walking on a Tree | <span class="lang-en">
<p>Score : <var>1500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Takahashi loves walking on a tree.
The tree where Takahashi walks has <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 Vertex <var>a_i</var> and Vertex <var>b_i</var>.</p>
<p>Takahashi has scheduled <var>M</var> walks. The <var>i</var>-th walk is done as follows:</p>
<ul>
<li>The walk involves two vertices <var>u_i</var> and <var>v_i</var> that are fixed beforehand.</li>
<li>Takahashi will walk from <var>u_i</var> to <var>v_i</var> or from <var>v_i</var> to <var>u_i</var> along the shortest path.</li>
</ul>
<p>The <em>happiness</em> gained from the <var>i</var>-th walk is defined as follows:</p>
<ul>
<li>The happiness gained is the number of the edges traversed during the <var>i</var>-th walk that satisfies one of the following conditions:<ul>
<li>In the previous walks, the edge has never been traversed.</li>
<li>In the previous walks, the edge has only been traversed in the direction opposite to the direction taken in the <var>i</var>-th walk.</li>
</ul>
</li>
</ul>
<p>Takahashi would like to decide the direction of each walk so that the total happiness gained from the <var>M</var> walks is maximized.
Find the maximum total happiness that can be gained, and one specific way to choose the directions of the walks that maximizes the total happiness.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 β€ N,M β€ 2000</var></li>
<li><var>1 β€ a_i , b_i β€ N</var></li>
<li><var>1 β€ u_i , v_i β€ N</var></li>
<li><var>a_i \neq b_i</var></li>
<li><var>u_i \neq v_i</var></li>
<li>The graph given as input is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>M</var>
<var>a_1</var> <var>b_1</var>
:
<var>a_{N-1}</var> <var>b_{N-1}</var>
<var>u_1</var> <var>v_1</var>
:
<var>u_M</var> <var>v_M</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum total happiness <var>T</var> that can be gained, and one specific way to choose the directions of the walks that maximizes the total happiness, in the following format:</p>
<pre><var>T</var>
<var>u^'_1</var> <var>v^'_1</var>
:
<var>u^'_M</var> <var>v^'_M</var>
</pre>
<p>where (<var>u^'_i</var>,<var>v^'_i</var>) is either (<var>u_i</var>,<var>v_i</var>) or (<var>v_i</var>,<var>u_i</var>), which means that the <var>i</var>-th walk is from vertex <var>u^'_i</var> to <var>v^'_i</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4 3
2 1
3 1
4 1
2 3
3 4
4 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>6
2 3
3 4
4 2
</pre>
<p>If we decide the directions of the walks as above, he gains the happiness of <var>2</var> in each walk, and the total happiness will be <var>6</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5 3
1 2
1 3
3 4
3 5
2 4
3 5
1 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>6
2 4
3 5
5 1
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>6 4
1 2
2 3
1 4
4 5
4 6
2 4
3 6
5 6
4 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>9
2 4
6 3
5 6
4 5
</pre></section>
</div>
</span> | [
[
"4 3\n2 1\n3 1\n4 1\n2 3\n3 4\n4 2\n",
"4 3\n2 1\n3 1\n4 1\n2 3\n3 4\n4 2\n"
]
] |
p03336 | AtCoder Grand Contest 025 - Addition and Andition | <span class="lang-en">
<p>Score : <var>2400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Takahashi and Aoki love calculating things, so they will play with numbers now.</p>
<p>First, they came up with one positive integer each. Takahashi came up with <var>X</var>, and Aoki came up with <var>Y</var>.
Then, they will enjoy themselves by repeating the following operation <var>K</var> times:</p>
<ul>
<li>Compute the bitwise AND of the number currently kept by Takahashi and the number currently kept by Aoki. Let <var>Z</var> be the result.</li>
<li>Then, add <var>Z</var> to both of the numbers kept by Takahashi and Aoki.</li>
</ul>
<p>However, it turns out that even for the two math maniacs this is just too much work.
Could you find the number that would be kept by Takahashi and the one that would be kept by Aoki eventually?</p>
<p>Note that input and output are done in binary.
Especially, <var>X</var> and <var>Y</var> are given as strings <var>S</var> and <var>T</var> of length <var>N</var> and <var>M</var> consisting of <code>0</code> and <code>1</code>, respectively, whose initial characters are guaranteed to be <code>1</code>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 β€ K β€ 10^6</var></li>
<li><var>1 β€ N,M β€ 10^6</var></li>
<li>The initial characters of <var>S</var> and <var>T</var> are <code>1</code>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>M</var> <var>K</var>
<var>S</var>
<var>T</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>In the first line, print the number that would be kept by Takahashi eventually; in the second line, print the number that would be kept by Aoki eventually.
Those numbers should be represented in binary and printed as strings consisting of <code>0</code> and <code>1</code> that begin with <code>1</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 3 3
11
101
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>10000
10010
</pre>
<p>The values of <var>X</var> and <var>Y</var> after each operation are as follows:</p>
<ul>
<li>After the first operation: <var>(X,Y)=(4,6)</var>.</li>
<li>After the second operation: <var>(X,Y)=(8,10)</var>.</li>
<li>After the third operation: <var>(X,Y)=(16,18)</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5 8 3
10101
10101001
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>100000
10110100
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>10 10 10
1100110011
1011001101
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>10000100000010001000
10000100000000100010
</pre></section>
</div>
</span> | [
[
"2 3 3\n11\n101\n",
"2 3 3\n11\n101\n"
]
] |
p03337 | AtCoder Beginner Contest 098 - Add Sub Mul | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given two integers <var>A</var> and <var>B</var>.
Find the largest value among <var>A+B</var>, <var>A-B</var> and <var>A \times B</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>-1000 \leq A,B \leq 1000</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>A</var> <var>B</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the largest value among <var>A+B</var>, <var>A-B</var> and <var>A \times B</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>4
</pre>
<p><var>3+1=4</var>, <var>3-1=2</var> and <var>3 \times 1=3</var>. The largest among them is <var>4</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4 -2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>6
</pre>
<p>The largest is <var>4 - (-2) = 6</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>0 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0
</pre></section>
</div>
</span> | [
[
"3 1\n",
"3 1\n"
]
] |
p03338 | AtCoder Beginner Contest 098 - Cut and Count | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a string <var>S</var> of length <var>N</var> consisting of lowercase English letters.
We will cut this string at one position into two strings <var>X</var> and <var>Y</var>.
Here, we would like to maximize the number of different letters contained in both <var>X</var> and <var>Y</var>.
Find the largest possible number of different letters contained in both <var>X</var> and <var>Y</var> when we cut the string at the optimal position.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq N \leq 100</var></li>
<li><var>|S| = N</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>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 largest possible number of different letters contained in both <var>X</var> and <var>Y</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6
aabbca
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>If we cut the string between the third and fourth letters into <var>X =</var> <code>aab</code> and <var>Y =</var> <code>bca</code>, the letters contained in both <var>X</var> and <var>Y</var> are <code>a</code> and <code>b</code>.
There will never be three or more different letters contained in both <var>X</var> and <var>Y</var>, so the answer is <var>2</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>10
aaaaaaaaaa
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1
</pre>
<p>However we divide <var>S</var>, only <code>a</code> will be contained in both <var>X</var> and <var>Y</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>45
tgxgdqkyjzhyputjjtllptdfxocrylqfqjynmfbfucbir
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>9
</pre></section>
</div>
</span> | [
[
"6\naabbca\n",
"6\naabbca\n"
]
] |
p03339 | AtCoder Beginner Contest 098 - Attention | <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 standing in a row from west to east.
Each person is facing east or west.
The directions of the people is given as a string <var>S</var> of length <var>N</var>.
The <var>i</var>-th person from the west is facing east if <var>S_i =</var> <code>E</code>, and west if <var>S_i =</var> <code>W</code>.</p>
<p>You will appoint one of the <var>N</var> people as the leader, then command the rest of them to face in the direction of the leader.
Here, we do not care which direction the leader is facing.</p>
<p>The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized.
Find the minimum number of people who have to change their directions.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq N \leq 3 \times 10^5</var></li>
<li><var>|S| = N</var></li>
<li><var>S_i</var> is <code>E</code> or <code>W</code>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>S</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of people who have to change their directions.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5
WEEWW
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
</pre>
<p>Assume that we appoint the third person from the west as the leader.
Then, the first person from the west needs to face east and has to turn around.
The other people do not need to change their directions, so the number of people who have to change their directions is <var>1</var> in this case.
It is not possible to have <var>0</var> people who have to change their directions, so the answer is <var>1</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>12
WEWEWEEEWWWE
</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
WWWWWEEE
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>3
</pre></section>
</div>
</span> | [
[
"5\nWEEWW\n",
"5\nWEEWW\n"
]
] |
p03340 | AtCoder Beginner Contest 098 - Xor Sum 2 | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There is an integer sequence <var>A</var> of length <var>N</var>.</p>
<p>Find the number of the pairs of integers <var>l</var> and <var>r</var> (<var>1 \leq l \leq r \leq N</var>) that satisfy the following condition:</p>
<ul>
<li><var>A_l\ xor\ A_{l+1}\ xor\ ...\ xor\ A_r = A_l\ +\ A_{l+1}\ +\ ...\ +\ A_r</var></li>
</ul>
<p>Here, <var>xor</var> denotes the bitwise exclusive OR.</p>
<p><details>
<summary style="display:list-item">Definition of XOR</summary></details></p>
<p>The XOR of integers <var>c_1, c_2, ..., c_m</var> is defined as follows:</p>
<ul>
<li>Let the XOR be <var>X</var>. In the binary representation of <var>X</var>, the digit in the <var>2^k</var>'s place (<var>0 \leq k</var>; <var>k</var> is an integer) is <var>1</var> if there are an odd number of integers among <var>c_1, c_2, ...c_m</var> whose binary representation has <var>1</var> in the <var>2^k</var>'s place, and <var>0</var> if that number is even.</li>
</ul>
<p>For example, let us compute the XOR of <var>3</var> and <var>5</var>. The binary representation of <var>3</var> is <var>011</var>, and the binary representation of <var>5</var> is <var>101</var>, thus the XOR has the binary representation <var>110</var>, that is, the XOR is <var>6</var>.</p>
<p></p></section></div></span> | [
[
"",
""
]
] |
p03341 | AtCoder Regular Contest 098 - Attention | <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 standing in a row from west to east.
Each person is facing east or west.
The directions of the people is given as a string <var>S</var> of length <var>N</var>.
The <var>i</var>-th person from the west is facing east if <var>S_i =</var> <code>E</code>, and west if <var>S_i =</var> <code>W</code>.</p>
<p>You will appoint one of the <var>N</var> people as the leader, then command the rest of them to face in the direction of the leader.
Here, we do not care which direction the leader is facing.</p>
<p>The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized.
Find the minimum number of people who have to change their directions.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq N \leq 3 \times 10^5</var></li>
<li><var>|S| = N</var></li>
<li><var>S_i</var> is <code>E</code> or <code>W</code>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>S</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of people who have to change their directions.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5
WEEWW
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
</pre>
<p>Assume that we appoint the third person from the west as the leader.
Then, the first person from the west needs to face east and has to turn around.
The other people do not need to change their directions, so the number of people who have to change their directions is <var>1</var> in this case.
It is not possible to have <var>0</var> people who have to change their directions, so the answer is <var>1</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>12
WEWEWEEEWWWE
</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
WWWWWEEE
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>3
</pre></section>
</div>
</span> | [
[
"5\nWEEWW\n",
"5\nWEEWW\n"
]
] |
p03342 | AtCoder Regular Contest 098 - Xor Sum 2 | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There is an integer sequence <var>A</var> of length <var>N</var>.</p>
<p>Find the number of the pairs of integers <var>l</var> and <var>r</var> (<var>1 \leq l \leq r \leq N</var>) that satisfy the following condition:</p>
<ul>
<li><var>A_l\ xor\ A_{l+1}\ xor\ ...\ xor\ A_r = A_l\ +\ A_{l+1}\ +\ ...\ +\ A_r</var></li>
</ul>
<p>Here, <var>xor</var> denotes the bitwise exclusive OR.</p>
<p><details>
<summary style="display:list-item">Definition of XOR</summary></details></p>
<p>The XOR of integers <var>c_1, c_2, ..., c_m</var> is defined as follows:</p>
<ul>
<li>Let the XOR be <var>X</var>. In the binary representation of <var>X</var>, the digit in the <var>2^k</var>'s place (<var>0 \leq k</var>; <var>k</var> is an integer) is <var>1</var> if there are an odd number of integers among <var>c_1, c_2, ...c_m</var> whose binary representation has <var>1</var> in the <var>2^k</var>'s place, and <var>0</var> if that number is even.</li>
</ul>
<p>For example, let us compute the XOR of <var>3</var> and <var>5</var>. The binary representation of <var>3</var> is <var>011</var>, and the binary representation of <var>5</var> is <var>101</var>, thus the XOR has the binary representation <var>110</var>, that is, the XOR is <var>6</var>.</p>
<p></p></section></div></span> | [
[
"",
""
]
] |
p03343 | AtCoder Regular Contest 098 - Range Minimum Queries | <span class="lang-en">
<p>Score : <var>600</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> and an integer <var>K</var>.
You will perform the following operation on this sequence <var>Q</var> times:</p>
<ul>
<li>Choose a contiguous subsequence of length <var>K</var>, then remove the smallest element among the <var>K</var> elements contained in the chosen subsequence (if there are multiple such elements, choose one of them as you like).</li>
</ul>
<p>Let <var>X</var> and <var>Y</var> be the values of the largest and smallest element removed in the <var>Q</var> operations. You would like <var>X-Y</var> to be as small as possible.
Find the smallest possible value of <var>X-Y</var> when the <var>Q</var> operations are performed optimally.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 2000</var></li>
<li><var>1 \leq K \leq N</var></li>
<li><var>1 \leq Q \leq N-K+1</var></li>
<li><var>1 \leq A_i \leq 10^9</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var> <var>Q</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 smallest possible value of <var>X-Y</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5 3 2
4 3 1 5 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
</pre>
<p>In the first operation, whichever contiguous subsequence of length <var>3</var> we choose, the minimum element in it is <var>1</var>.
Thus, the first operation removes <var>A_3=1</var> and now we have <var>A=(4,3,5,2)</var>.
In the second operation, it is optimal to choose <var>(A_2,A_3,A_4)=(3,5,2)</var> as the contiguous subsequence of length <var>3</var> and remove <var>A_4=2</var>.
In this case, the largest element removed is <var>2</var>, and the smallest is <var>1</var>, so their difference is <var>2-1=1</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>10 1 6
1 1 2 3 5 8 13 21 34 55
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>7
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>11 7 5
24979445 861648772 623690081 433933447 476190629 262703497 211047202 971407775 628894325 731963982 822804784
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>451211184
</pre></section>
</div>
</span> | [
[
"5 3 2\n4 3 1 5 2\n",
"5 3 2\n4 3 1 5 2\n"
]
] |
p03344 | AtCoder Regular Contest 098 - Donation | <span class="lang-en">
<p>Score : <var>1000</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There is a simple undirected graph with <var>N</var> vertices and <var>M</var> edges.
The vertices are numbered <var>1</var> through <var>N</var>, and the edges are numbered <var>1</var> through <var>M</var>.
Edge <var>i</var> connects Vertex <var>U_i</var> and <var>V_i</var>.
Also, Vertex <var>i</var> has two predetermined integers <var>A_i</var> and <var>B_i</var>.
You will play the following game on this graph.</p>
<p>First, choose one vertex and stand on it, with <var>W</var> yen (the currency of Japan) in your pocket.
Here, <var>A_s \leq W</var> must hold, where <var>s</var> is the vertex you choose.
Then, perform the following two kinds of operations any number of times in any order:</p>
<ul>
<li>Choose one vertex <var>v</var> that is directly connected by an edge to the vertex you are standing on, and move to vertex <var>v</var>. Here, you need to have at least <var>A_v</var> yen in your pocket when you perform this move.</li>
<li>Donate <var>B_v</var> yen to the vertex <var>v</var> you are standing on. Here, the amount of money in your pocket must not become less than <var>0</var> yen.</li>
</ul>
<p>You win the game when you donate once to every vertex.
Find the smallest initial amount of money <var>W</var> that enables you to win the game.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 10^5</var></li>
<li><var>N-1 \leq M \leq 10^5</var></li>
<li><var>1 \leq A_i,B_i \leq 10^9</var></li>
<li><var>1 \leq U_i < V_i \leq N</var></li>
<li>The given graph is connected and simple (there is at most one edge between any pair of vertices).</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>M</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
<var>:</var>
<var>A_N</var> <var>B_N</var>
<var>U_1</var> <var>V_1</var>
<var>U_2</var> <var>V_2</var>
<var>:</var>
<var>U_M</var> <var>V_M</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the smallest initial amount of money <var>W</var> that enables you to win the game.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4 5
3 1
1 2
4 1
6 2
1 2
2 3
2 4
1 4
3 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>6
</pre>
<p>If you have <var>6</var> yen initially, you can win the game as follows:</p>
<ul>
<li>Stand on Vertex <var>4</var>. This is possible since you have not less than <var>6</var> yen.</li>
<li>Donate <var>2</var> yen to Vertex <var>4</var>. Now you have <var>4</var> yen.</li>
<li>Move to Vertex <var>3</var>. This is possible since you have not less than <var>4</var> yen.</li>
<li>Donate <var>1</var> yen to Vertex <var>3</var>. Now you have <var>3</var> yen.</li>
<li>Move to Vertex <var>2</var>. This is possible since you have not less than <var>1</var> yen.</li>
<li>Move to Vertex <var>1</var>. This is possible since you have not less than <var>3</var> yen.</li>
<li>Donate <var>1</var> yen to Vertex <var>1</var>. Now you have <var>2</var> yen.</li>
<li>Move to Vertex <var>2</var>. This is possible since you have not less than <var>1</var> yen.</li>
<li>Donate <var>2</var> yen to Vertex <var>2</var>. Now you have <var>0</var> yen.</li>
</ul>
<p>If you have less than <var>6</var> yen initially, you cannot win the game. Thus, the answer is <var>6</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5 8
6 4
15 13
15 19
15 1
20 7
1 3
1 4
1 5
2 3
2 4
2 5
3 5
4 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>44
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>9 10
131 2
98 79
242 32
231 38
382 82
224 22
140 88
209 70
164 64
6 8
1 6
1 4
1 3
4 7
4 9
3 7
3 9
5 9
2 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>582
</pre></section>
</div>
</span> | [
[
"4 5\n3 1\n1 2\n4 1\n6 2\n1 2\n2 3\n2 4\n1 4\n3 4\n",
"4 5\n3 1\n1 2\n4 1\n6 2\n1 2\n2 3\n2 4\n1 4\n3 4\n"
]
] |
p03345 | AtCoder Grand Contest 024 - Fairness | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Takahashi, Nakahashi and Hikuhashi have integers <var>A</var>, <var>B</var> and <var>C</var>, respectively.
After repeating the following operation <var>K</var> times, find the integer Takahashi will get minus the integer Nakahashi will get:</p>
<ul>
<li>Each of them simultaneously calculate the sum of the integers that the other two people have, then replace his own integer with the result.</li>
</ul>
<p>However, if the absolute value of the answer exceeds <var>10^{18}</var>, print <code>Unfair</code> instead.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq A,B,C \leq 10^9</var></li>
<li><var>0 \leq K \leq 10^{18}</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>A</var> <var>B</var> <var>C</var> <var>K</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the integer Takahashi will get minus the integer Nakahashi will get, after repeating the following operation <var>K</var> times.
If the absolute value of the answer exceeds <var>10^{18}</var>, print <code>Unfair</code> instead.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>1 2 3 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
</pre>
<p>After one operation, Takahashi, Nakahashi and Hikuhashi have <var>5</var>, <var>4</var> and <var>3</var>, respectively. We should print <var>5-4=1</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2 3 2 0
</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>1000000000 1000000000 1000000000 1000000000000000000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0
</pre></section>
</div>
</span> | [
[
"1 2 3 1\n",
"1 2 3 1\n"
]
] |
p03346 | AtCoder Grand Contest 024 - Backfront | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a sequence <var>(P_1,P_2,...,P_N)</var> which is a permutation of the integers from <var>1</var> through <var>N</var>.
You would like to sort this sequence in ascending order by repeating the following operation:</p>
<ul>
<li>Choose an element in the sequence and move it to the beginning or the end of the sequence.</li>
</ul>
<p>Find the minimum number of operations required.
It can be proved that it is actually possible to sort the sequence using this operation.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 2\times 10^5</var></li>
<li><var>(P_1,P_2,...,P_N)</var> is a permutation of <var>(1,2,...,N)</var>.</li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>P_1</var>
<var>:</var>
<var>P_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of operations required.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4
1
3
2
4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>For example, the sequence can be sorted in ascending order as follows:</p>
<ul>
<li>Move <var>2</var> to the beginning. The sequence is now <var>(2,1,3,4)</var>.</li>
<li>Move <var>1</var> to the beginning. The sequence is now <var>(1,2,3,4)</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6
3
2
5
1
4
6
</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
6
3
1
2
7
4
8
5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>5
</pre></section>
</div>
</span> | [
[
"4\n1\n3\n2\n4\n",
"4\n1\n3\n2\n4\n"
]
] |
p03347 | AtCoder Grand Contest 024 - Sequence Growing Easy | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There is a sequence <var>X</var> of length <var>N</var>, where every element is initially <var>0</var>. Let <var>X_i</var> denote the <var>i</var>-th element of <var>X</var>.</p>
<p>You are given a sequence <var>A</var> of length <var>N</var>. The <var>i</var>-th element of <var>A</var> is <var>A_i</var>. Determine if we can make <var>X</var> equal to <var>A</var> by repeating the operation below. If we can, find the minimum number of operations required.</p>
<ul>
<li>Choose an integer <var>i</var> such that <var>1\leq i\leq N-1</var>. Replace the value of <var>X_{i+1}</var> with the value of <var>X_i</var> plus <var>1</var>.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 2 \times 10^5</var></li>
<li><var>0 \leq A_i \leq 10^9(1\leq i\leq N)</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>A_1</var>
<var>:</var>
<var>A_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If we can make <var>X</var> equal to <var>A</var> by repeating the operation, print the minimum number of operations required. If we cannot, print <var>-1</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4
0
1
1
2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p>We can make <var>X</var> equal to <var>A</var> as follows:</p>
<ul>
<li>Choose <var>i=2</var>. <var>X</var> becomes <var>(0,0,1,0)</var>.</li>
<li>Choose <var>i=1</var>. <var>X</var> becomes <var>(0,1,1,0)</var>.</li>
<li>Choose <var>i=3</var>. <var>X</var> becomes <var>(0,1,1,2)</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
1
2
1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>-1
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>9
0
1
1
0
1
2
2
1
2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>8
</pre></section>
</div>
</span> | [
[
"4\n0\n1\n1\n2\n",
"4\n0\n1\n1\n2\n"
]
] |
p03348 | AtCoder Grand Contest 024 - Isomorphism Freak | <span class="lang-en">
<p>Score : <var>1100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Coloring of the vertices of a tree <var>G</var> is called a <em>good coloring</em> when, for every pair of two vertices <var>u</var> and <var>v</var> painted in the same color, picking <var>u</var> as the root and picking <var>v</var> as the root would result in isomorphic rooted trees.</p>
<p>Also, the <em>colorfulness</em> of <var>G</var> is defined as the minimum possible number of different colors used in a good coloring of <var>G</var>.</p>
<p>You are given a tree with <var>N</var> vertices. The vertices are numbered <var>1</var> through <var>N</var>, and the <var>i</var>-th edge connects Vertex <var>a_i</var> and Vertex <var>b_i</var>. We will construct a new tree <var>T</var> by repeating the following operation on this tree some number of times:</p>
<ul>
<li>Add a new vertex to the tree by connecting it to one of the vertices in the current tree with an edge.</li>
</ul>
<p>Find the minimum possible colorfulness of <var>T</var>.
Additionally, print the minimum number of leaves (vertices with degree <var>1</var>) in a tree <var>T</var> that achieves the minimum colorfulness.</p>
</section>
</div>
<div class="part">
<section>
<h3>Notes</h3><p>The phrase "picking <var>u</var> as the root and picking <var>v</var> as the root would result in isomorphic rooted trees" for a tree <var>G</var> means that there exists a bijective function <var>f_{uv}</var> from the vertex set of <var>G</var> to itself such that both of the following conditions are met:</p>
<ul>
<li><var>f_{uv}(u)=v</var></li>
<li>For every pair of two vertices <var>(a,b)</var>, edge <var>(a,b)</var> exists if and only if edge <var>(f_{uv}(a),f_{uv}(b))</var> exists.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq N \leq 100</var></li>
<li><var>1 \leq a_i,b_i \leq N(1\leq i\leq 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>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>a_1</var> <var>b_1</var>
<var>:</var>
<var>a_{N-1}</var> <var>b_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print two integers with a space in between.
First, print the minimum possible colorfulness of a tree <var>T</var> that can be constructed.
Second, print the minimum number of leaves in a tree that achieves it.</p>
<p>It can be shown that, under the constraints of this problem, the values that should be printed fit into <var>64</var>-bit signed integers.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5
1 2
2 3
3 4
3 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2 4
</pre>
<p>If we connect a new vertex <var>6</var> to vertex <var>2</var>, painting the vertices <var>(1,4,5,6)</var> red and painting the vertices <var>(2,3)</var> blue is a good coloring.
Since painting all the vertices in a single color is not a good coloring, we can see that the colorfulness of this tree is <var>2</var>.
This is actually the optimal solution. There are four leaves, so we should print <var>2</var> and <var>4</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>8
1 2
2 3
4 3
5 4
6 7
6 8
3 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>3 4
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>10
1 2
2 3
3 4
4 5
5 6
6 7
3 8
5 9
3 10
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>4 6
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>13
5 6
6 4
2 8
4 7
8 9
3 2
10 4
11 10
2 4
13 10
1 8
12 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>4 12
</pre></section>
</div>
</span> | [
[
"5\n1 2\n2 3\n3 4\n3 5\n",
"5\n1 2\n2 3\n3 4\n3 5\n"
]
] |
p03349 | AtCoder Grand Contest 024 - Sequence Growing Hard | <span class="lang-en">
<p>Score : <var>1200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Find the number of the possible tuples of sequences <var>(A_0,A_1,...,A_N)</var> that satisfy all of the following conditions, modulo <var>M</var>:</p>
<ul>
<li>For every <var>i</var> <var>(0\leq i\leq N)</var>, <var>A_i</var> is a sequence of length <var>i</var> consisting of integers between <var>1</var> and <var>K</var> (inclusive);</li>
<li>For every <var>i</var> <var>(1\leq i\leq N)</var>, <var>A_{i-1}</var> is a subsequence of <var>A_i</var>, that is, there exists <var>1\leq x_i\leq i</var> such that the removal of the <var>x_i</var>-th element of <var>A_i</var> would result in a sequence equal to <var>A_{i-1}</var>;</li>
<li>For every <var>i</var> <var>(1\leq i\leq N)</var>, <var>A_i</var> is lexicographically larger than <var>A_{i-1}</var>.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N,K \leq 300</var></li>
<li><var>2 \leq M \leq 10^9</var></li>
<li><var>N</var>, <var>K</var> and <var>M</var> are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var> <var>M</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of the possible tuples of sequences <var>(A_0,A_1,...,A_N)</var>, modulo <var>M</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 2 100
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>5
</pre>
<p>Five tuples below satisfy the conditions:</p>
<ul>
<li><var>(),(1),(1,1)</var></li>
<li><var>(),(1),(1,2)</var></li>
<li><var>(),(1),(2,1)</var></li>
<li><var>(),(2),(2,1)</var></li>
<li><var>(),(2),(2,2)</var></li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4 3 999999999
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>358
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>150 150 998244353
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>186248260
</pre></section>
</div>
</span> | [
[
"2 2 100\n",
"2 2 100\n"
]
] |
p03350 | AtCoder Grand Contest 024 - Simple Subsequence Problem | <span class="lang-en">
<p>Score : <var>2300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a set <var>S</var> of strings consisting of <code>0</code> and <code>1</code>, and an integer <var>K</var>.</p>
<p>Find the longest string that is a subsequence of <var>K</var> or more different strings in <var>S</var>.
If there are multiple strings that satisfy this condition, find the lexicographically smallest such string.</p>
<p>Here, <var>S</var> is given in the format below:</p>
<ul>
<li>The data directly given to you is an integer <var>N</var>, and <var>N+1</var> strings <var>X_0,X_1,...,X_N</var>. For every <var>i</var> <var>(0\leq i\leq N)</var>, the length of <var>X_i</var> is <var>2^i</var>.</li>
<li>For every pair of two integers <var>(i,j)</var> <var>(0\leq i\leq N,0\leq j\leq 2^i-1)</var>, the <var>j</var>-th character of <var>X_i</var> is <code>1</code> if and only if the binary representation of <var>j</var> with <var>i</var> digits (possibly with leading zeros) belongs to <var>S</var>. Here, the first and last characters in <var>X_i</var> are called the <var>0</var>-th and <var>(2^i-1)</var>-th characters, respectively.</li>
<li><var>S</var> does not contain a string with length <var>N+1</var> or more.</li>
</ul>
<p>Here, a string <var>A</var> is a subsequence of another string <var>B</var> when there exists a sequence of integers <var>t_1 < ... < t_{|A|}</var> such that, for every <var>i</var> <var>(1\leq i\leq |A|)</var>, the <var>i</var>-th character of <var>A</var> and the <var>t_i</var>-th character of <var>B</var> is equal.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>0 \leq N \leq 20</var></li>
<li><var>X_i(0\leq i\leq N)</var> is a string of length <var>2^i</var> consisting of <code>0</code> and <code>1</code>.</li>
<li><var>1 \leq K \leq |S|</var></li>
<li><var>K</var> is an integer.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>X_0</var>
<var>:</var>
<var>X_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the lexicographically smallest string among the longest strings that are subsequences of <var>K</var> or more different strings in <var>S</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 4
1
01
1011
01001110
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>10
</pre>
<p>The following strings belong to <var>S</var>: the empty string, <code>1</code>, <code>00</code>, <code>10</code>, <code>11</code>, <code>001</code>, <code>100</code>, <code>101</code> and <code>110</code>.
The lexicographically smallest string among the longest strings that are subsequences of four or more of them is <code>10</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4 6
1
01
1011
10111010
1101110011111101
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>100
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>2 5
0
11
1111
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>
</pre>
<p>The answer is the empty string.</p></section>
</div>
</span> | [
[
"3 4\n1\n01\n1011\n01001110\n",
"3 4\n1\n01\n1011\n01001110\n"
]
] |
p03351 | AtCoder Beginner Contest 097 - Colorful Transceivers | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Three people, A, B and C, are trying to communicate using transceivers.
They are standing along a number line, and the coordinates of A, B and C are <var>a</var>, <var>b</var> and <var>c</var> (in meters), respectively.
Two people can directly communicate when the distance between them is at most <var>d</var> meters.
Determine if A and C can communicate, either directly or indirectly.
Here, A and C can indirectly communicate when A and B can directly communicate and also B and C can directly communicate.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1</var> <var>β€</var> <var>a,b,c</var> <var>β€</var> <var>100</var></li>
<li><var>1</var> <var>β€</var> <var>d</var> <var>β€</var> <var>100</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>a</var> <var>b</var> <var>c</var> <var>d</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If A and C can communicate, print <code>Yes</code>; if they cannot, print <code>No</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4 7 9 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<p>A and B can directly communicate, and also B and C can directly communicate, so we should print <code>Yes</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>100 10 1 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>No
</pre>
<p>They cannot communicate in this case.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>10 10 10 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>Yes
</pre>
<p>There can be multiple people at the same position.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>1 100 2 10
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>Yes
</pre></section>
</div>
</span> | [
[
"4 7 9 3\n",
"4 7 9 3\n"
]
] |
p03352 | AtCoder Beginner Contest 097 - Exponential | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a positive integer <var>X</var>.
Find the largest <em>perfect power</em> that is at most <var>X</var>.
Here, a perfect power is an integer that can be represented as <var>b^p</var>, where <var>b</var> is an integer not less than <var>1</var> and <var>p</var> is an integer not less than <var>2</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1</var> <var>β€</var> <var>X</var> <var>β€</var> <var>1000</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>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 largest perfect power that is at most <var>X</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>10
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>9
</pre>
<p>There are four perfect powers that are at most <var>10</var>: <var>1</var>, <var>4</var>, <var>8</var> and <var>9</var>.
We should print the largest among them, <var>9</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>999
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>961
</pre></section>
</div>
</span> | [
[
"10\n",
"10\n"
]
] |
p03353 | AtCoder Beginner Contest 097 - K-th Substring | <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>.
Among the <strong>different</strong> substrings of <var>s</var>, print the <var>K</var>-th lexicographically smallest one.</p>
<p>A substring of <var>s</var> is a string obtained by taking out a non-empty contiguous part in <var>s</var>.
For example, if <var>s</var> <var>=</var> <code>ababc</code>, <code>a</code>, <code>bab</code> and <code>ababc</code> are substrings of <var>s</var>, while <code>ac</code>, <code>z</code> and an empty string are not.
Also, we say that substrings are different when they are different as strings.</p>
<p>Let <var>X = x_{1}x_{2}...x_{n}</var> and <var>Y = y_{1}y_{2}...y_{m}</var> be two distinct strings. <var>X</var> is lexicographically larger than <var>Y</var> if and only if <var>Y</var> is a prefix of <var>X</var> or <var>x_{j} > y_{j}</var> where <var>j</var> is the smallest integer such that <var>x_{j} \neq y_{j}</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1</var> <var>β€</var> <var>|s|</var> <var>β€</var> <var>5000</var></li>
<li><var>s</var> consists of lowercase English letters.</li>
<li><var>1</var> <var>β€</var> <var>K</var> <var>β€</var> <var>5</var></li>
<li><var>s</var> has at least <var>K</var> different substrings.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Partial Score</h3><ul>
<li><var>200</var> points will be awarded as a partial score for passing the test set satisfying <var>|s|</var> <var>β€</var> <var>50</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>s</var>
<var>K</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the <var>K</var>-th lexicographically smallest substring of <var>K</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>aba
4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>b
</pre>
<p><var>s</var> has five substrings: <code>a</code>, <code>b</code>, <code>ab</code>, <code>ba</code> and <code>aba</code>.
Among them, we should print the fourth smallest one, <code>b</code>.
Note that we do not count <code>a</code> twice.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>atcoderandatcodeer
5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>andat
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>z
1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>z
</pre></section>
</div>
</span> | [
[
"aba\n4\n",
"aba\n4\n"
]
] |
p03354 | AtCoder Beginner Contest 097 - Equals | <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a permutation of the integers from <var>1</var> through <var>N</var>, <var>p_1</var>, <var>p_2</var>, .., <var>p_N</var>.
We also have <var>M</var> pairs of two integers between <var>1</var> and <var>N</var> (inclusive), represented as <var>(x_1,y_1)</var>, <var>(x_2,y_2)</var>, .., <var>(x_M,y_M)</var>.
AtCoDeer the deer is going to perform the following operation on <var>p</var> as many times as desired so that the number of <var>i</var> (<var>1</var> <var>β€</var> <var>i</var> <var>β€</var> <var>N</var>) such that <var>p_i = i</var> is maximized:</p>
<ul>
<li>Choose <var>j</var> such that <var>1</var> <var>β€</var> <var>j</var> <var>β€</var> <var>M</var>, and swap <var>p_{x_j}</var> and <var>p_{y_j}</var>.</li>
</ul>
<p>Find the maximum possible number of <var>i</var> such that <var>p_i = i</var> after operations.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2</var> <var>β€</var> <var>N</var> <var>β€</var> <var>10^5</var></li>
<li><var>1</var> <var>β€</var> <var>M</var> <var>β€</var> <var>10^5</var></li>
<li><var>p</var> is a permutation of integers from <var>1</var> through <var>N</var>.</li>
<li><var>1</var> <var>β€</var> <var>x_j,y_j</var> <var>β€</var> <var>N</var></li>
<li><var>x_j</var> <var>β </var> <var>y_j</var></li>
<li>If <var>i</var> <var>β </var> <var>j</var>, <var>\{x_i,y_i\}</var> <var>β </var> <var>\{x_j,y_j\}</var>.</li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>M</var>
<var>p_1</var> <var>p_2</var> <var>..</var> <var>p_N</var>
<var>x_1</var> <var>y_1</var>
<var>x_2</var> <var>y_2</var>
<var>:</var>
<var>x_M</var> <var>y_M</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum possible number of <var>i</var> such that <var>p_i = i</var> after operations.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5 2
5 3 1 4 2
1 3
5 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>If we perform the operation by choosing <var>j=1</var>, <var>p</var> becomes <code>1 3 5 4 2</code>, which is optimal, so the answer is <var>2</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 2
3 2 1
1 2
2 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>3
</pre>
<p>If we perform the operation by, for example, choosing <var>j=1</var>, <var>j=2</var>, <var>j=1</var> in this order, <var>p</var> becomes <code>1 2 3</code>, which is obviously optimal.
Note that we may choose the same <var>j</var> any number of times.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>10 8
5 3 6 8 7 10 9 1 2 4
3 1
4 1
5 9
2 5
6 5
3 5
8 9
7 9
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>8
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>5 1
1 2 3 4 5
1 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>5
</pre>
<p>We do not have to perform the operation. </p></section>
</div>
</span> | [
[
"5 2\n5 3 1 4 2\n1 3\n5 4\n",
"5 2\n5 3 1 4 2\n1 3\n5 4\n"
]
] |
p03355 | AtCoder Regular Contest 097 - K-th Substring | <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>.
Among the <strong>different</strong> substrings of <var>s</var>, print the <var>K</var>-th lexicographically smallest one.</p>
<p>A substring of <var>s</var> is a string obtained by taking out a non-empty contiguous part in <var>s</var>.
For example, if <var>s</var> <var>=</var> <code>ababc</code>, <code>a</code>, <code>bab</code> and <code>ababc</code> are substrings of <var>s</var>, while <code>ac</code>, <code>z</code> and an empty string are not.
Also, we say that substrings are different when they are different as strings.</p>
<p>Let <var>X = x_{1}x_{2}...x_{n}</var> and <var>Y = y_{1}y_{2}...y_{m}</var> be two distinct strings. <var>X</var> is lexicographically larger than <var>Y</var> if and only if <var>Y</var> is a prefix of <var>X</var> or <var>x_{j} > y_{j}</var> where <var>j</var> is the smallest integer such that <var>x_{j} \neq y_{j}</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1</var> <var>β€</var> <var>|s|</var> <var>β€</var> <var>5000</var></li>
<li><var>s</var> consists of lowercase English letters.</li>
<li><var>1</var> <var>β€</var> <var>K</var> <var>β€</var> <var>5</var></li>
<li><var>s</var> has at least <var>K</var> different substrings.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Partial Score</h3><ul>
<li><var>200</var> points will be awarded as a partial score for passing the test set satisfying <var>|s|</var> <var>β€</var> <var>50</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>s</var>
<var>K</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the <var>K</var>-th lexicographically smallest substring of <var>K</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>aba
4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>b
</pre>
<p><var>s</var> has five substrings: <code>a</code>, <code>b</code>, <code>ab</code>, <code>ba</code> and <code>aba</code>.
Among them, we should print the fourth smallest one, <code>b</code>.
Note that we do not count <code>a</code> twice.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>atcoderandatcodeer
5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>andat
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>z
1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>z
</pre></section>
</div>
</span> | [
[
"aba\n4\n",
"aba\n4\n"
]
] |