Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 100 点
問題文
N 個の整数の 2 つ組 (A_1, B_1), (A_2, B_2), \ldots, (A_N, B_N) が与えられます。 各 i = 1, 2, \ldots, N について、A_i + B_i を出力してください。
制約
- 1 \leq N \leq 1000
- -10^9 \leq A_i, B_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
N 行出力せよ。 i = 1, 2, \ldots, N について、i 行目には A_i+B_i を出力せよ。
入力例 1
4 3 5 2 -6 -5 0 314159265 123456789
出力例 1
8 -4 -5 437616054
- 1 行目には、A_1 + B_1 = 3 + 5 = 8 を、
- 2 行目には、A_2 + B_2 = 2 + (-6) = -4 を、
- 3 行目には、A_3 + B_3 = (-5) + 0 = -5 を、
- 4 行目には、A_4 + B_4 = 314159265 + 123456789 = 437616054 を出力します。
Score : 100 points
Problem Statement
You are given N pairs of integers: (A_1, B_1), (A_2, B_2), \ldots, (A_N, B_N). For each i = 1, 2, \ldots, N, print A_i + B_i.
Constraints
- 1 \leq N \leq 1000
- -10^9 \leq A_i, B_i \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N A_1 B_1 A_2 B_2 \vdots A_N B_N
Output
Print N lines. For i = 1, 2, \ldots, N, the i-th line should contain A_i+B_i.
Sample Input 1
4 3 5 2 -6 -5 0 314159265 123456789
Sample Output 1
8 -4 -5 437616054
- The first line should contain A_1 + B_1 = 3 + 5 = 8.
- The second line should contain A_2 + B_2 = 2 + (-6) = -4.
- The third line should contain A_3 + B_3 = (-5) + 0 = -5.
- The fourth line should contain A_4 + B_4 = 314159265 + 123456789 = 437616054.
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 200 点
問題文
N 人の人があるコンテストに参加し、i 位の人のハンドルネームは S_i でした。
上位 K 人のハンドルネームを辞書順に出力してください。
辞書順とは?
辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、相異なる文字列 S と文字列 T の大小を判定するアルゴリズムを以下に説明します。
以下では「 S の i 文字目の文字」を S_i のように表します。また、 S が T より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。
- S と T のうち長さが短い方の文字列の長さを L とします。i=1,2,\dots,L に対して S_i と T_i が一致するか調べます。
- S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_j と T_j を比較して、 S_j がアルファベット順で T_j より小さい場合は S \lt T 、大きい場合は S \gt T と決定して、アルゴリズムを終了します。
- S_i \neq T_i である i が存在しない場合、 S と T の長さを比較して、S が T より短い場合は S \lt T 、長い場合は S \gt T と決定して、アルゴリズムを終了します。
制約
- 1 \leq K \leq N \leq 100
- K, N は整数
- S_i は英小文字からなる長さ 10 以下の文字列
- i \neq j ならば S_i \neq S_j
入力
入力は以下の形式で標準入力から与えられる。
N K S_1 S_2 \vdots S_N
出力
答えを改行区切りで出力せよ。
入力例 1
5 3 abc aaaaa xyz a def
出力例 1
aaaaa abc xyz
このコンテストには 5 人が参加し、1 位の人のハンドルネームは abc
、2 位の人のハンドルネームは aaaaa
、3 位の人のハンドルネームは xyz
、4 位の人のハンドルネームは a
、5 位の人のハンドルネームは def
でした。
上位 3 人のハンドルネームは abc
、aaaaa
、xyz
であるため、これを辞書順に並べ替えて aaaaa
、abc
、xyz
の順に出力します。
入力例 2
4 4 z zyx zzz rbg
出力例 2
rbg z zyx zzz
入力例 3
3 1 abc arc agc
出力例 3
abc
Score : 200 points
Problem Statement
There were N participants in a contest. The participant ranked i-th had the nickname S_i.
Print the nicknames of the top K participants in lexicographical order.
What is lexicographical order?
Simply put, the lexicographical order is the order of words in a dictionary. As a formal description, below is an algorithm to order distinct strings S and T.
Let S_i denote the i-th character of a string S. We write S \lt T if S is lexicographically smaller than T, and S \gt T if S is larger.
- Let L be the length of the shorter of S and T. For i=1,2,\dots,L, check whether S_i equals T_i.
- If there is an i such that S_i \neq T_i, let j be the smallest such i. Compare S_j and T_j. If S_j is alphabetically smaller than T_j, we get S \lt T; if S_j is larger, we get S \gt T.
- If there is no i such that S_i \neq T_i, compare the lengths of S and T. If S is shorter than T, we get S \lt T; if S is longer, we get S \gt T.
Constraints
- 1 \leq K \leq N \leq 100
- K and N are integers.
- S_i is a string of length 10 consisting of lowercase English letters.
- S_i \neq S_j if i \neq j.
Input
The input is given from Standard Input in the following format:
N K S_1 S_2 \vdots S_N
Output
Print the nicknames, separated by newlines.
Sample Input 1
5 3 abc aaaaa xyz a def
Sample Output 1
aaaaa abc xyz
This contest had five participants. The participants ranked first, second, third, fourth, and fifth had the nicknames abc
, aaaaa
, xyz
, a
, and def
, respectively.
The nicknames of the top three participants were abc
, aaaaa
, xyz
, so print these in lexicographical order: aaaaa
, abc
, xyz
.
Sample Input 2
4 4 z zyx zzz rbg
Sample Output 2
rbg z zyx zzz
Sample Input 3
3 1 abc arc agc
Sample Output 3
abc
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 300 点
問題文
N 頂点 M 辺の単純無向グラフが与えられます。頂点には 1 から N の番号がついており、i 番目の辺は頂点 A_i と頂点 B_i を結んでいます。 このグラフから 0 本以上のいくつかの辺を削除してグラフが閉路を持たないようにするとき、削除する辺の本数の最小値を求めてください。
単純無向グラフとは
単純無向グラフとは、自己ループや多重辺を含まず、辺に向きの無いグラフのことをいいます。
閉路とは
単純無向グラフが閉路を持つとは、i \neq j ならば v_i \neq v_j を満たす長さ 3 以上の頂点列 (v_0, v_1, \ldots, v_{n-1}) であって、各 0 \leq i < n に対し v_i と v_{i+1 \bmod n} の間に辺が存在するものがあることをいいます。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq N
- 与えられるグラフは単純
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 A_2 B_2 \vdots A_M B_M
出力
答えを出力せよ。
入力例 1
6 7 1 2 1 3 2 3 4 2 6 5 4 6 4 5
出力例 1
2
頂点 1 と頂点 2 を結ぶ辺・頂点 4 と頂点 5 を結ぶ辺の 2 本を削除するなどの方法でグラフが閉路を持たないようにすることができます。
1 本以下の辺の削除でグラフが閉路を持たないようにすることはできないので、2 を出力します。
入力例 2
4 2 1 2 3 4
出力例 2
0
入力例 3
5 3 1 2 1 3 2 3
出力例 3
1
Score : 300 points
Problem Statement
You are given a simple undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects vertex A_i and vertex B_i. Let us delete zero or more edges to remove cycles from the graph. Find the minimum number of edges that must be deleted for this purpose.
What is a simple undirected graph?
A simple undirected graph is a graph without self-loops or multi-edges whose edges have no direction.
What is a cycle?
A cycle in a simple undirected graph is a sequence of vertices (v_0, v_1, \ldots, v_{n-1}) of length at least 3 satisfying v_i \neq v_j if i \neq j such that for each 0 \leq i < n, there is an edge between v_i and v_{i+1 \bmod n}.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq N
- The given graph is simple.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 A_2 B_2 \vdots A_M B_M
Output
Print the answer.
Sample Input 1
6 7 1 2 1 3 2 3 4 2 6 5 4 6 4 5
Sample Output 1
2
One way to remove cycles from the graph is to delete the two edges between vertex 1 and vertex 2 and between vertex 4 and vertex 5.
There is no way to remove cycles from the graph by deleting one or fewer edges, so you should print 2.
Sample Input 2
4 2 1 2 3 4
Sample Output 2
0
Sample Input 3
5 3 1 2 1 3 2 3
Sample Output 3
1
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 400 点
問題文
長さ N の整数列 A = (A_1, A_2, \ldots, A_N) と正整数 K が与えられます。
各 i = 1, 2, \ldots, Q について、A の連続部分列 (A_{l_i}, A_{l_i+1}, \ldots, A_{r_i}) が良い数列かどうかを判定してください。
ここで、長さ n の数列 X = (X_1, X_2, \ldots, X_n) は、下記の操作を好きな回数( 0 回でも良い)だけ行うことによって、X のすべての要素を 0 にすることができるとき、かつ、そのときに限り良い数列です。
1 \leq i \leq n-K+1 を満たす整数 i および、整数 c (負の数でも良い)を選び、K 個の要素 X_{i}, X_{i+1}, \ldots, X_{i+K-1} のそれぞれに c を加算する。
なお、すべての i = 1, 2, \ldots, Q について、r_i - l_i + 1 \geq K が保証されます。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq \min\lbrace 10, N \rbrace
- -10^9 \leq A_i \leq 10^9
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq l_i, r_i \leq N
- r_i-l_i+1 \geq K
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \ldots A_N Q l_1 r_1 l_2 r_2 \vdots l_Q r_Q
出力
Q 行出力せよ。
i = 1, 2, \ldots, Q について、i 行目には数列 (A_{l_i}, A_{l_i+1}, \ldots, A_{r_i}) が良い数列である場合は Yes
を、
そうでない場合は No
を出力せよ。
入力例 1
7 3 3 -1 1 -2 2 0 5 2 1 6 2 7
出力例 1
Yes No
数列 X \coloneqq (A_1, A_2, A_3, A_4, A_5, A_6) = (3, -1, 1, -2, 2, 0) は良い数列です。 実際、下記の手順で操作を行うことで、すべての要素を 0 にすることができます。
- まず、i = 2, c = 4 として操作を行う。その結果、X = (3, 3, 5, 2, 2, 0) となる。
- 次に、i = 3, c = -2 として操作を行う。その結果、X = (3, 3, 3, 0, 0, 0) となる。
- 最後に、i = 1, c = -3 として操作を行う。その結果、X = (0, 0, 0, 0, 0, 0) となる。
よって、1 行目には Yes
を出力します。
一方、数列 (A_2, A_3, A_4, A_5, A_6, A_7) = (-1, 1, -2, 2, 0, 5) は、
どのような手順で操作を行ってもすべての要素を 0 にすることはできないため、良い数列ではありません。
よって、2 行目には No
を出力します。
入力例 2
20 4 -19 -66 -99 16 18 33 32 28 26 11 12 0 -16 4 21 21 37 17 55 -19 5 13 16 4 11 3 12 13 18 4 10
出力例 2
No Yes No Yes No
Score : 400 points
Problem Statement
You are given an integer sequence of length N, A = (A_1, A_2, \ldots, A_N), and a positive integer K.
For each i = 1, 2, \ldots, Q, determine whether a contiguous subsequence of A, (A_{l_i}, A_{l_i+1}, \ldots, A_{r_i}), is a good sequence.
Here, a sequence of length n, X = (X_1, X_2, \ldots, X_n), is good if and only if there is a way to perform the operation below some number of times (possibly zero) to make all elements of X equal 0.
Choose an integer i such that 1 \leq i \leq n-K+1 and an integer c (possibly negative). Add c to each of the K elements X_{i}, X_{i+1}, \ldots, X_{i+K-1}.
It is guaranteed that r_i - l_i + 1 \geq K for every i = 1, 2, \ldots, Q.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq \min\lbrace 10, N \rbrace
- -10^9 \leq A_i \leq 10^9
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq l_i, r_i \leq N
- r_i-l_i+1 \geq K
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \ldots A_N Q l_1 r_1 l_2 r_2 \vdots l_Q r_Q
Output
Print Q lines.
For each i = 1, 2, \ldots, Q, the i-th line should contain Yes
if the sequence (A_{l_i}, A_{l_i+1}, \ldots, A_{r_i}) is good, and No
otherwise.
Sample Input 1
7 3 3 -1 1 -2 2 0 5 2 1 6 2 7
Sample Output 1
Yes No
The sequence X \coloneqq (A_1, A_2, A_3, A_4, A_5, A_6) = (3, -1, 1, -2, 2, 0) is good. Indeed, you can do the following to make all elements equal 0.
- First, do the operation with i = 2, c = 4, making X = (3, 3, 5, 2, 2, 0).
- Next, do the operation with i = 3, c = -2, making X = (3, 3, 3, 0, 0, 0).
- Finally, do the operation with i = 1, c = -3, making X = (0, 0, 0, 0, 0, 0).
Thus, the first line should contain Yes
.
On the other hand, for the sequence (A_2, A_3, A_4, A_5, A_6, A_7) = (-1, 1, -2, 2, 0, 5), there is no way to make all elements equal 0, so it is not a good sequence.
Thus, the second line should contain No
.
Sample Input 2
20 4 -19 -66 -99 16 18 33 32 28 26 11 12 0 -16 4 21 21 37 17 55 -19 5 13 16 4 11 3 12 13 18 4 10
Sample Output 2
No Yes No Yes No
Time Limit: 3 sec / Memory Limit: 1024 MB
配点 : 500 点
問題文
お店に N 個の商品が並んでおり、それらは商品 1 、商品 2 、\ldots 、商品 N と番号づけられています。
i = 1, 2, \ldots, N について、商品 i の定価は A_i 円です。また、各商品の在庫は 1 つです。
高橋君は、商品 X_1 、商品 X_2 、\ldots 、商品 X_M の M 個の商品が欲しいです。
高橋君は、欲しい商品をすべて手に入れるまで、下記の行動を繰り返します。
現在売れ残っている商品の個数を r とする。 1 \leq j \leq r を満たす整数 j を選び、現在売れ残っている商品のうち番号が j 番目に小さい商品を、その定価に C_j 円だけ加えた金額で購入する。
高橋君が欲しい商品をすべて手に入れるまでにかかる合計費用としてあり得る最小値を出力してください。
なお、高橋君は欲しい商品ではない商品を購入することもできます。
制約
- 1 \leq M \leq N \leq 5000
- 1 \leq A_i \leq 10^9
- 1 \leq C_i \leq 10^9
- 1 \leq X_1 \lt X_2 \lt \cdots \lt X_M \leq N
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \ldots A_N C_1 C_2 \ldots C_N X_1 X_2 \ldots X_M
出力
答えを出力せよ。
入力例 1
5 2 3 1 4 1 5 9 2 6 5 3 3 5
出力例 1
17
高橋君は下記の手順で行動することで、欲しい商品をすべて手に入れるまでにかかる合計費用を最小にすることができます。
- はじめ、商品 1, 2, 3, 4, 5 の 5 個の商品が売れ残っています。 高橋君は j = 5 を選び、売れ残っている商品のうち番号が 5 番目に小さい商品 5 を、A_5 + C_5 = 5 + 3 = 8 円で購入します。
- その後、商品 1, 2, 3, 4 の 4 個の商品が売れ残っています。 高橋君は j = 2 を選び、売れ残っている商品のうち番号が 2 番目に小さい商品 2 を、A_2 + C_2 = 1 + 2 = 3 円で購入します。
- その後、商品 1, 3, 4 の 3 個の商品が売れ残っています。 高橋君は j = 2 を選び、売れ残っている商品のうち番号が 2 番目に小さい商品 3 を、A_3 + C_2 = 4 + 2 = 6 円で購入します。
以上の手順によって、高橋君は欲しい商品である商品 3, 5 のすべて(および、欲しい商品ではない商品 2 )を手に入れることができ、 それまでにかかる合計費用は 8 + 3 + 6 = 17 円です。これが合計費用としてあり得る最小値です。
入力例 2
20 8 29 27 79 27 30 4 93 89 44 88 70 75 96 3 78 39 97 12 53 62 32 38 84 49 93 53 26 13 25 2 76 32 42 34 18 77 14 67 88 12 1 3 4 5 8 14 16 20
出力例 2
533
Score : 500 points
Problem Statement
There are N items in a shop, numbered as Item 1, Item 2, \ldots, Item N.
For each i = 1, 2, \ldots, N, the regular price of Item i is A_i yen. For each item, there is only one in stock.
Takahashi wants M items: Item X_1, Item X_2, \ldots, Item X_M.
He repeats the following until he gets all items he wants.
Let r be the number of unsold items now. Choose an integer j such that 1 \leq j \leq r, and buy the item with the j-th smallest item number among the unsold items, for its regular price plus C_j yen.
Print the smallest total amount of money needed to get all items Takahashi wants.
Takahashi may also buy items other than the ones he wants.
Constraints
- 1 \leq M \leq N \leq 5000
- 1 \leq A_i \leq 10^9
- 1 \leq C_i \leq 10^9
- 1 \leq X_1 \lt X_2 \lt \cdots \lt X_M \leq N
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \ldots A_N C_1 C_2 \ldots C_N X_1 X_2 \ldots X_M
Output
Print the answer.
Sample Input 1
5 2 3 1 4 1 5 9 2 6 5 3 3 5
Sample Output 1
17
Here is a way for Takahashi to get all items he wants with the smallest total amount of money.
- Initially, five items, Items 1, 2, 3, 4, 5, are remaining. Choose j = 5 to buy the item with the fifth smallest item number among the remaining, Item 5, for A_5 + C_5 = 5 + 3 = 8 yen.
- Then, four items, Items 1, 2, 3, 4, are remaining. Choose j = 2 to buy the item with the second smallest item number among the remaining, Item 2, for A_2 + C_2 = 1 + 2 = 3 yen.
- Then, three items, Items 1, 3, 4, are remaining. Choose j = 2 to buy the item with the second smallest item number among the remaining, Item 3, for A_3 + C_2 = 4 + 2 = 6 yen.
Now, Takahashi has all items he wants, Items 3 and 5, (along with Item 2, which is not wanted) for a total cost of 8 + 3 + 6 = 17 yen, the minimum possible.
Sample Input 2
20 8 29 27 79 27 30 4 93 89 44 88 70 75 96 3 78 39 97 12 53 62 32 38 84 49 93 53 26 13 25 2 76 32 42 34 18 77 14 67 88 12 1 3 4 5 8 14 16 20
Sample Output 2
533
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 500 点
問題文
10 進表記で N 桁の正整数 X が与えられます。X の各桁は 0 ではありません。
\lbrace 1,2, \ldots, N-1 \rbrace の部分集合 S に対し、f(S) を以下のように定義します。
X を 10 進表記したものを長さ N の文字列とみなし、i \in S のとき、またそのときに限り文字列の i 文字目と i + 1 文字目に区切りを入れることで |S| + 1 個の文字列に分解する。
このようにして得られた |S|+1 個の文字列を 10 進表記された整数とみなし、f(S) をこれら |S|+1 個の整数の積で定める。
S としてあり得るものは空集合を含めて 2^{N-1} 通りありますが、これら全てに対する f(S) の総和を 998244353 で割った余りを求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- X は 10 進表記で N 桁で、各桁は 0 でない
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X
出力
答えを出力せよ。
入力例 1
3 234
出力例 1
418
S = \emptyset とすると、f(S) = 234 です。
S = \lbrace 1 \rbrace とすると、f(S) = 2 \times 34 = 68 です。
S = \lbrace 2 \rbrace とすると、f(S) = 23 \times 4 = 92 です。
S = \lbrace 1, 2 \rbrace とすると、f(S) = 2 \times 3 \times 4 = 24 です。
234 + 68 + 92 + 24 = 418 であるため、418 を出力します。
入力例 2
4 5915
出力例 2
17800
入力例 3
9 998244353
出力例 3
258280134
Score : 500 points
Problem Statement
You are given a positive integer X with N digits in decimal representation. None of the digits of X is 0.
For a subset S of \lbrace 1,2, \ldots, N-1 \rbrace , let f(S) be defined as follows.
See the decimal representation of X as a string of length N, and decompose it into |S| + 1 strings by splitting it between the i-th and (i + 1)-th characters if and only if i \in S.
Then, see these |S| + 1 strings as integers in decimal representation, and let f(S) be the product of these |S| + 1 integers.
There are 2^{N-1} subsets of \lbrace 1,2, \ldots, N-1 \rbrace , including the empty set, which can be S. Find the sum of f(S) over all these S, modulo 998244353.
Constraints
- 2 \leq N \leq 2 \times 10^5
- X has N digits in decimal representation, none of which is 0.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N X
Output
Print the answer.
Sample Input 1
3 234
Sample Output 1
418
For S = \emptyset, we have f(S) = 234.
For S = \lbrace 1 \rbrace, we have f(S) = 2 \times 34 = 68.
For S = \lbrace 2 \rbrace, we have f(S) = 23 \times 4 = 92.
For S = \lbrace 1, 2 \rbrace, we have f(S) = 2 \times 3 \times 4 = 24.
Thus, you should print 234 + 68 + 92 + 24 = 418.
Sample Input 2
4 5915
Sample Output 2
17800
Sample Input 3
9 998244353
Sample Output 3
258280134
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 600 点
問題文
位置 0, 1, 2, \ldots, 3^N-1 にそれぞれ 0 個あるいは 1 個の爆弾があります。
また、位置 x と位置 y は i=0,1, \ldots, N-1 すべてに対し以下の条件を満たすとき、またそのときに限り近い位置であるとします。
- x, y を 3 進表記したときの 3^i の位の数字をそれぞれ x', y' として、|x' - y'| \leq 1 が成立する。
位置 i と近い位置にある爆弾の個数が A_i 個であるとわかっているとき、爆弾の配置としてありえるものを 1 つ出力してください。
制約
- 1 \leq N \leq 12
- A_0, A_1, \ldots, A_{3^N-1} に対応する爆弾の配置が存在する
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_0 A_1 \ldots A_{3^N-1}
出力
位置 i に爆弾がないとき B_i = 0 、位置 i に爆弾があるとき B_i = 1 として B_0, B_1, \ldots, B_{3^N-1} を空白区切りで出力せよ。
入力例 1
1 0 1 1
出力例 1
0 0 1
0 と近い位置は 0 と 1 で、位置 0 と位置 1 に爆弾は合計で 0 個あります。
1 と近い位置は 0 と 1 と 2 で、位置 0 と位置 1 と位置 2 に爆弾は合計で 1 個あります。
2 と近い位置は 1 と 2 で、位置 1 と位置 2 に爆弾は合計で 1 個あります。
2 にのみ爆弾があるような配置は上の条件を全て満たすため、正答となります。
入力例 2
2 2 3 2 4 5 3 3 4 2
出力例 2
0 1 0 1 0 1 1 1 0
入力例 3
2 0 0 0 0 0 0 0 0 0
出力例 3
0 0 0 0 0 0 0 0 0
Score : 600 points
Problem Statement
There is zero or one bomb at each of positions 0, 1, 2, \ldots, 3^N-1.
Let us say that positions x and y are neighboring each other if and only if the following condition is satisfied for every i=1, \ldots, N.
- Let x' and y' be the i-th lowest digits of x and y in ternary representation, respectively. Then, |x' - y'| \leq 1.
It is known that there are exactly A_i bombs in total at the positions neighboring position i. Print a placement of bombs consistent with this information.
Constraints
- 1 \leq N \leq 12
- There is a placement of bombs consistent with A_0, A_1, \ldots, A_{3^N-1}.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N A_0 A_1 \ldots A_{3^N-1}
Output
Print B_0, B_1, \ldots, B_{3^N-1} with spaces in between, where B_i = 0 if position i has no bomb and B_i = 1 if position i has a bomb.
Sample Input 1
1 0 1 1
Sample Output 1
0 0 1
Position 0 is neighboring positions 0 and 1, which have 0 bombs in total.
Position 1 is neighboring positions 0, 1, and 2, which have 1 bomb in total.
Position 2 is neighboring positions 1 and 2, which have 1 bombs in total.
If there is a bomb at only position 2, all conditions above are satisfied, so this placement is correct.
Sample Input 2
2 2 3 2 4 5 3 3 4 2
Sample Output 2
0 1 0 1 0 1 1 1 0
Sample Input 3
2 0 0 0 0 0 0 0 0 0
Sample Output 3
0 0 0 0 0 0 0 0 0
Time Limit: 3 sec / Memory Limit: 1024 MB
配点 : 600 点
問題文
下記の 2 つの条件をともに満たす長さ N の整数列 A = (A_1, A_2, \ldots, A_N) の個数を 998244353 で割ったあまりを出力してください。
- 0 \leq A_1 \leq A_2 \leq \cdots \leq A_N \leq M
- A_1 \oplus A_2 \oplus \cdots \oplus A_N = X
ここで、\oplus はビットごとの排他的論理和を表します。
ビットごとの排他的論理和とは?
非負整数 A, B のビットごとの排他的論理和 A \oplus B は、以下のように定義されます。- A \oplus B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
制約
- 1 \leq N \leq 200
- 0 \leq M \lt 2^{30}
- 0 \leq X \lt 2^{30}
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M X
出力
答えを出力せよ。
入力例 1
3 3 2
出力例 1
5
問題文中の 2 つの条件をともに満たす長さ N の整数列 A は、(0, 0, 2), (0, 1, 3), (1, 1, 2), (2, 2, 2), (2, 3, 3) の 5 個です。
入力例 2
200 900606388 317329110
出力例 2
788002104
Score : 600 points
Problem Statement
Print the number of integer sequences of length N, A = (A_1, A_2, \ldots, A_N), that satisfy both of the following conditions, modulo 998244353.
- 0 \leq A_1 \leq A_2 \leq \cdots \leq A_N \leq M
- A_1 \oplus A_2 \oplus \cdots \oplus A_N = X
Here, \oplus denotes bitwise XOR.
What is bitwise XOR?
The bitwise XOR of non-negative integers A and B, A \oplus B, is defined as follows.
- When A \oplus B is written in binary, the k-th lowest bit (k \geq 0) is 1 if exactly one of the k-th lowest bits of A and B is 1, and 0 otherwise.
Constraints
- 1 \leq N \leq 200
- 0 \leq M \lt 2^{30}
- 0 \leq X \lt 2^{30}
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M X
Output
Print the answer.
Sample Input 1
3 3 2
Sample Output 1
5
Here are the five sequences of length N that satisfy both conditions in the statement: (0, 0, 2), (0, 1, 3), (1, 1, 2), (2, 2, 2), (2, 3, 3).
Sample Input 2
200 900606388 317329110
Sample Output 2
788002104