url
stringlengths 49
92
| description
stringlengths 22
4.78k
| cases
listlengths 0
6
|
---|---|---|
https://atcoder.jp/contests/abc274/tasks/abc274_b | Problem Statement
There is a grid with
H
rows from top to bottom and
W
columns from left to right. Let
(i, j)
denote the square at the
i
-th row from the top and
j
-th column from the left.
The squares are described by characters
C_{i,j}
. If
C_{i,j}
is
.
,
(i, j)
is empty; if it is
#
,
(i, j)
contains a box.
For integers
j
satisfying
1 \leq j \leq W
, let the integer
X_j
defined as follows.
X_j
is the number of boxes in the
j
-th column. In other words,
X_j
is the number of integers
i
such that
C_{i,j}
is
#
.
Find all of
X_1, X_2, \dots, X_W
. | [
{
"input": "3 4\n#..#\n.#.#\n.#.#\n",
"output": "1 2 0 3\n"
},
{
"input": "3 7\n.......\n.......\n.......\n",
"output": "0 0 0 0 0 0 0\n"
},
{
"input": "8 3\n.#.\n###\n.#.\n.#.\n.##\n..#\n##.\n.##\n",
"output": "2 7 4\n"
},
{
"input": "5 47\n.#..#..#####..#...#..#####..#...#...###...#####\n.#.#...#.......#.#...#......##..#..#...#..#....\n.##....#####....#....#####..#.#.#..#......#####\n.#.#...#........#....#......#..##..#...#..#....\n.#..#..#####....#....#####..#...#...###...#####\n",
"output": "0 5 1 2 2 0 0 5 3 3 3 3 0 0 1 1 3 1 1 0 0 5 3 3 3 3 0 0 5 1 1 1 5 0 0 3 2 2 2 2 0 0 5 3 3 3 3\n"
}
] |
https://atcoder.jp/contests/abc274/tasks/abc274_c | Problem Statement
You observed amoebae and kept some records.
Initially, there was one amoeba, numbered
1
.
You made
N
records. According to the
i
-th record, the amoeba numbered
A_i
disappeared by dividing itself into two new amoebae, which were then numbered
2i
and
2i+1
.
Here, amoeba
A_i
is said to be the parent of amoebae
2i
and
2i+1
.
For each
k=1,\ldots,2N+1
, how many generations away is amoeba
k
from amoeba
1
? | [
{
"input": "2\n1 2\n",
"output": "0\n1\n1\n2\n2\n"
},
{
"input": "4\n1 3 5 2\n",
"output": "0\n1\n1\n2\n2\n3\n3\n2\n2\n"
}
] |
https://atcoder.jp/contests/abc274/tasks/abc274_d | Problem Statement
You are given a sequence
A = (A_1, A_2, \dots, A_N)
of length
N
consisting of positive integers, and integers
x
and
y
.
Determine whether it is possible to place
N+1
points
p_1, p_2, \dots, p_N, p_{N+1}
in the
xy
-coordinate plane to satisfy all of the following conditions. (It is allowed to place two or more points at the same coordinates.)
p_1 = (0, 0)
.
p_2 = (A_1, 0)
.
p_{N+1} = (x, y)
.
The distance between the points
p_i
and
p_{i+1}
is
A_i
. (
1 \leq i \leq N
)
The segments
p_i p_{i+1}
and
p_{i+1} p_{i+2}
form a
90
degree angle. (
1 \leq i \leq N - 1
) | [
{
"input": "3 -1 1\n2 1 3\n",
"output": "Yes\n"
},
{
"input": "5 2 0\n2 2 2 2 2\n",
"output": "Yes\n"
},
{
"input": "4 5 5\n1 2 3 4\n",
"output": "No\n"
},
{
"input": "3 2 7\n2 7 4\n",
"output": "No\n"
},
{
"input": "10 8 -7\n6 10 4 1 5 9 8 6 5 1\n",
"output": "Yes\n"
}
] |
https://atcoder.jp/contests/abc274/tasks/abc274_e | Problem Statement
In a two-dimensional plane, there are
N
towns and
M
chests. Town
i
is at the coordinates
(X_i,Y_i)
, and chest
i
is at the coordinates
(P_i,Q_i)
.
Takahashi will go on a trip where he starts at the origin, visits all
N
towns, and then returns to the origin.
It is not mandatory to visit chests, but each chest contains an accelerator. Each time he picks up an accelerator, his moving speed gets multiplied by
2
.
Takahashi's initial moving speed is
1
. Find the shortest time needed to complete the trip. | [
{
"input": "2 1\n1 1\n0 1\n1 0\n",
"output": "2.5000000000\n"
},
{
"input": "2 1\n1 1\n0 1\n100 0\n",
"output": "3.4142135624\n"
},
{
"input": "1 2\n4 4\n1 0\n0 1\n",
"output": "4.3713203436\n"
}
] |
https://atcoder.jp/contests/abc274/tasks/abc274_f | Problem Statement
On a number line, there are
N
fish swimming.
Fish
i
, which has a weight of
W_i
, is at the coordinate
X_i
at time
0
and moves at a speed of
V_i
in the positive direction.
Takahashi will choose an arbitrary real number
t
greater than or equal to
0
and do the following action at time
t
just once.
Action: Choose an arbitrary real number
x
. Catch all fish whose coordinates are between
x
and
x+A
, inclusive.
Find the maximum total weight of fish that he can catch. | [
{
"input": "3 10\n100 0 100\n1 10 30\n10 20 10\n",
"output": "111\n"
},
{
"input": "3 10\n100 100 100\n1 10 30\n10 20 10\n",
"output": "100\n"
},
{
"input": "4 10\n1000 100 10\n100 99 1\n10 0 100\n1 1 1\n",
"output": "1110\n"
}
] |
https://atcoder.jp/contests/abc274/tasks/abc274_g | Problem Statement
There is a grid with
H
rows from top to bottom and
W
columns from left to right. Let
(i, j)
denote the square at the
i
-th row from the top and
j
-th column from the left.
Square
(i, j)
is occupied by an obstacle if
S_{i,j}=
#
, and is empty if
S_{i,j}=
.
.
Takahashi will install some surveillance cameras in the grid.
A surveillance camera can be placed at a square without an obstacle, in one of the four directions: up, down, left, or right.
Multiple surveillance cameras may be placed at the same square.
Each surveillance camera monitors the square it is placed at, and the squares in the direction it is placed in, as far as there is no obstacle in between.
At least how many surveillance cameras are needed to monitor all squares without an obstacle? | [
{
"input": "3 3\n...\n.#.\n...\n",
"output": "4\n"
},
{
"input": "3 5\n...##\n.#...\n...#.\n",
"output": "5\n"
},
{
"input": "14 107\n...........................................................................................................\n...........................................................................................................\n..#########..###....###########..###.......###...###########..####.......###...###########...###########...\n..########..###....###########....###.....###...###########...#####......###..###########...###########....\n..#######..###.....###.............###...###....###...........######.....###..###...........###............\n..######..###......###..............###.###.....###...........###.###....###..###...........###............\n..#####..###.......############......#####......############..###..###...###..###...........############...\n..####....###......############.......###.......############..###...###..###..###...........############...\n..###......###.....###................###.......###...........###....###.###..###...........###............\n..##........###....###................###.......###...........###.....######..###...........###............\n..#..........###...############.......###.......############..###......#####..############..############...\n..............###...###########.......###........###########..###.......####...###########...###########...\n...........................................................................................................\n...........................................................................................................\n",
"output": "91\n"
}
] |
https://atcoder.jp/contests/abc274/tasks/abc274_h | Problem Statement
For sequences
B=(B_1,B_2,\dots,B_M)
and
C=(C_1,C_2,\dots,C_M)
, each of length
M
, consisting of non-negative integers, let the
XOR sum
S(B,C)
of
B
and
C
be defined as the sequence
(B_1\oplus C_1, B_2\oplus C_2, ..., B_{M}\oplus C_{M})
of length
M
consisting of non-negative integers. Here,
\oplus
represents bitwise XOR.
For instance, if
B = (1, 2, 3)
and
C = (3, 5, 7)
, we have
S(B, C) = (1\oplus 3, 2\oplus 5, 3\oplus 7) = (2, 7, 4)
.
You are given a sequence
A = (A_1, A_2, \dots, A_N)
of non-negative integers. Let
A(i, j)
denote the contiguous subsequence composed of the
i
-th through
j
-th elements of
A
.
You will be given
Q
queries explained below and asked to process all of them.
Each query gives you integers
a
,
b
,
c
,
d
,
e
, and
f
, each between
1
and
N
, inclusive. These integers satisfy
a \leq b
,
c \leq d
,
e \leq f
, and
b-a=d-c
. If
S(A(a, b), A(c, d))
is
strictly
lexicographically smaller than
A(e, f)
, print
Yes
; otherwise, print
No
.
What is bitwise XOR?
The exclusive logical sum
a \oplus b
of two integers
a
and
b
is defined as follows.
The
2^k
's place (
k \geq 0
) in the binary notation of
a \oplus b
is
1
if exactly one of the
2^k
's places in the binary notation of
a
and
b
is
1
; otherwise, it is
0
.
For example,
3 \oplus 5 = 6
(In binary notation:
011 \oplus 101 = 110
).
What is lexicographical order on sequences?
A sequence
A = (A_1, \ldots, A_{|A|})
is said to be
strictly lexicographically smaller
than a sequence
B = (B_1, \ldots, B_{|B|})
if and only if 1. or 2. below is satisfied.
|A|<|B|
and
(A_{1},\ldots,A_{|A|}) = (B_1,\ldots,B_{|A|})
.
There is an integer
1\leq i\leq \min\{|A|,|B|\}
that satisfies both of the following.
(A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1})
.
A_i < B_i
. | [
{
"input": "4 5\n1 2 3 1\n1 3 2 4 1 4\n1 2 2 3 3 4\n1 1 2 2 3 4\n1 2 2 3 3 3\n1 4 1 4 1 1\n",
"output": "No\nNo\nYes\nNo\nYes\n"
},
{
"input": "10 10\n725560240 9175925348 9627229768 7408031479 623321125 4845892509 8712345300 1026746010 4844359340 2169008582\n5 6 5 6 2 6\n5 6 1 2 1 1\n3 8 3 8 1 6\n5 10 1 6 1 7\n3 4 1 2 5 5\n7 10 4 7 2 3\n3 6 1 4 7 9\n4 5 3 4 8 9\n2 6 1 5 5 8\n4 8 1 5 1 9\n",
"output": "Yes\nYes\nYes\nYes\nNo\nNo\nNo\nNo\nNo\nNo\n"
}
] |
https://atcoder.jp/contests/arc151/tasks/arc151_a | Problem Statement
Below, a
01
-sequence is a string consisting of
0
and
1
.
You are given two
01
-sequences
S
and
T
of length
N
each.
Print the lexicographically smallest
01
-sequence
U
of length
N
that satisfies the condition below.
The Hamming distance between
S
and
U
equals the Hamming distance between
T
and
U
.
If there is no such
01
-sequence
U
of length
N
, print
-1
instead.
What is Hamming distance?
The
Hamming distance
between
01
-sequences
X = X_1X_2\ldots X_N
and
Y = Y_1Y_2\ldots Y_N
is the number of integers
1 \leq i \leq N
such that
X_i \neq Y_i
.
What is lexicographical order?
A
01
-sequence
X = X_1X_2\ldots X_N
is
lexicographically smaller
than a
01
-sequence
Y = Y_1Y_2\ldots Y_N
when there is an integer
1 \leq i \leq N
that satisfies both of the conditions below.
X_1X_2\ldots X_{i-1} = Y_1Y_2\ldots Y_{i-1}
.
X_i =
0
and
Y_i =
1
. | [
{
"input": "5\n00100\n10011\n",
"output": "00001\n"
},
{
"input": "1\n0\n1\n",
"output": "-1\n"
}
] |
https://atcoder.jp/contests/arc151/tasks/arc151_b | Problem Statement
You are given a permutation
P = (P_1, P_2, \ldots, P_N)
of
(1, 2, \ldots, N)
.
Print the number of integer sequences
A = (A_1, A_2, \ldots, A_N)
of length
N
that satisfy both of the conditions below, modulo
998244353
.
1 \leq A_i \leq M
for every
i = 1, 2, \ldots, N
.
The integer sequence
A
is lexicographically smaller than the integer sequence
(A_{P_1}, A_{P_2}, \ldots, A_{P_N})
.
What is lexicographical order?
An integer sequence
X = (X_1,X_2,\ldots,X_N)
is
lexicographically smaller
than an integer sequence
Y = (Y_1,Y_2,\ldots,Y_N)
when there is an integer
1 \leq i \leq N
that satisfies both of the conditions below.
For all integers
j
(
1 \leq j \leq i-1
),
X_j=Y_j
.
X_i < Y_i | [
{
"input": "4 2\n4 1 3 2\n",
"output": "6\n"
},
{
"input": "1 1\n1\n",
"output": "0\n"
},
{
"input": "20 100000\n11 15 3 20 17 6 1 9 5 19 10 16 7 8 12 2 18 14 4 13\n",
"output": "55365742\n"
}
] |
https://atcoder.jp/contests/arc151/tasks/arc151_c | Problem Statement
There are
N
squares called square
1
, square
2
,
\ldots
, square
N
, where square
i
and square
i+1
are adjacent for each
i = 1, 2, \ldots, N-1
.
Initially,
M
of the squares have
0
or
1
written on them.
Specifically, for each
i = 1, 2, \ldots, M
,
Y_i
is written on square
X_i
.
The other
N-M
squares have nothing written on them.
Takahashi and Aoki will play a game against each other.
The two will alternately act as follows, with Takahashi going first.
Choose a square with nothing written yet, and write
0
or
1
on that square.
Here, it is forbidden to make two adjacent squares have the same digit written on them.
The first player to be unable to act loses; the other player wins.
Determine the winner when both players adopt the optimal strategy for their own victory. | [
{
"input": "7 2\n2 0\n4 1\n",
"output": "Takahashi\n"
},
{
"input": "3 3\n1 1\n2 0\n3 1\n",
"output": "Aoki\n"
},
{
"input": "1000000000000000000 0\n",
"output": "Aoki\n"
}
] |
https://atcoder.jp/contests/arc151/tasks/arc151_d | Problem Statement
You are given an integer sequence
A = (A_0, A_1, \ldots, A_{2^N-1})
of length
2^N
.
Additionally,
Q
queries are given.
For each
i = 1, 2, \ldots, Q
, the
i
-th query is represented by two integers
X_i
and
Y_i
and asks you to perform the operation below.
For each
j = 0, 1, 2, \ldots, 2^N-1
in this order, do the following.
First, let
b_{N-1}b_{N-2}\ldots b_0
be the binary representation of
j
with
N
digits. Additionally, let
j'
be the integer represented by the binary representation
b_{N-1}b_{N-2}\ldots b_0
after flipping the bit
b_{X_i}
(changing
0
to
1
and
1
to
0
).
Then, if
b_{X_i} = Y_i
, add the value of
A_j
to
A_{j'}
.
Print each element of
A
, modulo
998244353
, after processing all the queries in the order they are given in the input.
What is binary representation with
N
digits?
The binary representation of an non-negative integer
X
(
0 \leq X < 2^N
) with
N
digits is the unique sequence
b_{N-1}b_{N-2}\ldots b_0
of length
N
consisting of
0
and
1
that satisfies:
\sum_{i = 0}^{N-1} b_i \cdot 2^i = X
. | [
{
"input": "2 2\n0 1 2 3\n1 1\n0 0\n",
"output": "2 6 2 5\n"
},
{
"input": "3 10\n606248357 338306877 919152167 981537317 808873985 845549408 680941783 921035119\n1 1\n0 0\n0 0\n0 0\n0 1\n0 1\n0 1\n2 0\n2 0\n2 0\n",
"output": "246895115 904824001 157201385 744260759 973709546 964549010 61683812 205420980\n"
}
] |
https://atcoder.jp/contests/arc151/tasks/arc151_e | Problem Statement
You are given an integer sequence
A = (A_1, A_2, \ldots, A_N)
of length
N
.
Additionally, its contiguous subsequences of lengths
P
and
Q
are given:
X = (X_1, X_2, \ldots, X_P)
and
Y = (Y_1, Y_2, \ldots, Y_Q)
.
You can perform the four operations on
X
below any number of times (possibly zero) in any order.
Add an arbitrary integer at the beginning of
X
.
Delete the element at the beginning of
X
.
Add an arbitrary integer at the end of
X
.
Delete the element at the end of
X
.
Here,
X
must be a
non-empty
contiguous subsequence of
A
before and after each operation.
Find the minimum total number of operations needed to make
X
equal
Y
.
Under the Constraints of this problem, it is guaranteed that one can always make
X
equal
Y
by repeating operations.
What is a contiguous subsequence?
A sequence
X = (X_1, X_2, \ldots, X_P)
is a
contiguous subsequence
of
A = (A_1, A_2, \ldots, A_N)
when there is an integer
l
satisfying
1 \leq l \leq N-P+1
such that
X_i = A_{l+i-1}
for every
i = 1, 2, \ldots, P
. | [
{
"input": "7\n3 1 4 1 5 7 2\n2\n3 1\n3\n1 5 7\n",
"output": "3\n"
},
{
"input": "20\n2 5 1 2 7 7 4 5 3 7 7 4 5 5 5 4 6 5 6 1\n6\n1 2 7 7 4 5\n7\n7 4 5 5 5 4 6\n",
"output": "7\n"
}
] |
https://atcoder.jp/contests/arc151/tasks/arc151_f | Problem Statement
Takahashi and Aoki will play a game against each other using cards in three colors: red, green, and blue.
Initially, Takahashi has
R_1
red,
G_1
green, and
B_1
blue cards, and Aoki has
R_2
red,
G_2
green, and
B_2
blue cards in their hands.
Each player knows the hands of both players.
The game starts with Takahashi on
offense
and Aoki on
defense
, and repeats the process below.
First, the player on offense plays an arbitrary card from his hand.
Then, the player on defense either plays a card with the same color from his hand, or does nothing. If a card is played, the players switch between offense and defense.
The first player to have zero cards in his hand wins the game. Determine the winner when both players adopt the optimal strategy for their own victory.
For each input file, solve
T
independent test cases. | [
{
"input": "10\n1 1 1 0 1 2\n1 2 3 4 5 6\n1 2 3 3 2 1\n1 0 1 0 1 0\n1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000\n711741968710511023 863182190136397525 935042422763027373 565732706644706921 453428280447672223 188382995979861200\n166020598057882490 762504522442931582 957390622951053643 932567512152300679 473764934043971365 82803157126515469\n895348321962139989 376963632541282296 624486091834022571 175064808312523035 217537722506696493 203742827664922704\n802346905414720749 973713209304621356 275109783325269828 588060532191410837 516874290286751783 747001196732741840\n539971830806602684 270896673960719346 124580938028911221 18175990488280605 360214649380675201 155957964634289774\n",
"output": "Takahashi\nTakahashi\nAoki\nTakahashi\nTakahashi\nAoki\nAoki\nAoki\nAoki\nTakahashi\n"
}
] |
https://atcoder.jp/contests/joi2023yo1b/tasks/joi2023_yo1b_a | 問題文
1
日は
24
時間である.
整数
X
が与えられる.
X
日は何時間か求めよ. | [
{
"input": "3\n",
"output": "72\n"
},
{
"input": "100\n",
"output": "2400\n"
}
] |
https://atcoder.jp/contests/joi2023yo1b/tasks/joi2023_yo1b_b | 問題文
2
つの整数
A, B
が与えられる.
A
と
B
の大小を比較し,
A < B
ならば
-1
を,
A = B
ならば
0
を,
A > B
ならば
1
を出力せよ. | [
{
"input": "3\n7\n",
"output": "-1\n"
},
{
"input": "10\n10\n",
"output": "0\n"
},
{
"input": "1000\n1\n",
"output": "1\n"
}
] |
https://atcoder.jp/contests/joi2023yo1b/tasks/joi2023_yo1b_c | 問題文
同じ文字列を
2
つつなげて得られる文字列を
繰り返し文字列
と呼ぶことにする.
例えば,
OIOI
や
JJJJJJ
,
JOIOIJOIOI
は繰り返し文字列であるが,
IOOI
や
JOIIOI
は繰り返し文字列ではない.
長さ
N
の文字列
S
が与えられる.
N
は偶数であり,
S
の各文字は
J
,
O
,
I
のいずれかである.
S
が繰り返し文字列である場合は
Yes
を,そうでない場合は
No
を出力せよ. | [
{
"input": "6\nJOIJOI\n",
"output": "Yes\n"
},
{
"input": "6\nIOIOIO\n",
"output": "No\n"
},
{
"input": "2\nOO\n",
"output": "Yes\n"
}
] |
https://atcoder.jp/contests/joi2023yo1b/tasks/joi2023_yo1b_d | 問題文
長さ
N
の整数列
A = (A_1, A_2, \ldots, A_N)
と長さ
M
の整数列
B = (B_1, B_2, \ldots, B_M)
が与えられる.
あなたはこれらの数列を用いてゲームを行う.最初,このゲームの点数は
0
である.
このゲームでは
N
回のラウンドを行う.
i
回目 (
1 \leqq i \leqq N
) のラウンドは以下のように進行する.
現在の点数に
A_i
を加算する.
もし加算後の点数が
B_1, B_2, \ldots, B_M
のいずれかと等しい場合,点数を
0
にする.
最後のラウンドが終了した時点でのこのゲームの点数を出力せよ. | [
{
"input": "4\n3 1 4 1\n4\n2 7 1 8\n",
"output": "0\n"
},
{
"input": "5\n1 4 1 4 2\n3\n1 3 5\n",
"output": "6\n"
},
{
"input": "2\n10 10\n3\n1 11 111\n",
"output": "20\n"
}
] |
https://atcoder.jp/contests/abc273/tasks/abc273_a | Problem Statement
A function
f(x)
defined for non-negative integer
x
satisfies the following conditions:
f(0) = 1
;
f(k) = k \times f(k-1)
for all positive integers
k
.
Find
f(N)
. | [
{
"input": "2\n",
"output": "2\n"
},
{
"input": "3\n",
"output": "6\n"
},
{
"input": "0\n",
"output": "1\n"
},
{
"input": "10\n",
"output": "3628800\n"
}
] |
https://atcoder.jp/contests/abc273/tasks/abc273_b | Problem Statement
Given a non-negative integer
X
, perform the following operation for
i=1,2,\dots,K
in this order and find the resulting
X
.
Round
X
off to the nearest
10^i
.
Formally, replace
X
with
Y
that is "the largest multiple of
10^i
that minimizes
|Y-X|
."
Here are some examples:
Rounding
273
off to the nearest
10^2
yields
300
.
Rounding
999
off to the nearest
10^3
yields
1000
.
Rounding
100
off to the nearest
10^{10}
yields
0
.
Rounding
1015
off to the nearest
10^1
yields
1020
. | [
{
"input": "2048 2\n",
"output": "2100\n"
},
{
"input": "1 15\n",
"output": "0\n"
},
{
"input": "999 3\n",
"output": "1000\n"
},
{
"input": "314159265358979 12\n",
"output": "314000000000000\n"
}
] |
https://atcoder.jp/contests/abc273/tasks/abc273_c | Problem Statement
You are given a sequence
A = (A_1, A_2, \ldots, A_N)
of length
N
.
For each
K = 0, 1, 2, \ldots, N-1
, solve the following problem.
Find the number of integers
i
between
1
and
N
(inclusive) such that:
A
contains exactly
K
distinct integers greater than
A_i
. | [
{
"input": "6\n2 7 1 8 2 8\n",
"output": "2\n1\n2\n1\n0\n0\n"
},
{
"input": "1\n1\n",
"output": "1\n"
},
{
"input": "10\n979861204 57882493 979861204 447672230 644706927 710511029 763027379 710511029 447672230 136397527\n",
"output": "2\n1\n2\n1\n2\n1\n1\n0\n0\n0\n"
}
] |
https://atcoder.jp/contests/abc273/tasks/abc273_d | Problem Statement
There is a grid with
H
horizontal rows and
W
vertical columns.
(i, j)
denotes the square at the
i
-th row from the top and
j
-th column from the left.
N
squares,
(r_1, c_1), (r_2, c_2), \ldots, (r_N, c_N)
, have walls.
Takahashi is initially at square
(r_\mathrm{s}, c_\mathrm{s})
.
Q
instructions are given to Takahashi.
For
i = 1, 2, \ldots, Q
, the
i
-th instruction is represented by a pair of a character
d_i
and a positive integer
l_i
.
d_i
is one of
L
,
R
,
U
, and
D
, representing the directions of left, right, up, and down, respectively.
Given the
i
-th direction, Takahashi repeats the following action
l_i
times:
If a square without a wall is adjacent to the current square in the direction represented by
d_i
, move to that square;
otherwise, do nothing.
For
i = 1, 2, \ldots, Q
, print the square where Takahashi will be after he follows the first
i
instructions. | [
{
"input": "5 5 4 4\n3\n5 3\n2 2\n1 4\n4\nL 2\nU 3\nL 2\nR 4\n",
"output": "4 2\n3 2\n3 1\n3 5\n"
},
{
"input": "6 6 6 3\n7\n3 1\n4 3\n2 6\n3 4\n5 5\n1 1\n3 2\n10\nD 3\nU 3\nL 2\nD 2\nU 3\nD 3\nU 3\nR 3\nL 3\nD 1\n",
"output": "6 3\n5 3\n5 1\n6 1\n4 1\n6 1\n4 1\n4 2\n4 1\n5 1\n"
}
] |
https://atcoder.jp/contests/abc273/tasks/abc273_e | Problem Statement
We have an integer sequence
A
and a notebook. The notebook has
10^9
pages.
You are given
Q
queries. Each query is of one of the following four kinds:
ADD
x
: append an integer
x
to the tail of
A
.
DELETE: remove the last term of
A
if
A
is not empty; do nothing otherwise.
SAVE
y
: erase the sequence recorded on the
y
-th page of the notebook, and record the current
A
onto the
y
-th page.
LOAD
z
: replace
A
with the sequence recorded on the
z
-th page of the notebook.
Initially,
A
is an empty sequence, and an empty sequence is recorded on each page of the notebook.
Process
Q
queries successively in the given order and print the last term of
A
after processing each query.
The use of fast input and output methods is recommended because of potentially large input and output. | [
{
"input": "11\nADD 3\nSAVE 1\nADD 4\nSAVE 2\nLOAD 1\nDELETE\nDELETE\nLOAD 2\nSAVE 1\nLOAD 3\nLOAD 1\n",
"output": "3 3 4 4 3 -1 -1 4 4 -1 4\n"
},
{
"input": "21\nADD 4\nADD 3\nDELETE\nADD 10\nLOAD 7\nSAVE 5\nSAVE 5\nADD 4\nADD 4\nADD 5\nSAVE 5\nADD 2\nDELETE\nADD 1\nSAVE 5\nADD 7\nADD 8\nDELETE\nADD 4\nDELETE\nLOAD 5\n",
"output": "4 3 4 10 -1 -1 -1 4 4 5 5 2 5 1 1 7 8 7 4 7 1\n"
}
] |
https://atcoder.jp/contests/abc273/tasks/abc273_f | Problem Statement
Takahashi is at the origin of a number line. Takahashi wants to reach the goal at coordinate
X
.
Also, there are
N
walls and
N
hammers on the number line.
At coordinates
Y_1,Y_2,\dots,Y_N
are walls of types
1,2,\dots,N
, respectively.
Initially, Takahashi cannot get over the walls.
At coordinates
Z_1,Z_2,\dots,Z_N
are hammers of types
1,2,\dots,N
, respectively.
When he arrives at a coordinate with a hammer, he obtains the hammer.
The hammer of type
i
is dedicated to destroying the wall of type
i
. After he obtains the hammer of type
i
, he can destroy the wall of type
i
and get over it.
Determine if he can reach the goal. If he can, find the minimum distance he travels. | [
{
"input": "3 10\n-2 8 -5\n5 -10 3\n",
"output": "40\n"
},
{
"input": "5 -1\n10 -20 30 -40 50\n-10 20 -30 40 -50\n",
"output": "1\n"
},
{
"input": "1 100\n30\n60\n",
"output": "-1\n"
},
{
"input": "4 865942261\n703164879 -531670946 -874856231 -700164975\n-941120316 599462305 -649785130 665402307\n",
"output": "4078987507\n"
}
] |
https://atcoder.jp/contests/abc273/tasks/abc273_g | Problem Statement
Find the number, modulo
998244353
, of square matrices of size
N
whose elements are non-negative integers, that satisfy both of the following two conditions:
for all
i = 1, 2, \ldots, N
, the sum of the elements in the
i
-th row is
R_i
;
for all
i = 1, 2, \ldots, N
, the sum of the elements in the
i
-th column is
C_i
.
Note that
R_i
and
C_i
given in the input are integers between
0
and
2
(see Constraints). | [
{
"input": "3\n1 1 1\n0 1 2\n",
"output": "3\n"
},
{
"input": "3\n1 1 1\n2 2 2\n",
"output": "0\n"
},
{
"input": "18\n2 0 1 2 0 1 1 2 1 1 2 0 1 2 2 1 0 0\n1 1 0 1 1 1 1 1 1 1 1 1 2 1 1 0 2 2\n",
"output": "968235177\n"
}
] |
https://atcoder.jp/contests/abc273/tasks/abc273_h | Problem Statement
We have a sequence
A
consisting of integer pairs. Initially,
A = ( (0, 1), (1, 0) )
.
You may perform the following operation on
A
as many (possibly zero) times as you want:
choose
adjacent
two integer pairs
(a, b)
and
(c, d)
, and insert
(a + c, b + d)
between them.
For a sequence
T
consisting of integer pairs, let us define
f(T)
as follows:
let
f(T) =
(the minimum number of operations required to make every element of
T
contained in
A
).
We say that "every element of
T
is contained in
A
" if, for all elements
x
contained in
T
,
x
is contained in (the set consisting of the elements contained in
A
).
Here, if there are no such operations, let
f(T) = 0
.
There is a sequence
S = ((a_1, b_1), (a_2, b_2), \dots, (a_N, b_N))
consisting of
N
integer pairs. Here, all elements of
S
are pairwise distinct.
There are
\frac{N \times (N+1)}{2}
possible consecutive subarrays
S_{l,r}=((a_l,b_l),(a_{l+1},b_{l+1}),\dots,(a_r,b_r))
of
S
. Find the sum, modulo
998244353
, of
f(S_{l,r})
over those subarrays.
Formally, find
\displaystyle \sum^{N} _ {l=1} \sum^{N} _ {r=l} f(S_{l,r})
, modulo
998244353
. | [
{
"input": "7\n1 2\n3 7\n3 5\n0 0\n1000000000 1\n0 1\n6 3\n",
"output": "3511324\n"
}
] |
https://atcoder.jp/contests/arc150/tasks/arc150_a | Problem Statement
You are given a string of length
N
,
S=S_1S_2\dots S_N
, consisting of
0
,
1
, and
?
.
We like to replace every
?
with
0
or
1
so that all of the following conditions are satisfied.
S
contains exactly
K
occurrences of
1
.
These
K
occurrences of
1
are consecutive. That is, there is an
i\ (1 \leq i \le N-K+1)
such that
S_i=S_{i+1}=\dots=S_{i+K-1}=
1
.
Determine whether there is exactly one way to replace the characters to satisfy the conditions.
You have
T
test cases to solve. | [
{
"input": "4\n3 2\n1??\n4 2\n?1?0\n6 3\n011?1?\n10 5\n00?1???10?\n",
"output": "Yes\nNo\nNo\nYes\n"
}
] |
https://atcoder.jp/contests/arc150/tasks/arc150_b | Problem Statement
You are given positive integers
A
and
B
.
Find the minimum value of
X+Y
for non-negative integers
X
and
Y
such that
B+Y
is a multiple of
A+X
.
You have
T
test cases to solve. | [
{
"input": "5\n11 23\n8 16\n4394 993298361\n95392025 569922442\n8399283 10293\n",
"output": "2\n0\n65\n2429708\n8388990\n"
}
] |
https://atcoder.jp/contests/arc150/tasks/arc150_c | Problem Statement
We have a connected undirected graph
G
with
N
vertices and
M
edges. The vertices are numbered
1
to
N
. The
i
-th edge connects vertices
U_i
and
V_i
.
Additionally, we are given an integer sequence of length
N
,
A=(A_1,\ A_2, \dots,\ A_N)
, and an integer sequence of length
K
,
B=(B_1,\ B_2,\ \dots,\ B_K)
.
Determine whether
G
,
A
, and
B
satisfy the following condition.
For every simple path from vertex
1
to
N
in
G
,
v=(v_1,\ v_2, \dots,\ v_k)\ (v_1=1,\ v_k=N)
,
B
is a (not necessarily contiguous) subsequence of
(A_{v_1},\ A_{v_2},\ \dots,\ A_{v_k})
. | [
{
"input": "6 6 3\n1 2\n1 3\n2 4\n3 5\n4 6\n5 6\n1 2 4 5 2 6\n1 2 6\n",
"output": "Yes\n"
},
{
"input": "5 5 3\n1 2\n2 3\n3 4\n4 5\n2 5\n1 2 3 5 2\n1 3 2\n",
"output": "No\n"
},
{
"input": "10 20 3\n5 6\n5 10\n5 7\n3 5\n3 7\n2 6\n3 8\n4 5\n5 8\n7 10\n1 6\n1 9\n4 6\n1 2\n1 4\n6 7\n4 8\n2 5\n3 10\n6 9\n2 5 8 5 1 5 1 1 5 10\n2 5 1\n",
"output": "Yes\n"
}
] |
https://atcoder.jp/contests/arc150/tasks/arc150_d | Problem Statement
We have a rooted tree with
N
vertices numbered
1
to
N
. Vertex
1
is the root of the tree, and the parent of vertex
i\ (2\leq i)
is vertex
p_i
.
Each vertex has a color: black or white. Initially, all vertices are white.
In this rooted tree, vertex
i
is said to be
good
when all vertices on the simple path connecting vertices
1
and
i
(including vertices
1
and
i
) are black, and
bad
otherwise.
Until all vertices are black, let us repeat the following operation: choose one vertex from the bad vertices uniformly at random, and repaint the chosen vertex black.
Find the expected value of the number of times we perform the operation, modulo
998244353
.
The definition of the expected value modulo
998244353
It can be proved that the sought expected value is always a rational number. Additionally, when that value is represented as an irreducible fraction
\frac{P}{Q}
, it can also be proved that
Q \not \equiv 0 \pmod{998244353}
. Therefore, there is a unique integer
R
such that
R \times Q \equiv P \pmod{998244353}
and
0 \leq R < 998244353
. Find this
R
. | [
{
"input": "4\n1 1 3\n",
"output": "831870300\n"
},
{
"input": "15\n1 2 1 1 4 5 3 3 5 10 3 6 3 13\n",
"output": "515759610\n"
}
] |
https://atcoder.jp/contests/arc150/tasks/arc150_e | Problem Statement
NK
people are standing in a line from left to right. Let us denote them as person
i
(0\leq i \leq NK-1)
from left to right.
Each person is always facing either left or right. The direction of each person at time
t=0
is represented by a string of length
N
,
S=S_0 S_1 \dots S_{N-1}
, consisting of
L
and
R
. At time
t=0
, person
i
is facing left if
S_{i \bmod N}
is
L
, and right if it is
R
.
At time
t=0.5,\ 1.5,\ 2.5 ,\ \dots
, these people
simultaneously
change their directions according to the following rules.
When a person is facing left:
If there are one or more people in the direction the person is facing, and more than half of those people are facing right, the person changes direction and faces right. Otherwise, the person does not change direction.
When a person is facing right:
If there are one or more people in the direction the person is facing, and more than half of those people are facing left, the person changes direction and faces left. Otherwise, the person does not change direction.
Find the sum, modulo
998244353
, of the numbers of times the
NK
people change direction from time
t=0
to
t=10^{100}
. | [
{
"input": "7 1\nRRLRLLL\n",
"output": "9\n"
},
{
"input": "4 10\nLLRR\n",
"output": "0\n"
},
{
"input": "23 200\nRLRRRLLLLLLLLRRRLLRLRRR\n",
"output": "2207\n"
}
] |
https://atcoder.jp/contests/arc150/tasks/arc150_f | Problem Statement
We have a sequence of positive integers of length
N^2
,
A=(A_1,\ A_2,\ \dots,\ A_{N^2})
, and a positive integer
S
. For this sequence of positive integers,
A_i=A_{i+N}
holds for positive integers
i\ (1\leq i \leq N^2-N)
, and only
A_1,\ A_2,\ \dots,\ A_N
are given as the input.
Find the minimum integer
L
such that every sequence
B
of positive integers totaling
S
is a (not necessarily contiguous) subsequence of the sequence
(A_1,\ A_2,\ \dots,\ A_L)
of positive integers.
It can be shown that at least one such
L
exists under the Constraints of this problem. | [
{
"input": "6 4\n1 1 2 1 4 3\n",
"output": "9\n"
},
{
"input": "14 5\n1 1 1 2 3 1 2 4 5 1 1 2 3 1\n",
"output": "11\n"
},
{
"input": "19 10\n1 6 2 7 4 8 5 9 1 10 4 1 3 1 3 2 2 2 1\n",
"output": "39\n"
}
] |
https://atcoder.jp/contests/abc272/tasks/abc272_a | Problem Statement
You are given
N
integers
A_1,A_2,\dots
, and
A_N
.
Find the sum of the
N
integers. | [
{
"input": "3\n2 7 2\n",
"output": "11\n"
},
{
"input": "1\n3\n",
"output": "3\n"
}
] |
https://atcoder.jp/contests/abc272/tasks/abc272_b | Problem Statement
There are
N
people numbered
1,2,\ldots,N
.
M
parties were held.
k_i
people attended the
i
-th
(1\leq i \leq M)
party, and they were People
x_{i,1},x_{i,2},\ldots,x_{i,k_i}
.
Determine if every two people attended the same party at least once. | [
{
"input": "3 3\n2 1 2\n2 2 3\n2 1 3\n",
"output": "Yes\n"
},
{
"input": "4 2\n3 1 2 4\n3 2 3 4\n",
"output": "No\n"
}
] |
https://atcoder.jp/contests/abc272/tasks/abc272_c | Problem Statement
You are given a sequence
A=(A_1,A_2,\ldots,A_N)
of length
N
consisting of non-negative integers.
Determine if there is an even number represented as the sum of two different elements of
A
. If it exists, find the maximum such number. | [
{
"input": "3\n2 3 4\n",
"output": "6\n"
},
{
"input": "2\n1 0\n",
"output": "-1\n"
}
] |
https://atcoder.jp/contests/abc272/tasks/abc272_d | Problem Statement
There is a grid with
N \times N
squares. We denote by
(i, j)
the square at the
i
-th row from the top and
j
-th column from the left.
Initially, a piece is placed on
(1, 1)
. You may repeat the following operation any number of times:
Let
(i, j)
be the square the piece is currently on. Move the piece to the square whose distance from
(i, j)
is exactly
\sqrt{M}
.
Here, we define the distance between square
(i, j)
and square
(k, l)
as
\sqrt{(i-k)^2+(j-l)^2}
.
For all squares
(i, j)
, determine if the piece can reach
(i, j)
. If it can, find the minimum number of operations required to do so. | [
{
"input": "3 1\n",
"output": "0 1 2\n1 2 3\n2 3 4\n"
},
{
"input": "10 5\n",
"output": "0 3 2 3 2 3 4 5 4 5\n3 4 1 2 3 4 3 4 5 6\n2 1 4 3 2 3 4 5 4 5\n3 2 3 2 3 4 3 4 5 6\n2 3 2 3 4 3 4 5 4 5\n3 4 3 4 3 4 5 4 5 6\n4 3 4 3 4 5 4 5 6 5\n5 4 5 4 5 4 5 6 5 6\n4 5 4 5 4 5 6 5 6 7\n5 6 5 6 5 6 5 6 7 6\n"
}
] |
https://atcoder.jp/contests/abc272/tasks/abc272_e | Problem Statement
You are given an integer sequence
A=(A_1,A_2,\ldots,A_N)
of length
N
.
Perform the following operation
M
times:
For each
i\ (1\leq i \leq N)
, add
i
to
A_i
. Then, find the minimum non-negative integer not contained in
A
. | [
{
"input": "3 3\n-1 -1 -6\n",
"output": "2\n2\n0\n"
},
{
"input": "5 6\n-2 -2 -5 -7 -15\n",
"output": "1\n3\n2\n0\n0\n0\n"
}
] |
https://atcoder.jp/contests/abc272/tasks/abc272_f | Problem Statement
You are given strings
S
and
T
of length
N
each, consisting of lowercase English letters.
For a string
X
and an integer
i
, let
f(X,i)
be the string obtained by performing on
X
the following operation
i
times:
Remove the first character of
X
and append the same character to the end of
X
.
Find the number of integer pairs
(i,j)
satisfying
0 \le i,j \le N-1
such that
f(S,i)
is lexicographically smaller than or equal to
f(T,j)
.
What is the lexicographical order?
Simply put, the lexicographical order is the order in which the words are arranged in a dictionary. Let us define it formally by describing an algorithm of finding the ordering of two distinct strings
S
and
T
consisting of lowercase English letters.
Here, we denote "the
i
-th character of a string
S
" by
S_i
. Also, we write
S \lt T
and
S \gt T
if
S
is lexicographically smaller and larger than
T
, respectively.
Let
L
be the smallest of the lengths of
S
and
T
. For
i=1,2,\dots,L
, we check if
S_i
equals
T_i
.
If there is an
i
such that
S_i \neq T_i
, let
j
be the smallest such
i
. Comparing
S_j
and
T_j
, we terminate the algorithm by determining that
S \lt T
if
S_j
is smaller than
T_j
in the alphabetical order, and that
S \gt T
otherwise.
If there is no
i
such that
S_i \neq T_i
, comparing the lengths of
S
and
T
, we terminate the algorithm by determining that
S \lt T
if
S
is shorter than
T
, and that
S \gt T
otherwise. | [
{
"input": "3\nadb\ncab\n",
"output": "4\n"
},
{
"input": "10\nwsiuhwijsl\npwqoketvun\n",
"output": "56\n"
}
] |
https://atcoder.jp/contests/abc272/tasks/abc272_g | Problem Statement
You are given a sequence
A=(A_1,A_2,\dots,A_N)
of length
N
consisting of positive integers, where the elements of
A
are distinct.
You will choose a positive integer
M
between
3
and
10^9
(inclusive) to perform the following operation once:
For each integer
i
such that
1 \le i \le N
, replace
A_i
with
A_i \bmod M
.
Can you choose an
M
so that
A
satisfies the following condition after the operation? If you can, find such an
M
.
There exists an integer
x
such that
x
is the majority in
A
.
Here, an integer
x
is said to be the majority in
A
if the number of integers
i
such that
A_i = x
is greater than the number of integers
i
such that
A_i \neq x
. | [
{
"input": "5\n3 17 8 14 10\n",
"output": "7\n"
},
{
"input": "10\n822848257 553915718 220834133 692082894 567771297 176423255 25919724 849988238 85134228 235637759\n",
"output": "37\n"
},
{
"input": "10\n1 2 3 4 5 6 7 8 9 10\n",
"output": "-1\n"
}
] |
https://atcoder.jp/contests/abc272/tasks/abc272_h | Problem Statement
N
coins numbered
0,1,\ldots,N-1
are arranged in a row. Initially, all coins are face up. Also, you are given a sequence
A
of length
N
consisting of integers between
0
and
N-1
.
Snuke will choose a permutation
p=(p_1,p_2,\ldots,p_N)
of
(1,\ldots,N)
at equal probability and perform
N
operations. In the
i
-th
(1\leq i \leq N)
operation,
he flips
(A_{p_i}+1)
coins: coin
(i-1) \bmod N
, coin
(i-1+1 ) \bmod N
,
\ldots
, and coin
(i -1+ A_{p_i}) \bmod N
.
After the
N
operations, Snuke receives
k
yen (the currency in Japan) from his mother, where
k
is the number of face-up coins.
Find the expected value, modulo
998244353
, of the money Snuke will receive.
Definition of expected value modulo
998244353
In this problem, we can prove that the sought expected value is always a rational number.
Moreover, under the Constraints of this problem, when the sought expected value is represented as an irreducible fraction
\frac{y}{x}
, it is guaranteed that
x
is indivisible by
998244353
.
Then, an integer
z
between
0
and
998244352
such that
xz \equiv y \pmod{998244353}
is uniquely determined. Find such
z
. | [
{
"input": "2\n0 1\n",
"output": "1\n"
},
{
"input": "4\n3 1 1 2\n",
"output": "665496237\n"
}
] |
https://atcoder.jp/contests/arc149/tasks/arc149_a | Problem Statement
You are given positive integers
N
and
M
. Find the maximum positive integer
X
that satisfies all of the following conditions.
X
is a positive integer less than
10^N
, and all digits in the decimal representation of
X
are the same.
X
is a multiple of
M
.
If no positive integer
X
satisfies the conditions, print
-1
. | [
{
"input": "7 12\n",
"output": "888888\n"
},
{
"input": "9 12\n",
"output": "888888888\n"
},
{
"input": "1 3\n",
"output": "9\n"
},
{
"input": "1000 25\n",
"output": "-1\n"
},
{
"input": "30 1\n",
"output": "999999999999999999999999999999\n"
}
] |
https://atcoder.jp/contests/arc149/tasks/arc149_b | Problem Statement
For a sequence
P = (P_1, \ldots, P_N)
, let
\mathrm{LIS}(P)
denote the length of a longest increasing subsequence.
You are given permutations
A = (A_1, \ldots, A_N)
and
B = (B_1, \ldots, B_N)
of integers from
1
through
N
. You may perform the following operation on these sequences any number of times (possibly zero).
Choose an integer
i
such that
1\leq i\leq N-1
. Swap
A_i
and
A_{i+1}
, and swap
B_i
and
B_{i+1}
.
Find the maximum possible final value of
\mathrm{LIS}(A) + \mathrm{LIS}(B)
.
What is a longest increasing subsequence?
A subsequence of a sequence is a sequence obtained by removing zero or more elements from the original sequence and then concatenating the remaining elements without changing the order.
For instance,
(10,30)
is a subsequence of
(10,20,30)
, but
(20,10)
is not a subsequence of
(10,20,30)
.
A longest increasing subsequence of a sequence is a subsequence of that sequence with the greatest length among its subsequences that are strictly increasing. | [
{
"input": "5\n5 2 1 4 3\n3 1 2 5 4\n",
"output": "8\n"
},
{
"input": "5\n1 2 3 4 5\n1 2 3 4 5\n",
"output": "10\n"
}
] |
https://atcoder.jp/contests/arc149/tasks/arc149_c | Problem Statement
You are given a positive integer
N
.
Fill each square of a grid with
N
rows and
N
columns by writing a positive integer not greater than
N^2
so that all of the following conditions are satisfied.
Two positive integers written in horizontally or vertically adjacent squares never sum to a prime number.
Every positive integer not greater than
N^2
is written in one of the squares.
Under the Constraints of this problem, it can be proved that such a way to fill the grid always exists. | [
{
"input": "4\n",
"output": "15 11 16 12\n13 3 6 9\n14 7 8 1\n4 2 10 5\n"
}
] |
https://atcoder.jp/contests/arc149/tasks/arc149_d | Problem Statement
There are
N
pieces placed at integer coordinates on a number line. The coordinate of the
i
-th piece is
X_i
.
Let us move these pieces
M
times as follows.
In the
i
-th move, given a positive integer
D_i
, we move each piece as follows.
A piece whose coordinate is a negative integer is moved a distance of
D_i
in the positive direction.
A piece whose coordinate is
0
is not moved.
A piece whose coordinate is a positive integer is moved a distance of
D_i
in the negative direction.
Determine whether each piece arrives at the origin. If it does, print the number of moves after which it arrives there for the first time. Otherwise, print its coordinate after the
M
moves. | [
{
"input": "6 4\n2 4 6 8 10 12\n8 2 5 7\n",
"output": "No -6\nNo -4\nYes 2\nYes 1\nYes 2\nNo 4\n"
}
] |
https://atcoder.jp/contests/arc149/tasks/arc149_e | Problem Statement
You are given positive integers
N
,
M
, and
K
. Consider the following operation on a sequence of positive integers
A = (A_0, \ldots, A_{N-1})
.
Do the following for
k=0, 1, \ldots, K-1
in this order.
Sort
A_{k\bmod N}, A_{(k+1)\bmod N}, \ldots, A_{(k+M-1)\bmod N}
in ascending order. That is, replace
A_{(k+j)\bmod N}
with
x_j
for each
0\leq j < M
, where
(x_0, \ldots, x_{M-1})
is the result of sorting
A_{k\bmod N}, A_{(k+1)\bmod N}, \ldots, A_{(k+M-1)\bmod N}
in ascending order.
You are given a permutation
B = (B_0, \ldots, B_{N-1})
of the integers from
1
through
N
. Find the number of sequences
A
of positive integers that will equal
B
after performing the operation above, modulo
998244353
. | [
{
"input": "6 3 5\n6 4 2 3 1 5\n",
"output": "18\n"
},
{
"input": "6 3 5\n6 5 4 3 2 1\n",
"output": "0\n"
},
{
"input": "20 20 149\n13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 11 12\n",
"output": "401576539\n"
}
] |
https://atcoder.jp/contests/arc149/tasks/arc149_f | Problem Statement
Let
r > 1
be a rational number, and
p
and
q
be the numerator and denominator of
r
, respectively. That is,
p
and
q
are positive integers such that
r = \frac{p}{q}
and
\gcd(p,q) = 1
.
Let the
base-
\boldsymbol{r}
expansion
of a positive integer
n
be the integer sequence
(a_1, \ldots, a_k)
that satisfies all of the following conditions.
a_i
is an integer between
0
and
p-1
.
a_1\neq 0
.
n = \sum_{i=1}^k a_ir^{k-i}
.
It can be proved that any positive integer
n
has a unique base-
r
expansion.
You are given positive integers
p
,
q
,
N
,
L
, and
R
. Here,
p
and
q
satisfy
1.01 \leq \frac{p}{q}
.
Consider sorting all positive integers not greater than
N
in ascending lexicographical order of their base-
\frac{p}{q}
expansions. Find the
L
-th,
(L+1)
-th,
\ldots
,
R
-th positive integers in this list.
As usual, decimal notations are used in the input and output of positive integers.
What is lexicographical order on sequences?
A sequence
A = (A_1, \ldots, A_{|A|})
is said to be
lexicographically smaller
than
B = (B_1, \ldots, B_{|B|})
when 1. or 2. below is satisfied. Here,
|A|
and
|B|
denote the lengths of
A
and
B
, respectively.
|A|<|B|
and
(A_{1},\ldots,A_{|A|}) = (B_1,\ldots,B_{|A|})
.
There is an integer
1\leq i\leq \min\{|A|,|B|\}
that satisfies both of the following.
(A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1})
.
A_i < B_i
. | [
{
"input": "3 1 9 1 9\n",
"output": "1\n3\n9\n4\n5\n2\n6\n7\n8\n"
},
{
"input": "3 2 9 1 9\n",
"output": "1\n2\n3\n4\n6\n9\n7\n8\n5\n"
},
{
"input": "3 2 9 3 8\n",
"output": "3\n4\n6\n9\n7\n8\n"
},
{
"input": "10 9 1000000000000000000 123456789123456789 123456789123456799\n",
"output": "806324968563249278\n806324968563249279\n725692471706924344\n725692471706924345\n725692471706924346\n725692471706924347\n725692471706924348\n725692471706924349\n653123224536231907\n653123224536231908\n653123224536231909\n"
}
] |
https://atcoder.jp/contests/abc271/tasks/abc271_a | Problem Statement
In the hexadecimal system, where the digits
ABCDEF
corresponding to
10,11,12,13,14
, and
15
are used in addition to
0123456789
, every integer between
0
and
255
is represented as a
1
- or
2
-digit numeral.
For example,
0
and
12
are represented as
1
-digit hexadecimal numerals
0
and
C
;
99
and
255
are represented as
2
-digit hexadecimals
63
and
FF
.
Given an integer
N
between
0
and
255
, convert it to an exactly two-digit hexadecimal numeral, prepending leading
0
s if necessary. | [
{
"input": "99\n",
"output": "63\n"
},
{
"input": "12\n",
"output": "0C\n"
},
{
"input": "0\n",
"output": "00\n"
},
{
"input": "255\n",
"output": "FF\n"
}
] |
https://atcoder.jp/contests/abc271/tasks/abc271_b | Problem Statement
There are
N
sequences of integers.
The
i
-th
(1 \leq i \leq N)
sequence has
L_i
terms; the
j
-th
(1 \leq j \leq L_i)
term of the
i
-th sequence is
a_{i, j}
.
You are given
Q
queries. For the
k
-th
(1 \leq k \leq Q)
query, given integers
s_k
and
t_k
, find the
t_k
-th term of the
s_k
-th sequence. | [
{
"input": "2 2\n3 1 4 7\n2 5 9\n1 3\n2 1\n",
"output": "7\n5\n"
},
{
"input": "3 4\n4 128 741 239 901\n2 1 1\n3 314 159 26535\n1 1\n2 2\n3 3\n1 4\n",
"output": "128\n1\n26535\n901\n"
}
] |
https://atcoder.jp/contests/abc271/tasks/abc271_c | Problem Statement
Takahashi is going to read a manga series "Snuke-kun" in
10^9
volumes.
Initially, Takahashi has
N
books of this series. The
i
-th book is Volume
a_i
.
Takahashi may repeat the following operation any number of (possibly zero) times
only before he begins to read
:
Do nothing if he has
1
or less books; otherwise, sell two of the books he has and buy one book of any volume instead.
Then, Takahashi reads Volume
1
, Volume
2
, Volume
3
,
\ldots
, in order. However, when he does not have a book of the next volume to read, he stops reading the series (regardless of the other volumes he has).
Find the latest volume of the series that he can read up to. If he cannot read any, let the answer be
0
. | [
{
"input": "6\n1 2 4 6 7 271\n",
"output": "4\n"
},
{
"input": "10\n1 1 1 1 1 1 1 1 1 1\n",
"output": "5\n"
},
{
"input": "1\n5\n",
"output": "0\n"
}
] |
https://atcoder.jp/contests/abc271/tasks/abc271_d | Problem Statement
There are
N
cards with an integer written on each side. Card
i
(1 \leq i \leq N)
has an integer
a_i
written on the front and an integer
b_i
written on the back.
You may choose whether to place each card with its front or back side visible.
Determine if you can place the cards so that the sum of the visible integers exactly equals
S
. If possible, find a placement of the cards to realize it. | [
{
"input": "3 11\n1 4\n2 3\n5 7\n",
"output": "Yes\nTHH\n"
},
{
"input": "5 25\n2 8\n9 3\n4 11\n5 1\n12 6\n",
"output": "No\n"
}
] |
https://atcoder.jp/contests/abc271/tasks/abc271_e | Problem Statement
There are
N
towns numbered
1, \dots, N
, and
M
roads numbered
1, \dots, M
.
Every road is directed; road
i
(1 \leq i \leq M)
leads you from Town
A_i
to Town
B_i
. The length of road
i
is
C_i
.
You are given a sequence
E = (E_1, \dots, E_K)
of length
K
consisting of integers between
1
and
M
. A way of traveling from town
1
to town
N
using roads is called a
good path
if:
the sequence of the roads' numbers arranged in the order used in the path is a subsequence of
E
.
Note that a subsequence of a sequence is a sequence obtained by removing
0
or more elements from the original sequence and concatenating the remaining elements without changing the order.
Find the minimum sum of the lengths of the roads used in a good path.
If there is no good path, report that fact. | [
{
"input": "3 4 4\n1 2 2\n2 3 2\n1 3 3\n1 3 5\n4 2 1 2\n",
"output": "4\n"
},
{
"input": "3 2 3\n1 2 1\n2 3 1\n2 1 1\n",
"output": "-1\n"
},
{
"input": "4 4 5\n3 2 2\n1 3 5\n2 4 7\n3 4 10\n2 4 1 4 3\n",
"output": "14\n"
}
] |
https://atcoder.jp/contests/abc271/tasks/abc271_f | Problem Statement
There is a grid with
N
rows and
N
columns. We denote by
(i, j)
the square at the
i
-th
(1 \leq i \leq N)
row from the top and
j
-th
(1 \leq j \leq N)
column from the left.
Square
(i, j)
has a non-negative integer
a_{i, j}
written on it.
When you are at square
(i, j)
, you can move to either square
(i+1, j)
or
(i, j+1)
. Here, you are not allowed to go outside the grid.
Find the number of ways to travel from square
(1, 1)
to square
(N, N)
such that the exclusive logical sum of the integers written on the squares visited (including
(1, 1)
and
(N, N)
) is
0
.
What is the exclusive logical sum?
The exclusive logical sum
a \oplus b
of two integers
a
and
b
is defined as follows.
The
2^k
's place (
k \geq 0
) in the binary notation of
a \oplus b
is
1
if exactly one of the
2^k
's places in the binary notation of
a
and
b
is
1
; otherwise, it is
0
.
For example,
3 \oplus 5 = 6
(In binary notation:
011 \oplus 101 = 110
).
In general, the exclusive logical sum of
k
integers
p_1, \dots, p_k
is defined as
(\cdots ((p_1 \oplus p_2) \oplus p_3) \oplus \cdots \oplus p_k)
. We can prove that it is independent of the order of
p_1, \dots, p_k
. | [
{
"input": "3\n1 5 2\n7 0 5\n4 2 3\n",
"output": "2\n"
},
{
"input": "2\n1 2\n2 1\n",
"output": "0\n"
},
{
"input": "10\n1 0 1 0 0 1 0 0 0 1\n0 0 0 1 0 1 0 1 1 0\n1 0 0 0 1 0 1 0 0 0\n0 1 0 0 0 1 1 0 0 1\n0 0 1 1 0 1 1 0 1 0\n1 0 0 0 1 0 0 1 1 0\n1 1 1 0 0 0 1 1 0 0\n0 1 1 0 0 1 1 0 1 0\n1 0 1 1 0 0 0 0 0 0\n1 0 1 1 0 0 1 1 1 0\n",
"output": "24307\n"
}
] |
https://atcoder.jp/contests/abc271/tasks/abc271_g | Problem Statement
Takahashi has decided to put a web counter on his webpage.
The accesses to his webpage are described as follows:
For
i=0,1,2,\ldots,23
, there is a possible access at
i
o'clock every day:
If
c_i=
T
, Takahashi accesses the webpage with a probability of
X
percent.
If
c_i=
A
, Aoki accesses the webpage with a probability of
Y
percent.
Whether or not Takahashi or Aoki accesses the webpage is determined independently every time.
There is no other access.
Also, Takahashi believes it is preferable that the
N
-th access since the counter is put is not made by Takahashi himself.
If Takahashi puts the counter
right before
0
o'clock
of one day, find the probability, modulo
998244353
, that the
N
-th access is made by Aoki. | [
{
"input": "1 50 50\nATATATATATATATATATATATAT\n",
"output": "665496236\n"
},
{
"input": "271 95 1\nTTTTTTTTTTTTTTTTTTTTTTTT\n",
"output": "0\n"
},
{
"input": "10000000000000000 62 20\nATAATTATATTTAAAATATTATAT\n",
"output": "744124544\n"
}
] |
https://atcoder.jp/contests/abc271/tasks/abc271_h | Problem Statement
Solve the following problem for
T
test cases.
A piece is placed at the origin
(0, 0)
on an
xy
-plane. You may perform the following operation any number of (possibly zero) times:
Choose an integer
i
such that
1 \leq i \leq 8
and
s_i=
1
. Let
(x, y)
be the current coordinates where the piece is placed.
If
i=1
, move the piece to
(x+1,y)
.
If
i=2
, move the piece to
(x+1,y+1)
.
If
i=3
, move the piece to
(x,y+1)
.
If
i=4
, move the piece to
(x-1,y+1)
.
If
i=5
, move the piece to
(x-1,y)
.
If
i=6
, move the piece to
(x-1,y-1)
.
If
i=7
, move the piece to
(x,y-1)
.
If
i=8
, move the piece to
(x+1,y-1)
.
Your objective is to move the piece to
(A, B)
.
Find the minimum number of operations needed to achieve the objective. If it is impossible, print
-1
instead. | [
{
"input": "7\n5 3 10101010\n5 3 01010101\n5 3 11111111\n5 3 00000000\n0 0 11111111\n0 1 10001111\n-1000000000 1000000000 10010011\n",
"output": "8\n5\n5\n-1\n0\n-1\n1000000000\n"
}
] |
https://atcoder.jp/contests/abc270/tasks/abc270_a | Problem Statement
There was an exam consisting of three problems worth
1
,
2
, and
4
points.
Takahashi, Aoki, and Snuke took this exam.
Takahashi scored
A
points, and Aoki scored
B
points.
Snuke solved all of the problems solved by at least one of Takahashi and Aoki, and failed to solve any of the problems solved by neither of them.
Find Snuke's score.
It can be proved that Snuke's score is uniquely determined under the Constraints of this problem. | [
{
"input": "1 2\n",
"output": "3\n"
},
{
"input": "5 3\n",
"output": "7\n"
},
{
"input": "0 0\n",
"output": "0\n"
}
] |
https://atcoder.jp/contests/abc270/tasks/abc270_b | Problem Statement
Takahashi is at the origin of a number line. He wants to reach a goal at coordinate
X
.
There is a wall at coordinate
Y
, which Takahashi cannot go beyond at first.
However, after picking up a hammer at coordinate
Z
, he can destroy that wall and pass through.
Determine whether Takahashi can reach the goal. If he can, find the minimum total distance he needs to travel to do so. | [
{
"input": "10 -10 1\n",
"output": "10\n"
},
{
"input": "20 10 -10\n",
"output": "40\n"
},
{
"input": "100 1 1000\n",
"output": "-1\n"
}
] |
https://atcoder.jp/contests/abc270/tasks/abc270_c | Problem Statement
There is a tree
T
with
N
vertices. The
i
-th edge
(1\leq i\leq N-1)
connects vertex
U_i
and vertex
V_i
.
You are given two different vertices
X
and
Y
in
T
.
List all vertices along the simple path from vertex
X
to vertex
Y
in order, including endpoints.
It can be proved that, for any two different vertices
a
and
b
in a tree, there is a unique simple path from
a
to
b
.
What is a simple path?
For vertices
X
and
Y
in a graph
G
, a
path
from vertex
X
to vertex
Y
is a sequence of vertices
v_1,v_2, \ldots, v_k
such that
v_1=X
,
v_k=Y
, and
v_i
and
v_{i+1}
are connected by an edge for each
1\leq i\leq k-1
.
Additionally, if all of
v_1,v_2, \ldots, v_k
are distinct, the path is said to be a
simple path
from vertex
X
to vertex
Y
. | [
{
"input": "5 2 5\n1 2\n1 3\n3 4\n3 5\n",
"output": "2 1 3 5\n"
},
{
"input": "6 1 2\n3 1\n2 5\n1 2\n4 1\n2 6\n",
"output": "1 2\n"
}
] |
https://atcoder.jp/contests/abc270/tasks/abc270_d | Problem Statement
Takahashi and Aoki will play a game of taking stones using a sequence
(A_1, \ldots, A_K)
.
There is a pile that initially contains
N
stones. The two players will alternately perform the following operation, with Takahashi going first.
Choose an
A_i
that is at most the current number of stones in the pile. Remove
A_i
stones from the pile.
The game ends when the pile has no stones.
If both players attempt to maximize the total number of stones they remove before the end of the game, how many stones will Takahashi remove? | [
{
"input": "10 2\n1 4\n",
"output": "5\n"
},
{
"input": "11 4\n1 2 3 6\n",
"output": "8\n"
},
{
"input": "10000 10\n1 2 4 8 16 32 64 128 256 512\n",
"output": "5136\n"
}
] |
https://atcoder.jp/contests/abc270/tasks/abc270_e | Problem Statement
There are
N
baskets numbered
1, 2, \ldots, N
arranged in a circle.
For each
1\leq i \leq N-1
, basket
i+1
is to the immediate right of basket
i
, and basket
1
is to the immediate right of basket
N
.
Basket
i
now contains
A_i
apples.
Takahashi starts in front of basket
1
and repeats the following action.
If the basket he is facing contains an apple, take one and eat it. Then, regardless of whether he has eaten an apple now, go on to the next basket to the immediate right.
Find the number of apples remaining in each basket when Takahashi has eaten exactly
K
apples in total. | [
{
"input": "3 3\n1 3 0\n",
"output": "0 1 0 \n"
},
{
"input": "2 1000000000000\n1000000000000 1000000000000\n",
"output": "500000000000 500000000000 \n"
}
] |
https://atcoder.jp/contests/abc270/tasks/abc270_h | Problem Statement
You are given a tuple of
N
non-negative integers
A=(A_1,A_2,\ldots,A_N)
such that
A_1=0
and
A_N>0
.
Takahashi has
N
counters. Initially, the values of all counters are
0
.
He will repeat the following operation until, for every
1\leq i\leq N
, the value of the
i
-th counter is at least
A_i
.
Choose one of the
N
counters uniformly at random and set its value to
0
. (Each choice is independent of others.)
Increase the values of the
other
counters by
1
.
Print the expected value of the number of times Takahashi repeats the operation, modulo
998244353
(see Notes). | [
{
"input": "2\n0 2\n",
"output": "6\n"
},
{
"input": "5\n0 1 3 10 1000000000000000000\n",
"output": "874839568\n"
}
] |
https://atcoder.jp/contests/abc269/tasks/abc269_a | Problem Statement
You are given integers
a
,
b
,
c
, and
d
. Print two lines as follows.
The first line should contain the result of calculating
(a + b) \times (c - d)
as an integer.
The second line should contain
Takahashi
, regardless of the input. | [
{
"input": "1 2 5 3\n",
"output": "6\nTakahashi\n"
},
{
"input": "10 -20 30 -40\n",
"output": "-700\nTakahashi\n"
},
{
"input": "100 100 100 -100\n",
"output": "40000\nTakahashi\n"
}
] |
https://atcoder.jp/contests/abc269/tasks/abc269_b | Problem Statement
Takahashi generated
10
strings
S_1,S_2,\dots,S_{10}
as follows.
First, let
S_i (1 \le i \le 10)=
..........
(
10
.
s in a row).
Next, choose four integers
A
,
B
,
C
, and
D
satisfying all of the following.
1 \le A \le B \le 10
.
1 \le C \le D \le 10
.
Then, for every pair of integers
(i,j)
satisfying all of the following, replace the
j
-th character of
S_i
with
#
.
A \le i \le B
.
C \le j \le D
.
You are given
S_1,S_2,\dots,S_{10}
generated as above. Find the integers
A
,
B
,
C
, and
D
Takahashi chose.
It can be proved that such integers
A
,
B
,
C
, and
D
uniquely exist (there is just one answer) under the Constraints. | [
{
"input": "..........\n..........\n..........\n..........\n...######.\n...######.\n...######.\n...######.\n..........\n..........\n",
"output": "5 8\n4 9\n"
},
{
"input": "..........\n..#.......\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n",
"output": "2 2\n3 3\n"
},
{
"input": "##########\n##########\n##########\n##########\n##########\n##########\n##########\n##########\n##########\n##########\n",
"output": "1 10\n1 10\n"
}
] |
https://atcoder.jp/contests/abc269/tasks/abc269_c | Problem Statement
You are given a non-negative integer
N
. Print all non-negative integers
x
that satisfy the following condition in ascending order.
The set of the digit positions containing
1
in the binary representation of
x
is a subset of the set of the digit positions containing
1
in the binary representation of
N
.
That is, the following holds for every non-negative integer
k
: if the digit in the "
2^k
s" place of
x
is
1
, the digit in the
2^k
s place of
N
is
1
. | [
{
"input": "11\n",
"output": "0\n1\n2\n3\n8\n9\n10\n11\n"
},
{
"input": "0\n",
"output": "0\n"
},
{
"input": "576461302059761664\n",
"output": "0\n524288\n549755813888\n549756338176\n576460752303423488\n576460752303947776\n576461302059237376\n576461302059761664\n"
}
] |
https://atcoder.jp/contests/abc269/tasks/abc269_d | Problem Statement
We have an infinite hexagonal grid shown below. Initially, all squares are white.
A hexagonal cell is represented as
(i,j)
with two integers
i
and
j
.
Cell
(i,j)
is adjacent to the following six cells:
(i-1,j-1)
(i-1,j)
(i,j-1)
(i,j+1)
(i+1,j)
(i+1,j+1)
Takahashi has painted
N
cells
(X_1,Y_1),(X_2,Y_2),\dots,(X_N,Y_N)
black.
Find the number of connected components formed by the black cells.
Two black cells belong to the same connected component when one can travel between those two black cells by repeatedly moving to an adjacent black cell. | [
{
"input": "6\n-1 -1\n0 1\n0 2\n1 0\n1 2\n2 0\n",
"output": "3\n"
},
{
"input": "4\n5 0\n4 1\n-3 -4\n-2 -5\n",
"output": "4\n"
},
{
"input": "5\n2 1\n2 -1\n1 0\n3 1\n1 -1\n",
"output": "1\n"
}
] |
https://atcoder.jp/contests/abc269/tasks/abc269_e | Problem Statement
This is an interactive task
(where your program interacts with the judge's program via input and output).
We have an
N
-by-
N
chessboard and
N
rooks. Below, the square at the
i
-th row from the top and
j
-th column from the left is denoted by
(i, j)
.
Consider placing the rooks on squares of the chessboard. Here, you have to place the rooks so that all of the following conditions are satisfied.
No row contains two or more rooks.
No column contains two or more rooks.
Now,
N-1
rooks are placed on the chessboard so that all of the above conditions are satisfied. You will choose a square that is not occupied by a rook and place a rook on that square. (It can be proved that there is at least one square on which a rook can be placed under the conditions.)
However, you cannot directly see which squares of the chessboard are occupied by a rook.
Instead, you may ask at most
20
questions to the judge in the following manner.
You choose integers
A
,
B
,
C
, and
D
such that
1 \leq A \leq B \leq N, 1 \leq C \leq D \leq N
, and ask the number of rooks in the rectangular region formed by the squares
(i, j)
such that
A \leq i \leq B, C \leq j \leq D
.
Find a square to place a rook. | [] |
https://atcoder.jp/contests/abc269/tasks/abc269_f | Problem Statement
We have a grid with
N
rows and
M
columns. The square
(i,j)
at the
i
-th row from the top and
j
-th column from the left has an integer
(i-1) \times M + j
written on it.
Let us perform the following operation on this grid.
For every square
(i,j)
such that
i+j
is odd, replace the integer on that square with
0
.
Answer
Q
questions on the grid after the operation.
The
i
-th question is as follows:
Find the sum of the integers written on all squares
(p,q)
that satisfy all of the following conditions, modulo
998244353
.
A_i \le p \le B_i
.
C_i \le q \le D_i
. | [
{
"input": "5 4\n6\n1 3 2 4\n1 5 1 1\n5 5 1 4\n4 4 2 2\n5 5 4 4\n1 5 1 4\n",
"output": "28\n27\n36\n14\n0\n104\n"
},
{
"input": "1000000000 1000000000\n3\n1000000000 1000000000 1000000000 1000000000\n165997482 306594988 719483261 992306147\n1 1000000000 1 1000000000\n",
"output": "716070898\n240994972\n536839100\n"
},
{
"input": "999999999 999999999\n3\n999999999 999999999 999999999 999999999\n216499784 840031647 84657913 415448790\n1 999999999 1 999999999\n",
"output": "712559605\n648737448\n540261130\n"
}
] |
https://atcoder.jp/contests/abc269/tasks/abc269_g | Problem Statement
We have
N
cards numbered
1
to
N
.
Card
i
has an integer
A_i
written on the front and an integer
B_i
written on the back. Here,
\sum_{i=1}^N (A_i + B_i) = M
.
For each
k=0,1,2,...,M
, solve the following problem.
The
N
cards are arranged so that their front sides are visible. You may choose between
0
and
N
cards (inclusive) and flip them.
To make the sum of the visible numbers equal to
k
, at least how many cards must be flipped? Print this number of cards.
If there is no way to flip cards to make the sum of the visible numbers equal to
k
, print
-1
instead. | [
{
"input": "3 6\n0 2\n1 0\n0 3\n",
"output": "1\n0\n2\n1\n1\n3\n2\n"
},
{
"input": "2 3\n1 1\n0 1\n",
"output": "-1\n0\n1\n-1\n"
},
{
"input": "5 12\n0 1\n0 3\n1 0\n0 5\n0 2\n",
"output": "1\n0\n1\n1\n1\n2\n1\n2\n2\n2\n3\n3\n4\n"
}
] |
https://atcoder.jp/contests/abc269/tasks/abc269_h | Problem Statement
We have a rooted tree
T
with
N
vertices numbered
1
to
N
. Vertex
1
is the root, and the parent of vertex
i
(2 \leq i \leq N)
is vertex
P_i
.
A non-empty subset
S
of the vertex set
V = \lbrace 1, 2,\dots, N\rbrace
of
T
is said to be a
good vertex set
when it satisfies the following condition.
For every pair of different vertices
(u, v)
in
S
, the following holds:
u
is not an ancestor of
v
.
For each
K = 1, 2, \dots, N
, find the number, modulo
998244353
, of good vertex sets with exactly
K
vertices. | [
{
"input": "4\n1 2 1\n",
"output": "4\n2\n0\n0\n"
},
{
"input": "6\n1 1 2 2 5\n",
"output": "6\n6\n2\n0\n0\n0\n"
},
{
"input": "6\n1 1 1 1 1\n",
"output": "6\n10\n10\n5\n1\n0\n"
},
{
"input": "10\n1 2 1 2 1 1 2 6 9\n",
"output": "10\n30\n47\n38\n16\n3\n0\n0\n0\n0\n"
}
] |
https://atcoder.jp/contests/ahc014/tasks/ahc014_a | Problem Statement
RectJoin is the following single-player game played with square grid paper and pencil.
Let
(0, 0)
be the coordinates of the lower left corner of the grid paper, with the
x
-axis to the right and the
y
-axis to the top.
The coordinates of the upper right corner of the grid paper are
(N-1, N-1)
.
Initially,
M
dots are placed on grid points, and you can repeat the following operations as many times as possible to place dots and draw rectangles on the grid paper.
In the
i
-th operation, choose a grid point
p_{i,1}
not containing a dot and three grid points
p_{i,2}, p_{i,3}, p_{i,4}
containing dots, which satisfy all of the following three conditions.
Connecting
p_{i,1} p_{i,2} p_{i,3} p_{i,4}
in this order forms a rectangle that is parallel to the axis or inclined at 45 degrees.
There are no dots other than
p_{i,2}, p_{i,3}, p_{i,4}
on the perimeter of this rectangle.
The perimeter of this rectangle does not share a common segment of positive length with the perimeter of an already drawn rectangle (intersecting at some points is allowed).
For the chosen four points, place a new dot on
p_{i,1}
and draw the perimeter of the rectangle
p_{i,1} p_{i,2} p_{i,3} p_{i,4}
on the grid paper.
Let
(c,c)=((N-1)/2,(N-1)/2)
be the coordinates of the center of the graph paper.
We define the weight of each grid point as
w(x,y)=(x-c)^2 + (y-c)^2 + 1
using the distance from the center.
Let
S=\sum_{x=0}^{N-1}\sum_{y=0}^{N-1} w(x,y)
be the sum of the weights of all grid points.
Let
Q
be the set of coordinates with dots in the final state (including the initially placed dots).
Then you will get the following score.
\[\mathrm{round}\left(10^6 \cdot\frac{N^2}{M}\cdot\frac{\sum_{(x, y)\in Q} w(x, y)}{S}\right)\]
Create a program to play the game to get as high a score as possible.
Additional explanation of the rules
You must choose
p_{i,1}
from the interior of the grid paper, i.e., from coordinates satisfying
0\leq x,y\leq N-1
.
Since a dot is placed on the grid point chosen as
p_{i,1}
, it cannot be chosen again as
p_{j,1}
in
j(>i)
-th operation, but it can be chosen again as
p_{j,k} (k=2,3,4)
.
Similarly,
p_{i,k} (k=2,3,4)
can be repeatedly chosen as
p_{j,k'} (k'=2,3,4)
in
j(>i)
-th operation.
As stated in condition 2, there must be no other dots on the perimeter of the chosen rectangle, but conversely, you can choose a point on the perimeter of an already drawn rectangle as
p_{i,1}
and place a dot on it.
Number of test cases
Provisional test: 50
System test: 2000. We will publish
seeds.txt
(sha256=907b41fcba240515612a21798a10b0df7dda744b1268b74b3bbd41b93a73095e) after the contest is over.
System test contains 125 inputs for each of
N=31,33,35,\cdots,61
.
The input of seed=0 is manually created and is not included in the provisional or system test.
The score of a submission is the total scores for each test case.
In the provisional test, if your submission produces illegal output or exceeds the time limit for some test cases, the submission itself will be judged as
WA
or
TLE
, and the score of the submission will be zero.
The final ranking will be determined by the system test with more inputs which will be run after the contest is over.
In the system test, if your submission produces illegal output or exceeds the time limit for some test cases, only the score for those test cases will be zero.
The system test will be performed only for
the last submission which received a result other than
CE
.
Be careful not to make a mistake in the final submission.
About execution time
Execution time may vary slightly from run to run.
In addition, since system tests simultaneously perform a large number of executions, it has been observed that execution time increases by several percent compared to provisional tests.
For these reasons, submissions that are very close to the time limit may result in
TLE
in the system test.
Please measure the execution time in your program to terminate the process, or have enough margin in the execution time. | [
{
"input": "33 58\n13 24\n14 24\n15 24\n16 24\n17 24\n12 23\n18 23\n11 22\n19 22\n10 21\n20 21\n9 20\n21 20\n8 19\n15 19\n18 19\n22 19\n8 18\n12 18\n15 18\n18 18\n22 18\n8 17\n12 17\n15 17\n18 17\n22 17\n8 16\n12 16\n15 16\n18 16\n22 16\n8 15\n12 15\n15 15\n18 15\n22 15\n9 14\n12 14\n15 14\n18 14\n21 14\n10 13\n12 13\n15 13\n18 13\n20 13\n22 13\n11 12\n12 12\n15 12\n18 12\n19 12\n23 12\n12 11\n15 11\n18 11\n24 11\n",
"output": "20\n9 15 12 12 15 15 12 18\n15 20 12 17 15 14 18 17\n23 22 19 22 19 12 23 12\n23 14 22 15 21 14 22 13\n10 14 10 13 12 13 12 14\n11 11 12 11 12 12 11 12\n18 20 15 20 15 19 18 19\n19 16 22 19 21 20 18 17\n12 19 12 18 15 18 15 19\n15 22 12 19 15 16 18 19\n14 22 15 22 15 24 14 24\n15 8 18 11 15 14 12 11\n10 15 9 15 9 14 10 14\n11 18 12 19 10 21 9 20\n22 23 20 21 21 20 23 22\n21 15 18 15 18 14 21 14\n15 26 13 24 15 22 17 24\n20 20 16 24 14 22 18 18\n21 17 18 20 15 17 18 14\n11 14 10 13 11 12 12 13\n"
}
] |
https://atcoder.jp/contests/joi2023yo1a/tasks/joi2023_yo1a_a | 問題文
整数
A, B
が与えられる.縦の辺の長さが
A\:\mathrm{cm}
,横の辺の長さが
B\:\mathrm{cm}
である下図のような長方形の面積は何
\mathrm{cm}^2
か求めよ. | [
{
"input": "2\n3\n",
"output": "6\n"
},
{
"input": "100\n1\n",
"output": "100\n"
},
{
"input": "4\n4\n",
"output": "16\n"
}
] |
https://atcoder.jp/contests/joi2023yo1a/tasks/joi2023_yo1a_b | 問題文
十進法で
2
桁の整数
N
が与えられる.
N
の十の位の数字と一の位の数字が同じである場合は
1
を,そうでない場合は
0
を出力せよ. | [
{
"input": "22\n",
"output": "1\n"
},
{
"input": "10\n",
"output": "0\n"
}
] |
https://atcoder.jp/contests/joi2023yo1a/tasks/joi2023_yo1a_c | 問題文
長さ
N
の文字列
S
が与えられる.
S
の各文字は
L
または
R
である.
1
つのボールと,ボールを入れることができる箱が
3
つある.箱には
1
,
2
,
3
と番号が付けられている.
最初,箱
1
にボールが入っていた.
ビーバーのビ太郎は,この状態から箱とボールに対して
N
回の操作を行った.
i
回目 (
1 \leqq i \leqq N
) の操作は,次のように行われた.
ボールが入っている箱を箱
x
とし,箱
x
からボールを取り出す.その後,文字列
S
の
i
文字目に従って,以下のいずれかを行う.
文字列
S
の
i
文字目が
L
の場合,箱
x-1
にボールを入れる.ただし,
x
が
1
である場合は箱
1
にボールを入れる.
文字列
S
の
i
文字目が
R
の場合,箱
x+1
にボールを入れる.ただし,
x
が
3
である場合は箱
3
にボールを入れる.
N
回の操作を通じて箱
3
にボールを入れた回数を出力せよ. | [
{
"input": "4\nLRRR\n",
"output": "2\n"
},
{
"input": "3\nLRL\n",
"output": "0\n"
},
{
"input": "7\nLRLRRRL\n",
"output": "2\n"
}
] |
https://atcoder.jp/contests/joi2023yo1a/tasks/joi2023_yo1a_d | 問題文
JOI 高校には
2N\,(=2 \times N)
人の生徒がおり,
1
から
2N
までの番号が付けられている.
来月 JOI 高校では運動会が開催され,その中の競技「二人三脚」では,
2N
人の生徒が
N
組の
2
人組に分かれレースを行う.
組には
1
から
N
までの番号が付けられており,生徒
i
(
1 \leqq i \leqq 2N
) は組
A_i
に属している.
あなたはどの生徒がどの組に属しているかの表を作ったが,どういうわけか,生徒
2N
がどの組に属しているか分からなくなってしまった.
A_1, A_2, \dots, A_{2N-1}
が与えられるので,生徒
2N
が属する組の番号
A_{2N}
を求めよ. | [
{
"input": "4\n1 4 2 1 3 4 3\n",
"output": "2\n"
},
{
"input": "10\n5 7 1 9 8 8 2 9 6 5 1 3 6 4 7 3 10 2 4\n",
"output": "10\n"
},
{
"input": "1\n1\n",
"output": "1\n"
}
] |
https://atcoder.jp/contests/arc148/tasks/arc148_a | Problem Statement
You are given a sequence
A = (A_1, A_2, ..., A_N)
.
You may perform the following operation exactly once.
Choose an integer
M
at least
2
. Then, for every integer
i
(
1 \leq i \leq N
), replace
A_i
with the remainder when
A_i
is divided by
M
.
For instance, if
M = 4
is chosen when
A = (2, 7, 4)
,
A
becomes
(2 \bmod 4, 7 \bmod 4, 4 \bmod 4) = (2, 3, 0)
.
Find the minimum possible number of different elements in
A
after the operation. | [
{
"input": "3\n1 4 8\n",
"output": "2\n"
},
{
"input": "4\n5 10 15 20\n",
"output": "1\n"
},
{
"input": "10\n3785 5176 10740 7744 3999 3143 9028 2822 4748 6888\n",
"output": "1\n"
}
] |
https://atcoder.jp/contests/arc148/tasks/arc148_b | Problem Statement
For a string
T
of length
L
consisting of
d
and
p
, let
f(T)
be
T
rotated
180
degrees. More formally, let
f(T)
be the string that satisfies the following conditions.
f(T)
is a string of length
L
consisting of
d
and
p
.
For every integer
i
such that
1 \leq i \leq L
, the
i
-th character of
f(T)
differs from the
(L + 1 - i)
-th character of
T
.
For instance, if
T =
ddddd
,
f(T) =
ppppp
; if
T =
dpdppp
,
f(T)=
dddpdp
.
You are given a string
S
of length
N
consisting of
d
and
p
.
You may perform the following operation
zero or one
time.
Choose a pair of integers
(L, R)
such that
1 \leq L \leq R \leq N
, and let
T
be the substring formed by the
L
-th through
R
-th characters of
S
. Then, replace the
L
-th through
R
-th characters of
S
with
f(T)
.
For instance, if
S=
dpdpp
and
(L,R)=(2,4)
, we have
T=
pdp
and
f(T)=
dpd
, so
S
becomes
ddpdp
.
Print the lexicographically smallest string that
S
can become.
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 one of the following 1. and 2. holds.
Here,
|S|
and
|T|
denote 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
that satisfies the following two conditions:
S_1S_2\ldots S_{i-1} = T_1T_2\ldots T_{i-1}
.
S_i
is smaller than
T_i
in alphabetical order. | [
{
"input": "6\ndpdppd\n",
"output": "dddpdd\n"
},
{
"input": "3\nddd\n",
"output": "ddd\n"
},
{
"input": "11\nddpdpdppddp\n",
"output": "ddddpdpdddp\n"
}
] |
https://atcoder.jp/contests/arc148/tasks/arc148_c | Problem Statement
We have a rooted tree with
N
vertices numbered
1
to
N
. Vertex
1
is the root, and the parent of vertex
i
is vertex
P_i
.
There are
N
coins with heads and tails, one on each vertex.
Additionally, there are
N
buttons numbered
1
to
N
. Pressing button
n
flips all coins on the vertices in the subtree rooted at vertex
n
.
Process
Q
queries described below.
In the
i
-th query, you are given a vertex set of size
M_i
:
S_i = \lbrace v_{i,1}, v_{i,2},\dots, v_{i,M_i} \rbrace
.
Now, the coins on the vertices in
S_i
are facing heads-up, and the others are facing tails-up. In order to make all
N
coins face tails-up by repeatedly choosing a button and pressing it, at least how many button presses are needed? Print the answer, or
-1
if there is no way to make all the coins face tails-up. | [
{
"input": "6 6\n1 1 2 2 5\n6 1 2 3 4 5 6\n3 2 5 6\n1 3\n3 1 2 3\n3 4 5 6\n4 2 3 4 5\n",
"output": "1\n2\n1\n3\n2\n3\n"
}
] |
https://atcoder.jp/contests/arc148/tasks/arc148_d | Problem Statement
There are
2N
integers
A_1, A_2, ..., A_{2N}
written on a blackboard, and an integer
M
at least
2
.
Alice and Bob will play a game.
They will alternately perform the following operation, with Alice going first, until there is no number on the blackboard.
Choose a number and delete it from the blackboard.
At the end of the game, if the sum of numbers deleted by Alice modulo
M
equals the sum of numbers deleted by Bob modulo
M
, Bob wins; otherwise, Alice wins.
Which player will win if both plays optimally? | [
{
"input": "2 9\n1 4 8 5\n",
"output": "Alice\n"
},
{
"input": "3 998244353\n1 2 3 1 2 3\n",
"output": "Bob\n"
}
] |
https://atcoder.jp/contests/arc148/tasks/arc148_e | Problem Statement
You are given a sequence of length
N
,
A = (A_1, ..., A_N)
, and an integer
K
.
How many permutations of
A
are there such that no two adjacent elements sum to less than
K
? Find the count modulo
998244353
. | [
{
"input": "4 5\n1 2 3 4\n",
"output": "4\n"
},
{
"input": "4 3\n1 2 3 3\n",
"output": "12\n"
},
{
"input": "10 7\n3 1 4 1 5 9 2 6 5 3\n",
"output": "108\n"
}
] |
https://atcoder.jp/contests/arc148/tasks/arc148_f | Problem Statement
This problem is output-only.
We have a programming language equipped with the following operations of unsigned 64-bit integers: addition, multiplication, and a modulo operation where the divisor is
998244353
.
Write a program that performs multiplication modulo
1000000007
in this language.
More formally, write a program that receives integers
a
and
b
between
0
and
1000000006
and computes
a \times b \bmod{1000000007}
under the following
Specification
and
Format
. | [] |
https://atcoder.jp/contests/abc268/tasks/abc268_a | Problem Statement
Print how many distinct integers there are in given five integers
A, B, C, D
, and
E
. | [
{
"input": "31 9 24 31 24\n",
"output": "3\n"
},
{
"input": "0 0 0 0 0\n",
"output": "1\n"
}
] |
https://atcoder.jp/contests/abc268/tasks/abc268_b | Problem Statement
You are given two strings
S
and
T
consisting of lowercase English letters.
Determine if
S
is a prefix of
T
.
What is a prefix?
A prefix of a string
T_1T_2\ldots T_N
of length
N
is a string expressed as the first
i
characters of
T
,
T_1T_2\ldots T_i
, where
i
is an integer such that
0 \leq i \leq N
. For example, when
T =
abc, there are four prefixes of
T
: an empty string, a, ab, and abc. | [
{
"input": "atco\natcoder\n",
"output": "Yes\n"
},
{
"input": "code\natcoder\n",
"output": "No\n"
},
{
"input": "abc\nabc\n",
"output": "Yes\n"
},
{
"input": "aaaa\naa\n",
"output": "No\n"
}
] |
https://atcoder.jp/contests/abc268/tasks/abc268_c | Problem Statement
Person
0
, Person
1
,
\ldots
, and Person
(N-1)
are sitting around a turntable in their counterclockwise order, evenly spaced. Dish
p_i
is in front of Person
i
on the table.
You may perform the following operation
0
or more times:
Rotate the turntable by one
N
-th of a counterclockwise turn. As a result, the dish that was in front of Person
i
right before the rotation is now in front of Person
(i+1) \bmod N
.
When you are finished, Person
i
is happy if Dish
i
is in front of Person
(i-1) \bmod N
, Person
i
, or Person
(i+1) \bmod N
.
Find the maximum possible number of happy people.
What is
a \bmod m
?
For an integer
a
and a positive integer
m
,
a \bmod m
denotes the integer
x
between
0
and
(m-1)
(inclusive) such that
(a-x)
is a multiple of
m
. (It can be proved that such
x
is unique.) | [
{
"input": "4\n1 2 0 3\n",
"output": "4\n"
},
{
"input": "3\n0 1 2\n",
"output": "3\n"
},
{
"input": "10\n3 9 6 1 7 2 8 0 5 4\n",
"output": "5\n"
}
] |
https://atcoder.jp/contests/abc268/tasks/abc268_d | Problem Statement
Takahashi is having trouble with deciding a username for a service. Write a code to help him.
Find a string
X
that satisfies all of the following conditions:
X
is obtained by the following procedure:
Let
S_1', S_2', \ldots,S_N'
be a permutation of
S_1, S_2, \ldots,S_N
. Let
X
be the concatenation of
S_1'
, (
1
or more copies of
_
),
S_2'
, (
1
or more copies of
_
),
\ldots
, (
1
or more copies of
_
), and
S_N'
, in this order.
The length of
X
is between
3
and
16
, inclusive.
X
does not coincide with any of
M
strings
T_1,T_2,\ldots,T_M
.
If there is no
X
that satisfies all of the conditions, print
-1
instead. | [
{
"input": "1 1\nchokudai\nchokudai\n",
"output": "-1\n"
},
{
"input": "2 2\nchoku\ndai\nchokudai\nchoku_dai\n",
"output": "dai_choku\n"
},
{
"input": "2 2\nchokudai\natcoder\nchokudai_atcoder\natcoder_chokudai\n",
"output": "-1\n"
},
{
"input": "4 4\nab\ncd\nef\ngh\nhoge\nfuga\n____\n_ab_cd_ef_gh_\n",
"output": "ab__ef___cd_gh\n"
}
] |
https://atcoder.jp/contests/abc268/tasks/abc268_e | Problem Statement
Person
0
, Person
1
,
\ldots
, and Person
(N-1)
are sitting around a turntable in counterclockwise order, evenly spaced. Dish
p_i
is in front of Person
i
on the table.
You may perform the following operation
0
or more times:
Rotate the turntable by one
N
-th of a counterclockwise turn. The dish that was in front of Person
i
right before the rotation is now in front of Person
(i+1) \bmod N
.
When you are finished, Person
i
gains frustration of
k
, where
k
is the minimum integer such that Dish
i
is in front of either Person
(i-k) \bmod N
or Person
(i+k) \bmod N
.
Find the minimum possible sum of frustration of the
N
people.
What is
a \bmod m
?
For an integer
a
and a positive integer
m
,
a \bmod m
denotes the integer
x
between
0
and
(m-1)
(inclusive) such that
(a-x)
is a multiple of
m
. (It can be proved that such
x
is unique.) | [
{
"input": "4\n1 2 0 3\n",
"output": "2\n"
},
{
"input": "3\n0 1 2\n",
"output": "0\n"
},
{
"input": "10\n3 9 6 1 7 2 8 0 5 4\n",
"output": "20\n"
}
] |
https://atcoder.jp/contests/abc268/tasks/abc268_f | Problem Statement
You are given
N
strings
S_1, S_2, \ldots, S_N
consisting of digits from
1
through
9
and the character
X
.
We will choose a permutation
P = (P_1, P_2, \ldots, P_N)
of
(1, 2, \ldots, N)
to construct a string
T = S_{P_1} + S_{P_2} + \cdots + S_{P_N}
, where
+
denotes a concatenation of strings.
Then, we will calculate the "score" of the string
T = T_1T_2\ldots T_{|T|}
(where
|T|
denotes the length of
T
).
The score is calculated by the following
9
steps, starting from the initial score
0
:
Add
1
point to the score as many times as the number of integer pairs
(i, j)
such that
1 \leq i \lt j \leq |T|
,
T_i =
X
, and
T_j =
1
.
Add
2
points to the score as many times as the number of integer pairs
(i, j)
such that
1 \leq i \lt j \leq |T|
,
T_i =
X
, and
T_j =
2
.
Add
3
points to the score as many times as the number of integer pairs
(i, j)
such that
1 \leq i \lt j \leq |T|
,
T_i =
X
, and
T_j =
3
.
\cdots
Add
9
points to the score as many times as the number of integer pairs
(i, j)
such that
1 \leq i \lt j \leq |T|
,
T_i =
X
, and
T_j =
9
.
Find the maximum possible score of
T
when
P
can be chosen arbitrarily. | [
{
"input": "3\n1X3\n59\nXXX\n",
"output": "71\n"
},
{
"input": "10\nX63X395XX\nX2XX3X22X\n13\n3716XXX6\n45X\nX6XX\n9238\n281X92\n1XX4X4XX6\n54X9X711X1\n",
"output": "3010\n"
}
] |
https://atcoder.jp/contests/abc268/tasks/abc268_g | Problem Statement
Takahashi Elementary School has
N
new students. For
i = 1, 2, \ldots, N
, the name of the
i
-th new student is
S_i
(which is a string consisting of lowercase English letters).
The names of the
N
new students are distinct.
The
N
students will be assigned a student ID
1, 2, 3, \ldots, N
in
ascending lexicographical order
of their names. However, instead of the ordinary order of lowercase English letters where
a
is the minimum and
z
is the maximum, we use the following order:
First, Principal Takahashi chooses a string
P
from the
26!
permutations of the string
abcdefghijklmnopqrstuvwxyz
of length
26
, uniformly at random.
The lowercase English characters that occur earlier in
P
are considered smaller.
For each of the
N
students, find the expected value, modulo
998244353
, of the student ID assigned (see Notes).
What is the 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 one of the following 1. and 2. holds.
Here,
|S|
and
|T|
denote the lengths of
S
and
T
, respectively.
|S| \lt |T|
and
S_1S_2\ldots S_{|S|} = T_1T_2\ldots T_{|S|}
.
There exists an integer
1 \leq i \leq \min\lbrace |S|, |T| \rbrace
satisfying the following two conditions:
S_1S_2\ldots S_{i-1} = T_1T_2\ldots T_{i-1}
S_i
is a smaller character than
T_i
. | [
{
"input": "3\na\naa\nab\n",
"output": "1\n499122179\n499122179\n"
},
{
"input": "3\na\naa\naaa\n",
"output": "1\n2\n3\n"
}
] |
https://atcoder.jp/contests/abc268/tasks/abc268_h | Problem Statement
You are given a string
S
. Takahashi may perform the following operation
0
or more times:
Choose an integer
i
such that
1 \leq i \leq |S|
and change the
i
-th character of
S
to
*
.
Takahashi's objective is to make
S
not contain any of
N
strings
T_1,T_2,\ldots,T_N
as a substring
.
Find the minimum number of operations required to achieve the objective. | [
{
"input": "abcdefghijklmn\n3\nabcd\nijk\nghi\n",
"output": "2\n"
},
{
"input": "atcoderbeginnercontest\n1\nabc\n",
"output": "0\n"
},
{
"input": "aaaaaaaaa\n2\naa\nxyz\n",
"output": "4\n"
}
] |
https://atcoder.jp/contests/arc147/tasks/arc147_a | Problem Statement
You are given a sequence of
N
positive integers:
A=(A_1,A_2,\dots,A_N)
.
You will repeat the following operation until the length of
A
becomes
1
.
Let
k
be the length of
A
before this operation.
Choose integers
i
and
j
such that
\max(\{A_1,A_2,\dots,A_{k}\})=A_i,\min(\{A_1,A_2,\dots,A_{k}\})=A_j
, and
i \neq j
.
Then, replace
A_i
with
(A_i \bmod A_j)
.
If
A_i
becomes
0
at this moment, delete
A_i
from
A
.
Find the number of operations that you will perform.
We can prove that, no matter how you choose
i,j
in the operations, the total number of operations does not change. | [
{
"input": "3\n2 3 6\n",
"output": "3\n"
},
{
"input": "6\n1232 452 23491 34099 57341 21488\n",
"output": "12\n"
}
] |
https://atcoder.jp/contests/arc147/tasks/arc147_b | Problem Statement
You are given a permutation
P=(P_1,P_2,\ldots,P_N)
of
(1,2,\ldots,N)
.
You can repeat the following two kinds of operations in any order to make
P
sorted in increasing order.
Operation
A
: Choose an integer
i
such that
1 \leq i \leq N-1
, and swap
P_i
and
P_{i+1}
.
Operation
B
: Choose an integer
i
such that
1 \leq i \leq N-2
, and swap
P_i
and
P_{i+2}
.
Find a sequence of operations with the following property:
The number of Operations
A
is the minimum possible.
The total number of operations is not larger than
10^5
.
Under the Constraints of this problem, we can prove that a solution always exists. | [
{
"input": "4\n3 2 4 1\n",
"output": "4\nA 3\nB 1\nB 2\nB 2\n"
},
{
"input": "3\n1 2 3\n",
"output": "0\n"
},
{
"input": "6\n2 1 4 3 6 5\n",
"output": "3\nA 1\nA 3\nA 5\n"
}
] |
https://atcoder.jp/contests/arc147/tasks/arc147_c | Problem Statement
N
people, numbered
1,2,\ldots ,N
, are going to stand on the number line.
Let's denote by
x_i
the coordinate the Person
i
stands at.
Then,
x_i
should be an integer satisfying
L_i \leq x_i \leq R_i
.
Multiple people can occupy the same coordinate.
We define the
dissatisfaction level
as the following formula:
\displaystyle\sum_{i=1}^{N-1}\sum_{j=i+1}^{N}|x_j-x_i|
Find the minimum possible value of the dissatisfaction level. | [
{
"input": "3\n1 3\n2 4\n5 6\n",
"output": "4\n"
},
{
"input": "3\n1 1\n1 1\n1 1\n",
"output": "0\n"
},
{
"input": "6\n1 5\n2 4\n1 1\n4 4\n3 6\n3 3\n",
"output": "15\n"
}
] |
https://atcoder.jp/contests/arc147/tasks/arc147_d | Problem Statement
Consider a sequence of integer sets of length
N
:
S=(S_1,S_2,\dots,S_N)
.
We call a sequence
brilliant
if it satisfies all of the following conditions:
S_i
is a (possibly empty) integer set, and its elements are in the range
[1,M]
.
(1 \le i \le N)
The number of integers that is included in exactly one of
S_i
and
S_{i+1}
is
1
.
(1 \le i \le N-1)
We define the score of a brilliant sequence
S
as
\displaystyle \prod_{i=1}^{M}
(
the number of sets among
S_1,S_2,\dots,S_N
that include
i
.
)
.
Find, modulo
998244353
, the sum of the scores of all possible brilliant sequences. | [
{
"input": "2 3\n",
"output": "24\n"
},
{
"input": "12 34\n",
"output": "786334067\n"
}
] |
https://atcoder.jp/contests/arc147/tasks/arc147_e | Problem Statement
N
students, numbered
1,2,\ldots,N
, took an examination.
Student
i\,(1 \leq i \leq N)
had to score at least
B_i
points to graduate, where they actually scored
A_i
points.
You can repeat the following operation any number of times (possibly zero):
Choose two students, and swap their scores.
Your goal is to make everyone graduate.
Determine whether it is possible.
If it is possible, find the maximum number of students whose scores do not change during the process. | [
{
"input": "3\n1 2\n3 1\n3 3\n",
"output": "1\n"
},
{
"input": "2\n100 1\n100 1\n",
"output": "2\n"
},
{
"input": "6\n3 2\n1 6\n4 5\n1 3\n5 5\n9 8\n",
"output": "-1\n"
},
{
"input": "6\n3 1\n4 5\n5 2\n2 3\n5 4\n5 1\n",
"output": "3\n"
}
] |
https://atcoder.jp/contests/arc147/tasks/arc147_f | Problem Statement
Consider strings of length
N
consisting of
A
,
B
, and
C
.
Among them, find the number of strings that satisfy the following condition,
modulo
2
:
Let
S_i
be the string formed by the first
i
characters of
S
.
Also let
A_i
,
B_i
, and
C_i
be the numbers of
A
's,
B
's, and
C
's in
S_i
, respectively.
For all
i
such that
1 \le i \le N
, the following holds:
A_i-B_i \le X
B_i-C_i \le Y
C_i-A_i \le Z
You have
T
test cases to solve. | [
{
"input": "1\n3 2 1 0\n",
"output": "0\n"
},
{
"input": "10\n1 22 9 92\n14 7 74 39\n23 50 8 6\n93 40 9 60\n68 8 47 64\n11 68 18 24\n3 26 54 8\n46 17 90 86\n86 76 45 55\n80 68 79 62\n",
"output": "1\n0\n0\n0\n1\n1\n1\n0\n1\n0\n"
}
] |
https://atcoder.jp/contests/abc267/tasks/abc267_a | Problem Statement
One day, tired from going to school, Takahashi wanted to know how many days there were until Saturday.
We know that the day was a weekday, and the name of the day of the week was
S
in English.
How many days were there until the first Saturday after that day (including Saturday but not the starting day)? | [
{
"input": "Wednesday\n",
"output": "3\n"
},
{
"input": "Monday\n",
"output": "5\n"
}
] |
https://atcoder.jp/contests/abc267/tasks/abc267_b | Problem Statement
Bowling pins are numbered
1
through
10
. The following figure is a top view of the arrangement of the pins:
Let us call each part between two dotted lines in the figure a
column
.
For example, Pins
1
and
5
belong to the same column, and so do Pin
3
and
9
.
When some of the pins are knocked down, a special situation called
split
may occur.
A placement of the pins is a split if both of the following conditions are satisfied:
Pin
1
is knocked down.
There are two different columns that satisfy both of the following conditions:
Each of the columns has one or more standing pins.
There exists a column between these columns such that all pins in the column are knocked down.
See also Sample Inputs and Outputs for examples.
Now, you are given a placement of the pins as a string
S
of length
10
.
For
i = 1, \dots, 10
, the
i
-th character of
S
is
0
if Pin
i
is knocked down, and is
1
if it is standing.
Determine if the placement of the pins represented by
S
is a split. | [
{
"input": "0101110101\n",
"output": "Yes\n"
},
{
"input": "0100101001\n",
"output": "Yes\n"
},
{
"input": "0000100110\n",
"output": "No\n"
},
{
"input": "1101110101\n",
"output": "No\n"
}
] |
https://atcoder.jp/contests/abc267/tasks/abc267_c | Problem Statement
You are given an integer sequence
A=(A_1,A_2,\dots,A_N)
of length
N
.
Find the maximum value of
\displaystyle \sum_{i=1}^{M} i \times B_i
for a contiguous subarray
B=(B_1,B_2,\dots,B_M)
of
A
of length
M
. | [
{
"input": "4 2\n5 4 -1 8\n",
"output": "15\n"
},
{
"input": "10 4\n-3 1 -4 1 -5 9 -2 6 -5 3\n",
"output": "31\n"
}
] |
https://atcoder.jp/contests/abc267/tasks/abc267_d | Problem Statement
You are given an integer sequence
A=(A_1,A_2,\dots,A_N)
of length
N
.
Find the maximum value of
\displaystyle \sum_{i=1}^{M} i \times B_i
for a (not necessarily contiguous) subsequence
B=(B_1,B_2,\dots,B_M)
of length
M
of
A
. | [
{
"input": "4 2\n5 4 -1 8\n",
"output": "21\n"
},
{
"input": "10 4\n-3 1 -4 1 -5 9 -2 6 -5 3\n",
"output": "54\n"
}
] |
https://atcoder.jp/contests/abc267/tasks/abc267_e | Problem Statement
You are given a simple undirected graph with
N
vertices and
M
edges. The
i
-th edge connects Vertices
U_i
and
V_i
. Vertex
i
has a positive integer
A_i
written on it.
You will repeat the following operation
N
times.
Choose a Vertex
x
that is not removed yet, and remove Vertex
x
and all edges incident to Vertex
x
. The cost of this operation is the sum of the integers written on the vertices directly connected by an edge with Vertex
x
that are not removed yet.
We define the cost of the entire
N
operations as the maximum of the costs of the individual operations. Find the minimum possible cost of the entire operations. | [
{
"input": "4 3\n3 1 4 2\n1 2\n1 3\n4 1\n",
"output": "3\n"
},
{
"input": "7 13\n464 661 847 514 74 200 188\n5 1\n7 1\n5 7\n4 1\n4 5\n2 4\n5 2\n1 3\n1 6\n3 5\n1 2\n4 6\n2 7\n",
"output": "1199\n"
}
] |
https://atcoder.jp/contests/abc267/tasks/abc267_f | Problem Statement
You are given a tree with
N
vertices. The vertices are numbered
1, \dots, N
, and the
i
-th (
1 \leq i \leq N - 1
) edge connects Vertices
A_i
and
B_i
.
We define the
distance
between Vertices
u
and
v
on this tree by the number of edges in the shortest path from Vertex
u
to Vertex
v
.
You are given
Q
queries. In the
i
-th (
1 \leq i \leq Q
) query, given integers
U_i
and
K_i
, print the index of any vertex whose distance from Vertex
U_i
is exactly
K_i
. If there is no such vertex, print
-1
. | [
{
"input": "5\n1 2\n2 3\n3 4\n3 5\n3\n2 2\n5 3\n3 3\n",
"output": "4\n1\n-1\n"
},
{
"input": "10\n1 2\n2 3\n3 5\n2 8\n3 4\n4 6\n4 9\n5 7\n9 10\n5\n1 1\n2 2\n3 3\n4 4\n5 5\n",
"output": "2\n4\n10\n-1\n-1\n"
}
] |
https://atcoder.jp/contests/abc267/tasks/abc267_g | Problem Statement
You are given an integer sequence
A = (A_1, \dots, A_N)
of length
N
.
Find the number, modulo
998244353
, of permutations
P = (P_1, \dots, P_N)
of
(1, 2, \dots, N)
such that:
there exist exactly
K
integers
i
between
1
and
(N-1)
(inclusive) such that
A_{P_i} \lt A_{P_{i + 1}}
. | [
{
"input": "4 2\n1 1 2 2\n",
"output": "4\n"
},
{
"input": "10 3\n3 1 4 1 5 9 2 6 5 3\n",
"output": "697112\n"
}
] |
https://atcoder.jp/contests/abc267/tasks/abc267_h | Problem Statement
You are given a sequence
A=(A_1,A_2,\dots,A_N)
of length
N
.
Find the number, modulo
998244353
, of ways to choose an odd number of elements from
A
so that the sum of the chosen elements equals
M
.
Two choices are said to be different if there exists an integer
i (1 \le i \le N)
such that one chooses
A_i
but the other does not. | [
{
"input": "5 6\n1 2 3 3 6\n",
"output": "3\n"
},
{
"input": "10 23\n1 2 3 4 5 6 7 8 9 10\n",
"output": "18\n"
}
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.