AtCoder Grand Contest 036
AtCoder Grand Contest 036
jp/contests/agc036/tasks_print
A - Triangle
Problem Statement
Given is an integer S . Find a combination of six integers X1 , Y1 , X2 , Y2 , X3 , and Y3 that satisfies all of the
following conditions:
0 ≤ X1 , Y1 , X2 , Y2 , X3 , Y3 ≤ 109
The area of the triangle in a two-dimensional plane whose vertices are (X1 , Y1 ), (X2 , Y2 ), and
(X3 , Y3 ) is S/2.
We can prove that there always exist six integers that satisfy the conditions under the constraints of this
problem.
Constraints
1 ≤ S ≤ 1018
All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print six integers X1 , Y1 , X2 , Y2 , X3 , and Y3 that satisfy the conditions, in this order, with spaces in
between. If multiple solutions exist, any of them will be accepted.
Sample Input 1
3
Sample Output 1
1 0 2 2 0 1
The area of the triangle in a two-dimensional plane whose vertices are (1, 0), (2, 2), and (0, 1) is 3/2.
Printing ' 3 0 3 1 0 1 ' or ' 1 0 0 1 2 2 ' will also be accepted.
1 of 11 21/07/19, 20.57
AtCoder Grand Contest 036 https://atcoder.jp/contests/agc036/tasks_print
Sample Input 2
100
Sample Output 2
0 0 10 0 0 10
Sample Input 3
311114770564041497
Sample Output 3
314159265 358979323 846264338 327950288 419716939 937510582
B - Do Not Duplicate
Problem Statement
We have a sequence of N × K integers: X = (X0 , X1 , ⋯ , XN×K−1 ). Its elements are represented by
another sequence of N integers: A = (A0 , A1 , ⋯ , AN−1 ). For each pair i, j (
0 ≤ i ≤ K − 1, 0 ≤ j ≤ N − 1), Xi×N+j = Aj holds.
Snuke has an integer sequence s, which is initially empty. For each i = 0, 1, 2, ⋯ , N × K − 1, in this
order, he will perform the following operation:
Constraints
1 ≤ N ≤ 2 × 105
1 ≤ K ≤ 1012
1 ≤ Ai ≤ 2 × 105
All values in input are integers.
2 of 11 21/07/19, 20.57
AtCoder Grand Contest 036 https://atcoder.jp/contests/agc036/tasks_print
Input
Input is given from Standard Input in the following format:
N K
A0 A1 ⋯ AN−1
Output
Print the elements of s after Snuke finished the operations, in order from beginning to end, with spaces in
between.
Sample Input 1
3 2
1 2 3
Sample Output 1
2 3
Sample Input 2
5 10
1 2 3 2 3
Sample Output 2
3
3 of 11 21/07/19, 20.57
AtCoder Grand Contest 036 https://atcoder.jp/contests/agc036/tasks_print
Sample Input 3
6 1000000000000
1 1 2 2 3 3
Sample Output 3
Sample Input 4
11 97
3 1 4 1 5 9 2 6 5 3 5
Sample Output 4
9 2 6
C - GP 2
Problem Statement
We have a sequence of N integers: x = (x0 , x1 , ⋯ , xN−1 ). Initially, xi = 0 for each i (0 ≤ i ≤ N − 1).
Choose two distinct indices i, j (0 ≤ i, j ≤ N − 1, i ≠ j). Then, replace xi with xi + 2 and xj with
x j + 1.
Find the number of different sequences that can result after M operations. Since it can be enormous,
compute the count modulo 998244353.
Constraints
2 ≤ N ≤ 106
1 ≤ M ≤ 5 × 105
All values in input are integers.
4 of 11 21/07/19, 20.57
AtCoder Grand Contest 036 https://atcoder.jp/contests/agc036/tasks_print
Input
Input is given from Standard Input in the following format:
N M
Output
Print the number of different sequences that can result after M operations, modulo 998244353.
Sample Input 1
2 2
Sample Output 1
3
x = (2, 4)
x = (3, 3)
x = (4, 2)
For example, x = (3, 3) can result after the following sequence of operations:
Sample Input 2
3 2
Sample Output 2
19
Sample Input 3
10 10
Sample Output 3
211428932
5 of 11 21/07/19, 20.57
AtCoder Grand Contest 036 https://atcoder.jp/contests/agc036/tasks_print
Sample Input 4
100000 50000
Sample Output 4
3463133
D - Negative Cycle
Problem Statement
We have a weighted directed graph with N vertices numbered 0 to N − 1.
The graph initially has N − 1 edges. The i-th edge (0 ≤ i ≤ N − 2) is directed from Vertex i to Vertex
i + 1 and has a weight of 0.
Snuke will now add a new edge (i → j) for every pair i, j (0 ≤ i, j ≤ N − 1, i ≠ j). The weight of the edge
will be −1 if i < j, and 1 otherwise.
Ringo is a boy. A negative cycle (a cycle whose total weight is negative) in a graph makes him sad. He will
delete some of the edges added by Snuke so that the graph will no longer contain a negative cycle. The cost
of deleting the edge (i → j) is Ai,j . He cannot delete edges that have been present from the beginning.
Constraints
3 ≤ N ≤ 500
1 ≤ Ai,j ≤ 109
All values in input are integers.
6 of 11 21/07/19, 20.57
AtCoder Grand Contest 036 https://atcoder.jp/contests/agc036/tasks_print
Input
Input is given from Standard Input in the following format:
N
A0,1 A0,2 A0,3 ⋯ A0,N−1
A1,0 A1,2 A1,3 ⋯ A1,N−1
A2,0 A2,1 A2,3 ⋯ A2,N−1
⋮
AN−1,0 AN−1,1 AN−1,2 ⋯ AN−1,N−2
Output
Print the minimum total cost required to achieve Ringo's objective.
Sample Input 1
3
2 1
1 4
3 3
Sample Output 1
2
If we delete the edge (0 → 1) added by Snuke, the graph will no longer contain a negative cycle. The cost
will be 2 in this case, which is the minimum possible.
Sample Input 2
4
1 1 1
1 1 1
1 1 1
1 1 1
Sample Output 2
2
If we delete the edges (1 → 2) and (3 → 0) added by Snuke, the graph will no longer contain a negative
cycle. The cost will be 1 + 1 = 2 in this case, which is the minimum possible.
7 of 11 21/07/19, 20.57
AtCoder Grand Contest 036 https://atcoder.jp/contests/agc036/tasks_print
Sample Input 3
10
190587 2038070 142162180 88207341 215145790 38 2 5 20
32047998 21426 4177178 52 734621629 2596 102224223 5 1864
41 481241221 1518272 51 772 146 8805349 3243297 449
918151 126080576 5186563 46354 6646 491776 5750138 2897 161
3656 7551068 2919714 43035419 495 3408 26 3317 2698
455357 3 12 1857 5459 7870 4123856 2402 258
3 25700 16191 102120 971821039 52375 40449 20548149 16186673
2 16 130300357 18 6574485 29175 179 1693 2681
99 833 131 2 414045824 57357 56 302669472 95
8408 7 1266941 60620177 129747 41382505 38966 187 5151064
Sample Output 3
2280211
E - ABC String
Problem Statement
Given is a string S consisting of ' A ',' B ', and ' C '.
Consider the (not necessarily contiguous) subsequences x of S that satisfy all of the following conditions:
' A ', ' B ', and ' C ' all occur the same number of times in x.
No two adjacent characters in x are the same.
Among these subsequences, find one of the longest. Here a subsequence of S is a string obtained by deleting
zero or more characters from S .
Constraints
1 ≤ |S| ≤ 106
S consists of ' A ',' B ', and ' C '.
Input
Input is given from Standard Input in the following format:
8 of 11 21/07/19, 20.57
AtCoder Grand Contest 036 https://atcoder.jp/contests/agc036/tasks_print
Output
Print one longest subsequence that satisfies the conditions. If multiple solutions exist, any of them will be
accepted.
Sample Input 1
ABBCBCAB
Sample Output 1
ACBCAB
Consider the subsequence ' ACBCAB ' of S . It satisfies the conditions and is one of the longest with these
properties, along with ' ABCBCA '. On the other hand, the subsequences ' ABCBCAB ' and ' ABBCCA ' do not
satisfy the conditions.
Sample Input 2
ABABABABACACACAC
Sample Output 2
BABCAC
Sample Input 3
ABCABACBCBABABACBCBCBCBCBCAB
Sample Output 3
ACABACABABACBCBCBCBCA
Sample Input 4
AAA
9 of 11 21/07/19, 20.57
AtCoder Grand Contest 036 https://atcoder.jp/contests/agc036/tasks_print
Sample Output 4
F - Square Constraints
Problem Statement
Given is an integer N . How many permutations (P0 , P1 , ⋯ , P2N−1 ) of (0, 1, ⋯ , 2N − 1) satisfy the
following condition?
Constraints
1 ≤ N ≤ 250
2 ≤ M ≤ 109
All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M
Output
Print the number of permutations that satisfy the condition, modulo M .
Sample Input 1
2 998244353
10 of 11 21/07/19, 20.57
AtCoder Grand Contest 036 https://atcoder.jp/contests/agc036/tasks_print
Sample Output 1
4
(2, 3, 0, 1)
(2, 3, 1, 0)
(3, 2, 0, 1)
(3, 2, 1, 0)
Sample Input 2
10 998244353
Sample Output 2
53999264
Sample Input 3
200 998244353
Sample Output 3
112633322
11 of 11 21/07/19, 20.57