url
stringlengths 49
92
| description
stringlengths 22
4.78k
| cases
listlengths 0
6
|
---|---|---|
https://atcoder.jp/contests/arc158/tasks/arc158_a
|
Problem Statement
You are given integers
x_1
,
x_2
, and
x_3
. For these integers, you can perform the following operation any number of times, possibly zero.
Choose a permutation
(i,j,k)
of
(1,2,3)
, that is, a triple of integers
(i,j,k)
such that
1\leq i,j,k\leq 3
and
i\neq j, i\neq k, j\neq k
.
Then, simultaneously replace
x_i
with
x_i+3
,
x_j
with
x_j+5
, and
x_k
with
x_k+7
.
Your objective is to satisfy
x_1=x_2=x_3
. Determine whether it is achievable. If it is, print the minimum number of times you need to perform the operation to achieve it.
You have
T
test cases to solve.
|
[
{
"input": "4\n2 8 8\n1 1 1\n5 5 10\n10 100 1000\n",
"output": "2\n0\n-1\n315\n"
}
] |
https://atcoder.jp/contests/arc158/tasks/arc158_b
|
Problem Statement
You are given non-zero integers
x_1, \ldots, x_N
. Find the minimum and maximum values of
\dfrac{x_i+x_j+x_k}{x_ix_jx_k}
for integers
i
,
j
,
k
such that
1\leq i < j < k\leq N
.
|
[
{
"input": "4\n-2 -4 4 5\n",
"output": "-0.175000000000000\n-0.025000000000000\n"
},
{
"input": "4\n1 1 1 1\n",
"output": "3.000000000000000\n3.000000000000000\n"
},
{
"input": "5\n1 2 3 4 5\n",
"output": "0.200000000000000\n1.000000000000000\n"
}
] |
https://atcoder.jp/contests/arc158/tasks/arc158_c
|
Problem Statement
For a positive integer
x
, let
f(x)
denote the sum of its digits. For instance,
f(158) = 1 + 5 + 8 = 14
,
f(2023) = 2 + 0 + 2 + 3 = 7
, and
f(1) = 1
.
You are given a sequence of positive integers
A = (A_1, \ldots, A_N)
. Find
\sum_{i=1}^N\sum_{j=1}^N f(A_i + A_j)
.
|
[
{
"input": "2\n53 28\n",
"output": "36\n"
},
{
"input": "1\n999999999999999\n",
"output": "135\n"
},
{
"input": "5\n123 456 789 101 112\n",
"output": "321\n"
}
] |
https://atcoder.jp/contests/arc158/tasks/arc158_d
|
Problem Statement
You are given a positive integer
n
, and a prime number
p
at least
5
.
Find a triple of integers
(x,y,z)
that satisfies all of the following conditions.
1\leq x < y < z \leq p - 1
.
(x+y+z)(x^n+y^n+z^n)(x^{2n}+y^{2n}+z^{2n}) \equiv x^{3n}+y^{3n}+z^{3n}\pmod{p}
.
It can be proved that such a triple
(x,y,z)
always exists.
You have
T
test cases to solve.
|
[
{
"input": "3\n1 7\n2 7\n10 998244353\n",
"output": "1 4 6\n1 2 5\n20380119 21549656 279594297\n"
}
] |
https://atcoder.jp/contests/arc158/tasks/arc158_e
|
Problem Statement
We have a grid with
2
rows and
N
columns. Let
(i,j)
denote the square at the
i
-th row from the top and
j
-th column from the left.
(i,j)
has a postive integer
x_{i,j}
written on it.
Two squares are said to be
adjacent
when they share a side.
A
path
from square
X
to
Y
is a sequence
(P_1, \ldots, P_n)
of distinct squares such that
P_1 = X
,
P_n = Y
, and
P_i
and
P_{i+1}
are adjacent for every
1\leq i \leq n-1
. Additionally, the
weight
of that path is the sum of integers written on
P_1, \ldots, P_n
.
For two squares
X
and
Y
, let
f(X, Y)
denote the minimum weight of a path from
X
to
Y
. Find the sum of
f(X, Y)
over all pairs of squares
(X,Y)
, modulo
998244353
.
|
[
{
"input": "1\n3\n5\n",
"output": "24\n"
},
{
"input": "2\n1 2\n3 4\n",
"output": "76\n"
},
{
"input": "5\n1 1000000000 1 1 1\n1 1 1 1000000000 1\n",
"output": "66714886\n"
}
] |
https://atcoder.jp/contests/arc158/tasks/arc158_f
|
Problem Statement
For non-negative integers
x
and
k
, the
k
-th lowest digit of
x
is the remainder when
\bigl\lfloor \frac{x}{10^k}\bigr\rfloor
is divided by
10
. For instance, the
0
-th,
1
-st,
2
-nd, and
3
-rd lowest digits of
123
are
3
,
2
,
1
, and
0
, respectively.
You are given positive integers
N
,
M
,
K
, and sequences of non-negative integers
A = (A_1, \ldots, A_N)
and
B = (B_1, \ldots, B_N)
.
Consider rearranging
A
by the following process.
Do the following
M
times.
Choose an integer
k
such that
0\leq k \leq K - 1
.
Then, perform a stable sort on
A
by
k
-th lowest digit. That is, let
A^{(d)}
be the subsequence of
A
composed of all elements of
A
whose
k
-th lowest digits are
d
, and replace
A
with the concatenation of
A^{(0)}, A^{(1)}, \ldots, A^{(9)}
in this order.
There are
K^M
ways to execute this process. How many of them end up making
A
equal
B
? Find this count modulo
998244353
.
|
[
{
"input": "3 2 3\n74 42 54\n42 54 74\n",
"output": "5\n"
},
{
"input": "2 1 1\n2 3\n3 2\n",
"output": "0\n"
},
{
"input": "5 100 4\n0 12 34 56 78\n0 12 34 56 78\n",
"output": "982924732\n"
}
] |
https://atcoder.jp/contests/abc293/tasks/abc293_a
|
Problem Statement
You are given a string
S
of even length consisting of lowercase English letters. Let
|S|
be the length of
S
, and
S_i
be the
i
-th character of
S
.
Perform the following operation for each
i = 1, 2, \ldots, \frac{|S|}{2}
in this order, and print the final
S
.
Swap
S_{2i-1}
and
S_{2i}
.
|
[
{
"input": "abcdef\n",
"output": "badcfe\n"
},
{
"input": "aaaa\n",
"output": "aaaa\n"
},
{
"input": "atcoderbeginnercontest\n",
"output": "taocedbrgeniencrnoetts\n"
}
] |
https://atcoder.jp/contests/abc293/tasks/abc293_b
|
Problem Statement
There are
N
people whose IDs are
1
,
2
,
\ldots
, and
N
.
Each of person
1
, person
2
,
\ldots
, and person
N
performs the following action once in this order:
If person
i
's ID has not been called out yet, call out person
A_i
's ID.
Enumerate the IDs of all the people whose IDs are never called out until the end
in ascending order
.
|
[
{
"input": "5\n3 1 4 5 4\n",
"output": "2\n2 4\n"
},
{
"input": "20\n9 7 19 7 10 4 13 9 4 8 10 15 16 3 18 19 12 13 2 12\n",
"output": "10\n1 2 5 6 8 11 14 17 18 20\n"
}
] |
https://atcoder.jp/contests/abc293/tasks/abc293_c
|
Problem Statement
There is a grid with
H
horizontal rows and
W
vertical columns.
For two integers
i
and
j
such that
1 \leq i \leq H
and
1 \leq j \leq W
,
the square at the
i
-th row from the top and
j
-th column from the left (which we denote by
(i, j)
) has an integer
A_{i, j}
written on it.
Takahashi is currently at
(1,1)
.
From now on, he repeats moving to an adjacent square to the right of or below his current square until he reaches
(H, W)
.
When he makes a move, he is not allowed to go outside the grid.
Takahashi will be happy if the integers written on the squares he visits (including initial
(1, 1)
and final
(H, W)
) are distinct.
Find the number of his possible paths that make him happy.
|
[
{
"input": "3 3\n3 2 2\n2 1 3\n1 5 4\n",
"output": "3\n"
},
{
"input": "10 10\n1 2 3 4 5 6 7 8 9 10\n11 12 13 14 15 16 17 18 19 20\n21 22 23 24 25 26 27 28 29 30\n31 32 33 34 35 36 37 38 39 40\n41 42 43 44 45 46 47 48 49 50\n51 52 53 54 55 56 57 58 59 60\n61 62 63 64 65 66 67 68 69 70\n71 72 73 74 75 76 77 78 79 80\n81 82 83 84 85 86 87 88 89 90\n91 92 93 94 95 96 97 98 99 100\n",
"output": "48620\n"
}
] |
https://atcoder.jp/contests/abc293/tasks/abc293_d
|
Problem Statement
There are
N
ropes numbered
1
through
N
. One end of each rope is painted red, and the other is painted blue.
You are going to perform
M
operations of tying ropes. In the
i
-th operation, you tie the end of rope
A_i
painted
B_i
with the end of rope
C_i
painted
D_i
, where
R
means red and
B
means blue. For each rope, an end with the same color is not tied multiple times.
Find the number of groups of connected ropes that form cycles, and the number of those that do not, after all the operations.
Here, a group of connected ropes
\lbrace v_0, v_1, \ldots, v_{x-1} \rbrace
is said to form a cycle if one can rearrange the elements of
v
so that, for each
0 \leq i < x
, rope
v_i
is tied to rope
v_{(i+1) \bmod x}
.
|
[
{
"input": "5 3\n3 R 5 B\n5 R 3 B\n4 R 2 B\n",
"output": "1 2\n"
},
{
"input": "7 0\n",
"output": "0 7\n"
},
{
"input": "7 6\n5 R 3 R\n7 R 4 R\n4 B 1 R\n2 R 3 B\n2 B 5 B\n1 B 7 B\n",
"output": "2 1\n"
}
] |
https://atcoder.jp/contests/abc293/tasks/abc293_e
|
Problem Statement
Given integers
A
,
X
, and
M
, find
\displaystyle \sum_{i = 0}^{X-1} A^i
, modulo
M
.
|
[
{
"input": "3 4 7\n",
"output": "5\n"
},
{
"input": "8 10 9\n",
"output": "0\n"
},
{
"input": "1000000000 1000000000000 998244353\n",
"output": "919667211\n"
}
] |
https://atcoder.jp/contests/abc293/tasks/abc293_f
|
Problem Statement
Given an integer
N
not less than
2
, find the number of integers
b
not less than
2
such that:
when
N
is written in base
b
, every digit is
0
or
1
.
Find the answer for
T
independent test cases.
It can be proved that there is a finite number of desired integers
b
not less than
2
under the constraints of this problem.
|
[
{
"input": "3\n12\n2\n36\n",
"output": "4\n1\n5\n"
}
] |
https://atcoder.jp/contests/abc293/tasks/abc293_g
|
Problem Statement
You are given a length-
N
sequence
(A_1, A_2, \ldots, A_N)
of positive integers, and
Q
queries about the sequence.
For each
q = 1, 2, \ldots, Q
, the
q
-th query gives you an integer pair
(l_q, r_q)
;
print the number of integer triplets
(i, j, k)
such that
l_q \leq i \lt j \lt k \leq r_q
, and
A_i = A_j = A_k
.
|
[
{
"input": "10 4\n2 7 1 8 2 8 1 8 2 8\n1 10\n1 9\n2 10\n5 5\n",
"output": "5\n2\n4\n0\n"
},
{
"input": "20 10\n2 2 2 2 1 1 2 2 1 1 1 2 1 2 1 2 2 1 2 1\n12 16\n17 18\n12 18\n4 9\n13 13\n2 5\n6 13\n2 14\n7 14\n8 12\n",
"output": "1\n0\n5\n2\n0\n1\n11\n55\n8\n1\n"
}
] |
https://atcoder.jp/contests/abc293/tasks/abc293_h
|
Problem Statement
You are given a tree with
N
vertices numbered
1
through
N
. The
i
-th edge connects vertex
A_i
and vertex
B_i
.
Find the minimum integer
K
such that you can paint each vertex in a color so that the following two conditions are satisfied. You may use any number of colors.
For each color, the set of vertices painted in the color is connected and forms a simple path.
For all simple paths on the tree, there are at most
K
distinct colors of the vertices in the path.
|
[
{
"input": "7\n3 4\n1 5\n4 5\n1 2\n7 4\n1 6\n",
"output": "3\n"
},
{
"input": "6\n3 5\n6 4\n6 3\n4 2\n1 5\n",
"output": "1\n"
},
{
"input": "9\n1 3\n9 5\n8 7\n2 1\n5 2\n5 8\n4 8\n6 1\n",
"output": "3\n"
}
] |
https://atcoder.jp/contests/toyota-hc-2023spring/tasks/toyota_hc_2023spring_a
|
問題文
トヨタ自動車株式会社では、たくさんの荷物をコンテナに入れて運んでいる。
荷物の集合とコンテナのサイズのデータが与えられるので、全ての荷物をコンテナに上手く積み込む方法を出力せよ。
荷物を積み込む順番は任意ではあるが、積み込み作業の効率化のため、出来るだけ順番通りに積んで欲しい。
|
[
{
"input": "4 1120 680 30 1200\n355 360 285 3 Y Y\n155 210 120 1 Y Y\n510 820 380 1 Y N\n370 710 330 1 Y Y\n",
"output": "0 0 0 30 0\n0 1 360 30 0\n0 0 715 0 0\n1 1 0 385 0\n3 0 30 30 285\n2 0 0 30 615\n"
}
] |
https://atcoder.jp/contests/abc292/tasks/abc292_a
|
Problem Statement
You are given a string
S
consisting of lowercase English letters.
Uppercase each character of
S
and print the resulting string
T
.
|
[
{
"input": "abc\n",
"output": "ABC\n"
},
{
"input": "a\n",
"output": "A\n"
},
{
"input": "abcdefghjiklnmoqprstvuwxyz\n",
"output": "ABCDEFGHJIKLNMOQPRSTVUWXYZ\n"
}
] |
https://atcoder.jp/contests/abc292/tasks/abc292_b
|
Problem Statement
N
players numbered
1
to
N
will play a soccer game.
When a player commits an offense, that player will receive a
yellow card
or a
red card
.
A player who satisfies one of the following conditions will be
removed
from the game.
Accumulates two yellow cards.
Receives a red card.
Once a player is removed, that player will no longer receive any cards.
You will watch this game. Initially, the players have not received any cards.
There will be
Q
events. Correctly answer the questions asked in the events.
There are three kinds of events, which are given in the format
c x
from the input, where
c
is
1
,
2
, or
3
. The events are as follows.
1 x
: Player
x
receives a yellow card.
2 x
: Player
x
receives a red card.
3 x
: You are asked whether player
x
has been removed from the game. Answer
Yes
or
No
.
|
[
{
"input": "3 9\n3 1\n3 2\n1 2\n2 1\n3 1\n3 2\n1 2\n3 2\n3 3\n",
"output": "No\nNo\nYes\nNo\nYes\nNo\n"
}
] |
https://atcoder.jp/contests/abc292/tasks/abc292_c
|
Problem Statement
You are given a positive integer
N
.
Find the number of quadruples of positive integers
(A,B,C,D)
such that
AB + CD = N
.
Under the constraints of this problem, it can be proved that the answer is at most
9 \times 10^{18}
.
|
[
{
"input": "4\n",
"output": "8\n"
},
{
"input": "292\n",
"output": "10886\n"
},
{
"input": "19876\n",
"output": "2219958\n"
}
] |
https://atcoder.jp/contests/abc292/tasks/abc292_d
|
Problem Statement
You are given an 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
.
Determine whether every connected component in this graph satisfies the following condition.
The connected component has the same number of vertices and edges.
|
[
{
"input": "3 3\n2 3\n1 1\n2 3\n",
"output": "Yes\n"
},
{
"input": "5 5\n1 2\n2 3\n3 4\n3 5\n1 5\n",
"output": "Yes\n"
},
{
"input": "13 16\n7 9\n7 11\n3 8\n1 13\n11 11\n6 11\n8 13\n2 11\n3 3\n8 12\n9 11\n1 11\n5 13\n3 12\n6 9\n1 10\n",
"output": "No\n"
}
] |
https://atcoder.jp/contests/abc292/tasks/abc292_e
|
Problem Statement
You are given a simple directed graph with
N
vertices numbered
1
to
N
and
M
edges numbered
1
to
M
. Edge
i
is a directed edge from vertex
u_i
to vertex
v_i
.
You may perform the following operation zero or more times.
Choose distinct vertices
x
and
y
such that there is no directed edge from vertex
x
to vertex
y
, and add a directed edge from vertex
x
to vertex
y
.
Find the minimum number of times you need to perform the operation to make the graph satisfy the following condition.
For every triple of distinct vertices
a
,
b
, and
c
, if there are directed edges from vertex
a
to vertex
b
and from vertex
b
to vertex
c
, there is also a directed edge from vertex
a
to vertex
c
.
|
[
{
"input": "4 3\n2 4\n3 1\n4 3\n",
"output": "3\n"
},
{
"input": "292 0\n",
"output": "0\n"
},
{
"input": "5 8\n1 2\n2 1\n1 3\n3 1\n1 4\n4 1\n1 5\n5 1\n",
"output": "12\n"
}
] |
https://atcoder.jp/contests/abc292/tasks/abc292_f
|
Problem Statement
Find the maximum side length of a regular triangle that can be drawn within a rectangle whose side lengths are
A
and
B
.
|
[
{
"input": "1 1\n",
"output": "1.03527618041008295791\n"
}
] |
https://atcoder.jp/contests/abc292/tasks/abc292_g
|
Problem Statement
You are given a sequence
S_1,\ldots,S_N
of length-
M
strings consisting of digits (
0123456789
) and
?
.
There are
10^q
ways to replace the occurrences of
?
with digits independently, where
q
is the total number of
?
in
S_1,\ldots,S_N
. Among them, how many satisfy
S_1\lt S_2 \lt \ldots \lt S_N
when the resulting strings are seen as integers? Find this count modulo
998244353
.
The resulting strings may have leading zeros. For instance,
0000000292
is seen as
292
.
|
[
{
"input": "3 2\n?0\n??\n05\n",
"output": "4\n"
},
{
"input": "2 1\n0\n0\n",
"output": "0\n"
},
{
"input": "10 10\n1?22??37?4\n1??8?0??49\n3?02??8044\n51?4?8?7??\n5?9?20???2\n68?7?6?800\n?3??2???23\n?442312158\n??2??921?8\n????5?96??\n",
"output": "137811792\n"
}
] |
https://atcoder.jp/contests/abc292/tasks/abc292_h
|
Problem Statement
You will participate in
N
contests, numbered
1
to
N
in chronological order.
A participant in each contest will be given a value called
performance
for that contest. Let
P_i
be the performance for contest
i
.
Additionally, you have a value called
rating
, which changes according to the performances in contests. The initial rating is
0
, and the rating after contest
n
is
\frac{1}{n} \left(\sum_{i=1}^n P_i \right)
.
However, once your rating is
B
or higher
, later contests will not affect your rating.
Before the contests, you have decided to estimate your performance in each contest. Let
a_i
be the initial estimate of your performance in contest
i
. Process
Q
queries in the order they are given.
In each query, you are given two integers
c
and
x
. First, change the estimate of your performance in contest
c
to
x
. (This change is persistent.) Then, assuming that you get the estimated performances in all
N
contests, print your final rating after the contests.
|
[
{
"input": "5 6 7\n5 1 9 3 8\n4 9\n2 10\n1 0\n3 0\n3 30\n5 100\n1 100\n",
"output": "6.000000000000000\n7.500000000000000\n6.333333333333333\n5.400000000000000\n13.333333333333334\n13.333333333333334\n100.000000000000000\n"
}
] |
https://atcoder.jp/contests/past202303-open/tasks/past202303_a
|
Problem Statement
Alice and Bob play a game.
Initially, the pile has
N
stones. The two players alternately remove one stone from the pile.
If
S
is
0
, Alice goes first; if it is
1
, Bob goes first.
If
T
is
0
, the player who removes the last stone wins; if it is
1
, the player who removes the last stone loses (and the other player wins).
Determine which player will win.
|
[
{
"input": "3 0 1\n",
"output": "Bob\n"
},
{
"input": "2 1 0\n",
"output": "Alice\n"
}
] |
https://atcoder.jp/contests/past202303-open/tasks/past202303_b
|
Problem Statement
You are given decimals
A
and
B
with
D
decimal places.
Precisely compute
A+B
and print the result in the same format.
|
[
{
"input": "1\n1.2\n1.8\n",
"output": "3.0\n"
},
{
"input": "3\n1.234\n7.890\n",
"output": "9.124\n"
},
{
"input": "4\n1.2345\n8.7655\n",
"output": "10.0000\n"
}
] |
https://atcoder.jp/contests/past202303-open/tasks/past202303_c
|
Problem Statement
You are given a sequence
P = (P_1, P_2, \ldots, P_N)
that is a permutation of a length-
N
sequence
(1, 2, 3, \ldots, N)
.
For each
K = 1, 2, \ldots, N
, find the integer
X_K
such that
K
is the
X_K
-th element of the sequence
P
.
|
[
{
"input": "5\n5 2 4 1 3\n",
"output": "4 2 5 3 1\n"
},
{
"input": "1\n1\n",
"output": "1\n"
},
{
"input": "10\n10 2 3 7 8 6 1 9 5 4\n",
"output": "7 2 3 10 9 6 4 5 8 1\n"
}
] |
https://atcoder.jp/contests/past202303-open/tasks/past202303_d
|
Problem Statement
You are playing a game, where you buy and use items to beat an enemy with stamina
H
. There are two kinds of items.
Item
1
:
when you use it, the enemy's stamina is reduced by
A
. The price is
B
yen (the currency in Japan).
Item
2
:
when you use it, the enemy's stamina is reduced by
C
. Let
h
be the resulting stamina. Then, if
h>0
, the enemy's stamina is further reduced by
\lfloor \frac{h}{2}\rfloor
, where
\lfloor A \rfloor
denotes the value
A
rounded down to an integer.
An item disappears once you use it, but you may buy any number of the items. Also, you may use items in any order.
Find the minimum amount of money required to make the enemy's stamina
0
or less.
|
[
{
"input": "13 6 5 1 7\n",
"output": "12\n"
},
{
"input": "1000 1 1 1 10000\n",
"output": "1000\n"
}
] |
https://atcoder.jp/contests/past202303-open/tasks/past202303_e
|
Problem Statement
You are given figures
S
and
T
with
H
rows and
W
columns consisting of
#
and
.
.
The figure
S
is given as
H
strings
S _ 1,S _ 2,\ldots,S _ H
; the
j
-th character of
S _ i
represents the element at the
i
-th row and
j
-th column of
S
. The figure
T
is given similarly.
Determine whether one can rearrange each row of
S
to make
S
equal
T
.
Here, rearranging each row of a figure
X
is the following operation.
For each
i=1,2,\ldots,H
, perform the following procedure independently.
Choose a permutation
P=(P _ 1,P _ 2,\ldots,P _ W)
of
(1,2,\ldots,W)
.
For all integers
j
such that
1 \leq j \leq W
, simultaneously replace the element at the
i
-th row and
j
-th column of
X
with the one at the
i
-th row and
P_j
-th column.
Note that you may choose different permutations
P
for different
i
.
|
[
{
"input": "3 4\n##.#\n##..\n.#..\n###.\n#..#\n...#\n",
"output": "Yes\n"
},
{
"input": "3 4\n#...\n#..#\n.###\n..##\n#..#\n##..\n",
"output": "No\n"
},
{
"input": "2 1\n#\n.\n#\n.\n",
"output": "Yes\n"
},
{
"input": "8 7\n..#.#.#\n..#....\n#.#....\n..#.#.#\n..#..#.\n..#...#\n..#....\n#.#....\n##.#...\n...#...\n#..#...\n..#..##\n...#.#.\n....#.#\n......#\n#....#.\n",
"output": "Yes\n"
}
] |
https://atcoder.jp/contests/past202303-open/tasks/past202303_f
|
Problem Statement
Given a set of
N
integers,
S=\{S_1,S_2,\dots,S_N\}
, answer
Q
queries in the following format:
given a set of
M
integers,
T=\{T_1,T_2,\dots,T_M\}
, find how many integers the union of
S
and
T
contains.
|
[
{
"input": "5\n2 3 5 7 11\n5\n6\n1 3 5 7 9 11\n6\n12 10 8 6 4 2\n3\n5 7 3\n3\n8 4 6\n12\n3 2 1 6 5 4 9 8 7 12 11 10\n",
"output": "7\n10\n5\n8\n12\n"
}
] |
https://atcoder.jp/contests/past202303-open/tasks/past202303_g
|
Problem Statement
There is a grid with
H
horizontal rows and
W
vertical columns. The square at the
i
-th row from the top and
j
-th column from the left has a number
A_{i,j}
written on it.
A_{i,j}
are distinct integers between
1
and
HW
.
Find all the integer pairs
(x,y)
satisfying
1\leq x < y \leq HW
such that the square with
x
written on it is adjacent to the one with
y
.
A square is said to be adjacent to another if these two squares share a side.
|
[
{
"input": "2 3\n2 3 4\n1 6 5\n",
"output": "1 2\n1 6\n2 3\n3 4\n3 6\n4 5\n5 6\n"
},
{
"input": "3 1\n1\n2\n3\n",
"output": "1 2\n2 3\n"
}
] |
https://atcoder.jp/contests/past202303-open/tasks/past202303_h
|
Problem Statement
You have a hand of
N
cards. The
i
-th card
(1 \leq i \leq N)
has an integer
A_i
written on it. You can perform the following operation any number of times.
Choose three cards with consecutive integers written on them, and discard them from hand. In other words, for some integer
x
, choose a card with
x
, a card with
x+1
, and a card with
x+2
written on them, and discard them from hand.
What is the minimum possible number of cards in your final hand?
|
[
{
"input": "7\n2 4 2 1 6 3 5\n",
"output": "1\n"
},
{
"input": "10\n1 2 2 4 6 7 9 9 10 12\n",
"output": "10\n"
},
{
"input": "3\n999999998 999999999 1000000000\n",
"output": "0\n"
}
] |
https://atcoder.jp/contests/past202303-open/tasks/past202303_i
|
Problem Statement
N
players numbered
1, \dots, N
sitting around a table play a card game. There are
26
kinds of cards, each of which has
A
,
B
, …, or
Z
written on it. Five cards are placed on the table, and four are dealt to each player.
The cards on the table are represented by a string
X
of length five, and the cards dealt to player
i \, (1 \leq i \leq N)
are represented by a string
S_i
of length four.
X, S_1, \dots
, and
S_N
consist of uppercase English letters, representing the kinds of the cards.
Each player chooses two cards from those dealt to the player and three from those on the table to make a set of five cards. This set is called the player's
hand
.
For each
i \, (1 \leq i \leq N)
, define
n_i
and
c_i
as follows:
let
n_i = n
and
c_i = c
, where
n
is the maximum integer such that player
i
's hand contains
n
cards of the same kind, and
c
is the character written on them. If there are multiple candidates, choose the alphabetically smallest
c
.
The superiority between two players
A
and
B
(A \neq B)
is determined as follows:
A
is superior if
n_A > n_B
, and
B
is superior if
n_B > n_A
.
If
n_A = n_B
, it is determined as follows:
If
c_A \neq c_B
,
A
is superior if
c_A
is alphabetically smaller than
c_B
, and
B
is superior if it is larger.
If
c_A = c_B
,
A
is superior if
A < B
, and
B
is superior if
B > A
.
Find the supreme player if all players play optimally. In other words, find
i
such that:
for all
j \neq i
, player
i
is superior to player
j
.
|
[
{
"input": "3\nAABCD\nDDBC\nABBQ\nXYZA\n",
"output": "2\n"
},
{
"input": "4\nABBDE\nADDZ\nBEEZ\nDDDD\nBDDE\n",
"output": "2\n"
}
] |
https://atcoder.jp/contests/past202303-open/tasks/past202303_j
|
Problem Statement
You are given figures
S
and
T
with
H
rows and
W
columns consisting of
#
and
.
.
The figure
S
is given as
H
strings
S _ 1,S _ 2,\ldots,S _ H
; the
j
-th character of
S _ i
represents the element at the
i
-th row and
j
-th column of
S
. The figure
T
is given similarly.
Determine whether it is possible to shift the columns of
S
so that
S
will equal
T
.
Here, shifting the columns of
S
is the following operation.
Choose an integer
s
between
1
and
W
, inclusive.
Then, for every integer
i
such that
1 \leq i \leq H
, simultaneously perform the following:
for every integer
j
such that
1 \leq j \leq W
, simultaneously replace the element at the
i
-th row and
j
-th column with that of the
i
-th row and
(j+s)
-th column of
X
.
Here, for
x
such that
W \gt x
, the
x
-th column means the
y
-th column where
y\ (1\leq y\leq W)
is the only integer such that
x-y
is a multiple of
W
.
|
[
{
"input": "3 4\n##.#\n##..\n.#..\n.###\n..##\n...#\n",
"output": "Yes\n"
},
{
"input": "3 4\n##..\n#..#\n..##\n..##\n#..#\n##..\n",
"output": "No\n"
},
{
"input": "2 1\n#\n.\n#\n.\n",
"output": "Yes\n"
},
{
"input": "8 7\n.#.....\n..#....\n....#..\n#.....#\n.#.....\n..#....\n....#..\n#.....#\n.....#.\n......#\n.#.....\n...##..\n.....#.\n......#\n.#.....\n...##..\n",
"output": "Yes\n"
}
] |
https://atcoder.jp/contests/past202303-open/tasks/past202303_k
|
Problem Statement
Takahashi and Aoki are playing a game with
N
rounds.
For
i=1,\ldots,N
, the
i
-th round consists of the following steps in the order from top to bottom.
If
i \geq 2
and Takahashi received a penalty in the
(i-1)
-th round, the
i
-th round ends without performing the steps below.
Takahashi is dealt
a_i
gold coins.
Takahashi does one of the following actions.
Put
\left \lfloor \frac{a_i \times t}{100} \right \rfloor
gold coins into a bag and show it to Aoki. (
\lfloor x \rfloor
is the greatest integer not exceeding
x
.) Here, it is guaranteed that
\left \lfloor \frac{a_i \times t}{100} \right \rfloor \geq 1
.
Show an empty bag to Aoki.
Aoki chooses an integer
x
between
1
and
100
, inclusive, with equal probability.
If
x\leq p
, he examines the bag. If it contains no gold coins, Takahashi puts
\left \lfloor \frac{a_i \times t}{100} \right \rfloor
gold coins into the bag and receives a penalty.
If
x \gt p
, he does nothing.
Takashi earns all gold coins dealt in this round that are not in the bag now.
Here, Aoki chooses
x
independently in each round.
Takahashi will make his choice in each round to maximize the expected value
E
of the total number of gold coins earned in all
N
rounds. Find this maximized value of
E
.
|
[
{
"input": "3 10 50\n100 10 300\n",
"output": "384.50000000000000000000\n"
},
{
"input": "3 1 10\n192 191 190\n",
"output": "570.89999999999997726263\n"
},
{
"input": "4 17 55\n32 10 77 1000000000\n",
"output": "906500100.00000000000000000000\n"
}
] |
https://atcoder.jp/contests/past202303-open/tasks/past202303_l
|
Problem Statement
There are
N
players numbered
1,2,\dots
, and
N
.
These
N
players are playing the same game. In this game, two players play a match against each other. The
N
players have distinct strengths; the player with the greater strength always wins a match.
There were
M
matches. The
i
-th match was between players
A_i
and
B_i
, and player
A_i
won.
Find the lexicographically smallest possible sequence of the
N
players sorted by their strengths in descending order.
|
[
{
"input": "3 1\n3 1\n",
"output": "2 3 1\n"
},
{
"input": "7 0\n",
"output": "1 2 3 4 5 6 7\n"
}
] |
https://atcoder.jp/contests/past202303-open/tasks/past202303_m
|
Problem Statement
We have
N
parcels numbered
1
to
N
. Parcel
i
has a size of
A_i
.
There are
M
boxes numbered
1
to
M
arranged in a row in numerical order: box
1
is the leftmost, and box
M
is the rightmost.
Box
i
has a size of
B_i
.
Each box can contain any number of parcels as long as the total size of the parcels is not greater than the size of the box.
For each parcel in the order from parcel
1
to parcel
N
, Takahashi will try to do the following:
put the parcel into the leftmost box that can contain it.
Determine whether Takahashi can put all parcels into boxes.
Additionally, if he can, find the total size of parcels in each box after putting all parcels into boxes; otherwise, find the smallest number of a parcel that he cannot put into a box.
|
[
{
"input": "3 3\n4 7 3\n10 4 8\n",
"output": "Yes\n7 0 7\n"
},
{
"input": "3 2\n4 7 3\n10 4\n",
"output": "No\n2\n"
},
{
"input": "5 5\n5 4 3 2 1\n1 2 3 4 5\n",
"output": "Yes\n1 2 3 4 5\n"
}
] |
https://atcoder.jp/contests/tupc2022/tasks/tupc2022_f
|
問題文
N \times N
のマス目があります。
i = 1,2,\dots,N
について、左から
i
列目には下から
M_i
個の
1 \times 1
の正方形のブロックがあり、それぞれのブロックには色が塗られています。ここで、
M_1,M_2,\dots,M_N
は単調非増加です。左から
i
列目の下から
j
個目のブロックには色
C_{i,j}
が塗られています。
マス目に対して、以下のような操作を考えます。
マス目を時計回りに瞬間的に
90^\circ
回転させた後、色のついたブロックを重力に従って同時に落下させる。より形式的には、左から
i
列目の下から
j
個目のブロックについて、そのブロックと同じ高さでそのブロックよりも右にあるブロックが
c_{i,j}
個あるとすると、このブロックを左から
j
列目の下から
c_{i,j}+1
番目のマスに移動させる。この移動は全てのブロックに対して同時に行う。
マス目が最初の配置に戻ってくるまでに、最小で何回の操作が必要でしょうか?
998244353
で割った余りを求めてください。
マス目の配置の一致性の定義
マス目の配置に対して、
N \times N
行列
A
を対応させる。
A_{i,j} \, (i,j=1,2,\dots,N)
は次のようにして定める。
マス目の左から
i
列目の下から
j
番目のマスにブロックが存在すればその色、存在しなければ
0
2
つのマス目の配置が一致するとは、それぞれの配置に対応する行列
A
が一致することである。
|
[
{
"input": "2\n2 1 2\n1 3\n",
"output": "3\n"
},
{
"input": "2\n2 1 1\n1 1\n",
"output": "1\n"
},
{
"input": "3\n3 1 2 3\n1 4\n0\n",
"output": "6\n"
},
{
"input": "5\n5 1 2 3 4 5\n4 6 7 7 9\n3 10 7 12\n2 13 14\n0\n",
"output": "22\n"
}
] |
https://atcoder.jp/contests/tupc2022/tasks/tupc2022_g
|
問題文
正整数
N
が与えられます。長さ
M
の正整数列
A
は、次の条件を満たすとき
良い数列
であるといいます。
任意の整数の組
(x,y)
(1\leq x\lt y\leq N)
に対して、
(A_i,A_{i+1})=(x,y)
または
(A_i,A_{i+1})=(y,x)
を満たす整数
i\ (1\leq i\leq M-1)
が存在する。
長さ
M
が最小であるような良い数列のうち、辞書順最小のものを求めてください。
T
個のテストケースが与えられるので、それぞれについて答えを求めてください。
|
[
{
"input": "2\n3\n2\n",
"output": "1 2 3 1\n1 2\n"
}
] |
https://atcoder.jp/contests/tupc2022/tasks/tupc2022_h
|
問題文
(1,2,\ldots,N)
の順列を以下では単に順列と呼びます。
順列
P
に対して、順列
\mathrm{NextPermutation} (P)
を次で定めます。
P=(N,N-1,\ldots,2,1)
のとき
(1,2,\ldots,N-1,N)
そうでないとき、長さ
N
の順列全てを辞書順昇順に並べたときに
P
の次に来るもの
2
つの順列
P=(P_1, P_2, \ldots,P_N), Q=(Q_1, Q_2, \ldots, Q_N)
が与えられます。
P
が
(1,2,\ldots, N-1,N)
に一致するまで、次の操作を繰り返します。
P
を
\mathrm{NextPermutation} (P)
で、
Q
を
\mathrm{NextPermutation} (Q)
で置き換える。
最終的な
Q
を求めてください。
|
[
{
"input": "3\n2 3 1\n3 2 1\n",
"output": "2 1 3\n"
},
{
"input": "4\n4 2 1 3\n2 1 3 4\n",
"output": "2 4 1 3\n"
},
{
"input": "4\n1 2 3 4\n1 2 3 4\n",
"output": "1 2 3 4\n"
}
] |
https://atcoder.jp/contests/tupc2022/tasks/tupc2022_i
|
問題文
正整数
L,R
が与えられます。
L
以上
R
以下の正整数からなる空でない集合
S
であって、
S
のすべての要素を割り切るような正整数のうち最大のものが
1
であるようなものはいくつありますか?
998244353
で割った余りを求めてください。
|
[
{
"input": "1 4\n",
"output": "11\n"
},
{
"input": "5 10\n",
"output": "51\n"
},
{
"input": "100 100\n",
"output": "0\n"
},
{
"input": "123456789 987654321\n",
"output": "884200911\n"
}
] |
https://atcoder.jp/contests/tupc2022/tasks/tupc2022_j
|
問題文
N
本の縦線からなるアミダクジがあります。
それぞれの縦線の長さは
2M+1
[\mathrm{cm}]
であり、横線は以下の条件を満たす必要があります。
横線の端点となれるのは上から
1,2,\dots,2M
[\mathrm{cm}]
の高さのみであり、横線の両端点の高さは等しい
全ての横線は、隣り合う
2
つの縦線のみを繋ぐ
同じ高さにある横線は高々
1
本
初め、このアミダクジに
M
本の横線がひかれています。
j\ (j=1,2,\dots,M)
本目の横線は、上から
2j
[\mathrm{cm}]
の位置にひかれており、左から
a_j
本目の縦線と
a_j+1
本目の縦線を結んでいます。
このアミダクジに以下の操作を任意の回数 (
0
回も可) 行う事が出来ます。
まだ横線のひかれていない高さ
h
及び、
1
以上
N-1
以下の整数
b
を選ぶ。
上から
h
[\mathrm{cm}]
の位置に左から
b
本目の縦線と
b+1
本目の縦線を結ぶ横線を追加する。
すべての
i=1,2,\dots,N
に対して左から
i
本目の縦線からクジを辿り始めると結果が左から
i
本目となるアミダクジを 「
恒等的なアミダクジ
」 と呼ぶとき、与えられたアミダクジを恒等的なアミダクジにするために必要な最小の操作回数を求めてください。
また、最小の操作回数を達成する操作の方法を
1
つ示してください。
この制約の元、恒等的なアミダクジにする操作方法が常に存在することが示せます。
|
[
{
"input": "3 2\n1 2\n",
"output": "2\n1 1\n3 2\n"
},
{
"input": "5 4\n2 3 4 1\n",
"output": "4\n1 2\n3 3\n5 4\n7 1\n"
},
{
"input": "100000 4\n1 2 2 1\n",
"output": "0\n"
}
] |
https://atcoder.jp/contests/tupc2022/tasks/tupc2022_k
|
問題文
長さ
N
の正整数列
(A_1,A_2,\dots,A_N)
が与えられます。
M=\max\,\{A_i\mid 1\leq i\leq N\}
とし、区間
(0,M]
を
K
個の区間に分割します。すなわち、非負実数
m_i \, (i=0,1,\dots,K)
を
0=m_0<m_1<m_2<\dots<m_{K-1}<m_K=M
を満たすように選んで
(0,M]=(m_0,m_1]\cup(m_1,m_2]\cup\dots\cup(m_{Kー1},m_{K}]
とします。
区切り方を適切に定めたとき、
\[
\displaystyle \sum_{i=1}^{K} m_i \times (A_j \in (m_{i-1}, m_i]\text{となる}j\text{の個数})
\]
の最小値を求めてください。答えは必ず整数になることが証明できます。
|
[
{
"input": "4 2\n3 7 4 5\n",
"output": "22\n"
},
{
"input": "19 4\n3279 97197 36049 32099 29257 18290 96531 13435 88697 97081 71483 11396 77398 55303 4166 3906 12281 28658 30496\n",
"output": "917886\n"
}
] |
https://atcoder.jp/contests/tupc2022/tasks/tupc2022_m
|
問題文
根付き木
S
に対して、根付き木
f(S)
を、
S
に対して次の操作を行ったものとします。
木
S
の葉をすべて
S
自身で置き換える
根付き木
S
に対して、木の無限列
(S_1, S_2, \dots)
を次のようにして定義します。
S_1 := S
S_i := f(S_{i-1}) \quad (i > 1)
無限木
S_{\infty}:= \displaystyle\lim_{n\to\infty} S_n
を
S
から誘導される
fractal tree
と呼びます。
K
個の根付き木
T_1, T_2, \dots, T_K
が与えられます。
T_i\,(i=1,2,\dots,K)
は、以下の条件をみたす根付き木です。
頂点数は
N_i \geq 2
頂点には
1,2,\dots,N_i
の番号が付けられている
根は頂点
1
である
j=2,3,\dots,N_i
について、頂点
j
の親は頂点
p_{i,j}
である
各
(i,j) \, (i,j=1,2,\dots,K)
について、
T_i,\,T_j
からそれぞれ誘導される fractal tree
(T_i)_\infty
と
(T_j)_\infty
が同型か判定してください。
Fractal tree の同型性の定義
Fractal tree
T
の頂点集合を
V(T)
、辺集合を
E(T)
とする。2 つの fractal tree
S,T
が同型であるとは、以下の 2 つの条件を満たす全単射
\varphi:V(S)\to V(T)
が存在することである。
\{u,v\}\in E(S) \iff \{\varphi(u),\varphi(v)\}\in E(T)
S,T
の根をそれぞれ
r_S, r_T
としたとき、
\varphi(r_S)=r_T
|
[
{
"input": "4\n2 1\n3 1 2\n3 1 1\n5 1 1 2 2\n",
"output": "1100\n1100\n0011\n0011\n"
}
] |
https://atcoder.jp/contests/tupc2022/tasks/tupc2022_n
|
問題文
2 \times 2
行列
A = \begin{pmatrix} a & b \\ c & d \end{pmatrix}
が与えられます。
A
の要素は非負整数です。
あおばさんとひろせさんがこの行列を用いてゲームをします。あおばさんが先手で、交互に以下の行動をします。
A
の行または列を一つ選んで、選んだ行または列の各要素から同じ正整数を引く。このとき、
A
の要素が負になってはいけない。
行動ができなくなった方の負けで、負けなかった方が勝ちです。両者がそれぞれ自身が勝つために最適な戦略をとる場合に、どちらが勝つかを判定してください。
T
個のテストケースが与えられるので、それぞれについて答えを求めてください。
|
[
{
"input": "4\n1 3 5 0\n3 0 0 7\n3 1 4 1\n5 9 2 5\n",
"output": "First\nSecond\nFirst\nSecond\n"
}
] |
https://atcoder.jp/contests/tupc2022/tasks/tupc2022_o
|
問題文
S_{n,m}
を
n
文字の
0
と
m
文字の
1
からなる長さ
n+m
の文字列全体の集合とします。
s_1, s_2\in S_{n,m}
に対して、
s_1, s_2
の距離
d(s_1, s_2)
を「隣り合った
2
文字を入れ替える操作によって文字列
s_1
を文字列
s_2
に並び替えるのに必要な最小の操作回数」と定義します。
また、
s_1, s_2\in S_{n,m}
に対して、
f(s_1,s_2)
を次で定めます。
d(s_1,t)=d(s_2,t)
となる文字列
t\in S_{n,m}
が存在するとき、そのような文字列の中で辞書順最小のものを返す。そのような文字列が存在しないときは
-1
を返す。
正整数
n,m
と文字列
T \in S_{n,m}
が与えられます。
f(s_1,s_2)=T
となるような文字列の組
(s_1,s_2)\ (s_1, s_2\in S_{n,m})
はいくつありますか?
998244353
で割った余りを求めてください。
|
[
{
"input": "2 2\n0101\n",
"output": "4\n"
},
{
"input": "4 1\n00100\n",
"output": "4\n"
},
{
"input": "3 3\n111000\n",
"output": "0\n"
},
{
"input": "6 4\n0001111000\n",
"output": "1254\n"
}
] |
https://atcoder.jp/contests/abc291/tasks/abc291_a
|
Problem Statement
You are given a string
S
consisting of uppercase and lowercase English letters.
Here, exactly one character of
S
is uppercase, and the others are all lowercase.
Find the integer
x
such that the
x
-th character of
S
is uppercase.
Here, the initial character of
S
is considered the
1
-st one.
|
[
{
"input": "aBc\n",
"output": "2\n"
},
{
"input": "xxxxxxXxxx\n",
"output": "7\n"
},
{
"input": "Zz\n",
"output": "1\n"
}
] |
https://atcoder.jp/contests/abc291/tasks/abc291_b
|
Problem Statement
Takahashi is participating in a gymnastic competition.
In the competition, each of
5N
judges grades Takahashi's performance, and his score is determined as follows:
Invalidate the grades given by the
N
judges who gave the highest grades.
Invalidate the grades given by the
N
judges who gave the lowest grades.
Takahashi's score is defined as the average of the remaining
3N
judges' grades.
More formally, Takahashi's score is obtained by performing the following procedure on the multiset of the judges' grades
S
(
|S|=5N
):
Repeat the following operation
N
times: choose the maximum element (if there are multiple such elements, choose one of them) and remove it from
S
.
Repeat the following operation
N
times: choose the minimum element (if there are multiple such elements, choose one of them) and remove it from
S
.
Takahashi's score is defined as the average of the
3N
elements remaining in
S
.
The
i
-th
(1\leq i\leq 5N)
judge's grade for Takahashi's performance was
X_i
points.
Find Takahashi's score.
|
[
{
"input": "1\n10 100 20 50 30\n",
"output": "33.333333333333336\n"
},
{
"input": "2\n3 3 3 4 5 6 7 8 99 100\n",
"output": "5.500000000000000\n"
}
] |
https://atcoder.jp/contests/abc291/tasks/abc291_c
|
Problem Statement
Takahashi is on a two-dimensional plane. Starting from the origin, he made
N
moves.
The
N
moves are represented by a string of length
N
as described below:
Takahashi's coordinates after the
i
-th move are:
(x+1,y)
if the
i
-th character of
S
is
R
;
(x-1,y)
if the
i
-th character of
S
is
L
;
(x,y+1)
if the
i
-th character of
S
is
U
; and
(x,y-1)
if the
i
-th character of
S
is
D
,
where
(x,y)
is his coordinates before the move.
Determine if Takahashi visited the same coordinates multiple times in the course of the
N
moves (including the starting and ending points).
|
[
{
"input": "5\nRLURU\n",
"output": "Yes\n"
},
{
"input": "20\nURDDLLUUURRRDDDDLLLL\n",
"output": "No\n"
}
] |
https://atcoder.jp/contests/abc291/tasks/abc291_d
|
Problem Statement
N
cards, numbered
1
through
N
, are arranged in a line. For each
i\ (1\leq i < N)
, card
i
and card
(i+1)
are adjacent to each other.
Card
i
has
A_i
written on its front, and
B_i
written on its back. Initially, all cards are face up.
Consider flipping zero or more cards chosen from the
N
cards.
Among the
2^N
ways to choose the cards to flip, find the number, modulo
998244353
, of such ways that:
when the chosen cards are flipped, for every pair of adjacent cards, the integers written on their face-up sides are different.
|
[
{
"input": "3\n1 2\n4 2\n3 4\n",
"output": "4\n"
},
{
"input": "4\n1 5\n2 6\n3 7\n4 8\n",
"output": "16\n"
},
{
"input": "8\n877914575 602436426\n861648772 623690081\n476190629 262703497\n971407775 628894325\n822804784 450968417\n161735902 822804784\n161735902 822804784\n822804784 161735902\n",
"output": "48\n"
}
] |
https://atcoder.jp/contests/abc291/tasks/abc291_e
|
Problem Statement
There is a length-
N
sequence
A=(A_1,\ldots,A_N)
that is a permutation of
1,\ldots,N
.
While you do not know
A
, you know that
A_{X_i}<A_{Y_i}
for
M
pairs of integers
(X_i,Y_i)
.
Can
A
be uniquely determined? If it is possible, find
A
.
|
[
{
"input": "3 2\n3 1\n2 3\n",
"output": "Yes\n3 1 2\n"
},
{
"input": "3 2\n3 1\n3 2\n",
"output": "No\n"
},
{
"input": "4 6\n1 2\n1 2\n2 3\n2 3\n3 4\n3 4\n",
"output": "Yes\n1 2 3 4\n"
}
] |
https://atcoder.jp/contests/abc291/tasks/abc291_f
|
Problem Statement
There are
N
cities numbered city
1
, city
2
,
\ldots
, and city
N
.
There are also one-way teleporters that send you to different cities.
Whether a teleporter can send you directly from city
i
(1\leq i\leq N)
to another is represented by a length-
M
string
S_i
consisting of
0
and
1
. Specifically, for
1\leq j\leq N
,
if
1\leq j-i\leq M
and the
(j-i)
-th character of
S_i
is
1
, then a teleporter can send you directly from city
i
to city
j
;
otherwise, it cannot send you directly from city
i
to city
j
.
Solve the following problem for
k=2,3,\ldots, N-1
:
Can you travel from city
1
to city
N
without visiting city
k
by repeatedly using a teleporter?
If you can, print the minimum number of times you need to use a teleporter;
otherwise, print
-1
.
|
[
{
"input": "5 2\n11\n01\n11\n10\n00\n",
"output": "2 3 2\n"
},
{
"input": "6 3\n101\n001\n101\n000\n100\n000\n",
"output": "-1 3 3 -1\n"
}
] |
https://atcoder.jp/contests/abc291/tasks/abc291_g
|
Problem Statement
There are length-
N
sequences
A=(A_0,A_1,\ldots,A_{N-1})
and
B=(B_0,B_1,\ldots,B_{N-1})
.
Takahashi may perform the following operation on
A
any number of times (possibly zero):
apply a left cyclic shift to the sequence
A
. In other words, replace
A
with
A'
defined by
A'_i=A_{(i+1)\% N}
, where
x\% N
denotes the remainder when
x
is divided by
N
.
Takahashi's objective is to maximize
\displaystyle\sum_{i=0}^{N-1} (A_i|B_i)
, where
x|y
denotes the bitwise logical sum (bitwise OR) of
x
and
y
.
Find the maximum possible
\displaystyle\sum_{i=0}^{N-1} (A_i|B_i)
.
What is the bitwise logical sum (bitwise OR)?
The
logical sum
(or the OR operation) is an operation on two one-bit integers (
0
or
1
) defined by the table below.
The
bitwise logical sum (bitwise OR)
is an operation of applying the logical sum bitwise.
x
y
x|y
0
0
0
0
1
1
1
0
1
1
1
1
The logical sum yields
1
if at least one of the bits
x
and
y
is
1
.
Conversely, it yields
0
only if both of them are
0
.
Example
0110 | 0101 = 0111
|
[
{
"input": "3\n0 1 3\n0 2 3\n",
"output": "8\n"
},
{
"input": "5\n1 6 1 4 3\n0 6 4 0 1\n",
"output": "23\n"
}
] |
https://atcoder.jp/contests/abc291/tasks/abc291_h
|
Problem Statement
You are given a tree
T
with
N
vertices. The
i
-th edge connects vertices
A_i
and
B_i
.
Construct an
N
-vertex rooted tree
R
that satisfies the following conditions.
For all integer pairs
(x,y)
such that
1 \leq x < y \leq N
,
if the lowest common ancestor in
R
of vertices
x
and
y
is vertex
z
, then vertex
z
in
T
is on the simple path between vertices
x
and
y
.
In
R
, for all vertices
v
except for the root, the number of vertices of the subtree rooted at
v
, multiplied by
2
, does not exceed the number of vertices of the subtree rooted at the parent of
v
.
We can prove that such a rooted tree always exists.
|
[
{
"input": "4\n1 2\n2 3\n3 4\n",
"output": "2 -1 4 2\n"
},
{
"input": "5\n1 2\n1 3\n1 4\n1 5\n",
"output": "-1 1 1 1 1\n"
}
] |
https://atcoder.jp/contests/arc157/tasks/arc157_a
|
Problem Statement
Determine whether there is a string
S
of length
N
consisting of
X
and
Y
that satisfies the following condition.
Condition:
Among the
(N - 1)
pairs of consecutive characters in
S
,
exactly
A
are
XX
,
exactly
B
are
XY
,
exactly
C
are
YX
, and
exactly
D
are
YY
.
|
[
{
"input": "5 1 1 1 1\n",
"output": "Yes\n"
},
{
"input": "5 1 2 1 0\n",
"output": "Yes\n"
},
{
"input": "5 0 4 0 0\n",
"output": "No\n"
}
] |
https://atcoder.jp/contests/arc157/tasks/arc157_b
|
Problem Statement
You are given a string
S
of length
N
consisting of
X
and
Y
.
You will choose
K
characters at distinct positions in
S
and change each of them:
X
becomes
Y
and
Y
becomes
X
.
Find the maximum possible number of pairs of consecutive
Y
s in the resulting string.
|
[
{
"input": "5 1\nXYXYX\n",
"output": "2\n"
},
{
"input": "5 4\nXYXYX\n",
"output": "2\n"
}
] |
https://atcoder.jp/contests/arc157/tasks/arc157_c
|
Problem Statement
There is a grid with
H
rows and
W
columns where each square has one of the characters
X
and
Y
written on it.
Let
(i, j)
denote the square at the
i
-th row from the top and
j
-th column from the left.
The characters on the grid are given as
H
strings
S_1, S_2, \dots, S_H
: the
j
-th character of
S_i
is the character on square
(i, j)
.
For a path
P
from square
(1, 1)
to square
(H, W)
obtained by repeatedly moving down or right to the adjacent square, the
score
is defined as follows.
Let
\mathrm{str}(P)
be the string of length
(H + W - 1)
obtained by concatenating the characters on the squares visited by
P
in the order they are visited.
The score of
P
is the
square of the number
of pairs of consecutive
Y
s in
\mathrm{str}(P)
.
There are
\displaystyle\binom{H + W - 2}{H - 1}
such paths. Find the sum of the scores over all those paths, modulo
998244353
.
What is
\binom{N}{K}
?
\displaystyle\binom{N}{K}
is the binomial coefficient representing the number of ways to choose
K
from
N
distinct elements.
|
[
{
"input": "2 2\nYY\nXY\n",
"output": "4\n"
},
{
"input": "2 2\nXY\nYY\n",
"output": "2\n"
},
{
"input": "10 20\nYYYYYYYYYYYYYYYYYYYY\nYYYYYYYYYYYYYYYYYYYY\nYYYYYYYYYYYYYYYYYYYY\nYYYYYYYYYYYYYYYYYYYY\nYYYYYYYYYYYYYYYYYYYY\nYYYYYYYYYYYYYYYYYYYY\nYYYYYYYYYYYYYYYYYYYY\nYYYYYYYYYYYYYYYYYYYY\nYYYYYYYYYYYYYYYYYYYY\nYYYYYYYYYYYYYYYYYYYY\n",
"output": "423787835\n"
}
] |
https://atcoder.jp/contests/arc157/tasks/arc157_d
|
Problem Statement
There is a grid with
H
rows and
W
columns where each square has one of the characters
X
and
Y
written on it.
Let
(i, j)
denote the square at the
i
-th row from the top and
j
-th column from the left.
The characters on the grid are given as
H
strings
S_1, S_2, \dots, S_H
: the
j
-th character of
S_i
is the character on square
(i, j)
.
You can set up fences across the grid between adjacent rows and columns.
Fences may intersect.
After setting up fences, a
block
is defined as the set of squares reachable from some square by repeatedly moving up, down, left, or right to the adjacent square without crossing fences. (See also Sample Output 1.)
There are
2^{H-1} \times 2^{W-1}
ways to set up fences. How many of them satisfy the following condition, modulo
998244353
?
Condition:
Each block has exactly two squares with
Y
written on it.
|
[
{
"input": "2 3\nXYY\nYXY\n",
"output": "2\n"
},
{
"input": "2 3\nXYX\nYYY\n",
"output": "0\n"
},
{
"input": "2 58\nYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXY\nYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXY\n",
"output": "164036797\n"
}
] |
https://atcoder.jp/contests/arc157/tasks/arc157_e
|
Problem Statement
You are given a rooted tree with
N
vertices.
The vertices are numbered
1
to
N
, and the root is vertex
1
.
The parent of each vertex
i
except the root is vertex
P_i
. Each vertex, including the root, has
no child or exactly two children
.
Determine whether it is possible to write one of the characters
X
and
Y
on each vertex of the given tree to satisfy the following condition.
Condition:
For each edge of the tree, consider the string of length
2
obtained by concatenating the characters written on the endpoints in the order from the parent
P_i
to the child
i
. Among the
(N - 1)
strings obtained in this way,
exactly
A
are
XX
,
exactly
B
are
XY
,
exactly
C
are
YX
, and
none is
YY
.
You have
T
test cases to solve.
|
[
{
"input": "3\n7 2 2 2\n1 1 2 2 3 3\n7 0 2 4\n1 1 2 2 3 3\n7 2 0 4\n1 1 2 2 4 4\n",
"output": "Yes\nYes\nNo\n"
}
] |
https://atcoder.jp/contests/arc157/tasks/arc157_f
|
Problem Statement
You are given strings
S
and
T
of length
N
consisting of
X
and
Y
.
For each
i = 1, 2, \dots, N
, you can swap the
i
-th character of
S
and the
i
-th character of
T
or choose not to do it.
There are
2^N
pairs of strings that can result from this process, including duplicates. Find the longest string that is a common subsequence (not necessarily contiguous) of one of such pairs.
If there are multiple such strings, find the lexicographically smallest of them.
What is a common subsequence?
A
subsequence
of string
S
is a string obtained by deleting zero or more characters from
S
and concatenating the remaining characters without changing the order.
A
common subsequence
of strings
S
and
T
is a string that is a subsequence of both
S
and
T
.
(See also Sample Output 1.)
|
[
{
"input": "3\nXXX\nYYY\n",
"output": "XY\n"
},
{
"input": "1\nX\nY\n",
"output": "\n"
},
{
"input": "4\nXXYX\nYYYY\n",
"output": "XYY\n"
}
] |
https://atcoder.jp/contests/abc290/tasks/abc290_a
|
Problem Statement
There was a contest with
N
problems. The
i
-th
(1\leq i\leq N)
problem was worth
A_i
points.
Snuke took part in this contest and solved
M
problems: the
B_1
-th,
B_2
-th,
\ldots
, and
B_M
-th ones.
Find his total score.
Here, the total score is defined as the sum of the points for the problems he solved.
|
[
{
"input": "3 2\n10 20 30\n1 3\n",
"output": "40\n"
},
{
"input": "4 1\n1 1 1 100\n4\n",
"output": "100\n"
},
{
"input": "8 4\n22 75 26 45 72 81 47 29\n4 6 7 8\n",
"output": "202\n"
}
] |
https://atcoder.jp/contests/abc290/tasks/abc290_b
|
Problem Statement
There were
N
contestants in the qualification round of a programming contest. All contestants got distinct ranks.
You are given a length-
N
string
S
, which represents whether the contestants want to participate in the final round or not. Specifically,
if the
i
-th character of
S
is
o
, the contestant ranked
i
-th in the qualification wants to participate in the final;
if the
i
-th character of
S
is
x
, the contestant ranked
i
-th in the qualification does not want to participate in the final.
Among those who want to participate in the final,
K
contestants with the smallest ranks advance to the final.
Print a string
T
of length
N
that satisfies the following conditions:
if the contestant ranked
i
-th in the qualification advances to the final, the
i
-th character of
T
is
o
;
if the contestant ranked
i
-th in the qualification does not advance to the final, the
i
-th character of
T
is
x
.
|
[
{
"input": "10 3\noxxoxooxox\n",
"output": "oxxoxoxxxx\n"
}
] |
https://atcoder.jp/contests/abc290/tasks/abc290_c
|
Problem Statement
You are given a length-
N
sequence of non-negative integers.
When
B
is a sequence obtained by choosing
K
elements from
A
and concatenating them without changing the order, find the maximum possible value of
MEX(B)
.
Here, for a sequence
X
, we define
MEX(X)
as the unique non-negative integer
m
that satisfies the following conditions:
Every integer
i
such that
0 \le i < m
is contained in
X
.
m
is not contained in
X
.
|
[
{
"input": "7 3\n2 0 2 3 2 1 9\n",
"output": "3\n"
}
] |
https://atcoder.jp/contests/abc290/tasks/abc290_d
|
Problem Statement
There are
N
squares indexed
0
through
(N-1)
arranged in a line.
Snuke is going to mark every square by the following procedure.
Mark square
0
.
Repeat the following steps i - iii
(N-1)
times.
Initialize a variable
x
with
(A+D) \bmod N
, where
A
is the index of the square marked last time.
While square
x
is marked, repeat replacing
x
with
(x+1) \bmod N
.
Mark square
x
.
Find the index of the square that Snuke marks for the
K
-th time.
Given
T
test cases, find the answer for each of them.
|
[
{
"input": "9\n4 2 1\n4 2 2\n4 2 3\n4 2 4\n5 8 1\n5 8 2\n5 8 3\n5 8 4\n5 8 5\n",
"output": "0\n2\n1\n3\n0\n3\n1\n4\n2\n"
}
] |
https://atcoder.jp/contests/abc290/tasks/abc290_e
|
Problem Statement
For a sequence
X
, let
f(X) =
(the minimum number of elements one must modify to make
X
a palindrome).
Given a sequence
A
of length
N
, find the sum of
f(X)
over all
contiguous
subarrays of
A
.
Here, a sequence
X
of length
m
is said to be a palindrome if and only if the
i
-th and the
(m+1-i)
-th elements of
X
are equal for all
1 \le i \le m
.
|
[
{
"input": "5\n5 2 1 2 2\n",
"output": "9\n"
}
] |
https://atcoder.jp/contests/abc290/tasks/abc290_f
|
Problem Statement
For a sequence
X=(X_1,X_2\ldots,X_N)
of length
N
consisting of positive integers, we define
f(X)
as follows:
A tree with
N
vertices is said to be good if and only if the degree of the
i
-th
(1 \leq i \leq N)
vertex is
X_i
.
If a good tree exists,
f(X)
is the maximum diameter of a good tree; if it doesn't,
f(X)=0
.
Here, the distance between two vertices is the minimum number of edges that must be traversed to travel from one vertex to the other,
and the diameter of a tree is the maximum distance between two vertices.
Find the sum, modulo
998244353
, of
f(X)
over all possible sequences
X
of length
N
consisting of positive integers.
We can prove that the sum of
f(X)
is a finite value.
Given
T
test cases, find the answer for each of them.
|
[
{
"input": "10\n2\n3\n5\n8\n13\n21\n34\n55\n89\n144\n",
"output": "1\n6\n110\n8052\n9758476\n421903645\n377386885\n881422708\n120024839\n351256142\n"
}
] |
https://atcoder.jp/contests/abc290/tasks/abc290_g
|
Problem Statement
Solve the following problem for
T
test cases.
We have a perfect
K
-ary tree of depth
D
(with
1+K+K^2+\dots+K^D
vertices).
Your objective is to cut some of the edges to obtain a connected component with exactly
X
vertices.
At least how many edges must be cut to achieve the objective?
|
[
{
"input": "11\n2 2 1\n2 2 2\n2 2 3\n2 2 4\n2 2 5\n2 2 6\n2 2 7\n1 999999999999999999 1\n1 999999999999999999 2\n1 999999999999999999 999999999999999999\n1 999999999999999999 1000000000000000000\n",
"output": "1\n2\n1\n1\n2\n1\n0\n1\n999999999999999998\n1\n0\n"
}
] |
https://atcoder.jp/contests/abc290/tasks/abc290_h
|
Problem Statement
There are
N
dogs numbered
1
through
N
and
M
cats numbered
1
through
M
.
You will arrange the
(N+M)
animals in a line from left to right.
An arrangement causes each animal's
frustration
as follows:
The frustration of dog
i
is
A_i\times|x-y|
, where
x
and
y
are the numbers of cats to the left and right of that dog, respectively.
The frustration of cat
i
is
B_i\times|x-y|
, where
x
and
y
are the numbers of dogs to the left and right of that cat, respectively.
Find the minimum possible sum of the frustrations.
|
[
{
"input": "2 2\n1 3\n2 4\n",
"output": "6\n"
},
{
"input": "1 2\n100\n100 290\n",
"output": "390\n"
},
{
"input": "5 7\n522 575 426 445 772\n81 447 629 497 202 775 325\n",
"output": "13354\n"
}
] |
https://atcoder.jp/contests/arc156/tasks/arc156_a
|
Problem Statement
We have
N
coins numbered
1
to
N
with two distinguishable sides. A string
S
represents the current state of the coins. If the
i
-th character of
S
is
1
, coin
i
is showing heads; if that character is
0
, coin
i
is showing tails.
You can repeat the following operation zero or more times.
Choose a pair of integers
(i,j)
such that
1\leq i < j\leq N
and
j-i\geq \bm{2}
. Flip coin
i
and coin
j
.
Determine whether it is possible to make all the
N
coins show tails. If it is possible, find the minimum number of operations needed.
You are given
T
test cases to solve.
|
[
{
"input": "5\n3\n101\n6\n101101\n5\n11111\n6\n000000\n30\n111011100110101100101000000111\n",
"output": "1\n2\n-1\n0\n8\n"
}
] |
https://atcoder.jp/contests/arc156/tasks/arc156_b
|
Problem Statement
For a finite multiset
S
of non-negative integers, let us define
\mathrm{mex}(S)
as the smallest non-negative integer not in
S
. For instance,
\mathrm{mex}(\lbrace 0,0, 1,3\rbrace ) = 2, \mathrm{mex}(\lbrace 1 \rbrace) = 0, \mathrm{mex}(\lbrace \rbrace) = 0
.
There are
N
non-negative integers on a blackboard. The
i
-th integer is
A_i
.
You will perform the following operation exactly
K
times.
Choose zero or more integers on the blackboard. Let
S
be the multiset of chosen integers, and write
\mathrm{mex}(S)
on the blackboard once.
How many multisets can be the multiset of integers on the final blackboard? Find this count modulo
998244353
.
|
[
{
"input": "3 1\n0 1 3\n",
"output": "3\n"
},
{
"input": "2 1\n0 0\n",
"output": "2\n"
},
{
"input": "5 10\n3 1 4 1 5\n",
"output": "7109\n"
}
] |
https://atcoder.jp/contests/arc156/tasks/arc156_c
|
Problem Statement
We have a tree
T
with vertices numbered
1
to
N
. The
i
-th edge of
T
connects vertex
u_i
and vertex
v_i
.
Let us use
T
to define the
similarity
of a permutation
P = (P_1,P_2,\ldots,P_N)
of
(1,2,\ldots,N)
as follows.
For a simple path
x=(x_1,x_2,\ldots,x_k)
in
T
, let
y=(P_{x_1}, P_{x_2},\ldots,P_{x_k})
. The similarity is the maximum possible length of a longest common subsequence of
x
and
y
.
Construct a permutation
P
with the minimum similarity.
What is a subsequence?
A
subsequence
of a sequence is a sequence obtained by removing zero or more elements from that sequence and concatenating the remaining elements without changing the relative order.
For instance,
(10,30)
is a subsequence of
(10,20,30)
, but
(20,10)
is not.
What is a simple path?
For vertices
X
and
Y
in a graph
G
, a
walk
from
X
to
Y
is a sequence of vertices
v_1,v_2, \ldots, v_k
such that
v_1=X
,
v_k=Y
, and there is an edge connecting
v_i
and
v_{i+1}
. A
simple path
(or simply a
path
) is a walk such that
v_1,v_2, \ldots, v_k
are all different.
|
[
{
"input": "3\n1 2\n2 3\n",
"output": "3 2 1\n"
},
{
"input": "4\n2 1\n2 3\n2 4\n",
"output": "3 4 1 2\n"
}
] |
https://atcoder.jp/contests/arc156/tasks/arc156_d
|
Problem Statement
You are given a sequence of
N
non-negative integers
A=(A_1,A_2,\dots,A_N)
and a positive integer
K
.
Find the bitwise
\mathrm{XOR}
of
\displaystyle \sum_{i=1}^{K} A_{X_i}
over all
N^K
sequences of
K
positive integer sequences
X=(X_1,X_2,\dots,X_K)
such that
1 \leq X_i \leq N\ (1\leq i \leq K)
.
What is bitwise
\mathrm{XOR}
?
The bitwise
\mathrm{XOR}
of non-negative integers
A
and
B
,
A \oplus B
, is defined as follows:
When
A \oplus B
is written in base two, the digit in the
2^k
's place (
k \geq 0
) is
1
if exactly one of the digits in that place of
A
and
B
is
1
, and
0
otherwise.
For example, we have
3 \oplus 5 = 6
(in base two:
011 \oplus 101 = 110
).
Generally, the bitwise
\mathrm{XOR}
of
k
non-negative integers
p_1, p_2, p_3, \dots, p_k
is defined as
(\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k)
. We can prove that this value does not depend on the order of
p_1, p_2, p_3, \dots, p_k
.
|
[
{
"input": "2 2\n10 30\n",
"output": "40\n"
},
{
"input": "4 10\n0 0 0 0\n",
"output": "0\n"
},
{
"input": "11 998244353\n314 159 265 358 979 323 846 264 338 327 950\n",
"output": "236500026047\n"
}
] |
https://atcoder.jp/contests/arc156/tasks/arc156_e
|
Problem Statement
Find the number, modulo
998244353
, of
good sequences
of length
N
whose elements are integers between
0
and
M
, inclusive, and whose sum is at most
K
.
Here, a length-
N
sequence
X=(X_1,X_2,\ldots,X_N)
is said to be good if and only if there is a graph
G
that satisfies all of the following conditions.
G
is a graph with
N
vertices numbered
1
to
N
without self-loops. (It may have multi-edges.)
For each
i\ (1\leq i \leq N)
, the degree of vertex
i
is
X_i
.
For each
i\ (1\leq i \leq N)
, no edge connects vertex
i
and vertex
i+1
. Here, vertex
N+1
means vertex
1
.
|
[
{
"input": "4 1 2\n",
"output": "3\n"
},
{
"input": "10 0 0\n",
"output": "1\n"
},
{
"input": "314 159 26535\n",
"output": "248950743\n"
}
] |
https://atcoder.jp/contests/arc156/tasks/arc156_f
|
Problem Statement
You are given three integer sequences of length
N
:
A=(A_1,A_2,\dots,A_N),B=(B_1,B_2,\dots,B_N),C=(C_1,C_2,\dots,C_N)
.
Find one set of integers that satisfies the following conditions.
It can be obtained as follows: start with an empty set, and for each
i=1,2,\dots,N
in this order, add
A_i
or
B_i
to the set.
It can be obtained as follows: start with an empty set, and for each
i=1,2,\dots,N
in this order, add
A_i
or
C_i
to the set.
It has the maximum number of elements among the sets that satisfy the two conditions above.
|
[
{
"input": "3\n1 1 1\n2 3 4\n5 4 2\n",
"output": "3\n4 1 2\n"
},
{
"input": "15\n1 1 15 11 13 7 7 1 6 1 5 7 4 9 8\n11 30 1 18 16 15 19 17 3 27 22 7 21 29 9\n24 14 23 17 18 16 9 12 10 5 26 29 20 19 11\n",
"output": "12\n7 9 11 17 19 1 15 4 5 6 29 13\n"
}
] |
https://atcoder.jp/contests/ahc018/tasks/ahc018_a
|
Problem Statement
There is plot of land, represented by a grid of
N
rows and
N
columns.
On the grid, there are
water sources
in
W
spots, and
houses
in
K
spots.
Let us call the cell on
i
th (
0 \leq i \lt N
) row from top and
j
th column (
0 \leq j \lt N
) from left,
(i, j)
.
Each cell
(i, j)
has an associated integer
S_{i,j}
between
10
and
5000
(inclusive), representing the
sturdiness
of the
bedrock
of the cell. The
sturdiness
is pre-determined, but it's not given as a part of the input.
When the
bedrock
of a cell containing a
water source
is crushed, water flows into the cell.
Furthermore, with cells with flowing water as starting points, water will flow horizontally and vertically into neighboring cells with crushed
bedrock
and span every cell it can reach this way.
X-san, who is in charge of the land, wants to crush
bedrock
to build watercourses to make the water flow into every cell with
houses
.
Please note that for water to flow into a cell with a
house
, the
bedrock
of that cell must be crushed too.
To crush
bedrock
, X-san repeatedly performs
excavations
as explained below.
Choose a cell
(i, j)
whose
bedrock
isn't crushed yet, and a positive integer
P
less or equal than
5000
, representing
power
.
As
C+P
(
C
is a positive integer received as an input) of X-san's
stamina
gets consumed, the
sturdiness
S_{i,j}
of the
bedrock
in the cell diminishes by
P
.
If
sturdiness
then reaches zero
S_{i,j} \leq 0
, the
bedrock
of the cell
(i, j)
gets crushed.
Make the water flow into every cell with a
house
, while trying to conserve X-san's
stamina
as much as possible.
|
[] |
https://atcoder.jp/contests/agc061/tasks/agc061_a
|
Problem Statement
There is an array
A_1, \ldots, A_N
, and initially
A_i = i
for all
i
. Define the following routine
\mathrm{shuffle}(L, R)
:
If
R = L + 1
, swap values of
A_L
and
A_R
and terminate.
Otherwise, run
\mathrm{shuffle}(L, R - 1)
followed by
\mathrm{shuffle}(L + 1, R)
.
Suppose we run
\mathrm{shuffle}(1, N)
. Print the value of
A_K
after the routine finishes.
For each input file, solve
T
test cases.
|
[
{
"input": "7\n2 1\n2 2\n5 1\n5 2\n5 3\n5 4\n5 5\n",
"output": "2\n1\n2\n4\n1\n5\n3\n"
}
] |
https://atcoder.jp/contests/agc061/tasks/agc061_b
|
Problem Statement
There is a graph with
N
vertices
v_1, \ldots, v_N
on the left, and
N + 1
vertices
u_1, \ldots, u_{N + 1}
on the right. Each vertex
v_i
(
1 \leq i \leq N
) is connected to each vertex
u_j
(
1 \leq j \leq N + 1
), that is, the graph contains
N(N + 1)
edges.
We color every edge with one of the
N
colors
1, \ldots, N
. A coloring is
suitable
if for each
k = 1, \ldots, N
there are exactly
2k
edges of color
k
, and those edges form a simple path.
Formally, for each
k = 1, \ldots, N
there should exist a sequence of distinct vertices
w_0, \ldots, w_{2k}
such that all of the following is true:
For each
i = 0, \ldots, 2k - 1
, vertices
w_i
and
w_{i + 1}
are connected with an edge of color
k
,
No other edges of color
k
exist.
Find any suitable coloring, or determine that it doesn't exist.
For each input file, solve
T
test cases.
|
[
{
"input": "2\n1\n2\n",
"output": "Yes\n1 1\nNo\n"
}
] |
https://atcoder.jp/contests/agc061/tasks/agc061_c
|
Problem Statement
There are
N
customers named
1,\ldots,N
visiting a shop. Customer
i
arrives at time
A_i
and leaves at time
B_i
. The queue order is
first in-first out
, so
A_i
are increasing, and
B_i
are increasing. Additionally, all
A_i
and
B_i
are pairwise distinct.
At the entrance there's a list of visitors to put their names in. Each customer will write down their name next in the list exactly once, either when they arrive or when they leave. How many different orders of names can there be in the end?
Find the count modulo
998\,244\,353
.
|
[
{
"input": "3\n1 3\n2 5\n4 6\n",
"output": "3\n"
},
{
"input": "4\n1 2\n3 4\n5 6\n7 8\n",
"output": "1\n"
}
] |
https://atcoder.jp/contests/agc061/tasks/agc061_d
|
Problem Statement
There are positive integers
N, M
, and an
N \times M
positive integer matrix
A_{i, j}
. For two
(strictly) increasing
positive integer sequences
X = (X_1, \ldots, X_N)
and
Y = (Y_1, \ldots, Y_M)
, we define the penalty
D(X, Y)
as
\max_{1 \leq i \leq N, 1 \leq j \leq M} |X_i Y_j - A_{i, j}|
.
Find two
(strictly) increasing
positive integer sequences
X
and
Y
such that
D(X, Y)
is the smallest possible.
|
[
{
"input": "1 1\n853922530\n",
"output": "0\n31415\n27182\n"
},
{
"input": "3 3\n4 4 4\n4 4 4\n4 4 4\n",
"output": "5\n1 2 3 \n1 2 3 \n"
},
{
"input": "3 4\n4674 7356 86312 100327\n8737 11831 145034 167690\n47432 66105 809393 936462\n",
"output": "357\n129 216 1208 \n39 55 670 775 \n"
}
] |
https://atcoder.jp/contests/agc061/tasks/agc061_e
|
Problem Statement
There is a non-negative integer
X
initially equal to
S
. One can perform the following operations, in any order, any number of times:
Add
1
to
X
. This has a cost of
A
.
Choose
i
between
1
and
N
, and replace
X
with
X \oplus Y_i
. This has a cost of
C_i
. Here,
\oplus
denotes bitwise
\mathrm{XOR}
.
Find the smallest total cost to make
X
equal to a given non-negative integer
T
, or report that this is impossible.
What is bitwise
\mathrm{XOR}
?
The bitwise
\mathrm{XOR}
of non-negative integers
A
and
B
,
A \oplus B
, is defined as follows:
When
A \oplus B
is written in base two, the digit in the
2^k
's place (
k \geq 0
) is
1
if exactly one of the digits in that place of
A
and
B
is
1
, and
0
otherwise.
For example, we have
3 \oplus 5 = 6
(in base two:
011 \oplus 101 = 110
).
Generally, the bitwise
\mathrm{XOR}
of
k
non-negative integers
p_1, p_2, p_3, \dots, p_k
is defined as
(\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k)
. We can prove that this value does not depend on the order of
p_1, p_2, p_3, \dots, p_k
.
|
[
{
"input": "1 15 0 1\n8 2\n",
"output": "5\n"
},
{
"input": "3 21 10 100\n30 1\n12 1\n13 1\n",
"output": "3\n"
},
{
"input": "1 2 0 1\n1 1\n",
"output": "-1\n"
},
{
"input": "8 352217 670575 84912\n239445 2866\n537211 16\n21812 6904\n50574 8842\n380870 5047\n475646 8924\n188204 2273\n429397 4854\n",
"output": "563645\n"
}
] |
https://atcoder.jp/contests/agc061/tasks/agc061_f
|
Problem Statement
There are positive integers
N
and
M
.
A binary string
s
is called
good
if all of the followings are satisfied:
s
is non-empty.
The number of
1
s in
s
is a multiple of
N
.
The number of
0
s in
s
is a multiple of
M
.
A good string is called
perfect
if it doesn't contain shorter good (contiguous) substrings. For example, if
N = 3
and
M = 2
, then strings
111
,
00
and
10101
are perfect, but
0000
and
11001
are not.
One can show that for any
N, M
the number of perfect strings is finite. Find this number modulo
998\,244\,353
.
|
[
{
"input": "2 2\n",
"output": "4\n"
},
{
"input": "3 2\n",
"output": "7\n"
},
{
"input": "23 35\n",
"output": "212685109\n"
}
] |
https://atcoder.jp/contests/joi2023ho/tasks/joi2023ho_a
|
問題文
JOI 君は
N
個の碁石を持っている.それぞれの碁石には
1
から
N
までの番号が付けられており,
1
以上
10^9
以下の整数で表される色で塗られている.最初,碁石
i
(
1 \leqq i \leqq N
) の色は
A_i
である.
JOI 君はこれから
N
回の操作を行い,碁石をテーブルの上に
1
列に並べたい.
i
回目 (
1 \leqq i \leqq N
) の操作は以下のような手順で行われる.
碁石
i
を碁石
i-1
の右隣に置く.ただし,
i=1
の場合は,碁石
1
をテーブルの上に置く.
碁石
1, 2, \dots, i-1
のうち現在の色が碁石
i
と同じであるものが存在する場合,それらのうち番号が最も大きいものを
j
とすると,碁石
j+1, j+2, \dots, i-1
の色をすべて色
A_i
に塗り替える.
操作を正しく行ったか確認するために,JOI 君はすべての操作を行った後の碁石の色を予め知っておきたい.
碁石の情報が与えられたとき,
N
回の操作を行った後のそれぞれの碁石の色を求めるプログラムを作成せよ.
|
[
{
"input": "6\n1\n2\n1\n2\n3\n2\n",
"output": "1\n1\n1\n2\n2\n2\n"
},
{
"input": "10\n1\n1\n2\n2\n1\n2\n2\n1\n1\n2\n",
"output": "1\n1\n1\n1\n1\n1\n1\n1\n1\n2\n"
}
] |
https://atcoder.jp/contests/joi2023ho/tasks/joi2023ho_b
|
問題文
JOI 国には
N
人の住人がおり,
1
から
N
までの番号が付けられている.
住人
i
(
1 \leqq i \leqq N
) は数直線上の座標
X_i
に住んでおり,その
影響力
は
E_i
である.同じ座標に複数の住人が住んでいることもある.
影響力が高い住人ほど宣伝効果が大きい一方で,本を買うのに慎重になる.
理恵さんは情報科学の本を出版した.本を多くの人に買ってもらうため,何人かの住人に献本をすることができる.
住民
i
(
1 \leqq i \leqq N
) に献本した場合,住人
i
は理恵さんの本を手に入れる.
さらに,まだ理恵さんの本を持っていない住人のうち,次の条件を満たすようなすべての住人
j
(
1 \leqq j \leqq N
) は理恵さんの本を買って手に入れる.
住人
i
と住人
j
との数直線上での距離が
E_i - E_j
以下である.すなわち
|X_i - X_j| \leqq E_i - E_j
である.
もし理恵さんの本が JOI 国のすべての住人に読まれれば,情報オリンピックの知名度は大きく向上するだろう.
JOI 国の住人全員が理恵さんの本を手に入れるために,最小で何人に献本する必要があるかを求めるプログラムを作成せよ.
|
[
{
"input": "4\n4 2\n2 3\n3 4\n6 5\n",
"output": "2\n"
},
{
"input": "3\n7 10\n10 10\n7 10\n",
"output": "2\n"
},
{
"input": "10\n31447678 204745778\n430226982 292647686\n327782937 367372305\n843320852 822224390\n687565054 738216211\n970840050 766211141\n563662348 742939240\n103739645 854320982\n294864525 601612333\n375952316 469655019\n",
"output": "5\n"
}
] |
https://atcoder.jp/contests/joi2023ho/tasks/joi2023ho_c
|
問題文
迷路を解くのが好きな K 理事長は,迷路になりそうなマス目を見つけた.マス目は縦
R
行,横
C
列の長方形の形をしており,各マスは白または黒で塗られている.上から
i
行目 (
1 \leqq i \leqq R
),左から
j
列目 (
1 \leqq j \leqq C
) のマスをマス
(i, j)
と呼ぶことにする.
K 理事長は,マス目の白いマスは通れるマス,黒いマスは通れないマスとして,迷路を解くことにした.具体的には,以下のようにして迷路を解く.
白いマスの中からスタートのマス
(S_r, S_c)
とゴールのマス
(G_r, G_c)
を選ぶ.
上下左右に隣接する白いマスに移動することを繰り返して,スタートのマスからゴールのマスへ移動する経路を見つける.
K 理事長はスタートのマスとゴールのマスを決めたが,マス目の色の塗られ方によっては,白いマスのみを通ってスタートからゴールへ移動する経路が存在しない場合があることに気がついた.そこで, K 理事長の持っている
N \times N
マスの大きさのハンコを用いて以下の
操作
を繰り返すことで,スタートからゴールへ移動する経路が存在するようにしたい.
操作
:マス目から
N \times N
マスの正方形の領域を選び,この領域に含まれるマスをすべて白にする.より厳密には,
1 \leqq a \leqq R - N + 1
,
1 \leqq b \leqq C - N + 1
を満たす整数
a, b
を選び,
a \leqq i \leqq a + N - 1
,
b \leqq j \leqq b + N - 1
を満たすすべての整数の組
(i, j)
に対して,マス
(i, j)
を白にする.
ハンコを使うと手が汚れる可能性があるため,操作回数はできるだけ少なくしたい.マス目の塗られ方とハンコの大きさ,スタートのマスとゴールのマスが与えられたとき,白いマスのみを通ってスタートからゴールへ移動する経路が存在するようにするための,操作回数の最小値を求めるプログラムを作成せよ.
|
[
{
"input": "2 4 2\n1 1\n2 4\n.###\n###.\n",
"output": "1\n"
},
{
"input": "6 6 1\n1 6\n6 1\n..#.#.\n##.###\n####.#\n...###\n##.##.\n.#.###\n",
"output": "4\n"
},
{
"input": "6 7 6\n6 4\n3 1\n..#.#.#\n##.##..\n.######\n#..#.#.\n.######\n..#.##.\n",
"output": "1\n"
},
{
"input": "1 15 1\n1 15\n1 1\n...............\n",
"output": "0\n"
}
] |
https://atcoder.jp/contests/joi2023ho/tasks/joi2023ho_d
|
問題文
N
個のキャットタワーがあり,それぞれに
1
から
N
までの番号が付けられている.
タワー
i
(
1 \leqq i \leqq N
) の高さは
P_i
である.
タワーの高さは
1
以上
N
以下の相異なる整数である.
N-1
組のタワーが隣接しており,各
j
(
1 \leqq j \leqq N-1
) について,
タワー
A_j
とタワー
B_j
が隣接している.
はじめ,どのタワーからどのタワーへも隣接するタワーへ移動する操作を繰り返して移動できる.
最初,猫が高さ
N
のタワーの上にいる.
次に,
キャットエクササイズ
を行う.
キャットエクササイズとは,
1
つのタワーを選んでそこに障害物を置く操作の繰り返しである.
ただし,既に障害物を置いたタワーに再び障害物を置くことはできない.
操作によって以下のことが起こる.
選んだタワーに猫がいない場合,何も起こらない.
選んだタワーに猫がおり,かつそれに隣接するタワーすべてに障害物が置かれている場合,キャットエクササイズが終了する.
いずれでもない場合,障害物が置かれていない隣接するタワーへの移動を繰り返して選んだタワーから移動できるタワーのうち,選んだタワーを除いて最も高いタワーへ,隣接するタワーへの移動を繰り返すことで猫が移動する.このとき,猫は隣接するタワーへの移動の回数が最小になるように移動する.
タワーの高さと隣接するタワーの組の情報が与えられたとき,
障害物の置き方を工夫したときの,猫が隣接するタワーへ移動する回数の合計の最大値を求めるプログラムを作成せよ.
|
[
{
"input": "4\n3 4 1 2\n1 2\n2 3\n3 4\n",
"output": "3\n"
},
{
"input": "7\n3 2 7 1 5 4 6\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n",
"output": "7\n"
}
] |
https://atcoder.jp/contests/joi2023ho/tasks/joi2023ho_e
|
問題文
ビ太郎は,誕生日プレゼントとして JOI マシンをもらった.
JOI マシンは,
1
個の
ボール
,
N
個の
ライトタイル
,
M
個の
ボタン
からなる.
ライトタイルにはそれぞれ
1
から
N
までの番号が付けられており,
電源を入れると,ライトタイル
i
(
1 \leqq i \leqq N
) は色
C_i
(青 (
B
),赤 (
R
) のいずれか) で光るようになっている.
また,ボタンにはそれぞれ
1
から
M
までの番号が付けられており,
ボタン
j
(
1 \leqq j \leqq M
) を押すと,次のようなことが起こる.
ライトタイル
A_j
の上にボールが置かれる.
ライトタイル
A_j
の色が (元の色にかかわらず) 赤になる.
ボールが取り除かれるまで,以下のことが繰り返し行われる.
今,ボールが置かれているライトタイルの番号を
p
とする.
ライトタイル
p
の色が青の場合
ライトタイル
p
の色が赤に変わる.その後,もし
p=1
の場合はボールが取り除かれ,そうでない場合はボールがライトタイル
p-1
の上に移動する.
ライトタイル
p
の色が赤の場合
ライトタイル
p
の色が青に変わる.その後,もし
p=N
の場合はボールが取り除かれ,そうでない場合はボールがライトタイル
p+1
の上に移動する.
JOI マシンに興味を持ったビ太郎は,
Q
回の実験を計画している.
k
回目 (
1 \leqq k \leqq Q
) の実験は,JOI マシンの電源を入れた後,ボタン
L_k, L_k+1, \ldots, R_k
をこの順に押すというものである.
ただし,ボールが取り除かれるまでは,次のボタンを押さずに待つものとする.
JOI マシンの情報および実験の情報が与えられるので,
それぞれの実験について,実験が終わった後における,色が赤であるようなライトタイルの個数を求めるプログラムを作成せよ.
|
[
{
"input": "5 1\nRBRRB\n4\n1\n1 1\n",
"output": "1\n"
},
{
"input": "5 3\nRBRBR\n1 3 4\n2\n2 3\n1 3\n",
"output": "5\n0\n"
},
{
"input": "10 3\nBBRRBRBRRB\n2 10 5\n1\n1 3\n",
"output": "2\n"
},
{
"input": "10 10\nRRRRRRRRRR\n3 1 4 1 5 9 2 6 5 3\n5\n1 7\n2 8\n3 9\n4 10\n1 10\n",
"output": "4\n8\n10\n0\n9\n"
},
{
"input": "10 10\nRRRBBBBBBB\n3 1 4 1 5 9 2 6 5 3\n5\n1 10\n2 9\n3 8\n4 7\n5 6\n",
"output": "2\n6\n0\n10\n7\n"
},
{
"input": "30 10\nRRRBBRBBBRBBBRBRBRRRRRBBBBRBRR\n3 28 2 29 1 30 6 14 7 7\n10\n1 10\n2 3\n2 5\n2 8\n3 3\n3 6\n4 5\n4 7\n5 9\n10 10\n",
"output": "21\n15\n15\n4\n17\n16\n14\n20\n12\n23\n"
}
] |
https://atcoder.jp/contests/abc289/tasks/abc289_a
|
Problem Statement
You are given a string
s
consisting of two kinds of characters,
0
and
1
.
Print the string obtained by replacing
0
with
1
and
1
with
0
in
s
.
|
[
{
"input": "01\n",
"output": "10\n"
},
{
"input": "1011\n",
"output": "0100\n"
},
{
"input": "100100001\n",
"output": "011011110\n"
}
] |
https://atcoder.jp/contests/abc289/tasks/abc289_b
|
Problem Statement
Studying
Kanbun
, Takahashi is having trouble figuring out the order to read words. Help him out!
There are
N
integers from
1
through
N
arranged in a line in ascending order.
Between them are
M
"レ" marks. The
i
-th "レ" mark is between the integer
a_i
and integer
(a_i + 1)
.
You read each of the
N
integers once by the following procedure.
Consider an undirected graph
G
with
N
vertices numbered
1
through
N
and
M
edges. The
i
-th edge connects vertex
a_i
and vertex
(a_i+1)
.
Repeat the following operation until there is no unread integer:
let
x
be the minimum unread integer. Choose the connected component
C
containing vertex
x
, and read all the numbers of the vertices contained in
C
in descending order.
For example, suppose that integers and "レ" marks are arranged in the following order:
(In this case,
N = 5, M = 3
, and
a = (1, 3, 4)
.)
Then, the order to read the integers is determined to be
2, 1, 5, 4
, and
3
, as follows:
At first, the minimum unread integer is
1
, and the connected component of
G
containing vertex
1
has vertices
\lbrace 1, 2 \rbrace
, so
2
and
1
are read in this order.
Then, the minimum unread integer is
3
, and the connected component of
G
containing vertex
3
has vertices
\lbrace 3, 4, 5 \rbrace
, so
5
,
4
, and
3
are read in this order.
Now that all integers are read, terminate the procedure.
Given
N, M
, and
(a_1, a_2, \dots, a_M)
, print the order to read the
N
integers.
What is a connected component?
A
subgraph
of a graph is a graph obtained by choosing some vertices and edges from the original graph.
A graph is said to be
connected
if and only if one can travel between any two vertices in the graph via edges.
A
connected component
is a connected subgraph that is not contained in any larger connected subgraph.
|
[
{
"input": "5 3\n1 3 4\n",
"output": "2 1 5 4 3\n"
},
{
"input": "5 0\n\n",
"output": "1 2 3 4 5\n"
},
{
"input": "10 6\n1 2 3 7 8 9\n",
"output": "4 3 2 1 5 6 10 9 8 7\n"
}
] |
https://atcoder.jp/contests/abc289/tasks/abc289_c
|
Problem Statement
There are
M
sets, called
S_1, S_2, \dots, S_M
, consisting of integers between
1
and
N
.
S_i
consists of
C_i
integers
a_{i, 1}, a_{i, 2}, \dots, a_{i, C_i}
.
There are
(2^M-1)
ways to choose one or more sets from the
M
sets.
How many of them satisfy the following condition?
For all integers
x
such that
1 \leq x \leq N
, there is at least one chosen set containing
x
.
|
[
{
"input": "3 3\n2\n1 2\n2\n1 3\n1\n2\n",
"output": "3\n"
},
{
"input": "4 2\n2\n1 2\n2\n1 3\n",
"output": "0\n"
},
{
"input": "6 6\n3\n2 3 6\n3\n2 4 6\n2\n3 6\n3\n1 5 6\n3\n1 3 6\n2\n1 4\n",
"output": "18\n"
}
] |
https://atcoder.jp/contests/abc289/tasks/abc289_d
|
Problem Statement
There is a staircase with infinite steps.
The foot of the stairs is the
0
-th step, the next step is the
1
-st step, the next is the
2
-nd, and so on.
There is a stair-climbing robot on the
0
-th step.
The robot can climb up
A _ 1,A _ 2,\ldots
, or
A _ N
steps at a time.
In other words, when the robot is on the
i
-th step, it can step onto one of the
(i+A _ 1)
-th step,
(i+A _ 2)
-th step,
\ldots
, and
(i+A _ N)
-th step,
but not onto the others in a single step.
The robot cannot descend the stairs, either.
There are traps on the
B _ 1
-th,
B _ 2
-th,
\ldots
, and
B _ M
-th steps.
Once the robot steps onto a step with a trap, it cannot move anymore.
The robot wants to step onto the
X
-th step.
Determine whether it is possible to do so.
|
[
{
"input": "3\n3 4 5\n4\n4 5 6 8\n15\n",
"output": "Yes\n"
},
{
"input": "4\n2 3 4 5\n4\n3 4 5 6\n8\n",
"output": "No\n"
},
{
"input": "4\n2 5 7 8\n5\n2 9 10 11 19\n20\n",
"output": "Yes\n"
}
] |
https://atcoder.jp/contests/abc289/tasks/abc289_e
|
Problem Statement
There is a simple undirected graph with
N
vertices numbered
1
through
N
and
M
edges numbered
1
through
M
. Edge
i
connects vertex
u_i
and vertex
v_i
.
Every vertex is painted either red or blue. The color of vertex
i
is represented by
C_i
; vertex
i
is painted red if
C_i
is
0
and blue if
C_i
is
1
.
Now, Takahashi is on vertex
1
and Aoki is on vertex
N
.
They may repeat the following move zero or more times.
Each of the two simultaneously moves to a vertex adjacent to the current vertex.
Here, the vertices that Takahashi and Aoki move to must have different colors.
By repeating the move above, can Takahashi and Aoki simultaneously end up on vertices
N
and
1
, respectively?
If it is possible, find the minimum number of moves required. If it is impossible, print
-1
.
You are given
T
at the beginning of the input. Solve the problem for
T
test cases.
|
[
{
"input": "3\n4 4\n0 1 0 1\n1 2\n2 3\n1 3\n2 4\n3 3\n0 1 0\n1 2\n2 3\n1 3\n6 6\n0 0 1 1 0 1\n1 2\n2 6\n3 6\n4 6\n4 5\n2 4\n",
"output": "3\n-1\n3\n"
}
] |
https://atcoder.jp/contests/abc289/tasks/abc289_f
|
Problem Statement
Takahashi is on an
xy
-plane.
Initially, he is at point
(s _ x,s _ y)
,
and he wants to reach point
(t _ x,t _ y)
.
On the
xy
-plane is a rectangle
R\coloneqq\lbrace(x,y)\mid a-0.5\leq x\leq b+0.5,c-0.5\leq y\leq d+0.5\rbrace
.
Consider the following operation:
Choose a lattice point
(x,y)
contained in the rectangle
R
. Takahashi teleports to the point symmetric to his current position with respect to point
(x,y)
.
Determine if he can reach point
(t _ x,t _ y)
after repeating the operation above between
0
and
10^6
times, inclusive.
If it is possible, construct a sequence of operations that leads him to point
(t _ x,t _ y)
.
|
[
{
"input": "1 2\n7 8\n7 9 0 3\n",
"output": "Yes\n7 0\n9 3\n7 1\n8 1\n"
},
{
"input": "0 0\n8 4\n5 5 0 0\n",
"output": "No\n"
},
{
"input": "1 4\n1 4\n100 200 300 400\n",
"output": "Yes\n"
},
{
"input": "22 2\n16 7\n14 30 11 14\n",
"output": "No\n"
}
] |
https://atcoder.jp/contests/abc289/tasks/abc289_g
|
Problem Statement
Takahashi runs AtCoder store.
N
customers visit the store, and
M
items are sold in the store.
The motivation of the
i
-th
(1\leq i\leq N)
customer is
B _ i
.
The value of the
j
-th
(1\leq j\leq M)
item is
C _ j
.
Takahashi sets a price for each item.
The
i
-th customer buys one
j
-th item if and only if its price
P _ j
satisfies:
B _ i+C _ j\geq P _ j
.
For each
j=1,2,\ldots,M
, find the gross sale of the
j
-th item when Takahashi sets the price so that the sale is maximized.
The gross sale of the
j
-th item is defined as the product of
P _ j
and the number of customers that buys the
j
-th item.
|
[
{
"input": "5 4\n100 200 300 400 500\n120 370 470 80\n",
"output": "1280 2350 2850 1140\n"
},
{
"input": "4 4\n0 2 10 2\n13 13 0 4\n",
"output": "52 52 10 18\n"
},
{
"input": "12 15\n16 592 222 983 729 338 747 61 451 815 838 281\n406 319 305 519 317 590 507 946 365 5 673 478 340 176 2\n",
"output": "6280 5466 5382 7410 5454 8120 7290 11680 5870 3670 8950 7000 5620 4608 3655\n"
},
{
"input": "5 5\n1000000000 1000000000 1000000000 1000000000 1000000000\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"output": "10000000000 10000000000 10000000000 10000000000 10000000000\n"
}
] |
https://atcoder.jp/contests/abc289/tasks/abc289_h
|
Problem Statement
On a number line are person
1
, person
2
, and person
3
. At time
0
, person
1
is at point
A
, person
2
is at point
B
, and person
3
is at point
C
.
Here,
A
,
B
, and
C
are all integers, and
A \equiv B \equiv C \pmod{2}
.
At time
0
, the three people start random walks. Specifically, a person that is at point
x
at time
t
(
t
is a non-negative integer) moves to point
(x-1)
or point
(x+1)
at time
(t+1)
with equal probability. (All choices of moves are random and independent.)
Find the probability, modulo
998244353
, that it is at time
T
that the three people are at the same point for the first time.
What is rational number modulo
998244353
?
We can prove that the sought probability is always a rational number.
Moreover, under the Constraints of this problem, when the value is represented as
\frac{P}{Q}
by two coprime integers
P
and
Q
, we can prove that there is a unique integer
R
such that
R \times Q \equiv P\pmod{998244353}
and
0 \leq R \lt 998244353
. Find such
R
.
|
[
{
"input": "1 1 3 1\n",
"output": "873463809\n"
},
{
"input": "0 0 0 0\n",
"output": "1\n"
},
{
"input": "0 2 8 9\n",
"output": "744570476\n"
},
{
"input": "47717 21993 74147 76720\n",
"output": "844927176\n"
}
] |
https://atcoder.jp/contests/abc288/tasks/abc288_a
|
Problem Statement
You are given
N
pairs of integers:
(A_1, B_1), (A_2, B_2), \ldots, (A_N, B_N)
.
For each
i = 1, 2, \ldots, N
, print
A_i + B_i
.
|
[
{
"input": "4\n3 5\n2 -6\n-5 0\n314159265 123456789\n",
"output": "8\n-4\n-5\n437616054\n"
}
] |
https://atcoder.jp/contests/abc288/tasks/abc288_b
|
Problem Statement
There were
N
participants in a contest. The participant ranked
i
-th had the nickname
S_i
.
Print the nicknames of the top
K
participants in
lexicographical order
.
What is lexicographical order?
Simply put, the lexicographical order is the order of words in a dictionary. As a formal description, below is an algorithm to order distinct strings
S
and
T
.
Let
S_i
denote the
i
-th character of a string
S
. We write
S \lt T
if
S
is lexicographically smaller than
T
, and
S \gt T
if
S
is larger.
Let
L
be the length of the shorter of
S
and
T
. For
i=1,2,\dots,L
, check whether
S_i
equals
T_i
.
If there is an
i
such that
S_i \neq T_i
, let
j
be the smallest such
i
. Compare
S_j
and
T_j
. If
S_j
is alphabetically smaller than
T_j
, we get
S \lt T
; if
S_j
is larger, we get
S \gt T
.
If there is no
i
such that
S_i \neq T_i
, compare the lengths of
S
and
T
. If
S
is shorter than
T
, we get
S \lt T
; if
S
is longer, we get
S \gt T
.
|
[
{
"input": "5 3\nabc\naaaaa\nxyz\na\ndef\n",
"output": "aaaaa\nabc\nxyz\n"
},
{
"input": "4 4\nz\nzyx\nzzz\nrbg\n",
"output": "rbg\nz\nzyx\nzzz\n"
},
{
"input": "3 1\nabc\narc\nagc\n",
"output": "abc\n"
}
] |
https://atcoder.jp/contests/abc288/tasks/abc288_c
|
Problem Statement
You are given a simple undirected graph with
N
vertices and
M
edges. The vertices are numbered
1
to
N
, and the
i
-th edge connects vertex
A_i
and vertex
B_i
.
Let us delete zero or more edges to remove cycles from the graph. Find the minimum number of edges that must be deleted for this purpose.
What is a simple undirected graph?
A simple undirected graph
is a graph without self-loops or multi-edges whose edges have no direction.
What is a cycle?
A
cycle
in a simple undirected graph is a sequence of vertices
(v_0, v_1, \ldots, v_{n-1})
of length at least
3
satisfying
v_i \neq v_j
if
i \neq j
such that for each
0 \leq i < n
, there is an edge between
v_i
and
v_{i+1 \bmod n}
.
|
[
{
"input": "6 7\n1 2\n1 3\n2 3\n4 2\n6 5\n4 6\n4 5\n",
"output": "2\n"
},
{
"input": "4 2\n1 2\n3 4\n",
"output": "0\n"
},
{
"input": "5 3\n1 2\n1 3\n2 3\n",
"output": "1\n"
}
] |
https://atcoder.jp/contests/abc288/tasks/abc288_d
|
Problem Statement
You are given an integer sequence of length
N
,
A = (A_1, A_2, \ldots, A_N)
, and a positive integer
K
.
For each
i = 1, 2, \ldots, Q
, determine whether a contiguous subsequence of
A
,
(A_{l_i}, A_{l_i+1}, \ldots, A_{r_i})
, is a
good sequence
.
Here, a sequence of length
n
,
X = (X_1, X_2, \ldots, X_n)
, is
good
if and only if there is a way to perform the operation below some number of times (possibly zero) to make all elements of
X
equal
0
.
Choose an integer
i
such that
1 \leq i \leq n-K+1
and an integer
c
(possibly negative). Add
c
to each of the
K
elements
X_{i}, X_{i+1}, \ldots, X_{i+K-1}
.
It is guaranteed that
r_i - l_i + 1 \geq K
for every
i = 1, 2, \ldots, Q
.
|
[
{
"input": "7 3\n3 -1 1 -2 2 0 5\n2\n1 6\n2 7\n",
"output": "Yes\nNo\n"
},
{
"input": "20 4\n-19 -66 -99 16 18 33 32 28 26 11 12 0 -16 4 21 21 37 17 55 -19\n5\n13 16\n4 11\n3 12\n13 18\n4 10\n",
"output": "No\nYes\nNo\nYes\nNo\n"
}
] |
https://atcoder.jp/contests/abc288/tasks/abc288_e
|
Problem Statement
There are
N
items in a shop, numbered as Item
1
, Item
2
,
\ldots
, Item
N
.
For each
i = 1, 2, \ldots, N
, the
regular price
of Item
i
is
A_i
yen. For each item, there is only one in stock.
Takahashi wants
M
items: Item
X_1
, Item
X_2
,
\ldots
, Item
X_M
.
He repeats the following until he gets all items he wants.
Let
r
be the number of unsold items now.
Choose an integer
j
such that
1 \leq j \leq r
, and buy the item with the
j
-th smallest item number among the unsold items, for its
regular price
plus
C_j
yen.
Print the smallest total amount of money needed to get all items Takahashi wants.
Takahashi may also buy items other than the ones he wants.
|
[
{
"input": "5 2\n3 1 4 1 5\n9 2 6 5 3\n3 5\n",
"output": "17\n"
},
{
"input": "20 8\n29 27 79 27 30 4 93 89 44 88 70 75 96 3 78 39 97 12 53 62\n32 38 84 49 93 53 26 13 25 2 76 32 42 34 18 77 14 67 88 12\n1 3 4 5 8 14 16 20\n",
"output": "533\n"
}
] |
https://atcoder.jp/contests/abc288/tasks/abc288_f
|
Problem Statement
You are given a positive integer
X
with
N
digits in decimal representation. None of the digits of
X
is
0
.
For a subset
S
of
\lbrace 1,2, \ldots, N-1 \rbrace
, let
f(S)
be defined as follows.
See the decimal representation of
X
as a string of length
N
, and decompose it into
|S| + 1
strings by splitting it between the
i
-th and
(i + 1)
-th characters if and only if
i \in S
.
Then, see these
|S| + 1
strings as integers in decimal representation, and let
f(S)
be the product of these
|S| + 1
integers.
There are
2^{N-1}
subsets of
\lbrace 1,2, \ldots, N-1 \rbrace
, including the empty set, which can be
S
. Find the sum of
f(S)
over all these
S
, modulo
998244353
.
|
[
{
"input": "3\n234\n",
"output": "418\n"
},
{
"input": "4\n5915\n",
"output": "17800\n"
},
{
"input": "9\n998244353\n",
"output": "258280134\n"
}
] |
https://atcoder.jp/contests/abc288/tasks/abc288_g
|
Problem Statement
There is zero or one bomb at each of positions
0, 1, 2, \ldots, 3^N-1
.
Let us say that positions
x
and
y
are
neighboring
each other if and only if the following condition is satisfied for every
i=1, \ldots, N
.
Let
x'
and
y'
be the
i
-th lowest digits of
x
and
y
in ternary representation, respectively. Then,
|x' - y'| \leq 1
.
It is known that there are exactly
A_i
bombs in total at the positions neighboring position
i
. Print a placement of bombs consistent with this information.
|
[
{
"input": "1\n0 1 1\n",
"output": "0 0 1\n"
},
{
"input": "2\n2 3 2 4 5 3 3 4 2\n",
"output": "0 1 0 1 0 1 1 1 0\n"
},
{
"input": "2\n0 0 0 0 0 0 0 0 0\n",
"output": "0 0 0 0 0 0 0 0 0\n"
}
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.