question_id stringlengths 6 6 | content stringlengths 1 27.2k |
|---|---|
p00899 |
<H1><font color="#000">Problem F: </font>City Merger</H1>
<p>
Recent improvements in information and communication technology have made it possible to provide municipal service to a wider area more quickly and with less costs. Stimulated by this, and probably for saving their not sufficient funds, mayors of many cities started to discuss on mergers of their cities.
</p>
<p>
There are, of course, many obstacles to actually put the planned mergers in practice. Each city has its own culture of which citizens are proud. One of the largest sources of friction is with the name of the new city. All citizens would insist that the name of the new city should have the original name of their own city at least as a part of it. Simply concatenating all the original names would, however, make the name too long for everyday use.
</p>
<p>
You are asked by a group of mayors to write a program that finds the shortest possible name for the new city that includes all the original names of the merged cities. If two or more cities have common parts, they can be overlapped. For example, if "FUKUOKA", "OKAYAMA", and "YAMAGUCHI" cities are to be merged, "FUKUOKAYAMAGUCHI" is such a name that include all three of the original city names. Although this includes all the characters of the city name "FUKUYAMA" in this order, it does not appear as a consecutive substring, and thus "FUKUYAMA" is not considered to be included in the name.
</p>
<H2>Input</H2>
<p>
The input is a sequence of datasets. Each dataset begins with a line containing a positive integer <i>n</i> (<i>n</i> ≤ 14), which denotes the number of cities to be merged. The following <i>n</i> lines contain the names of the cities in uppercase alphabetical letters, one in each line. You may assume that none of the original city names has more than 20 characters. Of course, no two cities have the same name.
</p>
<p>
The end of the input is indicated by a line consisting of a zero.
</p>
<H2>Output</H2>
<p>
For each dataset, output the length of the shortest possible name of the new city in one line. The output should not contain any other characters.
</p>
<H2>Sample Input</H2>
<pre>
3<br>FUKUOKA<br>OKAYAMA<br>YAMAGUCHI<br>3<br>FUKUOKA<br>FUKUYAMA<br>OKAYAMA<br>2<br>ABCDE<br>EDCBA<br>4<br>GA<br>DEFG<br>CDDE<br>ABCD<br>2<br>ABCDE<br>C<br>14<br>AAAAA<br>BBBBB<br>CCCCC<br>DDDDD<br>EEEEE<br>FFFFF<br>GGGGG<br>HHHHH<br>IIIII<br>JJJJJ<br>KKKKK<br>LLLLL<br>MMMMM<br>NNNNN<br>0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
16<br>19<br>9<br>9<br>5<br>70
</pre> |
p00030 |
<H1>整数の和</H1>
<p>
0 から 9 の数字から異なる <var>n</var> 個の数を取り出して合計が <var>s</var> となる組み合わせの数を出力するプログラムを作成してください。<var>n</var> 個の数はおのおの 0 から 9 までとし、1つの組み合わせに同じ数字は使えません。たとえば、<var>n</var> が 3 で <var>s</var> が 6 のとき、3 個の数字の合計が 6 になる組み合わせは、<br/>
<br/>
1 + 2 + 3 = 6<br/>
0 + 1 + 5 = 6<br/>
0 + 2 + 4 = 6<br/>
<br/>
の 3 通りとなります。
</p>
<H2>Input</H2>
<p>
複数のデータセットが与えられます。各データセットに <var>n</var> (1 ≤ <var>n</var> ≤ 9) と <var>s</var> (0 ≤ <var>s</var> ≤ 100) が1つのスペースで区切られて1行に与えられます。<var>n</var> と <var>s</var> が共に 0 のとき入力の最後とします(この場合は処理せずにプログラムを終了する)。
</p>
<p>
データセットの数は 50 を超えません。
</p>
<H2>Output</H2>
<p>
各データセットに対して、<var>n</var> 個の整数の和が <var>s</var> になる組み合わせの数を1行に出力して下さい。
</p>
<H2>Sample Input</H2>
<pre>
3 6
3 1
0 0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
3
0
</pre>
|
p00460 |
<H1> ビンゴ</H1>
<h2>問題</h2>
<p>
あるプログラミングコンテストでは,競技後の懇親会でビンゴゲームをする習わしがある.しかし,このビンゴゲームで使うビンゴカードは少々特殊で,以下の条件に従って作成される.
</p>
<ul>
<li> ビンゴカードは N 行 N 列のマス目に区切られており,各マス目には正整数が1つずつ書かれている.それらの整数は全て異なる.</li>
<li> マス目に書かれている整数は 1 以上 M 以下である.</li>
<li> ビンゴカードに書かれているN×N個の整数の合計は S である.</li>
<li> どの列を見たときも,上から下に向かって整数は昇順に並んでいる.</li>
<li> どのマス目の整数も,そのマス目より左の列のどの整数よりも大きい.</li>
</ul>
<p>
以下は, N = 5, M = 50, S = 685 のときのビンゴカードの例である.
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_2009-yo-t6">
</center>
<br>
<p>
懇親会のために上の条件を満たすビンゴカードをできるだけたくさん作りたい.ただし,同一のカードを2枚以上作ってはならない.作ることができるビンゴカードの枚数の最大値を 100000 で割った余りを出力するプログラムを作成せよ.
</p>
<h2>入力</h2>
<p>
入力は複数のデータセットからなる.各データセットは以下の形式で与えられる.
</p>
<p>
入力は1行からなり, その行にはビンゴカードのサイズ N (1≤N≤7), マス目に書かれている整数の上限 M (1≤M≤2000),ビンゴカードに書かれている整数の合計 S (1≤S≤3000) を表す3つの正整数が空白区切りで書かれている.ただし, 与えられるどの入力データにおいても,条件を満たすビンゴカードを1枚以上作ることができる.
</p>
<p>
N, M, S が 0 のとき入力の終了を示す. データセットの数は 5 を超えない.
</p>
<h2>出力</h2>
<p>
データセットごとに, 作ることができるビンゴカードの枚数の最大値を 100000 で割った余りを1行に出力せよ.
</p>
<h2>入出力例</h2>
<h3>入力例</h3>
<pre>
3 9 45
3 100 50
5 50 685
0 0 0
</pre>
<h3>出力例</h3>
<pre>
1
7
74501
</pre>
<p>
3つ目の入力例に対して,作ることができるビンゴカードの枚数の最大値は 642499974501 であるので, 100000 で割った余りの 74501 を出力する.
</p>
<div class="source">
<p class="source">
上記問題文と自動審判に使われるデータは、<a href="http://www.ioi-jp.org">情報オリンピック日本委員会</a>が作成し公開している問題文と採点用テストデータです。
</p>
</div>
|
p00175 |
<H1>4進数</H1>
<p>
10 進数は現在使われている一般的な記数法で、0、1、2、3、4、5、6、7、8、9 の10 個の記号を用いて全ての数字を表します。
</p>
<p>
2 進数はコンピュータの世界でよく使われる記数法で、0 と1 の二つの記号を用いて全ての数字を表します。
</p>
<p>
4 進数で用いられるのは 0、1、2、3 の 4 つの数字だけです。4 進数では 0 から順に数を増やしていったときに 4 まで来たら次の位に桁上がりします。そのため、10 進数の 4 は桁上がりして「10」という表現になります。
</p>
<center>
<table>
<tr>
<td width="80">10 進数</td>
<td width="40">0</td>
<td width="40">1</td>
<td width="40">2</td>
<td width="40">3</td>
<td width="40">4</td>
<td width="40">5</td>
<td width="40">6</td>
<td width="40">7</td>
<td width="40">8</td>
<td width="40">9</td>
<td width="40">10</td>
<td>...</td>
</tr>
<tr>
<td>2 進数</td>
<td>0</td>
<td>1</td>
<td>10</td>
<td>11</td>
<td>100</td>
<td>101</td>
<td>110</td>
<td>111</td>
<td>1000</td>
<td>101</td>
<td>1010</td>
<td>...</td>
</tr>
<tr>
<td>4 進数</td>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td>20</td>
<td>21</td>
<td>22</td>
<td>...</td>
</tr>
</table>
<br>
</center>
<p>
昔ハワイでは魚やタロイモを指にはさんで数えていたので、10 進数ではなく4 進数を使っていたそうです。
</p>
<p>
10 進数で入力される整数 <var>n</var> を 4 進数に変換し出力するプログラムを作成してください。
</p>
<H2>Input</H2>
<p>
複数のデータセットの並びが入力として与えられます。入力の終わりは -1 ひとつの行で示されます。各データセットとして1つの整数 <var>n</var> (0 ≤ <var>n</var> ≤ 1000000) が1行に与えられます。
</p>
<p>
データセットの数は 2000 を超えません。
</p>
<H2>Output</H2>
<p>
入力データセット毎に 4 進数に変換した結果を1行に出力します。
</p>
<H2>Sample Input</H2>
<pre>
7
4
0
12
10
10000
-1
</pre>
<H2>Output for the Sample Input</H2>
<pre>
13
10
0
30
22
2130100
</pre>
|
p02148 | <h1>Problem L: Tousa Tree</h1>
<h2>Problem</h2>
<p>君は一昨日、友人のトウサから一つ、頼み事をされた。なんでも、木に等差数列を飾って誕生日パーティーに使いたいのだが、木が大きすぎて一人ではとてもできないのだという。そこで、優秀なプログラマである君に、効率的に木に等差数列を飾るプログラムを書いてもらいたいそうだ。君とトウサは旧知の親友である。君はトウサの頼み事を受けることにした。</p>
<p>飾り付けをする$N$ノードの木が与えられる。木のノードには$0$から$N-1$までの番号が振られている。最初、木のノードの点数は全て$0$である。プログラムは、以下の二つのクエリに対応しなければならない。</p>
<ul>
<li>$0$.木の指定された区間のノードに書かれた点数の和を求める。</li>
<li>$1$.木の指定された区間のノードに等差数列を足す。</li>
</ul>
<p>トウサの誕生日を祝うために、木を美しく飾ろう。</p>
<h2>Input</h2>
<p>入力は以下の形式で与えられる。</p>
<pre>
$N$ $Q$
$A_0$ $B_0$
$A_1$ $B_1$
:
$A_{N-2}$ $B_{N-2}$
$COM_0$
$COM_1$
:
$COM_{Q-1}$
</pre>
<p>
木のノード数$N$とクエリの数$Q$が$1$行に与えられる。続いて$N-1$行に木の辺の情報が与えられる。$A_i$,$B_i$は、ノード$A_i$と$B_i$の間に直接辺があることを示している。その後、$Q$個の命令$COM_j$が与えられる。<br>
</p>
<p>
$COM_j$の形式は以下の通りである。
</p>
<pre>
$0$ $S$ $T$
</pre>
<p>
$S$、$T$は頂点の番号である。区間の指定は二つのノードによって行われる。$S$から$T$までの最短経路が指定された区間である。区間に含まれているノードに書かれた点数の総和を報告する。ただし、$S$と$T$も区間に含む。答えは非常に大きくなることがあるので、$10^9+7$で割ったあまりを出力すること。<br>
</p>
<pre>
$1$ $S$ $T$ $K$ $M$
</pre>
<p>
上と同じく、$S$と$T$で指定された区間に含まれているノードに点数を足す。足す点数は、$S$からの最短距離を$L$として、以下のように算出される。<br>
</p>
<pre>
$K+L{\times}M$
</pre>
<h2>Constraints</h2>
<p>入力は以下の条件を満たす。</p>
<ul>
<li>入力はすべては整数</li>
<li>$1 \le N,Q \le 10^5$</li>
<li>$0 \le A,B,S,T \le N-1$</li>
<li>$0 \le K,M \le 10^5$</li>
</ul>
<h2>Output</h2>
<p>$0$のクエリのとき、指定された区間に含まれているノードに書かれた点数の総和を一行に出力する。</p>
<h2>Sample Input 1</h2>
<pre>
3 3
0 2
1 2
1 1 0 0 1
0 0 2
0 0 1
</pre>
<h2>Sample Output 1</h2>
<pre>
3
3
</pre>
<p>
まず、$1-2-0$の区間に初項$0$、公差$1$の数列を足す。ノードの点数は$0,1,2$の順に$2,0,1$となる。<br>
次に、$0-2$の区間のノードの点数の和を出力する。$2+1=3$なので、$3$を出力する。<br>
次に、$0-2-1$の区間のノードの点数の和を出力する。$2+1+0=3$なので、$3$を出力する。
</p>
<h2>Sample Input 2</h2>
<pre>
5 6
1 0
2 0
3 2
4 1
1 1 4 0 4
0 4 1
1 3 0 7 3
0 2 2
1 4 1 8 0
0 4 2
</pre>
<h2>Sample Output 2</h2>
<pre>
4
10
43
</pre>
|
p00525 |
<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> 切り取り線(Cutting)
</H1>
<br/>
<p>
JOI 君はペーパークラフトが趣味である.今日もJOI 君はペーパークラフトの作品を作ろうとしている.まず,JOI 君は設計図にしたがって1 枚の長方形の紙に $N$ 本の切り取り線を印刷した.各切り取り線は,紙の縦または横の辺に平行な線分である.
</p>
<p>
紙を切り出してできるすべての部分は作品の中で何らかの部品として用いられる.当然のことながら,
部品数の多い作品は製作が大変である.JOI 君は,すべての切り取り線にしたがって紙を切り出したとき,
紙がいくつの部分に分かれるかを知りたい.
</p>
<h2>課題</h2>
<p>
紙の大きさと,$N$ 本の切り取り線の情報が与えられる.これらの切り取り線にしたがって紙を切り分けたとき,紙がいくつの部分に分かれるかを求めるプログラムを作成せよ.
</p>
<h2>入力</h2>
<p>
標準入力から以下のデータを読み込め.
</p>
<ul>
<li> 1 行目には,整数 $W, H, N$ が空白を区切りとして書かれている.$W$ は紙の横の辺の長さを,$H$ は紙の縦の辺の長さを,$N$ は切り取り線の本数をそれぞれ表す.紙の左下,右下,左上,右上を,それぞれ座標 $(0, 0), (W, 0), (0, H), (W, H)$ で表す.
</li>
<li> 続く $N$ 行のうちの $i$ 行目 $(1 \leq i \leq N)$ には,整数 $A_i, B_i, C_i, D_i (0 \leq A_i \leq C_i \leq W, 0 \leq B_i \leq D_i \leq H)$ が空白を区切りとして書かれている.これは $i$ 番目の切り取り線が$(A_i, B_i)$ と$(C_i, D_i)$ を結ぶ線分であることを表す.この線分は紙のいずれかの辺に平行な線分である.すなわち,$A_i = C_i$ と $B_i = D_i$ のうちのちょうど1 つが成り立つ.また,ある切り取り線と,それに平行な他の切り取り線が共有点を持つことはなく,ある切り取り線と,それに平行な紙の辺が共有点を持つこともない.
</li>
</ul>
<h2>出力</h2>
<p>
標準出力に,紙がいくつの部分に分かれるかを表す整数を1 行で出力せよ.
</p>
<h2>制限</h2>
<p>
すべての入力データは以下の条件を満たす.
</p>
<ul>
<li>$ 1 \leq W \leq 1000000 000$</li>
<li>$ 1 \leq H \leq 1000000 000$</li>
<li>$ 1 \leq N \leq 100000$</li>
</ul>
<h2>入出力例</h2>
<h3>入力例 1 </h3>
<pre>
10 10 5
6 0 6 7
0 6 7 6
2 3 9 3
2 3 2 10
1 9 8 9
</pre>
<h3>出力例 1 </h3>
<pre>
4
</pre>
<p>
この入力の場合,切り取り線は下図のようになる.
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_cutting_1">
</center>
<br>
<p>
よって,切り取り線によって紙は4 つの部分に分かれる.
</p>
<br>
<h3>入力例 2 </h3>
<pre>
13 7 28
1 1 4 1
1 1 1 3
2 2 3 2
2 2 2 3
1 3 2 3
3 2 3 6
4 1 4 6
3 6 4 6
5 1 8 1
5 1 5 6
6 2 7 2
6 2 6 5
7 2 7 5
6 5 7 5
8 1 8 6
5 6 8 6
9 1 12 1
9 1 9 2
9 2 10 2
12 1 12 2
11 2 12 2
10 2 10 5
9 5 10 5
9 5 9 6
11 2 11 5
11 5 12 5
12 5 12 6
9 6 12 6
</pre>
<h3>出力例 2 </h3>
<pre>
5
</pre>
<p>
この入力の場合,切り取り線は次ページの図のようになる.
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_cutting_2">
</center>
<br>
<p>
よって,切り取り線によって紙は5 つの部分に分かれる.
</p>
<div class="source">
<p class="source">
問題文と自動審判に使われるデータは、<a href="http://www.ioi-jp.org">情報オリンピック日本委員会</a>が作成し公開している問題文と採点用テストデータです。
</p>
</div>
|
p00876 |
<H1><font color="#000">Problem C:</font> Swimming Jam</H1>
<p>
Despite urging requests of the townspeople, the municipal office cannot afford to improve many
of the apparently deficient city amenities under this recession. The city swimming pool is one
of the typical examples. It has only two swimming lanes. The Municipal Fitness Agency, under
this circumstances, settled usage rules so that the limited facilities can be utilized fully.
</p>
<p>
Two lanes are to be used for one-way swimming of different directions. Swimmers are requested
to start swimming in one of the lanes, from its one end to the other, and then change the lane to
swim his/her way back. When he or she reaches the original starting end, he/she should return
to his/her initial lane and starts swimming again.
</p>
<p>
Each swimmer has his/her own natural constant pace. Swimmers, however, are not permitted
to pass other swimmers except at the ends of the pool; as the lanes are not wide enough, that
might cause accidents. If a swimmer is blocked by a slower swimmer, he/she has to follow the
slower swimmer at the slower pace until the end of the lane is reached. Note that the blocking
swimmer’s natural pace may be faster than the blocked swimmer; the blocking swimmer might
also be blocked by another swimmer ahead, whose natural pace is slower than the blocked
swimmer. Blocking would have taken place whether or not a faster swimmer was between them.
</p>
<p>
Swimmers can change their order if they reach the end of the lane simultaneously. They change
their order so that ones with faster natural pace swim in front. When a group of two or more
swimmers formed by a congestion reaches the end of the lane, they are considered to reach there
simultaneously, and thus change their order there.
</p>
<p>
The number of swimmers, their natural paces in times to swim from one end to the other, and
the numbers of laps they plan to swim are given. Note that here one "lap" means swimming
from one end to the other and then swimming back to the original end. Your task is to calculate
the time required for all the swimmers to finish their plans. All the swimmers start from the
same end of the pool at the same time, the faster swimmers in front.
</p>
<p>
In solving this problem, you can ignore the sizes of swimmers' bodies, and also ignore the time
required to change the lanes and the order in a group of swimmers at an end of the lanes.
</p>
<H2>Input</H2>
<p>
The input is a sequence of datasets. Each dataset is formatted as follows.
</p>
<p>
<i>n</i><br>
<i>t</i><sub>1</sub> <i>c</i><sub>1</sub><br>
...<br>
<i>t<sub>n</sub> c<sub>n</sub></i><br>
</p>
<p>
<i>n</i> is an integer (1 ≤ <i>n</i> ≤ 50) that represents the number of swimmers. <i>t<sub>i</sub></i> and <i>c<sub>i</sub></i> are integers
(1 ≤ <i>t<sub>i</sub></i> ≤ 300, 1 ≤ <i>c<sub>i</sub></i> ≤ 250) that represent the natural pace in times to swim from one end
to the other and the number of planned laps for the <i>i</i>-th swimmer, respectively. ti and ci are
separated by a space.
</p>
<p>
The end of the input is indicated by a line containing one zero.
</p>
<H2>Output</H2>
<p>
For each dataset, output the time required for all the swimmers to finish their plans in a line.
No extra characters should occur in the output.
</p>
<H2>Sample Input</H2>
<pre>
2
10 30
15 20
2
10 240
15 160
3
2 6
7 2
8 2
4
2 4
7 2
8 2
18 1
0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
600
4800
36
40
</pre>
|
p03759 | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Three poles stand evenly spaced along a line. Their heights are <var>a</var>, <var>b</var> and <var>c</var> meters, from left to right.
We will call the arrangement of the poles <em>beautiful</em> if the tops of the poles lie on the same line, that is, <var>b-a = c-b</var>.</p>
<p>Determine whether the arrangement of the poles is beautiful.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq a,b,c \leq 100</var></li>
<li><var>a</var>, <var>b</var> and <var>c</var> are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>a</var> <var>b</var> <var>c</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <code>YES</code> if the arrangement of the poles is beautiful; print <code>NO</code> otherwise.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 4 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>YES
</pre>
<p>Since <var>4-2 = 6-4</var>, this arrangement of poles is beautiful.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2 5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>NO
</pre>
<p>Since <var>5-2 \neq 6-5</var>, this arrangement of poles is not beautiful.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>3 2 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>YES
</pre>
<p>Since <var>1-2 = 2-3</var>, this arrangement of poles is beautiful.</p></section>
</div>
</span> |
p01764 |
<h1>E : Sigma / Σ</h1>
<h1>Problem</h1>
<p>小学生のづあい君は同じ足し算にも得意不得意があり、 0 以上の整数 <em>X</em>, <em>Y</em> に対して、<em>X</em> + <em>Y</em> を計算するのにかかる時間(秒)が次のように決まる。</p>
<ul>
<li>1 の位から順番に計算していく。</li>
<li>10 進数表記で 10<sup><em>i</em></sup> 位の計算には <em>x<sub>i</sub></em> × <em>y<sub>i</sub></em> + <em>c<sub>i</sub></em> 秒かかる。
<ul>
<li><em>x<sub>i</sub></em> は <em>X</em> の10進数表記で 10<sup><em>i</em></sup> の位の数である。</li>
<li><em>y<sub>i</sub></em> は <em>Y</em> の10進数表記で 10<sup><em>i</em></sup> の位の数である。</li>
<li>ただし <em>i</em> が <em>X</em> の桁数より大きくなる時は <em>x<sub>i</sub></em> = 0 とする。<em>Y</em> についても同様である。</li>
<li><em>c<sub>i</sub></em> は、 <em>i</em> = 0 または <em>x<sub>i-1</sub></em> + <em>y<sub>i-1</sub></em> + <em>c<sub>i-1</sub></em> < 10 ならば 0、 そうでなければ 1 である。</li>
</ul></li>
<li>0 以上の整数 i についての <em>x<sub>i</sub></em> × <em>y<sub>i</sub></em> + <em>c<sub>i</sub></em> の総和が <em>X</em> + <em>Y</em> の計算にかかる時間である。</li>
</ul>
<p>ある日、づあい君の先生は長さ <em>N</em> の整数列 <em>a<sub>0</sub></em>, ... , <em>a<sub>N-1</sub></em> の和を計算する宿題を出した。づあい君は数列の順番を並び替えることはできないが、足し算の式に括弧を付けることにより、足し算の順番を自由に変えることができる。例えば <em>N</em> = 4 の場合、順番は次の 5 通りある。</p>
<ul>
<li>((<em>a<sub>0</sub></em> + <em>a<sub>1</sub></em>) + <em>a<sub>2</sub></em>) + <em>a<sub>3</sub></em></li>
<li>(<em>a<sub>0</sub></em> + <em>a<sub>1</sub></em>) + (<em>a<sub>2</sub></em> + <em>a<sub>3</sub></em>)</li>
<li>(<em>a<sub>0</sub></em> + (<em>a<sub>1</sub></em> + <em>a<sub>2</sub></em>)) + <em>a<sub>3</sub></em></li>
<li><em>a<sub>0</sub></em> + ((<em>a<sub>1</sub></em> + <em>a<sub>2</sub></em>) + <em>a<sub>3</sub></em>)</li>
<li><em>a<sub>0</sub></em> + (<em>a<sub>1</sub></em> + (<em>a<sub>2</sub></em> + <em>a<sub>3</sub></em>))</li>
</ul>
<p>それぞれの括弧 (<em>x</em> + <em>y</em>) の中で上で説明した時間がかかるとすると、総和を求めるのにかかる時間は最小で何秒になるだろうか。</p>
<h1>Input</h1>
<p>入力は次のような形式で与えられる。</p>
<p><em>N</em><br><em>a<sub>0</sub></em> <em>a<sub>1</sub></em> ... <em>a<sub>N-1</sub></em></p>
<h1>Constraints</h1>
<ul>
<li>入力はすべて整数である。</li>
<li>2 ≦ <em>N</em> ≦ 300</li>
<li>0 ≦ <em>a<sub>i</sub></em> ≦ 1,000,000</li>
</ul>
<h1>Output</h1>
<p>答えの秒数を1行で出力せよ。</p>
<h1>Samples</h1>
<h2>Sample Input 1</h2>
<pre>3
1 2 3</pre>
<h2>Sample Output 1</h2>
<pre>11</pre>
<p>次のように計算すると最も速い。</p>
<ul>
<li>2 + 3 = 5 ( 2 × 3 = 6 秒かかる)</li>
<li>1 + 5 = 6 ( 1 × 5 = 5 秒かかる)</li>
</ul> |
p03309 | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Snuke has an integer sequence <var>A</var> of length <var>N</var>.</p>
<p>He will freely choose an integer <var>b</var>.
Here, he will get sad if <var>A_i</var> and <var>b+i</var> are far from each other.
More specifically, the <em>sadness</em> of Snuke is calculated as follows:</p>
<ul>
<li><var>abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))</var></li>
</ul>
<p>Here, <var>abs(x)</var> is a function that returns the absolute value of <var>x</var>.</p>
<p>Find the minimum possible sadness of Snuke.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 2 \times 10^5</var></li>
<li><var>1 \leq A_i \leq 10^9</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum possible sadness of Snuke.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5
2 2 3 5 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>If we choose <var>b=0</var>, the sadness of Snuke would be <var>abs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2</var>.
Any choice of <var>b</var> does not make the sadness of Snuke less than <var>2</var>, so the answer is <var>2</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>9
1 2 3 4 5 6 7 8 9
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>6
6 5 4 3 2 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>18
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>7
1 1 1 1 2 3 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>6
</pre></section>
</div>
</span> |
p01334 |
<h1><font color="#000">Problem B:</font> Let's JUMPSTYLE</h1>
<h2>Description</h2>
<p>踊りの好きなFは,とあるダンスホールでJUMPSTYLEと呼ばれるハードコアな踊りの練習をすることにした.</p>
<p>フロアはN×Nの格子状にタイルが敷き詰められている.
踊りの練習をサポートするため,各タイルには次のステップに飛び移るべきタイルの座標が書かれている.Fは日々の練習によって強靭な運動神経を持っており,フロア内の任意のタイルへ飛び移ることができる.</p>
<p>Fは,十分に長い時間この踊りを続けていけば,いつか定常状態に入り,同じルートをただループする状態になることに気付いた.Fはこのようなループがこのフロアにいくつ存在するのか疑問に思い,プログラマであるあなたに相談することにした.</p>
<h2>Input</h2>
<p>入力は複数のテストケースから成る.</p>
<p>各テストケースはフロアの1辺の長さNを含む1行で始まる.(1 ≦ N ≦ 100)</p>
<p>続くN行は,フロアの各タイルに書いてあるジャンプ先の座標を以下のように表している.</p>
<p><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_03Bmath" alt="
\begin{array}{ccccccc}
x_{0,0} & y_{0,0} & x_{1,0} & y_{1,0} & \cdots & x_{N-1,0} & y_{N-1,0} \\
x_{0,1} & y_{0,1} & x_{1,1} & y_{1,1} & \cdots & x_{N-1,1} & y_{N-1,1} \\
\vdots & \vdots & \vdots & \vdots & \ddots & \vdots & \vdots \\
x_{0,N-1} & y_{0,N-1} & x_{1,N-1} & y_{1,N-1} & \cdots & x_{N-1,N-1} & y_{N-1,N-1}
\end{array}
">
</p>
<p><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_03Bmath2" alt="x_{i,j},\ y_{i,j}"> は,それぞれ座標(i, j)にあるタイルに書いてあるジャンプ先のx座標およびy座標を表す.座標は0-originであり,すべての座標の値は0以上N未満の整数である.</p>
<p>入力は0のみから成る行で終わる.</p>
<h2>Output</h2>
<p>各テストケースに対し,異なるループの個数を1行で出力する.</p>
<h2>Sample Input</h2>
<pre>1
0 0
2
1 1 0 1
1 0 0 0
2
1 1 0 1
1 1 1 0
3
0 1 2 2 2 1
0 2 1 2 2 1
0 0 0 1 1 1
4
3 2 2 0 3 2 2 1
1 1 0 3 1 1 3 1
0 3 2 3 3 0 2 3
1 1 1 1 3 2 1 3
0
</pre>
<h2>Output for Sample Input</h2>
<pre>1
2
1
2
3
</pre>
|
p01123 | <!--<h2>Problem G</h2>-->
<!-- begin en only -->
<h3>Let's Move Tiles!</h3>
<!-- end en only -->
<!-- begin en only -->
<p>
You have a framed square board forming a grid of square cells.
Each of the cells is either empty or with a square tile fitting in the cell engraved with a Roman character.
You can tilt the board to one of four directions, away from you, toward you, to the left, or to the right,
making all the tiles slide up, down, left, or right, respectively, until they are squeezed to an edge frame of the board.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
The figure below shows an example of how the positions of the tiles change by tilting the board toward you.
</p>
<!-- end en only -->
<center><img src="https://judgeapi.u-aizu.ac.jp/resources/images/ICPCDomestic2019_G1.png" width="50%"></center>
<!-- begin en only -->
<p>
Let the characters <tt>U</tt>, <tt>D</tt>, <tt>L</tt>, and <tt>R</tt> represent the operations of tilting the board away from you, toward you, to the left, and to the right, respectively.
Further, let strings consisting of these characters represent the sequences of corresponding operations.
For example, the string <tt>DRU</tt> means a sequence of tilting toward you first, then to the right, and, finally, away from you.
This will move tiles down first, then to the right, and finally, up.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
To deal with very long operational sequences, we introduce a notation for repeated sequences.
For a non-empty sequence <i>seq</i>, the notation <tt>(</tt><i>seq</i><tt>)</tt><i>k</i> means that the sequence <i>seq</i> is repeated <i>k</i> times.
Here, <i>k</i> is an integer.
For example, <tt>(LR)3</tt> means the same operational sequence as <tt>LRLRLR</tt>.
This notation for repetition can be nested.
For example, <tt>((URD)3L)2R</tt> means <tt>URDURDURDLURDURDURDLR</tt>.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
Your task is to write a program that,
given an initial positions of tiles on the board and an operational sequence,
computes the tile positions after the given operations are finished.
</p>
<!-- end en only -->
<h3>Input</h3>
<!-- begin en only -->
<p>
The input consists of at most 100 datasets, each in the following format.
</p>
<!-- end en only -->
<p>
<blockquote>
<i>n</i> <br>
<i>s</i><sub>11</sub> ... <i>s</i><sub>1<i>n</i></sub> <br>
... <br>
<i>s</i><sub><i>n</i>1</sub> ... <i>s</i><sub><i>nn</i></sub> <br>
<i>seq</i>
</blockquote>
</p>
<!-- begin en only -->
<p>
In the first line, <i>n</i> is the number of cells in one row (and also in one column) of the board (2 ≤ <i>n</i> ≤ 50).
The following <i>n</i> lines contain the initial states of the board cells.
<i>s</i><sub><i>ij</i></sub> is a character indicating the initial state of the cell in the <i>i</i>-th row from the top and in the <i>j</i>-th column from the left (both starting with one).
It is either ‘<tt>.</tt>’ (a period), meaning the cell is empty, or an uppercase letter ‘<tt>A</tt>’-‘<tt>Z</tt>’, meaning a tile engraved with that character is there.
</p>
<p>
<i>seq</i> is a string that represents an operational sequence.
The length of <i>seq</i> is between 1 and 1000, inclusive.
It is guaranteed that <i>seq</i> denotes an operational sequence as described above.
The numbers in <i>seq</i> denoting repetitions are between 2 and 10<sup>18</sup>, inclusive, and are without leading zeros.
Furthermore, the length of the operational sequence after unrolling all the nested repetitions is guaranteed to be at most 10<sup>18</sup>.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
The end of the input is indicated by a line containing a zero.
</p>
<!-- end en only -->
<h3>Output</h3>
<!-- begin en only -->
<p>
For each dataset, output the states of the board cells, after performing the given operational sequence starting from the given initial states.
The output should be formatted in <i>n</i> lines, in the same format as the initial board cell states are given in the input.
</p>
<!-- end en only -->
<h3>Sample Input</h3>
<pre>
4
..E.
.AD.
B...
..C.
D
4
..E.
.AD.
B...
..C.
DR
3
...
.A.
BC.
((URD)3L)2R
5
...P.
PPPPP
PPP..
PPPPP
..P..
LRLR(LR)12RLLR
20
....................
....................
.III..CC..PPP...CC..
..I..C..C.P..P.C..C.
..I..C....P..P.C....
..I..C....PPP..C....
..I..C....P....C....
..I..C..C.P....C..C.
.III..CC..P.....CC..
....................
..XX...XX...X...XX..
.X..X.X..X..X..X..X.
....X.X..X..X..X..X.
...X..X..X..X..X..X.
...X..X..X..X...XXX.
..X...X..X..X.....X.
..X...X..X..X.....X.
.XXXX..XX...X...XX..
....................
....................
((LDRU)1000(DLUR)2000(RULD)3000(URDL)4000)123456789012
6
...NE.
MFJ..G
...E..
.FBN.K
....MN
RA.I..
((((((((((((((((((((((((URD)2L)2L)2L)2L)2L)2L)2L)2L)2L)2L)2L)2L)2L)2L)2L)2L)2L)2L)2L)2L)2L)2L)2L)2L
0
</pre>
<h3>Output for the Sample Input</h3>
<pre>
....
..E.
..D.
BAC.
....
...E
...D
.BAC
...
..C
.AB
....P
PPPPP
..PPP
PPPPP
....P
....................
....................
....................
....................
....................
XXXX................
PXXXX...............
CXXXX...............
XXXXX...............
XXPCXX..............
CCXXCX..............
CXXXXX..............
CXXXXX..............
CPCIXXX.............
CXPPCXX.............
PCXXXIC.............
CPPCCXI.............
CXCIPXXX............
XCPCICIXX...........
PIPPICIXII..........
......
......
N.....
JNG...
RMMKFE
BEAIFN
</pre>
|
p01573 |
<H1><font color="#000">Problem C:</font> Complex Integer Solutions</H1>
<p>
Let <i>f</i>(<i>x</i>) = <i>a</i><sub>0</sub> + <i>a</i><sub>1</sub><i>x</i> + <i>a</i><sub>2</sub><i>x</i><sup>2</sup> + ... + <i>a<sub>d</sub>x<sup>d</sup></i> be the function where each <i>a<sub>i</sub></i> (0 ≤ <i>i</i> ≤ <i>d</i>) is a constant integer (and <i>a<sub>d</sub></i> is non-zero) and <i>x</i> is a variable. Your task is to write a program that finds all <i>complex integer solutions</i> of the equation <i>f</i>(<i>x</i>) = 0 for a given <i>f</i>(<i>x</i>). Here, by complex integers, we mean complex numbers whose
real and imaginary parts are both integers.
</p>
<H2>Input</H2>
<p>
The input consists of two lines. The first line of the input contains <i>d</i>, the degree of <i>f</i>(<i>x</i>). The second line contains (<i>d</i> + 1) integers <i>a</i><sub>0</sub>, ... , <i>a<sub>d</sub></i>, the coeffcients of the equation. You may assume all the following: 1 ≤ <i>d</i> ≤ 10, |<i>a<sub>i</sub></i>| ≤ 10<sup>6</sup> and <i>a<sub>d</sub></i> ≠ 0.
</p>
<H2>Output</H2>
<p>
There should be two lines in the output. In the first line, print the number <i>m</i> of complex integer solutions. In the second line, print <i>m</i> solutions separated by space. Each solution should be counted and printed exactly once even if it is a multiple root. The solutions should be printed in ascending order of their real parts then their imaginary parts, and in the following fashion: <span>0</span>, <span>-2</span>, <span>i</span>, <span>-3i</span>, <span>2+i</span>, and <span>3-4i</span>.
</p>
<H2>Sample Input 1</H2>
<pre>
4
-2 0 0 0 2
</pre>
<H2>Output for the Sample Input 1</H2>
<pre>
4
-1 -i i 1
</pre>
<br/>
<H2>Sample Input 2</H2>
<pre>
8
0 0 25 15 17 -10 1 -1 1
</pre>
<H2>Output for the Sample Input 2</H2>
<pre>
5
-1-2i -1+2i 0 2-i 2+i
</pre>
|
p01089 |
<h2>Deadlock Detection</h2>
<p>
In concurrent processing environments, a deadlock is an undesirable
situation where two or more threads are mutually waiting for others to
finish using some resources and cannot proceed further.
Your task is to detect whether there is any possibility of deadlocks
when multiple threads try to execute a given instruction sequence concurrently.
</p>
<p>
The instruction sequence consists of characters '<t>u</t>' or digits from
'<t>0</t>' to '<t>9</t>', and each of them represents one instruction.
10 threads are trying to execute the same single instruction sequence.
Each thread starts its execution from the beginning of the sequence and
continues in the given order, until all the instructions are
executed.
</p>
<p>
There are 10 shared resources called <i>locks</i> from <i>L</i><sub>0</sub> to <i>L</i><sub>9</sub>.
A digit <i>k</i> is the instruction for <i>acquiring</i> the lock <i>L<sub>k</sub></i>.
After one of the threads acquires a lock <i>L<sub>k</sub></i>,
it is kept by the thread until it is <i>released</i> by the instruction '<t>u</t>'.
While a lock is kept, none of the threads, including one already acquired it,
cannot newly acquire the same lock <i>L<sub>k</sub></i>.
</p>
<p>
Precisely speaking, the following steps are repeated until all threads
finish.
</p>
<ol>
<li>One thread that has not finished yet is chosen arbitrarily.</li>
<li>The chosen thread tries to execute the next instruction that is not executed yet.</li>
<ul>
<li>If the next instruction is a digit <i>k</i> and
the lock <i>L<sub>k</sub></i> is not kept by any thread,
the thread executes the instruction <i>k</i> and acquires <i>L<sub>k</sub></i>.</li>
<li>If the next instruction is a digit <i>k</i> and
the lock <i>L<sub>k</sub></i> is already kept by some thread,
the instruction <i>k</i> is not executed.</li>
<li>If the next instruction is '<t>u</t>',
the instruction is executed and <i>all</i> the locks currently kept by the thread
are released.</li>
</ul>
</ol>
<p>
After executing several steps, sometimes, it falls into the situation
that the next instructions
of all unfinished threads are for acquiring already kept locks.
Once such a situation happens, no instruction will ever be executed no
matter which thread is chosen. This situation is called a <i>deadlock</i>.
</p>
<p>
There are instruction sequences for which threads can never reach a
deadlock regardless of the execution order.
Such instruction sequences are called <i>safe</i>.
Otherwise, in other words, if there exists one or more execution orders
that lead to a deadlock, the execution sequence is called <i>unsafe</i>.
Your task is to write a program that tells whether the given instruction sequence is safe or unsafe.
</p>
<h3>Input</h3>
<p>
The input consists of at most 50 datasets, each in the following format.
</p>
<blockquote>
<i>n</i><br>
<i>s</i><br>
</blockquote>
<p>
<i>n</i> is the length of the instruction sequence and <i>s</i> is a string representing the sequence.
<i>n</i> is a positive integer not exceeding 10,000.
Each character of <i>s</i> is either a digit ('<t>0</t>' to '<t>9</t>') or '<t>u</t>',
and <i>s</i> always ends with '<t>u</t>'.
</p>
<p>
The end of the input is indicated by a line with a zero.
</p>
<h3>Output</h3>
<p>
For each dataset, if the given instruction sequence is safe, then print <t>"SAFE"</t> in a line.
If it is unsafe, then print <t>"UNSAFE"</t> in a line.
</p>
<h3>Sample Input</h3>
<pre>11
01u12u0123u
6
01u10u
8
201u210u
9
01u12u20u
3
77u
12
9u8u845u954u
0
</pre>
<h3>Output for the Sample Input</h3>
<pre>SAFE
UNSAFE
SAFE
UNSAFE
UNSAFE
UNSAFE
</pre>
<p>
The second input "<t>01u10u</t>" may possibly cause a deadlock. After one
thread has executed the initial four instructions "<t>01u1</t>", the
thread keeps only one lock <i>L</i><sub>1</sub>. If another thread executes the first
instruction '<t>0</t>' at this time, this thread acquires <i>L</i><sub>0</sub>. Then, the
first thread tries to acquire <i>L</i><sub>0</sub>, already kept by the second
thread, while the second thread tries to acquire <i>L</i><sub>1</sub>, kept by the
first thread; This leads to a deadlock.
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ICPCDomestic2015_E-1" valign="middle" align="middle" width="28%"> →
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ICPCDomestic2015_E-2" valign="middle" align="middle" width="28%"> →
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ICPCDomestic2015_E-3" valign="middle" align="middle" width="28%">
<br>
Figure 1: Why the Sample Input 2 "<t>01u10u</t>" is unsafe.
</center>
<p>
Contrarily, the third input "<t>201u210u</t>" is safe.
If one thread had executed up to "<t>201u21</t>" and another to "<t>20</t>", then
one may think it would lead to a deadlock, but this
can never happen because no two threads can simultaneously keep <i>L</i><sub>2</sub>.
</p>
|
p00732 |
<h1><font color="#000000">Problem E:</font> Twirl Around</h1>
<!-- begin en only -->
<p>
Let's think about a bar rotating clockwise as if it were a
twirling baton moving on a planar surface surrounded by a
polygonal wall (see Figure 1).
</p>
<!-- end en only -->
<!-- <img src="https://judgeapi.u-aizu.ac.jp/resources/images/rot_triangle" /><br /> -->
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_e2" />
<br />
<!-- begin en only -->
<p>Figure 1. A bar rotating in a polygon</p>
<!-- end en only -->
</center>
<!-- begin en only -->
<p>
Initially, an end of the bar (called "end A") is at (0,0), and
the other end (called "end B") is at (0,<I>L</I>) where <I>L</I> is the
length of the bar. Initially, the bar is touching the wall only
at the end A.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
The bar turns fixing a touching point as the center.
The center changes as a new point touches the wall.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
Your task is to calculate the coordinates of the end A when the
bar has fully turned by the given count <I>R</I>.
</p>
<!-- end en only -->
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_e5" />
<br />
<!-- begin en only -->
<p>Figure 2. Examples of turning bars</p>
<!-- end en only -->
</center>
<!-- begin en only -->
<p>In Figure 2, some examples are shown. In cases (D) and
(E), the bar is stuck prematurely (cannot rotate clockwise anymore
with any point touching the wall as the center)
before <i>R</i> rotations. In such cases, you should answer the
coordinates of the end A in that (stuck) position.</p>
<!-- end en only -->
<!-- begin en only -->
<p>You can assume the following:</p>
<blockquote>
<p>
When the bar's length <i>L</i> changes by ε (|ε| <
0.00001), the final (<i>x</i>,<i>y</i>) coordinates
will not change more than 0.0005.
</p>
</blockquote>
<!-- end en only -->
<h3>Input</h3>
<!-- begin en only -->
<p>The input consists of multiple datasets. The number of
datasets is no more than 100. The end of the input is represented
by "0 0 0".</p>
<!-- end en only -->
<!-- begin en only -->
<p>
The format of each dataset is as follows:
</p>
<!-- end en only -->
<pre>
<i>L</i> <i>R</i> <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>N</sub> <i>Y</i><sub>N</sub>
</pre>
<!-- begin en only -->
<p>
<i>L</i> is the length of the bar.
The bar rotates 2π× <i>R</i> radians (if it is not
stuck prematurely).
<i>N</i> is the number of vertices which make the polygon.
</p>
<p>
The vertices of the polygon are arranged in a counter-clockwise order.
You may assume that the polygon is <em>simple</em>, that is,
its border never crosses or touches itself.
</p>
<p>
<i>N</i>, <i>X</i><sub>i</sub> and <i>Y</i><sub>i</sub> are
integer numbers; <i>R</i> and <i>L</i> are decimal fractions.
Ranges of those values are as follows:
</p>
<!-- end en only -->
<pre>
1.0 ≤ <i>L</i> ≤ 500.0,
1.0 ≤ <i>R</i> ≤ 10.0,
3 ≤ <i>N</i> ≤ 100,
-1000 ≤ <i>X</i><sub>i</sub> ≤ 1000,
-1000 ≤ <i>Y</i><sub>i</sub> ≤ 1000,
</pre>
<pre>
<i>X</i><sub>1</sub> ≤ -1, <i>Y</i><sub>1</sub> = 0,
<i>X</i><sub>2</sub> ≥ 1, <i>Y</i><sub>2</sub> = 0.
</pre>
<h3>Output</h3>
<!-- begin en only -->
<p>
For each dataset, print one line containing <i>x-</i> and
<i>y-</i>coordinates of the final position of the end A,
separated by a space.
The value may contain an error less than or equal to 0.001.
You may print any number of digits after the decimal point.
</p>
<!-- end en only -->
<h3>Sample Input</h3>
<pre>
4.0 2.0 8
-1 0
5 0
5 -2
7 -2
7 0
18 0
18 6
-1 6
4.0 2.0 4
-1 0
10 0
10 12
-1 12
4.0 1.0 7
-1 0
2 0
-1 -3
-1 -8
6 -8
6 6
-1 6
4.0 2.0 6
-1 0
10 0
10 3
7 3
7 5
-1 5
5.0 2.0 6
-1 0
2 0
2 -4
6 -4
6 6
-1 6
6.0 1.0 8
-1 0
8 0
7 2
9 2
8 4
11 4
11 12
-1 12
0 0 0
</pre>
<h3>Output for the Sample Input</h3>
<pre>
16.0 0.0
9.999999999999998 7.4641016151377535
0.585786437626906 -5.414213562373095
8.0 0.0
6.0 0.0
9.52786404500042 4.0
</pre>
<!-- begin en only -->
<p>
Note that the above sample input corresponds to the
cases in Figure 2. For convenience, in Figure 3, we will show an
animation and corresponding photographic playback for the case (C).
</p>
<!-- end en only -->
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_e6" />
<br />
<TABLE>
<TR><TD><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_stopMotion_Eframe0000"></TD>
<TD><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_stopMotion_Eframe0045"></TD>
<TD><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_stopMotion_Eframe0090"></TD>
<TD><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_stopMotion_Eframe0135"></TD>
<TD><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_stopMotion_Eframe0180"></TD>
<TD><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_stopMotion_Eframe0225"></TD>
<TD><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_stopMotion_Eframe0270"></TD>
<TD><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_stopMotion_Eframe0315"></TD>
<TD><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_stopMotion_EframeLast"></TD></TR>
</TABLE>
<!-- begin en only -->
<p>Figure 3. Animation and photographic playback for case (C)</p>
<!-- end en only -->
</center>
|
p01820 |
<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>
<h3>Problem B:
Vector Field</h3>
<p>
In 20015, JAG (Jagan Acceleration Group) has succeeded in inventing a new accelerator named
Force Point for an experiment of proton collision on the two-dimensional $xy$-plane. If a proton
touches a Force Point, it is accelerated to twice its speed and its movement direction is veered. A
proton may be veered by a Force Field in four ways: the positive or negative directions parallel
to the $x$- or the $y$-axis. The direction in which a proton veers is determined by the type of the
Force Point. A Force Point can accelerate a proton only once because the Force Point disappears
immediately after the acceleration. Generating many Force Points on the two-dimensional plane,
which is called a 2D Force Point Field, allows us to accelerate a proton up to a target speed by
sequentially accelerating the proton with the Force Points in the 2D Force Point Filed.
</p>
<p>
The Force Point generation method is still under experiment and JAG has the following technical
limitations:
</p>
<ul>
<li> JAG cannot generate a Force Point with a specified position and a type.</li>
<li> JAG cannot generate a Force Point after putting a proton into a 2D Force Point Field.</li>
<li> JAG cannot move Force Points.</li>
<li> JAG cannot change a protons direction except by the effect of Force Points.</li>
<li> JAG can use only one proton for a 2D Force Point Field.</li>
<li> JAG can put a proton moving in any direction with its speed 1 at any position in a 2D
Force Point Field.</li>
</ul>
<p>
In order to achieve the maximum speed of a proton, the engineers at JAG have to choose the
optimal initial position and the optimal initial direction of the proton so that the proton is
accelerated by as many Force Points as possible, after carefully observing the generated 2D
Force Point Field.
</p>
<p>
By observing a generated 2D Force Point Field, the number of the generated Force Points is
known to be $n$. The position ($x_i$, $y_i$) and the direction veering type $d_i$ of the $i$-th point are
also known. Your task is to write a program to calculate the maximum speed of a proton by
acceleration on a given 2D Force Point Field when JAG puts a proton optimally.
</p>
<h3>Input</h3>
<p>
The input consists of a single test case which describes a 2D Force Point Field in the following
format.<br>
<br>
$n$<br>
$x_1$ $y_1$ $d_1$<br>
...<br>
$x_n$ $y_n$ $d_n$<br>
<br>
<p>
The first line contains one integer $n$ ($1 \leq n \leq 3,000$) which is the number of the Force Points on
the 2D Force Point Field. Each of the next $n$ lines contains two integers $x_i$ and $y_i$ ($|x_i|$, $|y_i| \leq 10^9$)
and one character $d_i$ ($d_i$ is one of '>', 'v', '<' or '^'). $x_i$ and $y_i$ represent a coordinate of the $i$-th
Force Point, and $d_i$ is the direction veering type of the $i$-th force point. A force point with a type
'>' changes protons direction to the positive direction of the $x$-axis, 'v' represents the positive
direction of the $y$-axis, '<' represents the negative direction of the $x$-axis, and '^' represents the
negative direction of the $y$-axis. You can assume that any two Force Points are not generated
on the same coordinates.
</p>
<h3>Output</h3>
<p>
Display a line containing the integer $log_2 v_{max}$, where $v_{max}$ is the protons possible fastest speed.
</p>
<h3>Sample Input</h3>
<pre>
9
0 0 v
1 0 >
2 0 <
0 1 >
1 1 v
2 1 v
0 2 ^
1 2 ^
2 2 <
</pre>
<h3>Output for the Sample Input</h3>
<pre>
9
</pre>
<p>
The input looks like the following diagram. All the Force Points will disappear if you put a
proton at (1, 1).
</p>
<pre>
v><
>vv
^^<
</pre>
<h3>Sample Input</h3>
<pre>
9
0 0 ^
1 0 ^
2 0 ^
0 1 <
1 1 ^
2 1 >
0 2 v
1 2 v
2 2 v
</pre>
<h3>Output for the Sample Input</h3>
<pre>
2
</pre> |
p00362 | <!--<H1>Network Charging System</H1>-->
<h1>Charging System for Network</h1>
<p>
There is a network consisting of <var>N</var> machines (sequentially numbered from 0 to <var>N</var>-1) interconnected through <var>N</var>-1 bidirectional communication cables. Any two machines can perform bidirectional communication through one or more cables. From time to time, machines in the network are renewed. When a new machine is introduced, the cables directly connected to it are also replaced with thicker ones to cope with the increased volume of communication traffic.
</p>
<p>
The communication fee arising from communication traffic between any two machines is calculated by summing the charges assigned to all the cables routing via the two. A unique charging scheme is employed in this system: if the size of a cable is a multiple of <var>K</var>, then the cable is not charged (free of charge). Other cables are charged according to their sizes.
</p>
<p>
Based on the given information on the network topology and <var>Q</var> instructions, write a program to execute each instruction.
</p>
<ul>
<li>Increase the sizes of all cables directly connected to the machine <var>x</var> by <var>d</var>.</li>
<li>Report the communication charge between the machines <var>s</var> and <var>t</var>.</li>
</ul>
<h2>Input</h2>
<p>
The input is given in the following format.
</p>
<pre>
<var>N</var> <var>K</var>
<var>a_1</var> <var>b_1</var> <var>c_1</var>
<var>a_2</var> <var>b_2</var> <var>c_2</var>
:
<var>a_{N−1}</var> <var>b_{N−1}</var> <var>c_{N−1}</var>
<var>Q</var>
<var>query_1</var>
<var>query_2</var>
:
<var>query_Q</var>
</pre>
<p>
The first line provides the number of machines <var>N</var> (2 ≤ <var>N</var> ≤ 10<sup>5</sup>) and the cable size <var>K</var> (1 ≤ <var>K</var> ≤ 10<sup>5</sup>) (the reference value for determining free of charge cables). Each of subsequent <var>N</var>-1 lines provides the <var>i</var>-th cable information that directly connects two machines <var>a_i</var> and <var>b_i</var> (0 ≤ <var>a_i</var> < <var>b_i</var> ≤ <var>N</var>-1), followed by the cable’s initial size <var>c_i</var> (1 ≤ <var>c_i</var> ≤ 10<sup>5</sup>). For any pair of machines, the number of cables directly connecting them is either one or zero. The next line following them provides the number of instructions <var>Q</var> (1 ≤ <var>Q</var> ≤ 10<sup>5</sup>). Each of the Q lines following it provides the <var>i</var>-th instruction <var>query_i</var>, which is either one of the following two:
</p>
<pre>
add <var>x</var> <var>d</var>
</pre>
<p>
or
</p>
<pre>
send <var>s</var> <var>t</var>
</pre>
<p>
The instruction <span>add</span> <var>x</var> <var>d</var> increase the size of all cables directly connected to machine <var>x</var> (0 ≤ <var>x</var> ≤ <var>N</var>-1) by <var>d</var> (1 ≤ <var>d</var> ≤ 10<sup>5</sup>).
</p>
<p>
The instruction <span>send</span> <var>s</var> <var>t</var> reports the charge imposed to the communication between the two machines <var>s</var> (0 ≤ <var>s</var> ≤ <var>N</var>-1) and <var>t</var> (0 ≤ <var>t</var> ≤ <var>N</var>-1), where <var>s ≠ t</var>.
</p>
<p>
At least one <span>send</span> instruction is included in the input information.
</p>
<h2>Output</h2>
<p>
For each <span>send</span> command, output the communication charge between <var>s</var> and <var>t</var>.
</p>
<h2>Sample Input 1</h2>
<pre>
6 3
0 1 1
0 2 1
0 3 1
2 4 1
2 5 1
3
send 1 4
add 2 2
send 1 4
</pre>
<h2>Sample Output 1</h2>
<pre>
3
1
</pre>
|
p00698 |
<H1>
Missing Numbers
</H1>
<P>
Toshizo is the manager of a convenience store chain in Hakodate.
Every day, each of the stores in his chain sends him a table of the
products that they have sold. Toshizo's job is to compile these
figures and calculate how much the stores have sold in total.
</P>
<P>
The type of a table that is sent to Toshizo by the stores looks like this
(all numbers represent units of products sold):
<PRE>
Store1 Store2 Store3 Totals
Product A -5 40 70 | 105
Product B 20 50 80 | 150
Product C 30 60 90 | 180
-----------------------------------------------------------
Store's total sales 45 150 240 435
</PRE>
<p>
By looking at the table, Toshizo can tell at a glance which goods are
selling well or selling badly, and which stores are paying well and
which stores are too empty. Sometimes, customers will bring products
back, so numbers in a table can be negative as well as positive.
</P>
<P>
Toshizo reports these figures to his boss in Tokyo, and together they
sometimes decide to close stores that are not doing well and sometimes
decide to open new stores in places that they think will be profitable.
So, the total number of stores managed by Toshizo is not fixed. Also,
they often decide to discontinue selling some products that are not
popular, as well as deciding to stock new items that are likely to be
popular. So, the number of products that Toshizo has to monitor is also
not fixed.
</P>
<P>
One New Year, it is very cold in Hakodate. A water pipe bursts in
Toshizo's office and floods his desk. When Toshizo comes to work, he
finds that the latest sales table is not legible. He can make out some
of the figures, but not all of them. He wants to call his boss in
Tokyo to tell him that the figures will be late, but he knows that his
boss will expect him to reconstruct the table if at all possible.
</P>
<P>
Waiting until the next day won't help, because it is the New Year, and
his shops will be on holiday. So, Toshizo decides either to work out
the values for himself, or to be sure that there really is no unique
solution. Only then can he call his boss and tell him what has
happened.
</P>
<P>
But Toshizo also wants to be sure that a problem like this never
happens again. So he decides to write a computer program that all the
managers in his company can use if some of the data goes missing
from their sales tables. For instance, if they have a table like:
<PRE>
Store 1 Store 2 Store 3 Totals
Product A ? ? 70 | 105
Product B ? 50 ? | 150
Product C 30 60 90 | 180
--------------------------------------------------------
Store's total sales 45 150 240 435
</PRE>
<p>
then Toshizo's program will be able to tell them the correct figures
to replace the question marks.
In some cases, however, even his program will not be able to replace
all the question marks, in general.
For instance, if a table like:</p>
<PRE>
Store 1 Store 2 Totals
Product A ? ? | 40
Product B ? ? | 40
---------------------------------------------
Store's total sales 40 40 80
</PRE>
<p>
is given, there are infinitely many possible solutions.
In this sort of case, his program will just say "NO".
Toshizo's program works for any data where the totals row and column
are still intact.
Can you reproduce Toshizo's program?
</P>
</P>
<H2>Input</H2>
<P>The input consists of multiple data sets, each in the following
format:</p>
<PRE>
<I>p s</I>
<I>row</I><SUB>1</SUB>
<I>row</I><SUB>2</SUB>
...
<I>row</I><SUB>p</SUB>
<I>totals</I>
</PRE>
<p>
The first line consists of two integers <I>p</I> and <I>s</I>,
representing the numbers of products and stores, respectively.
The former is less than 100 and the latter is less than 10.
</P>
<P>They are separated by a blank character.
Each of the subsequent lines represents a row of the table and
consists of <I>s</I>+1 elements, each of which is either an integer or
a question mark.
The <I>i</I>-th line (1 <= <I>i</I> <= <I>p</I>) corresponds to
the <I>i</I>-th product and the last line the totals row.
The first <I>s</I> elements in a line represent the sales of each
store and the last one the total sales of the product.
Numbers or question marks in a line are separated by a blank character.
There are no missing numbers in the totals column or row.
There is at least one question mark in each data set.
</P>
<P>The known numbers in the table except the totals row and column is
between -1,000 and 1,000, inclusive.
However, unknown numbers, represented by question marks, may not be in
this range.
The total sales number of each product is between -10,000 and
10,000, inclusive.
The total sales number of each store is between -100,000 and
100,000, inclusive.
</P>
<P>There is a blank line between each data set in the input, and the input
is terminated by a line consisting of a zero.
</P>
</PRE>
<H2>Output</H2>
<P>When there is a unique solution, your program should print the missing
numbers in the occurrence order in the input data set.
Otherwise, your program should print just "NO".
The answers of consecutive data sets should be separated by an empty
line.
Each non-empty output line contains only a single number or "NO".
</P>
<H2>Sample Input</H2>
<PRE>
3 3
? ? 70 105
? 50 ? 150
30 60 90 180
45 150 240 435
2 2
? ? 40
? ? 40
40 40 80
2 3
? 30 40 90
50 60 70 180
70 90 110 270
0
</PRE>
<H2>Output for the Sample Input</H2>
<PRE>
-5
40
20
80
NO
20
</PRE>
|
p00677 |
<script src="./IMAGE/varmath.js" charset="UTF-8"></script>
<h1>Make KND So Fat</h1>
<h2>Problem</h2>
<p>KND君は会津大学に在籍する学生プログラマである。彼の隣人はとても迷惑なことで知られている。隣人は彼が甘党であることを知り、必要以上に甘味を差し入れて彼を太らせようと画策している。そのために、当の隣人であるあなたは友人からとある甘味処を紹介してもらった。</p>
<p>しかし、その甘味処での商品の販売の仕方は少し変わっている。その販売方法とは、甘味セットと呼ばれる甘味の組を日替わりで販売するというものだ。予算内であればその日に販売されている甘味セットの中から自由に甘味を買うことができる。しかし、甘味セットに含まれる甘味は一種類につき1個しか販売されていない。
また、予算は期間の始めに用意した額から支払い、期間途中での追加予算はないものとする。あなたは友人から教えられた甘味の体重に与える影響の度合いと値段をもとに、ある期間中に一定の予算内で彼の体重を最適に増やす甘味の買い方を計算するプログラムを作成することにした。</p>
<h2>Input</h2>
<p>
入力は複数のテストケースからなる。空行区切りで与えられる。ひとつのテストケースは以下の形式で与えられる。入力の終了をEOFで示す。
</p>
<pre>
<var>s</var> <var>d</var> <var>m</var>
<var>k<sub>1</sub></var>
<var>w<sub>1,1</sub></var> <var>p<sub>1,1</sub></var> <var>w<sub>1,2</sub></var> <var>p<sub>1,2</sub></var> ... <var>w<sub>1,k</sub></var> <var>p<sub>1,k</sub></var>
<var>k<sub>2</sub></var>
<var>w<sub>2,1</sub></var> <var>p<sub>2,1</sub></var> <var>w<sub>2,2</sub></var> <var>p<sub>2,2</sub></var> ... <var>w<sub>2,k</sub></var> <var>p<sub>2,k</sub></var>
...
<var>k<sub>s</sub></var>
<var>w<sub>s,1</sub></var> <var>p<sub>s,1</sub></var> <var>w<sub>s,2</sub></var> <var>p<sub>s,2</sub></var> ... <var>w<sub>s,k</sub></var> <var>p<sub>s,k</sub></var>
<var>f<sub>1</sub></var> <var>f<sub>2</sub></var> ... <var>f<sub>d</sub></var>
</pre>
<p>
ここで、
</p>
<ul>
<li><var>s</var>:甘味セットの種類</li>
<li><var>d</var>:対象となる期間</li>
<li><var>m</var>:予算</li>
<li><var>k<sub>i</sub></var>:i番目の甘味セットに含まれる甘味の種類数</li>
<li><var>w<sub>i,j</sub></var>:i番目の甘味セットに含まれるj番目の甘味の体重に与える影響</li>
<li><var>p<sub>i,j</sub></var>:i番目の甘味セットに含まれるj番目の甘味の値段</li>
<li><var>f<sub>i</sub></var>:i日目に販売されている甘味セットの番号(0から始まる)</li>
</ul>
<p>
である。
</p>
<h2>Constraints</h2>
<p>入力は以下の条件を満たす。</p>
<ul>
<li>入力はすべて整数。</li>
<li>1 ≤ <var>s</var> ≤ <var>d</var> ≤ 100</li>
<li>1 ≤ <var>m</var> ≤ 300</li>
<li>1 ≤ <var>k<sub>i</sub></var> ≤ 50</li>
<li>0 ≤ <var>w<sub>i,j</sub></var> ≤ 50</li>
<li>0 ≤ <var>p<sub>i,j</sub></var> ≤ 300</li>
<li>0 ≤ <var>f<sub>i</sub></var> < <var>s</var></li>
</ul>
<h2>Output</h2>
<p>各テストケースにつき予算内で体重に与えられる最大の影響とその影響を与えるために必要な最小の金額を空白区切りで1行に出力せよ。</p>
<h2>Sample Input</h2>
<pre>
3 3 100
2
5 20 3 10
2
10 50 12 60
2
8 30 22 100
0 1 2
1 1 30
1
13 8
0
</pre>
<h2>Sample Output</h2>
<pre>
23 100
13 8
</pre> |
p01965 |
<script type="text/x-mathjax-config">
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], skipTags: ["script","noscript","style","textarea","code"], processEscapes: true }});
</script>
<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS_HTML"></script>
<H1>
Farm Village
</H1>
<p>
There is a village along a road. This village has $N$ houses numbered $1$ to $N$ in order along the road. Each house has a field that can make up to two units of the crop and needs just one unit of the crop. The total cost to distribute one unit of the crop to each house is the summation of carrying costs and growing costs.
</p>
<ul>
<li>The carrying cost: The cost to carry one unit of the crop between the $i$-th house and the ($i+1$)-th house is $d_i$. It takes the same cost in either direction to carry.</li>
<li>The growing cost: The cost to grow one unit of the crop in the $i$-th house's field is $g_i$.</li>
</ul>
<p>
Your task is to calculate the minimum total cost to supply one unit of the crop to each house.
</p>
<H2>Input</H2>
<p>
The input consists of a single test case formatted as follows.
</p>
<pre>
$N$
$d_1$ ... $d_{N-1}$
$g_1$ ... $g_{N}$
</pre>
<p>
The first line consists of an integer $N$ ($2 \leq 200,000$), which is the number of the houses. The second line consists of $N-1$ integers separated by spaces. The $i$-th integer $d_i$ ($1 \leq d_i \leq 10^9$, $1 \leq i \leq N-1$)represents the carrying cost between the $i$-th and the ($i+1$)-th houses. The third line consists of $N$ integers separated by spaces. The $i$-th integer $g_i$ ($1 \leq g_i \leq 10^9$, $1 \leq i \leq N$) represents the growing cost of the $i$-th house's field.
</p>
<H2>Output</H2>
<p>
Print the minimum cost to supply one unit of the crop to each house.
</p>
<H2>Sample Input 1</H2>
<pre>
2
3
1 5
</pre>
<H2>Output for Sample Input 1</H2>
<pre>
5
</pre>
<H2>Sample Input 2</H2>
<pre>
3
100 100
1 2 3
</pre>
<H2>Output for Sample Input 2</H2>
<pre>
6
</pre>
<H2>Sample Input 3</H2>
<pre>
4
1 2 3
1 1 100 100
</pre>
<H2>Output for Sample Input 3</H2>
<pre>
12
</pre> |
p03958 | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There are <var>K</var> pieces of cakes.
Mr. Takahashi would like to eat one cake per day, taking <var>K</var> days to eat them all.</p>
<p>There are <var>T</var> types of cake, and the number of the cakes of type <var>i</var> (<var>1 ≤ i ≤ T</var>) is <var>a_i</var>. </p>
<p>Eating the same type of cake two days in a row would be no fun,
so Mr. Takahashi would like to decide the order for eating cakes that minimizes the number of days on which he has to eat the same type of cake as the day before. </p>
<p>Compute the minimum number of days on which the same type of cake as the previous day will be eaten.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≤ K ≤ 10000</var></li>
<li><var>1 ≤ T ≤ 100</var></li>
<li><var>1 ≤ a_i ≤ 100</var></li>
<li><var>a_1 + a_2 + ... + a_T = K</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>K</var> <var>T</var>
<var>a_1</var> <var>a_2</var> <var>...</var> <var>a_T</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of days on which the same type of cake as the previous day will be eaten.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>7 3
3 2 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>0
</pre>
<p>For example, if Mr. Takahashi eats cakes in the order of <var>2, 1, 2, 3, 1, 3, 1</var>, he can avoid eating the same type of cake as the previous day.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 3
1 4 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>1
</pre>
<p>There are <var>6</var> cakes.
For example, if Mr. Takahashi eats cakes in the order of <var>2, 3, 2, 2, 1, 2</var>, he has to eat the same type of cake (i.e., type <var>2</var>) as the previous day only on the fourth day.
Since this is the minimum number, the answer is <var>1</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>100 1
100
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>99
</pre>
<p>Since Mr. Takahashi has only one type of cake, he has no choice but to eat the same type of cake as the previous day from the second day and after.</p></section>
</div>
</span> |
p00227 |
<H1>お客様大感謝祭</H1>
<p>
悪天候が続き野菜の価格が高騰する中、セブンマートではお客様に野菜のまとめ買いセールを実施しています。 日ごろなかなか店頭に並ばない野菜もお手頃価格で手に入るとあって、 店内はとても賑わっています。
</p>
<p>
ある日、松長団地に住む仲良し 3 人組がセブンマートの広告を手に話に花を咲かせていました。今回のセールは「お客様大感謝祭」と銘打っただけに、袋詰めした野菜の中で最も安いものが無料になるのが目玉となっています。広告を読んでみると、どうやら以下のようなセールのようです。
</p>
<ul>
<li> 1 つの袋には <var>m</var> 個まで野菜を詰められる。</li>
<li> 野菜が <var>m</var> 個詰めてある袋については、その中で最も安い野菜が無料となる。</li>
<li> 野菜の個数が <var>m</var> 個に達しない袋は割引の対象外。</li>
</ul>
<p>
3人は早速セブンマートへ買い物に行きました。
</p>
<p>
買い物が終わり、 お店の外で待ち合わせた 3 人は安くてたくさん購入できたことに満足した様子で話をしていると、どうやら 3 人とも同じ野菜を購入していたことが分かりました。ある一人が、「本当に安いわよねぇ。これでXXX円だもの!」と言うと、もう一人は、「え?私はそれより**円高かったわ!どうして?」と驚き、また、残りの一人はレシートを見て自分が一番安く購入したことに気付きました。
</p>
<p>
さて、どのように袋詰めすれば購入価格を一番安くできるでしょうか。 購入する野菜の個数、袋に入る野菜の個数、各野菜の値段を入力とし、最低購入価格を出力するプログラムを作成してください。
</p>
<H2>Input</H2>
<p>
複数のデータセットの並びが入力として与えられます。入力の終わりはゼロふたつの行で示されます。
各データセットは以下の形式で与えられます。
</p>
<pre>
<var>n</var> <var>m</var>
<var>p<sub>1</sub></var> <var>p<sub>2</sub></var> ... <var>p<sub>n</sub></var>
</pre>
<p>
1 行目に購入する野菜の個数 <var>n</var> (1 ≤ <var>n</var> ≤ 1000) と袋に入る野菜の個数 <var>m</var> (1 ≤ <var>m</var> ≤ 1000) が与えられます。2行目に各野菜の値段 <var>p<sub>i</sub></var> (10 ≤ <var>p<sub>i</sub></var> ≤ 10000) が与えられます。
</p>
<p>
データセットの数は 100 を超えません。
</p>
<H2>Output</H2>
<p>
入力データセットごとに、最低購入価格を1行に出力します。
</p>
<H2>Sample Input</H2>
<pre>
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
150
2100
</pre>
|
p01066 |
<h1>Reflection Warp Machine</h1>
<h2>Problem</h2>
<p>
とある宇宙では、2次元の格子点上に<var>n</var>個の星が存在し、宇宙人達はReflection Warp Machineを使い、星間を移動している。<br>
この装置は、任意の位置、角度で直線を引くことができる。<br>
この直線を対称軸として、現在いる座標から線対称の座標に移動することができる。<br>
ただし、星が存在しない座標に移動することはできない。<br>
一度引いた直線は何度でも利用できる。<br>
現在、(<var>x<sub>0</sub></var>,<var>y<sub>0</sub></var>)の星にいるある宇宙人はすべての星を訪れたいと考えている。<br>
星は好きな順番に訪れることができる。<br>
すべての星を訪れるのに最小で何本の直線を引く必要があるか求めよ。
</p>
<h2>Input</h2>
<pre>
<var>n</var>
<var>x<sub>0</sub></var> <var>y<sub>0</sub></var>
...
<var>x<sub>n−1</sub></var> <var>y<sub>n−1</sub></var>
</pre>
<p>
入力は全て整数で与えられる。<br>
1行目に<var>n</var>が与えられる。<br>
2行目以降<var>n</var>行に<var>i</var>番目の星の座標(<var>x<sub>i</sub></var>,<var>y<sub>i</sub></var>)が空白区切りで与えられる。
</p>
<h2>Constraints</h2>
<ul>
<li>2 ≤ <var>n</var> ≤ 8</li>
<li>−100 ≤ <var>x<sub>i</sub></var>,<var>y<sub>i</sub></var> ≤ 100</li>
<li>(<var>x<sub>i</sub></var>,<var>y<sub>i</sub></var>) ≠ (<var>x<sub>j</sub></var>,<var>y<sub>j</sub></var>) (<var>i</var> ≠ <var>j</var>)<br>
</ul>
<h2>Output</h2>
<p>
すべての星を訪れるために必要な最小の直線の数を1行に出力せよ。
</p>
<h2>Sample Input 1</h2>
<pre>
3
0 0
0 1
1 0
</pre>
<h2>Sample Output 1</h2>
<pre>
2
</pre>
<h2>Sample Input 2</h2>
<pre>
4
0 0
0 1
0 2
0 3
</pre>
<h2>Sample Output 2</h2>
<pre>
2
</pre>
|
p01436 |
<script type="text/javascript" src="./varmath.js" charset="UTF-8"></script>
<h2>問題文</h2>
<p>
<i>キュゥべえ</i>はなかなか契約を結んでくれない<i>鹿目まどか</i>にしびれを切らせて,ソウルジェムゲームと称して<i>佐倉杏子</i>と<i>美樹さやか</i>のソウルジェムの中身 (魂) を <var>W</var> 列 <var>H</var> 段のロッカーの中に隠してしまった.そこで,<i>まどか</i>は<i>杏子</i>と<i>さやか</i>を助け出したい.しかし,契約を結ばずに助けるにはロッカーボックスの壁を操作して <var>2</var> 人のソウルジェムの中身を,指定されたロッカーボックスに正しく移さなければならない.ロッカーはロッカーボックスが <var>W</var> 列 <var>H</var> 段に並ぶことで構成され,隣り合うロッカーボックスは壁で仕切られている.
</p>
<p>
<i>まどか</i>は,<var>1</var> 回の操作でいずれかの隣りあった <var>2</var> つのロッカーボックスの壁を取り除くか取り付けることができる.ただし,ロッカーボックスの壁を開けるには制約があり,各ロッカーボックスから隣接する上下左右の壁のうち,高々 <var>2</var> 枚までの壁しか同時に開けておくことはできない.ロッカーボックスには上の段から下の段に向かって重力が働いている.ソウルジェムの中身は液体と同じような動きをし,隣りあった <var>2</var> つのロッカーボックスの壁が取り除かれている時,同じ高さであれば互いに同じ量になり,違う高さであれば低い方へ移動する.すべての操作が終わった時に指定された <var>2</var> つのロッカーボックスに正しい組み合わせでソウルジェムの中身がすべて入っていれば<i>まどか</i>の勝利となる.また,一部のロッカーボックスは穢れており,その中へソウルジェムの中身が入ってしまうと大変なことになってしまうので,そのロッカーボックスはソウルジェムの中身の移動のために使うことはできない.
</p>
<p>
最小で何度の操作を行えば<i>まどか</i>は <var>2</var> 人を救いだすことができるかを求めよ.
</p>
<h2>入力形式</h2>
<p>入力は以下の形式で与えられる.</p>
<pre><var>H\ W\\
s_{11}\ s_{12}\ … s_{1W}\\
s_{21}\ s_{22}\ … s_{2W}\\
…\\
s_{H1}\ s_{H2}\ … s_{HW}\\
</var></pre>
<p><var>H</var> はロッカーの段数,<var>W</var> はロッカーの列数である.<var>s_{ij}</var> は上から <var>i</var> 段目の左から <var>j</var> 列目のロッカーボックスの状態を表す.<i>杏子</i>のソウルジェムの中身が入っているときは <code>'K'</code> であり,<i>さやか</i>のソウルジェムの中身が入っているときは <code>'S'</code> ,<i>杏子</i>のソウルジェムの中身を移すべきロッカーボックスであるときは <code>'k'</code>,<i>さやか</i>のソウルジェムの中身を移すべきロッカーボックスであるときは <code>'s'</code>,穢れたロッカーボックスであるときは <code>'#'</code>,それ以外のロッカーボックスであるときは <code>'.'</code> である.</p>
<h2>出力形式</h2>
<var>2</var> 人を救い出すために必要な操作の最小回数を出力せよ.どのように操作しても救い出すことが出来ない場合は <code>CONTRACT</code> と出力せよ.
<h2>制約</h2>
<ul>
<li><var>1 ≤ H ≤ 10</var></li>
<li><var>1 ≤ W ≤ 10</var></li>
<li><var>s_{ij}</var> は <code>'#'</code>, <code>'.'</code>, <code>'S'</code>, <code>'K'</code>, <code>'s'</code>, <code>'k'</code> のいずれかである.</li>
<li><var>s_{ij}\ =\ </var><code>'S'</code> であるような <var>i, j</var> がちょうど 1 つだけ存在する.</li>
<li><var>s_{ij}\ =\ </var><code>'K'</code> であるような <var>i, j</var> がちょうど 1 つだけ存在する.</li>
<li><var>s_{ij}\ =\ </var><code>'s'</code> であるような <var>i, j</var> がちょうど 1 つだけ存在する.</li>
<li><var>s_{ij}\ =\ </var><code>'k'</code> であるような <var>i, j</var> がちょうど 1 つだけ存在する.</li>
</ul>
<h2>入出力例</h2>
<h3>入力例 1</h3>
<pre>4 4
.S..
K...
....
..sk</pre>
<h3>出力例1</h3>
<pre>9</pre>
<h3>入力例 2</h3>
<pre>5 5
.S.K.
..#..
.###.
..#..
.k.s.</pre>
<h3>出力例 2</h3>
<pre>CONTRACT</pre>
<h3>入力例 3</h3>
<pre>8 5
S....
####K
##...
#..##
#..##
##k##
#...#
#.s.#</pre>
<h3>出力例 3</h3>
<pre>19</pre>
<h3>入力例 4</h3>
<pre>8 5
S....
####K
##...
##.##
##.##
##k##
#...#
#.s.#</pre>
<h3>出力例 4</h3>
<pre>CONTRACT</pre>
<h3>入力例 5</h3>
<pre>10 10
..........
##......##
#K......S#
..........
..........
.#..##..#.
..##..##..
..........
..........
....s.k...</pre>
<h3>出力例 5</h3>
<pre>22</pre>
<h3>入力例 6</h3>
<pre>3 3
S.s
K..
..k</pre>
<h3>出力例 6</h3>
<pre>CONTRACT</pre>
<hr>
<address>Problem Setter: Flat35</address> |
p02133 | <h1>Problem J: Matrix</h1>
<h2>Problem</h2>
<p>
長さ$H$の数列$A$と長さ$W$の数列$B$が与えられる。<br><br>
$H$行$W$列からなる行列$C$を以下のように定義する。<br>
$ C_{i,j} = A_i \times B_j (1 \leq i \leq H, 1 \leq j \leq W) $ <br><br>
以下の四種類のクエリを合計$Q$回処理せよ。<br>
<br>クエリ1<br>
$1$ $a$ $b$ $v$<br>
$ A_i ( a \leq i \leq b) $に$v$を加える <br>
<br>クエリ2<br>
$2$ $c$ $d$ $v$<br>
$ B_j ( c \leq j \leq d) $に$v$を加える <br>
<br>クエリ3<br>
$3$ $a$ $b$ $c$ $d$<br>
$ C_{i,j} ( a \leq i \leq b, c \leq j \leq d) $の最小値とその個数を出力する <br>
<br>クエリ4<br>
$4$ $a$ $b$ $c$ $d$<br>
$ C_{i,j} ( a \leq i \leq b, c \leq j \leq d) $の最大値とその個数を出力する <br>
<br>
詳しくはサンプル入出力を参考にせよ。<br>
</p>
<h2>Input</h2>
<p>入力は以下の形式で与えられる。</p>
<pre>
$H$ $W$ $Q$
$A_1$ $A_2$ ... $A_H$
$B_1$ $B_2$ ... $B_W$
$query_1$
$query_2$
...
$query_Q$
</pre>
<p>各クエリは以下の四種類のいずれかの形式で与えられる。</p>
<pre>
$1$ $a$ $b$ $v$
$2$ $c$ $d$ $v$
$3$ $a$ $b$ $c$ $d$
$4$ $a$ $b$ $c$ $d$
</pre>
<p>
入力はすべて整数で与えられる。<br>
1行目に$H$, $W$, $Q$が空白区切りで与えられる。<br>
2行目に数列$A$の要素$A_i$($1 \leq i \leq H$)が空白区切りで与えられる。<br>
3行目に数列$B$の要素$B_j$($1 \leq j \leq W$)が空白区切りで与えられる。<br>
4行目以降の$Q$行にクエリが改行区切りで与えられる。<br>
各クエリ内の数値は全て空白区切りである。
</p>
<h2>Constraints</h2>
<p>入力は以下の条件を満たす。</p>
<ul>
<li>$1 \leq H, W, Q\leq 10^5 $</li>
<li>$-1000 \leq A_i \leq 1000 $</li>
<li>$-1000 \leq B_j \leq 1000 $</li>
</ul>
<p>各クエリについて、入力は以下の条件を満たす。<br>
<ul>
<li>$1 \leq a \leq b \leq H$</li>
<li>$1 \leq c \leq d \leq W$</li>
<li>$-1000 \leq v \leq 1000$</li>
</ul>
<h2>Output</h2>
<p>
各クエリ3及びクエリ4について、値と個数を空白区切りで一行に出力せよ。<br>
</p>
<h2>Sample Input 1</h2>
<pre>
4 4 7
1 2 3 4
1 2 3 4
3 2 3 2 3
4 2 3 2 3
3 1 1 1 1
4 1 1 1 1
1 1 1 1
3 1 4 1 2
4 2 3 2 4
</pre>
<h2>Sample Output 1</h2>
<pre>
4 1
9 1
1 1
1 1
2 2
12 1
</pre>
クエリ1, 2で指定されている範囲は以下の画像の緑色に塗られた領域である。<br>
クエリ3, 4で指定されている範囲は以下の画像の橙色に塗られた領域である。<br>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/acpc2018_aizu_J1" alt="img1" title="クエリ1, 2, 3, 4"><br>
クエリ5によって$A_1$に1が加えられる。<br>
クエリ6で指定されている範囲は以下の画像の水色に塗られた領域である。<br>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/acpc2018_aizu_J2" alt="img2" title="クエリ6"><br>
クエリ7で指定されている範囲は以下の画像の紫色に塗られた領域である。<br>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/acpc2018_aizu_J3" alt="img3" title="クエリ7"><br>
<h2>Sample Input 2</h2>
<pre>
4 4 6
1 1 1 1
1 1 1 1
3 1 4 1 4
4 1 4 1 4
1 2 3 1
2 2 3 1
3 1 4 1 4
4 1 4 1 4
</pre>
<h2>Sample Output 2</h2>
<pre>
1 16
1 16
1 4
4 4
</pre>
初期状態の$C$は以下のようになる。<br>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/acpc2018_aizu_J4" alt="img4" ><br>
クエリ3, 4以後の$C$は以下のようになる。<br>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/acpc2018_aizu_J5" alt="img5" ><br>
<h2>Sample Input 3</h2>
<pre>
4 4 7
0 1 0 1
1 0 1 0
3 2 4 1 3
4 1 4 1 4
1 1 4 -1
3 1 3 1 3
1 3 3 2
2 3 3 -2
3 2 4 2 4
</pre>
<h2>Sample Output 3</h2>
<pre>
0 5
1 4
-1 4
-1 1
</pre>
<h2>Sample Input 4</h2>
<pre>
36 4 20
-523 -460 -536 -885 652 782 513 -558 -521 -808 124 -708 -943 52 -856 -755 -958 913 380 -767 373 -731 -492 213 538 392 -39 509 -840 135 78 -285 -241 522 -572 -915
-691 -16 812 -385
4 7 17 4 4
3 24 33 1 1
4 3 35 2 4
3 28 32 4 4
2 3 3 438
3 7 15 1 1
4 2 17 4 4
3 24 28 2 4
4 15 16 4 4
4 18 29 1 1
3 24 27 2 4
2 2 3 -828
3 18 23 3 3
1 27 31 -701
2 2 3 298
1 21 32 237
2 3 3 -71
1 14 29 772
4 13 19 3 3
3 19 34 1 2
</pre>
<h2>Sample Output 4</h2>
<pre>
368830 1
-371758 1
741356 1
-195965 1
-354483 1
368830 1
-207130 1
329560 1
580440 1
-207130 1
-323674 1
1093565 1
-1068977 1
</pre>
|
p02099 |
<h1>Problem B: GPA JANKEN</h1>
<h2>Problem</h2>
<p>
GPAとは「Galaxy Point of Aizu」の略で、0から4までの実数値を取る。<br>
GPAじゃんけんは二人で行うゲームである。各々が「じゃんけん、ぽん(ほい)」の合図で互いにグー、チョキ、パーのどれか好きな手を出した後、GPAが高い人が勝者となり、低い人が敗者となる。同じ場合は引き分けとなる。
</p>
<p>
<var>N</var> 人のGPAのデータが与えられるので、総当たり戦をした時のそれぞれの勝ち点を出力せよ。<br>
ただし、勝ち点は勝ちの時3、引き分けの時1、負けの時0とする。
</p>
<p>
総当たり戦とはすべての参加者が自分以外の参加者と丁度一回ずつ対戦する方式である。
</p>
<h2>Input</h2>
<p>入力は以下の形式で与えられる。</p>
<pre>
<var>N</var>
<var>a<sub>1</sub></var>
<var>a<sub>2</sub></var>
...
<var>a<sub>N</sub></var>
</pre>
<p>
一行目にGPAじゃんけんをする人数 <var>N</var> が与えられる。<br>
続く <var>N</var> 行に <var>i</var> 番目の人のGPAのデータ <var>a<sub>i</sub></var> が一行に与えられる。
</p>
<h2>Constraints</h2>
<p>入力は以下の条件を満たす。</p>
<ul>
<li>2 ≤ <var>N</var> ≤ 10<sup>5</sup></li>
<li><var>N</var> は整数</li>
<li>0.000 ≤ <var>a<sub>i</sub></var> ≤ 4.000 ( 1 ≤ <var>i</var> ≤ <var>N</var> )</li>
<li><var>a<sub>i</sub></var> は実数 ( 1 ≤ <var>i</var> ≤ <var>N</var> )</li>
<li>各GPAは小数点以下3桁まで与えられる</li>
</ul>
<h2>Output</h2>
<p>
<var>i</var> 行目に <var>i</var> 番目の人の勝ち点を一行に出力せよ。
( 1 ≤ <var>i</var> ≤ <var>N</var> )<br>
</p>
<h2>Sample Input 1</h2>
<pre>
3
1.000
3.000
3.000
</pre>
<h2>Sample Output 1</h2>
<pre>
0
4
4
</pre>
<h2>Sample Input 2</h2>
<pre>
3
1.000
2.000
3.000
</pre>
<h2>Sample Output 2</h2>
<pre>
0
3
6
</pre> |
p02563 | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given two integer arrays <var>a_0, a_1, ..., a_{N - 1}</var> and <var>b_0, b_1, ..., b_{M - 1}</var>. Calculate the array <var>c_0, c_1, ..., c_{(N - 1) + (M - 1)}</var>, defined by <var>c_i = \sum_{j = 0}^i a_j b_{i - j} \bmod 998244353</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N, M \leq 524288</var></li>
<li><var>0 \leq a_i, b_i < 998244353</var></li>
<li>All values in Input are integer.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>M</var>
<var>a_0</var> <var>a_1</var> ... <var>a_{N-1}</var>
<var>b_0</var> <var>b_1</var> ... <var>b_{M-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the answer in the following format:</p>
<pre><var>c_0</var> <var>c_1</var> ... <var>c_{(N - 1) + (M - 1)}</var>
</pre>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4 5
1 2 3 4
5 6 7 8 9
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>5 16 34 60 70 70 59 36
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>1 1
10000000
10000000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>871938225
</pre></section>
</div>
</span> |
p02830 | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given are strings <var>s</var> and <var>t</var> of length <var>N</var> each, both consisting of lowercase English letters.</p>
<p>Let us form a new string by alternating the characters of <var>S</var> and the characters of <var>T</var>, as follows: the first character of <var>S</var>, the first character of <var>T</var>, the second character of <var>S</var>, the second character of <var>T</var>, <var>...</var>, the <var>N</var>-th character of <var>S</var>, the <var>N</var>-th character of <var>T</var>. Print this new string.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 100</var></li>
<li><var>|S| = |T| = N</var></li>
<li><var>S</var> and <var>T</var> are strings consisting of lowercase English letters.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>S</var> <var>T</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the string formed.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2
ip cc
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>icpc
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>8
hmhmnknk uuuuuuuu
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>humuhumunukunuku
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>5
aaaaa aaaaa
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>aaaaaaaaaa
</pre></section>
</div>
</span> |
p03722 | <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There is a directed graph with <var>N</var> vertices and <var>M</var> edges.
The <var>i</var>-th edge <var>(1≤i≤M)</var> points from vertex <var>a_i</var> to vertex <var>b_i</var>, and has a weight <var>c_i</var>.
We will play the following single-player game using this graph and a piece.</p>
<p>Initially, the piece is placed at vertex <var>1</var>, and the score of the player is set to <var>0</var>.
The player can move the piece as follows:</p>
<ul>
<li>When the piece is placed at vertex <var>a_i</var>, move the piece along the <var>i</var>-th edge to vertex <var>b_i</var>. After this move, the score of the player is increased by <var>c_i</var>.</li>
</ul>
<p>The player can end the game only when the piece is placed at vertex <var>N</var>.
The given graph guarantees that it is possible to traverse from vertex <var>1</var> to vertex <var>N</var>.</p>
<p>When the player acts optimally to maximize the score at the end of the game, what will the score be?
If it is possible to increase the score indefinitely, print <code>inf</code>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≤N≤1000</var> </li>
<li><var>1≤M≤min(N(N-1),2000)</var> </li>
<li><var>1≤a_i,b_i≤N (1≤i≤M)</var> </li>
<li><var>a_i≠b_i (1≤i≤M)</var> </li>
<li><var>a_i≠a_j</var> or <var>b_i≠b_j (1≤i<j≤M)</var> </li>
<li><var>-10^9≤c_i≤10^9 (1≤i≤M)</var></li>
<li><var>c_i</var> is an integer.</li>
<li>In the given graph, there exists a path from vertex <var>1</var> to vertex <var>N</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format: </p>
<pre><var>N</var> <var>M</var>
<var>a_1</var> <var>b_1</var> <var>c_1</var>
<var>a_2</var> <var>b_2</var> <var>c_2</var>
<var>:</var>
<var>a_M</var> <var>b_M</var> <var>c_M</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the maximum possible score at the end of the game, if it is finite. If it is possible to increase the score indefinitely, print <code>inf</code>. </p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 3
1 2 4
2 3 3
1 3 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>7
</pre>
<p>There are two ways to move the piece to vertex <var>N=3</var>:</p>
<ul>
<li>vertex <var>1</var> → vertex <var>2</var> → vertex <var>3</var> : score <var>4+3=7</var></li>
<li>vertex <var>1</var> → vertex <var>3</var> : score <var>5</var></li>
</ul>
<p>Thus, the maximum possible score at the end of the game is <var>7</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2 2
1 2 1
2 1 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>inf
</pre>
<p>It is possible to increase the score indefinitely by alternating between vertex <var>1</var> and <var>2</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>6 5
1 2 -1000000000
2 3 -1000000000
3 4 -1000000000
4 5 -1000000000
5 6 -1000000000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>-5000000000
</pre></section>
</div>
</span> |
p03688 | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There are <var>N</var> cats.
We number them from <var>1</var> through <var>N</var>.</p>
<p>Each of the cats wears a hat.
Cat <var>i</var> says: "there are exactly <var>a_i</var> different colors among the <var>N - 1</var> hats worn by the cats except me."</p>
<p>Determine whether there exists a sequence of colors of the hats that is consistent with the remarks of the cats.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 ≤ N ≤ 10^5</var></li>
<li><var>1 ≤ a_i ≤ N-1</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <code>Yes</code> if there exists a sequence of colors of the hats that is consistent with the remarks of the cats; print <code>No</code> otherwise.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
1 2 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<p>For example, if cat <var>1</var>, <var>2</var> and <var>3</var> wears red, blue and blue hats, respectively, it is consistent with the remarks of the cats.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
1 1 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>No
</pre>
<p>From the remark of cat <var>1</var>, we can see that cat <var>2</var> and <var>3</var> wear hats of the same color.
Also, from the remark of cat <var>2</var>, we can see that cat <var>1</var> and <var>3</var> wear hats of the same color.
Therefore, cat <var>1</var> and <var>2</var> wear hats of the same color, which contradicts the remark of cat <var>3</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>5
4 3 4 3 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>No
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>3
2 2 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>Yes
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 5</h3><pre>4
2 2 2 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 5</h3><pre>Yes
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 6</h3><pre>5
3 3 3 3 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 6</h3><pre>No
</pre></section>
</div>
</span> |
p03372 | <span class="lang-en">
<p>Score : <var>500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>"Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is <var>C</var> meters. Customers cannot go inside the counter.</p>
<p>Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are <var>N</var> pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the <var>i</var>-th sushi is placed, is <var>x_i</var> meters. Also, the <var>i</var>-th sushi has a nutritive value of <var>v_i</var> kilocalories.</p>
<p>Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes <var>1</var> kilocalories per meter.</p>
<p>Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≤ N ≤ 10^5</var></li>
<li><var>2 ≤ C ≤ 10^{14}</var></li>
<li><var>1 ≤ x_1 < x_2 < ... < x_N < C</var></li>
<li><var>1 ≤ v_i ≤ 10^9</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Subscores</h3><ul>
<li><var>300</var> points will be awarded for passing the test set satisfying <var>N ≤ 100</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>C</var>
<var>x_1</var> <var>v_1</var>
<var>x_2</var> <var>v_2</var>
<var>:</var>
<var>x_N</var> <var>v_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If Nakahashi can take in at most <var>c</var> kilocalories on balance before he leaves the restaurant, print <var>c</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 20
2 80
9 120
16 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>191
</pre>
<p>There are three sushi on the counter with a circumference of <var>20</var> meters. If he walks two meters clockwise from the initial place, he can eat a sushi of <var>80</var> kilocalories. If he walks seven more meters clockwise, he can eat a sushi of <var>120</var> kilocalories. If he leaves now, the total nutrition taken in is <var>200</var> kilocalories, and the total energy consumed is <var>9</var> kilocalories, thus he can take in <var>191</var> kilocalories on balance, which is the largest possible value.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 20
2 80
9 1
16 120
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>192
</pre>
<p>The second and third sushi have been swapped. Again, if he walks two meters clockwise from the initial place, he can eat a sushi of <var>80</var> kilocalories. If he walks six more meters counterclockwise this time, he can eat a sushi of <var>120</var> kilocalories. If he leaves now, the total nutrition taken in is <var>200</var> kilocalories, and the total energy consumed is <var>8</var> kilocalories, thus he can take in <var>192</var> kilocalories on balance, which is the largest possible value.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1 100000000000000
50000000000000 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0
</pre>
<p>Even though the only sushi is so far that it does not fit into a <var>32</var>-bit integer, its nutritive value is low, thus he should immediately leave without doing anything.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>15 10000000000
400000000 1000000000
800000000 1000000000
1900000000 1000000000
2400000000 1000000000
2900000000 1000000000
3300000000 1000000000
3700000000 1000000000
3800000000 1000000000
4000000000 1000000000
4100000000 1000000000
5200000000 1000000000
6600000000 1000000000
8000000000 1000000000
9300000000 1000000000
9700000000 1000000000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>6500000000
</pre>
<p>All these sample inputs above are included in the test set for the partial score.</p></section>
</div>
</span> |
p00948 |
<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 C
Distribution Center
</h2>
<p>
The factory of the Impractically Complicated Products Corporation has many manufacturing lines and the same number of corresponding storage rooms. The same number of conveyor lanes are laid out in parallel to transfer goods from manufacturing lines directly to the corresponding storage rooms. Now, they plan to install a number of robot arms here and there between pairs of adjacent conveyor lanes so that goods in one of the lanes can be picked up and released down on the other, and also in the opposite way. This should allow mixing up goods from different manufacturing lines to the storage rooms.
</p>
<p>
Depending on the positions of robot arms, the goods from each of the manufacturing lines can only be delivered to some of the storage rooms. Your task is to find the number of manufacturing lines from which goods can be transferred to each of the storage rooms, given the number of conveyor lanes and positions of robot arms.
</p>
<h3>Input</h3>
<p>
The input consists of a single test case, formatted as follows.<br/>
<br/>
$n$ $m$<br/>
$x_1$ $y_1$<br/>
...<br/>
$x_m$ $y_m$<br/>
</p>
<p>
An integer $n$ ($2 \leq n \leq 200000$) in the first line is the number of conveyor lanes. The lanes are numbered from 1 to $n$, and two lanes with their numbers differing with 1 are adjacent. All of them start from the position $x = 0$ and end at $x = 100000$. The other integer $m$ ($1 \leq m < 100000$) is the number of robot arms.
</p>
<p>
The following $m$ lines indicate the positions of the robot arms by two integers $x_i$ ($0 < x_i < 100000$) and $y_i$ ($1 \leq y_i < n$). Here, $x_i$ is the <i>x</i>-coordinate of the $i$-th robot arm, which can pick goods on either the lane $y_i$ or the lane $y_i + 1$ at position $x = x_i$, and then release them on the other at the same <i>x</i>-coordinate.
</p>
<p>
You can assume that positions of no two robot arms have the same $x$-coordinate, that is, $x_i \ne x_j$ for any $i \ne j$.
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_ICPCAsia2016_Figure_C1"><br/>
Figure C.1. Illustration of Sample Input 1
</center><br/>
<h3>Output</h3>
<p>
Output $n$ integers separated by a space in one line. The $i$-th integer is the number of the manufacturing lines from which the storage room connected to the conveyor lane $i$ can accept goods.
</p>
<h3>Sample Input 1</h3>
<pre>4 3
1000 1
2000 2
3000 3</pre>
<h3>Sample Output 1</h3>
<pre>2 3 4 4</pre>
<br/>
<h3>Sample Input 2</h3>
<pre>4 3
1 1
3 2
2 3</pre>
<h3>Sample Output 2</h3>
<pre>2 4 4 2</pre>
|
p02975 | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Snuke has <var>N</var> hats. The <var>i</var>-th hat has an integer <var>a_i</var> written on it.</p>
<p>There are <var>N</var> camels standing in a circle.
Snuke will put one of his hats on each of these camels.</p>
<p>If there exists a way to distribute the hats to the camels such that the following condition is satisfied for every camel, print <code>Yes</code>; otherwise, print <code>No</code>.</p>
<ul>
<li>The bitwise XOR of the numbers written on the hats on both adjacent camels is equal to the number on the hat on itself.</li>
</ul>
<details>
<summary style="display: list-item; outline: none;">What is XOR?</summary>
The bitwise XOR <var>x_1 \oplus x_2 \oplus \ldots \oplus x_n</var> of <var>n</var> non-negative integers <var>x_1, x_2, \ldots, x_n</var> is defined as follows:
- When <var>x_1 \oplus x_2 \oplus \ldots \oplus x_n</var> is written in base two, the digit in the <var>2^k</var>'s place (<var>k \geq 0</var>) is <var>1</var> if the number of integers among <var>x_1, x_2, \ldots, x_n</var> whose binary representations have <var>1</var> in the <var>2^k</var>'s place is odd, and <var>0</var> if that count is even.
For example, <var>3 \oplus 5 = 6</var>.
</details>
</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}</var></li>
<li><var>0 \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>\ldots</var> <var>a_{N}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the answer.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
1 2 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<ul>
<li>If we put the hats with <var>1</var>, <var>2</var>, and <var>3</var> in this order, clockwise, the condition will be satisfied for every camel, so the answer is <code>Yes</code>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4
1 2 4 8
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>No
</pre>
<ul>
<li>There is no such way to distribute the hats; the answer is <code>No</code>.</li>
</ul></section>
</div>
</span> |
p03667 | <span class="lang-en">
<p>Score : <var>1000</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There are <var>N</var> balls in a row.
Initially, the <var>i</var>-th ball from the left has the integer <var>A_i</var> written on it.</p>
<p>When Snuke cast a spell, the following happens:</p>
<ul>
<li>Let the current number of balls be <var>k</var>. All the balls with <var>k</var> written on them disappear at the same time.</li>
</ul>
<p>Snuke's objective is to vanish all the balls by casting the spell some number of times.
This may not be possible as it is. If that is the case, he would like to modify the integers on the minimum number of balls to make his objective achievable.</p>
<p>By the way, the integers on these balls sometimes change by themselves.
There will be <var>M</var> such changes. In the <var>j</var>-th change, the integer on the <var>X_j</var>-th ball from the left will change into <var>Y_j</var>.</p>
<p>After each change, find the minimum number of modifications of integers on the balls Snuke needs to make if he wishes to achieve his objective before the next change occurs. We will assume that he is quick enough in modifying integers. Here, note that he does not actually perform those necessary modifications and leaves them as they are.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 200000</var></li>
<li><var>1 \leq M \leq 200000</var></li>
<li><var>1 \leq A_i \leq N</var></li>
<li><var>1 \leq X_j \leq N</var></li>
<li><var>1 \leq Y_j \leq N</var></li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Subscore</h3><ul>
<li>In the test set worth <var>500</var> points, <var>N \leq 200</var> and <var>M \leq 200</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>M</var>
<var>A_1</var> <var>A_2</var> ... <var>A_N</var>
<var>X_1</var> <var>Y_1</var>
<var>X_2</var> <var>Y_2</var>
:
<var>X_M</var> <var>Y_M</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <var>M</var> lines.
The <var>j</var>-th line should contain the minimum necessary number of modifications of integers on the balls to make Snuke's objective achievable.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5 3
1 1 3 4 5
1 2
2 5
5 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>0
1
1
</pre>
<ul>
<li>After the first change, the integers on the balls become <var>2</var>, <var>1</var>, <var>3</var>, <var>4</var>, <var>5</var>, from left to right. Here, all the balls can be vanished by casting the spell five times. Thus, no modification is necessary.</li>
<li>After the second change, the integers on the balls become <var>2</var>, <var>5</var>, <var>3</var>, <var>4</var>, <var>5</var>, from left to right. In this case, at least one modification must be made. One optimal solution is to modify the integer on the fifth ball from the left to <var>2</var>, and cast the spell four times.</li>
<li>After the third change, the integers on the balls become <var>2</var>, <var>5</var>, <var>3</var>, <var>4</var>, <var>4</var>, from left to right. Also in this case, at least one modification must be made. One optimal solution is to modify the integer on the third ball from the left to <var>2</var>, and cast the spell three times.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4 4
4 4 4 4
4 1
3 1
1 1
2 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
1
2
3
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>10 10
8 7 2 9 10 6 6 5 5 4
8 1
6 3
6 2
7 10
9 7
9 9
2 4
8 1
1 8
7 7
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>1
0
1
2
2
3
3
3
3
2
</pre></section>
</div>
</span> |
p03237 | <span class="lang-en">
<p>Score : <var>1500</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p><strong>Problem F and F2 are the same problem, but with different constraints and time limits.</strong></p>
<p>We have a board divided into <var>N</var> horizontal rows and <var>N</var> vertical columns of square cells.
The cell at the <var>i</var>-th row from the top and the <var>j</var>-th column from the left is called Cell <var>(i,j)</var>.
Each cell is either empty or occupied by an obstacle.
Also, each empty cell has a digit written on it.
If <var>A_{i,j}=</var> <code>1</code>, <code>2</code>, ..., or <code>9</code>, Cell <var>(i,j)</var> is empty and the digit <var>A_{i,j}</var> is written on it.
If <var>A_{i,j}=</var> <code>#</code>, Cell <var>(i,j)</var> is occupied by an obstacle.</p>
<p>Cell <var>Y</var> is <em>reachable</em> from cell <var>X</var> when the following conditions are all met:</p>
<ul>
<li>Cells <var>X</var> and <var>Y</var> are different.</li>
<li>Cells <var>X</var> and <var>Y</var> are both empty.</li>
<li>One can reach from Cell <var>X</var> to Cell <var>Y</var> by repeatedly moving right or down to an adjacent empty cell.</li>
</ul>
<p>Consider all pairs of cells <var>(X,Y)</var> such that cell <var>Y</var> is reachable from cell <var>X</var>.
Find the sum of the products of the digits written on cell <var>X</var> and cell <var>Y</var> for all of those pairs.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 1500</var></li>
<li><var>A_{i,j}</var> is one of the following characters: <code>1</code>, <code>2</code>, ... <code>9</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>A_{1,1}A_{1,2}...A_{1,N}</var>
<var>A_{2,1}A_{2,2}...A_{2,N}</var>
<var>:</var>
<var>A_{N,1}A_{N,2}...A_{N,N}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the sum of the products of the digits written on cell <var>X</var> and cell <var>Y</var> for all pairs <var>(X,Y)</var> such that cell <var>Y</var> is reachable from cell <var>X</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2
11
11
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>5
</pre>
<p>There are five pairs of cells <var>(X,Y)</var> such that cell <var>Y</var> is reachable from cell <var>X</var>, as follows:</p>
<ul>
<li><var>X=(1,1)</var>, <var>Y=(1,2)</var></li>
<li><var>X=(1,1)</var>, <var>Y=(2,1)</var></li>
<li><var>X=(1,1)</var>, <var>Y=(2,2)</var></li>
<li><var>X=(1,2)</var>, <var>Y=(2,2)</var></li>
<li><var>X=(2,1)</var>, <var>Y=(2,2)</var></li>
</ul>
<p>The product of the digits written on cell <var>X</var> and cell <var>Y</var> is <var>1</var> for all of those pairs, so the answer is <var>5</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4
1111
11#1
1#11
1111
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>47
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>10
76##63##3#
8445669721
75#9542133
3#285##445
749632##89
2458##9515
5952578#77
1#3#44196#
4355#99#1#
#298#63587
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>36065
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>10
4177143673
7#########
5#1716155#
6#4#####5#
2#3#597#6#
6#9#8#3#5#
5#2#899#9#
1#6#####6#
6#5359657#
5#########
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>6525
</pre></section>
</div>
</span> |
p04008 | <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> towns in Snuke Kingdom, conveniently numbered <var>1</var> through <var>N</var>.
Town <var>1</var> is the capital.</p>
<p>Each town in the kingdom has a <em>Teleporter</em>, a facility that instantly transports a person to another place.
The destination of the Teleporter of town <var>i</var> is town <var>a_i</var> (<var>1≤a_i≤N</var>).
It is guaranteed that <strong>one can get to the capital from any town by using the Teleporters some number of times</strong>.</p>
<p>King Snuke loves the integer <var>K</var>.
The selfish king wants to change the destination of the Teleporters so that the following holds:</p>
<ul>
<li>Starting from any town, one will be at the capital after using the Teleporters exactly <var>K</var> times in total.</li>
</ul>
<p>Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≤N≤10^5</var></li>
<li><var>1≤a_i≤N</var></li>
<li>One can get to the capital from any town by using the Teleporters some number of times.</li>
<li><var>1≤K≤10^9</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>a_1</var> <var>a_2</var> <var>...</var> <var>a_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 1
2 3 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>Change the destinations of the Teleporters to <var>a = (1,1,1)</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4 2
1 1 2 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>There is no need to change the destinations of the Teleporters, since the king's desire is already satisfied.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>8 2
4 1 2 3 1 2 3 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>3
</pre>
<p>For example, change the destinations of the Teleporters to <var>a = (1,1,2,1,1,2,2,4)</var>.</p></section>
</div>
</span> |
p02076 | <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>"Rooks Game" is a single-player game and uses a chessboard which has $N \times N$ grid and $M$ rook pieces.</p>
<p>A rook moves through any number of unoccupied squares horizontally or vertically. When a rook can attack another rook, it can capture the rook and move to the square which was occupied. Note that, in Rooks Game, we don't distinguish between white and black, in other words, every rook can capture any of other rooks.</p>
<p><center><img src="https://judgeapi.u-aizu.ac.jp/resources/images/JAGSummer19Day1_J_rooks_001.png" width="400pt"></center></p>
<p>Initially, there are $M$ rooks on the board. In each move, a rook captures another rook. The player repeats captures until any rook cannot be captured. There are two types of goal of this game. One is to minimize the number of captured rooks, and the other is to maximize it.</p>
<p>In this problem, you are requested to calculate the minimum and maximum values of the number of captured rooks.</p>
<hr />
<h3>Input</h3>
<p>The input consists of a single test case in the format below.</p>
<blockquote>$N$ $M$
$x_{1}$ $y_{1}$
$\vdots$
$x_{M}$ $y_{M}$</blockquote>
<p>The first line contains two integers $N$ and $M$ which are the size of the chessboard and the number of rooks, respectively ($1 \le N, M \le 1000$). Each of the following $M$ lines gives the position of each rook. The $i$-th line with $x_{i}$ and $y_{i}$ means that the $i$-th rook is in the $x_{i}$-th column and $y_{i}$-th row ($1 \le x_{i}, y_{i} \le N$). You can assume any two rooks are not in the same place.</p>
<h3>Output</h3>
<p>Output the minimum and maximum values of the number of captured rooks separated by a single space.</p>
<p><div class="no-page-break"><h3>Examples</h3><table class="ioexample"><tr><th>Input</th><th>Output</th></tr><tr><td><pre>8 3
1 1
1 8
8 8
</pre></td><td><pre>1 2
</pre></td></tr><tr><td><pre>8 4
1 1
1 8
8 8
8 1
</pre></td><td><pre>2 3
</pre></td></tr><tr><td><pre>5 5
1 1
2 2
3 3
4 4
5 5
</pre></td><td><pre>0 0
</pre></td></tr><tr><td><pre>100 1
100 100
</pre></td><td><pre>0 0
</pre></td></tr><tr><td><pre>10 12
1 2
1 4
1 6
3 6
5 6
10 7
8 7
6 7
4 7
2 7
7 3
9 5
</pre></td><td><pre>7 8
</pre></td></tr></table></div></p>
|
p02426 | <h1>Bit Mask</h1>
<p>
A state with $n$ flags of ON or OFF can be represented by a sequence of bits where $0, 1, ..., n-1$ th flag corresponds to 1 (ON) or 0 (OFF).
The state can be managed by the corresponding decimal integer, because the sequence of bits is a binary representation where each bit is 0 or 1.
</p>
<p>
On the other hand, a mask is a special bit sequence which can be used to set specified bits of a given bit sequence to ON/OFF. It can also be used to extract/exclude a bit sequence based on a specified pattern.
</p>
<p>
Given a sequence of bits with 64 flags which represent a state, perform the following operations using a set of pre-defined masks. Note that each flag of the bits is initialized by OFF.
</p>
<ul>
<li>test(i): Print 1 if $i$-th flag is ON, otherwise 0</li>
<li>set(m): Set flags specified by mask $m$ to ON</li>
<li>clear(m): Set flags specified by mask $m$ to OFF</li>
<li>flip(m): Inverse flags specified by mask $m$</li>
<li>all(m): Print 1 if all flags specified by mask $m$ are ON, otherwise 0</li>
<li>any(m): Print 1 if at least one flag specified by mask $m$ is ON, otherwise 0</li>
<li>none(m): Print 1 if all flags specified by mask $m$ are OFF, otherwise 0</li>
<li>count(m): Print the number of flags specifed by mask $m$ with ON</li>
<li>val(m): Print the decimal value of the state specified by mask $m$</li>
</ul>
<h2>Input</h2>
<p>
The input is given in the following format.
</p>
<pre>
$n$
$mask_0$
$mask_1$
:
$mask_{n-1}$
$q$
$query_1$
$query_2$
:
$query_q$
</pre>
<p>
$n$ represents the number of masks. $mask_i$ represents state of $i$-th mask and is given in the following format:
</p>
<pre>
$k$ $b_0$ $b_1$ ... $b_k$
</pre>
<p>
$k$ is the number of ON in the bits. The following $k$ integers $b_j$ show that $b_j$-th bit is ON.
</p>
<p>
$query_i$ represents $i$-th query and is given in the following format:
</p>
<pre>
0 $i$
</pre>
<p>or</p>
<pre>
1 $m$
</pre>
<p>or</p>
<pre>
2 $m$
</pre>
<p>or</p>
<pre>
3 $m$
</pre>
<p>or</p>
<pre>
4 $m$
</pre>
<p>or</p>
<pre>
5 $m$
</pre>
<p>or</p>
<pre>
6 $m$
</pre>
<p>or</p>
<pre>
7 $m$
</pre>
<p>or</p>
<pre>
8 $m$
</pre>
<p>
The first digit <span>0</span>, <span>1</span>,...,<span>8</span> represents the operation test(i), set(m), clear(m), flip(m), all(m), any(m), none(m), count(m) or val(m) respectively.
</p>
<h2>Output</h2>
<p>
Print the result in a line for each test, all, any, none, count and val operation.
</p>
<h2>Constraints</h2>
<ul>
<li>$1 \leq n \leq 10$</li>
<li>$1 \leq k \leq 64$</li>
<li>$1 \leq q \leq 200,000$</li>
<li>$0 \leq i < 64$</li>
<li>$0 \leq m < n$</li>
</ul>
<h2>Sample Input 1</h2>
<pre>
3
3 0 1 3
1 3
3 0 1 2
8
1 0
2 1
3 1
4 2
5 2
6 2
7 2
8 2
</pre>
<h2>Sample Output 1</h2>
<pre>
0
1
0
2
3
</pre>
|
p02308 |
<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>Cross Points of a Circe and a Line</H1>
<p>
For given a circle $c$ and a line $l$, print the coordinates of the cross points of them.
</p>
<H2>Input</H2>
<p>
The input is given in the following format.
</p>
<p>
$cx\; cy\; r$<br>
$q$<br>
$Line_1$<br>
$Line_2$<br>
:<br>
$Line_q$<br>
</p>
<p>
In the first line, the center coordinate of the circle and its radius are given by $cx$, $cy$ and $r$. In the second line, the number of queries $q$ is given.
</p>
<p>
In the following $q$ lines, as queries, $Line_i$ are given ($1 \leq i \leq q$) in the following format.
</p>
<p>
$x_1\; y_1\; x_2\; y_2$
</p>
<p>
Each line is represented by two points $p1$ and $p2$ which the line crosses. The coordinate of $p1$ and $p2$ are given by ($x1$, $y1$) and ($x2$, $y2$) respectively. All input values are given in integers.
</p>
<H2>Output</H2>
<p>
For each query, print the coordinates of the cross points in the following rules.
</p>
<ul>
<li>If there is one cross point, print two coordinates with the same values.</li>
<li>Print the coordinate with smaller $x$ first. In case of a tie, print the coordinate with smaller $y$ first.</li>
</ul>
<p>
The output values should be in a decimal fraction with an error less than 0.000001.
</p>
<H2>Constraints</H2>
<ul>
<li>$p1$ and $p2$ are different</li>
<li>The circle and line have at least one cross point</li>
<li>$1 \leq q \leq 1,000$</li>
<li>$-10,000 \leq cx, cy, x1, y1, x2, y2 \leq 10,000$</li>
<li>$1 \leq r \leq 10,000$</li>
</ul>
<H2>Sample Input and Output</H2>
<br>
<H2>Sample Input</H2>
<pre>
2 1 1
2
0 1 4 1
3 0 3 3
</pre>
<H2>Sample Output</H2>
<pre>
1.00000000 1.00000000 3.00000000 1.00000000
3.00000000 1.00000000 3.00000000 1.00000000
</pre>
|
p00335 |
<script type="text/x-mathjax-config">
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});
</script>
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML">
</script>
<H1>パンケーキ</H1>
<p>
あなたが勤めているパンケーキ屋では、細長い鉄板にパンケーキの生地を横1列に並べて焼きます。パンケーキはへらで何回か裏返せば完成します。何回以上裏返せば完成するかはパンケーキごとに異なります。
</p>
<p>
へらは大きいので、隣り合ったパンケーキは2枚同時に裏返されてしまいます。このとき、これら2枚の位置は入れ替わりません。ただし、両端だけは、隣のパンケーキといっしょに裏返すだけでなく、1枚だけ裏返すこともできます。すべてのパンケーキを必要な回数以上裏返したら、全部いっぺんに鉄板からおろして完成です。
</p>
<p>
パンケーキを必要な回数より多く裏返すと固くなってしまうので、あまり多く裏返したくありません。そこであなたは、すべて完成するまでに、各パンケーキが裏返る回数の総和が最小になるような方法を見つけようと考えました。
</p>
<p>
鉄板の上のパンケーキの枚数と、完成するまでに何回以上裏返さなければならないかがパンケーキごとに与えられているとき、すべて完成するまでに各パンケーキが裏返る回数(へらを操作する回数ではない)の総和の最小値を計算するプログラムを作成せよ。
</p>
<h2>Input</h2>
<p>
入力は以下の形式で与えられる。
</p>
<pre>
<var>N</var>
<var>p<sub>1</sub></var> <var>p<sub>2</sub></var> ... <var>p<sub>N</sub></var>
</pre>
<p>
1行目にパンケーキの枚数 <var>N</var> (3 ≤ <var>N</var> ≤ 5000)が与えられる。2行目に各パンケーキが完成するまでに必要な裏返す回数 <var>p<sub>i</sub></var> (0 ≤ <var>p<sub>i</sub></var> ≤ 3)が与えられる。
</p>
<h2>Output</h2>
<p>
すべて完成するまでに各パンケーキが裏返る回数の総和の最小値を1行に出力する。
</p>
<h2>Sample Input 1</h2>
<pre>
3
1 2 1
</pre>
<h2>Sample Output 1</h2>
<pre>
4
</pre>
<p>
へらを1回操作して左端と真ん中のパンケーキを裏返すと、2個のパンケーキが1回ずつ裏返るので、この操作で裏返る回数は2回。さらに、へらを1回操作して真ん中と右端のパンケーキを裏返すと、2個のパンケーキが1回ずつ裏返るので、この操作で裏返る回数は2回。以上の総和4回が答えになる。
</p>
<br>
<h2>Sample Input 2</h2>
<pre>
3
0 3 0
</pre>
<h2>Sample Output 2</h2>
<pre>
6
</pre>
<p>
へらを1回操作して左端と真ん中のパンケーキを裏返すと、この操作で裏返る回数は2回。これを3回繰り返したときの、総和の6回が答えになる(真ん中のパンケーキは、両隣のどちらかのパンケーキと一緒に裏返すことしかできないことに注意)。
</p> |
p02758 | <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> robots numbered <var>1</var> to <var>N</var> placed on a number line. Robot <var>i</var> is placed at coordinate <var>X_i</var>. When activated, it will travel the distance of <var>D_i</var> in the positive direction, and then it will be removed from the number line. All the robots move at the same speed, and their sizes are ignorable.</p>
<p>Takahashi, who is a mischievous boy, can do the following operation any number of times (possibly zero) as long as there is a robot remaining on the number line.</p>
<ul>
<li>Choose a robot and activate it. This operation cannot be done when there is a robot moving.</li>
</ul>
<p>While Robot <var>i</var> is moving, if it touches another robot <var>j</var> that is remaining in the range <var>[X_i, X_i + D_i)</var> on the number line, Robot <var>j</var> also gets activated and starts moving. This process is repeated recursively.</p>
<p>How many possible sets of robots remaining on the number line are there after Takahashi does the operation some number of times? Compute this count modulo <var>998244353</var>, since it can be enormous.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 2 \times 10^5</var></li>
<li><var>-10^9 \leq X_i \leq 10^9</var></li>
<li><var>1 \leq D_i \leq 10^9</var></li>
<li><var>X_i \neq X_j (i \neq j)</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>X_1</var> <var>D_1</var>
<var>:</var>
<var>X_N</var> <var>D_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of possible sets of robots remaining on the number line, modulo <var>998244353</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2
1 5
3 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p>There are three possible sets of robots remaining on the number line: <var>\{1, 2\}</var>, <var>\{1\}</var>, and <var>\{\}</var>.</p>
<p>These can be achieved as follows:</p>
<ul>
<li>
<p>If Takahashi activates nothing, the robots <var>\{1, 2\}</var> will remain.</p>
</li>
<li>
<p>If Takahashi activates Robot <var>1</var>, it will activate Robot <var>2</var> while moving, after which there will be no robots on the number line. This state can also be reached by activating Robot <var>2</var> and then Robot <var>1</var>.</p>
</li>
<li>
<p>If Takahashi activates Robot <var>2</var> and finishes doing the operation, the robot <var>\{1\}</var> will remain.</p>
</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
6 5
-1 10
3 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>5
</pre>
<p>There are five possible sets of robots remaining on the number line: <var>\{1, 2, 3\}</var>, <var>\{1, 2\}</var>, <var>\{2\}</var>, <var>\{2, 3\}</var>, and <var>\{\}</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>4
7 10
-10 3
4 3
-4 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>16
</pre>
<p>None of the robots influences others.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>20
-8 1
26 4
0 5
9 1
19 4
22 20
28 27
11 8
-3 20
-25 17
10 4
-18 27
24 28
-11 19
2 27
-2 18
-1 12
-24 29
31 29
29 7
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>110
</pre>
<p>Remember to print the count modulo <var>998244353</var>.</p></section>
</div>
</span> |
p00765 |
<h1>Generic Poker</h1>
<!-- end en only -->
<!-- begin en only -->
<p>
You have a deck of <i>N</i> × <i>M</i> cards. Each card in the deck has a rank. The range of ranks is 1 through <i>M</i>, and the deck includes <i>N</i> cards of each rank.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
We denote a card with rank <i>m</i> by <i>m</i> here.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
You can draw a hand of <i>L</i> cards at random from the deck.
If the hand matches the given pattern,
some bonus will be rewarded.
A pattern is described as follows.
</p>
<!-- end en only -->
<pre>
hand_pattern = card_pattern<sub>1</sub> ' ' card_pattern<sub>2</sub> ' ' ... ' ' card_pattern<sub><i>L</i></sub>
card_pattern = '*' | var_plus
var_plus = variable | var_plus '+'
variable = 'a' | 'b' | 'c'
</pre>
<p>
<dl>
<dt> hand_pattern
<dd>
<!-- begin en only -->
A hand matches the hand_pattern if each card_pattern in the hand_pattern matches with a distinct card in the hand.
<!-- end en only -->
<dt> card_pattern
<dd>
<!-- begin en only -->
If the card_pattern is an asterisk ('*'), it matches any card.
Characters 'a', 'b', and 'c' denote variables and all the occurrences of the same variable match cards of the same rank.
A card_pattern with a variable followed by plus ('+')
characters matches a card whose rank is the sum of the rank corresponding to the variable
and the number of plus characters.
You can assume that, when a hand_pattern includes a card_pattern with
a variable followed by some number of plus characters, it also
includes card_patterns with that variable and all smaller numbers (including zero) of plus characters.
For example, if 'a+++' appears in a hand_pattern, card_patterns 'a',
'a+', and 'a++' also appear in the hand_pattern.
<!-- end en only -->
</dl>
</p>
<!-- begin en only -->
<p>
There is no restriction on which ranks different variables mean.
For example, 'a' and 'b' may or may not match cards of the same rank.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
We show some example hand_patterns. The pattern
<pre>
a * b a b
</pre>
matches the hand:
<pre>
3 3 10 10 9
</pre>
with 'a's and 'b's meaning 3 and 10 (or 10 and 3),
respectively. This pattern also matches the following hand.
<pre>
3 3 3 3 9
</pre>
In this case, both 'a's and 'b's mean 3. The pattern
<pre>
a a+ a++ a+++ a++++
</pre>
matches the following hand.
<pre>
4 5 6 7 8
</pre>
In this case, 'a' should mean 4.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
Your mission is to write a program that computes the probability that
a hand randomly drawn from the deck matches the given hand_pattern.
</p>
<!-- end en only -->
<h3>Input</h3>
<!-- begin en only -->
<p>
The input is a sequence of datasets. Each dataset is formatted as follows.
</p>
<!-- end en only -->
<blockquote>
<i>N</i> <i>M</i> <i>L</i><br>
<i>card_pattern<sub>1</sub></i> <i>card_pattern<sub>2</sub></i> ... <i>card_pattern<sub>L</sub></i>
</blockquote>
<!-- begin en only -->
<p>
The first line consists of three positive integers <i>N</i>, <i>M</i>, and <i>L</i>.
<i>N</i> indicates the number of cards in each rank, <i>M</i> indicates the number of ranks, and <i>L</i> indicates the number of cards in a hand. <i>N</i>, <i>M</i>, and <i>L</i> are constrained as follows.
</p>
<!-- end en only -->
<blockquote>
1 ≤ <i>N</i> ≤ 7<br>
1 ≤ <i>M</i> ≤ 60<br>
1 ≤ <i>L</i> ≤ 7<br>
<i>L</i> ≤ <i>N</i> × <i>M</i><br>
</blockquote>
<!-- begin en only -->
<p>
The second line describes a hand_pattern.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
The end of the input is indicated by a line containing three zeros
separated by a single space.
</p>
<!-- end en only -->
<h3>Output</h3>
<!-- begin en only -->
<p>
For each dataset, output a line containing a decimal fraction which means the probability of a hand matching the hand_pattern.
</p>
<p>
The output should not contain an error greater than 10<sup>−8</sup>.
</p>
<p>
No other characters should be contained in the output.
</p>
<!-- end en only -->
<h3>Sample Input</h3>
<pre>
1 1 1
a
3 3 4
a+ * a *
2 2 3
a a b
2 2 3
* * *
2 2 3
* b b
2 2 2
a a
2 3 3
a a+ a++
2 6 6
a a+ a++ b b+ b++
4 13 5
a a * * *
4 13 5
a a b b *
4 13 5
a a a * *
4 13 5
a a+ a++ a+++ a++++
4 13 5
* * * * *
4 13 5
a a a b b
4 13 5
a a a a *
7 60 7
a b a b c c *
7 60 7
* * * * * * *
7 60 7
a a+ a++ a+++ a++++ a+++++ a++++++
1 14 4
b a+ a a
0 0 0
</pre>
<h3>Output for the Sample Input</h3>
<pre>
1.0000000000
0.8809523810
1.0000000000
1.0000000000
1.0000000000
0.3333333333
0.4000000000
0.1212121212
0.4929171669
0.0492196879
0.0228091236
0.0035460338
1.0000000000
0.0014405762
0.0002400960
0.0002967709
1.0000000000
0.0000001022
0.0000000000
</pre>
|
p01877 |
<h2>F: きっちり - Kitsuchiri -</h2>
<h3>問題</h3>
<p>若ヶ松高校二年へ組の合津千里(あつちり)さんは数列が「きっちり」していなければ気がすまない。</p>
<p>
合津さんによると、「きっちり」した数列とは、長さが偶数で左右対称である数列のことである。
すなわち、長さ<var>N</var>(<var>N</var>は偶数)の数列<var>S</var>について、次の条件を満たせば数列<var>S</var>は「きっちり」している。
</p>
<pre><var>S_1 = S_N, S_2 = S_{N − 1},</var> ... <var>, S_{N/2} = S_{N − N/2 + 1}</var></pre>
<p>二年へ組の数学担当の先生は、合津さんから「きっちりしてください。」と要望され、授業で用いる数列を作り直さなければならない。</p>
<p>先生は、数列の<var>l</var>番目から<var>r</var>番目までのそれぞれの要素に数字<var>x</var>を足すクエリをいくつも適用することで、数列を「きっちり」させようと奮闘しているが、うまくいかないようだ。</p>
<p>二年へ組に所属する凄腕プログラマーのあなたの仕事は、先生が絶望してしまう前に、先生が作り直している数列が「きっちり」しているかを調べるプログラムを作ることだ。</p>
<h3>入力形式</h3>
<p>入力は以下の形式からなる。</p>
<pre>
<var>N</var>
<var>S_1</var> ... <var>S_N</var>
<var>Q</var>
<var>q_1</var>
...
<var>q_Q</var>
</pre>
<p><var>Q</var>はクエリの総数であり、<var>1 \≤ i \≤ Q</var>について、各<var>q_i</var>は<var>l</var>, <var>r</var>, <var>x</var>を順に1つの半角スペースで区切って与えらる。</p>
<p>また、次の制約を満たす。</p>
<ul>
<li><var>2 \≤ N \≤ 500,000</var>である。</li>
<li><var>N</var>は偶数である。</li>
<li><var>1 \≤ j \≤ N</var>について、<var>−100,000,000 \≤ S_j \≤ 100,000,000</var>である。</li>
<li><var>i</var>番目の各クエリを適用した後の数列の各要素<var>T_{i,j}</var>は<var>−100,000,000 \≤ T_{i,j} \≤ 100,000,000</var>を満たす。</li>
<li><var>1 \≤ Q \≤ 100,000</var>である。</li>
<li><var>1 \≤ l \≤ r \≤ N</var>である。</li>
<li><var>−1,000 \≤ x \≤ 1,000</var>である。</li>
</ul>
<h3>出力形式</h3>
<p>クエリ<var>i</var>まで処理した後の数列が「きっちり」していれば“1”を、そうでなければ“0”を<var>i</var>行目に出力せよ。</p>
<h3>入力例1</h3>
<pre>
10
0 1 2 3 4 4 3 2 1 0
7
2 6 0
2 4 5
7 9 10
2 4 5
3 8 100
4 6 1000
7 7 1000
</pre>
<h3>出力例1</h3>
<pre>
1
0
0
1
1
0
1
</pre>
<h3>入力例2</h3>
<pre>
10
4 4 4 4 4 4 4 4 6 4
5
9 9 -2
1 10 1000
1 10 -1000
3 8 100
5 6 1
</pre>
<h3>出力例2</h3>
<pre>
1
1
1
1
1
</pre> |
p01524 |
<h1>E - じゃんけん</h1>
<h2>問題文</h2>
<p>
E 君はじゃんけんが大好きである (ちなみにじゃんけんのルールについては<a href="http://ja.wikipedia.org/wiki/%E3%81%98%E3%82%83%E3%82%93%E3%81%91%E3%82%93">Wikipediaのじゃんけんのページ</a>を参照せよ).
ある日,町内でじゃんけん大会があるということを知った E 君は,じゃんけんの腕に自信があったこともあり早速出場することにした.
ところが大会当日,E 君を待ち受けていたのは,普通の「グー・チョキ・パー」のみからなる普通のじゃんけんではなく,
より多くの手からなる<strong>一般化されたじゃんけん</strong>であった.
</p>
<p>
<strong>一般化されたじゃんけん</strong>は 2 人のプレイヤーで行われるゲームである.
まず,2 人にはこのじゃんけんにおいて使用出来る手の個数 <var>N</var> と,<var>N</var> 個の手の勝敗関係を示す表が伝えられる.
そして,通常のじゃんけんと同じように同時に手を出しあい,伝えられた手の勝敗関係に基いて勝敗を決める.これを 1,000 回 繰り返す.
1 回のじゃんけんの勝敗の結果によって得点が手に入り,相手に勝利した場合は 3 点,相手と「あいこ」になった場合は 1 点が手に入る.相手に負けてしまった場合は得点を得られない.
</p>
<p>
このなんだかよくわからないルールのじゃんけんにすっかり戦意を喪失してしまった E 君であったが,どうやら聞くところによると,最終的に相手に得点で勝てなくても,<strong>350 点</strong>以上を獲得すれば何か景品がもらえるらしい.
めんどくさいので <strong>350 点</strong>を得てさっさと大会を後にすることにした.
</p>
<p>
この問題の目的は,一般化されたじゃんけんをプレイする AI を作成し,ジャッジ側の用意した AI と対戦させて <strong>350 点</strong>以上を獲得することである.
</p>
<h2>入出力形式</h2>
<p>
まず入力が以下の形式で与えられる.
</p>
<pre><var>N</var>
<var>a<sub>1,1</sub></var> ... <var>a<sub>1,N</sub> </var>
...
<var>a<sub>N,1</sub></var> ... <var>a<sub>N,N</sub></var>
</pre>
<p>
<var>N</var> はじゃんけんの手の数である.</br>
各 <var>a<sub>i,j</sub></var> は <var>N</var> 個の手の勝敗関係を表す.
<var>a<sub>i,j</sub></var> は <code>-</code>,<code>o</code>,<code>x</code> のいずれかであり,<code>-</code> ならば手 <var>i</var> が手 <var>j</var> に対し引き分けることを,<code>o</code> ならば手 <var>i</var> が手 <var>j</var> に対し勝つことを,<code>x</code> なら負けることを示す.
</p>
<p>
この条件の下で 1,000回 AI とじゃんけんをする.
</p>
<p>
プログラムは自分の使う手を出力すると,ジャッジの AI が使った手を入力で受けとる事ができる.
例えば C/C++で 3 番目の手を使う場合は </p>
<pre class="prettyprint">printf("3\n"); fflush(stdout);</pre>
<p>とする.次に,</p>
<pre class="prettyprint">scanf("%d", &judge_ai_hand);</pre>
<p>
とすると変数 <tt>judge_ai_hand</tt> にジャッジの AI が使った手が入る.
なおじゃんけんの手は1-indexedである.
</p>
<p>
1,000 回のじゃんけんを終えた後は直ちにプログラムを終了せよ.その後,獲得できた得点の合計が 350 点以上であれば正答と判定される.
</p>
<h2>制約</h2>
<ul>
<li><var>3 ≤ N ≤ 10</var> </li>
<li><var>i ≠ j</var> のとき,<var>a<sub>i,j</sub></var> ∈ { <code>o</code>, <code>x</code> } であり,
<var>a<sub>i,j</sub>=</var><code>o</code>,<var>a<sub>j,i</sub>=</var><code>x</code> または
<var>a<sub>i,j</sub>=</var><code>x</code>,<var>a<sub>j,i</sub>=</var><code>o</code> のどちらか片方が成り立つ.
<li><var>i = j</var> のとき, <var>a<sub>i,j</sub>=</var><code>-</code> である.</li>
<li>AI の出す手は勝敗関係の表 <var>a<sub>i,j</sub></var> と AI の過去の手にだけ依存し,競技者のプログラムの過去の手や直前に出した手には依存しない.</li>
<li><var>N</var> は整数である.</li>
<li>データセットに <var>N=10</var> のテストケースは高々 6 個しか含まれていない.</li>
</ul>
<h2>入出力例1</h2>
<!--
<pre>
<table class = "table table-striped table-bordered table-condensed">
<tr><th>じゃんけんの説明</th><th>プログラムの出力</th><th>プログラムへの入力</th></tr>
<tr><th></th><td></td><td>
4
-oox
x-oo
xx-o
oxx-
</td></tr>
<tr><th>1 回目に競技者の AI が使った手</th><td>1</td><td></td></tr>
<tr><th>1 回目にジャッジの AI が使った手</th><td></td><td>2</td></tr>
<tr><th> ... </th><td> ... </td><td> ... </td></tr>
<tr><th>1,000 回目に競技者の AI が使った手</th><td>4</td><td></td></tr>
<tr><th>1,000 回目にジャッジの AI が使った手</th><td></td><td>3</td></tr>
</table>
</pre>
-->
<pre>
<table width="600" class="withborder">
<tr><th>じゃんけんの説明</th><th>プログラムの出力</th><th>プログラムへの入力</th></tr>
<tr><th></th><td></td><td>
4
-oox
x-oo
xx-o
oxx-
</td></tr>
<tr><th>1 回目に競技者の AI が使った手</th><td>1</td><td></td></tr>
<tr><th>1 回目にジャッジの AI が使った手</th><td></td><td>2</td></tr>
<tr><th> ... </th><td> ... </td><td> ... </td></tr>
<tr><th>1,000 回目に競技者の AI が使った手</th><td>4</td><td></td></tr>
<tr><th>1,000 回目にジャッジの AI が使った手</th><td></td><td>3</td></tr>
</table>
</pre>
<p>
最初にプログラムはじゃんけんの手の表を受け取る.その後,1,000 回のじゃんけんを行う.
ここで,1 回目のじゃんけんでは競技者の AI はジャッジの AI に勝利しているが,
1,000 回目のじゃんけんでは競技者の AI はジャッジの AI に敗北していることに注意せよ.
</p>
<hr>
<address>Writer: 楠本充,小浜翔太郎</address>
<address>Tester: 森槙悟</address> |
p03519 | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given two sequences <var>a</var> and <var>b</var>, both of length <var>2N</var>. The <var>i</var>-th elements in <var>a</var> and <var>b</var> are <var>a_i</var> and <var>b_i</var>, respectively.
Using these sequences, Snuke is doing the job of calculating the <em>beauty</em> of pairs of <strong>balanced sequences of parentheses</strong> (defined below) of length <var>2N</var>.
The beauty of a pair <var>(s,t)</var> is calculated as follows:</p>
<ul>
<li>Let <var>X=0</var>.</li>
<li>For each <var>i</var> between <var>1</var> and <var>2N</var> (inclusive), increment <var>X</var> by <var>a_i</var> if <var>s_i = t_i</var>, and increment <var>X</var> by <var>b_i</var> otherwise.</li>
<li>The beauty of <var>(s,t)</var> is the final value of <var>X</var>.</li>
</ul>
<p>You will be given <var>Q</var> queries. Process them in order.
In the <var>i</var>-th query, update the value of <var>a_{p_i}</var> to <var>x_i</var>, and the value of <var>b_{p_i}</var> to <var>y_i</var>. Then, find the maximum possible beauty of a pair of balanced sequences of parentheses.</p>
<p>In this problem, only the sequences below are defined to be balanced sequences of parentheses.</p>
<ul>
<li>An empty string</li>
<li>The concatenation of <code>(</code>, <var>s</var>, <code>)</code> in this order, where <var>s</var> is a balanced sequence of parentheses</li>
<li>The concatenation of <var>s</var>, <var>t</var> in this order, where <var>s</var> and <var>t</var> are balanced sequences of parentheses</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N,Q \leq 10^{5}</var></li>
<li><var>-10^{9} \leq a_i,b_i,x_i,y_i \leq 10^{9}</var></li>
<li><var>1 \leq p_i \leq 2N</var></li>
<li>All input values are integers.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Partial Scores</h3><ul>
<li>In the test set worth <var>200</var> points, <var>N \leq 5</var> and <var>Q \leq 10</var>.</li>
<li>In the test set worth <var>300</var> points, <var>Q = 1</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>Q</var>
<var>a_1</var> <var>a_2</var> <var>...</var> <var>a_{2N}</var>
<var>b_1</var> <var>b_2</var> <var>...</var> <var>b_{2N}</var>
<var>p_1</var> <var>x_1</var> <var>y_1</var>
<var>:</var>
<var>p_Q</var> <var>x_Q</var> <var>y_Q</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print <var>Q</var> lines. The <var>i</var>-th line should contain the response to the <var>i</var>-th query.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2 2
1 1 7 3
4 2 3 3
2 4 6
3 2 5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>15
15
</pre>
<ul>
<li>The first query updates <var>a</var> and <var>b</var> to <var>a=(1,4,7,3),b=(4,6,3,3)</var>. The maximum possible beauty is <var>15</var> for <var>(s,t) =(</var><code>()()</code>,<code>()()</code><var>)</var>.</li>
<li>The second query updates <var>a</var> and <var>b</var> to <var>a=(1,4,2,3),b=(4,6,5,3)</var>. The maximum possible beauty is <var>15</var> for <var>(s,t) =(</var><code>()()</code>,<code>(())</code><var>)</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>7 7
34 -20 -27 42 44 29 9 11 20 44 27 19 -31 -29
46 -50 -11 20 28 46 12 13 33 -22 -48 -27 35 -17
7 27 34
12 -2 22
4 -50 -12
3 -32 15
8 -7 23
3 -30 11
4 -2 23
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>311
312
260
286
296
292
327
</pre></section>
</div>
</span> |
p01174 |
<H1><font color="#000">Problem D:</font> Rotation Estimation</H1>
<p>
Mr. Nod is an astrologist and has defined a new constellation. He took two photos of the
constellation to foretell a future of his friend. The constellation consists of <i>n</i> stars. The shape
of the constellation in these photos are the same, but the angle of them are different because
these photos were taken on a different day. He foretells a future by the difference of the angle
of them.
</p>
<p>
Your job is to write a program to calculate the difference of the angle of two constellation.
</p>
<H2>Input</H2>
<p>
The input is a sequence of datasets. Each dataset is given in the following format:
</p>
<pre>
<i>n</i>
<i>x</i><sub>1,1</sub> <i>y</i><sub>1,1</sub>
...
<i>x</i><sub>1,<i>n</i></sub> <i>y</i><sub>1,<i>n</i></sub>
<i>x</i><sub>2,1</sub> <i>y</i><sub>2,1</sub>
...
<i>x</i><sub>2,<i>n</i></sub> <i>y</i><sub>2,<i>n</i></sub>
</pre>
<p>
The first line of each dataset contains a positive integers <i>n</i> (<i>n</i> ≤ 1,000). The next <i>n</i> lines contain
two real numbers <i>x</i><sub>1,<i>i</i></sub> and <i>y</i><sub>1,<i>i</i></sub> (|<i>x</i><sub>1,<i>i</i></sub>|, |<i>y</i><sub>1,<i>i</i></sub>| ≤ 100), where (<i>x</i><sub>1,<i>i</i></sub> , <i>y</i><sub>1,<i>i</i></sub>) denotes the coordinates of
the <i>i</i>-th star of the constellation in the first photo. The next <i>n</i> lines contain two real numbers
<i>x</i><sub>2,<i>i</i></sub> and <i>y</i><sub>2,<i>i</i></sub> (|<i>x</i><sub>2,<i>i</i></sub>|, |<i>y</i><sub>2,<i>i</i></sub>| ≤ 100), where (<i>x</i><sub>2,<i>i</i></sub> , <i>y</i><sub>2,<i>i</i></sub> ) denotes the coordinates of the <i>i</i>-th star of the
constellation in the second photo.
</p>
<p>
Note that the ordering of the stars does <i>not</i> matter for the sameness. It is guaranteed that
distance between every pair of stars in each photo is larger than 10<sup>-5</sup>.
</p>
<p>
The input is terminated in case of <i>n</i> = 0. This is not part of any datasets and thus should not
be processed.
</p>
<H2>Output</H2>
<p>
For each dataset, you should print a non-negative real number which is the difference of the
angle of the constellation in the first photo and in the second photo. The difference should be in radian, and should not be negative. If there are two or more solutions, you should print
the smallest one. The difference may be printed with any number of digits after decimal point,
provided the absolute error does not exceed 10<sup>-7</sup>. No extra space or character is allowed.
</p>
<H2>Sample Input</H2>
<pre>
3
0.0 0.0
1.0 1.0
0.0 1.0
3.0 3.0
2.0 2.0
3.0 2.0
0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
3.14159265359
</pre>
|
p03149 | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given four digits <var>N_1, N_2, N_3</var> and <var>N_4</var>. Determine if these can be arranged into the sequence of digits "<var>1974</var>".</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>0 \leq N_1, N_2, N_3, N_4 \leq 9</var></li>
<li><var>N_1, N_2, N_3</var> and <var>N_4</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_1</var> <var>N_2</var> <var>N_3</var> <var>N_4</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If <var>N_1, N_2, N_3</var> and <var>N_4</var> can be arranged into the sequence of digits "<var>1974</var>", print <code>YES</code>; if they cannot, print <code>NO</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>1 7 9 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>YES
</pre>
<p>We can get <var>1974</var> by swapping <var>N_2</var> and <var>N_3</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>1 9 7 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>YES
</pre>
<p>We already have <var>1974</var> before doing anything.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1 2 9 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>NO
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>4 9 0 8
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>NO
</pre></section>
</div>
</span> |
p01461 |
<script src="./IMAGE/varmath.js" charset="UTF-8"></script>
<H1>Multi-ending Story</H1>
<p>
You are a programmer who loves bishojo games (a sub-genre of dating simulation games).
A game, which is titled "I * C * P * C!" and was released yesterday, has arrived to you just now.
This game has multiple endings. When you complete all of those endings, you can get a special figure of the main heroine, Sakuya.
So, you want to hurry and play the game!
But, let's calm down a bit and think how to complete all of the endings in the shortest time first.
</p>
<p>
In fact, you have a special skill that allows you to know the structure of branching points of games.
By using the skill, you have found out that all of the branching points in this game are to select two choices "Yes" or "No", and once a different choice is taken the branched stories flow to different endings; they won't converge any more, like a binary tree.
You also noticed that it takes exactly one minute to proceed the game from a branching point to another branching point or to an ending.
In addition, you can assume it only takes negligible time to return to the beginning of the game (``reset'') and to play from the beginning to the first branching point.
</p>
<p>
The game has an additional feature called "Quick Save", which can significantly reduce the playing time for completion.
The feature allows you to record the point where you are currently playing and return there at any time later.
You can record any number of times, but you can hold only the last recorded point.
That is, when you use Quick Save, you overwrite the previous record. If you want to return to the overwritten point, you must play the game from the beginning once again.
</p>
<p>
Well, let's estimate how long it will take for completing all of the endings in the shortest time.
</p>
<H2>Input</H2>
<p>
A data set is given in the following format.
</p>
<p>
The first line of the data set contains one integer <var>N</var> (<var>2 \leq N \leq 500{,}000</var>), which denotes the number of the endings in this game.
The following <var>N-1</var> lines describe the branching points.
The <var>i</var>-th line describes the branching point of ID number i and contains two integers <var>Yes_i</var> and <var>No_i</var> (<var>i + 1 \leq Yes_i, No_i \leq N</var>), which denote the ID numbers of the next branching points when you select Yes or No respectively.
<var>Yes_i = N</var> means that you can reach an ending if you select Yes, and so for <var>No_i = N</var>.
The branching point with ID 1 is the first branching point.
The branching points with ID between 2 and <var>N-1</var> (inclusive) appear exactly once in <var>Yes_i</var>'s and <var>No_i</var>'s.
</p>
<H2>Output</H2>
<p>
Print the shortest time in a line.
</p>
<H2>Sample Input 1</H2>
<pre>
4
2 3
4 4
4 4
</pre>
<H2>Output for the Sample Input 1</H2>
<pre>
6
</pre>
<H2>Sample Input 2</H2>
<pre>
5
5 2
3 5
5 4
5 5
</pre>
<H2>Output for the Sample Input 2</H2>
<pre>
8
</pre>
|
p01031 |
<h1>Problem D: Smell Searcher</h1>
<h2>Problem</h2>
<p>
廃津大学では強烈な香りを放つ<var>N</var>個の廃材が一列に並んでいます。
廃材には1から<var>N</var>の番号が順番にふられていて、<var>i</var>番目の廃材は強さ<var>a<sub>i</sub></var>の香りを放っています。
</p>
<p>
リヒト君は仕事で、すべての廃材の放つ香りの総和を求めるよう依頼されました。
香りの総和が<var>M</var>以上になると「大変きつい仕事」とみなされ特別支給金がもらえます。
</p>
<p>
この仕事を行うために、リヒト君は精度<var>R</var>の香り検出器を使います。
精度<var>R</var>の香り検出器を使うと<var>i</var>番目の廃材の香りを測ろうとすると同時に<var>i</var>−<var>R</var>,<var>i</var>−<var>R</var>+1,...,<var>i</var>−1,<var>i</var>,<var>i</var>+1,...,<var>i</var>+<var>R</var>−1,<var>i</var>+<var>R</var>番目の廃材の香りも検出されます。言い換えると、閉区間[ max(<var>i</var>−<var>R</var>,1), min(<var>i</var>+<var>R</var>,<var>N</var>) ] の廃材の香りを検出します。ここで、max(<var>a</var>,<var>b</var>)は<var>a</var>と<var>b</var>の最大値、min(<var>a</var>,<var>b</var>)は<var>a</var>と<var>b</var>の最小値を表します。
ただし、測った廃材の1つ隣の廃材の香りの強さは本来の香りの強さより<var>C</var>減らされ、2つ隣の廃材の強さ香りは本来の香りの強さより2*<var>C</var>減らされて検出されます。つまり、<var>j</var>(0≤<var>j</var>≤<var>R</var>)個隣にある廃材の香りの強さは <var>a<sub>i</sub></var> − <var>j</var> * <var>C</var> として検出されます。結果的に、<var>i</var>番目の廃材に精度<var>R</var>の検出器を使うことで検出された香りの強さの最大値が<var>i</var>番目の廃材の香りの強さとして認識されます。
</p>
<p>
精度の高い検出器を使うとその分費用がかかるため、リヒト君は検出器が認識した1から<var>N</var>番目の廃材の香りの総和が<var>M</var>以上になる最低の精度<var>R</var>の値を知りたいと思っています。
</p>
<h2>Input</h2>
<p>
入力は以下の形式で与えられる。
</p>
<pre>
<var>N</var> <var>M</var> <var>C</var>
<var>a<sub>1</sub></var> <var>a<sub>2</sub></var> ... <var>a<sub>N</sub></var>
</pre>
<p>
1行目に、1つの整数<var>N</var>,<var>M</var>,<var>C</var>が空白区切りで与えられる。2行目に<var>N</var>つの整数が空白区切りで与えられる。<var>a<sub>i</sub></var>は<var>i</var>番目の廃材の香りの強さを表す。
</p>
<h2>Constraints</h2>
<p>
入力は以下の制約を満たす。
</p>
<ul>
<li>1 ≤ <var>N</var> ≤ 10<sup>5</sup></li>
<li>1 ≤ <var>M</var> ≤ 10<sup>14</sup></li>
<li>1 ≤ <var>C</var> ≤ 10<sup>9</sup></li>
<li>0 ≤ <var>a<sub>i</sub></var> ≤ 10<sup>9</sup> (1 ≤ <var>i</var> ≤ <var>N</var>)</li>
</ul>
<h2>Output</h2>
<p>
精度<var>R</var>の検出器を使って認識される廃材の香りの総和を<var>M</var>以上にしたときの<var>R</var>の最小値を出力せよ。
それが不可能な場合は−1を出力せよ。
<var>R</var>は必ず0以上であり、負の値の精度は存在しない。
</p>
<h2>Sample Input1</h2>
<pre>
6 25 3
8 5 1 1 2 6
</pre>
<h2>Sample Output1</h2>
<pre>
1
</pre>
<p>
このとき、精度0の検出器を使うと
8 + 5 + 1 + 1 + 2 + 6 = 23 で25以上になりません。
精度1の検出器を使うと
max(8,5-3) + max(8-3,5,1-3) + max(5-3,1,1-3) + max(1-3,1,2-3) + max(1-3,2,6-3) + max(2-3,6)
= 8 + 5 + 2 + 1 + 3 + 6 = 25 で25以上になるので1が正解です。
</p>
<h2>Sample Input2</h2>
<pre>
4 10 1
1 2 3 4
</pre>
<h2>Sample Output2</h2>
<pre>
0
</pre>
<h2>Sample Input3</h2>
<pre>
4 11 1
1 2 3 4
</pre>
<h2>Sample Output3</h2>
<pre>
-1
</pre>
|
p00270 |
<H1>鉄道路線</H1>
<p>
ある国の鉄道網に、自動改札を導入することになりました。導入にあたって難しい問題の一つは、与えられた切符で、指定された駅の間を移動できるかどうかを判定することです。それぞれの切符には乗車駅と降車駅が記載されています。この切符でできるのは、「乗車駅で乗って、降車駅で降りる」ことだけではなく、途中乗車や途中下車も許されています。
</p>
<p>
この鉄道網にはS 個の駅があり、そのうちR 組の駅は隣り合っていて、他の駅を経由せずに双方向に鉄道で移動することができます。隣り合った駅を結ぶ線路はひとつしかありません。隣り合った駅の間の距離は、この線路に沿って測った距離です。ある駅からある駅までの経路は鉄道網の形状によっては複数通り考えられますが、そのうち最も距離が短くなるような経路を最短経路と呼ぶことにします。そのような経路が複数ある場合、どちらも最短経路として認められます。
</p>
<p>
乗車駅 a、降車駅 b の切符で駅 c から駅 d まで移動できるのは、以下の条件をすべて満たす経路 p が存在するときです。
</p>
<ul>
<li> 経路 p は、駅 a から駅 b への最短経路である。</li>
<li> 経路 p は、駅 a から出発し、駅 c、駅 d の順に経由し、駅 b で終わる経路である。また、駅 c から駅 d の区間はこの2 駅の最短経路になっている。</li>
</ul>
<p>
路線図と切符の情報が与えられます。次に、始点と終点の組がいくつか与えられるので、その切符で始点から終点へ移動できるかどうかを判定するプログラムを作成してください。
</p>
<h2>入力</h2>
<p>
入力は1つのデータセットからなる。入力データは以下の形式で与えられる。
</p>
<pre>
S R
u<sub>1</sub> v<sub>1</sub> w<sub>1</sub>
u<sub>2</sub> v<sub>2</sub> w<sub>2</sub>
:
u<sub>R</sub> v<sub>R</sub> w<sub>R</sub>
a b Q
c<sub>1</sub> d<sub>1</sub>
:
c<sub>Q</sub> d<sub>Q</sub>
</pre>
<p>
各行で与えられる数値は1つの空白で区切られている。
</p>
<p>
1行目は2つの整数からなる。S (2 ≤ S ≤ 100000) は鉄道路線図に現れる駅の数、R (1 ≤ R ≤ 200000) は隣り合った駅の組の数である。続く R 行に、隣り合った駅の間を直接つなぐ線路の情報が与えられる。u<sub>i</sub> と v<sub>i</sub> (1 ≤ u<sub>i</sub>, v<sub>i</sub> ≤ S) は i 番目の線路の両端の駅の番号を示す。w<sub>i</sub> (1 ≤ w<sub>i</sub> ≤ 1000) はこれらの駅の間の距離を表す整数である。ただし、各駅には 1 から S までの番号が重複なく割り振られており、u<sub>i</sub> ≠ v<sub>i</sub> とする。
</p>
<p>
続く1行は3つの整数からなる。最初の2つの整数は切符の区間を表し、a は乗車駅、b は降車駅 (1 ≤ a, b ≤ S) である。3つ目の整数 Q (1 ≤ Q ≤ 40000) は質問の数を示す。続く Q 行に質問が与えられる。 c<sub>i</sub> とd<sub>i</sub> (1 ≤ c<sub>i</sub>, d<sub>i</sub> ≤ S)は i 番目の質問の乗車駅と降車駅を示す。ただし、a ≠ b、c<sub>i</sub> ≠ d<sub>i</sub> とする。
</p>
<h2>出力</h2>
<p>
質問ごとに、与えられた切符で移動できるなら Yes を、できないなら No を1行に出力する。
</p>
<h2>入力例</h2>
<pre>
6 7
1 2 3
1 4 1
2 3 5
4 3 1
3 6 2
4 5 2
5 6 1
1 6 6
1 6
4 3
4 6
5 6
2 6
2 5
</pre>
<h2>出力例</h2>
<pre>
Yes
Yes
Yes
Yes
No
No
</pre> |
p01898 |
<link rel="stylesheet" href="css/description.css" type="text/css" />
<script language="JavaScript" type="text/javascript" src="js/varmath.js" charset="UTF-8"></script>
<h2>A: 席取り - Taking a Seat -</h2>
<h3>物語</h3>
<p>
A君はある試験を受けるために試験会場の教室に入った。
ただ、A君はとても神経質なタイプだ。
そのため、状況によってA君が座りたくない席がもしかしたらあるかもしれない。
そこで、A君はある条件をもとに座れる席がいくつあるか調べることにした。
</p>
<h3>問題</h3>
<p>
A君は神経質なタイプの人間だ。
まず、A君は前から1行目の席には座れない。
というのも、試験監督の近くに座ると緊張してしまって問題が解けなくなる。
また、右隣か左隣に人がいるとA君は座れない。
テスト中に隣の鉛筆の音で集中できなくなってしまうからだ。
さらに、うるさい奴の近く (上下左右斜めの8近傍) には座れない。休憩中、声が頭に響いて休めないからだ。
あと、当たり前だがすでに生徒が座っている席は座れない。
</p>
<p>
この条件の中で果たしてA君が座れる場所があるだろうか。
教室の席は<var>M × N</var>個並んでいるとして、その個数を求めよう。
</p>
<h3>入力形式</h3>
<p>
1行目では<var>M</var> (行数:縦の席数) と<var>N</var> (列数:横の席数) が与えられる。
2行目から<var>M</var>行、長さ<var>N</var>の文字列が1行ずつ与えられる。<var>i</var>番目の文字列の<var>j</var>文字目は<var>i</var>行<var>j</var>列目の席の状態を表す。各文字は'-'、'x'、もしくは'o'のいずれかであり、空席を'-'、休憩中うるさい生徒が座る席を'x'、それ以外の生徒が座っている席を'o'とする。
</p>
<h3>制約</h3>
<ul>
<li><var>0<M ≤ 100</var></li>
<li><var>0<N ≤ 100</var></li>
</ul>
<h3>出力形式</h3>
<p>条件によってA君が座れる席の個数を1行で出力せよ。最後に改行を忘れないこと。</p>
<h3>入力例1</h3>
<pre>
5 5
--o--
--xo-
--x--
o---x
--xoo
</pre>
<h3>出力例1</h3>
<pre>3</pre>
<h3>入力例2</h3>
<pre>
2 6
--oooo
x--o--
</pre>
<h3>出力例2</h3>
<pre>1</pre>
<h3>入力例3</h3>
<pre>
3 5
-----
-----
-----
</pre>
<h3>出力例3</h3>
<pre>10</pre>
<h3>入力例4</h3>
<pre>
4 6
o-oxoo
oo-ooo
oooxo-
o-ooox
</pre>
<h3>出力例4</h3>
<pre>0</pre> |
p00620 |
<H1><font color="#000000">Problem F:</font> Line Puzzle</H1>
<p>
プログラミングでパズルを解いてみよう。
</p>
<p>
<i>n</i> × <i>n</i> の数字が格子状に並んでいる。数字のいくつかは丸で囲まれており、これらを起点と呼ぶことにする。パズルのルールは以下のとおりである:
</p>
<ul>
<li>各起点から縦・横に進む1本の線を引く(斜めには引けない)。</li>
<li>通った数字の和が起点の数字と同じになるように線を伸ばす。</li>
<li>線は枝分かれしてはいけない。</li>
<li>線はすでに引かれた数字を通ることはできない(線は交差してはいけない)。</li>
<li>線は2個以上の起点を通ることはできない。</li>
</ul>
<p>
下図に示すように、パズルのゴールはすべての起点を使い、すべての数字に線を引くことである。
</p>
<br>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_wiringPuzzle">
</center>
<br>
<p>
あなたの仕事は、パズルを解くプログラムを作成することである。
ただしこの問題では、与えられたパズルが解けるものかどうかを判定するだけでよい。
</p>
<p>
</p>
<H2>Input</H2>
<p>
入力は複数のデータセットからなる。各データセットの形式は以下のとおりである:
</p>
<pre>
<i>n</i>
<i>n</i> × <i>n</i> の数字
</pre>
<p>
<i>n</i> × <i>n</i> の数字が与えられるパズルを示し、起点の数字は負の数として与えられる。
</p>
<p>
<i>n</i> が 0 のとき入力の終わりを示す。
</p>
<p>
<i>n</i> は 3 以上 8 以下、起点以外の数字は 1 以上 50 以下、起点は -50 以上 -1 以下であると仮定してよい。また、入力されるパズルの性質として以下のことを仮定してよい:
</p>
<ul>
<li>
与えられるパズルの各行、各列には最低でも1つの起点がある。
</li>
<li>
起点の個数は、全体の数字の個数 (<i>n</i> × <i>n</i>) の 20 % から 40 % 程度である。
</li>
</ul>
<H2>Output</H2>
<p>
各データセットに対して、パズルが解けるものであれば "YES" を、そうでなければ "NO" と1行に出力せよ。
</p>
<H2>Sample Input</H2>
<pre>
3
-3 1 1
2 -4 1
2 1 -1
3
-4 1 1
1 1 -6
1 -5 3
4
-8 6 -2 1
2 -7 -2 1
1 -1 1 1
1 1 1 -5
6
2 2 3 -7 3 2
1 -10 1 1 3 2
2 6 5 2 -6 1
3 4 -23 2 2 5
3 3 -6 2 3 7
-7 2 3 2 -5 -13
6
2 2 3 -7 3 2
1 -10 1 1 3 2
2 6 5 2 -6 1
3 4 -23 2 2 5
3 3 -6 2 3 7
-7 2 3 2 -5 -12
0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
YES
NO
NO
YES
NO
</pre>
|
p01932 |
<!-- - - - - - begin nicebody - - - - - -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({ tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]} });
</script>
<script type="text/javascript" async src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML">
</script>
<h1>B: 全日本帰りたい協会</h1>
<h2>問題</h2>
<p>株式会社 AOR (Association Of Return home)には $N$ 人の社員がいる。</p>
<p>
社員 $i$ はエレベーターを使って $1$ 階に下りたいと考えており、時刻 $t_i$ に $F_i$ 階のエレベーター前にやってくる。あなたは時刻 $0$ に $1$ 階に $1$ 基だけあるエレベーターを遠隔操作し、すべての社員を $1$ 階に送ることにした。エレベーターには $D$ 人までしか乗せられない。
エレベーターは単位時間に一階分移動するかその場にとどまる事ができる。 社員 $i$ はエレベーターが時刻 $t_i$ に $F_i$ 階にあって、自分が乗っても定員を超過しないときのみエレベーターに乗る。 時刻 $t_i$ にエレベーターに乗れないとき、階段で $1$ 階におりてしまう。 なお、乗り降りにかかる時間は無視できる。
</p>
<p>それぞれの社員がエレベーターに乗っている時間の合計の最小値を求めよ。 ただし、エレベーターで $1$ 階へ運べない人が $1$ 人でもいる場合は $-1$ を出力せよ。</p>
<h2>制約</h2>
<ul>
<li>$1 \le N \le 10^5$</li>
<li>$1 \le D \le 10^5$</li>
<li>$1 \le t_i \lt t_{i+1} \le 10^6$</li>
<li>$2 \le F_i \le 10^6$</li>
<li>入力は全て整数で与えらえる。</li>
</ul>
<h2>入力形式</h2>
<p>入力は以下の形式で与えられる。</p>
<p>$N \ D$<br>$t_1 \ F_1$<br>$t_2 \ F_2$<br>$\vdots$<br>$t_N \ F_N$</p>
<h2>出力</h2>
<p>それぞれの社員がエレベーターに乗っている時間の合計の最小値を出力せよ。ただし、エレベーターで $1$ 階へ運べない人が $1$ 人でもいる場合は $-1$ を出力せよ。また、末尾に改行も出力せよ。</p>
<h2>サンプル</h2>
<h3>サンプル入力 1</h3>
<pre>2 2
2 2
3 3
</pre>
<h3>サンプル出力 1</h3>
<pre>5
</pre>
<h3>サンプル入力 2</h3>
<pre>2 2
2 2
5 3
</pre>
<h3>サンプル出力 2</h3>
<pre>3
</pre>
<h3>サンプル入力 3</h3>
<pre>2 2
2 2
3 5
</pre>
<h3>サンプル出力 3</h3>
<pre>-1
</pre>
<!-- - - - - - end nicebody - - - - - --> |
p03325 | <span class="lang-en">
<p>Score: <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3>
<p>As AtCoder Beginner Contest 100 is taking place, the office of AtCoder, Inc. is decorated with a sequence of length <var>N</var>, <var>a = </var>{<var>a_1, a_2, a_3, ..., a_N</var>}.<br/>
Snuke, an employee, would like to play with this sequence.</p>
<p>Specifically, he would like to repeat the following operation as many times as possible:</p>
<pre>For every <var>i</var> satisfying <var>1 \leq i \leq N</var>, perform one of the following: "divide <var>a_i</var> by <var>2</var>" and "multiply <var>a_i</var> by <var>3</var>".
Here, choosing "multiply <var>a_i</var> by <var>3</var>" for every <var>i</var> is not allowed, and the value of <var>a_i</var> after the operation must be an integer.
</pre>
<p>At most how many operations can be performed?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3>
<ul>
<li><var>N</var> is an integer between <var>1</var> and <var>10 \ 000</var> (inclusive).</li>
<li><var>a_i</var> is an integer between <var>1</var> and <var>1 \ 000 \ 000 \ 000</var> (inclusive).</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3>
<p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>a_1</var> <var>a_2</var> <var>a_3</var> <var>...</var> <var>a_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3>
<p>Print the maximum number of operations that Snuke can perform.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
5 2 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p>The sequence is initially <var>{5, 2, 4}</var>. Three operations can be performed as follows:</p>
<ul>
<li>First, multiply <var>a_1</var> by <var>3</var>, multiply <var>a_2</var> by <var>3</var> and divide <var>a_3</var> by <var>2</var>. The sequence is now <var>{15, 6, 2}</var>.</li>
<li>Next, multiply <var>a_1</var> by <var>3</var>, divide <var>a_2</var> by <var>2</var> and multiply <var>a_3</var> by <var>3</var>. The sequence is now <var>{45, 3, 6}</var>.</li>
<li>Finally, multiply <var>a_1</var> by <var>3</var>, multiply <var>a_2</var> by <var>3</var> and divide <var>a_3</var> by <var>2</var>. The sequence is now <var>{135, 9, 3}</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4
631 577 243 199
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>No operation can be performed since all the elements are odd. Thus, the answer is <var>0</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>10
2184 2126 1721 1800 1024 2528 3360 1945 1280 1776
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>39
</pre></section>
</div>
</span> |
p01318 |
<h1><font color="#000">Problem E:</font> Immortal Jewels</h1>
<!--
<h3><U>不死の宝石</U></h3>
-->
<p>English text is not available in this practice contest.</p>
<p>ある時ある貴族が,ある貧乏な国のおてんばで勇敢なお姫様に惚れ込み結婚を申し込んだ.お姫様は貴族にある条件を出した.その条件とは「不死の宝石」と呼ばれている宝石を大量に持ってくることであった.不死の宝石は,ある山の特定の場所でしか取ることができない非常に希少な宝石である.しかも大変壊れやすいため,採取するには特別な方法が必要だった.</p>
<p>不死の宝石は円形をしており,二次元空間上に複数存在している.この宝石を取るには,ある特別な金属の棒でそれらを吸着する必要がある.金属の棒は無限の長さを持つ直線であり,太さは無視することができる.宝石は一つ一つ異なる強さの磁力を持っており,金属がその磁力に反応するほど十分に近ければ宝石は吸着される.具体的には,金属と宝石の表面との距離を <i>d</i> ,宝石の磁力の強さを <i>m</i> としたとき,</p>
<blockquote>0 ≤ <i>d</i> ≤ <i>m</i></blockquote>
<p>であれば宝石は金属に吸着される.逆に,金属の棒と宝石がその磁力よりも離れている場合は吸着できない.また,棒が少しでも宝石を貫通してしまった場合にも,その宝石が壊れてしまうため吸着できない.</p>
<p>例を見てみよう.下の図は二次元空間上に置かれた宝石の例である.宝石1から宝石6まであり,磁力はそれぞれ1, 0, 1, 1, 1, 2であるとする.</p>
<center>
<p><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_E1"/><br />
図 E-1: 宝石の配置例</p>
</center>
<p>上の図に加えて金属の棒を配置した例が下の図である.宝石を吸着した結果も表に示してある.この例の場合,宝石3は磁力の届く範囲より離れており,また宝石4は棒に貫通されているため吸着できないが,残りの4つは全て吸着することができる.</p>
<center>
<p><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_E2" /><br />
図 E-2: 金属の棒の配置例</p>
</center>
<center>
<table border="1">
<tr>
<th>宝石名</th>
<th>磁力</th>
<th>金属との距離</th>
<th>吸着できるか</th>
</tr>
<tr>
<td>宝石1</td>
<td>1</td>
<td>約0.21</td>
<td>できる</td>
</tr>
<tr>
<td>宝石2</td>
<td>0</td>
<td>0</td>
<td>できる</td>
</tr>
<tr>
<td>宝石3</td>
<td>1</td>
<td>約5.37</td>
<td>できない</td>
</tr>
<tr>
<td>宝石4</td>
<td>1</td>
<td>貫通している</td>
<td>できない</td>
</tr>
<tr>
<td>宝石5</td>
<td>1</td>
<td>約0.97</td>
<td>できる</td>
</tr>
<tr>
<td>宝石6</td>
<td>2</td>
<td>約0.53</td>
<td>できる</td>
</tr>
</table>
表 E-3: 吸着結果</center>
<p>貴族は全財産を注ぎ込み,特別な金属の棒を必死に探し求めた.しかしながら,この金属も非常に貴重であったため,結局たった一本しか入手することができなかった.したがって吸着のチャンスはただ一回しか無い.</p>
<p>あなたはある貴族に仕えるプログラマーである.あなたの仕事は,与えられた二次元上の宝石の配置に対して,金属の棒をうまく配置したときに,最大で何個の宝石を吸着することができるかを求めるプログラムを書くことである.</p>
<h2>Input</h2>
<p>入力は複数のデータセットから成り, 1つのデータセットは以下の形式で与えられる.</p>
<blockquote><i>N</i><br />
<i>x<sub>1</sub></i> <i>y<sub>1</sub></i> <i>r<sub>1</sub></i> <i>m<sub>1</sub></i><br />
<i>x<sub>2</sub></i> <i>y<sub>2</sub></i> <i>r<sub>2</sub></i> <i>m<sub>2</sub></i><br />
...<br />
<i>x<sub>N</sub></i> <i>y<sub>N</sub></i> <i>r<sub>N</sub></i> <i>m<sub>N</sub></i><br />
</blockquote>
<p>データセットの最初の行は宝石の数 <i>N</i> (1 ≤ <i>N</i> ≤ 50) を表している.続く <i>N</i> 行の各行には4つの整数 <i>x<sub>i</sub></i>, <i>y<sub>i</sub></i>, <i>r<sub>i</sub></i>, <i>m<sub>i</sub></i> (-1000 ≤ <i>x<sub>i</sub></i>, <i>y<sub>i</sub></i> ≤ 1000, 1 ≤ <i>r<sub>i</sub></i> ≤ 100, 0 ≤ <i>m<sub>i</sub></i> ≤ 100) が記述されており,宝石の位置,大きさ,および磁力を表す.すなわち,宝石 <i>i</i> は中心を(<i>x<sub>i</sub></i>, <i>y<sub>i</sub></i>)とし半径が <i>r<sub>i</sub></i> であるような円形をしており,その磁力は <i>m<sub>i</sub></i> である.宝石は互いに重なり合うことは無い.</p>
<p>入力の終わりは,0のみからなる行で表される.</p>
<h2>Output</h2>
<p>各データセットについて, 一度に吸着することができる宝石の最大数を1行に出力せよ.</p>
<h2>Sample Input</h2>
<pre>
6
-2 -2 1 1
2 2 2 0
5 7 1 1
8 0 3 1
13 4 1 1
16 1 1 2
3
0 0 2 1
10 0 2 1
0 10 2 1
3
0 0 2 1
10 0 2 1
0 6 2 1
3
0 0 2 1
10 0 2 1
0 4 2 1
1
0 0 1 1
0
</pre>
<h2>Output for the Sample Input</h2>
<pre>
4
2
3
3
1
</pre>
|
p02867 | <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given are two integer sequences of <var>N</var> elements each: <var>A_1,...,A_N</var> and <var>B_1,...,B_N</var>.
Determine if it is possible to do the following operation at most <var>N-2</var> times (possibly zero) so that, for every integer <var>i</var> from <var>1</var> to <var>N</var>, <var>A_i \leq B_i</var> holds:</p>
<ul>
<li>Choose two distinct integers <var>x</var> and <var>y</var> between <var>1</var> and <var>N</var> (inclusive), and swap the values of <var>A_x</var> and <var>A_y</var>.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq N \leq 10^5</var></li>
<li><var>1 \leq A_i,B_i \leq 10^9</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>A_1</var> <var>A_2</var> <var>...</var> <var>A_N</var>
<var>B_1</var> <var>B_2</var> <var>...</var> <var>B_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If the objective is achievable, print <code>Yes</code>; if it is not, print <code>No</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
1 3 2
1 2 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<p>We should swap the values of <var>A_2</var> and <var>A_3</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
1 2 3
2 2 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>No
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>6
3 1 2 6 3 4
2 2 8 3 4 3
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>Yes
</pre></section>
</div>
</span> |
p03775 | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given an integer <var>N</var>.<br/>
For two positive integers <var>A</var> and <var>B</var>, we will define <var>F(A,B)</var> as the larger of the following: the number of digits in the decimal notation of <var>A</var>, and the number of digits in the decimal notation of <var>B</var>.<br/>
For example, <var>F(3,11) = 2</var> since <var>3</var> has one digit and <var>11</var> has two digits.<br/>
Find the minimum value of <var>F(A,B)</var> as <var>(A,B)</var> ranges over all pairs of positive integers such that <var>N = A \times B</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 10^{10}</var></li>
<li><var>N</var> is an integer.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum value of <var>F(A,B)</var> as <var>(A,B)</var> ranges over all pairs of positive integers such that <var>N = A \times B</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>10000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p><var>F(A,B)</var> has a minimum value of <var>3</var> at <var>(A,B)=(100,100)</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>1000003
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>7
</pre>
<p>There are two pairs <var>(A,B)</var> that satisfy the condition: <var>(1,1000003)</var> and <var>(1000003,1)</var>. For these pairs, <var>F(1,1000003)=F(1000003,1)=7</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>9876543210
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>6
</pre></section>
</div>
</span> |
p01748 |
<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>n</var> 個の都市と <var>n − 1</var> 本の道路があり,木になっている.都市には 1 から <var>n</var> までの番号がつけられている.都市 1 を根とみたとき,都市 <var>i</var> の親は <var>p<sub>i</sub></var> であり,<var>i</var> と <var>p<sub>i</sub></var> の距離は <var>d<sub>i</sub></var> である.すぬけ君は,1 以上 <var>n</var> 以下の各 <var>k</var> に対し,次の問題を解きたい.
</p>
<p>
ある都市から都市 1, . . . . , <var>k</var> への距離の和の最小値
\begin{eqnarray}
min_{1 \leq v \leq n} \{ \sum^k_{i=1} dist(i, v)\} \;\;\;\;\;\;\;\;\;\;\;\;\;\;(2)
\end{eqnarray}
<p>
を求めよ.ただし <var>dist(u, v)</var> は <var>u</var> と <var>v</var> の距離をあらわす.
</p>
<h2>Constraints</h2>
<ul>
<li> 1 ≤ <var>n</var> ≤ 200000 </li>
<li> 1 ≤ <var>p<sub>i</sub></var> ≤ <var>n</var> </li>
<li> 1 ≤ <var>d<sub>i</sub></var> ≤ 200000 </li>
<li> <var>p<sub>i</sub></var> によって表されるグラフは木になっている </li>
<li> 入力は全て整数である </li>
</ul>
<h2>Input</h2>
<pre>
<var>n</var>
<var>p<sub>2</sub></var> <var>d<sub>2</sub></var>
. . .
<var>p<sub>n</sub></var> <var>d<sub>n</sub></var>
</pre>
<h2>Output</h2>
<p>
<var>n</var> 行出力せよ.<var>i</var> 行目には,<var>k = i</var> のときの答えを出力せよ.
</p>
<h2>Sample Input 1</h2>
<pre>
10
4 1
1 1
3 1
3 1
5 1
6 1
6 1
8 1
4 1
</pre>
<h2>Sample Output 1</h2>
<pre>
0
3
3
4
5
7
10
13
16
19
</pre>
<h2>Sample Input 2</h2>
<pre>
15
1 3
12 5
5 2
12 1
7 5
5 1
6 1
12 1
11 1
12 4
1 1
5 5
10 4
1 2
</pre>
<h2>Sample Output 2</h2>
<pre>
0
3
9
13
14
21
22
29
31
37
41
41
47
56
59
</pre> |
p00509 | <H1>問題5</H1>
<br/>
<p>
J中学校の運動会で次のようなクラス対抗の競技を行なうことになった.
各クラスから生徒代表の男女 n ペア<br/>
<br/>
(男子生徒 b<SUB>1</SUB>, 女子生徒 g<SUB>1</SUB>), (男子生徒 b<SUB>2</SUB>, 女子生徒 g<SUB>2</SUB>), … , (男子生徒 b<SUB>n</SUB>, 女子生徒 g<SUB>n</SUB>)
<br/>
<br/>
を選び,
0 から 9 までの数字が書かれた旗を自由に選んでもらい
(それぞれの旗は十分な個数ある),
横一列に並んでもらう.
ただし,
ペアになっている男子生徒と女子生徒は同じ番号の旗を一人1つずつ
選ばなければならず,
並び順は
<br/>
<br/>
b<SUB>1</SUB> b<SUB>2</SUB> … b<SUB>n</SUB> c g<SUB>n</SUB> … g<SUB>2</SUB> g<SUB>1</SUB>
<br/>
<br/>
のように,
男子生徒達 n 人の並び順
b<SUB>1</SUB> b<SUB>2</SUB> … b<SUB>n</SUB> とは逆の順番
g<SUB>n</SUB> … g<SUB>2</SUB> g<SUB>1</SUB> で女子生徒達は
並ばなければならない.
中央の c には,
クラス担任の先生があらかじめ審判長によって指定された
番号の付いた旗を持って立つ場合と,
立たないことを審判長に指定される場合とがある.
</p>
<p>
こうして並んだ生徒達(と先生)が持った旗の番号
を 2n 桁(または 2n+1 桁)の整数と考えたとき,
それが素数である方が勝ちである.
ただし,
両クラスとも素数だった場合あるいは両クラスとも素数でなかった場合には,
数が大きい方が勝ちである. また,
<br/>
<br/>
0 b<sub>2</sub> … b<sub>n</sub> c g<sub>n</sub> … g<sub>2</sub> 0
あるいは
0 b<sub>2</sub> … b<sub>n</sub> g<sub>n</sub> … g<sub>2</sub> 0
<br/>
<br/>
のように先頭に 0 が来るものは,
普通使われる数の表し方ではないので禁止されている.
</p>
<p>
J中学校の生徒であるあなたは,
自分のクラスが勝つためにはどのように並ぶのがよいかを
考えなければならない.
</p>
<p>
入力ファイルの1行目に正整数 n と
1桁の整数 c(先生が持つ旗の番号)が
1つの半角空白文字で区切られて書かれている.
c<0 のときは,
先生が中央に立たないことを表わす.
<!--5つの入力ファイルのうち4つでは 1≦n≦4 である.-->
与えられる入力は 1≦n≦10 である.
</p>
<p>
負けない並び方の順番
<br/>
<br/>
b<SUB>1</SUB> b<SUB>2</SUB> … b<SUB>n</SUB> c g<SUB>n</SUB> … g<SUB>2</SUB> g<SUB>1</SUB>
または
b<SUB>1</SUB> b<SUB>2</SUB> … b<SUB>n</SUB> g<SUB>n</SUB> … g<SUB>2</SUB> g<SUB>1</SUB>
<br/>
<br/>
を出力せよ.
</p>
<p>
<!-- 入力ファイルの改行コードは CR+LF である.
また,-->
出力ファイルにおいては,
出力の最後にも改行コードを入れること.
<h2>入出力例</h2>
<h3>入力例1</h3>
<pre>1 0</pre>
<h3>出力例1</h3>
<pre>101</pre>
<h3>入力例2</h3>
<pre>1 5</pre>
<h3>出力例2</h3>
<pre>757</pre>
<h3>入力例3</h3>
<pre>3 7</pre>
<h3>出力例3</h3>
<pre>9957599</pre>
<h3>入力例4</h3>
<pre>1 -1</pre>
<h3>出力例4</h3>
<pre>11</pre>
<div class="source">
<p class="source">
問題文と自動審判に使われるデータは、<a href="http://www.ioi-jp.org">情報オリンピック日本委員会</a>が作成し公開している問題文と採点用テストデータです。
</p>
</div>
|
p02534 | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given an integer <var>K</var>.
Print the string obtained by repeating the string <code>ACL</code> <var>K</var> times and concatenating them.</p>
<p>For example, if <var>K = 3</var>, print <code>ACLACLACL</code>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq K \leq 5</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>K</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the string obtained by repeating the string <code>ACL</code> <var>K</var> times and concatenating them.</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>ACLACLACL
</pre></section>
</div>
</span> |
p00159 |
<H1>理想の体型</H1>
<p>
肥満度を表す指数としてBMI(Body Mass Index)があります。BMIの値は以下の式で計算します。
</p>
<center>
<p>
BMI = 体重(kg) / (身長(m))<sup>2</sup>
</p>
</center>
<p>
BMIの値が標準値に近いほど「理想の体型」と考えられます。そこで、BMIの標準値を 22 とした場合、対象者の情報を入力とし、最も「理想の体型」に近い人の情報を出力するプログラムを作成してください。
</p>
<p>
対象者の数を <var>n</var> とすると、各対象者には重複のないように1 以上 <var>n</var> 以下の整数値の受付番号 <var>p</var> が振られています。
</p>
<H2>Input</H2>
<p>
複数のデータセットの並びが入力として与えられます。入力の終わりはゼロひとつの行で示されます。各データセットは以下の形式で与えられます。
</p>
<pre>
<var>n</var>
<var>p<sub>1</sub></var> <var>h<sub>1</sub></var> <var>w<sub>1</sub></var>
<var>p<sub>2</sub></var> <var>h<sub>2</sub></var> <var>w<sub>2</sub></var>
:
<var>p<sub>n</sub></var> <var>h<sub>n</sub></var> <var>w<sub>n</sub></var>
</pre>
<p>
1行目に対象者の人数 <var>n</var> (<var>n</var> ≤ 1000)、続く <var>n</var> 行に <var>i</var> 人目の対象者の受付番号 <var>p<sub>i</sub></var> (1 ≤ <var>p<sub>i</sub></var> ≤ <var>n</var>)、センチメートル単位の身長 <var>h<sub>i</sub></var> (1 ≤ <var>h<sub>i</sub></var> ≤ 200)、キログラム単位の体重 <var>w<sub>i</sub></var> (1 ≤ <var>w<sub>i</sub></var> ≤ 200) が与えられます。入力は全て整数で与えられます。
</p>
<H2>Output</H2>
<p>
データセットごとに「理想の体型」に最も近い人の受付番号(整数)を1行に出力します。最も「理想の体型」に近い人が二人以上いる場合は、受付番号の小さい方を出力することとします。
</p>
<H2>Sample Input</H2>
<pre>
6
1 165 66
2 178 60
3 180 72
4 160 65
5 185 62
6 182 62
3
3 160 65
2 180 70
1 170 75
0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
3
2
</pre>
|
p02164 | <h1>Problem C: Satake likes straight</h1>
<h2>Problem</h2>
<p>
Satake君は曲がったことが嫌いです。<br/>
例えば、鉛筆や箸、家が曲がった形をしているのは嫌いですし、右折や左折などの行動をとることも嫌いです。
</p>
<p>
さて、XY 平面上で暮らすSatake君は $N$ 個のお店 $(X_{1},Y_{1}),(X_{2},Y_{2}), \ldots , (X_{N},Y_{N})$ で買い物をするようお使いを頼まれました。座標 $(0, 0)$ にある家から $(1, 0)$ の方向を向いた状態で出発し、すべてのお店で買い物をしたあと、家に帰ります。お店を回る順番は自由です。Satake君はお使いとして次に示す任意の行動を何度でも行うことができます。
</p>
<ol>
<li>向いている方向に好きなだけ進む</li>
<li>お店か家と同じ座標のとき時計回りか反時計回りに好きな角度だけその場で回転する</li>
<li>お店と同じ座標のとき座標や向いている方向の状態を維持したまま買い物をする</li>
</ol>
<p>
先程も言ったようにSatake君は曲がることが嫌いです。心の準備ができるように、Satake君が行動2.で回転する角度の和の最小値を求めてください。例として、時計回りに $90^{\circ}$ 回転した後、反時計回りに $90^{\circ}$ 回転した場合、角度の和は $180^{\circ}$ となります。
</p>
<h2>Input</h2>
<p>入力は以下の形式で与えられます。</p>
<pre>
$N$
$X_{1}$ $Y_{1}$
$\vdots$
$X_{N}$ $Y_{N}$
</pre>
<p>
入力は $N+1$ 行からなります。<br/>
$1$ 行目には買い物をする店の個数を表す $N$ が与えられます。<br/>
$2$ 行目から続く $N$ 行には、買い物をするお店の座標 $X_{i}, Y_{i}$ が空白区切りで与えられます。<br/>
</p>
<h2>Constraints</h2>
<p>入力は以下の条件を満たします。</p>
<ul>
<li>$2 \le N \le 8$</li>
<li>$-1000 \le X_{i}, Y_{i} \le 1000 \quad (1 \le i \le N)$</li>
<li>$(X_{i}, Y_{i}) \ne (0, 0) \quad (1 \le i \le N)$</li>
<li>$(X_{i}, Y_{i}) \ne (X_{j}, Y_{j}) \quad (i \ne j)$</li>
<li>入力はすべて整数</li>
</ul>
<h2>Output</h2>
<p>Satake君が回転する角度の和の最小値を度数法で出力してください。ただし想定解との絶対誤差が $10^{-4}$ 以下のときのみ正解とします。</p>
<h2>Sample Input 1</h2>
<pre>
2
0 1
0 -1
</pre>
<h2>Sample Output 1</h2>
<pre>
450.00000000
</pre>
<p>Satake君は最初 $(1, 0)$ 方向を向いて出発することに注意してください。</p>
<h2>Sample Input 2</h2>
<pre>
3
1 0
0 1
-2 -1
</pre>
<h2>Sample Output 2</h2>
<pre>
386.565051
</pre>
|
p02471 |
<H1>Extended Euclid Algorithm</H2>
<br/>
<p>
Given positive integers <var>a</var> and <var>b</var>, find the integer solution (<var>x</var>, <var>y</var>) to <var>ax + by = gcd(a, b)</var>, where <var>gcd(a, b)</var> is the greatest common divisor of <var>a</var> and <var>b</var>.
</p>
<H2>Input</H2>
<pre>
<var>a</var> <var>b</var>
</pre>
<p>
Two positive integers <var>a</var> and <var>b</var> are given separated by a space in a line.
</p>
<H2>Output</H2>
<p>
Print two integers <var>x</var> and <var>y</var> separated by a space. If there are several pairs of such <var>x</var> and <var>y</var>, print that pair for which <var>|x| + |y|</var> is the minimal (primarily) and <var>x</var> ≤ <var>y</var> (secondarily).
</p>
<h2>Constraints</h2>
<ul>
<li> 1 ≤ <var>a</var>, <var>b</var> ≤ 10<sup>9</sup></li>
</ul>
<H2>Sample Input 1</H2>
<pre>
4 12
</pre>
<H2>Sample Output 1</H2>
<pre>
1 0
</pre>
<br/>
<H2>Sample Input 2</H2>
<pre>
3 8
</pre>
<H2>Sample Output 2</H2>
<pre>
3 -1
</pre> |
p02021 | <h2>G: 労働 (Working)</h2>
<p>コウさんはこれから $N$ 日間、毎日<b>同じ件数の</b>仕事をすることにした。</p>
<p>$N$ 日間のうち $i$ 日目には仕事が $A_i$ 件追加される。</p>
<p>コウさんには今溜まっている仕事はなく、$N$ 日目までにすべての仕事が終わっている必要はない。</p>
<p>一日で最大何件の仕事ができるか。</p>
<p>ただしコウさんは優秀なので、仕事が存在する限り何件でも仕事をすることができる。</p>
<h3>入力</h3>
<p>1 行目には $N$が与えられる。</p>
<p>2 行目には $N$ 個の整数 $A_1, A_2, A_3, \dots, A_N$ が空白区切りで与えられる。</p>
<h3>出力</h3>
<p>一日にできる仕事の最大の件数を出力せよ。最後には改行を入れること。</p>
<h3>制約</h3>
<ul>
<li>$N$ は $1$ 以上 $100$ 以下の整数</li>
<li>$A_1, A_2, A_3, \dots, A_N$ は $1$ 以上 $100$ 以下の整数</li>
</ul>
<h3>入力例1</h3>
<pre>
5
4 2 5 3 1
</pre>
<h3>出力例1</h3>
<pre>
3
</pre>
<p>一日 $4$ 件以上仕事することにすると、2 日目に仕事が尽きてしまいます。</p>
<h3>入力例2</h3>
<pre>
5
9 9 1 9 9
</pre>
<h3>出力例2</h3>
<pre>
6
</pre>
|
p02888 | <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Takahashi has <var>N</var> sticks that are distinguishable from each other. The length of the <var>i</var>-th stick is <var>L_i</var>.</p>
<p>He is going to form a triangle using three of these sticks. Let <var>a</var>, <var>b</var>, and <var>c</var> be the lengths of the three sticks used. Here, all of the following conditions must be satisfied:</p>
<ul>
<li><var>a < b + c</var></li>
<li><var>b < c + a</var></li>
<li><var>c < a + b</var></li>
</ul>
<p>How many different triangles can be formed? Two triangles are considered different when there is a stick used in only one of them.</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 2 \times 10^3</var></li>
<li><var>1 \leq L_i \leq 10^3</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>L_1</var> <var>L_2</var> <var>...</var> <var>L_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><p>Print the number of different triangles that can be formed.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4
3 4 2 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
</pre>
<p>Only one triangle can be formed: the triangle formed by the first, second, and third sticks.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
1 1000 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>No triangles can be formed.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>7
218 786 704 233 645 728 389
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>23
</pre></section>
</div>
</span> |
p03260 | <span class="lang-en">
<p>Score : <var>100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>You are given integers <var>A</var> and <var>B</var>, each between <var>1</var> and <var>3</var> (inclusive).</p>
<p>Determine if there is an integer <var>C</var> between <var>1</var> and <var>3</var> (inclusive) such that <var>A \times B \times C</var> is an odd number.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>All values in input are integers.</li>
<li><var>1 \leq A, B \leq 3</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>A</var> <var>B</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>If there is an integer <var>C</var> between <var>1</var> and <var>3</var> that satisfies the condition, print <code>Yes</code>; otherwise, print <code>No</code>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>Yes
</pre>
<p>Let <var>C = 3</var>. Then, <var>A \times B \times C = 3 \times 1 \times 3 = 9</var>, which is an odd number.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>1 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>No
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>2 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>No
</pre></section>
</div>
</span> |
p02922 | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Takahashi's house has only one socket.</p>
<p>Takahashi wants to extend it with some number of power strips, each with <var>A</var> sockets, into <var>B</var> or more empty sockets.</p>
<p>One power strip with <var>A</var> sockets can extend one empty socket into <var>A</var> empty sockets.</p>
<p>Find the minimum number of power strips required.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li>All values in input are integers.</li>
<li><var>2 \leq A \leq 20</var></li>
<li><var>1 \leq B \leq 20</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>A</var> <var>B</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of power strips required.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>4 10
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p><var>3</var> power strips, each with <var>4</var> sockets, extend the socket into <var>10</var> empty sockets.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>8 9
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>2
</pre>
<p><var>2</var> power strips, each with <var>8</var> sockets, extend the socket into <var>15</var> empty sockets.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>8 8
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>1
</pre></section>
</div>
</span> |
p03630 | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a board with an <var>H \times W</var> grid.
Each square in the grid is painted in black or white. The square at the <var>i</var>-th row from the top and <var>j</var>-th column from the left is black if the <var>j</var>-th character in <var>S_i</var> is <code>#</code>, and white if that character is <code>.</code>.</p>
<p>Snuke can perform the following operation on the grid any number of times:</p>
<ul>
<li>Select a row or column in the grid, and invert the color of all the squares in that row or column (that is, black squares become white and vice versa).</li>
</ul>
<p>Then, Snuke draws a rectangle along grid lines. Here, all the squares contained in the rectangle must be painted in black.</p>
<p>Find the maximum possible area of Snuke's rectangle when the operation is performed optimally.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq H \leq 2000</var></li>
<li><var>2 \leq W \leq 2000</var></li>
<li><var>|S_i| = W</var></li>
<li><var>S_i</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>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 maximum possible area of Snuke's rectangle.</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>6
</pre>
<p>If the first row from the top and the third column from the left are inverted, a <var>2 \times 3</var> rectangle can be drawn, as shown below:</p>
<p><img alt="" src="https://atcoder.jp/img/arc081/2995c3921ed4dffc8ee528b63b9c6118.png"/></p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>4 4
....
....
....
....
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>16
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>10 8
##...#.#
##...#.#
..###.#.
#.##.#.#
.#..#.#.
..##.#.#
##.#.#..
...#.#..
###.#.##
###..###
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>27
</pre></section>
</div>
</span> |
p01162 |
<H1><font color="#000">Problem I:</font> The Extreme Slalom</H1>
<p>
Automobile Company Moving has developed a new ride for switchbacks. The most impressive feature of
this machine is its mobility that we can move and turn to any direction at the same speed.
</p>
<p>
As the promotion of the ride, the company started a new competition named <i>the extreme slalom</i>. A
course of the extreme slalom consists of some gates numbered from 1. Each gate is represented by a line
segment. A rider starts at the first gate and runs to the last gate by going through or touching the gates
in the order of their numbers. Going through or touching gates other than the target one is allowed but
not counted.
</p>
<p>
Your team is going to participate at the next competition. To win the competition, it is quite important
to run on the shortest path. Your task is to write a program that computes the shortest length to support
your colleagues.
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_slalom">
<p>Figure 1: An Example Course for the Extreme Slalom</p>
</center>
<H2>Input</H2>
<p>
The input consists of a number of courses.
</p>
<p>
The first line of a course is an integer indicating the number n (2 ≤ <i>n</i> ≤ 12) of gates. The following
n lines specify the gates in the order of traversals. A line contains four integers <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>
(0 ≤ <i>x</i><sub>1</sub> , <i>y</i><sub>1</sub> , <i>x</i><sub>2</sub> , <i>y</i><sub>2</sub> ≤ 100) specifying the two endpoints of the gate. You may assume that no couple of
gates touch or intersect each other.
</p>
<p>
The end of the input is indicated by a line containing a single zero.
</p>
<H2>Output</H2>
<p>
Print the shortest length out in one line for each course. You may print an arbitrary number of digits
after the decimal points provided that difference from the exact answer is not greater than 0.0001.
</p>
<H2>Sample Input</H2>
<pre>
4
0 4 2 4
0 2 2 0
3 0 4 2
6 2 6 0
0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
8.0000
</pre>
|
p01498 |
<H1><font color="#000">Problem D: </font>King Slime</H1>
<p>
There is a grid of size <i>W</i> × <i>H</i> surrounded by walls. Some cells in the grid are occupied by slimes. The slimes want to unite with each other and become a "King Slime".
</p>
<p>
In each move, a slime can move to east, west, south and north direction until it enters a cell occupied by another slime or hit the surrounding wall. If two slimes come together, they unite and become a new slime.
</p>
<p>
Your task is write a program which calculates the minimum number of moves that all the slimes unite and become a King Slime. Suppose slimes move one by one and they never move simultaneously.
</p>
<H2>Input</H2>
<p>
The first line contains three integers <i>N</i> (2 ≤ <i>N</i> ≤ 40,000), <i>W</i> and <i>H</i> (1 ≤ <i>W</i>, <i>H</i> ≤ 100,000), which denote the
number of slimes, the width and the height of the grid respectively.
</p>
<p>
The following <i>N</i> lines describe the initial coordinates of the slimes. The <i>i</i>-th line contains two integers <i>x<sub>i</sub></i> (1 ≤ <i>x<sub>i</sub></I> ≤ <i>W</i>) and <i>y<sub>i</sub></i> (1 ≤ <i>y<sub>i</sub></i> ≤ <i>H</i>), which indicate the coordinates of the <i>i</i>-th slime . All the coordinates are 1-based.
</p>
<p>
You may assume that each cell is occupied by at most one slime initially.
</p>
<H2>Output</H2>
<p>
Output the minimum number of moves that all the slimes unite and become a King Slime.
</p>
<H2>Sample Input 1</H2>
<pre>
4 3 3
1 1
1 3
3 1
3 3
</pre>
<H2>Output for the Sample Input 1</H2>
<pre>
3
</pre>
<br/>
<H2>Sample Input 2</H2>
<pre>
2 3 3
2 2
3 3
</pre>
<H2>Output for the Sample Input 2</H2>
<pre>
2
</pre>
<br/>
<H2>Sample Input 3</H2>
<pre>
2 4 4
2 2
3 3
</pre>
<H2>Output for the Sample Input 3</H2>
<pre>
3
</pre>
<br/>
<H2>Sample Input 4</H2>
<pre>
2 4 4
2 2
2 3
</pre>
<H2>Output for the Sample Input 4</H2>
<pre>
1
</pre>
<br/> |
p01532 |
<h1> Problem B War II</h1>
<h2> 第二次ProblemB大戦</h2>
<p>
R大学の2D好きの人たち (2DRespecters)は,A津大学で開催されるプログラミングの練習合宿に参加する.
この練習合宿では,参加者たちが自作のプログラミング問題を持ち寄り,練習に用いる.
今年は,2DRespectersの面々は手軽に作問できるB問題の作問担当者に立候補したため,B問題担当者の座を争うことになった.
</p>
<p>
戦いの舞台は,とある自販機である.
この自販機は,90円のジュースを1種類だけ販売しており,お手頃価格で購入できることで有名である.
ただし,自販機には,10円玉と100円玉の2つの硬貨しか投入することができず,釣銭もこれらの硬貨でしか支払われない.
</p>
<p>
自販機の内部には,10円玉を保存するための10円玉ストレージと,100円玉を保存するための100円玉ストレージが備え付けられており,それぞれには,始めに<i>T</i>枚の10円玉と<i>H</i>枚の100円玉が保存されている.
自販機には,硬貨を一度に一枚しか投入できず,投入された硬貨は,硬貨に対応する内部のストレージに保存される.
自販機の残金表示は,始め0円になっており,硬貨を投入すると,投入した硬貨の金額分,自販機の残金が増加する.
硬貨を投入した結果,自販機の残金が,90円以上になったならば,1本の缶ジュースがジュースの取り出し口に出てくると同時に,自販機の残金から90円を引いた額とちょうど同じ額の硬貨が釣銭として釣銭の取り出し口に出てくる.
このときの釣銭は,内部のストレージにある硬貨で払える範囲で,できるだけ多くの10円玉を用いて支払われる.
釣銭が支払われた後,自販機の残金は0円に戻る.
</p>
<p>
ただし,この自販機は次の操作を行うと壊れる.
</p>
<ul>
<li> 釣銭を払えなくなったとき</li>
<li> 10円玉ストレージの保存可能枚数の上限である<i>L</i>枚を超える10円玉が保存されようとしたとき</li>
</ul>
<p>
自販機の紹介が終わったので,<i>N</i>人の2DRespectersのメンバーが参加したゲームについて説明する.
ゲームの開始時には,2DRespectersのメンバーiはそれぞれ10円玉<i>t<sub>i</sub></i>枚と100円玉を<i>h<sub>i</sub></i>枚ずつ持っている.
このゲームでは,<i>N</i>人全員が順番に1枚ずつ硬貨を自販機に投入していく.
このとき,メンバー1,メンバー2,....,メンバー<i>N</i>の順で投入する.
<i>N</i>人全員が1枚ずつ硬貨の投入を終えたら,前回と同じ順番で順番に1枚ずつ硬貨の投入を行い,3週目以降も同様である.
硬貨の投入は,10円玉を持っている場合は必ず10円玉を投入し,10円玉を持っていない場合は100円玉を投入するものとする.
メンバー<i>i</i>が硬貨を投入したとき,釣銭の取り出し口に釣銭が出てきた場合は,その全ての釣銭をメンバー<i>i</i>が受け取り,取り出し口に何もない状態にしてから,次のメンバーのターンに移る.
</p>
<p>
このゲームは,次の条件を最初に満たしたメンバーが勝者となり,B問題の作問担当者となる.
</p>
<ul>
<li> 自販機を壊したとき</li>
<li> 自分の順番に,硬貨を1枚も持っていないとき</li>
</ul>
<p>
なお,自販機には無限にジュースが保存されており,100円玉ストレージには無限に100円玉を保存することができる.
</p>
<p>
あなたの仕事は,B問題担当者が誰になったかを求めるものである.
</p>
<p>
なけなしの硬貨を犠牲に,B問題を賭けた壮絶な戦いが??今,始まる.
ちなみに,購入したジュースはちゃんとB問題担当者が後でおいしくいただきます.
</p>
<h2> Input</h2>
<p>
入力は,次の形式で与えられる.
</p>
<pre>
<i>N</i> <i>T</i> <i>H</i> <i>L</i>
<i>t<sub>1</sub></i> <i>h<sub>1</sub></i>
<i>t<sub>2</sub></i> <i>h<sub>2</sub></i>
...
<i>t<sub>N</sub></i> <i>h<sub>N</sub></i>
</pre>
<p>
<i>N</i>は2DRespectersの人数であり,1 <= <i>N</i> <= 100である.
<i>T</i>と<i>H</i>はそれぞれ,10円玉ストレージと100円玉ストレージに最初に保存されている硬貨の枚数であり,0 <= <i>T</i>, <i>H</i> <= 100である.
<i>L</i>は10円玉ストレージの保存可能枚数の上限であり,<i>T</i> <= <i>L</i> <= 100である.
1 <= <i>i</i> <= <i>N</i>のとき,<i>t<sub>i</sub></i>は,2DRespectersのメンバー<i>i</i>の10円玉の所持枚数を表し,<i>h<sub>i</sub></i>は100円玉の所持枚数を表す.0 <= <i>t<sub>i</sub></i>, <i>h<sub>i</sub></i> <= 100である.
</p>
<h2> Output</h2>
<p>
B問題担当者になったメンバーの番号を1行に出力せよ.
</p>
<h2> Sample Input 1</h2>
<pre>
3 0 0 100
4 0
3 0
3 0
</pre>
<h2> Sample Output 1</h2>
<pre>
2
</pre>
<h2> Sample Input 2</h2>
<pre>
3 0 0 8
4 0
3 0
3 0
</pre>
<h2> Sample Output 2</h2>
<pre>
3
</pre>
<h2> Sample Input 3</h2>
<pre>
3 0 0 100
2 1
3 0
3 0
</pre>
<h2> Sample Output 3</h2>
<pre>
1
</pre> |
p01861 |
<!--<script language="JavaScript" type="text/javascript" src="js/varmath.js" charset="UTF-8"></script>-->
<h2>D: みゃんぱすーれつ - Myampus Sequence -</h2>
<h3>物語</h3>
<p>
みゃんぱすー.
ウチ,田舎の分校に通う小学一年生なん.
</p>
<p>
あんなー,今日の授業はプログラミングだったん.
みんな苦戦してたけど,まっつんのにぃにぃ,凄い勢いでタイピングしてたん.
さすが中三なんなー.
</p>
<p>
にぃにぃ,プログラムが完成してどこか行ってしまったん.
そんで,画面見たら,色んな数列が出力されてたん.
だんだん出力と同じような数列を書きたくなったん,ウチ,出力テキストに同じような数列をいくつも書き加えてたん.
そしたら,にぃにぃが戻ってきて,すごい怒られたん.
ウチ,にぃにぃの声初めて聞いたから,びっくりしたん.
</p>
<p>
でな,にぃにぃに謝ってプログラムを見せてもらったん.
プログラムが出力してたん,「みゃんぱすーれつ」いうん.
数列がみゃんぱすーれつか調べて,にぃにぃの機嫌を直したいん.
でも,ウチ,プログラミング初めてなん,教えてほしいのん.
</p>
<h3>問題</h3>
<p><var>N</var> 個の整数からなる数列と <var>M</var> 個の関数からなるプログラムが与えられる.プログラムが開始されると <var>1</var> 番目の関数を呼び出し,この関数の処理が終了すると,プログラムは終了する.また, <var>i</var> (<var>1 ≤ i ≤ M</var>) 番目の関数は,次のどちらか一方の処理を行い,関数の処理を終了する.</p>
<ul>
<li>整数 <var>a_i</var> を出力する.</li>
<li><var>b_i</var> (<var>1 ≤ b_i ≤ M</var>) 番目の関数を呼び出し,呼び出した関数の処理が終了したら, <var>c_i</var> (<var>1 ≤ c_i ≤ M</var>) 番目の関数を呼び出し,呼び出した関数の処理の終了を待つ.</li>
</ul>
<p>どちらの処理を行うかは,処理の度にランダムに決定される.つまり,プログラムが開始されると1個の数列を出力して終了する.ここで,プログラムが出力する数列としてあり得るものを「みゃんぱすーれつ」と定義する.</p>
<p>与えられた数列がみゃんぱすーれつか判定せよ.</p>
<h3>入力形式</h3>
<p>入力は次の形式で与えられる.</p>
<pre>
<var>N</var>
<var>x_1</var> ... <var>x_N</var>
<var>M</var>
<var>a_1</var> <var>b_1</var> <var>c_1</var>
...
<var>a_M</var> <var>b_M</var> <var>c_M</var>
</pre>
<p>
1行目に,判定すべき数列の長さ <var>N</var> が与えられる.
2行目に,判定すべき整数列 <var>x_1</var>, <var>x_2</var>, ..., <var>x_N</var> が順に与えられる.
3行目に,関数の個数 <var>M</var> が与えられる.
<var>i + 3</var> (<var> 1 ≤ i ≤ M</var>) 行目に, <var>i</var> 番目の関数の情報を表す <var>a_i</var>, <var>b_i</var>, <var>c_i</var> が順に与えられる.
</p>
<p>入力は次の制約を満たす.</p>
<ul>
<li><var>1 ≤ N ≤ 100</var></li>
<li><var>i = 1, </var>...<var>, N</var> に対して, <var>x_i</var> は <var>0 ≤ x_i ≤ 9</var> を満たす整数</li>
<li><var>1 ≤ M ≤ 10</var></li>
<li><var>i = 1, </var>...<var>, M</var> に対して, <var>a_i</var> は <var>0 ≤ a_i ≤ 9</var> を満たす整数</li>
<li><var>i = 1, </var>...<var>, M</var> に対して, <var>b_i</var> は <var>1 ≤ b_i ≤ M</var> を満たす整数</li>
<li><var>i = 1, </var>...<var>, M</var> に対して, <var>c_i</var> は <var>1 ≤ c_i ≤ M</var> を満たす整数</li>
</ul>
<h3>出力形式</h3>
<p>与えれた数列がみゃんぱすーれつの場合には “Yes” と出力し,数列がみゃんぱすーれつではない場合には “No” と出力せよ.また,出力の最後に改行せよ.</p>
<h3>入力例1</h3>
<pre>
3
3 5 3
3
5 2 3
3 3 1
7 1 2
</pre>
<h3>出力例1</h3>
<pre>Yes</pre>
<p>次のようにプログラムが動作すると,与えられた数列を生成します.</p>
<ol>
<li>プログラムが関数 <var>1</var> を呼び出す.</li>
<li>関数 <var>1</var> が関数 <var>2</var> と関数 <var>3</var> を順に呼び出す.</li>
<li>関数 <var>2</var> が <var>3</var> を出力する.</li>
<li>関数 <var>3</var> が関数 <var>1</var> と関数 <var>2</var> を順に呼び出す.</li>
<li>関数 <var>1</var> が <var>5</var> を出力する.</li>
<li>関数 <var>2</var> が <var>3</var> を出力する.</li>
</ol>
<h3>入力例2</h3>
<pre>
10
9 9 9 9 9 9 9 9 9 9
4
6 2 3
7 3 1
8 1 2
9 2 1
</pre>
<h3>出力例2</h3>
<pre>No</pre>
<p>どのような処理を行っても,関数 <var>4</var> を呼び出すことがないので, <var>9</var> を出力することができません.そのため, <var>9</var> を含む数列を出力することはできません.</p>
<h3>入力例3</h3>
<pre>
2
2 4
4
1 2 3
2 1 1
3 4 1
4 1 1
</pre>
<h3>出力例3</h3>
<pre>No</pre>
<p>このプログラムは, <var>2, 4, 1</var> のように, <var>2, 4</var> を含む数列を生成することがありますが, <var>2, 4</var> 自体を生成することはありません.</p> |
p00773 |
<h3>Tax Rate Changed</h3>
<p>
VAT (value-added tax) is a tax imposed at a certain rate proportional to the sale price.
</p>
<p>
Our store uses the following rules to calculate the after-tax prices.
</p>
<ul>
<li>
When the VAT rate is <i>x</i>%,
for an item with the before-tax price of <i>p</i> yen,
its after-tax price of the item is <i>p</i> (100+<i>x</i>) / 100 yen, fractions rounded off.
</li>
<li>
The total after-tax price of multiple items paid at once is
the sum of after-tax prices of the items.
</li>
</ul>
<p>
The VAT rate is changed quite often.
Our accountant has become aware that
"different pairs of items that had the same total after-tax price
may have different total after-tax prices after VAT rate changes."
For example, when the VAT rate rises from 5% to 8%,
a pair of items that had the total after-tax prices of 105 yen before
can now have after-tax prices either of 107, 108, or 109 yen, as shown in the table below.
</p>
<center>
<table border="1" style="border: 1px #aaa solid;">
<tbody><tr><th style="border: 1px #aaa solid;">Before-tax prices of two items</th><th style="border: 1px #aaa solid;">After-tax price with 5% VAT</th><th style="border: 1px #aaa solid;">After-tax price with 8% VAT</th></tr>
<tr><td style="border: 1px #aaa solid;" align="center">20, 80</td><td style="border: 1px #aaa solid;"align="center">21 + 84 = 105</td><td style="border: 1px #aaa solid;" align="center">21 + 86 = 107</td></tr>
<tr><td style="border: 1px #aaa solid;" align="center">2, 99</td><td style="border: 1px #aaa solid;" align="center">2 + 103 = 105</td><td style="border: 1px #aaa solid;" align="center">2 + 106 = 108</td></tr>
<tr><td style="border: 1px #aaa solid;" align="center">13, 88</td><td style="border: 1px #aaa solid;" align="center">13 + 92 = 105</td><td style="border: 1px #aaa solid;" align="center">14 + 95 = 109</td></tr>
</tbody></table>
</center>
<br>
<p>
Our accountant is examining effects of VAT-rate changes on after-tax prices.
You are asked to write a program that calculates the possible maximum
total after-tax price of two items with the new VAT rate,
knowing their total after-tax price before the VAT rate change.
</p>
<h3>Input</h3>
<p>
The input consists of multiple datasets.
Each dataset is in one line,
which consists of three integers <i>x</i>, <i>y</i>, and <i>s</i> separated by a space.
<i>x</i> is the VAT rate in percent <i>before</i> the VAT-rate change,
<i>y</i> is the VAT rate in percent <i>after</i> the VAT-rate change,
and <i>s</i> is the sum of after-tax prices of two items <i>before</i> the VAT-rate change.
For these integers, 0 < <i>x</i> < 100, 0 < <i>y</i> < 100,
10 < <i>s</i> < 1000, and <i>x</i> ≠ <i>y</i> hold.
For before-tax prices of items, all possibilities of 1 yen through <i>s</i>-1 yen should be considered.
</p>
<p>
The end of the input is specified by three zeros separated by a space.
</p>
<h3>Output</h3>
<p>
For each dataset,
output in a line the possible maximum total after-tax price when the VAT rate is changed to <i>y</i>%.
</p>
<h3>Sample Input</h3>
<pre>5 8 105
8 5 105
1 2 24
99 98 24
12 13 26
1 22 23
1 13 201
13 16 112
2 24 50
1 82 61
1 84 125
1 99 999
99 1 999
98 99 999
1 99 11
99 1 12
0 0 0
</pre>
<h3>Output for the Sample Input</h3>
<pre>109
103
24
24
26
27
225
116
62
111
230
1972
508
1004
20
7
</pre>
<h3>Hints</h3>
<p>
In the following table,
an instance of a before-tax price pair that has the maximum after-tax price after the VAT-rate change
is given for each dataset of the sample input.
</p>
<table border="1" style="border: 1px #aaa solid;">
<tbody><tr><th style="border: 1px #aaa solid;">Dataset</th><th style="border: 1px #aaa solid;">Before-tax prices</th><th style="border: 1px #aaa solid;">After-tax price with <i>y</i>% VAT</th></tr>
<tr><td style="border: 1px #aaa solid;"align="center">5 8 105</td> <td style="border: 1px #aaa solid;"align="center"> 13, 88</td><td style="border: 1px #aaa solid;"align="center"> 14 + 95 = 109</td></tr>
<tr><td style="border: 1px #aaa solid;"align="center">8 5 105</td> <td style="border: 1px #aaa solid;"align="center"> 12, 87</td><td style="border: 1px #aaa solid;"align="center"> 12 + 91 = 103</td></tr>
<tr><td style="border: 1px #aaa solid;"align="center">1 2 24</td> <td style="border: 1px #aaa solid;"align="center"> 1, 23</td><td style="border: 1px #aaa solid;"align="center"> 1 + 23 = 24</td></tr>
<tr><td style="border: 1px #aaa solid;"align="center">99 98 24</td> <td style="border: 1px #aaa solid;"align="center"> 1, 12</td><td style="border: 1px #aaa solid;"align="center"> 1 + 23 = 24</td></tr>
<tr><td style="border: 1px #aaa solid;"align="center">12 13 26</td> <td style="border: 1px #aaa solid;"align="center"> 1, 23</td><td style="border: 1px #aaa solid;"align="center"> 1 + 25 = 26</td></tr>
<tr><td style="border: 1px #aaa solid;"align="center">1 22 23</td> <td style="border: 1px #aaa solid;"align="center"> 1, 22</td><td style="border: 1px #aaa solid;"align="center"> 1 + 26 = 27</td></tr>
<tr><td style="border: 1px #aaa solid;"align="center">1 13 201</td> <td style="border: 1px #aaa solid;"align="center"> 1,199</td><td style="border: 1px #aaa solid;"align="center"> 1 +224 = 225</td></tr>
<tr><td style="border: 1px #aaa solid;"align="center">13 16 112</td><td style="border: 1px #aaa solid;"align="center"> 25, 75</td><td style="border: 1px #aaa solid;"align="center"> 29 + 87 = 116</td></tr>
<tr><td style="border: 1px #aaa solid;"align="center">2 24 50</td> <td style="border: 1px #aaa solid;"align="center"> 25, 25</td><td style="border: 1px #aaa solid;"align="center"> 31 + 31 = 62</td></tr>
<tr><td style="border: 1px #aaa solid;"align="center">1 82 61</td> <td style="border: 1px #aaa solid;"align="center"> 11, 50</td><td style="border: 1px #aaa solid;"align="center"> 20 + 91 = 111</td></tr>
<tr><td style="border: 1px #aaa solid;"align="center">1 84 125</td> <td style="border: 1px #aaa solid;"align="center"> 50, 75</td><td style="border: 1px #aaa solid;"align="center"> 92 +138 = 230</td></tr>
<tr><td style="border: 1px #aaa solid;"align="center">1 99 999</td> <td style="border: 1px #aaa solid;"align="center"> 92,899</td><td style="border: 1px #aaa solid;"align="center">183+1789 =1972</td></tr>
<tr><td style="border: 1px #aaa solid;"align="center">99 1 999</td> <td style="border: 1px #aaa solid;"align="center"> 1,502</td><td style="border: 1px #aaa solid;"align="center"> 1 +507 = 508</td></tr>
<tr><td style="border: 1px #aaa solid;"align="center">98 99 999</td><td style="border: 1px #aaa solid;"align="center"> 5,500</td><td style="border: 1px #aaa solid;"align="center"> 9 +995 =1004</td></tr>
<tr><td style="border: 1px #aaa solid;"align="center">1 99 11</td> <td style="border: 1px #aaa solid;"align="center"> 1, 10</td><td style="border: 1px #aaa solid;"align="center"> 1 + 19 = 20</td></tr>
<tr><td style="border: 1px #aaa solid;"align="center">99 1 12</td> <td style="border: 1px #aaa solid;"align="center"> 1, 6</td><td style="border: 1px #aaa solid;"align="center"> 1 + 6 = 7</td></tr>
</tbody></table>
|
p00289 |
<h1>アカ・ベコ捕獲作戦</h1>
<p>
怪盗アカ・ベコは大胆にも、ツルガジョーから銀のシャチホコを盗み去った。警部であるあなたは、アカ・ベコが仲間のコボウ氏にシャチホコを渡すらしいという情報を手に入れた。ただ、コボウ氏はアカ・ベコのアジトまで出向けないので、アジトの外で渡すらしい。あなたはアカ・ベコを待ち伏せして捕まえることにした。この町は古い城下町なので、道が複雑に入り組んでいて、どの道も一方通行になっている。アカ・ベコがアジトから受け渡し場所までどの経路を通るのかはわからないが、人員不足のため、待ち伏せ場所は1つに設定しなければならない。
</p>
<p>
あなたは、アジトを含むいくつかの地点を選び、それらをつなぐ道を調べ、地図を作った。地図上の地点のどこかで受け渡しが行われるらしい。受け渡し場所が判明次第、以下のように待ち伏せ場所を選ぶ。
</p>
<ol>
<li> アジトは危険なのでアジト以外の地点。</li>
<li> アカ・ベコがアジトから受け渡し場所までどのような経路を通っても必ず通る地点。</li>
<li> 1, 2 の両方を満たす地点のうち、受け渡し場所にたどり着くまでに通過しなければならない地点の数がより少ない地点。ただし、コボウ氏に見つからないようにするために、1, 2 を満たす地点が他にない場合のみ、受け渡し場所を待ち伏せ場所にする。</li>
</ol>
<p>
アジトと待ち伏せ場所の候補からなる地点をつなぐ道の情報のあとに、質問として受け渡し場所が複数入力されたとき、それぞれの受け渡し場所について待ち伏せ場所を出力するプログラムを作成してください。
</p>
<h2>入力</h2>
<p>
入力は1つのデータセットからなる。入力データは以下の形式で与えられる。
</p>
<pre>
<var>N</var> <var>M</var>
<var>s</var><sub>1</sub> <var>t</var><sub>1</sub>
:
<var>s<sub>M</sub></var> <var>t<sub>M</sub></var>
<var>Q</var>
<var>r</var><sub>1</sub>
:
<var>r<sub>Q</sub></var>
</pre>
<p>
1行目は2つの整数からなる。<var>N</var> (3 ≤ <var>N</var> ≤ 100000) は地点の数、<var>M</var>(<var>N</var>-1 ≤ <var>M</var> ≤ 300000) は地図に書いたすべての道の数を表す。続く <var>M</var> 行に隣り合った地点の間を直接つなぐ道が与えられる。<var>s<sub>i</sub></var> と <var>t<sub>i</sub></var> (1 ≤ <var>s<sub>i</sub></var> ≠ <var>t<sub>i</sub></var> ≤ <var>N</var>) は <var>i</var> 番目の道のそれぞれ始点、終点となる地点の番号を表す。ただし、アジトの番号は1とし、アジトからはすべての地点へ到達できる。
</p>
<p>
続く1行に質問の数 <var>Q</var>(1 ≤ <var>Q</var> < <var>N</var> ) が与えられる。続く <var>Q</var> 行に質問が与えられる。<var>i</var> 番目の質問として、受け渡し場所の番号 <var>r<sub>i</sub></var> (2 ≤ <var>r<sub>i</sub></var> ≤ <var>N</var>) が与えられる。
</p>
<h2>出力</h2>
<p>
質問ごとに、待ち伏せ場所の番号を1行に出力する。
</p>
<h2>入出力例</h2>
<br>
<h2>入力例 1</h2>
<pre>
6 7
1 2
2 3
1 3
1 4
4 5
5 6
6 1
5
2
3
4
5
6
</pre>
<h2>出力例 1</h2>
<pre>
2
3
4
4
5
</pre>
<h2>入力例 2</h2>
<pre>
11 15
1 2
1 3
1 4
2 5
3 6
3 7
3 9
4 7
4 10
4 11
6 2
6 8
7 9
8 11
9 6
10
6
2
10
8
9
3
5
11
4
7
</pre>
<h2>出力例 2</h2>
<pre>
6
2
4
6
9
3
2
11
4
7
</pre> |
p00323 |
<script type="text/x-mathjax-config">
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});
</script>
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML">
</script>
<H1>貴金属リサイクル</H1>
<p>
会津特産の貴金属であるアイヅニウムをリサイクルするPCK社は、全国各地にネットワークを持ち、たくさんの回収車でアイヅニウムを集めてきます。この会社は、処理の効率化のために、塊の重さと個数の単位を規格で定めています。
</p>
<p>
塊の重さには「ボッコ」という単位を使います。<var>x</var> ボッコのアイヅニウムの重さは 2<sup><var>x</var></sup>グラムです。宝石で例えると、「カラット」のようなものです。また、塊の個数には「マルグ」という単位を使います。<var>y</var> マルグは 2<sup><var>y</var></sup> 個です。1箱に入っている品物の個数である「ダース」のようなものです。ただし、<var>x</var> と <var>y</var> は 0 以上の整数でなければいけません。
</p>
<p>
回収車 <var>i</var> は、 <var>a<sub>i</sub></var> ボッコの重さのアイヅニウムを <var>b<sub>i</sub></var> マルグずつ集めます。こうして集まったアイヅニウムを、炉の中に入れて溶かし、いくつかのアイヅニウムの塊を再生しますが、なるべくアイヅニウムの塊の数が少なくなるようにします。このとき、集めてきたアイヅニウムの重さの合計と、再生してできるアイヅニウムの重さの合計は変わりません。
</p>
<p>
回収車が集めたアイヅニウムの塊のボッコ単位の重さとマルグ単位の個数が与えられたとき、再生後のアイヅニウムの塊の数が最小になるような結果を求めるプログラムを作成せよ。
</p>
<h2>Input</h2>
<p>
入力は以下の形式で与えられる。
</p>
<pre>
<var>N</var>
<var>a<sub>1</sub></var> <var>b<sub>1</sub></var>
<var>a<sub>2</sub></var> <var>b<sub>2</sub></var>
:
<var>a<sub>N</sub></var> <var>b<sub>N</sub></var>
</pre>
<p>
1行目に、回収車の数 <var>N</var> (1 ≤ <var>N</var> ≤ 100000) が与えられる。続く <var>N</var> 行に、回収車 <var>i</var> が回収したアイヅニウムの塊の、「ボッコ」単位の重さを表す整数 <var>a<sub>i</sub></var> (0 ≤ <var>a<sub>i</sub></var> ≤ 100000) と「マルグ」単位の個数を表す整数 <var>b<sub>i</sub></var> (0 ≤ <var>b<sub>i</sub></var> ≤ 100000) が与えられる。
</p>
<h2>Output</h2>
<p>
再生した後に得られるアイヅニウムの塊の数が最小になるような、ボッコ単位の重さとマルグ単位の個数を、重さの小さい順に出力する。
</p>
<h2>Sample Input 1</h2>
<pre>
3
2 1
1 3
2 2
</pre>
<h2>Sample Output 1</h2>
<pre>
3 0
5 0
</pre>
<br/>
<h2>Sample Input 2</h2>
<pre>
1
100000 2
</pre>
<h2>Sample Output 2</h2>
<pre>
100002 0
</pre> |
p03919 | <span class="lang-en">
<p>Score : <var>100</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.</p>
<p>The square at the <var>i</var>-th row and <var>j</var>-th column contains a string <var>S_{i,j}</var> of length <var>5</var>.</p>
<p>The rows are labeled with the numbers from <var>1</var> through <var>H</var>, and the columns are labeled with the uppercase English letters from <code>A</code> through the <var>W</var>-th letter of the alphabet.</p>
<p><img alt="" src="https://atcoder.jp/img/code-festival-2016-final/90cfa9fb5e04213219ccbc8d08286b4d.png"/></p>
<p>Exactly one of the squares in the grid contains the string <code>snuke</code>. Find this square and report its location.</p>
<p>For example, the square at the <var>6</var>-th row and <var>8</var>-th column should be reported as <code>H6</code>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1≦H, W≦26</var></li>
<li>The length of <var>S_{i,j}</var> is <var>5</var>.</li>
<li><var>S_{i,j}</var> consists of lowercase English letters (<code>a</code>-<code>z</code>).</li>
<li>Exactly one of the given strings is equal to <code>snuke</code>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>H</var> <var>W</var>
<var>S_{1,1}</var> <var>S_{1,2}</var> <var>...</var> <var>S_{1,W}</var>
<var>S_{2,1}</var> <var>S_{2,2}</var> <var>...</var> <var>S_{2,W}</var>
<var>:</var>
<var>S_{H,1}</var> <var>S_{H,2}</var> <var>...</var> <var>S_{H,W}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the labels of the row and the column of the square containing the string <code>snuke</code>, with no space inbetween.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>15 10
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snuke snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>H6
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>1 1
snuke
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>A1
</pre></section>
</div>
</span> |
p01924 |
<script type="text/x-mathjax-config">
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});
</script>
<script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>
<h3>海岸線</h3>
<p>海岸には毎秒波が押し寄せる.基準点 <i>P</i> を超えて何 m の地点まで波が押し寄せたかを,<i>T</i> 秒の間だけ毎秒観測し記録したデータがある.データは <i>T</i> 個の整数 <i>x<sub>1</sub></i>, <i>...</i>, <i>x<sub>T</sub></i> からなり,各 <i>i (1 ≤ i ≤ T)</i> に対し,観測を始めてから <i>i</i> 秒後には地点 <i>P</i> からちょうど <i>x<sub>i</sub></i> m の地点までの波が押し寄せ,海水に浸っていたことを表す.
</p>
<p>海岸は,最後に海水に浸ってから <i>D</i> 秒後に乾くことが分かっている.ここで,乾くまでの時間は最後に海水に浸った時間のみに依存し,それ以前に波に浸っていた回数や時間には依存しないことに注意せよ.
</p>
<p>基準点 <i>P</i> から,陸の方向に距離 <i>L</i> だけ離れた地点が,観測を開始してから 1 秒後と <i>T</i> 秒後の間に少なくとも何秒間濡れていたかを求めよ.ただし,時刻 0 で海岸は乾いていたことがわかっている.
</p>
<p>以下に,Sample Input 第1ケースの図を示す.
</p>
<div style="text-align:center">
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_JAGDomestic2017_fig_b_0" witdh="300" height="300">
<p>図B1: Sample Input 第1ケースの場合
</p>
</div>
<h3>Input</h3>
<blockquote></blockquote>
<p>入力データセットは複数のケースから構成される.データセットの個数は最大でも 40 個を超えない.各ケースは次のような形式である.
</p><blockquote><i>T</i> <i>D</i> <i>L</i><br><i>x<sub>1</sub></i><br>...<br><i>x<sub>T</sub></i><br></blockquote>
<p>1 行目には <i>T, D, L (1 ≤ T, D, L ≤ 100,</sub>000)</i> が半角スペース区切りで与えられる.続く <i>T</i> 行のうち <i>i (1 ≤ i ≤ T)</i> 行目には <i>x<sub>i</sub> (0 ≤ x<sub>i</sub> ≤ 100,</sub>000)</i> が与えられる.これらは全て整数である.
</p><blockquote></blockquote>
<p>データセットの終わりは,3 つの 0 からなる行で表される.
</p><blockquote></blockquote>
<h3>Output</h3>
<blockquote></blockquote>
<p>各ケースに対し,基準点 <i>P</i> から,陸の方向に距離 <i>L</i> だけ離れた地点が 1 秒後から <i>T</i> 秒後の間で確実に濡れていた時間 (秒) を 1 行で出力せよ.
</p><blockquote></blockquote>
<h3>Sample Input</h3>
<pre>5 2 3
3
5
1
2
3
3 100 100
3
3
4
20 3 8
3
2
6
1
9
1
8
4
2
2
8
1
8
8
2
5
3
4
3
8
7 2 2
0
2
5
2
5
2
1
0 0 0
</pre>
<h3>Output for Sample Input</h3>
<pre>3
0
11
5</pre>
|
p00636 |
<H1><font color="#000000">Problem 10:</font> The Last Dungeon</H1>
<p>
勇者ポン太はいよいよ最終ダンジョンの目前までやってきた。ここは、悪の帝王ボロモスの砦の前に広がる闇の荒野であり、かなり強者のモンスターが、それぞれの縄張りを守っている。
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_lastDangion1"><br>
<p>図1:荒野</p>
</center>
<p>
荒野は図1に示すように、南西を原点(0, 0)とした 4 × 4 の正方形の領域で表されている。この領域の中に、図の点で示されているように、モンスターが構えている。勇者ポン太はこの領域を西から東へ(WからEへ)横断しなければならない。つまり、x 座標が 0.0 であり y 座標が 0.0 以上 4.0 以下である点から出発し、x 座標が 4.0 であり y 座標が 0.0 以上 4.0 以下である点へ移動しなければならない。
</p>
<p>
モンスターは、敵が領域に進入すると、自分と敵との距離が他のどのモンスターと敵との距離よりも短い場合、敵に襲いかかってくる。
</p>
<p>
十分なアイテムもない今、ボロモスと戦う前になるべく戦いは避けたいのが本音のポン太だった。そこで、ポン太はあることに気づいた:自分との距離が最も短くなるモンスターが常に2匹以上存在する道を進めばモンスターに襲われることはない。
</p>
<p>
勇者ポン太が無傷でボロモスのもとに辿りつけるかは、あなたのコントロール操作にかかっている。
闇の荒野を抜ける最短距離を求めるプログラムを作成せよ。
</p>
<p>
参考として、図1における最短経路を図2に示す。
</p>
<center>
<img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_lastDangion3"><br>
<p>図2:最短経路</p>
</center>
<H2>Input</H2>
<p>
入力として複数のデータセットが与えられる。各データセットは以下の形式で与えられる:<br><br>
<i>n</i> (モンスターの数:整数)<br>
<i>x</i><sub>1</sub> <i>y</i><sub>1</sub> (1匹目のモンスターの位置:空白区切りの実数)<br>
<i>x</i><sub>2</sub> <i>y</i><sub>2</sub> (2匹目のモンスターの位置:空白区切りの実数)<br>
.<br>
.<br>
<i>x</i><sub><i>n</i></sub> <i>y</i><sub><i>n</i></sub> (<i>n</i> 匹目のモンスターの位置:空白区切りの実数)<br>
</p>
<p>
<i>n</i> は 1 以上 20 以下である。また、モンスターの位置を示す <i>x</i>, <i>y</i> 座標値は 0.0 以上 4.0 以下である。
</p>
<p>
<i>n</i> が 0 のとき入力の終わりを示す。
</p>
<H2>Output</H2>
<p>
各データセットについて、最短距離を1行に出力せよ。モンスターに襲われることなく荒野を横断することができない場合は "<span>impossible</span>" と出力せよ。
</p>
<p>
距離の出力は 0.00001 以下の誤差があっても良いものとする。
</p>
<H2>Sample Input</H2>
<pre>
2
1 1
3 3
4
1.0 0.5
1.0 2.5
3.0 1.5
3.0 3.5
1
2.0 2.0
0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
5.656854249492
4.618033988750
impossible
</pre>
|
p00266 |
<H1>イヅア国語辞典</H1>
<p>
あなたはイヅア国の公用語であるイヅア語の国語辞典と、イヅア語のアルファベット(文字の一覧表)
を手に入れました。イヅア語のアルファベットにはN 種類の文字があります。イヅア語の国語辞典に現れる単語の順番は、イヅア語のアルファベット順に並んでいます。
</p>
<p>
辞典を見ると、載っているどの単語もN 文字で、しかも、N 種類の文字をひとつずつ含んでいることがわかりました。さらに調べてみると、辞典にはN 種類の文字の可能な並び方がすべて書かれていることを発見しました。
</p>
<p>
この発見から、あなたはある単語が辞典の何番目に出てくるかわかるようになりました。この知識を利用してイヅア国の人を驚かせてみましょう。まず、N 種類の文字をひとつずつアルファベット順に並べます。次に、任意の2文字の順番を入れ替える操作を R 回繰り返してもらいます。あなたは、出来上がった単語がイヅア国語辞典の何番目に出てくるか当てて見せます。そのための準備として、国語辞典中の単語の場所を求めるプログラムを作成してください。ただし、アルファベット順で最初の単語を 0 番目の単語とします。
</p>
<h2>入力</h2>
<p>
入力は複数のデータセットからなる。入力の終わりはゼロ1つの行で示される。各データセットは以下の形式で与えられる。
</p>
<pre>
N
R
s<sub>1</sub> t<sub>1</sub>
s<sub>2</sub> t<sub>2</sub>
:
s<sub>R</sub> t<sub>R</sub>
</pre>
<p>
1行目にアルファベットを構成する文字の数 N (1 ≤ N ≤ 100000) が与えられ、2行目に文字を入れ替えてもらう回数 R (0 ≤ R ≤ 50) が与えられる。続く R 行に、入れ替えられる文字の位置の組が与えられる。s<sub>i</sub> と t<sub>i</sub> (1 ≤ s<sub>i</sub> < t<sub>i</sub> ≤ N) は、先頭から数えて s<sub>i</sub> 番目と t<sub>i</sub> 番目の文字を i 回目に入れ替えることを表す。s<sub>i</sub> と t<sub>i</sub> は1つの空白で区切られている。
</p>
<p>
データセットの数は100 を超えない。
</p>
<h2>出力</h2>
<p>
データセットごとに、入れ替えが終わった時点で得られた単語が国語辞典の何番目に現れるかを示す数を1行に出力する。ただし、出力すべき値は非常に大きくなりうるので、代わりに 1,000,000,007 で割った余りを出力する。
</p>
<h2>入力例</h2>
<pre>
3
2
1 2
2 3
4
2
2 3
2 4
0
</pre>
<h2>出力例</h2>
<pre>
3
4
</pre>
<p>
入出力例の最初のデータセットについて説明する。説明のため、アルファベットは小さい方から順番に、'A', 'B', 'C' の3文字からなるとする。これらの文字をアルファベット順に並べると "ABC" という文字列が得られる。この文字列の1番目の文字 'A' と2番目の文字 'B' を入れ替えると、"BAC" という文字列が得られる。次に、得られた文字列の2番目の文字 'A' と3番目の文字 'C' を入れ替えると、単語 "BCA" が得られる。辞典には、先頭から順に "ABC", "ACB", "BAC", "BCA", "CAB", "CBA" の6種類の単語が載っている。"BCA" はこの中で3番目(最初の"ABC"が0番目であることに注意)に現れるので、3 を 1,000,000,007 で割った余りである 3 が答えになる。
</P> |
p01027 | <h1>Problem L: WW</h1>
<h2>Problem</h2>
<p>
19XX年、nつの国で連合国軍が結成された。連合国軍に属する各国では、敵軍の侵略から自国を守るために国内に1つ以上の拠点を作り、周囲を警戒している。図1は連合国の例を示し、長方形が各国を、その中にある円がその国の拠点を表す。
</p>
<br>
<center>
<img width="680" src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE3_RitsCamp14Day2_WW1.png" alt="図1"><br>
図1
</center>
<br>
<p>
敵軍が侵略してきた場合、それを一番最初に発見した拠点からその情報を連合国軍に属する他国または自国の拠点に発信することになっている。
情報を他の拠点に伝えるには正の整数のコストがかかる。
</p>
<p>
また、各国では自国の拠点のうち、連合国軍翻訳通信部(ATIS : Allied Translator and Interpreter Section)に属する兵士が配属されている拠点をちょうど1つだけ持っている。
この兵士だけが連合国軍に属する全ての国の言語を話すことができ、その他の兵士は自分の国の言語しか話せない。
その為、情報を受け取った拠点が他国に情報を伝えようとした際にその拠点がATISに属する兵士をもたない場合、そこから自国のATISに属する兵士をもつ拠点に情報を伝え他国に情報を発信してもらう必要がある。
</p>
<p>
国毎で各拠点に0から順に[その国がもつ拠点の数-1]までの番号が割り振られており、0番目の拠点にATISに属する兵士がいる。
つまり、他国の拠点に情報を伝達できるのは0番目の拠点だけであり、その他の拠点から他国へは通信できない。
また、その国の0番目の拠点に情報が伝わればその国全体に情報が伝わったものとする。
</p>
<p>
様々な理由から、全ての拠点の間で通信ができるとは限らない。
ある拠点から別の拠点に情報を伝える事ができたとしても、その逆が成り立つとは限らない。
また、ある拠点から別の拠点に情報を伝えた際のコストとその逆でかかるコストが同じとも限らない。
</p>
<p>
これらのことを踏まえて、ある拠点が敵軍を発見した際に、連合国軍全体に情報が行き渡るコストの総和が最も少ない伝達方法を知りたい。
連合国軍に属する全ての国の0番目の拠点に対して最初に敵軍を見つけた拠点から情報が伝達された時、連合国軍全体に情報が行き渡ったものとする。
</p>
<p>
敵軍を発見した拠点が質問として与えられるので、最も少ないコストでその情報が連合国軍全体に行き渡る伝達方法とそのコストを出力せよ。
</p>
<p>
例えば、以下のケースについて考える ( Sample Input 4、1つめの質問 )
</p>
<p>
国neepalの1番目の拠点が敵軍を発見したとする。
この場合、1番目の拠点から他国へは通信できないため、まず自国の0番目の拠点へ情報を伝える。
</p>
<p>
次に、0番目の拠点から国luxenbourg,nowayに情報を伝える。
neepalからluxenbourgの0番目の拠点へ直接通信することはできるが、3番目の拠点に情報を伝え、そこから0番目の拠点へ伝えたほうがコストが少なく済む。
この場合、全ての国の0番目の国へ情報を伝えるためにかかる最小のコストは12となる。
コストが12で済む伝達方法は複数存在するが、そのうちのどれか一つを答えとして出力すれば良い。
</p>
<p>
以下の図2の赤い矢印はその答えのうちの1つである。
</p>
<br>
<center>
<img width="680" src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE3_RitsCamp14Day2_WW2.png"><br>
図2
</center>
<h2>Input</h2>
<pre>
<var>n</var>
<var>countryname<sub>0</sub></var> <var>m<sub>0</sub></var>
<var>countryname<sub>1</sub></var> <var>m<sub>1</sub></var>
…
<var>countryname<sub>n-1</sub></var> <var>m<sub>n-1</sub></var>
<var>e</var>
<var>c1<sub>0</sub></var> <var>v1<sub>0</sub></var> <var>c2<sub>0</sub></var> <var>v2<sub>0</sub></var> <var>cost<sub>0</sub></var>
<var>c1<sub>1</sub></var> <var>v1<sub>1</sub></var> <var>c2<sub>1</sub></var> <var>v2<sub>1</sub></var> <var>cost<sub>1</sub></var>
…
<var>c1<sub>e-1</sub></var> <var>v1<sub>e-1</sub></var> <var>c2<sub>e-1</sub></var> <var>v2<sub>e-1</sub></var> <var>cost<sub>e-1</sub></var>
<var>q</var>
<var>c3<sub>0</sub></var> <var>v3<sub>0</sub></var>
<var>c3<sub>1</sub></var> <var>v3<sub>1</sub></var>
…
<var>c3<sub>q-1</sub></var> <var>v3<sub>q-1</sub></var>
</pre>
<ul>
<li><var>n</var>は連合国軍に属する国の数を表す</li>
<li><var>countryname<sub>i</sub></var>は連合国軍に属する<var>i</var>番目の国名を,<var>m<sub>i</sub></var>は<var>i</var>番目の国の拠点の数を表す</li>
<li><var>e</var>は通信可能な拠点のペアの数を表す</li>
<li>続く<var>e</var>行には国<var>c1<sub>i</sub></var>の拠点<var>v1<sub>i</sub></var>から国<var>c2<sub>i</sub></var>の拠点<var>v2<sub>i</sub></var>に情報を伝えるのにかかるコスト<var>cost<sub>i</sub></var>が与えられる</li>
<li><var>q</var>は質問の数を表す</li>
<li>続く<var>q</var>行には敵軍を発見した国の名前<var>c3<sub>i</sub></var>とその拠点<var>v3<sub>i</sub></var>が与えられる</li>
<li><var>c1<sub>i</sub></var>,<var>c2<sub>i</sub></var>,<var>c3<sub>i</sub></var>は連合国軍に属する国のいずれかである</li>
<li><var>v1<sub>i</sub></var>,<var>v2<sub>i</sub></var>,<var>v3<sub>i</sub></var>はその国の拠点の数以上である事は無い</li>
</ul>
<h2>Constraints</h2>
<ul>
<li> 1 ≤ <var>n</var> ≤ 26 </li>
<li> 1 ≤ <var>m<sub>i</sub></var> ≤ 100</li>
<li> 0 ≤ <var>e</var> ≤ 322400</li>
<li> 0 ≤ <var>cost<sub>i</sub></var> ≤ 100</li>
<li> 1 ≤ <var>q</var> ≤ 10</li>
<li> <var>countryname</var> は小文字のアルファベット ‘a’ から’z’までで構成される長さ1以上20以下の文字列である</li>
<li> 同じ名前の国が2つ以上与えられる事はない</li>
<li> 通信可能な拠点のペアの<var>c1<sub>i</sub></var>と<var>c2<sub>i</sub></var>が同じ時、<var>v1<sub>i</sub></var>と<var>v2<sub>i</sub></var>は異なる</li>
<li> 通信可能な拠点のペアの<var>c1<sub>i</sub></var>と<var>c2<sub>i</sub></var>が異なる時、<var>v1<sub>i</sub></var>は必ず0である</li>
</ul>
<h2>Output</h2>
<p>
各質問に対して以下の形式で答えを出力する。
</p>
<p>
各質問に対する答えの最後の行には、5つの '-’ からなる"-----” ( “ は除く ) という1行を出力すること。
</p>
<p>
<b>答えが存在する場合</b>
</p>
<p>1行目に質問で与えられた国の拠点から連合国軍全体に情報が行き渡るために必要なコストの最小値を出力する。</p>
<p>2行目以降にはその時の伝達方法を出力する。
伝達方法は以下の形式で表現される。
</p>
<pre>
<var>c4<sub>0</sub></var> <var>v4<sub>0</sub></var> <var>c5<sub>0</sub></var> <var>v5<sub>0</sub></var>
<var>c4<sub>1</sub></var> <var>v4<sub>1</sub></var> <var>c5<sub>1</sub></var> <var>v5<sub>1</sub></var>
...
</pre>
<p>国<var>c4<sub>i</sub></var>の拠点<var>v4<sub>i</sub></var>から国<var>c5<sub>i</sub></var>の拠点<var>v5<sub>i</sub></var>に情報を伝えたことを表す。</p>
<p>以下の事に気をつける事。</p>
<ul>
<li>通信が行われた順番通りに出力する必要はない</li>
<li>コストの総和が最小となる伝達方法が1つ以上存在する場合、そのうちのいずれか1つを出力すれば良い</li>
<li>同じ通信可能なペアを2回以上出力してはいけない</li>
<li><var>c4<sub>i</sub></var>,<var>c5<sub>i</sub></var>は入力で与えられた国のいずれかでなければならない</li>
<li><var>v4<sub>i</sub></var>,<var>v5<sub>i</sub></var>はその国の拠点の数未満でなければならない</li>
</ul>
<br>
<p>
<b>答えが存在しない場合</b>
</p>
<p>
1行に ”Impossible” ( “ は除く ) と出力すること。
</p>
<h2>Sample Input 1</h2>
<pre>
3
frence 1
usi 1
powland 1
2
usi 0 frence 0 10
frence 0 powland 0 10
2
usi 0
powland 0
</pre>
<h2>Sample Output 1</h2>
<pre>
20
usi 0 frence 0
frence 0 powland 0
-----
Impossible
-----
</pre>
<h2>Sample Input 2</h2>
<pre>
2
usso 2
caneda 2
4
usso 1 usso 0 1
usso 0 caneda 0 10
usso 0 caneda 1 2
caneda 1 caneda 0 2
1
usso 1
</pre>
<h2>Sample Output 2</h2>
<pre>
5
usso 1 usso 0
usso 0 caneda 1
caneda 1 caneda 0
-----
</pre>
<h2>Sample Input 3</h2>
<pre>
3
chinax 1
ok 1
austraria 2
6
chinax 0 austraria 0 5
austraria 0 chinax 0 5
austraria 1 austraria 0 1
ok 0 austraria 0 5
austraria 0 ok 0 5
ok 0 austraria 1 1
2
chinax 0
ok 0
</pre>
<h2>Sample Output 3</h2>
<pre>
10
chinax 0 austraria 0
austraria 0 ok 0
-----
7
ok 0 austraria 1
austraria 1 austraria 0
austraria 0 chinax 0
-----
</pre>
<h2>Sample Input 4</h2>
<pre>
3
neepal 2
luxenbourg 4
noway 2
12
neepal 1 neepal 0 2
neepal 0 noway 1 2
neepal 0 luxenbourg 0 10
neepal 0 luxenbourg 3 2
luxenbourg 3 luxenbourg 1 2
luxenbourg 3 luxenbourg 2 2
luxenbourg 1 luxenbourg 0 2
luxenbourg 2 luxenbourg 0 2
noway 1 noway 0 2
noway 0 neepal 0 2
noway 0 luxenbourg 3 2
noway 0 luxenbourg 0 10
3
neepal 1
luxenbourg 3
noway 1
</pre>
<h2>Sample Output 4</h2>
<pre>
12
neepal 1 neepal 0
neepal 0 luxenbourg 3
luxenbourg 3 luxenbourg 2
luxenbourg 2 luxenbourg 0
neepal 0 noway 1
noway 1 noway 0
-----
Impossible
-----
10
noway 1 noway 0
neepal 0 luxenbourg 3
luxenbourg 3 luxenbourg 2
luxenbourg 2 luxenbourg 0
noway 0 neepal 0
-----
</pre>
|
p01477 |
<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,1}∨X_{1,2})∧(X_{2,1}∨X_{2,2})∧...∧(X_{M,1}∨X_{M,2})$ で表される論理式が与えられる。ただし $X_{i,j} \in \{x_1, x_2, ..., x_N, ~x_1, ~x_2, ..., ~x_N\}$である。<br />
与えられた論理式が真となるように各変数 $x_i$ ($1 \leq i \leq N$)に真偽値を割り当てたい。そのような割り当て方が何通りあるかを求めよ。</p>
<h2>入力</h2>
<p>入力は以下の形式に従う。与えられる数は全て整数である。</p>
<pre>$N$ $M$
$Y_{1,1}$ $Y_{1,2}$
$Y_{2,1}$ $Y_{2,2}$
$...$
$Y_{M,1}$ $Y_{M,2}$</pre>
<p>$Y_{i,j}>0$ のとき、$X_{i,j} = x_{Y_{i,j}}$ であり、$Y_{i,j}<0$ のとき、$X_{i,j} = ~x_{-Y_{i,j}}$ である。</p>
<h2>制約</h2>
<ul><li>$1 \leq N \leq 1000$</li>
<li>$N/2 \leq M \leq N$</li>
<li>$1 \leq |Y_{i,j}| \leq N$</li>
<li>各変数は論理式に1回か2回だけ現れる。すなわち、任意の $k$ ($1 \leq k \leq N$)に対して $X_{i,j}=x_k$ または $X_{i,j}=~x_k$ となるような $(i,j)$ の組は1個か2個だけ存在する。</li></ul>
<h2>出力</h2>
<p>論理式を真にするような各変数の真偽値の割り当て方が何通りあるかを $10^9+7$ で割った余りを1行に出力せよ。</p>
<h2>Sample Input 1</h2>
<pre>2 1
1 -2 </pre>
<h2>Output for the Sample Input 1</h2>
<pre>3</pre>
<p>論理式 $(x1∨~x2)$ を真にする割り当ては $(x1,x2) = (True, True), (True, False), (False, False)$ の3通りである。</p>
<h2>Sample Input 2</h2>
<pre>3 2
-1 -2
1 -3</pre>
<h2>Output for the Sample Input 2</h2>
<pre>4</pre>
<p>論理式 $(~x1∨~x2)∧(x1∨~x3)$ を真にする割り当ては $(x1,x2,x3) = (True, False, True), (True, False, False), (False, True, False), (False, False, False)$ の4通りである。</p>
|
p02172 | <h1>Problem K: Gold Rush</h1>
<h2>Problem</h2>
<p>
moritaoy君とは、コンピュータのなかで暮らしている謎の大学生です。<br>
moritaoy君が暮らしている区画は、縦に $2^H$ 、横に $2^W$ 、の大きさのある二次元空間 $S= \{ (p,q) \in \mathbb{Z}^2 | 0 \leq p \lt 2^H, 0 \leq q \lt 2^W \} $ として表されます。<br>
moritaoy君はいくつかの非負整数を要素とする二次元ベクトルを持っていて、この空間上を足し算を用いて移動します。<br>
moritaoy君が $(a,b)$ にいて、ベクトル $(i,j)$ によって移動するとは、具体的には以下のような行動を示します。
</p>
<ul>
<li>$(a+i,b+j)$ に移動する。ただし、そのような点が $S$ に存在しない場合は、オーバーフローを起こし、$a+i \equiv k \bmod 2^H , b+j \equiv l \bmod 2^W$ を満たすようなmoritaoy君の暮らす空間上の点 $(k,l) \in S$ に移動する。</li>
</ul>
<p>
以下がmoritaoy君の一日の予定です。
</p>
<ol>
<li>一日を開始する。このとき、moritaoy君は $(0,0)$ にいて、疲労度は $0$ である。2.を行う。</li>
<li>今日、moritaoy君が移動を行った回数が $K$ 以上なら4.を、そうでないなら3.か4.のどちらか一方を行う。</li>
<li>moritaoy君が持っているベクトルのなかから一つを選び、これを $v$ とする。$v$ によって移動する。moritaoy君の疲労度が $T_v$ 増加する。その後のmoritaoy君の疲労度を $T$ とし、 $T \times G_v$ 単位時間休憩する。2.を行う。</li>
<li>一日を終了する。</li>
</ol>
<p>
moritaoy君は、色々な予定を立てて遊ぶことにしました。<br>
各 $(a,b)$ に対して、一日を終了したときにmoritaoy君が $(a,b)$ にいるような予定全ての、一日に休憩する時間の総和を求めてください。<br>
ただし、二つの予定が異なるとは、二つの予定の一日に移動する回数が異なる、またはある $i$ があって $i$ 回目の移動に用いるベクトルが異なることをいいます。
</p>
<h2>Input</h2>
<p>入力は以下の形式で与えられる。</p>
<pre>
$H$ $W$ $K$
$T_{(0,0)}$ $\ldots$ $T_{(0,2^W-1)}$
$\vdots$
$T_{(2^H-1,0)}$ $\ldots$ $T_{(2^H-1,2^W-1)}$
$G_{(0,0)}$ $\ldots$ $G_{(0,2^W-1)}$
$\vdots$
$G_{(2^H-1,0)}$ $\ldots$ $G_{(2^H-1,2^W-1)}$
</pre>
<p>
$T_{(i,j)}$ が $-1$ でないとき、かつそのときに限り、moritaoy君はベクトル $(i,j)$ を持つ。<br>
$2^H \leq i$ または $2^W \leq j$ ならばmoritaoy君はベクトル $(i,j)$ を持たない。
</p>
<h2>Constraints</h2>
<p>入力は以下の条件を満たす。</p>
<ul>
<li>$0 \leq H,W \leq 9$</li>
<li>$1 \leq K \leq 10^{5}$</li>
<li>$-1 \leq T_{(i,j)} \lt 998244353$</li>
<li>$-1 \leq G_{(i,j)} \lt 998244353$</li>
<li>$T_{(i,j)}=-1$ のとき、かつそのときに限り、$G_{(i,j)}=-1$</li>
<li>入力は全て整数である</li>
</ul>
<h2>Output</h2>
<p>
出力は $2^H$ 行からなる。<br>
$i$ 行目には $2^W$ 個の要素を空白区切りで出力する。<br>
$i$ 行目の $j$ 番目の要素は、一日を終了したときにmoritaoy君が $(i-1,j-1)$ にいるような予定全ての、一日に休憩する時間の総和である。<br>
ただし、答えは非常に大きくなることがあるので、$998244353$ で割ったあまりを出力すること。
</p>
<h2>Sample Input 1</h2>
<pre>
0 0 10
1
2
</pre>
<h2>Sample Output 1</h2>
<pre>
440
</pre>
<h2>Sample Input 2</h2>
<pre>
1 2 2
1 2 3 4
5 6 7 0
9 8 7 6
1 3 3 3
</pre>
<h2>Sample Output 2</h2>
<pre>
355 358 363 386
391 408 419 378
</pre>
<h2>Sample Input 3</h2>
<pre>
1 1 100000
900000000 -1
-1 902010312
218738721 -1
-1 281371299
</pre>
<h2>Sample Output 3</h2>
<pre>
311157817 0
0 640524124
</pre>
|
p03299 | <span class="lang-en">
<p>Score : <var>1100</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Let us consider a grid of squares with <var>10^9</var> rows and <var>N</var> columns. Let <var>(i, j)</var> be the square at the <var>i</var>-th column <var>(1 \leq i \leq N)</var> from the left and <var>j</var>-th row <var>(1 \leq j \leq 10^9)</var> from the bottom.</p>
<p>Snuke has cut out some part of the grid so that, for each <var>i = 1, 2, ..., N</var>, the bottom-most <var>h_i</var> squares are remaining in the <var>i</var>-th column from the left.
Now, he will paint the remaining squares in red and blue.
Find the number of the ways to paint the squares so that the following condition is satisfied:</p>
<ul>
<li>Every remaining square is painted either red or blue.</li>
<li>For all <var>1 \leq i \leq N-1</var> and <var>1 \leq j \leq min(h_i, h_{i+1})-1</var>, there are exactly two squares painted red and two squares painted blue among the following four squares: <var>(i, j), (i, j+1), (i+1, j)</var> and <var>(i+1, j+1)</var>.</li>
</ul>
<p>Since the number of ways can be extremely large, print the count modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 100</var></li>
<li><var>1 \leq h_i \leq 10^9</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>h_1</var> <var>h_2</var> <var>...</var> <var>h_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of the ways to paint the squares, modulo <var>10^9+7</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>9
2 3 5 4 1 2 4 2 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>12800
</pre>
<p>One of the ways to paint the squares is shown below:</p>
<pre>
<span style="color:blue">#</span>
<span style="color:blue">#</span><span style="color:red">#</span> <span style="color:blue">#</span>
<span style="color:red">#</span><span style="color:blue">#</span><span style="color:red">#</span> <span style="color:blue">#</span>
<span style="color:blue">#</span><span style="color:red">#</span><span style="color:blue">#</span><span style="color:red">#</span> <span style="color:blue">#</span><span style="color:blue">#</span><span style="color:red">#</span>
<span style="color:red">#</span><span style="color:blue">#</span><span style="color:red">#</span><span style="color:blue">#</span><span style="color:red">#</span><span style="color:red">#</span><span style="color:red">#</span><span style="color:blue">#</span><span style="color:blue">#</span>
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2
2 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>6
</pre>
<p>There are six ways to paint the squares, as follows:</p>
<pre>
<span style="color:blue">#</span><span style="color:blue">#</span> <span style="color:blue">#</span><span style="color:red">#</span> <span style="color:red">#</span><span style="color:red">#</span> <span style="color:red">#</span><span style="color:blue">#</span> <span style="color:blue">#</span><span style="color:red">#</span> <span style="color:red">#</span><span style="color:blue">#</span>
<span style="color:red">#</span><span style="color:red">#</span> <span style="color:blue">#</span><span style="color:red">#</span> <span style="color:blue">#</span><span style="color:blue">#</span> <span style="color:red">#</span><span style="color:blue">#</span> <span style="color:red">#</span><span style="color:blue">#</span> <span style="color:blue">#</span><span style="color:red">#</span>
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>5
2 1 2 1 2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>256
</pre>
<p>Every way to paint the squares satisfies the condition.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>9
27 18 28 18 28 45 90 45 23
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>844733013
</pre>
<p>Remember to print the number of ways modulo <var>10^9 + 7</var>.</p></section>
</div>
</span> |
p03763 | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Snuke loves "paper cutting": he cuts out characters from a newspaper headline and rearranges them to form another string.</p>
<p>He will receive a headline which contains one of the strings <var>S_1,...,S_n</var> tomorrow.
He is excited and already thinking of what string he will create.
Since he does not know the string on the headline yet, he is interested in strings that can be created regardless of which string the headline contains.</p>
<p>Find the longest string that can be created regardless of which string among <var>S_1,...,S_n</var> the headline contains.
If there are multiple such strings, find the lexicographically smallest one among them.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq n \leq 50</var></li>
<li><var>1 \leq |S_i| \leq 50</var> for every <var>i = 1, ..., n</var>.</li>
<li><var>S_i</var> consists of lowercase English letters (<code>a</code> - <code>z</code>) for every <var>i = 1, ..., n</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>n</var>
<var>S_1</var>
<var>...</var>
<var>S_n</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the lexicographically smallest string among the longest strings that satisfy the condition.
If the answer is an empty string, print an empty line.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
cbaa
daacc
acacac
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>aac
</pre>
<p>The strings that can be created from each of <code>cbaa</code>, <code>daacc</code> and <code>acacac</code>, are <code>aa</code>, <code>aac</code>, <code>aca</code>, <code>caa</code> and so forth.
Among them, <code>aac</code>, <code>aca</code> and <code>caa</code> are the longest, and the lexicographically smallest of these three is <code>aac</code>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
a
aa
b
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>
</pre>
<p>The answer is an empty string.</p></section>
</div>
</span> |
p02871 | <span class="lang-en">
<div class="part">
<section>
<h3>Problem Setting</h3></section>
</div>
<div class="part">
<section>
<h3>Overview</h3>
<ul>
<li><strong>Concept:</strong> In this programming contest, you will run a delivery service. Customers will place orders with your shop. Each order has a unique <var>\text{ID}</var> and should be delivered to the corresponding customer. Your delivery service has one car. The car will fetch the ordered item from the shop and deliver it to the customer.</li>
<li><strong>Score:</strong> Your goal is to deliver as many items as possible, as quickly as possible in a given amount of time <var>T_{\text{max}}</var>. (Orders are expected until <var>0.95 \times T_{\text{max}}</var>).</li>
<li><strong>Constraint:</strong> In this contest there is no constraint on the number of items you can place in the car. However, an item can only be loaded in the car, by fetching it from the shop, after the order has been placed.</li>
<li><strong>Problem A/B:</strong> In problem A all order positions and times are given to the contestant in advance and the contestant algorithm shall optimize the moves of the car to make as many deliveries as possible as fast as possible. On the other hand, in problem B orders appear online, that is new orders appear, while you move your car to make as many deliveries as possible as fast as possible.</li>
</ul>
<div style="text-align:center;">
<img alt="overview" src="https://img.atcoder.jp/hokudai-hitachi2019-1/7faf401c00c790b9a8cb5c6968dc80c3.png" title="overview">
</img></div>
</section>
</div>
<div class="part">
<section>
<h3>Specification of Time and Space:</h3>
<ul>
<li><strong>Time:</strong> In this contest we model the progress of time by integer values <var>0 \le t < T_{\text{max}}</var>.</li>
<li><strong>Map:</strong> In this contest we model a map by a simple, undirected, and connected graph <var>G=(V, E)</var>, consisting of a set of vertices <var>V</var> and a set of edges <var>E</var></li>
<li><strong>Shop and customer locations:</strong> The vertices <var>u \in V</var> are labeled from <var>1</var> to <var>|V|</var> and the vertex <var>u=1</var> denotes the location of your shop, while vertices <var>u = 2,...,|V|</var> denote locations of potential customers. Here, <var>|V|</var> denotes the number of elements of the set <var>V</var>.</li>
<li><strong>Streets:</strong> Each edge <var>\left\{ u, v \right\} \in E</var> represents a street connecting the vertices <var>u, v \in V</var>. The corresponding length is given by an integer edge weight <var>d_{u, v} \ge 1</var>.</li>
<li><strong>Graph creation:</strong> The algorithm for generating the map graph based on a random seed is specified in the following pseudo-code. For further details, please see the sample code below.</li>
</ul>
<details>
<summary style="display:list-item"> <b>Pseudo code:</b> Map graph generator </summary>
<ul>
<li> <b>Input:</b><var>|V|</var>, <var>|E|</var>, <var>\mathrm{MaxDegree}=5</var></li>
<li> <b>2d vertex grid:</b>
<ul>
<li> First, find the largest integer <var>R>0</var> such that <var>|V| = R^{2} + r</var>, with <var>r</var> being the smallest possible non-negative integer.</li>
<li> Then we plot points <var>(x, y)</var> on the 2d vertex grid <var>(0 \leq x, y \lt R)</var>.</li>
<li> For each point <var>(x, y)</var> add a uniform random offset <var>dx, dy \in [0, 1] </var>, giving the final vertex position <var>(x + dx, y + dy)\in [0,R] \times [0,R]</var>.</li>
<li> Finally, add the remaining <var>r</var> vertices at a uniform random position <var>(x, y)</var> with <var>0 \leq x, y \leq R</var>.</li>
<li> Vertex labels <var>u \in V</var> are assigned by random shuffling. The shop is the vertex <var>u=1</var>.</li>
</ul>
</li>
<li> <b>How we create Highways:</b>
<ul>
<li> To generate a highway network, we create a complete graph <var>G_{\text{comp}}</var> on the vertex set <var>u \in V</var>, assigning each vertex pair <var>u, v \in V \times V</var> the Euclidean distance <var>W_{u, v}</var> as an edge weight.</li>
<li> Next, we construct a <a href="https://en.wikipedia.org/wiki/Minimum_spanning_tree"> minimum spanning </a> tree of <var>G_{\text{comp}}</var>. The <var>|V|-1</var> edges of the minimum spanning tree are the highway network of the graph <var>G</var>. We assign each of those edges <var>\left\{ u, v \right\}</var> an edge weight <var>d_{u,v} \leftarrow \lceil 2 \times W_{u, v} \rceil </var>.
</li>
</ul>
</li>
<li> <b>How we add side roads:</b>
<ul>
<li> To create a network of side roads, we successively add <var>|E|-(|V|-1)</var> edges to the graph <var>G</var> as follows:
<ul>
<li> Update <var>\mathrm{cost}(u,v)</var>. </li>
<li> Among the vertex pairs <var>\left( u, v \right) \in V\times V</var>, not yet connected by an edge, select a pair with minimal <var>\mathrm{cost}(u,v)</var>.</li>
<li> Assign the edge weight <var>d_{u,v} \leftarrow \lceil 4 \times W_{u, v} \rceil </var>.</li>
</ul>
</li>
<li>
Here, <var>\mathrm{cost}(u,v)</var> is essentially based on the Euclidean distance of vertices, giving preference to connecting nearby vertices with low degree. In addition, preference is given to side roads along the rectangular grid, to avoid too many bridges. The detailed definitions are as follows:
<ul>
<li> Define <var>\mathrm{degree}(u)</var>, the degree of vertex <var>u\in V</var> as the number of incident edges. </li>
<li> Define <var>\mathrm{color}(u)</var> for each vertex <var>u\in V</var> according to its original position <var>(x,y)</var> on the vertex grid as:
<ul>
<li> If <var>x+y</var> is even : <var>\mathrm{color}(u) = 0</var> </li>
<li> If <var>x+y</var> is odd : <var>\mathrm{color}(u) = 1</var> </li>
<li> For the remaining <var>r</var> vertices : Assign a color <var>\mathrm{color}(u) \in \left\{0,1\right\}</var> at random. </li>
</ul>
</li>
<li> Define a factor <var>f(u,v)</var> as follows:
<ul>
<li> If <var>\mathrm{color}(u)</var> and <var>\mathrm{color}(v)</var> are the same : Set <var>\mathrm{f}(u,v) = 5</var> </li>
<li> If <var>\mathrm{color}(u)</var> and <var>\mathrm{color}(v)</var> are different : Set <var>\mathrm{f}(u,v) = 1</var> </li>
</ul>
</li>
<li> Define a factor <var>g(u)</var> as follows:
<ul>
<li> If <var>\mathrm{degree}(u) \lt \mathrm{MaxDegree}</var> : Set <var>g(u)=1</var> </li>
<li> If <var>\mathrm{degree}(u) \geq \mathrm{MaxDegree}</var> : Set <var>g(u)=\infty</var> </li>
</ul>
</li>
<li> Finally, the cost is defined as follows:
<ul>
<li> <var>\mathrm{cost}(u,v) = W_{u,v}\times \mathrm{degree}(u) \times \mathrm{degree}(v) \times f(u,v) \times g(u) \times g(v)</var>. </li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li> <b> How we assign order frequencies: </b>
<ul>
<li> Assign each vertex <var>u \in V</var> an order frequency <var>f_u \in \left\{0,1,2\right\}</var>.</li>
<li> Init the order frequency of the shop vertex: <var>f_1 \leftarrow 0</var>. </li>
<li> Init the order frequency of the other vertices: <var>f_u \leftarrow 1</var></li>
<li> Now determine vertices with order frequency 2. For this draw a uniform random center point <var>c=(c_x,c_y)\in [R/4,3R/4]\times[R/4,3R/4]</var> and then for all vertices <var>u=2,...,|V|</var> do:
<ul>
<li> If <var>\mathrm{EuclideanDistance}(c,u)\le R/8 + \mathrm{uniformRandom}[0,R/8]</var>: <var>f_{u} \leftarrow 2</var> </li>
</ul>
</li>
</ul>
</li>
</ul>
</details>
</section>
</div>
<div class="part">
<section>
<h3>Specification of Car Locations and Moves:</h3>
<p>In order to make deliveries you will operate a delivery car, which can take positions and make moves as specified below.</p>
<ul>
<li>
<p><strong>Car position:</strong> A car can generally take two types of position:</p>
<ul>
<li>on a vertex <var>u \in V</var>.</li>
<li>on an edge <var>\left\{ u, v \right\} \in E</var>. More specifically, it is located at a distance <var>x</var> <var>(0 \lt x \lt d_{u, v})</var> from <var> u </var> to <var> v </var>.</li>
</ul>
</li>
<li>
<p><strong>Car move:</strong> At each step <var> 0 \le t < T_{\text{max}} </var> you have to choose one of the following actions in order to control your delivery car.</p>
<ul>
<li><code>stay</code>: stay at the current position.</li>
<li><code>move w</code>: Take one step towards vertex <var>w \in V</var>.</li>
</ul>
<p>In case of choosing <code>move w</code>, <var>w</var> must obey the following constraints. A failure to obey these constraints results in a wrong answer <code>WA</code>.
<ul>
<li> <var>w</var> must be a vertex, i.e., <var>w \in V</var>.</li>
<li> If the car is on vertex <var>u \in V</var>, there must be an edge connecting <var>u</var> and <var>v</var>, i.e., <var>\left\{ u, w \right\} \in E</var>.</li>
<li> If the car is on the edge <var>\left\{ u, v \right\} \in E</var>, <var>w</var> must either be <var>w = u</var> or <var>w = v</var>.</li>
</ul></p>
</li>
</ul>
<div style="text-align:center;">
<img alt="Car position and moves" src="https://img.atcoder.jp/hokudai-hitachi2019-1/60c6d261238fb10fb2bb26d89d275f9c.png" title="Car position and moves"/>
</div>
</section>
</div>
<div class="part">
<section>
<h3>Orders, Deliveries, and Constraints:</h3>
<ul>
<li><strong>Orders:</strong> Throughout the contest each order is characterized by three quantities: A unique order ID, a vertex <var>v \in V</var> indicating the order destination, and the order time <var>t</var> at which the order appeared. For the detailed format see below.</li>
<li><strong>Order generation:</strong> At each time <var> 0 \le t \le T_{\text{last}} = 0.95 \times T_{\text{max}}</var> up to one new order can appear with probability <var>p_{\text{order}}(t)</var>. In case there is an order, the order destination <var>i</var> is chosen from the vertex set <var>V</var> with probability proportional to the order frequency <var>f_i</var>. For details, see the pseudo-code below or the sample code further below.</li>
</ul>
<details>
<summary style="display:list-item"> <b>Pseudo code:</b> Order generation </summary>
<ul>
<li> <b>Input:</b> Last order time <var>T_{\text{last}}</var> and average order probability <var>p_{\text{order}}(t)</var>. </li>
<li> <b>Init:</b> <var> \mathrm{ID} \leftarrow 0</var>. </li>
<li> For each step <var>t = 0, ..., T_{\text{last}} </var> do:
<ul>
<li> Generate a uniform random number <var>r \in [0,1] </var>. </li>
<li> <b>If</b> <var> r \le p_{\text{order}}(t) </var>:
<ul>
<li> Select a vertex position <var> u \in V </var> at random with probability proportional to the order frequency <var> f_{u} </var> of the vertex. </li>
<li> <var>\mathrm{ID} \leftarrow \mathrm{ID} + 1</var> </li>
<li> place order (new order ID, order time <var>t</var>, vertex position
<var> u \in V </var>)</li>
</ul>
</li>
<li> <b>Else:</b> place no order</li>
</ul>
</li>
</ul>
<ul style="list-style:none;">
<li> <b>Note:</b> The average order probability is defined as follows:
<li>
<var> p_{\text{order}}(t) =
\begin{cases}
t / T_{\text{peak}}, &
\text{if } 0\le t \lt T_{\text{peak}}, \\
(T_{\text{last}} - t) / (T_{\text{last}}- T_{\text{peak}}), &
\text{if } T_{\text{peak}} \le t \lt T_{\text{last}}, \\
0, &
\text{if } T_{\text{last}} \le t,
\end{cases}
</var></li>
<li>where <var>T_{\text{last}}:=0.95 \times T_{\max}</var> and <var>T_{\text{peak}}</var> is drawn randomly uniform from the interval <var>[0, T_{\text{last}}]</var>.</li>
<li> Note: The value of <var>T_{\text{peak}}</var> will not be given as an input. </li>
</li></ul>
<div style="text-align:center;">
<img src="https://img.atcoder.jp/hokudai-hitachi2019-1/caa24f9e6a715e0d1a778f1fdfe4e14b.png"/>
</div>
</details>
<ul>
<li><strong>Delivery:</strong> To deliver an order, the contestant must do the following steps after the order has been placed:<ul>
<li><strong>(1st) Move the car onto the shop:</strong> Note: When moving a car onto the shop, all orders with order time less than or equal to the current time, will be transfered into the car. On the other hand, orders which have not appeared yet, cannot be placed into the car.</li>
<li><strong>(2nd) Move the car to the customer position:</strong> To finalize a delivery, move the car onto the vertex of the customer position. Note: Orders which have not been transfered into the car yet, will not be delivered, even if you arrive at the customer position.</li>
</ul>
</li>
</ul>
<div style="text-align:center;">
<img alt="constraint image" src="https://img.atcoder.jp/hokudai-hitachi2019-1/03b28647c6ddc92cc3cbb33ade09f468.png" title="Constraints"/>
</div>
<ul>
<li><strong>Constraints:</strong> Throughout the contest, we assume each order has a unique <var>\text{ID}</var> and should be delivered to the corresponding customer. It is further assumed that an unlimited number of orders can be placed in the car.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Scoring</h3>
<ul>
<li> During the contest the total score of a submission is determined by summing the score of the submission with respect to 30 input cases.</li>
<li> After the contest a system test will be performed. To this end, the contestant's <b>last submission</b> will be scored by summing the score of the submission on 100 previously unseen input cases.</li>
<li> For each input case, the score is calculated as follows:
<p style="text-align:center;">
<var> \text{Score} =
\sum_{i \in D} {(T_{\text{max}})}^{2} - {(\mathrm{waitingTime}_i)}^{2}, </var>
</p>
Here we use the following definitions:
<ul>
<li> <var> D </var> : the set of orders delivered until <var>t=T_{\text{max}}</var></li>
<li> the waiting time of the <var>i</var>th order: <var>\mathrm{waitingTime}_i = \mathrm{deliveredTime}_i - \mathrm{orderedTime}_i</var>.</li>
<li>Note that an input case giving the output <code>WA</code> will receive <var>0</var> points.</li>
</ul>
</li>
</ul></section>
</div>
<div class="part">
<section>
<h3>Particulars of Problem A</h3>
<p>In problem A we pass all orders which appear at time <var> 0 \le t < 0.95 \times T_{\text{max}} </var> as an input to the contestant code. The task is to provide an algorithm which optimizes the moves of the car such that the above score becomes maximal.</p>
<hr/>
</section>
</div>
<div class="part">
<section>
<h3>Input Format</h3>
<p>Input is provided in the following form:</p>
<pre><var>|V|</var> <var>|E|</var>
<var>u_{1}</var> <var>v_{1}</var> <var>d_{u_{1}, v_{1}}</var>
<var>u_{2}</var> <var>v_{2}</var> <var>d_{u_{2}, v_{2}}</var>
:
<var>u_{|E|}</var> <var>v_{|E|}</var> <var>d_{u_{|E|}, v_{|E|}}</var>
<var>T_{\text{max}}</var>
<var>\mathrm{info}_{0}</var>
<var>\mathrm{info}_{1}</var>
:
<var>\mathrm{info}_{T_{\text{max}}-1}</var>
</pre>
<ul>
<li> In the first line <var>|V|</var> denotes the number of vertices, while <var>|E|</var> denotes the number of edges.</li>
<li> The following <var>|E|</var> lines denote the edges of the graph. In particular, in the <var>i</var>th line <var>u_{i}</var> and <var>v_{i}</var> denote the edge connecting <var>u_{i}</var> and <var>v_{i}</var> and <var>d_{u_{i}, v_{i}}</var> the corresponding distance.</li>
<li> The following line denotes the number of steps <var>T_{\text{max}}</var>.</li>
</ul>
<p>In the following line, <var>\mathrm{info}_t</var> is information about the order from the customer that occurs at time <var>t</var>. <var>\mathrm{info}_t</var> is given in the form:</p>
<pre><var>N_{\text{new}}</var>
<var>\mathrm{new\_id}_1</var> <var>\mathrm{dst}_1</var>
<var>\mathrm{new\_id}_2</var> <var>\mathrm{dst}_2</var>
<var>\vdots</var>
<var>\mathrm{new\_id}_{N_{\text{new}}}</var> <var>\mathrm{dst}_{N_{\text{new}}}</var>
</pre>
<ul>
<li> <var>N_{\text{new}}</var> represents the number of new orders which appear at time <var>t</var>.</li>
<li> The next <var>N_{\text{new}}</var> lines give the newly generated order information. The <var>i</var>-th order information indicates that the order ID <var>\mathrm{new\_{id}}_i</var> of the new order, while <var>\mathrm{dst}_i</var>
denotes the vertex to which the customer wishes the order to be delivered.</li>
<li> Note: If <var>N_{\text{new}}=0</var>, there are no new lines. </li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Requirements</h3>
<ul>
<li> All inputs are of non-negative integer value. </li>
<li> <var>T_{\text{max}} = 10000</var></li>
<li> <var>200 \leq |V| \leq 400</var> </li>
<li> <var>1.5 |V| \leq |E| \leq 2 |V|</var></li>
<li><var>1 \leq u_{i}, v_{i} \leq |V|</var> <var>(1 \leq i \leq |E|)</var></li>
<li><var>1 \leq d_{u_i, v_i} \leq \lceil 4\sqrt{2|V|} \rceil</var> <var>(1 \leq i \leq |E|)</var></li>
<li>The given graph has no self-loops / multiple edges and is guaranteed to be connected.</li>
<li><var>0 \leq N_{\text{new}} \leq 1</var></li>
<li><var>1 \leq \mathrm{new\_id}_{i} \leq T_{\text{last}}+1</var> <var>(1 \leq i \leq N_{\text{new}})</var>. Note: If all orders are generated by the order generation rule as explained above, the total number of orders is at most <var>T_{\text{last}}+1</var>. Therefore, the possible range of <var>\mathrm{new\_id}_{i}</var> should be from <var>1</var> to <var>T_{\text{last}}+1</var>.</li>
<li><var>2 \leq \mathrm{dst}_i \leq |V|</var> <var>(1 \leq i \leq N_{\text{new}})</var></li>
<li>The order IDs <var>\mathrm{new\_{id}}_i</var> are unique. </li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Output Format</h3>
<p>The Output expects <var>T_{\text{max}}</var> integers in the format specified below.</p>
<pre><var>\mathrm{command}_{0}</var>
<var>\mathrm{command}_{1}</var>
:
<var>\mathrm{command}_{T_{\text{max}}-1}</var>
</pre>
<p>In particular, <var>\mathrm{command}_{i}</var> shall specify the movement of the delivery car by using one of the following two options:</p>
<p>1) <code>stay</code>, if the car shall not move:</p>
<pre>-1
</pre>
<p>2) <code>move w</code>, if the car shall be moved one step towards the neighboring vertex <var>w</var></p>
<pre>w
</pre>
<p>Note that in case of 2) <var>w</var> has to satisfy the following conditions:</p>
<ul>
<li><var>w \in V</var></li>
<li>If the car is at vertex <var>u</var>: <var>\left\{ u, w \right\} \in E </var>.</li>
<li>If the car is on the edge <var>\left\{ u, v \right\}</var>, <var>w</var> must either satisfy <var>u = w</var> or <var>v = w</var>.
</li>
</ul>
<hr/>
</section>
</div>
<div class="part">
<section>
<h3>Input Example</h3>
<pre>5 7
1 2 5
5 3 4
2 4 8
1 5 1
2 3 3
4 5 3
4 3 9
4
1
1 2
1
2 5
1
3 4
0
</pre>
<p><strong>Note that this input is an example of small size and does not meet the constraints of the contest.</strong></p>
</section>
</div>
<div class="part">
<section>
<h3>Output Example</h3>
<pre>2
-1
1
5
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Explanation</h3>
<p>The example operates on a graph with <var>|V| = 5</var> vertices, <var>|E| = 7</var> edges, and <var>T_{\text{max}} = 4</var> time steps. We now describe the orders which have occured and the movement of the car.</p>
<h4>Time <var>t=0</var></h4>
<p>At time <var>t=0</var> an order is placed at the shop. This order has ID<var>= 1</var> and should be delivered to vertex <var> 2</var>. Because your car is currently at vertex one, the order will be automatically transfered into your car. In this way, when your car is at the shop, all orders which have been made at present and before, will automatically be loaded into your car.</p>
<h4>Time <var>0</var> → <var>1</var></h4>
<p>You choose to move towards vertex <code>move 2</code>. You will now move one step towards vertex 2.</p>
<h4>Time <var>t=1</var></h4>
<p>A new order has appeared. It has ID <var>=2</var> and shall be delivered at vertex <var>5</var>.</p>
<h4>Time <var>1</var> → <var>2</var></h4>
<p>You decided to <code>stay</code>. You can now stay on the edge where you are.</p>
<h4>Time <var>t=2</var></h4>
<p>A new order has appeared. It has ID <var>=3</var> and shall be delivered at vertex <var>4</var>.</p>
<h4>Time <var>2</var> → <var>3</var></h4>
<p>You decided to move <code>move 1</code> back to vertex 1.
You are approaching one step towards vertex 1.
Doing a U-turn in this way is explicitly allowed.</p>
<h4>Time <var>t=3</var></h4>
<p>New orders have not occurred. Now that you are at the vertex <var> 1 </var> (store), the orders with order ID <var> 2, 3 </var> are loaded into your car. In a similar way, whenever you return to the store, all the orders that have already been placed are loaded into your car automatically.</p>
<h4>Time <var>3</var> → <var>4</var></h4>
<p>Being at vertex 1 you choose <code>move 5</code>. You are moving your car one step towards vertex <var>5</var>. You arrive at vertex <var>5</var>.</p>
<h4>Time <var>t=4</var></h4>
<p>Since you arrived at vertex <var>5</var>, the order with order ID <var>2</var> can be delivered.</p>
</section>
</div>
<div class="part">
<section>
<h3>Sample Code A</h3>
<p>A software toolkit for generation of input samples, scoring and testing on a local contestant environment, and sample codes for beginners
is provided through the following <a href="https://img.atcoder.jp/hokudai-hitachi2019-1/5bc882a275d9543da1b79d9a670c6523.zip">link</a>. In addition we provide <a href="https://img.atcoder.jp/hokudai-hitachi2019-1/d2dded0de70ff38cbb8aa6dbfb88dfc3.zip">software for visualizing</a> the contestants results.</p></section>
</div>
</span> |
p03333 | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Takahashi and Aoki will play a game with a number line and some segments.
Takahashi is standing on the number line and he is initially at coordinate <var>0</var>.
Aoki has <var>N</var> segments. The <var>i</var>-th segment is <var>[L_i,R_i]</var>, that is, a segment consisting of points with coordinates between <var>L_i</var> and <var>R_i</var> (inclusive).</p>
<p>The game has <var>N</var> steps. The <var>i</var>-th step proceeds as follows:</p>
<ul>
<li>First, Aoki chooses a segment that is still not chosen yet from the <var>N</var> segments and tells it to Takahashi.</li>
<li>Then, Takahashi walks along the number line to some point within the segment chosen by Aoki this time.</li>
</ul>
<p>After <var>N</var> steps are performed, Takahashi will return to coordinate <var>0</var> and the game ends.</p>
<p>Let <var>K</var> be the total distance traveled by Takahashi throughout the game. Aoki will choose segments so that <var>K</var> will be as large as possible, and Takahashi walks along the line so that <var>K</var> will be as small as possible. What will be the value of <var>K</var> in the end?</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 ≤ N ≤ 10^5</var></li>
<li><var>-10^5 ≤ L_i < R_i ≤ 10^5</var></li>
<li><var>L_i</var> and <var>R_i</var> are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>L_1</var> <var>R_1</var>
:
<var>L_N</var> <var>R_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the total distance traveled by Takahashi throughout the game when Takahashi and Aoki acts as above.
It is guaranteed that <var>K</var> is always an integer when <var>L_i,R_i</var> are integers.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
-5 1
3 7
-4 -2
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>10
</pre>
<p>One possible sequence of actions of Takahashi and Aoki is as follows:</p>
<ul>
<li>Aoki chooses the first segment. Takahashi moves from coordinate <var>0</var> to <var>-4</var>, covering a distance of <var>4</var>.</li>
<li>Aoki chooses the third segment. Takahashi stays at coordinate <var>-4</var>.</li>
<li>Aoki chooses the second segment. Takahashi moves from coordinate <var>-4</var> to <var>3</var>, covering a distance of <var>7</var>.</li>
<li>Takahashi moves from coordinate <var>3</var> to <var>0</var>, covering a distance of <var>3</var>.</li>
</ul>
<p>The distance covered by Takahashi here is <var>14</var> (because Takahashi didn't move optimally).
It turns out that if both players move optimally, the distance covered by Takahashi will be <var>10</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
1 2
3 4
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>12
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>5
-2 0
-2 0
7 8
9 10
-2 -1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>34
</pre></section>
</div>
</span> |
p03626 | <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a board with a <var>2 \times N</var> grid.
Snuke covered the board with <var>N</var> dominoes without overlaps.
Here, a domino can cover a <var>1 \times 2</var> or <var>2 \times 1</var> square.</p>
<p>Then, Snuke decided to paint these dominoes using three colors: red, cyan and green.
Two dominoes that are adjacent by side should be painted by different colors.
Here, it is not always necessary to use all three colors.</p>
<p>Find the number of such ways to paint the dominoes, modulo <var>1000000007</var>.</p>
<p>The arrangement of the dominoes is given to you as two strings <var>S_1</var> and <var>S_2</var> in the following manner:</p>
<ul>
<li>Each domino is represented by a different English letter (lowercase or uppercase).</li>
<li>The <var>j</var>-th character in <var>S_i</var> represents the domino that occupies the square at the <var>i</var>-th row from the top and <var>j</var>-th column from the left.</li>
</ul>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 52</var></li>
<li><var>|S_1| = |S_2| = N</var></li>
<li><var>S_1</var> and <var>S_2</var> consist of lowercase and uppercase English letters.</li>
<li><var>S_1</var> and <var>S_2</var> represent a valid arrangement of dominoes.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>S_1</var>
<var>S_2</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the number of such ways to paint the dominoes, modulo <var>1000000007</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>3
aab
ccb
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>6
</pre>
<p>There are six ways as shown below:</p>
<p><img alt="" src="https://atcoder.jp/img/arc081/899673bd23529f4fb5e41c6e7d5bc372.png"/></p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>1
Z
Z
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>3
</pre>
<p>Note that it is not always necessary to use all the colors.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>52
RvvttdWIyyPPQFFZZssffEEkkaSSDKqcibbeYrhAljCCGGJppHHn
RLLwwdWIxxNNQUUXXVVMMooBBaggDKqcimmeYrhAljOOTTJuuzzn
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>958681902
</pre></section>
</div>
</span> |
p02934 | <span class="lang-en">
<p>Score : <var>200</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given is a sequence of <var>N</var> integers <var>A_1, \ldots, A_N</var>.</p>
<p>Find the (multiplicative) inverse of the sum of the inverses of these numbers, <var>\frac{1}{\frac{1}{A_1} + \ldots + \frac{1}{A_N}}</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 100</var></li>
<li><var>1 \leq A_i \leq 1000</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var>
<var>A_1</var> <var>A_2</var> <var>\ldots</var> <var>A_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print a decimal number (or an integer) representing the value of <var>\frac{1}{\frac{1}{A_1} + \ldots + \frac{1}{A_N}}</var>.</p>
<p>Your output will be judged correct when its absolute or relative error from the judge's output is at most <var>10^{-5}</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>2
10 30
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>7.5
</pre>
<p><var>\frac{1}{\frac{1}{10} + \frac{1}{30}} = \frac{1}{\frac{4}{30}} = \frac{30}{4} = 7.5</var>.</p>
<p>Printing <code>7.50001</code>, <code>7.49999</code>, and so on will also be accepted.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3
200 200 200
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>66.66666666666667
</pre>
<p><var>\frac{1}{\frac{1}{200} + \frac{1}{200} + \frac{1}{200}} = \frac{1}{\frac{3}{200}} = \frac{200}{3} = 66.6666...</var>.</p>
<p>Printing <code>6.66666e+1</code> and so on will also be accepted.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1
1000
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>1000
</pre>
<p><var>\frac{1}{\frac{1}{1000}} = 1000</var>.</p>
<p>Printing <code>+1000.0</code> and so on will also be accepted.</p></section>
</div>
</span> |
p00909 |
<H1><font color="#000">Problem F: </font>Never Wait for Weights</H1>
<p>
In a laboratory, an assistant, Nathan Wada, is measuring weight differences between sample pieces pair by pair. He is using a balance because it can more precisely measure the weight difference between two samples than a spring scale when the samples have nearly the same weight.
</p>
<p>
He is occasionally asked the weight differences between pairs of samples. He can or cannot answer based on measurement results already obtained.
</p>
<p>
Since he is accumulating a massive amount of measurement data, it is now not easy for him to promptly tell the weight differences. Nathan asks you to develop a program that records measurement results and automatically tells the weight differences.
</p>
<H2>Input</H2>
<p>
The input consists of multiple datasets. The first line of a dataset contains two integers <i>N</i> and <i>M</i>. <i>N</i> denotes the number of sample pieces (2 ≤ <i>N</i> ≤ 100,000). Each sample is assigned a unique number from 1 to <i>N</i> as an identifier. The rest of the dataset consists of <i>M</i> lines (1 ≤ <i>M</i> ≤ 100,000), each of which corresponds to either a measurement result or an inquiry. They are given in chronological order.
</p>
<p>
A measurement result has the format,
</p>
<p>
! <i>a b w</i>
</p>
<p>
which represents the sample piece numbered <i>b</i> is heavier than one numbered <i>a</i> by <i>w</i> micrograms (<i>a</i> ≠ <i>b</i>). That is, <i>w</i> = <i>w<sub>b</sub></i> − <i>w<sub>a</sub></i>, where <i>w<sub>a</sub></i> and <i>w<sub>b</sub></i> are the weights of <i>a</i> and <i>b</i>, respectively. Here, <i>w</i> is a non-negative integer not exceeding 1,000,000.
</p>
<p>
You may assume that all measurements are exact and consistent.
</p>
<p>
An inquiry has the format,
</p>
<p>
? <i>a b</i>
</p>
<p>
which asks the weight difference between the sample pieces numbered <i>a</i> and <i>b</i> (<i>a</i> ≠ <i>b</i>).
</p>
<p>
The last dataset is followed by a line consisting of two zeros separated by a space.
</p>
<H2>Output</H2>
<p>
For each inquiry, ? <i>a</i> <i>b</i>, print the weight difference in micrograms between the sample pieces numbered <i>a</i> and <i>b</i>, <i>w<sub>b</sub></i> − <i>w<sub>a</sub></i>, followed by a newline if the weight difference can be computed based on the measurement results prior to the inquiry. The difference can be zero, or negative as well as positive. You can assume that its absolute value is at most 1,000,000. If the difference cannot be computed based on the measurement results prior to the inquiry, print <span>UNKNOWN</span> followed by a newline.
</p>
<H2>Sample Input</H2>
<pre>
2 2
! 1 2 1
? 1 2
2 2
! 1 2 1
? 2 1
4 7
! 1 2 100
? 2 3
! 2 3 100
? 2 3
? 1 3
! 4 3 150
? 4 1
0 0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
1
-1
UNKNOWN
100
200
-50
</pre> |
p03276 | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>There are <var>N</var> candles placed on a number line.
The <var>i</var>-th candle from the left is placed on coordinate <var>x_i</var>.
Here, <var>x_1 < x_2 < ... < x_N</var> holds.</p>
<p>Initially, no candles are burning.
Snuke decides to light <var>K</var> of the <var>N</var> candles.</p>
<p>Now, he is at coordinate <var>0</var>.
He can move left and right along the line with speed <var>1</var>.
He can also light a candle when he is at the same position as the candle, in negligible time.</p>
<p>Find the minimum time required to light <var>K</var> candles.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>1 \leq N \leq 10^5</var></li>
<li><var>1 \leq K \leq N</var></li>
<li><var>x_i</var> is an integer.</li>
<li><var>|x_i| \leq 10^8</var></li>
<li><var>x_1 < x_2 < ... < x_N</var></li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>x_1</var> <var>x_2</var> <var>...</var> <var>x_N</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum time required to light <var>K</var> candles.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>5 3
-30 -10 10 20 50
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>40
</pre>
<p>He should move and light candles as follows:</p>
<ul>
<li>Move from coordinate <var>0</var> to <var>-10</var>.</li>
<li>Light the second candle from the left.</li>
<li>Move from coordinate <var>-10</var> to <var>10</var>.</li>
<li>Light the third candle from the left.</li>
<li>Move from coordinate <var>10</var> to <var>20</var>.</li>
<li>Light the fourth candle from the left.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>3 2
10 20 30
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>20
</pre>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1 1
0
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0
</pre>
<ul>
<li>There may be a candle placed at coordinate <var>0</var>.</li>
</ul>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 4</h3><pre>8 5
-9 -7 -4 -3 1 2 3 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 4</h3><pre>10
</pre></section>
</div>
</span> |
p04049 | <span class="lang-en">
<p>Score : <var>600</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given an undirected tree, let the distance between vertices <var>u</var> and <var>v</var> be the
number of edges on the simple path from <var>u</var> to <var>v</var>.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree <em>good</em> if and only if its diameter is at most <var>K</var>.</p>
<p>You are given an undirected tree with <var>N</var> vertices numbered <var>1</var> through <var>N</var>.
For each <var>i (1≦i≦N-1)</var>, there is an edge connecting vertices <var>A_i</var> and <var>B_i</var>.</p>
<p>You want to remove zero or more vertices from the tree, so that the resulting tree is good.
When a vertex is removed, all incident edges will also be removed.
The resulting graph must be connected.</p>
<p>Find the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2≦N≦2000</var></li>
<li><var>1≦K≦N-1</var></li>
<li><var>1≦A_i≦N, 1≦B_i≦N</var></li>
<li>The graph defined by <var>A_i</var> and <var>B_i</var> is a tree.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>The input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
<var>A_1</var> <var>B_1</var>
<var>A_2</var> <var>B_2</var>
:
<var>A_{N-1}</var> <var>B_{N-1}</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum number of vertices that you need to remove in order to produce a good tree.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6 2
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>2
</pre>
<p>The tree is shown below. Removing vertices <var>5</var> and <var>6</var> will result in a good tree with the diameter of <var>2</var>.</p>
<div style="text-align: center;">
<img alt="ctree.png" src="https://agc001.contest.atcoder.jp/img/agc/001/Gg9pvPKw/ctree.png">
</img></div>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>6 5
1 2
3 2
4 2
1 6
5 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>0
</pre>
<p>Since the given tree is already good, you do not need to remove any vertex.</p></section>
</div>
</span> |
p02037 | <h1>A: タイル貼り</h1>
<h2>問題</h2>
<p>
縦 $h$ , 横 $w$ の大きさの長方形の壁に, 縦 $a$ , 横 $b$ の大きさの長方形のタイルを出来るだけたくさん貼りたい.
</p>
<p>
タイルを貼る際は以下の条件を満たす必要がある.<br>
<ul>
<li>タイルを重ねてはいけない.</li>
<li>タイルを斜めに貼ってはいけない. すなわち, タイルのどの縁も, 壁のどの縁に対しても平行, または垂直である.</li>
<li>タイルの向きを変えない. すなわち, 縦横を入れ替えない.</li>
</ul>
</p>
<p>
出来るだけ多くのタイルを貼ったとき, タイルに覆われていない部分の面積の総和を求めよ.
</p>
<h2>制約</h2>
<ul>
<li>入力値は全て整数である.</li>
<li>$1 \leq h,w,a,b \leq 100$</li>
</ul>
<h2>入力形式</h2>
<p> 入力は以下の形式で与えられる. </p>
<p>
$h\ w$<br>
$a\ b$<br>
</p>
<h2>出力</h2>
<p>
タイルに覆われていない部分の面積の総和を出力せよ. また, 末尾に改行も出力せよ.
</p>
<h2>サンプル</h2>
<h3>サンプル入力 1</h3>
<pre>
5 8
2 2
</pre>
<h3>サンプル出力 1</h3>
<pre>
8
</pre>
<h3>サンプル入力 2</h3>
<pre>
11 10
4 3
</pre>
<h3>サンプル出力 2</h3>
<pre>
38
</pre>
<h3>サンプル入力 3</h3>
<pre>
4 7
7 4
</pre>
<h3>サンプル出力 3</h3>
<pre>
28
</pre>
|
p02467 |
<H1>Prime Factorization</H1>
<br/>
<p>
Factorize a given integer <var>n</var>.
</p>
<H2>Input</H2>
<pre>
<var>n</var>
</pre>
<p>
An integer <var>n</var> is given in a line.
</p>
<H2>Output</H2>
<p>
Print the given integer <var>n</var> and <span>:</span>. Then, print prime factors in ascending order. If <var>n</var> is divisible by a prime factor several times, the prime factor should be printed according to the number of times. Print a space before each prime factor.
</p>
<H2>Constraints</H2>
<ul>
<li>
2 ≤ <var>n</var> ≤ 10<sup>9</sup>
</li>
</ul>
<H2>Sample Input 1</H2>
<pre>
12
</pre>
<H2>Sample Output 1</H2>
<pre>
12: 2 2 3
</pre>
<br/>
<H2>Sample Input 2</H2>
<pre>
126
</pre>
<H2>Sample Output 2</H2>
<pre>
126: 2 3 3 7
</pre>
|
p00374 | <H1>Iron Bars</H1>
<p>
A boy PCK had <var>N</var> straight iron bars, which were serially indexed. Unfortunately, the first <var>M</var> bars (0 ≤ <var>M</var> ≤ <var>N</var>) among them were bent during transportation. They all suffered a perpendicular bend at one point.
</p>
<p>
He is planning to make a cube using a set of bars selected using the following rules: <var>X</var> bars from bent ones, <var>Y</var> bars from straight ones, where <var>2X + Y = 12</var>. Any two bars can be jointed only at the apexes of the cube. He wants to count how many types of rectangular parallelepipeds (hereafter RP) he can make using these bars.
</p>
<p>
Make a program to count out types (different shapes) of RPs that PCK can make using the following information: the number of bars and length of each one, position of the bend, and the number of bars to be used to construct an RP. Note that any two RPs similar in shape are considered identical: namely if the length of three defining sides of two RPs coincide if arranged in increasing/decreasing order (e.g., three sides of RP <var>i</var> and <var>j</var> are <var>A_i, B_i, C_i</var>, and <var>A_j, B_j</var> and <var>C_j</var> in increasing order, then the relations <var>A_i = A_j, B_i = B_j</var>, and <var>C_i = C_j</var> hold. Note also that the bars are sufficiently thin to allow you to consider them as idealized lines.
</p>
<h2>Input</h2>
<p>
The input is given in the following format.
</p>
<pre>
<var>N</var> <var>M</var> <var>X</var> <var>Y</var>
<var>a_1</var>
<var>a_2</var>
:
<var>a_N</var>
<var>b_1</var>
<var>b_2</var>
:
<var>b_M</var>
</pre>
<p>
The first line provides the total number of iron bars and bent bars, and those straight and bent bars used to construct an RP: <var>N</var> (6 ≤ <var>N</var> ≤ 6000), <var>M</var> (0 ≤ <var>M</var> ≤ <var>N</var>), <var>X</var> (0 ≤ <var>X</var> ≤ 6), and <var>Y</var> (0 ≤ <var>Y</var> ≤ 12). The following relations always hold for them: <var>2X+Y=12</var>, <var>X+Y</var> ≤ <var>N</var>, <var>X</var> ≤ <var>M</var>. Each of the subsequent <var>N</var> lines provides the length of the <var>i</var>-th bar <var>a_i</var> (1 ≤ <var>a_i</var> ≤ 6000) in integers. Furthermore, each of the subsequent <var>M</var> lines provides the location at which the <var>i</var>-th bent bar suffered a perpendicular bend <var>b_i</var> (1 ≤ <var>b_i</var> ≤ 3000) in centimeters from one end of the bar (note: 1 ≤ <var>a_i-b_i</var> ≤ 3000).
</p>
<h2>Output</h2>
<p>
Output the number of constructible rectangular parallelepipeds.
</p>
<h2>Sample Input 1</h2>
<pre>
18 8 3 6
4
3
3
3
3
2
2
2
1
1
1
1
1
2
2
3
3
3
1
1
1
1
1
1
1
1
</pre>
<h2>Sample Output 1</h2>
<pre>
3
</pre>
|
p02349 |
<H1>Range Add Query (RAQ)</H1>
<p>
Write a program which manipulates a sequence <var>A</var> = {<var>a<sub>1</sub>, a<sub>2</sub>, . . . , a<sub>n</sub></var>} with the following operations:
</p>
<ul>
<li><var>add(s, t, x)</var>: add <var>x</var> to <var>a<sub>s</sub></var>, <var>a<sub>s+1</sub></var>, ..., <var>a<sub>t</sub></var>.</li>
<li><var>get(i)</var>: output the value of <var>a<sub>i</sub>.</li>
</ul>
<p>
Note that the initial values of <var>a<sub>i</sub></var> (<var>i = 1, 2, . . . , n</var>) are 0.
</p>
<H2>Input</H2>
<pre>
<var>n</var> <var>q</var>
<var>query<sub>1</sub></var>
<var>query<sub>2</sub></var>
:
<var>query<sub>q</sub></var>
</pre>
<p>
In the first line, <var>n</var> (the number of elements in <var>A</var>) and <var>q</var> (the number of queries) are given. Then, <i>i</i>th query <var>query<sub>i</sub></var> is given in the following format:
</p>
<pre>
0 <var>s</var> <var>t</var> <var>x</var>
</pre>
<p>
or
</p>
<pre>
1 <var>t</var>
</pre>
<p>
The first digit represents the type of the query. '0' denotes <var>add(s, t, x)</var> and '1' denotes <var>get(i)</var>.
</p>
<H2>Output</H2>
<p>
For each <var>get</var> operation, print the value.
</p>
<H2>Constraints</H2>
<ul>
<li>
<var>1 ≤ n ≤ 100000</var>
</li>
<li>
<var>1 ≤ q ≤ 100000</var>
</li>
<li>
<var>1 ≤ s ≤ t ≤ n</var>
</li>
<li>
<var>1 ≤ i ≤ n</var>
</li>
<li>
<var>0 ≤ x ≤ 1000</var>
</li>
</ul>
<H2>Sample Input 1</H2>
<pre>
3 5
0 1 2 1
0 2 3 2
0 3 3 3
1 2
1 3
</pre>
<H2>Sample Output 1</H2>
<pre>
3
5
</pre>
<br/>
<H2>Sample Input 2</H2>
<pre>
4 3
1 2
0 1 4 1
1 2
</pre>
<H2>Sample Output 2</H2>
<pre>
0
1
</pre> |
p01836 |
<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>Shortest Bridge</h2>
<p>
There is a city whose shape is a 1,000 $\times$ 1,000 square. The city has a big river, which flows from the north to the south and separates the city into just two parts: the west and the east.
</p>
<p>
Recently, the city mayor has decided to build a highway from a point $s$ on the west part to a point $t$ on the east part. A highway consists of a bridge on the river, and two roads: one of the roads connects $s$ and the west end of the bridge, and the other one connects $t$ and the east end of the bridge. Note that each road doesn't have to be a straight line, but the intersection length with the river must be zero.
</p>
<p>
In order to cut building costs, the mayor intends to build a highway satisfying the following conditions:
</p>
<ul>
<li> Since bridge will cost more than roads, at first the length of a bridge connecting the east part and the west part must be as short as possible.</li>
<li> Under the above condition, the sum of the length of two roads is minimum.</li>
</ul>
<p>
Your task is to write a program computing the total length of a highway satisfying the above conditions.
</p>
<h3>Input</h3>
<p>
The input consists of a single test case. The test case is formatted as follows.<br/>
<br/>
$sx$ $sy$ $tx$ $ty$<br/>
$N$<br/>
$wx_1$ $wy_1$<br/>
...<br/>
$wx_N$ $wy_N$<br/>
$M$<br/>
$ex_1$ $ey_1$<br/>
...<br/>
$ex_M$ $ey_M$<br/>
</p>
<p>
At first, we refer to a point on the city by a coordinate ($x, y$): the distance from the west side is $x$ and the distance from the north side is $y$.
</p>
<p>
The first line contains four integers $sx$, $sy$, $tx$, and $ty$ ($0 \leq sx, sy, tx, ty \leq 1,000$): points $s$ and $t$ are located at ($sx, sy$) and ($tx, ty$) respectively. The next line contains an integer $N$ ($2 \leq N \leq 20$), where $N$ is the number of points composing the west riverside. Each of the following $N$ lines contains two integers $wx_i$ and $wy_i$ ($0 \leq wx_i, wy_i \leq 1,000$): the coordinate of the $i$-th point of the west riverside is ($wx_i, wy_i$). The west riverside is a polygonal line obtained by connecting the segments between ($wx_i, wy_i$) and ($wx_{i+1}, wy_{i+1}$) for all $1 \leq i \leq N -1$. The next line contains an integer $M$ ($2 \leq M \leq 20$), where $M$ is the number of points composing the east riverside. Each of the following $M$ lines contains two integers $ex_i$ and $ey_i$ ($0 \leq ex_i, ey_i \leq 1,000$): the coordinate of the $i$-th point of the east riverside is ($ex_i, ey_i$). The east riverside is a polygonal line obtained by connecting the segments between ($ex_i, ey_i$) and ($ex_{i+1}, ey_{i+1}$) for all $1 \leq i \leq M - 1$.
</p>
<p>
You can assume that test cases are under the following conditions.
</p>
<ul>
<li> $wy_1$ and $ey_1$ must be 0, and $wy_N$ and $ey_M$ must be 1,000.</li>
<li> Each polygonal line has no self-intersection.</li>
<li> Two polygonal lines representing the west and the east riverside have no cross point.</li>
<li> A point $s$ must be on the west part of the city. More precisely, $s$ must be on the region surrounded by the square side of the city and the polygonal line of the west riverside and not containing the east riverside points.</li>
<li> A point $t$ must be on the east part of the city. More precisely, $t$ must be on the region surrounded by the square side of the city and the polygonal line of the east riverside and not containing the west riverside points.</li>
<li> Each polygonal line intersects with the square only at the two end points. In other words, $0 < wx_i, wy_i < 1,000$ holds for $2 \leq i \leq N - 1$ and $0 < ex_i, ey_i < 1,000$ holds for $2 \leq i \leq M - 1$.</li>
</ul>
<h3>Output</h3>
<p>
Output single-space separated two numbers in a line: the length of a bridge and the total length of a highway (i.e. a bridge and two roads) satisfying the above mayor's demand. The output can contain an absolute or a relative error no more than $10^{-8}$.
</p>
<h3>Sample Input 1</h3>
<pre>
200 500 800 500
3
400 0
450 500
400 1000
3
600 0
550 500
600 1000
</pre>
<h3>Output for the Sample Input 1</h3>
<pre>
100 600
</pre>
<h3>Sample Input 2</h3>
<pre>
300 300 700 100
5
300 0
400 100
300 200
400 300
400 1000
4
700 0
600 100
700 200
700 1000
</pre>
<h3>Output for the Sample Input 2</h3>
<pre>
200 541.421356237
</pre>
<h3>Sample Input 3</h3>
<pre>
300 400 700 600
2
400 0
400 1000
2
600 0
600 1000
</pre>
<h3>Output for the Sample Input 3</h3>
<pre>
200 482.842712475
</pre>
<h3>Sample Input 4</h3>
<pre>
200 500 800 500
3
400 0
450 500
400 1000
5
600 0
550 500
600 100
650 500
600 1000
</pre>
<h3>Output for the Sample Input 4</h3>
<pre>
100 1200.326482915
</pre>
|
p00724 |
<h1><font color="#000">Problem C:</font> Hexerpents of Hexwamp</h1>
<!-- end en only -->
<p>
Hexwamp is a strange swamp, paved with regular hexagonal dimples.
Hexerpents crawling in this area are serpents adapted to the
environment, consisting of a chain of regular hexagonal sections. Each
section fits in one dimple.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
Hexerpents crawl moving some of their sections from the dimples they
are in to adjacent ones. To avoid breaking their bodies, sections
that are adjacent to each other before the move should also be
adjacent after the move. When one section moves, sections adjacent to
it support the move, and thus they cannot move at that time. Any
number of sections, as far as no two of them are adjacent to each
other, can move at the same time.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
You can easily find that a hexerpent can move its sections at its
either end to only up to two dimples, and can move intermediate
sections to only one dimple, if any.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
For example, without any obstacles, a hexerpent can crawl forward
twisting its body as shown in Figure C-1, left to right. In this
figure, the serpent moves four of its eight sections at a time, and
moves its body forward by one dimple unit after four steps of moves.
Actually, they are much better in crawling sideways, like sidewinders.
</p>
<!-- end en only -->
<p>
</p><center>
<img class="center" src="IMAGE1/Forward.gif" border="1"><br>
<!-- begin en only -->
Figure C-1: Crawling forward
<!-- end en only -->
</center>
<!-- begin en only -->
<p>
Their skin is so sticky that if two sections of a serpent that are not
originally adjacent come to adjacent dimples (Figure C-2), they will
stick together and the serpent cannot but die. Two sections cannot
fit in one dimple, of course. This restricts serpents' moves
further. Sometimes, they have to make some efforts to get a food
piece even when it is in the dimple next to their head.
</p>
<!-- end en only -->
<p>
</p><center>
<img class="center" src="IMAGE1/Fatal.gif" border="1"><br>
<!-- begin en only -->
Figure C-2: Fatal case
<!-- end en only -->
</center>
<!-- begin en only -->
<p>
Hexwamp has rocks here and there. Each rock fits in a dimple.
Hexerpents' skin does not stick to rocks, but they cannot crawl over
the rocks. Although avoiding dimples with rocks restricts their
moves, they know the geography so well that they can plan the fastest
paths.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
You are appointed to take the responsibility of the head of the
scientist team to carry out academic research on this swamp and the
serpents. You are expected to accomplish the research, but never at
the sacrifice of any casualty. Your task now is to estimate how soon
a man-eating hexerpent may move its head (the first section) to the
position of a scientist in the swamp. Their body sections except for
the head are quite harmless and the scientist wearing high-tech
anti-sticking suit can stay in the same dimple with a body section of
the hexerpent.
</p>
<!-- end en only -->
<h2>Input</h2>
<!-- begin en only -->
<p>
The input is a sequence of several datasets, and the end of the input
is indicated by a line containing a single zero. The number of
datasets never exceeds 10.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
Each dataset looks like the following.
</p><blockquote>
<i>the number of sections the serpent has </i>(=<i>n</i>) <br>
<i>x</i><sub>1</sub> <i>y</i><sub>1</sub><br>
<i>x</i><sub>2</sub> <i>y</i><sub>2</sub><br>
...<br>
<i>x</i><sub><i>n</i></sub> <i>y</i><sub><i>n</i></sub><br>
<i>the number of rocks the swamp has (=k)</i> <br>
<i>u</i><sub>1</sub> <i>v</i><sub>1</sub><br>
<i>u</i><sub>2</sub> <i>v</i><sub>2</sub><br>
...<br>
<i>u<sub>k</sub></i> <i>v<sub>k</sub></i><br>
<i>X</i> <i>Y</i><br>
</blockquote>
<p>The first line of the dataset has an integer <i>n</i> that indicates
the number of sections the hexerpent has, which is 2 or greater and
never exceeds 8. Each of the <i>n</i> following lines contains two
integers <i>x</i> and <i>y</i> that indicate the coordinates of a
serpent's section. The lines show the initial positions of the
sections from the serpent's head to its tail, in this order.
<p>
The next line of the dataset indicates the number of rocks <i>k</i>
the swamp has, which is a non-negative integer not exceeding 100.
Each of the <i>k</i> following lines contains two integers <i>u</i>
and <i>v</i> that indicate the position of a rock.
</p><p>
Finally comes a line containing two integers <i>X</i> and <i>Y</i>,
indicating the goal position of the hexerpent, where the scientist is.
The serpent's head is not initially here.
</p><p>
All of the coordinates <i>x, y, u, v, X,</i> and <i>Y</i> are between
−999999 and 999999, inclusive. Two integers in a line are
separated by a single space. No characters other than decimal digits,
minus signs, and spaces to separate two integers appear in the input.
The coordinate system used to indicate a position is as shown in
Figure C-3.
</p>
<!-- end en only -->
<p>
</p><center>
<img class="center" src="IMAGE1/Coordinates.gif" border="1"><br>
<!-- begin en only -->
Figure C-3: The coordinate system
<!-- end en only -->
</center>
<h2>Output</h2>
<!-- begin en only -->
<p>
For each dataset, output a line that contains a decimal integer that
indicates the minimum number of steps the serpent requires for moving
its head to the goal position. Output lines should not contain any
other characters.
</p>
<!-- end en only -->
<!-- begin en only -->
<p>
You can assume that the hexerpent can reach the goal within 20 steps.
</p>
<!-- end en only -->
<h2>Sample Input</h2>
<pre>
3
2 -2
2 -1
1 0
1
0 2
0 0
4
2 -2
2 -1
2 0
3 0
2
1 -1
0 2
0 0
8
-6 0
-5 0
-4 0
-3 0
-2 0
-1 0
0 0
1 0
1
-1 1
0 0
6
2 -3
3 -3
3 -2
3 -1
3 0
2 1
3
1 -1
1 0
1 1
0 0
3
-8000 4996
-8000 4997
-8000 4998
2
-7999 4999
-8001 5000
-8000 5000
8
10 -8
9 -7
9 -6
9 -5
9 -4
9 -3
9 -2
9 -1
0
0 0
0
</pre>
<h2>Output for the Sample Input</h2>
<pre>
3
9
18
18
19
20
</pre>
|
p02719 | <span class="lang-en">
<p>Score : <var>300</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Given any integer <var>x</var>, Aoki can do the operation below.</p>
<p>Operation: Replace <var>x</var> with the absolute difference of <var>x</var> and <var>K</var>.</p>
<p>You are given the initial value of an integer <var>N</var>. Find the minimum possible value taken by <var>N</var> after Aoki does the operation zero or more times.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>0 ≤ N ≤ 10^{18}</var></li>
<li><var>1 ≤ K ≤ 10^{18}</var></li>
<li>All values in input are integers.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>N</var> <var>K</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the minimum possible value taken by <var>N</var> after Aoki does the operation zero or more times.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>7 4
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>1
</pre>
<p>Initially, <var>N=7</var>.</p>
<p>After one operation, <var>N</var> becomes <var>|7-4| = 3</var>.</p>
<p>After two operations, <var>N</var> becomes <var>|3-4| = 1</var>, which is the minimum value taken by <var>N</var>.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>2 6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>2
</pre>
<p><var>N=2</var> after zero operations is the minimum.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>1000000000000000000 1
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>0
</pre></section>
</div>
</span> |
p03558 | <span class="lang-en">
<p>Score : <var>700</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>Find the smallest possible sum of the digits in the decimal notation of a positive multiple of <var>K</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var>2 \leq K \leq 10^5</var></li>
<li><var>K</var> is an integer.</li>
</ul>
</section>
</div>
<hr/>
<div class="io-style">
<div class="part">
<section>
<h3>Input</h3><p>Input is given from Standard Input in the following format:</p>
<pre><var>K</var>
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Output</h3><p>Print the smallest possible sum of the digits in the decimal notation of a positive multiple of <var>K</var>.</p>
</section>
</div>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 1</h3><pre>6
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 1</h3><pre>3
</pre>
<p><var>12=6×2</var> yields the smallest sum.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 2</h3><pre>41
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 2</h3><pre>5
</pre>
<p><var>11111=41×271</var> yields the smallest sum.</p>
</section>
</div>
<hr/>
<div class="part">
<section>
<h3>Sample Input 3</h3><pre>79992
</pre>
</section>
</div>
<div class="part">
<section>
<h3>Sample Output 3</h3><pre>36
</pre></section>
</div>
</span> |
p01565 |
<h1>Move On Dice</h1>
<p>There is a cube on a rectangle map with <var>H</var>-rows and <var>W</var>-columns grid.
Two special squares a start and a goal are marked on the map.
Initially, the cube is on the start square. Let's repeat to roll it and take it to the goal square.
Rolling the cube means to select one of four edges which touch the map, and push the cube down without detaching the edge from the map.
That is, there are four directions you can move the cube toward.
</p>
<p>Directions where we can roll the cube are limited depending on each square.
An instruction is written in each square and represented by a single character as follows:
</p><dl>
<dt>'+'</dt><dd>all</dd>
<dt>'|'</dt><dd>only vertical</dd>
<dt>'-'</dt><dd>only horizontal</dd>
<dt>'<'</dt><dd>only to left</dd>
<dt>'>'</dt><dd>only to right</dd>
<dt>'^'</dt><dd>only to up</dd>
<dt>'v' </dt><dd>only to down</dd>
<dt>'.'</dt><dd>none</dd>
</dl>
<p>Regardless of instructions, it is not allowed to roll the cube to outside of the map.
</p>
<p>On each face of the cube, a string is written.
Let's output the string which concatenates strings written on the top face seen during the rollings from the start square to the goal square.
Since there may be multiple paths that take the cube to the goal square, choose the minimal string in ascending lexicographic order.
</p>
<p>Please note that there are cases where no path exists from the start to the goal, or the cases you can make the lexicographically minimal string infinitely longer.
</p>
<h2>Input</h2>
<p>A data set has following format:
</p>
<blockquote>
<var>H</var> <var>W</var><br>
<var>C<sub>11</sub></var> ... <var>C<sub>1W</sub></var><br>
...<br>
<var>C<sub>H1</sub></var> ... <var>C<sub>HW</sub></var><br>
<var>T<sub>1</sub></var><br>
...<br>
<var>T<sub>6</sub></var><br>
<var>R<sub>S</sub></var> <var>C<sub>S</sub></var><br>
<var>R<sub>D</sub></var> <var>C<sub>D</sub></var><br>
</blockquote>
<p>The first line of the input contains two integers <var>H</var> (<var>1 ≤ H ≤ 12</var>) and <var>W</var> (<var>1 ≤ W ≤ 12</var>), which indicate the number of rows and columns of the map respectively.
The following <var>W</var> lines describe the map.
The <var>j</var>-th character of the <var>i</var>-th line indicates the instruction of the square, which is placed on <var>i</var>-th row (from the top) and <var>j</var>-th column (from the left).
</p>
<p>Then the following 6 lines describe the strings on each face of the cube.
All of these strings are not empty and shorter than 12 characters (inclusive).
In addition, they only consist of uppercase alphabets or digits.
The faces where the strings are written are given as figure 1.
Initially, the cube is placed on the start square in a direction as the face No. 1 is facing top and the upper direction of face No. 1 faces toward the top row of the map.
</p>
<p><center><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE_dice" height="183" width="341"></center>
</p>
<p><center>Figure 1. a net of a cube</center>
</p>
<p>The last two lines contain two integers each that indicate the row number and column number of the start square and the goal square in this order.
You can assume that the start square and the goal square are always different.
</p>
<h2>Output</h2>
<p>Print the lexicographically minimal string in a line.
If there is no path, print "no" in a line.
If you can make the lexicographically minimal string infinitely longer, print "infinite" in a line.
</p>
<h2>Sample Input 1</h2>
<pre>1 3
+++
6
5
4
3
2
1
1 3
1 1
</pre>
<h2>Output for the Sample Input 1</h2>
<pre>621
</pre>
<h2>Sample Input 2</h2>
<pre>1 3
+++
1
2
3
4
5
6
1 3
1 1
</pre>
<h2>Output for the Sample Input 2</h2>
<pre>infinite
</pre>
<h2>Sample Input 3</h2>
<pre>1 3
...
1
2
3
4
5
6
1 3
1 1
</pre>
<h2>Output for the Sample Input 3</h2>
<pre>no
</pre>
<h2>Sample Input 4</h2>
<pre>3 3
->|
..v
.^<
JAG
2012
SUMMER
HOGE
HOGE
CAMP
1 1
2 2
</pre>
<h2>Output for the Sample Input 4</h2>
<pre>JAGSUMMERCAMP2012JAGSUMMER2012
</pre>
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.