実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
文字列が N 個与えられます。
i 番目 (1\leq i\leq N) に与えられる文字列 S _ i は Takahashi
か Aoki
のどちらかと等しいです。
S _ i が Takahashi
と等しい i がいくつあるか求めてください。
制約
- 1\leq N\leq 100
- N は整数
- S _ i は
Takahashi
かAoki
のいずれか (1\leq i\leq N)
入力
入力は以下の形式で標準入力から与えられる。
N S _ 1 S _ 2 \vdots S _ N
出力
S _ i が Takahashi
と等しい i の個数を整数として一行に出力せよ。
入力例 1
3 Aoki Takahashi Takahashi
出力例 1
2
S _ 2,S _ 3 の 2 つが Takahashi
と等しく、S _ 1 はそうではありません。
よって、2
を出力してください。
入力例 2
2 Aoki Aoki
出力例 2
0
Takahashi
と等しい S _ i が存在しないこともあります。
入力例 3
20 Aoki Takahashi Takahashi Aoki Aoki Aoki Aoki Takahashi Aoki Aoki Aoki Takahashi Takahashi Aoki Takahashi Aoki Aoki Aoki Aoki Takahashi
出力例 3
7
Score : 100 points
Problem Statement
You are given N strings.
The i-th string S_i (1 \leq i \leq N) is either Takahashi
or Aoki
.
How many i are there such that S_i is equal to Takahashi
?
Constraints
- 1 \leq N \leq 100
- N is an integer.
- Each S_i is
Takahashi
orAoki
. (1 \leq i \leq N)
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print the count of i such that S_i is equal to Takahashi
as an integer in a single line.
Sample Input 1
3 Aoki Takahashi Takahashi
Sample Output 1
2
S_2 and S_3 are equal to Takahashi
, while S_1 is not.
Therefore, print 2
.
Sample Input 2
2 Aoki Aoki
Sample Output 2
0
It is possible that no S_i is equal to Takahashi
.
Sample Input 3
20 Aoki Takahashi Takahashi Aoki Aoki Aoki Aoki Takahashi Aoki Aoki Aoki Takahashi Takahashi Aoki Takahashi Aoki Aoki Aoki Aoki Takahashi
Sample Output 3
7
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
鉄道の AtCoder 線には N 個の駅があり、それぞれ 1, 2, \ldots, N の番号が付けられています。
AtCoder 線では、駅 1 を始発駅として駅 2, 3, \ldots, N の順に各駅に停車する上り列車および、駅 N を始発駅として駅 N - 1, N - 2, \ldots, 1 の順に各駅に停車する下り列車が運行されています。
高橋君は AtCoder 線の上り列車あるいは下り列車の一方のみを使うことで駅 X から駅 Y まで移動しようとしています。
この移動の間に高橋君が乗っている電車が駅 Z に停車することがあるか判定してください。
制約
- 3 \leq N \leq 100
- 1 \leq X, Y, Z \leq N
- X, Y, Z は相異なる
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X Y Z
出力
駅 X から駅 Y への移動の間に高橋君が乗っている電車が駅 Z に停車することがあるならば Yes
を、そうでないならば No
を出力せよ。
入力例 1
7 6 1 3
出力例 1
Yes
駅 6 から駅 1 に移動するためには下り列車に乗車します。
駅 6 を出発し、駅 5, 4, 3, 2, 1 の順に停車するため移動の間に電車が駅 3 に停車することがあり、Yes
を出力します。
入力例 2
10 3 2 9
出力例 2
No
入力例 3
100 23 67 45
出力例 3
Yes
Score: 100 points
Problem Statement
The AtCoder railway line has N stations, numbered 1, 2, \ldots, N.
On this line, there are inbound trains that start at station 1 and stop at the stations 2, 3, \ldots, N in order, and outbound trains that start at station N and stop at the stations N - 1, N - 2, \ldots, 1 in order.
Takahashi is about to travel from station X to station Y using only one of the inbound and outbound trains.
Determine whether the train stops at station Z during this travel.
Constraints
- 3 \leq N \leq 100
- 1 \leq X, Y, Z \leq N
- X, Y, and Z are distinct.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X Y Z
Output
If the train stops at station Z during the travel from station X to station Y, print Yes
; otherwise, print No
.
Sample Input 1
7 6 1 3
Sample Output 1
Yes
To travel from station 6 to station 1, Takahashi will take an outbound train.
After departing from station 6, the train stops at stations 5, 4, 3, 2, 1 in order, which include station 3, so you should print Yes
.
Sample Input 2
10 3 2 9
Sample Output 2
No
Sample Input 3
100 23 67 45
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
人 1 、人 2 、\ldots 、人 N の N 人の人がルーレットの賭けに参加しました。 このルーレットの出目は、0 から 36 までの 37 個の整数のうちいずれかです。 i = 1, 2, \ldots, N について、人 i は 37 個の目のうち C_i 個の目 A_{i, 1}, A_{i, 2}, \ldots, A_{i, C_i} に賭けました。
ルーレットが回され、出目は X でした。 X に賭けた人たちのうち、賭けた目の個数が最も少ない人たちの番号を昇順にすべて出力してください。
より形式的には、1 以上 N 以下の整数 i であって、下記の 2 つの条件をともに満たすものを昇順にすべて出力してください。
- 人 i は X に賭けている。
- 任意の j = 1, 2, \ldots, N について「人 j が X に賭けているならば、C_i \leq C_j 」が成り立つ。
出力するべき番号が 1 つも無い場合もあることに注意してください(入力例2を参照)。
制約
- 1 \leq N \leq 100
- 1 \leq C_i \leq 37
- 0 \leq A_{i, j} \leq 36
- 任意の i = 1, 2, \ldots, N について、A_{i, 1}, A_{i, 2}, \ldots, A_{i, C_i} はすべて異なる。
- 0 \leq X \leq 36
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N C_1 A_{1, 1} A_{1, 2} \ldots A_{1, C_1} C_2 A_{2, 1} A_{2, 2} \ldots A_{2, C_2} \vdots C_N A_{N, 1} A_{N, 2} \ldots A_{N, C_N} X
出力
出力するべき番号を昇順にすベて並べた列を、B_1, B_2, \ldots, B_K とする。 下記の形式にしたがい、1 行目には出力するべき番号の個数 K を、 2 行目には B_1, B_2, \ldots, B_K を空白区切りで、それぞれ出力せよ。
K B_1 B_2 \ldots B_K
入力例 1
4 3 7 19 20 4 4 19 24 0 2 26 10 3 19 31 24 19
出力例 1
2 1 4
ルーレットが回され、出目は 19 でした。 19 に賭けた人は人 1 、人 2 、人 4 の 3 人であり、それぞれが賭けた目の個数は 3, 4, 3 です。 よって、19 に賭けた人のうち、賭けた目の個数が最も少ない人は人 1 と人 4 の 2 人です。
入力例 2
3 1 1 1 2 1 3 0
出力例 2
0
ルーレットが回され出目は 0 でしたが、0 に賭けた人は一人もいないため、 出力するべき番号は 1 つもありません。
Score : 200 points
Problem Statement
N people, person 1, person 2, \ldots, person N, are playing roulette. The outcome of a spin is one of the 37 integers from 0 to 36. For each i = 1, 2, \ldots, N, person i has bet on C_i of the 37 possible outcomes: A_{i, 1}, A_{i, 2}, \ldots, A_{i, C_i}.
The wheel has been spun, and the outcome is X. Print the numbers of all people who have bet on X with the fewest bets, in ascending order.
More formally, print all integers i between 1 and N, inclusive, that satisfy both of the following conditions, in ascending order:
- Person i has bet on X.
- For each j = 1, 2, \ldots, N, if person j has bet on X, then C_i \leq C_j.
Note that there may be no number to print (see Sample Input 2).
Constraints
- 1 \leq N \leq 100
- 1 \leq C_i \leq 37
- 0 \leq A_{i, j} \leq 36
- A_{i, 1}, A_{i, 2}, \ldots, A_{i, C_i} are all different for each i = 1, 2, \ldots, N.
- 0 \leq X \leq 36
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N C_1 A_{1, 1} A_{1, 2} \ldots A_{1, C_1} C_2 A_{2, 1} A_{2, 2} \ldots A_{2, C_2} \vdots C_N A_{N, 1} A_{N, 2} \ldots A_{N, C_N} X
Output
Let B_1, B_2, \ldots, B_K be the sequence of numbers to be printed in ascending order. Using the following format, print the count of numbers to be printed, K, on the first line, and B_1, B_2, \ldots, B_K separated by spaces on the second line:
K B_1 B_2 \ldots B_K
Sample Input 1
4 3 7 19 20 4 4 19 24 0 2 26 10 3 19 31 24 19
Sample Output 1
2 1 4
The wheel has been spun, and the outcome is 19. The people who has bet on 19 are person 1, person 2, and person 4, and the number of their bets are 3, 4, and 3, respectively. Therefore, among the people who has bet on 19, the ones with the fewest bets are person 1 and person 4.
Sample Input 2
3 1 1 1 2 1 3 0
Sample Output 2
0
The wheel has been spun and the outcome is 0, but no one has bet on 0, so there is no number to print.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
整数 N と長さ N の数列 A=(A _ 1,A _ 2,\ldots,A _ N) が与えられます。
クエリが Q 個与えられるので、与えられた順番に処理してください。 クエリは次の 2 種類のいずれかです。
1 k x
: A _ k の値を x に変更する。2 k
: A _ k の値を出力する。
制約
- 1 \leq N \leq 10 ^ 5
- 1 \leq Q \leq 10 ^ 5
- 0 \leq A _ i \leq 10 ^ 9\ (1\leq i\leq N)
- どのクエリについても、1\leq k\leq N
- 1 番目の形式のクエリについて、0\leq x\leq 10 ^ 9
- 2 番目の形式のクエリが 1 つ以上存在する
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A _ 1 A _ 2 \ldots A _ N Q \operatorname{query} _ 1 \operatorname{query} _ 2 \vdots \operatorname{query} _ Q
ただし、\operatorname{query} _ i は i 個目のクエリを表しており、次の形式のいずれかで与えられる。
1 k x
2 k
出力
2 番目の形式のクエリの回数を q 回として q 行出力せよ。 j\ (1\leq j\leq q) 行目には、2 番目の形式のクエリのうち j 個目のものに対する答えを出力せよ。
入力例 1
3 1 3 5 7 2 2 2 3 1 3 0 2 3 1 2 8 2 2 2 1
出力例 1
3 5 0 8 1
はじめ、A=(1,3,5) です。
- 1 つめのクエリにおいて、A=(1,3,5) です。A _ 2=3 なので、3 を出力します。
- 2 つめのクエリにおいて、A=(1,3,5) です。A _ 3=5 なので、5 を出力します。
- 3 つめのクエリでは、A _ 3 の値を 0 に変更し、A=(1,3,0) となります。
- 4 つめのクエリにおいて、A=(1,3,0) です。A _ 3=0 なので、0 を出力します。
- 5 つめのクエリでは、A _ 2 の値を 8 に変更し、A=(1,8,0) となります。
- 6 つめのクエリにおいて、A=(1,8,0) です。A _ 2=8 なので、8 を出力します。
- 7 つめのクエリにおいて、A=(1,8,0) です。A _ 1=1 なので、1 を出力します。
入力例 2
5 22 2 16 7 30 10 1 4 0 1 5 0 2 2 2 3 2 4 2 5 1 4 100 1 5 100 2 3 2 4
出力例 2
2 16 0 0 16 100
入力例 3
7 478 369 466 343 541 42 165 20 2 1 1 7 729 1 6 61 1 6 838 1 3 319 1 4 317 2 4 1 1 673 1 3 176 1 5 250 1 1 468 2 6 1 7 478 1 5 595 2 6 1 6 599 1 6 505 2 3 2 5 2 1
出力例 3
478 317 838 838 176 595 468
Score : 200 points
Problem Statement
You are given an integer N and a sequence A=(A _ 1,A _ 2,\ldots,A _ N) of length N.
Given Q queries, process them in the given order. Each query is of one of the following two kinds:
1 k x
: set the value A _ k to x.2 k
: print the value A _ k.
Constraints
- 1 \leq N \leq 10 ^ 5
- 1 \leq Q \leq 10 ^ 5
- 0 \leq A _ i \leq 10 ^ 9\ (1\leq i\leq N)
- 1\leq k\leq N for all queries.
- 0\leq x\leq 10 ^ 9 for all queries of the first kind.
- There is at least one query of the second kind.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N A _ 1 A _ 2 \ldots A _ N Q \operatorname{query} _ 1 \operatorname{query} _ 2 \vdots \operatorname{query} _ Q
Here, \operatorname{query} _ i denotes the i-th query, given in one of the following formats:
1 k x
2 k
Output
Print q lines, where q is the number of queries of the second kind. The j-th (1\leq j\leq q) line should contain the response to the j-th such query.
Sample Input 1
3 1 3 5 7 2 2 2 3 1 3 0 2 3 1 2 8 2 2 2 1
Sample Output 1
3 5 0 8 1
Initially, A=(1,3,5).
- For the 1-st query, A=(1,3,5), where A _ 2=3, so 3 should be printed.
- For the 2-nd query, A=(1,3,5), where A _ 3=5, so 5 should be printed.
- The 3-rd query sets the value A _ 3 to 0, making A=(1,3,0).
- For the 4-th query, A=(1,3,0), where A _ 3=0, so 0 should be printed.
- The 5-th query sets the value A _ 2 to 8, making A=(1,8,0).
- For the 6-th query, A=(1,8,0), where A _ 2=8, so 8 should be printed.
- For the 7-th query, A=(1,8,0), where A _ 1=1, so 1 should be printed.
Sample Input 2
5 22 2 16 7 30 10 1 4 0 1 5 0 2 2 2 3 2 4 2 5 1 4 100 1 5 100 2 3 2 4
Sample Output 2
2 16 0 0 16 100
Sample Input 3
7 478 369 466 343 541 42 165 20 2 1 1 7 729 1 6 61 1 6 838 1 3 319 1 4 317 2 4 1 1 673 1 3 176 1 5 250 1 1 468 2 6 1 7 478 1 5 595 2 6 1 6 599 1 6 505 2 3 2 5 2 1
Sample Output 3
478 317 838 838 176 595 468
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋くんは、プログラミングコンテストを主催することにしました。
コンテストは A 問題、B 問題、C 問題、D 問題、E 問題の 5 問からなり、それぞれの配点は a 点、b 点、c 点、d 点、e 点です。
コンテストには 31 人が参加し、全員が 1 問以上解きました。
より具体的には、文字列 ABCDE
の空でない(連続するとは限らない)部分列すべてについて、その部分列を名前とする参加者が存在し、その参加者は名前に含まれる文字に対応する問題をすべて解き、それ以外の問題は解きませんでした。
例えば、A さんは A 問題のみを、BCE さんは B 問題、C 問題、E 問題を解きました。
参加者の名前を、取った点数が大きいほうから順に出力してください。 ただし、参加者が取った点数は、その参加者が解いた問題の配点の合計です。
ただし、同じ点数を獲得した参加者については、名前が辞書順で小さいほうを先に出力してください。
辞書順で小さいとは?
辞書順とは、一言で説明すると「単語が辞書に載っている順番」を意味します。
より厳密には、英大文字からなる相異なる文字列 S,T について、S が T より辞書順で小さいとは、以下の条件のどちらかが成り立つことを意味します。
- S の長さ |S| が T の長さより短く、T の先頭 |S| 文字が S と一致する
- ある整数 1\leq i\leq\min\lbrace|S|,|T|\rbrace が存在して、次の 2 つの条件を両方を満たす
- 1\leq j\lt i を満たすすべての整数 j に対して S の j 文字目と T の j 文字目が等しい
- S の i 文字目が T の i 文字目よりアルファベット順で小さい
例えば、S= AB
,T= ABC
とすると、ひとつめの条件が成り立つため S は T より小さいです。
また、S= ABD
,T= ACD
とすると、ふたつめの条件が i=2 で成り立つため S は T より小さいです。
制約
- 100\leq a\leq b\leq c\leq d\leq e\leq 2718
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
a b c d e
出力
31 行出力せよ。 i 行目 (1\leq i\leq 31) には、i 番目に高い得点を獲得した参加者の名前を出力せよ。 同じ得点を獲得した参加者については、それらのうち辞書順で小さい名前をもつ参加者を先に出力せよ。
入力例 1
400 500 600 700 800
出力例 1
ABCDE BCDE ACDE ABDE ABCE ABCD CDE BDE ADE BCE ACE BCD ABE ACD ABD ABC DE CE BE CD AE BD AD BC AC AB E D C B A
それぞれの参加者の得点は以下のようになります。
例えば、ADE さんと BCE さんは同じ得点を獲得していますが、ADE さんのほうが辞書順で小さい名前をもつため、ADE さんを先に出力してください。
入力例 2
800 800 900 900 1000
出力例 2
ABCDE ACDE BCDE ABCE ABDE ABCD CDE ACE ADE BCE BDE ABE ACD BCD ABC ABD CE DE AE BE CD AC AD BC BD AB E C D A B
入力例 3
128 256 512 1024 2048
出力例 3
ABCDE BCDE ACDE CDE ABDE BDE ADE DE ABCE BCE ACE CE ABE BE AE E ABCD BCD ACD CD ABD BD AD D ABC BC AC C AB B A
Score : 300 points
Problem Statement
Takahashi decided to hold a programming contest.
The contest consists of five problems: A, B, C, D, E, with scores a, b, c, d, e, respectively.
There are 31 participants, and all of them solved at least one problem.
More specifically, for every non-empty subsequence (not necessarily contiguous) of the string ABCDE
, there is a participant named after that subsequence who solved the problems corresponding to the letters in their name and did not solve the other problems.
For example, participant A solved only problem A, and participant BCE solved problems B, C, and E.
Print the names of the participants in order of their obtained scores, from the largest to the smallest. The score obtained by a participant is the sum of the scores of the problems they solved.
If two participants obtained the same score, print the one whose name is lexicographically smaller first.
What does "lexicographically smaller" mean?
In short, "lexicographically smaller" refers to the order in which words would appear in a dictionary.
More precisely, for distinct strings S,T consisting of uppercase English letters, S is lexicographically smaller than T if either of the following conditions holds:
- The length |S| of S is less than the length of T, and the first |S| characters of T match S.
- There exists an integer 1\leq i\leq\min\{ |S|,|T|\} that satisfy both of the following two conditions:
- For every integer j with 1\leq j\lt i, the j-th character of S equals the j-th character of T.
- The i-th character of S is alphabetically smaller than the i-th character of T.
For example, if S= AB
and T= ABC
, the first condition holds, so S is lexicographically smaller than T.
If S= ABD
and T= ACD
, the second condition holds for i=2, so S is lexicographically smaller than T.
Constraints
- 100\leq a\leq b\leq c\leq d\leq e\leq 2718
- All input values are integers.
Input
The input is given from Standard Input in the following format:
a b c d e
Output
Print 31 lines. The i-th line (1\leq i\leq 31) should contain the name of the participant who obtained the i-th highest score. If multiple participants have the same score, print them in lexicographical order.
Sample Input 1
400 500 600 700 800
Sample Output 1
ABCDE BCDE ACDE ABDE ABCE ABCD CDE BDE ADE BCE ACE BCD ABE ACD ABD ABC DE CE BE CD AE BD AD BC AC AB E D C B A
The score of each participant is as follows:
For example, ADE and BCE obtained the same score, and ADE is lexicographically smaller, so print ADE before BCE.
Sample Input 2
800 800 900 900 1000
Sample Output 2
ABCDE ACDE BCDE ABCE ABDE ABCD CDE ACE ADE BCE BDE ABE ACD BCD ABC ABD CE DE AE BE CD AC AD BC BD AB E C D A B
Sample Input 3
128 256 512 1024 2048
Sample Output 3
ABCDE BCDE ACDE CDE ABDE BDE ADE DE ABCE BCE ACE CE ABE BE AE E ABCD BCD ACD CD ABD BD AD D ABC BC AC C AB B A
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
正整数 D が与えられます。
非負整数 x,y に対する |x^2+y^2-D| の最小値を求めてください。
制約
- 1\leq D \leq 2\times 10^{12}
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
D
出力
答えを出力せよ。
入力例 1
21
出力例 1
1
x=4,y=2 のとき |x^2+y^2-D| = |16+4-21|=1 となります。
|x^2+y^2-D|=0 を満たすような非負整数 x,y は存在しないので、答えは 1 です。
入力例 2
998244353
出力例 2
0
入力例 3
264428617
出力例 3
32
Score : 300 points
Problem Statement
You are given a positive integer D.
Find the minimum value of |x^2+y^2-D| for non-negative integers x and y.
Constraints
- 1\leq D \leq 2\times 10^{12}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
D
Output
Print the answer.
Sample Input 1
21
Sample Output 1
1
For x=4 and y=2, we have |x^2+y^2-D| = |16+4-21|=1.
There are no non-negative integers x and y such that |x^2+y^2-D|=0, so the answer is 1.
Sample Input 2
998244353
Sample Output 2
0
Sample Input 3
264428617
Sample Output 3
32
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
N 次多項式 A(x)=A_Nx^N+A_{N-1}x^{N-1}+\cdots +A_1x+A_0 と
M 次多項式 B(x)=B_Mx^M+B_{M-1}x^{M-1}+\cdots +B_1x+B_0 があります。
ここで、A(x), B(x) の各係数は絶対値が 100 以下の整数であり、最高次の係数は 0 ではありません。
また、それらの積を C(x)=A(x)B(x)=C_{N+M}x^{N+M}+C_{N+M-1}x^{N+M-1}+\cdots +C_1x+C_0 とします。
A_0,A_1,\ldots, A_N および C_0,C_1,\ldots, C_{N+M} が与えられるので、B_0,B_1,\ldots, B_M を求めてください。
ただし、与えられる入力に対して、条件をみたす B_0,B_1,\ldots, B_M がただ一つ存在することが保証されます。
制約
- 1 \leq N < 100
- 1 \leq M < 100
- |A_i| \leq 100
- |C_i| \leq 10^6
- A_N \neq 0
- C_{N+M} \neq 0
- 条件をみたす B_0,B_1,\ldots, B_M がただ一つ存在する。
入力
入力は以下の形式で標準入力から与えられる。
N M A_0 A_1 \ldots A_{N-1} A_N C_0 C_1 \ldots C_{N+M-1} C_{N+M}
出力
M+1 個の整数 B_0,B_1,\ldots, B_M を空白区切りで一行に出力せよ。
入力例 1
1 2 2 1 12 14 8 2
出力例 1
6 4 2
A(x)=x+2, B(x)=2x^2+4x+6 のとき、C(x)=A(x)B(x)=(x+2)(2x^2+4x+6)=2x^3+8x^2+14x+12 であるので、 B(x)=2x^2+4x+6 が条件をみたします。 よって、B_0=6, B_1=4, B_2=2 をこの順に空白区切りで出力します。
入力例 2
1 1 100 1 10000 0 -1
出力例 2
100 -1
A(x)=x+100, C(x)=-x^2+10000 であり、B(x)=-x+100 が条件をみたします。 よって、100, -1 をこの順に空白区切りで出力します。
Score : 400 points
Problem Statement
We have a polynomial of degree N, A(x)=A_Nx^N+A_{N-1}x^{N-1}+\cdots +A_1x+A_0,
and another of degree M, B(x)=B_Mx^M+B_{M-1}x^{M-1}+\cdots +B_1x+B_0.
Here, each coefficient of A(x) and B(x) is an integer whose absolute value is at most 100, and the leading coefficients are not 0.
Also, let the product of them be C(x)=A(x)B(x)=C_{N+M}x^{N+M}+C_{N+M-1}x^{N+M-1}+\cdots +C_1x+C_0.
Given A_0,A_1,\ldots, A_N and C_0,C_1,\ldots, C_{N+M}, find B_0,B_1,\ldots, B_M.
Here, the given inputs guarantee that there is a unique sequence B_0, B_1, \ldots, B_M that satisfies the given conditions.
Constraints
- 1 \leq N < 100
- 1 \leq M < 100
- |A_i| \leq 100
- |C_i| \leq 10^6
- A_N \neq 0
- C_{N+M} \neq 0
- There is a unique sequence B_0, B_1, \ldots, B_M that satisfies the conditions given in the statement.
Input
Input is given from Standard Input in the following format:
N M A_0 A_1 \ldots A_{N-1} A_N C_0 C_1 \ldots C_{N+M-1} C_{N+M}
Output
Print the M+1 integers B_0,B_1,\ldots, B_M in one line, with spaces in between.
Sample Input 1
1 2 2 1 12 14 8 2
Sample Output 1
6 4 2
For A(x)=x+2 and B(x)=2x^2+4x+6, we have C(x)=A(x)B(x)=(x+2)(2x^2+4x+6)=2x^3+8x^2+14x+12, so B(x)=2x^2+4x+6 satisfies the given conditions. Thus, B_0=6, B_1=4, B_2=2 should be printed in this order, with spaces in between.
Sample Input 2
1 1 100 1 10000 0 -1
Sample Output 2
100 -1
We have A(x)=x+100, C(x)=-x^2+10000, for which B(x)=-x+100 satisfies the given conditions. Thus, 100, -1 should be printed in this order, with spaces in between.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 個の頂点と M 本の辺からなる、単純(自己ループおよび多重辺を含まない)かつ連結な無向グラフが与えられます。
i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結ぶ無向辺です。
各頂点を白または黒で塗る方法であって、下記の 2 つの条件をともに満たすものが存在するかを判定し、存在する場合はその一例を示してください。
- 1 個以上の頂点が黒で塗られている。
- すべての i = 1, 2, \ldots, K について、下記の条件が成り立つ。
- 頂点 p_i と「黒で塗られた頂点のうち頂点 p_i からの距離が最小であるもの」の距離がちょうど d_i である。
ここで、頂点 u と頂点 v の距離は、u と v を結ぶパスの辺の本数としてあり得る最小値として定義されます。
制約
- 1 \leq N \leq 2000
- N-1 \leq M \leq \min\lbrace N(N-1)/2, 2000 \rbrace
- 1 \leq u_i, v_i \leq N
- 0 \leq K \leq N
- 1 \leq p_1 \lt p_2 \lt \cdots \lt p_K \leq N
- 0 \leq d_i \leq N
- 与えられるグラフは単純かつ連結
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 u_2 v_2 \vdots u_M v_M K p_1 d_1 p_2 d_2 \vdots p_K d_K
出力
問題文中の条件を満たすように各頂点を白または黒で塗る方法が存在しない場合は、No
を出力せよ。
存在する場合は、下記の形式の通り、1 行目に Yes
と出力し、2 行目に各頂点を塗る方法を表す文字列 S を出力せよ。
ここで、S は長さ N の文字列であり、i = 1, 2, \ldots, N について S の i 文字目は、頂点 i を黒で塗るとき 1 、白で塗るとき 0 である。
Yes S
答えが複数ある場合、どれを出力しても正解とみなされる。
入力例 1
5 5 1 2 2 3 3 1 3 4 4 5 2 1 0 5 2
出力例 1
Yes 10100
例えば、頂点 1, 3 を黒に、頂点 2, 4, 5 を白に塗るという方法が、問題文中の条件を満たします。
実際、i = 1, 2, 3, 4, 5 について、頂点 i と「黒で塗られた頂点のうち頂点 i からの距離が最小であるもの」の距離を A_i で表すと、
(A_1, A_2, A_3, A_4, A_5) = (0, 1, 0, 1, 2) であり、特に、A_1 = 0, A_5 = 2 が成り立ちます。
入力例 2
5 5 1 2 2 3 3 1 3 4 4 5 5 1 1 2 1 3 1 4 1 5 1
出力例 2
No
問題文中の条件を満たすように各頂点を白または黒で塗る方法が存在しないため、No
を出力します。
入力例 3
1 0 0
出力例 3
Yes 1
Score : 500 points
Problem Statement
You are given a simple connected undirected graph with N vertices and M edges (a simple graph contains no self-loop and no multi-edges).
For i = 1, 2, \ldots, M, the i-th edge connects vertex u_i and vertex v_i bidirectionally.
Determine whether there is a way to paint each vertex black or white to satisfy both of the following conditions, and show one such way if it exists.
- At least one vertex is painted black.
- For every i = 1, 2, \ldots, K, the following holds:
- the minimum distance between vertex p_i and a vertex painted black is exactly d_i.
Here, the distance between vertex u and vertex v is the minimum number of edges in a path connecting u and v.
Constraints
- 1 \leq N \leq 2000
- N-1 \leq M \leq \min\lbrace N(N-1)/2, 2000 \rbrace
- 1 \leq u_i, v_i \leq N
- 0 \leq K \leq N
- 1 \leq p_1 \lt p_2 \lt \cdots \lt p_K \leq N
- 0 \leq d_i \leq N
- The given graph is simple and connected.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M u_1 v_1 u_2 v_2 \vdots u_M v_M K p_1 d_1 p_2 d_2 \vdots p_K d_K
Output
If there is no way to paint each vertex black or white to satisfy the conditions, print No
.
Otherwise, print Yes
in the first line, and a string S representing a coloring of the vertices in the second line, as shown below.
Here, S is a string of length N such that, for each i = 1, 2, \ldots, N, the i-th character of S is 1 if vertex i is painted black and 0 if white.
Yes S
If multiple solutions exist, you may print any of them.
Sample Input 1
5 5 1 2 2 3 3 1 3 4 4 5 2 1 0 5 2
Sample Output 1
Yes 10100
One way to satisfy the conditions is to paint vertices 1, 3 black and vertices 2, 4, 5 white.
Indeed, for each i = 1, 2, 3, 4, 5, let A_i denote the minimum distance between vertex i and a vertex painted black, and we have (A_1, A_2, A_3, A_4, A_5) = (0, 1, 0, 1, 2), where A_1 = 0, A_5 = 2.
Sample Input 2
5 5 1 2 2 3 3 1 3 4 4 5 5 1 1 2 1 3 1 4 1 5 1
Sample Output 2
No
There is no way to satisfy the conditions by painting each vertex black or white, so you should print No
.
Sample Input 3
1 0 0
Sample Output 3
Yes 1
実行時間制限: 5 sec / メモリ制限: 1024 MiB
配点 : 575 点
問題文
円形のケーキがあり、ケーキは切り目によって N 個のピースに分けられています。各切り目は円の中心と円弧上の点を結ぶ線分です。
ピースおよび切り目には時計回りに 1, 2, \ldots, N の番号が付けられており、ピース i の質量は A_i です。ピース 1 をピース N + 1 とも呼ぶこととします。
切り目 i は ピース i とピース i + 1 の間にあり、ピース 1, 切り目 1, ピース 2, 切り目 2, \ldots, ピース N, 切り目 N の順に時計回りに並んでいます。
このケーキを以下の条件を満たすように K 人に分けようとしています。ただし、i 番目の人が受け取るピースの質量の合計を w_i とします。
- すべての人が 1 つ以上の連続するピースを受け取る
- 誰も受け取らないピースは存在しない
- 上の 2 つの条件を満たすという条件下で \min(w_1, w_2, \ldots, w_K) が最大になるようにする
条件を満たす分け方における \min(w_1, w_2, \ldots, w_K) の値および条件を満たすすべての分け方で切られることのない切り目の個数を求めてください。ただし、切り目 i が切られるとは、ピース i とピース i + 1 が異なる人に分けられることを指します。
制約
- 2 \leq K \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^4
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \ldots A_N
出力
条件を満たす分け方における \min(w_1, w_2, \ldots, w_K) の値を x、 切られることのない切り目の個数を y として、x と y をこの順に空白区切りで出力せよ。
入力例 1
5 2 3 6 8 6 4
出力例 1
13 1
以下の分け方が条件を満たします。
- 一方の人にピース 2, 3 を渡し、もう一方の人にピース 4, 5, 1 を渡す。ピース 2, 3 の質量の合計は 14、ピース 4, 5, 1 の質量の合計は 13 である。
- 一方の人にピース 3, 4 を渡し、もう一方の人にピース 5, 1, 2 を渡す。ピース 3, 4 の質量の合計は 14、ピース 5, 1, 2 の質量の合計は 13 である。
条件を満たす分け方における \min(w_1, w_2) の値は 13 であり、どちらの分け方でも切られない切り目は切り目 5 の 1 つです。
入力例 2
6 3 4 7 11 3 9 2
出力例 2
11 1
入力例 3
10 3 2 9 8 1 7 9 1 3 5 8
出力例 3
17 4
Score : 575 points
Problem Statement
There is a circular cake divided into N pieces by cut lines. Each cut line is a line segment connecting the center of the circle to a point on the arc.
The pieces and cut lines are numbered 1, 2, \ldots, N in clockwise order, and piece i has a mass of A_i. Piece 1 is also called piece N + 1.
Cut line i is between pieces i and i + 1, and they are arranged clockwise in this order: piece 1, cut line 1, piece 2, cut line 2, \ldots, piece N, cut line N.
We want to divide this cake among K people under the following conditions. Let w_i be the sum of the masses of the pieces received by the i-th person.
- Each person receives one or more consecutive pieces.
- There are no pieces that no one receives.
- Under the above two conditions, \min(w_1, w_2, \ldots, w_K) is maximized.
Find the value of \min(w_1, w_2, \ldots, w_K) in a division that satisfies the conditions, and the number of cut lines that are never cut in the divisions that satisfy the conditions. Here, cut line i is considered cut if pieces i and i + 1 are given to different people.
Constraints
- 2 \leq K \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^4
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \ldots A_N
Output
Let x be the value of \min(w_1, w_2, \ldots, w_K) in a division that satisfies the conditions, and y be the number of cut lines that are never cut. Print x and y in this order, separated by a space.
Sample Input 1
5 2 3 6 8 6 4
Sample Output 1
13 1
The following divisions satisfy the conditions:
- Give pieces 2, 3 to one person and pieces 4, 5, 1 to the other. Pieces 2, 3 have a total mass of 14, and pieces 4, 5, 1 have a total mass of 13.
- Give pieces 3, 4 to one person and pieces 5, 1, 2 to the other. Pieces 3, 4 have a total mass of 14, and pieces 5, 1, 2 have a total mass of 13.
The value of \min(w_1, w_2) in divisions satisfying the conditions is 13, and there is one cut line that is not cut in either division: cut line 5.
Sample Input 2
6 3 4 7 11 3 9 2
Sample Output 2
11 1
Sample Input 3
10 3 2 9 8 1 7 9 1 3 5 8
Sample Output 3
17 4