Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
0
と 1
からなる長さ 16 の文字列 S が与えられます。
2 以上 16 以下のすべての偶数 i について S の i 文字目が 0
ならば Yes
を、
そうでないならば No
を出力してください。
制約
- S は
0
と1
からなる長さ 16 の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
2 以上 16 以下のすべての偶数 i について S の i 文字目が 0
ならば Yes
を、
そうでないならば No
を出力せよ。
入力例 1
1001000000001010
出力例 1
No
S= 1001000000001010
の 4 文字目が 1
であるため、No
を出力します。
入力例 2
1010100000101000
出力例 2
Yes
S= 1010100000101000
の偶数番目の文字はすべて 0
であるため、Yes
を出力します。
入力例 3
1111111111111111
出力例 3
No
S の偶数文字目はすべて 1
となっています。
特に「すべて 0
」ではないため、No
を出力します。
Score : 100 points
Problem Statement
You are given a string S of length 16 consisting of 0
and 1
.
If the i-th character of S is 0
for every even number i from 2 through 16, print Yes
; otherwise, print No
.
Constraints
- S is a string of length 16 consisting of
0
and1
.
Input
The input is given from Standard Input in the following format:
S
Output
If the i-th character of S is 0
for every even number i from 2 through 16, print Yes
; otherwise, print No
.
Sample Input 1
1001000000001010
Sample Output 1
No
The 4-th character of S= 1001000000001010
is 1
, so you should print No
.
Sample Input 2
1010100000101000
Sample Output 2
Yes
Every even-positioned character in S= 1010100000101000
is 0
, so you should print Yes
.
Sample Input 3
1111111111111111
Sample Output 3
No
Every even-positioned character in S is 1
.
Particularly, they are not all 0
, so you should print No
.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
長さ N の数列 A=(A_1,A_2,\ldots,A_N) および正整数 P,Q,R,S が与えられます。
ここで、P,Q,R,S は、1\leq P\leq Q<R\leq S \leq N および Q-P=S-R をみたしています。
数列 A の P 番目から Q 番目の項までと R 番目から S 番目の項までを入れ替えた数列を B=(B_1, B_2,\ldots, B_N) とします。
数列 B を出力してください。
制約
- 1\leq N \leq 100
- 1\leq A_i\leq 100
- 1\leq P\leq Q<R\leq S \leq N
- Q-P=S-R
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N P Q R S A_1 A_2 \ldots A_N
出力
B_1, B_2,\ldots, B_N を空白区切りで出力せよ。
入力例 1
8 1 3 5 7 1 2 3 4 5 6 7 8
出力例 1
5 6 7 4 1 2 3 8
数列 A=(1,2,3,4,5,6,7,8) の 1 番目から 3 番目の項 (1,2,3) と 5 番目から 7 番目までの項 (5,6,7) を 入れ替えると, B=(5,6,7,4,1,2,3,8) となります。 よってこれを空白区切りで出力します。
入力例 2
5 2 3 4 5 2 2 1 1 1
出力例 2
2 1 1 2 1
数列には同じ整数が複数回現れる事もあります。
入力例 3
2 1 1 2 2 50 100
出力例 3
100 50
入力例 4
10 2 4 7 9 22 75 26 45 72 81 47 29 97 2
出力例 4
22 47 29 97 72 81 75 26 45 2
Score : 100 points
Problem Statement
You are given a sequence A=(A_1,A_2,\ldots,A_N) of length N and positive integers P,Q,R, and S.
Here, P,Q,R, and S satisfy 1\leq P\leq Q<R\leq S \leq N and Q-P=S-R.
Let B=(B_1, B_2,\ldots, B_N) be the sequence obtained by swapping the P-th through Q-th terms and the R-th through S-th terms of A.
Print the sequence B.
Constraints
- 1\leq N \leq 100
- 1\leq A_i\leq 100
- 1\leq P\leq Q<R\leq S \leq N
- Q-P=S-R
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N P Q R S A_1 A_2 \ldots A_N
Output
Print B_1, B_2,\ldots, B_N, with spaces in between.
Sample Input 1
8 1 3 5 7 1 2 3 4 5 6 7 8
Sample Output 1
5 6 7 4 1 2 3 8
Swapping the 1-st through 3-rd terms (1,2,3) and the 5-th through 7-th terms (5,6,7) of the sequence A=(1,2,3,4,5,6,7,8) results in B=(5,6,7,4,1,2,3,8), which should be printed with spaces in between.
Sample Input 2
5 2 3 4 5 2 2 1 1 1
Sample Output 2
2 1 1 2 1
The same integer may occur multiple times in the sequence.
Sample Input 3
2 1 1 2 2 50 100
Sample Output 3
100 50
Sample Input 4
10 2 4 7 9 22 75 26 45 72 81 47 29 97 2
Sample Output 4
22 47 29 97 72 81 75 26 45 2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
空の数列 A があります。クエリが Q 個与えられるので、与えられた順に処理してください。
クエリは次の 2 種類のいずれかです。
1 x
: A の末尾に x を追加する。2 k
: A の後ろから k 番目の値を求める。このクエリが与えられるとき、A の長さは k 以上であることが保証される。
制約
- 1 \leq Q \leq 100
- 1 種類目のクエリにおいて x は 1 \leq x \leq 10^9 を満たす整数
- 2 種類目のクエリにおいて k はその時点の数列 A の長さ以下の正の整数
入力
入力は以下の形式で標準入力から与えられる。
Q \mathrm{query}_1 \mathrm{query}_2 \vdots \mathrm{query}_Q
クエリは以下の 2 つのいずれかの形式である。
1 x
2 k
出力
2 種類目のクエリの個数を q として q 行出力せよ。
i 行目にはそのような i 回目のクエリに対する答えを出力せよ。
入力例 1
5 1 20 1 30 2 1 1 40 2 3
出力例 1
30 20
- 最初 A は空である。
- 1 番目のクエリにより A の末尾に 20 が追加され A=(20) となる。
- 2 番目のクエリにより A の末尾に 30 が追加され A=(20,30) となる。
- 3 番目のクエリの答えは A=(20,30) の後ろから 1 番目の値の 30 である。
- 4 番目のクエリにより A の末尾に 40 が追加され A=(20,30,40) となる。
- 5 番目のクエリの答えは A=(20,30,40) の後ろから 3 番目の値の 20 である。
Score: 200 points
Problem Statement
You have an empty sequence A. There are Q queries given, and you need to process them in the order they are given.
The queries are of the following two types:
1 x
: Append x to the end of A.2 k
: Find the k-th value from the end of A. It is guaranteed that the length of A is at least k when this query is given.
Constraints
- 1 \leq Q \leq 100
- In the first type of query, x is an integer satisfying 1 \leq x \leq 10^9.
- In the second type of query, k is a positive integer not greater than the current length of sequence A.
Input
The input is given from Standard Input in the following format:
Q \mathrm{query}_1 \mathrm{query}_2 \vdots \mathrm{query}_Q
Each query is in one of the following two formats:
1 x
2 k
Output
Print q lines, where q is the number of queries of the second type.
The i-th line should contain the answer to the i-th such query.
Sample Input 1
5 1 20 1 30 2 1 1 40 2 3
Sample Output 1
30 20
- Initially, A is empty.
- The first query appends 20 to the end of A, making A=(20).
- The second query appends 30 to the end of A, making A=(20,30).
- The answer to the third query is 30, which is the 1-st value from the end of A=(20,30).
- The fourth query appends 40 to the end of A, making A=(20,30,40).
- The answer to the fifth query is 20, which is the 3-rd value from the end of A=(20,30,40).
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
人 1, 人 2, \dots 人 N の N 人の人がいます。人 i の姓は s_i、名は t_i です。
N 人の人すべてにあだ名をつけることを考えます。人 i のあだ名 a_i は以下の条件を満たす必要があります。
- a_i は人 i の姓あるいは名と一致する。言い換えると、a_i = s_i または a_i = t_i の少なくとも一方が成り立つ。
- a_i は自分以外の人の姓および名のどちらとも一致しない。言い換えると、1 \leq j \leq N, i \neq j を満たすすべての整数 j について a_i \neq s_j かつ a_i \neq t_j が成り立つ。
N 人全員に条件を満たすあだ名をつけることは可能でしょうか。可能ならば Yes
を、そうでないならば No
を出力してください。
制約
- 2 \leq N \leq 100
- N は整数である。
- s_i,t_i は英小文字からなる 1 文字以上 10 文字以下の文字列である。
入力
入力は以下の形式で標準入力から与えられる。
N s_1 t_1 s_2 t_2 \vdots s_N t_N
出力
N 人すべてにあだ名をつけることが可能ならば Yes
を、そうでないならば No
を出力せよ。
入力例 1
3 tanaka taro tanaka jiro suzuki hanako
出力例 1
Yes
a_1 = taro
, a_2 = jiro
, a_3 = hanako
とすれば、これは問題文にあるあだ名の条件を満たしています。(a_3 は suzuki
でもよいです。)
ここで、a_1 = tanaka
とはできないことに注意してください。なぜならば 人 2 の姓 s_2 もまた tanaka
であるため、あだ名の条件の 2 つ目を満たさなくなるからです。
入力例 2
3 aaa bbb xxx aaa bbb yyy
出力例 2
No
問題文の条件を満たすあだ名のつけ方は存在しません。
入力例 3
2 tanaka taro tanaka taro
出力例 3
No
同姓同名である人の組が存在する場合もあります。
入力例 4
3 takahashi chokudai aoki kensho snu ke
出力例 4
Yes
a_1 = chokudai
, a_2 = kensho
, a_3 = ke
とすればよいです。
Score : 200 points
Problem Statement
There are N people numbered Person 1, Person 2, \dots, and Person N. Person i has a family name s_i and a given name t_i.
Consider giving a nickname to each of the N people. Person i's nickname a_i should satisfy all the conditions below.
- a_i coincides with Person i's family name or given name. In other words, a_i = s_i and/or a_i = t_i holds.
- a_i does not coincide with the family name and the given name of any other person. In other words, for all integer j such that 1 \leq j \leq N and i \neq j, it holds that a_i \neq s_j and a_i \neq t_j.
Is it possible to give nicknames to all the N people? If it is possible, print Yes
; otherwise, print No
.
Constraints
- 2 \leq N \leq 100
- N is an integer.
- s_i and t_i are strings of lengths between 1 and 10 (inclusive) consisting of lowercase English alphabets.
Input
Input is given from Standard Input in the following format:
N s_1 t_1 s_2 t_2 \vdots s_N t_N
Output
If it is possible to give nicknames to all the N people, print Yes
; otherwise, print No
.
Sample Input 1
3 tanaka taro tanaka jiro suzuki hanako
Sample Output 1
Yes
The following assignment satisfies the conditions of nicknames described in the Problem Statement: a_1 = taro
, a_2 = jiro
, a_3 = hanako
. (a_3 may be suzuki
, too.)
However, note that we cannot let a_1 = tanaka
, which violates the second condition of nicknames, since Person 2's family name s_2 is tanaka
too.
Sample Input 2
3 aaa bbb xxx aaa bbb yyy
Sample Output 2
No
There is no way to give nicknames satisfying the conditions in the Problem Statement.
Sample Input 3
2 tanaka taro tanaka taro
Sample Output 3
No
There may be a pair of people with the same family name and the same given name.
Sample Input 4
3 takahashi chokudai aoki kensho snu ke
Sample Output 4
Yes
We can let a_1 = chokudai
, a_2 = kensho
, and a_3 = ke
.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
1 から 2N の番号がついた 2N 人でじゃんけん大会をします。
大会は M ラウンドからなり、各ラウンドは、全ての人が 1 度ずつ参加するような 1 対 1 の N 試合からなります。
i=0,1,\ldots,M について、i ラウンド目の終了時点での順位を次のように決めます。
- i ラウンド目までの勝数が多い方が上位
- i ラウンド目までの勝数が同じときは、番号が小さい方が上位
また、i=1,\ldots,M について、i ラウンド目の各試合の組み合わせを次のように決めます。
- 各 k=1,2,\ldots,N について、i-1 ラウンド目終了時点の順位が 2k-1 位の人と 2k 位の人が試合をする
各試合では、対戦する 2 人がそれぞれ 1 度だけ手を出し、勝ち・負け・引き分けのいずれかの結果が発生します。
未来予知ができる高橋君は、人 i が j ラウンド目の試合で出す手が A_{i,j} であることを知っています。
A_{i,j} は G
, C
, P
のいずれかであり、それぞれグー、チョキ、パーを表します。
M ラウンド目終了時点の順位を求めてください。
じゃんけんのルール
じゃんけんの結果は、2 人の出した手に応じて次のように決まります。- 一方がグーで他方がチョキのとき、グーを出した人が勝ち、チョキを出した人は負け
- 一方がチョキで他方がパーのとき、チョキを出した人が勝ち、パーを出した人は負け
- 一方がパーで他方がグーのとき、パーを出した人が勝ち、グーを出した人は負け
- 両者が同じ手を出したとき、引き分け
制約
- 1 \leq N \leq 50
- 1 \leq M \leq 100
- A_{i,j} は
G
,C
,P
のいずれか
入力
入力は以下の形式で標準入力から与えられる。
N M A_{1,1}A_{1,2}\ldots A_{1,M} A_{2,1}A_{2,2}\ldots A_{2,M} \vdots A_{2N,1}A_{2N,2}\ldots A_{2N,M}
出力
2N 行出力せよ。
i 行目には、M ラウンド目終了時点での順位が i 位である人の番号を出力せよ。
入力例 1
2 3 GCP PPP CCC PPC
出力例 1
3 1 2 4
1 ラウンド目では人 1 と 2、3 と 4 がそれぞれ試合をし、前者の試合は人 2 が、後者の試合は人 3 が勝ちます。
2 ラウンド目では人 2 と 3、1 と 4 がそれぞれ試合をし、前者の試合は人 3 が、後者の試合は人 1 が勝ちます。
3 ラウンド目では人 3 と 1、2 と 4 がそれぞれ試合をし、前者の試合は人 3 が、後者の試合は人 4 が勝ちます。
よって最終的な順位は、上位から順に人 3,1,2,4 となります。
入力例 2
2 2 GC PG CG PP
出力例 2
1 2 3 4
1 ラウンド目では人 1 と 2、3 と 4 がそれぞれ試合をし、前者の試合は人 2 が、後者の試合は人 3 が勝ちます。
2 ラウンド目では人 2 と 3、1 と 4 がそれぞれ試合をし、前者の試合は引き分け、後者の試合は人 1 が勝ちます。
よって最終的な順位は、上位から順に人 1,2,3,4 となります。
Score : 300 points
Problem Statement
2N players, with ID numbers 1 through 2N, will participate in a rock-scissors-paper contest.
The contest has M rounds. Each round has N one-on-one matches, where each player plays in one of them.
For each i=0, 1, \ldots, M, the players' ranks at the end of the i-th round are determined as follows.
- A player with more wins in the first i rounds ranks higher.
- Ties are broken by ID numbers: a player with a smaller ID number ranks higher.
Additionally, for each i=1, \ldots, M, the matches in the i-th round are arranged as follows.
- For each k=1, 2, \ldots, N, a match is played between the players who rank (2k-1)-th and 2k-th at the end of the (i-1)-th round.
In each match, the two players play a hand just once, resulting in one player's win and the other's loss, or a draw.
Takahashi, who can foresee the future, knows that Player i will play A_{i, j} in their match in the j-th round, where A_{i,j} is G
, C
, or P
.
Here, G
stands for rock, C
stands for scissors, and P
stands for paper. (These derive from Japanese.)
Find the players' ranks at the end of the M-th round.
Rules of rock-scissors-paper
The result of a rock-scissors-paper match is determined as follows, based on the hands played by the two players.- If one player plays rock (G) and the other plays scissors (C), the player playing rock (G) wins.
- If one player plays scissors (C) and the other plays paper (P), the player playing scissors (C) wins.
- If one player plays paper (P) and the other plays rock (G), the player playing paper (P) wins.
- If the players play the same hand, the match is drawn.
Constraints
- 1 \leq N \leq 50
- 1 \leq M \leq 100
- A_{i,j} is
G
,C
, orP
.
Input
Input is given from Standard Input in the following format:
N M A_{1,1}A_{1,2}\ldots A_{1,M} A_{2,1}A_{2,2}\ldots A_{2,M} \vdots A_{2N,1}A_{2N,2}\ldots A_{2N,M}
Output
Print 2N lines.
The i-th line should contain the ID number of the player who ranks i-th at the end of the M-th round.
Sample Input 1
2 3 GCP PPP CCC PPC
Sample Output 1
3 1 2 4
In the first round, matches are played between Players 1 and 2, and between Players 3 and 4. Player 2 wins the former, and Player 3 wins the latter.
In the second round, matches are played between Players 2 and 3, and between Players 1 and 4. Player 3 wins the former, and Player 1 wins the latter.
In the third round, matches are played between Players 3 and 1, and between Players 2 and 4. Player 3 wins the former, and Player 4 wins the latter.
Therefore, in the end, the players are ranked in the following order: 3,1,2,4, from highest to lowest.
Sample Input 2
2 2 GC PG CG PP
Sample Output 2
1 2 3 4
In the first round, matches are played between Players 1 and 2, and between Players 3 and 4. Player 2 wins the former, and Player 3 wins the latter.
In the second round, matches are played between Players 2 and 3, and between Players 1 and 4. The former is drawn, and Player 1 wins the latter.
Therefore, in the end, the players are ranked in the following order: 1,2,3,4, from highest to lowest.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
1,2,\dots,N がちょうど 3 回ずつ現れる長さ 3N の数列 A=(A_1,A_2,\dots,A_{3N}) が与えられます。
i=1,2,\dots,N について、A の中にある i のうち真ん中にあるものの添字を f(i) と定めます。 1,2,\dots,N を f(i) の昇順に並べ替えてください。
f(i) の定義は厳密には以下の通りです。
- A_j = i を満たす j が j=\alpha,\beta,\gamma\ (\alpha < \beta < \gamma) であるとする。このとき、f(i) = \beta である。
制約
- 1\leq N \leq 10^5
- 1 \leq A_j \leq N
- i=1,2,\dots,N それぞれについて、A の中に i はちょうど 3 回現れる
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_{3N}
出力
1,2,\dots,N を f(i) の昇順に並べ替えてできる長さ N の数列を空白区切りで出力せよ。
入力例 1
3 1 1 3 2 3 2 2 3 1
出力例 1
1 3 2
- A の中にある 1 は A_1,A_2,A_9 なので、f(1) = 2 です。
- A の中にある 2 は A_4,A_6,A_7 なので、f(2) = 6 です。
- A の中にある 3 は A_3,A_5,A_8 なので、f(3) = 5 です。
よって、f(1) < f(3) < f(2) であるため 1,3,2 の順に出力します。
入力例 2
1 1 1 1
出力例 2
1
入力例 3
4 2 3 4 3 4 1 3 1 1 4 2 2
出力例 3
3 4 1 2
Score : 250 points
Problem Statement
You are given a sequence A=(A_1,A_2,\dots,A_{3N}) of length 3N where each of 1,2,\dots, and N occurs exactly three times.
For i=1,2,\dots,N, let f(i) be the index of the middle occurrence of i in A. Sort 1,2,\dots,N in ascending order of f(i).
Formally, f(i) is defined as follows.
- Suppose that those j such that A_j = i are j=\alpha,\beta,\gamma\ (\alpha < \beta < \gamma). Then, f(i) = \beta.
Constraints
- 1\leq N \leq 10^5
- 1 \leq A_j \leq N
- i occurs in A exactly three times, for each i=1,2,\dots,N.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_{3N}
Output
Print the sequence of length N obtained by sorting 1,2,\dots,N in ascending order of f(i), separated by spaces.
Sample Input 1
3 1 1 3 2 3 2 2 3 1
Sample Output 1
1 3 2
- 1 occurs in A at A_1,A_2,A_9, so f(1) = 2.
- 2 occurs in A at A_4,A_6,A_7, so f(2) = 6.
- 3 occurs in A at A_3,A_5,A_8, so f(3) = 5.
Thus, f(1) < f(3) < f(2), so 1,3, and 2 should be printed in this order.
Sample Input 2
1 1 1 1
Sample Output 2
1
Sample Input 3
4 2 3 4 3 4 1 3 1 1 4 2 2
Sample Output 3
3 4 1 2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
H 行 W 列の 2 つのグリッド A, B が与えられます。
1 \leq i \leq H と 1 \leq j \leq W を満たす各整数の組 (i, j) について、 i 行目 j 列目にあるマスをマス (i, j) と呼ぶとき、 グリッド A の マス (i, j) には整数 A_{i, j} が、 グリッド B の マス (i, j) には整数 B_{i, j} がそれぞれ書かれています。
あなたは「下記の 2 つのうちのどちらか 1 つを行う」という操作を好きな回数( 0 回でもよい)だけ繰り返します。
- 1 \leq i \leq H-1 を満たす整数 i を選び、グリッド A の i 行目と (i+1) 行目を入れ替える。
- 1 \leq i \leq W-1 を満たす整数 i を選び、グリッド A の i 列目と (i+1) 列目を入れ替える。
上記の操作の繰り返しによって、グリッド A をグリッド B に一致させることが可能かどうかを判定してください。 さらに、一致させることが可能な場合は、そのために行う操作回数の最小値を出力してください。
ここで、グリッド A とグリッド B が一致しているとは、 1 \leq i \leq H と 1 \leq j \leq W を満たす全ての整数の組 (i, j) について、 グリッド A の マス (i, j) とグリッド B の マス (i, j) に書かれた整数が等しいこととします。
制約
- 入力される値は全て整数
- 2 \leq H, W \leq 5
- 1 \leq A_{i, j}, B_{i, j} \leq 10^9
入力
入力は以下の形式で標準入力から与えられる。
H W A_{1, 1} A_{1, 2} \cdots A_{1, W} A_{2, 1} A_{2, 2} \cdots A_{2, W} \vdots A_{H, 1} A_{H, 2} \cdots A_{H, W} B_{1, 1} B_{1, 2} \cdots B_{1, W} B_{2, 1} B_{2, 2} \cdots B_{2, W} \vdots B_{H, 1} B_{H, 2} \cdots B_{H, W}
出力
グリッド A をグリッド B に一致させることが不可能な場合は -1
を、
可能な場合はグリッド A をグリッド B に一致させるために行う操作回数の最小値を出力せよ。
入力例 1
4 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 3 2 5 4 11 13 12 15 14 6 8 7 10 9 16 18 17 20 19
出力例 1
3
初期状態のグリッド A の 4 列目と 5 列目を入れ替えると、グリッド A は下記の通りになります。
1 2 3 5 4 6 7 8 10 9 11 12 13 15 14 16 17 18 20 19
続けて、グリッド A の 2 行目と 3 行目を入れ替えると、グリッド A は下記の通りになります。
1 2 3 5 4 11 12 13 15 14 6 7 8 10 9 16 17 18 20 19
最後に、グリッド A の 2 列目と 3 列目を入れ替えると、グリッド A は下記の通りになり、グリッド B に一致します。
1 3 2 5 4 11 13 12 15 14 6 8 7 10 9 16 18 17 20 19
上に述べた 3 回の操作でグリッド A をグリッド B に一致させることができ、 これより少ない回数の操作でグリッド A をグリッド B に一致させることはできないため、 3 を出力します。
入力例 2
2 2 1 1 1 1 1 1 1 1000000000
出力例 2
-1
問題文中の操作をどのように行ってもグリッド A をグリッド B に一致させることは不可能であるため -1
を出力します。
入力例 3
3 3 8 1 6 3 5 7 4 9 2 8 1 6 3 5 7 4 9 2
出力例 3
0
グリッド A ははじめからグリッド B に一致しています。
入力例 4
5 5 710511029 136397527 763027379 644706927 447672230 979861204 57882493 442931589 951053644 152300688 43971370 126515475 962139996 541282303 834022578 312523039 506696497 664922712 414720753 304621362 325269832 191410838 286751784 732741849 806602693 806602693 732741849 286751784 191410838 325269832 304621362 414720753 664922712 506696497 312523039 834022578 541282303 962139996 126515475 43971370 152300688 951053644 442931589 57882493 979861204 447672230 644706927 763027379 136397527 710511029
出力例 4
20
Score : 425 points
Problem Statement
You are given two grids, A and B, each with H rows and W columns.
For each pair of integers (i, j) satisfying 1 \leq i \leq H and 1 \leq j \leq W, let (i, j) denote the cell in the i-th row and j-th column. In grid A, cell (i, j) contains the integer A_{i, j}. In grid B, cell (i, j) contains the integer B_{i, j}.
You will repeat the following operation any number of times, possibly zero. In each operation, you perform one of the following:
- Choose an integer i satisfying 1 \leq i \leq H-1 and swap the i-th and (i+1)-th rows in grid A.
- Choose an integer i satisfying 1 \leq i \leq W-1 and swap the i-th and (i+1)-th columns in grid A.
Determine whether it is possible to make grid A identical to grid B by repeating the above operation. If it is possible, print the minimum number of operations required to do so.
Here, grid A is identical to grid B if and only if, for all pairs of integers (i, j) satisfying 1 \leq i \leq H and 1 \leq j \leq W, the integer written in cell (i, j) of grid A is equal to the integer written in cell (i, j) of grid B.
Constraints
- All input values are integers.
- 2 \leq H, W \leq 5
- 1 \leq A_{i, j}, B_{i, j} \leq 10^9
Input
The input is given from Standard Input in the following format:
H W A_{1, 1} A_{1, 2} \cdots A_{1, W} A_{2, 1} A_{2, 2} \cdots A_{2, W} \vdots A_{H, 1} A_{H, 2} \cdots A_{H, W} B_{1, 1} B_{1, 2} \cdots B_{1, W} B_{2, 1} B_{2, 2} \cdots B_{2, W} \vdots B_{H, 1} B_{H, 2} \cdots B_{H, W}
Output
If it is impossible to make grid A identical to grid B, output -1
. Otherwise, print the minimum number of operations required to make grid A identical to grid B.
Sample Input 1
4 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 3 2 5 4 11 13 12 15 14 6 8 7 10 9 16 18 17 20 19
Sample Output 1
3
Swapping the fourth and fifth columns of the initial grid A yields the following grid:
1 2 3 5 4 6 7 8 10 9 11 12 13 15 14 16 17 18 20 19
Then, swapping the second and third rows yields the following grid:
1 2 3 5 4 11 12 13 15 14 6 7 8 10 9 16 17 18 20 19
Finally, swapping the second and third columns yields the following grid, which is identical to grid B:
1 3 2 5 4 11 13 12 15 14 6 8 7 10 9 16 18 17 20 19
You can make grid A identical to grid B with the three operations above and cannot do so with fewer operations, so print 3.
Sample Input 2
2 2 1 1 1 1 1 1 1 1000000000
Sample Output 2
-1
There is no way to perform the operation to make grid A match grid B, so print -1
.
Sample Input 3
3 3 8 1 6 3 5 7 4 9 2 8 1 6 3 5 7 4 9 2
Sample Output 3
0
Grid A is already identical to grid B at the beginning.
Sample Input 4
5 5 710511029 136397527 763027379 644706927 447672230 979861204 57882493 442931589 951053644 152300688 43971370 126515475 962139996 541282303 834022578 312523039 506696497 664922712 414720753 304621362 325269832 191410838 286751784 732741849 806602693 806602693 732741849 286751784 191410838 325269832 304621362 414720753 664922712 506696497 312523039 834022578 541282303 962139996 126515475 43971370 152300688 951053644 442931589 57882493 979861204 447672230 644706927 763027379 136397527 710511029
Sample Output 4
20
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
以下の条件を満たす正の整数 n を、 等差数 と呼びます。
- (n を先頭に余計な 0 を付けずに 10 進法で表記した際、) n の上から i 桁目を d_i とする。このとき、 n が k 桁の整数であったとすると、 (d_2-d_1)=(d_3-d_2)=\dots=(d_k-d_{k-1}) が成立する。
- この条件は、「 数列 (d_1,d_2,\dots,d_k) が等差数列である」と言い換えることができる。
- 但し、 n が 1 桁の整数である時、 n は等差数であるものとする。
たとえば、 234,369,86420,17,95,8,11,777 は等差数ですが、 751,919,2022,246810,2356 は等差数ではありません。
等差数のうち、 X 以上で最小のものを求めてください。
制約
- X は 1 以上 10^{17} 以下の整数である
入力
入力は以下の形式で標準入力から与えられる。
X
出力
答えを整数として出力せよ。
入力例 1
152
出力例 1
159
152 以上で最小の等差数は 159 です。
入力例 2
88
出力例 2
88
X 自身が等差数である場合もあります。
入力例 3
8989898989
出力例 3
9876543210
Score : 500 points
Problem Statement
Let us call a positive integer n that satisfies the following condition an arithmetic number.
- Let d_i be the i-th digit of n from the top (when n is written in base 10 without unnecessary leading zeros.) Then, (d_2-d_1)=(d_3-d_2)=\dots=(d_k-d_{k-1}) holds, where k is the number of digits in n.
- This condition can be rephrased into the sequence (d_1,d_2,\dots,d_k) being arithmetic.
- If n is a 1-digit integer, it is assumed to be an arithmetic number.
For example, 234,369,86420,17,95,8,11,777 are arithmetic numbers, while 751,919,2022,246810,2356 are not.
Find the smallest arithmetic number not less than X.
Constraints
- X is an integer between 1 and 10^{17} (inclusive).
Input
Input is given from Standard Input in the following format:
X
Output
Print the answer as an integer.
Sample Input 1
152
Sample Output 1
159
The smallest arithmetic number not less than 152 is 159.
Sample Input 2
88
Sample Output 2
88
X itself may be an arithmetic number.
Sample Input 3
8989898989
Sample Output 3
9876543210
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
各要素が 2 以上である長さ N の正整数列 A = (A_1, A_2, \dots ,A_N) が与えられます。Anna と Bruno はこれらの整数を用いてゲームをします。二人は、 Anna を先手として交互に以下の操作を行います。
- 整数 i \ (1 \leq i \leq N) を自由に選ぶ。A_i の正の約数であって A_i 自身でないものを自由に選び x とし、 A_i を x で置き換える。
操作が行えなくなった方が負けで、負けなかった方が勝ちです。両者が勝ちを目指して最適な行動を取るとき、どちらが勝つか判定してください。
制約
- 1 \leq N \leq 10^5
- 2 \leq A_i \leq 10^5
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \cdots A_N
出力
Anna がゲームに勝つ場合 Anna
と、 Bruno がゲームに勝つ場合 Bruno
と出力せよ。
入力例 1
3 2 3 4
出力例 1
Anna
例えば、ゲームの進行として以下のようなものが考えられます。必ずしも両者が最適な行動を取った例とは限らないことに注意してください。
- Anna が A_3 を 2 にする。
- Bruno が A_1 を 1 にする。
- Anna が A_2 を 1 にする。
- Bruno が A_3 を 1 にする。
- Anna のターンで操作ができないので Bruno の勝ちとなる。
実際には、このサンプルでは Anna が最適に行動すると常に Anna が勝つことができます。
入力例 2
4 2 3 4 6
出力例 2
Bruno
Score : 475 points
Problem Statement
You are given a sequence of N positive integers A = (A_1, A_2, \dots ,A_N), where each element is at least 2. Anna and Bruno play a game using these integers. They take turns, with Anna going first, performing the following operation.
- Choose an integer i \ (1 \leq i \leq N) freely. Then, freely choose a positive divisor x of A_i that is not A_i itself, and replace A_i with x.
The player who cannot perform the operation loses, and the other player wins. Determine who wins assuming both players play optimally for victory.
Constraints
- 1 \leq N \leq 10^5
- 2 \leq A_i \leq 10^5
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \cdots A_N
Output
Print Anna
if Anna wins the game, and Bruno
if Bruno wins.
Sample Input 1
3 2 3 4
Sample Output 1
Anna
For example, the game might proceed as follows. Note that this example may not necessarily represent optimal play by both players:
- Anna changes A_3 to 2.
- Bruno changes A_1 to 1.
- Anna changes A_2 to 1.
- Bruno changes A_3 to 1.
- Anna cannot operate on her turn, so Bruno wins.
Actually, for this sample, Anna always wins if she plays optimally.
Sample Input 2
4 2 3 4 6
Sample Output 2
Bruno