実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君はレベル N の赤い宝石を 1 個持っています。(他に宝石は持っていません。)
高橋君は次の操作を好きなだけ行うことができます。
- レベル n の赤い宝石 (n は 2 以上) を「レベル n-1 の赤い宝石 1 個と、レベル n の青い宝石 X 個」に変換する。
- レベル n の青い宝石 (n は 2 以上) を「レベル n-1 の赤い宝石 1 個と、レベル n-1 の青い宝石 Y 個」に変換する。
高橋君はレベル 1 の青い宝石ができるだけたくさんほしいです。操作によって高橋君はレベル 1 の青い宝石を最大何個入手できますか?
制約
- 1 \leq N \leq 10
- 1 \leq X \leq 5
- 1 \leq Y \leq 5
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X Y
出力
答えを出力せよ。
入力例 1
2 3 4
出力例 1
12
次のような変換を行うことで、高橋君はレベル 1 の青い宝石を 12 個手に入れることができます。
- まず、レベル 2 の赤い宝石 1 個を、レベル 1 の赤い宝石 1 個とレベル 2 の青い宝石 3 個に変換します。
- 操作後の高橋君は、レベル 1 の赤い宝石 1 個とレベル 2 の青い宝石 3 個を持っています。
- 次に、レベル 2 の青い宝石 1 個を、レベル 1 の赤い宝石 1 個とレベル 1 の青い宝石 4 個に変換します。この変換を 3 回繰り返します。
- 操作後の高橋君は、レベル 1 の赤い宝石 4 個とレベル 1 の青い宝石 12 個を持っています。
- これ以上変換を行うことはできません。
12 個より多くレベル 1 の青い宝石を手に入れることはできないので、答えは 12 になります。
入力例 2
1 5 5
出力例 2
0
高橋君がレベル 1 の青い宝石を 1 個も手に入れられない場合もあります。
入力例 3
10 5 5
出力例 3
3942349900
答えが 32 bit 整数に収まらない場合があることに注意してください。
Score : 300 points
Problem Statement
Takahashi has a red jewel of level N. (He has no other jewels.)
Takahashi can do the following operations any number of times.
- Convert a red jewel of level n (n is at least 2) into "a red jewel of level (n-1) and X blue jewels of level n".
- Convert a blue jewel of level n (n is at least 2) into "a red jewel of level (n-1) and Y blue jewels of level (n-1)".
Takahashi wants as many blue jewels of level 1 as possible. At most, how many blue jewels of level 1 can he obtain by the operations?
Constraints
- 1 \leq N \leq 10
- 1 \leq X \leq 5
- 1 \leq Y \leq 5
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X Y
Output
Print the answer.
Sample Input 1
2 3 4
Sample Output 1
12
Takahashi can obtain 12 blue jewels of level 1 by the following conversions.
- First, he converts a red jewel of level 2 into a red jewel of level 1 and 3 blue jewels of level 2.
- After this operation, Takahashi has 1 red jewel of level 1 and 3 blue jewels of level 2.
- Next, he repeats the following conversion 3 times: converting a blue jewel of level 2 into a red jewel of level 1 and 4 blue jewels of level 1.
- After these operations, Takahashi has 4 red jewels of level 1 and 12 blue jewels of level 1.
- He cannot perform a conversion anymore.
He cannot obtain more than 12 blue jewels of level 1, so the answer is 12.
Sample Input 2
1 5 5
Sample Output 2
0
Takahashi may not be able to obtain a blue jewel of level 1.
Sample Input 3
10 5 5
Sample Output 3
3942349900
Note that the answer may not fit into a 32-bit integer type.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
子供と大人があわせて N 人います。i 番目の人の体重は W_i です。
それぞれの人が子供か大人かは、0
と 1
からなる長さ N の文字列 S によって表され、
S の i 文字目が 0
であるとき i 番目の人が子供であることを、1
であるとき i 番目の人が大人であることをさします。
ロボットである高橋君に対して実数 X を設定すると、
高橋君はそれぞれの人に対して、体重が X 未満なら子供、X 以上なら大人と判定します。
実数 X に対してf(X) を、高橋君に X を設定したときに N 人のうち子供か大人かを正しく判定できる人数で定めます。
X が実数全体を動くとき、f(X) の最大値を求めてください。
制約
- 1\leq N\leq 2\times 10^5
- S は
0
と1
からなる長さ N の文字列 - 1\leq W_i\leq 10^9
- N,W_i は整数
入力
入力は以下の形式で標準入力から与えられる。
N S W_1 W_2 \ldots W_N
出力
f(X) の最大値を整数で一行に出力せよ。
入力例 1
5 10101 60 45 30 40 80
出力例 1
4
X=50 と設定すると、高橋君は 2,3,4 番目の人を子供、 1,5 番目の人を大人と判定します。
実際には 2,4 番目の人が子供、 1,3,5 番目の人が大人であるので、このとき、1,2,4,5 番目の合計 4 人に対して正しく判定できています。
よって、f(50)=4 です。
5 人全員に対して正しく判定できるような X は存在しないのでこのときが最大です。よって、4 を出力します。
入力例 2
3 000 1 2 3
出力例 2
3
例えば、X=10 とすると最大値 f(10)=3 を達成します。
全員が大人、または全員が子供である可能性もあることに注意してください。
入力例 3
5 10101 60 50 50 50 60
出力例 3
4
例えば、X=55 とすると最大値 f(55)=4 を達成します。
同じ体重の人が複数人存在する可能性もあることに注意してください。
Score : 300 points
Problem Statement
There are N people, each of whom is either a child or an adult. The i-th person has a weight of W_i.
Whether each person is a child or an adult is specified by a string S of length N consisting of 0
and 1
.
If the i-th character of S is 0
, then the i-th person is a child; if it is 1
, then the i-th person is an adult.
When Takahashi the robot is given a real number X,
Takahashi judges a person with a weight less than X to be a child and a person with a weight more than or equal to X to be an adult.
For a real value X, let f(X) be the number of people whom Takahashi correctly judges whether they are children or adults.
Find the maximum value of f(X) for all real values of X.
Constraints
- 1\leq N\leq 2\times 10^5
- S is a string of length N consisting of
0
and1
. - 1\leq W_i\leq 10^9
- N and W_i are integers.
Input
Input is given from Standard Input in the following format:
N S W_1 W_2 \ldots W_N
Output
Print the maximum value of f(X) as an integer in a single line.
Sample Input 1
5 10101 60 45 30 40 80
Sample Output 1
4
When Takahashi is given X=50, it judges the 2-nd, 3-rd, and 4-th people to be children and the 1-st and 5-th to be adults.
In reality, the 2-nd and 4-th are children, and the 1-st, 3-rd, and 5-th are adults, so the 1-st, 2-nd, 4-th, and 5-th people are correctly judged.
Thus, f(50)=4.
This is the maximum since there is no X that judges correctly for all 5 people. Thus, 4 should be printed.
Sample Input 2
3 000 1 2 3
Sample Output 2
3
For example, X=10 achieves the maximum value f(10)=3.
Note that the people may be all children or all adults.
Sample Input 3
5 10101 60 50 50 50 60
Sample Output 3
4
For example, X=55 achieves the maximum value f(55)=4.
Note that there may be multiple people with the same weight.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
長さ N の正整数列 A=(A_1,A_2,\dots,A_N) が与えられるので、以下の条件を満たす 1 以上 M 以下の整数 k を全て求めてください。
- 全ての 1 \le i \le N を満たす整数 i について、 \gcd(A_i,k)=1 である。
制約
- 入力は全て整数
- 1 \le N,M \le 10^5
- 1 \le A_i \le 10^5
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N
出力
1 行目に、出力する整数の数 x を出力せよ。
続く x 行に、答えとなる整数を小さい方から順に 1 行に 1 つずつ出力せよ。
入力例 1
3 12 6 1 5
出力例 1
3 1 7 11
例えば、 7 は \gcd(6,7)=1,\gcd(1,7)=1,\gcd(5,7)=1 を満たすので答えとなる整数の集合に含まれます。
一方、 9 は \gcd(6,9)=3 となるため、答えとなる整数の集合に含まれません。
条件を満たす 1 以上 12 以下の整数は 1,7,11 の 3 つです。これらを小さい方から出力することに注意してください。
Score : 400 points
Problem Statement
Given a sequence of N positive integers A=(A_1,A_2,\dots,A_N), find every integer k between 1 and M (inclusive) that satisfies the following condition:
- \gcd(A_i,k)=1 for every integer i such that 1 \le i \le N.
Constraints
- All values in input are integers.
- 1 \le N,M \le 10^5
- 1 \le A_i \le 10^5
Input
Input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_N
Output
In the first line, print x: the number of integers satisfying the requirement.
In the following x lines, print the integers satisfying the requirement, in ascending order, each in its own line.
Sample Input 1
3 12 6 1 5
Sample Output 1
3 1 7 11
For example, 7 has the properties \gcd(6,7)=1,\gcd(1,7)=1,\gcd(5,7)=1, so it is included in the set of integers satisfying the requirement.
On the other hand, 9 has the property \gcd(6,9)=3, so it is not included in that set.
We have three integers between 1 and 12 that satisfy the condition: 1, 7, and 11. Be sure to print them in ascending order.
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
H 行 W 列のグリッド状に分割されたフィールドがあります。
北 (上側) から i 行目、西 (左側) から j 列目のマスは文字 A_{i, j} で表されます。各文字の意味は次の通りです。
.
: 空きマス。進入できる。#
: 障害物。進入できない。>
,v
,<
,^
: それぞれ東・南・西・北を向いている人がいるマス。進入できない。人の視線は 1 マス分の幅を持ち、人が向いている方向にまっすぐ伸び、障害物や別の人に遮られる。(入出力例 1 にある説明も参考にしてください。)S
: スタート地点。進入できる。ちょうど 1 ヵ所だけ存在する。人の視線に入っていないことが保証される。G
: ゴール地点。進入できる。ちょうど 1 ヵ所だけ存在する。人の視線に入っていないことが保証される。
ナオヒロくんはスタート地点にいて、東西南北への 1 マス分の移動を好きな回数行えます。ただし、進入できないマスへの移動やフィールドの外への移動はできません。
彼が人の視線に一度も入らずにゴール地点に到達できるか判定して、できる場合はそのために最小で何回の移動が必要か求めてください。
制約
- 2 \leq H, W \leq 2000
- A_{i,j} は
.
,#
,>
,v
,<
,^
,S
,G
のいずれかである S
,G
は A_{i, j} の中にちょうど 1 回ずつ現れる- スタート地点・ゴール地点はともに人の視線に入っていない
入力
入力は以下の形式で標準入力から与えられる。
H W A_{1,1}A_{1,2}\dots A_{1,W} A_{2,1}A_{2,2}\dots A_{2,W} \vdots A_{H,1}A_{H,2}\dots A_{H,W}
出力
ナオヒロ君が人の視線に一度も入らずにゴール地点に到達できる場合は、そのために必要な(最小の)移動回数を出力せよ。できない場合は -1
を出力せよ。
入力例 1
5 7 ....Sv. .>..... ....... >..<.#< ^G....>
出力例 1
15
入力例 1 について、1 人以上の視線に入っている空きマスを !
で表すと次の図のようになります。
いくつかのマスについて具体的に説明すると次のようになります。(ここで、北から i 行目、西から j 列目のマスを (i, j) と表します。)
- (2, 4) は (2, 2) にいる東を向いている人からの視線に入っているマスである。
- (2, 6) は (2, 2) にいる東を向いている人と (1, 6) にいる南を向いている人の 2 人の視線に入っているマスである。
- (4, 5) は誰の視線にも入っていないマスである。(4, 7) にいる西を向いている人の視線は (4, 6) の障害物に遮られていて、(4, 1) にいる東を向いている人の視線は (4, 4) の人に遮られている。
ナオヒロ君は進入できないマス・視線に入っているマスのどちらも通らずにゴール地点へ行く必要があります。
入力例 2
4 3 S.. .<. .>. ..G
出力例 2
-1
ナオヒロ君がゴール地点に到達できない場合は -1
を出力してください。
Score : 425 points
Problem Statement
There is a field divided into a grid of H rows and W columns.
The square at the i-th row from the north (top) and the j-th column from the west (left) is represented by the character A_{i, j}. Each character represents the following.
.
: An empty square. Passable.#
: An obstacle. Impassable.>
,v
,<
,^
: Squares with a person facing east, south, west, and north, respectively. Impassable. The person's line of sight is one square wide and extends straight in the direction the person is facing, and is blocked by an obstacle or another person. (See also the description at Sample Input/Output 1.)S
: The starting point. Passable. There is exactly one starting point. It is guaranteed not to be in a person's line of sight.G
: The goal. Passable. There is exactly one goal. It is guaranteed not to be in a person's line of sight.
Naohiro is at the starting point and can move one square to the east, west, south, and north as many times as he wants. However, he cannot enter an impassable square or leave the field.
Determine if he can reach the goal without entering a person's line of sight, and if so, find the minimum number of moves required to do so.
Constraints
- 2 \leq H, W \leq 2000
- A_{i,j} is
.
,#
,>
,v
,<
,^
,S
, orG
. - Each of
S
andG
occurs exactly once among A_{i, j}. - Neither the starting point nor the goal is in a person's line of sight.
Input
The input is given from Standard Input in the following format:
H W A_{1,1}A_{1,2}\dots A_{1,W} A_{2,1}A_{2,2}\dots A_{2,W} \vdots A_{H,1}A_{H,2}\dots A_{H,W}
Output
If Naohiro can reach the goal without entering a person's line of sight, print the (minimum) number of moves required to do so. Otherwise, print -1
.
Sample Input 1
5 7 ....Sv. .>..... ....... >..<.#< ^G....>
Sample Output 1
15
For Sample Input 1, the following figure shows the empty squares that are in the lines of sight of one or more people as !
.
Let us describe some of the squares. (Let (i, j) denote the square in the i-th row from the north and the j-th column from the west.)
- (2, 4) is a square in the line of sight of the east-facing person at (2, 2).
- (2, 6) is a square in the lines of sight of two people, one facing east at (2, 2) and the other facing south at (1, 6).
- The square (4, 5) is not in anyone's line of sight. The line of sight of the west-facing person at (4, 7) is blocked by the obstacle at (4, 6), and the line of sight of the east-facing person at (4, 1) is blocked by the person at (4, 4).
Naohiro must reach the goal without passing through impassable squares or squares in a person's line of sight.
Sample Input 2
4 3 S.. .<. .>. ..G
Sample Output 2
-1
Print -1
if he cannot reach the goal.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
座標平面上に高橋君と荷物があります。
高橋君は現在 (X_A,Y_A) におり、荷物は (X_B,Y_B) にあります。 高橋君は荷物を (X_C,Y_C) まで運びたいです。
高橋君は (x,y) にいるとき、1 回の行動で次のいずれかの動きをすることができます。
- (x+1,y) に移動する。移動前の時点で荷物が (x+1,y) にあった時、荷物を (x+2,y) に移動させる。
- (x-1,y) に移動する。移動前の時点で荷物が (x-1,y) にあった時、荷物を (x-2,y) に移動させる。
- (x,y+1) に移動する。移動前の時点で荷物が (x,y+1) にあった時、荷物を (x,y+2) に移動させる。
- (x,y-1) に移動する。移動前の時点で荷物が (x,y-1) にあった時、荷物を (x,y-2) に移動させる。
荷物を (X_C,Y_C) に移動させるまでに必要な最小の行動回数を求めてください。
制約
- -10^{17}\leq X_A,Y_A,X_B,Y_B,X_C,Y_C\leq 10^{17}
- (X_A,Y_A)\neq (X_B,Y_B)
- (X_B,Y_B)\neq (X_C,Y_C)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
X_A Y_A X_B Y_B X_C Y_C
出力
荷物を (X_C,Y_C) に移動させるまでに必要な最小の行動回数を出力せよ。
入力例 1
1 2 3 3 0 5
出力例 1
9
高橋君は次のように行動することで 9 回で荷物を (0,5) に運ぶことができます。
- (2,2) へ移動する。
- (3,2) へ移動する。
- (3,3) へ移動する。荷物は (3,4) に移動する。
- (3,4) へ移動する。荷物は (3,5) に移動する。
- (4,4) へ移動する。
- (4,5) へ移動する。
- (3,5) へ移動する。荷物は (2,5) に移動する。
- (2,5) へ移動する。荷物は (1,5) に移動する。
- (1,5) へ移動する。荷物は (0,5) に移動する。
8 回以下で荷物を (0,5) に運ぶことができないので、9 を出力します。
入力例 2
0 0 1 0 -1 0
出力例 2
6
入力例 3
-100000000000000000 -100000000000000000 100000000000000000 100000000000000000 -100000000000000000 -100000000000000000
出力例 3
800000000000000003
Score : 525 points
Problem Statement
Takahashi and a cargo are on a coordinate plane.
Takahashi is currently at (X_A,Y_A), and the cargo is at (X_B,Y_B). He wants to move the cargo to (X_C,Y_C).
When he is at (x,y), he can make one of the following moves in a single action.
- Move to (x+1,y). If the cargo is at (x+1,y) before the move, move it to (x+2,y).
- Move to (x-1,y). If the cargo is at (x-1,y) before the move, move it to (x-2,y).
- Move to (x,y+1). If the cargo is at (x,y+1) before the move, move it to (x,y+2).
- Move to (x,y-1). If the cargo is at (x,y-1) before the move, move it to (x,y-2).
Find the minimum number of actions required to move the cargo to (X_C,Y_C).
Constraints
- -10^{17}\leq X_A,Y_A,X_B,Y_B,X_C,Y_C\leq 10^{17}
- (X_A,Y_A)\neq (X_B,Y_B)
- (X_B,Y_B)\neq (X_C,Y_C)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
X_A Y_A X_B Y_B X_C Y_C
Output
Print the minimum number of actions required to move the cargo to (X_C,Y_C).
Sample Input 1
1 2 3 3 0 5
Sample Output 1
9
Takahashi can move the cargo to (0,5) in nine actions as follows.
- Move to (2,2).
- Move to (3,2).
- Move to (3,3). The cargo moves to (3,4).
- Move to (3,4). The cargo moves to (3,5).
- Move to (4,4).
- Move to (4,5).
- Move to (3,5). The cargo moves to (2,5).
- Move to (2,5). The cargo moves to (1,5).
- Move to (1,5). The cargo moves to (0,5).
It is impossible to move the cargo to (0,5) in eight or fewer actions, so you should print 9.
Sample Input 2
0 0 1 0 -1 0
Sample Output 2
6
Sample Input 3
-100000000000000000 -100000000000000000 100000000000000000 100000000000000000 -100000000000000000 -100000000000000000
Sample Output 3
800000000000000003