question_id stringlengths 6 6 | content stringlengths 1 27.2k |
|---|---|
p02066 | <h2>G: トレジャーハンター (Treasure Hunter)</h2>
<h3>問題</h3>
<p>財宝の山が <var>N</var> 個あり、それぞれの山は <var>1</var> から <var>N</var> までで番号づけられている。<var>i</var> 番目の山には価値 <var>p_i</var> のお宝が眠っており、このお宝はその山に訪れた時点で得ることができる。一度お宝を得るとその山からはお宝がなくなってしまうので、お宝はそれぞれ一度しか得ることはできない。</p>
<p>異なる山に移動するには必ず道を利用しなければならない。山と山の間には合計 <var>N-1</var> 本の道があり、<var>i</var> 番目の道は山 <var>u_i</var> と <var>v_i</var> を双方向に結んでいる。もしもどの道も問題なく通行可能であると仮定すると、任意の <var>2</var> つの山について相互に行き来可能であることが分かっている。</p>
<p>どの道も長らく誰も通行しておらず修復しないと渡れないため、<var>i</var> 番目の道について最初に渡る際には <var>c_i</var> 円を支払って工事し、通行可能にする必要がある。一度工事した道であればそれ以上お金を支払うことなく通行が可能である。</p>
<p>トレジャーハンターであるあなたは、道の工事費用の予算を <var>W</var> 円持っている。あなたの目的は、はじめに降り立つ山を任意に <var>1</var> つ決め、合計 <var>W</var> 円以下で道を工事して異なる山に移動することで、得られるお宝の価値の合計を最大化することである。あなたは最大でどれだけの価値を得られるだろうか?</p>
<p>なお、お宝を途中で換金することはできないため、お宝を工事費用として使用することはできないことに注意せよ。</p>
<h3>入力形式</h3>
<p>入力は以下の形式で与えられる。</p>
<pre>
<var>N</var> <var>W</var>
<var>p_1</var> <var>p_2</var> <var>...</var> <var>p_N</var>
<var>u_1</var> <var>v_1</var> <var>c_1</var>
<var>:</var>
<var>u_{N-1}</var> <var>v_{N-1}</var> <var>c_{N-1}</var>
</pre>
<ul>
<li><var>1</var> 行目では、山の数 <var>N</var> と工事費用の予算 <var>W</var> が与えられる。</li>
<li><var>2</var> 行目では、<var>i</var> 番目の山に眠っているお宝の価値 <var>p_i</var> が与えられる。</li>
<li><var>3</var> 行目から <var>N+1</var> 行目では、道の情報が与えられる。<var>2+i</var> 行目は <var>i</var> 番目の道の情報を表しており、山 <var>u_i</var> と <var>v_i</var> の間に工事費用 <var>c_i</var> の道があることを表す。</li>
</ul>
<h3>制約</h3>
<ul>
<li><var>1 \leq N \leq 10^2</var></li>
<li><var>1 \leq W \leq 10^5</var></li>
<li><var>1 \leq p_i \leq 10^9</var></li>
<li><var>1 \leq u_i \lt v_i \leq N</var></li>
<li><var>i \neq j</var> ならば <var>u_i \neq u_j</var> または <var>v_i \neq v_j</var></li>
<li><var>1 \leq c_i \leq 10^5</var></li>
<li>与えられる入力は全て整数である</li>
</ul>
<h3>出力形式</h3>
<p>得られるお宝の価値の合計の最大値を <var>1</var> 行に出力せよ。末尾の改行を忘れないこと。</p>
<h3>入力例 1</h3>
<pre>
3 10
6 8 2
1 2 3
2 3 8
</pre>
<h3>出力例 1</h3>
<pre>14</pre>
<ul>
<li>山 <var>1</var> または山 <var>2</var> に降り立ち、山 <var>1</var> と山 <var>2</var> を結ぶ道を工事し、山 <var>1</var> と山 <var>2</var> にあるお宝を得るのが最適である。工事費用の予算は <var>10</var> であるので、ふたつの道を両方とも工事することはできない。</li>
</ul>
<h3>入力例 2</h3>
<pre>
3 15
10 10 12
1 2 6
1 3 4
</pre>
<h3>出力例 2</h3>
<pre>32</pre>
<ul>
<li>お宝を全て得ることができる。</li>
</ul>
<h3>入力例 3</h3>
<pre>
5 1
4 8 8 2 10
1 2 3
2 4 5
2 5 2
1 3 7
</pre>
<h3>出力例 3</h3>
<pre>10</pre>
<ul>
<li>予算が不足しており、どの道も工事できない場合もある。</li>
</ul>
|
p02088 |
<h2>A: Union Ball</h2>
<h3>Problem Statement</h3>
<p>There are <var>N</var> balls in a box. The <var>i</var>-th ball is labeled with a positive integer <var>A_i</var>.</p>
<p>You can interact with balls in the box by taking actions under the following rules:</p>
<ul>
<li> If integers on balls in the box are all odd or all even, you cannot take actions anymore.</li>
<li> Otherwise, you select arbitrary two balls in the box and remove them from the box. Then, you generate a new ball labeled with the sum of the integers on the two balls and put it into the box.</li>
</ul>
<p>For given balls, what is the maximum number of actions you can take under the above rules?</p>
<h3>Input</h3>
<pre>
<var>N</var>
<var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var>
</pre>
<ul>
<li> The first line gives an integer <var>N</var> representing the initial number of balls in a box.</li>
<li> The second line contains <var>N</var> integers, the <var>i</var>-th of which is the integer on the <var>i</var>-th ball.</li>
</ul>
<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> Inputs consist only of integers.</li>
</ul>
<h3>Output</h3>
<p>Output the maximum number of actions you can take in one line.</p>
<h3>Sample Input 1</h3>
<pre>
3
4 5 6
</pre>
<h3>Output for Sample Input 1</h3>
<pre>2</pre>
<p>
First, you select and remove balls labeled with <var>4</var> and <var>5</var>, respectively, and add a ball labeled with <var>9</var>.
Next, you select and remove balls labeled with <var>6</var> and <var>9</var>, respectively, and add a ball labeled with <var>15</var>.
Now, the balls in the box only have odd numbers. So you cannot take any actions anymore. The number of actions you took is two, and there is no way to achieve three actions or more. Thus the maximum is two and the series of the above actions is one of the optimal ways.
</p>
<h3>Sample Input 2</h3>
<pre>
4
4 2 4 2
</pre>
<h3>Output for Sample Input 2</h3>
<pre>0</pre>
<p>You cannot take any actions in this case.</p>
|
p02572 | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given are <var>N</var> integers <var>A_1,\ldots,A_N</var>.</p>
<p>Find the sum of <var>A_i \times A_j</var> over all pairs <var>(i,j)</var> such that <var>1\leq i < j \leq N</var>, modulo <var>(10^9+7)</var>.</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>0 \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>\ldots</var> <var>A_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <var>\sum_{i=1}^{N-1}\sum_{j=i+1}^{N} A_i A_j</var>, modulo <var>(10^9+7)</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
1 2 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>11
</pre>
<p>We have <var>1 \times 2 + 1 \times 3 + 2 \times 3 = 11</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4
141421356 17320508 22360679 244949
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>437235829
</pre></section>
</div>
</span> |
p02122 |
<script type="text/x-mathjax-config">
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});
</script>
<script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML">
</script>
<h1>Problem L: RMQ 2</h1>
<h2>Problem</h2>
<p>長さ$N$の2つの数列$A$と$B$が与えられる。はじめ、数列$A$の$i$項目は$a_i$であり、数列$B$の$i$項目は$b_i$である。</p>
<p>以下のような形式の命令文が合計$Q$個与えられるので、与えられた順に処理を行うプログラムを作成せよ。</p>
<p>
各命令文は3つの整数$x,y,z$で表される。
</p>
<ul>
<li>数列$A$の$y$項目の値を$z$にする。 ($x=1$のとき)</li>
<li>数列$B$の$y$項目の値を$z$にする。 ($x=2$のとき)</li>
<li>数列$A$の$y$項目から$z$項目の中で最小の値を見つけて報告する。 ($x=3$のとき)</li>
<li>数列$B$の$y$項目から$z$項目の中で最小の値を見つけて報告する。 ($x=4$のとき)</li>
<li>数列$A$を、数列$B$と全く同じになるように変更する。 ($x=5$のとき)</li>
<li>数列$B$を、数列$A$と全く同じになるように変更する。 ($x=6$のとき)</li>
</ul>
<h2>Input</h2>
<p>入力は以下の形式で与えられる。</p>
<p>
$N$<br>
$a_{1}$ $a_{2}$ ... $a_{N}$<br>
$b_{1}$ $b_{2}$ ... $b_{N}$<br>
$Q$<br>
$x_1$ $y_1$ $z_1$<br>
$x_2$ $y_2$ $z_2$<br>
...<br>
$x_Q$ $y_Q$ $z_Q$<br>
</p>
<h2>Constraints</h2>
<p>入力は以下の条件を満たす。</p>
<ul>
<li>$2 \le N \le 2 \times 10^5$</li>
<li>$2 \le Q \le 2 \times 10^5$</li>
<li>$1 \le a_i \le 10^9$</li>
<li>$1 \le b_i \le 10^9$</li>
<li>$1 \le x_i \le 6$</li>
<li>$1 \le y_i \le N $ ($1 \le x_i \le 4$のとき)</li>
<li>$y_i = -1$ ($x_i=5, 6$のとき)</li>
<li>$1 \le z_i \le 10^9$ ($x_i=1, 2$のとき)</li>
<li>$y_i \le z_i \le N$ ($x_i=3, 4$のとき)</li>
<li>$z_i = -1$ ($x_i=5, 6$のとき)</li>
<li>入力はすべて整数</li>
</ul>
<h2>Output</h2>
<p>
$x=3$または$x=4$の命令文が入力で与えられる度に、見つけた値を1行に出力する。
</p>
<h2>Sample Input 1</h2>
<pre>
5
1 3 5 7 9
6 2 3 2 6
10
1 3 4
3 4 5
4 2 3
5 -1 -1
2 3 8
3 2 5
4 3 3
1 1 1
6 -1 -1
3 1 5
</pre>
<h2>Sample Output 1</h2>
<pre>
7
2
2
8
1
</pre> |
p03699 | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are taking a computer-based examination. The examination consists of <var>N</var> questions, and the score allocated to the <var>i</var>-th question is <var>s_i</var>. Your answer to each question will be judged as either "correct" or "incorrect", and your grade will be the sum of the points allocated to questions that are answered correctly. When you finish answering the questions, your answers will be immediately judged and your grade will be displayed... if everything goes well.</p>
<p>However, the examination system is actually flawed, and if your grade is a multiple of <var>10</var>, the system displays <var>0</var> as your grade. Otherwise, your grade is displayed correctly. In this situation, what is the maximum value that can be displayed as your grade?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>All input values are integers.</li>
<li><var>1 ≤ N ≤ 100</var></li>
<li><var>1 ≤ s_i ≤ 100</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>s_1</var>
<var>s_2</var>
<var>:</var>
<var>s_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum value that can be displayed as your grade.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
5
10
15
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>25
</pre>
<p>Your grade will be <var>25</var> if the <var>10</var>-point and <var>15</var>-point questions are answered correctly and the <var>5</var>-point question is not, and this grade will be displayed correctly. Your grade will become <var>30</var> if the <var>5</var>-point question is also answered correctly, but this grade will be incorrectly displayed as <var>0</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
10
10
15
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>35
</pre>
<p>Your grade will be <var>35</var> if all the questions are answered correctly, and this grade will be displayed correctly.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>3
10
20
30
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0
</pre>
<p>Regardless of whether each question is answered correctly or not, your grade will be a multiple of <var>10</var> and displayed as <var>0</var>.</p></section>
</div>
</span> |
p03363 | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have an integer sequence <var>A</var>, whose length is <var>N</var>.</p>
<p>Find the number of the non-empty <strong>contiguous</strong> subsequences of <var>A</var> whose sums are <var>0</var>.
Note that we are counting <strong>the ways to take out subsequences</strong>.
That is, even if the contents of some two subsequences are the same, they are counted individually if they are taken from different positions.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 2 \times 10^5</var></li>
<li><var>-10^9 \leq A_i \leq 10^9</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Find the number of the non-empty contiguous subsequences of <var>A</var> whose sum is <var>0</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6
1 3 -4 2 2 -2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p>There are three contiguous subsequences whose sums are <var>0</var>: <var>(1,3,-4)</var>, <var>(-4,2,2)</var> and <var>(2,-2)</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>7
1 -1 1 -1 1 -1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>12
</pre>
<p>In this case, some subsequences that have the same contents but are taken from different positions are counted individually.
For example, three occurrences of <var>(1, -1)</var> are counted.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>5
1 -2 3 -4 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0
</pre>
<p>There are no contiguous subsequences whose sums are <var>0</var>.</p></section>
</div>
</span> |
p03733 | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>In a public bath, there is a shower which emits water for <var>T</var> seconds when the switch is pushed.</p>
<p>If the switch is pushed when the shower is already emitting water, from that moment it will be emitting water for <var>T</var> seconds.
Note that it does not mean that the shower emits water for <var>T</var> additional seconds.</p>
<p><var>N</var> people will push the switch while passing by the shower.
The <var>i</var>-th person will push the switch <var>t_i</var> seconds after the first person pushes it.</p>
<p>How long will the shower emit water in total?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≤ N ≤ 200,000</var></li>
<li><var>1 ≤ T ≤ 10^9</var></li>
<li><var>0 = t_1 < t_2 < t_3 < , ..., < t_{N-1} < t_N ≤ 10^9</var></li>
<li><var>T</var> and each <var>t_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>T</var>
<var>t_1</var> <var>t_2</var> ... <var>t_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Assume that the shower will emit water for a total of <var>X</var> seconds. Print <var>X</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 4
0 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>7
</pre>
<p>Three seconds after the first person pushes the water, the switch is pushed again and the shower emits water for four more seconds, for a total of seven seconds.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2 4
0 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>8
</pre>
<p>One second after the shower stops emission of water triggered by the first person, the switch is pushed again.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>4 1000000000
0 1000 1000000 1000000000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>2000000000
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>1 1
0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>1
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 5</h3><pre>9 10
0 3 5 7 100 110 200 300 311
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 5</h3><pre>67
</pre></section>
</div>
</span> |
p02821 | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Takahashi has come to a party as a special guest.
There are <var>N</var> ordinary guests at the party. The <var>i</var>-th ordinary guest has a <em>power</em> of <var>A_i</var>.</p>
<p>Takahashi has decided to perform <var>M</var> <em>handshakes</em> to increase the <em>happiness</em> of the party (let the current happiness be <var>0</var>).
A handshake will be performed as follows:</p>
<ul>
<li>Takahashi chooses one (ordinary) guest <var>x</var> for his left hand and another guest <var>y</var> for his right hand (<var>x</var> and <var>y</var> can be the same).</li>
<li>Then, he shakes the left hand of Guest <var>x</var> and the right hand of Guest <var>y</var> simultaneously to increase the happiness by <var>A_x+A_y</var>.</li>
</ul>
<p>However, Takahashi should not perform the same handshake more than once. Formally, the following condition must hold:</p>
<ul>
<li>Assume that, in the <var>k</var>-th handshake, Takahashi shakes the left hand of Guest <var>x_k</var> and the right hand of Guest <var>y_k</var>. Then, there is no pair <var>p, q</var> <var>(1 \leq p < q \leq M)</var> such that <var>(x_p,y_p)=(x_q,y_q)</var>.</li>
</ul>
<p>What is the maximum possible happiness after <var>M</var> handshakes?</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 M \leq N^2</var></li>
<li><var>1 \leq A_i \leq 10^5</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>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 maximum possible happiness after <var>M</var> handshakes.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5 3
10 14 19 34 33
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>202
</pre>
<p>Let us say that Takahashi performs the following handshakes:</p>
<ul>
<li>In the first handshake, Takahashi shakes the left hand of Guest <var>4</var> and the right hand of Guest <var>4</var>.</li>
<li>In the second handshake, Takahashi shakes the left hand of Guest <var>4</var> and the right hand of Guest <var>5</var>.</li>
<li>In the third handshake, Takahashi shakes the left hand of Guest <var>5</var> and the right hand of Guest <var>4</var>.</li>
</ul>
<p>Then, we will have the happiness of <var>(34+34)+(34+33)+(33+34)=202</var>.</p>
<p>We cannot achieve the happiness of <var>203</var> or greater, so the answer is <var>202</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>9 14
1 3 5 110 24 21 34 5 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1837
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>9 73
67597 52981 5828 66249 75177 64141 40773 79105 16076
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>8128170
</pre></section>
</div>
</span> |
p03226 | <span class="lang-en">
<p>Score : <var>900</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a sequence of <var>N</var> integers: <var>A_1,A_2,...,A_N</var>.</p>
<p>Find the number of permutations <var>p_1,p_2,...,p_N</var> of <var>1,2,...,N</var> that can be changed to <var>A_1,A_2,...,A_N</var> by performing the following operation some number of times (possibly zero), modulo <var>998244353</var>:</p>
<ul>
<li>For each <var>1\leq i\leq N</var>, let <var>q_i=min(p_{i-1},p_{i})</var>, where <var>p_0=p_N</var>. Replace the sequence <var>p</var> with the sequence <var>q</var>.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 3 × 10^5</var></li>
<li><var>1 \leq 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>:</var>
<var>A_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of the sequences that satisfy the condition, modulo <var>998244353</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
1
2
1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p><var>(2,3,1)</var> and <var>(3,2,1)</var> satisfy the condition.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5
3
1
4
1
5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>8
4
4
4
1
1
1
2
2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>24
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>6
1
1
6
2
2
2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>0
</pre></section>
</div>
</span> |
p04019 | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Snuke lives on an infinite two-dimensional plane. He is going on an <var>N</var>-day trip.
At the beginning of Day <var>1</var>, he is at home. His plan is described in a string <var>S</var> of length <var>N</var>.
On Day <var>i(1 ≦ i ≦ N)</var>, he will travel a positive distance in the following direction:</p>
<ul>
<li>North if the <var>i</var>-th letter of <var>S</var> is <code>N</code></li>
<li>West if the <var>i</var>-th letter of <var>S</var> is <code>W</code></li>
<li>South if the <var>i</var>-th letter of <var>S</var> is <code>S</code></li>
<li>East if the <var>i</var>-th letter of <var>S</var> is <code>E</code></li>
</ul>
<p>He has not decided each day's travel distance. Determine whether it is possible to set each day's travel distance so that he will be back at home at the end of Day <var>N</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≦ | S | ≦ 1000</var></li>
<li><var>S</var> consists of the letters <code>N</code>, <code>W</code>, <code>S</code>, <code>E</code>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>S</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <code>Yes</code> if it is possible to set each day's travel distance so that he will be back at home at the end of Day <var>N</var>. Otherwise, print <code>No</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>SENW
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<p>If Snuke travels a distance of <var>1</var> on each day, he will be back at home at the end of day <var>4</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>NSNNSNSN
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>Yes
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>NNEW
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>No
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>W
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>No
</pre></section>
</div>
</span> |
p03676 | <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 of length <var>n+1</var>, <var>a_1,a_2,...,a_{n+1}</var>, which consists of the <var>n</var> integers <var>1,...,n</var>.
It is known that each of the <var>n</var> integers <var>1,...,n</var> appears at least once in this sequence.</p>
<p>For each integer <var>k=1,...,n+1</var>, find the number of the different subsequences (not necessarily contiguous) of the given sequence with length <var>k</var>, modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Notes</h3><ul>
<li>
<p>If the contents of two subsequences are the same, they are not separately counted even if they originate from different positions in the original sequence.</p>
</li>
<li>
<p>A subsequence of a sequence <var>a</var> with length <var>k</var> is a sequence obtained by selecting <var>k</var> of the elements of <var>a</var> and arranging them without changing their relative order. For example, the sequences <var>1,3,5</var> and <var>1,2,3</var> are subsequences of <var>1,2,3,4,5</var>, while <var>3,1,2</var> and <var>1,10,100</var> are not.</p>
</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq n \leq 10^5</var></li>
<li><var>1 \leq a_i \leq n</var></li>
<li>Each of the integers <var>1,...,n</var> appears in the sequence.</li>
<li><var>n</var> and <var>a_i</var> are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>n</var>
<var>a_1</var> <var>a_2</var> ... <var>a_{n+1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <var>n+1</var> lines.
The <var>k</var>-th line should contain the number of the different subsequences of the given sequence with length <var>k</var>, modulo <var>10^9+7</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
1 2 1 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
5
4
1
</pre>
<p>There are three subsequences with length <var>1</var>: <var>1</var> and <var>2</var> and <var>3</var>.</p>
<p>There are five subsequences with length <var>2</var>: <var>1,1</var> and <var>1,2</var> and <var>1,3</var> and <var>2,1</var> and <var>2,3</var>.</p>
<p>There are four subsequences with length <var>3</var>: <var>1,1,3</var> and <var>1,2,1</var> and <var>1,2,3</var> and <var>2,1,3</var>.</p>
<p>There is one subsequence with length <var>4</var>: <var>1,2,1,3</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>1
1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1
1
</pre>
<p>There is one subsequence with length <var>1</var>: <var>1</var>.</p>
<p>There is one subsequence with length <var>2</var>: <var>1,1</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>32
29 19 7 10 26 32 27 4 11 20 2 8 16 23 5 14 6 12 17 22 18 30 28 24 15 1 25 3 13 21 19 31 9
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>32
525
5453
40919
237336
1107568
4272048
13884156
38567100
92561040
193536720
354817320
573166440
818809200
37158313
166803103
166803103
37158313
818809200
573166440
354817320
193536720
92561040
38567100
13884156
4272048
1107568
237336
40920
5456
528
33
1
</pre>
<p>Be sure to print the numbers modulo <var>10^9+7</var>.</p></section>
</div>
</span> |
p02964 | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a sequence of <var>N \times K</var> integers: <var>X=(X_0,X_1,\cdots,X_{N \times K-1})</var>.
Its elements are represented by another sequence of <var>N</var> integers: <var>A=(A_0,A_1,\cdots,A_{N-1})</var>. For each pair <var>i, j</var> (<var>0 \leq i \leq K-1,\ 0 \leq j \leq N-1</var>), <var>X_{i \times N + j}=A_j</var> holds.</p>
<p>Snuke has an integer sequence <var>s</var>, which is initially empty.
For each <var>i=0,1,2,\cdots,N \times K-1</var>, in this order, he will perform the following operation:</p>
<ul>
<li>If <var>s</var> does not contain <var>X_i</var>: add <var>X_i</var> to the end of <var>s</var>.</li>
<li>If <var>s</var> does contain <var>X_i</var>: repeatedly delete the element at the end of <var>s</var> until <var>s</var> no longer contains <var>X_i</var>. Note that, in this case, we do <strong>not</strong> add <var>X_i</var> to the end of <var>s</var>.</li>
</ul>
<p>Find the elements of <var>s</var> after Snuke finished the operations.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 2 \times 10^5</var></li>
<li><var>1 \leq K \leq 10^{12}</var></li>
<li><var>1 \leq A_i \leq 2 \times 10^5</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_0</var> <var>A_1</var> <var>\cdots</var> <var>A_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the elements of <var>s</var> after Snuke finished the operations, in order from beginning to end, with spaces in between.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 2
1 2 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2 3
</pre>
<p>In this case, <var>X=(1,2,3,1,2,3)</var>.
We will perform the operations as follows:</p>
<ul>
<li><var>i=0</var>: <var>s</var> does not contain <var>1</var>, so we add <var>1</var> to the end of <var>s</var>, resulting in <var>s=(1)</var>.</li>
<li><var>i=1</var>: <var>s</var> does not contain <var>2</var>, so we add <var>2</var> to the end of <var>s</var>, resulting in <var>s=(1,2)</var>.</li>
<li><var>i=2</var>: <var>s</var> does not contain <var>3</var>, so we add <var>3</var> to the end of <var>s</var>, resulting in <var>s=(1,2,3)</var>.</li>
<li><var>i=3</var>: <var>s</var> does contain <var>1</var>, so we repeatedly delete the element at the end of <var>s</var> as long as <var>s</var> contains <var>1</var>, which causes the following changes to <var>s</var>: <var>(1,2,3)→(1,2)→(1)→()</var>.</li>
<li><var>i=4</var>: <var>s</var> does not contain <var>2</var>, so we add <var>2</var> to the end of <var>s</var>, resulting in <var>s=(2)</var>.</li>
<li><var>i=5</var>: <var>s</var> does not contain <var>3</var>, so we add <var>3</var> to the end of <var>s</var>, resulting in <var>s=(2,3)</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5 10
1 2 3 2 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>3
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>6 1000000000000
1 1 2 2 3 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre></pre>
<p><var>s</var> may be empty in the end.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>11 97
3 1 4 1 5 9 2 6 5 3 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>9 2 6
</pre></section>
</div>
</span> |
p00959 |
<script type="text/x-mathjax-config">
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], skipTags: ["script","noscript","style","textarea","code"], processEscapes: true }});
</script>
<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS_HTML"></script>
<h2>Problem C
Medical Checkup
</h2>
<p>
Students of the university have to go for a medical checkup, consisting of lots of checkup items, numbered 1, 2, 3, and so on.
</p>
<p>
Students are now forming a long queue, waiting for the checkup to start. Students are also numbered 1, 2, 3, and so on, from the top of the queue. They have to undergo checkup items in the order of the item numbers, not skipping any of them nor changing the order. The order of students should not be changed either.
</p>
<p>
Multiple checkup items can be carried out in parallel, but each item can be carried out for only one student at a time. Students have to wait in queues of their next checkup items until all the others before them finish.
</p>
<p>
Each of the students is associated with an integer value called <i>health condition</i>. For a student with the health condition $h$, it takes $h$ minutes to finish each of the checkup items. You may assume that no interval is needed between two students on the same checkup item or two checkup items for a single student.
</p>
<p>
Your task is to find the items students are being checked up or waiting for at a specified time $t$.
</p>
<h3>Input</h3>
<p>
The input consists of a single test case in the following format.
</p>
<pre>
$n$ $t$
$h_1$
...
$h_n$
</pre>
<p>
$n$ and $t$ are integers. $n$ is the number of the students ($1 \leq n \leq 10^5$). $t$ specifies the time of our concern ($0 \leq t \leq 10^9$). For each $i$, the integer $h_i$ is the health condition of student $i$ ($1 \leq h_ \leq 10^9$).
</p>
<h3>Output</h3>
<p>
Output $n$ lines each containing a single integer. The $i$-th line should contain the checkup item number of the item which the student $i$ is being checked up or is waiting for, at ($t+0.5$) minutes after the checkup starts. You may assume that all the students are yet to finish some of the checkup items at that moment.
</p>
<h3>Sample Input 1</h3>
<pre>
3 20
5
7
3
</pre>
<h3>Sample Output 1</h3>
<pre>
5
3
2
</pre>
<h3>Sample Input 2</h3>
<pre>
5 1000000000
5553
2186
3472
2605
1790
</pre>
<h3>Sample Output 2</h3>
<pre>
180083
180083
180082
180082
180082
</pre> |
p02437 | <h1>Priority Queue</h1>
<p>
Priority queue is a container of elements which the element with the highest priority should be extracted first.
</p>
<p>
For $n$ priority queues $Q_i$ ($i = 0, 1, ..., n-1$) of integers, perform a sequence of the following operations.
</p>
<ul>
<li>insert($t$, $x$): Insert $x$ to $Q_t$.</li>
<li>getMax($t$): Report the maximum value in $Q_t$. If $Q_t$ is empty, do nothing.</li>
<li>deleteMax($t$): Delete the maximum element from $Q_t$. If $Q_t$ is empty, do nothing.</li>
</ul>
<p>
In the initial state, all queues are empty.
</p>
<h2>Input</h2>
<p>
The input is given in the following format.
</p>
<pre>
$n \; q$
$query_1$
$query_2$
:
$query_q$
</pre>
<p>
Each query $query_i$ is given by
</p>
<pre>
0 $t$ $x$
</pre>
<p>or</p>
<pre>
1 $t$
</pre>
<p>or</p>
<pre>
2 $t$
</pre>
<p>
where the first digits <span>0</span>, <span>1</span> and <span>2</span> represent insert, getMax and deleteMax operations respectively.
</p>
<h2>Output</h2>
<p>
For each getMax operation, print an integer in a line.
</p>
<h2>Constraints</h2>
<ul>
<li>$1 \leq n \leq 1,000$</li>
<li>$1 \leq q \leq 200,000$</li>
<li>$-1,000,000,000 \leq x \leq 1,000,000,000$</li>
</ul>
<h2>Sample Input 1</h2>
<pre>
2 10
0 0 3
0 0 9
0 0 1
1 0
2 0
1 0
0 0 4
1 0
0 1 8
1 1
</pre>
<h2>Sample Output 1</h2>
<pre>
9
3
4
8
</pre>
|
p02067 | <style type="text/css">
blockquote {
font-family: Menlo, Monaco, "Courier New", monospace;
display: block;
margin: 10px 0 10px 30px;
font-size: 16px;
line-height: 18px;
white-space: pre;
white-space: pre-wrap;
word-break: break-all;
word-wrap: break-word;
}
table.ioexample {
width: 100%;
border-collapse: collapse;
}
table.ioexample td {
width: 50%;
border: 1px solid rgba(0, 0, 0, 0.15);
vertical-align: top;
padding: 5px;
}
.no-page-break {
page-break-inside: avoid;
}
.page-break {
page-break-before: always;
}
</style>
<h3>Problem Statement</h3>
<p>Recently, AIs which play Go (a traditional board game) are well investigated.
Your friend Hikaru is planning to develop a new awesome Go AI named Sai and promote it to company F or company G in the future.
As a first step, Hikaru has decided to develop an AI for 1D-Go, a restricted version of the original Go.</p>
<p>In both of the original Go and 1D-Go, capturing stones is an important strategy.
Hikaru asked you to implement one of the functions of capturing.</p>
<p>In 1D-Go, the game board consists of $L$ grids lie in a row.
A state of 1D-go is described by a string $S$ of length $L$.
The $i$-th character of $S$ describes the $i$-th grid as the following:</p>
<ul>
<li>When the $i$-th character of $S$ is <code>B</code>, the $i$-th grid contains a stone which is colored black.</li>
<li>When the $i$-th character of $S$ is <code>W</code>, the $i$-th grid contains a stone which is colored white.</li>
<li>When the $i$-th character of $S$ is <code>.</code>, the $i$-th grid is empty.</li>
</ul>
<p>Maximal continuous stones of the same color are called a chain.
When a chain is surrounded by stones with opponent's color, the chain will be captured.</p>
<p>More precisely, if $i$-th grid and $j$-th grids ($1 < i + 1 < j \leq L$) contain white stones and every grid of index $k$ ($i < k < j$) contains a black stone, these black stones will be captured, and vice versa about color.</p>
<p>Please note that some of the rules of 1D-Go are quite different from the original Go.
Some of the intuition obtained from the original Go may curse cause some mistakes.</p>
<p>You are given a state of 1D-Go that next move will be played by the player with white stones.
The player can put a white stone into one of the empty cells.
However, the player can not make a chain of white stones which is surrounded by black stones even if it simultaneously makes some chains of black stones be surrounded.
It is guaranteed that the given state has at least one grid where the player can put a white stone and there are no chains which are already surrounded.</p>
<p>Write a program that computes the maximum number of black stones which can be captured by the next move of the white stones player.</p>
<hr />
<h3>Input</h3>
<p>The input consists of one line and has the following format:</p>
<blockquote>$L$ $S$</blockquote>
<p>$L$ ($1 \leq L \leq 100$) means the length of the game board and $S$ ($|S| = L$) is a string which describes the state of 1D-Go. The given state has at least one grid where the player can put a white stone and there are no chains which are already surrounded.</p>
<h3>Output</h3>
<p>Output the maximum number of stones which can be captured by the next move in a line.</p>
<p><div class="no-page-break"><h3>Examples</h3><table class="ioexample"><tr><th>Input</th><th>Output</th></tr><tr><td><pre>5 .WB..
</pre></td><td><pre>1
</pre></td></tr><tr><td><pre>5 .WBB.
</pre></td><td><pre>2
</pre></td></tr><tr><td><pre>6 .WB.B.
</pre></td><td><pre>0
</pre></td></tr><tr><td><pre>6 .WB.WB
</pre></td><td><pre>0
</pre></td></tr><tr><td><pre>5 BBB..
</pre></td><td><pre>0
</pre></td></tr></table></div></p>
<p>In the 3rd and 4th test cases, the player cannot put a white stone on the 4th grid since the chain of the white stones will be surrounded by black stones. This rule is different from the original Go.</p>
<p>In the 5th test case, the player cannot capture any black stones even if the player put a white stone on the 4th grid. The player cannot capture black stones by surrounding them with the edge of the game board and the white stone. This rule is also different from the original Go.</p>
|
p01562 |
<h1>Area Folding</h1>
<p> You are given one polygonal line, which is a collection of line segments.
Your task is to calculate the sum of areas enclosed by the polygonal line.
</p>
<p>
A point is defined to be "enclosed" if and only if the point is unreachable without crossing at least one line segment from the point at infinity.
</p>
<h2>Input</h2>
<p>The first line contains one integers <var>N</var> <var>(2 ≤ N ≤ 100)</var>.
<var>N</var> is the number of segments.
</p>
<p> Each of the following <var>N</var> lines consists of two integers <var>X<sub>i</sub></var> and <var>Y<sub>i</sub></var> <var>(-10<sup>5</sup> ≤ X<sub>i</sub>, Y<sub>i</sub> ≤ 10<sup>5</sup>, 1 ≤ i ≤ N)</var> which represents a vertex.
A polygonal line is the segments which connect <var>(X<sub>j</sub>, Y<sub>j</sub>)</var> and <var>(X<sub>j+1</sub>, Y<sub>j+1</sub>)</var> <var>((X<sub>j</sub>, Y<sub>j</sub>) ≠ (X<sub>j+1</sub>, Y<sub>j+1</sub>), 1 ≤ j ≤ N-1)</var>.
The distance between a segment <var>S<sub>j</sub></var> and all vertices except the end points on segment <var>S<sub>j</sub></var> is guaranteed to be greater than 0.01.
</p>
<h2>Output</h2>
<p>Output the answer in a line.
The answer may be printed with an arbitrary number of decimal digits, but may not contain an absolute or relative error greater than or equal to <var>10<sup>-6</sup></var>.
</p>
<h2>Sample Input 1</h2>
<pre>5
0 0
1 1
1 0
0 1
0 0
</pre>
<h2>Output for the Sample Input 1</h2>
<pre>0.5
</pre>
<h2>Sample Input 2</h2>
<pre>5
0 0
1 1
1 0
0 1
1 0
</pre>
<h2>Output for the Sample Input 2</h2>
<pre>0.25
</pre>
<h2>Sample Input 3</h2>
<pre>21
1 1
-1 1
-1 2
-2 2
-2 1
-1 1
-1 -1
-2 -1
-2 -2
-1 -2
-1 -1
1 -1
1 -2
2 -2
2 -1
1 -1
1 1
2 1
2 2
1 2
1 1
</pre>
<h2>Output for the Sample Input 3</h2>
<pre>8
</pre>
<h2>Sample Input 4</h2>
<pre>16
0 0
1 0
1 1
0 1
0 2
0 3
1 3
1 2
2 2
2 3
3 3
3 2
3 1
2 1
2 0
3 0
</pre>
<h2>Output for the Sample Input 4</h2>
<pre>0
</pre>
<h2>Sample Input 5</h2>
<pre>7
26 52
33 12
-51 68
16 61
43 -26
87 24
12 10
</pre>
<h2>Output for the Sample Input 5</h2>
<pre>2714.840579710
</pre>
|
p01098 |
<h3>Deciphering Characters</h3>
<p>
Image data which are left by a mysterious syndicate were discovered.
You are requested to analyze the data.
The syndicate members used characters invented independently.
A binary image corresponds to one character written in black ink on white paper.
</p>
<p>
Although you found many variant images that represent the same character,
you discovered that you can judge whether or not two images represent the same character with the surrounding relation of connected components. We present some definitions as follows. Here, we assume that white pixels fill outside of the given image.
<ul>
<li> <b>White connected component</b> : A set of white pixels connected to each other horizontally or vertically (see below).</li>
<li> <b>Black connected component</b> : A set of black pixels connected to each other horizontally, vertically, or diagonally (see below).</li>
<li> <b>Connected component</b> : A white or a black connected component.</li>
<li> <b>Background component</b> : The connected component including pixels outside of the image. Any white pixels on the periphery of the image are thus included in the background component.</li>
</ul>
</p>
<p align="center">
<table>
<tr>
<td><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ICPCDomestic2016_F1_3"></td>
<td><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ICPCDomestic2016_F1_4"></td>
<td><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ICPCDomestic2016_F1_1"></td>
<td><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ICPCDomestic2016_F1_2"></td>
</tr>
<tr>
<td align="center">connected</td>
<td align="center">disconnected</td>
<td align="center">connected</td>
<td align="center">connected</td>
</tr>
<tr>
<td align="center" colspan="2">Connectedness of white pixels </td>
<td align="center" colspan="2">Connectedness of black pixels</td>
</tr>
</table>
</p>
<p>
Let <i>C<sub>1</sub></i> be a connected component in an image and <i>C<sub>2</sub></i> be another connected component in the same image with the opposite color.
Let's think of a modified image in which colors of all pixels not included in <i>C<sub>1</sub></i> nor <i>C<sub>2</sub></i> are changed to that of <i>C<sub>2</sub></i>. If neither <i>C<sub>1</sub></i> nor <i>C<sub>2</sub></i> is the background component, the color of the background component is changed to that of <i>C<sub>2</sub></i>. We say that <b><i>C<sub>1</sub></i> surrounds <i>C<sub>2</sub></i></b> in the original image when pixels in <i>C<sub>2</sub></i> are not included in the background component in the modified image. (see below)
</p>
<p align="center">
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ICPCDomestic2016_F5">
</p>
<p>
Two images represent the same character if both of the following conditions are satisfied.
<ul>
<li> The two images have the same number of connected components.</li>
<li> Let <i>S</i> and <i>S'</i> be the sets of connected components of the two images. A bijective function <i>f</i> : <i>S → S'</i> satisfying the following conditions exists.</li>
<ul>
<li> For each connected component <i>C</i> that belongs to <i>S</i>, <i>f</i> (<i>C</i>) has the same color as <i>C</i>. </li>
<li> For each of <i>C<sub>1</sub></i> and <i>C<sub>2</sub></i> belonging to <i>S</i>, <i>f</i> (<i>C<sub>1</sub></i>) surrounds <i>f</i> (<i>C<sub>2</sub></i>) if and only if <i>C<sub>1</sub></i> surrounds <i>C<sub>2</sub></i>.</li>
</ul>
</ul>
</p>
<p>
Let's see an example. Connected components in the images of the figure below has the following surrounding relations.
<ul>
<li> <i>C<sub>1</sub></i> surrounds <i>C<sub>2</sub></i>.</li>
<li> <i>C<sub>2</sub></i> surrounds <i>C<sub>3</sub></i>.</li>
<li> <i>C<sub>2</sub></i> surrounds <i>C<sub>4</sub></i>.</li>
<li> <i>C'<sub>1</sub></i> surrounds <i>C'<sub>2</sub></i>.</li>
<li> <i>C'<sub>2</sub></i> surrounds <i>C'<sub>3</sub></i>.</li>
<li> <i>C'<sub>2</sub></i> surrounds <i>C'<sub>4</sub></i>.</li>
</ul>
A bijective function defined as <i>f</i> (<i>C<sub>i</sub></i>) = <i>C'<sub>i</sub></i> for each connected component satisfies the conditions stated above. Therefore, we can conclude that the two images represent the same character.
</p>
<p align="center">
<table>
<tr>
<td><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ICPCDomestic2016_F2_2"></td>
<td><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ICPCDomestic2016_F2_1"></td>
</tr>
</table>
</p>
<p>
Make a program judging whether given two images represent the same character.
</p>
<h3>Input</h3>
<p>
The input consists of at most 200 datasets. The end of the input is represented by a line containing two zeros. Each dataset is formatted as follows.
</p>
<p>
image 1<br>
image 2
</p>
<p>
Each image has the following format.
</p>
<p>
<i>h</i> <i>w</i><br>
<i>p</i><sub>(1,1)</sub> ... <i>p</i><sub>(1,<i>w</i>)</sub><br>
...<br>
<i>p</i><sub>(<i>h</i>,1)</sub> ... <i>p</i><sub>(<i>h</i>,<i>w</i>)</sub><br>
</p>
<p>
<i>h</i> and <i>w</i> are the height and the width of the image in numbers of pixels.
You may assume that 1 ≤ <i>h</i> ≤ 100 and 1 ≤ <i>w</i> ≤ 100.
Each of the following <i>h</i> lines consists of <i>w</i> characters.
<i>p</i><sub>(<i>y,x</i>)</sub> is a character representing the color
of the pixel in the <i>y</i>-th row from the top and the <i>x</i>-th column from the left.
Characters are either a period ("<tt>.</tt>") meaning white or a sharp sign ("<tt>#</tt>") meaning black.
</p>
<h3>Output</h3>
<p>
For each dataset, output "<tt>yes</tt>" if the two images represent the same character, or output "<tt>no</tt>", otherwise, in a line. The output should not contain extra characters.
</p>
<h3>Sample Input</h3>
<pre>
3 6
.#..#.
#.##.#
.#..#.
8 7
.#####.
#.....#
#..#..#
#.#.#.#
#..#..#
#..#..#
.#####.
...#...
3 3
#..
...
#..
3 3
#..
.#.
#..
3 3
...
...
...
3 3
...
.#.
...
3 3
.#.
#.#
.#.
3 3
.#.
###
.#.
7 7
.#####.
#.....#
#..#..#
#.#.#.#
#..#..#
#.....#
.#####.
7 7
.#####.
#.....#
#..#..#
#.#.#.#
#..#..#
#..#..#
.#####.
7 7
.#####.
#.....#
#..#..#
#.#.#.#
#..#..#
#.....#
.#####.
7 11
.#####.....
#.....#....
#..#..#..#.
#.#.#.#.#.#
#..#..#..#.
#.....#....
.#####.....
7 3
.#.
#.#
.#.
#.#
.#.
#.#
.#.
7 7
.#####.
#..#..#
#..#..#
#.#.#.#
#..#..#
#..#..#
.#####.
3 1
#
#
.
1 2
#.
0 0
</pre>
<h3>Output for the Sample Input</h3>
<pre>
yes
no
no
no
no
no
yes
yes
</pre>
|
p01132 |
<h1><font color="#000000">Problem B:</font> Make Purse Light</h1>
<p>
Mr. Bill は店で買い物をしている。 彼の財布にはいくらかの硬貨(10 円玉、50 円玉、100 円玉、500 円玉)が入っているが、彼は今この小銭をできるだけ消費しようとしている。 つまり、適切な枚数の硬貨によって品物の代金を払うことで、釣り銭を受け取った後における硬貨の合計枚数を最小にしようとしている。
</p>
<p>
幸いなことに、この店の店員はとても律儀かつ親切であるため、釣り銭は常に最適な方法で渡される。 したがって、例えば 1 枚の 500 円玉の代わりに 5 枚の 100 円玉が渡されるようなことはない。 また、例えば 10 円玉を 5 枚出して、50 円玉を釣り銭として受け取ることもできる。 ただし、出した硬貨と同じ種類の硬貨が釣り銭として戻ってくるような払いかたをしてはいけない。 例えば、10 円玉を支払いの際に出したにも関わらず、別の 10 円玉が釣り銭として戻されるようでは、完全に意味のないやりとりが発生してしまうからである。
</p>
<p>
ところが Mr. Bill は計算が苦手なため、実際に何枚の硬貨を使用すればよいかを彼自身で求めることができなかった。 そこで、彼はあなたに助けを求めてきた。 あなたの仕事は、彼の財布の中にある硬貨の枚数と支払い代金をもとに、使用すべき硬貨の種類と枚数を求めるプログラムを書くことである。なお、店員はお釣りに紙幣を使用することはない。
<h2>Input</h2>
<p>
入力にはいくつかのテストケースが含まれる。
</p>
<p>
それぞれのテストケースは 2 行から構成される。 1 行目には Mr. Bill の支払い金額を円単位で表した 1 つの整数が含まれている。 2 行目には 4 つの整数が含まれており、それらは順に財布の中にある 10 円玉、50 円玉、100 円玉、500 円玉の枚数をそれぞれ表す。
</p>
<p>
支払い金額は常に 10 円単位である。 すなわち、支払い金額の一円の位は常に 0 である。 また財布の中に、同じ種類の硬貨は最大で 20 枚までしかないと仮定してよい。 支払いが不可能なケースが入力中に与えられることはない。
</p>
<p>
入力の終了は単一の 0 を含む行によって表される。
</p>
<h2>Output</h2>
<p>
それぞれのテストケースについて、Mr. Bill が使用すべき硬貨の種類と枚数を出力せよ。
</p>
<p>
出力の各行には 2 つの整数 <i>c<sub>i</sub></i> 、<i>k<sub>i</sub></i> が含まれる。これは支払いの際に <i>c<sub>i</sub></i> 円玉を <i>k<sub>i</sub></i> 枚使用することを意味している。
複数種類の硬貨を使用する場合は、<i>c<sub>i</sub></i> の小さいほうから順に、必要な行数だけ出力を行うこと。 下記の出力例を参考にせよ。
</p>
<p>
なお、出力には余計なスペースを含めてはならない。 連続するテストケースの間は空行で区切ること。
</p>
<h2>Sample Input</h2>
<pre>
160
1 1 2 0
160
1 0 2 10
0
</pre>
<h2>Output for the Sample Input</h2>
<pre>
10 1
50 1
100 1
10 1
100 2
</pre>
<!--
<h2>First Input Data</h2>
<p>
ここに<a href="B1">1番目のデータ </a>がある。
</p>
-->
<hr>
|
p00373 | <H1>Aerial Photos</H1>
<p>
Hideyo has come by two aerial photos of the same scale and orientation. You can see various types of buildings, but some areas are hidden by clouds. Apparently, they are of the same area, and the area covered by the second photograph falls entirely within the first. However, because they were taken at different time points, different shapes and distribution of clouds obscure identification where the area in the second photograph is located in the first. There may even be more than one area within the first that the second fits equally well.
</p>
<p>
A set of pixel information is given for each of the photographs. Write a program to extract candidate sub-areas within the first photograph that compare with the second equally well and output the number of the candidates.
</p>
<h2>Input</h2>
<p>
The input is given in the following format.
</p>
<pre>
<var>AW</var> <var>AH</var> <var>BW</var> <var>BH</var>
<var>arow<sub>1</sub></var>
<var>arow<sub>2</sub></var>
:
<var>arow<sub>AH</sub></var>
<var>brow<sub>1</sub></var>
<var>brow<sub>2</sub></var>
:
<var>brow<sub>BH</sub></var>
</pre>
<p>
The first line provides the number of pixels in the horizontal and the vertical direction <var>AW</var> (1 ≤ <var>AW</var> ≤ 800) and <var>AH</var> (1 ≤ <var>AH</var> ≤ 800) for the first photograph, followed by those for the second <var>BW</var> (1 ≤ <var>BW</var> ≤ 100) and <var>BH</var> (1 ≤ <var>BH</var> ≤ 100) (<var>AW</var> ≥ <var>BW</var> and<var>AH</var> ≥ <var>BH</var>). Each of the subsequent <var>AH</var> lines provides pixel information (<var>arow<sub>i</sub></var>) of the <var>i</var>-th row from the top of the first photograph. Each of the <var>B</var>H lines that follows provides pixel information (<var>brow<sub>i</sub></var>) of the <var>i</var>-th row from the top of the second photograph.
</p>
<p>
Each of the pixel information <var>arow<sub>i</sub></var> and <var>brow<sub>i</sub></var> is a string of length <var>AW</var> and <var>BW</var>, respectively, consisting of upper/lower-case letters, numeric characters, or "<span>?</span>". Each one character represents a pixel, whereby upper/lower-case letters and numeric characters represent a type of building, and "<span>?</span>" indicates a cloud-covered area.
</p>
<h2>Output</h2>
<p>
Output the number of the candidates.
</p>
<h2>Sample Input 1</h2>
<pre>
5 5 3 3
AF??z
W?p88
???Hk
pU?m?
F???c
F??
p?8
?H?
</pre>
<h2>Sample Output 1</h2>
<pre>
4
</pre>
<h2>Sample Input 2</h2>
<pre>
6 5 4 2
aaaaaa
aaaaaa
aaaaaa
aaaaaa
aaaaaa
aaaa
aaaa
</pre>
<h2>Sample Output 2</h2>
<pre>
12
</pre>
|
p00689 |
<h1>
Spiral Footrace </h1>
<p>
Let us enjoy extraordinary footraces at an athletic field.
Before starting the race, small flags representing checkpoints are set up
at random on the field. Every runner has to pass all the checkpoints one by
one in the spiral order specified below.
The starting point is the southwest corner of the athletic field. </p>
<p>
At the starting point, runners are facing north.
During the race, they run clockwise in a spiral passing
every checkpoint as illustrated in the following figure.
When moving from one flag to the next, they take a straight path.
This means that the course of a race is always piecewise linear. </p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_spiral">
</center>
<p>
Upon arriving at some flag, a runner has to determine the next flag
among those that are not yet visited.
That flag shall be the one at the smallest angle to the right of the direction
that the runner is facing. When more than two flags are on the same straight
line, the runner visits the nearest flag first.
The goal position is the place of the last visited flag.</p>
<p>
Your job is to write a program that calculates the length of the course from
the starting point to the goal, supposing that the course consists of
line segments connected by flags.
The position of each flag is given by a coordinate pair
(<i>x</i><sub><i>i</i></sub>, <i>y</i><sub><i>i</i></sub>), which is limited
to the first quadrant. The starting point is fixed to the origin (0, 0) where a
flag is always set up.</p>
<h2>Input</h2>
<p>The input contains multiple data sets, each representing a collection of
flag positions for a footrace. A data set is given in the following
format.</p>
<blockquote>
<i>
n</i>
<br>
<i>x</i><sub>1</sub> <i>y</i><sub>1</sub>
<br>
<i>x</i><sub>2</sub> <i>y</i><sub>2</sub>
<br>
...
<br>
<i>x</i><sub><i>n</i></sub> <i>y</i><sub><i>n</i></sub>
<br>
</blockquote>
<p>
Here, <i>n</i> is the number of flag positions (1 <= <i>n</i> <= 400).
The position of the <i>i</i>-th flag is given by
(<i>x</i><sub><i>i</i></sub>, <i>y</i><sub><i>i</i></sub>), where
<i>x</i><sub><i>i</i></sub> and <i>y</i><sub><i>i</i></sub> are integers
in meter between 0 and 500 inclusive.
There may be white spaces (blanks and/or tabs) before, in between, or after
<i>x</i><sub><i>i</i></sub> and <i>y</i><sub><i>i</i></sub>.
Once a certain
coordinate pair (<i>x</i><sub><i>i</i></sub>, <i>y</i><sub><i>i</i></sub>) is given,
the same one will not appear later in the data set, and the origin (0, 0)
will not appear either.</p>
<p>
A data set beginning with <i>n</i> = 0 indicates end of the input.
</p>
<h2> Output </h2>
<p>
For each data set, your program should output the length of the running course
in meter. Each length should appear on a separate line, with
one digit to the right of the decimal point, after rounding it to one
decimal place.</p>
<h2> Sample Input </h2>
<pre>
8
60 70
20 40
50 50
70 10
10 70
80 90
70 50
100 50
5
60 50
0 80
60 20
10 20
60 80
0
</pre>
<h2>Output for the Sample Input</h2>
<pre>
388.9
250.0
</pre>
|
p01831 | <script type="text/x-mathjax-config">
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});
</script>
<script type='text/javascript' src='http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>
</script>
<h2>Line Gimmick</h2>
<p>
You are in front of a linear gimmick of a game. It consists of $N$ panels in a row, and each of them displays a right or a left arrow.
</p>
<p>
You can step in this gimmick from any panel. Once you get on a panel, you are forced to move following the direction of the arrow shown on the panel and the panel will be removed immediately. You keep moving in the same direction until you get on another panel, and if you reach a panel, you turn in (or keep) the direction of the arrow on the panel. The panels you passed are also removed. You repeat this procedure, and when there is no panel in your current direction, you get out of the gimmick.
</p>
<p>
For example, when the gimmick is the following image and you first get on the 2nd panel from the left, your moves are as follows.
</p>
<ul>
<li> Move right and remove the 2nd panel.</li>
<li> Move left and remove the 3rd panel.</li>
<li> Move right and remove the 1st panel.</li>
<li> Move right and remove the 4th panel.</li>
<li> Move left and remove the 5th panel.</li>
<li> Get out of the gimmick.</li>
</ul>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_JAGAsia2015_D_1"><br/>
</center>
<p>
You are given a gimmick with $N$ panels. Compute the maximum number of removed panels after you get out of the gimmick.
</p>
<h3>Input</h3>
<p>
The input consists of two lines. The first line contains an integer $N$ ($1 \leq N \leq 100,000$) which represents the number of the panels in the gimmick. The second line contains a character string $S$ of length $N$, which consists of '<span>></span>' or '<span><</span>'. The $i$-th character of $S$ corresponds to the direction of the arrow on the $i$-th panel from the left. '<span><</span>' and '<span>></span>' denote left and right directions respectively.
</p>
<h3>Output</h3>
<p>
Output the maximum number of removed panels after you get out of the gimmick.
</p>
<h3>Sample Input 1</h3>
<pre>
7
>><><<<
</pre>
<h3>Output for the Sample Input 1</h3>
<pre>
7
</pre>
<h3>Sample Input 2</h3>
<pre>
5
>><<<
</pre>
<h3>Output for the Sample Input 2</h3>
<pre>
5
</pre>
<h3>Sample Input 3</h3>
<pre>
6
><<><<
</pre>
<h3>Output for the Sample Input 3</h3>
<pre>
6
</pre>
<h3>Sample Input 4</h3>
<pre>
7
<<><<>>
</pre>
<h3>Output for the Sample Input 4</h3>
<pre>
5
</pre>
|
p00723 |
<h1><font color="#000">Problem B:</font> <a name="section_B">Organize Your Train part II</a></h1>
<p>
RJ Freight, a Japanese railroad company for freight operations
has recently constructed exchange lines at Hazawa, Yokohama.
The layout of the lines is shown in Figure B-1.
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_Layout"><br>
<!-- begin en only -->
Figure B-1: Layout of the exchange lines
<!-- end en only -->
</center>
<!-- begin en only -->
<p>
A freight train consists of 2 to 72 freight cars. There are 26
types of freight cars, which are denoted by 26 lowercase letters
from "a" to "z". The cars of the same type are indistinguishable from
each other, and each car's direction doesn't matter either.
Thus, a string of lowercase letters of length 2 to 72 is sufficient
to completely express the configuration of a train.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
Upon arrival at the exchange lines, a train is divided into two
sub-trains at an arbitrary position (prior to entering the
storage lines). Each of the sub-trains may have its direction
reversed (using the reversal line). Finally, the two sub-trains
are connected in either order to form the final configuration.
Note that the reversal operation is optional for each of the
sub-trains.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
For example, if the arrival configuration is "abcd", the train
is split into two sub-trains of either 3:1, 2:2 or 1:3 cars.
For each of the splitting, possible final configurations are
as follows ("+" indicates final concatenation position):
</p>
<!-- end en only -->
<pre> [3:1]
abc+d cba+d d+abc d+cba
[2:2]
ab+cd ab+dc ba+cd ba+dc cd+ab cd+ba dc+ab dc+ba
[1:3]
a+bcd a+dcb bcd+a dcb+a
</pre>
<p>
<!-- begin en only -->
Excluding duplicates, 12 distinct configurations are possible.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
Given an arrival configuration, answer
the number of distinct configurations which can be
constructed using the exchange lines described above.
</p>
<!-- end en only -->
<h2>Input</h2>
<!-- begin en only -->
<p>The entire input looks like the following.</p>
<blockquote>
<i>the number of datasets = m</i><br>
<i>1st dataset</i> <br>
<i>2nd dataset</i> <br>
... <br>
<i>m-th dataset</i> <br>
</blockquote>
<p>
Each dataset represents an arriving train, and is a string of
2 to 72 lowercase letters in an input line.
</p>
<!-- end en only -->
<h2>Output</h2>
<!-- begin en only -->
<p>
For each dataset, output the number of possible train configurations
in a line. No other characters should appear in the output.
</p>
<!-- end en only -->
<h2>Sample Input</h2>
<pre>
4
aa
abba
abcd
abcde
</pre>
<h2>Output for the Sample Input</h2>
<pre>
1
6
12
18
</pre> |
p00236 |
<H1>宇宙人のメッセージ</H1>
<p>
宇宙人 Mr.X は地球への来星記念として、地球人に向けてのメッセージを残しました。 Mr.X がメッセージを残す場所に選んだのは古代遺跡として有名な"トロンコ遺跡"でした。 ここはいろいろな大きさの碁盤のマス目に、奇抜な石像が無造作に配置されている不思議な場所でした。
</p>
<p>
Mr.X は、メッセージとして、石像が置かれていない全てのマス目を一度だけ通る、単一の閉曲線を描きました。Mr.X はとても賢く、このような閉曲線を描くことが可能な碁盤であれば必ず閉曲線を描き、メッセージを残しました。しかし、石像の配置によっては、閉曲線を描くことが不可能な碁盤もあり、その場合はメッセージを残しませんでした。 図1の碁盤に描かれている閉曲線は、全ての空いているマス目を一度だけ通っています。Mr.X はこのような閉曲線をメッセージとして残しました。
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_UFOMessage1"><br>
図1
</center>
<br>
<p>
Mr.X は、図2の碁盤に描かれているような閉曲線は描きませんでした。
</p>
<center>
<table>
<tr>
<td>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_UFOMessage2" width="160">
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_UFOMessage3" width="160">
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_UFOMessage4" width="160">
</td>
</tr></table><br>
図2<br>
</center>
<p>
後に Mr.X のメッセージは、古代遺跡と見事に調和した宇宙人の美として、地球人に夢とロマンを与える伝説となりました。しかし、長年に渡る風化によってメッセージは消えてしまい、伝説だけが残りました。
</p>
<!--
<p>
トロンコ遺跡の近くに住んでいるあなたは、Mr.X が残したメッセージを巡るツアーを企画したいと考えており、遺跡にあるどの碁盤に Mr.X がメッセージを残したかを知る必要が生じました。
</p>
-->
<p>
碁盤の情報を入力とし、Mr.X がその碁盤上にメッセージを残した場合は Yes と、残さなかった場合は No と出力するプログラムを作成してください。ただし、全てのマスに石像が配置されている場合は、No と出力することとします。
</p>
<H2>Input</H2>
<p>
複数のデータセットの並びが入力として与えられます。入力の終わりはゼロふたつの行で示されます。各データセットは以下の形式で与えられます。
</p>
<pre>
<var>W</var> <var>H</var>
<var>c<sub>1,1</sub></var> <var>c<sub>1,2</sub></var> ... <var>c<sub>1,W</sub></var>
<var>c<sub>2,1</sub></var> <var>c<sub>2,2</sub></var> ... <var>c<sub>2,W</sub></var>
:
<var>c<sub>H,1</sub></var> <var>c<sub>H,2</sub></var> ... <var>c<sub>H,W</sub></var>
</pre>
<p>
1 行目に碁盤の横方向に並ぶマス目の数 <var>W</var>, 縦方向に並ぶマス目の数 <var>H</var> (1 ≤ <var>W, H</var> ≤ 7) が与えられる。
</p>
<p>
続く <var>H</var> 行に碁盤の状態が与えられる。<var>c<sub>i,j</sub></var> は碁盤の <var>i</var> 行目の左から <var>j</var> 番目のマス目を表す整数であり、0 のとき何も置かれていないマス目、1 のとき石像が置かれているマス目を表す。
</p>
<p>
データセットの数は 1000 を超えない。
</p>
<H2>Output</H2>
<p>
データセットごとに、Yes または No を1行に出力する。
</p>
<H2>Sample Input</H2>
<pre>
5 4
0 0 0 0 0
0 1 1 0 0
0 0 1 0 1
1 0 0 0 1
5 4
0 0 0 0 0
0 1 1 0 0
0 0 0 0 1
1 0 0 0 1
0 0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
Yes
No
</pre>
|
p03949 | <span class="lang-en">
<p>Score : <var>800</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a tree with <var>N</var> vertices. The vertices are numbered <var>1, 2, ..., N</var>. The <var>i</var>-th (<var>1 ≦ i ≦ N - 1</var>) edge connects the two vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>Takahashi wrote integers into <var>K</var> of the vertices. Specifically, for each <var>1 ≦ j ≦ K</var>, he wrote the integer <var>P_j</var> into vertex <var>V_j</var>. The remaining vertices are left empty. After that, he got tired and fell asleep.</p>
<p>Then, Aoki appeared. He is trying to surprise Takahashi by writing integers into all empty vertices so that the following condition is satisfied:</p>
<ul>
<li>Condition: For any two vertices directly connected by an edge, the integers written into these vertices differ by exactly <var>1</var>.</li>
</ul>
<p>Determine if it is possible to write integers into all empty vertices so that the condition is satisfied. If the answer is positive, find one specific way to satisfy the condition.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≦ N ≦ 10^5</var></li>
<li><var>1 ≦ K ≦ N</var></li>
<li><var>1 ≦ A_i, B_i ≦ N</var> (<var>1 ≦ i ≦ N - 1</var>)</li>
<li><var>1 ≦ V_j ≦ N</var> (<var>1 ≦ j ≦ K</var>) (21:18, a mistake in this constraint was corrected)</li>
<li><var>0 ≦ P_j ≦ 10^5</var> (<var>1 ≦ j ≦ K</var>)</li>
<li>The given graph is a tree.</li>
<li>All <var>v_j</var> are distinct.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
<var>:</var>
<var>A_{N-1}</var> <var>B_{N-1}</var>
<var>K</var>
<var>V_1</var> <var>P_1</var>
<var>V_2</var> <var>P_2</var>
<var>:</var>
<var>V_K</var> <var>P_K</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If it is possible to write integers into all empty vertices so that the condition is satisfied, print <code>Yes</code>. Otherwise, print <code>No</code>.</p>
<p>If it is possible to satisfy the condition, print <var>N</var> lines in addition. The <var>v</var>-th (<var>1 ≦ v ≦ N</var>) of these <var>N</var> lines should contain the integer that should be written into vertex <var>v</var>. If there are multiple ways to satisfy the condition, any of those is accepted.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5
1 2
3 1
4 3
3 5
2
2 6
5 7
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
5
6
6
5
7
</pre>
<p>The figure below shows the tree when Takahashi fell asleep. For each vertex, the integer written beside it represents the index of the vertex, and the integer written into the vertex is the integer written by Takahashi.</p>
<div style="text-align: center;">
<img alt="6da26f89839711a520acdf5c3e1cc309.png" src="https://atcoder.jp/img/arc063/6da26f89839711a520acdf5c3e1cc309.png">
</img></div>
<p>Aoki can, for example, satisfy the condition by writing integers into the remaining vertices as follows:</p>
<div style="text-align: center;">
<img alt="1858d5af5a2c0e51aca39a39d765debb.png" src="https://atcoder.jp/img/arc063/1858d5af5a2c0e51aca39a39d765debb.png">
</img></div>
<p>This corresponds to Sample Output 1. Note that other outputs that satisfy the condition will also be accepted, such as:</p>
<pre>Yes
7
6
8
7
7
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5
1 2
3 1
4 3
3 5
3
2 6
4 3
5 7
</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>4
1 2
2 3
3 4
1
1 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>Yes
0
-1
-2
-3
</pre>
<p>The integers written by Aoki may be negative or exceed <var>10^6</var>.</p></section>
</div>
</span> |
p01974 | <h1>A: 鳩ノ巣原理</h1>
<h2>問題</h2>
<p>
$N$ 個の相違なる自然数 $a_i$ が与えられる。与えられた自然数から相違なる自然数を選び、ペアを作ることにした。
作ることができるペアのうち、値の差が $N - 1$ の倍数であるペアを一つ出力せよ。
</p>
<p>
なお、そのようなペアは必ず存在する。
</p>
<h2>制約</h2>
<ul>
<li>$2 \le N \le 1000$</li>
<li>$1 \le a_i \le 1000$</li>
<li>$a_i\neq a_j \ (i \neq j)$</li>
</ul>
<h2>入力:</h2>
<p>
$N$<br>
$a_1 \cdots a_N$<br>
</p>
<h2>出力:</h2>
<p>
条件を満たすペア $(x, y)$ を空白区切りで一行で出力せよ。また末尾に改行を出力せよ。
</p>
<h2>サンプル</h2>
<h3>サンプル入力 1</h3>
<pre>
5
1 2 4 7 10
</pre>
<h3>サンプル出力 1</h3>
<pre>
2 10
</pre>
<p>
$2$ と $10$ の差は $8$ であり、これは $4$ の倍数である。
$10 \ 2$ でも正解となる。
</p>
<h3>サンプル入力 2</h3>
<pre>
6
1 6 11 16 21 26
</pre>
<h3>サンプル出力 2</h3>
<pre>
16 11
</pre>
<h3>サンプル入力 3</h3>
<pre>
10
486 668 354 649 626 253 830 162 146 363
</pre>
<h3>サンプル出力 3</h3>
<pre>
486 162
</pre>
<!-- - - - - - end nicebody - - - - - -->
|
p00666 |
<H1>Problem H: World domination</H1>
<p>
世界征服を企む悪の組織、なんてものは、古今東西フィクションノンフィクションを問わず、いろんな所にいるものだが、一体彼らは何のために世界征服をしようとしたのだろうか。そんなことをふと考えてしまうのは、私もまた、世界征服を企てているからであった。私が世界征服を目論む理由はただ一つ、私がロボット工学の分野で世界一優秀であることを世界に示すためである。征服したあとの世界になど興味はない、そこらへんの犬にでもくれてやっても構わないのだ。
</p>
<p>
誠に遺憾だが、それはつまり、私よりも優秀であるとされている研究者がいる、ということだ。その人物には心当たりがある。誰あろう、私の学生時代の同期 R 博士である。彼の実績は認めるが、しかし私のそれより優れていたとは思わない。ただ彼は、平和のためのロボット利用などという甘っちょろいことを言っていたがために、教授連中の受けが良かったのだ。主席こそ逃したものの、本当は私の方がずっと優秀なのだ。
</p>
<p>
その優秀さを示す端的な例が、この世界征服のために用意した戦闘ロボットである。戦闘ロボットはその特性に応じた専用の武器を持ち、その武器はチップに収められているが、このチップは強力な互換性を持つ。ただ開発者自身の他の戦闘ロボットと互換性をもつだけではない (その程度のことは、他ならぬ R 博士が既に達成している) 。私のチップのすごいところは、その R 博士の家事手伝いロボットとも互換性があるのだ。その家事手伝いロボットは非売品のワンオフモデルなので、私はそのロボットの設計を知らないで互換チップを開発したのだ。我ながら神業と言わざるを得ない。この仕様を敵が利するだけの無駄な仕様と言う人もいるが、それは愚かな考え方だ。私は私の優秀さを示すために戦闘ロボットを開発しているのだ。ただ勝てばいいというものではないのである。
</p>
<p>
世界征服計画に話を戻そう。私は n 機の戦闘ロボットを製作し、それぞれに異なる武器チップを 1 つずつインストールした。これを用いて世界の主要施設を奪取するつもりだが、この計画を阻むために R 博士は彼の家事手伝いロボットを送り込んでくることだろう。家事手伝いロボットは自身の武器で私の戦闘ロボットに戦闘を挑んでくるが、1 回の戦闘には 1 単位時間かかり、戦闘の結果どちらか片方のロボットが敗北し破壊される。
</p>
<p>
戦闘の結果私のロボットが破壊された場合、そのロボットの武器チップは相手に奪われることになる。ある私のロボットの武器チップは他の私のロボットのうちの 1 機の弱点になっていて、またどの私のロボットも弱点となる武器チップをただ 1 つだけ持つ。私は私の各ロボットについて、相手がそのロボットの弱点武器を持っていた場合の敗北確率・持っていなかった場合の敗北確率をそれぞれ見積もることができる。
</p>
<p>
戦闘の結果相手の家事手伝いロボットが破壊された場合、R 博士はスペアボディ転送システムによって同じ家事手伝いロボットを送り込んでくる。このとき、相手のロボットが既に得ていた武器チップは失われることがない。R 博士は無制限にスペアボディ転送システムを使用してくるが、その隙に私は私の戦闘ロボットを修理することができるので、何度戦っても先に説明した敗北確率は変化することはない。また、スペアボディ転送システムは家事手伝いロボットが破壊されたときに破壊された場所でしか使えないので、家事手伝いロボットは敗北した直後に別の私の戦闘ロボットに挑むことはできない。
</p>
<p>
R 博士は無制限にスペアボディ転送システムを使えるのだから、悔しいが n 機の戦闘ロボットが全て破壊されるのは時間の問題だ。そこで私は戦闘ロボットを戦わせている裏で、スペアボディ転送システムごと破壊できるようなさらなるロボットの開発に勤しむつもりだ。しかし、私には一体どれだけの時間が残されているのだろうか。R 博士のロボットが最も早く私の n 機のロボットを撃破するような戦略を取ったとき、どれだけの時間がかかるだろうか。まずはそれを計算しなければなるまい。ついでに、そのようなロボットの撃破順序が何通りあるかも確認しておきたい。
</p>
<h2>Input</h2>
<p>
入力は複数のケースからなる。
各ケースは以下のフォーマットで与えられる。
</p>
<pre>
<i>n</i>
<i>p<sub>0</sub></i> <i>id<sub>0</sub></i> <i>w<sub>0</sub></i>
<i>p<sub>1</sub></i> <i>id<sub>1</sub></i> <i>w<sub>1</sub></i>
.
.
.
<i>p<sub>1</sub></i> <i>id<sub>n-1</sub></i> <i>w<sub>n-1</sub></i>
</pre>
<p>
<i>p<sub>i</sub></i> は弱点の武器を相手が持っていない時に<i>i</i> 番目の戦闘ロボットが敗北する確率を表す。<br>
<i>id<sub>i</sub></i> は<i>i</i> 番目の戦闘ロボットの弱点となる武器を持っている戦闘ロボットを表す。<br>
<i>w<sub>i</sub></i> は弱点の武器を相手が持っている時に<i>i</i> 番目の戦闘ロボットが敗北する確率を表す。<br>
</pre>
<p>
入力の終わりは<i>n</i> = 0によって与えられる。
</p>
<p>
各値は以下の条件を満たす<br>
2 ≤ n ≤ 100<br>
<i>p<sub>i</sub></i> と <i>w<sub>i</sub></i>は小数点以下3桁の数で表される。<br>
0.001 ≤ <i>p<sub>i</sub></i> < <i>w<sub>i</sub></i> ≤ 1.000<br>
<i>id<sub>i</sub></i> != <i>i</i> <br>
</p>
<p>
テストケースの数は500を超えない。<br>
</p>
<h2>Output</h2>
<p>
n体の戦闘ロボットが撃破されるまでの時間の期待値と、その撃破順序の組み合わせの数を100000007で割った余りを空白で区切って出力せよ。<br>
期待値の値はジャッジの用意した回答との誤差1e-9までが認められる。<br>
</p>
<h2>Sample input</h2>
<pre>
4
0.200 3 0.500
0.100 0 0.400
0.900 1 1.000
0.400 2 0.600
2
0.600 1 0.800
0.500 0 1.000
9
0.200 7 0.600
0.400 0 0.500
0.700 8 1.000
0.100 4 0.400
0.200 2 0.600
0.100 6 0.400
0.100 5 0.400
0.600 3 0.900
0.500 1 0.900
9
0.300 8 0.900
0.700 3 0.800
0.300 0 0.900
0.700 6 0.800
0.200 5 0.700
0.200 2 0.700
0.700 4 0.800
0.700 1 0.800
0.300 7 0.900
0
</pre>
<H2>Sample output</H2>
<pre>
7.27777777778 1
2.66666666667 1
23.98412698413 72
11.36904761905 4
</pre>
<hr>
<p>
The University of Aizu Programming Contest 2011 Summer<br>
原案: Tomoya Sakai<br>
問題文: Takashi Tayama<br>
</p> |
p01427 |
<script type="text/javascript" src="./varmath.js" charset="UTF-8"></script>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE_logo" width="400"><br>
<i>〜魔女からこの世界を守るため<i>キュゥべえ</i>と共に戦う 5 人の魔法少女達の物語〜</i>
</center>
<h1>問題 A</h1>
<h2>問題文</h2>
薔薇園の魔女 GERTRUD は幅 <var>W</var> メートル,高さ <var>H</var> メートルの長方形の庭でバラを育てている.庭は一辺 <var>1</var> メートルの正方形で区切られており,<var>W\times H</var> のブロックに分けられている.各ブロックはバラが育てられているか育てられていないかのどちらかである.
<p>
ただ驚くべきことに,この庭で育てられているバラはブロックを越えて絡み合い <var>1</var> つの生命体となっている.この生命体は辺を共有しているブロック間を越えて絡み合うことができ,どこかある <var>1</var> 点を共有していれば同じ生命体となる.
<p>
<i>巴マミ</i>は庭の左下の角から直線に必殺技「ティロ・フィナーレ」で巨大な銃を撃ち,その弾丸が通る直線上でその生命体を分割した時,生命体が出来る限り多くの分割数に分断されるようにすることによりダメージを与えたい.ティロ・フィナーレは最大でこの生物を何分割できるか求めよ.
<h2>入力形式</h2>
<p>入力は以下の形式で与えられる.</p>
<pre><var>
H\ W\\
f_{11} f_{12} ... f_{1W}\\
f_{21} f_{22} ... f_{2W}\\
...\\
f_{H1} f_{H2} ... f_{HW}\\
</var></pre>
<p>
<var>H</var> は庭の高さ,<var>W</var> は庭の幅を表す.
<p>
<var>f_{ij}</var> は庭の各ブロックを表す文字であり,上から <var>i</var> 行目,左から <var>j</var> 列目のブロックの状態を表す.ブロックにバラが育てられている場合は <var>f_{ij}</var> は <code>'#'</code> となり,そうでない場合は <code>'.'</code> となる.
<h2>出力形式</h2>
生命体を直線で分割する時に,最大でいくつの部分に分割できるかを1行で出力せよ.
<h2>制約</h2>
<ul>
<li><var>1 ≤ H ≤ 600</var></li>
<li><var>1 ≤ W ≤ 600</var></li>
<li><var>f_{ij}</var>は <code>'#'</code>, <code>'.'</code>のいずれかである.</li>
<li>少なくとも <var>1</var> つは<var>f_{ij} = </var><code>'#'</code> となるマス <var>f_{ij}</var> が存在する.</li>
<li><var>2</var> つのブロックが辺を共有するとき,それらは<b>隣接している</b>と呼ぶことにする.任意の <var>2</var> つの異なる <code>'#'</code> のブロック <var>f_A, f_B</var> に対して,<var>f_A</var> から隣接している <code>'#'</code> のブロックを辿っていって <var>f_B</var> に着くことができる.(すなわち,薔薇のブロックは連結である.)</li>
<li>入力中で <var>1</var> 行目の行,<var>H</var> 行目の行,<var>1</var> 列目の列,または <var>W</var> 列目の列にあるブロックを<b>盤面の端のブロック</b>と呼ぶことにする.任意の <code>'.'</code> のブロック <var>f_C</var> に対して,<var>f_C</var> から隣接している <code>'.'</code> のブロックを辿っていって,ある <code>'.'</code> の盤面の端のブロックに着くことができる.(すなわち,入力に"空洞"のブロックは存在しない.)</li>
</ul>
<h2>入出力例</h2>
<h3>入力例 1</h3>
<pre>
3 5
##..#
#..##
####.
</pre>
<h3>出力例1</h3>
<pre>4</pre>
このとき以下のように <var>4</var> つの部分に分割することができる.
<p>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE_laser_example1">
<h3>入力例 2</h3>
<pre>3 3
#.#
###
#.#</pre>
<h3>出力例 2</h3>
<pre>3</pre>
<h3>入力例 3</h3>
<pre>
10 9
.........
.........
####.....
#..#.##..
#..#..#..
#..##.###
#..##...#
##..##.##
###.#..#.
###.####.
</pre>
<h3>出力例 3</h3>
<pre>6</pre>
<h3>入力例 4</h3>
<pre>
10 11
###########
#.#.#.#.#.#
#.#.#.#.#.#
#.#.#.#.#.#
#.#.#.#.#.#
#.#.#.#.#.#
#.#.#.#.#.#
#.#.#.#.#.#
#.#.#.#.#.#
#.#.#.#.#.#
</pre>
<h3>出力例 4</h3>
<pre>7</pre>
<h3>入力例 5</h3>
<pre>
25 38
...........#...............#..........
...........###..........####..........
...........#####.......####...........
............###############...........
............###############...........
............##############............
.............#############............
............###############...........
...........#################..........
.......#...#################...#......
.......##.##################..##......
........####################.##.......
..........####################........
.........#####..########..#####.......
.......######....#####....######......
......########...#####..########.#....
....#######..#...#####..#..########...
..#########.....#######......#######..
...#######......#######........###....
..####.........#########........###...
...............#########..............
..............##########..............
..............##########..............
...............########...............
...............########...............
</pre>
<h3>出力例 5</h3>
<pre>8</pre>
<hr>
<address>Problem Setter: Flat35</address> |
p01077 |
<h1>Problem F: Curling Puzzle</h1>
<h2>Problem</h2>
<p>
カーリングのストーンを移動させて、ゴールのマスにストーンをぴったり重ねるパズルゲームがある。詳しいルールは次のとおりである。
</p>
<p>
フィールドは<var>H</var>×<var>W</var>のマスからなり、はじめにいくつかのマスにストーンが置かれている。
ゴールのマスは1つだけ存在して、はじめからゴールのマスにストーンが置かれていることはない。
また、フィールドは外壁で囲まれており、途中でストーンがフィールドの外へ飛び出してしまうことはない。
</p>
<p>
プレイヤーは1手ごとにストーンを1つ選び、上下左右のいずれかの方向に打ち出して滑らせることができる。
滑っているストーンは別のストーンかフィールドの外壁にぶつかるまで滑り続ける。別のストーンにぶつかった場合は、
ぶつけられた側のストーンが反動を受け、最初に打ち出した方向と同じ方向へ連鎖的に滑り出してしまう。
</p>
<p>例えば、図1の状態でストーンAを右方向に打ち出した場合、最終的に図2のような状態になる。</p>
<center>
<div style="width: 500px;">
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE3_ACPC2016Day2AIZU_F_figure1" alt="図1" style="width: 500px;"><br>
<div style="text-align: center; font-weight: bold;">図1</div><br><br>
</div>
<div style="width: 500px;">
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE3_ACPC2016Day2AIZU_F_figure2" alt="図2" style="width: 500px;"><br>
<div style="text-align: center; font-weight: bold;">図2</div><br><br>
</div>
</center>
<p>
このパズルのクリア条件は、フィールドに存在するストーンのいずれか1つをゴールのマスにぴったり重ねることである。ゴールのマスを通り過ぎた場合にはクリアにならない。
</p>
<p>
あなたの仕事はフィールドの初期状態を入力として受け取り、そのパズルがクリア可能かどうかを出力するプログラムを作成することである。
</p>
<h2>Input</h2>
<p>入力は以下の形式で与えられる。</p>
<pre>
<var>H</var> <var>W</var>
<var>F<sub>11</sub></var> <var>F<sub>12</sub></var> ... <var>F<sub>1W</sub></var>
<var>F<sub>21</sub></var> <var>F<sub>22</sub></var> ... <var>F<sub>2W</sub></var>
...
<var>F<sub>H1</sub></var> <var>F<sub>H2</sub></var> ... <var>F<sub>HW</sub></var>
</pre>
<p>
<var>F<sub>ij</sub></var>は '.' か 'o' か '@' のいずれかであり、それぞれ以下の意味を持つ。
( 1 ≤ <var>i</var> ≤ <var>H</var> , 1 ≤ <var>j</var> ≤ <var>W</var> )</li>
</p>
<ul>
<li>'.' : 空白のマス</li>
<li>'o' : カーリングのストーンが置かれているマス</li>
<li>'@' : ゴールのマス</li>
</ul>
<h2>Constraints</h2>
<ul>
<li>1 ≤ <var>H</var> ≤ 16</li>
<li>1 ≤ <var>W</var> ≤ 16</li>
<li>2 ≤ <var>H</var> × <var>W</var> ≤ 256</li>
<li>ゴールのマスが1つだけ存在することが保証されている</li>
<li>カーリングのストーンが置かれているマスが1つ以上存在することが保障されている</li>
</ul>
<h2>Output</h2>
<p>
パズルがクリア可能かどうか "yes" または "no" (""は含まない)を1行に出力せよ。
</p>
<h2>Sample Input 1</h2>
<pre>
1 10
o........@
</pre>
<h2>Sample Output 1</h2>
<pre>
yes
</pre>
<h2>Sample Input 2</h2>
<pre>
3 6
......
.o..@.
......
</pre>
<h2>Sample Output 2</h2>
<pre>
no
</pre>
<h2>Sample Input 3</h2>
<pre>
6 4
....
.oo.
.oo.
....
.@..
....
</pre>
<h2>Sample Output 3</h2>
<pre>
yes
</pre> |
p01759 |
<script type="text/x-mathjax-config">
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});
</script>
<script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>
<p>
太郎君は Vongress と呼ばれるゲームが大好きである.Vongress とは,<var>n</var> 頂点からなる凸多角形である盤の上で行われる陣取りゲームである.
</p>
<p>
このゲームでは,<var>m</var> 人のプレイヤーが,盤の内側にそれぞれ 1 つ駒を置く.駒の位置は <var>x</var>座標と <var>y</var> 座標の組で表され,駒の大きさは考えない.
</p>
<p>
あるプレイヤーの陣地とは,盤上でその人が置いた駒が最も近くにあるような場所からなる領域である.プレイヤーは,自分の陣地の面積をスコアとして得る.
</p>
<p>
太郎君はいつも盤の形状,置かれた駒の位置,各プレイヤーのスコアを記録していたが,ある日うっかり駒の位置を記録するのを忘れてしまった.そこで太郎君は,盤の形状とプレイヤーの得点から,矛盾のない駒の位置を書き加えることにした.
</p>
<p>
あなたの仕事は,太郎君に代わって各プレイヤーの得点と矛盾しない駒の位置を求めることである.
</p>
<h3>Input</h3>
<p>
入力は以下の形式で与えられる.
</p>
<pre>
<var>n</var> <var>m</var>
<var>x<sub>1</sub></var> <var>y<sub>1</sub></var>
<var>x<sub>2</sub></var> <var>y<sub>2</sub></var>
:
<var>x<sub>n</sub></var> <var>y<sub>n</sub></var>
<var>r<sub>1</sub></var>
<var>r<sub>2</sub></var>
:
<var>r<sub>m</sub></var>
</pre>
<p>
<var>n</var> は盤である凸多角形の頂点数,<var>m</var> はプレイヤーの数を表す.<var>(x<sub>i</sub>, y<sub>i</sub>)</var> は盤の頂点の座標を表す.<var>r<sub>1</sub>, . . . , r<sub>m</sub></var> は各プレイヤーが得たスコアの比を表す.
</p>
<h3>Constraints</h3>
<ul>
<li> 3 ≤ <var>n</var> ≤ 100 </li>
<li> 1 ≤ <var>m</var> ≤ 100 </li>
<li> −10<sup>4</sup> ≤ <var>x<sub>i</sub>, y<sub>i</sub></var> ≤ 10<sup>4</sup></li>
<li> 1 ≤ <var>r<sub>j</sub> ≤ 100</li>
<li> 入力は全て整数である.</li>
<li> 凸多角形の頂点 <var>(x<sub>i</sub>, y<sub>i</sub>)</var> は反時計回りの順番で与えられる.</li>
</ul>
<h3>Output</h3>
<p>
<var>m</var> 個の駒の位置を以下の形式で出力せよ.
</p>
<pre>
<var>x'<sub>1</sub></var> <var>y'<sub>1</sub></var>
<var>x'<sub>2</sub></var> <var>y'<sub>2</sub></var>
:
<var>x'<sub>m</sub></var> <var>y'<sub>m</sub></var>
</pre>
<p>
<var>(x'<sub>k</sub>, y'<sub>k</sub>)</var> は,<var>k</var> 番目のプレイヤーの駒の位置である.
</p>
<p>
出力は次の条件を満たさなければならない.
</p>
<ul>
<li> 駒が凸多角形の内側にある.境界線上にあってもよい.</li>
<li> どの 2 つの駒も 10<sup>−5</sup> 以上距離が離れている.</li>
<li> 与えられる凸多角形の面積を <var>S</var> とする.出力から求まる <var>k</var> 番目のプレイヤーの陣地の面積について, $\frac{r_k}{r_1+...+r_m} S$ との絶対誤差または相対誤差が 10<sup>−3</sup> 未満である.
</ul>
<h3>Sample Input 1</h3>
<pre>
4 5
1 1
-1 1
-1 -1
1 -1
1
1
1
1
1
</pre>
<h3>Output for the Sample Input 1</h3>
<pre>
0.0000000000 0.0000000000
0.6324555320 0.6324555320
-0.6324555320 0.6324555320
-0.6324555320 -0.6324555320
0.6324555320 -0.6324555320
</pre>
<p>
下図のように駒を配置すれば,全プレイヤーが同じスコアを得られる.黒い線分が盤,赤い点がプレイヤーの駒,青い線分がプレイヤーの陣地の境界線をそれぞれ表す.
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_JAGSummer2014Day4_vongress1" title="Vongress" alt="Vongress">
</center>
<br>
<h3>Sample Input 2</h3>
<pre>
4 3
1 1
-1 1
-1 -1
1 -1
9
14
9
</pre>
<h3>Output for the Sample Input 2</h3>
<pre>
-0.5 -0.5
0 0
0.5 0.5
</pre>
<p>
下図のように駒を配置すればよい.
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_JAGSummer2014Day4_vongress2" title="Vongress" alt="Vongress">
</center>
<br>
<h3>Sample Input 3</h3>
<pre>
8 5
2 0
1 2
-1 2
-2 1
-2 -1
0 -2
1 -2
2 -1
3
6
9
3
5
</pre>
<h3>Output for the Sample Input 3</h3>
<pre>
1.7320508076 -0.5291502622
1.4708497378 -0.2679491924
-0.4827258592 0.2204447068
-0.7795552932 0.5172741408
-1.0531143674 0.9276127521
</pre>
<p>
下図のように駒を配置すればよい.
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_JAGSummer2014Day4_vongress3" title="Vongress" alt="Vongress">
</center> |
p03764 | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>On a two-dimensional plane, there are <var>m</var> lines drawn parallel to the <var>x</var> axis, and <var>n</var> lines drawn parallel to the <var>y</var> axis.
Among the lines parallel to the <var>x</var> axis, the <var>i</var>-th from the bottom is represented by <var>y = y_i</var>.
Similarly, among the lines parallel to the <var>y</var> axis, the <var>i</var>-th from the left is represented by <var>x = x_i</var>.</p>
<p>For every rectangle that is formed by these lines, find its area, and print the total area modulo <var>10^9+7</var>.</p>
<p>That is, for every quadruple <var>(i,j,k,l)</var> satisfying <var>1\leq i < j\leq n</var> and <var>1\leq k < l\leq m</var>, find the area of the rectangle formed by the lines <var>x=x_i</var>, <var>x=x_j</var>, <var>y=y_k</var> and <var>y=y_l</var>, and print the sum of these areas modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq n,m \leq 10^5</var></li>
<li><var>-10^9 \leq x_1 < ... < x_n \leq 10^9</var></li>
<li><var>-10^9 \leq y_1 < ... < y_m \leq 10^9</var></li>
<li><var>x_i</var> and <var>y_i</var> are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>n</var> <var>m</var>
<var>x_1</var> <var>x_2</var> <var>...</var> <var>x_n</var>
<var>y_1</var> <var>y_2</var> <var>...</var> <var>y_m</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the total area of the rectangles, modulo <var>10^9+7</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 3
1 3 4
1 3 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>60
</pre>
<p>The following figure illustrates this input:</p>
<p><img alt="sample1-1" src="https://atcoder.jp/img/arc071/aec4d5cc2e5c73dbee455be237a649a5.png"/></p>
<p>The total area of the nine rectangles A, B, ..., I shown in the following figure, is <var>60</var>.</p>
<p><img alt="sample1-2" src="https://atcoder.jp/img/arc071/f0771c0f7e68af2b00e7513186f585ff.png"/></p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
-790013317 -192321079 95834122 418379342 586260100 802780784
-253230108 193944314 363756450 712662868 735867677
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>835067060
</pre></section>
</div>
</span> |
p02876 | <span class="lang-en">
<p>Score : <var>1100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have <var>N</var> balance beams numbered <var>1</var> to <var>N</var>.
The length of each beam is <var>1</var> meters.
Snuke walks on Beam <var>i</var> at a speed of <var>1/A_i</var> meters per second, and Ringo walks on Beam <var>i</var> at a speed of <var>1/B_i</var> meters per second.</p>
<p>Snuke and Ringo will play the following game:</p>
<ul>
<li>First, Snuke connects the <var>N</var> beams in any order of his choice and makes a long beam of length <var>N</var> meters.</li>
<li>Then, Snuke starts at the left end of the long beam. At the same time, Ringo starts at a point chosen uniformly at random on the long beam. Both of them walk to the right end of the long beam.</li>
<li>Snuke wins if and only if he catches up to Ringo before Ringo reaches the right end of the long beam. That is, Snuke wins if there is a moment when Snuke and Ringo stand at the same position, and Ringo wins otherwise.</li>
</ul>
<p>Find the probability that Snuke wins when Snuke arranges the <var>N</var> beams so that the probability of his winning is maximized.</p>
<p>This probability is a rational number, so we ask you to represent it as an irreducible fraction <var>P/Q</var> (to represent <var>0</var>, use <var>P=0, Q=1</var>).</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 A_i,B_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>B_1</var>
<var>A_2</var> <var>B_2</var>
<var>\vdots</var>
<var>A_N</var> <var>B_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the numerator and denominator of the irreducible fraction that represents the maximum probability of Snuke's winning.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2
3 2
1 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1 4
</pre>
<p>When the beams are connected in the order <var>2,1</var> from left to right, the probability of Snuke's winning is <var>1/4</var>, which is the maximum possible value.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4
1 5
4 7
2 1
8 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1 2
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>3
4 1
5 2
6 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0 1
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>10
866111664 178537096
705445072 318106937
472381277 579910117
353498483 865935868
383133839 231371336
378371075 681212831
304570952 16537461
955719384 267238505
844917655 218662351
550309930 62731178
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>697461712 2899550585
</pre></section>
</div>
</span> |
p01309 |
<h1><font color="#000">Problem G:</font> 挨拶の多い本屋さん</h1>
<p>
なつめは大のねこ好きである。なつめはある日、いつも親しくしている野良ねこたちから、ねこたちが開いている不思議な本屋に行こうと誘われた。その本屋ではたくさんのねこの写真が載った本が売られていると聞き、なつめは喜んでついていくことにした。
</p>
<p>
なつめは知らなかったのだが、ねこたちに連れられていった本屋は全国にチェーン店を持つ有名店であった。その店ではしっかりとした店員用マニュアルが用意されており、どの店舗でも店員の行動が統一されている。以下はそのマニュアルからの抜粋である。
</p>
<ol>
<li>店員からユークリッド距離10以内のドアからお客様が入店された際には、 <var>X</var> 秒間かけて大声で「いらっしゃいませこんにちは」といわなければならない。</li>
<li>ユークリッド距離50以内の他の店員が「いらっしゃいませこんにちは」と言っているのを聞いた店員は、その「いらっしゃいませこんにちは」の発声終了時に「いらっしゃいませこんにちは」と <var>X</var> 秒かけて大声で復唱しなければならない。ただし、この復唱の発声開始時から過去 <var>Y</var> 秒以内に、別の「いらっしゃいませこんにちは」を発声完了していた店員は、復唱してはならない。</li>
</ol>
<p>
<var>X</var>, <var>Y</var>, 客の位置、店員の配置が与えられるので、客が入ってから店が静かになるまで何秒かかるか計算せよ。復唱が鳴り止まない場合は、"You're always welcome!"と出力せよ。
</p>
<h2>Input</h2>
<p>
入力の1行目には、店員の数 <var>N</var>、およびマニュアルに書かれている <var>X</var>, <var>Y</var> がそれぞれ1つの空白文字で区切られて与えられる。2行目はなつめが入店する位置であり、それに続く <var>N</var> 行は <var>N</var> 人の店員の位置を表す座標である。それぞれの位置は <var>x</var>, <var>y</var> 座標を1つの空白文字で区切って与えられ、各数値とも0以上1000以下の整数で与えられる。また、1 <= <var>N</var> <= 1000, 1 <= <var>X</var>, <var>Y</var> <= 100を満たす。
<h2>Output</h2>
<p>
復唱が鳴り止む場合は客が入店してから静かになるまでの時間を、鳴り止まない場合は "You're always welcome!" を1行に出力せよ。
</p>
<h2>Notes on Submission</h2>
<p>
上記形式で複数のデータセットが与えられます。入力データの 1 行目にデータセットの数が与えられます。各データセットに対する出力を上記形式で順番に出力するプログラムを作成して下さい。
</p>
<h2>Sample Input</h2>
<pre>
4
3 3 5
0 0
10 0
40 40
40 90
4 5 10
100 100
50 100
50 150
100 50
100 150
4 60 10
100 100
90 100
110 100
100 90
100 110
4 60 10
100 100
80 100
110 100
100 80
100 110
</pre>
<h2>Output for the Sample Input</h2>
<pre>
9
0
60
You're always welcome!
</pre>
|
p03334 | <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> |
p02175 | <h1>A: 不思議な植物</h1>
<h2>問題</h2>
<p>
あるところに不思議な $X$ [cm] の植物が生えている.
この植物は以下の不思議な性質を持っている.
</p>
<ul>
<li>
この植物に "nobiro" と声を掛けると, その植物は $A$ [cm] 成長する.
</li>
<li>
この植物に "tidime" と声を掛けると, その植物は $B$ [cm] 成長する.
</li>
<li>
この植物に "karero" と声をかけると, その植物は $0$ [cm] になる.
</li>
</ul>
<p>
ただし, この植物は負の長さにはならないとする.
具体的には, $C$ [cm] の状態から $D$ [cm] $(C + D \lt 0)$ 成長するとき, 植物は $0$ [cm] になったときに成長が止まる.
</p>
<p>
この植物に $N$ 日間, 毎日一度だけ "nobiro", "tidime", "karero" のうちどれか
一つの言葉をかけた. $N$ 日後の植物の長さ [cm] を求めよ.
</p>
<h2>制約</h2>
<ul>
<li>入力値は全て整数である.</li>
<li>$0 \leq X \leq 100$</li>
<li>$-100 \leq A,B \leq 100$</li>
<li>$1 \leq N \leq 1000$</li>
<li>$S_i$ は "nobiro", "tidime" または "karero" のいずれかである.</li>
</ul>
<p>
</p>
<h2>入力形式</h2>
<p> 入力は以下の形式で与えられる. </p>
<p>
$X\ A\ B$<br>
$N $<br>
$S_1$<br>
$S_2$<br>
$\vdots$<br>
$S_N$<br>
</p>
<h2>出力</h2>
<p>
$N$ 日後の植物の長さを出力せよ. また末尾に改行を出力せよ.
</p>
<h2>サンプル</h2>
<h3>サンプル入力 1</h3>
<pre>
10 30 10
3
nobiro
nobiro
tidime
</pre>
<h3>サンプル出力 1</h3>
<pre>
80
</pre>
<h3>サンプル入力 2</h3>
<pre>
10 -20 -30
1
nobiro
</pre>
<h3>サンプル出力 2</h3>
<pre>
0
</pre>
|
p00148 |
<H1>キャンディーとクラス旗</H1>
<p>
3年C組では、平成19年11月10日の体育祭で使用する「クラス旗」を、将来のクラス会の時にも使うことにしました。そこで「クラス旗」を保管する生徒を決めるために、先生が先日差し入れてくれた大量のキャンディーを使って次のようなゲームを行うことにしました。
</p>
<ul>
<li>各生徒は生徒番号の順に1個ずつキャンディーを取ります。</li>
<li>一巡してもキャンディーが残っていたら、最初の生徒番号の人から順々にキャンディーを取り続けます。</li>
<li>最後のキャンディーを取った人が「クラス旗」を保管する生徒になります。</li>
</ul>
<p>
3年C組のクラスの人数は 39 人です。彼らの生徒番号は 3C01 から 3C39 です。例えば、キャンディーの数が 50 個の場合、クラス全員が1個目のキャンディーを取り終えると、キャンディーの残りは 11 個となります。それを再び生徒番号順に取ると、最後の 1 個は、3C11 の生徒が取ることとなります。すなわち 3C11 の生徒が「クラス旗」を保管する生徒となります。
</p>
<p>
キャンディーの個数を入力とし、「クラス旗」を保管する生徒の生徒番号を出力するプログラムを作成してください。
</p>
<H2>Input</H2>
<p>
複数のテストケースが与えられます。各テストケースは以下の形式で与えられます。各テストケースとして、キャンディーの個数を表す整数 <var>a</var> (1 ≤ <var>a</var> ≤ 10000) が1行に与えられます。入力の最後(EOF)まで処理してください。
</p>
<p>
データセットの数は 20 を超えません。
</p>
<H2>Output</H2>
<p>
各テストケースごとに、「クラス旗」を保管する生徒の生徒番号(半角英数字)を1行に出力してください。
</p>
<H2>Sample Input</H2>
<pre>
50
5576
5577
5578
</pre>
<H2>Output for the Sample Input</H2>
<pre>
3C11
3C38
3C39
3C01
</pre>
|
p00518 |
<H1>問題 4: 部活のスケジュール表 (Schedule)
</H1>
<br/>
<h2>問題</h2>
<p>
IOI 高校のプログラミング部には,J 君と O 君と I 君の 3 人の部員がいる.
プログラミング部では,部活のスケジュールを組もうとしている.
</p>
<p>
今,N 日間の活動のスケジュールを決めようとしている.
各活動日のスケジュールとして考えられるものは,各部員については活動に参加するかどうかの 2 通りがあり,部としては全部で 8 通りある.
部室の鍵はただ 1 つだけであり,最初は J 君が持っている.
各活動日には,その日の活動に参加する部員のうちのいずれか 1 人が鍵を持っている必要があり,活動後に参加した部員のいずれかが鍵を持って帰る.
</p>
<p>
プログラミング部では,活動日には毎回必ず活動が行われるように,あらかじめ各活動日の責任者を定めた.
責任者は,必ずその日の活動に出席しなければならない.
</p>
<p>
スケジュールを決めようとしている日数と,各活動日の責任者が誰であるかの情報が与えられた時,すべての活動日に部活動を行うことができるようなスケジュール表として考えられるものの数を 10007 で割った余りを求めるプログラムを作成せよ.
ただし,部活の終了時に鍵を持って帰る部員は,その日の活動に参加している部員の誰でもよく,最終日は誰が鍵を持って帰ってもよいものとする.
</p>
<h2>入力</h2>
<p>
入力は,2 行からなる.
</p>
<p>
1 行目には,スケジュールを決めようとしている日数を表す 1 つの整数 N (2 ≤ N ≤ 1000) が書かれている.
</p>
<p>
2 行目には,各活動日の責任者を表す N 文字からなる文字列が書かれている.
この文字列の i 文字目 (1 ≤ i ≤ N) は,i 日目の活動日の責任者を表す.
すなわち,i 文字目が J, O, I であることはそれぞれ i 日目の活動日の責任者が J 君,O 君,I 君であることを意味する.
</p>
<h2>出力</h2>
<p>
スケジュール表として考えられるものの数を 10007 で割った余りを 1 行で出力せよ.
</p>
<h2>入出力例</h2>
<h3>入力例 1</h3>
<pre>
2
OI
</pre>
<h3>出力例 1</h3>
<pre>
7
</pre>
<p>
入出力例 1 において,2 日間の活動日のスケジュールを考える.1 日目の責任者は O 君,2 日目の責任者は I 君である.
問題文の条件をみたすスケジュール表は 7 通り考えられる.
</p>
<table style="margin-left: 50px; margin-right: 50px;">
<tr>
<th></th>
<th> 1 日目 </th>
<th> 2 日目 </th>
</tr>
<tr>
<th>スケジュール 1 </th>
<td align="center">J, O</td>
<td align="center">O, I</td>
</tr>
<tr>
<th>スケジュール 2 </th>
<td align="center">J, O</td>
<td align="center">J, I</td>
</tr>
<tr>
<th>スケジュール 3 </th>
<td align="center">J, O</td>
<td align="center">J, O, I</td>
</tr>
<tr>
<th>スケジュール 4 </th>
<td align="center">J, O, I</td>
<td align="center">I</td>
</tr>
<tr>
<th>スケジュール 5 </th>
<td align="center">J, O, I</td>
<td align="center">J, I</td>
</tr>
<tr>
<th>スケジュール 6 </th>
<td align="center">J, O, I</td>
<td align="center">O, I</td>
</tr>
<tr>
<th>スケジュール 7 </th>
<td align="center">J, O, I</td>
<td align="center">J, O, I</td>
</tr>
</table>
<br>
<p>
この表では,J, O, I はそれぞれその日に J 君, O 君, I 君が参加することを表す.
1 日目の責任者は O 君であるが,最初鍵を持っているのは J 君であるため,1 日目の活動には J 君, O 君の両方が参加する必要があることに注意せよ.
また,1 日目に鍵を持って帰った人は 2 日目にも参加しないといけないので,1 日目と 2 日目の両方に参加する人が少なくとも 1 人存在しなければならないことに注意せよ.
</p>
<br>
<h3>入力例 2</h3>
<pre>
20
JIOIJOIJOJOIIIOJIOII
</pre>
<h3>出力例 2</h3>
<pre>
4976
</pre>
<p>
入出力例 2 において,条件をみたすスケジュール表は全部で 72493594992 通り考えられる.それを 10007 で割った余りである 4976 を出力する.
</p>
<div class="source">
<p class="source">
問題文と自動審判に使われるデータは、<a href="http://www.ioi-jp.org">情報オリンピック日本委員会</a>が作成し公開している問題文と採点用テストデータです。
</p>
</div>
|
p02030 | <h2>A: 情報検索 / Information Search</h2>
<h3>問題</h3>
<p>ポスティングリストとは、検索語と出現文書 ID の対応関係を持たせたリストである。例えば</p>
<ul>
<li>北海道: 1, 2, 4, 9</li>
<li>観光: 1, 3, 4, 7</li>
</ul>
<p>などである。</p>
<p>上記のポスティングリストから、and 検索をすると、ID 1, 4 の文書がヒットし、or 検索をすると、ID 1, 2, 3, 4, 7, 9 がヒットする。</p>
<p>ここで and 検索とは「どちらのリストにも含まれる要素を列挙する」ことであり、 or 検索とは「少なくともどちらか一方のリストに含まれる要素を列挙する」ことである。</p>
<p>ポスティングリストが与えられるので、and 検索と or 検索の結果をそれぞれ出力せよ。</p>
<h3>入力形式</h3>
<pre>
<var>n</var> <var>m</var>
<var>a_1</var> <var>a_2</var> $\ldots$ <var>a_n</var>
<var>b_1</var> <var>b_2</var> $\ldots$ <var>b_m</var>
</pre>
<p>入力はすべて整数からなる。</p>
<p>1 行目には検索すべき 2 つのポスティングリストの長さ <var>n</var> と <var>m</var> が空白区切りで与えられる。</p>
<p>2 行目と 3 行目にはそれぞれのポスティングリストに含まれる ID が空白区切りで与えられる。</p>
<h3>制約</h3>
<ul>
<li> <var>1 \leq n, m \leq 2\times10^5</var></li>
<li> <var>a_i</var> < <var>a_j</var> (<var>i < j</var>)</li>
<li> <var>b_i</var> < <var>b_j</var> (<var>i < j</var>)</li>
<li> <var>1 \leq a_i, b_i \leq 10^9</var></li>
</ul>
<h3>出力形式</h3>
<p>and 検索のヒット数を <var>A</var>、or 検索のヒット数を <var>B</var> とする。</p>
<p>最初の一行に <var>A</var> <var>B</var> の順に空白区切りで出力せよ。</p>
<p>続く <var>A</var> 行に and 検索でヒットした ID を昇順に出力せよ。</p>
<p>続く <var>B</var> 行に or 検索でヒットした ID を昇順に出力せよ。</p>
<h3>入力例1</h3>
<pre>
4 4
1 2 4 9
1 3 4 7
</pre>
<h3>出力例1</h3>
<pre>
2 6
1
4
1
2
3
4
7
9
</pre>
<h3>入力例2</h3>
<pre>
4 4
1 3 5 7
2 4 6 8
</pre>
<h3>出力例2</h3>
<pre>
0 8
1
2
3
4
5
6
7
8
</pre>
<h3>入力例3</h3>
<pre>
3 5
1 2 3
1 2 3 4 5
</pre>
<h3>出力例3</h3>
<pre>
3 5
1
2
3
1
2
3
4
5
</pre>
|
p02460 | <h1>Map: Delete</h1>
<p>
For a dictionary $M$ that stores elements formed by a pair of a string key and an integer value, perform a sequence of the following operations. Note that <u>each key in $M$ must be unique</u>.
</p>
<ul>
<li>insert($key$, $x$): Insert an element formed by a pair of $key$ and $x$ to $M$.</li>
<li>get($key$): Print the value with the specified $key$. <u>Print 0 if there is no such element</u>.</li>
<li>delete($key$): Delete the element with the specified $key$.</li>
</ul>
<h2>Input</h2>
<p>
The input is given in the following format.
</p>
<pre>
$q$
$query_1$
$query_2$
:
$query_q$
</pre>
<p>
Each query $query_i$ is given by
</p>
<pre>
0 $key$ $x$
</pre>
<p>or</p>
<pre>
1 $key$
</pre>
<p>or</p>
<pre>
2 $key$
</pre>
<p>
where the first digits <span>0</span>, <span>1</span> and <span>2</span> represent insert, get and delete operations respectively.
</p>
<h2>Output</h2>
<p>
For each get operation, print an integer in a line.
</p>
<h2>Constraints</h2>
<ul>
<li>$1 \leq q \leq 200,000$</li>
<li>$1 \leq x \leq 1,000,000,000$</li>
<li>$1 \leq $ length of $key$ $ \leq 20$ </li>
<li>$key$ consits of lower case letters</li>
</ul>
<h2>Sample Input 1</h2>
<pre>
8
0 blue 4
0 red 1
0 white 5
1 red
1 blue
2 red
1 black
1 red
</pre>
<h2>Sample Output 1</h2>
<pre>
1
4
0
0
</pre>
|
p03621 | <span class="lang-en">
<p>Score : <var>1700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You have two strings <var>A = A_1 A_2 ... A_n</var> and <var>B = B_1 B_2 ... B_n</var> of the same length consisting of <var>0</var> and <var>1</var>.
The number of <var>1</var>'s in <var>A</var> and <var>B</var> is equal.</p>
<p>You've decided to transform <var>A</var> using the following algorithm:</p>
<ul>
<li>Let <var>a_1</var>, <var>a_2</var>, ..., <var>a_k</var> be the indices of <var>1</var>'s in <var>A</var>.</li>
<li>Let <var>b_1</var>, <var>b_2</var>, ..., <var>b_k</var> be the indices of <var>1</var>'s in <var>B</var>.</li>
<li>Replace <var>a</var> and <var>b</var> with their random permutations, chosen independently and uniformly.</li>
<li>For each <var>i</var> from <var>1</var> to <var>k</var>, in order, swap <var>A_{a_i}</var> and <var>A_{b_i}</var>.</li>
</ul>
<p>Let <var>P</var> be the probability that strings <var>A</var> and <var>B</var> become equal after the procedure above.</p>
<p>Let <var>Z = P \times (k!)^2</var>. Clearly, <var>Z</var> is an integer.</p>
<p>Find <var>Z</var> modulo <var>998244353</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq |A| = |B| \leq 10,000</var></li>
<li><var>A</var> and <var>B</var> consist of <var>0</var> and <var>1</var>.</li>
<li><var>A</var> and <var>B</var> contain the same number of <var>1</var>'s.</li>
<li><var>A</var> and <var>B</var> contain at least one <var>1</var>.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Partial Score</h3><ul>
<li><var>1200</var> points will be awarded for passing the testset satisfying <var>1 \leq |A| = |B| \leq 500</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>A</var>
<var>B</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the value of <var>Z</var> modulo <var>998244353</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>1010
1100
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p>After the first two steps, <var>a = [1, 3]</var> and <var>b = [1, 2]</var>. There are <var>4</var> possible scenarios after shuffling <var>a</var> and <var>b</var>:</p>
<ul>
<li><var>a = [1, 3]</var>, <var>b = [1, 2]</var>. Initially, <var>A = 1010</var>. After swap(<var>A_1</var>, <var>A_1</var>), <var>A = 1010</var>. After swap(<var>A_3</var>, <var>A_2</var>), <var>A = 1100</var>.</li>
<li><var>a = [1, 3]</var>, <var>b = [2, 1]</var>. Initially, <var>A = 1010</var>. After swap(<var>A_1</var>, <var>A_2</var>), <var>A = 0110</var>. After swap(<var>A_3</var>, <var>A_1</var>), <var>A = 1100</var>.</li>
<li><var>a = [3, 1]</var>, <var>b = [1, 2]</var>. Initially, <var>A = 1010</var>. After swap(<var>A_3</var>, <var>A_1</var>), <var>A = 1010</var>. After swap(<var>A_1</var>, <var>A_2</var>), <var>A = 0110</var>.</li>
<li><var>a = [3, 1]</var>, <var>b = [2, 1]</var>. Initially, <var>A = 1010</var>. After swap(<var>A_3</var>, <var>A_2</var>), <var>A = 1100</var>. After swap(<var>A_1</var>, <var>A_1</var>), <var>A = 1100</var>.</li>
</ul>
<p>Out of <var>4</var> scenarios, <var>3</var> of them result in <var>A = B</var>. Therefore, <var>P = 3</var> / <var>4</var>, and <var>Z = 3</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>01001
01001
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>4
</pre>
<p>No swap ever changes <var>A</var>, so we'll always have <var>A = B</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>101010
010101
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>36
</pre>
<p>Every possible sequence of three swaps puts the <var>1</var>'s in <var>A</var> into the right places.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>1101011011110
0111101011101
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>932171449
</pre></section>
</div>
</span> |
p02933 | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You will be given an integer <var>a</var> and a string <var>s</var> consisting of lowercase English letters as input.</p>
<p>Write a program that prints <var>s</var> if <var>a</var> is not less than <var>3200</var> and prints <code>red</code> if <var>a</var> is less than <var>3200</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2800 \leq a < 5000</var></li>
<li><var>s</var> is a string of length between <var>1</var> and <var>10</var> (inclusive).</li>
<li>Each character of <var>s</var> is a lowercase English letter.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>a</var>
<var>s</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If <var>a</var> is not less than <var>3200</var>, print <var>s</var>; if <var>a</var> is less than <var>3200</var>, print <code>red</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3200
pink
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>pink
</pre>
<p><var>a = 3200</var> is not less than <var>3200</var>, so we print <var>s =</var> <code>pink</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3199
pink
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>red
</pre>
<p><var>a = 3199</var> is less than <var>3200</var>, so we print <code>red</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>4049
red
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>red
</pre>
<p><var>a = 4049</var> is not less than <var>3200</var>, so we print <var>s =</var> <code>red</code>.</p></section>
</div>
</span> |
p02899 | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3>
<p>Takahashi is a teacher responsible for a class of <var>N</var> students.</p>
<p>The students are given distinct student numbers from <var>1</var> to <var>N</var>.</p>
<p>Today, all the students entered the classroom at different times.</p>
<p>According to Takahashi's record, there were <var>A_i</var> students in the classroom when student number <var>i</var> entered the classroom (including student number <var>i</var>).</p>
<p>From these records, reconstruct the order in which the students entered the classroom.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3>
<ul>
<li><var> 1 \le N \le 10^5 </var></li>
<li><var> 1 \le A_i \le N </var></li>
<li><var> A_i \neq A_j </var> <var>(i \neq 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>A_1</var> <var>A_2</var> <var>\ldots</var> <var>A_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3>
<p>Print the student numbers of the students in the order the students entered the classroom.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
2 3 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3 1 2
</pre>
<p>First, student number <var>3</var> entered the classroom.</p>
<p>Then, student number <var>1</var> entered the classroom.</p>
<p>Finally, student number <var>2</var> entered the classroom.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5
1 2 3 4 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1 2 3 4 5
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>8
8 2 7 3 4 5 6 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>8 2 4 5 6 7 3 1
</pre></section>
</div>
</span> |
p03271 | <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> |
p01866 |
<!-- - - - - - begin nicebody - - - - - -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}
});
</script>
<script type="text/javascript" async
src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML">
</script>
<h1>B: ハミング距離 / Hamming Distance</h1>
<h2>問題文</h2>
<p>
Leading-zeros を含めた桁数が $N$ の,符号なし $2$ 進数表記された整数 $X$ がある.
$X$ とのハミング距離が $D$ となる $N$ 桁で $2$ 進数表記できる非負整数のうち,
値が最大のものを出力せよ.
</p>
<p>
$2$ 進数表記された整数間のハミング距離とは,
$2$ つの数において値が異なる桁の数である.例えば $000$ と $110$ のハミング距離は $2$ である.
</p>
<h2>入力</h2>
<p>
$N$<br>
$X$<br>
$D$<br>
</p>
<h2>制約</h2>
<p>
$1 \leq N \leq 1000$<br>
$0 \leq D \leq N$<br>
入力は全て非負整数である<br>
</p>
<h2>出力</h2>
<p>
答えの整数を符号なし $2$ 進数表記で $1$ 行に出力せよ.
</p>
<h2>サンプル</h2>
<h3>サンプル入力1</h3>
<pre>
5
00001
3
</pre>
<h3>サンプル出力1</h3>
<pre>
11101
</pre>
<h3>サンプル入力2</h3>
<pre>
7
0110100
4
</pre>
<h3>サンプル出力2</h3>
<pre>
1111111
</pre>
<h3>サンプル入力3</h3>
<pre>
18
110001001110100100
6
</pre>
<h3>サンプル出力3</h3>
<pre>
111111111111100100
</pre>
<h3>サンプル入力4</h3>
<pre>
3
000
0
</pre>
<h3>サンプル出力4</h3>
<pre>
000
</pre>
<!-- - - - - - end nicebody - - - - - --> |
p00774 |
<h3>Chain Disappearance Puzzle</h3>
<p>
We are playing a puzzle.
An upright board with <i>H</i> rows by 5 columns of cells, as shown in
the figure below, is used in this puzzle.
A stone engraved with a digit, one of 1 through 9, is placed in each of
the cells.
When three or more stones in horizontally adjacent cells are engraved
with the same digit, the stones will disappear.
If there are stones in the cells above the cell with a disappeared
stone, the stones in the above cells will drop down, filling the
vacancy.
</p>
<br>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_domestic2014_B_board" border="0" width="300">
</center>
<br>
<p>
The puzzle proceeds taking the following steps.
</p>
<ol>
<li>When three or more stones in horizontally adjacent cells are
engraved with the same digit, the stones will disappear. Disappearances
of all such groups of stones take place simultaneously.</li>
<li>When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. </li>
<li>After the completion of all stone drops, if one or more groups of
stones satisfy the disappearance condition, repeat by returning to the
step 1.</li>
</ol>
<p>
The score of this puzzle is the sum of the digits on the disappeared stones.
</p>
<p>
Write a program that calculates the score of given configurations of stones.
</p>
<br>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_domestic2014_B_disappear" border="0" width="500">
</center>
<br>
<h3>Input</h3>
<p>
The input consists of multiple datasets.
Each dataset is formed as follows.
</p>
<blockquote>
Board height <i>H</i><br>
Stone placement of the row 1<br>
Stone placement of the row 2<br>
...<br>
Stone placement of the row <i>H</i><br>
</blockquote>
<p>
The first line specifies the height ( <i>H </i> ) of the puzzle board (1 ≤ <i>H</i> ≤ 10).
The remaining <i>H</i> lines give placement of stones on each of the rows from top to bottom.
The placements are given by five digits (1 through 9), separated by a space.
These digits are engraved on the five stones in the corresponding row, in the same order.
</p>
<p>
The input ends with a line with a single zero.
</p>
<h3>Output</h3>
<p>
For each dataset, output the score in a line.
Output lines may not include any characters except the digits expressing the scores.
</p>
<h3>Sample Input</h3>
<pre>1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
</pre>
<h3>Output for the Sample Input</h3>
<pre>36
38
99
0
72
</pre>
|
p02749 | <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3>
<p>We have a tree with <var>N</var> vertices. The vertices are numbered <var>1</var> to <var>N</var>, and the <var>i</var>-th edge connects Vertex <var>a_i</var> and Vertex <var>b_i</var>.</p>
<p>Takahashi loves the number <var>3</var>. He is seeking a permutation <var>p_1, p_2, \ldots , p_N</var> of integers from <var>1</var> to <var>N</var> satisfying the following condition:</p>
<ul>
<li>For every pair of vertices <var>(i, j)</var>, if the distance between Vertex <var>i</var> and Vertex <var>j</var> is <var>3</var>, the sum or product of <var>p_i</var> and <var>p_j</var> is a multiple of <var>3</var>.</li>
</ul>
<p>Here the distance between Vertex <var>i</var> and Vertex <var>j</var> is the number of edges contained in the shortest path from Vertex <var>i</var> to Vertex <var>j</var>.</p>
<p>Help Takahashi by finding a permutation that satisfies the condition.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3>
<ul>
<li><var>2\leq N\leq 2\times 10^5</var></li>
<li><var>1\leq a_i, b_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>a_1</var> <var>b_1</var>
<var>a_2</var> <var>b_2</var>
<var>\vdots</var>
<var>a_{N-1}</var> <var>b_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3>
<p>If no permutation satisfies the condition, print <code>-1</code>.</p>
<p>Otherwise, print a permutation satisfying the condition, with space in between.
If there are multiple solutions, you can print any of them.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5
1 2
1 3
3 4
3 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1 2 5 4 3
</pre>
<p>The distance between two vertices is <var>3</var> for the two pairs <var>(2, 4)</var> and <var>(2, 5)</var>.</p>
<ul>
<li><var>p_2 + p_4 = 6</var></li>
<li><var>p_2\times p_5 = 6</var></li>
</ul>
<p>Thus, this permutation satisfies the condition.</p></section>
</div>
</span> |
p00324 |
<script type="text/x-mathjax-config">
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});
</script>
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML">
</script>
<H1>完全平等二国間貿易</H1>
<p>
サイバースペースにあるアイヅ国はワカマツ国と情報貿易を行っています。2つの国はお互いに有用なデータを交換することで経済発展を遂げています。博愛と平等、そして何よりも会津地方の古い言葉である、「ならぬことはならぬものです」を国是とする両国は、定期的に貿易状況の調査を行っています。
</p>
<p>
調査では、バイト単位でアイヅ国から見たデータ流入量から流出量を引いた値を、1ナノ秒ごとに求めた表が与えられます。その表から、値の総和が0になる最長の区間を見つけます。この区間が長いほど、平等性が保たれていると判断します。
</p>
<p>
貿易状況が記録された表が与えられたとき、値の総和が0になる最長の区間の長さを求めるプログラムを作成せよ。
</p>
<h2>Input</h2>
<p>
入力は以下の形式で与えられる。
</p>
<pre>
<var>N</var>
<var>d<sub>1</sub></var>
<var>d<sub>2</sub></var>
:
<var>d<sub>N</sub></var>
</pre>
<p>
1行目に、表に書かれた値の数 <var>N</var> (1 ≤ <var>N</var> ≤ 200000) が与えられる。続く <var>N</var> 行に、表の <var>i</var> 行目に書かれた値を示す整数 <var>d<sub>i</sub></var> (-10<sup>9</sup> ≤ <var>d<sub>i</sub></var> ≤ 10<sup>9</sup>) が与えられる。
</p>
<h2>Output</h2>
<p>
表から得られる、総和が0になる最長の区間の長さを1行に出力する。そのような区間が存在しない場合、「0」を1行に出力する。
</p>
<h2>Sample Input 1</h2>
<pre>
5
18
102
-155
53
32
</pre>
<h2>Sample Output 1</h2>
<pre>
3
</pre>
<p>
入力例1では、2行目から4行目までの値の総和が0になるので、最長の区間の長さが3になる。
</p>
<br/>
<h2>Sample Input 2</h2>
<pre>
4
1
1
-1
-1
</pre>
<h2>Sample Output 2</h2>
<pre>
4
</pre>
<p>
入力例2では、2行目から3行目の総和が0になるが、1行目から4行目までの値の総和も0になるので、最長の区間の長さが4になる。
</p> |
p02319 |
<H1>0-1 Knapsack Problem II</H1>
<br/>
<p>
You have <var>N</var> items that you want to put them into a knapsack. Item <var>i</var> has value <var>v<sub>i</sub></var> and weight <var>w<sub>i</sub></var>.
</p>
<p>
You want to find a subset of items to put such that:
</p>
<ul>
<li>The total value of the items is as large as possible.</li>
<li>The items have combined weight at most <var>W</var>, that is capacity of the knapsack.</li>
</ul>
<p>
Find the maximum total value of items in the knapsack.
</p>
<H2>Input</H2>
<pre>
<var>N</var> <var>W</var>
<var>v<sub>1</sub></var> <var>w<sub>1</sub></var>
<var>v<sub>2</sub></var> <var>w<sub>2</sub></var>
:
<var>v<sub>N</sub></var> <var>w<sub>N</sub></var>
</pre>
<p>
The first line consists of the integers <var>N</var> and <var>W</var>. In the following <var>N</var> lines, the value and weight of the <var>i</var>-th item are given.
</p>
<H2>Output</H2>
<p>
Print the maximum total values of the items in a line.
</p>
<h2>Constraints</h2>
<ul>
<li> 1 ≤ <var>N</var> ≤ 100</li>
<li> 1 ≤ <var>v<sub>i</sub></var> ≤ 100</li>
<li> 1 ≤ <var>w<sub>i</sub></var> ≤ 10,000,000</li>
<li> 1 ≤ <var>W</var> ≤ 1,000,000,000</li>
</ul>
<H2>Sample Input 1</H2>
<pre>
4 5
4 2
5 2
2 1
8 3
</pre>
<H2>Sample Output 1</H2>
<pre>
13
</pre>
<br/>
<H2>Sample Input 2</H2>
<pre>
2 20
5 9
4 10
</pre>
<H2>Sample Output 2</H2>
<pre>
9
</pre> |
p03158 | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There are <var>N</var> cards. The <var>i</var>-th card has an integer <var>A_i</var> written on it.
For any two cards, the integers on those cards are different.</p>
<p>Using these cards, Takahashi and Aoki will play the following game:</p>
<ul>
<li>Aoki chooses an integer <var>x</var>.</li>
<li>Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:<ul>
<li>Takahashi should take the card with the largest integer among the remaining card.</li>
<li>Aoki should take the card with the integer closest to <var>x</var> among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.</li>
</ul>
</li>
<li>The game ends when there is no card remaining.</li>
</ul>
<p>You are given <var>Q</var> candidates for the value of <var>x</var>: <var>X_1, X_2, ..., X_Q</var>.
For each <var>i</var> (<var>1 \leq i \leq Q</var>), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses <var>x = X_i</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq N \leq 100</var> <var>000</var></li>
<li><var>1 \leq Q \leq 100</var> <var>000</var></li>
<li><var>1 \leq A_1 < A_2 < ... < A_N \leq 10^9</var></li>
<li><var>1 \leq X_i \leq 10^9</var> (<var>1 \leq i \leq Q</var>)</li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>Q</var>
<var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var>
<var>X_1</var>
<var>X_2</var>
<var>:</var>
<var>X_Q</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <var>Q</var> lines. The <var>i</var>-th line (<var>1 \leq i \leq Q</var>) should contain the answer for <var>x = X_i</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5 5
3 5 7 11 13
1
4
9
10
13
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>31
31
27
23
23
</pre>
<p>For example, when <var>x = X_3(= 9)</var>, the game proceeds as follows:</p>
<ul>
<li>Takahashi takes the card with <var>13</var>.</li>
<li>Aoki takes the card with <var>7</var>.</li>
<li>Takahashi takes the card with <var>11</var>.</li>
<li>Aoki takes the card with <var>5</var>.</li>
<li>Takahashi takes the card with <var>3</var>.</li>
</ul>
<p>Thus, <var>13 + 11 + 3 = 27</var> should be printed on the third line.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4 3
10 20 30 40
2
34
34
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>70
60
60
</pre></section>
</div>
</span> |
p01165 |
<H1><font color="#000">Problem C:</font> Disarmament of the Units</H1>
<p>
This is a story in a country far away. In this country, two armed groups, ACM and ICPC, have
fought in the civil war for many years. However, today October 21, reconciliation was approved
between them; they have marked a turning point in their histories.
</p>
<p>
In this country, there are a number of roads either horizontal or vertical, and a town is built on
every endpoint and every intersection of those roads (although only one town is built at each
place). Some towns have units of either ACM or ICPC. These units have become unneeded
by the reconciliation. So, we should make them disarm in a verifiable way simultaneously. All
disarmament must be carried out at the towns designated for each group, and only one unit can
be disarmed at each town. Therefore, we need to move the units.
</p>
<p>
The command of this mission is entrusted to you. You can have only one unit moved by a
single order, where the unit must move to another town directly connected by a single road.
You cannot make the next order to any unit until movement of the unit is completed. The unit
cannot pass nor stay at any town another unit stays at. In addition, even though reconciliation
was approved, members of those units are still irritable enough to start battles. For these
reasons, two units belonging to different groups may not stay at towns on the same road. You
further have to order to the two groups alternatively, because ordering only to one group may
cause their dissatisfaction. In spite of this restriction, you can order to either group first.
</p>
<p>
Your job is to write a program to find the minimum number of orders required to complete the
mission of disarmament.
</p>
<H2>Input</H2>
<p>
The input consists of multiple datasets. Each dataset has the following format:
</p>
<pre>
<i>n m<sub>A</sub> m<sub>I</sub></i>
<i>rx</i><sub>1,1</sub> <i>ry</i><sub>1,1</sub> <i>rx</i><sub>1,2</sub> <i>ry</i><sub>1,2</sub>
...
<i>rx</i><sub><i>n</i>,1</sub> <i>ry</i><sub><i>n</i>,1</sub> <i>rx</i><sub><i>n</i>,2</sub> <i>ry</i><sub><i>n</i>,2</sub>
<i>sx</i><sub><i>A</i>,1</sub> <i>sy</i><sub><i>A</i>,1</sub>
...
<i>sx</i><sub><i>A</i>,<i>m<sub>A</sub></i></sub> <i>sy</i><sub><i>A</i>,<i>m<sub>A</sub></i></sub>
<i>sx</i><sub><i>I</i>,1</sub> <i>sy</i><sub><i>I</i>,1</sub>
...
<i>sx</i><sub><i>I</i>,<i>m<sub>I</sub></i></sub> <i>sy</i><sub><i>I</i>,<i>m<sub>I</sub></i></sub>
<i>tx</i><sub><i>A</i>,1</sub> <i>ty</i><sub><i>A</i>,1</sub>
...
<i>tx</i><sub><i>A</i>,<i>m<sub>A</sub></i></sub> <i>ty</i><sub><i>A</i>,<i>m<sub>A</sub></i></sub>
<i>tx</i><sub><i>I</i>,1</sub> <i>ty</i><sub><i>I</i>,1</sub>
...
<i>tx</i><sub><i>I</i>,<i>m<sub>I</sub></i></sub> <i>ty</i><sub><i>I</i>,<i>m<sub>I</sub></i></sub>
</pre>
<p>
<i>n</i> is the number of roads. <i>m<sub>A</sub></i> and <i>m<sub>I</sub></i> are the number of units in ACM and ICPC respectively
(<i>m<sub>A</sub></i> > 0, <i>m<sub>I</sub></i> > 0, <i>m<sub>A</sub></i> + <i>m<sub>B</sub></i> < 8). (<i>rx</i><sub><i>i</i>,1</sub>, <i>ry</i><sub><i>i</i>,1</sub> ) and (<i>rx</i><sub><i>i</i>,2</sub>, <i>ry</i><sub><i>i</i>,2</sub> ) indicate the two endpoints of the
<i>i</i>-th road, which is always parallel to either <i>x</i>-axis or <i>y</i>-axis. A series of (<i>sx</i><sub><i>A</i>,<i>i</i></sub>, <i>sy</i><sub><i>A</i>,<i>i</i></sub> ) indicates
the coordinates of the towns where the ACM units initially stay, and a series of (<i>sx</i><sub><i>I</i>,<i>i</i></sub>, <i>sy</i><sub><i>I</i>,<i>i</i></sub> )
indicates where the ICPC units initially stay. A series of (<i>tx</i><sub><i>A</i>,<i>i</i></sub>, <i>ty</i><sub><i>A</i>,<i>i</i></sub> ) indicates the coordinates
of the towns where the ACM units can be disarmed, and a series of (<i>tx</i><sub><i>I</i>,<i>i</i></sub> , <i>ty</i><sub><i>I</i>,<i>i</i></sub> ) indicates where
the ICPC units can be disarmed.
</p>
<p>
You may assume the following:
</p>
<ul>
<li> the number of towns, that is, the total number of coordinates where endpoints and/or
intersections of the roads exist does not exceed 18;</li>
<li> no two horizontal roads are connected nor ovarlapped; no two vertical roads, either;</li>
<li> all the towns are connected by roads; and</li>
<li> no pair of ACM and ICPC units initially stay in the towns on the same road.</li>
</ul>
<p>
The input is terminated by a line with triple zeros, which should not be processed.
</p>
<H2>Output</H2>
<p>
For each dataset, print the minimum required number of orders in a line. It is guaranteed that
there is at least one way to complete the mission.
</p>
<H2>Sample Input</H2>
<pre>
5 1 1
0 0 2 0
1 1 2 1
1 2 3 2
1 0 1 2
2 0 2 2
0 0
3 2
2 1
1 2
2 1 1
1 0 1 2
0 1 2 1
1 0
0 1
1 0
2 1
2 1 1
1 0 1 2
0 1 2 1
1 0
0 1
1 2
2 1
4 1 1
0 0 2 0
1 1 3 1
1 0 1 1
2 0 2 1
0 0
3 1
1 0
2 1
5 1 1
0 0 2 0
1 1 2 1
1 2 3 2
1 0 1 2
2 0 2 2
0 0
3 2
3 2
0 0
8 3 3
1 1 3 1
0 2 4 2
1 3 5 3
2 4 4 4
1 1 1 3
2 0 2 4
3 1 3 5
4 2 4 4
0 2
1 1
2 0
3 5
4 4
5 3
3 5
4 4
5 3
0 2
1 1
2 0
0 0 0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
3
1
2
2
7
18
</pre>
|
p03508 | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given an undirected graph <var>G</var>. <var>G</var> has <var>N</var> vertices and <var>M</var> edges. The vertices are numbered from <var>1</var> through <var>N</var>, and the <var>i</var>-th edge <var>(1 ≤ i ≤ M)</var> connects Vertex <var>a_i</var> and <var>b_i</var>. <var>G</var> does not have self-loops and multiple edges.</p>
<p>You can repeatedly perform the operation of adding an edge between two vertices. However, <var>G</var> must not have self-loops or multiple edges as the result. Also, if Vertex <var>1</var> and <var>2</var> are connected directly or indirectly by edges, your body will be exposed to a voltage of <var>1000000007</var> volts. This must also be avoided.</p>
<p>Under these conditions, at most how many edges can you add? Note that Vertex <var>1</var> and <var>2</var> are never connected directly or indirectly in the beginning.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 ≤ N ≤ 10^5</var></li>
<li><var>0 ≤ M ≤ 10^5</var></li>
<li><var>1 ≤ a_i < b_i ≤ N</var></li>
<li>All pairs <var>(a_i, b_i)</var> are distinct.</li>
<li>Vertex <var>1</var> and <var>2</var> in <var>G</var> are not connected directly or indirectly by edges.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>M</var>
<var>a_1</var> <var>b_1</var>
<var>:</var>
<var>a_M</var> <var>b_M</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum number of edges that can be added.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4 1
1 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p><img alt="" src="https://img.atcoder.jp/relay2/ff912c5f290ee6a475d4c8a2ac29bfff.png"/></p>
<p>As shown above, two edges can be added. It is not possible to add three or more edges.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p><img alt="" src="https://img.atcoder.jp/relay2/b8da065e6d2dfe3bc9ea98095cf9f3de.png"/></p>
<p>No edge can be added.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>9 6
1 4
1 8
2 5
3 6
4 8
5 7
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>12
</pre></section>
</div>
</span> |
p01535 |
<h1> Markup language has Declined</h1>
<h2> E: マークアップ言語は衰退しました</h2>
<p>
わたしたち人類がゆるやかな衰退を迎えて,はや数世紀.
すでに地球は”ぷろぐらまー”のものだったりします.
平均身長170センチで7頭身,高い知能を持ち,こーでぃんぐが大好きなぷろぐらまーさんたち.
わたしは,そんなぷろぐらまーさんと人との間を取り持つ重要な職,国際公務員の” えすいー ”となり,故郷のニブンキの里に帰ってきました.
祖父の年齢でも現役でできる仕事なのだから,さぞや楽なのだろうとこの職を選んだのですが….
</p>
<p>
ある日,ぷろぐらまーさんたちが渡してくれたのは,2枚の設計書のようなもの.
そのぷろぐらまーさん曰く,テキストとスクリプトを利用して,様々な情報を容易に交換できるのだと言います.
</p>
<p>
1枚目の設計書は,文章構造を表すファイルの説明でした.
このファイルのファイル名が.dmlで終わり,DMLファイルと呼ばれます.
</p>
<p>
DMLファイルの中は,タグと呼ばれる文字列で挟むことで文章の構造を表しています.
タグは,開始タグと終了タグが存在し,
</p>
<pre>
<開始タグ名>内容</終了タグ名>
</pre>
<p>
の要素で表されます.
このとき,開始タグ名と終了タグ名は同じ文字列で表されます.
</p>
<p>
タグは入れ子構造にすることができ,<tagA><tagB></tagB></tagA>のようにしても構いません.
また,<tagA><tagB></tagA></tagB>のような構造は許されません.
</p>
<p>
DMLファイル内のタグ名はいくつか特殊なものを除いて任意の文字列です.
特殊なタグは以下の5つです.
</p>
<ul>
<li> タグ名:dml</li>
<ul>
<li> 全てのタグの根を表します.</li>
</ul>
<ul>
<li> この開始タグは必ずファイルの先頭1度だけに現れ,このタグの終了タグはファイルの末尾に1度だけ出現します.</li>
</ul>
<li> タグ名:script</li>
<ul>
<li> 必ずdmlタグで囲まれた中で先頭,もしくはこの終了タグに連続して現れます.</li>
</ul>
<ul>
<li> このタグは内部に入れ子構造を持つことが出来ません.</li>
</ul>
<ul>
<li> このタグに囲まれたスクリプトファイルを関連付けます.</li>
</ul>
<ul>
<li> このタグに囲まれた文字列は出力されません.</li>
</ul>
<li> タグ名:br</li>
<ul>
<li> 表示に際し,改行を行います.</li>
</ul>
<ul>
<li> 終了タグを持ちません.</li>
</ul>
<li> タグ名:link</li>
<ul>
<li> このタグは内部に入れ子構造を持つことが出来ません.</li>
</ul>
<ul>
<li> ユーザがこのタグに囲まれた文字列をクリックすると,現在の画面全てを消去し,その文字列で表されるファイル名のDMLファイルを表示します.</li>
</ul>
<li> タグ名:button</li>
<ul>
<li> このタグは内部に入れ子構造を持つことが出来ません.</li>
</ul>
<ul>
<li> ユーザがこのタグに囲まれた文字列をクリックすると,scriptタグによって関連付けられているスクリプトの中から,その文字列で表される名前のサブルーチンを実行します.</li>
</ul>
</ul>
<p>
タグ名は,アルファベットの大文字,小文字のみで表されます.
タグ名にスペースが現れることはありません.
DMLファイル中に現れる文字列は,アルファベットの大文字,小文字,スペース,'<','>','/'となります.
また,同名のタグが存在することもありえます.
</p>
<p>
scriptタグ以外で囲まれたタグ以外の文字列は,画面の左上(0, 0)から左詰めで出力され,
画面端かbrタグが出現するまで改行されません.
</p>
<p>
2枚目の設計書は,DMLファイル中のボタンを押した時の動作を表すDSファイルの説明でした.
DSファイルは,サブルーチンが並べられています.
</p>
<pre>
サブルーチン名 {
式;
式;
...;
}
</pre>
<p>
セミコロンは式の終わりを表します.
</p>
<p>
例えば,DMLファイル中に,<title>?</title>で囲まれた文章があるとします.
そのとき可能な式は以下の4つの代入式です.
</p>
<pre>
title.visible = true;
title.visible = false;
title.visible != true;
title.visible != false;
</pre>
<p>
visibleへの真偽値の代入は,そのタグの内容を表示するかしないかを変更します.表示され無くなった場合には,それ以降の文章は左に詰められます.
最初やlinkタグのクリックによって現在表示しているDMLファイルが書き変わった時,初期値は全てtrueになっています.
</p>
<p>
'!='は否定の代入を表します.
上の例では,1行目と4行目,2行目と3行目はそれぞれ等価です.
</p>
<p>
また式は,以下のように複数の値を同時に変更することもできます.
</p>
<pre>
titleA.visible = titleB.visible = true;
</pre>
<p>
この時,右から順に処理が行われます.
すなわち,
</p>
<pre>
titleB.visible = true;
titleA.visible = titleB.visible;
</pre>
<p>
の2行と等価です.
ただし,この表し方は便宜上であって,スクリプト内で文の最も右において,タグによる指定が行われることはありません.
また,
</p>
<pre>
titleA.visible != titleB.visible = true;
</pre>
<p>
は
</p>
<pre>
titleB.visible = true;
titleA.visible != titleB.visible;
</pre>
<p>
と等価です.
</p>
<p>
タグの指定方法は,'.'で絞り込むことによって行われます.
例えば,
</p>
<pre>
dml.body.title
</pre>
<p>
は,dmlタグに囲まれた,bodyタグに囲まれた,titleタグを指します.
ただし,scriptタグとbrタグが絞り込みにつかわれたり,指定されることはありません.
</p>
<p>
このとき,絞り込まれる要素が直接囲まれているとは限らないことに注意して下さい.
例えば, <a><b><c></c></b></a> とあるとき,a.b.cでもa.cでもcを指すことができます.
</p>
<p>
また,同名のタグが存在する場合には,指定されたタグが1つとは限りません.
指定したタグが存在しない場合には,表示に影響は与えられませんが,複数の値を同時に変更する際に現れた場合には,存在している場合と同様に評価されます.
</p>
<p>
BNFは以下のようになります.
</p>
<pre>
<script_file> :: = <subroutine> | <script_file> <subroutine>
<subroutine> ::= <identifier> '{' <expressions> '}'
<expressions> ::= <expression> ';' | <expressions> <expression> ';'
<expression> ::= <visible_var> '=' <visible_exp_right>
| <visible_var> '!=' <visible_exp_right>
| <visible_var> '=' <expression>
| <visible_var> '!=' <expression>
<visible_exp_right> ::= 'true' | 'false'
<visible_var> ::= <selector> '.visible'
<selector> ::= <identifier> | <selector> '.' <identifier>
<identifier> ::= <alphabet> | <identifier> <alphabet>
<alphabet> ::= 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g'
| 'h' | 'i' | 'j' | 'k' | 'l' | 'm' | 'n'
| 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u'
| 'v' | 'w' | 'x' | 'y' | 'z'
| 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G'
| 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N'
| 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U'
| 'V' | 'W' | 'X' | 'Y' | 'Z'
</pre>
<p>
頭が痛くなりそうですね.ならないですか?そうですか.
</p>
<p>
ユーザは,最初のDMLファイルが表示された後,画面上の座標(<i>x</i>, <i>y</i>)をクリックします.
すると,その場所のlinkやbuttonに応じて,画面が変化します.
それ以外の場所をクリックしたらですか?なにも起こりませんよ.流石に.
</p>
<p>
さて,どんなものができるのでしょう?
大好物のえなじーどりんくでも渡しつつ,見守ることにします.
</p>
<h2> Input</h2>
<p>
入力は,次の形式で与えられる.
</p>
<pre>
<i>N</i>
<i>filename<sub>1</sub></i>
<i>file<sub>1</sub></i>
<i>filename<sub>2</sub></i>
<i>file<sub>2</sub></i>
...
<i>filename<sub>N</sub></i>
<i>file<sub>N</sub></i>
<i>M</i>
<i>w<sub>1</sub></i> <i>h<sub>1</sub></i> <i>s<sub>1</sub></i> <i>startfile<sub>1</sub></i>
<i>x<sub>11</sub></i> <i>y<sub>11</sub></i>
...
<i>x<sub>1s1</sub></i> <i>y<sub>1s1</sub></i>
...
<i>w<sub>M</sub></i> <i>h<sub>M</sub></i> <i>s<sub>M</sub></i> <i>startfile<sub>M</sub></i>
<i>x<sub>M1</sub></i> <i>y<sub>11</sub></i>
...
<i>x<sub>MsM</sub></i> <i>y<sub>MsM</sub></i>
...
</pre>
<p>
<i>N</i>(1 <= <i>N</i> <= 20)は,ファイルの数を表し,その後,ファイル名とファイルの内容が交互に与えられる.
filenameは,任意のアルファベット16文字に'.dml'または'.ds'が加えられた形で表される.
'.dml'で終わる場合はDMLファイルであり,'.ds'で終わる場合はDSファイルである.
ファイルの文字列は一行で与えられ,500文字以下である.
</p>
<p>
<i>M</i>(1 <= <i>M</i> <= 50)は訪れたユーザの数を表す.
ユーザ1人につき,画面の幅w,高さh(1 <= <i>w * h</i> <= 500),操作数<i>s</i>(0 <= <i>s</i> <= 50),開始DMLファイル名と,<i>s</i>行のクリックした座標<i>x</i>, <i>y</i>(0 <= <i>x</i> < <i>w</i>, 0 <= <i>y</i> < <i>h</i>)が与えられる.
</p>
<p>
DSファイルに空白は含まれない.
また,与えられるDMLファイル中のタグは必ず閉じられている.
入力の全体を通して,スクリプト内に同名のサブルーチン名が現れることはない.
</p>
<h2> Output</h2>
<p>
ユーザ1人につき,最終的な画面を幅<i>w</i>, 高さ<i>h</i>で出力して下さい.
画面の右下を超えた分については出力しません.
ある行において,出力する文字が無くなった場合には,その行が<i>w</i>文字になるまで'.'を出力して下さい.
</p>
<h2> Sample Input 1</h2>
<pre>
1
index.dml
<dml><title>Markup language has Declined</title><br>Programmers world</dml>
1
15 3 0 index
</pre>
<h2> Sample Output 1</h2>
<pre>
Markup language
has Declined..
Programmers wor
</pre>
<h2> Sample Input 2</h2>
<pre>
2
hello.dml
<dml><link>cut</link></dml>
cut.dml
<dml>hello very short</dml>
1
10 2 1 hello
1 0
</pre>
<h2> Sample Output 2</h2>
<pre>
hello very
short....
</pre>
<h2> Sample Input 3</h2>
<pre>
2
index.dml
<dml><script>s</script>slip<fade>akkariin</fade><br><button>on</button> <button>off</button></dml>
s.ds
on{fade.visible=true;}off{fade.visible!=true;}
2
15 3 0 index
15 3 3 index
3 1
1 1
3 1
</pre>
<h2> Sample Output 3</h2>
<pre>
slipakkariin...
on off.........
...............
slip...........
on off.........
...............
</pre> |
p01020 | <h1>Problem E: Lonely Adventurer</h1>
<p>
13歳の誕生日の祝いに少年Gは父親と一緒に船旅に出ていた。しかし航海の途中、不運にも船が嵐に見舞われて船が転覆してしまう。彼が目を覚ますと、そこは無人島だった。冒険家の父親の影響もあり、彼はこの状況にひるむ事なく、無人島でサバイバル生活をすることを決意した。動物を倒して肉を焼いたり、木の実を採取することで食いつなぎ、大木の下で野宿をすることでなんとか生き延びることができた。しかし、どうやら最近島の様子がおかしいようだ。動物たちは皆奇妙な行動を取るし、これは何か悪い事の予兆なのかもしれない。早くこの島から脱出せねば。
</p>
<p>
とはいえ、島からどう離れればよいのか。海の向こうには別の島が見えるが、そこまで行く方法が見当たらない。途方に暮れ、海岸沿いを歩いていたとき、ふと彼は <var>N</var> 隻のボートを発見した。調べてみたところ、これらはどれもうまく動くようだ。よし、このボートを使って向こうの島まで脱出しよう。ただ次の島でもまた同じようなサバイバル生活が始まるかもしれない。そのとき困らないためにも、予備として全てのボートを運んでおこう。この島で何かが起こる前に早い事済ませないと。
</p>
<h2>Problem</h2>
<p><var>N</var> 隻の各ボート <var>i</var> (1 ≤ <var>i</var> ≤ <var>N</var>) にはそれぞれ島Aから島Bまで(島Bから島Aまで)の移動にかかる時間 <var>T<sub>i</sub></var> (分)が与えられている。少年Gはできるだけ早く、全てのボートを初めにいる島Aから島Bに運びたいと考えている。彼がボートを運び終えるのに要する最短の時間(分)を出力せよ。ただし、ボートは一度に 2 隻まで連結して運ぶことができ、そのときにかかる時間は 2 隻のボートの内、より時間がかかるボートの時間に等しい。例えば 2 分と 4 分のボートを連結させて運ぶ場合、島Aから島Bまで(島Bから島Aまで)の移動に 4 分かかる。また 3 分のボート 2 隻を連結させて運ぶ場合は 3 分かかることになる。なお、ボートの乗り換えにかかる時間は無視してよいものとする。</p>
<h2>Input</h2>
<p>1行目に整数 <var>N</var> が与えられる。<br>2行目には <var>N</var> 個の整数 <var>T<sub>i</sub></var> が空白区切りで与えられる。</p>
<h2>Constraints</h2>
<p>入力は以下の条件を満たす。</p>
<ul>
<li>1 ≤ <var>N</var> ≤ 1000</li>
<li>1 ≤ <var>T<sub>i</sub></var> ≤ 10000</li>
</ul>
<h2>Output</h2>
<p>全てのボートを運び終えるのに要する最短の時間(分)を1行で出力せよ。</p>
<h2>Sample Input 1</h2>
<pre>
4
1 2 3 4
</pre>
<h2>Sample Output 1</h2>
<pre>
11
</pre>
<h2>Sample Input 2</h2>
<pre>
5
3 3 3 3 3
</pre>
<h2>Sample Output 2</h2>
<pre>
21
</pre>
|
p01470 |
<script type="text/x-mathjax-config">
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], skipTags: ["script","noscript","style","textarea","code"], processEscapes: true }});
</script>
<script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>
<h2>問題文</h2>
<p>有理数列 $X_0, X_1, X_2, ..., X_N$ がある。各項は以下のように定義される。<br /></p>
<ol><li>$X_0 = 0$</li>
<li>$X_i = X_{i-1}$ $op_i$ $Y_i$ ($1 \leq i \leq N$). ただし $op_i$ は $+$、$−$、$×$、$÷$のいずれかである。</li></ol>
<p>$X_N$ を求めよ。</p>
<h2>入力</h2>
<p>入力は以下の形式に従う。与えられる数は全て整数である。</p>
<pre>$N$
$o_1$ $Y_1$
$o_2$ $Y_2$
$...$
$o_N$ $Y_N$</pre>
<p>$o_i = 1$ のとき $op_i$ は+、$o_i = 2$ のとき $op_i$ は−、$o_i = 3$ のとき $op_i$ は×、$o_i = 4$ のとき $op_i$ は÷である。</p>
<h2>制約</h2>
<ul><li>$1 \leq N \leq 10^5$</li>
<li>$1 \leq o_i \leq 4$</li>
<li>$-10^6 \leq Y_i \leq 10^6$</li>
<li>$o_i=4$ ならば $Y_i \neq 0$</li>
<li>$X_N$ は $-2^{31}$ 以上 $2^{31}$ 未満の整数となる。</li></ul>
<h2>出力</h2>
<p>$X_N$ の値を1行に出力せよ。</p>
<h2>Sample Input 1</h2>
<pre>4
1 1
4 2
2 4
3 4</pre>
<h2>Output for the Sample Input 1</h2>
<pre>-14</pre>
<p>$X_0 = 0$<br />
$X_1 = X_0 + 1 = 1$<br />
$X_2 = X_1 ÷ 2 = 1/2$<br />
$X_3 = X_2 − 4 = -7/2$<br />
$X_4 = X_3 × 4 = -14$</p>
|
p01923 |
<script type="text/x-mathjax-config">
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});
</script>
<script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>
<h3>JAG 模擬予選練習会</h3>
<p>ACM-ICPC OB/OGの会 (Japanese Alumni Group; JAG) には模擬コンテストで出題する問題のストックが <i>N</i> 問あり,それぞれの問題は 1 から <i>N</i> の整数で番号付けられている.
それぞれの問題について難易度評価と推薦投票が行われており,問題 <i>i</i> の難易度は <i>d<sub>i</sub></i> で,推薦度は <i>v<sub>i</sub></i> である.
また,難易度の最大値は <i>M</i> である.
</p>
<p>次のコンテストでは,難易度 1 から <i>M</i> の問題をそれぞれ 1 問ずつ,計 <i>M</i> 問出題する予定である.
コンテストのクオリティを上げるために推薦度の合計が最大になるように問題を選定したい.
ただし,JAG の作問力はすさまじいので,各難易度の問題が最低でも 1 問以上ずつあると仮定してよい.
</p>
<p>あなたの仕事は,条件を満たすように問題を選定したときの推薦度の和の最大値を求めるプログラムを作成することである.
</p>
<h3>Input</h3>
<p>入力は複数のデータセットからなる.
各データセットは次の形式で表される.
</p><blockquote><i>N</i> <i>M</i><br><i>d<sub>1</sub></i> <i>v<sub>1</sub></i><br>...<br><i>d<sub>N</sub></i> <i>v<sub>N</sub></i><br></blockquote>
<p>データセットの 1 行目には,問題ストックの数を表す整数 <i>N</i> と,難易度の最大値 <i>M</i> が空白区切りで与えられる.
これらの数は <i>1 ≤ M ≤ N ≤ 100</i> を満たす.
続く <i>N</i> 行の内 <i>i</i> 行目には,問題 <i>i</i> の難易度と推薦度を表す整数 <i>d<sub>i</sub></i> と <i>v<sub>i</sub></i> が空白区切りで与えられる.
これらの数は <i>1 ≤ d<sub>i</sub> ≤ M</i> および <i>0 ≤ v<sub>i</sub> ≤ 100</i> を満たす.
また,各 <i>1 ≤ j ≤ M</i> について,<i>d<sub>i</sub> = j</i> となる <i>i</i> が 1 つ以上存在することが保証される.
</p><blockquote></blockquote>
<p>入力の終わりは空白で区切られた 2 つのゼロで表される.
また,データセットの数は 50 を超えない.
</p><blockquote></blockquote>
<h3>Output</h3>
<p>各データセットについて,各難易度から 1 問ずつ出題するときの,出題する問題の推薦度の和の最大値を 1 行に出力せよ.
</p><blockquote></blockquote>
<h3>Sample Input</h3>
<pre>5 3
1 1
2 3
3 2
3 5
2 1
4 2
1 7
2 1
1 3
1 5
6 1
1 3
1 2
1 8
1 2
1 7
1 6
20 9
4 10
2 4
5 3
6 6
6 3
7 4
8 10
4 6
7 5
1 8
5 7
1 5
6 6
9 9
5 8
6 7
1 4
6 4
7 10
3 5
19 6
4 1
6 5
5 10
1 10
3 10
4 6
2 3
5 4
2 10
1 8
3 4
3 1
5 4
1 10
1 3
5 6
5 2
1 10
2 3
0 0
</pre>
<p>
サンプルのデータセットは5つあり,順に<br>
1行目から6行目までが1つ目 (<i>N</i> = 5, <i>M</i> = 3) のテストケース,<br>
7行目から11行目までが2つ目 (<i>N</i> = 4, <i>M</i> = 2) のテストケース,<br>
12行目から18行目までが3つ目 (<i>N</i> = 6, <i>M</i> = 1) のテストケース,<br>
19行目から39行目までが4つ目 (<i>N</i> = 20, <i>M</i> = 9) のテストケース,<br>
40行目から59行目までが5つ目 (<i>N</i> = 19, <i>M</i> = 6) のテストケースをそれぞれ表す.
</p>
<h3>Output for Sample Input</h3>
<pre>9
8
8
71
51</pre>
|
p00631 |
<H1><font color="#000000">Problem 05:</font> Split Up!</H1>
<p>
勇者ポン太とその親友同じく勇者ゴン太は、これから壮大な冒険へと出発するために、それぞれ仲間を求めてルイーダの酒場へやって来ました。酒場には、冒険に出たくてうずうずしている武闘家や僧侶、魔法使いがたくさん居ます。
</p>
<p>
心やさしいゴン太はポン太を気遣い、「先に仲間を選んでいいよ。」と言いました。
</p>
<p>
一方、ポン太もゴン太が心配でなりません。「いや、君が先に選んでいいよ。」と言い返しました。
</p>
<p>
お互い譲り合いが続き、ついにルイーダがこう提案しました。「じゃあ、ここにいる <i>n</i> 人の登録者をそれぞれのパーティの合計戦闘力がなるべく均等になるように、この"パーティ分けマシン"で分けてあげるわ。」
</p>
<!--
-->
<p>
あなたに与えられた仕事は、パーティ分けマシンに組み込まれているプログラムを作成することです。
</p>
<p>
このプログラムは <i>n</i> 個の整数を入力し、それらを2つのグループ <i>A</i>, <i>B</i> に分けたときの、<i>A</i> に含まれる整数の合計値と <i>B</i> に含まれる整数の合計値の差の最小値を出力しなければなりません。
</p>
<p>
このマシンのおかげで、ポン太とゴン太は仲良く仲間を見つけ、壮大な冒険へと出かけて行きました・・。
</p>
<H2>Input</H2>
<p>
入力として複数のデータセットが与えられます。各データセットは以下の形式で与えられます:<br>
<br>
<i>n</i> (登録者の数: 整数)<br>
<i>a</i><sub>1</sub> <i>a</i><sub>2</sub> ... <i>a</i><sub><i>n</i></sub> (各登録者の戦闘力: 空白区切りの整数)
</p>
<p>
<i>n</i> は 20 以下であり、各登録者の戦闘力は 100 万を越えません。
</p>
<p>
<i>n</i> が 0 のとき入力の終わりとします。
</p>
<H2>Output</H2>
<p>
各データセットに対して、最小値を1行に出力して下さい。
</p>
<H2>Sample Input</H2>
<pre>
5
1 2 3 4 5
4
2 3 5 7
0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
1
1
</pre>
|
p00261 |
<H1>アカ・ベコと40人の盗賊</H1>
<p>
40 人の盗賊から逃れようとしているアカ・ベコは、A 市の街中で道に迷ってしまった。アカ・ベコは新しいアジトがあるB 市に行きたいのだが地図を盗賊に盗まれてしまった。
</p>
<p>
盗賊の一人であるコボー氏はアカ・ベコに同情し、気の毒に思っていた。そこで、密かにアカ・ベコに、「あなたがB 市に行くお手伝いをしたいが、仲間にばれない様にしなければならないので、直接道順を教えたいが教えることができない。しかし、あなたの質問には答えられる。」と伝言を送った。
</p>
<p>
コボー氏は、「○○という道順はどうか。」という質問をアカ・ベコから受け取ると、それがA 市からB市までちょうどたどり付ける道順ならYes、そうでなければNo という答えを伝える。道順のチェックは以下の地図に従って行う。
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_akabeko" width="380">
</center>
<br/>
<p>
各都市は一方通行の道でつながっており、それぞれの道には、0 か1 の数字が付いている。アカ・ベコは数字の並びで道順を指定する。例えば、0100 は A 市から X、Z、W 市を経由して B 市にちょうどたどり着く道順である。地図にない道順を選ぶと砂漠に迷い込んでしまい、決して B 市にたどり着くことはない。アカ・ベコは自分がいる都市の名前を知ることはできないため、道順をたどり終えたときにちょうど B 市に到達する必要がある。
</p>
<p>
コボー氏はアカ・ベコからの質問に答えるために、あなたを密かに雇いプログラムの作成を依頼した。アカ・ベコからの質問を入力すると、それがA 市からB 市にちょうどたどり付ける道順なら Yes、そうでなければ No と出力するプログラムを作成してほしい。
</p>
<h2>入力</h2>
<p>
入力は複数のデータセットからなる。入力の終わりは #(シャープ)1つの行で示される。各データセットは以下の形式で与えられる。
</p>
<pre>
p
</pre>
<p>
1行に道順を示す数字 0, 1 の並び p が与えられる。p は100文字を超えない文字列である。
</p>
<p>
データセットの数は 100 を超えない。
</p>
<h2>出力</h2>
<p>
データセットごとに、Yes または No を1行に出力する。
</p>
<h2>入力例</h2>
<pre>
0100
0101
10100
01000
0101011
0011
011111
#
</pre>
<h2>出力例</h2>
<pre>
Yes
No
Yes
No
Yes
No
Yes
</pre>
<p>
4つ目のデータセットでは、B 市を通りすぎてY 市に到達する道順なので No と出力する。
</p>
<p>
5つ目のデータセットでは、同じ都市を何度か通っているが、B 市にちょうどたどり着く道順なので Yes と出力する。
</p>
<p>
6つ目のデータセットでは、X 市から砂漠に迷い込んでしまいB 市にはたどり着けないので No と出力する。
</p>
<p>
最後のデータセットでは、B 市をいったん通り過ぎてから、X 市とZ 市を経由してB 市にちょうどたどり着く道順なので Yes と出力する。
</p> |
p01889 |
<script type="text/x-mathjax-config">
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});
</script>
<script type='text/javascript' src='http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>
</script>
<h2>Compressed Formula</h2>
<p>
You are given a simple, but long formula in a compressed format. A compressed formula is a sequence of $N$ pairs of an integer $r_i$ and a string $s_i$, which consists only of digits ('0'-'9'), '+', '-', and '*'. To restore the original formula from a compressed formula, first we generate strings obtained by repeating $s_i$ $r_i$ times for all $i$, then we concatenate them in order of the sequence.
</p>
<p>
You can assume that a restored original formula is well-formed. More precisely, a restored formula satisfies the following BNF:
</p>
<pre>
<expression> := <term> | <expression> '+' <term> | <expression> '-' <term>
<term> := <number> | <term> * <number>
<number> := <digit> | <non-zero-digit> <number>
<digit> := '0' | <non-zero-digit>
<non-zero-digit> := '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
</pre>
<p>
Here, '+' means addition, '-' means subtraction, and '*' means multiplication of integers.
</p>
<p>
Your task is to write a program computing the answer of a given formula modulo 1,000,000,007, where $x$ modulo $m$ is a non-negative integer $r$ such that there exists an integer $k$ satisfying $x = km + r$ and $0 \leq r < m$; it is guaranteed that such $r$ is uniquely determined for integers $x$ and $m$.
</p>
<h3>Input</h3>
<p>
The input consists of a single test case.<br/>
<br/>
$N$<br/>
$r_1$ $s_1$<br/>
$r_2$ $s_2$<br/>
...<br/>
$r_N$ $s_N$
</p>
<p>
The first line contains a single integer $N$ ($1 \leq N \leq 10^4$), which is the length of a sequence of a compressed formula. The following $N$ lines represents pieces of a compressed formula. The $i$-th line consists of an integer $r_i$ ($1 \leq r_i \leq 10^9$) and a string $s_i$ ($1 \leq |s_i| \leq 10$), where $t_i$ is the number of repetition of $s_i$, and $s_i$ is a piece of an original formula. You can assume that an original formula, restored from a given compressed formula by concatenation of repetition of pieces, satisfies the BNF in the problem statement.
</p>
<h3>Output</h3>
<p>
Print the answer of a given compressed formula modulo 1,000,000,007.
</p>
<h3>Sample Input 1</h3>
<pre>
1
5 1
</pre>
<h3>Output for the Sample Input 1</h3>
<pre>
11111
</pre>
<h3>Sample Input 2</h3>
<pre>
2
19 2*
1 2
</pre>
<h3>Output for the Sample Input 2</h3>
<pre>
1048576
</pre>
<h3>Sample Input 3</h3>
<pre>
2
1 1-10
10 01*2+1
</pre>
<h3>Output for the Sample Input 3</h3>
<pre>
999999825
</pre>
<h3>Sample Input 4</h3>
<pre>
4
3 12+45-12
4 12-3*2*1
5 12345678
3 11*23*45
</pre>
<h3>Output for the Sample Input 4</h3>
<pre>
20008570
</pre>
|
p02533 | <h2>Problem 9999</h2>
<p>
これはテスト問題です。
This is test problem. Compute A + B.
</p>
|
p02163 | <h1>Problem B: How old are you</h1>
<h2>Problem</h2>
<p>
胡散臭いマジシャンがマジックを披露するようです。<br>
<br>
「さて、1つの好きな整数を思い浮かべてください。」<br>
あなたは、頭の中に自分の数え年を思い浮かべることにしました。<br>
<br>
マジシャンはクエリを $N$ 回投げてきました。<br>
各クエリは、以下のうちのどれかです。<br>
</p>
<ol type="1">
<li>「今思い浮かべている数に、 $x$ を掛けてください。」</li>
<li>「今思い浮かべている数に、 $x$ を足してください。」</li>
<li>「今思い浮かべている数から、 $x$ を引いてください。」</li>
</ol>
<p>
各クエリごとに、思い浮かべている数に対してクエリを処理し、結果として出た値を頭の中に思い浮かべます。<br>
<br>
「さて、今から最初にあなたの思い浮かべた数を当ててみせましょう。」<br>
そういったマジシャンは、何かを言おうとしています。<br>
しかし、何を言えばいいのか忘れてしまったようです。<br>
胡散臭いマジシャンは汗ダラダラであなたを見つめています。<br>
仕方がないので、何を言えばいいのか教えてあげましょう。<br>
<br>
<br>
マジシャンは最後に以下のように言うつもりだったようです。<br>
「今あなたが思い浮かべている整数に、 $A$ を足して、 $B$ で割れば、それはあなたが最初に思い浮かべた整数なのです」<br>
<br>
制約から、整数の組 $(A,B)$ であって、思い浮かべた整数がいくつであっても上の条件を満たすものが、ただ1つに定まります。<br>
整数の組 $(A,B)$ を求めてください。<br>
ただし、 $B$ は $0$ であってはいけません。<br>
</p>
<h2>Input</h2>
<p>入力は以下の形式で与えられる。</p>
<pre>
$N$
$q_1$ $x_1$
$\vdots$
$q_n$ $x_n$
</pre>
<p>
$1$ 行目に与えられるクエリの数 $N$ が与えられる。<br>
$2$ 行目から続く $N$ 行にクエリの情報が与えられる。<br>
$q$ はクエリの種類を表し、問題文中のリストの数字に対応する。<br>
</p>
<h2>Constraints</h2>
<p>入力は以下の条件を満たす。</p>
<ul>
<li>$1 \leq N \leq 15 $</li>
<li>$1 \leq q \leq 3 $</li>
<li>$1 \leq x \leq 10 $</li>
<li>入力は全て整数である</li>
</ul>
<h2>Output</h2>
<p>
$1$ 行に条件を満たす整数 $A,B$ を空白区切りで出力せよ。<br>
</p>
<h2>Sample Input 1</h2>
<pre>
3
1 2
2 10
3 8
</pre>
<h2>Sample Output 1</h2>
<pre>
-2 2
</pre>
<p>
</p>
<h2>Sample Input 2</h2>
<pre>
10
1 10
1 10
1 10
1 10
1 10
1 10
1 10
1 10
1 10
1 10
</pre>
<h2>Sample Output 2</h2>
<pre>
0 10000000000
</pre>
<p>32bit整数型に答えが収まるとは限らないことに注意してください。</p> |
p03322 | <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> |
p03288 | <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> |
p02860 | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given are a positive integer <var>N</var> and a string <var>S</var> of length <var>N</var> consisting of lowercase English letters.</p>
<p>Determine whether the string is a concatenation of two copies of some string.
That is, determine whether there is a string <var>T</var> such that <var>S = T + T</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 100</var></li>
<li><var>S</var> consists of lowercase English letters.</li>
<li><var>|S| = N</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>S</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If <var>S</var> is a concatenation of two copies of some string, print <code>Yes</code>; otherwise, print <code>No</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6
abcabc
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<p>Let <var>T = </var> <code>abc</code>, and <var>S = T + T</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6
abcadc
</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>1
z
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>No
</pre></section>
</div>
</span> |
p03772 | <span class="lang-en">
<p>Score : <var>2000</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Snuke received an integer sequence <var>a</var> of length <var>2N-1</var>.</p>
<p>He arbitrarily permuted the elements in <var>a</var>, then used it to construct a new integer sequence <var>b</var> of length <var>N</var>, as follows:</p>
<ul>
<li><var>b_1 =</var> the median of <var>(a_1)</var></li>
<li><var>b_2 =</var> the median of <var>(a_1, a_2, a_3)</var></li>
<li><var>b_3 =</var> the median of <var>(a_1, a_2, a_3, a_4, a_5)</var></li>
<li><var>:</var></li>
<li><var>b_N =</var> the median of <var>(a_1, a_2, a_3, ..., a_{2N-1})</var></li>
</ul>
<p>How many different sequences can be obtained as <var>b</var>? Find the count modulo <var>10^{9} + 7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≤ N ≤ 50</var></li>
<li><var>1 ≤ a_i ≤ 2N-1</var></li>
<li><var>a_i</var> are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>a_1</var> <var>a_2</var> <var>...</var> <var>a_{2N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the answer.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2
1 3 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p>There are three sequences that can be obtained as <var>b</var>: <var>(1,2)</var>, <var>(2,2)</var> and <var>(3,2)</var>. Each of these can be constructed from <var>(1,2,3)</var>, <var>(2,1,3)</var> and <var>(3,1,2)</var>, respectively.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4
1 3 2 3 2 4 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>16
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>15
1 5 9 11 1 19 17 18 20 1 14 3 3 8 19 15 16 29 10 2 4 13 6 12 7 15 16 1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>911828634
</pre>
<p>Print the count modulo <var>10^{9} + 7</var>.</p></section>
</div>
</span> |
p03267 | <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> |
p00918 |
<H1><font color="#000">Problem E: </font>Dragon's Cruller</H1>
<p>
Dragon's Cruller is a sliding puzzle on a torus. The torus surface is partitioned into nine squares as shown in its development in Figure E.1. Here, two squares with one of the sides on the development labeled with the same letter are adjacent to each other, actually sharing the side. Figure E.2 shows which squares are adjacent to which and in which way. Pieces numbered from 1 through 8 are placed on eight out of the nine squares, leaving the remaining one empty.
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_dragonsCruller1" style="aling:center"><br/>
<p>
Figure E.1. A 3 × 3 Dragon’s Cruller torus
</p>
</center>
<br/>
<p>
A piece can be slid to an empty square from one of its adjacent squares. The goal of the puzzle is, by sliding pieces a number of times, to reposition the pieces from the given starting arrangement into the given goal arrangement. Figure E.3 illustrates arrangements directly reached from the arrangement shown in the center after four possible slides. The cost to slide a piece depends on the direction but is independent of the position nor the piece.
</p>
<p>
Your mission is to find the minimum cost required to reposition the pieces from the given starting
arrangement into the given goal arrangement.
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_dragonsCruller2" style="aling:center"><br/>
<p>
Figure E.2. Adjacency
</p>
</center>
<br/>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_dragonsCruller3" style="aling:center"><br/>
<p>
Figure E.3. Examples of sliding steps
</p>
</center>
<br/>
<p>
Unlike some sliding puzzles on a flat square, it is known that any goal arrangement can be reached from any starting arrangements on this torus.
</p>
<H2>Input</H2>
<p>
The input is a sequence of at most 30 datasets.
</p>
<p>
A dataset consists of seven lines. The first line contains two positive integers <var>c<sub>h</sub></var> and <var>c<sub>v</sub></var>, which represent the respective costs to move a piece horizontally and vertically. You can assume that
both <var>c<sub>h</sub></var> and <var>c<sub>v</sub></var> are less than 100. The next three lines specify the starting arrangement and the last three the goal arrangement, each in the following format.
</p>
<pre>
<var>d<sub>A</sub></var> <var>d<sub>B</sub></var> <var>d<sub>C</sub></var>
<var>d<sub>D</sub></var> <var>d<sub>E</sub></var> <var>d<sub>F</sub></var>
<var>d<sub>G</sub></var> <var>d<sub>H</sub></var> <var>d<sub>I</sub></var>
</pre>
<p>
Each line consists of three digits separated by a space. The digit <var>d<sub>X</sub></var> (<var>X</var> is one of <var>A</var> through <var>I</var>) indicates the state of the square <var>X</var> as shown in Figure E.2. Digits <span>1</span>, . . . , <span>8</span> indicate that the piece of that number is on the square. The digit <span>0</span> indicates that the square is empty.
</p>
<p>
The end of the input is indicated by two zeros separated by a space.
</p>
<H2>Output</H2>
<p>
For each dataset, output the minimum total cost to achieve the goal, in a line. The total cost is the sum of the costs of moves from the starting arrangement to the goal arrangement. No other characters should appear in the output.
</p>
<H2>Sample Input</H2>
<pre>
4 9
6 3 0
8 1 2
4 5 7
6 3 0
8 1 2
4 5 7
31 31
4 3 6
0 1 5
8 2 7
0 3 6
4 1 5
8 2 7
92 4
1 5 3
4 0 7
8 2 6
1 5 0
4 7 3
8 2 6
12 28
3 4 5
0 2 6
7 1 8
5 7 1
8 6 2
0 3 4
0 0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
0
31
96
312
</pre> |
p02925 | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p><var>N</var> players will participate in a tennis tournament. We will call them Player <var>1</var>, Player <var>2</var>, <var>\ldots</var>, Player <var>N</var>.</p>
<p>The tournament is round-robin format, and there will be <var>N(N-1)/2</var> matches in total.
Is it possible to schedule these matches so that all of the following conditions are satisfied? If the answer is yes, also find the minimum number of days required.</p>
<ul>
<li>Each player plays at most one matches in a day.</li>
<li>Each player <var>i</var> <var>(1 \leq i \leq N)</var> plays one match against Player <var>A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1}</var> in this order.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>3 \leq N \leq 1000</var></li>
<li><var>1 \leq A_{i, j} \leq N</var></li>
<li><var>A_{i, j} \neq i</var></li>
<li><var>A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1}</var> are all different.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>A_{1, 1}</var> <var>A_{1, 2}</var> <var>\ldots</var> <var>A_{1, N-1}</var>
<var>A_{2, 1}</var> <var>A_{2, 2}</var> <var>\ldots</var> <var>A_{2, N-1}</var>
<var>:</var>
<var>A_{N, 1}</var> <var>A_{N, 2}</var> <var>\ldots</var> <var>A_{N, N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If it is possible to schedule all the matches so that all of the conditions are satisfied, print the minimum number of days required; if it is impossible, print <code>-1</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
2 3
1 3
1 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p>All the conditions can be satisfied if the matches are scheduled for three days as follows:</p>
<ul>
<li>Day <var>1</var>: Player <var>1</var> vs Player <var>2</var></li>
<li>Day <var>2</var>: Player <var>1</var> vs Player <var>3</var></li>
<li>Day <var>3</var>: Player <var>2</var> vs Player <var>3</var></li>
</ul>
<p>This is the minimum number of days required.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4
2 3 4
1 3 4
4 1 2
3 1 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>4
</pre>
<p>All the conditions can be satisfied if the matches are scheduled for four days as follows:</p>
<ul>
<li>Day <var>1</var>: Player <var>1</var> vs Player <var>2</var>, Player <var>3</var> vs Player <var>4</var></li>
<li>Day <var>2</var>: Player <var>1</var> vs Player <var>3</var></li>
<li>Day <var>3</var>: Player <var>1</var> vs Player <var>4</var>, Player <var>2</var> vs Player <var>3</var></li>
<li>Day <var>4</var>: Player <var>2</var> vs Player <var>4</var></li>
</ul>
<p>This is the minimum number of days required.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>3
2 3
3 1
1 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>-1
</pre>
<p>Any scheduling of the matches violates some condition.</p></section>
</div>
</span> |
p03637 | <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a sequence of length <var>N</var>, <var>a = (a_1, a_2, ..., a_N)</var>.
Each <var>a_i</var> is a positive integer.</p>
<p>Snuke's objective is to permute the element in <var>a</var> so that the following condition is satisfied:</p>
<ul>
<li>For each <var>1 ≤ i ≤ N - 1</var>, the product of <var>a_i</var> and <var>a_{i + 1}</var> is a multiple of <var>4</var>.</li>
</ul>
<p>Determine whether Snuke can achieve his objective.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 ≤ N ≤ 10^5</var></li>
<li><var>a_i</var> is an integer.</li>
<li><var>1 ≤ a_i ≤ 10^9</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If Snuke can achieve his objective, 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 10 100
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<p>One solution is <var>(1, 100, 10)</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4
1 2 3 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>No
</pre>
<p>It is impossible to permute <var>a</var> so that the condition is satisfied.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>3
1 4 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>Yes
</pre>
<p>The condition is already satisfied initially.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>2
1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>No
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 5</h3><pre>6
2 7 1 8 2 8
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 5</h3><pre>Yes
</pre></section>
</div>
</span> |
p02476 | <h2>Remainder of Big Integers</h2>
<p>
Given two integers $A$ and $B$, compute the remainder of $\frac{A}{B}$.
</p>
<h3>Input</h3>
<p>
Two integers $A$ and $B$ separated by a space character are given in a line.
</p>
<h3>Output</h3>
<p>
Print the remainder in a line.
</p>
<h3>Constraints</h3>
<ul>
<li>$0 \leq A, B \leq 10^{1000}$</li>
<li>$B \ne 0$</li>
</ul>
<h3>Sample Input 1</h3>
<pre>
5 8
</pre>
<h3>Sample Output 1</h3>
<pre>
5
</pre>
<h3>Sample Input 2</h3>
<pre>
100 25
</pre>
<h3>Sample Output 2</h3>
<pre>
0
</pre>
|
p02026 | <h2>L: スギ (Demon's Cedar)</h2>
<p>月の瀬さんは悪魔らしいことをするため、街じゅうの樹木をスギの木に変えて、人々を花粉症で困らせることにした。</p>
<p>樹木は $N$ 種類存在する。$i$ 種類目の樹木の天界でのランクは $A_i$、魔界でのランクは $B_i$ である。また、$1$ 種類目の樹木はスギである。</p>
<p>月の瀬さんは、次の操作を何度でも行うことができる。</p>
<ul>
<li>天界で、種類 $i$ の樹木を種類 $j$ に変える。これには $|A_i - A_j|$ の時間がかかる。</li>
<li>魔界で、種類 $i$ の樹木を種類 $j$ に変える。これには $|B_i - B_j|$ の時間がかかる。</li>
</ul>
<p>天界と魔界の移動や、その他の行動に時間はかからない。</p>
<p>それぞれの樹木の種類について、スギの木に変えるためにかかる最短の時間を求めよ。</p>
<h3>入力</h3>
<p>1 行目に、$N$ が与えられる。</p>
<p>2 行目に、$A_1, A_2, A_3, \dots, A_N$ が空白区切りで与えられる。</p>
<p>3 行目に、$B_1, B_2, B_3, \dots, B_N$ が空白区切りで与えられる。</p>
<h3>出力</h3>
<p>$N$ 行のうち $i$ 行目には、$i$ 種類目の樹木をスギの木に変えるための時間の最小値を出力せよ。</p>
<h3>制約</h3>
<ul>
<li>$N$ は $1$ 以上 $100 \ 000$ 以下の整数</li>
<li>$A_i, B_i$ は $1$ 以上 $1 \ 000 \ 000 \ 000$ 以下の整数</li>
</ul>
<h3>入力例1</h3>
<pre>
3
1 2 5
6 3 4
</pre>
<h3>出力例1</h3>
<pre>
0
1
2
</pre>
<p>樹木 1 は元からスギの木なので、コストはかかりません。</p>
<p>樹木 2 を天界で樹木 1 に変えると、コストは $|A_1 - A_2| = |1 - 2| = 1$ です。</p>
<p>樹木 3 を魔界で樹木 2 に変えたのち、天界で樹木 1 に変えるとコストは $|B_3 - B_2| + |A_2 - A_1| = |4 - 3| + |1 - 2| = 2$ です。</p>
<h3>入力例2</h3>
<pre>
5
1 10 5 6 9
2 3 4 7 8
</pre>
<h3>出力例2</h3>
<pre>
0
1
2
3
2
</pre>
|
p01523 |
<h1>D - 権力</h1>
<h2>問題文</h2>
<p>
10号館とはとある大学にある建物で,J研究科のメンバーが日夜研究に勤しんでいる施設である.
</p>
<p>
10号館は建物が古いことで有名であったが,今年ついに改装されることになった.
改装工事終了後は綺麗な環境で研究が出来ると皆が期待していたある日,
K研究科の一部が,改装工事終了後の10号館の部屋を侵略するという知らせが届いた.
</p>
<p>
もしK研究科に10号館の部屋が侵略されれば,その部屋で活動していたJ研究科のメンバーはこれまた建物が古いことで知られる2号館に移動する必要がある.
何とか阻止しなければならないので,J研究科は教授が権力を出しあって10号館の各部屋をK研究科から守ることにした.
</p>
<p>
10号館は <var>N</var> 個の部屋が1直線上に並んでおり,順番に <var>1,2,...,N</var> の番号が付けられている.
K研究科は,この <var>N</var> 個の部屋を全て侵略しようとしてくる.
J研究科には <var>M</var> 人の教授が在籍しており,各教授はある区間に含まれる部屋に対して権力を発揮できる.
1人以上の教授に権力を発揮された部屋はK研究科に侵略されることはなく守ることが出来るが,どの教授からも権力を発揮されなかった部屋は侵略されてしまう.
</p>
<p>
教授は研究で忙しいので,出来るだけ少人数で全ての部屋を守りたい.
全ての部屋を守るために権力を発揮するべき教授の最少人数を求めよ.
なお,教授全員が権力を発揮しても全ての部屋を守ることが出来無い場合があることに注意せよ.
</p>
<h2>入力形式</h2>
<p>
入力は以下の形式で与えられる.
</p>
<pre>
<var>N</var> <var>M</var>
<var>a<sub>1</sub></var> <var>b<sub>1</sub></var>
...
<var>a<sub>M</sub></var> <var>b<sub>M</sub></var>
</pre>
<p>
<var>N</var> は10号館の部屋の数であり,<var>M</var> はJ研究科の教授の数である.
</p>
<p>
<var>a<sub>i</sub>, b<sub>i</sub></var> は, 教授 <var>i</var> が部屋 <var>a<sub>i</sub>, a<sub>i</sub>+1, ..., b<sub>i</sub></var>
に対して権力を発揮することが出来る事を意味する.
</p>
<h2>出力形式</h2>
<p>10号館の全ての部屋を守ることが出来るなら,そのために権力を発揮するべき教授の最少人数を1行に出力せよ.
そうでなければ<code>Impossible</code>
を1行に出力せよ. </p>
<h2>制約</h2>
<ul>
<li><var>1 ≤ N ≤ 100, 1 ≤ M ≤ 100</var></li>
<li><var>1 ≤ a<sub>i</sub> ≤ b<sub>i</sub> ≤ N</var></li>
<li>入力値はすべて整数である.</li>
</ul>
<h2>入出力例</h2>
<h3>入力例 1</h3>
<pre>
8 4
3 7
1 5
2 5
4 8
</pre>
<h3>出力例 1</h3>
<pre>
2
</pre>
<p>
<var>2</var> 番目の教授と <var>4</var> 番目の教授が権力を発揮することにより,10号館の全ての部屋を守ることが出来る.
</p>
<h3>入力例 2</h3>
<pre>
8 4
1 4
2 5
3 6
4 7
</pre>
<h3>出力例 2</h3>
<pre>
Impossible
</pre>
<hr>
<address>Writer: 田村和範</address>
<address>Tester: 花田裕一朗</address>
|
p01173 |
<H1><font color="#000">Problem C:</font> Dig or Climb</H1>
<p>
Benjamin Forest VIII is a king of a country. One of his best friends Nod lives in a village far from
his castle. Nod gets seriously sick and is on the verge of death. Benjamin orders his subordinate
Red to bring good medicine for him as soon as possible. However, there is no road from the
castle to the village. Therefore, Red needs to climb over mountains and across canyons to reach
the village. He has decided to get to the village on the shortest path on a map, that is, he will
move on the straight line between the castle and the village. Then his way can be considered as
polyline with <i>n</i> points (<i>x</i><sub>1</sub>, <i>y</i><sub>1</sub>) . . . (<i>x<sub>n</sub></i> , <i>y<sub>n</sub></i> ) as illustlated in the following figure.
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_digOrClimb">
<p>Figure 1: An example route from the castle to the village</p>
</center>
<p>
Here, <i>x<sub>i</sub></i> indicates the distance between the castle and the point <i>i</i>, as the crow flies, and <i>y<sub>i</sub></i>
indicates the height of the point <i>i</i>. The castle is located on the point (<i>x</i><sub>1</sub> , <i>y</i><sub>1</sub> ), and the village is
located on the point (<i>x<sub>n</sub></i> , <i>y<sub>n</sub></i>).
</p>
<p>
Red can walk in speed <i>v<sub>w</sub></i> . Also, since he has a skill to cut a tunnel through a mountain
horizontally, he can move inside the mountain in speed <i>v<sub>c</sub></i>.
</p>
<p>
Your job is to write a program to the minimum time to get to the village.
</p>
<H2>Input</H2>
<p>
The input is a sequence of datasets. Each dataset is given in the following format:
</p>
<pre>
<i>n</i>
<i>v<sub>w</sub> v<sub>c</sub></i>
<i>x</i><sub>1</sub> <i>y</i><sub>1</sub>
...
<i>x<sub>n</sub> y<sub>n</sub></i>
</pre>
<p>
You may assume all the following: <i>n</i> ≤ 1,000, 1 ≤ <i>v<sub>w</sub></i>, <i>v<sub>c</sub></i> ≤ 10, -10,000 ≤ <i>x<sub>i</sub></i>, <i>y<sub>i</sub></i> ≤ 10,000, and
<i>x<sub>i</sub></i> < <i>x<sub>j</sub></i> for all <i>i</i> < <i>j</i>.
</p>
<p>
The input is terminated in case of <i>n</i> = 0. This is not part of any datasets and thus should not
be processed.
</p>
<H2>Output</H2>
<p>
For each dataset, you should print the minimum time required to get to the village in a line.
Each minimum time should be given as a decimal with an arbitrary number of fractional digits
and with an absolute error of at most 10<sup>-6</sup> . No extra space or character is allowed.
</p>
<H2>Sample Input</H2>
<pre>
3
2 1
0 0
50 50
100 0
3
1 1
0 0
50 50
100 0
3
1 2
0 0
50 50
100 0
3
2 1
0 0
100 100
150 50
6
1 2
0 0
50 50
100 0
150 0
200 50
250 0
0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
70.710678
100.000000
50.000000
106.066017
150.000000
</pre>
|
p01489 |
<script type="text/x-mathjax-config">
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});
</script>
<script type='text/javascript' src='http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>
</script>
<h2>Problem D:
IkaNumber
</h2>
<p>
数直線上の整数座標に, ねこの Taro の家, にんじん, うさぎの Hanako の家がこの順に並んでいる. 点 $x$ にいるイカは, 一回のジャンプで $x + 1$ または $x + 2$ に移動することができる.
</p>
<p>
あなたは, イカが Taro の家から Hanako の家までにんじんの置かれている座標に止まらずに行く経路が何通りあるか求めようとしたが, Taro の家, にんじん, Hanako の家の座標を知らないので
求めることができなかった. そこで, 代わりに経路の数として考えられる数をすべて列挙することにした.
</p>
<p>
ある Taro の家, にんじん, Hanako の家の配置に対してイカの経路数となる整数をイカ数と呼ぶことにする. $K$ 番目に小さいイカ数 (1-indexed) を mod 1,000,000,007 で求めよ.
</p>
<h3>Constraints</h3>
<ul>
<li>$K$ will be between 1 and 1,000,000,000,000,000,000, inclusive.</li>
</ul>
<h3>Input</h3>
<p>
入力はイカの形式で与えられる:<br>
<br>
$K$
</p>
<h3>Output</h3>
<p>
$K$ 番目に小さいイカ数を 1,000,000,007 で割った余りを 1 行に出力せよ.
</p>
<h3>Sample Input 1</h3>
<pre>5</pre>
<h3>Sample Output 1</h3>
<pre>5</pre>
<h3>Sample Input 2</h3>
<pre>8</pre>
<h3>Sample Output 2</h3>
<pre>9</pre> |
p00332 |
<script type="text/x-mathjax-config">
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});
</script>
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML">
</script>
<H1>日本の暦 </H1>
<p>
「西暦」は西洋から輸入された概念ですが、日本には暦上の年を表す方法として「元号」に年を付けて識別する和暦という概念があります。例えば今年は西暦なら2016年ですが、和暦なら平成28年です。どちらもよく使われる年の表現方法ですが、ある年の西暦がわかっていても和暦で何年かわからない、またはその逆の状況を経験したことはありませんか?
</p>
<p>
西暦で年が与えられたとき和暦の年を、和暦で年が与えられたとき西暦の年を出力するプログラムを作成せよ。ただし、西暦と和暦の対応は、簡単のため以下のようにする。
</p>
<center>
<table>
<tr>
<th width="260">西暦</th>
<th width="260">和暦</th>
</tr>
<tr>
<td>1868年から1911年</td>
<td>明治元年から明治44年</td>
</tr>
<tr>
<td>1912年から1925年</td>
<td>大正元年から大正14年</td>
</tr>
<tr>
<td>1926年から1988年</td>
<td>昭和元年から昭和63年</td>
</tr>
<tr>
<td>1989年から2016年</td>
<td>平成元年から平成28年</td>
</tr>
</table>
</center>
<br>
<h2>Input</h2>
<p>
入力は以下の形式で与えられる。
</p>
<pre>
<var>E</var> <var>Y</var>
</pre>
<p>
入力は1行からなり、<var>E</var> (0 ≤ <var>E</var> ≤ 4) は与えられる暦の種類、<var>Y</var> はその暦での年であり、<var>E</var> が 0 のときは西暦 <var>Y</var> (1868 ≤ <var>Y</var> ≤ 2016)年、1 のときは和暦の明治 <var>Y</var> (1 ≤ <var>Y</var> ≤ 44)年、2 のときは和暦の大正 <var>Y</var> (1 ≤ <var>Y</var> ≤ 14)年、3 のときは和暦の昭和 <var>Y</var> (1 ≤ <var>Y</var> ≤ 63)年、4 のときは和暦の平成 <var>Y</var> (1 ≤ <var>Y</var> ≤ 28)年を表す。
</p>
<h2>Output</h2>
<p>
西暦ならば和暦に、和暦ならば西暦に変換した結果を、1行に出力する。ただし、西暦を和暦に変換した結果は、明治なら文字「M」、大正なら文字「T」、昭和なら文字「S」、平成なら文字「H」を先頭に付けて出力する。
</p>
<h2>Sample Input 1</h2>
<pre>
0 2015
</pre>
<h2>Sample Output 1</h2>
<pre>
H27
</pre>
<br/>
<h2>Sample Input 2</h2>
<pre>
0 1912
</pre>
<h2>Sample Output 2</h2>
<pre>
T1
</pre>
<br/>
<h2>Sample Input 3</h2>
<pre>
2 1
</pre>
<h2>Sample Output 3</h2>
<pre>
1912
</pre>
<br/>
<h2>Sample Input 4</h2>
<pre>
4 28
</pre>
<h2>Sample Output 4</h2>
<pre>
2016
</pre> |
p00762 |
<h1>Biased Dice</h1>
<!-- end en only -->
<!-- begin en only -->
<p>
Professor Random, known for his research on randomized algorithms, is now conducting an experiment on biased dice. His experiment consists of dropping a number of dice onto a plane, one after another from a fixed position above the plane. The dice fall onto the plane or dice already there, without rotating, and may roll and fall according to their property. Then he observes and records the status of the stack formed on the plane, specifically, how many times each number appears on the faces visible from above. All the dice have the same size and their face numbering is identical, which we show in Figure C-1.
</p>
<!-- end en only -->
<div align="center">
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_1181_1">
<!-- begin en only -->
<p>Figure C-1: Numbering of a die</p>
<!-- end en only -->
</div>
<!-- begin en only -->
<p>
The dice have very special properties, as in the following.
</p>
<p>
(1) Ordinary dice can roll in four directions, but the dice used in this experiment never roll in the directions of faces 1, 2 and 3; they can only roll in the directions of faces 4, 5 and 6. In the situation shown in Figure C-2, our die can only roll to one of two directions.
</p>
<!-- end en only -->
<div align="center">
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_1181_2">
<!-- begin en only -->
<p>Figure C-2: An ordinary die and a biased die</p>
<!-- end en only -->
</div>
<!-- begin en only -->
<p>
(2) The die can only roll when it will fall down after rolling, as shown in Figure C-3. When multiple possibilities exist, the die rolls towards the face with the largest number among those directions it can roll to.
</p>
<!-- end en only -->
<div align="center">
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_1181_3">
<!-- begin en only -->
<p>Figure C-3: A die can roll only when it can fall</p>
<!-- end en only -->
</div>
<!-- begin en only -->
<p>
(3) When a die rolls, it rolls exactly 90 degrees, and then falls straight down until its bottom face touches another die or the plane, as in the case [B] or [C] of Figure C-4.
</p>
<p>
(4) After rolling and falling, the die repeatedly does so according to the rules (1) to (3) above.
</p>
<!-- end en only -->
<div align="center">
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_1181_4">
<!-- begin en only -->
<p>Figure C-4: Example stacking of biased dice</p>
<!-- end en only -->
</div>
<!-- begin en only -->
<p>
For example, when we drop four dice all in the same orientation, 6 at the top and 4 at the front, then a stack will be formed as shown in Figure C-4.
</p>
<!-- end en only -->
<div align="center">
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_1181_5">
<!-- begin en only -->
<p>Figure C-5: Example records</p>
<!-- end en only -->
</div>
<!-- begin en only -->
<p>
After forming the stack, we count the numbers of faces with 1 through 6 visible from above and record them. For example, in the left case of Figure C-5, the record will be "0 2 1 0 0 0", and in the right case, "0 1 1 0 0 1".
</p>
<!-- end en only -->
<h3>Input</h3>
<!-- begin en only -->
<p>
The input consists of several datasets each in the following format.
</p>
<!-- end en only -->
<blockquote>
<i>n</i> <br>
<i>t</i><sub>1</sub> <i>f</i><sub>1</sub><br>
<i>t</i><sub>2</sub> <i>f</i><sub>2</sub><br>
... <br>
<i>t</i><sub><i>n</i></sub> <i>f</i><sub><i>n</i></sub><br>
</blockquote>
<!-- begin en only -->
<p>
Here, <i>n</i> (1 ≤ <i>n</i> ≤ 100) is an integer and is the number of the dice to be dropped. <i>t</i><sub><i>i</i></sub> and <i>f</i><sub><i>i</i></sub> (1 ≤ <i>t</i><sub><i>i</i></sub>, <i>f</i><sub><i>i</i></sub> ≤ 6) are two integers separated by a space and represent the numbers on the top and the front faces of the <i>i</i>-th die, when it is released, respectively.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
The end of the input is indicated by a line containing a single zero.
</p>
<!-- end en only -->
<h3>Output</h3>
<!-- begin en only -->
<p>
For each dataset, output six integers separated by a space. The integers represent the numbers of faces with 1 through 6 correspondingly, visible from above. There may not be any other characters in the output.
</p>
<!-- end en only -->
<h3>Sample Input</h3>
<pre>
4
6 4
6 4
6 4
6 4
2
5 3
5 3
8
4 2
4 2
4 2
4 2
4 2
4 2
4 2
4 2
6
4 6
1 5
2 3
5 3
2 4
4 2
0
</pre>
<h3>Output for the Sample Input</h3>
<pre>
0 1 1 0 0 1
1 0 0 0 1 0
1 2 2 1 0 0
2 3 0 0 0 0
</pre>
|
p01870 |
<!-- - - - - - begin nicebody - - - - - -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}
});
</script>
<script type="text/javascript" async
src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML">
</script>
<h1>F: リレー / Relay</h1>
<h2>問題文</h2>
<p>
湖に浮かぶ $N$ 個の小島からなるビワコという村がある.
ビワコ村には $N-1$ 本の簡単な作りの橋がある.
島には $0$ から $N-1$ まで,橋には $0$ から $N-2$ までの番号が付けられている.
$i$ 番の橋は $i+1$ 番の島と $p_i$ 番の島を直接つなぎ,長さは $w_i$ である.
村人はどの島の間もいくつかの橋を通って相互に移動できるようになっている.
</p>
<p>
ある村人の提案により,ビワコ村でリレー大会が開催されることとなった.
しかし,ビワコ村には閉路がなくトラックを用意できないため,
現在ある橋を $1$ つだけ掛け替えることによって閉路を作ろうと考えた.
この操作によって用意できる閉路のうち,長さが最大となるものの長さを求めなさい.
</p>
<h2>入力</h2>
<p>
$N$<br>
$p_0 \ w_0$<br>
$\vdots$<br>
$p_{n-2} \ w_{n-2}$<br>
</p>
<h2>制約</h2>
<p>
$2 \leq N \leq 100000$<br>
$0 \leq p_i \leq N-1$<br>
$1 \leq w_i \leq 1000$<br>
全て整数<br>
全ての島と島の間が到達可能である<br>
</p>
<h2>出力</h2>
<p>
答えを $1$ 行で出力せよ.
</p>
<h2>サンプル</h2>
<p>
各サンプルを図示すると次のようになる.
</p>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE3_RUPC20160306_F">
<h3>サンプル入力1</h3>
<pre>
5
0 1
0 2
0 3
0 4
</pre>
<h3>サンプル出力1</h3>
<pre>
9
</pre>
<h3>サンプル入力2</h3>
<pre>
12
0 6
1 1
0 3
3 4
0 2
5 1
6 1
0 2
8 1
9 1
10 2
</pre>
<h3>サンプル出力2</h3>
<pre>
19
</pre>
<h3>サンプル入力3</h3>
<pre>
2
0 1
</pre>
<h3>サンプル出力3</h3>
<pre>
1
</pre> |
p00298 |
<h1>力持ち</h1>
<p>
力持ちたちが集う力持ち学園がありました。力持ち学園の運動会では、力持ちたちが隊列を組んで行進します。
</p>
<p>
力持ちたちは常に自分たちの力を誇示したい一方で、彼らの大半は自分で歩きたくありません。そこで彼らの一部が一番下になり、その上に大勢の人を縦一列に持ちあげて歩くことで、実際に歩く人数を減らそうと考えました。
</p>
<p>
はじめに、<var>N</var> 人の力持ちは地面の上に横一列に並んでいて、それぞれ左側から 1,2,3,..., <var>N</var> と通し番号で呼ばれています。通し番号 <var>i</var> の力持ちの体重は <var>w<sub>i</sub></var> で最大 <var>c<sub>i</sub></var> の重量まで持つことができます。
</p>
<p>
力持ちは、以下の条件をすべて満たすときだけ、隣に立っている左右どちらかの力持ちを持ちあげることができます。
</p>
<ul>
<li> 自分の上下には力持ちはいない。つまり、誰かに持ち上げられてもいないし、誰かを持ちあげてもいない。</li>
<li> 隣の力持ちの体重が、自分が持つことのできる最大の重量以下である。ただし、隣の力持ちが既に誰かを持ち上げているなら、縦に積み上がった力持ちたちの体重の合計が、自分が持つことのできる最大の重量以下でなければならない。</li>
</ul>
<p>
例えば、次のような3人の力持ちの隊列を考えてみましょう。
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_PCK2014_mightyMan1" width="240">
</center>
<br>
<p>
下図のように、2の力持ちが持つことのできる重さが <var>w<sub>3</sub></var> 以上のとき、2の力持ちは3の力持ちを持ちあげることができます。続いて、1の力持ちが持つことのできる重さが <var>w<sub>2</sub></var> + <var>w<sub>3</sub></var> 以上のとき、1の力持ちは2の力持ちを持ちあげることができます。
</p>
<center>
<table>
<tr>
<td><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_PCK2014_mightyMan2" width="240"></td>
<td>
→
</td>
<td><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_PCK2014_mightyMan3" width="240"></td>
</tr>
</table>
</center>
<br>
<p>
また、下図のように3の力持ちが2の力持ちを持ちあげた場合は、1の力持ちの隣が、2の力持ちを持った3の力持ちになるので、1の力持ちは3の力持ちを持ちあげることができます。
</p>
<center>
<table>
<tr>
<td><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_PCK2014_mightyMan4" width="240"></td>
<td>
→
</td>
<td><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_PCK2014_mightyMan5" width="240"></td>
</tr>
</table>
</center>
<br>
<p>
2の力持ちが、下の図のように1と3の力持ちを両方持つことはできません。
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_PCK2014_mightyMan6" width="240">
</center>
<br>
<p>
力持ち学園の専属プログラマーとして、一番下で歩くことになる最小の人数を求めてください。
</p>
<h2>入力</h2>
<p>
入力は以下の形式で与えられる。
</p>
<pre>
<var>N</var>
<var>c<sub>1</sub></var> <var>w<sub>1</sub></var>
<var>c<sub>2</sub></var> <var>w<sub>2</sub></var>
:
<var>c<sub>N</sub></var> <var>w<sub>N</sub></var>
</pre>
<p>
1 行目に力持ちの人数 <var>N</var> (1 ≤ <var>N</var> ≤ 1000) が与えられる。続く <var>N</var> 行に <var>i</var> 番目の力持ちが持てる重量の最大値 <var>c<sub>i</sub></var> (1 ≤ <var>c<sub>i</sub></var> ≤ 100000) と体重 <var>w<sub>i</sub></var> (1 ≤ <var>w<sub>i</sub></var> ≤ 100000) が与えられる。
</p>
<h2>出力</h2>
<p>
最小の人数を1行に出力する。
</p>
<h2>入出力例 </h2>
<br>
<h2>入力例1 </h2>
<pre>
3
150 120
100 50
80 100
</pre>
<h2>出力例1</h2>
<pre>
1
</pre>
<br>
<h2>入力例2</h2>
<pre>
8
50 100
20 20
30 20
90 50
140 30
30 60
59 120
10 10
</pre>
<h2>出力例2</h2>
<pre>
3
</pre>
|
p00277 |
<H1>プログラミングコンテスト</H1>
<p>
白虎大学では毎年プログラミングコンテストが開催されています。チームの総数をNとしたとき、チームには1からNのチームIDがそれぞれ割り当てられています。コンテストは全てのチームの得点が0の状態から開始し、L秒間連続して行われます。
</p>
<p>
今年のコンテストは、テレビで中継されることになりました。コンテストの間テレビに映るのは、その時点で最も得点の高いチームだけです。ただし、該当するチームが複数いるときは、その中でチームIDの一番小さいチームが映ります。映すべきチームが変わると、瞬時にカメラが切り替わります。
</p>
<p>
あなたはコンテストのログを入手しました。ログには、あるチームの得点が変化した時のレコードがすべて含まれています。各レコードは「チーム<var>d</var>がコンテスト開始<var>t</var>秒後の時点で<var>x</var>点獲得した」という形式で与えられています。なお、減点される場合もあるので、現在の得点が0より小さくなることもあります。
</p>
<p>
コンテストのログを入力とし、コンテスト開始から終了までの間にテレビに映った時間が最も長いチームのIDを報告するプログラムを作成してください。
</p>
<h2>入力</h2>
<p>
入力は1つのデータセットからなる。入力は以下の形式で与えられる。
</p>
<pre>
<var>N</var> <var>R</var> <var>L</var>
<var>d<sub>1</sub></var> <var>t<sub>1</sub></var> <var>x<sub>1</sub></var>
<var>d<sub>2</sub></var> <var>t<sub>2</sub></var> <var>x<sub>2</sub></var>
:
<var>d<sub>R</sub></var> <var>t<sub>R</sub></var> <var>x<sub>R</sub></var>
</pre>
<p>
1行目は3つの整数からなる。<var>N</var>(2 ≤ <var>N</var> ≤ 100000)はチーム数、<var>R</var>(0 ≤ <var>R</var> ≤ 1000000)はログに含まれるレコードの数である。<var>L</var>(1 ≤ <var>L</var> ≤ 1000000)はコンテストの時間(長さ)を表す。続く<var>R</var>行に各レコードの情報が与えられる。各レコードはチーム<var>d<sub>i</sub></var>(1 ≤ <var>d<sub>i</sub></var> ≤ <var>N</var>)がコンテスト開始<var>t<sub>i</sub></var>(0 < <var>t<sub>i</sub></var> < <var>L</var>)秒後の時点で<var>x<sub>i</sub></var>(-1000 ≤ <var>x<sub>i</sub></var> ≤ 1000)点獲得あるいは減点されたことを示す。ただし、<var>t<sub>i</sub></var>, <var>x<sub>i</sub></var>は整数であり、<var>t<sub>i-1</sub></var> ≤ <var>t<sub>i</sub></var>である。
</p>
<h2>出力</h2>
<p>
コンテスト開始から終了までの間にテレビに映った時間が最も長いチームのIDを1行に出力する。ただし、最も長いチームが複数あるときは、その中でチームIDの一番小さいチームを出力する。
</p>
<h2>入力例 1</h2>
<pre>
3 4 600
3 100 5
1 200 10
2 400 20
3 500 20
</pre>
<h2>出力例 1</h2>
<pre>
1
</pre>
<br/>
<h2>入力例 2</h2>
<pre>
3 5 600
3 100 5
1 200 10
2 300 30
1 400 -8
2 400 -27
</pre>
<h2>出力例 2</h2>
<pre>
3
</pre> |
p00627 |
<H1><font color="#000000">Problem 01:</font> Kyudo: A Japanese Art of Archery</H1>
<p>
伊藤くんは高校入学後弓道部に入部しました。はじめは的に矢が届かず大苦戦していましたが、高校1年の秋になんとか的まで矢が届くくらいまで上達しました。
</p>
<p>
そんなある日先輩加藤くんから「弓道の近的競技に出場してみないか?」と提案が、、。
伊藤くんは初めての大会にチャレンジする気満々です。
</p>
<p>
近的競技とは、1回に4本行射(矢を射つ)し、そのうち的に当たった数(的中数)を記録します。これを複数回繰り返し、的中数の合計を競います。
</P>
<p>
大会に向けて一生懸命練習している伊藤くんのために、加藤くんは本番を想定して合計的中数を算出できるプログラムを作成してあげることにしました。
</p>
<p>
このプログラムは、合計射数 <i>n</i> と各回の的中数を読み込み、合計的中数を出力します。
</p>
<p>
例えば、合計射数 <i>n</i> が 20 の場合は、1 回に 4 本行射することを 5 回繰り返すので、的中回数を 5 回入力します。
</p>
<H2>Input</H2>
<p>
複数のデータセットが入力として与えられます。
</p>
<p>
各データセットの1行目に <i>n</i> (4 の倍数である整数) が与えられます。続いて各回の的中数を示す n/4 個の整数がそれぞれ 1 行に与えられます。
</p>
<p>
<i>n</i> が 0 のとき入力の終わりを示します。この入力に対する出力を行ってはいけません。
</p>
<H2>Output</H2>
<p>
各データセットに対して、合計的中数を 1 行に出力して下さい。
</p>
<H2>Sample Input</H2>
<pre>
20
4
3
2
1
3
8
2
0
0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
13
2
</pre>
|
p01935 |
<!-- - - - - - begin nicebody - - - - - -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({ tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]} });
</script>
<script type="text/javascript" async src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML">
</script>
<h1>E: 敵襲から守れ</h1>
<h2>問題</h2>
<p>島が $V$ 島あり、それぞれ $0, 1, ..., V-1$ と番号がついている。橋が $E$ 本あり、それぞれ $0, 1, ..., E-1$ と番号がついている。 $i$ 番目の橋は島 $s_i$ と島 $t_i$ にかかってあり、幅は $c_i$ である。</p>
<p>
島 $0$ に基地があるAORイカちゃん軍団 (通称イカ団) は規模が弱小であるため、島 $V-1$ にあるSosu-Usa軍団 (通称そすうさ団) にいつもやられっぱなしである。ある日、イカ団は「そすうさ団が明日攻めてくる」との情報を得た。そすうさ団の基地から大人数のそすうさ団の部下が橋を渡って島を移動し、部下が一人でもイカ団の基地にたどり着かれるとイカ団は滅びてしまう……。
</p>
<p>
そこでイカ団は橋に通行止めテープを張ることで橋を通れないようにし、危機を免れようとした。使うテープの長さは通行止めにした橋の幅の総和である。また、そすうさ団の部下全員が必ず<b>ある一本の幅 $1$ の橋</b>を通る場合、イカ団がそこで待ち伏せすることで、そすうさ団を通れなくすることができる。
</p>
<p>
保持しているテープの長さは $10^4$ である。今後のことも考え、消費するテープの量はなるべく少なくしたい。そすうさ団の部下がイカ団の基地にたどり着けないようにするために使うテープの長さの最小値を求めよ。ただし、テープの長さが足りずどうしても攻められてしまう合は
$-1$ を出力せよ。
</p>
<h2>制約</h2>
<ul>
<li>$2 \le V \le 5 \times 10^3$</li>
<li>$1 \le E \le min(5 \times 10^3, V(V-1)/2)$</li>
<li>$1 \le c_i \le 5 \times 10^3$</li>
<li>$0 \le s_i, t_i \lt V$</li>
<li>与えられるグラフは連結かつ単純である。</li>
<li>入力は全て整数で与えらえる。</li>
</ul>
<h2>入力形式</h2>
<p>入力は以下の形式で与えられる。</p>
<p>$V \ E$<br>$s_0 \ t_0 \ c_0$<br>$s_1 \ t_1 \ c_1$<br>$\vdots$<br>$s_{E-1} \ t_{E-1} \ c_{E-1}$</p>
<h2>出力</h2>
<p>そすうさ団の部下がイカ団の基地にたどり着けないようにするために使うテープの長さの最小値を出力せよ。ただし、テープの長さが足りずどうしても攻められてしまう場合は $-1$ を出力せよ。また、末尾に改行も出力せよ。</p>
<h2>サンプル</h2>
<h3>サンプル入力 1</h3>
<pre>4 4
0 1 3
0 2 4
1 3 1
2 3 5
</pre>
<h3>サンプル出力 1</h3>
<pre>4
</pre>
<p>
イカ団は $2$ 番目の橋で待ち伏せし、$1$ 番の橋にテープを貼ることで、そすうさ団を止められる。
</p>
<h3>サンプル入力 2</h3>
<pre>2 1
0 1 100
</pre>
<h3>サンプル出力 2</h3>
<pre>100
</pre>
<p>$0$ 番目の橋にテープを貼ることで、そすうさ団を止められる。 橋の幅が $1$ でないため、待ち伏せはできない。</p>
<h3>サンプル入力 3</h3>
<pre>5 6
0 1 5000
0 2 5000
0 3 5000
1 4 5000
2 4 5000
3 4 5000
</pre>
<h3>サンプル出力 3</h3>
<pre>-1
</pre>
<p>テープが足りないため、どのようにテープを貼ってもそうすさ団は止められない。</p>
<!-- - - - - - end nicebody - - - - - --> |
p03908 | <span class="lang-en">
<p>Score : <var>1500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>The currency used in Takahashi Kingdom is <em>Myon</em>.
There are <var>1</var>-, <var>10</var>-, <var>100</var>-, <var>1000</var>- and <var>10000</var>-Myon coins, and so forth. Formally, there are <var>10^n</var>-Myon coins for any non-negative integer <var>n</var>.</p>
<p>There are <var>N</var> items being sold at Ex Store.
The price of the <var>i</var>-th <var>(1≦i≦N)</var> item is <var>A_i</var> Myon.</p>
<p>Takahashi is going to buy some, at least one, possibly all, of these <var>N</var> items.
He hates receiving change, so he wants to bring coins to the store so that he can pay the total price without receiving change, no matter what items he chooses to buy.
Also, since coins are heavy, he wants to bring as few coins as possible.</p>
<p>Find the minimum number of coins he must bring to the store. It can be assumed that he has an infinite supply of coins.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1≦N≦20,000</var></li>
<li><var>1≦A_i≦10^{12}</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>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 coins Takahashi must bring to the store, so that he can pay the total price without receiving change, no matter what items he chooses to buy.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
43 24 37
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>16
</pre>
<p>There are seven possible total prices: <var>24, 37, 43, 61, 67, 80,</var> and <var>104</var>.
With seven <var>1</var>-Myon coins, eight <var>10</var>-Myon coins and one <var>100</var>-Myon coin, Takahashi can pay any of these without receiving change.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5
49735011221 970534221705 411566391637 760836201000 563515091165
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>105
</pre></section>
</div>
</span> |
p01466 |
<script src="./IMAGE/varmath.js" charset="UTF-8"></script>
<H1>World Trip</H1>
<p>
Kita_masa is planning a trip around the world.
This world has <var>N</var> countries and the country <var>i</var> has <var>M_i</var> cities.
Kita_masa wants to visit every city exactly once, and return back to the starting city.
</p>
<p>
In this world, people can travel only by airplane.
There are two kinds of airlines: domestic and international lines.
Since international airports require special facilities such as customs and passport control,
only a few cities in each country have international airports.
</p>
<p>
You are given a list of all flight routes in this world and prices for each route.
Now it's time to calculate the cheapest route for Kita_masa's world trip!
</p>
<H2>Input</H2>
<p>
</p>
<p>
The first line contains two integers <var>N</var> and <var>K</var>, which represent the number of countries and the number of flight routes, respectively.
The second line contains <var>N</var> integers, and the <var>i</var>-th integer <var>M_i</var> represents the number of cities in the country <var>i</var>.
The third line contains <var>N</var> integers too, and the <var>i</var>-th integer <var>F_i</var> represents the number of international airports in the country <var>i</var>.
Each of the following <var>K</var> lines contains five integers [Country 1] [City 1] [Country 2] [City 2] [Price].
This means that, there is a bi-directional flight route between the [City 1] in [Country 1] and the [City 2] in [Country 2], and its price is [Price].
</p>
<p>
Note that cities in each country are numbered from 1, and the cities whose city number is smaller or equal to <var>F_i</var> have international airports.
That is, if there is a flight route between the city <var>c_1</var> in the country <var>n_1</var> and the city <var>c_2</var> in the country <var>n_2</var> with <var>n_1 \neq n_2</var>, it must be <var>c_1 \leq F_{n_1}</var> and <var>c_2 \leq F_{n_2}</var>.
You can assume that there's no flight route which departs from one city and flies back to the same city, and that at most one flight route exists in this world for each pair of cities.
</p>
<p>
The following constraints hold for each dataset:
</p>
<ul>
<li> <var>1 \leq N \leq 15</var></li>
<li> <var>1 \leq M_i \leq 15</var></li>
<li> <var>1 \leq F_i \leq 4</var></li>
<li> <var>sum(F_i) \leq 15</var></li>
<li> <var>1 \leq Price \leq 10,000</var></li>
</ul>
<H2>Output</H2>
<p>
Print a line that contains a single integer representing the minimum price to make a world trip.
</p>
<p>
If such a trip is impossible, print -1 instead.
</p>
<H2>Sample Input 1</H2>
<pre>
4 6
1 1 1 1
1 1 1 1
1 1 2 1 1
1 1 3 1 2
1 1 4 1 1
2 1 3 1 1
2 1 4 1 2
3 1 4 1 1
</pre>
<H2>Output for the Sample Input 1</H2>
<pre>
4
</pre>
<H2>Sample Input 2</H2>
<pre>
2 16
4 4
2 2
1 1 1 2 1
1 1 1 3 2
1 1 1 4 1
1 2 1 3 1
1 2 1 4 2
1 3 1 4 1
2 1 2 2 1
2 1 2 3 2
2 1 2 4 1
2 2 2 3 1
2 2 2 4 2
2 3 2 4 1
1 1 2 1 1
1 1 2 2 2
1 2 2 1 2
1 2 2 2 1
</pre>
<H2>Output for the Sample Input 2</H2>
<pre>
8
</pre>
<H2>Sample Input 3</H2>
<pre>
2 2
2 1
1 1
1 1 1 2 1
1 1 2 1 1
</pre>
<H2>Output for the Sample Input 3</H2>
<pre>
-1
</pre>
|
p01036 | <h1>Problem J: Yu-kun Likes To Play Darts</h1>
<h2>Background</h2>
<p>
会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらいダーツが大好きだ。
そんなゆう君は最近ダーツにはまっていたが、普通のダーツに飽きてしまったため独自のダーツボードを作ることにした。
</p>
<p>
ダーツについては<a href="http://ja.wikipedia.org/wiki/ダーツ">ダーツ</a>を参照。
</p>
<h2>Problem</h2>
<p>
ゆう君が考えたダーツの内容は以下の通りだ。
</p>
<p>
無限に広いダーツボードには得点の書かれた多角形が幾つかある。
プレイヤーはダーツの矢を1本だけ持っている。
プレイヤーは矢を投げ、いずれかの多角形に矢を刺すことでそこに書かれている得点を得る。
それ以外に刺さった場合は得点は得られない。
</p>
<p>
ゆう君は矢を投げる位置を決めたのだが、正確に刺さるとは限らない。
ダーツボードを2次元平面とし、ゆう君が狙う位置を点(<var>cx</var>,<var>cy</var>)とする。
ゆう君の投げた矢が刺さる場所は、点(<var>cx</var>,<var>cy</var>)を中心とする半径<var>r</var>の円に含まれる任意の点から一様な確率で選ばれる。
ばれる点の座標は整数である必要はない。
</p>
<p>
ダーツボードの情報とゆう君が狙う位置(<var>cx</var>,<var>cy</var>)、半径<var>r</var>が与えれるので、ゆう君が獲得することの出来る得点の期待値を答えよ。
</p>
<h2>Input</h2>
<pre>
<var>n</var> <var>cx</var> <var>cy</var> <var>r</var>
0番目の多角形の情報
1番目の多角形の情報
...
<var>(n-1)</var>番目の多角形の情報
</pre>
<p>
<var>n</var>はダーツボード上にある多角形の数である。
多角形の情報は以下の形式で与えられる。
</p>
<pre>
<var>p</var> <var>score</var>
<var>x<sub>0</sub></var> <var>y<sub>0</sub></var>
<var>x<sub>1</sub></var> <var>y<sub>1</sub></var>
...
<var>x<sub>(p-1)</sub></var> <var>y<sub>(p-1)</sub></var>
</pre>
<p>
<var>p</var>は多角形の頂点数を表し、<var>score</var>は多角形に書かれている得点を表す。
多角形の各線分は(<var>x<sub>i</sub></var>,<var>y<sub>i</sub></var>)と(<var>x<sub>i+1</sub></var>,<var>y<sub>i+1</sub></var>) ( <var>i</var> < <var>p</var>-1 )の頂点を繋いだ線分と、(<var>x<sub>p-1</sub></var>,<var>y<sub>p-1</sub></var>)と(<var>x<sub>0</sub></var>,<var>y<sub>0</sub></var>)の頂点を繋いだ線分である。
</pre>
<h2>Constraints</h2>
<!-- 多角形をカットするのであれば、nを減らすこと -->
<p>入力は以下の条件を満たす。</p>
<ul>
<li>入力は全て整数で与えられる</li>
<li>1 ≤ <var>n</var> ≤ 50</li>
<li>0 ≤ <var>cx</var>, <var>cy</var>, <var>x</var>, <var>y</var> ≤ 1000</li>
<li>1 ≤ <var>r</var> ≤ 100</var>
<li>3 ≤ <var>p</var> ≤ 10</li>
<li>1 ≤ <var>score</var> ≤ 100</li>
<li>多角形の頂点は、隣り合った頂点を時計回り、または反時計回りに訪問するような順番で与えられる</li>
<li>多角形の辺が別の多角形の辺と共通な点をもつことはない</li>
<li>多角形が別の多角形を内包することはない</li>
<li>矢が多角形の辺上に刺さった場合、得点は得られないものとする</li>
</ul>
<h2>Output</h2>
<p>
ゆう君が獲得することの出来る得点の期待値を1行で出力せよ。小数点以下は何桁数字を出力しても構わない。ただし、解答の誤差は0.000001(10<sup>-6</sup>)を超えてはならない。
</p>
<h2>Sample Input 1</h2>
<pre>
1 2 2 1
4 1
0 0
2 0
2 2
0 2
</pre>
<h2>Sample Output 1</h2>
<pre>
0.2500000000
</pre>
<h2>Sample Input 2</h2>
<pre>
1 2 2 1
4 1
0 0
5 0
5 5
0 5
</pre>
<h2>Sample Output 2</h2>
<pre>
1.0000000000
</pre>
<h2>Sample Input 3</h2>
<pre>
4 3 3 2
3 1
1 1
3 3
1 5
4 2
2 0
5 0
4 2
3 2
3 3
4 3
6 1
6 5
4 4
3 4
4 4
5 6
2 6
</pre>
<h2>Sample Output 3</h2>
<pre>
1.0574955319
</pre>
<h2>Sample Input 4</h2>
<pre>
1 10 10 1
4 10
0 0
1 0
1 1
0 1
</pre>
<h2>Sample Output 4</h2>
<pre>
0.0000000000
</pre>
|
p02837 | <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 numbered <var>1</var> to <var>N</var>. Each of them is either an <em>honest</em> person whose testimonies are always correct or an <em>unkind</em> person whose testimonies may be correct or not.</p>
<p>Person <var>i</var> gives <var>A_i</var> testimonies. The <var>j</var>-th testimony by Person <var>i</var> is represented by two integers <var>x_{ij}</var> and <var>y_{ij}</var>. If <var>y_{ij} = 1</var>, the testimony says Person <var>x_{ij}</var> is honest; if <var>y_{ij} = 0</var>, it says Person <var>x_{ij}</var> is unkind.</p>
<p>How many honest persons can be among those <var>N</var> people at most?</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 15</var></li>
<li><var>0 \leq A_i \leq N - 1</var></li>
<li><var>1 \leq x_{ij} \leq N</var></li>
<li><var>x_{ij} \neq i</var></li>
<li><var>x_{ij_1} \neq x_{ij_2} (j_1 \neq j_2)</var></li>
<li><var>y_{ij} = 0, 1</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>A_1</var>
<var>x_{11}</var> <var>y_{11}</var>
<var>x_{12}</var> <var>y_{12}</var>
<var>:</var>
<var>x_{1A_1}</var> <var>y_{1A_1}</var>
<var>A_2</var>
<var>x_{21}</var> <var>y_{21}</var>
<var>x_{22}</var> <var>y_{22}</var>
<var>:</var>
<var>x_{2A_2}</var> <var>y_{2A_2}</var>
<var>:</var>
<var>A_N</var>
<var>x_{N1}</var> <var>y_{N1}</var>
<var>x_{N2}</var> <var>y_{N2}</var>
<var>:</var>
<var>x_{NA_N}</var> <var>y_{NA_N}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum possible number of honest persons among the <var>N</var> people.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
1
2 1
1
1 1
1
2 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>If Person <var>1</var> and Person <var>2</var> are honest and Person <var>3</var> is unkind, we have two honest persons without inconsistencies, which is the maximum possible number of honest persons.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
2
2 1
3 0
2
3 1
1 0
2
1 1
2 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Assuming that one or more of them are honest immediately leads to a contradiction.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>2
1
2 0
1
1 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>1
</pre></section>
</div>
</span> |
p03725 | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Takahashi is locked within a building.</p>
<p>This building consists of <var>H×W</var> rooms, arranged in <var>H</var> rows and <var>W</var> columns.
We will denote the room at the <var>i</var>-th row and <var>j</var>-th column as <var>(i,j)</var>. The state of this room is represented by a character <var>A_{i,j}</var>. If <var>A_{i,j}=</var> <code>#</code>, the room is locked and cannot be entered; if <var>A_{i,j}=</var> <code>.</code>, the room is not locked and can be freely entered.
Takahashi is currently at the room where <var>A_{i,j}=</var> <code>S</code>, which can also be freely entered.</p>
<p>Each room in the <var>1</var>-st row, <var>1</var>-st column, <var>H</var>-th row or <var>W</var>-th column, has an exit.
Each of the other rooms <var>(i,j)</var> is connected to four rooms: <var>(i-1,j)</var>, <var>(i+1,j)</var>, <var>(i,j-1)</var> and <var>(i,j+1)</var>.</p>
<p>Takahashi will use his magic to get out of the building. In one cast, he can do the following:</p>
<ul>
<li>Move to an adjacent room at most <var>K</var> times, possibly zero. Here, locked rooms cannot be entered.</li>
<li>Then, select and unlock at most <var>K</var> locked rooms, possibly zero. Those rooms will remain unlocked from then on.</li>
</ul>
<p>His objective is to reach a room with an exit. Find the minimum necessary number of casts to do so.</p>
<p>It is guaranteed that Takahashi is initially at a room without an exit.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>3 ≤ H ≤ 800</var></li>
<li><var>3 ≤ W ≤ 800</var></li>
<li><var>1 ≤ K ≤ H×W</var></li>
<li>Each <var>A_{i,j}</var> is <code>#</code> , <code>.</code> or <code>S</code>.</li>
<li>There uniquely exists <var>(i,j)</var> such that <var>A_{i,j}=</var> <code>S</code>, and it satisfies <var>2 ≤ i ≤ H-1</var> and <var>2 ≤ j ≤ W-1</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>H</var> <var>W</var> <var>K</var>
<var>A_{1,1}A_{1,2}</var>...<var>A_{1,W}</var>
:
<var>A_{H,1}A_{H,2}</var>...<var>A_{H,W}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum necessary number of casts.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 3 3
#.#
#S.
###
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
</pre>
<p>Takahashi can move to room <var>(1,2)</var> in one cast.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 3 3
###
#S#
###
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>2
</pre>
<p>Takahashi cannot move in the first cast, but can unlock room <var>(1,2)</var>.
Then, he can move to room <var>(1,2)</var> in the next cast, achieving the objective in two casts.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>7 7 2
#######
#######
##...##
###S###
##.#.##
###.###
#######
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>2
</pre></section>
</div>
</span> |
p01718 |
<h1>問題 G : Perm Query </h1>
<h2>問題文</h2>
<p>
<var>\{1,  2,  … ,  N\}</var>の順列 <var>(p(1),  p(2),  … ,  p(n))</var> が与えられる.
<var>(l_i,  r_i)</var> からなるクエリが <var>Q</var> 個与えられるので,各クエリに対して以下の擬似コードによる処理結果を出力せよ.
</p>
<ol>
<li><var>ret   :=   0</var>, <var>(x(1),  x(2),  … ,  x(N))  :=  (1,  2,  … ,  N)</var> とおく.</li>
<li>各<var>i   ∈ \{1,   2,  … ,   N\}</var> について <var>y(i) := p(x(i))</var> とする.</li>
<li>各<var>i   ∈ \{1,   2,  … ,   N\}</var> について <var>x(i)   =  y(i)</var>とする.</li>
<li><var>ret   :=  ret + x(l_i) + x(l_i+1) + …  + x(r_i)</var></li>
<li>もし <var>(x(l_i),  x(l_i+1),  … ,  x(r_i)) = (l_i,  l_i+1,  … ,  r_i)</var> なら <var>(ret   </var><b>mod</b><var>10^9+7)</var> を出力して終了する.そうでないなら 2 行目に戻る.</li>
</ol>
<h2>入力形式</h2>
<p>
入力は以下の形式で与えられる
<pre>
<var>N</var> <var>Q</var>
<var>p(1)</var> <var>p(2)</var> <var>...</var> <var>p(N)</var>
<var>l_0</var> <var>r_0</var>
<var>...</var>
<var>l_{Q-1}</var> <var>r_{Q-1}</var>
</pre>
<h2>出力形式</h2>
<p>
各クエリに対する出力を1行ずつ出力せよ.
</p>
<h2>制約</h2>
<ul>
<li><var>1 ≤ N ≤ 10^5</var></li>
<li><var>1 ≤ Q ≤ 10^4</var></li>
<li><var>(p_1,  p_2,  … ,  p_N)</var> は <var>(1,  2,  … ,  N)</var> の順列になっている.</li>
<li>各 <var>i</var> に対して,ある <var>1 ≤ k ≤ 40</var> が存在して,<var>p^k(i)=i</var> となる.ここで,<var>p^k(i)</var> は <var>p(p(p(… p(i)… )))</var> で <var>p</var> が <var>k</var> 回現れるもの.</li>
<li><var>1 ≤ l_i   ≤   r_i   ≤ N</var></li>
</ul>
<h2>入出力例</h2>
<h3>入力例 1</h3>
<pre>
5 2
5 1 2 3 4
1 1
2 4
</pre>
<h3>出力例 1</h3>
<pre>
15
45
</pre>
<p>
擬似コード中の順列<var>(x(1),   x(2),   … ,  x(N))</var>は以下のように変化する.
<pre>
1 2 3 4 5
5 1 2 3 4
4 5 1 2 3
3 4 5 1 2
2 3 4 5 1
1 2 3 4 5
</pre>
</p>
<h3>入力例 2</h3>
<pre>
10 5
3 1 2 5 4 10 6 7 9 8
1 10
1 5
3 3
5 10
9 10
</pre>
<h3>出力例 2</h3>
<pre>
660
90
6
178
67
</pre>
|
p03375 | <span class="lang-en">
<p>Score : <var>900</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>In "Takahashi-ya", a ramen restaurant, basically they have one menu: "ramen", but <var>N</var> kinds of toppings are also offered. When a customer orders a bowl of ramen, for each kind of topping, he/she can choose whether to put it on top of his/her ramen or not. There is no limit on the number of toppings, and it is allowed to have all kinds of toppings or no topping at all. That is, considering the combination of the toppings, <var>2^N</var> types of ramen can be ordered.</p>
<p>Akaki entered Takahashi-ya. She is thinking of ordering some bowls of ramen that satisfy both of the following two conditions:</p>
<ul>
<li>Do not order multiple bowls of ramen with the exactly same set of toppings.</li>
<li>Each of the <var>N</var> kinds of toppings is on two or more bowls of ramen ordered.</li>
</ul>
<p>You are given <var>N</var> and a prime number <var>M</var>. Find the number of the sets of bowls of ramen that satisfy these conditions, disregarding order, modulo <var>M</var>. Since she is in extreme hunger, ordering any number of bowls of ramen is fine.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq N \leq 3000</var></li>
<li><var>10^8 \leq M \leq 10^9 + 9</var></li>
<li><var>N</var> is an integer.</li>
<li><var>M</var> is a prime number.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Subscores</h3><ul>
<li><var>600</var> points will be awarded for passing the test set satisfying <var>N ≤ 50</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>M</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of the sets of bowls of ramen that satisfy the conditions, disregarding order, modulo <var>M</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 1000000007
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>Let the two kinds of toppings be A and B. Four types of ramen can be ordered: "no toppings", "with A", "with B" and "with A, B". There are two sets of ramen that satisfy the conditions:</p>
<ul>
<li>The following three ramen: "with A", "with B", "with A, B".</li>
<li>Four ramen, one for each type.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 1000000009
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>118
</pre>
<p>Let the three kinds of toppings be A, B and C. In addition to the four types of ramen above, four more types of ramen can be ordered, where C is added to the above four. There are <var>118</var> sets of ramen that satisfy the conditions, and here are some of them:</p>
<ul>
<li>The following three ramen: "with A, B", "with A, C", "with B, C".</li>
<li>The following five ramen: "no toppings", "with A", "with A, B", "with B, C", "with A, B, C".</li>
<li>Eight ramen, one for each type.</li>
</ul>
<p>Note that the set of the following three does not satisfy the condition: "'with A', 'with B', 'with A, B'", because C is not on any of them.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>50 111111113
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>1456748
</pre>
<p>Remember to print the number of the sets modulo <var>M</var>. Note that these three sample inputs above are included in the test set for the partial score.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>3000 123456791
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>16369789
</pre>
<p>This sample input is not included in the test set for the partial score.</p></section>
</div>
</span> |
p01348 |
<H1><font color="#000">Problem J: </font> Cruel Bingo</H1>
<p>
Bingo is a party game played by many players and one game master. Each player is given a bingo card containing <i>N</i><sup>2</sup> different numbers in a <i>N</i> × <i>N</i> grid (typically <i>N</i> = 5). The master draws numbers from a lottery one by one during the game. Each time a number is drawn, a player marks a square with that number if it exists. The player’s goal is to have <i>N</i> marked squares in a single vertical, horizontal, or diagonal line and then call “Bingo!” The first player calling “Bingo!” wins the game.
</p>
<p>
In ultimately unfortunate cases, a card can have exactly <i>N</i> unmarked squares, or <i>N</i>(<i>N</i>-1) marked squares, but not a bingo pattern. Your task in this problem is to write a program counting how many such patterns are possible from a given initial pattern, which contains zero or more marked squares.
</p>
<H2>Input</H2>
<p>
The input is given in the following format:
</p>
<p>
<i>N K</i><br>
<i>x</i><sub>1</sub> <i>y</i><sub>1</sub><br>
.<br>
.<br>
.<br>
<i>x<sub>K</sub> y<sub>K</sub></i><br>
</p>
<p>
The input begins with a line containing two numbers <i>N</i> (1 ≤ <i>N</i> ≤ 32) and <i>K</i> (0 ≤ <i>K</i> ≤ 8), which represent the size of the bingo card and the number of marked squares in the initial pattern respectively.
Then <i>K</i> lines follow, each containing two numbers <i>x<sub>i</sub></i> and <i>y<sub>i</sub></i> to indicate the square at (<i>x<sub>i</sub></i>, <i>y<sub>i</sub></i>) is marked. The coordinate values are zero-based (i.e. 0 ≤ <i>x<sub>i</sub></i>, <i>y<sub>i</sub></i> ≤ <i>N</i> - 1). No pair of marked squares coincides.
</p>
<H2>Output</H2>
<p>
Count the number of possible non-bingo patterns with exactly <i>N</i> unmarked squares that can be made from the given initial pattern, and print the number in modulo 10007 in a line (since it is supposed to be huge). Rotated and mirrored patterns should be regarded as different and counted each.
</p>
<H2>Sample Input 1</H2>
<pre>
4 2
0 2
3 1
</pre>
<H2>Output for the Sample Input 1</H2>
<pre>
6
</pre>
<H2>Sample Input 2</H2>
<pre>
4 2
2 3
3 2
</pre>
<H2>Output for the Sample Input 2</H2>
<pre>
6
</pre>
<H2>Sample Input 3</H2>
<pre>
10 3
0 0
4 4
1 4
</pre>
<H2>Output for the Sample Input 3</H2>
<pre>
1127
</pre>
|
p00109 |
<H1>Smart Calculator</H1>
<p>
Your task is to write a program which reads an expression and evaluates it.
</p>
<ul>
<li>The expression consists of numerical values, operators and parentheses, and the ends with '='.</li>
<li>The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.</li>
<li> Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.
</li>
<li>You may assume that there is no division by zero.</li>
<li>All calculation is performed as integers, and after the decimal point should be truncated</li>
<li>Length of the expression will not exceed 100.</li>
<li>-1 × 10<sup>9</sup> ≤ intermediate results of computation ≤ 10<sup>9</sup></li>
</ul>
<H2>Input</H2>
<p>
The input is a sequence of datasets. The first line contains an integer <i>n</i> which represents the number of datasets. There will be <i>n</i> lines where each line contains an expression.
</p>
<H2>Output</H2>
<p>
For each datasets, prints the result of calculation.
</p>
<H2>Sample Input</H2>
<pre>
2
4-2*3=
4*(8+4+3)=
</pre>
<H2>Output for the Sample Input</H2>
<pre>
-2
60
</pre>
|
p02134 | <h1>Problem K: Move on Ice</h1>
<h2>Problem</h2>
<p>
ペンギンのフロロは無限に広い氷の上のマス($sx$,$sy$)にいる。<br>
マス($tx$,$ty$)に穴があり、そこから水に入ることができる。<br>
氷の上に$n$個の氷の塊があり、それぞれマス($x_i$,$y_i$)にある。<br>
</p>
<p>
フロロは上下左右の4方向に移動できる。<br>
氷の上は滑りやすいので、移動すると氷の塊にぶつかるまで動き続ける。<br>
氷の塊にぶつかると氷の塊のあるマスの1マス手前で止まる。<br>
穴のマスを通過することで穴に入ることができる。<br>
</p>
<p>
フロロは1度だけ全力で踏ん張ることにより途中で止まることができる。
</p>
<p>
氷の塊にぶつかると痛いのでできるだけぶつかる回数を少なくしたい。<br>
穴のあるマス($tx$,$ty$)へ移動するまでにぶつかる回数の最小値を求めよ。<br>
辿り着けない場合は$-$1を出力せよ。
</p>
<h2>Input</h2>
<p>
入力は以下の形式で与えられる。
</p>
<pre>
$sx$ $sy$
$tx$ $ty$
$n$
$x_1$ $y_1$
...
$x_n$ $y_n$
</pre>
<p>
入力はすべて整数で与えられる。<br>
1行目にフロロがいるマスの座標が空白区切りで与えられる。<br>
2行目に穴があるマスの座標が空白区切りで与えられる。<br>
3行目に氷の塊の数$n$が与えられる。<br>
続く$n$行目に氷の塊があるマスの座標が空白区切りで与えられる。
</p>
<h2>Constraints</h2>
<p>
入力は以下の条件を満たす。
</p>
<ul>
<li>$0 \leq n \leq 10^5$</li>
<li>$0 \leq sx$,$sy$,$tx$,$ty$,$x_i$,$y_i \leq 10^9$</li>
<li>与えられる座標はすべて異なる</li>
</ul>
<h2>Output</h2>
<p>
穴に移動するまでにぶつかる回数の最小値を出力せよ。<br>
辿り着けない場合は$-$1を出力せよ。
</p>
<h2>Sample Input 1</h2>
<pre>
0 0
13 33
2
0 34
14 0
</pre>
<h2>Sample Output 1</h2>
<pre>
0
</pre>
<h2>Sample Input 2</h2>
<pre>
0 1
5 0
4
2 1
1 3
4 2
3 0
</pre>
<h2>Sample Output 2</h2>
<pre>
4
</pre>
<h2>Sample Input 3</h2>
<pre>
0 0
0 2
1
0 1
</pre>
<h2>Sample Output 3</h2>
<pre>
-1
</pre>
|
p00559 |
<script type="text/x-mathjax-config">
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});
</script>
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML">
</script>
<h1>Foehn Phenomena</h1>
<p>
In the Kingdom of IOI, the wind always blows from sea to land. There are $N + 1$ spots numbered from $0$ to $N$. The wind from Spot $0$ to Spot $N$ in order. Mr. JOI has a house at Spot $N$. The altitude of Spot $0$ is $A_0 = 0$, and the altitude of Spot $i$ ($1 \leq i \leq N$) is $A_i$.
</p>
<p>
The wind blows on the surface of the ground. The temperature of the wind changes according to the change of the altitude. The temperature of the wind at Spot $0$, which is closest to the sea, is $0$ degree. For each $i$ ($0 \leq i \leq N - 1$), the change of the temperature of the wind from Spot $i$ to Spot $i + 1$ depends only on the values of $A_i$ and $A_{i+1}$ in the following way:
</p>
<ul>
<li>If $A_i < A_{i+1}$, the temperature of the wind decreases by $S$ degrees per altitude. </li>
<li>If $A_i \geq A_{i+1}$, the temperature of the wind increases by $T$ degrees per altitude. </li>
</ul>
<p>
The tectonic movement is active in the land of the Kingdom of IOI. You have the data of tectonic movements for $Q$ days. In the $j$-th ($1 \leq j \leq Q$) day, the change of the altitude of Spot $k$ for $L_j \leq k \leq R_j$ ($1 \leq L_j \leq R_j \leq N$) is described by $X_j$. If $X_j$ is not negative, the altitude increases by $X_j$. If $X_j$ is negative, the altitude decreases by $|X_j|$.
</p>
<p>
Your task is to calculate the temperature of the wind at the house of Mr. JOI after each tectonic movement.
</p>
<h3>Task</h3>
<p>
Given the data of tectonic movements, write a program which calculates, for each $j$ ($1 \leq j \leq Q$), the temperature of the wind at the house of Mr. JOI after the tectonic movement on the $j$-th day.
</p>
<h3>Input</h3>
<p>
Read the following data from the standard input.
</p>
<ul>
<li>The first line of input contains four space separated integers $N$, $Q$, $S$, $T$. This means there is a house of Mr. JOI at Spot $N$, there are $Q$ tectonic movements, the temperature of the wind decreases by $S$ degrees per altitude if the altitude increases, and the temperature of the wind increases by $T$ degrees per altitude if the altitude decreases.</li>
<li>The $i$-th line ($1 \leq i \leq N +1$) of the following $N +1$ lines contains an integer $A_{i-1}$, which is the initial altitude at Spot ($i - 1$) before tectonic movements.</li>
<li>The $j$-th line ($1 \leq j \leq Q$) of the following $Q$ lines contains three space separated integers $L_j$, $R_j$, $X_j$. This means, for the tectonic movement on the $j$-th day, the change of the altitude at the spots from $L_j$ to $R_j$ is described by $X_j$.</li>
</ul>
<h3>Output</h3>
<p>
Write $Q$ lines to the standard output. The $j$-th line ($1 \leq j \leq Q$) of output contains the temperature of the wind
at the house of Mr. JOI after the tectonic movement on the $j$-th day.
</p>
<h3>Constraints</h3>
<p>
All input data satisfy the following conditions.
</p>
<ul>
<li>$1 \leq N \leq 200 000.$</li>
<li>$1 \leq Q \leq 200 000.$</li>
<li>$1 \leq S \leq 1 000 000.$</li>
<li>$1 \leq T \leq 1 000 000.$</li>
<li>$A_0 = 0.$</li>
<li>$-1 000 000 \leq A_i \leq 1 000 000 (1 \leq i \leq N).$</li>
<li>$1 \leq L_j \leq R_j \leq N (1 \leq j \leq Q).$</li>
<li>$ -1 000 000 \leq X_j \leq 1 000 000 (1 \leq j \leq Q).$</li>
</ul>
<h3>Sample Input and Output</h3>
<h3>Sample Input 1</h3>
<pre>
3 5 1 2
0
4
1
8
1 2 2
1 1 -2
2 3 5
1 2 -1
1 3 5
</pre>
<h3>Sample Output 1</h3>
<pre>
-5
-7
-13
-13
-18
</pre>
<p>
Initially, the altitudes of the Spot 0, 1, 2, 3 are 0, 4, 1, 8, respectively. After the tectonic movement on the first day, the altitudes become 0, 6, 3, 8, respectively. At that moment, the temperatures of the wind are 0, -6, 0, -5,respectively.
</p>
<h3>Sample Input 2</h3>
<pre>
2 2 5 5
0
6
-1
1 1 4
1 2 8
</pre>
<h3>Sample Output 2</h3>
<pre>
5
-35
</pre>
<h3>Sample Input 3</h3>
<pre>
7 8 8 13
0
4
-9
4
-2
3
10
-9
1 4 8
3 5 -2
3 3 9
1 7 4
3 5 -1
5 6 3
4 4 9
6 7 -10
</pre>
<h3>Sample output 3</h3>
<pre>
277
277
322
290
290
290
290
370
</pre>
<br/>
<div class="source">
<p class="source">
<a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/"><img alt="Creatie Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-sa/4.0/80x15.png"/></a>
</p>
<p class="source">
<a href="https://www.ioi-jp.org/joi/2016/2017-ho/index.html">The 16th Japanese Olympiad in Informatics (JOI 2016/2017)
Final Round</a>
</p>
</div> |
p02564 | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a directed graph with <var>N</var> vertices and <var>M</var> edges, not necessarily simple. The <var>i</var>-th edge is oriented from the vertex <var>a_i</var> to the vertex <var>b_i</var>.
Divide this graph into strongly connected components and print them in their topological order.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 500,000</var></li>
<li><var>1 \leq M \leq 500,000</var></li>
<li><var>0 \leq a_i, b_i < 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>M</var>
<var>a_0</var> <var>b_0</var>
<var>a_1</var> <var>b_1</var>
:
<var>a_{M - 1}</var> <var>b_{M - 1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <var>1+K</var> lines, where <var>K</var> is the number of strongly connected components.
Print <var>K</var> on the first line.
Print the information of each strongly connected component in next <var>K</var> lines in the following format, where <var>l</var> is the number of vertices in the strongly connected component and <var>v_i</var> is the index of the vertex in it.</p>
<pre><var>l</var> <var>v_0</var> <var>v_1</var> ... <var>v_{l-1}</var>
</pre>
<p>Here, for each edge <var>(a_i, b_i)</var>, <var>b_i</var> should not appear in <strong>earlier</strong> line than <var>a_i</var>.</p>
<p>If there are multiple correct output, print any of them.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 7
1 4
5 2
3 0
5 5
4 1
0 3
4 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>4
1 5
2 4 1
1 2
2 3 0
</pre></section>
</div>
</span> |
p02071 | <style type="text/css">
blockquote {
font-family: Menlo, Monaco, "Courier New", monospace;
display: block;
margin: 10px 0 10px 30px;
font-size: 16px;
line-height: 18px;
white-space: pre;
white-space: pre-wrap;
word-break: break-all;
word-wrap: break-word;
}
table.ioexample {
width: 100%;
border-collapse: collapse;
}
table.ioexample td {
width: 50%;
border: 1px solid rgba(0, 0, 0, 0.15);
vertical-align: top;
padding: 5px;
}
.no-page-break {
page-break-inside: avoid;
}
.page-break {
page-break-before: always;
}
</style>
<h3>Problem Statement</h3>
<p>Your company is developing a video game. In this game, players can exchange items. This trading follows the rule set by the developers. The rule is defined as the following format: "Players can exchange one item $A_i$ and $x_i$ item $B_i$". Note that the trading can be done in both directions. Items are exchanged between players and the game system. Therefore players can exchange items any number of times.</p>
<p>Sometimes, testers find bugs that a repetition of a specific sequence of tradings causes the unlimited increment of items. For example, the following rule set can cause this bug.</p>
<ol>
<li>Players can exchange one item 1 and two item 2.</li>
<li>Players can exchange one item 2 and two item 3.</li>
<li>Players can exchange one item 1 and three item 3.</li>
</ol>
<p>In this rule set, players can increase items unlimitedly. For example, players start tradings with one item 1. Using rules 1 and 2, they can exchange it for four item 3. And, using rule 3, they can get one item 1 and one item 3. By repeating this sequence, the amount of item 3 increases unlimitedly.</p>
<p>These bugs can spoil the game, therefore the developers decided to introduce the system which prevents the inconsistent trading. Your task is to write a program which detects whether the rule set contains the bug or not.</p>
<hr />
<h3>Input</h3>
<p>The input consists of a single test case in the format below.</p>
<blockquote>$N$ $M$
$A_{1}$ $B_{1}$ $x_{1}$
$\vdots$
$A_{M}$ $B_{M}$ $x_{M}$</blockquote>
<p>The first line contains two integers $N$ and $M$ which are the number of types of items and the number of rules, respectively ($1 \le N \le 100 000$, $1 \le M \le 100 000$). Each of the following $M$ lines gives the trading rule that one item $A_{i}$ and $x_{i}$ item $B_{i}$ ($1 \le A_{i},B_{i} \le N$, $1 \le x_{i} \le 1 000 000 000$) can be exchanged in both directions. There are no exchange between same items, i.e., $A_{i} \ne B_{i}$.</p>
<h3>Output</h3>
<p>If there are no bugs, i.e., repetition of any sequence of tradings does not cause an unlimited increment of items, output "Yes". If not, output "No".</p>
<p><div class="no-page-break"><h3>Examples</h3><table class="ioexample"><tr><th>Input</th><th>Output</th></tr><tr><td><pre>4 4
1 2 2
2 3 2
3 4 2
4 2 3
</pre></td><td><pre>No
</pre></td></tr><tr><td><pre>4 3
1 2 7
2 3 5
4 1 2
</pre></td><td><pre>Yes
</pre></td></tr><tr><td><pre>4 4
1 2 101
2 3 99
1 4 100
4 3 100
</pre></td><td><pre>No
</pre></td></tr><tr><td><pre>5 6
3 1 4
2 3 4
5 4 15
2 1 16
2 4 20
5 3 3
</pre></td><td><pre>Yes
</pre></td></tr></table></div></p>
|
p02421 |
<H1>Card Game</H1><br>
<p>
Taro and Hanako are playing card games. They have <var>n</var> cards each, and they compete <var>n</var> turns. At each turn Taro and Hanako respectively puts out a card.
The name of the animal consisting of alphabetical letters is written on each card, and the bigger one in lexicographical order becomes the winner of that turn. The winner obtains 3 points. In the case of a draw, they obtain 1 point each.
</p>
<p>
Write a program which reads a sequence of cards Taro and Hanako have and reports the final scores of the game.
</p>
<H2>Input</H2>
<p>
In the first line, the number of cards <var>n</var> is given. In the following <var>n</var> lines, the cards for <var>n</var> turns are given respectively. For each line, the first string represents the Taro's card and the second one represents Hanako's card.
</p>
<h2>Constraints</h2>
<ul>
<li><var>n</var> ≤ 1000</li>
<li>The length of the string ≤ 100</li>
</ul>
<H2>Output</H2>
<p>
Print the final scores of Taro and Hanako respectively. Put a single space character between them.
</p>
<H2>Sample Input</H2>
<pre>
3
cat dog
fish fish
lion tiger
</pre>
<H2>Sample Output</H2>
<pre>
1 7
</pre> |
p02972 | <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> empty boxes arranged in a row from left to right.
The integer <var>i</var> is written on the <var>i</var>-th box from the left <var>(1 \leq i \leq N)</var>.</p>
<p>For each of these boxes, Snuke can choose either to put a ball in it or to put nothing in it.</p>
<p>We say a set of choices to put a ball or not in the boxes is good when the following condition is satisfied:</p>
<ul>
<li>For every integer <var>i</var> between <var>1</var> and <var>N</var> (inclusive), the total number of balls contained in the boxes with multiples of <var>i</var> written on them is congruent to <var>a_i</var> modulo <var>2</var>.</li>
</ul>
<p>Does there exist a good set of choices? If the answer is yes, find one good set of choices.</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 2 \times 10^5</var></li>
<li><var>a_i</var> is <var>0</var> or <var>1</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If a good set of choices does not exist, print <code>-1</code>.</p>
<p>If a good set of choices exists, print one such set of choices in the following format:</p>
<pre><var>M</var>
<var>b_1</var> <var>b_2</var> <var>...</var> <var>b_M</var>
</pre>
<p>where <var>M</var> denotes the number of boxes that will contain a ball, and <var>b_1,\ b_2,\ ...,\ b_M</var> are the integers written on these boxes, in any order.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
1 0 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
1
</pre>
<p>Consider putting a ball only in the box with <var>1</var> written on it.</p>
<ul>
<li>There are three boxes with multiples of <var>1</var> written on them: the boxes with <var>1</var>, <var>2</var>, and <var>3</var>. The total number of balls contained in these boxes is <var>1</var>.</li>
<li>There is only one box with a multiple of <var>2</var> written on it: the box with <var>2</var>. The total number of balls contained in these boxes is <var>0</var>.</li>
<li>There is only one box with a multiple of <var>3</var> written on it: the box with <var>3</var>. The total number of balls contained in these boxes is <var>0</var>.</li>
</ul>
<p>Thus, the condition is satisfied, so this set of choices is good.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5
0 0 0 0 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Putting nothing in the boxes can be a good set of choices.</p></section>
</div>
</span> |
p03660 | <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Fennec and Snuke are playing a board game.</p>
<p>On the board, there are <var>N</var> cells numbered <var>1</var> through <var>N</var>, and <var>N-1</var> roads, each connecting two cells. Cell <var>a_i</var> is adjacent to Cell <var>b_i</var> through the <var>i</var>-th road. Every cell can be reached from every other cell by repeatedly traveling to an adjacent cell. In terms of graph theory, the graph formed by the cells and the roads is a tree.</p>
<p>Initially, Cell <var>1</var> is painted black, and Cell <var>N</var> is painted white. The other cells are not yet colored.
Fennec (who goes first) and Snuke (who goes second) alternately paint an uncolored cell.
More specifically, each player performs the following action in her/his turn:</p>
<ul>
<li>Fennec: selects an uncolored cell that is adjacent to a <strong>black</strong> cell, and paints it <strong>black</strong>.</li>
<li>Snuke: selects an uncolored cell that is adjacent to a <strong>white</strong> cell, and paints it <strong>white</strong>.</li>
</ul>
<p>A player loses when she/he cannot paint a cell. Determine the winner of the game when Fennec and Snuke play optimally.</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 a_i, b_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>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>If Fennec wins, print <code>Fennec</code>; if Snuke wins, print <code>Snuke</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>7
3 6
1 2
3 1
7 4
5 7
1 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Fennec
</pre>
<p>For example, if Fennec first paints Cell <var>2</var> black, she will win regardless of Snuke's moves.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4
1 4
4 2
2 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>Snuke
</pre></section>
</div>
</span> |
p03230 | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given an integer <var>N</var>. Determine if there exists a tuple of subsets of <var>\{1,2,...N\}</var>, <var>(S_1,S_2,...,S_k)</var>, that satisfies the following conditions:</p>
<ul>
<li>Each of the integers <var>1,2,...,N</var> is contained in exactly two of the sets <var>S_1,S_2,...,S_k</var>.</li>
<li>Any two of the sets <var>S_1,S_2,...,S_k</var> have exactly one element in common.</li>
</ul>
<p>If such a tuple exists, construct one such tuple.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 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>If a tuple of subsets of <var>\{1,2,...N\}</var> that satisfies the conditions does not exist, print <code>No</code>.
If such a tuple exists, print <code>Yes</code> first, then print such subsets in the following format:</p>
<pre><var>k</var>
<var>|S_1|</var> <var>S_{1,1}</var> <var>S_{1,2}</var> <var>...</var> <var>S_{1,|S_1|}</var>
<var>:</var>
<var>|S_k|</var> <var>S_{k,1}</var> <var>S_{k,2}</var> <var>...</var> <var>S_{k,|S_k|}</var>
</pre>
<p>where <var>S_i={S_{i,1},S_{i,2},...,S_{i,|S_i|}}</var>.</p>
<p>If there are multiple such tuples, any of them will be accepted.</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>Yes
3
2 1 2
2 3 1
2 2 3
</pre>
<p>It can be seen that <var>(S_1,S_2,S_3)=(\{1,2\},\{3,1\},\{2,3\})</var> satisfies the conditions.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>No
</pre></section>
</div>
</span> |
p02708 | <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have <var>N+1</var> integers: <var>10^{100}</var>, <var>10^{100}+1</var>, ..., <var>10^{100}+N</var>.</p>
<p>We will choose <var>K</var> or more of these integers. Find the number of possible values of the sum of the chosen numbers, modulo <var>(10^9+7)</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 2\times 10^5</var></li>
<li><var>1 \leq K \leq N+1</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of possible values of the sum, modulo <var>(10^9+7)</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>10
</pre>
<p>The sum can take <var>10</var> values, as follows:</p>
<ul>
<li><var>(10^{100})+(10^{100}+1)=2\times 10^{100}+1</var></li>
<li><var>(10^{100})+(10^{100}+2)=2\times 10^{100}+2</var></li>
<li><var>(10^{100})+(10^{100}+3)=(10^{100}+1)+(10^{100}+2)=2\times 10^{100}+3</var></li>
<li><var>(10^{100}+1)+(10^{100}+3)=2\times 10^{100}+4</var></li>
<li><var>(10^{100}+2)+(10^{100}+3)=2\times 10^{100}+5</var></li>
<li><var>(10^{100})+(10^{100}+1)+(10^{100}+2)=3\times 10^{100}+3</var></li>
<li><var>(10^{100})+(10^{100}+1)+(10^{100}+3)=3\times 10^{100}+4</var></li>
<li><var>(10^{100})+(10^{100}+2)+(10^{100}+3)=3\times 10^{100}+5</var></li>
<li><var>(10^{100}+1)+(10^{100}+2)+(10^{100}+3)=3\times 10^{100}+6</var></li>
<li><var>(10^{100})+(10^{100}+1)+(10^{100}+2)+(10^{100}+3)=4\times 10^{100}+6</var></li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>200000 200001
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1
</pre>
<p>We must choose all of the integers, so the sum can take just <var>1</var> value.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>141421 35623
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>220280457
</pre></section>
</div>
</span> |
p00735 |
<h1><font color="#000000">Problem B: </font>Monday-Saturday Prime Factors</h1>
<!-- end en only -->
<p>
Chief Judge's log, stardate 48642.5.
We have decided to make a problem from elementary number theory.
The problem looks like finding all prime factors of a positive integer,
but it is not.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
A positive integer whose remainder divided by 7 is either 1 or 6 is called
a 7<b><i>N</i></b>+{1,6} number.
But as it is hard to pronounce,
we shall call it a <em>Monday-Saturday number</em>.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
For Monday-Saturday numbers <I>a</I> and <I>b</I>,
we say <I>a</I> is a Monday-Saturday divisor of <I>b</I>
if there exists a Monday-Saturday number <I>x</I>
such that <I>a</I><I>x</I> = <I>b</I>.
It is easy to show that
for any Monday-Saturday numbers <I>a</I> and <I>b</I>,
it holds that <I>a</I> is
a Monday-Saturday divisor of <I>b</I>
if and only if
<I>a</I> is a divisor of <I>b</I> in the usual sense.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
We call a Monday-Saturday number a <em>Monday-Saturday prime</em>
if it is greater than 1 and has no Monday-Saturday divisors
other than itself and 1.
A Monday-Saturday number which is a prime in the usual sense
is a Monday-Saturday prime
but the converse does not always hold.
For example, 27 is a Monday-Saturday prime
although it is not a prime in the usual sense.
We call a Monday-Saturday prime
which is a Monday-Saturday divisor of a Monday-Saturday number <I>a</I>
a <em>Monday-Saturday prime factor</em> of <I>a</I>.
For example, 27 is one of the Monday-Saturday prime factors of 216,
since 27 is a Monday-Saturday prime
and 216 = 27 × 8 holds.
</p>
<!-- end en only -->
<p>
<!-- begin en only -->
Any Monday-Saturday number greater than 1
can be expressed as a product of one or more Monday-Saturday primes.
The expression is not always unique
even if differences in order are ignored.
For example,
<!-- end en only -->
216 = 6 × 6 × 6 = 8 × 27
<!-- begin en only -->
holds.
<!-- end en only -->
</p>
<!-- begin en only -->
<p>
Our contestants should write a program that outputs
all Monday-Saturday prime factors
of each input Monday-Saturday number.
</p>
<!-- end en only -->
<h3>Input</h3>
<!-- begin en only -->
<p>
The input is a sequence of lines each of which contains a single
Monday-Saturday number.
Each Monday-Saturday number is greater than 1
and less than 300000 (three hundred thousand).
The end of the input is indicated by a line
containing a single digit 1.
</p>
<!-- end en only -->
<h3>Output</h3>
<!-- begin en only -->
<p>
For each input Monday-Saturday number,
it should be printed, followed by a colon `:'
and the list of its Monday-Saturday prime factors on a single line.
Monday-Saturday prime factors should be listed in ascending order
and each should be preceded by a space.
All the Monday-Saturday prime factors should be printed only once
even if they divide the input Monday-Saturday number more than once.
</p>
<!-- end en only -->
<h3>Sample Input</h3>
<pre>
205920
262144
262200
279936
299998
1
</pre>
<h3>Output for the Sample Input</h3>
<pre>
205920: 6 8 13 15 20 22 55 99
262144: 8
262200: 6 8 15 20 50 57 69 76 92 190 230 475 575 874 2185
279936: 6 8 27
299998: 299998
</pre>
|
p01827 |
<script type="text/x-mathjax-config">
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});
</script>
<script type='text/javascript' src='http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>
</script>
<h2>Problem J:
Black Company
</h2>
<p>
JAG Company is a sweatshop (sweatshop is called "burakku kigyo" in Japanese), and you are
the CEO for the company.
</p>
<p>
You are planning to determine $N$ employees' salary as low as possible (employees are numbered
from 1 to $N$). Each employee's salary amount must be a positive integer greater than zero. At
that time, you should pay attention to the employees' contribution degree. If the employee $i$'s
contribution degree $c_i$ is greater than the employee $j$'s contribution degree $c_j$ , the employee i
must get higher salary than the employee $j$'s salary. If the condition is not satisfied, employees
may complain about their salary amount.
</p>
<p>
However, it is not necessarily satisfied in all pairs of the employees, because each employee can
only know his/her close employees' contribution degree and salary amount. Therefore, as long as
the following two conditions are satisfied, employees do not complain to you about their salary
amount.
</p>
<ul>
<li> If the employees $i$ and $j$ are close to each other, $c_i < c_j \Leftrightarrow p_i < p_j$ must be satisfied, where
$p_i$ is the employee $i$'s salary amount.</li>
<li> If the employee $i$ is close to the employees $j$ and $k$, $c_j < c_k \Leftrightarrow p_j < p_k$ must be satisfied.
Write a program that computes the minimum sum of all employees' salary amount such that no
employee complains about their salary.</li>
</ul>
<h3>Input</h3>
<p>
Each input is formatted as follows:<br>
<br>
$N$<br>
$c_1$ ... $c_N$<br>
$M$<br>
$a_1$ $b_1$<br>
...<br>
$a_M$ $b_M$<br>
<br>
</p>
<p>
The first line contains an integer $N$ ($1 \leq N \leq 100,000$), which indicates the number of employees.
The second line contains $N$ integers $c_i$ ($1 \leq c_i \leq 100,000$) representing the contribution degree
of employee $i$.
</p>
<p>
The third line contains an integer $M$ ($0 \leq M \leq 200,000$), which specifies the number of relationships. Each of the following $M$ lines contains two integers $a_i$ and $b_i$ ($a_i \ne b_i, 1 \leq a_i, b_i \leq N$).
It represents that the employees $a_i$ and $b_i$ are close to each other. There is no more than one
relationship between each pair of the employees.
</p>
<h3>Output</h3>
<p>
Print the minimum sum of all employees' salary amounts in a line.
</p>
<h3>Sample Input</h3>
<pre>
3
1 3 3
2
1 2
1 3
</pre>
<h3>Output for the Sample Input</h3>
<pre>
5
</pre>
<h3>Sample Input</h3>
<pre>
3
1 2 3
2
1 2
1 3
</pre>
<h3>Output for the Sample Input</h3>
<pre>
6
</pre>
<h3>Sample Input</h3>
<pre>
4
1 1 2 2
2
1 2
3 4
</pre>
<h3>Output for the Sample Input</h3>
<pre>
4
</pre>
<h3>Sample Input</h3>
<pre>
5
1 2 5 5 1
6
1 2
4 1
2 3
5 2
4 3
4 5
</pre>
<h3>Output for the Sample Input</h3>
<pre>
10
</pre>
<h3>Sample Input</h3>
<pre>
6
4 3 2 1 5 3
7
4 2
1 5
2 6
6 5
4 1
1 6
6 3
</pre>
<h3>Output for the Sample Input</h3>
<pre>
13
</pre>
|
p02358 |
<script type="text/x-mathjax-config">
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});
</script>
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML">
</script>
<H1>Union of Rectangles</H1>
<p>
Given a set of $N$ axis-aligned rectangles in the plane, find the area of regions which are covered by at least one rectangle.
</p>
<h2>Input</h2>
<p>
The input is given in the following format.
</p>
<p>
$N$<br>
$x1_1$ $y1_1$ $x2_1$ $y2_1$<br>
$x1_2$ $y1_2$ $x2_2$ $y2_2$<br>
:<br>
$x1_N$ $y1_N$ $x2_N$ $y2_N$<br>
</p>
<p>
($x1_i, y1_i$) and ($x2_i, y2_i$) are the coordinates of the top-left corner and the bottom-right corner of the $i$-th rectangle respectively.
</p>
<h2>Constraints</h2>
<ul>
<li>$ 1 \leq N \leq 2000 $</li>
<li>$ −10^9 \leq x1_i < x2_i\leq 10^9 $</li>
<li>$ −10^9 \leq y1_i < y2_i\leq 10^9 $</li>
</ul>
<h2>Output</h2>
<p>
Print the area of the regions.
</p>
<h2>Sample Input 1</h2>
<pre>
2
0 0 3 4
1 2 4 3
</pre>
<h2>Sample Output 1</h2>
<pre>
13
</pre>
<h2>Sample Input 2</h2>
<pre>
3
1 1 2 5
2 1 5 2
1 2 2 5
</pre>
<h2>Sample Output 2</h2>
<pre>
7
</pre>
<h2>Sample Input 3</h2>
<pre>
4
0 0 3 1
0 0 1 3
0 2 3 3
2 0 3 3
</pre>
<h2>Sample Output 3</h2>
<pre>
8
</pre>
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.