url stringlengths 49 92 | description stringlengths 22 4.78k | cases listlengths 0 6 |
|---|---|---|
https://atcoder.jp/contests/newjudge-2308-algorithm/tasks/abc260_f | Problem Statement
We have a simple undirected graph
G
with
(S+T)
vertices and
M
edges. The vertices are numbered
1
through
(S+T)
, and the edges are numbered
1
through
M
. Edge
i
connects Vertices
u_i
and
v_i
.
Here, vertex sets
V_1 = \lbrace 1, 2,\dots, S\rbrace
and
V_2 = \lbrace S+1, S+2, \dots, S+T \rbrace
are both independent sets.
A cycle of length
4
is called a 4-cycle.
If
G
contains a 4-cycle, choose any of them and print the vertices in the cycle. You may print the vertices in any order.
If
G
does not contain a 4-cycle, print
-1
.
What is an independent set?
An independent set of a graph
G
is a set
V'
of some of the vertices in
G
such that no two vertices of
V'
have an edge between them. | [
{
"input": "2 3 5\n1 3\n1 4\n1 5\n2 4\n2 5\n",
"output": "1 2 4 5\n"
},
{
"input": "3 2 4\n1 4\n1 5\n2 5\n3 5\n",
"output": "-1\n"
},
{
"input": "4 5 9\n3 5\n1 8\n3 7\n1 9\n4 6\n2 7\n4 8\n1 7\n2 9\n",
"output": "1 7 2 9\n"
}
] |
https://atcoder.jp/contests/newjudge-2308-algorithm/tasks/abc254_g | Problem Statement
There is a complex composed of
N
10^9
-story skyscrapers. The skyscrapers are numbered
1
to
N
, and the floors are numbered
1
to
10^9
.
From any floor of any skyscraper, one can use a skybridge to get to the same floor of any other skyscraper in one minute.
Additionally, there are
M
elevators. The
i
-th elevator runs between Floor
B_i
and Floor
C_i
of Skyscraper
A_i
. With this elevator, one can get from Floor
x
to Floor
y
of Skyscraper
A_i
in
|x-y|
minutes, for every pair of integers
x,y
such that
B_i \le x,y \le C_i
.
Answer the following
Q
queries.
Determine whether it is possible to get from Floor
Y_i
of Skyscraper
X_i
to Floor
W_i
of Skyscraper
Z_i
, and find the shortest time needed to get there if it is possible. | [
{
"input": "3 4 3\n1 2 10\n2 3 7\n3 9 14\n3 1 3\n1 3 3 14\n3 1 2 7\n1 100 1 101\n",
"output": "12\n7\n-1\n"
},
{
"input": "1 1 1\n1 1 2\n1 1 1 2\n",
"output": "1\n"
}
] |
https://atcoder.jp/contests/newjudge-2308-algorithm/tasks/abc294_h | Problem Statement
You are given a simple undirected graph with
N
vertices numbered
1
to
N
and
M
edges numbered
1
to
M
. Edge
i
connects vertex
u_i
and vertex
v_i
.
Find the number, modulo
998244353
, of ways to write an integer between
1
and
K
, inclusive, on each vertex of this graph to satisfy the following condition:
two vertices connected by an edge always have different numbers written on them. | [
{
"input": "4 3 2\n1 2\n2 4\n2 3\n",
"output": "2\n"
},
{
"input": "4 0 10\n",
"output": "10000\n"
},
{
"input": "5 10 5\n3 5\n1 3\n1 2\n1 4\n3 4\n2 5\n4 5\n1 5\n2 3\n2 4\n",
"output": "120\n"
},
{
"input": "5 6 294\n1 2\n2 4\n1 3\n2 3\n4 5\n3 5\n",
"output": "838338733\n... |
https://atcoder.jp/contests/codequeen2023-final-open/tasks/codequeen2023_final_a | 問題文
文字列
S
と文字
c
が与えられます。
文字列
S
のすべての文字
x
に対して、以下の操作を同時に一度だけ行ってできる文字列を出力してください。
x
が
c
と等しいとき、
x
を
xx
に置き換える。そうでないときは何もしない。 | [
{
"input": "quen\ne\n",
"output": "queen\n"
},
{
"input": "kenkoo\no\n",
"output": "kenkoooo\n"
},
{
"input": "abracadabra\na\n",
"output": "aabraacaadaabraa\n"
}
] |
https://atcoder.jp/contests/codequeen2023-final-open/tasks/codequeen2023_final_b | 問題文
縦に
N
マス、横に
N
マスからなる、マス目状に区切られた盤面があります。上から
i
番目
(1 \leq i \leq N)
、左から
j
番目
(1 \leq j \leq N)
のマスを、マス
(i, j)
と呼ぶことにします。
盤面には
N-1
個のクイーンが配置されており、
i
番目のクイーンはマス
(r_i, c_i)
にあります。
ここで、盤面が次の条件を満たすとき、盤面は
良い状態
です。
盤面上のどの縦・横・斜め 45 度のマス目の列を見ても、クイーンが
2
つ以上存在しない。
また、与えられる盤面は
良い状態
であることが保証されます。
この盤面に対して、
良い状態
を保ちつつクイーンを追加で
1
個配置することができるかを判定し、できる場合は配置する位置を出力してください。 | [
{
"input": "5\n5 5\n3 1\n1 2\n4 3\n",
"output": "2 4\n"
},
{
"input": "5\n2 5\n3 1\n1 2\n4 4\n",
"output": "-1\n"
}
] |
https://atcoder.jp/contests/codequeen2023-final-open/tasks/codequeen2023_final_c | 問題文
頂点に
1, 2, \ldots, N
の番号がついた
N
頂点の木と、頂点の番号
S, T
が与えられます。
i = 1, 2, \ldots, N-1
について、
i
番目の辺は頂点
u_i
と頂点
v_i
を結んでいます。
木のそれぞれの頂点
j
について、以下の質問に答えてください。
頂点
S
から頂点
j
までの最短経路に含まれる頂点集合(頂点
S
や頂点
j
を含む)と、頂点
T
から頂点
j
までの最短経路に含まれる頂点集合(頂点
T
や頂点
j
を含む)の両方に属する頂点の数はいくつでしょうか? | [
{
"input": "11 1 4\n1 2\n2 3\n3 5\n3 6\n2 4\n4 7\n1 8\n8 9\n8 10\n9 11\n",
"output": "1\n1\n2\n1\n3\n3\n2\n2\n3\n3\n4\n"
}
] |
https://atcoder.jp/contests/codequeen2023-final-open/tasks/codequeen2023_final_d | 問題文
縦に
R
マス、横に
C
マスからなる、マス目状に区切られた盤面があります。上から
i
番目
(1 \leq i \leq R)
、左から
j
番目
(1 \leq j \leq C)
のマスを、マス
(i, j)
と呼ぶことにします。
現在マス
(r_s, c_s)
に
1
個のクイーンが配置されています。 以下の行動を
三回
繰り返した時、 クイーンが
(r_t, c_t)
にある様な動き方は何通りありますか。
クイーンを盤面上の現在配置されているマスを含む縦・横・斜め 45 度のマス目の列上の
他のマス
に移動する(現在配置されているマスに留まることはできない) | [
{
"input": "3 3 1 1 3 3\n",
"output": "29\n"
},
{
"input": "3 3 2 2 2 2\n",
"output": "40\n"
},
{
"input": "100000 100000 1 1 100000 100000\n",
"output": "10001499973\n"
}
] |
https://atcoder.jp/contests/codequeen2023-final-open/tasks/codequeen2023_final_e | 問題文
長さ
N
の数列
A = \left( a_1, a_2, \ldots, a_N \right)
が与えられます。
この数列を、いくつかの連続する空でない部分列
B_1, B_2, \ldots, B_K
に分割することを考えます。たとえば、
A = (5, -3, 6, 2, 4)
のとき、
A
の分割の例は以下のとおりです。
B_1 = (5), B_2 = (-3, 6, 2), B_3 = (4)
B_1 = (5, -3), B_2 = (6, 2, 4)
B_1 = (5, -3, 6, 2, 4)
部分列
B_i
の
スコア
S \left( B_i \right)
を、
B_i
に含まれる項の最大値と最小値の差
\max B_i - \min B_i
と定義します。
A
を最適に分割したときの、部分列のスコアの和
\sum_i S \left( B_i \right)
の最大値を求めてください。 | [
{
"input": "9\n1 2 2 4 5 2 3 4 1\n",
"output": "9\n"
},
{
"input": "12\n3 -1 4 1 5 -9 2 6 5 -3 5 9\n",
"output": "37\n"
}
] |
https://atcoder.jp/contests/codequeen2023-final-open/tasks/codequeen2023_final_f | 問題文
以下の条件を満たす様に二次元平面上に
N
個の点
P_1
,
P_2
,
\ldots
,
P_N
を置きます。この条件の元、
N
角形
P_1 P_2 \dots P_N
の面積の最大値を求めてください。ただし、原点と点
P_i
を結ぶ直線の
x
軸正の向きとのなす角を
\theta_i\ \mathrm{rad}
とします。
i
番目の点
P_i
は原点を中心とする半径
R_i
の円周上に存在する
与えられる
R
は、
\displaystyle R_1 = R_N
かつ
\displaystyle R_1, R_N \leq R_i
(1 \leq i \leq N)
を満たすことが保証されます
\displaystyle \frac{\pi}{2N} \leq \theta_{i+1} -\theta_{i}
(1 \leq i \leq N-1)
(14:07 修正)
\displaystyle \theta_1 = 0
,
\displaystyle \theta_N = \frac{2\pi}{3}
\displaystyle x\ \mathrm{rad}
の定義
半径が
1
で弧の長さが
x
である様な扇形の中心角を
\displaystyle x\ \mathrm{rad}
と定義します。
\displaystyle x\ \mathrm{rad}
は
\displaystyle \frac{180}{\pi} x
度と等しい角度となります。 | [
{
"input": "3\n1 1 1\n",
"output": "0.433012701892219\n"
},
{
"input": "7\n1 2 1 3 1 2 1\n",
"output": "2.147031208123904\n"
}
] |
https://atcoder.jp/contests/abc313/tasks/abc313_a | Problem Statement
There are
N
people numbered
1
through
N
.
Each person has a integer score called programming ability; person
i
's programming ability is
P_i
points.
How many more points does person
1
need, so that person
1
becomes the strongest?
In other words, what is the minimum non-negative integer
x
such that
P_1 + x > P_i
for all
i \neq 1
? | [
{
"input": "4\n5 15 2 10\n",
"output": "11\n"
},
{
"input": "4\n15 5 2 10\n",
"output": "0\n"
},
{
"input": "3\n100 100 100\n",
"output": "1\n"
}
] |
https://atcoder.jp/contests/abc313/tasks/abc313_b | Problem Statement
There are
N
competitive programmers numbered person
1
, person
2
,
\ldots
, and person
N
.
There is a relation called
superiority
between the programmers. For all pairs of distinct programmers
(
person
X
, person
Y
)
, exactly one of the following two relations holds: "person
X
is stronger than person
Y
" or "person
Y
is stronger than person
X
."
The superiority is
transitive
. In other words, for all triplets of distinct programmers
(
person
X
, person
Y
, person
Z
)
, it holds that:
if person
X
is stronger than person
Y
and person
Y
is stronger than person
Z
, then person
X
is stronger than person
Z
.
A person
X
is said to be the
strongest programmer
if person
X
is stronger than person
Y
for all people
Y
other than person
X
. (Under the constraints above, we can prove that there is always exactly one such person.)
You have
M
pieces of information on their superiority. The
i
-th of them is that "person
A_i
is stronger than person
B_i
."
Can you determine the strongest programmer among the
N
based on the information?
If you can, print the person's number. Otherwise, that is, if there are multiple possible strongest programmers, print
-1
. | [
{
"input": "3 2\n1 2\n2 3\n",
"output": "1\n"
},
{
"input": "3 2\n1 3\n2 3\n",
"output": "-1\n"
},
{
"input": "6 6\n1 6\n6 5\n6 2\n2 3\n4 3\n4 2\n",
"output": "-1\n"
}
] |
https://atcoder.jp/contests/abc313/tasks/abc313_c | Problem Statement
You are given an integer sequence
A=(A_1,A_2,\dots,A_N)
.
You can perform the following operation any number of times (possibly zero).
Choose integers
i
and
j
with
1\leq i,j \leq N
. Decrease
A_i
by one and increase
A_j
by one.
Find the minimum number of operations required to make the difference between the minimum and maximum values of
A
at most one. | [
{
"input": "4\n4 7 3 7\n",
"output": "3\n"
},
{
"input": "1\n313\n",
"output": "0\n"
},
{
"input": "10\n999999997 999999999 4 3 2 4 999999990 8 999999991 999999993\n",
"output": "2499999974\n"
}
] |
https://atcoder.jp/contests/abc313/tasks/abc313_d | Problem Statement
This is an
interactive task
(where your program and the judge interact via Standard Input and Output).
You are given an integer
N
and an
odd number
K
.
The judge has a hidden length-
N
sequence
A = (A_1, A_2, \dots, A_N)
consisting of
0
and
1
.
While you cannot directly access the elements of sequence
A
,
you are allowed to ask the judge the following query at most
N
times.
Choose distinct integers
x_1, x_2, \dots
, and
x_K
between
1
and
N
, inclusive, to ask the parity of
A_{x_1} + A_{x_2} + \dots + A_{x_K}
.
Determine
(A_1, A_2, \dots, A_N)
by at most
N
queries, and print the answer.
Here,
the judge is adaptive
. In other words, the judge may modify the contents of
A
as long as it is consistent with the responses to the past queries.
Therefore, your program is considered correct if the output satisfies the following condition, and incorrect otherwise:
your program prints a sequence consistent with the responses to the queries so far, and that is the only such sequence. | [] |
https://atcoder.jp/contests/abc313/tasks/abc313_e | Problem Statement
For a string
S
consisting of digits from
1
through
9
, let
f(S)
be the string
T
obtained by the following procedure. (
S_i
denotes the
i
-th character of
S
.)
Let
T
be an initially empty string.
For
i=1, 2, \dots, |S| - 1
, perform the following operation:
Append
n
copies of
S_i
to the tail of
T
, where
n
is the value when
S_{i+1}
is interpreted as an integer.
For example,
S =
313
yields
f(S) =
3111
by the following steps.
T
is initially empty.
For
i=1
, we have
n = 1
. Append one copy of
3
to
T
, which becomes
3
.
For
i=2
, we have
n = 3
. Append three copies of
1
to
T
, which becomes
3111
.
Terminate the procedure. We obtain
T =
3111
.
You are given a length-
N
string
S
consisting of digits from
1
through
9
.
You repeat the following operation until the length of
S
becomes
1
: replace
S
with
f(S)
.
Find how many times, modulo
998244353
, you perform the operation until you complete it. If you will repeat the operation indefinitely, print
-1
instead. | [
{
"input": "3\n313\n",
"output": "4\n"
},
{
"input": "9\n123456789\n",
"output": "-1\n"
},
{
"input": "2\n11\n",
"output": "1\n"
}
] |
https://atcoder.jp/contests/abc313/tasks/abc313_f | Problem Statement
There are
N
cards numbered
1
through
N
.
Each face of a card has an integer written on it; card
i
has
A_i
on its front and
B_i
on its back.
Initially, all cards are face up.
There are
M
machines numbered
1
through
M
.
Machine
j
has two (not necessarily distinct) integers
X_j
and
Y_j
between
1
and
N
. If you power up machine
j
,
it flips card
X_j
with the probability of
\frac{1}{2}
, and flips card
Y_j
with the remaining probability of
\frac{1}{2}
.
This probability is independent for each power-up.
Snuke will perform the following procedure.
Choose a set
S
consisting of integers from
1
through
M
.
For each element in
S
in ascending order, power up the machine with that number.
Among Snuke's possible choices of
S
, find the maximum expected value of the sum of the integers written on the face-up sides of the cards after the procedure. | [
{
"input": "3 1\n3 10\n10 6\n5 2\n1 2\n",
"output": "19.500000\n"
},
{
"input": "1 3\n5 100\n1 1\n1 1\n1 1\n",
"output": "100.000000\n"
},
{
"input": "8 10\n6918 9211\n16 1868\n3857 8537\n3340 8506\n6263 7940\n1449 4593\n5902 1932\n310 6991\n4 4\n8 6\n3 5\n1 1\n4 2\n5 6\n7 5\n3 3\n1 5\n3... |
https://atcoder.jp/contests/abc313/tasks/abc313_g | Problem Statement
There are
N
plates numbered
1
through
N
. Dish
i
has
a_i
stones on it. There is also an empty bag.
You can perform the following two kinds of operations any number of times (possibly zero) in any order.
Remove one stone from each plate with one or more stones. Put the removed stones into the bag.
Take
N
stones out of the bag, and put one stone to each plate. This operation can be performed only when the bag has
N
or more stones.
Let
b_i
be the number of stones on plate
i
after you finished the operations. Print the number, modulo
998244353
, of sequences of integers
(b_1, b_2, \dots, b_N)
of length
N
that can result from the operations. | [
{
"input": "3\n3 1 3\n",
"output": "7\n"
},
{
"input": "1\n0\n",
"output": "1\n"
},
{
"input": "5\n1 3 5 7 9\n",
"output": "36\n"
},
{
"input": "10\n766294629 440423913 59187619 725560240 585990756 965580535 623321125 550925213 122410708 549392044\n",
"output": "666174028... |
https://atcoder.jp/contests/abc313/tasks/abc313_h | Problem Statement
(2N+1)
people are forming two rows to take a group photograph.
There are
N
people in the front row; the
i
-th of them has a height of
A_i
.
There are
(N+1)
people in the back row; the
i
-th of them has a height of
B_i
.
It is guaranteed that the heights of the
(2N+1)
people are distinct.
Within each row, we can freely rearrange the people.
Suppose that the heights of the people in the front row are
a_1,a_2,\dots,a_N
from the left, and those in the back row are
b_1,b_2,\dots,b_{N+1}
from the left.
This arrangement is said to be
good
if all of the following conditions are satisfied:
a_i < b_i
or
a_{i-1} < b_i
for all
i\ (2 \leq i \leq N)
.
a_1 < b_1
.
a_N < b_{N+1}
.
Among the
N!
ways to rearrange the front row, how many of them, modulo
998244353
, are such ways that we can rearrange the back row to make the arrangement good? | [
{
"input": "3\n1 12 6\n4 3 10 9\n",
"output": "2\n"
},
{
"input": "1\n5\n1 10\n",
"output": "0\n"
},
{
"input": "10\n189330739 910286918 802329211 923078537 492686568 404539679 822804784 303238506 650287940 1\n125660016 430302156 982631932 773361868 161735902 731963982 317063340 88089572... |
https://atcoder.jp/contests/agc063/tasks/agc063_a | Problem Statement
You are given a string of length
N+1
consisting of
A
and
B
:
S = S_0\cdots S_N
.
For each
k=1, \ldots, N
, solve the following problem.
Alice and Bob will play a game using a set
X
, which is initially empty. For
t=1,\ldots, k
in this order, they will do the following action:
if
t
is odd, Alice will choose a non-negative integer
x
and replace
X
with
X\cup \{x\}
;
if
t
is even, Bob will choose a non-negative integer
x
and replace
X
with
X\cup \{x\}
.
Let
x
be
\mathrm{mex}(X)
after all
k
actions. If the character
S_x
is
A
, Alice wins; if
S_x
is
B
, Bob wins. Note that
X
has at most
k
elements, so
x = \mathrm{mex}(X) \leq k
and the character
S_x
exists.
Print the name of the winner when both players play optimally.
What is
\mathrm{mex}(X)
?
For a finite set
X
consisting of non-negative integers,
\mathrm{mex}(X)
is the smallest non-negative integer
x
such that
x\notin X
. | [
{
"input": "2\nABB\n",
"output": "Alice\nBob\n"
},
{
"input": "4\nAAAAA\n",
"output": "Alice\nAlice\nAlice\nAlice\n"
},
{
"input": "7\nBBAABABA\n",
"output": "Bob\nBob\nAlice\nBob\nAlice\nBob\nAlice\n"
}
] |
https://atcoder.jp/contests/agc063/tasks/agc063_b | Problem Statement
A sequence
a = (a_1, \ldots, a_n)
consisting of positive integers is said to be
generatable
when one can obtain
a
by repeating the following operation on an empty sequence.
Operation: Choose a positive integer
k
, and insert
(1, 2, \ldots, k-1, k)
into some position in the sequence. More formally, for a sequence
a = (a_1, \ldots, a_m)
, choose an integer
i
such that
0\leq i\leq m
and a positive integer
k
, and replace
a
with
(a_1,\ldots,a_{i}, 1, 2, \ldots, k-1, k, a_{i+1}, \ldots, a_m)
.
For instance,
a = (1,2,1,1,2,1,3,4,2,3)
is generatable. Here is one way to generate it:
() \to (\boldsymbol{1,2}) \to (1,2,\boldsymbol{1,2,3}) \to (1,2,1,\boldsymbol{1,2,3,4},2,3) \to (1,2,1,1,2,\boldsymbol{1},3,4,2,3).
You are given a sequence
A = (A_1, \ldots, A_N)
consisting of positive integers. Find the number of pairs of integers
(L, R)
such that:
1\leq L\leq R\leq N
and the contiguous subsequence
(A_L, \ldots, A_R)
is generatable. | [
{
"input": "6\n1 2 1 2 1 3\n",
"output": "11\n"
},
{
"input": "5\n1 1 1 1 1\n",
"output": "15\n"
},
{
"input": "7\n1 2 1 2 1 3 4\n",
"output": "13\n"
}
] |
https://atcoder.jp/contests/agc063/tasks/agc063_c | Problem Statement
You are given sequences of non-negative integers:
A = (A_1, \ldots, A_N)
and
B=(B_1, \ldots, B_N)
.
Determine whether one can make
A
equal
B
by performing the following operation between
0
and
N
times, inclusive.
Operation: Choose integers
x,y
such that
0\leq x < y\leq 10^{18}
. For every
i
, replace
A_i
with
(A_i+x)\bmod y
.
If one can make
A
equal
B
, print one way to do so. | [
{
"input": "4\n7 2 4 5\n3 3 5 0\n",
"output": "Yes\n2\n3 5\n3 6\n"
},
{
"input": "1\n5\n3\n",
"output": "Yes\n1\n2 4\n"
},
{
"input": "2\n3 1\n3 1\n",
"output": "Yes\n0\n"
},
{
"input": "2\n0 0\n1 2\n",
"output": "No\n"
}
] |
https://atcoder.jp/contests/agc063/tasks/agc063_d | Problem Statement
You are given positive integers
N, a, b, c, d
.
Determine whether there is a non-negative integer
x
such that
x\equiv a+kb \pmod{c+kd}
for every
k=0,1,\ldots,N-1
. If it exists, find the smallest such
x
modulo
998244353
. | [
{
"input": "2 1 2 3 4\n",
"output": "10\n"
},
{
"input": "2 1 1 10 10\n",
"output": "-1\n"
},
{
"input": "100 20 30 2 3\n",
"output": "0\n"
},
{
"input": "9 12 34 56 78\n",
"output": "827501367\n"
}
] |
https://atcoder.jp/contests/agc063/tasks/agc063_e | Problem Statement
There is a rooted tree with
N
vertices numbered
1
to
N
. Vertex
1
is the root, and the parent of vertex
i
(
2\leq i\leq N
) is
P_i
.
You are given a non-negative integer
r
and a sequence of non-negative integers
A = (A_1, \ldots, A_N)
. You can perform the following operation on the sequence any number of times, possibly zero.
Choose an
i
such that
i\geq 2
and
A_i \geq 1
. Replace
A_i
with
A_i - 1
and
A_{P_i}
with
A_{P_i}+r
.
Find the number, modulo
998244353
, of possible final states of the sequence
A
. | [
{
"input": "3\n1 1\n2\n1 1 1\n",
"output": "4\n"
},
{
"input": "3\n1 2\n1\n1 1 1\n",
"output": "5\n"
},
{
"input": "3\n1 2\n2\n1 1 1\n",
"output": "6\n"
},
{
"input": "5\n1 1 3 3\n2\n0 1 0 1 2\n",
"output": "48\n"
},
{
"input": "5\n1 1 3 3\n123456789\n1 2 3 4 5\n"... |
https://atcoder.jp/contests/agc063/tasks/agc063_f | Problem Statement
You are given pairs of non-negative integers
a = (a_1,a_2)
and
b = (b_1,b_2)
.
You can perform the following operation on the pair
a
any number of times, possibly zero.
Operation: Choose a
positive real number
x
. Replace
a = (a_1,a_2)
with
(\lfloor a_1x\rfloor, \lfloor a_2x\rfloor)
.
Your objective is to make the pair
a
equal the pair
b
. Determine whether it is achievable. If it is, find the minimum number of times you must perform the operation to achieve it.
You have
T
test cases to solve. | [
{
"input": "7\n2 3 1 1\n1 1 2 3\n3 2 9 8\n12 34 56 78\n56 78 12 34\n87 65 43 21\n43 21 87 65\n",
"output": "1\n-1\n3\n-1\n4\n2\n-1\n"
},
{
"input": "9\n5 5 5 5\n5 5 3 3\n3 9 0 2\n3 9 0 3\n0 3 3 9\n3 0 2 0\n5 2 0 0\n0 0 5 2\n0 0 0 0\n",
"output": "0\n1\n1\n2\n-1\n1\n1\n-1\n0\n"
}
] |
https://atcoder.jp/contests/abc312/tasks/abc312_a | Problem Statement
Given a length-
3
string
S
consisting of uppercase English letters, print
Yes
if
S
equals one of
ACE
,
BDF
,
CEG
,
DFA
,
EGB
,
FAC
, and
GBD
; print
No
otherwise. | [
{
"input": "ABC\n",
"output": "No\n"
},
{
"input": "FAC\n",
"output": "Yes\n"
},
{
"input": "XYX\n",
"output": "No\n"
}
] |
https://atcoder.jp/contests/abc312/tasks/abc312_b | Problem Statement
Takahashi invented Tak Code, a two-dimensional code. A TaK Code satisfies all of the following conditions:
It is a region consisting of nine horizontal rows and nine vertical columns.
All the
18
cells in the top-left and bottom-right three-by-three regions are black.
All the
14
cells that are adjacent (horizontally, vertically, or diagonally) to the top-left or bottom-right three-by-three region are white.
It is not allowed to rotate a TaK Code.
You are given a grid with
N
horizontal rows and
M
vertical columns.
The state of the grid is described by
N
strings,
S_1,\ldots
, and
S_N
, each of length
M
. The cell at the
i
-th row from the top and
j
-th column from the left is black if the
j
-th character of
S_i
is
#
, and white if it is
.
.
Find all the nine-by-nine regions, completely contained in the grid, that satisfy the conditions of a TaK Code. | [
{
"input": "19 18\n###......###......\n###......###......\n###..#...###..#...\n..............#...\n..................\n..................\n......###......###\n......###......###\n......###......###\n.###..............\n.###......##......\n.###..............\n............###...\n...##.......###...\n...##.......#... |
https://atcoder.jp/contests/abc312/tasks/abc312_c | Problem Statement
There are
N
sellers and
M
buyers in an apple market.
The
i
-th seller may sell an apple for
A_i
yen or more (yen is the currency in Japan).
The
i
-th buyer may buy an apple for
B_i
yen or less.
Find the minimum integer
X
that satisfies the following condition.
Condition: The number of people who may sell an apple for
X
yen is greater than or equal to the number of people who may buy an apple for
X
yen. | [
{
"input": "3 4\n110 90 120\n100 80 120 10000\n",
"output": "110\n"
},
{
"input": "5 2\n100000 100000 100000 100000 100000\n100 200\n",
"output": "201\n"
},
{
"input": "3 2\n100 100 100\n80 120\n",
"output": "100\n"
}
] |
https://atcoder.jp/contests/abc312/tasks/abc312_d | Problem Statement
You are given a non-empty string
S
consisting of
(
,
)
, and
?
.
There are
2^x
ways to obtain a new string by replacing each
?
in
S
with
(
and
)
, where
x
is the number of occurrences of
?
in
S
. Among them, find the number, modulo
998244353
, of ways that yield a
parenthesis string
.
A string is said to be a parenthesis string if one of the following conditions is satisfied.
It is an empty string.
It is a concatenation of
(
,
A
, and
)
, for some parenthesis string
A
.
It is a concatenation of
A
and
B
, for some non-empty parenthesis strings
A
and
B
. | [
{
"input": "(???(?\n",
"output": "2\n"
},
{
"input": ")))))\n",
"output": "0\n"
},
{
"input": "??????????????(????????(??????)?????????(?(??)\n",
"output": "603032273\n"
}
] |
https://atcoder.jp/contests/abc312/tasks/abc312_e | Problem Statement
There are
N
rectangular cuboids in a three-dimensional space.
These cuboids do not overlap. Formally, for any two different cuboids among them, their intersection has a volume of
0
.
The diagonal of the
i
-th cuboid is a segment that connects two points
(X_{i,1},Y_{i,1},Z_{i,1})
and
(X_{i,2},Y_{i,2},Z_{i,2})
, and its edges are all parallel to one of the coordinate axes.
For each cuboid, find the number of other cuboids that share a face with it.
Formally, for each
i
, find the number of
j
with
1\leq j \leq N
and
j\neq i
such that the intersection of the surfaces of the
i
-th and
j
-th cuboids has a positive area. | [
{
"input": "4\n0 0 0 1 1 1\n0 0 1 1 1 2\n1 1 1 2 2 2\n3 3 3 4 4 4\n",
"output": "1\n1\n0\n0\n"
},
{
"input": "3\n0 0 10 10 10 20\n3 4 1 15 6 10\n0 9 6 1 20 10\n",
"output": "2\n1\n1\n"
},
{
"input": "8\n0 0 0 1 1 1\n0 0 1 1 1 2\n0 1 0 1 2 1\n0 1 1 1 2 2\n1 0 0 2 1 1\n1 0 1 2 1 2\n1 1 0 2... |
https://atcoder.jp/contests/abc312/tasks/abc312_f | Problem Statement
There are
N
items.
Each of these is one of a pull-tab can, a regular can, or a can opener.
The
i
-th item is described by an integer pair
(T_i, X_i)
as follows:
If
T_i = 0
, the
i
-th item is a pull-tab can; if you obtain it, you get a happiness of
X_i
.
If
T_i = 1
, the
i
-th item is a regular can; if you obtain it and use a can opener against it, you get a happiness of
X_i
.
If
T_i = 2
, the
i
-th item is a can opener; it can be used against at most
X_i
cans.
Find the maximum total happiness that you get by obtaining
M
items out of
N
. | [
{
"input": "8 4\n0 6\n0 6\n1 3\n1 5\n1 15\n2 1\n2 10\n2 100\n",
"output": "27\n"
},
{
"input": "5 5\n1 5\n1 5\n1 5\n1 5\n1 5\n",
"output": "0\n"
},
{
"input": "12 6\n2 2\n0 1\n0 9\n1 3\n1 5\n1 3\n0 4\n2 1\n1 8\n2 1\n0 1\n0 4\n",
"output": "30\n"
}
] |
https://atcoder.jp/contests/abc312/tasks/abc312_g | Problem Statement
You are given a tree with
N
vertices. The vertices are numbered from
1
through
N
, and the
i
-th edge connects vertex
A_i
and vertex
B_i
.
Find the number of tuples of integers
(i,j,k)
such that:
1 \leq i < j < k \leq N
; and
the given tree does not contain a simple path that contains all of vertices
i
,
j
, and
k
. | [
{
"input": "5\n1 2\n2 3\n2 4\n1 5\n",
"output": "2\n"
},
{
"input": "6\n1 2\n2 3\n3 4\n4 5\n5 6\n",
"output": "0\n"
},
{
"input": "12\n1 6\n3 4\n10 4\n5 9\n3 1\n2 3\n7 2\n2 12\n1 5\n6 8\n4 11\n",
"output": "91\n"
}
] |
https://atcoder.jp/contests/abc312/tasks/abc312_h | Problem Statement
Takahashi is going to decide nicknames of
N
people, person
1,\ldots,N
.
Person
i
wants a nickname
S_i
. To avoid giving the same nickname to multiple people, he is going to decide their nicknames as follows:
For each
i=1,\ldots,N
in order, decide person
i
's nickname as follows:
Initialize a variable
k_i
with
1
.
Repeatedly increment
k_i
by one while the
k_i
-time repetition of
S_i
is someone's nickname.
Let person
i
's nickname be the
k_i
-time repetition of
S_i
.
Find
k_1,\ldots
, and
k_N
after deciding nicknames of the
N
people. | [
{
"input": "3\nsnuke\nsnuke\nrng\n",
"output": "1 2 1\n"
},
{
"input": "4\naa\na\na\naaa\n",
"output": "1 1 3 2\n"
},
{
"input": "5\nx\nx\nx\nx\nx\n",
"output": "1 2 3 4 5\n"
}
] |
https://atcoder.jp/contests/abc311/tasks/abc311_a | Problem Statement
You are given a string
S
consisting of
A
,
B
, and
C
.
S
is guaranteed to contain all of
A
,
B
, and
C
.
If the characters of
S
are checked one by one from the left, how many characters will have been checked when the following condition is satisfied for the first time?
All of
A
,
B
, and
C
have appeared at least once. | [
{
"input": "5\nACABB\n",
"output": "4\n"
},
{
"input": "4\nCABC\n",
"output": "3\n"
},
{
"input": "30\nAABABBBABABBABABCABACAABCBACCA\n",
"output": "17\n"
}
] |
https://atcoder.jp/contests/abc311/tasks/abc311_b | Problem Statement
There are
N
people numbered
1
to
N
.
You are given their schedule for the following
D
days. The schedule for person
i
is represented by a string
S_i
of length
D
. If the
j
-th character of
S_i
is
o
, person
i
is free on the
j
-th day; if it is
x
, they are occupied that day.
From these
D
days, consider choosing some
consecutive
days when all the people are free.
How many days can be chosen at most? If no day can be chosen, report
0
. | [
{
"input": "3 5\nxooox\noooxx\noooxo\n",
"output": "2\n"
},
{
"input": "3 3\noxo\noxo\noxo\n",
"output": "1\n"
},
{
"input": "3 3\noox\noxo\nxoo\n",
"output": "0\n"
},
{
"input": "1 7\nooooooo\n",
"output": "7\n"
},
{
"input": "5 15\noxooooooooooooo\noxooxoooooooo... |
https://atcoder.jp/contests/abc311/tasks/abc311_c | Problem Statement
There is a directed graph with
N
vertices and
N
edges.
The
i
-th edge goes from vertex
i
to vertex
A_i
. (The constraints guarantee that
i \neq A_i
.)
Find a directed cycle without the same vertex appearing multiple times.
It can be shown that a solution exists under the constraints of this problem.
Notes
The sequence of vertices
B = (B_1, B_2, \dots, B_M)
is called a directed cycle when all of the following conditions are satisfied:
M \geq 2
The edge from vertex
B_i
to vertex
B_{i+1}
exists.
(1 \leq i \leq M-1)
The edge from vertex
B_M
to vertex
B_1
exists.
If
i \neq j
, then
B_i \neq B_j
. | [
{
"input": "7\n6 7 2 1 3 4 5\n",
"output": "4\n7 5 3 2\n"
},
{
"input": "2\n2 1\n",
"output": "2\n1 2\n"
},
{
"input": "8\n3 7 4 7 3 3 8 2\n",
"output": "3\n2 7 8\n"
}
] |
https://atcoder.jp/contests/abc311/tasks/abc311_d | Problem Statement
There is an
N \times M
grid and a player standing on it.
Let
(i,j)
denote the square at the
i
-th row from the top and
j
-th column from the left of this grid.
Each square of this grid is ice or rock, which is represented by
N
strings
S_1,S_2,\dots,S_N
of length
M
as follows:
if the
j
-th character of
S_i
is
.
, square
(i,j)
is ice;
if the
j
-th character of
S_i
is
#
, square
(i,j)
is rock.
The outer periphery of this grid (all squares in the
1
-st row,
N
-th row,
1
-st column,
M
-th column) is rock.
Initially, the player rests on the square
(2,2)
, which is ice.
The player can make the following move zero or more times.
First, specify the direction of movement: up, down, left, or right.
Then, keep moving in that direction until the player bumps against a rock. Formally, keep doing the following:
if the next square in the direction of movement is ice, go to that square and keep moving;
if the next square in the direction of movement is rock, stay in the current square and stop moving.
Find the number of ice squares the player can touch (pass or rest on). | [
{
"input": "6 6\n######\n#....#\n#.#..#\n#..#.#\n#....#\n######\n",
"output": "12\n"
},
{
"input": "21 25\n#########################\n#..............###...####\n#..............#..#...###\n#........###...#...#...##\n#........#..#..#........#\n#...##...#..#..#...#....#\n#..#..#..###...#..#.....#\n#..#... |
https://atcoder.jp/contests/abc311/tasks/abc311_e | Problem Statement
There is a grid with
H
rows and
W
columns. Let
(i, j)
denote the square at the
i
-th row from the top and
j
-th column from the left of the grid.
Each square of the grid is holed or not. There are exactly
N
holed squares:
(a_1, b_1), (a_2, b_2), \dots, (a_N, b_N)
.
When the triple of positive integers
(i, j, n)
satisfies the following condition, the square region whose top-left corner is
(i, j)
and whose bottom-right corner is
(i + n - 1, j + n - 1)
is called a
holeless square
.
i + n - 1 \leq H
.
j + n - 1 \leq W
.
For every pair of non-negative integers
(k, l)
such that
0 \leq k \leq n - 1, 0 \leq l \leq n - 1
, square
(i + k, j + l)
is not holed.
How many holeless squares are in the grid? | [
{
"input": "2 3 1\n2 3\n",
"output": "6\n"
},
{
"input": "3 2 6\n1 1\n1 2\n2 1\n2 2\n3 1\n3 2\n",
"output": "0\n"
},
{
"input": "1 1 0\n",
"output": "1\n"
},
{
"input": "3000 3000 0\n",
"output": "9004500500\n"
}
] |
https://atcoder.jp/contests/abc311/tasks/abc311_f | Problem Statement
There is an
N \times M
grid and a player standing on it.
Let
(i,j)
denote the square at the
i
-th row from the top and
j
-th column from the left of this grid.
Each square of this grid is black or white, which is represented by
N
strings
S_1,S_2,\dots,S_N
of length
M
as follows:
if the
j
-th character of
S_i
is
.
, square
(i,j)
is white;
if the
j
-th character of
S_i
is
#
, square
(i,j)
is black.
The grid is said to be
beautiful
when the following condition is satisfied.
For every pair of integers
(i,j)
such that
1 \le i \le N, 1 \le j \le M
, if square
(i,j)
is black, the square under
(i,j)
and the square to the immediate lower right of
(i,j)
are also black (if they exist).
Formally, all of the following are satisfied.
If square
(i,j)
is black and square
(i+1,j)
exists, square
(i+1,j)
is also black.
If square
(i,j)
is black and square
(i+1,j+1)
exists, square
(i+1,j+1)
is also black.
Takahashi can paint zero or more white squares black, and he will do so to make the grid beautiful.
Find the number of different beautiful grids he can make, modulo
998244353
.
Two grids are considered different when there is a square that has different colors in those two grids. | [
{
"input": "2 2\n.#\n..\n",
"output": "3\n"
},
{
"input": "5 5\n....#\n...#.\n..#..\n.#.#.\n#...#\n",
"output": "92\n"
},
{
"input": "25 25\n.........................\n.........................\n.........................\n.........................\n.........................\n............... |
https://atcoder.jp/contests/abc311/tasks/abc311_g | Problem Statement
There is an
N \times M
grid, where the square at the
i
-th row from the top and
j
-th column from the left has a non-negative integer
A_{i,j}
written on it.
Let us choose a rectangular region
R
.
Formally, the region is chosen as follows.
Choose integers
l_x, r_x, l_y, r_y
such that
1 \le l_x \le r_x \le N, 1 \le l_y \le r_y \le M
.
Then, square
(i,j)
is in
R
if and only if
l_x \le i \le r_x
and
l_y \le j \le r_y
.
Find the maximum possible value of
f(R) =
(the sum of integers written on the squares in
R
)
\times
(the smallest integer written on a square in
R
). | [
{
"input": "3 3\n5 4 3\n4 3 2\n3 2 1\n",
"output": "48\n"
},
{
"input": "4 5\n3 1 4 1 5\n9 2 6 5 3\n5 8 9 7 9\n3 2 3 8 4\n",
"output": "231\n"
},
{
"input": "6 6\n1 300 300 300 300 300\n300 1 300 300 300 300\n300 300 1 300 300 300\n300 300 300 1 300 300\n300 300 300 300 1 300\n300 300 30... |
https://atcoder.jp/contests/abc311/tasks/abc311_h | Problem Statement
There is a rooted tree
T
with
N
vertices numbered
1
to
N
. The root is vertex
1
, and the parent of vertex
i
(2 \leq i \leq N)
is
P_i
.
Each vertex has two non-negative integer values called
beauty
and
weight
. The beauty and weight of vertex
i
are
B_i
and
W_i
, respectively.
Additionally, each vertex is painted red or blue. The color of vertex
i
is represented by an integer
C_i
: vertex
i
is painted red if
C_i = 0
, and blue if
C_i = 1
.
For vertex
v
, let
F(v)
be the answer to the following problem.
Let
U
be the rooted tree that is the subtree of
T
rooted at
v
.
You can perform the following sequence of operations on
U
zero or more times. (The operations do not affect the beauties, weights, or colors of the vertices that are not being deleted.)
Choose a vertex
c
other than the root. Let
p
be the parent of
c
.
For each edge whose endpoint on the parent side is
c
, do the following:
Let
u
be the endpoint of the edge other than
c
. Delete the edge and connect
p
and
u
with a new edge, with
p
on the parent side.
Delete the vertex
c
, and the edge connecting
p
and
c
.
A rooted tree that can be obtained as
U
after some operations is called a
good rooted tree
when all of the following conditions are satisfied.
For every edge in
U
, the edge's endpoints have different colors.
The vertices have a total weight of at most
X
.
Find the maximum possible total beauty of the vertices in a good rooted tree.
Find all of
F(1), F(2), \dots, F(N)
. | [
{
"input": "4 10\n1 2 2\n2 1 0\n4 2 1\n6 8 0\n7 4 1\n",
"output": "9\n10\n6\n7\n"
},
{
"input": "5 5\n1 2 2 3\n1 1 0\n10 1 1\n100 1 0\n1000 1 1\n10000 1 1\n",
"output": "11001\n10110\n10100\n1000\n10000\n"
},
{
"input": "20 100\n1 2 1 1 1 6 6 5 1 7 9 4 6 4 15 16 8 2 5\n887945036308847 12... |
https://atcoder.jp/contests/abc310/tasks/abc310_a | Problem Statement
Takahashi wants a beverage called AtCoder Drink in a restaurant.
It can be ordered at a regular price of
P
yen.
He also has a discount coupon that allows him to order it at a lower price of
Q
yen.
However, he must additionally order one of the restaurant's
N
dishes to use that coupon.
For each
i = 1, 2, \ldots, N
, the price of the
i
-th dish is
D_i
yen.
Print the minimum total amount of money that he must pay to get the drink. | [
{
"input": "3 100 50\n60 20 40\n",
"output": "70\n"
},
{
"input": "3 100 50\n60000 20000 40000\n",
"output": "100\n"
}
] |
https://atcoder.jp/contests/abc310/tasks/abc310_b | Problem Statement
AtCoder Shop has
N
products.
The price of the
i
-th product
(1\leq i\leq N)
is
P _ i
.
The
i
-th product
(1\leq i\leq N)
has
C_i
functions. The
j
-th function
(1\leq j\leq C _ i)
of the
i
-th product
(1\leq i\leq N)
is represented as an integer
F _ {i,j}
between
1
and
M
, inclusive.
Takahashi wonders whether there is a product that is strictly superior to another.
If there are
i
and
j
(1\leq i,j\leq N)
such that the
i
-th and
j
-th products satisfy all of the following conditions, print
Yes
; otherwise, print
No
.
P _ i\geq P _ j
.
The
j
-th product has all functions of the
i
-th product.
P _ i\gt P _ j
, or the
j
-th product has one or more functions that the
i
-th product lacks. | [
{
"input": "5 6\n10000 2 1 3\n15000 3 1 2 4\n30000 3 1 3 5\n35000 2 1 5\n100000 6 1 2 3 4 5 6\n",
"output": "Yes\n"
},
{
"input": "4 4\n3 1 1\n3 1 2\n3 1 2\n4 2 2 3\n",
"output": "No\n"
},
{
"input": "20 10\n72036 3 3 4 9\n7716 4 1 2 3 6\n54093 5 1 6 7 8 10\n25517 7 3 4 5 6 7 9 10\n96930... |
https://atcoder.jp/contests/abc310/tasks/abc310_c | Problem Statement
There are
N
sticks with several balls stuck onto them. Each ball has a lowercase English letter written on it.
For each
i = 1, 2, \ldots, N
, the letters written on the balls stuck onto the
i
-th stick are represented by a string
S_i
.
Specifically, the number of balls stuck onto the
i
-th stick is the length
|S_i|
of the string
S_i
, and
S_i
is the sequence of letters on the balls starting from one end of the stick.
Two sticks are considered the same when the sequence of letters on the balls starting from one end of one stick is equal to the sequence of letters starting from one end of the other stick.
More formally, for integers
i
and
j
between
1
and
N
, inclusive, the
i
-th and
j
-th sticks are considered the same if and only if
S_i
equals
S_j
or its reversal.
Print the number of different sticks among the
N
sticks. | [
{
"input": "6\na\nabc\nde\ncba\nde\nabc\n",
"output": "3\n"
}
] |
https://atcoder.jp/contests/abc310/tasks/abc310_d | Problem Statement
There are
N
sports players.
Among them, there are
M
incompatible pairs. The
i
-th incompatible pair
(1\leq i\leq M)
is the
A_i
-th and
B_i
-th players.
You will divide the players into
T
teams.
Every player must belong to exactly one team, and every team must have one or more players.
Additionally, for each
i=1,2,\ldots,M
, the
A_i
-th and
B_i
-th players must not belong to the same team.
Find the number of ways to satisfy these conditions.
Here, two divisions are considered different when there are two players who belong to the same team in one division and different teams in the other. | [
{
"input": "5 2 2\n1 3\n3 4\n",
"output": "4\n"
},
{
"input": "5 1 2\n1 3\n3 4\n",
"output": "0\n"
},
{
"input": "6 4 0\n",
"output": "65\n"
},
{
"input": "10 6 8\n5 9\n1 4\n3 8\n1 6\n4 10\n5 7\n5 6\n3 7\n",
"output": "8001\n"
}
] |
https://atcoder.jp/contests/abc310/tasks/abc310_e | Problem Statement
You are given a string
S
of length
N
consisting of
0
and
1
.
It describes a length-
N
sequence
A=(A _ 1,A _ 2,\ldots,A _ N)
. If the
i
-th character of
S
(1\leq i\leq N)
is
0
, then
A _ i=0
; if it is
1
, then
A _ i=1
.
Find the following:
\[\sum _ {1\leq i\leq j\leq N}(\cdots((A _ i\barwedge A _ {i+1})\barwedge A _ {i+2})\barwedge\cdots\barwedge A _ j)\]
More formally, find
\displaystyle\sum _ {i=1} ^ {N}\sum _ {j=i} ^ Nf(i,j)
for
f(i,j)\ (1\leq i\leq j\leq N)
defined as follows:
\[f(i,j)=\left\{\begin{matrix}
A _ i&(i=j)\\
f(i,j-1)\barwedge A _ j\quad&(i\lt j)
\end{matrix}\right.\]
Here,
\barwedge
, NAND, is a binary operator satisfying the following:
\[0\barwedge0=1,0\barwedge1=1,1\barwedge0=1,1\barwedge1=0.\] | [
{
"input": "5\n00110\n",
"output": "9\n"
},
{
"input": "30\n101010000100101011010011000010\n",
"output": "326\n"
}
] |
https://atcoder.jp/contests/abc310/tasks/abc310_f | Problem Statement
We have
N
dice.
For each
i = 1, 2, \ldots, N
, when the
i
-th die is thrown, it shows a random integer between
1
and
A_i
, inclusive, with equal probability.
Find the probability, modulo
998244353
, that the following condition is satisfied when the
N
dice are thrown simultaneously.
There is a way to choose some (possibly all) of the
N
dice so that the sum of their results is
10
.
How to find a probability modulo
998244353
It can be proved that the sought probability is always a rational number. Additionally, the constraints of this problem guarantee that if the sought probability is represented as an irreducible fraction
\frac{y}{x}
, then
x
is not divisible by
998244353
.
Here, there is a unique integer
z
such that
xz \equiv y \pmod{998244353}
. Report this
z
. | [
{
"input": "4\n1 7 2 9\n",
"output": "942786334\n"
},
{
"input": "7\n1 10 100 1000 10000 100000 1000000\n",
"output": "996117877\n"
}
] |
https://atcoder.jp/contests/abc310/tasks/abc310_g | Problem Statement
There are
N
Takahashi.
The
i
-th Takahashi has an integer
A_i
and
B_i
balls.
An integer
x
between
1
and
K
, inclusive, will be chosen uniformly at random, and they will repeat the following operation
x
times.
For every
i
, the
i
-th Takahashi gives all his balls to the
A_i
-th Takahashi.
Beware that all
N
Takahashi simultaneously perform this operation.
For each
i=1,2,\ldots,N
, find the expected value, modulo
998244353
, of the number of balls the
i
-th Takahashi has at the end of the operations.
How to find a expected value modulo
998244353
It can be proved that the sought probability is always a rational number. Additionally, the constraints of this problem guarantee that if the sought probability is represented as an irreducible fraction
\frac{y}{x}
, then
x
is not divisible by
998244353
.
Here, there is a unique
0\leq z\lt998244353
such that
y\equiv xz\pmod{998244353}
, so report this
z
. | [
{
"input": "5 2\n3 1 4 1 5\n1 1 2 3 5\n",
"output": "3 0 499122179 499122178 5\n"
},
{
"input": "3 1000\n1 1 1\n1 10 100\n",
"output": "111 0 0\n"
},
{
"input": "16 1000007\n16 12 6 12 1 8 14 14 5 7 6 5 9 6 10 9\n719092922 77021920 539975779 254719514 967592487 476893866 368936979 465399... |
https://atcoder.jp/contests/abc310/tasks/abc310_h | Problem Statement
A monster with health
H
has appeared right in front of you.
Your magic power is now
0
.
You can use
N
moves called move
1
, move
2
,
\ldots
, move
N
, any number of times in any order.
For each
i = 1, 2, \ldots, N
, move
i
can only be used when your magic power is at least
C_i
, and its use will decrease your magic power by
C_i
and the monster's health by
D_i
.
Here, if
C_i
is negative, decreasing your magic power by
C_i
means increasing it by
-C_i
.
Find the minimum possible number of times you use moves before the monster's health is
0
or lower.
The constraints of this problem guarantee that a finite number of uses of moves can make it
0
or lower (see below). | [
{
"input": "3 48\n3 20\n-4 2\n1 5\n",
"output": "5\n"
},
{
"input": "20 583988303060450752\n-64 273760634\n-238 960719353\n-114 191410838\n-250 357733867\n232 304621362\n-286 644706927\n210 37849132\n-230 556412112\n-142 136397527\n101 380675202\n-140 152300688\n190 442931589\n-187 940659077\n-12 31... |
https://atcoder.jp/contests/arc164/tasks/arc164_a | Problem Statement
You are given integers
N
and
K
.
Is it possible to express
N
as the sum of exactly
K
numbers of the form
3^m
(
m
is a non-negative integer)?
In other words, is there a sequence of non-negative integers
(m_1, m_2,\ldots, m_K)
such that:
N= 3^{m_1}+3^{m_2}+...+3^{m_K}
?
You are given
T
test cases. Answer each of them. | [
{
"input": "4\n5 3\n17 2\n163 79\n1000000000000000000 1000000000000000000\n",
"output": "Yes\nNo\nYes\nYes\n"
}
] |
https://atcoder.jp/contests/arc164/tasks/arc164_b | Problem Statement
There is a simple connected undirected graph with
N
vertices numbered from
1
to
N
.
This graph has
M
edges, and the
i
-th edge connects two vertices
a_i
and
b_i
.
Each vertex has a color, either white or black, and the initial state is given by
c_i
.
c_i
is either
0
or
1
, where
c_i=0
means that vertex
i
is initially white, and
c_i=1
means that vertex
i
is initially black.
On this graph, you can choose any vertex as your starting point and repeat the following operation as many times as you like.
Move to a vertex of a different color connected by an edge from the current vertex. Immediately after moving, reverse the color of the vertex you moved from (change to black if it was white, and vice versa).
Is it possible to return to the starting point after performing the operation at least once? | [
{
"input": "4 4\n1 2\n2 3\n3 4\n4 2\n0 1 0 1\n",
"output": "Yes\n"
},
{
"input": "5 6\n1 2\n2 3\n3 4\n4 5\n1 4\n2 5\n0 1 0 1 0\n",
"output": "No\n"
}
] |
https://atcoder.jp/contests/arc164/tasks/arc164_c | Problem Statement
There are
N
cards, each with a number written on both sides. On the
i
-th card, the number
A_i
is written in red on one side, and the number
B_i
is written in blue on the other side. Initially, all cards are placed with the red number side facing up. Alice and Bob play a game in which they repeat the following steps.
First, Alice chooses one of the remaining cards and flips it over. Next, Bob removes one of the remaining cards. Then, Bob scores points equal to the number written on the face-up side of the removed card.
The game ends when there are no cards left.
Alice tries to minimize Bob's score at the end of the game, and Bob tries to maximize it. What is Bob's score at the end of the game when both players take optimal steps? | [
{
"input": "3\n6 4\n2 1\n5 3\n",
"output": "12\n"
},
{
"input": "5\n166971716 552987438\n219878198 619875818\n918378176 518975015\n610749017 285601372\n701849287 307601390\n",
"output": "3078692091\n"
}
] |
https://atcoder.jp/contests/arc164/tasks/arc164_d | Problem Statement
Consider the following problem for a string
s
of length
2N
formed by
N
+
and
N
-
, and let
p(s)
denote the answer.
There are
2N
balls lined up at positions
x=1,2,3,\ldots , 2N
on a number line, of which
N
have a charge of
+1
and the remaining
N
have a charge of
-1
. The arrangement of the charges of the balls is represented by
s
. If the
i
-th character of
s
is
+
, a ball with a charge of
+1
is placed at
x=i
; if it is
-
, a ball with a charge of
-1
is placed at
x=i
.
Each ball starts motion simultaneously according to the following rules. Here, we call the direction where smaller numbers are located on the number line "left", and the direction where larger numbers are located "right".
For each ball, define
F
at each moment by the following formula:
F=\lbrace
(
the sum of the charges of the balls strictly to the left of itself
)
-
(
the sum of the charges of the balls strictly to the right of itself
)
\rbrace
\times
(
the charge of itself
)
.
At each moment, each ball moves to the right if
F
is positive, and to the left if
F
is negative, at a speed of
1
per second.
If a ball with a charge of
+1
and a ball with a charge of
-1
exist at the same coordinate simultaneously, they cancel out each other and disappear.
Then, what is the sum of the distances moved by the balls after they start motion and before they disappear (the distance moved by a ball is the absolute difference between the coordinates where it starts and where it disappears)?
You are given a string
T
of length
2N
consisting of
+
,
-
, and
?
. Find the sum of
p(s)
over all strings
s
formed by
N
+
and
N
-
that can be obtained by replacing each
?
in
T
with
+
or
-
, modulo
998244353
.
Under the given constraints and the rules of motion, it can be shown that all balls disappear in finite time, that the value of
F
for each ball does not become
0
until that ball disappears, that there is no moment when three or more balls are at the same coordinate simultaneously, and that
p(s)
is an integer. | [
{
"input": "2\n+??-\n",
"output": "6\n"
},
{
"input": "17\n??????????????????????????????????\n",
"output": "285212526\n"
}
] |
https://atcoder.jp/contests/arc164/tasks/arc164_e | Problem Statement
There is a sequence of
N
terms, and
Q
queries will be given on this sequence. The
i
-th query is for the interval
[L_i, R_i]
(the interval
[a,b]
is a set of integers between
a
and
b
, inclusive).
You will answer this problem using a binary tree that satisfies the following conditions. Here,
i,j,k
represent integers.
Each node has an interval.
The root node has the interval
[1, N]
.
The node with the interval
[i, i]
is a leaf. Also, the interval of a leaf can be represented as
[i,i]
.
Each non-leaf node has exactly two children. Also, if the interval of a non-leaf node is
[i,j]
, the intervals of the children of this node are
[i,k]
and
[k+1,j]
(
i\leq k<j
).
In this binary tree, when a query for the interval
[L,R]
is given, a search is performed recursively according to the following rules.
Initially, the root is investigated.
When a node is investigated, if the interval of this node is included in
[L, R]
, its descendants are not investigated.
When a node is investigated, if the interval of this node have no intersection with
[L,R]
, its descendants are not investigated.
When a node is investigated, if neither 2. nor 3. applies, the two child nodes are investigated. (It can be shown that either 2. or 3. always applies to a leaf node.)
When answering
Q
queries, let
d
be the maximum depth (distance from the root) of the investigated nodes, and
c
be the total number of times nodes of depth
d
are investigated. Here, if multiple nodes of depth
d
are investigated in a single query, or if the same node is investigated in multiple queries, all those investigations count separately.
You want to design the binary tree to minimize
d
, and then to minimize
c
while minimizing
d
. What are the values of
d
and
c
then? | [
{
"input": "6 4\n2 3\n3 4\n2 4\n3 3\n",
"output": "3 4\n"
},
{
"input": "12 6\n1 10\n2 7\n3 6\n4 9\n5 8\n11 12\n",
"output": "4 4\n"
}
] |
https://atcoder.jp/contests/arc164/tasks/arc164_f | Problem Statement
You are given a rooted tree with
N
vertices numbered from
1
to
N
, rooted at vertex
1
. The parent of vertex
i
is vertex
p_i
(
2\leq i\leq N
).
Alice and Bob play a game using this tree as follows.
Alice goes first, and Bob goes second. They take turns placing a stone, with a white side and a black side, on a vertex of the tree. Alice places the stone with the white side up, and Bob places the stone with the black side up.
In each turn, a stone can only be placed on a vertex that does not have a stone on itself while all its descendants already have stones on them.
When placing a stone on a vertex, all the stones on the descendants of that vertex are flipped over (the placed stone itself is not flipped).
The game ends when all vertices have stones. Alice's score is the number of stones with the white side up at this point.
Alice tries to maximize her score, and Bob tries to minimize Alice's score. If both play optimally, what will be Alice's score? | [
{
"input": "4\n1 1 2\n",
"output": "2\n"
},
{
"input": "7\n1 1 2 4 4 4\n",
"output": "5\n"
}
] |
https://atcoder.jp/contests/abc309/tasks/abc309_a | Problem Statement
We have the following
3 \times 3
board with integers from
1
through
9
written on it.
You are given two integers
A
and
B
between
1
and
9
, where
A < B
.
Determine if the two squares with
A
and
B
written on them are adjacent horizontally. | [
{
"input": "7 8\n",
"output": "Yes\n"
},
{
"input": "1 9\n",
"output": "No\n"
},
{
"input": "3 4\n",
"output": "No\n"
}
] |
https://atcoder.jp/contests/abc309/tasks/abc309_b | Problem Statement
You are given a grid with
N
rows and
N
columns. An integer
A_{i, j}
is written on the square at the
i
-th row from the top and
j
-th column from the left. Here, it is guaranteed that
A_{i,j}
is either
0
or
1
.
Shift the integers written on the outer squares clockwise by one square each, and print the resulting grid.
Here, the outer squares are those in at least one of the
1
-st row,
N
-th row,
1
-st column, and
N
-th column. | [
{
"input": "4\n0101\n1101\n1111\n0000\n",
"output": "1010\n1101\n0111\n0001\n"
},
{
"input": "2\n11\n11\n",
"output": "11\n11\n"
},
{
"input": "5\n01010\n01001\n10110\n00110\n01010\n",
"output": "00101\n11000\n00111\n00110\n10100\n"
}
] |
https://atcoder.jp/contests/abc309/tasks/abc309_c | Problem Statement
Snuke the doctor prescribed
N
kinds of medicine for Takahashi. For the next
a_i
days (including the day of the prescription), he has to take
b_i
pills of the
i
-th medicine. He does not have to take any other medicine.
Let the day of the prescription be day
1
. On or after day
1
, when is the first day on which he has to take
K
pills or less? | [
{
"input": "4 8\n6 3\n2 5\n1 9\n4 2\n",
"output": "3\n"
},
{
"input": "4 100\n6 3\n2 5\n1 9\n4 2\n",
"output": "1\n"
},
{
"input": "15 158260522\n877914575 2436426\n24979445 61648772\n623690081 33933447\n476190629 62703497\n211047202 71407775\n628894325 31963982\n822804784 50968417\n4303... |
https://atcoder.jp/contests/abc309/tasks/abc309_d | Problem Statement
We have an undirected graph with
(N_1+N_2)
vertices and
M
edges. For
i=1,2,\ldots,M
, the
i
-th edge connects vertex
a_i
and vertex
b_i
.
The following properties are guaranteed:
Vertex
u
and vertex
v
are connected, for all integers
u
and
v
with
1 \leq u,v \leq N_1
.
Vertex
u
and vertex
v
are connected, for all integers
u
and
v
with
N_1+1 \leq u,v \leq N_1+N_2
.
Vertex
1
and vertex
(N_1+N_2)
are disconnected.
Consider performing the following operation exactly once:
choose an integer
u
with
1 \leq u \leq N_1
and an integer
v
with
N_1+1 \leq v \leq N_1+N_2
, and add an edge connecting vertex
u
and vertex
v
.
We can show that vertex
1
and vertex
(N_1+N_2)
are always connected in the resulting graph; so let
d
be the minimum length (number of edges) of a path between vertex
1
and vertex
(N_1+N_2)
.
Find the maximum possible
d
resulting from adding an appropriate edge to add.
Definition of "connected"
Two vertices
u
and
v
of an undirected graph are said to be connected if and only if there is a path between vertex
u
and vertex
v
. | [
{
"input": "3 4 6\n1 2\n2 3\n4 5\n4 6\n1 3\n6 7\n",
"output": "5\n"
},
{
"input": "7 5 20\n10 11\n4 5\n10 12\n1 2\n1 5\n5 6\n2 4\n3 5\n9 10\n2 5\n1 4\n11 12\n9 12\n8 9\n5 7\n3 7\n3 6\n3 4\n8 12\n9 11\n",
"output": "4\n"
}
] |
https://atcoder.jp/contests/abc309/tasks/abc309_e | Problem Statement
There is a family consisting of person
1
, person
2
,
\ldots
, and person
N
. For
i\geq 2
, person
i
's parent is person
p_i
.
They bought insurance
M
times. For
i=1,2,\ldots,M
, person
x_i
bought the
i
-th insurance, which covers that person and their descendants in the next
y_i
generations.
How many people are covered by at least one insurance? | [
{
"input": "7 3\n1 2 1 3 3 3\n1 1\n1 2\n4 3\n",
"output": "4\n"
},
{
"input": "10 10\n1 1 3 1 2 3 3 5 7\n2 1\n5 1\n4 3\n6 3\n2 1\n7 3\n9 2\n1 2\n6 2\n8 1\n",
"output": "10\n"
}
] |
https://atcoder.jp/contests/abc309/tasks/abc309_f | Problem Statement
There are
N
boxes. The
i
-th box has a shape of a rectangular cuboid whose height, width, and depth are
h_i,w_i
, and
d_i
, respectively.
Determine if there are two boxes such that one's height, width, and depth are strictly greater than those of the other after rotating them if necessary. | [
{
"input": "3\n19 8 22\n10 24 12\n15 25 11\n",
"output": "Yes\n"
},
{
"input": "3\n19 8 22\n10 25 12\n15 24 11\n",
"output": "No\n"
},
{
"input": "2\n1 1 2\n1 2 2\n",
"output": "No\n"
}
] |
https://atcoder.jp/contests/abc309/tasks/abc309_g | Problem Statement
Find the number, modulo
998244353
, of permutations
P=(P_1,P_2,\dots,P_N)
of
(1,2,\dots,N)
such that:
|P_i - i| \ge X
for all integers
i
with
1 \le i \le N
. | [
{
"input": "3 1\n",
"output": "2\n"
},
{
"input": "5 2\n",
"output": "4\n"
},
{
"input": "98 5\n",
"output": "809422418\n"
}
] |
https://atcoder.jp/contests/abc309/tasks/abc309_h | Problem Statement
We have a grid with
N
rows and
M
columns. We denote by
(i,j)
the cell in the
i
-th row from the top and
j
-th column from the left.
You are given integer sequences
A=(A_1,A_2,\dots,A_K)
and
B=(B_1,B_2,\dots,B_L)
of lengths
K
and
L
, respectively.
Find the sum, modulo
998244353
, of the answers to the following question over all integer pairs
(i,j)
such that
1 \le i \le K
and
1 \le j \le L
.
A piece is initially placed at
(1,A_i)
. How many paths are there to take it to
(N,B_j)
by repeating the following move
(N-1)
times?
Let
(p,q)
be the piece's current cell. Move it to
(p+1,q-1),(p+1,q)
, or
(p+1,q+1)
, without moving it outside the grid. | [
{
"input": "3 4 1 2\n1\n1 2\n",
"output": "4\n"
},
{
"input": "5 8 4 5\n3 1 4 1\n2 7 1 8 2\n",
"output": "137\n"
},
{
"input": "883671387 87719 10 12\n86879 64174 47274 41688 17713 50897 53989 7210 30894 5714\n60358 28835 48036 48450 67149 36558 35929 69025 77539 19195 60762 60721\n",
... |
https://atcoder.jp/contests/arc163/tasks/arc163_a | Problem Statement
You are given a string
S
of length
N
consisting of lowercase English letters. Determine whether it is possible to divide
S
into two or more consecutive substrings so that they are strictly increasing in lexicographical order.
To be precise, determine whether there is a sequence of strings
t=(t_1,t_2,\dots,t_k)
that satisfies all of the following conditions.
The length of the sequence
k
is at least
2
.
t_i
is not empty. (
1 \le i \le k
)
Concatenating
t_1,t_2,\dots,t_k
in this order results in
S
.
t_i
is lexicographically smaller than
t_{i+1}
for every integer
i
such that
1 \le i < k
.
You are given
T
test cases. Find the answer for each of them.
What is lexicographical order?
A string
S = S_1S_2\ldots S_{|S|}
is said to be
lexicographically smaller
than a string
T = T_1T_2\ldots T_{|T|}
if either 1. or 2. below holds.
Here,
|S|
and
|T|
represent the lengths of
S
and
T
, respectively.
|S| \lt |T|
and
S_1S_2\ldots S_{|S|} = T_1T_2\ldots T_{|S|}
.
There is an integer
1 \leq i \leq \min\lbrace |S|, |T| \rbrace
such that both of the following hold.
S_1S_2\ldots S_{i-1} = T_1T_2\ldots T_{i-1}
.
The character
S_i
comes before
T_i
in alphabetical order. | [
{
"input": "5\n4\nabac\n3\ncac\n2\nab\n12\nabababababab\n5\nedcba\n",
"output": "Yes\nNo\nYes\nYes\nNo\n"
}
] |
https://atcoder.jp/contests/arc163/tasks/arc163_b | Problem Statement
You are given an integer sequence of length
N
:
A=(A_1,A_2,\dots,A_N)
. You can perform the following operation any number of times (possibly zero).
Choose an integer
i
such that
1 \le i \le N
, and increase or decrease
A_i
by
1
.
Your goal is to make at least
M
integers
i(3 \le i \le N)
satisfy
A_1 \le A_i \le A_2
. Find the minimum number of operations required to achieve this goal. | [
{
"input": "3 1\n2 3 5\n",
"output": "2\n"
},
{
"input": "5 2\n1 4 2 3 5\n",
"output": "0\n"
},
{
"input": "8 5\n15 59 64 96 31 17 88 9\n",
"output": "35\n"
}
] |
https://atcoder.jp/contests/arc163/tasks/arc163_c | Problem Statement
Determine whether there is a length-
N
sequence of positive integers
A=(A_1,A_2,\dots,A_N)
that satisfies all of the following conditions, and if it exists, construct one.
\sum_{i=1}^{N} \frac{1}{A_i} = 1
All elements of
A
are distinct.
1 \le A_i \le 10^9(1 \le i \le N)
You are given
T
test cases. Find the answer for each of them. | [
{
"input": "2\n3\n5\n",
"output": "Yes\n2 3 6 \nYes\n3 4 5 6 20 \n"
}
] |
https://atcoder.jp/contests/arc163/tasks/arc163_d | Problem Statement
Consider a directed graph
G
with
N
vertices numbered
1
to
N
that satisfies all of the following conditions.
G
is a tournament. In other words,
G
has no multi-edges or self-loops, and for any two vertices
u,v
of
G
, exactly one of the edges
u \rightarrow v
and
v \rightarrow u
exists.
Among the edges of
G
, exactly
M
are directed from a vertex with a smaller number to a vertex with a larger number.
Find the total number of strongly connected components over all such directed graphs
G
, modulo
998244353
. | [
{
"input": "3 1\n",
"output": "7\n"
},
{
"input": "6 2\n",
"output": "300\n"
},
{
"input": "25 156\n",
"output": "902739687\n"
}
] |
https://atcoder.jp/contests/arc163/tasks/arc163_e | Problem Statement
Alice and Bob are playing a game using a length-
N
sequence of non-negative integers
A=(A_1,A_2,\dots,A_N)
.
Starting with Alice, they take turns performing the following operation. The player who cannot make a move first loses.
Choose a non-negative integer
X
such that there is an integer
i
satisfying
A_i > A_i \oplus X
.
For each
1 \le i \le N
, replace
A_i
with
\min(A_i,A_i \oplus X)
.
Determine who wins when both players play optimally.
Here,
\oplus
represents the bitwise XOR.
You are given
T
test cases. Find the answer for each of them. | [
{
"input": "5\n2\n3 1\n5\n1 1 1 1 1\n4\n0 0 0 0\n4\n8 1 6 4\n5\n3 8 7 12 15\n",
"output": "Bob\nAlice\nBob\nBob\nAlice\n"
}
] |
https://atcoder.jp/contests/arc163/tasks/arc163_f | Problem Statement
PCT-kun created the following problem.
Increasing Problem
You are given a length-
N
sequence of non-negative integers
A_1,A_2,\dots,A_N
. You can perform the following operation any number of times (possibly zero).
Choose an integer
i
such that
1 \le i \le N
, and increase or decrease
A_i
by
1
.
Your goal is to make
A
non-decreasing. Find the minimum number of operations required to achieve this goal.
Thinking that this problem is too easy to be placed at the end of the contest, PCT-kun has revised it as follows.
Many Increasing Problems
There are
M^N
integer sequences
A
of length
N
where all elements are between
1
and
M
, inclusive. Find the sum of the answers to
Increasing Problem
for all those sequences, modulo
998244353
.
Solve
Many Increasing Problems
. | [
{
"input": "2 2\n",
"output": "1\n"
},
{
"input": "6 4\n",
"output": "14668\n"
},
{
"input": "163 702\n",
"output": "20728656\n"
},
{
"input": "98765 99887\n",
"output": "103564942\n"
}
] |
https://atcoder.jp/contests/abc308/tasks/abc308_a | Problem Statement
Given eight integers
S_1,S_2,\dots
, and
S_8
,
print
Yes
if they satisfy all of the following three conditions, and
No
otherwise.
The sequence
(S_1,S_2,\dots,S_8)
is monotonically non-decreasing. In other words,
S_1 \leq S_2 \leq \dots \leq S_8
.
S_1,S_2,\dots
, and
S_8
are all between
100
and
675
, inclusive.
S_1,S_2,\dots
, and
S_8
are all multiples of
25
. | [
{
"input": "125 175 250 300 400 525 600 650\n",
"output": "Yes\n"
},
{
"input": "100 250 300 400 325 575 625 675\n",
"output": "No\n"
},
{
"input": "0 23 24 145 301 413 631 632\n",
"output": "No\n"
}
] |
https://atcoder.jp/contests/abc308/tasks/abc308_b | Problem Statement
Takahashi ate
N
plates of sushi at a sushi restaurant. The color of the
i
-th plate is represented by a string
C_i
.
The price of a sushi corresponds to the color of the plate. For each
i=1,\ldots,M
, the sushi on a plate whose color is represented by a string
D_i
is worth
P_i
yen a plate (yen is the currency of Japan). If the color does not coincide with any of
D_1,\ldots
, and
D_M
, it is worth
P_0
yen a plate.
Find the total amount of the prices of sushi that Takahashi ate. | [
{
"input": "3 2\nred green blue\nblue red\n800 1600 2800\n",
"output": "5200\n"
},
{
"input": "3 2\ncode queen atcoder\nking queen\n10 1 1\n",
"output": "21\n"
}
] |
https://atcoder.jp/contests/abc308/tasks/abc308_c | Problem Statement
N
people numbered
1
through
N
tossed a coin several times. We know that person
i
's tosses resulted in
A_i
heads and
B_i
tails.
Person
i
's
success rate
of the tosses is defined by
\displaystyle\frac{A_i}{A_i+B_i}
. Sort people
1,\ldots,N
in descending order of their success rates, with ties broken in ascending order of their assigned numbers. | [
{
"input": "3\n1 3\n3 1\n2 2\n",
"output": "2 3 1\n"
},
{
"input": "2\n1 3\n2 6\n",
"output": "1 2\n"
},
{
"input": "4\n999999999 1000000000\n333333333 999999999\n1000000000 999999997\n999999998 1000000000\n",
"output": "3 1 4 2\n"
}
] |
https://atcoder.jp/contests/abc308/tasks/abc308_d | Problem Statement
We have a grid with
H
horizontal rows and
W
vertical columns.
We denote by
(i,j)
the cell at the
i
-th row from the top and
j
-th column from the left.
Each cell in the grid has a lowercase English letter written on it. The letter written on
(i,j)
equals the
j
-th character of a given string
S_i
.
Snuke will repeat moving to an adjacent cell sharing a side to travel from
(1,1)
to
(H,W)
.
Determine if there is a path
in which the letters written on the visited cells (including initial
(1,1)
and final
(H,W)
) are
s
\rightarrow
n
\rightarrow
u
\rightarrow
k
\rightarrow
e
\rightarrow
s
\rightarrow
n
\rightarrow \dots
, in the order of visiting.
Here, a cell
(i_1,j_1)
is said to be an adjacent cell of
(i_2,j_2)
sharing a side if and only if
|i_1-i_2|+|j_1-j_2| = 1
.
Formally, determine if there is a sequence of cells
((i_1,j_1),(i_2,j_2),\dots,(i_k,j_k))
such that:
(i_1,j_1) = (1,1),(i_k,j_k) = (H,W)
;
(i_{t+1},j_{t+1})
is an adjacent cell of
(i_t,j_t)
sharing a side, for all
t\ (1 \leq t < k)
; and
the letter written on
(i_t,j_t)
coincides with the
(((t-1) \bmod 5) + 1)
-th character of
snuke
, for all
t\ (1 \leq t \leq k)
. | [
{
"input": "2 3\nsns\neuk\n",
"output": "Yes\n"
},
{
"input": "2 2\nab\ncd\n",
"output": "No\n"
},
{
"input": "5 7\nskunsek\nnukesnu\nukeseku\nnsnnesn\nuekukku\n",
"output": "Yes\n"
}
] |
https://atcoder.jp/contests/abc308/tasks/abc308_e | Problem Statement
You are given a length-
N
sequence
A=(A_1,A_2,\dots,A_N)
consisting of
0
,
1
, and
2
,
and a length-
N
string
S=S_1S_2\dots S_N
consisting of
M
,
E
, and
X
.
Find the sum of
\text{mex}(A_i,A_j,A_k)
over all tuples of integers
(i,j,k)
such that
1 \leq i < j < k \leq N
and
S_iS_jS_k=
MEX
.
Here,
\text{mex}(A_i,A_j,A_k)
denotes the minimum non-negative integer that equals neither
A_i,A_j
, nor
A_k
. | [
{
"input": "4\n1 1 0 2\nMEEX\n",
"output": "3\n"
},
{
"input": "3\n0 0 0\nXXX\n",
"output": "0\n"
},
{
"input": "15\n1 1 2 0 0 2 0 2 0 0 0 0 0 2 2\nEXMMXXXEMEXEXMM\n",
"output": "13\n"
}
] |
https://atcoder.jp/contests/abc308/tasks/abc308_f | Problem Statement
You are in a store to buy
N
items. The regular price of the
i
-th item is
P_i
yen (the currency in Japan).
You have
M
coupons. You can use the
i
-th coupon to buy an item whose regular price is at least
L_i
yen at a
D_i
-yen discount.
Here, each coupon can be used only once. Besides, multiple coupons cannot be used for the same item.
If no coupon is used for an item, you will buy it for a regular price.
Find the minimum possible total amount of money required to buy all the
N
items. | [
{
"input": "3 3\n4 3 1\n4 4 2\n2 3 1\n",
"output": "4\n"
},
{
"input": "10 5\n9 7 1 5 2 2 5 5 7 6\n7 2 7 8 2\n3 2 4 1 2\n",
"output": "37\n"
}
] |
https://atcoder.jp/contests/abc308/tasks/abc308_g | Problem Statement
There is a blackboard on which you can write integers. Initially, no integer is written on the blackboard. Given
Q
queries, process them in order.
The query is of one of the following three kinds:
1 x
: write an
x
on the blackboard.
2 x
: erase an
x
from the blackboard. At the point this query is given, it is guaranteed that at least one
x
is written on the blackboard.
3
: print the minimum possible bitwise XOR of two of the integers written on the blackboard. At the point this query is processed, it is guaranteed that at least two integers are written on the blackboard.
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
2^k
s place (
k \geq 0
) is
1
if exactly one of the
2^k
s places of
A
and
B
is
1
, and
0
otherwise.
For instance,
3 \oplus 5 = 6
(in binary:
011 \oplus 101 = 110
). | [
{
"input": "9\n1 2\n1 10\n3\n1 3\n3\n2 2\n3\n1 10\n3\n",
"output": "8\n1\n9\n0\n"
}
] |
https://atcoder.jp/contests/abc308/tasks/abc308_h | Problem Statement
There is a simple undirected graph with
N
vertices and
M
edges. The edges are initially painted white.
The vertices are numbered
1
through
N
, and the edges are numbered
1
through
M
.
Edge
i
connects vertex
A_i
and vertex
B_i
, and the cost required to paint it black is
C_i
.
"Making a Q" means painting four or more edges so that:
all but one of the edges painted black form a simple cycle, and
the edge painted black not forming the cycle connects a vertex on the cycle and another not on the cycle.
Determine if one can make a Q. If one can, find the minimum total cost required to make a Q. | [
{
"input": "5 6\n1 2 6\n2 3 4\n1 3 5\n2 4 3\n4 5 2\n3 5 1\n",
"output": "15\n"
},
{
"input": "4 4\n1 2 1\n2 3 1\n3 4 1\n1 4 1\n",
"output": "-1\n"
},
{
"input": "6 15\n2 6 48772\n2 4 36426\n1 6 94325\n3 6 3497\n2 3 60522\n4 5 63982\n4 6 4784\n1 2 14575\n5 6 68417\n1 5 7775\n3 4 33447\n3 ... |
https://atcoder.jp/contests/ahc021/tasks/ahc021_a | Problem Statement
There are
N(N+1)/2
balls arranged in an
N
-tiered pyramid as shown in the figure below.
Let
(0, 0)
be the coordinates of the ball at the top of the pyramid, and let
(x,y)
be the coordinates of the
y (0\leq y\leq x)
-th ball from the left in the
x (0\leq x-1)
-th tier from the top.
Each ball is labeled with a number from
0
to
N(N+1)/2-1
, and the numbers on each ball are all different.
You can swap two adjacent balls in six directions in a single operation.
Here, the balls at coordinates
(x_1,y_1)
and
(x_2,y_2)
are adjacent in six directions if one of the following conditions is satisfied.
x_1=x_2-1
and
y_1=y_2-1
x_1=x_2-1
and
y_1=y_2
x_1=x_2
and
y_1=y_2-1
x_1=x_2
and
y_1=y_2+1
x_1=x_2+1
and
y_1=y_2
x_1=x_2+1
and
y_1=y_2+1
By performing this operation at most
10000
times, please arrange the balls so that every ball
(x,y) (0\leq x\leq N-2, 0\leq y\leq x)
except those in the lowest tier has a smaller number than the two balls
(x+1,y), (x+1,y+1)
directly below it.
Please achieve this with as few operations as possible. | [
{
"input": "236\n11 200\n453 2 378\n85 410 239 54\n50 240 113 25 294\n303 231 146 65 155 252\n368 327 321 251 451 182 142\n101 17 43 403 217 161 347 398\n350 287 363 48 80 447 385 233 197\n438 424 439 121 357 380 51 245 57 304\n141 91 100 344 194 250 432 322 58 281 219\n412 266 26 318 269 111 59 450 99 301 36 3... |
https://atcoder.jp/contests/abc307/tasks/abc307_a | Problem Statement
Takahashi has recorded the number of steps he walked for
N
weeks. He walked
A_i
steps on the
i
-th day.
Find the total number of steps Takahashi walked each week.
More precisely, find the sum of the steps for the first week (the
1
-st through
7
-th day), the sum of the steps for the second week (the
8
-th through
14
-th day), and so on. | [
{
"input": "2\n1000 2000 3000 4000 5000 6000 7000 2000 3000 4000 5000 6000 7000 8000\n",
"output": "28000 35000\n"
},
{
"input": "3\n14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59230 78164 6286 20899 86280 34825 34211 70679 82148\n",
"output": "314333 419427 335328\n"... |
https://atcoder.jp/contests/abc307/tasks/abc307_b | Problem Statement
You are given
N
strings
S_1,S_2,\ldots,S_N
consisting of lowercase English letters.
Determine if there are
distinct
integers
i
and
j
between
1
and
N
, inclusive, such that the concatenation of
S_i
and
S_j
in this order is a palindrome.
A string
T
of length
M
is a palindrome if and only if the
i
-th character and the
(M+1-i)
-th character of
T
are the same for every
1\leq i\leq M
. | [
{
"input": "5\nab\nccef\nda\na\nfe\n",
"output": "Yes\n"
},
{
"input": "3\na\nb\naba\n",
"output": "No\n"
},
{
"input": "2\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n",
"output": "Yes\n"
}
] |
https://atcoder.jp/contests/abc307/tasks/abc307_c | Problem Statement
Takahashi has two sheets
A
and
B
, each composed of black squares and transparent squares, and an infinitely large sheet
C
composed of transparent squares.
There is also an ideal sheet
X
for Takahashi composed of black squares and transparent squares.
The sizes of sheets
A
,
B
, and
X
are
H_A
rows
\times
W_A
columns,
H_B
rows
\times
W_B
columns, and
H_X
rows
\times
W_X
columns, respectively.
The squares of sheet
A
are represented by
H_A
strings of length
W_A
,
A_1, A_2, \ldots, A_{H_A}
consisting of
.
and
#
.
If the
j
-th character
(1\leq j\leq W_A)
of
A_i
(1\leq i\leq H_A)
is
.
, the square at the
i
-th row from the top and
j
-th column from the left is transparent; if it is
#
, that square is black.
Similarly, the squares of sheets
B
and
X
are represented by
H_B
strings of length
W_B
,
B_1, B_2, \ldots, B_{H_B}
, and
H_X
strings of length
W_X
,
X_1, X_2, \ldots, X_{H_X}
, respectively.
Takahashi's goal is to create sheet
X
using
all black squares
in sheets
A
and
B
by following the steps below with sheets
A
,
B
, and
C
.
Paste sheets
A
and
B
onto sheet
C
along the grid. Each sheet can be pasted anywhere by translating it, but it cannot be cut or rotated.
Cut out an
H_X\times W_X
area from sheet
C
along the grid. Here, a square of the cut-out sheet will be black if a black square of sheet
A
or
B
is pasted there, and transparent otherwise.
Determine whether Takahashi can achieve his goal by appropriately choosing the positions where the sheets are pasted and the area to cut out, that is, whether he can satisfy both of the following conditions.
The cut-out sheet includes
all black squares
of sheets
A
and
B
. The black squares of sheets
A
and
B
may overlap on the cut-out sheet.
The cut-out sheet coincides sheet
X
without rotating or flipping. | [
{
"input": "3 5\n#.#..\n.....\n.#...\n2 2\n#.\n.#\n5 3\n...\n#.#\n.#.\n.#.\n...\n",
"output": "Yes\n"
},
{
"input": "2 2\n#.\n.#\n2 2\n#.\n.#\n2 2\n##\n##\n",
"output": "No\n"
},
{
"input": "1 1\n#\n1 2\n##\n1 1\n#\n",
"output": "No\n"
},
{
"input": "3 3\n###\n...\n...\n3 3\n... |
https://atcoder.jp/contests/abc307/tasks/abc307_d | Problem Statement
You are given a string
S
of length
N
consisting of lowercase English letters and the characters
(
and
)
.
Print the string
S
after performing the following operation as many times as possible.
Choose and delete a contiguous substring of
S
that starts with
(
, ends with
)
, and does not contain
(
or
)
other than the first and last characters.
It can be proved that the string
S
after performing the operation as many times as possible is uniquely determined without depending on how it is performed. | [
{
"input": "8\na(b(d))c\n",
"output": "ac\n"
},
{
"input": "5\na(b)(\n",
"output": "a(\n"
},
{
"input": "2\n()\n",
"output": "\n"
},
{
"input": "6\n)))(((\n",
"output": ")))(((\n"
}
] |
https://atcoder.jp/contests/abc307/tasks/abc307_e | Problem Statement
There are
N
people numbered from
1
to
N
standing in a circle. Person
1
is to the right of person
2
, person
2
is to the right of person
3
, ..., and person
N
is to the right of person
1
.
We will give each of the
N
people an integer between
0
and
M-1
, inclusive.
Among the
M^N
ways to distribute integers, find the number, modulo
998244353
, of such ways that no two adjacent people have the same integer. | [
{
"input": "3 3\n",
"output": "6\n"
},
{
"input": "4 2\n",
"output": "2\n"
},
{
"input": "987654 456789\n",
"output": "778634319\n"
}
] |
https://atcoder.jp/contests/abc307/tasks/abc307_f | Problem Statement
There are
N
rooms numbered
1
,
2
,
\ldots
,
N
, each with one person living in it, and
M
corridors connecting two different rooms. The
i
-th corridor connects room
U_i
and room
V_i
with a length of
W_i
.
One day (we call this day
0
), the
K
people living in rooms
A_1, A_2, \ldots, A_K
got (newly) infected with a virus. Furthermore, on the
i
-th of the following
D
days
(1\leq i\leq D)
, the infection spread as follows.
People who were infected at the end of the night of day
(i-1)
remained infected at the end of the night of day
i
.
For those who were not infected, they were newly infected if and only if they were living in a room within a distance of
X_i
from at least one room where an infected person was living at the end of the night of day
(i-1)
.
Here, the distance between rooms
P
and
Q
is defined as the minimum possible sum of the lengths of the corridors when moving from room
P
to room
Q
using only corridors.
If it is impossible to move from room
P
to room
Q
using only corridors, the distance is set to
10^{100}
.
For each
i
(
1\leq i\leq N
), print the day on which the person living in room
i
was newly infected. If they were not infected by the end of the night of day
D
, print
-1
. | [
{
"input": "4 4\n1 2 2\n2 3 1\n2 4 3\n3 4 2\n1\n1\n2\n3 3\n",
"output": "0\n1\n1\n2\n"
},
{
"input": "7 7\n1 2 2\n2 3 3\n3 4 1\n4 5 1\n5 6 3\n3 7 1\n4 7 1\n2\n1 6\n2\n2 3\n",
"output": "0\n1\n2\n-1\n2\n0\n-1\n"
},
{
"input": "5 1\n1 2 5\n2\n1 3\n3\n3 7 5\n",
"output": "0\n2\n0\n-1\n-... |
https://atcoder.jp/contests/abc307/tasks/abc307_g | Problem Statement
You are given an integer sequence of length
N
:
A=(A_1,A_2,\ldots,A_N)
.
Takahashi can perform the following two operations any number of times, possibly zero, in any order.
Choose an integer
i
such that
1\leq i\leq N-1
, and decrease
A_i
by
1
and increase
A_{i+1}
by
1
.
Choose an integer
i
such that
1\leq i\leq N-1
, and increase
A_i
by
1
and decrease
A_{i+1}
by
1
.
Find the minimum number of operations required to make the sequence
A
satisfy the following condition:
\lvert A_i-A_j\rvert\leq 1
for any pair
(i,j)
of integers between
1
and
N
, inclusive. | [
{
"input": "3\n2 7 6\n",
"output": "4\n"
},
{
"input": "3\n-2 -5 -2\n",
"output": "2\n"
},
{
"input": "5\n1 1 1 1 -7\n",
"output": "13\n"
}
] |
https://atcoder.jp/contests/abc307/tasks/abc307_h | Problem Statement
There is a length
L
string
S
consisting of uppercase and lowercase English letters displayed on an electronic bulletin board with a width of
W
. The string
S
scrolls from right to left by a width of one character at a time.
The display repeats a cycle of
L+W-1
states, with the first character of
S
appearing from the right edge when the last character of
S
disappears from the left edge.
For example, when
W=5
and
S=
ABC
, the board displays the following seven states in a loop:
ABC..
BC...
C....
....A
...AB
..ABC
.ABC.
(
.
represents a position where no character is displayed.)
More precisely, there are distinct states for
k=0,\ldots,L+W-2
in which the display is as follows.
Let
f(x)
be the remainder when
x
is divided by
L+W-1
. The
(i+1)
-th position from the left of the board displays the
(f(i+k)+1)
-th character of
S
when
f(i+k)<L
, and nothing otherwise.
You are given a length
W
string
P
consisting of uppercase English letters, lowercase English letters,
.
, and
_
.
Find the number of states among the
L+W-1
states of the board that coincide
P
except for the positions with
_
.
More precisely, find the number of states that satisfy the following condition.
For every
i=1,\ldots,W
, one of the following holds.
The
i
-th character of
P
is
_
.
The character displayed at the
i
-th position from the left of the board is equal to the
i
-th character of
P
.
Nothing is displayed at the
i
-th position from the left of the board, and the
i
-th character of
P
is
.
. | [
{
"input": "3 5\nABC\n..___\n",
"output": "3\n"
},
{
"input": "11 15\nabracadabra\n__.._________ab\n",
"output": "2\n"
},
{
"input": "20 30\nabaababbbabaabababba\n__a____b_____a________________\n",
"output": "2\n"
},
{
"input": "1 1\na\n_\n",
"output": "1\n"
}
] |
https://atcoder.jp/contests/atcoder11live/tasks/atcoder11live_a | 問題文
n \times n
マスの盤面がある。
一番左上のマスの座標を
(0, 0)
とし、そこから下方向に
i
マス、右方向に
j
マス進んだ先のマスの座標を
(i, j)
とする。
初期状態で
(si, sj)
の位置に右向きの状態で車のおもちゃが置かれている。
いくつかのマスには初期状態で障害物が設置されており、これらの障害物は取り除いたり他のマスに移動させたりすることは出来ない。
また、
n\times n
の範囲外のマスは全て障害物が置かれているものとして扱う。
残りの空きマスに好きなように障害物を設置せよ。
ただし、車のおもちゃの初期位置には障害物は設置されていないことが保証されており、新たに障害物を設置することも出来ない。
障害物の設置が完了すると、以下のルールに従って車のおもちゃが移動する。
現在の向きに1マス進んだ先に障害物がある場合、その場で右方向に90度回転する。
現在の向きに1マス進んだ先に障害物がない場合、その方向に1マス進む。
この動作を繰り返していると、そのうち既に通ったことのあるマスを同じ向きで通過し、そこから先は同じルートを無限ループすることになる。
無限ループに突入するまでの移動距離が出来るだけ長くなるように障害物を設置せよ。
ここで「無限ループに突入するまでの移動距離」は、以前と同じ位置かつ同じ向きの状態に初めてなった時点での、それまでのルール2の適用回数(最後がルール2の場合はそれを含む)を表す。 | [
{
"input": "20\n10 8\n....................\n........#...........\n....................\n....................\n....................\n.....#.............#\n....................\n...................#\n....................\n....................\n....................\n....................\n....................\n....... |
https://atcoder.jp/contests/arc162/tasks/arc162_a | Problem Statement
There are
N
people, numbered from
1
to
N
, who participated in a round-trip race between two points. The following information is recorded about this race.
The
outward
times of any two people were different, and person
i
(1 \leq i \leq N)
had the
i
-th fastest outward time.
The
round-trip
times (the sum of the outward and return times) of any two people were different, and person
i
(1 \leq i \leq N)
had the
P_i
-th fastest round-trip time.
The person (or persons) with the fastest
return
time was awarded the
fastest return award
.
Here,
P_1, P_2, \dots, P_N
is a permutation of
1, 2, \dots, N
.
How many people could have received the
fastest return award
?
There are
T
test cases. Answer each of them. | [
{
"input": "3\n2\n2 1\n4\n1 2 3 4\n20\n13 2 7 1 5 9 3 4 12 10 15 6 8 14 20 16 19 18 11 17\n",
"output": "1\n4\n7\n"
}
] |
https://atcoder.jp/contests/arc162/tasks/arc162_b | Problem Statement
A permutation
P=(P_1,P_2,\ldots,P_N)
of
(1,2,\ldots,N)
is given.
Determine whether it is possible to rearrange
P
in ascending order by performing the following operation at most
2\times 10^3
times, and if possible, show one such sequence of operations.
Choose integers
i
and
j
such that
1\leq i \leq N-1,0 \leq j \leq N-2
. Let
Q = (Q_1, Q_2,\ldots,Q_{N-2})
be the sequence obtained by removing
(P_i,P_{i+1})
from
P
. Replace
P
with
(Q_1,\ldots,Q_j, P_i, P_{i+1}, Q_{j+1},\ldots,Q_{N-2})
. | [
{
"input": "5\n1 4 2 3 5\n",
"output": "Yes\n1\n3 1\n"
},
{
"input": "2\n2 1\n",
"output": "No\n"
},
{
"input": "4\n3 4 1 2\n",
"output": "Yes\n3\n3 0\n1 2\n3 0\n"
}
] |
https://atcoder.jp/contests/arc162/tasks/arc162_c | Problem Statement
You are given a rooted tree with
N
vertices numbered
1
to
N
. Vertex
1
is the root, and the parent of vertex
i\ (2\leq i \leq N)
is
P_i
.
Some vertices of the rooted tree have non-negative integers from
0
to
N
written on them. This information is given by the sequence
A=(A_1,A_2,\ldots,A_N)
. If
A_i \neq -1
, vertex
i
has the integer
A_i
written on it; if
A_i=-1
, vertex
i
does not have an integer written on it.
Alice and Bob play a game. Alice goes first, and they take turns performing the following operation until all vertices have an integer written on them.
Choose one vertex without an integer written on it and write a non-negative integer between
0
and
N
on it.
After the operations, for each vertex
v
, let
f(v)
be the smallest non-negative integer not written on any vertex (including
v
) in the subtree rooted at vertex
v
.
If there is a vertex
v
such that
f(v) = K
, Alice wins. Otherwise, Bob wins. Determine the winner when both players play optimally.
There are
T
test cases. Answer each of them. | [
{
"input": "2\n4 2\n1 1 2\n-1 -1 3 1\n6 4\n1 2 2 1 3\n-1 -1 -1 -1 -1 -1\n",
"output": "Alice\nBob\n"
}
] |
https://atcoder.jp/contests/arc162/tasks/arc162_d | Problem Statement
In this problem, a rooted directed tree is a rooted tree where all edges are directed from the root to the leaves.
You are given a sequence of non-negative integers
d=(d_1,d_2,\ldots,d_N)
with a sum of
N-1
.
Among the
N
-vertex rooted directed trees with vertex numbered
1
to
N
and vertex
1
as the root, a
good tree
is one that satisfies the following condition:
the out-degree of vertex
i\ (1\leq i \leq N)
is
d_i
.
Furthermore, for a vertex
v
of a good tree, let
f(v)
be the minimum vertex number of the vertices (including
v
) in the subtree rooted at vertex
v
, and
v
is called a
good vertex
if it satisfies
f(v)=v
.
Find the sum of the numbers of good vertices for all good trees, modulo
998244353
. | [
{
"input": "4\n2 0 1 0\n",
"output": "7\n"
},
{
"input": "10\n3 1 0 0 2 0 1 2 0 0\n",
"output": "37542\n"
}
] |
https://atcoder.jp/contests/arc162/tasks/arc162_e | Problem Statement
You are given a sequence of length
N
consisting of integers from
1
to
N
,
A=(A_1,A_2,\ldots,A_N)
.
Find the number, modulo
998244353
, of sequences of length
N
consisting of integers from
1
to
N
,
B=(B_1,B_2,\ldots,B_N)
, that satisfy the following conditions for all
i=1,2,\ldots,N
.
The number of occurrences of
i
in
B
is at most
A_i
.
The number of occurrences of
B_i
in
B
is at most
A_i
. | [
{
"input": "3\n1 2 3\n",
"output": "10\n"
},
{
"input": "4\n4 4 4 4\n",
"output": "256\n"
},
{
"input": "5\n1 1 1 1 1\n",
"output": "120\n"
},
{
"input": "14\n6 5 14 3 6 7 3 11 11 2 3 7 8 10\n",
"output": "628377683\n"
}
] |
https://atcoder.jp/contests/arc162/tasks/arc162_f | Problem Statement
You are given positive integers
N
and
M
. Among the
2^{NM}
matrices
A
with
N
rows and
M
columns where each element is
0
or
1
, find the number, modulo
998244353
, of ones that satisfy the following condition:
A_{a, b} \times A_{c, d} \leq A_{a, d} \times A_{c, b}
for every quadruple of integers
(a, b, c, d)
such that
1 \leq a < c \leq N
and
1 \leq b < d \leq M
. | [
{
"input": "2 2\n",
"output": "13\n"
},
{
"input": "1 30\n",
"output": "75497471\n"
},
{
"input": "400 400\n",
"output": "412670892\n"
}
] |
https://atcoder.jp/contests/abc306/tasks/abc306_a | Problem Statement
You are given a string
S
of length
N
consisting of lowercase English letters.
We denote the
i
-th character of
S
by
S_i
.
Print the string of length
2N
obtained by concatenating
S_1,S_1,S_2,S_2,\dots,S_N
, and
S_N
in this order.
For example, if
S
is
beginner
, print
bbeeggiinnnneerr
. | [
{
"input": "8\nbeginner\n",
"output": "bbeeggiinnnneerr\n"
},
{
"input": "3\naaa\n",
"output": "aaaaaa\n"
}
] |
https://atcoder.jp/contests/abc306/tasks/abc306_b | Problem Statement
You are given a sequence
A=(A_0,A_1,\dots,A_{63})
of length
64
consisting of
0
and
1
.
Find
A_0 2^0 + A_1 2^1 + \dots + A_{63} 2^{63}
. | [
{
"input": "1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
"output": "13\n"
},
{
"input": "1 0 1 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1 1 1 1 1 1 0 0 0 0 1 0 1 0 1 0 1 1 1 1 0 0 1 1 0 0 0 0 1 0 1 0 1 0 1 0 0 ... |
https://atcoder.jp/contests/abc306/tasks/abc306_c | 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
. | [
{
"input": "3\n1 1 3 2 3 2 2 3 1\n",
"output": "1 3 2\n"
},
{
"input": "1\n1 1 1\n",
"output": "1\n"
},
{
"input": "4\n2 3 4 3 4 1 3 1 1 4 2 2\n",
"output": "3 4 1 2\n"
}
] |
https://atcoder.jp/contests/abc306/tasks/abc306_d | Problem Statement
Takahashi has decided to enjoy a wired full-course meal consisting of
N
courses in a restaurant.
The
i
-th course is:
if
X_i=0
, an
antidotal
course with a tastiness of
Y_i
;
if
X_i=1
, a
poisonous
course with a tastiness of
Y_i
.
When Takahashi eats a course, his state changes as follows:
Initially, Takahashi has a healthy stomach.
When he has a
healthy stomach
,
if he eats an
antidotal
course, his stomach
remains healthy
;
if he eats a
poisonous
course, he
gets an upset stomach
.
When he has an
upset stomach
,
if he eats an
antidotal
course, his stomach
becomes healthy
;
if he eats a
poisonous
course, he
dies
.
The meal progresses as follows.
Repeat the following process for
i = 1, \ldots, N
in this order.
First, the
i
-th course is served to Takahashi.
Next, he chooses whether to "eat" or "skip" the course.
If he chooses to "eat" it, he eats the
i
-th course. His state also changes depending on the course he eats.
If he chooses to "skip" it, he does not eat the
i
-th course. This course cannot be served later or kept somehow.
Finally, (if his state changes, after the change) if he is not dead,
if
i \neq N
, he proceeds to the next course.
if
i = N
, he makes it out of the restaurant alive.
An important meeting awaits him, so he must make it out of there alive.
Find the
maximum possible sum of tastiness of the courses that he eats
(or
0
if he eats nothing) when he decides whether to "eat" or "skip" the courses under that condition. | [
{
"input": "5\n1 100\n1 300\n0 -200\n1 500\n1 300\n",
"output": "600\n"
},
{
"input": "4\n0 -1\n1 -2\n0 -3\n1 -4\n",
"output": "0\n"
},
{
"input": "15\n1 900000000\n0 600000000\n1 -300000000\n0 -700000000\n1 200000000\n1 300000000\n0 -600000000\n1 -900000000\n1 600000000\n1 -100000000\n1... |
https://atcoder.jp/contests/abc306/tasks/abc306_e | Problem Statement
We have a sequence
A=(A_1,A_2,\dots,A_N)
of length
N
. Initially, all the terms are
0
.
Using an integer
K
given in the input, we define a function
f(A)
as follows:
Let
B
be the sequence obtained by sorting
A
in descending order (so that it becomes monotonically non-increasing).
Then, let
f(A)=B_1 + B_2 + \dots + B_K
.
We consider applying
Q
updates on this sequence.
Apply the following operation on the sequence
A
for
i=1,2,\dots,Q
in this order, and print the value
f(A)
at that point after each update.
Change
A_{X_i}
to
Y_i
. | [
{
"input": "4 2 10\n1 5\n2 1\n3 3\n4 2\n2 10\n1 0\n4 0\n3 1\n2 0\n3 0\n",
"output": "5\n6\n8\n8\n15\n13\n13\n11\n1\n0\n"
}
] |
https://atcoder.jp/contests/abc306/tasks/abc306_f | Problem Statement
For two sets of integers,
A
and
B
, such that
A \cap B = \emptyset
, we define
f(A,B)
as follows.
Let
C=(C_1,C_2,\dots,C_{|A|+|B|})
be a sequence consisting of the elements of
A \cup B
, sorted in ascending order.
Take
k_1,k_2,\dots,k_{|A|}
such that
A=\lbrace C_{k_1},C_{k_2},\dots,C_{k_{|A|}}\rbrace
.
Then, let
\displaystyle f(A,B)=\sum_{i=1}^{|A|} k_i
.
For example, if
A=\lbrace 1,3\rbrace
and
B=\lbrace 2,8\rbrace
, then
C=(1,2,3,8)
, so
A=\lbrace C_1,C_3\rbrace
; thus,
f(A,B)=1+3=4
.
We have
N
sets of integers,
S_1,S_2\dots,S_N
, each of which has
M
elements. For each
i\ (1 \leq i \leq N)
,
S_i = \lbrace A_{i,1},A_{i,2},\dots,A_{i,M}\rbrace
.
Here, it is guaranteed that
S_i \cap S_j = \emptyset\ (i \neq j)
.
Find
\displaystyle \sum_{1\leq i<j \leq N} f(S_i, S_j)
. | [
{
"input": "3 2\n1 3\n2 8\n4 6\n",
"output": "12\n"
},
{
"input": "1 1\n306\n",
"output": "0\n"
},
{
"input": "4 4\n155374934 164163676 576823355 954291757\n797829355 404011431 353195922 138996221\n191890310 782177068 818008580 384836991\n160449218 545531545 840594328 501899080\n",
"... |
https://atcoder.jp/contests/abc306/tasks/abc306_g | Problem Statement
We have a directed graph with
N
vertices and
M
edges.
The vertices are numbered from
1
through
N
, and the
i
-th edge goes from vertex
U_i
to vertex
V_i
.
You are currently at vertex
1
.
Determine if you can make the following move
10^{10^{100}}
times to end up at vertex
1
:
choose an edge going from the vertex you are currently at, and move to the vertex that the edge points at.
Given
T
test cases, solve each of them. | [
{
"input": "4\n2 2\n1 2\n2 1\n3 3\n1 2\n2 3\n3 1\n7 10\n1 6\n6 3\n1 4\n5 1\n7 1\n4 5\n2 1\n4 7\n2 7\n4 3\n7 11\n1 6\n6 3\n1 4\n5 1\n7 1\n4 5\n2 1\n4 7\n2 7\n4 3\n3 7\n",
"output": "Yes\nNo\nNo\nYes\n"
}
] |
https://atcoder.jp/contests/abc306/tasks/abc306_h | Problem Statement
There are
N
weights numbered
1,2, \dots,N
.
Using a balance, we will compare weights
M
times.
Before the comparisons, prepare an empty string
S
.
For the
i
-th comparison, put just weight
A_i
to the left bowl, and just weight
B_i
to the right.
Then, one of the following three results is obtained.
If weight
A_i
is heavier than weight
B_i
,
append
>
to the tail of
S
.
If weight
A_i
and weight
B_i
have the same mass,
append
=
to the tail of
S
.
If weight
A_i
is lighter than weight
B_i
,
append
<
to the tail of
S
.
The result is always accurate.
After the experiment, you will obtain a string
S
of length
M
.
Among the
3^M
strings of length
M
consisting of
>
,
=
, and
<
, how many can be obtained as
S
by the experiment?
Since the answer can be enormous, print the answer modulo
998244353
. | [
{
"input": "3 3\n1 2\n1 3\n2 3\n",
"output": "13\n"
},
{
"input": "4 4\n1 4\n2 3\n1 3\n3 4\n",
"output": "39\n"
},
{
"input": "14 15\n1 2\n1 3\n2 4\n2 5\n2 6\n4 8\n5 6\n6 8\n7 8\n9 10\n9 12\n9 13\n10 11\n11 12\n11 13\n",
"output": "1613763\n"
}
] |
https://atcoder.jp/contests/ahc020/tasks/ahc020_a | Problem Statement
You are given a weighted planar undirected graph
G
with
N
vertices and
M
edges.
The coordinates of vertex
i
are
(x_i, y_i)
.
The
j
-th edge connects vertices
u_j
and
v_j
with the weight
w_j
.
Let
D_j=\mathrm{round}\left(\sqrt{(x_{u_j}-x_{v_j})^2+(y_{u_j}-y_{v_j})^2}\right)
be the rounded Euclidean distance between vertices
u_j
and
v_j
.
Then, weight
w_j
satisfies
100D_j\le w_j\le 2500D_j
.
You are also given
K
coordinates of the residents, and the coordinates of
k
-th resident are
(a_k, b_k)
.
You should set the
power
ON/OFF for each edge and the
output strength
integer
P_i\ (0\le P_i\le 5000)
for each vertex
i=1,2,\cdots,N
.
Let
E'
be the set of edges whose
power
is ON.
Consider a subgraph
G'
obtained from
G
by removing edges not included in
E'
, and let
V'
be the set of vertices reachable from vertex
1
in
G'
.
For each
i\in V'
, residents living within a circular region of radius
P_i
centered at coordinates
(x_i, y_i)
(including the circumference) will be able to view the live broadcast.
Setting the
power
of edge
j
to ON incurs a cost
w_j
.
Also, setting the
output strength
of vertex
i
to
P_i
incurs a cost
P_i^2
.
You may set
P_i
to a positive value for
i\notin V'
, but this will not expand the broadcasting coverage area and incurs unnecessary costs.
Please build a TV network that can deliver live broadcasts to all residents while reducing the sum of the costs
S=\sum_{i=1}^N{P_i^2}+\sum_{j\in E'} w_j
as small as possible. | [
{
"input": "100 284 5000\n0 0\n957 -6517\n-412 -5500\n-299 4489\n-8357 6534\n1974 -6619\n1410 -8691\n8833 -9829\n7785 2682\n-9839 443\n-5084 6063\n8064 4821\n7008 1946\n2894 -5980\n-3445 9927\n-1403 3591\n5030 5758\n3178 1199\n-9263 -3926\n-3126 -2879\n4179 4360\n5715 595\n7209 -8569\n823 1184\n-746 749\n-3409 ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.