question_id stringlengths 6 6 | content stringlengths 1 27.2k |
|---|---|
p02654 | <span class="lang-en">
<p>Score : <var>1200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have <var>N</var> lamps numbered <var>1</var> to <var>N</var>, and <var>N</var> buttons numbered <var>1</var> to <var>N</var>.
Initially, Lamp <var>1, 2, \cdots, A</var> are on, and the other lamps are off.</p>
<p>Snuke and Ringo will play the following game.</p>
<ul>
<li>
<p>First, Ringo generates a permutation <var>(p_1,p_2,\cdots,p_N)</var> of <var>(1,2,\cdots,N)</var>.
The permutation is chosen from all <var>N!</var> possible permutations with equal probability, without being informed to Snuke.</p>
</li>
<li>
<p>Then, Snuke does the following operation any number of times he likes:</p>
<ul>
<li>Choose a lamp that is on at the moment. (The operation cannot be done if there is no such lamp.)
Let Lamp <var>i</var> be the chosen lamp.
Press Button <var>i</var>, which switches the state of Lamp <var>p_i</var>. That is, Lamp <var>p_i</var> will be turned off if it is on, and vice versa.</li>
</ul>
</li>
</ul>
<p>At every moment, Snuke knows which lamps are on.
Snuke wins if all the lamps are on, and he will surrender when it turns out that he cannot win.
What is the probability of winning when Snuke plays optimally?</p>
<p>Let <var>w</var> be the probability of winning. Then, <var>w \times N!</var> will be an integer.
Compute <var>w \times 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 10^7</var></li>
<li><var>1 \leq A \leq \min(N-1,5000)</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</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <var>w \times N!</var> modulo <var>(10^9+7)</var>, where <var>w</var> is the probability of Snuke's winning.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>First, Snuke will press Button <var>1</var>.
If Lamp <var>1</var> turns off, he loses.
Otherwise, he will press the button that he can now press.
If the remaining lamp turns on, he wins; if Lamp <var>1</var> turns off, he loses.
The probability of winning in this game is <var>1/3</var>, so we should print <var>(1/3)\times 3!=2</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 2
</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>8 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>16776
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>9999999 4999
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>90395416
</pre></section>
</div>
</span> |
p00669 |
<h1>Problem A: K Cards</h1>
<p>
ある日、先生は次のようなゲームを思いついた。<br>
ゲームは 1 から 10 までの数がひとつ書かれたカードを n 枚使用し、以下のように進む。<br>
</p>
<ol>
<li>先生が n 枚のカードを数が見えるようにして横一列に黒板に貼り付け、ある整数 k (k ≥ 1) を生徒に宣言する。横一列に並べられた n 枚のカードについて、連続した k 枚のカードの積の最大値を C<sub>k</sub> とする。また、先生が並べた時点での C<sub>k</sub> を C<sub>k</sub>' とおく。</li>
<li>生徒は 1. で貼られたカードの列を見て C<sub>k</sub> を大きくすることを考える。ある 2 枚を入れ替えることで C<sub>k</sub> をより大きくすることができた場合、生徒の成績は C<sub>k</sub> - C<sub>k</sub>' 点上がる。誰かが成績点を得たらゲームを終了する。</li>
</ol>
<p>
あなたの仕事は先生が並べたカードの列を入力し、生徒が得られる最大の成績点を出力するプログラムを書くことである。ただし、そこからどの 2 枚を選んで交換してもC<sub>k</sub>を下げることしかできない (C<sub>k</sub> - C<sub>k</sub>' < 0) 場合、文字列 "NO GAME" (引用符を含まない)を出力せよ。<br>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE_1084"><br>
先生が並べたカードが7, 2, 3, 5の場合。このとき 7 と 3 を交換することで生徒は最大で 35 - 15 = 20 成績点を得る。
</p>
<h2>Input</h2>
<p>
入力は複数のテストケースからなる。ひとつのテストケースは以下の形式に従う。
</p>
<pre>
n k
c<sub>1</sub>
c<sub>2</sub>
c<sub>3</sub>
…
c<sub>n</sub>
</pre>
<p>
n は先生が並べるカードの枚数、k は宣言する整数である。
また c<sub>i</sub> (1 ≤ i ≤ n) はカードに書かれた数を示す。また、この順で先生が横に黒板に貼り付けるとする。入力の終わりは、ふたつの0が一文字の空白で区切られる一行で示される。
</p>
<h2>Constraints</h2>
<ul>
<li>入力はすべて整数</li>
<li>2 ≤n ≤ 100</li>
<li>1 ≤k ≤ 5</li>
<li>k ≤ n</li>
<li>1 ≤ c<sub>i</sub> ≤ 10 (1 ≤ i ≤ n)</li>
<li>テストケースの数は 100 を超えない。</li>
</ul>
<h2>Output</h2>
<p>
生徒が得られる成績点の最大値あるいは文字列 "NO GAME" (引用符を含まない)を各テストケースに付き 1 行で出力せよ。
</p>
<h2>Sample Input</h2>
<pre>
4 2
2
3
7
5
0 0
</pre>
<h2>Sample Output</h2>
<pre>
0
</pre>
<h2>Hint</h2>
<p>
サンプルにおいて C<sub>2</sub>' = 35 であり、ここからどの 2 枚を並び替えても C<sub>2</sub> の最大値は 35 より大きくならない。したがって生徒が得られる成績点は最大 0 点である。
</p> |
p03946 | <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> towns located in a line, conveniently numbered <var>1</var> through <var>N</var>. Takahashi the merchant is going on a travel from town <var>1</var> to town <var>N</var>, buying and selling apples.</p>
<p>Takahashi will begin the travel at town <var>1</var>, with no apple in his possession. The actions that can be performed during the travel are as follows:</p>
<ul>
<li><em>Move</em>: When at town <var>i</var> (<var>i < N</var>), move to town <var>i + 1</var>.</li>
<li><em>Merchandise</em>: Buy or sell an arbitrary number of apples at the current town. Here, it is assumed that one apple can always be bought and sold for <var>A_i</var> yen (the currency of Japan) at town <var>i</var> (<var>1 ≦ i ≦ N</var>), where <var>A_i</var> are distinct integers. Also, you can assume that he has an infinite supply of money.</li>
</ul>
<p>For some reason, there is a constraint on merchandising apple during the travel: the sum of the number of apples bought and the number of apples sold during the whole travel, must be at most <var>T</var>. (Note that a single apple can be counted in both.)</p>
<p>During the travel, Takahashi will perform actions so that the <em>profit</em> of the travel is maximized. Here, the profit of the travel is the amount of money that is gained by selling apples, minus the amount of money that is spent on buying apples. Note that we are not interested in apples in his possession at the end of the travel.</p>
<p>Aoki, a business rival of Takahashi, wants to trouble Takahashi by manipulating the market price of apples. Prior to the beginning of Takahashi's travel, Aoki can change <var>A_i</var> into another arbitrary non-negative integer <var>A_i'</var> for any town <var>i</var>, any number of times. The cost of performing this operation is <var>|A_i - A_i'|</var>. After performing this operation, different towns may have equal values of <var>A_i</var>.</p>
<p>Aoki's objective is to decrease Takahashi's expected profit by at least <var>1</var> yen. Find the minimum total cost to achieve it. You may assume that Takahashi's expected profit is initially at least <var>1</var> yen.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≦ N ≦ 10^5</var></li>
<li><var>1 ≦ A_i ≦ 10^9</var> (<var>1 ≦ i ≦ N</var>)</li>
<li><var>A_i</var> are distinct.</li>
<li><var>2 ≦ T ≦ 10^9</var></li>
<li>In the initial state, Takahashi's expected profit is at least <var>1</var> yen.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>T</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 total cost to decrease Takahashi's expected profit by at least <var>1</var> yen.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 2
100 50 200
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
</pre>
<p>In the initial state, Takahashi can achieve the maximum profit of <var>150</var> yen as follows:</p>
<ol>
<li>Move from town <var>1</var> to town <var>2</var>.</li>
<li>Buy one apple for <var>50</var> yen at town <var>2</var>.</li>
<li>Move from town <var>2</var> to town <var>3</var>.</li>
<li>Sell one apple for <var>200</var> yen at town <var>3</var>.</li>
</ol>
<p>If, for example, Aoki changes the price of an apple at town <var>2</var> from <var>50</var> yen to <var>51</var> yen, Takahashi will not be able to achieve the profit of <var>150</var> yen. The cost of performing this operation is <var>1</var>, thus the answer is <var>1</var>.</p>
<p>There are other ways to decrease Takahashi's expected profit, such as changing the price of an apple at town <var>3</var> from <var>200</var> yen to <var>199</var> yen.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5 8
50 30 40 10 20
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>2
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>10 100
7 10 4 5 9 3 6 8 2 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>2
</pre></section>
</div>
</span> |
p00393 | <h1>Beautiful Sequence</h1>
<p>
Alice is spending his time on an independent study to apply to the Nationwide Mathematics Contest. This year’s theme is "Beautiful Sequence." As Alice is interested in the working of computers, she wants to create a beautiful sequence using only 0 and 1. She defines a "Beautiful" sequence of length $N$ that consists only of 0 and 1 if it includes $M$ successive array of 1s as its sub-sequence.
</p>
<p>
Using his skills in programming, Alice decided to calculate how many "Beautiful sequences" she can generate and compile a report on it.
</p>
<p>
Make a program to evaluate the possible number of "Beautiful sequences" given the sequence length $N$ and sub-sequence length $M$ that consists solely of 1. As the answer can be extremely large, divide it by $1,000,000,007 (= 10^9 + 7)$ and output the remainder.
</p>
<h2>Input</h2>
<p>
The input is given in the following format.
</p>
<pre>
$N$ $M$
</pre>
<p>
The input line provides the length of sequence $N$ ($1 \leq N \leq 10^5$) and the length $M$ ($1 \leq M \leq N$) of the array that solely consists of 1s.
</p>
<h2>Output</h2>
<p>
Output the number of Beautiful sequences in a line.
</p>
<h2>Sample Input 1</h2>
<pre>
4 3
</pre>
<h2>Sample Output 1</h2>
<pre>
3
</pre>
<p>
The sequences with length 4 that include 1s in successive array of length 3 are: 0111, 1110 and 1111.
</p>
<h2>Sample Input 2</h2>
<pre>
4 2
</pre>
<h2>Sample Output 2</h2>
<pre>
8
</pre>
<p>
The sequences with length 4 that include 1s in successive array of length 2 are: 0011, 0110, 0111, 1011, 1100, 1101, 1110 and 1111.
</p>
|
p02204 | <h2>コンテストTシャツ (Contest T-shirts)</h2>
<p>Segtree 君は、 $M$ 枚のコンテストTシャツを持っています。</p>
<p>彼は今から $N$ 日間、コンテストTシャツだけで過ごそうと考え、$i = 1, 2, 3, \dots, N$ に対して「 $i$ 日目に $A_i$ 枚目のTシャツを着る」という $N$ 個の計画を立てました。</p>
<p>しかし、今の計画のままだと洗濯が間に合わない可能性があるので、必要に応じて計画を変更し、2日連続で同じ服を着ないようにしたいです。</p>
<p>変更する必要のある計画の個数の最小値を求めてください。なお、与えられた制約の元で、計画の変更によって必ず条件を満たすようにできることが証明できます。</p>
<h3>入力</h3>
<p>入力は以下の形式で標準入力から与えられる。</p>
<pre>
$M$ $N$
$A_1$ $A_2$ $\ldots$ $A_N$
</pre>
<h3>出力</h3>
<p>変更する必要のある計画の個数の最小値を出力してください。</p>
<p>ただし、最後には改行を入れること。</p>
<h3>制約</h3>
<ul>
<li>$2 \leq M \leq 10^9$</li>
<li>$1 \leq N \leq 10^5$</li>
<li>$1 \leq A_i \leq M$</li>
<li>入力は全て整数である。</li>
</ul>
<h3>入力例1</h3>
<pre>
2 3
2 2 1
</pre>
<h3>出力例1</h3>
<pre>
1
</pre>
<h3>入力例2</h3>
<pre>
3 6
1 1 1 2 2 3
</pre>
<h3>出力例2</h3>
<pre>
2
</pre>
|
p00239 |
<H1>カロリー計算</H1>
<!--
<p>
天高く馬肥ゆる秋。そろそろ食べ物が美味しくなる季節です。高校生のよしお君
も、テレビや読書のお供についついお菓子に手が伸びてしまいます。そのせいか最近体重は増える一方で、健康的にもよろしくありません。しかしどうしても直ぐに間食がやめられないよしお君は、お菓子の栄養素やカロリーが制限された場合に、食べたいお菓子を、食べてもよいお菓子と食べてはいけないお菓子に分類することにしました。
</p>
-->
<p>
食べ物には3大栄養素と呼ばれる「たんぱく質」「脂質」「炭水化物」の3つの栄養素が含まれています。1g(グラム)あたりたんぱく質と炭水化物は 4 kcal(キロカロリー)、脂質は 9 kcal と計算されます。たとえば、下の表によると番号1のケーキにはタンパク質 7 g、脂質 14 g、炭水化物 47 g が含まれています。これをもとに含まれるカロリーを計算すると、4 × 7 + 9 × 14 + 4 × 47 = 342 kcal となります。その他も同様に計算されています。
</p>
<center>
<table style="border: 1px #000 solid" border="1" cellpadding="3" cellspacing="0">
<tr>
<td width="50" style="border: 1px #000 solid">番号</td><td width="160" style="border: 1px #000 solid">名前</td><td style="border: 1px #000 solid">たんぱく質(g)</td><td style="border: 1px #000 solid">脂質(g)</td><td style="border: 1px #000 solid">炭水化物(g)</td><td style="border: 1px #000 solid">カロリー(kcal)</td>
</tr>
<tr>
<td style="border: 1px #000 solid">1</td><td style="border: 1px #000 solid">ケーキ</td><td style="border: 1px #000 solid">7</td><td style="border: 1px #000 solid">14</td><td style="border: 1px #000 solid">47</td><td style="border: 1px #000 solid">342</td>
</tr>
<tr>
<td style="border: 1px #000 solid">2</td><td style="border: 1px #000 solid">ポテトチップス</td><td style="border: 1px #000 solid">5</td><td style="border: 1px #000 solid">35</td><td style="border: 1px #000 solid">55</td><td style="border: 1px #000 solid">555</td>
</tr>
<tr>
<td style="border: 1px #000 solid">3</td><td style="border: 1px #000 solid">どら焼き</td><td style="border: 1px #000 solid">6</td><td style="border: 1px #000 solid">3</td><td style="border: 1px #000 solid">59</td><td style="border: 1px #000 solid">287</td>
</tr>
<tr>
<td style="border: 1px #000 solid">4</td><td style="border: 1px #000 solid">プリン</td><td style="border: 1px #000 solid">6</td><td style="border: 1px #000 solid">5</td><td style="border: 1px #000 solid">15</td><td style="border: 1px #000 solid">129</td>
</tr>
</table>
</center>
<br>
<p>
分類の対象となるお菓子の個数 <var>n</var> と各お菓子の情報、制限の情報を入力とし、そのお菓子一つだけなら制限を超えることがない(食べてもよい)お菓子の一覧を出力するプログラムを作成してください。
</p>
<p>
お菓子の情報は、お菓子の番号 <var>s</var>、そのお菓子に含まれるたんぱく質の重量 <var>p</var>、脂質の重量 <var>q</var>、炭水化物の重量 <var>r</var> から構成されています。制限の情報には、含むことができる最大のたんぱく質の重量 <var>P</var>、脂質の重量 <var>Q</var>、炭水化物の重量 <var>R</var>、および摂取することができる最大のカロリー <var>C</var> からなっており、タンパク質、脂質、炭水化物、カロリーのいずれかの一つでも超えると制限違反となり、「食べてはいけないお菓子」と判断されます。
</p>
<p>
食べてもよいお菓子の一覧は、食べてもよいお菓子の番号を入力順に出力してください。なお、食べてもよいお菓子がなければ”NA”と出力してください。
上の表にある4個のお菓子について、<var>P</var> = 10, <var>Q</var> = 15, <var>R</var> = 50, <var>C</var> = 400 という制限の場合、ケーキとプリンはそれぞれの栄養素及びカロリーが制限値以下なので食べてもよいお菓子に分類されますが、ポテトチップスは炭水化物とカロリー、どら焼きは炭水化物の量が制限値を超えているため食べてはいけないお菓子に分類されます。
</p>
<h2>入力</h2>
<p>
複数のデータセットの並びが与えられます。入力の終わりはゼロひとつの行で示されます。各データセットは以下の形式で与えられます。
</p>
<pre>
<var>n</var>
<var>s<sub>1</sub></var> <var>p<sub>1</sub></var> <var>q<sub>1</sub></var> <var>r<sub>1</sub></var>
<var>s<sub>2</sub></var> <var>p<sub>2</sub></var> <var>q<sub>2</sub></var> <var>r<sub>2</sub></var>
:
<var>s<sub>n</sub></var> <var>p<sub>n</sub></var> <var>q<sub>n</sub></var> <var>r<sub>n</sub></var>
<var>P</var> <var>Q</var> <var>R</var> <var>C</var>
</pre>
<p>
1行目にお菓子の数 <var>n</var> (1 ≤ <var>n</var> ≤ 1000)が与えられます。続く <var>n</var> 行に第 <var>i</var> のお菓子の番号 <var>s<sub>i</sub></var> (1 ≤ <var>s<sub>i</sub></var> ≤ 1000)、各栄養素の重量を表す整数 <var>p<sub>i</sub></var>, <var>q<sub>i</sub></var>, <var>r<sub>i</sub></var> (0 ≤ <var>p<sub>i</sub></var>, <var>q<sub>i</sub></var>, <var>r<sub>i</sub></var> ≤ 100) が与えられます。
</p>
<p>
続く行に各栄養素及びカロリーの制限値を表す整数 <var>P</var>, <var>Q</var>, <var>R</var> (0 ≤ <var>P</var>, <var>Q</var>, <var>R</var> ≤ 100), <var>C</var> (0 ≤ <var>C</var> ≤ 1700) が与えられます。
</p>
<p>
データセットの数は 100 を超えません。
</p>
<h2>出力</h2>
<p>
データセットごとに、食べてもよいお菓子の番号または ”NA” を出力します。
</p>
<h2>入力例</h2>
<pre>
4
1 7 14 47
2 5 35 55
3 6 3 59
4 6 5 15
10 15 50 400
2
1 8 10 78
2 4 18 33
10 10 50 300
0
</pre>
<h2>出力例</h2>
<pre>
1
4
NA
</pre> |
p01078 |
<h1>Problem G: Star</h1>
<h2>Problem</h2>
<p>
半径1の円に内接する正<var>N</var>/<var>K</var>角形の面積を求めよ。
</p>
<p>
ただし、正<var>N</var>/<var>K</var>角形を
「円周上に等間隔に<var>N</var>個の点を取り、
<var>K</var>-1個おきにそれぞれの点を結んだ一番外側の図形」
と定義する。
</p>
<p>
例えば、5/2角形は次のように描くことができる。
まず、半径1の円周上に等間隔に5つの点を取る。<br /><br/>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE3_ACPC2016Day2AIZU_G_image01" alt="Sample Input 2の図" style="width:500px;"><br />
</p>
<p>
次に、それぞれの点を2-1=1つおきに結ぶ。<br /><br/>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE3_ACPC2016Day2AIZU_G_image03" alt="Sample Input 2の図" style="width:500px;"><br />
</p>
<p>
一番外側の図形が正5/2角形になる。<br /><br/>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE3_ACPC2016Day2AIZU_G_image00" alt="Sample Input 2の図" style="width:500px;"><br />
</p>
<h2>Input</h2>
<p>
入力は以下の形式で与えられる。
</p>
<pre>
<var>N</var> <var>K</var>
</pre>
<p>
2つの整数 <var>N</var>, <var>K</var> が1行に与えられる。
</p>
<h2>Constraints</h2>
<p>
入力は以下の制約を満たす。
</p>
<ul>
<li>5 ≤ <var>N</var> ≤ 10<sup>6</sup></li>
<li>1 < <var>K</var> < <var>N</var>/2 </li>
<li> <var>N</var>,<var>K</var>は互いに素である整数</li>
</ul>
<h2>Output</h2>
<p>
半径1の円に内接する正<var>N</var>/<var>K</var>角形の面積を1行に出力せよ。10<sup>-5</sup>以下の誤差が許容される。
</p>
<h2>Sample Input 1</h2>
<pre>
5 2
</pre>
<h2>Sample Output 1</h2>
<pre>
1.12256994
</pre>
<p>
正5/2角形は上記の図形である。
</p>
<h2>Sample Input 2</h2>
<pre>
20 3
</pre>
<h2>Sample Output 2</h2>
<pre>
2.93114293
</pre>
<p>
正20/3角形は下のような図形である。<br /><br/>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE3_ACPC2016Day2AIZU_G_image02" alt="Sample Input 2の図" style="width:500px;"><br />
</p>
<h2>Sample Input 3</h2>
<pre>
7 3
</pre>
<h2>Sample Output 3</h2>
<pre>
1.08395920
</pre>
<h2>Sample Input 4</h2>
<pre>
100000 3
</pre>
<h2>Sample Output 4</h2>
<pre>
3.14159265
</pre> |
p03045 | <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 placed face down in a row. On each card, an integer <var>1</var> or <var>2</var> is written.</p>
<p>Let <var>A_i</var> be the integer written on the <var>i</var>-th card.</p>
<p>Your objective is to guess <var>A_1, A_2, ..., A_N</var> correctly.</p>
<p>You know the following facts:</p>
<ul>
<li>For each <var>i = 1, 2, ..., M</var>, the value <var>A_{X_i} + A_{Y_i} + Z_i</var> is an even number.</li>
</ul>
<p>You are a magician and can use the following magic any number of times:</p>
<p><strong>Magic</strong>: Choose one card and know the integer <var>A_i</var> written on it. The cost of using this magic is <var>1</var>.</p>
<p>What is the minimum cost required to determine all of <var>A_1, A_2, ..., A_N</var>?</p>
<p>It is guaranteed that there is no contradiction in given input.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>All values in input are integers.</li>
<li><var>2 \leq N \leq 10^5</var></li>
<li><var>1 \leq M \leq 10^5</var></li>
<li><var>1 \leq X_i < Y_i \leq N</var></li>
<li><var>1 \leq Z_i \leq 100</var></li>
<li>The pairs <var>(X_i, Y_i)</var> are distinct.</li>
<li>There is no contradiction in input. (That is, there exist integers <var>A_1, A_2, ..., A_N</var> that satisfy the conditions.)</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>M</var>
<var>X_1</var> <var>Y_1</var> <var>Z_1</var>
<var>X_2</var> <var>Y_2</var> <var>Z_2</var>
<var>\vdots</var>
<var>X_M</var> <var>Y_M</var> <var>Z_M</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum total cost required to determine all of <var>A_1, A_2, ..., A_N</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 1
1 2 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>You can determine all of <var>A_1, A_2, A_3</var> by using the magic for the first and third cards.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2 1
2 3 2
1 3 3
4 5 4
5 6 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>2
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>100000 1
1 100000 100
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>99999
</pre></section>
</div>
</span> |
p01582 |
<H1><font color="#000">Problem B:</font> Cover Time</H1>
<p>
Let <i>G</i> be a connected undirected graph where <i>N</i> vertices of <i>G</i> are labeled by numbers from 1 to <i>N</i>. <i>G</i> is simple, i.e. <i>G</i> has no self loops or parallel edges.
</p>
<p>
Let <i>P</i> be a particle walking on vertices of <i>G</i>. At the beginning, <i>P</i> is on the vertex 1. In each step, <i>P</i> moves to one of the adjacent vertices. When there are multiple adjacent vertices, each is selected in the same
probability.
</p>
<p>
The <i>cover time</i> is the expected number of steps necessary for <i>P</i> to visit all the vertices.
</p>
<p>
Your task is to calculate the cover time for each given graph G.
</p>
<H2>Input</H2>
<p>
The input has the following format.
</p>
<p>
<i>N M</i><br/>
<i>a</i><sub>1</sub> <i>b</i><sub>1</sub><br/>
.<br/>
.<br/>
.<br/>
<i>a<sub>M</sub> b<sub>M</sub></i><br/>
</p>
<p>
<i>N</i> is the number of vertices and <i>M</i> is the number of edges. You can assume that 2 ≤ <i>N</i> ≤ 10. <i>a<sub>i</sub></i> and <i>b<sub>i</sub></i> (1 ≤ <i>i</i> ≤ <i>M</i>) are positive integers less than or equal to <i>N</i>, which represent the two vertices connected by the <i>i</i>-th edge. You can assume that the input satisfies the constraints written in the problem description, that is, the given graph <i>G</i> is connected and simple.
</p>
<H2>Output</H2>
<p>
There should be one line containing the cover time in the output.
</p>
<p>
The answer should be printed with six digits after the decimal point, and should not have an error greater than 10<sup>-6</sup>.
</p>
<H2>Sample Input and Output</H2>
<H2>Input #1</H2>
<pre>
3 2
1 2
2 3
</pre>
<H2>Output #1</H2>
<pre>
4.000000
</pre>
<br/>
<H2>Input #2</H2>
<pre>
4 6
1 2
1 3
1 4
2 3
2 4
3 4
</pre>
<H2>Output #2</H2>
<pre>
5.500000
</pre> |
p01428 |
<script type="text/javascript" src="./varmath.js" charset="UTF-8"></script>
<h2>問題文</h2>
<p>
お菓子の魔女 CHARLOTTE は<i>巴マミ</i>とクッキーゲームを楽しんでいる.クッキーゲームは <var>8\times 8</var> の格子状に区切られたテーブルクロスの上にチーズクッキーとチョコレートクッキーを置いて行われる.各格子には高々 <var>1</var> 個のチョコレートクッキーまたはチーズクッキーしか置くことはできない.
</p>
<p>
お菓子の魔女はチーズクッキーを,<i>巴マミ</i>はチョコレートクッキーを交互に置いてゲームを行う.自分のクッキーを置いたあと,そのクッキーから上下左右斜めの各 <var>8</var> 方向について,置くクッキーとすでに置いていた自分のクッキーの間に相手のクッキーのみが直線に並んでいた場合に,その挟まれた相手のクッキーのすべてが自分のクッキーで置き換えられる.クッキーゲームのプレイヤーは自分のターンが回ってきた時,<var>1</var> つ自分のクッキーを置くことができる.ただし,相手のクッキーを少なくとも <var>1</var> つ以上自分のクッキーに置き換えられなければならない.そのような置き場がない場合,自分のターンをパスをしなければならない.
</p>
<p>
お菓子の魔女も<i>巴マミ</i>も考えるのが少々苦手である.そこで,回ってきたターン毎にそのターンの中で置き換えられるクッキーの数を最大化することを考えることにした.<i>巴マミ</i>のターンのときに置き換えられるクッキーの数を最大にするようなクッキーを置く場所の候補が複数ある場合は,より上の場所を,それでも複数ある場合はより左の場所を選択することにした.また同様に,お菓子の魔女のターンのときに候補が複数ある場合はより下の場所を,それでも複数ある場合はより右の場所を選択することにした.
</p>
<p>
テーブルクロスに置かれたクッキーの状態が与えられるので,巴マミからはじめ,彼女たちがそこからクッキーゲームを行い,共に新たなクッキーが置けなくなるまでゲームを続けた時のテーブルクロスの上に置かれたクッキーの状態を求めよ.
<p>
<h2>入力形式</h2>
<p>
入力は以下の形式で与えられる.
</p>
<pre><var>
s_{11} s_{12} ... s_{18}\\
s_{21} s_{22} ... s_{28}\\
...\\
s_{81} s_{82} ... s_{88}\\
</var></pre>
<p>
<var>s_{ij}</var> はテーブルクロスに置かれたクッキーの初期状態を表す文字で,上から <var>i</var> 行目,左から <var>j</var> 列目の格子の状態を表す.チョコレートクッキーが置かれているとき<var>s_{ij}</var>は <code>'o'</code> であり,チーズクッキーが置かれているときは <code>'x'</code>, 何も置かれていないときは <code>'.'</code> となる.
</p>
<h2>出力形式</h2>
<p>
クッキーゲームが行われた後のテーブルクロスの上に置かれたクッキーの状態を,入力形式と同じ形式で出力せよ.
</p>
<h2>制約</h2>
<ul>
<li><var>s_{ij}</var> は <code>'o'</code>, <code>'x'</code>, <code>'.'</code> のいずれかである.</li>
</ul>
<h2>入出力例</h2>
<h3>入力例 1</h3>
<pre>ooox....
.x......
ooo.....
........
........
........
........
........</pre>
<h3>出力例1</h3>
<pre>ooooo...
.o......
ooo.....
...o....
....o...
........
........
........</pre>
<h3>入力例 2</h3>
<pre>........
........
........
...ox...
...xo...
........
........
........</pre>
<h3>出力例 2</h3>
<pre>xxxxxxxx
xxxooxxx
xxxxooxx
xxxxxxxx
ooxxooox
ooxoooox
oxooooox
ooooooox</pre>
<h3>入力例 3</h3>
<pre>........
........
..ooo...
..oxo...
..ooo...
........
........
........</pre>
<h3>出力例 3</h3>
<pre>........
........
..ooo...
..ooo...
..ooo...
.....o..
......o.
........</pre>
<hr>
<address>Problem Setter: Flat35</address> |
p03415 | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a <var>3×3</var> square grid, where each square contains a lowercase English letters.
The letter in the square at the <var>i</var>-th row from the top and <var>j</var>-th column from the left is <var>c_{ij}</var>.</p>
<p>Print the string of length <var>3</var> that can be obtained by concatenating the letters in the squares on the diagonal connecting the top-left and bottom-right corner of the grid, from the top-left to bottom-right.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>Input consists of lowercase English letters.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>c_{11}c_{12}c_{13}</var>
<var>c_{21}c_{22}c_{23}</var>
<var>c_{31}c_{32}c_{33}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the string of length <var>3</var> that can be obtained by concatenating the letters on the diagonal connecting the top-left and bottom-right corner of the grid, from the top-left to bottom-right.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>ant
obe
rec
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>abc
</pre>
<p>The letters in the squares on the diagonal connecting the top-left and bottom-right corner of the grid are <code>a</code>, <code>b</code> and <code>c</code> from top-right to bottom-left. Concatenate these letters and print <code>abc</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>edu
cat
ion
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>ean
</pre></section>
</div>
</span> |
p03100 | <span class="lang-en">
<p>Score : <var>2000</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a connected graph with <var>N</var> vertices and <var>M</var> edges. The vertices are numbered <var>1</var> to <var>N</var>. The <var>i</var>-th edge is an undirected edge of length <var>C_i</var> connecting Vertex <var>A_i</var> and Vertex <var>B_i</var>.</p>
<p>Additionally, an odd number <var>MOD</var> is given.</p>
<p>You will be given <var>Q</var> queries, which should be processed. The queries take the following form:</p>
<ul>
<li>Given in the <var>i</var>-th query are <var>S_i</var>, <var>T_i</var> and <var>R_i</var>. Print <code>YES</code> if there exists a path from Vertex <var>S_i</var> to Vertex <var>T_i</var> whose length is <var>R_i</var> modulo <var>MOD</var>, and print <code>NO</code> otherwise. A path may traverse the same edge multiple times, or go back using the edge it just used.</li>
</ul>
<p>Here, in this problem, the length of a path is <font color="red"><strong>NOT</strong></font> the sum of the lengths of its edges themselves, but the length of the first edge used in the path gets multiplied by <var>1</var>, the second edge gets multiplied by <var>2</var>, the third edge gets multiplied by <var>4</var>, and so on. (More formally, let <var>L_1,...,L_k</var> be the lengths of the edges used, in this order. The length of that path is the sum of <var>L_i \times 2^{i-1}</var>.)</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N,M,Q \leq 50000</var></li>
<li><var>3 \leq MOD \leq 10^{6}</var></li>
<li><var>MOD</var> is odd.</li>
<li><var>1 \leq A_i,B_i\leq N</var></li>
<li><var>0 \leq C_i \leq MOD-1</var></li>
<li><var>1 \leq S_i,T_i \leq N</var></li>
<li><var>0 \leq R_i \leq MOD-1</var></li>
<li>The given graph is connected. (It may contain self-loops or multiple 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>Q</var> <var>MOD</var>
<var>A_1</var> <var>B_1</var> <var>C_1</var>
<var>\vdots</var>
<var>A_M</var> <var>B_M</var> <var>C_M</var>
<var>S_1</var> <var>T_1</var> <var>R_1</var>
<var>\vdots</var>
<var>S_Q</var> <var>T_Q</var> <var>R_Q</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the answers to the <var>i</var>-th query in the <var>i</var>-th line.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 2 2 2019
1 2 1
2 3 2
1 3 5
1 3 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>YES
NO
</pre>
<p>The answer to each query is as follows:</p>
<ul>
<li>The first query: If we take the path <var>1,2,3</var>, its length is <var>1 \times 2^0 + 2 \times 2^1 = 5</var>, so there exists a path whose length is <var>5</var> modulo <var>2019</var>. The answer is <code>YES</code>.</li>
<li>The second query: No matter what path we take from Vertex <var>1</var> to Vertex <var>3</var>, its length will never be <var>4</var> modulo <var>2019</var>. The answer is <code>NO</code>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 6 3 2019
1 2 4
2 3 4
3 4 4
4 5 4
5 6 4
6 1 4
2 6 1110
3 1 1111
4 5 1112
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>YES
NO
NO
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1 2 3 25
1 1 1
1 1 2
1 1 13
1 1 6
1 1 14
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>YES
YES
YES
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>10 15 10 15
1 2 1
2 3 6
3 4 6
2 5 1
5 6 1
4 7 6
1 8 11
2 9 6
5 10 11
9 10 11
3 6 1
2 5 1
2 7 11
9 10 11
5 6 11
1 3 5
9 8 3
7 7 7
7 10 13
4 1 10
9 3 12
10 10 14
9 2 1
6 6 5
8 8 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>YES
NO
NO
NO
NO
NO
NO
YES
YES
NO
</pre></section>
</div>
</span> |
p01097 |
<h3>3D Printing</h3>
<p>
We are designing an installation art piece consisting of
a number of cubes with 3D printing technology for submitting one to
Installation art Contest with Printed Cubes (ICPC).
At this time, we are trying to model a piece consisting of exactly <i>k</i> cubes
of the same size facing the same direction.
</p>
<p>
First, using a CAD system,
we prepare <i>n</i> (<i>n</i> ≥ <i>k</i>) positions as candidates
in the 3D space where cubes can be placed.
When cubes would be placed at all the candidate positions,
the following three conditions are satisfied.
<ul>
<li>Each cube may overlap zero, one or two other cubes, but not three or more.
<li>When a cube overlap two other cubes, those two cubes do not overlap.
<li>Two non-overlapping cubes do not touch at their surfaces, edges or corners.
</ul>
</p>
<p>
Second, choosing appropriate <i>k</i> different positions from <i>n</i> candidates
and placing cubes there,
we obtain a connected polyhedron as a union of the <i>k</i> cubes.
When we use a 3D printer,
we usually print only the thin surface of a 3D object.
In order to save the amount of filament material for the 3D printer,
we want to find the polyhedron with the minimal surface area.
</p>
<p>
Your job is to find the polyhedron with the minimal surface area
consisting of <i>k</i> connected
cubes placed at <i>k</i> selected positions among <i>n</i> given ones.
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ICPCDomestic2016_E1">
<p>
Figure E1. A polyhedron formed with connected identical cubes.
</p>
</center>
<h3>Input</h3>
<p>
The input consists of multiple datasets.
The number of datasets is at most 100.
Each dataset is in the following format.
</p>
<p>
<i>n</i> <i>k</i> <i>s</i><br>
<i>x</i><sub>1</sub> <i>y</i><sub>1</sub> <i>z</i><sub>1</sub><br>
... <br>
<i>x</i><sub><i>n</i></sub> <i>y</i><sub><i>n</i></sub> <i>z</i><sub><i>n</i></sub><br>
</p>
<p>
In the first line of a dataset,
<i>n</i> is the number of the candidate positions,
<i>k</i> is the number of the cubes to form the connected polyhedron,
and <i>s</i> is the edge length of cubes.
<i>n, k</i> and <i>s</i> are integers separated by a space.
The following <i>n</i> lines specify the <i>n</i> candidate positions.
In the <i>i</i>-th line, there are three integers
<i>x</i><sub><i>i</i></sub>,
<i>y</i><sub><i>i</i></sub> and
<i>z</i><sub><i>i</i></sub>
that specify the coordinates of a position,
where the corner of the cube with the smallest coordinate values
may be placed.
Edges of the cubes are to be aligned with either of three axes.
All the values of coordinates are integers separated by a space.
The three conditions on the candidate positions mentioned above are satisfied.
</p>
<p>
The parameters satisfy the following conditions:
1 ≤ <i>k</i> ≤ <i>n</i> ≤ 2000, 3 ≤ <i>s</i> ≤ 100, and
-4×10<sup>7</sup> ≤ <i>x</i><sub><i>i</i></sub>, <i>y</i><sub><i>i</i></sub>, <i>z</i><sub><i>i</i></sub> ≤ 4×10<sup>7</sup>.
</p>
<p>
The end of the input is indicated by a line containing three zeros
separated by a space.
</p>
<h3>Output</h3>
<p>
For each dataset, output a single line containing one integer
indicating the surface area of the connected polyhedron with
the minimal surface area.
When no <i>k</i> cubes form a connected polyhedron, output -1.
</p>
<h3>Sample Input</h3>
<pre>
1 1 100
100 100 100
6 4 10
100 100 100
106 102 102
112 110 104
104 116 102
100 114 104
92 107 100
10 4 10
-100 101 100
-108 102 120
-116 103 100
-124 100 100
-132 99 100
-92 98 100
-84 100 140
-76 103 100
-68 102 100
-60 101 100
10 4 10
100 100 100
108 101 100
116 102 100
124 100 100
132 102 100
200 100 103
192 100 102
184 100 101
176 100 100
168 100 103
4 4 10
100 100 100
108 94 100
116 100 100
108 106 100
23 6 10
100 100 100
96 109 100
100 118 100
109 126 100
118 126 100
127 118 98
127 109 104
127 100 97
118 91 102
109 91 100
111 102 100
111 102 109
111 102 118
111 102 91
111 102 82
111 114 96
111 114 105
102 114 114
93 114 114
84 114 105
84 114 96
93 114 87
102 114 87
10 3 10
100 100 100
116 116 102
132 132 104
148 148 106
164 164 108
108 108 108
124 124 106
140 140 104
156 156 102
172 172 100
0 0 0
</pre>
<h3>Output for the Sample Input</h3>
<pre>
60000
1856
-1
1632
1856
2796
1640
</pre> |
p03550 | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a deck consisting of <var>N</var> cards. Each card has an integer written on it. The integer on the <var>i</var>-th card from the top is <var>a_i</var>.</p>
<p>Two people X and Y will play a game using this deck. Initially, X has a card with <var>Z</var> written on it in his hand, and Y has a card with <var>W</var> written on it in his hand. Then, starting from X, they will alternately perform the following action:</p>
<ul>
<li>Draw some number of cards from the top of the deck. Then, discard the card in his hand and keep the last drawn card instead. Here, at least one card must be drawn.</li>
</ul>
<p>The game ends when there is no more card in the deck. The score of the game is the absolute difference of the integers written on the cards in the two players' hand.</p>
<p>X will play the game so that the score will be maximized, and Y will play the game so that the score will be minimized. What will be the score of the game?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>All input values are integers.</li>
<li><var>1 \leq N \leq 2000</var></li>
<li><var>1 \leq Z, W, a_i \leq 10^9</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>Z</var> <var>W</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 score.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 100 100
10 1000 100
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>900
</pre>
<p>If X draws two cards first, Y will draw the last card, and the score will be <var>|1000 - 100| = 900</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 100 1000
10 100 100
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>900
</pre>
<p>If X draws all the cards first, the score will be <var>|1000 - 100| = 900</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>5 1 1
1 1 1 1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>1 1 1
1000000000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>999999999
</pre></section>
</div>
</span> |
p02711 | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given is a three-digit integer <var>N</var>. Does <var>N</var> contain the digit <var>7</var>?</p>
<p>If so, print <code>Yes</code>; otherwise, print <code>No</code>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>100 \leq N \leq 999</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If <var>N</var> contains the digit <var>7</var>, print <code>Yes</code>; otherwise, print <code>No</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>117
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<p><var>117</var> contains <var>7</var> as its last digit.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>123
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>No
</pre>
<p><var>123</var> does not contain the digit <var>7</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>777
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>Yes
</pre></section>
</div>
</span> |
p03803 | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Alice and Bob are playing <em>One Card Poker</em>.<br/>
One Card Poker is a two-player game using playing cards. </p>
<p>Each card in this game shows an integer between <code>1</code> and <code>13</code>, inclusive.<br/>
The <em>strength</em> of a card is determined by the number written on it, as follows: </p>
<p>Weak <code>2</code> <var><</var> <code>3</code> <var><</var> <code>4</code> <var><</var> <code>5</code> <var><</var> <code>6</code> <var><</var> <code>7</code> <var><</var> <code>8</code> <var><</var> <code>9</code> <var><</var> <code>10</code> <var><</var> <code>11</code> <var><</var> <code>12</code> <var><</var> <code>13</code> <var><</var> <code>1</code> Strong </p>
<p>One Card Poker is played as follows: </p>
<ol>
<li>Each player picks one card from the deck. The chosen card becomes the player's hand.</li>
<li>The players reveal their hands to each other. The player with the stronger card wins the game.<br/>
If their cards are equally strong, the game is drawn. </li>
</ol>
<p>You are watching Alice and Bob playing the game, and can see their hands.<br/>
The number written on Alice's card is <var>A</var>, and the number written on Bob's card is <var>B</var>.<br/>
Write a program to determine the outcome of the game. </p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1≦A≦13</var> </li>
<li><var>1≦B≦13</var> </li>
<li><var>A</var> and <var>B</var> are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>A</var> <var>B</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <code>Alice</code> if Alice will win. Print <code>Bob</code> if Bob will win. Print <code>Draw</code> if the game will be drawn.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>8 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Alice
</pre>
<p><code>8</code> is written on Alice's card, and <code>6</code> is written on Bob's card.
Alice has the stronger card, and thus the output should be <code>Alice</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>Draw
</pre>
<p>Since their cards have the same number, the game will be drawn.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>13 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>Bob
</pre></section>
</div>
</span> |
p00686 |
<h1>
Where's Your Robot?</h1>
<p>
You have full control over a robot that walks around in a rectangular
field paved with square tiles like a chessboard. There are <i>m</i>
columns of tiles from west to east, and <i>n</i> rows of tiles from
south to north (1 <= <i>m, n</i> <= 100). Each tile is given a pair
of coordinates, such as (<i>i, j</i>), where 1 <= <i>i</i> <= <i>m</i>
and 1 <= <i>j</i> <= <i>n.</i>
</p>
<p>
Your robot is initially on the center of the tile at (1, 1), that is,
one at the southwest corner of the field, facing straight north. It
can move either forward or backward, or can change its facing
direction by ninety degrees at a time, according to a command you give
to it, which is one of the following.
</p>
<ul>
<li><tt><b>FORWARD</b></tt> <i>k</i><br>
Go forward by <i>k</i> tiles to its facing direction (1 <= <i>k</i> < 100).
<li><tt><b>BACKWARD</b></tt> <i>k</i><br>
Go backward by <i>k</i> tiles, without changing its facing direction
(1 <= <i>k</i> < 100).
<li><tt><b>RIGHT</b></tt><br>
Turn to the right by ninety degrees.
<li><tt><b>LEFT</b></tt><br>
Turn to the left by ninety degrees.
<li><tt><b>STOP</b></tt><br>
Stop.
</ul>
<p>
While executing either a "<tt><b>FORWARD</b></tt>" or a
"<tt><b>BACKWARD</b></tt>" command, the robot may bump against the
wall surrounding the field. If that happens, the robot gives up the
command execution there and stands at the center of the tile right in
front of the wall, without changing its direction.</p>
<p>
After finishing or giving up execution of a given command, your robot
will stand by for your next command.</p>
<h2>Input</h2>
<p>
The input consists of one or more command sequences. Each input line
has at most fifty characters.</p>
<p>
The first line of a command sequence contains two integer numbers
telling the size of the field, the first number being the number of
columns and the second being the number of rows. There might be white
spaces (blanks and/or tabs) before, in between, or after the two numbers.
Two zeros as field size indicates the end of input.</p>
<p>
Each of the following lines of a command sequence contains a command
to the robot. When a command has an argument, one or more white
spaces are put between them. White spaces may also appear before and
after the command and/or its argument.</p>
<p>
A command sequence is terminated by a line containing a
"<tt><b>STOP</b></tt>" command. The next command sequence, if any,
starts from the next line.</p>
<h2>Output</h2>
<p>
The output should be one line for each command sequence in the input.
It should contain two numbers <i>i</i> and <i>j</i> of the coordinate pair
(<i>i, j</i>), in this order, of the tile on which your robot stops.
Two numbers should be separated by one white spaces.</p>
<h2>Sample Input</h2>
<pre>
6 5
FORWARD 3
RIGHT
FORWARD 5
LEFT
BACKWARD 2
STOP
3 1
FORWARD 2
STOP
0 0
</pre>
<h2>Output for the Sample Input</h2>
<pre>
6 2
1 1
</pre>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_robot">
</center>
|
p01994 | <h2>F: 01 文字列と窓 (Binary String with Slit)</h2>
<h3>問題</h3>
<p>文字の種類が <var>0</var> と <var>1</var> のみからなる文字列 <var>S</var> が与えられます。以下の操作を繰り返すことで、<var>S</var> を <var>T</var> に変えたいです。</p>
<ul>
<li> 文字列 <var>S</var> 中の最も右側に登場する <var>1</var> を含むように、幅 <var>2</var> のスリットを置く。スリット内には連続した <var>2</var> 文字を必ず含まなければならない。つまり、文字列の端 <var>1</var> 文字のみを含むように置くことはできない。<var>2</var> 通り置ける場合も考えられるが、この場合はどちらの方法で置いても構わない。</li>
</ul>
<object data="https://judgeapi.u-aizu.ac.jp/resources/images/HUPC2018_acpc-slit-001.png" type="image/png" width="400"></object>
<ul>
<li> スリット中の <var>2</var> 文字を <var>2</var> 桁の二進数と捉えるとき、元の数値との差の絶対値が <var>1</var> になるようにスリット中の文字を変更する。ただし、スリット中の文字の両方が <var>0</var> になってはならない。つまり、変更後のスリット内の数値は <var>1</var> から <var>3</var> までのいずれかとなる。</li>
</ul>
<object data="https://judgeapi.u-aizu.ac.jp/resources/images/HUPC2018_acpc-slit-002.png" type="image/png" width="600"></object>
<p>クエリが <var>Q</var> 回与えられます。<var>i</var> 番目のクエリで <var>1</var> を少なくとも一つ含む文字列 <var>S_i, T_i</var> が与えられるので、<var>S_i</var> を <var>T_i</var> に変えるために必要な操作回数の最小値を、それぞれのクエリについて求めてください。</p>
<h3>入力形式</h3>
<pre>
<var>Q</var>
<var>S_1</var> <var>T_1</var>
<var>...</var>
<var>S_Q</var> <var>T_Q</var>
</pre>
<p><var>1</var> 行目では、クエリの個数 <var>Q</var> が与えられる。</p>
<p><var>2</var> 行目以降 <var>Q</var> 行は、クエリが与えられる。 <var>i+1</var> 行目では <var>S_i</var> と <var>T_i</var> が空白区切りで与えられる。</p>
<h3>制約</h3>
<ul>
<li> <var>1 \leq Q \leq 10^5</var></li>
<li> <var>2 \leq |S_i| = |T_i| \leq 50</var></li>
<li> <var>S_i, T_i</var> は <code>0</code> と <code>1</code> のみからなる文字列である。</li>
<li> <var>S_i, T_i</var> はともに <code>1</code> を少なくとも 1 つ含む文字列である。</li>
</ul>
<h3>出力形式</h3>
<p>出力は <var>Q</var> 行からなる。</p>
<p><var>i</var> 行目には、<var>i</var> 番目のクエリに対する結果を出力せよ。</p>
<h3>入力例1</h3>
<pre>
4
101 110
101 101
1010 1101
11011001 10010101
</pre>
<h3>出力例1</h3>
<pre>
1
0
3
12
</pre>
<ul>
<li> <var>1</var> 個目のクエリでは、<var>S =</var> <code>101</code> を <var>T =</var> <code>110</code> と一致させるために必要な操作回数の最小値を求める必要があります。以下の画像のように、 <var>S</var> の中に登場する <code>1</code> の中で最も右にあるものが含まれるようにスリットを置き、スリット中の文字列を書き換えることで <var>1</var> 回の操作で <var>S</var> と <var>T</var> を一致させることができます。</li>
</ul>
<object data="https://judgeapi.u-aizu.ac.jp/resources/images/HUPC2018_acpc-slit-003.png" type="image/png" width="250"></object>
<ul>
<li> <var>2</var> 個目のクエリでは、はじめから <var>S</var> と <var>T</var> が一致しているため、操作の必要がありません。</li>
<li> <var>3</var> 個目のクエリについて、以下の画像のように文字列を変更させると <var>3</var> 回の操作で <var>S</var> と <var>T</var> を一致させることができます。</li>
</ul>
<object data="https://judgeapi.u-aizu.ac.jp/resources/images/HUPC2018_acpc-slit-004.png" type="image/png" width="250"></object>
|
p02341 | <!--<h1>写像12相 その11:ボールに区別なし・箱に区別なし・箱の中身は1つ以下</h1>-->
<h1>Balls and Boxes 11</h1>
<table border="">
<tr><th>Balls</th><th>Boxes</th><th>Any way</th><th>At most one ball</th><th>At least one ball</th></tr>
<tr><th>Distinguishable</th><th>Distinguishable</th><td>1</td><td>2</td><td>3</td></tr>
<tr><th>Indistinguishable</th><th>Distinguishable</th><td>4</td><td>5</td><td>6</td></tr>
<tr><th>Distinguishable</th><th>Indistinguishable</th><td>7</td><td>8</td><td>9</td></tr>
<tr><th>Indistinguishable</th><th>Indistinguishable</th><td>10</td><td style="background-color:#aff">11</td><td>12</td></tr>
</table>
<h2>Problem</h2>
<p>You have $n$ balls and $k$ boxes. You want to put these balls into the boxes.</p>
<p>Find the number of ways to put the balls under the following conditions:</p>
<ul>
<li>Each ball is <b>not</b> distinguished from the other.</li>
<li>Each box is <b>not</b> distinguished from the other.</li>
<li>Each ball can go into only one box and no one remains outside of the boxes.</li>
<li>Each box can contain at most one ball.</li>
</ul>
<p>Note that you must print this count modulo $10^9+7$.</p>
<h2>Input</h2>
<pre>
$n$ $k$
</pre>
<p>The first line will contain two integers $n$ and $k$.</p>
<h2>Output</h2>
<p>Print the number of ways modulo $10^9+7$ in a line.</p>
<h2>Constraints</h2>
<ul>
<li>$1 \le n \le 1000$</li>
<li>$1 \le k \le 1000$</li>
</ul>
<h2>Sample Input 1</h2>
<pre>
5 10
</pre>
<h2>Sample Output 1</h2>
<pre>
1
</pre>
<h2>Sample Input 2</h2>
<pre>
200 100
</pre>
<h2>Sample Output 2</h2>
<pre>
0
</pre>
|
p03383 | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There is an <var>H \times W</var> grid (<var>H</var> vertical, <var>W</var> horizontal), where each square contains a lowercase English letter.
Specifically, the letter in the square at the <var>i</var>-th row and <var>j</var>-th column is equal to the <var>j</var>-th character in the string <var>S_i</var>.</p>
<p>Snuke can apply the following operation to this grid any number of times:</p>
<ul>
<li>Choose two different rows and swap them. Or, choose two different columns and swap them.</li>
</ul>
<p>Snuke wants this grid to be <em>symmetric</em>.
That is, for any <var>1 \leq i \leq H</var> and <var>1 \leq j \leq W</var>, the letter in the square at the <var>i</var>-th row and <var>j</var>-th column and the letter in the square at the <var>(H + 1 - i)</var>-th row and <var>(W + 1 - j)</var>-th column should be equal.</p>
<p>Determine if Snuke can achieve this objective.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq H \leq 12</var></li>
<li><var>1 \leq W \leq 12</var></li>
<li><var>|S_i| = W</var></li>
<li><var>S_i</var> consists of lowercase English letters.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>H</var> <var>W</var>
<var>S_1</var>
<var>S_2</var>
<var>:</var>
<var>S_H</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If Snuke can make the grid symmetric, print <code>YES</code>; if he cannot, print <code>NO</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 3
arc
rac
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>YES
</pre>
<p>If the second and third columns from the left are swapped, the grid becomes symmetric, as shown in the image below:</p>
<p><img alt="" src="https://img.atcoder.jp/arc095/2b61caf45e7c5c7311e3944f3418b0bb.png"/></p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 7
atcoder
regular
contest
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>NO
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>12 12
bimonigaloaf
faurwlkbleht
dexwimqxzxbb
lxdgyoifcxid
ydxiliocfdgx
nfoabgilamoi
ibxbdqmzxxwe
pqirylfrcrnf
wtehfkllbura
yfrnpflcrirq
wvcclwgiubrk
lkbrwgwuiccv
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>YES
</pre></section>
</div>
</span> |
p00956 |
<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 K
Black and White Boxes
</h2>
<p>
Alice and Bob play the following game.
</p>
<ol>
<li> There are a number of straight piles of boxes. The boxes have the same size and are painted either black or white.
</li>
<li> Two players, namely Alice and Bob, take their turns alternately. Who to play first is decided by a fair random draw.
</li>
<li> In Alice's turn, she selects a black box in one of the piles, and removes the box together with all the boxes above it, if any. If no black box to remove is left, she loses the game.
</li>
<li> In Bob's turn, he selects a white box in one of the piles, and removes the box together with all the boxes above it, if any. If no white box to remove is left, he loses the game.
</li>
</ol>
<p>
Given an initial configuration of piles and who plays first, the game is a <i>definite perfect information game</i>. In such a game, one of the players has sure win provided he or she plays best. The draw for the first player, thus, essentially decides the winner.
</p>
<p>
In fact, this seemingly boring property is common with many popular games, such as chess. The chess game, however, is complicated enough to prevent thorough analyses, even by supercomputers, which leaves us rooms to enjoy playing.
</p>
<p>
This game of box piles, however, is not as complicated. The best plays may be more easily found. Thus, initial configurations should be fair, that is, giving both players chances to win. A configuration in which one player can always win, regardless of who plays first, is undesirable.
</p>
<p>
You are asked to arrange an initial configuration for this game by picking a number of piles from the given candidate set. As more complicated configuration makes the game more enjoyable, you are expected to find the configuration with the maximum number of boxes among fair ones.
</p>
<h3>Input</h3>
<p>
The input consists of a single test case, formatted as follows.<br/>
<br/>
$n$<br/>
$p_1$<br/>
.<br/>
.<br/>
.<br/>
$p_n$<br/>
</p>
<p>
A positive integer $n$ ($\leq 40$) is the number of candidate piles. Each $p_i$ is a string of characters <span>B</span> and <span>W</span>, representing the $i$-th candidate pile. <span>B</span> and <span>W</span> mean black and white boxes, respectively. They appear in the order in the pile, from bottom to top. The number of boxes in a candidate pile does not exceed 40.
</p>
<h3>Output</h3>
<p>
Output in a line the maximum possible number of boxes in a fair initial configuration consisting of some of the candidate piles. If only the empty configuration is fair, output a zero.
</p>
<h3>Sample Input 1</h3>
<pre>4
B
W
WB
WB</pre>
<h3>Sample Output 1</h3>
<pre>5</pre>
<br/>
<h3>Sample Input 2</h3>
<pre>6
B
W
WB
WB
BWW
BWW</pre>
<h3>Sample Output 2</h3>
<pre>10</pre> |
p03679 | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most <var>X</var> days earlier.
He gets a stomachache if the "best-by" date of the food is <var>X+1</var> or more days earlier, though.</p>
<p>Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious.</p>
<p>Takahashi bought some food <var>A</var> days before the "best-by" date, and ate it <var>B</var> days after he bought it.</p>
<p>Write a program that outputs <code>delicious</code> if he found it delicious, <code>safe</code> if he did not found it delicious but did not get a stomachache either, and <code>dangerous</code> if he got a stomachache.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≤ X,A,B ≤ 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>X</var> <var>A</var> <var>B</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <code>delicious</code> if Takahashi found the food delicious; print <code>safe</code> if he neither found it delicious nor got a stomachache; print <code>dangerous</code> if he got a stomachache.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4 3 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>safe
</pre>
<p>He ate the food three days after the "best-by" date. It was not delicious or harmful for him.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>delicious
</pre>
<p>He ate the food by the "best-by" date. It was delicious for him.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>3 7 12
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>dangerous
</pre>
<p>He ate the food five days after the "best-by" date. It was harmful for him.</p></section>
</div>
</span> |
p01644 |
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: { inlineMath: [['\\(','\\)']] }
});
</script>
<script type='text/javascript' src='http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>
<h1 class="ndoc-heading1">Problem K: Collector</h1>
<p class="ndoc-top">※この物語はフィクションであり、実在の人物、団体などとは一切関係がありません。</p>
<p class="ndoc-top">最近ソーシャルゲームがとても人気で、
たくさんの会社がソーシャルゲームを開発しています。</p>
<p class="ndoc-top">あなたは競合しているソーシャルゲーム開発会社の一つにスパイとして潜入しています。</p>
<h2 class="ndoc-heading2">問題</h2>
<p class="ndoc-top">あなたの務めている会社では、ソーシャルゲームを開発している。
開発しているゲームは、カードを集めて戦うゲームであり、<i>1回300円</i>の有料”ガチャガチャ”を収益の柱にしている。
このゲームのカードには、”ノーマル”、”レア”、”Sレア”の3種類がある。”Sレア”カードはN種類存在する。
有料”ガチャガチャ”では、1回行うごとに97%の確率で1枚のレアカードが手に入り、3%の確率で1枚のSレアカードが手に入る。</p>
<p class="ndoc-top">
有料”ガチャガチャ”における各Sレアカードの出現割合(合計3%)を操作する権限を持っているあなたは、
会社の利益を「減少」させることにした。</p>
<p class="ndoc-top">まず、様々なプレイヤーの有料”ガチャガチャ”の利用傾向を調べたところ、 「各プレイヤーは、
各々の好きなSレアカードを引くと、 満足し、その後は”ガチャガチャ”の利用を行わない」ということが明らかになった。 以上の情報より、
あなたは ”各プレイヤーは、特定のSレアカードを好み、そのSレアカードを引くまでガチャガチャをし続ける”
という仮定の下で会社の利益をモデル化した。</p>
<p class="ndoc-top">すなわち会社の利益とは、 Sレアカードiを好む人の割合 ×
Sレアカードiが得られるまでの消費金額の期待値 の総和である。</p>
<p class="ndoc-top">Sレアカードの出現割合を適切に操作することで、 会社の利益を「最小化」せよ。</p>
<p class="ndoc-top">ただし、出現割合は以下の条件を満たさなければならない。</p>
<ul class="ndoc-indent">
<li>Sレアカード全体の出現割合の和は3%である</li>
<li>各Sレアカードの出現割合は最低0.01%である</li>
</ul>
<h2 class="ndoc-heading2">入力</h2>
<pre>
N (Sレアカードの種類)
P_1 (カード1が好きな人の割合)
P_2 (カード2が好きな人の割合)
...
P_N (カードNが好きな人の割合)
</pre>
<h2 class="ndoc-heading2">出力</h2>
<p class="ndoc-top">問題文で定義された会社の利益の最小値を出力せよ。</p>
<h2 class="ndoc-heading2">制約</h2>
<ul class="ndoc-indent">
<li>\( N \)は整数</li>
<li>\( 1 \leq N \leq 300 \)</li>
<li>\( 0.0 \leq P_i (1 \leq i \leq N) \leq 1.0 \)</li>
<li>\( P_1 + P_2 + \cdots + P_N = 1.0 \)</li>
<li>\( P_i \)は小数点以下5桁まで与えられる</li>
<li>誤差は相対\( 10 ^ {-6} \) まで許容される</li>
</ul>
<h2 class="ndoc-heading2">入出力例</h2>
<h3 class="ndoc-heading3">入力1</h3>
<pre>
1
1.00000
</pre>
<h3 class="ndoc-heading3">出力1</h3>
<pre>
10000
</pre>
<h3 class="ndoc-heading3">入力2</h3>
<pre>
2
0.00001
0.99999
</pre>
<h3 class="ndoc-heading3">出力2</h3>
<pre>
10063.3444816
</pre>
<p>カード1は\( 0.01% \)の割合で出現し、カード2は\( 2.99% \)の割合で出現するように設定する。</p>
<h3 class="ndoc-heading3">入力3</h3>
<pre>
2
0.50000
0.50000
</pre>
<h3 class="ndoc-heading3">出力3</h3>
<pre>
20000.0000000
</pre> |
p04016 | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>For integers <var>b (b \geq 2)</var> and <var>n (n \geq 1)</var>, let the function <var>f(b,n)</var> be defined as follows:</p>
<ul>
<li><var>f(b,n) = n</var>, when <var>n < b</var></li>
<li><var>f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b)</var>, when <var>n \geq b</var></li>
</ul>
<p>Here, <var>{\rm floor}(n / b)</var> denotes the largest integer not exceeding <var>n / b</var>,
and <var>n \ {\rm mod} \ b</var> denotes the remainder of <var>n</var> divided by <var>b</var>.</p>
<p>Less formally, <var>f(b,n)</var> is equal to the sum of the digits of <var>n</var> written in base <var>b</var>.
For example, the following hold:</p>
<ul>
<li><var>f(10,\,87654)=8+7+6+5+4=30</var></li>
<li><var>f(100,\,87654)=8+76+54=138</var></li>
</ul>
<p>You are given integers <var>n</var> and <var>s</var>.
Determine if there exists an integer <var>b (b \geq 2)</var> such that <var>f(b,n)=s</var>.
If the answer is positive, also find the smallest such <var>b</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq n \leq 10^{11}</var></li>
<li><var>1 \leq s \leq 10^{11}</var></li>
<li><var>n,\,s</var> are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>n</var>
<var>s</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If there exists an integer <var>b (b \geq 2)</var> such that <var>f(b,n)=s</var>, print the smallest such <var>b</var>.
If such <var>b</var> does not exist, print <code>-1</code> instead.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>87654
30
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>10
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>87654
138
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>100
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>87654
45678
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>-1
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>31415926535
1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>31415926535
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 5</h3><pre>1
31415926535
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 5</h3><pre>-1
</pre></section>
</div>
</span> |
p03229 | <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given <var>N</var> integers; the <var>i</var>-th of them is <var>A_i</var>.
Find the maximum possible sum of the absolute differences between the adjacent elements after arranging these integers in a row in any order you like.</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 \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>:</var>
<var>A_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum possible sum of the absolute differences between the adjacent elements after arranging the given integers in a row in any order you like.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5
6
8
1
2
3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>21
</pre>
<p>When the integers are arranged as <var>3,8,1,6,2</var>, the sum of the absolute differences between the adjacent elements is <var>|3 - 8| + |8 - 1| + |1 - 6| + |6 - 2| = 21</var>. This is the maximum possible sum.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6
3
1
4
1
5
9
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>25
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>3
5
5
1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>8
</pre></section>
</div>
</span> |
p01214 |
<H1><font color="#000">Problem H:</font> Petoris</H1>
<p>
You are playing a puzzle game named <i>petoris</i>. It is played with a board divided into square
grids and square tiles each of which fits to a single grid.
</p>
<p>
In each step of the game, you have a board partially filled with tiles. You also have a block
consisting of several tiles. You are to place this block somewhere on the board or to discard it,
under the following restrictions on placement:
</p>
<ul>
<li> the block can be rotated, but cannot be divided nor flipped;</li>
<li> no tiles of the block can collide with any tiles existing on the board; and</li>
<li> all the tiles of the block need to be placed inside the board.</li>
</ul>
<p>
Your task is to write a program to find the maximum score you can earn in this step. Here, the
score is the number of the horizontal lines fully filled with tiles after the block is placed, or -1
in case of discard.
</p>
<H2>Input</H2>
<p>
The first line of the input is <i>N</i>, the number of data sets. Then <i>N</i> data sets follow.
</p>
<p>
Each data set consists of lines describing a block and a board. Each description (both for a block
and a board) starts with a line containing two integer <i>H</i> and <i>W</i>, the vertical and horizontal
dimension. Then <i>H</i> lines follow, each with <i>W</i> characters, where a ‘#’ represents a tile and ‘.’ a
vacancy. You can assume that 0 < <i>H</i> ≤ 64 and 0 < <i>W</i> ≤ 64. Each block consists of one or more
tiles all of which are connected. Each board contains zero or more tiles, and has no horizontal
line fully filled with tiles at the initial state.
</p>
<H2>Output</H2>
<p>
For each data set, print in a single line the maximum possible score.
</p>
<H2>Sample Input</H2>
<pre>
5
4 4
....
....
####
....
12 8
........
........
........
........
........
.......#
##.##..#
.#######
.#######
.#######
.#######
.####.#.
4 4
....
....
.###
...#
12 8
........
........
........
........
........
........
........
##...###
##.#####
#######.
#######.
#######.
4 4
####
#..#
#..#
####
12 8
........
........
........
........
........
.......#
##.##..#
##....##
##.##.##
##.##.##
##....##
.####.#.
2 2
##
#.
3 3
.##
.##
##.
4 4
....
.##.
.##.
....
2 2
..
..
</pre>
<H2>Output for the Sample Input</H2>
<pre>
4
1
4
-1
2
</pre>
|
p02592 | <span class="lang-en">
<p>Score : <var>1800</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>This is an output-only problem. You shouldn't read anything from the input.</p>
<p>In short, your task is to simulate multiplication by using only comparison <var>(x < y)</var> and addition <var>(x + y)</var>.
There is no input in this problem, you just print a sequence of operations.</p>
<p>Imagine that there is a big array <var>a[0], a[1], ..., a[N-1]</var> of length <var>N</var>.
The first two values are initially two non-negative integers <var>A</var> and <var>B</var> (which are unknown to you),
the other elements are zeros.
Your goal is to get the product <var>A \cdot B</var> in <var>a[2]</var> at the end.</p>
<p>You are allowed operations of two types, with the following format (where <var>0 \leq i, j, k < N</var>):</p>
<ul>
<li><code>+ i j k</code> — applies operation <var>a[k] = a[i] + a[j]</var>.</li>
<li><code>< i j k</code> — applies operation <var>a[k] = a[i] < a[j]</var>.
That is, if <var>a[i] < a[j]</var> then <var>a[k]</var> becomes <var>1</var>, otherwise it becomes <var>0</var>.</li>
</ul>
<p>You can use at most <var>Q</var> operations.
Elements of <var>a</var> can't exceed <var>V</var>.
Indices <var>(i, j, k)</var> don't have to be distinct.
It's allowed to modify any element of the array (including the first two).
The actual checker simulates the process for multiple pairs <var>(A, B)</var> within a single test.
Each time, the checker chooses values <var>A</var> and <var>B</var>, creates the array <var>a = [A, B, 0, 0, \ldots, 0]</var>,
applies all your operations and ensures that <var>a[2] = A \cdot B</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>0 \leq A, B \leq 10^9</var></li>
<li><var>N = Q = 200\,000</var></li>
<li><var>V = 10^{19} = 10\,000\,000\,000\,000\,000\,000</var></li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Partial Score</h3><ul>
<li><var>800</var> points will be awarded for passing tests that satisfy <var>A, B \leq 10</var>.</li>
<li>Another <var>1000</var> points will be awarded for passing all tests.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The Standard Input is empty.</p>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>In the first line, print the number of operations.
Each operation should then be printed in a single line of format <code>+ i j k</code> or <code>< i j k</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre></pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>4
< 0 1 8
+ 0 1 2
+ 2 8 2
+ 0 0 0</pre>
<p>In the first sample test, the checker checks your sequence only for a pair <var>(A, B) = (2, 3)</var>.
The provided output is correct for this test:</p>
<ul>
<li>Initially, <var>a[0] = 2</var>, <var>a[1] = 3</var>, <var>a[2] = a[3] = \ldots = a[N-1] = 0</var>.</li>
<li><code>< 0 1 8</code> applies <var>a[8] = 1</var> because <var>a[0] < a[1]</var>.</li>
<li><code>+ 0 1 2</code> applies <var>a[2] = a[0] + a[1] = 5</var>.</li>
<li><code>+ 2 8 2</code> applies <var>a[2] = a[2] + a[8] = 6</var>.</li>
<li><code>+ 0 0 0</code> applies <var>a[0] = a[0] + a[0] = 4</var>.</li>
<li>As required, at the end we have <var>a[2] = 6 = A \cdot B</var>.</li>
</ul></section>
</div>
</span> |
p00055 |
<H1>数列</H1>
<p>
次のように定義されている数列があります。
</p>
<ul>
<li>すべての偶数番目の項は一つ前の項に 2 を掛けたものと等しい数である。</li>
<li>すべての奇数番目の項は一つ前の項を 3 で割ったものと等しい数である。</li>
</ul>
<p>
この数列の初項 <var>a</var> を読み込み、初項から第 10 項までの和 <var>s(10)</var> を出力するプログラムを作成してください。
</p>
<H2>Input</H2>
<p>
入力は複数のテストケースからなります。各テストケースとして、数列の初項を表す実数 <var>a</var> (1.0 ≤ <var>a</var> ≤ 10.0) が1行に与えられます。
</p>
<p>
テストケースの数は 50 を超えません。
</p>
<H2>Output</H2>
<p>
テストケースごとに <var>s(10)</var> を1行に出力します。
</p>
<p>
出力は0.000001以下の誤差を含んでもよい。
</p>
<H2>Sample Input</H2>
<pre>
1.0
2.0
3.0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
7.81481481
15.62962963
23.44444444
</pre>
|
p02068 | <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>You are given a positive integer sequence $A$ of length $N$. You can remove any numbers from the sequence to make the sequence “friendly".
A sequence is called friendly if there exists an integer $k$ (>1) such that every number in the sequence is a multiple of $k$.
Since the empty sequence is friendly, it is guaranteed that you can make the initial sequence friendly. </p>
<p>You noticed that there may be multiple ways to make the sequence friendly. So you decide to maximize the sum of all the numbers in the friendly sequence. Please calculate the maximum sum of the all numbers in the friendly sequence which can be obtained from the initial sequence.</p>
<hr />
<h3>Input</h3>
<p>The input consists of a single test case formatted as follows. </p>
<blockquote>$N$
$A_1$
$\vdots$
$A_N$</blockquote>
<p>The first line consists of a single integer $N$ ($1 \le N \le 1000$). The $i+1$-st line consists of an integer $A_i$ ($1 \le A_i \le 10^9$ for $1 \le i \le N$).</p>
<h3>Output</h3>
<p>Print the maximum sum of all the numbers in the friendly sequence which can be obtained from the initial sequence.</p>
<p><div class="no-page-break"><h3>Examples</h3><table class="ioexample"><tr><th>Input</th><th>Output</th></tr><tr><td><pre>6
1
2
3
4
5
6
</pre></td><td><pre>12
</pre></td></tr><tr><td><pre>3
173
1733
111733
</pre></td><td><pre>111733
</pre></td></tr><tr><td><pre>4
1
1
1
1
</pre></td><td><pre>0
</pre></td></tr><tr><td><pre>10
999999999
999999999
999999999
999999999
999999999
999999999
999999999
999999999
999999999
999999999
</pre></td><td><pre>9999999990
</pre></td></tr><tr><td><pre>1
999999999
</pre></td><td><pre>999999999
</pre></td></tr><tr><td><pre>10
28851
8842
9535
2311
25337
26467
12720
10561
8892
6435
</pre></td><td><pre>56898
</pre></td></tr></table></div></p>
|
p00405 | <h1>アカベコ20</h1>
<p>
「アカベコ20」は、イヅア地方の専用劇場で公演を開催しているグループです。アカベコ20のそれぞれのメンバーは、ある一定の日数ごとに公演に参加することになっています。
</p>
<p>
今日の公演ではメンバーが全員参加していました。プロデューサーであるあなたは、メンバーから、今後の公演のメンバーの組み合わせを教えてほしい、と頼まれました。あなたは、公演に参加するメンバーの組み合わせがいくつあるのかを数えることにしました。
</p>
<p>
アカベコ20のメンバー数と、それぞれのメンバーが公演に参加する周期が1日単位で与えられたとき、参加するメンバーの組み合わせが何通りあるかを数えるプログラムを作成せよ。このとき、グループは同じメンバーで永遠に存続すると仮定する。ただし、だれも参加しない場合は組み合わせに含めないこととする。
</p>
<h2>入力</h2>
<p>
入力は以下の形式で与えられる。
</p>
<pre>
$N$
$p_1$ $p_2$ ... $p_N$
</pre>
<p>
1行目にアカベコ20のメンバー数$N$ ($1 \leq N \leq 20$)が与えられる。続く1行に、それぞれのメンバーが公演に参加する周期$p_i$ ($1 \leq p_i \leq 40$)が与えられる。
</p>
<h2>出力</h2>
<p>
参加するメンバーの組み合わせの数を1行に出力する。
</p>
<h2>入出力例</h2>
<h3>入力例1</h3>
<pre>
3
3 5 2
</pre>
<h3>出力例1</h3>
<pre>
7
</pre>
<h3>入力例2</h3>
<pre>
3
2 3 6
</pre>
<h3>出力例2</h3>
<pre>
3
</pre>
<p>
周期が2日のメンバーのみが参加する公演,周期が3日のメンバーのみが参加する公演,周期が2日、3日、6日のメンバーが参加する公演の3通りとなる。
</p>
|
p02438 | <h1>Splice</h1>
<p>
For $n$ lists $L_i$ $(i = 0, 1, ..., n-1)$, perform a sequence of the following operations.
</p>
<ul>
<li>insert($t$, $x$): Insert an integer $x$ at the end of $L_t$.</li>
<li>dump($t$): Print all elements in $L_t$.</li>
<li>splice($s$, $t$): Transfer elements of $L_s$ to the end of $L_t$. $L_s$ becomes empty.</li>
</ul>
<p>
In the initial state, $L_i$ $(i = 0, 1, ..., n-1)$ 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 $s$ $t$
</pre>
<p>
where the first digits <span>0</span>, <span>1</span> and <span>2</span> represent insert, dump and splice operations respectively.
</p>
<h2>Output</h2>
<p>
For each dump operation, print elements of the corresponding list in a line. Separete adjacency elements by a space character (do not print the space after the last element). Note that, if the list is empty, an empty line should be printed.
</p>
<h2>Constraints</h2>
<ul>
<li>$1 \leq n \leq 1,000$</li>
<li>$1 \leq q \leq 500,000$</li>
<li>For a splice operation, $s \ne t$</li>
<li>For a splice operation, $L_s$ is not empty</li>
<li>The total number of elements printed by dump operations do not exceed 1,000,000</li>
<li>$-1,000,000,000 \leq x \leq 1,000,000,000$</li>
</ul>
<h2>Sample Input 1</h2>
<pre>
3 10
0 0 1
0 0 2
0 0 3
0 1 4
0 1 5
2 1 0
0 2 6
1 0
1 1
1 2
</pre>
<h2>Sample Output 1</h2>
<pre>
1 2 3 4 5
6
</pre>
|
p00110 |
<H1>覆面算</H1>
<p>
数式の一部を隠してしまって、隠した数字を探す問題を覆面算といいます。今回は、数式の中のある数字のいくつかを X で隠してしまった式を扱います。以下の数式を入力して、結果を出力するプログラムを作成してください。
</p>
<p> 数式</p>
<ul>
<li>「数字列 + 数字列 = 数字列」の形で、1 行の単純な足し算式です。</li>
<li> 「数字列」は、数字 0 から 9 と文字 X の並びです。</li>
<!--<li> 「数字列」は、8 桁以下の整数を表すものとします。</li>-->
<li> 2 桁以上の「数字列」の左端の数字は 0 ではないものとします。</li>
<li> X は、数式全体の中で必ず 1 つ以上含まれます。</li>
</ul>
<p> 結果</p>
<ul>
<li> 覆面算の答えです。数式が成り立つ様な X の値で 0 ~ 9 のどれか 1 つです。2 つ以上の答えはないものとします。</li>
<li> 答えがない場合、結果は“NA”としてください。</li>
</ul>
<H2>Input</H2>
<p>
複数のデータセットが与えられます。各データセットとして、X を 1 つ以上含む足し算式(空白を含まない 126 文字以内の文字列) が1行に与えられます。データセットの数は 150 を超えません。
</p>
<H2>Output</H2>
<p>
各データセットについて、覆面算の結果を1行に出力してください。数字 0 ~ 9 または NA を出力してください。
</p>
<H2>Sample Input</H2>
<pre>
123+4X6=X79
12X+4X6=X79
XX22+89=X2XX
</pre>
<H2>Output for the Sample Input</H2>
<pre>
5
NA
1
</pre>
|
p00540 |
<h2>舞踏会 (Ball)</h2>
<p>
IOI 王国では,王女である JOI 姫の誕生日を祝って舞踏会が開かれることになった.
</p>
<p>
舞踏会には <var>N</var> 人の貴族が参加する予定である.<var>N</var> は奇数である.貴族には 1 から <var>N</var> までの番号が付けられている.それぞれの貴族には踊りのうまさという整数が定められており,貴族 <var>i</var> (1 ≤ <var>i</var> ≤ <var>N</var>) の踊りのうまさは <var>D<sub>i</sub></var> である.
</p>
<p>
舞踏会では JOI 姫を含む <var>N</var> + 1 人で 2 人ずつ組を作って踊る.IOI 王国では,上級者が初級者を補助できるように,伝統的に以下の方法で踊りの組を決定している.
</p>
<ul>
<li> 最初に,<var>N</var> 人の貴族が 1 列に並ぶ.</li>
<li> 列に並んでいる貴族が 1 人になるまで,以下の操作を繰り返す.<br>
- 列の先頭から 3 人の貴族の踊りのうまさを調べる.<br>
- その 3 人の貴族の中で,最も踊りのうまさが大きい貴族を A とおく.ただし,複数いる場合は,最も踊りのうまさが大きい貴族の中で,最も番号の小さい貴族 を A とおく.<br>
- その 3 人の貴族の中で,最も踊りのうまさが小さい貴族を B とおく.ただし,複数いる場合は,最も踊りのうまさが小さい貴族の中で,最も番号の大きい貴族 を B とおく.<br>
- A と B が列から抜けて組になる.<br>
- 残った 1 人は列の最後尾に移動する.</li>
<li> 最終的に残った 1 人が JOI 姫と組になる.</li>
</ul>
<p>
貴族 1 から貴族 <var>M</var> (1 ≤ <var>M</var> ≤ <var>N</var> − 2) の <var>M</var> 人の貴族については,すでに初期状態で列の何番目に並ぶのかが決まっている.残りの <var>N</var> − <var>M</var> 人の貴族の並び方は国王が自由に決めることができる.
</p>
<p>
JOI 姫は踊りを学んだばかりなので,国王は JOI 姫と組になる貴族の踊りのうまさをできるだけ大きくしたいと考えている.JOI 姫と組になる貴族の踊りのうまさとして考えられる最大値を求めよ.
</p>
<h3>課題</h3>
<p>
それぞれの貴族の踊りのうまさと,<var>M</var> 人の貴族の初期状態で並ぶ場所が与えられたとき,JOI 姫と組になる貴族の踊りのうまさとして考えられる最大値を求めるプログラムを作成せよ.
</p>
<h3>入力</h3>
<p>
標準入力から以下のデータを読み込め.
</p>
<ul>
<li> 1 行目には,2 個の整数 <var>N</var>, <var>M</var> が空白を区切りとして書かれている.これは舞踏会に貴族が <var>N</var> 人参加し,列に並ぶ場所がすでに決まっている貴族が <var>M</var> 人いることを表す.</li>
<li> 続く <var>M</var> 行のうちの <var>i</var> 行目 (1 ≤ <var>i</var> ≤ <var>M</var>) には,2 個の整数 <var>D<sub>i</sub></var>, <var>P<sub>i</sub></var> が空白を区切りとして書かれている.これは貴族 <var>i</var> の踊りのうまさが <var>D<sub>i</sub></var> で,貴族 <var>i</var> が初期状態で列の先頭から <var>P<sub>i</sub></var> 番目に並ぶことを表す.</li>
<li> 続く <var>N</var> − <var>M</var> 行のうちの <var>i</var> 行目 (1 ≤ <var>i</var> ≤ <var>N</var> − <var>M</var>) には,整数 <var>D<sub>i+M</sub></var> が書かれている.これは貴族 (<var>i</var> + <var>M</var>)の踊りのうまさが <var>D<sub>i+M</sub></var> であることを表す.
</li>
</ul>
<h3>出力</h3>
<p>
標準出力に,JOI 姫と組になる貴族の踊りのうまさとして考えられる最大値を表す整数を 1 行で出力せよ.
</p>
<h3>制限</h3>
<p>
すべての入力データは以下の条件を満たす.
</p>
<ul>
<li> 3 ≤ <var>N</var> ≤ 99 999.</li>
<li> <var>N</var> は奇数である.</li>
<li> 1 ≤ <var>M</var> ≤ <var>N</var> − 2.</li>
<li> 1 ≤ <var>D<sub>i</sub></var> ≤ 1 000 000 000 (1 ≤ <var>i</var> ≤ <var>N</var>).</li>
<li> 1 ≤ <var>P<sub>i</sub></var> ≤ <var>N</var> (1 ≤ <var>i</var> ≤ <var>M</var>).</li>
<li> <var>P<sub>i</sub></var> ≠ <var>P<sub>j</sub></var> (1 ≤ <var>i</var> < <var>j</var> ≤ <var>M</var>).</li>
</ul>
<h3>入出力例</h3>
<h3>入力例 1</h3>
<pre>
7 3
5 2
5 5
8 6
6
2
8
9
</pre>
<h3>出力例 1</h3>
<pre>
8
</pre>
<p>
初期状態では 3 人の貴族の並ぶ場所がすでに決まっている.
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_JOI2014_ball1"><br>
括弧内の数字は踊りのうまさを表す.左端が列の先頭である.
</center>
<br>
<p>
例えば,先頭から順に貴族 5,貴族 1,貴族 4,貴族 6,貴族 2,貴族 3,貴族 7 という順番に並んだ場
合を考える.
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_JOI2014_ball2"><br>
すべての貴族が並んだあとの配置
</center>
<br>
<p>
この場合,以下のように列が変化していく.
</p>
<ul>
<li> 列の先頭の 3 人の貴族 (貴族 5,貴族 1,貴族 4) 中で,最も踊りのうまさが大きい貴族 4 と最も踊りのうまさが小さい貴族 5 が組になり,残った貴族 1 が最後尾に移動する.</li>
<li> 次に,列の先頭の 3 人の貴族 (貴族 6,貴族 2,貴族 3) の中で,最も踊りのうまさが大きい貴族は貴族 6 と貴族 3 の 2 人であり,このうち番号の小さい貴族は貴族 3 である.また,列の先頭の 3 人の貴族のうち最も踊りのうまさが小さい貴族は貴族 2 である.貴族 3 と貴族 2 が組になり,残った貴族 6 が最後尾に移動する.</li>
<li> 次に,列の先頭の 3 人の貴族 (貴族 7,貴族 1,貴族 6) の中で,最も踊りのうまさが大きい貴族 7 と最も踊りのうまさが小さい貴族 1 が組になり,残った貴族 6 が最後尾に移動する.</li>
<li> 最終的に貴族 6 が残り,JOI 姫と組になる.貴族 6 の踊りのうまさは 8 である.この値が JOI 姫と組になる貴族の踊りのうまさとして考えられる最大値である.</li>
</ul>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_JOI2014_ball3"><br>
列の変化の様子
</center>
<br>
<h3>入力例 2 </h3>
<pre>
3 1
5 3
5
5
</pre>
<h3>出力例 2</h3>
<pre>
5
</pre>
<p>
どのような順番で並んでも,貴族 2 と JOI 姫が組になる.
</p>
<h3>入力例 3</h3>
<pre>
7 2
32 4
27 6
37
41
41
30
27
</pre>
<h3> 出力例 3</h3>
<pre>
37
</pre>
<div class="source">
<p class="source">
問題文と自動審判に使われるデータは、<a href="http://www.ioi-jp.org">情報オリンピック日本委員会</a>が作成し公開している問題文と採点用テストデータです。
</p>
</div>
|
p02087 |
<h1>J: 左崎・右男</h1>
<h2>問題文</h2>
<p>左崎さんと右男さんは、長さ $N$ の数列 $a_1, a_2, \dots ,a_n$ を使ったゲームで遊ぶのが好きです。
このゲームでは左崎さんと右男さんで交互に以下の操作を行います。</p>
<ul>
<li>左崎さんの手番では $1 \leq k \leq n$ を指定し、$a_1, a_2, \dots ,a_k$ の値をそれぞれ $1$ ずつ減らす。</li>
<li>右男さんの手番では $1 \leq k \leq n$ を指定し、$a_k, a_{k+1}, \dots ,a_n$ の値をそれぞれ $1$ ずつ減らす。</li>
</ul>
<p>手番終了時に数列の中に負の数が存在していた場合、その操作をした人は負けます。負けなかった方は勝ちます。最初に操作をするのは左崎さんです。</p>
<p>左崎さんはこのゲームをプレイするために、$0, 1, \dots, S$ からなる長さ $N$ の数列を全種類($(S+1)^N$ 通り)買ってきました。左崎さんと右男さんがこれらの数列を使って $1$ 回ずつゲームをしました。左崎さんが勝った回数を $998244353$ で割ったあまりを求めてください。ただし、両者ともそれぞれが勝つために最適な操作をしたとします。</p>
<h2>制約</h2>
<ul>
<li>入力は全て整数</li>
<li>$2 \leq N \leq 1000$</li>
<li>$1 \leq S \leq 10^9$</li>
</ul>
<h2>入力</h2>
<p>入力は以下の形式で標準入力から与えられます。</p>
<pre>$N$ $S$</pre>
<h2>出力</h2>
<p>全ての数列を使って $1$ 回ずつゲームをしたときに、左崎さんが勝った回数を $998244353$ で割ったあまりを出力してください。</p>
<h2>入出力例</h2>
<h3>入力例1</h3>
<pre>2 1
</pre>
<h3>出力例1</h3>
<pre>2
</pre>
<p>左崎さんが買ってきた数列は (0, 0), (0, 1), (1, 0), (1, 1) の $4$ つです。</p>
<p>(0, 0), (0, 1) を使ったゲームでは、左崎さんは最初の手番でどの $k$ を選んでも負けます。
(1, 0) を使ったゲームでは、左崎さんは $k = 1$ を選べば、数列が (0, 0) となり、勝てます。
(1, 1) を使ったゲームでは、左崎さんは $k = 2$ を選べば、数列が (0, 0) となり、勝てます。</p>
<h3>入力例2</h3>
<pre>3 2
</pre>
<h3>出力例2</h3>
<pre>12
</pre>
<p>左崎さんが勝つ数列は以下の $12$ 個です。</p>
<pre>(1,0,0)
(1,1,0)
(1,1,1)
(1,2,0)
(1,2,1)
(2,0,0)
(2,0,1)
(2,1,0)
(2,1,1)
(2,1,2)
(2,2,0)
(2,2,1)
</pre>
<h3>入力例3</h3>
<pre>1000 1000000000
</pre>
<h3>出力例3</h3>
<pre>972070366
</pre>
|
p00813 | <H1><font color="#000">Problem C:</font> GIGA Universe Cup</H1>
<p>
Following FIFA World Cup, a larger competition called ``GIGA Universe Cup'' is taking place somewhere in our universe. Both FIFA World Cup and GIGA Universe Cup are two rounds competitions that consist of the first round, also known as ``group league,'' and the second called ``final tournament.'' In the first round, participating teams are divided into groups of four teams each. Each team in a group plays a match against each of the other teams in the same group. For example, let's say we have a group of the following four teams, ``Engband, Swedon, Argontina, and Nigerua.'' They play the following six matches: Engband - Swedon, Engband - Argontina, Engband - Nigerua, Swedon - Argontina, Swedon - Nigerua, and Argontina - Nigerua.
</p>
<p>
The result of a single match is shown by the number of goals scored by each team, like ``Engband 1 - 0 Argontina,'' which says Engband scored one goal whereas Argontina zero. Based on the result of a match, points are given to the two teams as follows and used to rank teams. If a team wins a match (i.e., scores more goals than the other), three points are given to it and zero to the other. If a match draws (i.e., the two teams score the same number of goals), one point is given to each.
</p>
<p>
The <i>goal difference</i> of a team in given matches is the total number of goals it scored minus the total number of goals its opponents scored in these matches. For example, if we have three matches ``Swedon 1 - 2 Engband,'' ``Swedon 3 - 4 Nigerua,'' and ``Swedon 5 - 6 Argontina,'' then the goal difference of Swedon in these three matches is (1 + 3 + 5) - (2 + 4+ 6) = -3.
</p>
<p>
Given the results of all the six matches in a group, teams are ranked by the following criteria, listed in the order of priority (that is, we first apply (a) to determine the ranking, with ties broken by (b), with ties broken by (c), and so on).
</p>
<p>(a) greater number of points in all the group matches; </p>
<p>(b) greater goal difference in all the group matches; </p>
<p>(c) greater number of goals scored in all the group matches. </p>
<p>
If two or more teams are equal on the basis of the above three criteria, their place shall be determined by the following criteria, applied in this order:
</p>
<p>(d) greater number of points obtained in the group matches between the teams concerned; </p>
<p>(e) greater goal difference resulting from the group matches between the teams concerned; </p>
<p>(f) greater number of goals scored in the group matches between the teams concerned; </p>
<p>
If two or more teams are stiIl equal, apply (d), (e), and (f) as necessary to each such group. Repeat this until those three rules to equal teams do not make any further resolution. Finally, teams that still remain equal are ordered by:
</p>
<p>(g) drawing lots by the Organizing Committee for the GIGA Universe Cup. </p>
<p>
The two teams coming first and second in each group qualify for the second round.
</p>
<p>
Your job is to write a program which, given the results of matches played so far in a group and one team specified in the group, calculates the probability that the specified team will qualify for the second round. You may assume each team has played exactly two matches and has one match to play. In total, four matches have been played and two matches are to be played.
</p>
<p>
Assume the probability that any team scores (exactly) p goals in any match is:
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_giga">
</center>
<p>
for <i>p</i> ≤ 8, and zero for <i>p</i> > 8 . Assume the lot in the step (g) is fair.
</p>
<H2>Input</H2>
<p>
The first line of the input is an integer, less than 1000, that indicates the number of subsequent records.
</p>
<p>
The rest of the input is the indicated number of records. A single record has the following format:
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_giga2">
</center>
<p>
In the above, <_> is a single underscore (_) and <<i>empty</i>> a sequence of exactly four underscores (____). Each of <<i>team</i>><sub>1</sub>, ... , <<i>team</i>><sub>4</sub> is either an asterisk character (*) followed by exactly three uppercase letters (e.g., *ENG), or an underscore followed by exactly three uppercase letters (e.g., _SWE). The former indicates that it is the team you are asked to calculate the probability of the second round qualification for. You may assume exactly one of <<i>team</i>><sub>1</sub>, ... , <<i>team</i>><sub>4</sub> is marked with an asterisk. Each <<i>m</i>><sub><i>ij</i></sub>(1 ≤ <i>i</i> < <i>j</i> ≤ 4) is a match result between the <<i>team</i>><sub><i>i</i></sub> and <<i>team</i>><sub><i>j</i></sub>. Each match result is either __-_ (i.e., two underscores, hyphen, and another underscore) or of the form _<i>x</i>-<i>y</i> where each of <i>x</i> and <i>y</i> is a single digit (≤ 8) . The former indicates that the corresponding match has not been played, whereas the latter that the result of the match was <i>x</i> goals by <<i>team</i>><sub><i>i</i></sub> and <i>y</i> goals by <<i>team</i>><sub><i>j</i></sub>. Since each team has played exactly two matches, exactly two match results are in the former format.
</p>
<H2>Output</H2>
<p>
The output should consist of <i>n</i> lines where <i>n</i> is the number of records in the input. The <i>i</i>th line should show the probability that the designated team (marked with an asterisk) will qualify for the second round in the <i>i</i>th record.
</p>
<p>
Numbers should be printed with exactly seven digits after the decimal point. Each number should not contain an error greater than 10<sup>-7</sup>.
</p>
<H2>Sample Input</H2>
<pre>
5
_____*AAA__BBB__CCC__DDD
*AAA_______0-0__0-0___-_
_BBB_____________-___0-0
_CCC_________________0-0
_DDD____________________
______CHN__CRC__TUR_*BRA
_CHN_______0-2___-___0-4
_CRC____________1-1___-_
_TUR_________________1-2
*BRA____________________
______CMR_*KSA__GER__IRL
_CMR_______1-0___-___1-1
*KSA____________0-8___-_
_GER_________________1-1
_IRL____________________
______TUN__JPN_*BEL__RUS
_TUN________-___1-1__0-2
_JPN____________2-2__1-0
*BEL__________________-_
_RUS____________________
______MEX__CRO_*ECU__ITA
_MEX_______1-0__2-1___-_
_CRO_____________-___2-1
*ECU_________________0-2
_ITA____________________
</pre>
<H2>Output for the Sample Input</H2>
<pre>
0.5000000
1.0000000
0.0000000
0.3852746
0.0353304
</pre>
|
p01701 |
<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>
<meta http-equiv="X-UA-Compatible" CONTENT="IE=EmulateIE7" /><style type="text/css">blockquote {
font-family: Menlo, Monaco, "Courier New", monospace;
color: #333333;
display: block;
padding: 8.5px;
margin: 0 0 9px;
font-size: 12px;
line-height: 18px;
background-color: #f5f5f5;
border: 1px solid #ccc;
border: 1px solid rgba(0, 0, 0, 0.15);
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
white-space: pre;
white-space: pre-wrap;
word-break: break-all;
word-wrap: break-word;
}</style><div class="part"><h3>Problem Statement</h3>
<p>We can describe detailed direction by repeating the directional names: north, south, east and west. For example, northwest is the direction halfway between north and west, and northnorthwest is between north and northwest.
</p>
<p>In this problem, we describe more detailed direction between north and west as follows.
</p><ul><li>"<samp>north</samp>" means $0$ degrees.
</li><li>"<samp>west</samp>" means $90$ degrees.
</li><li>If the direction $dir$ means $a$ degrees and the sum of the occurrences of "<samp>north</samp>" and "<samp>west</samp>" in $dir$ is $n$ ($\geq$ 1),
"<samp>north</samp>"$dir$ (the concatenation of "<samp>north</samp>" and $dir$) means $a - \frac{90}{2^n}$ degrees and "<samp>west</samp>"$dir$ means $a + \frac{90}{2^n}$ degrees.
</li></ul>
<p>Your task is to calculate the angle in degrees described by the given direction.
</p>
</div><hr /><div class="part"><h3>Input</h3>
<p>The input contains several datasets. The number of datasets does not exceed $100$.
</p>
<p>Each dataset is described by a single line that contains a string denoting a direction.
You may assume the given string can be obtained by concatenating some "<samp>north</samp>" and "<samp>west</samp>",
the sum of the occurrences of "<samp>north</samp>" and "<samp>west</samp>" in the given string is between $1$ and $20$, inclusive,
and the angle denoted by the given direction is between $0$ and $90$, inclusive.
The final dataset is followed by a single line containing only a single "<samp>#</samp>".
</p>
</div><div class="part"><h3>Output</h3>
<p>For each dataset, print an integer if the angle described by the given direction can be represented as an integer, otherwise print it as an irreducible fraction. Follow the format of the sample output.
</p>
</div><hr /><div class="part"><h3>Sample Input</h3>
<pre>north
west
northwest
northnorthwest
westwestwestnorth
#</pre>
</div><div class="part"><h3>Output for the Sample Input</h3>
<pre>0
90
45
45/2
315/4</pre>
</div> |
p01351 |
<H1>Problem C: Usagitobi</H1>
<p>
<i>m</i> × <i>n</i> マスの盤がある. <i>i</i> 行<i>j</i> 列のマスを(<i>i</i>, <i>j</i>) (0 ≤ <i>i</i> < <i>m</i>, 0 ≤ <i>j</i> < <i>n</i>) で表す.
</p>
<p>
うさぎは(<i>x</i>, <i>y</i>) にいるとき, ((<i>x</i> + <i>a</i>) mod <i>m</i>, (<i>y</i> + <i>b</i>) mod <i>n</i>) または((<i>x</i> + <i>c</i>) mod <i>m</i>, (<i>y</i> + <i>d</i>) mod <i>n</i>) へ跳ぶことができる.
</p>
<p>
いま, うさぎが(0, 0) にいる. 一度跳び立ったマスへ再び行くことはできないとするとき, うさぎは最大何回跳ぶことができるか.
</p>
<H2>Input</H2>
<p>
入力は一行に<i>m</i>, <i>n</i>, <i>a</i>, <i>b</i>, <i>c</i>, <i>d</i> がスペース区切りで与えられる. 1 ≤ <i>m</i>, <i>n</i>, <i>a</i>, <i>b</i>, <i>c</i>, <i>d</i> ≤ 100 000
</p>
<H2>Output</H2>
<p>
うさぎが跳べる最大回数を一行に出力せよ.
</p>
<H2>Sample Input 1</H2>
<pre>
6 6 2 2 2 4
</pre>
<H2>Sample Output 1</H2>
<pre>
8
</pre> |
p02984 | <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> mountains in a circle, called Mountain <var>1</var>, Mountain <var>2</var>, <var>...</var>, Mountain <var>N</var> in clockwise order. <var>N</var> is an <em>odd</em> number.</p>
<p>Between these mountains, there are <var>N</var> dams, called Dam <var>1</var>, Dam <var>2</var>, <var>...</var>, Dam <var>N</var>. Dam <var>i</var> (<var>1 \leq i \leq N</var>) is located between Mountain <var>i</var> and <var>i+1</var> (Mountain <var>N+1</var> is Mountain <var>1</var>).</p>
<p>When Mountain <var>i</var> (<var>1 \leq i \leq N</var>) receives <var>2x</var> liters of rain, Dam <var>i-1</var> and Dam <var>i</var> each accumulates <var>x</var> liters of water (Dam <var>0</var> is Dam <var>N</var>).</p>
<p>One day, each of the mountains received a non-negative <strong>even</strong> number of liters of rain.</p>
<p>As a result, Dam <var>i</var> (<var>1 \leq i \leq N</var>) accumulated a total of <var>A_i</var> liters of water.</p>
<p>Find the amount of rain each of the mountains received. We can prove that the solution is unique under the constraints of this problem.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>All values in input are integers.</li>
<li><var>3 \leq N \leq 10^5-1</var></li>
<li><var>N</var> is an odd number.</li>
<li><var>0 \leq A_i \leq 10^9</var></li>
<li>The situation represented by input can occur when each of the mountains receives a non-negative even number of liters of rain.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <var>N</var> integers representing the number of liters of rain Mountain <var>1</var>, Mountain <var>2</var>, <var>...</var>, Mountain <var>N</var> received, in this order.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
2 2 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>4 0 4
</pre>
<p>If we assume Mountain <var>1</var>, <var>2</var>, and <var>3</var> received <var>4</var>, <var>0</var>, and <var>4</var> liters of rain, respectively, it is consistent with this input, as follows:</p>
<ul>
<li>Dam <var>1</var> should have accumulated <var>\frac{4}{2} + \frac{0}{2} = 2</var> liters of water.</li>
<li>Dam <var>2</var> should have accumulated <var>\frac{0}{2} + \frac{4}{2} = 2</var> liters of water.</li>
<li>Dam <var>3</var> should have accumulated <var>\frac{4}{2} + \frac{4}{2} = 4</var> liters of water.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5
3 8 7 5 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>2 4 12 2 8
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>3
1000000000 1000000000 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0 2000000000 0
</pre></section>
</div>
</span> |
p03696 | <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a string <var>S</var> of length <var>N</var> consisting of <code>(</code> and <code>)</code>. Your task is to insert some number of <code>(</code> and <code>)</code> into <var>S</var> to obtain a <em>correct bracket sequence</em>.<br/>
Here, a correct bracket sequence is defined as follows: </p>
<ul>
<li><code>()</code> is a correct bracket sequence.</li>
<li>If <var>X</var> is a correct bracket sequence, the concatenation of <code>(</code>, <var>X</var> and <code>)</code> in this order is also a correct bracket sequence.</li>
<li>If <var>X</var> and <var>Y</var> are correct bracket sequences, the concatenation of <var>X</var> and <var>Y</var> in this order is also a correct bracket sequence.</li>
<li>Every correct bracket sequence can be derived from the rules above.</li>
</ul>
<p>Find the shortest correct bracket sequence that can be obtained. If there is more than one such sequence, find the lexicographically smallest one.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>The length of <var>S</var> is <var>N</var>.</li>
<li><var>1 ≤ N ≤ 100</var></li>
<li><var>S</var> consists of <code>(</code> and <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 lexicographically smallest string among the shortest correct bracket sequences that can be obtained by inserting some number of <code>(</code> and <code>)</code> into <var>S</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
())
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>(())
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6
)))())
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>(((()))())
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>8
))))((((
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>(((())))(((())))
</pre></section>
</div>
</span> |
p01185 |
<H1><font color="#000">Problem E:</font> Hide-and-seek</H1>
<p>
<i>Hide-and-seek</i> is a children’s game. Players hide here and there, and one player called <i>it</i> tries to find all
the other players.
</p>
<p>
Now you played <i>it</i> and found all the players, so it’s turn to hide from <i>it</i>. Since you have got tired of
running around for finding players, you don’t want to play <i>it</i> again. So you are going to hide yourself at
the place as far as possible from <i>it</i>. But where is that?
</p>
<p>
Your task is to find the place and calculate the maximum possible distance from <i>it</i> to the place to hide.
</p>
<H2>Input</H2>
<p>
The input contains a number of test cases.
</p>
<p>
The first line of each test case contains a positive integer <i>N</i> (<i>N</i> ≤ 1000). The following N lines give
the map where hide-and-seek is played. The map consists of <i>N corridors</i>. Each line contains four real
numbers <i>x</i><sub>1</sub>, <i>y</i><sub>1</sub>, <i>x</i><sub>2</sub>, and <i>y</i><sub>2</sub>, where (<i>x</i><sub>1</sub>, <i>y</i><sub>1</sub> ) and (<i>x</i><sub>2</sub>, <i>y</i><sub>2</sub> ) indicate the two end points of the corridor. All
corridors are straight, and their widths are negligible. After these <i>N</i> lines, there is a line containing two
real numbers <i>sx</i> and <i>sy</i>, indicating the position of <i>it</i>. You can hide at an arbitrary place of any corridor,
and <i>it</i> always walks along corridors. Numbers in the same line are separated by a single space.
</p>
<p>
It is guaranteed that <i>its</i> starting position (<i>sx</i>, <i>sy</i>) is located on some corridor and linked to all corridors
directly or indirectly.
</p>
<p>
The end of the input is indicated by a line containing a single zero.
</p>
<H2>Output</H2>
<p>
For each test case, output a line containing the distance along the corridors from ‘it”s starting position
to the farthest position. The value may contain an error less than or equal to 0.001. You may print any
number of digits below the decimal point.
</p>
<H2>Sample Input</H2>
<pre>
2
0 0 3 3
0 4 3 1
1 1
0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
4.243
</pre>
|
p03442 | <span class="lang-en">
<p>Score : <var>1000</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a tree with <var>N</var> vertices. The vertices are numbered <var>0</var> through <var>N-1</var>, and the edges are numbered <var>1</var> through <var>N-1</var>.
Edge <var>i</var> connects Vertex <var>x_i</var> and <var>y_i</var>, and has a value <var>a_i</var>.
You can perform the following operation any number of times:</p>
<ul>
<li>Choose a simple path and a non-negative integer <var>x</var>, then for each edge <var>e</var> that belongs to the path, change <var>a_e</var> by executing <var>a_e ← a_e ⊕ x</var> (⊕ denotes XOR).</li>
</ul>
<p>Your objective is to have <var>a_e = 0</var> for all edges <var>e</var>.
Find the minimum number of operations required to achieve it.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 ≤ N ≤ 10^5</var></li>
<li><var>0 ≤ x_i,y_i ≤ N-1</var></li>
<li><var>0 ≤ a_i ≤ 15</var></li>
<li>The given graph is a tree.</li>
<li>All input values are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>x_1</var> <var>y_1</var> <var>a_1</var>
<var>x_2</var> <var>y_2</var> <var>a_2</var>
<var>:</var>
<var>x_{N-1}</var> <var>y_{N-1}</var> <var>a_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Find the minimum number of operations required to achieve the objective.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5
0 1 1
0 2 3
0 3 6
3 4 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p>The objective can be achieved in three operations, as follows:</p>
<ul>
<li>First, choose the path connecting Vertex <var>1, 2</var>, and <var>x = 1</var>.</li>
<li>Then, choose the path connecting Vertex <var>2, 3</var>, and <var>x = 2</var>.</li>
<li>Lastly, choose the path connecting Vertex <var>0, 4</var>, and <var>x = 4</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2
1 0 0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre></section>
</div>
</span> |
p03012 | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have <var>N</var> weights indexed <var>1</var> to <var>N</var>. The mass of the weight indexed <var>i</var> is <var>W_i</var>.</p>
<p>We will divide these weights into two groups: the weights with indices not greater than <var>T</var>, and those with indices greater than <var>T</var>, for some integer <var>1 \leq T < N</var>. Let <var>S_1</var> be the sum of the masses of the weights in the former group, and <var>S_2</var> be the sum of the masses of the weights in the latter group.</p>
<p>Consider all possible such divisions and find the minimum possible absolute difference of <var>S_1</var> and <var>S_2</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq N \leq 100</var></li>
<li><var>1 \leq W_i \leq 100</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>W_1</var> <var>W_2</var> <var>...</var> <var>W_{N-1}</var> <var>W_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum possible absolute difference of <var>S_1</var> and <var>S_2</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>0
</pre>
<p>If <var>T = 2</var>, <var>S_1 = 1 + 2 = 3</var> and <var>S_2 = 3</var>, with the absolute difference of <var>0</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4
1 3 1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>2
</pre>
<p>If <var>T = 2</var>, <var>S_1 = 1 + 3 = 4</var> and <var>S_2 = 1 + 1 = 2</var>, with the absolute difference of <var>2</var>. We cannot have a smaller absolute difference.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>8
27 23 76 2 3 5 62 52
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>2
</pre></section>
</div>
</span> |
p00794 |
<H1><font color="#000">Problem G:</font> Walking Ant</H1>
<p>
Ants are quite diligent. They sometimes build their nests beneath flagstones.
</p>
<p>
Here, an ant is walking in a rectangular area tiled with square flagstones, seeking the only hole leading to her nest.
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_walkingAnt">
</center>
<p>
The ant takes exactly one second to move from one flagstone to another. That is, if the ant is on the flagstone with coordinates (<i>x</i>, <i>y</i>) at time <i>t</i>, she will be on one of the five flagstones with the following coordinates at time <i>t</i> + 1:
</p>
<p>
(<i>x</i>, <i>y</i>), (<i>x</i> + 1, <i>y</i>), (<i>x</i> - 1, <i>y</i>), (<i>x</i>, <i>y</i> + 1), (<i>x</i>, <i>y</i> - 1).
</p>
<p>
The ant cannot go out of the rectangular area. The ant can visit the same flagstone more than once.
</p>
<p>
Insects are easy to starve. The ant has to go back to her nest without starving. Physical strength of the ant is expressed by the unit "HP". Initially, the ant has the strength of 6 HP. Every second, she loses 1 HP. When the ant arrives at a flagstone with some food on it, she eats a small piece of the food there, and recovers her strength to the maximum value, i.e., 6 HP, without taking any time. The food is plenty enough, and she can eat it as many times as she wants.
</p>
<p>
When the ant's strength gets down to 0 HP, she dies and will not move anymore. If the ant's strength gets down to 0 HP at the moment she moves to a flagstone, she does not effectively reach the flagstone: even if some food is on it, she cannot eat it; even if the hole is on that stone, she has to die at the entrance of her home.
</p>
<p>
If there is a puddle on a flagstone, the ant cannot move there.
</p>
<p>
Your job is to write a program which computes the minimum possible time for the ant to reach the hole with positive strength from her start position, if ever possible.
</p>
<H2>Input</H2>
<p>
The input consists of multiple maps, each representing the size and the arrangement of the rectangular area. A map is given in the following format.
</p>
<pre>
<i>w h</i>
<i>d</i><sub>11</sub> <i>d</i><sub>12</sub> <i>d</i><sub>13</sub> ... <i>d</i><sub>1<i>w</i></sub>
<i>d</i><sub>2</sub> <i>d</i><sub>22</sub> <i>d</i><sub>23</sub> ... <i>d</i><sub>2<i>w</i></sub>
...
<i>d</i><sub><i>h</i>1</sub> <i>d</i><sub><i>h</i>2</sub> <i>d</i><sub><i>h</i>3</sub> ... <i>d</i><sub><i>h</i><i>w</i></sub>
</pre>
<p>
The integers <i>w</i> and <i>h</i> are the numbers of flagstones in the <i>x</i>- and <i>y</i>-directions, respectively. <i>w</i> and <i>h</i> are less than or equal to 8. The integer <i>d<sub>yx</sub></i> represents the state of the flagstone with coordinates (<i>x</i>, <i>y</i>) as follows.
</p>
<p>
0: There is a puddle on the flagstone, and the ant cannot move there.
</p>
<p>
1, 2: Nothing exists on the flagstone, and the ant can move there. '2' indicates where the ant initially stands.
</p>
<p>
3: The hole to the nest is on the flagstone.
</p>
<p>
4: Some food is on the flagstone.
</p>
<p>
There is one and only one flagstone with a hole. Not more than five flagstones have food on them.
</p>
<p>
The end of the input is indicated by a line with two zeros.
</p>
<p>
Integer numbers in an input line are separated by at least one space character.
</p>
<H2>Output</H2>
<p>
for each map in the input, your program should output one line containing one integer representing the minimum time. If the ant cannot return to her nest, your program should output -1 instead of the minimum time.
</p>
<H2>Sample Input</H2>
<pre>
3 3
2 1 1
1 1 0
1 1 3
8 4
2 1 1 0 1 1 1 0
1 0 4 1 1 0 4 1
1 0 0 0 0 0 0 1
1 1 1 4 1 1 1 3
8 5
1 2 1 1 1 1 1 4
1 0 0 0 1 0 0 1
1 4 1 0 1 1 0 1
1 0 0 0 0 3 0 1
1 1 4 1 1 1 1 1
0 0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
4
-1
13
</pre>
|
p01886 |
<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>Share the Ruins Preservation</h2>
<p>
Two organizations International Community for Preservation of Constructions (ICPC) and Japanese Archaeologist Group (JAG) engage in ruins preservation. Recently, many ruins were found in a certain zone. The two organizations decided to share the preservation of the ruins by assigning some of the ruins to ICPC and the other ruins to JAG.
</p>
<p>
Now, ICPC and JAG make a rule for assignment as follows:
</p>
<ol>
<li> Draw a vertical straight line from the north to the south, avoiding to intersect ruins.</li>
<li> Ruins located to the west of the line are preserved by ICPC. On the other hand, ruins located to the east of the line are preserved by JAG. (It is possible that no ruins are located to the east/west of the line; in this case, ICPC/JAG will preserve no ruins.)</li>
</ol>
<p>
A problem is where to draw a straight line. For each organization, the way to preserve its assigned ruins is to make exactly one fence such that all the assigned ruins are in the region surrounded by the fence. Furthermore, they should minimize the length of such a fence for their budget. If the surrounded areas are vast, expensive costs will be needed to maintain the inside of areas. Therefore, they want to minimize the total preservation cost, i.e. the sum of the areas surrounded by two fences. Your task is to write a program computing the minimum sum of the areas surrounded by two fences, yielded by drawing an appropriate straight line.
</p>
<h3>Input</h3>
<p>
The input consists of a single test case.<br/>
<br/>
$N$<br/>
$x_1$ $y_1$<br/>
$x_2$ $y_2$<br/>
...<br/>
$x_N$ $y_N$
</p>
<p>
The first line contains an integer $N$ ($1 \leq N \leq 100,000$), which is the number of founded ruins. The following $N$ lines represent the location of the ruins. The $i$-th line of them consists of two integers $x_i$ and $y_i$, which indicate the location of the $i$-th ruin is $x_i$ east and $y_i$ north from a certain location in the zone. You can assume the following things for the ruins:
</p>
<ul>
<li>$-10^9 \leq x_i, y_i \leq 10^9$</li>
<li>You can ignore the sizes of ruins. That is, you can assume ruins are points.</li>
<li>No pair of ruins has the same location.</li>
</ul>
<h3>Output</h3>
<p>
Print the minimum total preservation cost yielded by drawing an appropriate straight line. You should round off the cost to the nearest integer.
</p>
<h3>Sample Input 1</h3>
<pre>
8
-10 0
-10 5
-5 5
-5 0
10 0
10 -5
5 -5
5 0
</pre>
<h3>Output for the Sample Input 1</h3>
<pre>
50
</pre>
<h3>Sample Input 2</h3>
<pre>
5
0 0
0 1
0 2
1 0
1 1
</pre>
<h3>Output for the Sample Input 2</h3>
<pre>
0
</pre>
<h3>Sample Input 3</h3>
<pre>
6
1 5
1 6
0 5
0 -5
-1 -5
-1 -6
</pre>
<h3>Output for the Sample Input 3</h3>
<pre>
6
</pre>
<h3>Sample Input 4</h3>
<pre>
10
2 5
4 6
9 5
8 8
1 3
6 4
5 9
7 3
7 7
3 9
</pre>
<h3>Output for the Sample Input 4</h3>
<pre>
17
</pre> |
p02253 | <h1>Activity Selection Problem</h1>
<p>There are $n$ acitivities with start times $\{s_i\}$ and finish times $\{t_i\}$. Assuming that a person can only work on a single activity at a time, find the maximum number of activities that can be performed by a single person.</p>
<h2>Input</h2>
<pre>
$n$
$s_1$ $t_1$
$s_2$ $t_2$
:
$s_n$ $t_n$
</pre>
<p>The first line consists of the integer $n$. In the following $n$ lines, the start time $s_i$ and the finish time $t_i$ of the activity $i$ are given.</p>
<h2>出力</h2>
<p>Print the maximum number of activities in a line.</p>
<h2>Constraints</h2>
<ul>
<li>$1 \le n \le 10^5$</li>
<li>$1 \le s_i \lt t_i \le 10^9 (1 \le i \le n)$</li>
</ul>
<h2>Sample Input 1</h2>
<pre>
5
1 2
3 9
3 5
5 9
6 8
</pre>
<h2>Sample Output 1</h2>
<pre>
3
</pre>
<h2>Sample Input 2</h2>
<pre>
3
1 5
3 4
2 5
</pre>
<h2>Sample Output 2</h2>
<pre>
1
</pre>
<h2>Sample Input 3</h2>
<pre>
3
1 2
2 3
3 4
</pre>
<h2>Sample Output 3</h2>
<pre>
2
</pre>
|
p02603 | <span class="lang-en">
<p>Score: <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3>
<p>To become a millionaire, M-kun has decided to make money by trading in the next <var>N</var> days. Currently, he has <var>1000</var> yen and no stocks - only one kind of stock is issued in the country where he lives.</p>
<p>He is famous across the country for his ability to foresee the future. He already knows that the price of one stock in the next <var>N</var> days will be as follows:</p>
<ul>
<li><var>A_1</var> yen on the <var>1</var>-st day, <var>A_2</var> yen on the <var>2</var>-nd day, ..., <var>A_N</var> yen on the <var>N</var>-th day.</li>
</ul>
<p>In the <var>i</var>-th day, M-kun can make the following trade <strong>any number of times</strong> (possibly zero), <strong>within the amount of money and stocks that he has at the time</strong>.</p>
<ul>
<li>Buy stock: Pay <var>A_i</var> yen and receive one stock.</li>
<li>Sell stock: Sell one stock for <var>A_i</var> yen.</li>
</ul>
<p>What is the maximum possible amount of money that M-kun can have in the end by trading optimally?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3>
<ul>
<li><var>2 \leq N \leq 80</var></li>
<li><var>100 \leq A_i \leq 200</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3>
<p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>A_1</var> <var>A_2</var> <var>\cdots</var> <var>A_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3>
<p>Print the maximum possible amount of money that M-kun can have in the end, as an integer.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>7
100 130 130 130 115 115 150
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1685
</pre>
<p>In this sample input, M-kun has seven days of trading. One way to have <var>1685</var> yen in the end is as follows:</p>
<ul>
<li>Initially, he has <var>1000</var> yen and no stocks.</li>
<li>Day <var>1</var>: Buy <var>10</var> stocks for <var>1000</var> yen. Now he has <var>0</var> yen.</li>
<li>Day <var>2</var>: Sell <var>7</var> stocks for <var>910</var> yen. Now he has <var>910</var> yen.</li>
<li>Day <var>3</var>: Sell <var>3</var> stocks for <var>390</var> yen. Now he has <var>1300</var> yen.</li>
<li>Day <var>4</var>: Do nothing.</li>
<li>Day <var>5</var>: Buy <var>1</var> stock for <var>115</var> yen. Now he has <var>1185</var> yen.</li>
<li>Day <var>6</var>: Buy <var>10</var> stocks for <var>1150</var> yen. Now he has <var>35</var> yen.</li>
<li>Day <var>7</var>: Sell <var>11</var> stocks for <var>1650</var> yen. Now he has <var>1685</var> yen.</li>
</ul>
<p>There is no way to have <var>1686</var> yen or more in the end, so the answer is <var>1685</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6
200 180 160 140 120 100
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1000
</pre>
<p>In this sample input, it is optimal to do nothing throughout the six days, after which we will have <var>1000</var> yen.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>2
157 193
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>1216
</pre>
<p>In this sample input, it is optimal to buy <var>6</var> stocks in Day <var>1</var> and sell them in Day <var>2</var>, after which we will have <var>1216</var> yen.</p></section>
</div>
</span> |
p03911 | <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>On a planet far, far away, <var>M</var> languages are spoken. They are conveniently numbered <var>1</var> through <var>M</var>.</p>
<p>For <em>CODE FESTIVAL 20XX</em> held on this planet, <var>N</var> participants gathered from all over the planet.</p>
<p>The <var>i</var>-th <var>(1≦i≦N)</var> participant can speak <var>K_i</var> languages numbered <var>L_{i,1}, L_{i,2}, ..., L_{i,{}K_i}</var>.</p>
<p>Two participants <var>A</var> and <var>B</var> can <em>communicate</em> with each other if and only if one of the following conditions is satisfied:</p>
<ul>
<li>There exists a language that both <var>A</var> and <var>B</var> can speak.</li>
<li>There exists a participant <var>X</var> that both <var>A</var> and <var>B</var> can communicate with.</li>
</ul>
<p>Determine whether all <var>N</var> participants can communicate with all other participants.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦10^5</var></li>
<li><var>1≦M≦10^5</var></li>
<li><var>1≦K_i≦M</var></li>
<li><var>(</var>The sum of all <var>K_i)≦10^5</var></li>
<li><var>1≦L_{i,j}≦M</var></li>
<li><var>L_{i,1}, L_{i,2}, ..., L_{i,{}K_i}</var> are pairwise distinct.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Partial Score</h3><ul>
<li><var>200</var> points will be awarded for passing the test set satisfying the following: <var>N≦1000</var>, <var>M≦1000</var> and <var>(</var>The sum of all <var>K_i)≦1000</var>.</li>
<li>Additional <var>200</var> points will be awarded for passing the test set without additional constraints.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>M</var>
<var>K_1</var> <var>L_{1,1}</var> <var>L_{1,2}</var> <var>...</var> <var>L_{1,{}K_1}</var>
<var>K_2</var> <var>L_{2,1}</var> <var>L_{2,2}</var> <var>...</var> <var>L_{2,{}K_2}</var>
<var>:</var>
<var>K_N</var> <var>L_{N,1}</var> <var>L_{N,2}</var> <var>...</var> <var>L_{N,{}K_N}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If all <var>N</var> participants can communicate with all other participants, print <code>YES</code>. Otherwise, print <code>NO</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4 6
3 1 2 3
2 4 2
2 4 6
1 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>YES
</pre>
<p>Any two participants can communicate with each other, as follows:</p>
<ul>
<li>Participants <var>1</var> and <var>2</var>: both can speak language <var>2</var>.</li>
<li>Participants <var>2</var> and <var>3</var>: both can speak language <var>4</var>.</li>
<li>Participants <var>1</var> and <var>3</var>: both can communicate with participant <var>2</var>.</li>
<li>Participants <var>3</var> and <var>4</var>: both can speak language <var>6</var>.</li>
<li>Participants <var>2</var> and <var>4</var>: both can communicate with participant <var>3</var>.</li>
<li>Participants <var>1</var> and <var>4</var>: both can communicate with participant <var>2</var>.</li>
</ul>
<p>Note that there can be languages spoken by no participant.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4 4
2 1 2
2 1 2
1 3
2 4 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>NO
</pre>
<p>For example, participants <var>1</var> and <var>3</var> cannot communicate with each other.</p></section>
</div>
</span> |
p02316 |
<H1>Knapsack Problem</H1>
<br/>
<p>
You have <var>N</var> kinds of 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>
<li>You can select as many items as possible into a knapsack for each kind.</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 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> ≤ 1000</li>
<li> 1 ≤ <var>w<sub>i</sub></var> ≤ 1000</li>
<li> 1 ≤ <var>W</var> ≤ 10000</li>
</ul>
<H2>Sample Input 1</H2>
<pre>
4 8
4 2
5 2
2 1
8 3
</pre>
<H2>Sample Output 1</H2>
<pre>
21
</pre>
<br/>
<H2>Sample Input 2</H2>
<pre>
2 20
5 9
4 10
</pre>
<H2>Sample Output 2</H2>
<pre>
10
</pre>
<br/>
<H2>Sample Input 3</H2>
<pre>
3 9
2 1
3 1
5 2
</pre>
<H2>Sample Output 3</H2>
<pre>
27
</pre> |
p02746 | <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>For a non-negative integer <var>K</var>, we define a fractal of level <var>K</var> as follows:</p>
<ul>
<li>A fractal of level <var>0</var> is a grid with just one white square.</li>
<li>When <var>K > 0</var>, a fractal of level <var>K</var> is a <var>3^K \times 3^K</var> grid. If we divide this grid into nine <var>3^{K-1} \times 3^{K-1}</var> subgrids:<ul>
<li>The central subgrid consists of only black squares.</li>
<li>Each of the other eight subgrids is a fractal of level <var>K-1</var>.</li>
</ul>
</li>
</ul>
<p>For example, a fractal of level <var>2</var> is as follows:</p>
<p><img alt="A fractal of level 2" src="https://img.atcoder.jp/panasonic2020/e3a473bc5b6d3ac74c3ab8130213ef09.png"/></p>
<p>In a fractal of level <var>30</var>, let <var>(r, c)</var> denote the square at the <var>r</var>-th row from the top and the <var>c</var>-th column from the left.</p>
<p>You are given <var>Q</var> quadruples of integers <var>(a_i, b_i, c_i, d_i)</var>.
For each quadruple, find the distance from <var>(a_i, b_i)</var> to <var>(c_i, d_i)</var>.</p>
<p>Here the distance from <var>(a, b)</var> to <var>(c, d)</var> is the minimum integer <var>n</var> that satisfies the following condition:</p>
<ul>
<li>There exists a sequence of white squares <var>(x_0, y_0), \ldots, (x_n, y_n)</var> satisfying the following conditions:<ul>
<li><var>(x_0, y_0) = (a, b)</var></li>
<li><var>(x_n, y_n) = (c, d)</var></li>
<li>For every <var>i (0 \leq i \leq n-1)</var>, <var>(x_i, y_i)</var> and <var>(x_{i+1}, y_{i+1})</var> share a side.</li>
</ul>
</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq Q \leq 10000</var></li>
<li><var>1 \leq a_i, b_i, c_i, d_i \leq 3^{30}</var></li>
<li><var>(a_i, b_i) \neq (c_i, d_i)</var></li>
<li><var>(a_i, b_i)</var> and <var>(c_i, d_i)</var> are white squares.</li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>Q</var>
<var>a_1 \ b_1 \ c_1 \ d_1</var>
<var>:</var>
<var>a_Q \ b_Q \ c_Q \ d_Q</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <var>Q</var> lines.
The <var>i</var>-th line should contain the distance from <var>(a_i, b_i)</var> to <var>(c_i, d_i)</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2
4 2 7 4
9 9 1 9
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>5
8
</pre>
<p><img alt="" src="https://img.atcoder.jp/panasonic2020/b590cee9850abdad4109ab940f9efe5a.png"/></p></section>
</div>
</span> |
p01869 |
<!-- - - - - - 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: 28</h1>
<h2>問題文</h2>
<p>
トランプゲームの大富豪において,ランクが $2$, $8$ のカードは強力です.
そこで,$10$ 進数表記で数字の $2$, $8$ のみからなる整数を<b>良い整数</b>と呼ぶことにします.
良い整数を小さいものから列挙すると $2, 8, 22, 28, 82, 88, \cdots$ となります.
</p>
<p>
$n$ を正の整数とします.$n$ が良い整数の積の形で表現できるとき,
最大でいくつの積になるか求めてください.できないなら $-1$ と出力してください.
</p>
<h2>入力</h2>
<p>
$n$
</p>
<h2>制約</h2>
<p>
$1 \leq n \leq 10^{18}$
</p>
<h2>出力</h2>
<p>
答えを $1$ 行で出力してください.
</p>
<h2>サンプル</h2>
<h3>サンプル入力1</h3>
<pre>
1
</pre>
<h3>サンプル出力1</h3>
<pre>
-1
</pre>
<h3>サンプル入力2</h3>
<pre>
2
</pre>
<h3>サンプル出力2</h3>
<pre>
1
</pre>
<h3>サンプル入力3</h3>
<pre>
88
</pre>
<h3>サンプル出力3</h3>
<pre>
3
</pre>
<p>
$2 \times 2 \times 22$ と表せます.
</p>
<h3>サンプル入力4</h3>
<pre>
100
</pre>
<h3>サンプル出力4</h3>
<pre>
-1
</pre>
<h3>サンプル入力5</h3>
<pre>
173553147234869248
</pre>
<h3>サンプル出力5</h3>
<pre>
11
</pre>
<p>
$2^6 \times 28 \times 2222^3 \times 8828$ と表せます.
</p>
<!-- - - - - - end nicebody - - - - - --> |
p03854 | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given a string <var>S</var> consisting of lowercase English letters.
Another string <var>T</var> is initially empty.
Determine whether it is possible to obtain <var>S = T</var> by performing the following operation an arbitrary number of times:</p>
<ul>
<li>Append one of the following at the end of <var>T</var>: <code>dream</code>, <code>dreamer</code>, <code>erase</code> and <code>eraser</code>.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1≦|S|≦10^5</var></li>
<li><var>S</var> consists of lowercase English letters.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>S</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If it is possible to obtain <var>S = T</var>, print <code>YES</code>. Otherwise, print <code>NO</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>erasedream
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>YES
</pre>
<p>Append <code>erase</code> and <code>dream</code> at the end of <var>T</var> in this order, to obtain <var>S = T</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>dreameraser
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>YES
</pre>
<p>Append <code>dream</code> and <code>eraser</code> at the end of <var>T</var> in this order, to obtain <var>S = T</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>dreamerer
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>NO
</pre></section>
</div>
</span> |
p00281 |
<h2>親方の給料計算</h2>
<p>
ワシはパイプつなぎ組合の親方じゃ。職人を工事現場に派遣し、現場でパイプをつながせておる。去年は工事が増えて大儲けするかと思ったのじゃが、ちょっと給料の出し方がまずくてのぅ。ウチとしては大赤字になってしまったのじゃよ…。そこで、今年は職人たちへの給料の出し方を工夫したいのじゃ。
</p>
<p>
職人たちの給料は、工事の種類とこなした回数で決めておる。つまり、<br>
職人の給料 = 種類 1 の単価 × 種類 1 をこなした回数<br>
+ 種類 2 の単価 × 種類 2 をこなした回数<br>
....<br>
+ 種類 <var>M</var> の単価 × 種類 <var>M</var> をこなした回数<br>
<br>
となるのじゃ。これを計算するために、ウチでは、どの職人がどの種類の工事を何回こなしたかを次のような表に記録しておる。
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_PCK2013_salary">
</center>
<br>
<p>
例えば、上の表では、職人1が工事2を2回、工事4を1回こなしたことを示しておる。
</p>
<p>
職人たちがこなした仕事の回数はもう変えられんが、やつらは工事の単価を知らんので、単価をいろいろと変えながら皆の不満が出ぬよう給料を調整しようと思うておる。じゃが、ワシがこしらえたプログラムが今もって動かなくてのぅ。ちょっとみてくれんかね。
</p>
<pre>
//省略
int i, j;
for ( i = 0; i < N; i++ ){
c[i] = 0;
for ( j = 0; j < M; j++ ){
c[i] = c[i] + a[i][j]*b[j];
}
}
//省略
</pre>
<p>
<var>N</var> は職人の数で <var>M</var> は工事の種類の数じゃ。変数 <var>a[i][j]</var> に職人iが工事 <var>j</var> をこなした回数を、<var>b[j]</var> に工事 <var>j</var> の単価をいれて、<var>c[i]</var> に職人 <var>i</var> の給料を格納しておる。合っているはずなのに、うんともすんとも言わん!そろそろ今年の給料を職人たちに払わないとまずいのじゃが・・・・・なんとかならんかのぅ。
</p>
<p>
それでは、職人のこなした仕事の回数と各工事の単価の情報が与えられたとき、各職人の給料を計算するプログラムを作成してください。
</p>
<h2>入力</h2>
<p>
入力は1つのデータセットからなる。入力データは以下の形式で与えられる。
</p>
<pre>
<var>N</var> <var>M</var>
<var>s<sub>1</sub></var> <var>t<sub>1</sub></var> <var>e<sub>1</sub></var>
<var>s<sub>2</sub></var> <var>t<sub>2</sub></var> <var>e<sub>2</sub></var>
:
0 0 0
<var>L</var>
<var>b<sub>11</sub></var> <var>b<sub>12</sub></var> ... <var>b<sub>1M</sub></var>
<var>b<sub>21</sub></var> <var>b<sub>22</sub></var> ... <var>b<sub>2M</sub></var>
:
<var>b<sub>L1</sub></var> <var>b<sub>L2</sub></var> ... <var>b<sub>LM</sub></var>
</pre>
<ul>
<li> 1行目は職人の数 <var>N</var>(1 ≤ <var>N</var> ≤ 10000)と工事の種類の数 <var>M</var>(1 ≤ <var>M</var> ≤ 10000)。</li>
<li> 続いて、工事の記録として、職人の番号 <var>s<sub>i</sub></var> (1 ≤ <var>s<sub>i</sub></var> ≤ <var>N</var>) と工事の種類の番号 <var>t<sub>i</sub></var>(1 ≤ <var>t<sub>i</sub></var> ≤ <var>M</var>)、および職人 <var>s<sub>i</sub></var> が工事 <var>t<sub>i</sub></var> をこなした回数 <var>e<sub>i</sub></var> (1 ≤ <var>e<sub>i</sub></var> ≤ 50000) からなる行が1行以上与えられる。工事の記録はゼロ3つの行で終わる。ただし、<var>e<sub>i</sub></var> の合計は 1 以上 50000 以下である。また、工事の記録には、どの職人と工事の種類の組も2度以上現れない。工事をこなした回数が 0 である職人と工事の種類の組は与えられない。</li>
<li> 続く 1 行は給料の算出を行う回数 <var>L</var> (1 ≤ <var>L</var> ≤ 100) 。</li>
<li> 続く <var>L</var> 行は、<var>i</var> 回目の給料の算出に必要な、工事 <var>j</var> の単価 <var>b<sub>ij</sub></var> (0 ≤ <var>b<sub>ij</sub></var> ≤ 10000) の並び。</li>
</ul>
<h2>出力</h2>
<p>
以下の形式で、<var>i</var> 回目の給料の算出によって得られた職人 <var>j</var> の給料 <var>c<sub>ij</sub></var> を順番に出力する。各給料の間は空白1つで区切る。
</p>
<pre>
<var>c<sub>11</sub></var> <var>c<sub>12</sub></var> ... <var>c<sub>1N</sub></var>
<var>c<sub>21</sub></var> <var>c<sub>22</sub></var> ... <var>c<sub>2N</sub></var>
:
<var>c<sub>L1</sub></var> <var>c<sub>L2</sub></var> ... <var>c<sub>LN</sub></var>
</pre>
<h2>入出力例</h2>
<br>
<h2>入力例</h2>
<pre>
4 6
1 2 1
2 3 2
2 4 3
3 1 5
4 2 4
4 3 2
4 6 10
0 0 0
3
1 3 2 4 0 10
6 5 4 3 2 1
5 1 1 5 5 1
</pre>
<h2>出力例</h2>
<pre>
3 16 5 116
5 17 30 38
1 17 25 16
</pre> |
p03507 | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>In your garden, there is a long and narrow flowerbed that stretches infinitely to the east. You have decided to plant <var>N</var> kinds of flowers in this empty flowerbed. For convenience, we will call these <var>N</var> kinds of flowers Flower <var>1,</var> <var>2,</var> <var>…,</var> <var>N</var>. Also, we will call the position that is <var>p</var> centimeters from the west end of the flowerbed Position <var>p</var>.</p>
<p>You will plant Flower <var>i</var> <var>(1 ≤ i ≤ N)</var> as follows: first, plant one at Position <var>w_i</var>, then plant one every <var>d_i</var> centimeters endlessly toward the east. That is, Flower <var>i</var> will be planted at the positions <var>w_i,</var> <var>w_i + d_i,</var> <var>w_i + 2 d_i,</var> <var>…</var> Note that more than one flower may be planted at the same position.</p>
<p>Find the position at which the <var>K</var>-th flower from the west is planted. If more than one flower is planted at the same position, they are counted individually.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≤ N ≤ 10^5</var></li>
<li><var>1 ≤ K ≤ 10^9</var></li>
<li><var>1 ≤ w_i ≤ 10^{18}</var></li>
<li><var>1 ≤ d_i ≤ 10^9</var></li>
<li>All input values are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>w_1</var> <var>d_1</var>
<var>:</var>
<var>w_N</var> <var>d_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>When the <var>K</var>-th flower from the west is planted at Position <var>X</var>, print the value of <var>X</var>. (The westmost flower is counted as the <var>1</var>-st flower.)</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 6
20 10
25 15
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>50
</pre>
<p>Two kinds of flowers are planted at the following positions:</p>
<ul>
<li>Flower <var>1</var>: Position <var>20,</var> <var>30,</var> <var>40,</var> <var>50,</var> <var>60,</var> <var>…</var></li>
<li>Flower <var>2</var>: Position <var>25,</var> <var>40,</var> <var>55,</var> <var>70,</var> <var>85,</var> <var>…</var></li>
</ul>
<p>The sixth flower from the west is the Flower <var>1</var> planted at Position <var>50</var>. Note that the two flowers planted at Position <var>40</var> are counted individually.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 9
10 10
10 10
10 10
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>30
</pre>
<p>Three flowers are planted at each of the positions <var>10,</var> <var>20,</var> <var>30,</var> <var>…</var> Thus, the ninth flower from the west is planted at Position <var>30</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1 1000000000
1000000000000000000 1000000000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>1999999999000000000
</pre></section>
</div>
</span> |
p03157 | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There is a grid with <var>H</var> rows and <var>W</var> columns, where each square is painted black or white.</p>
<p>You are given <var>H</var> strings <var>S_1, S_2, ..., S_H</var>, each of length <var>W</var>.
If the square at the <var>i</var>-th row from the top and the <var>j</var>-th column from the left is painted black, the <var>j</var>-th character in the string <var>S_i</var> is <code>#</code>; if that square is painted white, the <var>j</var>-th character in the string <var>S_i</var> is <code>.</code>.</p>
<p>Find the number of pairs of a black square <var>c_1</var> and a white square <var>c_2</var> that satisfy the following condition:</p>
<ul>
<li>There is a path from the square <var>c_1</var> to the square <var>c_2</var> where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq H, W \leq 400</var></li>
<li><var>|S_i| = W</var> (<var>1 \leq i \leq H</var>)</li>
<li>For each <var>i</var> (<var>1 \leq i \leq H</var>), the string <var>S_i</var> consists of characters <code>#</code> and <code>.</code>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>H</var> <var>W</var>
<var>S_1</var>
<var>S_2</var>
<var>:</var>
<var>S_H</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the answer.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 3
.#.
..#
#..
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>10
</pre>
<p>Some of the pairs satisfying the condition are <var>((1, 2), (3, 3))</var> and <var>((3, 1), (3, 2))</var>, where <var>(i, j)</var> denotes the square at the <var>i</var>-th row from the top and the <var>j</var>-th column from the left.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2 4
....
....
</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>4 3
###
###
...
###
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>6
</pre></section>
</div>
</span> |
p01490 |
<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 E:
HullMarathon
</h2>
<p>
うさぎはフルマラソンという競技が好きである. この競技はチームで行う. チームメンバーは競技開始前に原点に集まる. 競技開始と同時に走り出し, 1 分後に立ち止まる.このとき,チームメンバーの位置の凸包の面積が最も大きなチームが勝ちとなる.
</p>
<p>
あなたは $N$ 匹のうさぎからなるチームの監督である. $i$ 匹目のうさぎは 1 分で $r_i$ 移動することができる. このチームが最適な戦略をとった場合の, 1 分後の凸包の面積の最大値を求めよ.
</p>
<h3>Constraints</h3>
<ul>
<li>$N$ will be between 3 and 8, inclusive.</li>
<li>$r_i$ will be an integer between 1 and 1,000, inclusive.</li>
</ul>
<h3>Input</h3>
<p>
入力は以下の形式で与えられる:<br>
<br>
$N$<br>
$r_1$<br>
...<br>
$r_N$<br>
<br>
</p>
<h3>Output</h3>
<p>
凸包の面積の最大値を表す実数を 1 行に出力せよ. 小数点以下何桁出力してもよいが, 絶対誤差または相対誤差が $10^{-6}$ 以下のとき Accepted になる.
</p>
<h3>Sample Input 1</h3>
<pre>4
5
8
58
85</pre>
<h3>Sample Output 1</h3>
<pre>2970.000000000</pre>
<h3>Sample Input 2</h3>
<pre>6
1
1
1
1
1
1</pre>
<h3>Sample Output 2</h3>
<pre>2.598076211</pre> |
p00452 |
<H1>ダーツ </H1>
<h2>問題</h2>
<p>
あなたは以下のルールでダーツゲームをすることになった.
</p>
<p>
あなたは,矢を的(まと)に向かって 4 本まで投げることができる.必ずしも 4 本全てを投げる必要はなく,1 本も投げなくてもかまわない.的は N 個の部分に区切られていて,各々の部分に点数 P<sub>1</sub>,..., P<sub>N</sub> が書か れている.矢が刺さった場所の点数の合計 S があなたの得点の基礎となる.S があらかじめ決められたある点数 M 以下の場合は S がそのままあなたの得点となる.しかし,S が M を超えた場合はあなたの得点は 0 点となる.
</p>
<p>
的に書かれている点数と M の値が与えられたとき,あなたが得ることのできる点数の最大値を求めるプログラムを作成せよ.
</p>
<h2>入力</h2>
<p>
<!-- 入力ファイルのファイル名は input.txt である.-->
入力は複数のデータセットからなる.各データセットは以下の形式で与えられる.
</p>
<p>
1 行目には,整数 N (1 ≤ N ≤ 1000) と M (1 ≤ M ≤ 200000000 = 2 × 10<sup>8</sup>)がこの順に空白区切りで書かれている.2 行目以降の第 1 + i 行目 (1 ≤ i ≤ N ) には, P<sub>i</sub> (1 ≤ P<sub>i</sub> ≤ 100000000 = 10<sup>8</sup> ) が書かれている.
</p>
<p>
採点用データのうち, 配点の 20% 分は N ≤ 100 を満たし,配点の 50% 分はN ≤ 300 を満たす.
</p>
<p>
N, M がともに 0 のとき入力の終了を示す. データセットの数は 10 を超えない.
</p>
<h2>出力</h2>
<p>
<!--
出力ファイルのファイル名は output.txt である.
output.txt は 1 行だけからなり,あなたが得ることのできる点数の最大値を含む.
-->
データセットごとにあなたが得ることのできる点数の最大値を1行に出力する.
</p>
<h2>入出力例</h2>
<h3>入力例</h3>
<pre>
4 50
3
14
15
9
3 21
16
11
2
0 0
</pre>
<h3>出力例</h3>
<pre>
48
20
</pre>
<p>
1つ目の例では,15 点の部分に 3 本,3 点の部分に 1 本の矢が刺さった場合にあなたの得点は最大になり,その得点は 48 点である.
</p>
<p>
2つ目の例では,16 点の場所に 1 本,2 点の場所に 2 本の矢が刺さった場合にあなたの得点は最大になり,その得点は 20 点である.
</p>
<div class="source">
<p class="source">
上記問題文と自動審判に使われるデータは、<a href="http://www.ioi-jp.org">情報オリンピック日本委員会</a>が作成し公開している問題文と採点用テストデータです。
</p>
</div>
|
p02195 | <h2>Bichrome Tree Connectivity</h2>
<p>木が与えられます。</p>
<p>はじめ、すべての頂点は白いです。</p>
<p>白い頂点の色を反転すると黒になり、黒い頂点の色を反転すると白になります。</p>
<p>二種類のクエリを処理してください。</p>
<p>一種類目のクエリでは、頂点<var>v</var>の色を反転します。</p>
<p>二種類目のクエリでは、白い頂点<var>v</var>から白い頂点とそれらを結ぶ辺だけを使ってたどり着ける頂点の個数を答えます。</p>
<h3>入力</h3>
<pre>
<var>N Q</var>
<var>a_1 b_1</var>
<var>a_2 b_2</var>
:
<var>a_{n-1} b_{n-1}</var>
<var>t_1 v_1</var>
<var>t_2 v_2</var>
:
<var>t_q v_q</var>
</pre>
<p><var>t_i</var>が<var>1</var>のとき一種類目のクエリ、<var>2</var>のとき二種類目のクエリであることを表す。</p>
<h3>出力</h3>
<pre>
<var>ans_1</var>
<var>ans_2</var>
:
<var>ans_k</var>
</pre>
<p>二種類目のクエリに対する答えを順に出力せよ。</p>
<h3>制約</h3>
<ul>
<li><var>1 \leq N,Q \leq 10^5 </var></li>
<li><var>1 \leq a_i,b_i \leq N</var></li>
<li><var>1 \leq t_i \leq 2</var></li>
<li><var>1 \leq v_i \leq N</var></li>
<li>与えられるグラフは木である。</li>
<li><var>t_i=2</var>のとき、頂点<var>v_i</var>は必ず白である。</li>
</ul>
<h3>入力例</h3>
<pre>
10 3
1 2
2 5
2 6
1 4
1 3
3 7
3 8
3 9
9 10
1 3
2 1
2 8
</pre>
<h3>出力例</h3>
<pre>
5
1
</pre>
|
p00002 |
<H1>Digit Number</H1>
<p>
Write a program which computes the digit number of sum of two integers <var>a</var> and <var>b</var>.
</p>
<H2>Input</H2>
<p>
There are several test cases. Each test case consists of two non-negative integers <var>a</var> and <i>b</i> which are separeted by a space in a line. The input terminates with EOF.
</p>
<h2>Constraints</h2>
<ul>
<li>0 ≤ <var>a</var>, <var>b</var> ≤ 1,000,000</li>
<li>The number of datasets ≤ 200</li>
</ul>
<H2>Output</H2>
<p>
Print the number of digits of <var>a</var> + <var>b</var> for each data set.
</p>
<H2>Sample Input</H2>
<pre>
5 7
1 99
1000 999
</pre>
<H2>Output for the Sample Input</H2>
<pre>
2
3
4
</pre>
|
p01243 |
<H1><font color="#000">Problem J:</font> Castle Wall</H1>
<p>
A new lord assumed the position by the death of the previous lord in a Far Eastern province.
</p>
<p>
The new greedy lord hates concave polygons, because he believes they need much wasted area to be drawn on
paper. He always wants to modify them to convex ones.
</p>
<p>
His castle is currently surrounded by a wall forming a concave polygon, when seen from the above. Of course
he hates it. He believes more area could be obtained with a wall of a convex polygon. Thus he has ordered his
vassals to have new walls built so they form a convex polygon.
</p>
<p>
Unfortunately, there is a limit in the budget. So it might be infeasible to have the new walls built completely. The
vassals has found out that only up to r meters of walls in total can be built within the budget. In addition, the new
walls must be built in such a way they connect the polygonal vertices of the present castle wall. It is impossible
to build both of intersecting walls.
</p>
<p>
After long persuasion of the vassals, the new lord has reluctantly accepted that the new walls might not be built
completely. However, the vassals still want to maximize the area enclosed with the present and new castle walls,
so they can satisfy the lord as much as possible.
</p>
<p>
Your job is to write a program to calculate, for a given integer <i>r</i>, the maximum possible area of the castle with
the new walls.
</p>
<H2>Input</H2>
<p>
The input file contains several test cases.
</p>
<p>
Each case begins with a line containing two positive integers <i>n</i> and <i>r</i>. <i>n</i> is the number of vertices of the concave
polygon that describes the present castle wall, satisfying 5 ≤ <i>n</i> ≤ 64. <i>r</i> is the maximum total length of new castle
walls feasible within the budget, satisfying 0 ≤ <i>r</i> ≤ 400.
</p>
<p>
The subsequent <i>n</i> lines are the <i>x</i>- and <i>y</i>-coordinates of the n vertices. The line segments (<i>x<sub>i</sub></i>, <i>y<sub>i</sub></i>) - (<i>x</i><sub><i>i</i>+1</sub>, <i>y</i><sub><i>i</i>+1</sub>)
(1 ≤ <i>i</i> ≤ <i>n</i> - 1) and (<i>x<sub>n</sub></i>, <i>y<sub>n</sub></i>) - (<i>x</i><sub>1</sub>, <i>y</i><sub>1</sub>) form the present castle wall of the concave polygon. Those coordinates are
given in meters and in the counterclockwise order of the vertices.
</p>
<p>
All coordinate values are integers between 0 and 100, inclusive. You can assume that the concave polygon is
simple, that is, the present castle wall never crosses or touches itself.
</p>
<p>
The last test case is followed by a line containing two zeros.
</p>
<H2>Output</H2>
<p>
For each test case in the input, print the case number (beginning with 1) and the maximum possible area enclosed
with the present and new castle walls. The area should be printed with exactly one fractional digit.
</p>
<H2>Sample Input</H2>
<pre>
5 4
0 0
4 0
4 4
2 2
0 4
8 80
45 41
70 31
86 61
72 64
80 79
40 80
8 94
28 22
0 0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
case 1: 16.0
case 2: 3375.0
</pre>
|
p02896 | <span class="lang-en">
<p>Score : <var>2000</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>For each of the <var>K^{NM}</var> ways to write an integer between <var>1</var> and <var>K</var> (inclusive) in every square in a square grid with <var>N</var> rows and <var>M</var> columns, find the value defined below, then compute the sum of all those <var>K^{NM}</var> values, modulo <var>D</var>.</p>
<ul>
<li>For each of the <var>NM</var> squares, find the minimum among the <var>N+M-1</var> integers written in the square's row or the square's column. The value defined for the grid is the product of all these <var>NM</var> values.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N,M,K \leq 100</var></li>
<li><var>10^8 \leq D \leq 10^9</var></li>
<li><var>N,M,K,</var> and <var>D</var> are integers.</li>
<li><var>D</var> is prime.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>M</var> <var>K</var> <var>D</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the sum of the <var>K^{NM}</var> values, modulo <var>D</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 2 2 998244353
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>35
</pre>
<p>We have <var>1</var> way to write integers such that the product of the <var>NM</var> values is <var>16</var>, <var>4</var> ways such that the product is <var>2</var>, and <var>11</var> ways such that the product is <var>1</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2 3 4 998244353
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>127090
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>31 41 59 998244353
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>827794103
</pre></section>
</div>
</span> |
p04041 | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p><em>Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order.</em></p>
<p>Iroha is looking for <em><var>X,Y,Z</var>-Haiku</em> (defined below) in integer sequences.</p>
<p>Consider all integer sequences of length <var>N</var> whose elements are between <var>1</var> and <var>10</var>, inclusive. Out of those <var>10^N</var> sequences, how many contain an <var>X,Y,Z</var>-Haiku?</p>
<p>Here, an integer sequence <var>a_0, a_1, ..., a_{N-1}</var> is said to <em>contain an <var>X,Y,Z</var>-Haiku</em> if and only if there exist four indices <var>x, y, z, w (0 ≦ x < y < z < w ≦ N)</var> such that all of the following are satisfied:</p>
<ul>
<li><var>a_x + a_{x+1} + ... + a_{y-1} = X</var></li>
<li><var>a_y + a_{y+1} + ... + a_{z-1} = Y</var></li>
<li><var>a_z + a_{z+1} + ... + a_{w-1} = Z</var></li>
</ul>
<p>Since the answer can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>3 ≦ N ≦ 40</var></li>
<li><var>1 ≦ X ≦ 5</var></li>
<li><var>1 ≦ Y ≦ 7</var></li>
<li><var>1 ≦ Z ≦ 5</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>X</var> <var>Y</var> <var>Z</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of the sequences that contain an <var>X,Y,Z</var>-Haiku, modulo <var>10^9+7</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 5 7 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
</pre>
<p>Here, the only sequence that contains a <var>5,7,5</var>-Haiku is <var>[5, 7, 5]</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4 5 7 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>34
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>37 4 2 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>863912418
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>40 5 7 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>562805100
</pre></section>
</div>
</span> |
p03784 | <span class="lang-en">
<p>Score : <var>1300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p><font color="red"><strong>This is an interactive task.</strong></font></p>
<p>AtCoDeer the deer came across <var>N</var> people. For convenience, the people are numbered <var>0</var> through <var>N-1</var>.
Among them, <var>A</var> are <em>honest</em> and the remaining <var>B(=N-A)</var> are <em>unkind</em>.
All of these <var>N</var> people know who are honest and who are unkind, but AtCoDeer only knows that there are <var>A</var> honest and <var>B</var> unkind people.
He is trying to identify all of the honest people by asking questions to these <var>N</var> people.
For one question, AtCoDeer selects <var>a</var> and <var>b</var> <var>(0≤a,b≤N-1)</var>, and asks person <var>a</var> the following question: "Is person <var>b</var> honest?"</p>
<p>An honest person will always answer correctly by "Yes" or "No".
An unkind person, however, will answer by selecting "Yes" or "No" <strong>arbitrarily</strong>.
That is, the algorithm used by an unkind person may not be simple one such as always lying or giving random fifty-fifty answers.</p>
<p>AtCoDeer can ask at most <var>2N</var> questions. He will ask questions one by one, and the responses to the previous questions can be used when deciding the next question to ask.</p>
<p>Identify all of the honest people.
If it is impossible (more formally, if, for any strategy of asking <var>2N</var> questions, there exists a strategy for unkind people to answer the questions so that there are two or more possible sets of the honest people), report that fact.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1≤A,B≤2000</var></li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Input and Output</h3><p>First, <var>A</var> and <var>B</var> are given from Standard Input in the following format:</p>
<pre><var>A</var> <var>B</var>
</pre>
<p>If identifying the honest people is impossible, the program must immediately print the following output and terminate itself:</p>
<pre>Impossible
</pre>
<p>Otherwise, the program shall ask questions.
Each question must be written to Standard Output in the following format:</p>
<pre>? <var>a</var> <var>b</var>
</pre>
<p>Here, <var>a</var> and <var>b</var> must be integers between <var>0</var> and <var>N-1</var> (inclusive).
The response to the question will be given from Standard Input in the following format:</p>
<pre><var>ans</var>
</pre>
<p>Here, <var>ans</var> is either <code>Y</code> or <code>N</code>.
<code>Y</code> represents "Yes"; <code>N</code> represents "No".</p>
<p>Finally, the answer must be written to Standard Output in the following format:</p>
<pre>! <var>s_0s_1...s_{N-1}</var>
</pre>
<p>Here, <var>s_i</var> must be <code>1</code> if person <var>i</var> is honest, and <code>0</code> if person <var>i</var> is unkind.</p>
</section>
</div>
<div class="part">
<section>
<h3>Judgement</h3><ul>
<li><font color="red"><strong>After each output, you must flush Standard Output.</strong></font> Otherwise you may get <code>TLE</code>.</li>
<li>After you print the answer, the program must be terminated immediately. Otherwise, the behavior of the judge is undefined.</li>
<li>When your output is invalid or incorrect, the behavior of the judge is undefined (it does not necessarily give <code>WA</code>).</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Samples</h3><p>In the following sample, <var>A = 2</var>, <var>B = 1</var>, and the answer is <code>101</code>.</p>
<table class="table table-bordered">
<thead>
<tr>
<th align="left">Input</th>
<th align="left">Output</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><var>2</var> <var>1</var></td>
<td align="left"></td>
</tr>
<tr>
<td align="left"></td>
<td align="left"><var>?</var> <var>0</var> <var>1</var></td>
</tr>
<tr>
<td align="left"><var>N</var></td>
<td align="left"></td>
</tr>
<tr>
<td align="left"></td>
<td align="left"><var>?</var> <var>0</var> <var>2</var></td>
</tr>
<tr>
<td align="left"><var>Y</var></td>
<td align="left"></td>
</tr>
<tr>
<td align="left"></td>
<td align="left"><var>?</var> <var>1</var> <var>0</var></td>
</tr>
<tr>
<td align="left"><var>Y</var></td>
<td align="left"></td>
</tr>
<tr>
<td align="left"></td>
<td align="left"><var>?</var> <var>2</var> <var>0</var></td>
</tr>
<tr>
<td align="left"><var>Y</var></td>
<td align="left"></td>
</tr>
<tr>
<td align="left"></td>
<td align="left"><var>?</var> <var>2</var> <var>2</var></td>
</tr>
<tr>
<td align="left"><var>Y</var></td>
<td align="left"></td>
</tr>
<tr>
<td align="left"></td>
<td align="left"><var>!</var> <var>101</var></td>
</tr>
</tbody>
</table>
<p>In the following sample, <var>A = 1</var>, <var>B = 2</var>, and the answer is <code>Impossible</code>.</p>
<table class="table table-bordered">
<thead>
<tr>
<th align="left">Input</th>
<th align="left">Output</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left"><var>1</var> <var>2</var></td>
<td align="left"></td>
</tr>
<tr>
<td align="left"></td>
<td align="left"><var>Impossible</var></td>
</tr>
</tbody>
</table></section>
</div>
</span> |
p00901 |
<H1><font color="#000">Problem H: </font>ASCII Expression</H1>
<p>
Mathematical expressions appearing in old papers and old technical articles are printed with typewriter in several lines, where a fixed-width or monospaced font is required to print characters (digits, symbols and spaces). Let us consider the following mathematical expression.
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_1322_1">
</center>
<p>
It is printed in the following four lines:
</p>
<pre>
4 2
( 1 - ---- ) * - 5 + 6
2
3
</pre>
<p>
where “- 5” indicates unary minus followed by 5. We call such an expression of lines “ASCII expression”.
</p>
<p>
For helping those who want to evaluate ASCII expressions obtained through optical character recognition (OCR) from old papers, your job is to write a program that recognizes the structure of ASCII expressions and computes their values.
</p>
<p>
For the sake of simplicity, you may assume that ASCII expressions are constructed by the following rules. Its syntax is shown in Table H.1.
</p>
<table>
<tr>
<td valign="top" width="28">(1)</td>
<td>Terminal symbols are ‘<span>0</span>’, ‘<span>1</span>’, ‘<span>2</span>’, ‘<span>3</span>’, ‘<span>4</span>’, ‘<span>5</span>’, ‘<span>6</span>’, ‘<span>7</span>’, ‘<span>8</span>’, ‘<span>9</span>’, ‘<span>+</span>’, ‘<span>-</span>’, ‘<span>*</span>’, ‘<span>(</span>’, ‘<span>)</span>’, and ‘<span> </span>’.</td>
</tr>
<tr>
<td valign="top">(2)</td>
<td>Nonterminal symbols are <i>expr</i>, <i>term</i>, <i>factor</i>, <i>powexpr</i>, <i>primary</i>, <i>fraction</i> and <i>digit</i>. The start symbol is <i>expr</i>.</td>
</tr>
<tr>
<td valign="top">(3)</td>
<td>A “cell” is a rectangular region of characters that corresponds to a terminal or nonterminal symbol (Figure H.1). In the cell, there are no redundant rows and columns that consist only of space characters. A cell corresponding to a terminal symbol consists of a single character. A cell corresponding to a nonterminal symbol contains cell(s) corresponding to its descendant(s) but never partially overlaps others.</td>
</tr>
<tr>
<td valign="top">(4)</td>
<td>Each cell has a base-line, a top-line, and a bottom-line. The base-lines of child cells of the right-hand side of rules I, II, III, and V should be aligned. Their vertical position defines the base-line position of their left-hand side cell.<br>
Table H.1: Rules for constructing ASCII expressions (similar to Backus-Naur Form) The box indicates the cell of the terminal or nonterminal symbol that corresponds to a rectan- gular region of characters. Note that each syntactically-needed space character is explicitly
indicated by the period character denoted by, here.</td>
</tr>
<tr>
<td colspan="2"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_1322_2">
</td>
</tr>
<tr>
<td valign="top">(5)</td>
<td><i>powexpr</i> consists of a <i>primary</i> and an optional <i>digit</i>. The <i>digit</i> is placed one line above the base-line of the <i>primary cell. They are horizontally adjacent to each other. The base-line of a <i>powexpr</i> is that of the <i>primary</i>.</td>
</tr>
<tr>
<td valign="top">(6)</td>
<td><i>fraction</i> is indicated by three or more consecutive hyphens called “vinculum”. Its dividend expr is placed just above the vinculum, and its divisor expr is placed just beneath it. The number of the hyphens of the vinculum, denoted by <i>w<sub>h</sub></i>, is equal to 2 + max(<i>w<sub>1</sub></i>, <i>w<sub>2</sub></i>), where <i>w<sub>1</sub></i> and <i>w<sub>2</sub></i> indicate the width of the cell of the dividend and that of the divisor, respectively. These cells are centered, where there are ⌈(<i>w<sub>h</sub></i>−<i>w<sub>k</sub></i>)/2⌉ space characters to the left and ⌊(<i>w<sub>h</sub></i>−<i>w<sub>k</sub></i>)/2⌋ space characters to the right, (<i>k</i> = 1, 2). The base-line of a fraction is at the position of the vinculum.</td>
<tr>
<td valign="top">(7)</td>
<td><i>digit</i> consists of one character.</td>
</tr>
</table>
<br>
<p>
For example, the negative fraction<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_1322_3"> is represented in three lines:
</p>
<pre> 3
- ---
4</pre>
<p>where the left-most hyphen means a unary minus operator. One space character is required between the unary minus and the vinculum of the fraction.</p>
<p>The fraction <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_1322_4"> is represented in four lines:</p>
<pre>
3 + 4 * - 2
-------------
2
- 1 - 2
</pre>
<p>
where the widths of the cells of the dividend and divisor are 11 and 8 respectively. Hence the number of hyphens of the vinculum is 2 + max(11, 8) = 13. The divisor is centered by ⌈(13−8)/2⌉ = 3 space characters (hyphens) to the left and ⌊(13−8)/2⌋ = 2 to the right.
</p>
<p>
The <i>powexpr</i> (4<sup>2</sup>)<sup>3</sup> is represented in two lines:
</p>
<pre>
2 3
( 4 )
</pre>
<p>
where the cell for 2 is placed one line above the base-line of the cell for 4, and the cell for 3 is placed one line above the base-line of the cell for a primary (4<sup>2</sup>).
</p>
<H2>Input</H2>
<p>
The input consists of multiple datasets, followed by a line containing a zero. Each dataset has the following format.
</p>
<pre>
n
str<sub>1</sub>
str<sub>2</sub>
.
.
.
str<sub>n</sub>
</pre>
<p>
<i>n</i> is a positive integer, which indicates the number of the following lines with the same length that represent the cell of an ASCII expression. <i>str<sub>k</sub></i> is the <i>k</i>-th line of the cell where each space character is replaced with a period.
</p>
<p>
You may assume that <i>n</i> ≤ 20 and that the length of the lines is no more than 80.
</p>
<H2>Output</H2>
<p>
For each dataset, one line containing a non-negative integer less than 2011 should be output. The integer indicates the value of the ASCII expression in modular arithmetic under modulo 2011. The output should not contain any other characters.
</p>
<p>
There is no <i>fraction</i> with the divisor that is equal to zero or a multiple of 2011.
</p>
<p>
Note that powexpr <i>x<sup>0</sup></i> is defined as 1, and <i>x<sup>y</sup></i> (y is a positive integer) is defined as the product <i>x×x×</i>...×<i>x</i> where the number of <i>x</i>'s is equal to <i>y</i>.
</p>
<p>
A fraction<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_1322_5">is computed as the multiplication of <i>x</i> and the inverse of <i>y</i>, i.e., <i>x</i>× inv(<i>y</i>), under <i>y</i> modulo 2011. The inverse of <i>y</i> (1 ≤ y < 2011) is uniquely defined as the integer <i>z</i> (1 ≤ z < 2011) that satisfies <i>z</i> × <i>y</i> ≡ 1 (mod 2011), since 2011 is a prime number.
</p>
<H2>Sample Input</H2>
<pre>
4
........4...2..........
(.1.-.----.)..*.-.5.+.6
........2..............
.......3...............
3
...3.
-.---
...4.
4
.3.+.4.*.-.2.
-------------
..........2..
...-.1.-.2...
2
...2..3
(.4..).
1
2.+.3.*.5.-.7.+.9
1
(.2.+.3.).*.(.5.-.7.).+.9
3
.2....3.
4..+.---
......5.
3
.2......-.-.3.
4..-.-.-------
..........5...
9
............1............
-------------------------
..............1..........
.1.+.-------------------.
................1........
......1.+.-------------..
..................1......
...........1.+.-------...
................1.+.2....
15
.................2......
................---.....
.......2.........5....3.
.(.---------.+.-----.)..
.....7...........3......
....---.+.1.............
.....4..................
------------------------
.......2................
......---...............
.......5.......2....2...
...(.-----.+.-----.)....
.......3.......3........
..............---.......
...............4........
2
.0....2....
3..+.4..*.5
20
............2............................2......................................
...........3............................3.......................................
..........----.........................----.....................................
............4............................4......................................
.....2.+.------.+.1...............2.+.------.+.1................................
............2............................2......................................
...........2............................2........................2..............
..........----.........................----.....................3...............
............2............................2.....................----.............
...........3............................3........................4..............
(.(.----------------.+.2.+.3.).*.----------------.+.2.).*.2.+.------.+.1.+.2.*.5
............2............................2.......................2..............
...........5............................5.......................2...............
..........----.........................----....................----.............
............6............................6.......................2..............
.........------.......................------....................3...............
............3............................3......................................
..........----.........................----.....................................
............2............................2......................................
...........7............................7.......................................
0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
501
502
1
74
19
2010
821
821
1646
148
81
1933
</pre> |
p01613 |
<h1>A: Grid Mori / グリッド森</h1>
<p>とある富豪の森さんが,グリッド状に区分けされた土地の <em>n</em> 区画を買って, 4つの工場 A, B, C, D を建てようとしている.</p>
<p>まず,土地全体の幅 <em>w</em> を1以上 <em>n</em> 以下の値で決定する. この土地の 1 番左上の区画を (0, 0) とする. そして, (0, 0) の区画を1番目に, (1, 0) の区画を2番目に購入する. このように, <em>i</em> 番目に (<em>x</em>, <em>y</em>) の区画を購入した後に, <em>i</em> + 1 番目に (<em>x</em> + 1, <em>y</em>) の区画を購入していく. もし, (<em>w</em>, <em>y</em>) の区画を購入しようとしたときは, 代わりに (0, <em>y</em> + 1) の区画を購入する.</p>
<p>さらに森さんは,土地を購入する前に占い師に次のようなことを言われていた.</p>
<p>「<em>a</em> 番目に購入するグリッドの土地に工場Aを,<em>b</em> 番目に購入するグリッドに工場Bを,<em>c</em> 番目のグリッドに工場Cを,<em>d</em> 番目のグリッドに工場Dを建てるとよいぞ.」</p>
<p>森さんは,非常に占い好きであり,必ず占い師の助言を守りたいと考えている. しかし,AとB,CとDの2つのペアは関連のある工場であるため,できるだけ近くにあった方がよい. 自分の土地の買い方と占い師の助言を両方守りつつ,関連する工場同士をなるべく近くしたいのである.</p>
<p>ここで,2つの土地が (<em>x</em><sub>1</sub>, <em>y</em><sub>1</sub>),(<em>x</em><sub>2</sub>, <em>y</em><sub>2</sub>) にあるとき, 距離は,|<em>x</em><sub>1</sub> - <em>x</em><sub>2</sub> |+ |<em>y</em><sub>1</sub> - <em>y</em><sub>2</sub> | で求めることができる. |<em>x</em>| は, <em>x</em> の絶対値を表している.</p>
<p>あなたの仕事は, (工場AとBの距離) + (工場CとDの距離) の値が最小になるように土地を購入したときの距離の合計を求めるプログラムを作成することである.</p>
<p>図1はサンプルの1番目における,すべての工場の配置の組み合わせである.幅3で土地を購入したとき,距離の合計が最小となる.</p>
<div align="center">
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE3_Rits_Camp13_Day3_A_A_sample1" style="margin-bottom: 8px" alt="A_sample1.png" />
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE3_Rits_Camp13_Day3_A_A_sample2" alt="A_sample2.png" />
<p><strong>図1:</strong> サンプル1の工場の配置</p>
</div>
<div>
<h1>Input</h1>
<p>入力は次の形式で表される.</p>
<div>
<em>n</em><br />
<em>a</em> <em>b</em><br />
<em>c</em> <em>d</em><br />
</div>
<br/>
<p>入力は,全て整数値である. <em>n</em> (4 ≦ <em>n</em> ≦ 100) はこれから購入する土地のグリッド数を表す. <em>a</em>, <em>b</em>, <em>c</em>, <em>d</em> (1 ≦ <em>a</em>, <em>b</em>, <em>c</em>, <em>d</em> ≦ <em>n</em>) は,それぞれ工場A, B, C, Dを何番目に購入したグリッドの土地に建てるかを表す. このとき,<em>a</em>, <em>b</em>, <em>c</em>, <em>d</em>は全て,異なる数が入力される.</p>
</div>
<div>
<h1>Output</h1>
<p>森さんが,<em>a, b</em> 間と <em>c, d</em> 間の距離の合計が最小となるように土地を購入したときの合計値を1行で出力せよ.</p>
</div>
<div>
<h1>Sample Input 1</h1>
<pre>
5
1 4
2 5
</pre>
</div>
<div>
<h1>Sample Output 1</h1>
<pre>
2
</pre>
</div>
<div>
<h1>Sample Input 2</h1>
<pre>
16
2 9
14 8
</pre>
</div>
<div>
<h1>Sample Output 2</h1>
<pre>
3
</pre>
</div> |
p01306 |
<h1><font color="#000">Problem D:</font> 単位変換器</h1>
<p>
国際単位系(SI)では、kilo, mega, giga などの接頭辞を使って、「数値 + 接頭辞 + 単位」という形でさまざまな物理量を表記する。たとえば、「3.5 kilometers」, 「5.1 milligrams」 といった具合である。
</p>
<p>
一方、これらの物理量は指数表記を使って「3.5 * 10^3 meters」、「5.1 * 10^-3 grams」のように表すことも可能である。
</p>
<p>
物理量の測定は必ず誤差を含む。そこで、測定された物理量がどれだけの精度を持つかを表すために、有効数字という概念がある。有効数字を考慮した表記では、最後の桁は誤差を含みうるが、それ以外の桁は信頼できるものと考える。たとえば、「1.23」 と表記した場合、真の値は 1.225 以上 1.235 未満であり、小数第1位以上の桁は信頼できるが、小数第2位には誤差を含む。このように「信頼できる桁 + 誤差を含む1桁」という形で物理量を表したときの桁数を有効桁数と呼ぶ。たとえば、「1.23」の有効桁数は3桁である。
</p>
<p>
0でない最上位の桁より前に0がある場合、その0は有効桁数には含めない。たとえば、「0.45」の有効桁数は2桁である。 0でない最下位の桁より後に0がある場合、その0を有効桁数に含むかどうかは小数点の位置により異なる。小数点より右まで0があるときは、その0は有効桁数に含める。たとえば、「12.300」の有効桁数は5桁である。一方、「12300」のように、小数点より右に0がないときは、右側の0を有効桁数に含めるかどうかは明確ではないが、この問題においては含めることとする。すなわち、「12300」の有効桁数は5桁である。
</p>
<p>
なつめは学校の課題で物理の問題を出された。その中で有効数字や単位が関わる計算をしなければならないのだが、困ったことになつめには有効数字や単位の使い方がまだよくわかっていない。そこで、自動でそれらの計算をするプログラムを作って、なつめを助けてあげてほしい。
</p>
<p>
あなたが書くのは、接頭辞を使った表記が与えられたとき、それを有効桁数が等しい指数表記に変換するプログラムである。使われる接頭辞は以下の20個である。
</p>
<ul>
<li>yotta = 10^24
<li> zetta = 10^21
<li> exa = 10^18
<li> peta = 10^15
<li> tera = 10^12
<li> giga = 10^9
<li> mega = 10^6
<li> kilo = 10^3
<li> hecto = 10^2
<li> deca = 10^1
<li> deci = 10^-1
<li> centi = 10^-2
<li> milli = 10^-3
<li> micro = 10^-6
<li> nano = 10^-9
<li> pico = 10^-12
<li> femto = 10^-15
<li> ato = 10^-18
<li> zepto = 10^-21
<li> yocto = 10^-24
</ul>
<h2>Input</h2>
<p>
入力は1行のみからなり、その唯一の行には、数値、(もしあれば)単位接頭辞、単位が含まれている。それぞれの間は空白1個で区切られている。単位接頭辞がない場合もあり、その場合は数値と単位のみが行に含まれる。単位接頭辞と同じ名前の単位は現れない。小数が与えられる場合の1の位を除き、最上位の桁が0になることはない。与えられる数値は正である。与えられる数値は小数点を含め1000桁以下で、単位の名前は50文字以下である。
</p>
<h2>Output</h2>
<p>
<var>a</var> * 10^<var>b</var> [単位] の形で指数表記で表現された量を出力せよ。ただし、1 <= <var>a</var> < 10 である。単位名の単数形・複数形の違いは考慮しない。入力に与えられた単位名をそのまま出力せよ。
</p>
<h2>Notes on Submission</h2>
<p>
上記形式で複数のデータセットが与えられます。入力データの 1 行目にデータセットの数が与えられます。各データセットに対する出力を上記形式で順番に出力するプログラムを作成して下さい。
</p>
<h2>Sample Input</h2>
<pre>
7
12.3 kilo meters
0.45 mega watts
0.000000000000000000000001 yotta grams
1000000000000000000000000 yocto seconds
42 amperes
0.42 joules
1234.56789012345678901234567890 hecto pascals
</pre>
<h2>Output for the Sample Input</h2>
<pre>
1.23 * 10^4 meters
4.5 * 10^5 watts
1 * 10^0 grams
1.000000000000000000000000 * 10^0 seconds
4.2 * 10^1 amperes
4.2 * 10^-1 joules
1.23456789012345678901234567890 * 10^5 pascals
</pre> |
p03291 | <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>The <em>ABC number</em> of a string <var>T</var> is the number of triples of integers <var>(i, j, k)</var> that satisfy all of the following conditions:</p>
<ul>
<li><var>1 ≤ i < j < k ≤ |T|</var> (<var>|T|</var> is the length of <var>T</var>.)</li>
<li><var>T_i =</var> <code>A</code> (<var>T_i</var> is the <var>i</var>-th character of <var>T</var> from the beginning.)</li>
<li><var>T_j =</var> <code>B</code></li>
<li><var>T_k =</var> <code>C</code></li>
</ul>
<p>For example, when <var>T =</var> <code>ABCBC</code>, there are three triples of integers <var>(i, j, k)</var> that satisfy the conditions: <var>(1, 2, 3), (1, 2, 5), (1, 4, 5)</var>. Thus, the ABC number of <var>T</var> is <var>3</var>.</p>
<p>You are given a string <var>S</var>. Each character of <var>S</var> is <code>A</code>, <code>B</code>, <code>C</code> or <code>?</code>.</p>
<p>Let <var>Q</var> be the number of occurrences of <code>?</code> in <var>S</var>. We can make <var>3^Q</var> strings by replacing each occurrence of <code>?</code> in <var>S</var> with <code>A</code>, <code>B</code> or <code>C</code>. Find the sum of the ABC numbers of all these strings.</p>
<p>This sum can be extremely large, so print the sum modulo <var>10^9 + 7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>3 ≤ |S| ≤ 10^5</var></li>
<li>Each character of <var>S</var> is <code>A</code>, <code>B</code>, <code>C</code> or <code>?</code>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>S</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the sum of the ABC numbers of all the <var>3^Q</var> strings, modulo <var>10^9 + 7</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>A??C
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>8
</pre>
<p>In this case, <var>Q = 2</var>, and we can make <var>3^Q = 9</var> strings by by replacing each occurrence of <code>?</code> with <code>A</code>, <code>B</code> or <code>C</code>. The ABC number of each of these strings is as follows:</p>
<ul>
<li><code>AAAC</code>: <var>0</var></li>
<li><code>AABC</code>: <var>2</var></li>
<li><code>AACC</code>: <var>0</var></li>
<li><code>ABAC</code>: <var>1</var></li>
<li><code>ABBC</code>: <var>2</var></li>
<li><code>ABCC</code>: <var>2</var></li>
<li><code>ACAC</code>: <var>0</var></li>
<li><code>ACBC</code>: <var>1</var></li>
<li><code>ACCC</code>: <var>0</var></li>
</ul>
<p>The sum of these is <var>0 + 2 + 0 + 1 + 2 + 2 + 0 + 1 + 0 = 8</var>, so we print <var>8</var> modulo <var>10^9 + 7</var>, that is, <var>8</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>ABCBC
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>3
</pre>
<p>When <var>Q = 0</var>, we print the ABC number of <var>S</var> itself, modulo <var>10^9 + 7</var>. This string is the same as the one given as an example in the problem statement, and its ABC number is <var>3</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>????C?????B??????A???????
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>979596887
</pre>
<p>In this case, the sum of the ABC numbers of all the <var>3^Q</var> strings is <var>2291979612924</var>, and we should print this number modulo <var>10^9 + 7</var>, that is, <var>979596887</var>.</p></section>
</div>
</span> |
p00844 |
<H1><font color="#000">Problem I:</font> Shy Polygons</H1>
<p>
You are given two solid polygons and their positions on the <i>xy</i>-plane. You can move one
of the two along the <i>x</i>-axis (they can overlap during the move). You cannot move it in other
directions. The goal is to place them as compactly as possible, subject to the following condition:
the distance between any point in one polygon and any point in the other must not be smaller
than a given minimum distance <i>L</i>.
</p>
<p>
We define the <i>width</i> of a placement as the difference between the maximum and the minimum
<i>x</i>-coordinates of all points in the two polygons.
</p>
<p>
Your job is to write a program to calculate the minimum width of placements satisfying the
above condition.
</p>
<p>
Let's see an example. If the polygons in Figure 13 are placed with <i>L</i> = 10.0, the result will be 100. Figure 14 shows one of the optimal placements.
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_shyPolygons1">
</center>
<H2>Input</H2>
<p>
The input consists of multiple datasets. Each dataset is given in the following format.
</p>
<pre>
<i>L</i>
<i>Polygon</i><sub>1</sub>
<i>Polygon</i><sub>2</sub>
</pre>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_shyPolygons2">
</center>
<p>
<i>L</i> is a decimal fraction, which means the required distance of two polygons. <i>L</i> is greater than
0.1 and less than 50.0.
</p>
<p>
The format of each polygon is as follows.
</p>
<pre>
<i>n</i>
<i>x</i><sub>1</sub> <i>y</i><sub>1</sub>
<i>x</i><sub>2</sub> <i>y</i><sub>2</sub>
.
.
.
<i>x</i><sub><i>n</i></sub> <i>y</i><sub><i>n</i></sub>
</pre>
<p>
<i>n</i> is a positive integer, which represents the number of vertices of the polygon. <i>n</i> is greater than
2 and less than 15.
</p>
<p>
Remaining lines represent the vertices of the polygon. A vertex data line has a pair of nonneg-
ative integers which represent the <i>x</i>- and y-coordinates of a vertex. <i>x</i>- and <i>y</i>-coordinates are
separated by a single space, and <i>y</i>-coordinate is immediately followed by a newline. <i>x</i> and <i>y</i> are
less than 500.
</p>
<p>
Edges of the polygon connect vertices given in two adjacent vertex data lines, and vertices given
in the last and the first vertex data lines. You may assume that the vertices are given in the
counterclockwise order, and the contours of polygons are simple, i.e. they do not cross nor touch
themselves.
</p>
<p>
Also, you may assume that the result is not sensitive to errors. In concrete terms, for a given
pair of polygons, the minimum width is a function of the given minimum distance <i>l</i>. Let us
denote the function <i>w</i>(<i>l</i>). Then you can assume that |<i>w</i>(<i>L</i> ± 10<sup>-7</sup>) - <i>w</i>(<i>L</i>)| < 10<sup>-4</sup>.
</p>
<p>
The end of the input is indicated by a line that only contains a zero. It is not a part of a dataset.
</p>
<H2>Output</H2>
<p>
The output should consist of a series of lines each containing a single decimal fraction. Each
number should indicate the minimum width for the corresponding dataset. The answer should
not have an error greater than 0.0001. You may output any number of digits after the decimal
point, provided that the above accuracy condition is satisfied.
</p>
<H2>Sample Input</H2>
<pre>
10.5235
3
0 0
100 100
0 100
4
0 50
20 50
20 80
0 80
10.0
4
120 45
140 35
140 65
120 55
8
0 0
100 0
100 100
0 100
0 55
80 90
80 10
0 45
10.0
3
0 0
1 0
0 1
3
0 100
1 101
0 101
10.0
3
0 0
1 0
0 100
3
0 50
100 50
0 51
0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
114.882476
100
1
110.5005
</pre>
|
p02879 | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Having learned the multiplication table, Takahashi can multiply two integers between <var>1</var> and <var>9</var> (inclusive) together. He cannot do any other calculation.</p>
<p>Given are two integers <var>A</var> and <var>B</var>.</p>
<p>If Takahashi can calculate <var>A \times B</var>, print the result; if he cannot, print <code>-1</code> instead.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq A \leq 20</var></li>
<li><var>1 \leq B \leq 20</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>A</var> <var>B</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If Takahashi can calculate <var>A \times B</var>, print the result; if he cannot, print <code>-1</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>10
</pre>
<p><var>2 \times 5 = 10</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5 10
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>-1
</pre>
<p><var>5\times 10 = 50</var>, but Takahashi cannot do this calculation, so print <code>-1</code> instead.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>9 9
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>81
</pre></section>
</div>
</span> |
p01756 |
<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>
文字列 <var>S</var> と, <var>m</var> 個のクエリが与えられる.<var>i</var> 番目のクエリは二つの文字列 <var>x<sub>i</sub>, y<sub>i</sub></var> で与えられる.
</p>
<p>
各クエリについて,文字列 <var>S</var> の部分文字列であり, <var>x<sub>i</sub></var> で始まり <var>y<sub>i</sub></var> で終わるものの中で,最長の長さを答えよ.
</p>
<p>
文字列 <var>S</var> について, <var>|S|</var> は <var>S</var> の長さを表す.また,文字列 <var>T</var> が文字列 <var>S</var> の部分文字列であるとは,ある整数 <var>i</var> が存在して, 1 ≤ <var>j</var> ≤ <var>|T|</var> に対して <var>T<sub>j</sub> = S<sub>i+j</sub></var> を満たすことを言う.ただし <var>T<sub>j</sub></var> は <var>T</var> の <var>j</var> 番目の文字を表す.
</p>
<h2>Input</h2>
<p>
入力は以下の形式で与えられる.
</p>
<pre>
<var>S</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>m</sub></var> <var>y<sub>m</sub></var>
</pre>
<ul>
<li> 1 行目には,文字列 <var>S</var> が与えられる.</li>
<li> 2 行目には,クエリの個数 <var>m</var> が与えられる.</li>
<li> 3 行目からの <var>m</var> 行のうち <var>i</var> 行目には <var>i</var> 番目のクエリ文字列 <var>x<sub>i</sub>, y<sub>i</sub></var> が空白区切りで与えられる.</li>
</ul>
<h2>Constraints</h2>
<ul>
<li> 1 ≤ <var>|S|</var> ≤ 2 × 10<sup>5</sup> </li>
<li> 1 ≤ <var>m</var> ≤ 10<sup>5</sup></li>
<li> 1 ≤ <var>|x<sub>i</sub>|, |y<sub>i</sub>|</var></li>
<li> $\sum^m_{i=1}$ (<var>|x<sub>i</sub>| + |y<sub>i</sub>|</var>) ≤ 2 × 10<sup>5</sup></li>
<li> <var>S</var> 及び <var>x<sub>i</sub>, y<sub>i</sub></var> は,半角の英小文字のみからなる.</li>
</ul>
<h2>Output</h2>
<p>
以下の形式で最大の部分文字列の長さを答えよ.
</p>
<pre>
<var>len<sub>1</sub></var>
<var>len<sub>2</sub></var>
:
<var>len<sub>m</sub></var>
</pre>
<p>
1 行目からの <var>m</var> 行のうち <var>i</var> 行目には, <var>i</var> 番目のクエリについて,条件を満たす最長の部分文字列の長さ <var>len<sub>i</sub></var> を出力せよ.そのような部分文字列がない場合,0 を出力せよ.
</p>
<h2>Sample Input 1</h2>
<pre>
abracadabra
5
ab a
a a
b c
ac ca
z z
</pre>
<h2>Output for the Sample Input 1</h2>
<pre>
11
11
4
3
0
</pre>
<p>
文字列 <var>S</var> として,abracadabra が与えられる.
</p>
<ul>
<li> "ab" で始まり "a" で終わる部分文字列は,"abra" や "abraca", "abracada", "abracadabra" の4 種類があるが,最長の部分文字列は "abracadabra" で,長さは 11 である.</li>
<li> "a" で始まり "a" で終わる最長の部分文字列も同様に "abracadabra" で,長さは 11 である.</li>
<li> "b" で始まり "c" で終わる最長の部分文字列は "brac" で,長さは 4 である.</li>
<li> "ac" で始まり "ca" で終わる最長の部分文字列は "aca" で,長さは 3 である.</li>
<li> "z" で始まり "z" で終わる部分文字列は存在しない.よって0 を出力する.</li>
</ul>
<h2>Sample Input 2</h2>
<pre>
howistheprogress
4
ist prog
s ss
how is
the progress
</pre>
<h2>Output for the Sample Input 2</h2>
<pre>
9
12
5
11
</pre>
<h2>Sample Input 3</h2>
<pre>
icpcsummertraining
9
mm m
icpc summer
train ing
summer mm
i c
i i
g g
train i
summer er
</pre>
<h2>Output for the Sample Input 3</h2>
<pre>
2
10
8
0
4
16
1
6
6
</pre>
|
p00517 |
<H1>問題 3: 超都観光 (Super Metropolis)
</H1>
<br/>
<h2>問題</h2>
<p>
JOI 君は,IOI 国にある超都という都市の観光ツアーを計画することになった.
</p>
<p>
超都は,南北方向にまっすぐに伸びる W 本の道路と,東西方向にまっすぐに伸びる H 本の道路により,碁盤の目の形に区分けされている.
</p>
<p>
南北方向の W 本の道路には,西から順に 1, 2, ... , W の番号が付けられている.また,東西方向の H 本の道路には,南から順に 1, 2, ... , H の番号が付けられている.西から i 番目の南北方向の道路と,南から j 番目の東西方向の道路との交差点を (i, j) で表す.
</p>
<p>
さらに,下図のように,各交差点からは1つ北東の交差点への道がある(最も北の道路上の交差点と最も東の道路上の交差点を除く).
また,1つ南西の交差点への道もある(最も南の道路上の交差点と最も西の道路上の交差点を除く).
すなわち,交差点 (i, j) からは,もし交差点 (i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1) があるときは,それらの交差点へ1本の道を使って行くことが出来る.それに加え,もし交差点 (i - 1, j - 1), (i + 1, j + 1) があるときは,それらの交差点へも1本の道を使って行くことが出来る.
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_2014-yo-t3-fig01">
</center>
<br/>
<p>
JOI 君はツアーの計画として既に N 個の観光スポットをどのような順番で訪れるかを決めている.
i 番目 (1 ≦ i ≦ N) に訪れる観光スポットは交差点 (X<sub>i</sub>, Y<sub>i</sub>) にある.
JOI 君は,ツアーにかかる時間をできるだけ短くするために,通らなければならない道の本数を少なくしたい.
観光スポットを事前に決めた順番で訪れるために通らなければならない道の本数の合計の最小値を求めるプログラムを作成せよ.
</p>
<p>
ただし,ツアーの開始地点は交差点 (X<sub>1</sub>, Y<sub>1</sub>) である.また,ツアーの途中で超都の外へ移動してはならないものとする.
また,JOI 君は,観光スポットのある交差点を,観光スポットを訪れずに通過することもできる.
</p>
<p>
「道の本数の合計」についての補足.ツアーの途中で同じ道を2回以上通ることもできる.その場合,「道の本数の合計」としては,その道については通った回数だけ重複して数えるものとする.
</p>
<h2>入力</h2>
<p>
入力は 1 + N 行からなる.
</p>
<p>
1 行目には,空白を区切りとして 3 つの整数 W, H, N (2 ≤ W ≤ 10000, 2 ≤ H ≤ 10000, 1 ≤ N ≤ 1000) が書かれている.
</p>
<p>
続く N 行のうちの i 行目 (1 ≤ i ≤ N) には,2 つの整数 X<sub>i</sub>, Y<sub>i</sub> (1 ≤ X<sub>i</sub> ≤ W, 1 ≤ Y<sub>i</sub> ≤ H) が空白を区切りとして書かれている.これは,i 番目に訪れる観光スポットのある交差点が (X<sub>i</sub>, Y<sub>i</sub>) であることを表す.
</p>
<h2>出力</h2>
<p>
観光スポットを順番に訪れるために通る道の本数の合計の最小値を 1 行で出力せよ.
</p>
<h2>入出力例</h2>
<h3>入力例 1</h3>
<pre>
4 3 3
1 1
3 3
4 1
</pre>
<h3>出力例 1</h3>
<pre>
5
</pre>
<p>
入出力例 1 では,例えば (1, 1), (2, 2), (3, 3), (3, 2), (4, 2), (4, 1) の順で交差点を訪れれば良い.
</p>
<h3>入力例 2</h3>
<pre>
4 3 5
1 3
4 3
2 2
2 2
1 3
</pre>
<h3>出力例 2</h3>
<pre>
7
</pre>
<p>
入出力例 2 のように,同じ交差点に複数回訪れることもある.
</p>
<div class="source">
<p class="source">
問題文と自動審判に使われるデータは、<a href="http://www.ioi-jp.org">情報オリンピック日本委員会</a>が作成し公開している問題文と採点用テストデータです。
</p>
</div>
|
p00147 |
<h1>福縞軒</h1>
<p>
「福縞軒」は行列のできる人気のラーメン屋です。でも最近、お客さんの間から「待ち時間が長いのに、店に入ったら空席があるのは許せない」という声が聞こえるようになってきました。どうしてそんな不満が出るのか調べたいのですが、お店が開いているあいだは忙しくて、実際の行列の様子を調べることができません。しかし、長年の経験からお客さんが来る間隔や人数は分かっているので、それをもとに待ち時間の分析をすることにしました。
</p>
<p>
店内にはカウンターに向かって 17 の席があります。開店時間は正午で、お客さんは次のようにやってきます。
</p>
<ul>
<li>0 番から 99 番までの 100 組のグループが来ます。</li>
<li><var>i</var> 番目のグループは正午から 5<var>i</var> 分後にお店に到着します。</li>
<li><var>i</var> 番目のグループの人数は <var>i</var> % 5 が 1 のとき 5 人、それ以外のときは 2 人です。<br>
(<var>x</var> % <var>y</var>は <var>x</var> を <var>y</var> で割ったときの余りを表わします。)</li>
<li><var>i</var> 番目のグループは、席に着くと 17(<var>i</var> % 2) + 3(<var>i</var> % 3) + 19 分間で食事を済ませます。</li>
</ul>
<p>
最初の 10 グループの到着時刻、人数、食事時間は次のようになります。
</p>
<center>
<table>
<tr><td width="140">グループ番号</td>
<td width="50">0 </td><td width="50"> 1 </td><td width="50"> 2</td><td width="50"> 3</td><td width="50"> 4</td><td width="50"> 5</td><td width="50"> 6</td><td width="50"> 7</td><td width="50"> 8</td><td width="50"> 9</td></tr>
<tr><td>到着時刻(分後)</td>
<td>0 </td><td>5</td><td> 10</td><td> 15</td><td> 20</td><td> 25</td><td> 30</td><td> 35</td><td> 40</td><td> 45</td></tr>
<tr><td>人数(人)</td>
<td>2 </td><td> 5</td><td> 2</td><td> 2</td><td> 2</td><td> 2</td><td> 5</td><td> 2</td><td> 2</td><td> 2</td></tr>
<tr><td>食事時間(分)</td>
<td>19</td><td> 39</td><td> 25</td><td> 36</td><td> 22</td><td> 42</td><td> 19</td><td> 39</td><td> 25</td><td> 36</td></tr>
</table>
</center>
<br/>
<p>
また、お客さんを席に案内するときには、次のようにしています。
</p>
<ul>
<li>席には 0 から 16 までの番号が付いています。</li>
<li><var>x</var> 人のグループは連続して <var>x</var> 個あいている席があった時だけ着席できます。</li>
</ul>
<p>
また、座れる場所が複数あった場合は、席の番号が最も小さくなるところに座ります。例えば、0、1、2、4、5番の席だけが空いていた場合、5 人のグループは着席できません。2 人のグループであれば 0、1 番に着席します。
</p>
<ul>
<li>一度着席したら、席を移動してもらうことはしません。</li>
<li>お客さんは 1 分単位で出入りします。各時刻には次の順序でお客さんを案内します。</li>
<ol>
<li>前のグループの離席と同時に次のグループの着席が可能となります。</li>
<li>お客さんを着席させる際には、行列の先頭にいるグループから順に、できる限り多くのグループを同じ時刻に着席させます。行列の順序を追い越すことはしません。つまり、先頭のグループが着席できなければ、行列内の他のグループが着席できたとしても、着席させません。</li>
<li>その時刻に到着したグループは、行列が残っている場合は行列の最後尾に並びます。行列が無く、着席できる場合は着席し、できない場合は並んで待ちます。例として最初の 10 グループが到着するまでの様子を示すと以下のようになります。各行の3つの欄は、左から時刻、座席の様子、行列の様子を示しています。座席は「_」が空席で、番号はその席にその番号のグループが座っていることを示しています。</li>
</ol>
</ul>
<pre>
時刻: 座席 行列
0: 00_______________:
5: 0011111__________:
10: 001111122________:
15: 00111112233______:
18: 00111112233______:
19: __111112233______:
20: 44111112233______:
25: 4411111223355____:
30: 4411111223355____: 66666 グループ6が到着
34: 4411111223355____: 66666
35: 4411111__3355____: 6666677 グループ7が到着
40: 4411111__3355____: 666667788 グループ8が到着
41: 4411111__3355____: 666667788
42: __11111__3355____: 666667788
43: __11111__3355____: 666667788
44: 6666677883355____: グループ6、7、8が着席
45: 666667788335599__: グループ9が到着、着席
</pre>
<p>
例えば、時刻 40 では 8 番目のグループが到着しますが、着席できないので行列に加わります。4 番目のグループは時刻 41 まで食事をします。時刻 42 では、4 番目のグループの席が空きますが、連続した席数が足りないので 6 番目のグループはまだ着席できません。1 番目のグループは時刻 43 まで食事をします。時刻 44 で 1 番目のグループの席が空くので、6 番目のグループが着席し、同時に 7 番目、8 番目のグループも着席します。9 番目のグループは時刻 45 で到着し、席が空いているのでそのまま着席します。
</p>
<p>
これらの情報を基にして、0 以上 99 以下の整数 <var>n</var> を入力として、<var>n</var> 番目グループのお客さんが待つ時間(分単位)を出力するプログラムを作成してください。
</p>
<H2>Input</H2>
<p>
複数のデータセットが与えられます。各データセットは1つの整数 <var>n</var> からなります。
</p>
<p>
データセットの数は 20 を超えません。
</p>
<H2>Output</H2>
<p>
各データセットについて、<var>n</var> 番目のお客の分単位の待ち時間(0 以上の整数)を1行に出力してください。
</p>
<H2>Sample Input</H2>
<pre>
5
6
7
8
</pre>
<H2>Output for the Sample Input</H2>
<pre>
0
14
9
4
</pre>
|
p03907 | <span class="lang-en">
<p>Score : <var>1500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There is an undirected connected graph with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
The lengths of all edges in this graph are <var>1</var>.
It is known that for each <var>i (1≦i≦N)</var>, the distance between vertex <var>1</var> and vertex <var>i</var> is <var>A_i</var>, and the distance between vertex <var>2</var> and vertex <var>i</var> is <var>B_i</var>.
Determine whether there exists such a graph.
If it exists, find the minimum possible number of edges in it.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦10^5</var></li>
<li><var>0≦A_i,B_i≦N-1</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_N</var> <var>B_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If there exists a graph satisfying the conditions, print the minimum possible number of edges in such a graph. Otherwise, print <code>-1</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4
0 1
1 0
1 1
2 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>4
</pre>
<p>The figure below shows two possible graphs. The graph on the right has fewer edges.</p>
<p><img alt="" src="https://atcoder.jp/img/code-festival-2016-final/dd1e04d837fd7fc1be56b231cd8c2a17.png"/></p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
0 1
1 0
2 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>-1
</pre>
<p>Such a graph does not exist.</p></section>
</div>
</span> |
p00628 |
<H1><font color="#000000">Problem P02:</font> Yes, I have a number</H1>
<p>
博士: ペーター君「Yes, I have a number」って知ってるかい?
</p>
<p>
ぺーター: このまえテレビでやってましたよ。文章に含まれる各単語の文字数で何かを覚えるというものですよね。
「Yes, I have a number」 だから、「3.14という数字」という意味になって円周率を覚えるためのキーワードになっているんですよね。
</p>
<p>
博士: ペーター君、それは違うんじゃ。
これは、3.1416 と解釈せねばならぬのじゃ。円周率は3.14159...だからのう。
</p>
<p>
ペーター: それじゃ、あの番組は日本が円周率を3.14と教えているからといって無理矢理省略したんですか!?
</p>
<p>
博士: 、、、。小学校で教える円周率がやっと3から3.14に戻っただけでもよしとしよう。
</p>
<p>
ペーター: そもそも、これで本当に覚えられるんですか?
</p>
<p>
博士: 日本人には難しいかもしれん。英語圏の人たちは使うようじゃよ、英語でゴロ合わせをするのは難しいからのぉ。
</p>
<p>
ペーター: それにしても、チェックするのが面倒くさそうですね。
</p>
<p>
博士: そこでじゃ、文章を単語の文字数の数列に変換するプログラムを作ってほしいのじゃ。
</p>
<p>
ペーター: 了解しました。詳しい仕様を教えてください博士。
</p>
<p>
博士: 1行の文章を入力としよう。簡単のためにアルファベットと空白のみを含む文章でよいぞ。
この文章について、空白・文頭・文末に挟まれた文字列の長さを順番に出力してくれ。
文字列の文字数は9を超えないと過程してよいぞ。
<br>
例えば、"Yes I have" の Yes は文頭と最初の空白で区切られているから文字数は 3 、 I は 1 つ目と 2 つ目の空白に区切られていて文字数は 1 じゃ。
</p>
<p>
博士: ・・・そうじゃ、空白が連続した場合の、文字数が 0 になる文字列も忘れずにな。
</p>
<H2>Input</H2>
<p>
複数のデータセットが入力として与えられます。各データセットで、アルファベットと空白を含む文字列が1行に与えられます。
</p>
<p>
文字列が "END OF INPUT" のとき、入力の終わりとします。この入力に対する出力を行ってはいけません。
</p>
<H2>Output</H2>
<p>
各データセットについて、文字列に対する文字数の数列を1行に出力して下さい。
</p>
<H2>Sample Input</H2>
<pre>
Yes I have a number
How I wish I could calculate an unused color for space
Thank you
END OF INPUT
</pre>
<H2>Output for the Sample Input</H2>
<pre>
31416
31415926535
53
</pre>
|
p02615 | <span class="lang-en">
<p>Score: <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3>
<p>Quickly after finishing the tutorial of the online game <em>ATChat</em>, you have decided to visit a particular place with <var>N-1</var> players who happen to be there. These <var>N</var> players, including you, are numbered <var>1</var> through <var>N</var>, and the <strong>friendliness</strong> of Player <var>i</var> is <var>A_i</var>.</p>
<p>The <var>N</var> players will arrive at the place one by one in some order. To make sure nobody gets lost, you have set the following rule: players who have already arrived there should form a circle, and a player who has just arrived there should cut into the circle somewhere.</p>
<p>When each player, except the first one to arrive, arrives at the place, the player gets <strong>comfort</strong> equal to the smaller of the friendliness of the clockwise adjacent player and that of the counter-clockwise adjacent player. The first player to arrive there gets the comfort of <var>0</var>.</p>
<p>What is the maximum total comfort the <var>N</var> players can get by optimally choosing the order of arrivals and the positions in the circle to cut into?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3>
<ul>
<li>All values in input are integers.</li>
<li><var>2 \leq N \leq 2 \times 10^5</var></li>
<li><var>1 \leq A_i \leq 10^9</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3>
<p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>A_1</var> <var>A_2</var> <var>\dots</var> <var>A_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3>
<p>Print the maximum total comfort the <var>N</var> players can get.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4
2 2 1 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>7
</pre>
<p>By arriving at the place in the order Player <var>4, 2, 1, 3</var>, and cutting into the circle as shown in the figure, they can get the total comfort of <var>7</var>.</p>
<p><img alt="Figure" src="https://img.atcoder.jp/ghi/766a260a0019ea93e86e0588cc4db868.png"/></p>
<p>They cannot get the total comfort greater than <var>7</var>, so the answer is <var>7</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>6
</pre></section>
</div>
</span> |
p00278 |
<H1>勉強会</H1>
<p>
プログラマー養成校のアカベ高校には、生徒自身で運営するユニークな勉強会があります。プログラマーは新しい技術を常に取り入れることが大切なので、この勉強会を通して自学自習の習慣を身につけることがこの活動のねらいです。
</p>
<p>
生徒は全部で<var>N</var>人おり、それぞれが入学時のプログラミングコンテストの結果で得られたスコアを持っています。勉強会では<var>N</var>人の生徒のうち何人かがリーダーになり、各リーダーがそれぞれのグループを運営するとともに、自らの運営するグループに参加します。
</p>
<p>
リーダー以外の生徒は、自分のスコアよりも低いスコアのリーダーが運営するグループには参加できません。また、0以上のある値<var>r</var>を1つ決め、グループに参加する生徒とリーダーのスコアの差が<var>r</var>以内となるようにしています。つまり、グループのリーダーのスコアが<var>s</var>のとき、自分のスコアが<var>s</var>を超えているか、あるいは <var>s</var> - <var>r</var> 未満ならば、そのグループには参加できません。
</p>
<p>
あなたは勉強会の実行委員長であり、運営準備のためにシミュレーションを行うことにしました。シミュレーションでは、リーダーが誰もいない状態から始め、以下の操作を何回か繰り返します。
</p>
<ul>
<li> 生徒をリーダーに加える。</li>
<li> 生徒をリーダーから外す。</li>
<li> 要求時点でのリーダーの組み合わせについて、どのグループにも参加できない生徒が<var>x</var>人以下になるような、最小の<var>r</var>を求める。</li>
</ul>
<p>
このようなシミュレーションを行うプログラムを作成してください。
</p>
<h2>入力</h2>
<p>
入力は1つのデータセットからなる。入力は以下の形式で与えられる。
</p>
<pre>
<var>N</var> <var>Q</var>
<var>s<sub>1</sub></var>
<var>s<sub>2</sub></var>
:
<var>s<sub>N</sub></var>
<var>QUERY<sub>1</sub></var>
<var>QUERY<sub>2</sub></var>
:
<var>QUERY<sub>Q</sub></var>
</pre>
<p>
1行目に生徒の数<var>N</var>(1 ≤ <var>N</var> ≤ 1000000)、処理要求の数<var>Q</var>(0 ≤ <var>Q</var> ≤ 1000)が与えられる。
</p>
<p>
続く<var>N</var>行に<var>i</var>番目の生徒のスコアを示す整数<var>s<sub>i</sub></var>(0 ≤ <var>s<sub>i</sub></var> ≤ 1,000,000,000)が与えられる。生徒は1,2,...,<var>N</var> で番号付けされているものとする。
</p>
<p>
続く<var>Q</var>行に処理要求<var>QUERY<sub>i</sub></var>が与えられる。処理要求は時系列順に与えられる。処理要求はADD, REMOVE, CHECKの3種類あり、各<var>QUERY<sub>i</sub></var>は以下のいずれかの形式で与えられる。
</p>
<pre>
ADD <var>a</var>
</pre>
<p>
または
</p>
<pre>
REMOVE <var>a</var>
</pre>
<p>
または
</p>
<pre>
CHECK <var>x</var>
</pre>
<p>
ADD <var>a</var>は番号<var>a</var>(1 ≤ <var>a</var> ≤ <var>N</var>)の生徒をリーダーに加えることを表す。
</p>
<p>
REMOVE <var>a</var>は番号<var>a</var>(1 ≤ <var>a</var> ≤ <var>N</var>)の生徒をリーダーから外すことを表す。
</p>
<p>
CHECK <var>x</var>は出力要求を表す。どのグループにも参加できない生徒の数の上限<var>x</var>(0 ≤ <var>x</var> ≤ <var>N</var>)が与えられる。
</p>
<p>
なお、入力は以下の条件を満たすものとする。
</p>
<ul>
<li> どの時点でも、リーダーの人数が100人を超えることはない。</li>
<li> その時点でリーダーである生徒をリーダーに加えることはない。</li>
<li> その時点でリーダーでない生徒をリーダーから外すことはない。</li>
</ul>
<h2>出力</h2>
<p>
時系列順に各出力要求の時点で、どのグループにも参加できない生徒がx人以下になるような最小の<var>r</var>を1行に出力する。ただし、どのような<var>r</var>を選んでも<var>x</var>人以下にすることが不可能であればNAと出力する。
</p>
<h2>入力例 1</h2>
<pre>
5 8
5
10
8
7
3
ADD 1
ADD 3
CHECK 0
CHECK 1
CHECK 2
CHECK 3
CHECK 4
CHECK 5
</pre>
<h2>出力例 1</h2>
<pre>
NA
2
1
0
0
0
</pre>
<br/>
<h2>入力例 2</h2>
<pre>
5 28
5
10
8
7
3
CHECK 0
CHECK 1
CHECK 2
CHECK 3
CHECK 4
CHECK 5
ADD 1
CHECK 0
CHECK 1
CHECK 2
CHECK 3
CHECK 4
CHECK 5
REMOVE 1
ADD 3
CHECK 0
CHECK 1
CHECK 2
CHECK 3
CHECK 4
CHECK 5
ADD 1
CHECK 0
CHECK 1
CHECK 2
CHECK 3
CHECK 4
CHECK 5
</pre>
<h2>出力例 2</h2>
<pre>
NA
NA
NA
NA
NA
0
NA
NA
NA
2
0
0
NA
5
3
1
0
0
NA
2
1
0
0
0
</pre> |
p02245 |
<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>8 Puzzle</H1>
<p>
The goal of the 8 puzzle problem is to complete pieces on $3 \times 3$ cells where one of the cells is empty space.
</p>
<p>
In this problem, the space is represented by 0 and pieces are represented by integers from 1 to 8 as shown below.
</p>
<pre>
1 3 0
4 2 5
7 8 6
</pre>
<p>
You can move a piece toward the empty space at one step. Your goal is to make the pieces the following configuration in the shortest move (fewest steps).
</p>
<pre>
1 2 3
4 5 6
7 8 0
</pre>
<p>
Write a program which reads an initial state of the puzzle and prints the fewest steps to solve the puzzle.
</p>
<H2>Input</H2>
<p>
The $3 \times 3$ integers denoting the pieces or space are given.
</p>
<H2>Output</H2>
<p>
Print the fewest steps in a line.
</p>
<H2>Constraints</H2>
<ul>
<li>There is a solution.</li>
</ul>
<H2>Sample Input </H2>
<pre>
1 3 0
4 2 5
7 8 6
</pre>
<H2>Sample Output</H2>
<pre>
4
</pre>
|
p01890 |
<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>Non-redundant Drive</h2>
<p>
The people of JAG kingdom hate redundancy. For example, the N cities in JAG kingdom are connected with just $N - 1$ bidirectional roads such that any city is reachable from any city through some roads. Under the condition, the number of paths from a city to another city is exactly one for all pairs of the cities. This is a non-redundant road network :)
</p>
<p>
One day, you, a citizen of JAG kingdom, decided to travel as many cities in the kingdom as possible with a car. The car that you will use has an infinitely large tank, but initially the tank is empty. The fuel consumption of your car is 1 liter per 1 km, i.e. it consumes 1 liter of gasoline to move 1 km.
</p>
<p>
Each city has exactly one gas station, and you can supply $g_x$ liters of gasoline to your car at the gas station of the city $x$. Of course, you have a choice not to visit some of the gas stations in your travel. But you will not supply gasoline twice or more at the same gas station, because it is redundant. Each road in the kingdom has a distance between two cities: the distance of $i$-th road is $d_i$ km. You will not pass the same city or the same road twice or more, of course, because it is redundant.
</p>
<p>
If a quantity of stored gasoline becomes zero, the car cannot move, and hence your travel will end there. But then, you may concern about an initially empty tank. Don't worry. You can start at any gas station of the cities in the kingdom. Furthermore, each road directly connects the gas stations of the its two ends (because the spirit of non-redundancy avoids redundant moves in a city), you therefore can supply gasoline to your car even if your car tank becomes empty just when you arrive the city.
</p>
<p>
Your task is to write a program computing the maximum number of cities so that you can travel under your non-redundancy policy.
</p>
<h3>Input</h3>
<p>
The input consists of a single test case.<br/>
<br/>
$N$<br/>
$g_1$ $g_2$ ... $g_N$<br/>
$a_1$ $b_1$ $d_1$<br/>
$a_2$ $b_2$ $d_2$<br/>
...<br/>
$a_{N-1}$ $b_{N-1}$ $d_{N-1}$
</p>
<p>
The first line contains an integer $N$ ($1 \leq N \leq 100,000$), which is the number of cities in JAG kingdom. The second line contains $N$ integers: the $i$-th of them is $g_i$ ($1 \leq g_i \leq 10,000$), the amount of gasoline can be supplied at the gas station of the city $i$. The following $N - 1$ lines give information of roads: the $j$-th line of them contains $a_j$ and $b_j$ , which indicates that the $j$-th road bidirectionally connects the cities $a_j$ and $b_j$ ($1 \leq a_j, b_j \leq N, a_j \ne b_j$) with distance $d_j$ ($1 \leq d_j \leq 10,000$). You can assume that all cities in the kingdom are connected by the roads.
</p>
<h3>Output</h3>
<p>
Print the maximum number of cities you can travel from any city under the constraint such that you can supply gasoline at most once per a gas station.
</p>
<h3>Sample Input 1</h3>
<pre>
5
5 8 1 3 5
1 2 4
2 3 3
2 4 3
1 5 7
</pre>
<h3>Output for the Sample Input 1</h3>
<pre>
4
</pre>
<h3>Sample Input 2</h3>
<Pre>
2
10 1
1 2 10
</pre>
<h3>Output for the Sample Input 2</h3>
<pre>
2
</pre>
<h3>Sample Input 3</h3>
<pre>
5
1 3 5 1 1
1 2 5
2 3 3
2 4 3
1 5 5
</pre>
<h3>Output for the Sample Input 3</h3>
<pre>
3
</pre>
|
p00782 |
<H1><font color="#000">Problem C:</font>Mobile Phone Coverage</H1>
<p>
A mobile phone company ACMICPC (Advanced Cellular, Mobile, and Internet-Connected Phone Corporation) is planning to set up a collection of antennas for mobile phones in a city called Maxnorm. The company ACMICPC has several collections for locations of antennas as their candidate plans, and now they want to know which collection is the best choice.
</p>
<p>
for this purpose, they want to develop a computer program to find the coverage of a collection of antenna locations. Each antenna <i>A<sub>i</sub></i> has power <i>r<sub>i</sub></i>, corresponding to "radius". Usually, the coverage region of the antenna may be modeled as a disk centered at the location of the antenna (<i>x<sub>i</sub>, y<sub>i</sub></i>) with radius <i>r<sub>i</sub></i>. However, in this city Maxnorm such a coverage region becomes the square [<i>x<sub>i</sub></i> − <i>r<sub>i</sub></i>, <i>x<sub>i</sub></i> + <i>r<sub>i</sub></i>] × [<i>y<sub>i</sub></i> − <i>r<sub>i</sub></i>, <i>y<sub>i</sub></i> + <i>r<sub>i</sub></i>]. In other words, the distance between two points (<i>x<sub>p</sub></i>, <i>y<sub>p</sub></i>) and (<i>x<sub>q</sub></i>, <i>y<sub>q</sub></i>) is measured by the max norm max{ |<i>x<sub>p</sub></i> − <i>x<sub>q</sub></i>|, |<i>y<sub>p</sub></i> − <i>y<sub>q</sub></i>|}, or, the <i>L</i><sub>∞</sub> norm, in this city Maxnorm instead of the ordinary Euclidean norm √ {(<i>x<sub>p</sub></i> − <i>x<sub>q</sub></i>)<sup>2</sup> + (<i>y<sub>p</sub></i> − <i>y<sub>q</sub></i>)<sup>2</sup>}.
</p>
<p>
As an example, consider the following collection of 3 antennas
</p>
<pre>
4.0 4.0 3.0
5.0 6.0 3.0
5.5 4.5 1.0
</pre>
<p>depicted in the following figure</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_mobilePhone">
</center>
<p>
where the <i>i</i>-th row represents <i>x<sub>i</sub>, y<sub>i</sub> r<sub>i</sub></i> such that (<i>x<sub>i</sub>, y<sub>i</sub></i>) is the position of the <i>i</i>-th antenna and <i>r<sub>i</sub></i> is its power. The area of regions of points covered by at least one antenna is 52.00 in this case.
</p>
<p>
Write a program that finds the area of coverage by a given collection of antenna locations.
</p>
<H2>Input</H2>
<p>
The input contains multiple data sets, each representing a collection of antenna locations. A data set is given in the following format.
</p>
<pre>
<i>
n
x<sub>1</sub> y<sub>1</sub> r<sub>1</sub>
x<sub>2</sub> y<sub>2</sub> r<sub>2</sub>
. . .
x<sub>n</sub> y<sub>n</sub> r<sub>n</sub>
</i>
</pre>
<p>
The first integer <i>n</i> is the number of antennas, such that 2 ≤ <i>n</i> ≤ 100. The coordinate of the <i>i</i>-th antenna is given by (<i>x<sub>i</sub>, y<sub>i</sub></i>), and its power is <i>r<sub>i</sub></i>. <i>x<sub>i</sub>, y<sub>i</sub></i> and <i>r<sub>i</sub></i> are fractional numbers between 0 and 200 inclusive.
</p>
<p>
The end of the input is indicated by a data set with 0 as the value of <i>n</i>.
</p>
<H2>Output</H2>
<p>
For each data set, your program should output its sequence number (1 for the first data set, 2 for the second, etc.) and the area of the coverage region. The area should be printed with two digits to the right of the decimal point, after rounding it to two decimal places.
</p>
<p>
The sequence number and the area should be printed on the same line with no spaces at the beginning and end of the line. The two numbers should be separated by a space.
</p>
<H2>Sample Input</H2>
<pre>
3
4.0 4.0 3.0
5.0 6.0 3.0
5.5 4.5 1.0
2
3.0 3.0 3.0
1.5 1.5 1.0
0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
1 52.00
2 36.00
</pre>
|
p03004 | <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There are <var>N</var> points in a two-dimensional plane. The initial coordinates of the <var>i</var>-th point are <var>(x_i, y_i)</var>. Now, each point starts moving at a speed of 1 per second, in a direction parallel to the <var>x</var>- or <var>y</var>- axis. You are given a character <var>d_i</var> that represents the specific direction in which the <var>i</var>-th point moves, as follows:</p>
<ul>
<li>If <var>d_i =</var> <code>R</code>, the <var>i</var>-th point moves in the positive <var>x</var> direction;</li>
<li>If <var>d_i =</var> <code>L</code>, the <var>i</var>-th point moves in the negative <var>x</var> direction;</li>
<li>If <var>d_i =</var> <code>U</code>, the <var>i</var>-th point moves in the positive <var>y</var> direction;</li>
<li>If <var>d_i =</var> <code>D</code>, the <var>i</var>-th point moves in the negative <var>y</var> direction.</li>
</ul>
<p>You can stop all the points at some moment of your choice after they start moving (including the moment they start moving).
Then, let <var>x_{max}</var> and <var>x_{min}</var> be the maximum and minimum among the <var>x</var>-coordinates of the <var>N</var> points, respectively. Similarly, let <var>y_{max}</var> and <var>y_{min}</var> be the maximum and minimum among the <var>y</var>-coordinates of the <var>N</var> points, respectively.</p>
<p>Find the minimum possible value of <var>(x_{max} - x_{min}) \times (y_{max} - y_{min})</var> and print it.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 10^5</var></li>
<li><var>-10^8 \leq x_i,\ y_i \leq 10^8</var></li>
<li><var>x_i</var> and <var>y_i</var> are integers.</li>
<li><var>d_i</var> is <code>R</code>, <code>L</code>, <code>U</code>, or <code>D</code>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>x_1</var> <var>y_1</var> <var>d_1</var>
<var>x_2</var> <var>y_2</var> <var>d_2</var>
<var>.</var>
<var>.</var>
<var>.</var>
<var>x_N</var> <var>y_N</var> <var>d_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum possible value of <var>(x_{max} - x_{min}) \times (y_{max} - y_{min})</var>.</p>
<p>The output will be considered correct when its absolute or relative error from the judge's output is at most <var>10^{-9}</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2
0 3 D
3 0 L
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>0
</pre>
<p>After three seconds, the two points will meet at the origin. The value in question will be <var>0</var> at that moment.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5
-7 -10 U
7 -6 U
-8 7 D
-3 3 D
0 -6 R
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>97.5
</pre>
<p>The answer may not be an integer.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>20
6 -10 R
-4 -9 U
9 6 D
-3 -2 R
0 7 D
4 5 D
10 -10 U
-1 -8 U
10 -6 D
8 -5 U
6 4 D
0 3 D
7 9 R
9 -4 R
3 10 D
1 9 U
1 -6 U
9 -8 R
6 7 D
7 -3 D
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>273
</pre></section>
</div>
</span> |
p01039 | <h1>Problem C: Manhattan Warp Machine 1</h1>
<h2>Problem</h2>
<p>
とある宇宙では、1次元の整数座標点上に星が存在し、宇宙人達はマンハッタンワープ装置を使い、星間を移動している。<br>
このワープ装置には、<var>N</var>個のボタンが付いており、ボタン<var>i</var>を押すと、現在いる星からのマンハッタン距離が<var>d<sub>i</sub></var>である任意の星にコスト<var>c<sub>i</sub></var>でワープすることができる。<br>
今、点0の星にいるある宇宙人が点<var>x</var>の星に行きたいと考えている。<br>
点<var>x</var>の星に辿り着くまでの最小のコストを答えよ。<br>
辿り着けない場合は-1を出力せよ。<br>
1次元上の整数座標点には必ず星が存在する。<br>
点<var>x<sub>1</sub></var>と点<var>x<sub>2</sub></var>間のマンハッタン距離は| <var>x<sub>1</sub></var> - <var>x<sub>2</sub></var> |で表される。
</p>
<h2>Input</h2>
<pre>
<var>N</var> <var>x</var>
<var>d<sub>1</sub></var> <var>c<sub>1</sub></var>
...
<var>d<sub>N</sub></var> <var>c<sub>N</sub></var>
</pre>
<p>
入力は全て整数で与えられる。<br>
1行目に<var>N</var>と行きたい星の座標<var>x</var>が空白区切りで与えられる。<br>
続く<var>N</var>行に、移動可能なマンハッタン距離<var>d<sub>i</sub></var>とコスト<var>c<sub>i</sub></var>が1行ずつ空白区切りで与えられる。<br>
</p>
<h2>Constraints</h2>
<p>
入力は以下の条件を満たす。
</p>
<ul>
<li>1 ≤ <var>N</var> ≤ 15</li>
<li>0 ≤ <var>x</var> ≤ 10<sup>5</sup></li>
<li>1 ≤ <var>d<sub>i</sub></var> ≤ 10<sup>5</sup></li>
<li>1 ≤ <var>c<sub>i</sub></var> ≤ 100</li>
<li>与えられるマンハッタン距離<var>d<sub>i</sub></var>は全て異なる。</li>
</ul>
<h2>Output</h2>
<p>
点<var>x</var>の星に辿り着くまでにかかる最小コストを1行に出力せよ。辿り着くことが不可能な場合は-1を出力せよ。
</p>
<h2>Sample Input 1</h2>
<pre>
2 5
1 1
2 1
</pre>
<h2>Sample Output 1</h2>
<pre>
3
</pre>
<h2>Sample Input 2</h2>
<pre>
2 12
9 1
3 2
</pre>
<h2>Sample Output 2</h2>
<pre>
3
</pre>
<h2>Sample Input 3</h2>
<pre>
1 3
4 1
</pre>
<h2>Sample Output 3</h2>
<pre>
-1
</pre>
|
p03454 | <span class="lang-en">
<p>Score : <var>900</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>For a positive integer <var>n</var>, let us define <var>f(n)</var> as the number of digits in base <var>10</var>.</p>
<p>You are given an integer <var>S</var>.
Count the number of the pairs of positive integers <var>(l, r)</var> (<var>l \leq r</var>) such that <var>f(l) + f(l + 1) + ... + f(r) = S</var>, and find the count modulo <var>10^9 + 7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq S \leq 10^8</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>S</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the answer.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>9
</pre>
<p>There are nine pairs <var>(l, r)</var> that satisfies the condition: <var>(1, 1)</var>, <var>(2, 2)</var>, <var>...</var>, <var>(9, 9)</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>98
</pre>
<p>There are <var>98</var> pairs <var>(l, r)</var> that satisfies the condition, such as <var>(1, 2)</var> and <var>(33, 33)</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>123
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>460191684
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>36018
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>966522825
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 5</h3><pre>1000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 5</h3><pre>184984484
</pre></section>
</div>
</span> |
p01469 |
<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_1,X_2,...,X_N$ がある。数列 $\{1,2,...,N\}$ から部分列 $S$ を選ぶ。ただし $S$ は以下の条件を満たす必要がある。</p>
<ul><li>$T=\{X_s | s \in S\}$ とする。このとき、任意の $x \in T$ について、$x$ の約数(ただし $x$ は除く)は $T$ に含まれない。</li></ul>
<p>条件を満たす $S$ のうち、最も多くの要素を含むものを求めよ。また、そのような $S$ が複数ある場合は辞書式順序で最小のものを求めよ。</p>
<h2>入力</h2>
<p>入力は以下の形式に従う。与えられる数は全て整数である。</p>
<pre>$N$
$X_1$ $X_2$ $...$ $X_N$</pre>
<h2>制約</h2>
<ul><li>$1 \leq N \leq 100$</li>
<li>$1 \leq X_i \leq 10^8$</li>
<li>$i \neq j$ ならば $X_i \neq X_j$</li></ul>
<h2>出力</h2>
<p>求める $S$ の各要素を1行にスペースを空けて昇順で出力せよ。</p>
<h2>Sample Input 1</h2>
<pre>3
25 125 5</pre>
<h2>Output for the Sample Input 1</h2>
<pre>1</pre>
<p>$T=\{X_1\}=\{25\}$ は条件を満たす。</p>
<h2>Sample Input 2</h2>
<pre>3
6 3 2</pre>
<h2>Output for the Sample Input 2</h2>
<pre>2 3</pre>
<p>$T=\{X_2,X_3\}=\{2,3\}$ は条件を満たす。</p>
<h2>Sample Input 3</h2>
<pre>10
10 9 8 7 6 5 4 3 2 1</pre>
<h2>Output for the Sample Input 3</h2>
<pre>1 2 3 4 5</pre>
<p>$T=\{X_1,X_2,X_3,X_4,X_5\}=\{6,7,8,9,10\}$ は条件を満たす。
$T=\{X_1,X_2,X_4,X_5,X_7\}=\{4,6,7,9,10\}$ も条件を満たすが、$\{1,2,4,5,7\}$は $\{1,2,3,4,5\}$ より辞書順で大きいので答とはならない。</p>
|
p01193 |
<H1><font color="#000"></font>First Experience</H1>
<!-- Problem E-->
<p>
After spending long long time, you had gotten tired of computer programming. Instead you were inter-
ested in electronic construction. As your first work, you built a simple calculator. It can handle positive
integers of up to four decimal digits, and perform addition, subtraction and multiplication of numbers.
You didn’t implement division just because it was so complicated. Although the calculator almost worked
well, you noticed that it displays unexpected results in several cases. So, you decided to try its simulation
by a computer program in order to figure out the cause of unexpected behaviors.
</p>
<p>
The specification of the calculator you built is as follows. There are three registers on the calculator. The
register <i>R</i>1 stores the value of the operation result of the latest operation; the register <i>R</i>2 stores the new
input value; and the register <i>R</i>3 stores the input operator. At the beginning, both <i>R</i>1 and <i>R</i>2 hold zero,
and <i>R</i>3 holds a null operator. The calculator has keys for decimal digits from ‘0’ to ‘9’ and operators
‘+’, ‘-’, ‘×’ and ‘=’. The four operators indicate addition, subtraction, multiplication and conclusion,
respectively. When a digit key is pressed, <i>R</i>2 is multiplied by ten, and then added by the pressed digit
(as a number). When ‘+’, ‘-’ or ‘×’ is pressed, the calculator first applies the binary operator held in <i>R</i>3
to the values in <i>R</i>1 and <i>R</i>2 and updates <i>R</i>1 with the result of the operation. Suppose <i>R</i>1 has 10, <i>R</i>2 has
3, and <i>R</i>3 has ‘-’ (subtraction operator), <i>R</i>1 is updated with 7 ( = 10 - 3). If <i>R</i>3 holds a null operator,
the result will be equal to the value of <i>R</i>2. After <i>R</i>1 is updated, <i>R</i>2 is cleared to zero and <i>R</i>3 is set to the
operator which the user entered. ‘=’ indicates termination of a computation. So, when ‘=’ is pressed,
the calculator applies the operator held in <i>R</i>3 in the same manner as the other operators, and displays the
final result to the user. After the final result is displayed, <i>R</i>3 is reinitialized with a null operator.
</p>
<p>
The calculator cannot handle numbers with five or more decimal digits. Because of that, if the intermediate computation produces a value less than 0 (i.e., a negative number) or greater than 9999, the calculator
displays “E” that stands for error, and ignores the rest of the user input until ‘=’ is pressed.
</p>
<p>
Your task is to write a program to simulate the simple calculator.
</p>
<H2>Input</H2>
<p>
The input consists of multiple test cases.
</p>
<p>
Each line of the input specifies one test case, indicating the order of key strokes which a user entered.
The input consists only decimal digits (from ‘0’ to ‘9’) and valid operators ‘+’, ‘-’, ‘*’ and ‘=’ where ‘*’
stands for ‘×’.
</p>
<p>
You may assume that ‘=’ occurs only at the end of each line, and no case contains more than 80 key
strokes.
</p>
<p>
The end of input is indicated by EOF.
</p>
<H2>Output</H2>
<p>
For each test case, output one line which contains the result of simulation.
</p>
<H2>Sample Input</H2>
<pre>
1+2+3+4+5+6+7+8+9+10=
1000-500-250-125=
1*2*3*4*5*6=
5000*5000=
10-100=
100*100=
10000=
</pre>
<H2>Output for the Sample Input</H2>
<pre>
55
125
720
E
E
E
E
</pre>
|
p01486 |
<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 A:
CatChecker
</h2>
<p>
外見からねこかどうかわからない動物がいる. あなたは, 鳴き声がねこの鳴き声であればねこであり, そうでなければうさぎであると判定することにした.
</p>
<p>
ねこの鳴き声は次のように定義される.
</p>
<ul>
<li> "" (empty string) はねこの鳴き声である.</li>
<li> $X$, $Y$ がねこの鳴き声であれば 'm' + $X$ + 'e' + $Y$ + 'w' は猫の鳴き声である. ただし + は文字列の連結を表す.</li>
<li>以上で定義されるものだけがねこの鳴き声である.</li>
</ul>
<p>
BNF で表すとねこの鳴き声 $CAT$ は<br>
$CAT$ := "" (empty string) $|$ 'm' + $CAT$ + 'e' + $CAT$ + 'w'<br>
と定義される.
</p>
<p>
鳴き声を表す文字列 $S$ が与えられる. 鳴き声から動物が何であるか判定せよ.
</p>
<h3>Constraints</h3>
<p>
$S$ will contain between 1 and 500 characters, inclusive. Each character in $S$ will be 'm', 'e' or 'w'.
</p>
<h3>Input</h3>
<p>
入力は以下の形式で与えられる:<br>
<br>
$S$
</p>
<h3>Output</h3>
<p>
$S$ が猫の鳴き声であれば "Cat", そうでなければ "Rabbit" と1 行に出力せよ.
</p>
<h3>Sample Input 1</h3>
<pre>mmemewwemeww</pre>
<h3>Sample Output 1</h3>
<pre>Cat</pre>
<h3>Sample Input 2</h3>
<pre>mewmew</pre>
<h3>Sample Output 2</h3>
<pre>Rabbit</pre> |
p03141 | <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> dishes of cuisine placed in front of Takahashi and Aoki.
For convenience, we call these dishes Dish <var>1</var>, Dish <var>2</var>, ..., Dish <var>N</var>.</p>
<p>When Takahashi eats Dish <var>i</var>, he earns <var>A_i</var> points of <em>happiness</em>; when Aoki eats Dish <var>i</var>, she earns <var>B_i</var> points of happiness.</p>
<p>Starting from Takahashi, they alternately choose one dish and eat it, until there is no more dish to eat.
Here, both of them choose dishes so that the following value is maximized: "the sum of the happiness he/she will earn in the end" minus "the sum of the happiness the other person will earn in the end".</p>
<p>Find the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".</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 \leq 10^9</var></li>
<li><var>1 \leq 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>:</var>
<var>A_N</var> <var>B_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
10 10
20 20
30 30
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>20
</pre>
<p>In this sample, both of them earns <var>10</var> points of happiness by eating Dish <var>1</var>, <var>20</var> points by eating Dish <var>2</var>, and <var>30</var> points by eating Dish <var>3</var>.</p>
<p>In this case, since Takahashi and Aoki have the same "taste", each time they will choose the dish with which they can earn the greatest happiness. Thus, first Takahashi will choose Dish <var>3</var>, then Aoki will choose Dish <var>2</var>, and finally Takahashi will choose Dish <var>1</var>, so the answer is <var>(30 + 10) - 20 = 20</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
20 10
20 20
20 30
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>20
</pre>
<p>In this sample, Takahashi earns <var>20</var> points of happiness by eating any one of the dishes <var>1, 2</var> and <var>3</var>, but Aoki earns <var>10</var> points of happiness by eating Dish <var>1</var>, <var>20</var> points by eating Dish <var>2</var>, and <var>30</var> points by eating Dish <var>3</var>.</p>
<p>In this case, since only Aoki has likes and dislikes, each time they will choose the dish with which Aoki can earn the greatest happiness. Thus, first Takahashi will choose Dish <var>3</var>, then Aoki will choose Dish <var>2</var>, and finally Takahashi will choose Dish <var>1</var>, so the answer is <var>(20 + 20) - 20 = 20</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>6
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>-2999999997
</pre>
<p>Note that the answer may not fit into a <var>32</var>-bit integer.</p></section>
</div>
</span> |
p03511 | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given two strings <var>s</var> and <var>t</var> consisting of lowercase English letters and an integer <var>L</var>.</p>
<p>We will consider generating a string of length <var>L</var> by concatenating one or more copies of <var>s</var> and <var>t</var>. Here, it is allowed to use the same string more than once.</p>
<p>For example, when <var>s =</var> <code>at</code><var>,</var> <var>t =</var> <code>code</code> and <var>L = 6,</var> the strings <code>atatat</code><var>,</var> <code>atcode</code> and <code>codeat</code> can be generated.</p>
<p>Among the strings that can be generated in this way, find the lexicographically smallest one. In the cases given as input, it is always possible to generate a string of length <var>L</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≤ L ≤ 2 × 10^5</var></li>
<li><var>1 ≤ |s|, |t| ≤ L</var></li>
<li><var>s</var> and <var>t</var> consist of lowercase English letters.</li>
<li>It is possible to generate a string of length <var>L</var> in the way described in Problem Statement.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>x_1</var> <var>s_1</var>
<var>x_2</var> <var>s_2</var>
<var>:</var>
<var>x_N</var> <var>s_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the lexicographically smallest string among the ones that can be generated in the way described in Problem Statement.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6
at
code
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>atatat
</pre>
<p>This input corresponds to the example shown in Problem Statement.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>8
coding
festival
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>festival
</pre>
<p>It is possible that either <var>s</var> or <var>t</var> cannot be used at all in generating a string of length <var>L</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>8
same
same
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>samesame
</pre>
<p>It is also possible that <var>s = t</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>10
coin
age
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>ageagecoin
</pre></section>
</div>
</span> |
p00297 |
<h1>天体観測</h1>
<p>
ある晴れた夜の帰り道、空を見上げるとそこには無数の星。強く輝く星々、かすかに見える星々、いろ
いろな明るさの星々が夜空を彩っています。
</p>
<p>
あなたはふと思いました。この星空に自分の星座があったらどんなに素敵だろうと。そこであなたはひとつの基準を決め、部屋の窓から見える星々を適当につなげて自分の星座を作ることにしました。その基準とは、「どの2つの星を選んでも、それらの明るさの差がある定数 <var>d</var> 以下になるような星のグループを作り、その中で一番大きいものを自分の星座にしよう!」というものです。例えば、図のような窓から見える夜空を考えてみましょう(外側の長方形は窓枠です)。
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_PCK2014_starWatching1" width="340">
</center>
<br>
<p>
この夜空には、明るさがそれぞれ 1,12, 2,4,1,8, 3,5,4 の9つの星がありますが、<var>d</var> = 2 とすると例えば以下のような3つの星座 A, B, C ができます。
</p>
<center>
<table>
<tr>
<td style="text-align:center"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_PCK2014_starWatching2" width="160"></td>
<td style="text-align:center"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_PCK2014_starWatching3" width="160"></td>
<td style="text-align:center"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_PCK2014_starWatching4" width="160"></td>
</tr>
<tr>
<td style="text-align:center" width="180">大きさが 12 の星座 A</td>
<td style="text-align:center" width="180">大きさが 10 の星座 B</td>
<td style="text-align:center" width="180">大きさが 16 の星座 C</td>
</tr>
</table>
</center>
<br>
<p>
星座の大きさを次のように決めることにしました。ある星座の星をすべて含むような、窓枠に平行な辺からなる長方形のうち、面積が最も小さいものを選びます。この長方形の面積をその星座の大きさとします。例えば、上の夜空では星座 A, B, Cの大きさはそれぞれ12, 10, 16になるので、星座Cが最も大きい星座となります。
</p>
<p>
<var>N</var> 個の星の位置と明るさ、および整数 <var>d</var> が与えられたとき、一番大きい星座の面積を求めるプログラムを作成してください。星の位置は窓枠の左下隅を原点とした座標で与えられ、軸は図のような向きとします。星座を構成する星が1つの場合や、星々が軸に平行な直線上にある場合は、その星座の面積は 0 となることに注意してください。
</p>
<h2>入力</h2>
<p>
入力は以下の形式で与えられる。
</p>
<pre>
<var>N</var> <var>d</var>
<var>x</var><sub>1</sub> <var>y</var><sub>1</sub> <var>b</var><sub>1</sub>
<var>x</var><sub>2</sub> <var>y</var><sub>2</sub> <var>b</var><sub>2</sub>
:
<var>x<sub>N</sub></var> <var>y<sub>N</sub></var> <var>b<sub>N</sub></var>
</pre>
<p>
1 行目に星の数 <var>N</var> (1 ≤ <var>N</var> ≤ 200000) と整数 <var>d</var> (0 ≤ <var>d</var> ≤ 10<sup>9</sup>) が与えられる。続く <var>N</var> 行に、<var>i</var> 番目の星の座標を表す整数 <var>x<sub>i</sub></var> (0 ≤ <var>x<sub>i</sub></var> ≤ 2000000) と <var>y<sub>i</sub></var> (0 ≤ <var>y<sub>i</sub></var> ≤ 2000000)、明るさを表す整数 <var>b<sub>i</sub></var> (0 ≤ <var>b<sub>i</sub></var> ≤ 10<sup>9</sup>) が与えられる。入力される星の座標はすべて異なる。
</p>
<h2>出力</h2>
<p>
一番大きい星座の面積を1行に出力する。
</p>
<h2>入出力例</h2>
<br>
<h2>入力例</h2>
<pre>
9 2
1 1 1
1 5 12
2 3 2
3 2 4
4 4 1
5 1 3
5 3 8
6 5 5
7 2 4
</pre>
<h2> 出力例</h2>
<pre>
16
</pre> |
p03842 | <span class="lang-en">
<p>Score : <var>1400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given an integer sequence <var>a</var> of length <var>N</var>.
How many permutations <var>p</var> of the integers <var>1</var> through <var>N</var> satisfy the following condition?</p>
<ul>
<li>For each <var>1 ≤ i ≤ N</var>, at least one of the following holds: <var>p_i = a_i</var> and <var>p_{p_i} = a_i</var>.</li>
</ul>
<p>Find the count modulo <var>10^9 + 7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≤ N ≤ 10^5</var></li>
<li><var>a_i</var> is an integer.</li>
<li><var>1 ≤ a_i ≤ N</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of the permutations <var>p</var> that satisfy the condition, modulo <var>10^9 + 7</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
1 2 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>4
</pre>
<p>The following four permutations satisfy the condition:</p>
<ul>
<li><var>(1, 2, 3)</var></li>
<li><var>(1, 3, 2)</var></li>
<li><var>(3, 2, 1)</var></li>
<li><var>(2, 1, 3)</var></li>
</ul>
<p>For example, <var>(1, 3, 2)</var> satisfies the condition because <var>p_1 = 1</var>, <var>p_{p_2} = 2</var> and <var>p_{p_3} = 3</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2
1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1
</pre>
<p>The following one permutation satisfies the condition:</p>
<ul>
<li><var>(2, 1)</var></li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>3
2 1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>2
</pre>
<p>The following two permutations satisfy the condition:</p>
<ul>
<li><var>(2, 3, 1)</var></li>
<li><var>(3, 1, 2)</var></li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>3
1 1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>0
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 5</h3><pre>13
2 1 4 3 6 7 5 9 10 8 8 9 11
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 5</h3><pre>6
</pre></section>
</div>
</span> |
p02750 | <span class="lang-en">
<p>Score : <var>800</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There are <var>N</var> stores called Store <var>1</var>, Store <var>2</var>, <var>\cdots</var>, Store <var>N</var>. Takahashi, who is at his house at time <var>0</var>, is planning to visit some of these stores.</p>
<p>It takes Takahashi one unit of time to travel from his house to one of the stores, or between any two stores.</p>
<p>If Takahashi reaches Store <var>i</var> at time <var>t</var>, he can do shopping there after standing in a queue for <var>a_i \times t + b_i</var> units of time. (We assume that it takes no time other than waiting.)</p>
<p>All the stores close at time <var>T + 0.5</var>. If Takahashi is standing in a queue for some store then, he cannot do shopping there.</p>
<p>Takahashi does not do shopping more than once in the same store.</p>
<p>Find the maximum number of times he can do shopping before time <var>T + 0.5</var>.</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>0 \leq a_i \leq 10^9</var></li>
<li><var>0 \leq b_i \leq 10^9</var></li>
<li><var>0 \leq T \leq 10^9</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>T</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 answer.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 7
2 0
3 2
0 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>Here is one possible way to visit stores:</p>
<ul>
<li>From time <var>0</var> to time <var>1</var>: in <var>1</var> unit of time, he travels from his house to Store <var>1</var>.</li>
<li>From time <var>1</var> to time <var>3</var>: for <var>2</var> units of time, he stands in a queue for Store <var>1</var> to do shopping.</li>
<li>From time <var>3</var> to time <var>4</var>: in <var>1</var> unit of time, he travels from Store <var>1</var> to Store <var>3</var>.</li>
<li>From time <var>4</var> to time <var>7</var>: for <var>3</var> units of time, he stands in a queue for Store <var>3</var> to do shopping.</li>
</ul>
<p>In this way, he can do shopping twice before time <var>7.5</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>1 3
0 3
</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>5 21600
2 14
3 22
1 3
1 10
1 9
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>5
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>7 57
0 25
3 10
2 4
5 15
3 22
2 14
1 15
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>3
</pre></section>
</div>
</span> |
p02300 |
<H1>Convex Hull</H1>
<br/>
<p>
Find the convex hull of a given set of points <var>P</var>. In other words, find the smallest convex polygon containing all the points of <var>P</var>. Here, in a convex polygon, all interior angles are less than or equal to 180 degrees.
</p>
<p>
Please note that you should find all the points of <var>P</var> on both corner and boundary of the convex polygon.
</p>
<H2>Input</H2>
<pre>
<var>n</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>
</pre>
<p>
The first integer <var>n</var> is the number of points in <var>P</var>. The coordinate of the <var>i</var>-th point <var>p<sub>i</sub></var> is given by two integers <var>x<sub>i</sub></var> and <var>y<sub>i</sub></var>.
</p>
<H2>Output</H2>
<p>
In the first line, print the number of points on the corner/boundary of the convex polygon. In the following lines, print <var>x y</var> coordinates of the set of points.
The coordinates should be given in the order of counter-clockwise visit of them starting from the point in <var>P</var> with the minimum <var>y</var>-coordinate, or the leftmost such point in case of a tie.
</p>
<H2>Constraints</H2>
<ul>
<li>
3 ≤ <var>n</var> ≤ 100000
</li>
<li>
-10000 ≤ <var>x<sub>i</sub></var>, <var>y<sub>i</sub></var> ≤ 10000
</li>
<li>No point in the <var>P</var> will occur more than once.</li>
</ul>
<H2>Sample Input 1</H2>
<pre>
7
2 1
0 0
1 2
2 2
4 2
1 3
3 3
</pre>
<H2>Sample Output 1</H2>
<pre>
5
0 0
2 1
4 2
3 3
1 3
</pre>
<br/>
<H2>Sample Input 2</H2>
<pre>
4
0 0
2 2
0 2
0 1
</pre>
<H2>Sample Output 2</H2>
<pre>
4
0 0
2 2
0 2
0 1
</pre>
|
p01605 |
<h2>replace</h2>
<h2>Problem Statement</h2>
<p>文字列<var>S</var>が与えられる.
以下の処理を<var>Q</var>個順番に行う.</p>
<ul class="list1" style="padding-left:16px;margin-left:16px"><li><var>S</var>に含まれる文字<var>c_i</var>を全て同時に文字列<var>p_i</var>で置き換える.</li></ul>
<p>最後に文字列<var>S</var>の<var>A</var>文字目から<var>B</var>文字目まで(1-indexed)を出力せよ.</p>
<h2>Input</h2>
<p>入力は以下の形式に従う.与えられる数は全て整数である.</p>
<pre><var>S</var>
<var>Q</var> <var>A</var> <var>B</var>
<var>c_1</var> <var>p_1</var>
<var>...</var>
<var>c_Q</var> <var>p_Q</var></pre>
<h2>Constraints</h2>
<ul class="list1" style="padding-left:16px;margin-left:16px"><li><var>1≦Q≦3 \times 10^5</var></li>
<li><var>1≦A≦B≦10^{18}</var></li>
<li><var>B-A≦10^5</var></li>
<li><var>1≦|S|≦10</var></li>
<li><var>1≦|p_i|≦10</var></li>
<li><var>S</var>は小文字のアルファベットからなる文字列である.</li>
<li><var>c_i</var>は小文字のアルファベット1文字である.</li>
<li><var>p_i</var>は"."(ピリオド)1文字または小文字のアルファベットからなる文字列である.<var>p_i=</var>"."(ピリオド)のとき,<var>p_i</var>は空文字列として扱いなさい.</li></ul>
<h2>Output</h2>
<p><var>Q</var>個の処理を順に行った後,<var>B</var>が<var>|S|</var>よりも大きかったときは"."(ピリオド)を1行に出力せよ.<br />
それ以外のときは文字列<var>S</var>の<var>A</var>文字目から<var>B</var>文字目まで(1-indexed)を出力せよ.</p>
<h2>Sample Input 1</h2>
<pre>abaz
3 1 5
a cab
b .
c x</pre>
<h2>Output for the Sample Input 1</h2>
<pre>xaxaz</pre>
<p>文字列<var>S</var>は,abaz → cabbcabz → cacaz → xaxazと変化する.</p>
<h2>Sample Input 2</h2>
<pre>original
1 2 5
x notchange</pre>
<h2>Output for the Sample Input 2</h2>
<pre>rigi</pre>
<h2>Sample Input 3</h2>
<pre>aaaa
2 1 1
a .
a nothing</pre>
<h2>Output for the Sample Input 3</h2>
<pre>.</pre>
|
p03638 | <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a grid with <var>H</var> rows and <var>W</var> columns of squares.
Snuke is painting these squares in colors <var>1</var>, <var>2</var>, <var>...</var>, <var>N</var>.
Here, the following conditions should be satisfied:</p>
<ul>
<li>For each <var>i</var> (<var>1 ≤ i ≤ N</var>), there are exactly <var>a_i</var> squares painted in Color <var>i</var>. Here, <var>a_1 + a_2 + ... + a_N = H W</var>.</li>
<li>For each <var>i</var> (<var>1 ≤ i ≤ N</var>), the squares painted in Color <var>i</var> are <em>4-connected</em>. That is, every square painted in Color <var>i</var> can be reached from every square painted in Color <var>i</var> by repeatedly traveling to a horizontally or vertically adjacent square painted in Color <var>i</var>.</li>
</ul>
<p>Find a way to paint the squares so that the conditions are satisfied.
It can be shown that a solution always exists.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≤ H, W ≤ 100</var></li>
<li><var>1 ≤ N ≤ H W</var></li>
<li><var>a_i ≥ 1</var></li>
<li><var>a_1 + a_2 + ... + a_N = H W</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>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 one way to paint the squares that satisfies the conditions.
Output in the following format:</p>
<pre><var>c_{1 1}</var> <var>...</var> <var>c_{1 W}</var>
<var>:</var>
<var>c_{H 1}</var> <var>...</var> <var>c_{H W}</var>
</pre>
<p>Here, <var>c_{i j}</var> is the color of the square at the <var>i</var>-th row from the top and <var>j</var>-th column from the left.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 2
3
2 1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1 1
2 3
</pre>
<p>Below is an example of an invalid solution:</p>
<pre>1 2
3 1
</pre>
<p>This is because the squares painted in Color <var>1</var> are not 4-connected.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 5
5
1 2 3 4 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1 4 4 4 3
2 5 4 5 3
2 5 5 5 3
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1 1
1
1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>1
</pre></section>
</div>
</span> |
p00917 |
<H1><font color="#000">Problem D: </font>Clock Hands</H1>
<p>
We have an analog clock whose three hands (the second hand, the <i>minute</i> hand and the <i>hour</i> hand) rotate quite smoothly. You can measure two angles between the second hand and two other hands.
</p>
<p>
Write a program to find the time at which "No two hands overlap each other" and "Two angles between the second hand and two other hands are equal" for the first time on or after a given time.
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_clockHands1" style="aling:center;width:480px"><br/>
<span>
Figure D.1. Angles between the second hand and two other hands
</span>
</center>
<br/>
<p>
Clocks are not limited to 12-hour clocks. The hour hand of an <var>H</var>-hour clock goes around once in <var>H</var> hours. The minute hand still goes around once every hour, and the second hand goes around once every minute. At 0:0:0 (midnight), all the hands are at the upright position.
</p>
<H2>Input</H2>
<p>
The input consists of multiple datasets. Each of the dataset has four integers <var>H</var>, <var>h</var>, <var>m</var> and <var>s</var> in one line, separated by a space. <var>H</var> means that the clock is an <var>H</var>-hour clock. <var>h</var>, <var>m</var> and <var>s</var> mean hour, minute and second of the specified time, respectively.
</p>
<p>
You may assume 2 ≤ <var>H</var> ≤ 100, 0 ≤ <var>h</var> < <var>H</var>, 0 ≤ <var>m</var> < 60, and 0 ≤ <var>s</var> < 60.
</p>
<p>
The end of the input is indicated by a line containing four zeros.
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_clockHands2" style="aling:center;width:480px"><br/>
<span>
Figure D.2. Examples of H-hour clock (6-hour clock and 15-hour clock)
</span>
</center>
<H2>Output</H2>
<p>
Output the time T at which "No two hands overlap each other" and "Two angles between the second hand and two other hands are equal" for the first time on and after the specified time.
</p>
<p>
For <var>T</var> being <var>h<sub>o</sub></var>:<var>m<sub>o</sub></var>:<var>s<sub>o</sub></var> (<var>s<sub>o</sub></var> seconds past <var>m<sub>o</sub></var> minutes past <var>h<sub>o</sub></var> o'clock), output four non-negative integers <var>h<sub>o</sub></var>, <var>m<sub>o</sub></var>, <var>n</var>, and <var>d</var> in one line, separated by a space, where <var>n</var>/<var>d</var> is the irreducible fraction representing <var>s<sub>o</sub></var>. For integer <var>s<sub>o</sub></var> including 0, let <var>d</var> be 1.
</p>
<p>
The time should be expressed in the remainder of <var>H</var> hours. In other words, one second after
(<var>H</var> − 1):59:59 is 0:0:0, not <var>H</var>:0:0.
</p>
<H2>Sample Input</H2>
<pre>
12 0 0 0
12 11 59 59
12 1 56 0
12 1 56 3
12 1 56 34
12 3 9 43
12 3 10 14
12 7 17 58
12 7 18 28
12 7 23 0
12 7 23 31
2 0 38 29
2 0 39 0
2 0 39 30
2 1 6 20
2 1 20 1
2 1 20 31
3 2 15 0
3 2 59 30
4 0 28 48
5 1 5 40
5 1 6 10
5 1 7 41
11 0 55 0
0 0 0 0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
0 0 43200 1427
0 0 43200 1427
1 56 4080 1427
1 56 47280 1427
1 57 4860 1427
3 10 18600 1427
3 10 61800 1427
7 18 39240 1427
7 18 82440 1427
7 23 43140 1427
7 24 720 1427
0 38 4680 79
0 39 2340 79
0 40 0 1
1 6 3960 79
1 20 2400 79
1 21 60 79
2 15 0 1
0 0 2700 89
0 28 48 1
1 6 320 33
1 6 40 1
1 8 120 11
0 55 0 1
</pre> |
p03792 | <span class="lang-en">
<p>Score : <var>1300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There is a square-shaped grid with <var>N</var> vertical rows and <var>N</var> horizontal columns.
We will denote the square at the <var>i</var>-th row from the top and the <var>j</var>-th column from the left as <var>(i,\ j)</var>.</p>
<p>Initially, each square is either white or black.
The initial color of the grid is given to you as characters <var>a_{ij}</var>, arranged in a square shape.
If the square <var>(i,\ j)</var> is white, <var>a_{ij}</var> is <code>.</code>. If it is black, <var>a_{ij}</var> is <code>#</code>.</p>
<p>You are developing a robot that repaints the grid.
It can repeatedly perform the following operation:</p>
<ul>
<li>Select two integers <var>i</var>, <var>j</var> (<var>1 ≤ i,\ j ≤ N</var>). Memorize the colors of the squares <var>(i,\ 1)</var>, <var>(i,\ 2)</var>, <var>...</var>, <var>(i,\ N)</var> as <var>c_1</var>, <var>c_2</var>, <var>...</var>, <var>c_N</var>, respectively. Then, repaint the squares <var>(1,\ j)</var>, <var>(2,\ j)</var>, <var>...</var>, <var>(N,\ j)</var> with the colors <var>c_1</var>, <var>c_2</var>, <var>...</var>, <var>c_N</var>, respectively.</li>
</ul>
<p>Your objective is to turn all the squares black.
Determine whether it is possible, and find the minimum necessary number of operations to achieve it if the answer is positive.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 ≤ N ≤ 500</var></li>
<li><var>a_{ij}</var> is either <code>.</code> or <code>#</code>.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Partial Score</h3><ul>
<li>In a test set worth <var>300</var> points, <var>N ≤ 3</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>a_{11}</var><var>...</var><var>a_{1N}</var>
<var>:</var>
<var>a_{N1}</var><var>...</var><var>a_{NN}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If it is possible to turn all the squares black, print the minimum necessary number of operations to achieve the objective.
If it is impossible, print <code>-1</code> instead.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2
#.
.#
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p>For example, perform the operation as follows:</p>
<ul>
<li>Select <var>i = 1</var>, <var>j = 2</var>.</li>
<li>Select <var>i = 1</var>, <var>j = 1</var>.</li>
<li>Select <var>i = 1</var>, <var>j = 2</var>.</li>
</ul>
<p>The transition of the colors of the squares is shown in the figure below:</p>
<div style="text-align: center;">
<img alt="6a0314bb2b1073694a7ef5a062e77b13.png" src="https://atcoder.jp/img/mujin/6a0314bb2b1073694a7ef5a062e77b13.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2
..
..
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>-1
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>2
##
##
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>3
.#.
###
.#.
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>2
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 5</h3><pre>3
...
.#.
...
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 5</h3><pre>5
</pre></section>
</div>
</span> |
p02880 | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Having learned the multiplication table, Takahashi can multiply two integers between <var>1</var> and <var>9</var> (inclusive) together.</p>
<p>Given an integer <var>N</var>, determine whether <var>N</var> can be represented as the product of two integers between <var>1</var> and <var>9</var>. If it can, print <code>Yes</code>; if it cannot, print <code>No</code>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 100</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 <var>N</var> can be represented as the product of two integers between <var>1</var> and <var>9</var> (inclusive), print <code>Yes</code>; if it cannot, print <code>No</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>10
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<p><var>10</var> can be represented as, for example, <var>2 \times 5</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>50
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>No
</pre>
<p><var>50</var> cannot be represented as the product of two integers between <var>1</var> and <var>9</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>81
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>Yes
</pre></section>
</div>
</span> |
p01255 |
<H1><font color="#000">Problem F:</font> Webby Subway</H1>
<p>
You are an officer of the Department of Land and Transport in Oykot City. The department has a plan to
build a subway network in the city central of Oykot.
</p>
<p>
In the plan, <i>n</i> subway lines are built, and each line has two or more stations. Because of technical
problems, a rail track between two stations should be straight and should not have any slope. To make
things worse, no rail track can contact with another rail track, even on a station. In other words, two
subways on the same floor cannot have any intersection.
</p>
<p>
Your job is to calculate the least number of required floors in their plan.
</p>
<H2>Input</H2>
<p>
The input consists of multiple datasets. Each dataset is formatted as follows:
</p>
<pre>
<i>N</i>
<i>Line</i><sub>1</sub>
<i>Line</i><sub>2</sub>
...
<i>Line</i><sub><i>N</i></sub>
</pre>
<p>
Here, <i>N</i> is a positive integer that indicates the number of subway lines to be built in the plan (<i>N</i> ≤ 22),
and <i>Line<sub>i</sub></i> is the description of the <i>i</i>-th subway line with the following format:
</p>
<pre>
<i>S</i>
<i>X</i><sub>1</sub> <i>Y</i><sub>1</sub>
<i>X</i><sub>2</sub> <i>Y</i><sub>2</sub>
...
<i>X</i><sub><i>S</i></sub> <i>Y</i><sub><i>S</i></sub>
</pre>
<p>
<i>S</i> is a positive integer that indicates the number of stations in the line (<i>S</i> ≤ 30), and (<i>X<sub>i</sub></i>, <i>Y<sub>i</sub></i>) indicates
the coordinates of the <i>i</i>-th station of the line (-10000 ≤ <i>X<sub>i</sub></i>, <i>Y<sub>i</sub></i> ≤ 10000). The rail tracks are going to
be built between two consecutive stations in the description. No stations of the same line have the same
coordinates.
</p>
<p>
The input is terminated by a dataset of <i>N</i> = 0, and it should not be processed.
</p>
<H2>Output</H2>
<p>
For each dataset, you should output the least number of required floors.
</p>
<H2>Sample Input</H2>
<pre>
2
2
0 0
10 0
2
0 10
10 10
2
2
0 0
10 10
2
0 10
10 0
3
2
0 0
10 10
2
0 10
10 0
2
1 0
1 10
0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
1
2
3
</pre>
|
p03268 | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given integers <var>N</var> and <var>K</var>. Find the number of triples <var>(a,b,c)</var> of positive integers not greater than <var>N</var> such that <var>a+b,b+c</var> and <var>c+a</var> are all multiples of <var>K</var>.
The order of <var>a,b,c</var> does matter, and some of them can be the same.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N,K \leq 2\times 10^5</var></li>
<li><var>N</var> and <var>K</var> are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of triples <var>(a,b,c)</var> of positive integers not greater than <var>N</var> such that <var>a+b,b+c</var> and <var>c+a</var> are all multiples of <var>K</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>9
</pre>
<p><var>(1,1,1),(1,1,3),(1,3,1),(1,3,3),(2,2,2),(3,1,1),(3,1,3),(3,3,1)</var> and <var>(3,3,3)</var> satisfy the condition.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>5 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>31415 9265
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>27
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>35897 932
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>114191
</pre></section>
</div>
</span> |
p02029 | <h2>O: 千咲ちゃんと遠足 (Chisaki and Picnic)</h2>
<p>幼稚園生の千咲ちゃんは、遠足にお菓子をもっていくことにした。</p>
<p>お菓子は $N$ 個あって、$i$ 番目のお菓子は値段が $A_i$ 円で、美味しさが $B_i$ である。</p>
<p>千咲ちゃんには友達が $M$ 人いて、$j$ 番目の友達は、値段が $C_j$ 円以上のお菓子を $D_j$ 個以上千咲ちゃんがもっていると泣きわめく。</p>
<p>友達が泣きわめいてしまうと千咲ちゃんはかなしいので、そうならないようにお菓子をもっていきたい。</p>
<p>お菓子の美味しさの合計は最大でいくつになるか求めて、千咲ちゃんを助けなさい。</p>
<h3>入力</h3>
<p>1 行目には、お菓子の個数 $N$ と、友達の人数 $M$ が空白区切りで与えられる。</p>
<p>続く $N$ 行のうち $i$ 行目には、$A_i, B_i$ が空白区切りで与えられる。</p>
<p>続く $M$ 行のうち $j$ 行目には、$C_j, D_j$ が空白区切りで与えられる。</p>
<h3>出力</h3>
<p>どの友達も泣かないように持っていくお菓子を選んだ時、その美味しさの合計としてありうる最大値を出力せよ。</p>
<h3>制約</h3>
<ul>
<li>$N, M$ は $1$ 以上 $100 \ 000$ 以下の整数</li>
<li>$A_1, A_2, A_3, \dots, A_N$ は $1$ 以上 $1 \ 000 \ 000 \ 000$ 以下の整数</li>
<li>$B_1, B_2, B_3, \dots, B_N$ は $1$ 以上 $1 \ 000 \ 000 \ 000$ 以下の整数</li>
<li>$A_1 \leq A_2 \leq A_3 \leq \cdots \leq A_N$ を満たす</li>
<li>$C_1, C_2, C_3, \dots, C_M$ は $1$ 以上 $1 \ 000 \ 000 \ 000$ 以下の整数</li>
<li>$D_1, D_2, D_3, \dots, D_M$ は $1$ 以上 $1 \ 000 \ 000 \ 000$ 以下の整数</li>
<li>$C_1 \leq C_2 \leq C_3 \leq \cdots \leq C_M$ を満たす</li>
</ul>
<h3>入力例1</h3>
<pre>
3 1
10 1
20 2
30 3
20 2
</pre>
<h3>出力例1</h3>
<pre>
4
</pre>
<p>$1, 3$ 番目のお菓子を持っていくと、友達を泣かせない範囲で、持ってくるお菓子の美味しさの合計を最大化することができます。</p>
<p>$2, 3$ 番目のお菓子両方を持っていくと、「値段が $20$ 円以上のお菓子を $2$ 個以上持ってくる」ことになるので、友達が泣きます。</p>
<h3>入力例2</h3>
<pre>
5 3
10 1
20 4
30 5
40 2
50 3
20 3
30 4
40 2
</pre>
<h3>出力例2</h3>
<pre>
10
</pre>
<p>$1, 2, 3$ 番目のお菓子を持っていくことによって、どの友達も泣かせない条件のもとで、持ってくるお菓子の美味しさの合計を最大化することができます。</p>
|
p00014 |
<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>Integral</H1>
<p>
Write a program which computes the area of a shape represented by the following three lines:<br/>
<br/>
$y = x^2$<br/>
$y = 0$<br/>
$x = 600$<br/>
<br/>
<!--<center><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_integralF1"></center>-->
</p>
<p>
It is clear that the area is $72000000$, if you use an integral you learn in high school. On the other hand, we can obtain an approximative area of the shape by adding up areas of many rectangles in the shape as shown in the following figure:
</p>
<center><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_integral"><br/>
$f(x) = x^2$<br/>
<br/>
</center>
<!--
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_integralF2">
</center>
-->
<p>
The approximative area $s$ where the width of the rectangles is $d$ is:<br/>
<br/>
area of rectangle where its width is $d$ and height is $f(d)$ $+$ <br/>
area of rectangle where its width is $d$ and height is $f(2d)$ $+$ <br/>
area of rectangle where its width is $d$ and height is $f(3d)$ $+$ <br/>
...<br/>
area of rectangle where its width is $d$ and height is $f(600 - d)$ <br/>
</p>
<p>
The more we decrease $d$, the higer-precision value which is close to $72000000$ we could obtain. Your program should read the integer $d$ which is a divisor of $600$, and print the area $s$.
</p>
<H2>Input</H2>
<p>
The input consists of several datasets. Each dataset consists of an integer $d$ in a line. The number of datasets is less than or equal to 20.
</p>
<H2>Output</H2>
<p>
For each dataset, print the area $s$ in a line.
</p>
<H2>Sample Input</H2>
<pre>
20
10
</pre>
<H2>Output for the Sample Input</H2>
<pre>
68440000
70210000
</pre>
|
p02183 | <h2>B: テトリス</h2>
<h3>問題</h3>
<p>
縦 4 マス x 横 10 マスの長方形からなる盤面を考えます。
縦 1 マス x 横 1 マスぶんの正方形をブロックと呼びます。
</p>
<p>ブロックを 4 つ繋げたものをテトロミノと呼び、以下の 7 種類(およびこれらに対して 90 度回転を任意回行ったもの)があります。</p>
<!--<object data="img/mino.png" type="image/png" width="640"></object>-->
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/ACPC2019Day3_B_tetoris_mino.png" type="image/png" width="640"/>
<p>さて、盤面のうち 28 個のマスにブロックが置かれた状態を考えます。</p>
<!--<object data="img/board.png" type="image/png" width="640"></object>-->
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/ACPC2019Day3_B_tetoris_board.png" type="image/png" width="640"/>
<p>左の図のように、各ブロック(赤色で表される)はマスに合うように置かれます。右の図のように半端な位置に置かれることはありません。</p>
<p>4 つのテトロミノ <var>t_1</var>, <var>t_2</var>, <var>t_3</var>, <var>t_4</var> が与えられるので、このうちちょうど 3 つを選んですきな位置に置くことで、盤面全体にブロックが置かれた状態にできるか判定してください。</p>
<p>ただし、テトロミノを配置するにあたり、以下の条件をすべて満たさなければなりません。</p>
<ul>
<li>ブロック同士が重なってはいけない。</li>
<li>与えられたテトロミノを回転させてはいけない。</li>
<li>各 <var>i</var> (<var>1 \leq i \leq 4</var>) について、テトロミノ <var>t_i</var> を 2 回以上使ってはいけない。</li>
<li>テトロミノの各ブロックは盤面の外に出てはいけない。</li>
</ul>
<p>盤面が <var>n</var> 個与えられるので、各盤面についてこの判定を行い、盤面全体にブロックを置けるならば <code>Yes</code>、不可能ならば <code>No</code> を出力してください。</p>
<h3>入力形式</h3>
<pre>
<var>t_1</var>
<var>t_2</var>
<var>t_3</var>
<var>t_4</var>
<var>n</var>
<var>B_1</var>
…
<var>B_n</var>
</pre>
<p>
はじめに、使えるテトロミノ <var>t_1</var>, <var>t_2</var>, <var>t_3</var>, <var>t_4</var> が与えられます。
各 <var>t_i</var> (<var>1 \leq i \leq 4</var>) は以下の形式で与えられます。
</p>
<pre>
<var>h</var> <var>w</var>
<var>s_1</var>
…
<var>s_h</var>
</pre>
<p>これはテトロミノを含む縦 <var>h</var> x 横 <var>w</var> マスの長方形であり、テトロミノの形状は <var>s_1 … s_h</var> で表されます。長方形のうち、テトロミノを表す部分は <code>#</code>、それ以外の部分は <code>.</code> で表されます。各長方形に <code>#</code> はちょうど 4 つ含まれます。<code>.</code> のみからなる行または列は存在しません。</p>
<p>
次に、盤面の個数 <var>n</var> が与えられます。
その後、<var>4\times 10</var> マスで表される盤面 <var>B</var> が <var>n</var> 個与えられます。
ブロックがある位置が <code>#</code>、ない位置が <code>.</code> で示されます。
</p>
<h3>制約</h3>
<ul>
<li><var>1\leq n\leq 10^5</var></li>
<li>与えられる各テトロミノは上で述べた条件に違反しない</li>
<li>各盤面について、<code>#</code> はちょうど 28 個存在する</li>
</ul>
<p>たとえば、以下のようなテトロミノは与えられない。</p>
<pre>
3 3
..#
.#.
##.
</pre>
<p>(連結でないブロックがあり、これは上で述べたテトロミノの条件を満たさない)</p>
<pre>
3 3
...
.#.
###
</pre>
<p>(一番上の行が <code>.</code> のみからなる)</p>
<h3>出力形式</h3>
<p><var>n</var> 行にわたって出力してください。<var>i</var> 行目には盤面 <var>B_i</var> に対する判定結果を <code>Yes</code> または <code>No</code> で出力してください。</p>
<h3>入力例1</h3>
<pre>
2 3
##.
.##
2 3
#..
###
1 4
####
2 3
###
.#.
2
####....##
####...###
####..####
####...###
###..#####
###...####
###...####
###....###
</pre>
<h3>出力例1</h3>
<pre>
Yes
Yes
</pre>
<p><var>t_1</var>, <var>t_2</var>, <var>t_3</var>, <var>t_4</var> に相当するテトロミノが置かれる場所をそれぞれ <code>1</code>, <code>2</code>, <code>3</code>, <code>4</code> で示すと、各盤面は次のように敷き詰められます。</p>
<pre>
####3333##
####444###
####24####
####222###
</pre>
<pre>
###11#####
###211####
###222####
###3333###
</pre>
<p>各盤面について、別々のテトロミノを選んで構いません。</p>
<h3>入力例2</h3>
<pre>
1 4
####
1 4
####
2 2
##
##
2 2
##
##
4
######....
....######
#######..#
#######..#
....######
######....
....######
##########
######.#.#
######.#.#
##..##.#.#
##..##.#.#
##.###.#.#
#.#.###.##
#.#.###.##
##.###.#.#
</pre>
<h3>出力例2</h3>
<pre>
Yes
No
No
No
</pre>
<p><code>####</code> が足りないため、2 つ目の盤面をブロックで敷き詰めることはできません。</p>
<p>また、テトロミノを回転させることはできないので、3 つ目の盤面を敷き詰めることもできません。</p>
<p>また、4 つ目の盤面のようなブロックの配置の仕方もありえることに注意してください。すなわち、入力で与えられる盤面はテトロミノを組み合わせて作られる盤面とは限りません。</p>
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.