url
stringlengths 49
92
| description
stringlengths 22
4.78k
| cases
listlengths 0
6
|
---|---|---|
https://atcoder.jp/contests/agc068/tasks/agc068_c | Problem Statement
There are
N
boxes numbered from
1
to
N
, and
N
balls numbered from
1
to
N
.
You have assigned Snuke the following
procedure
as homework.
Put each ball into any box he likes. Multiple balls can be placed in the same box, and there can be boxes without balls.
For
i = 1, 2, \cdots, N
in this order, perform the following operations.
If box
i
contains no balls, do nothing.
If box
i
contains balls, take all of them out and arrange them in a line
in any order he likes
. Let
k
be the number of balls taken out, and
(x_1, x_2, \cdots, x_k)
be the ball numbers in the line. For each
1 \leq j \leq k
, put ball
x_j
into box
x_{j+1}
. Here,
x_{k+1}
means
x_1
. All these operations of putting balls into boxes happen simultaneously.
He claims that he has completed the homework and reports the final state to you.
Specifically, he says that ball
i
is in box
A_i
after the procedure.
You doubt whether he has correctly performed the procedure.
Determine whether the state he reported is a possible result of the procedure.
There are
T
test cases for each input. | [
{
"input": "5\n3\n1 1 1\n3\n2 2 2\n5\n1 2 3 4 5\n10\n8 3 8 10 1 5 3 1 6 4\n10\n1 5 1 2 4 8 8 6 7 3\n",
"output": "Yes\nNo\nYes\nNo\nYes\n"
}
] |
https://atcoder.jp/contests/agc068/tasks/agc068_d | Problem Statement
There is a rooted tree
T
with
N
vertices numbered from
1
to
N
.
Vertex
1
is the root, and the parent of vertex
i
(
2 \leq i \leq N
) is
P_i
(
P_i < i
).
A permutation
x = (x_1, x_2, \cdots, x_N)
of
(1, 2, \cdots, N)
is judged to be a
good
permutation or not by the following criteria.
Consider the following operation on
x
.
Choose two adjacent elements
u
and
v
in
x
such that
u
and
v
are in an ancestor-descendant relationship in
T
. It does not matter which is the ancestor and which is the descendant. Then, swap
u
and
v
.
If it is possible to obtain a permutation that is lexicographically strictly smaller than the initial state by performing the above operation zero or more times,
x
is
not
a good permutation. If it is impossible to obtain a permutation lexicographically smaller than the initial state by any such operations,
x
is a good permutation.
You are given a positive integer
B
.
For a permutation
x
, define
\operatorname{hash}(x) = \sum_{1 \leq i \leq N} B^{i-1} \times x_i
.
Find the sum of
\operatorname{hash}(x)
over all good permutations
x
, modulo
998244353
.
What is lexicographical order on sequences?
A sequence
S = (S_1, S_2, \ldots, S_{|S|})
is said to be
lexicographically smaller
than a sequence
T = (T_1, T_2, \ldots, T_{|T|})
if and only if 1. or 2. below holds.
Here,
|S|
and
|T|
denote the lengths of
S
and
T
, respectively.
|S| \lt |T|
and
(S_1, S_2, \ldots, S_{|S|}) = (T_1, T_2, \ldots, T_{|S|})
.
There exists an integer
1 \leq i \leq \min\{ |S|, |T| \}
such that the following two statements hold.
(S_1, S_2, \ldots, S_{i-1}) = (T_1, T_2, \ldots, T_{i-1})
.
S_i
is smaller than
T_i
(as a number). | [
{
"input": "3 100\n1 1\n",
"output": "50502\n"
},
{
"input": "5 100\n1 2 3 4\n",
"output": "504030201\n"
},
{
"input": "10 248730679\n1 2 1 2 5 6 1 8 1\n",
"output": "856673861\n"
},
{
"input": "20 480124393\n1 2 3 2 3 4 3 8 3 4 11 10 4 14 15 12 17 18 19\n",
"output": "488941820\n"
}
] |
https://atcoder.jp/contests/agc068/tasks/agc068_e | Problem Statement
You are given an
N \times N
integer matrix
A = (A_{i,j})_{1 \leq i \leq N, 1 \leq j \leq N}
and an integer
M
.
For an integer sequence
x = (x_1, x_2, \cdots, x_M)
of length
M
consisting of integers between
1
and
N
, inclusive, define
f(x)
as follows:
Let
y = (y_1, y_2, \cdots, y_M)
be the sequence obtained by sorting the elements of
x
to be non-decreasing.
Define
f(x) = \prod_{1 \leq i \leq M} A_{x_i, y_i}
.
For each
k = 1, 2, \cdots, N
, solve the following problem:
Find the sum of
f(x)
over all sequences
x
satisfying
x_1 = k
, modulo
998244353
. | [
{
"input": "2 2\n1 2\n3 4\n",
"output": "5 22\n"
},
{
"input": "2 3\n1 2\n3 4\n",
"output": "27 118\n"
},
{
"input": "5 4\n785439575 250040585 709423541 945005786 19237225\n404191279 250876592 22672563 519729086 344065186\n273714212 560047125 139793596 542901248 520999410\n855572558 498896932 418633758 742973826 248730678\n238856535 319502970 908902333 164543594 245101681\n",
"output": "216530400 726773157 717209375 797938347 957133905\n"
},
{
"input": "10 50\n197971506 714635866 966125570 768080799 80565655 459117401 256810168 775681305 582857561 948631706\n437330820 321722967 531470304 255908811 45459908 504089816 695016247 91058795 905527130 30860430\n151769562 979797105 619322493 298241991 360690308 480124392 297323928 284686636 922571073 627798362\n753705854 712639027 721488863 69714419 485979799 88704853 758288417 423073188 595934547 86264514\n272481811 622712481 221745114 225051881 433378197 985573661 210619166 851716760 283615535 834897126\n366075547 933505674 858395194 490049033 22039836 361481447 735151983 518458804 422209788 28981946\n907645400 111982636 978445563 686621115 486475154 734616351 587635888 527524079 454844580 826849288\n868863954 490627044 967828344 887235558 138021910 435784230 343307056 118718683 547282350 757693154\n32344652 101428952 585897722 693817619 790433406 848494439 873744405 604427602 951889915 989125209\n865548541 642980476 603592935 911086893 466178404 79002814 902745597 825893950 147052664 805753279\n",
"output": "862518623 606960987 762676180 606184511 762408948 47716007 968649097 788324707 140177479 484063588\n"
}
] |
https://atcoder.jp/contests/abc373/tasks/abc373_a | Problem Statement
There are
12
strings
S_1, S_2, \ldots, S_{12}
consisting of lowercase English letters.
Find how many integers
i
(1 \leq i \leq 12)
satisfy that the length of
S_i
is
i
. | [
{
"input": "january\nfebruary\nmarch\napril\nmay\njune\njuly\naugust\nseptember\noctober\nnovember\ndecember\n",
"output": "1\n"
},
{
"input": "ve\ninrtfa\nnpccxva\ndjiq\nlmbkktngaovl\nmlfiv\nfmbvcmuxuwggfq\nqgmtwxmb\njii\nts\nbfxrvs\neqvy\n",
"output": "2\n"
}
] |
https://atcoder.jp/contests/abc373/tasks/abc373_b | Problem Statement
There is a keyboard with
26
keys arranged on a number line.
The arrangement of this keyboard is represented by a string
S
, which is a permutation of
ABCDEFGHIJKLMNOPQRSTUVWXYZ
.
The key corresponding to the character
S_x
is located at coordinate
x
(1 \leq x \leq 26)
. Here,
S_x
denotes the
x
-th character of
S
.
You will use this keyboard to input
ABCDEFGHIJKLMNOPQRSTUVWXYZ
in this order, typing each letter exactly once with your right index finger.
To input a character, you need to move your finger to the coordinate of the key corresponding to that character and press the key.
Initially, your finger is at the coordinate of the key corresponding to
A
. Find the minimal possible total traveled distance of your finger from pressing the key for
A
to pressing the key for
Z
. Here, pressing a key does not contribute to the distance. | [
{
"input": "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
"output": "25\n"
},
{
"input": "MGJYIZDKSBHPVENFLQURTCWOAX\n",
"output": "223\n"
}
] |
https://atcoder.jp/contests/abc373/tasks/abc373_c | Problem Statement
You are given two integer sequences
A
and
B
, each of length
N
. Choose integers
i, j
(1 \leq i, j \leq N)
to maximize the value of
A_i + B_j
. | [
{
"input": "2\n-1 5\n3 -7\n",
"output": "8\n"
},
{
"input": "6\n15 12 3 -13 -1 -19\n7 17 -13 -10 18 4\n",
"output": "33\n"
}
] |
https://atcoder.jp/contests/abc373/tasks/abc373_d | Problem Statement
You are given a directed graph with
N
vertices and
M
edges. The
j
-th directed edge goes from vertex
u_j
to vertex
v_j
and has a weight of
w_j
.
Find one way to write an integer between
-10^{18}
and
10^{18}
, inclusive, to each vertex such that the following condition is satisfied.
Let
x_i
be the value written on vertex
i
. For all edges
j=1,2,\dots,M
, it holds that
x_{v_j} - x_{u_j} = w_j
.
It is guaranteed that at least one such assignment exists for the given input. | [
{
"input": "3 3\n1 2 2\n3 2 3\n1 3 -1\n",
"output": "3 5 2\n"
},
{
"input": "4 2\n2 1 5\n3 4 -3\n",
"output": "5 0 6 3\n"
},
{
"input": "5 7\n2 1 18169343\n3 1 307110901\n4 1 130955934\n2 3 -288941558\n2 5 96267410\n5 3 -385208968\n4 3 -176154967\n",
"output": "200401298 182231955 -106709603 69445364 278499365\n"
}
] |
https://atcoder.jp/contests/abc373/tasks/abc373_e | Problem Statement
An election is being held with
N
candidates numbered
1, 2, \ldots, N
. There are
K
votes, some of which have been counted so far.
Up until now, candidate
i
has received
A_i
votes.
After all ballots are counted, candidate
i
(1 \leq i \leq N)
will be elected if and only if the number of candidates who have received more votes than them is less than
M
. There may be multiple candidates elected.
For each candidate, find the minimum number of additional votes they need from the remaining ballots to guarantee their victory regardless of how the other candidates receive votes.
Formally, solve the following problem for each
i = 1,2,\ldots,N
.
Determine if there is a non-negative integer
X
not exceeding
K - \displaystyle{\sum_{i=1}^{N}} A_i
satisfying the following condition. If it exists, find the minimum possible such integer.
If candidate
i
receives
X
additional votes, then candidate
i
will always be elected. | [
{
"input": "5 2 16\n3 1 4 1 5\n",
"output": "2 -1 1 -1 0\n"
},
{
"input": "12 1 570\n81 62 17 5 5 86 15 7 79 26 6 28\n",
"output": "79 89 111 117 117 74 112 116 80 107 117 106\n"
}
] |
https://atcoder.jp/contests/abc373/tasks/abc373_f | Problem Statement
There are
N
types of items. The
i
-th type of item has a weight of
w_i
and a value of
v_i
. Each type has
10^{10}
items available.
Takahashi is going to choose some items and put them into a bag with capacity
W
. He wants to maximize the value of the selected items while avoiding choosing too many items of the same type. Hence, he defines the
happiness
of choosing
k_i
items of type
i
as
k_i v_i - k_i^2
. He wants to choose items to maximize the total happiness over all types while keeping the total weight at most
W
. Calculate the maximum total happiness he can achieve. | [
{
"input": "2 10\n3 4\n3 2\n",
"output": "5\n"
},
{
"input": "3 6\n1 4\n2 3\n2 7\n",
"output": "14\n"
},
{
"input": "1 10\n1 7\n",
"output": "12\n"
}
] |
https://atcoder.jp/contests/abc373/tasks/abc373_g | Problem Statement
There are
2N
points
P_1,P_2,\ldots,P_N, Q_1,Q_2,\ldots,Q_N
on a two-dimensional plane.
The coordinates of
P_i
are
(A_i, B_i)
, and the coordinates of
Q_i
are
(C_i, D_i)
.
No three different points lie on the same straight line.
Determine whether there exists a permutation
R = (R_1, R_2, \ldots, R_N)
of
(1, 2, \ldots, N)
that satisfies the following condition. If such an
R
exists, find one.
For each integer
i
from
1
through
N
, let segment
i
be the line segment connecting
P_i
and
Q_{R_i}
. Then, segment
i
and segment
j
(1 \leq i < j \leq N)
never intersect. | [
{
"input": "3\n0 0\n2 4\n4 2\n0 2\n2 0\n4 4\n",
"output": "2 1 3\n"
},
{
"input": "8\n59 85\n60 57\n72 12\n3 27\n16 58\n41 94\n77 64\n97 20\n32 37\n7 2\n57 94\n35 70\n38 60\n97 100\n5 76\n38 8\n",
"output": "3 5 8 2 7 4 6 1\n"
}
] |
https://atcoder.jp/contests/arc184/tasks/arc184_a | Problem Statement
This problem is
interactive
, and the
judge is adaptive
. See the notes for details.
Also, the parameters in the problem statement are fixed at
N=1000
,
M=10
,
Q=950
.
There are
N
coins numbered
1, 2, \dots, N
.
Exactly
M
of these coins are counterfeit.
An appraiser can, in one appraisal, determine whether two coins are of the same type or different types. Specifically:
If the two coins are both genuine or both counterfeit, they are judged to be of the same type.
Otherwise, they are judged to be of different types.
Identify all the counterfeit coins using at most
Q
appraisals. | [] |
https://atcoder.jp/contests/arc184/tasks/arc184_b | Problem Statement
You are given a positive integer
N
. There is an empty set
S
, and you can perform the following operation any number of times:
Choose any positive integer
x
. For each of
x
,
2x
, and
3x
, add it to
S
if it is not already in
S
.
Find the minimum number of operations required to satisfy
\{1, 2, \dots, N\} \subseteq S
. | [
{
"input": "7\n",
"output": "4\n"
},
{
"input": "25\n",
"output": "14\n"
}
] |
https://atcoder.jp/contests/arc184/tasks/arc184_c | Problem Statement
We have a long, thin piece of paper whose thickness can be ignored. We perform the following operation
100
times: lift the right end, fold it so that it aligns with the left end using the center as a crease. After completing the
100
folds, we unfold the paper back to its original state. At this point, there are
2^{100} - 1
creases on the paper, and these creases can be classified into two types: mountain folds and valley folds. The figure below represents the state after performing the operation twice, where red solid lines represent mountain folds and red dashed lines represent valley folds.
About mountain and valley folds
A crease is a mountain fold if it is folded so that the back sides of the paper come together at the crease.
A crease is a valley fold if it is folded so that the front sides of the paper come together at the crease.
You are given a sequence
A = (A_1, A_2, \dots, A_N)
of
N
non-negative integers. Here,
0 = A_1 < A_2 < \dots < A_N \leq 10^{18}
.
For each integer
i
from
1
through
2^{100} - A_N - 1
, define
f(i)
as follows:
The number of
k = 1, 2, \dots, N
such that the
(i + A_k)
-th crease from the left is a mountain fold.
Find the maximum value among
f(1), f(2), \dots, f(2^{100} - A_N - 1)
. | [
{
"input": "4\n0 1 2 3\n",
"output": "3\n"
},
{
"input": "6\n0 2 3 5 7 8\n",
"output": "4\n"
}
] |
https://atcoder.jp/contests/arc184/tasks/arc184_d | Problem Statement
There are
N
balls on a two-dimensional plane, numbered from
1
to
N
. Ball
i
is at point
(X_i, Y_i)
. Here,
X = (X_1, X_2, \dots, X_N)
and
Y = (Y_1, Y_2, \dots, Y_N)
are permutations of
(1, 2, \dots, N)
.
You can perform the following operation any number of times:
Choose one of the remaining balls, say ball
k
. Then, for each remaining ball
i
, if either "
X_i < X_k
and
Y_i < Y_k
" or "
X_i > X_k
and
Y_i > Y_k
" holds, remove ball
i
.
Find the number of possible sets of balls remaining after performing operations, modulo
998244353
. | [
{
"input": "3\n1 3\n2 1\n3 2\n",
"output": "3\n"
},
{
"input": "4\n4 2\n2 1\n3 3\n1 4\n",
"output": "3\n"
}
] |
https://atcoder.jp/contests/arc184/tasks/arc184_e | Problem Statement
You are given
N
length-
M
sequences, where each element is
0
or
1
. The
i
-th sequence is
A_i = (A_{i, 1}, A_{i, 2}, \dots, A_{i, M})
.
For integers
i, j \ (1 \leq i, j \leq N)
, define
f(i, j)
as follows:
f(i, j) :=
The smallest non-negative integer
x
such that
A_i
and
A_j
become identical after performing the following operation
x
times, or
0
if such
x
does not exist.
For all integers
k \ (1 \leq k \leq M)
simultaneously, replace
A_{i, k}
with
\displaystyle \left (\sum_{l=1}^{k} A_{i, l} \right ) \bmod 2
.
Find
\displaystyle \sum_{i=1}^{N} \sum_{j=i}^{N} f(i, j)
, modulo
998244353
. | [
{
"input": "4 3\n1 0 0\n1 1 0\n1 0 1\n0 1 1\n",
"output": "8\n"
},
{
"input": "7 6\n1 0 0 0 0 0\n1 1 1 0 0 0\n1 0 1 1 0 0\n1 0 0 0 1 1\n1 0 0 0 0 1\n1 0 0 0 0 0\n1 1 1 1 1 1\n",
"output": "6\n"
}
] |
https://atcoder.jp/contests/abc372/tasks/abc372_a | Problem Statement
You are given a string
S
consisting of lowercase English letters and
.
.
Find the string obtained by removing all
.
from
S
. | [
{
"input": ".v.\n",
"output": "v\n"
},
{
"input": "chokudai\n",
"output": "chokudai\n"
},
{
"input": "...\n",
"output": "\n"
}
] |
https://atcoder.jp/contests/abc372/tasks/abc372_b | Problem Statement
You are given a positive integer
M
.
Find a positive integer
N
and a sequence of non-negative integers
A = (A_1, A_2, \ldots, A_N)
that satisfy all of the following conditions:
1 \le N \le 20
0 \le A_i \le 10
(1 \le i \le N)
\displaystyle \sum_{i=1}^N 3^{A_i} = M
It can be proved that under the constraints, there always exists at least one such pair of
N
and
A
satisfying the conditions. | [
{
"input": "6\n",
"output": "2\n1 1\n"
},
{
"input": "100\n",
"output": "4\n2 0 2 4\n"
},
{
"input": "59048\n",
"output": "20\n0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9\n"
}
] |
https://atcoder.jp/contests/abc372/tasks/abc372_c | Problem Statement
You are given a string
S
of length
N
. You are also given
Q
queries, which you should process in order.
The
i
-th query is as follows:
Given an integer
X_i
and a character
C_i
, replace the
X_i
-th character of
S
with
C_i
. Then, print the number of times the string
ABC
appears as a substring in
S
.
Here, a
substring
of
S
is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of
S
.
For example,
ab
is a substring of
abc
, but
ac
is not a substring of
abc
. | [
{
"input": "7 4\nABCDABC\n4 B\n3 A\n5 C\n4 G\n",
"output": "2\n1\n1\n0\n"
},
{
"input": "3 3\nABC\n1 A\n2 B\n3 C\n",
"output": "1\n1\n1\n"
},
{
"input": "15 10\nBBCCBCACCBACACA\n9 C\n11 B\n5 B\n11 B\n4 A\n8 C\n8 B\n5 B\n7 B\n14 B\n",
"output": "0\n0\n0\n0\n1\n1\n2\n2\n1\n1\n"
}
] |
https://atcoder.jp/contests/abc372/tasks/abc372_d | Problem Statement
There are
N
buildings, Building
1
, Building
2
,
\ldots
, Building
N
, arranged in a line in this order. The height of Building
i
(1 \leq i \leq N)
is
H_i
.
For each
i = 1, 2, \ldots, N
, find the number of integers
j
(i < j \leq N)
satisfying the following condition:
There is no building taller than Building
j
between Buildings
i
and
j
. | [
{
"input": "5\n2 1 4 3 5\n",
"output": "3 2 2 1 0\n"
},
{
"input": "4\n1 2 3 4\n",
"output": "3 2 1 0\n"
},
{
"input": "10\n1 9 6 5 2 7 10 4 8 3\n",
"output": "2 3 3 3 2 1 2 1 1 0\n"
}
] |
https://atcoder.jp/contests/abc372/tasks/abc372_e | Problem Statement
There is an undirected graph with
N
vertices and
0
edges. The vertices are numbered
1
to
N
.
You are given
Q
queries to process in order. Each query is of one of the following two types:
Type
1
: Given in the format
1 u v
. Add an edge between vertices
u
and
v
.
Type
2
: Given in the format
2 v k
. Print the
k
-th largest vertex number among the vertices connected to vertex
v
. If there are fewer than
k
vertices connected to
v
, print
-1
. | [
{
"input": "4 10\n1 1 2\n2 1 1\n2 1 2\n2 1 3\n1 1 3\n1 2 3\n1 3 4\n2 1 1\n2 1 3\n2 1 5\n",
"output": "2\n1\n-1\n4\n2\n-1\n"
},
{
"input": "6 20\n1 3 4\n1 3 5\n2 1 1\n2 3 1\n1 1 5\n2 6 9\n2 1 3\n2 6 1\n1 4 6\n2 2 1\n2 6 2\n2 4 7\n1 1 4\n2 6 2\n2 3 4\n1 2 5\n2 4 1\n1 1 6\n2 3 3\n2 1 3\n",
"output": "1\n5\n-1\n3\n6\n2\n5\n-1\n5\n3\n6\n4\n4\n"
}
] |
https://atcoder.jp/contests/abc372/tasks/abc372_f | Problem Statement
There is a simple directed graph
G
with
N
vertices and
N+M
edges. The vertices are numbered
1
to
N
, and the edges are numbered
1
to
N+M
.
Edge
i
(1 \leq i \leq N)
goes from vertex
i
to vertex
i+1
. (Here, vertex
N+1
is considered as vertex
1
.)
Edge
N+i
(1 \leq i \leq M)
goes from vertex
X_i
to vertex
Y_i
.
Takahashi is at vertex
1
. At each vertex, he can move to any vertex to which there is an outgoing edge from the current vertex.
Compute the number of ways he can move exactly
K
times.
That is, find the number of integer sequences
(v_0, v_1, \dots, v_K)
of length
K+1
satisfying all of the following three conditions:
1 \leq v_i \leq N
for
i = 0, 1, \dots, K
.
v_0 = 1
.
There is a directed edge from vertex
v_{i-1}
to vertex
v_i
for
i = 1, 2, \ldots, K
.
Since this number can be very large, print it modulo
998244353
. | [
{
"input": "6 2 5\n1 4\n2 5\n",
"output": "5\n"
},
{
"input": "10 0 200000\n",
"output": "1\n"
},
{
"input": "199 10 1326\n122 39\n142 49\n164 119\n197 127\n188 145\n69 80\n6 120\n24 160\n18 154\n185 27\n",
"output": "451022766\n"
}
] |
https://atcoder.jp/contests/abc372/tasks/abc372_g | Problem Statement
You are given three length-
N
sequences of positive integers:
A=(A_1,A_2,\ldots,A_N)
,
B=(B_1,B_2,\ldots,B_N)
, and
C=(C_1,C_2,\ldots,C_N)
.
Find the number of pairs of positive integers
(x, y)
that satisfy the following condition:
A_i \times x + B_i \times y < C_i
for all
1 \leq i \leq N
.
It can be proved that the number of such pairs of positive integers satisfying the condition is finite.
You are given
T
test cases, each of which should be solved. | [
{
"input": "2\n2\n1 1 4\n1 2 5\n1\n1 1 2\n",
"output": "2\n0\n"
},
{
"input": "3\n7\n138 16011 918976\n5478 7748 499926\n5234 17727 748589\n1157 10511 643136\n31200 3005 721285\n28839 14469 798851\n1933 5378 864127\n9\n17775 1665 386430\n37001 863 922418\n9756 4182 746671\n12379 9106 807578\n3984 4049 640539\n25333 9869 780810\n20372 7000 688738\n16107 11974 827227\n10779 10531 770510\n5\n4916 14132 460944\n11856 45422 610561\n56014 18216 825793\n10363 6220 945356\n37418 33866 851593\n",
"output": "660\n995\n140\n"
}
] |
https://atcoder.jp/contests/ahc037/tasks/ahc037_a | Problem Statement
The
sweetness
and
carbonation level
of beverages are represented by non-negative integers. A beverage with sweetness
x
and carbonation level
y
is denoted by
(x, y)
.
Initially you only have beverage
(0, 0)
. Below we write
S
for the set of beverages already made. Initially
S = \{(0, 0)\}
.
You can repeat the following operation to make multiple beverages.
Choose an already made beverage
(x, y) \in S
.
Choose integers
x', y'
such that
x' \ge x
and
y' \ge y
.
Divide beverage
(x, y)
into two containers, and keep one of them. Make a new beverage
(x', y')
by adding the appropriate amount of syrup and carbon dioxide to the other. Add
(x', y')
to
S
.
Notice that
(x, y)
will
not
be removed from
S
.
This operation costs
(x' - x) + (y' - y)
.
Given
N
beverages
(A_1, B_1), \dots, (A_N, B_N)
in the input, you can perform the above operation up to
5N
times. Find a sequence of operations that makes all of the specified beverages (that is, a sequence of operations such that
(A_i, B_i) \in S
holds for all
i \in \{1, 2, \dots, N\}
after all operations) with the total cost as small as possible. It is allowed to make beverages not present in the input during the process. | [
{
"input": "4\n0 6\n2 5\n3 2\n4 0\n",
"output": "6\n0 0 2 0\n0 0 0 6\n2 0 4 0\n2 0 2 2\n2 2 3 2\n2 2 2 5\n"
}
] |
https://atcoder.jp/contests/abc371/tasks/abc371_a | Problem Statement
There are three brothers named
A
,
B
, and
C
. The age relationships among them are given by three characters
S_{\mathrm{AB}}, S_{\mathrm{AC}}, S_{\mathrm{BC}}
, which mean the following:
If
S_{\mathrm{AB}}
is
<
, then
A
is younger than
B
; if it is
>
, then
A
is older than
B
.
If
S_{\mathrm{AC}}
is
<
, then
A
is younger than
C
; if it is
>
, then
A
is older than
C
.
If
S_{\mathrm{BC}}
is
<
, then
B
is younger than
C
; if it is
>
, then
B
is older than
C
.
Who is the middle brother, that is, the second oldest among the three? | [
{
"input": "< < <\n",
"output": "B\n"
},
{
"input": "< < >\n",
"output": "C\n"
}
] |
https://atcoder.jp/contests/abc371/tasks/abc371_b | Problem Statement
In the Kingdom of AtCoder, the eldest son is always given the name Taro. No one else is given the name Taro.
The eldest son is the earliest born male child in each family.
There are
N
families in the Kingdom, and
M
babies were born. Before the
M
babies were born, none of the
N
families had had any babies.
Information about the babies is given in chronological order of their birth.
The
i
-th baby born was born in family
A_i
, and the baby is male if
B_i
is
M
, and female if it is
F
.
Determine for each of the
M
babies whether the name given is Taro. | [
{
"input": "2 4\n1 M\n1 M\n2 F\n2 M\n",
"output": "Yes\nNo\nNo\nYes\n"
},
{
"input": "4 7\n2 M\n3 M\n1 F\n4 F\n4 F\n1 F\n2 M\n",
"output": "Yes\nYes\nNo\nNo\nNo\nNo\nNo\n"
}
] |
https://atcoder.jp/contests/abc371/tasks/abc371_c | Problem Statement
You are given simple undirected graphs
G
and
H
, each with
N
vertices: vertices
1
,
2
,
\ldots
,
N
.
Graph
G
has
M_G
edges, and its
i
-th edge
(1\leq i\leq M_G)
connects vertices
u_i
and
v_i
.
Graph
H
has
M_H
edges, and its
i
-th edge
(1\leq i\leq M_H)
connects vertices
a_i
and
b_i
.
You can perform the following operation on graph
H
any number of times, possibly zero.
Choose a pair of integers
(i,j)
satisfying
1\leq i<j\leq N
. Pay
A_{i,j}
yen, and if there is no edge between vertices
i
and
j
in
H
, add one; if there is, remove it.
Find the minimum total cost required to make
G
and
H
isomorphic.
What is a simple undirected graph?
A
simple undirected graph
is a graph without self-loops or multi-edges, where edges have no direction.
What does it mean for graphs to be isomorphic?
Two graphs
G
and
H
with
N
vertices are
isomorphic
if and only if there exists a permutation
(P_1,P_2,\ldots,P_N)
of
(1,2,\ldots,N)
such that for all
1\leq i\lt j\leq N
:
an edge exists between vertices
i
and
j
in
G
if and only if an edge exists between vertices
P_i
and
P_j
in
H
. | [
{
"input": "5\n4\n1 2\n2 3\n3 4\n4 5\n4\n1 2\n1 3\n1 4\n1 5\n3 1 4 1\n5 9 2\n6 5\n3\n",
"output": "9\n"
},
{
"input": "5\n3\n1 2\n2 3\n3 4\n4\n1 2\n2 3\n3 4\n4 5\n9 1 1 1\n1 1 1\n1 1\n9\n",
"output": "3\n"
},
{
"input": "5\n3\n1 2\n2 3\n3 4\n4\n1 2\n2 3\n3 4\n4 5\n5 4 4 4\n4 4 4\n4 4\n5\n",
"output": "5\n"
},
{
"input": "2\n0\n0\n371\n",
"output": "0\n"
},
{
"input": "8\n13\n1 8\n5 7\n4 6\n1 5\n7 8\n1 6\n1 2\n5 8\n2 6\n5 6\n6 7\n3 7\n4 8\n15\n3 5\n1 7\n4 6\n3 8\n7 8\n1 2\n5 6\n1 6\n1 5\n1 4\n2 8\n2 6\n2 4\n4 7\n1 3\n7483 1694 5868 3296 9723 5299 4326\n5195 4088 5871 1384 2491 6562\n1149 6326 2996 9845 7557\n4041 7720 1554 5060\n8329 8541 3530\n4652 3874\n3748\n",
"output": "21214\n"
}
] |
https://atcoder.jp/contests/abc371/tasks/abc371_d | Problem Statement
There are
N
villages on a number line. The
i
-th village is located at coordinate
X_i
, and has
P_i
villagers.
Answer
Q
queries. The
i
-th query is in the following format:
Given integers
L_i
and
R_i
, find the total number of villagers living in villages located between coordinates
L_i
and
R_i
, inclusive. | [
{
"input": "4\n1 3 5 7\n1 2 3 4\n4\n1 1\n2 6\n0 10\n2 2\n",
"output": "1\n5\n10\n0\n"
},
{
"input": "7\n-10 -5 -3 -1 0 1 4\n2 5 6 5 2 1 7\n8\n-7 7\n-1 5\n-10 -4\n-8 10\n-5 0\n-10 5\n-8 7\n-8 -3\n",
"output": "26\n15\n7\n26\n18\n28\n26\n11\n"
}
] |
https://atcoder.jp/contests/abc371/tasks/abc371_e | Problem Statement
You are given a sequence of integers
A = (A_1, A_2, \ldots, A_N)
of length
N
.
Define
f(l, r)
as:
the number of distinct values in the subsequence
(A_l, A_{l+1}, \ldots, A_r)
.
Evaluate the following expression:
\displaystyle \sum_{i=1}^{N}\sum_{j=i}^N f(i,j)
. | [
{
"input": "3\n1 2 2\n",
"output": "8\n"
},
{
"input": "9\n5 4 2 2 3 2 4 4 1\n",
"output": "111\n"
}
] |
https://atcoder.jp/contests/abc371/tasks/abc371_f | Problem Statement
There is a road extending east and west, and
N
persons are on the road.
The road extends infinitely long to the east and west from a point called the origin.
The
i
-th person
(1\leq i\leq N)
is initially at a position
X_i
meters east from the origin.
The persons can move along the road to the east or west.
Specifically, they can perform the following movement any number of times.
Choose one person.
If there is no other person at the destination
, move the chosen person
1
meter east or west.
They have
Q
tasks in total, and the
i
-th task
(1\leq i\leq Q)
is as follows.
The
T_i
-th person arrives at coordinate
G_i
.
Find the minimum total number of movements required to complete all
Q
tasks in order. | [
{
"input": "5\n10 20 30 40 50\n4\n3 45\n4 20\n1 35\n2 60\n",
"output": "239\n"
},
{
"input": "8\n0 1 2 3 4 5 6 100000000\n6\n1 100000000\n8 0\n1 100000000\n8 4\n1 100000000\n5 21006578\n",
"output": "4294967297\n"
},
{
"input": "12\n1558 3536 3755 3881 4042 4657 5062 7558 7721 8330 8542 9845\n8\n9 1694\n7 3296\n12 5299\n5 5195\n5 5871\n1 2491\n8 1149\n8 2996\n",
"output": "89644\n"
}
] |
https://atcoder.jp/contests/abc371/tasks/abc371_g | Problem Statement
You are given permutations
P = (P_1, P_2, \ldots, P_N)
and
A = (A_1, A_2, \ldots, A_N)
of
(1,2,\ldots,N)
.
You can perform the following operation any number of times, possibly zero:
replace
A_i
with
A_{P_i}
simultaneously
for all
i=1,2,\ldots,N
.
Print the lexicographically smallest
A
that can be obtained.
What is lexicographical order?
For sequences of length
N
,
A = (A_1, A_2, \ldots, A_N)
and
B = (B_1, B_2, \ldots, B_N)
,
A
is lexicographically smaller than
B
if and only if:
there exists an integer
i\ (1\leq i\leq N)
such that
A_i < B_i
, and
A_j = B_j
for all
1\leq j < i
. | [
{
"input": "6\n3 1 5 6 2 4\n4 3 1 6 2 5\n",
"output": "1 4 2 5 3 6\n"
},
{
"input": "8\n3 5 8 7 2 6 1 4\n1 2 3 4 5 6 7 8\n",
"output": "1 2 3 4 5 6 7 8\n"
},
{
"input": "26\n24 14 4 20 15 19 16 11 23 22 12 18 21 3 6 8 26 2 25 7 13 1 5 9 17 10\n15 3 10 1 13 19 22 24 20 4 14 23 7 26 25 18 11 6 9 12 2 21 5 16 8 17\n",
"output": "4 1 22 18 20 13 14 6 15 11 3 26 2 12 5 23 9 10 25 24 7 17 16 21 19 8\n"
}
] |
https://atcoder.jp/contests/joi2025yo1a/tasks/joi2025_yo1a_a | ๅ้กๆ
JOI ๅใฎๆๆ้ใฏ
A
ๅใงใใ๏ผJOI ๅใฏ
1
ๆฌ
5
ๅใฎ้็ญใๆๅคงใงไฝๆฌ่ฒทใใใใๆฑใใ๏ผ | [
{
"input": "9\n",
"output": "1\n"
},
{
"input": "10\n",
"output": "2\n"
},
{
"input": "3\n",
"output": "0\n"
},
{
"input": "100\n",
"output": "20\n"
}
] |
https://atcoder.jp/contests/joi2025yo1a/tasks/joi2025_yo1a_b | ๅ้กๆ
JOI ๅใฏ๏ผไธ็ด็ทใฎ้่ทฏไธใงๆฃๆญฉใใใฆใใ๏ผ
JOI ๅใฏไปฅไธใฎ่กๅใ่กๅ A โ ่กๅ B โ ่กๅ A โ
\cdots
ใฎใใใซไบคไบใซ็นฐใ่ฟใ๏ผ
่กๅ A๏ผ
3 \: \mathrm{m}
ๅใซ้ฒใ๏ผ
่กๅ B๏ผ
2 \: \mathrm{m}
ๅพใใซๆปใ๏ผ
่กๅ A ใจ่กๅ B ใๅใใใฆ
X
ๅ่กใฃใใจใ๏ผใฏใใใฎไฝ็ฝฎใใไฝ
\mathrm{m}
ๅใซ็งปๅใใใใจใซใชใใ๏ผ | [
{
"input": "3\n",
"output": "4\n"
},
{
"input": "6\n",
"output": "3\n"
},
{
"input": "1\n",
"output": "3\n"
},
{
"input": "37\n",
"output": "21\n"
},
{
"input": "100\n",
"output": "50\n"
}
] |
https://atcoder.jp/contests/joi2025yo1a/tasks/joi2025_yo1a_c | ๅ้กๆ
้ทใ
N
ใฎๆๅญๅ
S
ใไธใใใใ๏ผ
S
ใฎๅๆๅญใฏ
J
๏ผ
O
๏ผ
I
ใฎใใใใใงใใ๏ผ
S
ใฎๅๆๅญใซใคใใฆ๏ผ
J
ใ
O
ใซ๏ผ
O
ใ
I
ใซ๏ผ
I
ใ
J
ใซๅคๆใใๅพใฎๆๅญๅใๅบๅใใ๏ผ | [
{
"input": "3\nJOI\n",
"output": "OIJ\n"
},
{
"input": "10\nJOIOOJOOOJ\n",
"output": "OIJIIOIIIO\n"
}
] |
https://atcoder.jp/contests/joi2025yo1a/tasks/joi2025_yo1a_d | ๅ้กๆ
JOI ๅใฏใฉใ็ผใใไฝใใใจใใฆใใ๏ผ
ใฉใ็ผใใฏใกใใใฉ
1
ใคใใคใฎ้คกใจ็ฎใใไฝใใใจใใงใ๏ผใในใฆใฎ้คกใจ็ฎใซใฏๆญฃใฎๆดๆฐใง่กจใใใใ็พๅณใใใใๅฎ็พฉใใใฆใใ๏ผ
1
ใใ
N
ใพใงใฎ็ชๅทใไปใใใใ
N
็จฎ้กใฎ้คกใใใ๏ผ้คก
i
(
1 \leqq i \leqq N
) ใฎ็พๅณใใใฏ
A_i
ใงใใ๏ผใพใ๏ผ
1
ใใ
M
ใพใงใฎ็ชๅทใไปใใใใ
M
็จฎ้กใฎ็ฎใใใ๏ผ็ฎ
j
(
1 \leqq j \leqq M
) ใฎ็พๅณใใใฏ
B_j
ใงใใ๏ผ
JOI ๅใฏใใใใฎ้คกใจ็ฎใฎ็ตใฟๅใใใใในใฆ่ฉฆใ๏ผ
N \times M
ๅใฎใฉใ็ผใใไฝใ๏ผๅใฉใ็ผใใฎ็พๅณใใใฏ๏ผ้คกใจ็ฎใฎ็พๅณใใใฎๅใซ๏ผ้คกใจ็ฎใฎ็พๅณใใใฎใใกๅคงใใๆนใฎๅคใๆใใใใฎใงใใ๏ผ
N \times M
ๅใฎใฉใ็ผใใฎ็พๅณใใใฎ็ทๅใๆฑใใ๏ผ | [
{
"input": "2 2\n1 2\n2 5\n",
"output": "79\n"
},
{
"input": "1 5\n50\n9 7 5 4 1\n",
"output": "13800\n"
},
{
"input": "15 5\n5 10 52 31 14 16 19 1 9 20 80 19 11 34 72\n20 2 4 9 19\n",
"output": "116756\n"
}
] |
https://atcoder.jp/contests/abc370/tasks/abc370_a | Problem Statement
Takahashi decided to make takoyaki (octopus balls) and serve it to Snuke. Takahashi instructed Snuke to raise only his left hand if he wants to eat takoyaki, and only his right hand otherwise.
You are given the information about which hand Snuke is raising as two integers
L
and
R
.
He is raising his left hand if and only if
L = 1
, and raising his right hand if and only if
R = 1
. He might not follow the instructions and could raise both hands or not raise any hand at all.
If Snuke is raising only one hand, print
Yes
if he wants to eat takoyaki, and
No
if he does not. If he is raising both hands or not raising any hand, print
Invalid
.
Assume that if Snuke is raising only one hand, he is always following the instructions. | [
{
"input": "1 0\n",
"output": "Yes\n"
},
{
"input": "1 1\n",
"output": "Invalid\n"
}
] |
https://atcoder.jp/contests/abc370/tasks/abc370_b | Problem Statement
There are
N
types of elements numbered
1, 2, \ldots, N
.
Elements can be combined with each other. When elements
i
and
j
are combined, they transform into element
A_{i, j}
if
i \geq j
, and into element
A_{j, i}
if
i < j
.
Starting with element
1
, combine it with elements
1, 2, \ldots, N
in this order. Find the final element obtained. | [
{
"input": "4\n3\n2 4\n3 1 2\n2 1 2 4\n",
"output": "2\n"
},
{
"input": "5\n5\n5 5\n5 5 5\n5 5 5 5\n5 5 5 5 5\n",
"output": "5\n"
},
{
"input": "6\n2\n1 5\n1 6 3\n2 6 1 4\n2 1 1 1 6\n5 6 1 2 2 5\n",
"output": "5\n"
}
] |
https://atcoder.jp/contests/abc370/tasks/abc370_c | Problem Statement
You are given two strings
S
and
T
consisting of lowercase English letters. Here,
S
and
T
have equal lengths.
Let
X
be an empty array, and repeat the following operation until
S
equals
T
:
Change one character in
S
, and append
S
to the end of
X
.
Find the array of strings
X
with the minimum number of elements obtained in this way. If there are multiple such arrays with the minimum number of elements, find the lexicographically smallest one among them.
What is lexicographical order on arrays of strings?
A string
S = S_1 S_2 \ldots S_N
of length
N
is
lexicographically smaller
than a string
T = T_1 T_2 \ldots T_N
of length
N
if there exists an integer
1 \leq i \leq N
such that both of the following are satisfied:
S_1 S_2 \ldots S_{i-1} = T_1 T_2 \ldots T_{i-1}
S_i
comes earlier than
T_i
in alphabetical order.
An array of strings
X = (X_1,X_2,\ldots,X_M)
with
M
elements is
lexicographically smaller
than an array of strings
Y = (Y_1,Y_2,\ldots,Y_M)
with
M
elements if there exists an integer
1 \leq j \leq M
such that both of the following are satisfied:
(X_1,X_2,\ldots,X_{j-1}) = (Y_1,Y_2,\ldots,Y_{j-1})
X_j
is lexicographically smaller than
Y_j
. | [
{
"input": "adbe\nbcbc\n",
"output": "3\nacbe\nacbc\nbcbc\n"
},
{
"input": "abcde\nabcde\n",
"output": "0\n"
},
{
"input": "afwgebrw\noarbrenq\n",
"output": "8\naawgebrw\naargebrw\naarbebrw\naarbebnw\naarbebnq\naarbeenq\naarbrenq\noarbrenq\n"
}
] |
https://atcoder.jp/contests/abc370/tasks/abc370_d | Problem Statement
There is a grid with
H
rows and
W
columns. Let
(i, j)
denote the cell at the
i
-th row from the top and
j
-th column from the left.
Initially, there is one wall in each cell.
After processing
Q
queries explained below in the order they are given, find the number of remaining walls.
In the
q
-th query, you are given two integers
R_q
and
C_q
.
You place a bomb at
(R_q, C_q)
to destroy walls. As a result, the following process occurs.
If there is a wall at
(R_q, C_q)
, destroy that wall and end the process.
If there is no wall at
(R_q, C_q)
, destroy the first walls that appear when looking up, down, left, and right from
(R_q, C_q)
. More precisely, the following four processes occur simultaneously:
If there exists an
i \lt R_q
such that a wall exists at
(i, C_q)
and no wall exists at
(k, C_q)
for all
i \lt k \lt R_q
, destroy the wall at
(i, C_q)
.
If there exists an
i \gt R_q
such that a wall exists at
(i, C_q)
and no wall exists at
(k, C_q)
for all
R_q \lt k \lt i
, destroy the wall at
(i, C_q)
.
If there exists a
j \lt C_q
such that a wall exists at
(R_q, j)
and no wall exists at
(R_q, k)
for all
j \lt k \lt C_q
, destroy the wall at
(R_q, j)
.
If there exists a
j \gt C_q
such that a wall exists at
(R_q, j)
and no wall exists at
(R_q, k)
for all
C_q \lt k \lt j
, destroy the wall at
(R_q, j)
. | [
{
"input": "2 4 3\n1 2\n1 2\n1 3\n",
"output": "2\n"
},
{
"input": "5 5 5\n3 3\n3 3\n3 2\n2 2\n1 2\n",
"output": "10\n"
},
{
"input": "4 3 10\n2 2\n4 1\n1 1\n4 2\n2 1\n3 1\n1 3\n1 2\n4 3\n4 2\n",
"output": "2\n"
}
] |
https://atcoder.jp/contests/abc370/tasks/abc370_e | Problem Statement
You are given a sequence
A = (A_1, A_2, \dots, A_N)
of length
N
and an integer
K
.
There are
2^{N-1}
ways to divide
A
into several contiguous subsequences. How many of these divisions have no subsequence whose elements sum to
K
? Find the count modulo
998244353
.
Here, "to divide
A
into several contiguous subsequences" means the following procedure.
Freely choose the number
k
(1 \leq k \leq N)
of subsequences and an integer sequence
(i_1, i_2, \dots, i_k, i_{k+1})
satisfying
1 = i_1 \lt i_2 \lt \dots \lt i_k \lt i_{k+1} = N+1
.
For each
1 \leq n \leq k
, the
n
-th subsequence is formed by taking the
i_n
-th through
(i_{n+1} - 1)
-th elements of
A
, maintaining their order.
Here are some examples of divisions for
A = (1, 2, 3, 4, 5)
:
(1, 2, 3), (4), (5)
(1, 2), (3, 4, 5)
(1, 2, 3, 4, 5) | [
{
"input": "3 3\n1 2 3\n",
"output": "2\n"
},
{
"input": "5 0\n0 0 0 0 0\n",
"output": "0\n"
},
{
"input": "10 5\n-5 -1 -7 6 -6 -2 -5 10 2 -10\n",
"output": "428\n"
}
] |
https://atcoder.jp/contests/abc370/tasks/abc370_f | Problem Statement
There is a circular cake divided into
N
pieces by cut lines. Each cut line is a line segment connecting the center of the circle to a point on the arc.
The pieces and cut lines are numbered
1, 2, \ldots, N
in clockwise order, and piece
i
has a mass of
A_i
. Piece
1
is also called piece
N + 1
.
Cut line
i
is between pieces
i
and
i + 1
, and they are arranged clockwise in this order: piece
1
, cut line
1
, piece
2
, cut line
2
,
\ldots
, piece
N
, cut line
N
.
We want to divide this cake among
K
people under the following conditions. Let
w_i
be the sum of the masses of the pieces received by the
i
-th person.
Each person receives one or more
consecutive
pieces.
There are no pieces that no one receives.
Under the above two conditions,
\min(w_1, w_2, \ldots, w_K)
is maximized.
Find the value of
\min(w_1, w_2, \ldots, w_K)
in a division that satisfies the conditions, and the number of cut lines that are never cut in the divisions that satisfy the conditions. Here, cut line
i
is considered cut if pieces
i
and
i + 1
are given to different people. | [
{
"input": "5 2\n3 6 8 6 4\n",
"output": "13 1\n"
},
{
"input": "6 3\n4 7 11 3 9 2\n",
"output": "11 1\n"
},
{
"input": "10 3\n2 9 8 1 7 9 1 3 5 8\n",
"output": "17 4\n"
}
] |
https://atcoder.jp/contests/abc370/tasks/abc370_g | Problem Statement
We call a positive integer
n
a good integer if and only if the sum of its positive divisors is divisible by
3
.
You are given two positive integers
N
and
M
. Find the number, modulo
998244353
, of length-
M
sequences
A
of positive integers such that the product of the elements in
A
is a good integer not exceeding
N
. | [
{
"input": "10 1\n",
"output": "5\n"
},
{
"input": "4 2\n",
"output": "2\n"
},
{
"input": "370 907\n",
"output": "221764640\n"
},
{
"input": "10000000000 100000\n",
"output": "447456146\n"
}
] |
https://atcoder.jp/contests/abc369/tasks/abc369_a | Problem Statement
You are given two integers
A
and
B
.
How many integers
x
satisfy the following condition?
Condition: It is possible to arrange the three integers
A
,
B
, and
x
in some order to form an arithmetic sequence.
A sequence of three integers
p
,
q
, and
r
in this order is an arithmetic sequence if and only if
q-p
is equal to
r-q
. | [
{
"input": "5 7\n",
"output": "3\n"
},
{
"input": "6 1\n",
"output": "2\n"
},
{
"input": "3 3\n",
"output": "1\n"
}
] |
https://atcoder.jp/contests/abc369/tasks/abc369_b | Problem Statement
Takahashi has a piano with
100
keys arranged in a row.
The
i
-th key from the left is called key
i
.
He will play music by pressing
N
keys one by one.
For the
i
-th press, he will press key
A_i
, using his left hand if
S_i=
L
, and his right hand if
S_i=
R
.
Before starting to play, he can place both of his hands on any keys he likes, and his
fatigue level
at this point is 0.
During the performance, if he moves one hand from key
x
to key
y
, the fatigue level increases by
|y-x|
(conversely, the fatigue level does not increase for any reason other than moving hands).
To press a certain key with a hand, that hand must be placed on that key.
Find the minimum possible fatigue level at the end of the performance. | [
{
"input": "4\n3 L\n6 R\n9 L\n1 R\n",
"output": "11\n"
},
{
"input": "3\n2 L\n2 L\n100 L\n",
"output": "98\n"
},
{
"input": "8\n22 L\n75 L\n26 R\n45 R\n72 R\n81 R\n47 L\n29 R\n",
"output": "188\n"
}
] |
https://atcoder.jp/contests/abc369/tasks/abc369_c | Problem Statement
You are given a sequence of
N
positive integers
A=(A_1,A_2,\dots,A_N)
.
Find the number of pairs of integers
(l,r)
satisfying
1\leq l\leq r\leq N
such that the subsequence
(A_l,A_{l+1},\dots,A_r)
forms an arithmetic progression.
A sequence
(x_1,x_2,\dots,x_{|x|})
is an arithmetic progression if and only if there exists a
d
such that
x_{i+1}-x_i=d\ (1\leq i < |x|)
.
In particular, a sequence of length
1
is always an arithmetic progression. | [
{
"input": "4\n3 6 9 3\n",
"output": "8\n"
},
{
"input": "5\n1 1 1 1 1\n",
"output": "15\n"
},
{
"input": "8\n87 42 64 86 72 58 44 30\n",
"output": "22\n"
}
] |
https://atcoder.jp/contests/abc369/tasks/abc369_d | Problem Statement
Takahashi will encounter
N
monsters in order. The
i
-th monster
(1\leq i\leq N)
has a strength of
A_i
.
For each monster, he can choose to either let it go or defeat it.
Each action awards him experience points as follows:
If he lets a monster go, he gains
0
experience points.
If he defeats a monster with strength
X
, he gains
X
experience points.
If it is an even-numbered defeated monster (2nd, 4th, ...), he gains an additional
X
experience points.
Find the maximum total experience points he can gain from the
N
monsters. | [
{
"input": "5\n1 5 3 2 7\n",
"output": "28\n"
},
{
"input": "2\n1000000000 1000000000\n",
"output": "3000000000\n"
}
] |
https://atcoder.jp/contests/abc369/tasks/abc369_e | Problem Statement
There are
N
islands and
M
bidirectional bridges connecting two islands. The islands and bridges are numbered
1
,
2
,
\ldots
,
N
and
1
,
2
,
\ldots
,
M
, respectively.
Bridge
i
connects islands
U_i
and
V_i
, and the time it takes to cross it in either direction is
T_i
.
No bridge connects an island to itself, but it is possible for two islands to be directly connected by more than one bridge.
One can travel between any two islands using some bridges.
You are given
Q
queries, so answer each of them. The
i
-th query is as follows:
You are given
K_i
distinct bridges: bridges
B_{i,1}, B_{i,2}, \ldots, B_{i,K_i}
.
Find the minimum time required to travel from island
1
to island
N
using each of these bridges at least once.
Only consider the time spent crossing bridges.
You can cross the given bridges in any order and in any direction. | [
{
"input": "3 5\n1 2 10\n1 3 20\n1 3 30\n2 3 15\n2 3 25\n2\n1\n1\n2\n3 5\n",
"output": "25\n70\n"
},
{
"input": "6 6\n1 5 1\n2 5 1\n2 4 1\n3 4 1\n3 6 1\n1 6 1\n2\n5\n1 2 3 4 5\n1\n5\n",
"output": "5\n3\n"
},
{
"input": "5 5\n1 2 1000000000\n2 3 1000000000\n3 4 1000000000\n4 5 1000000000\n1 5 1000000000\n1\n1\n3\n",
"output": "4000000000\n"
}
] |
https://atcoder.jp/contests/abc369/tasks/abc369_f | Problem Statement
There is a grid with
H
rows and
W
columns.
Let
(i,j)
denote the cell at the
i
-th row from the top and
j
-th column from the left.
There are
N
coins on this grid, and the
i
-th coin can be picked up by passing through the cell
(R_i,C_i)
.
Your goal is to start from cell
(1,1)
, repeatedly move either down or right by one cell, and reach cell
(H,W)
while picking up as many coins as possible.
Find the maximum number of coins you can pick up and one of the paths that achieves this maximum. | [
{
"input": "3 4 4\n3 3\n2 1\n2 3\n1 4\n",
"output": "3\nDRRDR\n"
},
{
"input": "2 2 2\n2 1\n1 2\n",
"output": "1\nDR\n"
},
{
"input": "10 15 8\n2 7\n2 9\n7 9\n10 3\n7 11\n8 12\n9 6\n8 1\n",
"output": "5\nDRRRRRRRRDDDDDRRDRDDRRR\n"
}
] |
https://atcoder.jp/contests/abc369/tasks/abc369_g | Problem Statement
You are given a tree with
N
vertices.
The vertices are numbered
1
,
2
,
\ldots
,
N
.
The
i
-th edge (
1\leq i\leq N-1
) connects vertices
U_i
and
V_i
, with a length of
L_i
.
For each
K=1,2,\ldots, N
, solve the following problem.
Takahashi and Aoki play a game. The game proceeds as follows.
First, Aoki specifies
K
distinct vertices on the tree.
Then, Takahashi constructs a walk that starts and ends at vertex
1
, and passes through all the vertices specified by Aoki.
The score is defined as the length of the walk constructed by Takahashi. Takahashi wants to minimize the score, while Aoki wants to maximize it.
Find the score when both players play optimally.
Definition of a walk
A walk on an undirected graph (possibly a tree) is a sequence of
k
vertices and
k-1
edges
v_1,e_1,v_2,\ldots,v_{k-1},e_{k-1},v_k
(where
k
is a positive integer)
such that edge
e_i
connects vertices
v_i
and
v_{i+1}
. The same vertex or edge can appear multiple times in the sequence.
A walk is said to pass through vertex
x
if there exists at least one
i
(
1\leq i\leq k
) such that
v_i=x
. (There can be multiple such
i
.)
The walk is said to start and end at
v_1
and
v_k
, respectively, and the length of the walk is the sum of the lengths of
e_1
,
e_2
,
\ldots
,
e_{k-1}
. | [
{
"input": "5\n1 2 3\n2 3 5\n2 4 2\n1 5 3\n",
"output": "16\n22\n26\n26\n26\n"
},
{
"input": "3\n1 2 1000000000\n2 3 1000000000\n",
"output": "4000000000\n4000000000\n4000000000\n"
}
] |
https://atcoder.jp/contests/arc183/tasks/arc183_a | Problem Statement
You are given positive integers
N
and
K
.
An integer sequence of length
NK
where each integer from
1
to
N
appears exactly
K
times is called a
good
integer sequence.
Let
S
be the number of good integer sequences.
Find the
\operatorname{floor}((S+1)/2)
-th good integer sequence in lexicographical order.
Here,
\operatorname{floor}(x)
represents the largest integer not exceeding
x
.
What is lexicographical order for sequences?
A sequence
S = (S_1,S_2,\ldots,S_{|S|})
is
lexicographically smaller
than a sequence
T = (T_1,T_2,\ldots,T_{|T|})
if either 1. or 2. below holds.
Here,
|S|
and
|T|
represent the lengths of
S
and
T
, respectively.
|S| \lt |T|
and
(S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|})
.
There exists an integer
1 \leq i \leq \min\lbrace |S|, |T| \rbrace
such that both of the following hold:
(S_1,S_2,\ldots,S_{i-1}) = (T_1,T_2,\ldots,T_{i-1})
S_i
is (numerically) smaller than
T_i
. | [
{
"input": "2 2\n",
"output": "1 2 2 1\n"
},
{
"input": "1 5\n",
"output": "1 1 1 1 1\n"
},
{
"input": "6 1\n",
"output": "3 6 5 4 2 1\n"
},
{
"input": "3 3\n",
"output": "2 2 2 1 3 3 3 1 1\n"
}
] |
https://atcoder.jp/contests/arc183/tasks/arc183_b | Problem Statement
You are given integer sequences of length
N
:
A=(A_1,A_2,\cdots,A_N)
and
B=(B_1,B_2,\cdots,B_N)
, and an integer
K
.
You can perform the following operation zero or more times.
Choose integers
i
and
j
(
1 \leq i,j \leq N
).
Here,
|i-j| \leq K
must hold.
Then, change the value of
A_i
to
A_j
.
Determine whether it is possible to make
A
identical to
B
.
There are
T
test cases for each input. | [
{
"input": "4\n3 1\n1 1 2\n1 2 2\n5 4\n2 4 5 1 3\n2 1 3 2 2\n13 1\n3 1 3 3 5 3 3 4 2 2 2 5 1\n5 3 3 3 4 2 2 2 2 5 5 1 3\n20 14\n10 6 6 19 13 16 15 15 2 10 2 16 9 12 2 6 13 5 5 9\n5 9 6 2 10 19 16 15 13 12 10 2 9 6 5 16 19 12 15 13\n",
"output": "Yes\nYes\nNo\nYes\n"
}
] |
https://atcoder.jp/contests/arc183/tasks/arc183_c | Problem Statement
Find the number, modulo
998244353
, of permutations
P=(P_1,P_2,\cdots,P_N)
of
(1,2,\cdots,N)
that satisfy all of the following
M
conditions.
The
i
-th condition: The maximum among
P_{L_i},P_{L_i+1},\cdots,P_{R_i}
is
not
P_{X_i}
.
Here,
L_i
,
R_i
, and
X_i
are integers given in the input. | [
{
"input": "3 2\n1 3 2\n1 2 1\n",
"output": "1\n"
},
{
"input": "5 1\n1 1 1\n",
"output": "0\n"
},
{
"input": "10 5\n3 8 4\n3 10 4\n1 7 2\n1 8 3\n3 8 7\n",
"output": "1598400\n"
},
{
"input": "15 17\n2 11 9\n2 15 13\n1 14 2\n5 11 5\n3 15 11\n1 6 2\n4 15 12\n3 11 6\n9 13 10\n2 14 6\n10 15 11\n1 8 6\n6 14 8\n2 10 2\n6 12 6\n3 14 12\n2 6 2\n",
"output": "921467228\n"
}
] |
https://atcoder.jp/contests/arc183/tasks/arc183_d | Problem Statement
There is a tree with
N
vertices numbered from
1
to
N
.
The
i
-th edge connects vertices
A_i
and
B_i
.
Here,
N
is even, and furthermore, this tree has a perfect matching.
Specifically, for each
i
(
1 \leq i \leq N/2
), it is guaranteed that
A_i=i \times 2-1
and
B_i=i \times 2
.
You will perform the following operation
N/2
times:
Choose two leaves (vertices with degree exactly
1
) and remove them from the tree.
Here, the tree after removal must still have a perfect matching.
In this problem, we consider a graph with zero vertices to be a tree as well.
For each operation, its score is defined as the distance between the two chosen vertices (the number of edges on the simple path connecting the two vertices).
Show one procedure that maximizes the total score.
It can be proved that there always exists a procedure to complete
N/2
operations under the constraints of this problem. | [
{
"input": "4\n1 2\n3 4\n2 3\n",
"output": "4 1\n2 3\n"
},
{
"input": "8\n1 2\n3 4\n5 6\n7 8\n2 3\n1 5\n1 7\n",
"output": "4 8\n7 6\n5 3\n2 1\n"
},
{
"input": "14\n1 2\n3 4\n5 6\n7 8\n9 10\n11 12\n13 14\n2 8\n4 11\n5 12\n7 13\n11 14\n9 13\n",
"output": "1 6\n5 2\n8 12\n3 7\n10 4\n11 9\n13 14\n"
},
{
"input": "20\n1 2\n3 4\n5 6\n7 8\n9 10\n11 12\n13 14\n15 16\n17 18\n19 20\n8 10\n16 18\n16 19\n5 9\n10 17\n2 13\n7 14\n3 7\n3 12\n",
"output": "6 1\n2 15\n20 13\n14 19\n16 4\n11 18\n17 12\n3 5\n9 7\n8 10\n"
}
] |
https://atcoder.jp/contests/arc183/tasks/arc183_e | Problem Statement
There is a rooted tree with
N
vertices numbered from
1
to
N
.
The root is vertex
1
, and the parent of vertex
i
(
2 \leq i \leq N
) is vertex
P_i
(
P_i<i
).
There are also integer sequences of length
M
:
A=(A_1,A_2,\cdots,A_M)
and
B=(B_1,B_2,\cdots,B_M)
, consisting of integers between
1
and
N
, inclusive.
A
is said to be
good
if and only if for each
i
, vertex
A_i
is an ancestor of vertex
B_i
or
A_i=B_i
.
Initially,
A
is good.
Consider the following operation on
A
.
Choose an integer
i
(
1 \leq i \leq M-1
) and swap the values of
A_i
and
A_{i+1}
.
Here,
A
must remain good after the operation.
Find the number, modulo
998244353
, of sequences that can result from performing this operation on
A
zero or more times. | [
{
"input": "3 3\n1 2\n1 2 1\n1 2 3\n",
"output": "2\n"
},
{
"input": "4 3\n1 1 1\n2 3 4\n2 3 4\n",
"output": "1\n"
},
{
"input": "8 13\n1 2 2 3 4 4 3\n5 3 2 5 4 6 2 8 2 6 7 4 7\n5 5 8 5 6 6 5 8 3 6 7 4 7\n",
"output": "8\n"
},
{
"input": "30 27\n1 2 1 1 5 1 7 1 5 10 1 12 12 13 15 16 12 18 19 18 21 21 23 13 18 18 27 27 13\n1 18 1 5 11 12 1 1 1 12 1 12 1 15 1 1 21 1 12 10 2 8 3 1 1 30 12\n14 27 30 5 11 17 1 18 24 27 29 27 19 15 28 5 21 21 29 11 2 8 3 4 10 30 22\n",
"output": "60\n"
}
] |
https://atcoder.jp/contests/arc183/tasks/arc183_f | Problem Statement
You are given positive integers
A
,
B
,
X
,
Y
, and
N
.
Here, the following is guaranteed:
A<B
\gcd(A,B)=1
1 \leq N \leq A+B-1
For an integer
n
, define
f(n)
as follows:
You start with an integer
x=0
.
f(n)
is the minimum total cost to achieve
x=n
by repeatedly performing the following operations.
Replace the value of
x
with
x+A
. The cost of this operation is
X
.
Replace the value of
x
with
x-A
. The cost of this operation is
X
.
Replace the value of
x
with
x+B
. The cost of this operation is
Y
.
Replace the value of
x
with
x-B
. The cost of this operation is
Y
.
It can be proved from the constraints on
A
and
B
that
f(n)
is defined for any integer
n
.
Find the value of
\sum_{1 \leq n \leq N} f(n)
, modulo
998244353
.
There are
T
test cases for each input. | [
{
"input": "4\n1 2 1 1 2\n3 5 2 4 6\n79 85 72 95 4\n80980429 110892168 22712439 520643153 66132787\n",
"output": "2\n34\n18111\n785776602\n"
}
] |
https://atcoder.jp/contests/abc368/tasks/abc368_a | Problem Statement
There is a stack of
N
cards, and the
i
-th card from the top has an integer
A_i
written on it.
You take
K
cards from the bottom of the stack and place them on top of the stack, maintaining their order.
Print the integers written on the cards from top to bottom after the operation. | [
{
"input": "5 3\n1 2 3 4 5\n",
"output": "3 4 5 1 2\n"
},
{
"input": "6 2\n1 2 1 2 1 2\n",
"output": "1 2 1 2 1 2\n"
}
] |
https://atcoder.jp/contests/abc368/tasks/abc368_b | Problem Statement
You are given a sequence of
N
positive integers
A = (A_1, A_2, \dots ,A_N)
. Takahashi repeats the following operation until
A
contains one or fewer positive elements:
Sort
A
in descending order. Then, decrease both
A_1
and
A_2
by
1
.
Find the number of times he performs this operation. | [
{
"input": "4\n1 2 3 3\n",
"output": "4\n"
},
{
"input": "3\n1 1 100\n",
"output": "2\n"
}
] |
https://atcoder.jp/contests/abc368/tasks/abc368_c | Problem Statement
You are playing a game.
There are
N
enemies lined up in a row, and the
i
-th enemy from the front has a health of
H_i
.
You will repeat the following action until the healths of all enemies become
0
or less, using a variable
T
initialized to
0
.
Increase
T
by
1
. Then, attack the frontmost enemy with health
1
or more. If
T
is a multiple of
3
, the enemy's health decreases by
3
; otherwise, it decreases by
1
.
Find the value of
T
when the healths of all enemies become
0
or less. | [
{
"input": "3\n6 2 2\n",
"output": "8\n"
},
{
"input": "9\n1 12 123 1234 12345 123456 1234567 12345678 123456789\n",
"output": "82304529\n"
},
{
"input": "5\n1000000000 1000000000 1000000000 1000000000 1000000000\n",
"output": "3000000000\n"
}
] |
https://atcoder.jp/contests/abc368/tasks/abc368_d | Problem Statement
You are given a tree with
N
vertices numbered
1
to
N
. The
i
-th edge connects vertices
A_i
and
B_i
.
Consider a tree that can be obtained by removing some (possibly zero) edges and vertices from this graph. Find the minimum number of vertices in such a tree that includes all of
K
specified vertices
V_1,\ldots,V_K
. | [
{
"input": "7 3\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n1 3 5\n",
"output": "4\n"
},
{
"input": "4 4\n3 1\n1 4\n2 1\n1 2 3 4\n",
"output": "4\n"
},
{
"input": "5 1\n1 4\n2 3\n5 2\n1 2\n1\n",
"output": "1\n"
}
] |
https://atcoder.jp/contests/abc368/tasks/abc368_e | Problem Statement
In the nation of Atcoder, there are
N
cities numbered
1
to
N
, and
M
trains numbered
1
to
M
.
Train
i
departs from city
A_i
at time
S_i
and arrives at city
B_i
at time
T_i
.
Given a positive integer
X_1
, find a way to set non-negative integers
X_2,\ldots,X_M
that satisfies the following condition with the minimum possible value of
X_2+\ldots+X_M
.
Condition: For all pairs
(i,j)
satisfying
1 \leq i,j \leq M
, if
B_i=A_j
and
T_i \leq S_j
, then
T_i+X_i \leq S_j+X_j
.
In other words, for any pair of trains that are originally possible to transfer between, it is still possible to transfer even after delaying the departure and arrival times of each train
i
by
X_i
.
It can be proved that such a way to set
X_2,\ldots,X_M
with the minimum possible value of
X_2+\ldots+X_M
is unique. | [
{
"input": "3 6 15\n1 2 10 20\n1 2 20 30\n2 3 25 40\n2 3 35 50\n3 1 15 30\n3 1 45 60\n",
"output": "0 10 0 0 5\n"
},
{
"input": "10 9 100\n1 10 0 1\n10 2 1 100\n10 3 1 100\n10 4 1 100\n10 5 1 100\n10 6 1 100\n10 7 1 100\n10 8 1 100\n10 9 1 100\n",
"output": "100 100 100 100 100 100 100 100\n"
},
{
"input": "4 4 10\n1 2 0 1\n1 2 0 10\n2 3 100 200\n2 4 100 200\n",
"output": "0 0 0\n"
}
] |
https://atcoder.jp/contests/abc368/tasks/abc368_f | Problem Statement
You are given a sequence of
N
positive integers
A = (A_1, A_2, \dots ,A_N)
, where each element is at least
2
. Anna and Bruno play a game using these integers. They take turns, with Anna going first, performing the following operation.
Choose an integer
i \ (1 \leq i \leq N)
freely. Then, freely choose a positive divisor
x
of
A_i
that is not
A_i
itself, and replace
A_i
with
x
.
The player who cannot perform the operation loses, and the other player wins. Determine who wins assuming both players play optimally for victory. | [
{
"input": "3\n2 3 4\n",
"output": "Anna\n"
},
{
"input": "4\n2 3 4 6\n",
"output": "Bruno\n"
}
] |
https://atcoder.jp/contests/abc368/tasks/abc368_g | Problem Statement
You are given sequences of positive integers
A
and
B
of length
N
. Process
Q
queries given in the following forms in the order they are given. Each query is of one of the following three types.
Type
1
: Given in the form
1 i x
. Replace
A_i
with
x
.
Type
2
: Given in the form
2 i x
. Replace
B_i
with
x
.
Type
3
: Given in the form
3 l r
. Solve the following problem and print the answer.
Initially, set
v = 0
. For
i = l, l+1, ..., r
in this order, replace
v
with either
v + A_i
or
v \times B_i
. Find the maximum possible value of
v
at the end.
It is guaranteed that the answers to the given type
3
queries are at most
10^{18}
. | [
{
"input": "3\n3 2 4\n1 2 2\n3\n3 1 3\n1 1 1\n3 1 3\n",
"output": "12\n7\n"
},
{
"input": "6\n65 32 12 5 8 312\n4 1 3 15 16 2\n6\n3 2 6\n3 1 5\n1 5 6\n2 4 9\n3 2 6\n3 3 5\n",
"output": "46080\n69840\n27648\n1728\n"
}
] |
https://atcoder.jp/contests/jsc2024-final/tasks/jsc2024_final_a | ๅ้กๆ
้ทใ
N
ใฎๆญฃๆดๆฐๅ
A=(A_1,A_2,\cdots,A_N)
ใไธใใใใพใ๏ผ
ไปฅ้๏ผ
S=\sum_{1 \leq i \leq N} A_i
ใจใใใพใ๏ผ
้ทใ
S
ใฎๆดๆฐๅ
x
ใฏไปฅไธใฎๆกไปถใใในใฆๆบใใใจใ๏ผใใใฆใใฎใจใใฎใฟ๏ผ
good
ใงใใใจ่จใใพใ๏ผ
x
ใฎๅ่ฆ็ด ใฏ
1
ไปฅไธ
N
ไปฅไธ
ๅๆดๆฐ
i
(
1 \leq i \leq N
) ใซใคใใฆ๏ผ
x
ใฏใกใใใฉ
A_i
ๅใฎ
i
ใๅซใ
ๆดๆฐๅ
x=(x_1,x_2,\cdots,x_S)
ใซๅฏพใ๏ผ
x_i>x_{i+1}
ใๆบใใ index
i
ใฎๅๆฐใ
f(x)
ใจ่กจใใใจใซใใพใ๏ผ
good ใชๆดๆฐๅ
x
ใไธๆงใฉใณใใ ใซใจใใจใใฎ
f(x)
ใฎๆๅพ
ๅคใ
\pmod{998244353}
ใงๆฑใใฆใใ ใใ๏ผ
ๆๅพ
ๅค
\pmod{998244353}
ใฎๅฎ็พฉ
ๆฑใใๆๅพ
ๅคใฏๅฟ
ใๆ็ๆฐใซใชใใใจใ่จผๆใงใใพใ๏ผ ใพใ๏ผใใฎๅ้กใฎๅถ็ดใฎใใจใงใฏ๏ผใใฎๅคใๆข็ดๅๆฐ
\frac{P}{Q}
ใง่กจใใๆ๏ผ
Q \neq 0 \pmod{998244353}
ใจใชใใใจใ่จผๆใงใใพใ๏ผ ใใฃใฆ๏ผ
R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353
ใๆบใใๆดๆฐ
R
ใไธๆใซๅฎใพใใพใ๏ผ ใใฎ
R
ใ็ญใใฆใใ ใใ๏ผ | [
{
"input": "2\n1 2\n",
"output": "665496236\n"
},
{
"input": "5\n1 1 1 1 1\n",
"output": "2\n"
},
{
"input": "2\n2024 824\n",
"output": "841217737\n"
},
{
"input": "10\n19237224 3435339 227368010 836006 153314679 115537801 189694444 76016030 159566203 53238564\n",
"output": "873203747\n"
}
] |
https://atcoder.jp/contests/jsc2024-final/tasks/jsc2024_final_b | ๅ้กๆ
ๆดๆฐ
N
ใไธใใใใพใ๏ผ
้ๅ
S
ใ
S=\{1,2,\cdots,N\}
ใงๅๆๅใใพใ๏ผ
ใใฎๅพ๏ผ
S
ใ็ฉบใซใชใใพใงๆฌกใฎๆไฝใ็นฐใ่ฟใใพใ๏ผ
S
ใฎ่ฆ็ด ใไธๆงใฉใณใใ ใซ
1
ใค้ธใณ๏ผใใใ
v
ใจใใ๏ผ
S
ใใ
v
ใฎๅๆฐใใในใฆๅ้คใใ๏ผ
ๆไฝๅๆฐใฎๆๅพ
ๅคใ
\pmod{998244353}
ใงๆฑใใฆใใ ใใ๏ผ
ๆๅพ
ๅค
\pmod{998244353}
ใฎๅฎ็พฉ
ๆฑใใๆๅพ
ๅคใฏๅฟ
ใๆ็ๆฐใซใชใใใจใ่จผๆใงใใพใ๏ผ ใพใ๏ผใใฎๅ้กใฎๅถ็ดใฎใใจใงใฏ๏ผใใฎๅคใๆข็ดๅๆฐ
\frac{P}{Q}
ใง่กจใใๆ๏ผ
Q \neq 0 \pmod{998244353}
ใจใชใใใจใ่จผๆใงใใพใ๏ผ ใใฃใฆ๏ผ
R \times Q \equiv P \pmod{998244353}, 0 \leq R < 998244353
ใๆบใใๆดๆฐ
R
ใไธๆใซๅฎใพใใพใ๏ผ ใใฎ
R
ใ็ญใใฆใใ ใใ๏ผ | [
{
"input": "2\n",
"output": "499122178\n"
},
{
"input": "6\n",
"output": "582309209\n"
},
{
"input": "23\n",
"output": "515759591\n"
},
{
"input": "5000000000\n",
"output": "64399530\n"
}
] |
https://atcoder.jp/contests/jsc2024-final/tasks/jsc2024_final_c | ๅ้กๆ
้ทใ
N
ใฎๆดๆฐๅ
A=(A_1,A_2,\cdots,A_N)
ใจๆดๆฐ
K
ใไธใใใใพใ๏ผ
ใใชใใฏ
A
ใฎใณใใผใ
K
ๅไธฆในใฆ้ฃ็ตใ๏ผ้ทใ
NK
ใฎๆดๆฐๅ
x=(x_1,x_2,\cdots,x_{NK})
ใๅพใพใใ๏ผ
ใใชใใฏใใใใๆฌกใฎๆไฝใใกใใใฉ
1
ๅ่กใใพใ๏ผ
x
ใฎ๏ผ้ฃ็ถใจใฏ้ใใชใ๏ผ๏ผ็ฉบใงใใใ๏ผ้จๅๅใ้ธใณ๏ผใใฎ่ฆ็ด ใ้้ ใซไธฆในๆฟใใ๏ผ
ใใๆญฃ็ขบใซ่ฟฐในใใฐ๏ผindex ใฎๅ
1 \leq i_1<i_2<\cdots<i_s \leq NK
(ๅใฎ้ทใ
s
ใไปปๆ) ใ้ธใณ๏ผๅ
1 \leq j \leq s
ใซๅฏพใใฆ
x_{i_j}
ใฎๅคใ
x_{i_{s-j+1}}
ใง็ฝฎใๆใใ๏ผ
ใใฎ็ฝฎใๆใใฏใในใฆๅๆใซ่กใใใ๏ผ
ๆไฝๅพใฎ
x
ใจใใฆใใใใๆฐๅใฎๅๆฐใ
998244353
ใงๅฒใฃใใใพใใๆฑใใฆใใ ใใ๏ผ | [
{
"input": "2 2\n1 2\n",
"output": "6\n"
},
{
"input": "3 2\n3 3 3\n",
"output": "1\n"
},
{
"input": "5 3\n1 2 3 4 5\n",
"output": "9216\n"
},
{
"input": "30 1000000000000\n8 3 8 10 1 5 3 1 6 4 3 6 2 6 6 9 5 5 8 3 3 4 10 2 3 2 8 10 8 1\n",
"output": "401626004\n"
}
] |
https://atcoder.jp/contests/jsc2024-final/tasks/jsc2024_final_d | ๅ้กๆ
ๆฐๅ
a=(a_1,a_2,\cdots,a_k)
ใซๅฏพใ๏ผ
f(a)
ใไปฅไธใฎใใใซๅฎใใพใ๏ผ
f(a)=\sum_{1 \leq i \leq k} \min(a_1,a_2,\cdots,a_i)
้ทใ
N
ใฎๆดๆฐๅ
A=(A_1,A_2,\cdots,A_N)
ใไธใใใใพใ๏ผ
ๆดๆฐๅ
A
ใ
2
ใคใฎ๏ผ้ฃ็ถใจใฏ้ใใชใ๏ผ๏ผ็ฉบใงใใใ๏ผ้จๅๅใซๅ่งฃใ๏ผใใใใ
X_1,X_2
ใจใใใพใ๏ผ
ๅ่งฃใฎ้๏ผ
A
ใฎๅ่ฆ็ด ใใกใใใฉ
1
ใคใฎ้จๅๅใซๅซใพใใใใใซใใพใ๏ผ
f(X_1)+f(X_2)
ใจใใฆใใใใๆๅคงๅคใๆฑใใฆใใ ใใ๏ผ | [
{
"input": "6\n5 3 6 1 4 7\n",
"output": "23\n"
},
{
"input": "10\n22 7 20 26 1 11 7 1 15 10\n",
"output": "104\n"
},
{
"input": "15\n140039457 164832983 239276621 245532751 274195604 320064892 499774361 419370025 249168130 521915711 744280520 561032101 857077284 543856068 910500852\n",
"output": "4618736183\n"
},
{
"input": "20\n596982638 713892371 987307025 722757772 759622047 853214705 628902494 623807668 755031424 486834509 272961036 423817262 434140395 284114341 88860862 222135106 69837030 210989591 225447688 86416231\n",
"output": "9047041486\n"
}
] |
https://atcoder.jp/contests/jsc2024-final/tasks/jsc2024_final_e | ๅ้กๆ
1
ใใ
N
ใพใงใฎ็ชๅทใฎใคใใ
N
้ ็นใใใชใๆ นไปใๆจ
T
ใใใใพใ๏ผ
ๆ นใฏ้ ็น
1
ใง๏ผ้ ็น
i
(
2 \leq i \leq N
) ใฎ่ฆชใฏ้ ็น
P_i
(
P_i<i
) ใงใ๏ผ
ใใฎๆจใฎ่พบใฏๆๅ่พบใง๏ผๅ่พบใฏ่ฆชใใๅญใฎๆนๅใธๅใใฆใใพใ๏ผ
ใใชใใฏ
T
ใซ
M
ๆฌใฎ่พบใ่ฟฝๅ ใ๏ผๆๅใฐใฉใ
G
ใไฝใใพใใ๏ผ
i
็ช็ฎใซ่ฟฝๅ ใใ่พบใฏ๏ผ้ ็น
A_i
ใใ้ ็น
B_i
ใธๅใใ่พบใงใใ๏ผ
ใใใง๏ผ
A_i,B_i
ใฏไปฅไธใฎๆกไปถใๆบใใใใจใไฟ่จผใใใพใ๏ผ
1 \leq B_i<A_i \leq N
T
ใซใใใฆ้ ็น
A_i
ใฏ้ ็น
B_i
ใฎๅญๅญซใงใใ
T
ใซใใใฆ้ ็น
A_i
ใฏ่ใงใชใ
ใชใ๏ผๅใ่พบใ่คๆฐๆฌ่ฟฝๅ ใใใใจใใใใใพใ๏ผ
ใใชใใฏใใใใ
G
ใฎ้ ็น
1
ใใในใฟใผใใ๏ผๅๆญขใใใพใงๆฌกใฎๆไฝใ็นฐใ่ฟใใพใ๏ผ
ไปใใ้ ็นใใๅบใ่พบใๅญๅจใใชใใชใๅๆญขใใ๏ผ
ไปใใ้ ็นใใๅบใ่พบใๅญๅจใใใชใ๏ผใใฎใใกใฎ
1
ใคใไธๆงใฉใณใใ ใซ้ธใณ๏ผใใฎ่พบใๅใใ้ ็นใธ็งปๅใใ๏ผ
ๅๆญขใใใพใงใซ็งปๅใใๅๆฐใฎๆๅพ
ๅคใ
\pmod{P}
ใงๆฑใใฆใใ ใใ๏ผ
ใใ ใใใฎๅ้กใงใฏ
P
ใฎๅคใฏๅ
ฅๅใงไธใใใใพใใ๏ผ
ใใฎใใใ๏ผไปฅไธใฎๅถ็ดใใในใฆๆบใใใใใช
P
ใใใชใใ้ธใณ๏ผใใฎ
P
ใซๅฏพใใฆๅ้กใ่งฃใใฆใใ ใใ๏ผ
P
ใฏ็ด ๆฐ
998244353 \leq P < 2 \times 10^9
\pmod{P}
ใง็ญใใๅฎ็พฉใงใใ
ๆๅพ
ๅค
\pmod{P}
ใฎๅฎ็พฉ
ๆฑใใๆๅพ
ๅคใฏๅฟ
ใๆ็ๆฐใซใชใใใจใ่จผๆใงใใพใ๏ผ ใใฎๅคใๆข็ดๅๆฐ
\frac{P}{Q}
ใง่กจใใๆ๏ผ
Q \neq 0 \pmod{P}
ใจใชใฃใฆใใใฐใใฎๅคใฏ
\pmod{P}
ใงๅฎ็พฉใงใใใจ่จใ๏ผ
Q = 0 \pmod{P}
ใชใใฐๅฎ็พฉใงใใชใใจ่จใใพใ๏ผๅคใๅฎ็พฉใงใใๅ ดๅ๏ผ
R \times Q \equiv P \pmod{P}, 0 \leq R < P
ใๆบใใๆดๆฐ
R
ใไธๆใซๅฎใพใใพใ๏ผ ใใฎ
R
ใ็ญใใฆใใ ใใ๏ผ
ใใฎๅ้กใฎใธใฃใใธใซใคใใฆ
ใธใฃใใธใฏใพใ๏ผใใชใใฎ้ธใใ
P
ใๆๅใฎ
2
ใคใฎๆกไปถใๆบใใใใฉใใใใงใใฏใใพใ๏ผ
ๆกไปถใๆบใใใชใๅ ดๅใฏ๏ผใใฎใในใใฑใผในใฏไธๆญฃ่งฃใจๅคๅฎใใพใ๏ผ
ๆกไปถใๆบใใๅ ดๅ๏ผใธใฃใใธใฏใใฎ
P
ใซๅฏพใใฆๅ้กใ่งฃใใใญใฐใฉใ ใๅฎ่กใใพใ๏ผ
ใธใฃใใธใ็ญใ
\pmod{P}
ใฎๅคใ่จ็ฎใงใใๅ ดๅ๏ผใใใใใชใใฎๅบๅใจๆฏ่ผใใฆๆญฃ่ชคๅคๅฎใ่กใใพใ๏ผ
ใธใฃใใธใ็ญใ
\pmod{P}
ใ่จ็ฎใงใใชใใ๏ผ ็ญใใ
\pmod{P}
ใงๅฎ็พฉใงใใชใใจๅคๆญใงใใๅ ดๅ๏ผใใฎใในใใฑใผในใฏไธๆญฃ่งฃใจๅคๅฎใใพใ๏ผ
ใธใฃใใธใ็ญใ
\pmod{P}
ใ่จ็ฎใงใใ๏ผใใค ็ญใใ
\pmod{P}
ใงๅฎ็พฉใงใใใๅฆใใๅคๆญใงใใชใๅ ดๅใฏ๏ผใใฎใในใใฑใผในใฏๆญฃ่งฃใจๅคๅฎใใพใ๏ผ
ใชใ๏ผใใฎๅ้กใฎๅถ็ดไธใง๏ผๆๅใฎ
2
ใคใฎๆกไปถใๆบใใ
P
ใฎใใก
99.9
% ไปฅไธใงๆๅพ
ๅคใๅฎ็พฉใงใ๏ผใใคใธใฃใใธใใใใๆญฃใใ่จ็ฎใงใใใใจใ่จผๆใงใใพใ๏ผ | [
{
"input": "3 0\n1 2\n",
"output": "2 998244353\n"
},
{
"input": "3 1\n1 2\n2 1\n",
"output": "4 998244353\n"
},
{
"input": "7 5\n1 2 2 4 4 3\n3 1\n2 1\n3 2\n4 2\n3 2\n",
"output": "453747441 998244353\n"
},
{
"input": "20 25\n1 2 3 2 5 2 6 1 9 2 2 12 13 1 2 16 17 16 19\n13 12\n12 1\n17 1\n6 5\n5 1\n13 12\n12 2\n17 16\n17 16\n17 16\n19 16\n6 5\n16 2\n6 5\n9 1\n9 1\n13 12\n13 12\n6 5\n19 16\n6 5\n17 16\n6 5\n13 1\n13 12\n",
"output": "529789893 998244353\n"
}
] |
https://atcoder.jp/contests/jsc2024-final/tasks/jsc2024_final_f | ๅ้กๆ
1
ใใ
N
ใพใงใฎ็ชๅทใใคใใ
N
้ ็นใใใชใ DAG ใไธใใใใพใ๏ผ
่พบใฏ
M
ๆฌใใ๏ผ
i
็ช็ฎใฎ่พบใฏ้ ็น
U_i
ใใ้ ็น
V_i
ใซๅใใ่พบใงใ๏ผ
ใใใง๏ผ
U_i<V_i
ใไฟ่จผใใใพใ๏ผ
ๅ้ ็นใซใฏๆดๆฐใๆธใใใฆใใ๏ผ้ ็น
i
ใซใฏๆดๆฐ
A_i
ใๆธใใใฆใใพใ๏ผ
้ ็นใฎ้จๅ้ๅ
s
ใซๅฏพใ๏ผ
f(s)
ใไปฅไธใฎใใใซๅฎใใพใ๏ผ
f(s)=\sum_{v \in s} \min\{A_u\ |\ u \in s
ใใค DAG ใซใใใฆ้ ็น
u
ใใ้ ็น
v
ใธ็งปๅใงใใ
\}
ๅๆดๆฐ
K=1,2,\cdots,N
ใซใคใใฆ๏ผไปฅไธใฎๅ้กใซ็ญใใฆใใ ใใ๏ผ
้ ็น้ๅ
\{1,2,\cdots,N\}
ใ
K
ๅใฎ (็ฉบใงใใใ) ้จๅ้ๅใซๅ่งฃใ๏ผใใใใ
X_1,X_2,\cdots,X_K
ใจใใใพใ๏ผ
ๅ่งฃใฎ้๏ผๅ้ ็นใใกใใใฉ
1
ใคใฎ้จๅ้ๅใซๅซใพใใใใใซใใพใ๏ผ
\sum_{1 \leq i \leq K}f(X_i)
ใจใใฆใใใใๆๅคงๅคใๆฑใใฆใใ ใใ๏ผ | [
{
"input": "4 4\n1 2 3 4\n1 2\n1 3\n2 4\n3 4\n",
"output": "4 8 10 10\n"
},
{
"input": "10 9\n22 7 20 26 1 11 7 1 15 10\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n",
"output": "49 104 120 120 120 120 120 120 120 120\n"
},
{
"input": "15 13\n20 12 14 20 22 20 26 34 40 42 44 60 45 69 73\n1 14\n2 5\n2 8\n2 9\n2 12\n2 15\n4 7\n6 12\n7 9\n8 15\n10 15\n11 12\n13 14\n",
"output": "317 513 541 541 541 541 541 541 541 541 541 541 541 541 541\n"
},
{
"input": "20 31\n54 12 12 26 7 6 38 51 29 52 70 49 67 65 80 67 54 77 74 73\n1 2\n2 3\n3 4\n1 5\n2 6\n5 6\n3 7\n6 7\n4 8\n7 8\n5 9\n6 10\n9 10\n7 11\n10 11\n8 12\n11 12\n9 13\n10 14\n13 14\n11 15\n14 15\n12 16\n15 16\n13 17\n14 18\n17 18\n15 19\n18 19\n16 20\n19 20\n",
"output": "190 749 872 935 961 963 963 963 963 963 963 963 963 963 963 963 963 963 963 963\n"
}
] |
https://atcoder.jp/contests/ahc036/tasks/ahc036_a | Problem Statement
Country Y has
N
cities and
M
roads, where the
i
-th road connects city
u_i
and city
v_i
bidirectionally. None of the roads intersect, and the graph consisting of cities and roads is a planar graph.
Each city has a
traffic signal
to control the traffic entering the city.
The signals have two states:
red
and
green
. You can move to city
v
only if the signal in city
v
is green. (The signal in the city you are moving from can be in either state.)
The traffic signals of the cities in Country Y are managed by arrays
A
and
B
.
Array
A
has a length of
L_A
, and each element is an integer between
0
and
N-1
, inclusive.
Array
B
has a length of
L_B
, and initially, all elements are
-1
.
Specifically, array
B
represents the state of the traffic signals. If array
B
contains the value
v
, then the signal in city
v
is green. If array
B
does not contain the value
v
, then the signal in city
v
is red.
Array
A
is used to control array
B
. By performing the following operations, you can use the values in array
A
to change the contents of array
B
and thus change the state of the signals:
Select a contiguous subarray from
A
and call it
R_A
.
Select a contiguous subarray from
B
of the same length as
R_A
and call it
R_B
.
Overwrite the contents of
R_B
with the contents of
R_A
.
Travel enthusiast X is planning a trip through Country Y.
X has a list of city vertex numbers
t_0, t_1, \ldots, t_{T-1}
that he wants to visit in order. The same city may appear multiple times in the list, but the same city does not appear consecutively.
X starts the journey from city
0
. Initially, X asks his friend, the Minister of Transport of Country Y, to set the contents of array
A
to any desired values. After this, the contents of array
A
cannot be changed.
Then, X can repeat the following
signal operations
or
movements
up to
10^5
times in any order. See the "Sample Output" section below for specific examples of signal operation and movement.
Signal Operation
Ask the Minister of Transport of Country Y to perform the signal operation described above once, changing the state of the signals.
Movement
Move from the current city to one of the cities directly connected by a road, and update the current city to the destination city. The signal in the destination city must be green.
If X can visit all the cities in the travel plan in the order listed, the trip is successful.
More precisely, if there exists a strictly increasing sequence of non-negative integers
(i_0, i_1, \ldots, i_{T-1})
of length
T
such that
c_{i_j} = t_j (0 \le j \le T - 1)
, where the sequence of cities visited by X is
c_0 = 0, c_1, \ldots
, then the trip is successful.
The Minister of Transport of Country Y is very busy, so X wants to minimize the number of signal operations.
Please determine the elements of array
A
, and perform signal operations and movements in such a way that the trip is successful with the minimum number of signal operations. | [
{
"input": "7 9 3 7 4\n0 1\n0 2\n0 3\n1 2\n2 3\n3 4\n4 5\n5 6\n6 0\n4 1 5\n100 0\n200 0\n200 100\n100 100\n0 200\n0 100\n0 0\n",
"output": "0 1 3 4 5 6 2\ns 4 0 0\nm 3\nm 4\nm 3\nm 0\nm 1\ns 2 4 2\nm 0\nm 6\nm 5\n"
}
] |
https://atcoder.jp/contests/agc067/tasks/agc067_a | Problem Statement
You are given a simple undirected graph
G
with
N
vertices numbered
1
to
N
.
G
has
M
edges and the
i
-th edge connects vertices
A_i
and
B_i
.
Check if
G
satisfies the following condition:
For every subset
X
of the vertex set
\{1,2,\cdots,N\}
, there exists a subset
Y
of
X
such that
|Y|\ge \frac{|X|}{2}
and
Y
forms a clique.
You have
T
testcases to solve. | [
{
"input": "4\n3 3\n1 2\n1 3\n2 3\n3 2\n1 2\n1 3\n3 1\n1 2\n3 0\n",
"output": "Yes\nYes\nYes\nNo\n"
}
] |
https://atcoder.jp/contests/agc067/tasks/agc067_b | Problem Statement
You have an integer sequence
a=(a_1,a_2,\cdots,a_N)
of length
N
.
All elements are initially
0
.
You are given an integer
C
and
M
intervals
([L_1,R_1],[L_2,R_2],\cdots,[L_M,R_M])
.
You will choose a permutation
p
of
(1,2,\cdots,M)
and an integer sequence
w=(w_1,w_2,\cdots,w_M)
of length
M
.
Here,
1\le w_i\le C
must hold.
Then, you will do
M
modifications.
The
i
-th modification is the following:
Change
a_{L_{p_i}}, \cdots, a_{R_{p_i}}
to
w_i
.
It is guaranteed that every position in
a
is covered by at least one interval.
Find the number of achievable
a
after all modifications.
Print the answer modulo
998244353
. | [
{
"input": "5 5 2\n1 3\n2 2\n3 3\n1 5\n3 5\n",
"output": "16\n"
},
{
"input": "20 30 20\n1 14\n1 7\n1 16\n3 13\n1 17\n4 8\n2 11\n4 12\n9 14\n3 15\n11 19\n1 13\n4 15\n8 19\n3 17\n15 18\n10 18\n1 18\n17 19\n16 20\n1 8\n8 15\n13 17\n1 19\n13 19\n1 20\n6 13\n10 12\n11 20\n17 18\n",
"output": "258066445\n"
}
] |
https://atcoder.jp/contests/agc067/tasks/agc067_c | Problem Statement
We call an infinite sequence of positive integers
(a_1,a_2,\cdots)
good
if and only if it satisfies both of the following conditions:
There exists a finite constant
C
such that
a_n \le C \cdot n
for all
1 \le n
;
For all pairs of positive integers
(n,m)
,
a_n \mid a_m
if and only if
n\mid m
. Here,
x \mid y
means
x
divides
y
.
You are given a positive integer sequence
A=(A_1,A_2,\cdots,A_N)
of length
N
.
Check if there exists a good infinite sequence starting with
(A_1,A_2,\cdots,A_N)
.
You have
T
testcases to solve. | [
{
"input": "5\n5\n1 2 3 4 5\n5\n1 4 9 16 25\n5\n1 4 6 8 10\n5\n1 2 4 4 5\n5\n1 2 3 5 4\n",
"output": "Yes\nYes\nYes\nNo\nNo\n"
}
] |
https://atcoder.jp/contests/agc067/tasks/agc067_d | Problem Statement
You are given an integer
N
and prime
P
.
We call a sequence of
N
intervals
([L_1,R_1] ,[L_2,R_2], \cdots, [L_N,R_N])
good
if and only if both of the followings are satisfied:
1\le L_i\le R_i\le N
holds for all
1\le i\le N
.
There exists a
unique
permutation
x=(x_1,x_2,\cdots,x_N)
of
(1,2,\cdots,N)
such that
L_i\le x_i\le R_i
holds for all
1\le i\le N
.
Count the number, modulo
P
, of
good
sequences of intervals. | [
{
"input": "2 1005488041\n",
"output": "6\n"
},
{
"input": "5 1005488041\n",
"output": "102960\n"
},
{
"input": "100 1005488041\n",
"output": "47599495\n"
},
{
"input": "1000 1005488041\n",
"output": "632708165\n"
}
] |
https://atcoder.jp/contests/agc067/tasks/agc067_e | Problem Statement
You are given an integer
N
and a prime
P
.
Count the number, modulo
P
, of undirected connected graphs
G
of
N
vertices numbered
1
to
N
that satisfy the following condition:
There are no self-loops in
G
. Note that multiple edges are allowed.
For all edges
(u,v)
in
G
, if we delete
(u,v)
from
G
,
G
remains connected. In other words,
G
is edge-biconnected.
For all edges
(u,v)
in
G
, if we delete
(u,v)
from
G
,
G
is no longer edge-biconnected.
Two graphs are considered different if and only if there exists a pair of distinct vertices
(u,v)
such that the numbers of edges connecting
u
and
v
in the two graphs are different. | [
{
"input": "2 1005976817\n",
"output": "1\n"
},
{
"input": "5 1000837403\n",
"output": "372\n"
},
{
"input": "10 1001160547\n",
"output": "789846604\n"
},
{
"input": "20 1006779551\n",
"output": "888612770\n"
}
] |
https://atcoder.jp/contests/abc367/tasks/abc367_a | Problem Statement
In the Kingdom of AtCoder, residents are required to shout their love for takoyaki at
A
o'clock every day.
Takahashi, who lives in the Kingdom of AtCoder, goes to bed at
B
o'clock and wakes up at
C
o'clock every day (in the
24
-hour clock). He can shout his love for takoyaki when he is awake, but cannot when he is asleep. Determine whether he can shout his love for takoyaki every day. Here, a day has
24
hours, and his sleeping time is less than
24
hours. | [
{
"input": "21 8 14\n",
"output": "Yes\n"
},
{
"input": "0 21 7\n",
"output": "No\n"
},
{
"input": "10 7 17\n",
"output": "No\n"
}
] |
https://atcoder.jp/contests/abc367/tasks/abc367_b | Problem Statement
A real number
X
is given to the third decimal place.
Print the real number
X
under the following conditions.
The decimal part must not have trailing
0
s.
There must not be an unnecessary trailing decimal point. | [
{
"input": "1.012\n",
"output": "1.012\n"
},
{
"input": "12.340\n",
"output": "12.34\n"
},
{
"input": "99.900\n",
"output": "99.9\n"
},
{
"input": "0.000\n",
"output": "0\n"
}
] |
https://atcoder.jp/contests/abc367/tasks/abc367_c | Problem Statement
Print all integer sequences of length
N
that satisfy the following conditions, in ascending lexicographical order.
The
i
-th element is between
1
and
R_i
, inclusive.
The sum of all elements is a multiple of
K
.
What is lexicographical order for sequences?
A sequence
A = (A_1, \ldots, A_{|A|})
is
lexicographically smaller
than
B = (B_1, \ldots, B_{|B|})
if either 1. or 2. below holds:
|A|<|B|
and
(A_{1},\ldots,A_{|A|}) = (B_1,\ldots,B_{|A|})
.
There exists an integer
1\leq i\leq \min\{|A|,|B|\}
such that both of the following are true:
(A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1})
A_i < B_i | [
{
"input": "3 2\n2 1 3\n",
"output": "1 1 2\n2 1 1\n2 1 3\n"
},
{
"input": "1 2\n1\n",
"output": "\n"
},
{
"input": "5 5\n2 3 2 3 2\n",
"output": "1 1 1 1 1\n1 2 2 3 2\n1 3 1 3 2\n1 3 2 2 2\n1 3 2 3 1\n2 1 2 3 2\n2 2 1 3 2\n2 2 2 2 2\n2 2 2 3 1\n2 3 1 2 2\n2 3 1 3 1\n2 3 2 1 2\n2 3 2 2 1\n"
}
] |
https://atcoder.jp/contests/abc367/tasks/abc367_d | Problem Statement
There are
N
rest areas around a lake.
The rest areas are numbered
1
,
2
, ...,
N
in clockwise order.
It takes
A_i
steps to walk clockwise from rest area
i
to rest area
i+1
(where rest area
N+1
refers to rest area
1
).
The minimum number of steps required to walk clockwise from rest area
s
to rest area
t
(
s \neq t
) is a multiple of
M
.
Find the number of possible pairs
(s,t)
. | [
{
"input": "4 3\n2 1 4 3\n",
"output": "4\n"
},
{
"input": "2 1000000\n1 1\n",
"output": "0\n"
},
{
"input": "9 5\n9 9 8 2 4 4 3 5 3\n",
"output": "11\n"
}
] |
https://atcoder.jp/contests/abc367/tasks/abc367_e | Problem Statement
You are given a sequence
X
of length
N
where each element is between
1
and
N
, inclusive, and a sequence
A
of length
N
.
Print the result of performing the following operation
K
times on
A
.
Replace
A
with
B
such that
B_i = A_{X_i}
. | [
{
"input": "7 3\n5 2 6 3 1 4 6\n1 2 3 5 7 9 11\n",
"output": "7 2 3 5 1 9 3\n"
},
{
"input": "4 0\n3 4 1 2\n4 3 2 1\n",
"output": "4 3 2 1\n"
},
{
"input": "9 1000000000000000000\n3 7 8 5 9 3 7 4 2\n9 9 8 2 4 4 3 5 3\n",
"output": "3 3 3 3 3 3 3 3 3\n"
}
] |
https://atcoder.jp/contests/abc367/tasks/abc367_f | Problem Statement
You are given sequences of positive integers of length
N
:
A=(A_1,A_2,\ldots,A_N)
and
B=(B_1,B_2,\ldots,B_N)
.
You are given
Q
queries to process in order. The
i
-th query is explained below.
You are given positive integers
l_i,r_i,L_i,R_i
. Print
Yes
if it is possible to rearrange the subsequence
(A_{l_i},A_{l_i+1},\ldots,A_{r_i})
to match the subsequence
(B_{L_i},B_{L_i+1},\ldots,B_{R_i})
, and
No
otherwise. | [
{
"input": "5 4\n1 2 3 2 4\n2 3 1 4 2\n1 3 1 3\n1 2 3 5\n1 4 2 5\n1 5 1 5\n",
"output": "Yes\nNo\nNo\nYes\n"
},
{
"input": "4 4\n4 4 4 4\n4 4 4 4\n1 2 2 3\n3 3 1 1\n1 3 1 4\n1 4 2 3\n",
"output": "Yes\nYes\nNo\nNo\n"
}
] |
https://atcoder.jp/contests/abc367/tasks/abc367_g | Problem Statement
You are given positive integers
N
,
M
,
K
, and a sequence of non-negative integers:
A=(A_1,A_2,\ldots,A_N)
.
For a non-empty non-negative integer sequence
B=(B_1,B_2,\ldots,B_{|B|})
, we define its
score
as follows.
If the length of
B
is a multiple of
M
:
(B_1 \oplus B_2 \oplus \dots \oplus B_{|B|})^K
Otherwise:
0
Here,
\oplus
represents the bitwise XOR.
Find the sum, modulo
998244353
, of the scores of the
2^N-1
non-empty subsequences of
A
.
What is bitwise XOR?
The bitwise XOR of non-negative integers
A
and
B
, denoted as
A \oplus B
, is defined as follows:
In the binary representation of
A \oplus B
, the digit at position
2^k
(
k \geq 0
) is
1
if exactly one of
A
and
B
has a
1
in that position in their binary representations, and
0
otherwise.
For example,
3 \oplus 5 = 6
(in binary:
011 \oplus 101 = 110
).
In general, the XOR 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)
, and it can be proved that this is independent of the order of
p_1, \dots, p_k
. | [
{
"input": "3 2 2\n1 2 3\n",
"output": "14\n"
},
{
"input": "10 5 3\n100 100 100 100 100 100 100 100 100 100\n",
"output": "252000000\n"
},
{
"input": "16 4 100\n7053 3876 3178 8422 7802 5998 2334 6757 6889 6637 7365 9495 7848 9026 7312 6558\n",
"output": "432440016\n"
}
] |
https://atcoder.jp/contests/arc182/tasks/arc182_a | Problem Statement
There is an integer sequence
S
of length
N
. Initially, all elements of
S
are
0
.
You are also given two integer sequences of length
Q
:
P=(P_1,P_2,\dots,P_Q)
and
V=(V_1,V_2,\dots,V_Q)
.
Snuke wants to perform
Q
operations on the sequence
S
in order. The
i
-th operation is as follows:
Perform one of the following:
Replace each of the elements
S_1, S_2, \dots, S_{P_i}
with
V_i
. However, before this operation, if there is an element among
S_1, S_2, \dots, S_{P_i}
that is strictly greater than
V_i
, Snuke will start crying.
Replace each of the elements
S_{P_i}, S_{P_i+1}, \dots, S_N
with
V_i
. However, before this operation, if there is an element among
S_{P_i}, S_{P_i+1}, \dots, S_N
that is strictly greater than
V_i
, Snuke will start crying.
Find the number of sequences of
Q
operations where Snuke can perform all operations without crying, modulo
998244353
.
Two sequences of operations are distinguished if and only if there is
1 \leq i \leq Q
such that the choice for the
i
-th operation is different. | [
{
"input": "8 3\n1 8\n8 1\n2 1\n",
"output": "1\n"
},
{
"input": "8 3\n8 1\n1 8\n1 2\n",
"output": "0\n"
},
{
"input": "241 82\n190 3207371\n229 3639088\n61 4428925\n84 17258698\n34 42692503\n207 59753183\n180 67198566\n78 99285033\n60 102449991\n234 122146510\n111 126959145\n141 152331579\n78 159855439\n11 169658471\n22 189991287\n37 204602946\n73 209329065\n72 215363269\n152 236450854\n175 237822921\n22 261431608\n144 252550201\n54 268889550\n238 276997357\n69 313065279\n226 330144323\n6 335788783\n126 345410019\n220 348318997\n166 365778763\n142 382251905\n200 406191336\n234 392702679\n83 409660987\n183 410908761\n142 445707116\n205 470279207\n230 486436406\n156 494269002\n113 495687706\n200 500005738\n162 505246499\n201 548652987\n86 449551554\n62 459527873\n32 574001635\n230 601073337\n175 610244315\n174 613857555\n181 637452273\n158 637866397\n148 648101378\n172 646898076\n144 682578257\n239 703460335\n192 713255331\n28 727075136\n196 730768166\n111 751850547\n90 762445737\n204 762552166\n72 773170159\n240 803415865\n32 798873367\n195 814999380\n72 842641864\n125 851815348\n116 858041919\n200 869948671\n195 873324903\n5 877767414\n105 877710280\n150 877719360\n9 884707717\n230 880263190\n88 967344715\n49 977643789\n167 979463984\n70 981400941\n114 991068035\n94 991951735\n141 995762200\n",
"output": "682155965\n"
}
] |
https://atcoder.jp/contests/arc182/tasks/arc182_b | Problem Statement
You are given positive integers
N
and
K
.
An integer sequence of length
N
where all elements are between
1
and
2^K - 1
, inclusive, is called a
good sequence
.
The
score
of a good sequence
A=(A_1,A_2,\ldots,A_N)
is defined as follows:
The number of distinct integers that can be expressed as
\displaystyle \left\lfloor\frac{A_i}{2^k} \right\rfloor
using an integer
i
between
1
and
N
, inclusive, and a non-negative integer
k
.
For example, for
A=(3,5)
, five integers can be expressed as
\displaystyle \left\lfloor\frac{A_i}{2^k} \right\rfloor
:
0
,
1
,
2
,
3
, and
5
, so the score is
5
.
Find one good sequence with the maximum score.
For each input file, you are given
T
test cases to solve. | [
{
"input": "3\n3 3\n7 2\n8 20\n",
"output": "5 6 7\n2 2 3 3 1 3 3\n662933 967505 876482 840117 1035841 651549 543175 781219\n"
}
] |
https://atcoder.jp/contests/arc182/tasks/arc182_c | Problem Statement
An integer sequence of length between
1
and
N
, inclusive, where each element is between
1
and
M
, inclusive, is called a
good sequence
.
The
score
of a good sequence is defined as the number of positive divisors of
X
, where
X
is the product of the elements in the sequence.
There are
\displaystyle \sum_{k=1}^{N}M^k
good sequences. Find the sum of the scores of all those sequences modulo
998244353
. | [
{
"input": "1 7\n",
"output": "16\n"
},
{
"input": "3 11\n",
"output": "16095\n"
},
{
"input": "81131 14\n",
"output": "182955659\n"
}
] |
https://atcoder.jp/contests/arc182/tasks/arc182_d | Problem Statement
An integer sequence where no two adjacent elements are the same is called a
good sequence
.
You are given two good sequences of length
N
:
A=(A_1,A_2,\dots,A_N)
and
B=(B_1,B_2,\dots,B_N)
. Each element of
A
and
B
is between
0
and
M-1
, inclusive.
You can perform the following operations on
A
any number of times, possibly zero:
Choose an integer
i
between
1
and
N
, inclusive, and perform one of the following:
Set
A_i \leftarrow (A_i + 1) \bmod M
.
Set
A_i \leftarrow (A_i - 1) \bmod M
. Here,
(-1) \bmod M = M - 1
.
However, you cannot perform an operation that makes
A
no longer a good sequence.
Determine if it is possible to make
A
equal to
B
, and if it is possible, find the minimum number of operations required to do so. | [
{
"input": "3 9\n2 0 1\n4 8 1\n",
"output": "3\n"
},
{
"input": "3 9\n1 8 2\n1 8 2\n",
"output": "0\n"
},
{
"input": "24 182\n128 115 133 52 166 92 164 119 143 99 54 162 86 2 59 166 24 78 81 5 109 67 172 99\n136 103 136 28 16 52 2 85 134 64 123 74 64 28 85 161 19 74 14 110 125 104 180 75\n",
"output": "811\n"
}
] |
https://atcoder.jp/contests/arc182/tasks/arc182_e | Problem Statement
You are given positive integers
N
,
M
,
K
, a non-negative integer
C
, and an integer sequence
A=(A_1, A_2, \ldots, A_N)
of length
N
.
Find
\displaystyle \sum_{k=0}^{K-1}\min_{1\le i\le N}\lbrace(Ck+A_i)\ \mathrm{mod}\ M \rbrace
. | [
{
"input": "2 5 3 3\n1 3\n",
"output": "4\n"
},
{
"input": "5 4 3 182\n0 3 2 1 2\n",
"output": "0\n"
},
{
"input": "5 718 651 193855\n3 532 44 109 58\n",
"output": "29484897\n"
}
] |
https://atcoder.jp/contests/arc182/tasks/arc182_f | Problem Statement
You are given integers
N
and
Q
, and two integer sequences of length
Q
:
A=(A_1,A_2,\ldots,A_Q)
and
B=(B_1,B_2,\ldots, B_Q)
.
For each
k=1,2,\ldots,Q
, solve the following problem:
There is an undirected graph with
N
vertices numbered from
0
to
N-1
and
N
edges. The
i
-th edge
(0 \le i < N)
connects vertices
i
and
(A_k \times i + B_k) \mod N
bidirectionally. Find the number of connected components in this undirected graph. | [
{
"input": "6 3\n2 1\n0 1\n1 0\n",
"output": "2\n1\n6\n"
},
{
"input": "11 3\n9 1\n5 3\n8 0\n",
"output": "3\n3\n2\n"
},
{
"input": "182 3\n61 2\n77 88\n180 55\n",
"output": "36\n14\n9\n"
}
] |
https://atcoder.jp/contests/abc366/tasks/abc366_a | Problem Statement
A mayoral election is being held in AtCoder City. The candidates are Takahashi and Aoki.
There are
N
valid votes cast for either of the two candidates, and the counting is currently underway. Here,
N
is an odd number.
The current vote count is
T
votes for Takahashi and
A
votes for Aoki.
Determine if the outcome of the election is already decided at this point. | [
{
"input": "7 4 2\n",
"output": "Yes\n"
},
{
"input": "99 12 48\n",
"output": "No\n"
},
{
"input": "1 0 0\n",
"output": "No\n"
}
] |
https://atcoder.jp/contests/abc366/tasks/abc366_b | Problem Statement
You are given a horizontally written text. Convert it to vertical writing, filling spaces with
*
.
You are given
N
strings
S_1, S_2, \dots, S_N
consisting of lowercase English letters. Let
M
be the maximum length of these strings.
Print
M
strings
T_1, T_2, \dots, T_M
that satisfy the following conditions:
Each
T_i
consists of lowercase English letters and
*
.
Each
T_i
does not end with
*
.
For each
1 \leq i \leq N
, the following holds:
For each
1 \leq j \leq |S_i|
, the
(N-i+1)
-th character of
T_j
exists, and the concatenation of the
(N-i+1)
-th characters of
T_1, T_2, \dots, T_{|S_i|}
in this order equals
S_i
.
For each
|S_i| + 1 \leq j \leq M
, the
(N-i+1)
-th character of
T_j
either does not exist or is
*
.
Here,
|S_i|
denotes the length of the string
S_i
. | [
{
"input": "3\nabc\nde\nfghi\n",
"output": "fda\ngeb\nh*c\ni\n"
},
{
"input": "3\natcoder\nbeginner\ncontest\n",
"output": "cba\noet\nngc\ntio\nend\nsne\nter\n*r\n"
}
] |
https://atcoder.jp/contests/abc366/tasks/abc366_c | Problem Statement
You have an empty bag.
You are given
Q
queries, which must be processed in order.
There are three types of queries.
1 x
: Put one ball with the integer
x
written on it into the bag.
2 x
: Remove one ball with the integer
x
written on it from the bag and discard it. It is guaranteed that the bag has a ball with the integer
x
written on it when this query is given.
3
: Print the number of different integers written on the balls in the bag. | [
{
"input": "8\n1 3\n1 1\n1 4\n3\n2 1\n3\n1 5\n3\n",
"output": "3\n2\n3\n"
},
{
"input": "8\n1 2\n1 2\n3\n2 2\n1 4\n1 4\n2 2\n3\n",
"output": "1\n1\n"
}
] |
https://atcoder.jp/contests/abc366/tasks/abc366_d | Problem Statement
You are given a positive integer
N
, and an integer
A_{x,y,z}
for each triple of integers
(x, y, z)
such that
1 \leq x, y, z \leq N
.
You will be given
Q
queries in the following format, which must be processed in order.
For the
i
-th query
(1 \leq i \leq Q)
, you are given a tuple of integers
(Lx_i, Rx_i, Ly_i, Ry_i, Lz_i, Rz_i)
such that
1 \leq Lx_i \leq Rx_i \leq N
,
1 \leq Ly_i \leq Ry_i \leq N
, and
1 \leq Lz_i \leq Rz_i \leq N
. Find:
\displaystyle{\sum_{x=Lx_i}^{Rx_i} \sum_{y=Ly_i}^{Ry_i} \sum_{z=Lz_i}^{Rz_i} A_{x,y,z}}
. | [
{
"input": "2\n1 2\n3 4\n5 6\n7 8\n2\n1 2 2 2 1 1\n2 2 1 2 1 2\n",
"output": "10\n26\n"
},
{
"input": "3\n733 857 714\n956 208 257\n123 719 648\n840 881 245\n245 112 746\n306 942 694\n58 870 849\n13 208 789\n687 906 783\n8\n3 3 3 3 1 1\n1 3 2 3 3 3\n2 2 2 3 1 1\n1 3 1 1 1 1\n2 3 2 3 2 3\n1 2 1 1 1 2\n3 3 2 2 1 3\n1 2 2 3 2 3\n",
"output": "687\n3917\n551\n1631\n5180\n3311\n1010\n4326\n"
}
] |
https://atcoder.jp/contests/abc366/tasks/abc366_e | Problem Statement
You are given
N
points
(x_1, y_1), (x_2, y_2), \dots, (x_N, y_N)
on a two-dimensional plane, and a non-negative integer
D
.
Find the number of integer pairs
(x, y)
such that
\displaystyle \sum_{i=1}^N (|x-x_i|+|y-y_i|) \leq D
. | [
{
"input": "2 3\n0 0\n1 0\n",
"output": "8\n"
},
{
"input": "2 0\n0 0\n2 0\n",
"output": "0\n"
},
{
"input": "6 100\n9 -6\n10 -1\n2 10\n-1 7\n-7 5\n-1 -4\n",
"output": "419\n"
}
] |
https://atcoder.jp/contests/abc366/tasks/abc366_f | Problem Statement
You are given
N
linear functions
f_1, f_2, \ldots, f_N
, where
f_i(x) = A_i x + B_i
.
Find the maximum possible value of
f_{p_1}(f_{p_2}(\ldots f_{p_K}(1) \ldots ))
for a sequence
p = (p_1, p_2, \ldots, p_K)
of
K
distinct
integers between
1
and
N
, inclusive. | [
{
"input": "3 2\n2 3\n1 5\n4 2\n",
"output": "26\n"
},
{
"input": "10 3\n48 40\n34 22\n24 37\n45 40\n48 31\n49 44\n45 40\n44 6\n35 22\n39 28\n",
"output": "216223\n"
}
] |
https://atcoder.jp/contests/abc366/tasks/abc366_g | 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
bidirectionally.
Determine if there exists a way to write an integer between
1
and
2^{60} - 1
, inclusive, on each vertex of this graph so that the following condition is satisfied:
For every vertex
v
with a degree of at least
1
, the total XOR of the numbers written on its adjacent vertices (excluding
v
itself) is
0
.
What is XOR?
The XOR of two non-negative integers
A
and
B
, denoted as
A \oplus B
, is defined as follows:
In the binary representation of
A \oplus B
, the bit at position
2^k \, (k \geq 0)
is
1
if and only if exactly one of the bits at position
2^k
in the binary representations of
A
and
B
is
1
. Otherwise, it is
0
.
For example,
3 \oplus 5 = 6
(in binary:
011 \oplus 101 = 110
).
In general, the bitwise XOR 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)
. It can be proved that this is independent of the order of
p_1, \dots, p_k
. | [
{
"input": "3 3\n1 2\n1 3\n2 3\n",
"output": "Yes\n4 4 4\n"
},
{
"input": "2 1\n1 2\n",
"output": "No\n"
},
{
"input": "1 0\n",
"output": "Yes\n1\n"
},
{
"input": "4 5\n1 2\n1 3\n2 3\n2 4\n3 4\n",
"output": "Yes\n12 4 4 8\n"
}
] |
https://atcoder.jp/contests/arc181/tasks/arc181_a | Problem Statement
You are given a permutation
P=(P_1,P_2,\dots,P_N)
of
(1,2,\dots,N)
.
You want to satisfy
P_i=i
for all
i=1,2,\dots,N
by performing the following operation zero or more times:
Choose an integer
k
such that
1 \leq k \leq N
. If
k \geq 2
, sort the
1
-st through
(k-1)
-th terms of
P
in ascending order. Then, if
k \leq N-1
, sort the
(k+1)
-th through
N
-th terms of
P
in ascending order.
It can be proved that under the constraints of this problem, it is possible to satisfy
P_i=i
for all
i=1,2,\dots,N
with a finite number of operations for any
P
. Find the minimum number of operations required.
You have
T
test cases to solve. | [
{
"input": "3\n5\n2 1 3 5 4\n3\n1 2 3\n7\n3 2 1 7 5 6 4\n",
"output": "1\n0\n2\n"
}
] |
https://atcoder.jp/contests/arc181/tasks/arc181_b | Problem Statement
For strings
S
and
T
consisting of lowercase English letters, and a string
X
consisting of
0
and
1
, define the string
f(S,T,X)
consisting of lowercase English letters as follows:
Starting with an empty string, for each
i=1,2,\dots,|X|
, append
S
to the end if the
i
-th character of
X
is
0
, and append
T
to the end if it is
1
.
You are given a string
S
consisting of lowercase English letters, and strings
X
and
Y
consisting of
0
and
1
.
Determine if there exists a string
T
(which can be empty) such that
f(S,T,X)=f(S,T,Y)
.
You have
t
test cases to solve. | [
{
"input": "3\naraara\n01\n111\naraaaa\n100100\n0010111\nabacabac\n0\n1111\n",
"output": "Yes\nNo\nNo\n"
},
{
"input": "2\nempty\n10101\n00\nempty\n11111\n111\n",
"output": "Yes\nYes\n"
}
] |
https://atcoder.jp/contests/arc181/tasks/arc181_c | Problem Statement
You are given two permutations
P=(P_1,P_2,\dots,P_N)
and
Q=(Q_1,Q_2,\dots,Q_N)
of
(1,2,\dots,N)
.
Write one of the characters
0
and
1
in each cell of an
N
-by-
N
grid so that all of the following conditions are satisfied:
Let
S_i
be the string obtained by concatenating the characters in the
i
-th row from the
1
-st to the
N
-th column. Then,
S_{P_1} < S_{P_2} < \dots < S_{P_N}
in lexicographical order.
Let
T_i
be the string obtained by concatenating the characters in the
i
-th column from the
1
-st to the
N
-th row. Then,
T_{Q_1} < T_{Q_2} < \dots < T_{Q_N}
in lexicographical order.
It can be proved that for any
P
and
Q
, there is at least one way to write the characters that satisfies all the conditions.
What does "
X < Y
in lexicographical order" mean?
For strings
X=X_1X_2\dots X_{|X|}
and
Y = Y_1Y_2\dots Y_{|Y|}
, "
X < Y
in lexicographical order
" means that 1. or 2. below holds.
Here,
|X|
and
|Y|
denote the lengths of
X
and
Y
, respectively.
|X| \lt |Y|
and
X_1X_2\ldots X_{|X|} = Y_1Y_2\ldots Y_{|X|}
.
There exists an integer
1 \leq i \leq \min\lbrace |X|, |Y| \rbrace
such that both of the following are true:
X_1X_2\ldots X_{i-1} = Y_1Y_2\ldots Y_{i-1}
X_i
is less than
Y_i
. | [
{
"input": "3\n1 2 3\n2 1 3\n",
"output": "001\n101\n110\n"
},
{
"input": "15\n8 15 10 2 4 3 1 13 5 12 9 6 14 11 7\n4 1 5 14 3 12 13 7 11 8 6 2 9 15 10\n",
"output": "010001111110101\n001000000101001\n010001001100010\n010000011110010\n010011101101101\n100101110100000\n111100011001000\n000001001100000\n100011011000101\n000111101011110\n101010101010101\n011010101011110\n010011000010011\n100110010110101\n000101101100100\n"
}
] |
https://atcoder.jp/contests/arc181/tasks/arc181_d | Problem Statement
You are given a permutation
P=(P_1,P_2,\dots,P_N)
of
(1,2,\dots,N)
.
Consider the following operations
k\ (k=2,3,\dots,N)
on this permutation.
Operation
k
: For
i=1,2,\dots,k-1
in this order, if
P_i > P_{i+1}
, swap the values of the
i
-th and
(i+1)
-th elements of
P
.
You are also given a
non-decreasing
sequence
A=(A_1,A_2,\dots,A_M)\ (2 \leq A_i \leq N)
of length
M
.
For each
i=1,2,\dots,M
, find the inversion number of
P
after applying the operations
A_1, A_2, \dots, A_i
in this order.
What is the inversion number of a sequence?
The inversion number of a sequence
x=(x_1,x_2,\dots,x_n)
of length
n
is the number of pairs of integers
(i,j)\ (1\leq i < j \leq n)
such that
x_i > x_j
. | [
{
"input": "6\n3 2 4 1 6 5\n2\n4 6\n",
"output": "3\n1\n"
},
{
"input": "20\n12 14 16 8 7 15 19 6 18 5 13 9 10 17 4 1 11 20 2 3\n15\n3 4 6 8 8 9 10 12 13 15 18 18 19 19 20\n",
"output": "117\n116\n113\n110\n108\n105\n103\n99\n94\n87\n79\n72\n65\n58\n51\n"
}
] |
https://atcoder.jp/contests/arc181/tasks/arc181_e | Problem Statement
An undirected graph with numbered vertices is called a
good graph
if it has a spanning tree
T
that satisfies the following condition. Here, an edge connecting two vertices
u
and
v
(u < v)
is denoted as edge
(u,v)
.
For every edge
(u,v)
(u < v)
in the graph, the minimum and maximum vertex numbers on the unique simple path connecting vertices
u
and
v
in
T
are
u
and
v
, respectively.
You are given a simple connected undirected graph
G
with
N
vertices numbered from
1
to
N
. The graph
G
has
M
edges, and the
i
-th edge connects vertices
A_i
and
B_i
(A_i < B_i)
.
For each
i=1,2,\dots,M
, determine whether the graph obtained by removing the
i
-th edge from
G
is a
good graph
. | [
{
"input": "6 9\n1 3\n1 5\n2 5\n2 6\n3 4\n3 5\n3 6\n4 6\n5 6\n",
"output": "No\nNo\nNo\nNo\nYes\nNo\nNo\nYes\nYes\n"
},
{
"input": "5 4\n1 2\n2 3\n3 4\n4 5\n",
"output": "No\nNo\nNo\nNo\n"
},
{
"input": "15 20\n12 13\n7 8\n5 7\n8 10\n9 12\n4 5\n11 12\n2 4\n6 8\n4 14\n1 2\n14 15\n2 9\n3 8\n2 15\n10 11\n13 14\n8 9\n7 14\n5 13\n",
"output": "No\nNo\nNo\nYes\nYes\nNo\nYes\nNo\nNo\nNo\nNo\nNo\nNo\nNo\nNo\nYes\nNo\nNo\nNo\nNo\n"
}
] |
https://atcoder.jp/contests/arc181/tasks/arc181_f | Problem Statement
You are given an integer sequence
A=(A_1,A_2,\dots,A_N)
of length
N
. On this sequence, the following operation can be performed:
Choose
l
and
r
(1\leq l < r \leq N)
such that
A_l=A_r
,
A_{l+1}=A_{l+2}=\dots=A_{r-1}
, and
A_{l+1}\neq A_l
. Replace each of
A_{l+1},A_{l+2},\dots,A_{r-1}
with
A_l
. The cost of this operation is
r-l-1
.
You will repeat this operation until there is no
l
and
r
(1\leq l < r \leq N)
such that
A_l=A_r
,
A_{l+1}=A_{l+2}=\dots=A_{r-1}
, and
A_{l+1}\neq A_l
. Find the minimum total cost of such a series of operations. | [
{
"input": "7\n1 2 3 2 3 2 1\n",
"output": "7\n"
},
{
"input": "5\n1 2 3 4 5\n",
"output": "0\n"
},
{
"input": "40\n1 2 3 4 5 6 7 8 7 6 5 6 7 8 7 6 5 4 3 2 2 1 2 3 4 5 4 5 6 7 8 7 7 6 5 6 6 7 8 8\n",
"output": "44\n"
}
] |
https://atcoder.jp/contests/abc365/tasks/abc365_a | Problem Statement
You are given an integer
Y
between
1583
and
2023
.
Find the number of days in the year
Y
of the Gregorian calendar.
Within the given range, the year
Y
has the following number of days:
if
Y
is not a multiple of
4
, then
365
days;
if
Y
is a multiple of
4
but not a multiple of
100
, then
366
days;
if
Y
is a multiple of
100
but not a multiple of
400
, then
365
days;
if
Y
is a multiple of
400
, then
366
days. | [
{
"input": "2023\n",
"output": "365\n"
},
{
"input": "1992\n",
"output": "366\n"
},
{
"input": "1800\n",
"output": "365\n"
},
{
"input": "1600\n",
"output": "366\n"
}
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.