url
stringlengths
49
92
description
stringlengths
22
4.78k
cases
listlengths
0
6
https://atcoder.jp/contests/abc260/tasks/abc260_e
Problem Statement You are given an integer M and N pairs of integers (A_1, B_1), (A_2, B_2), \dots, (A_N, B_N) . For all i , it holds that 1 \leq A_i \lt B_i \leq M . A sequence S is said to be a good sequence if the following conditions are satisfied: S is a contiguous subsequence of the sequence (1,2,3,..., M) . For all i , S contains at least one of A_i and B_i . Let f(k) be the number of possible good sequences of length k . Enumerate f(1), f(2), \dots, f(M) .
[ { "input": "3 5\n1 3\n1 4\n2 5\n", "output": "0 1 3 2 1\n" }, { "input": "1 2\n1 2\n", "output": "2 1\n" }, { "input": "5 9\n1 5\n1 7\n5 6\n5 8\n2 6\n", "output": "0 0 1 2 4 4 3 2 1\n" } ]
https://atcoder.jp/contests/abc260/tasks/abc260_f
Problem Statement We have a simple undirected graph G with (S+T) vertices and M edges. The vertices are numbered 1 through (S+T) , and the edges are numbered 1 through M . Edge i connects Vertices u_i and v_i . Here, vertex sets V_1 = \lbrace 1, 2,\dots, S\rbrace and V_2 = \lbrace S+1, S+2, \dots, S+T \rbrace are both independent sets. A cycle of length 4 is called a 4-cycle. If G contains a 4-cycle, choose any of them and print the vertices in the cycle. You may print the vertices in any order. If G does not contain a 4-cycle, print -1 . What is an independent set? An independent set of a graph G is a set V' of some of the vertices in G such that no two vertices of V' have an edge between them.
[ { "input": "2 3 5\n1 3\n1 4\n1 5\n2 4\n2 5\n", "output": "1 2 4 5\n" }, { "input": "3 2 4\n1 4\n1 5\n2 5\n3 5\n", "output": "-1\n" }, { "input": "4 5 9\n3 5\n1 8\n3 7\n1 9\n4 6\n2 7\n4 8\n1 7\n2 9\n", "output": "1 7 2 9\n" } ]
https://atcoder.jp/contests/abc260/tasks/abc260_g
Problem Statement We have an N \times N grid. The square at the i -th row from the top and j -th column from the left in this grid is called (i,j) . Each square of the grid has at most one piece. The state of the grid is given by N strings S_i : if the j -th character of S_i is O , then (i,j) has a piece on it; if the j -th character of S_i is X , then (i,j) has no piece on it. You are given an integer M . Using this M , we define that a piece P placed at (s,t) covers a square (u,v) if all of the following conditions are satisfied: s \le u \le N t \le v \le N (u - s) + \frac{(v - t)}{2} < M For each of Q squares (X_i,Y_i) , find how many pieces cover the square.
[ { "input": "4 2\nOXXX\nXXXX\nXXXX\nXXXX\n6\n1 1\n1 4\n2 2\n2 3\n3 1\n4 4\n", "output": "1\n1\n1\n0\n0\n0\n" }, { "input": "5 10\nOOOOO\nOOOOO\nOOOOO\nOOOOO\nOOOOO\n5\n1 1\n2 3\n3 4\n4 2\n5 5\n", "output": "1\n6\n12\n8\n25\n" }, { "input": "8 5\nOXXOXXOX\nXOXXOXOX\nXOOXOOXO\nOXOOXOXO\nOXX...
https://atcoder.jp/contests/abc260/tasks/abc260_h
Problem Statement There are N balls numbered 1 through N . Ball i is painted in Color a_i . For a permutation P = (P_1, P_2, \dots, P_N) of (1, 2, \dots, N) , let us define C(P) as follows: The number of pairs of adjacent balls with different colors when Balls P_1, P_2, \dots, P_N are lined up in a row in this order. Let S_N be the set of all permutations of (1, 2, \dots, N) . Also, let us define F(k) by: \displaystyle F(k) = \left(\sum_{P \in S_N}C(P)^k \right) \bmod 998244353 . Enumerate F(1), F(2), \dots, F(M) .
[ { "input": "3 4\n1 1 2\n", "output": "8 12 20 36\n" }, { "input": "2 1\n1 1\n", "output": "0\n" }, { "input": "10 5\n3 1 4 1 5 9 2 6 5 3\n", "output": "30481920 257886720 199419134 838462446 196874334\n" } ]
https://atcoder.jp/contests/arc144/tasks/arc144_a
Problem Statement For a positive integer x , let f(x) be the sum of its digit. For example, f(144) = 1+4+4 = 9 and f(1)=1 . You are given a positive integer N . Find the following positive integers M and x : The maximum positive integer M for which there exists a positive integer x such that f(x)=N and f(2x)=M . The minimum positive integer x such that f(x)=N and f(2x)=M for the M above.
[ { "input": "3\n", "output": "6\n3\n" }, { "input": "6\n", "output": "12\n24\n" }, { "input": "100\n", "output": "200\n4444444444444444444444444\n" } ]
https://atcoder.jp/contests/arc144/tasks/arc144_b
Problem Statement You are given positive integers a and b such that a\leq b , and a sequence of positive integers A = (A_1, A_2, \ldots, A_N) . On this sequence, you can perform the following operation any number of times (possibly zero): Choose distinct indices i, j ( 1\leq i, j \leq N ). Add a to A_i and subtract b from A_j . Find the maximum possible value of \min(A_1, A_2, \ldots, A_N) after your operations.
[ { "input": "3 2 2\n1 5 9\n", "output": "5\n" }, { "input": "3 2 3\n11 1 2\n", "output": "3\n" }, { "input": "3 1 100\n8 5 6\n", "output": "5\n" }, { "input": "6 123 321\n10 100 1000 10000 100000 1000000\n", "output": "90688\n" } ]
https://atcoder.jp/contests/arc144/tasks/arc144_c
Problem Statement You are given positive integers N and K . Find the lexicographically smallest permutation A = (A_1, A_2, \ldots, A_N) of the integers from 1 through N that satisfies the following condition: \lvert A_i - i\rvert \geq K for all i ( 1\leq i\leq N ). If there is no such permutation, print -1 . What is lexicographical order on sequences? The following is an algorithm to determine the lexicographical order between different sequences S and T . Below, let S_i denote the i -th element of S . Also, if S is lexicographically smaller than T , we will denote that fact as S \lt T ; if S is lexicographically larger than T , we will denote that fact as S \gt T . Let L be the smaller of the lengths of S and T . For each i=1,2,\dots,L , we check whether S_i and T_i are the same. If there is an i such that S_i \neq T_i , let j be the smallest such i . Then, we compare S_j and T_j . If S_j is less than T_j (as a number), we determine that S \lt T and quit; if S_j is greater than T_j , we determine that S \gt T and quit. If there is no i such that S_i \neq T_i , we compare the lengths of S and T . If S is shorter than T , we determine that S \lt T and quit; if S is longer than T , we determine that S \gt T and quit.
[ { "input": "3 1\n", "output": "2 3 1\n" }, { "input": "8 3\n", "output": "4 5 6 7 8 1 2 3\n" }, { "input": "8 6\n", "output": "-1\n" } ]
https://atcoder.jp/contests/arc144/tasks/arc144_d
Problem Statement You are given positive integers N and K . Find the number, modulo 998244353 , of integer sequences \bigl(f(0), f(1), \ldots, f(2^N-1)\bigr) that satisfy all of the following conditions: 0\leq f(x)\leq K for all non-negative integers x ( 0\leq x \leq 2^N-1 ). f(x) + f(y) = f(x \,\mathrm{AND}\, y) + f(x \,\mathrm{OR}\, y) for all non-negative integers x and y ( 0\leq x, y \leq 2^N-1 ) Here, \mathrm{AND} and \mathrm{OR} denote the bitwise AND and OR, respectively.
[ { "input": "2 1\n", "output": "6\n" }, { "input": "2 2\n", "output": "19\n" }, { "input": "100 123456789123456789\n", "output": "34663745\n" } ]
https://atcoder.jp/contests/arc144/tasks/arc144_e
Problem Statement You are given a directed graph G with N vertices and M edges. The vertices are numbered 1, 2, \ldots, N . The i -th edge is directed from Vertex a_i to Vertex b_i , where a_i < b_i . The beautifulness of a sequence of positive integers W = (W_1, W_2, \ldots, W_N) is defined as the maximum positive integer x that satisfies the following: For every path (v_1, \ldots, v_k) ( v_1 = 1, v_k = N ) from Vertex 1 to Vertex N in G , \sum_{i=1}^k W_{v_i} is a multiple of x . You are given an integer sequence A = (A_1, A_2, \ldots, A_N) . Find the maximum beautifulness of a sequence of positive integers W = (W_1, \ldots, W_N) such that A_i \neq -1 \implies W_i = A_i . If the maximum beautifulness does not exist, print -1 .
[ { "input": "4 4\n1 2\n1 3\n2 4\n3 4\n-1 3 7 -1\n", "output": "4\n" }, { "input": "4 5\n1 2\n1 3\n2 4\n3 4\n1 4\n-1 3 7 -1\n", "output": "1\n" }, { "input": "4 4\n1 2\n1 3\n2 4\n3 4\n3 -1 -1 7\n", "output": "-1\n" }, { "input": "5 5\n1 3\n3 5\n2 3\n3 4\n1 4\n2 -1 3 -1 4\n", ...
https://atcoder.jp/contests/arc144/tasks/arc144_f
Problem Statement You are given a positive integer m , a non-negative integer a ( 0\leq a < m ), and a sequence of positive integers A = (A_1, \ldots, A_N) . A set X of positive integers is defined as X = \{x>0\mid x\equiv a \pmod{m}\} . Alice and Bob will play a game against each other. They will alternate turns performing the following operation, with Alice going first: Choose a pair (i,x) of an index i ( 1\leq i\leq N ) and a positite integer x\in X such that x\leq A_i . Change A_i to A_i - x . If there is no such (i, x) , the current player loses and the game ends. Find the number, modulo 998244353 , of pairs (i, x) that Alice can choose in her first turn so that she wins if both players play optimally in subsequent turns.
[ { "input": "3 1 0\n5 6 7\n", "output": "3\n" }, { "input": "5 10 3\n5 9 18 23 27\n", "output": "3\n" }, { "input": "4 10 8\n100 101 102 103\n", "output": "0\n" }, { "input": "5 2 1\n111111111111111 222222222222222 333333333333333 444444444444444 555555555555555\n", "outpu...
https://atcoder.jp/contests/abc259/tasks/abc259_a
Problem Statement Takahashi had his N -th birthday, when he was T centimeters tall. Additionally, we know the following facts: In each year between Takahashi's birth ( 0 -th birthday) and his X -th birthday, his height increased by D centimeters. More formally, for each i = 1, 2, \ldots, X , his height increased by D centimeters between his (i-1) -th birthday and his i -th birthday. Between Takahashi's X -th birthday and his N -th birthday, his height did not change. Find Takahashi's height on his M -th birthday, in centimeters.
[ { "input": "38 20 17 168 3\n", "output": "168\n" }, { "input": "1 0 1 3 2\n", "output": "1\n" }, { "input": "100 10 100 180 1\n", "output": "90\n" } ]
https://atcoder.jp/contests/abc259/tasks/abc259_b
Problem Statement In an xy -coordinate plane whose x -axis is oriented to the right and whose y -axis is oriented upwards, rotate a point (a, b) around the origin d degrees counterclockwise and find the new coordinates of the point.
[ { "input": "2 2 180\n", "output": "-2 -2\n" }, { "input": "5 0 120\n", "output": "-2.49999999999999911182 4.33012701892219364908\n" }, { "input": "0 0 11\n", "output": "0.00000000000000000000 0.00000000000000000000\n" }, { "input": "15 5 360\n", "output": "15.000000000000...
https://atcoder.jp/contests/abc259/tasks/abc259_c
Problem Statement You are given two strings S and T . Determine whether it is possible to make S equal T by performing the following operation some number of times (possibly zero). Between two consecutive equal characters in S , insert a character equal to these characters. That is, take the following three steps. Let N be the current length of S , and S = S_1S_2\ldots S_N . Choose an integer i between 1 and N-1 (inclusive) such that S_i = S_{i+1} . (If there is no such i , do nothing and terminate the operation now, skipping step 3.) Insert a single copy of the character S_i(= S_{i+1}) between the i -th and (i+1) -th characters of S . Now, S is a string of length N+1 : S_1S_2\ldots S_i S_i S_{i+1} \ldots S_N .
[ { "input": "abbaac\nabbbbaaac\n", "output": "Yes\n" }, { "input": "xyzz\nxyyzz\n", "output": "No\n" } ]
https://atcoder.jp/contests/abc259/tasks/abc259_d
Problem Statement You are given N circles on the xy -coordinate plane. For each i = 1, 2, \ldots, N , the i -th circle is centered at (x_i, y_i) and has a radius of r_i . Determine whether it is possible to get from (s_x, s_y) to (t_x, t_y) by only passing through points that lie on the circumference of at least one of the N circles.
[ { "input": "4\n0 -2 3 3\n0 0 2\n2 0 2\n2 3 1\n-3 3 3\n", "output": "Yes\n" }, { "input": "3\n0 1 0 3\n0 0 1\n0 0 2\n0 0 3\n", "output": "No\n" } ]
https://atcoder.jp/contests/abc259/tasks/abc259_e
Problem Statement There are N integers a_1,\ldots,a_N written on a whiteboard. Here, a_i can be represented as a_i = p_{i,1}^{e_{i,1}} \times \ldots \times p_{i,m_i}^{e_{i,m_i}} using m_i prime numbers p_{i,1} \lt \ldots \lt p_{i,m_i} and positive integers e_{i,1},\ldots,e_{i,m_i} . You will choose one of the N integers to replace it with 1 . Find the number of values that can be the least common multiple of the N integers after the replacement.
[ { "input": "4\n1\n7 2\n2\n2 2\n5 1\n1\n5 1\n2\n2 1\n7 1\n", "output": "3\n" }, { "input": "1\n1\n998244353 1000000000\n", "output": "1\n" } ]
https://atcoder.jp/contests/abc259/tasks/abc259_f
Problem Statement You are given a tree with N vertices. For each i = 1, 2, \ldots, N-1 , the i -th edge connects Vertex u_i and Vertex v_i and has a weight w_i . Consider choosing some of the N-1 edges (possibly none or all). Here, for each i = 1, 2, \ldots, N , one may choose at most d_i edges incident to Vertex i . Find the maximum possible total weight of the chosen edges.
[ { "input": "7\n1 2 1 0 2 1 1\n1 2 8\n2 3 9\n2 4 10\n2 5 -3\n5 6 8\n5 7 3\n", "output": "28\n" }, { "input": "20\n0 2 0 1 2 1 0 0 3 0 1 1 1 1 0 0 3 0 1 2\n4 9 583\n4 6 -431\n5 9 325\n17 6 131\n17 2 -520\n2 16 696\n5 7 662\n17 15 845\n7 8 307\n13 7 849\n9 19 242\n20 6 909\n7 11 -775\n17 18 557\n14 20 ...
https://atcoder.jp/contests/abc259/tasks/abc259_g
Problem Statement There are H \times W cards on a grid of squares with H rows and W columns. For each pair of integers (i, j) such that 1 \leq i \leq H, 1 \leq j \leq W , the card at the i -th row and j -th column has an integer A_{i, j} written on it. Takahashi and Aoki will cooperate to play a game, which consists of the following steps. First, Takahashi chooses some of the H rows (possibly none or all) and places a red token on each card in the chosen rows. Second, Aoki chooses some of the W columns (possibly none or all) and places a blue token on each card in the chosen columns. Now, they compute their score as follows. If there is a card with a negative integer that has both red and blue tokens placed on it, the play is a "total failure"; the score is -10^{100} . Otherwise, they collect all cards that have one or more tokens placed on them. The score is the sum of the integers written on the collected cards. Find their maximum possible score.
[ { "input": "2 3\n-9 5 1\n6 -2 4\n", "output": "9\n" }, { "input": "15 20\n-14 74 -48 38 -51 43 5 37 -39 -29 80 -44 -55 59 17 89 -37 -68 38 -16\n14 31 43 -73 49 -7 -65 13 -40 -45 36 88 -54 -43 99 87 -94 57 -22 31\n-85 67 -46 23 95 68 55 17 -56 51 -38 64 32 -19 65 -62 76 66 -53 -16\n35 -78 -41 35 -51 ...
https://atcoder.jp/contests/abc259/tasks/abc259_h
Problem Statement We have a grid of squares with N rows arranged vertically and N columns arranged horizontally. The square at the i -th row from the top and j -th column from the left has an integer label a_{i,j} . Consider paths obtained by starting on one of the squares and going right or down to an adjacent square zero or more times. Find the number, modulo 998244353 , of such paths that start and end on squares with the same label. Two paths are distinguished when they visit different sets of squares (including the starting and ending squares).
[ { "input": "2\n1 3\n3 1\n", "output": "6\n" } ]
https://atcoder.jp/contests/ahc012/tasks/ahc012_a
Problem Statement There is a circular cake with a radius of 10^4 centered at the origin and N strawberries on top of it. The center of the i -th strawberry is at the coordinates (x_i, y_i) and satisfies x_i^2+y_i^2<10^8 . Takahashi can cut the cake in at most K straight lines (not segments), which may intersect each other. You should specify the line to be cut as a straight line passing through two different integer coordinates (p_x,p_y) and (q_x,q_y) satisfying -10^9\leq p_x,p_y,q_x,q_y\leq 10^9 . The two specified points can be outside of the cake. Because he is clumsy, he cannot stop or curve a single cut in the middle of the cut. For each d=1,2,\cdots,10 , you are given the number a_d of attendees who have been participating in AtCoder's contests for d years. Let b_d be the number of pieces with d strawberries on them. Then we can distribute \sum_{d=1}^{10} \min(a_d,b_d) pieces to attendees. Here, the i -th strawberry belongs to a piece if and only if its center (x_i, y_i) is contained inside (excluding the circumference) of the piece. If a strawberry is cut in a straight line that passes through its center, it belongs to no pieces.
[ { "input": "2835 100\n51 3 44 99 1 87 83 19 10 90\n484 -7595\n606 -4986\n-70 3067\n3481 -4705\n-3464 -3044\n1329 -4256\n-3512 -5340\n-2544 6126\n3435 7021\n-7712 -6197\n-3097 569\n7192 1127\n-1048 1879\n4813 -3083\n-5549 6929\n-442 2382\n2945 3994\n6014 -7099\n7351 -3206\n957 2312\n4759 3636\n5006 2830\n1295 -3...
https://atcoder.jp/contests/abc258/tasks/abc258_a
Problem Statement AtCoder Beginner Contest usually starts at 21:00 JST and lasts for 100 minutes. You are given an integer K between 0 and 100 (inclusive). Print the time K minutes after 21:00 in the HH:MM format, where HH denotes the hour on the 24 -hour clock and MM denotes the minute. If the hour or the minute has just one digit, append a 0 to the beginning to represent it as a 2 -digit integer.
[ { "input": "63\n", "output": "22:03\n" }, { "input": "45\n", "output": "21:45\n" }, { "input": "100\n", "output": "22:40\n" } ]
https://atcoder.jp/contests/abc258/tasks/abc258_b
Problem Statement You are given a positive integer N . We have a grid with N rows and N columns, where the square at the i -th row from the top and j -th column from the left has a digit A_{i,j} written on it. Assume that the upper and lower edges of this grid are connected, as well as the left and right edges. In other words, all of the following holds. (N,i) is just above (1,i) , and (1,i) is just below (N,i) . (1\le i\le N) . (i,N) is just to the left of (i,1) , and (i,1) is just to the right of (i,N) . (1\le i\le N) . Takahashi will first choose one of the following eight directions: up, down, left, right, and the four diagonal directions. Then, he will start on a square of his choice and repeat moving one square in the chosen direction N-1 times. In this process, Takahashi visits N squares. Find the greatest possible value of the integer that is obtained by arranging the digits written on the squares visited by Takahashi from left to right in the order visited by him.
[ { "input": "4\n1161\n1119\n7111\n1811\n", "output": "9786\n" }, { "input": "10\n1111111111\n1111111111\n1111111111\n1111111111\n1111111111\n1111111111\n1111111111\n1111111111\n1111111111\n1111111111\n", "output": "1111111111\n" } ]
https://atcoder.jp/contests/abc258/tasks/abc258_c
Problem Statement You are given positive integers N and Q , and a string S of length N consisting of lowercase English letters. Process Q queries. Each query is of one of the following two types. 1 x : Perform the following x times in a row: delete the last character of S and append it to the beginning. 2 x : Print the x -th character of S .
[ { "input": "3 3\nabc\n2 2\n1 1\n2 2\n", "output": "b\na\n" }, { "input": "10 8\ndsuccxulnl\n2 4\n2 7\n1 2\n2 7\n1 1\n1 2\n1 3\n2 5\n", "output": "c\nu\nc\nu\n" } ]
https://atcoder.jp/contests/abc258/tasks/abc258_d
Problem Statement We have a video game consisting of N stages. The i -th stage (1 \leq i \leq N) is composed of a movie lasting A_i minutes and gameplay lasting B_i minutes. To clear the i -th stage for the first time, one must watch the movie and do the gameplay for that stage. For the second and subsequent times, one may skip the movie and do just the gameplay. In the beginning, only the 1 -st stage is unlocked, and clearing the i -th stage (1 \leq i \leq N - 1) unlocks the (i+1) -th stage. Find the shortest time needed to clear a stage X times in total. Here, if the same stage is cleared multiple times, all of them count.
[ { "input": "3 4\n3 4\n2 3\n4 2\n", "output": "18\n" }, { "input": "10 1000000000\n3 3\n1 6\n4 7\n1 8\n5 7\n9 9\n2 4\n6 4\n5 1\n3 1\n", "output": "1000000076\n" } ]
https://atcoder.jp/contests/abc258/tasks/abc258_e
Problem Statement 10^{100} potatoes are coming from a conveyor belt one by one. The weights of the potatoes are described by a sequence W = (W_0, \dots, W_{N-1}) of length N : the weight of the i -th potato coming is W_{(i-1) \bmod N} , where (i-1) \bmod N denotes the remainder when i - 1 is divided by N . Takahashi will prepare an empty box and then pack the potatoes in order, as follows. Pack the incoming potato into the box. If the total weight of the potatoes in the box is now X or greater, seal that box and prepare a new empty box. You are given Q queries. In the i -th query (1 \leq i \leq Q) , given a positive integer K_i , find the number of potatoes in the K_i -th box to be sealed. It can be proved that, under the Constraints of the problem, there will be at least K_i sealed boxes.
[ { "input": "3 2 5\n3 4 1\n1\n2\n", "output": "2\n3\n" }, { "input": "10 5 20\n5 8 5 9 8 7 4 4 8 2\n1\n1000\n1000000\n1000000000\n1000000000000\n", "output": "4\n5\n5\n5\n5\n" } ]
https://atcoder.jp/contests/abc258/tasks/abc258_f
Problem Statement The roads in the Kingdom of AtCoder, which lies on the xy-plane, are the lines x=n and y=n for all integers n . Among them, the lines x=Bn and y=Bn for all integers n are main roads. When Takahashi is at (x,y) , he can move to (x,y-1) , (x,y+1) , (x+1,y) , or (x-1,y) . Each move takes 1 second along a main road and K seconds otherwise. Find the minimum number of seconds Takahashi needs to get from (S_x, S_y) to (G_x, G_y) . You will have T test cases to solve.
[ { "input": "4\n3 4 2 2 4 4\n5 6 2 3 2 3\n1 1000000000 0 0 1000000000 1000000000\n1000000000 1000000000 500000000 500000000 1000000000 1000000000\n", "output": "10\n0\n2000000000\n500000000500000000\n" }, { "input": "10\n928184439 674654465 203937094 186855052 851783856 805293696\n55480262 448852233 ...
https://atcoder.jp/contests/abc258/tasks/abc258_g
Problem Statement You are given a simple undirected graph G with N vertices. G is given as the N \times N adjacency matrix A . That is, there is an edge between Vertices i and j if A_{i,j} is 1 , and there is not if A_{i,j} is 0 . Find the number of triples of integers (i,j,k) satisfying 1 \le i < j < k \le N such that there is an edge between Vertices i and j , an edge between Vertices j and k , and an edge between Vertices i and k .
[ { "input": "4\n0011\n0011\n1101\n1110\n", "output": "2\n" }, { "input": "10\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n", "output": "0\n" } ]
https://atcoder.jp/contests/abc258/tasks/abc258_h
Problem Statement Find the number, modulo 998244353 , of sequences X that satisfy all of the following conditions. Every term in X is a positive odd number . The sum of the terms in X is S . The prefix sums of X contain none of A_1, \dots, A_N . Formally, if we define Y_i = X_1 + \dots + X_i for each i , then Y_i \neq A_j holds for all integers i and j such that 1 \leq i \leq |X| and 1 \leq j \leq N .
[ { "input": "3 7\n2 4 5\n", "output": "3\n" }, { "input": "5 60\n10 20 30 40 50\n", "output": "37634180\n" }, { "input": "10 1000000000000000000\n1 2 4 8 16 32 64 128 256 512\n", "output": "75326268\n" } ]
https://atcoder.jp/contests/arc143/tasks/arc143_a
Problem Statement Three non-negative integers A , B , and C are written on a blackboard. You can perform the following two operations any number of times in any order. Subtract 1 from two of the written integers of your choice. Subtract 1 from all of the written integers. Your objective is to make all the numbers on the blackboard 0 . Determine whether it is achievable. If it is, find the minimum number of times you need to perform an operation to achieve it.
[ { "input": "2 2 3\n", "output": "3\n" }, { "input": "0 0 1\n", "output": "-1\n" }, { "input": "0 0 0\n", "output": "0\n" } ]
https://atcoder.jp/contests/arc143/tasks/arc143_b
Problem Statement Find the number of ways, modulo 998244353 , to fill the squares of an N \times N grid using each integer from 1 to N^2 once so that every square satisfies at least one of the following conditions. In the same column, there is a square containing a number greater than that of the concerned square. In the same row, there is a square containing a number less than that of the concerned square.
[ { "input": "2\n", "output": "8\n" }, { "input": "5\n", "output": "704332752\n" }, { "input": "100\n", "output": "927703658\n" } ]
https://atcoder.jp/contests/arc143/tasks/arc143_c
Problem Statement There are N piles of pebbles. Initially, the i -th pile has A_i pebbles. Takahashi and Aoki will play a game using these piles. They will alternately perform the following operation, with Takahashi going first, and the one who becomes unable to do so loses the game. Choose one or more piles, and remove the following number of pebbles from each chosen pile: X pebbles if this operation is performed by Takahashi, and Y pebbles if performed by Aoki. Here, a pile with an insufficient number of pebbles cannot be chosen. Determine the winner of the game if both players play optimally.
[ { "input": "2 1 1\n3 3\n", "output": "First\n" }, { "input": "2 1 2\n3 3\n", "output": "Second\n" } ]
https://atcoder.jp/contests/arc143/tasks/arc143_d
Problem Statement We have two sequences A_1,\ldots, A_M and B_1,\ldots,B_M consisting of intgers between 1 and N (inclusive). For a string of length M consisting of 0 and 1 , consider the following undirected graph with 2N vertices and (M+N) edges corresponding to that string. If the i -th character of the string is 0 , the i -th edge connects Vertex A_i and Vertex (B_i+N) . If the i -th character of the string is 1 , the i -th edge connects Vertex B_i and Vertex (A_i+N) . The (j+M) -th edge connects Vertex j and Vertex (j+N) . Here, i and j are integers such that 1 \leq i \leq M and 1 \leq j \leq N , and the vertices are numbered 1 to 2N . Find one string of length M consisting of 0 and 1 such that the corresponding undirected graph has the minimum number of bridges. Notes on bridges A bridge is an edge of a graph whose removal increases the number of connected components.
[ { "input": "2 2\n1 1\n2 2\n", "output": "01\n" }, { "input": "6 7\n1 1 2 3 4 4 5\n2 3 3 4 5 6 6\n", "output": "0100010\n" } ]
https://atcoder.jp/contests/arc143/tasks/arc143_e
Problem Statement We have a tree with N vertices. The vertices are numbered 1 to N , and the i -th edge connects Vertex A_i and Vertex B_i . Additionally, each vertex has a reversi piece on it. The status of the piece on each vertex is given by a string S : if the i -th character of S is B , the piece on Vertex i is placed with the black side up; if the i -th character of S is W , the piece on Vertex i is placed with the white side up. Determine whether it is possible to perform the operation below N times to remove the pieces from all vertices. If it is possible, find the lexicographically smallest possible sequence P_1, P_2, \ldots, P_N such that Vertices P_1, P_2, \ldots, P_N can be chosen in this order during the process. Choose a vertex containing a piece with the white side up, and remove the piece from that vertex. Then, flip all pieces on the vertices adjacent to that vertex. Notes on reversi pieces A reversi piece has a black side and a white side, and flipping it changes which side faces up. What is the lexicographical order on sequences? The following is an algorithm to determine the lexicographical order between different sequences S and T . Below, let S_i denote the i -th element of S . Also, if S is lexicographically smaller than T , we will denote that fact as S \lt T ; if S is lexicographically larger than T , we will denote that fact as S \gt T . Let L be the smaller of the lengths of S and T . For each i=1,2,\dots,L , we check whether S_i and T_i are the same. If there is an i such that S_i \neq T_i , let j be the smallest such i . Then, we compare S_j and T_j . If S_j is less than T_j (as a number), we determine that S \lt T and quit; if S_j is greater than T_j , we determine that S \gt T and quit. If there is no i such that S_i \neq T_i , we compare the lengths of S and T . If S is shorter than T , we determine that S \lt T and quit; if S is longer than T , we determine that S \gt T and quit.
[ { "input": "4\n1 2\n2 3\n3 4\nWBWW\n", "output": "1 2 4 3 \n" }, { "input": "4\n1 2\n2 3\n3 4\nBBBB\n", "output": "-1\n" } ]
https://atcoder.jp/contests/arc143/tasks/arc143_f
Problem Statement Given a positive integer N , find the number, modulo 998244353 , of subsets S of \{1, 2, \ldots, N\} that satisfy the following condition. Every positive integer at most N can be represented as the sum of some distinct elements of S , and there are at most two such representations.
[ { "input": "3\n", "output": "2\n" }, { "input": "5\n", "output": "5\n" }, { "input": "1000\n", "output": "742952024\n" } ]
https://atcoder.jp/contests/abc257/tasks/abc257_a
Problem Statement Find the X -th character from the beginning of the string that is obtained by concatenating these characters: N copies of A 's, N copies of B 's, …, and N copies of Z 's, in this order.
[ { "input": "1 3\n", "output": "C\n" }, { "input": "2 12\n", "output": "F\n" } ]
https://atcoder.jp/contests/abc257/tasks/abc257_b
Problem Statement There are N squares, indexed Square 1 , Square 2 , …, Square N , arranged in a row from left to right. Also, there are K pieces. The i -th piece from the left is initially placed on Square A_i . Now, we will perform Q operations against them. The i -th operation is as follows: If the L_i -th piece from the left is on its rightmost square, do nothing. Otherwise, move the L_i -th piece from the left one square right if there is no piece on the next square on the right; if there is, do nothing. Print the index of the square on which the i -th piece from the left is after the Q operations have ended, for each i=1,2,\ldots,K .
[ { "input": "5 3 5\n1 3 4\n3 3 1 1 2\n", "output": "2 4 5\n" }, { "input": "2 2 2\n1 2\n1 2\n", "output": "1 2\n" }, { "input": "10 6 9\n1 3 5 7 8 9\n1 2 3 4 5 6 5 6 2\n", "output": "2 5 6 7 9 10\n" } ]
https://atcoder.jp/contests/abc257/tasks/abc257_c
Problem Statement There are N people, each of whom is either a child or an adult. The i -th person has a weight of W_i . Whether each person is a child or an adult is specified by a string S of length N consisting of 0 and 1 . If the i -th character of S is 0 , then the i -th person is a child; if it is 1 , then the i -th person is an adult. When Takahashi the robot is given a real number X , Takahashi judges a person with a weight less than X to be a child and a person with a weight more than or equal to X to be an adult. For a real value X , let f(X) be the number of people whom Takahashi correctly judges whether they are children or adults. Find the maximum value of f(X) for all real values of X .
[ { "input": "5\n10101\n60 45 30 40 80\n", "output": "4\n" }, { "input": "3\n000\n1 2 3\n", "output": "3\n" }, { "input": "5\n10101\n60 50 50 50 60\n", "output": "4\n" } ]
https://atcoder.jp/contests/abc257/tasks/abc257_d
Problem Statement There are N trampolines on a two-dimensional planar town where Takahashi lives. The i -th trampoline is located at the point (x_i, y_i) and has a power of P_i . Takahashi's jumping ability is denoted by S . Initially, S=0 . Every time Takahashi trains, S increases by 1 . Takahashi can jump from the i -th to the j -th trampoline if and only if: P_iS\geq |x_i - x_j| +|y_i - y_j| . Takahashi's objective is to become able to choose a starting trampoline such that he can reach any trampoline from the chosen one with some jumps. At least how many times does he need to train to achieve his objective?
[ { "input": "4\n-10 0 1\n0 0 5\n10 0 1\n11 0 1\n", "output": "2\n" }, { "input": "7\n20 31 1\n13 4 3\n-10 -15 2\n34 26 5\n-2 39 4\n0 -50 1\n5 -20 2\n", "output": "18\n" } ]
https://atcoder.jp/contests/abc257/tasks/abc257_e
Problem Statement Takahashi has an integer x . Initially, x=0 . Takahashi may do the following operation any number of times. Choose an integer i\ (1\leq i \leq 9) . Pay C_i yen (the currency in Japan) to replace x with 10x + i . Takahashi has a budget of N yen. Find the maximum possible value of the final x resulting from operations without exceeding the budget.
[ { "input": "5\n5 4 3 3 2 5 3 5 3\n", "output": "95\n" }, { "input": "20\n1 1 1 1 1 1 1 1 1\n", "output": "99999999999999999999\n" } ]
https://atcoder.jp/contests/abc257/tasks/abc257_f
Problem Statement There are N towns numbered Town 1 , Town 2 , \ldots , Town N . There are also M Teleporters , each of which connects two towns bidirectionally so that a person can travel from one to the other in one minute. The i -th Teleporter connects Town U_i and Town V_i bidirectionally. However, for some of the Teleporters, one of the towns it connects is undetermined; U_i=0 means that one of the towns the i -th Teleporter connects is Town V_i , but the other end is undetermined. For i=1,2,\ldots,N , answer the following question. When the Teleporters with undetermined ends are all determined to be connected to Town i , how many minutes is required at minimum to travel from Town 1 to Town N ? If it is impossible to travel from Towns 1 to N using Teleporters only, print -1 instead.
[ { "input": "3 2\n0 2\n1 2\n", "output": "-1 -1 2\n" }, { "input": "5 5\n1 2\n1 3\n3 4\n4 5\n0 2\n", "output": "3 3 3 3 2\n" } ]
https://atcoder.jp/contests/abc257/tasks/abc257_g
Problem Statement You are given two strings S and T consisting of lowercase English letters. Find the minimum positive integer k such that you can choose (not necessarily distinct) k prefixes of S so that their concatenation coincides with T . In other words, find the minimum positive integer k such that there exists a k -tuple (a_1,a_2,\ldots, a_k) of integers between 1 and |S| such that T=S_{a_1}+S_{a_2}+\cdots +S_{a_k} , where S_i denotes the substring of S from the 1 -st through the i -th characters and + denotes the concatenation of strings. If it is impossible to make it coincide with T , print -1 instead.
[ { "input": "aba\nababaab\n", "output": "3\n" }, { "input": "atcoder\nac\n", "output": "-1\n" } ]
https://atcoder.jp/contests/abc257/tasks/abc257_h
Problem Statement The six-sided dice speciality shop "Saikoroya" sells N dice. The i -th die (singular of dice) has A_{i,1},A_{i,2},\ldots,A_{i,6} written on its each side, and has a price of C_i . Takahashi is going to choose exactly K of them and buy them. Currently, "Saikoroya" is conducting a promotion: Takahashi may roll each of the purchased dice once and claim money whose amount is equal to the square of the sum of the numbers shown by the dice. Here, each die shows one of the six numbers uniformly at random and independently. Maximize the expected value of (the amount of money he claims) - (the sum of money he pays for the purchased K dice) by properly choosing K dice to buy. Print the maximized expected value modulo 998244353 . Definition of the expected value modulo 998244353 We can prove that the sought expected value is always a rational number. Moreover, under the Constraints of this problem, the sought expected value can be expressed by an irreducible fraction \frac{y}{x} where x is indivisible by 998244353 . In this case, we can uniquely determine the integer z between 0 and 998244352 (inclusive) such that xz \equiv y \pmod{998244353} . Print such z .
[ { "input": "3 2\n1 2 3\n1 1 1 1 1 1\n2 2 2 2 2 2\n3 3 3 3 3 3\n", "output": "20\n" }, { "input": "10 5\n2 5 6 5 2 1 7 9 7 2\n5 5 2 4 7 6\n2 2 8 7 7 9\n8 1 9 6 10 8\n8 6 10 3 3 9\n1 10 5 8 1 10\n7 8 4 8 6 5\n1 10 2 5 1 7\n7 4 1 4 5 4\n5 10 1 5 1 2\n5 1 2 3 6 2\n", "output": "1014\n" } ]
https://atcoder.jp/contests/arc142/tasks/arc142_a
Problem Statement For a positive integer x , let f(x) be the answer to the question below. The following operation on x can be performed zero or more times. Let x' be the integer obtained by reversing the decimal notation of x . Then, replace x with x' . If x now has one or more leading zeros, delete them so that it begins with a non-zero digit. For example, from x=1420 , you get x=241 after one operation, x=142 after two operations, and x=241 after three operations. Find the minimum possible value of x after operations. Find the number of integers x such that 1 \leq x \leq N and f(x)=K .
[ { "input": "1420 142\n", "output": "3\n" }, { "input": "1419 142\n", "output": "2\n" }, { "input": "6 19\n", "output": "0\n" } ]
https://atcoder.jp/contests/arc142/tasks/arc142_b
Problem Statement We have an N \times N grid. Let (i,j) denote the square at the i -th row from the top and j -th column from the left in this grid. Find one way to write an integer on every square to satisfy the conditions below. Each integer between 1 and N^2 is written exactly once. For every pair of integers i,j\, (1 \leq i,j \leq N) , the square (i,j) satisfies the following. Among the squares horizontally, vertically, or diagonally adjacent to (i,j) (there are at most eight of them), let a and b be the number of squares with integers larger than and smaller than that of (i,j) , respectively. Then, a \neq b holds. Under the Constraints of this problem, it can be proved that such a way to write integers always exists.
[ { "input": "2\n", "output": "1 2\n3 4\n" }, { "input": "3\n", "output": "1 2 3\n5 4 6\n7 8 9\n" } ]
https://atcoder.jp/contests/arc142/tasks/arc142_c
Problem Statement There is a tree with N vertices, numbered 1, \ldots, N . For each pair of integers u,v\, (1 \leq u,v \leq N) , the distance d_{u,v} between Vertices u, v is defined as the following. The number of edges contained in the shortest path connecting Vertices u and v . You are allowed to ask between 0 and 2N questions (inclusive) in the following form. Ask the distance d_{u,v} between Vertices u,v for integers u,v of your choice such that 1\leq u,v \leq N and u+v>3 . Find the distance d_{1,2} between Vertices 1,2 .
[]
https://atcoder.jp/contests/arc142/tasks/arc142_d
Problem Statement We have a tree with N vertices, numbered 1, \ldots, N . For each i=1,\ldots,N-1 , the i -th edge connects Vertex a_i and Vertex b_i . An operation on this tree when at most one piece is put on each vertex is defined as follows. Simultaneously move every piece to one of the vertices adjacent to the one occupied by the piece. An operation is said to be good when the conditions below are satisfied. Each edge is traversed by at most one piece. Each vertex will be occupied by at most one piece after the operation. Takahashi will put a piece on one or more vertices of his choice. Among the 2^N-1 ways to put pieces, find the number of ones that satisfy the condition below, modulo 998244353 . For every non-negative integer K , the following holds. It is possible to perform a good operation K times. Let S_K be the set of vertices occupied by pieces just after K good operations. Then, S_K is unique.
[ { "input": "3\n1 2\n1 3\n", "output": "2\n" }, { "input": "7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n", "output": "0\n" }, { "input": "19\n9 14\n2 13\n1 3\n17 19\n13 18\n12 19\n4 5\n2 10\n4 9\n8 11\n3 15\n6 8\n8 10\n6 19\n9 13\n11 15\n7 17\n16 17\n", "output": "100\n" } ]
https://atcoder.jp/contests/arc142/tasks/arc142_e
Problem Statement There are N wizards, numbered 1, \ldots, N . Wizard i has a strength of a_i and plans to defeat a monster with a strength of b_i . You can perform the following operation any number of times. Increase the strength of any wizard of your choice by 1 . A pair of Wizard i and Wizard j (let us call this pair (i, j) ) is said to be good when at least one of the following conditions is satisfied. Wizard i has a strength of at least b_i and Wizard j has a strength of at least b_j . Wizard i has a strength of at least b_j and Wizard j has a strength of at least b_i . Your objective is to make the pair (x_i, y_i) good for every i=1, \ldots, M . Find the minimum number of operations needed to achieve it.
[ { "input": "5\n1 5\n2 4\n3 3\n4 2\n5 1\n3\n1 4\n2 5\n3 5\n", "output": "2\n" }, { "input": "4\n1 1\n1 1\n1 1\n1 1\n3\n1 2\n2 3\n3 4\n", "output": "0\n" }, { "input": "9\n1 1\n2 4\n5 5\n7 10\n9 3\n9 13\n10 9\n3 9\n2 9\n7\n1 5\n2 5\n1 6\n2 4\n3 4\n4 9\n8 9\n", "output": "22\n" } ]
https://atcoder.jp/contests/arc142/tasks/arc142_f
Problem Statement Two wizards, X and Y , are fighting with a monster. Initially, each of the wizards has a magic power of 0 . They know the following two spells. Spell 1 : Increases the caster's magic power by 1 . Spell 2 : Deals damage equal to the caster's magic power to the monster. After each wizard uses a spell N times, they will withdraw from the battle. For each i=1, \ldots, N , they must use one of the following combinations of spells for their i -th spells. X casts Spell a_i , and Y casts Spell b_i . X casts Spell c_i , and Y casts Spell d_i . Find the maximum total damage that can be dealt to the monster before the withdrawal.
[ { "input": "3\n1 1 2 2\n2 1 2 2\n2 1 1 1\n", "output": "3\n" }, { "input": "5\n2 2 2 2\n2 2 2 2\n2 2 2 2\n2 2 2 2\n2 2 2 2\n", "output": "0\n" }, { "input": "8\n1 1 2 2\n2 2 2 1\n1 1 2 1\n1 1 2 2\n2 1 1 1\n1 2 1 2\n2 1 1 2\n2 1 2 1\n", "output": "20\n" }, { "input": "20\n2 1 ...
https://atcoder.jp/contests/abc256/tasks/abc256_a
Problem Statement Given N , print 2^N .
[ { "input": "3\n", "output": "8\n" }, { "input": "30\n", "output": "1073741824\n" } ]
https://atcoder.jp/contests/abc256/tasks/abc256_b
Problem Statement Takahashi is trying to create a game inspired by baseball, but he is having difficulty writing the code. Write a program for Takahashi that solves the following problem. There are 4 squares called Square 0 , Square 1 , Square 2 , and Square 3 . Initially, all squares are empty. There is also an integer P ; initially, P = 0 . Given a sequence of positive integers A = (A_1, A_2, \dots, A_N) , perform the following operations for i = 1, 2, \dots, N in this order: Put a piece on Square 0 . Advance every piece on the squares A_i squares ahead. In other words, if Square x has a piece, move the piece to Square (x + A_i) . If, however, the destination square does not exist (i.e. x + A_i is greater than or equal to 4 ) for a piece, remove it. Add to P the number of pieces that have been removed. Print the value of P after all the operations have been performed.
[ { "input": "4\n1 1 3 2\n", "output": "3\n" }, { "input": "3\n1 1 1\n", "output": "0\n" }, { "input": "10\n2 2 4 1 1 1 4 2 2 1\n", "output": "8\n" } ]
https://atcoder.jp/contests/abc256/tasks/abc256_c
Problem Statement You are given six integers: h_1, h_2, h_3, w_1, w_2 , and w_3 . Consider writing a positive integer on each square of a 3 \times 3 grid so that all of the following conditions are satisfied: For i=1,2,3 , the sum of numbers written in the i -th row from the top is h_i . For j=1,2,3 , the sum of numbers written in the j -th column from the left is w_i . For example, if (h_1, h_2, h_3) = (5, 13, 10) and (w_1, w_2, w_3) = (6, 13, 9) , then all of the following three ways satisfy the conditions. (There are other ways to satisfy the conditions.) How many ways are there to write numbers to satisfy the conditions?
[ { "input": "3 4 6 3 3 7\n", "output": "1\n" }, { "input": "3 4 5 6 7 8\n", "output": "0\n" }, { "input": "5 13 10 6 13 9\n", "output": "120\n" }, { "input": "20 25 30 22 29 24\n", "output": "30613\n" } ]
https://atcoder.jp/contests/abc256/tasks/abc256_d
Problem Statement For real numbers L and R , let us denote by [L,R) the set of real numbers greater than or equal to L and less than R . Such a set is called a right half-open interval. You are given N right half-open intervals [L_i,R_i) . Let S be their union. Represent S as a union of the minimum number of right half-open intervals.
[ { "input": "3\n10 20\n20 30\n40 50\n", "output": "10 30\n40 50\n" }, { "input": "3\n10 40\n30 60\n20 50\n", "output": "10 60\n" } ]
https://atcoder.jp/contests/abc256/tasks/abc256_e
Problem Statement There are N people numbered 1 through N . Takahashi has decided to choose a sequence P = (P_1, P_2, \dots, P_N) that is a permutation of integers from 1 through N , and give a candy to Person P_1 , Person P_2 , \dots , and Person P_N , in this order. Since Person i dislikes Person X_i , if Takahashi gives a candy to Person X_i prior to Person i , then Person i gains frustration of C_i ; otherwise, Person i 's frustration is 0 . Takahashi may arbitrarily choose P . What is the minimum possible sum of their frustration?
[ { "input": "3\n2 3 2\n1 10 100\n", "output": "10\n" }, { "input": "8\n7 3 5 5 8 4 1 2\n36 49 73 38 30 85 27 45\n", "output": "57\n" } ]
https://atcoder.jp/contests/abc256/tasks/abc256_f
Problem Statement You are given N , Q , and A=(A_1,\ldots,A_N) . Process Q queries, each of which is of one of the following two kinds: 1 x v : update A_x to v . 2 x : let B_i=\sum_{j=1}^{i}A_j , C_i=\sum_{j=1}^{i}B_j , and D_i=\sum_{j=1}^{i}C_j . Print D_x modulo 998244353 .
[ { "input": "3 3\n1 2 3\n2 3\n1 2 0\n2 3\n", "output": "15\n9\n" }, { "input": "2 1\n998244353 998244353\n2 1\n", "output": "0\n" } ]
https://atcoder.jp/contests/abc256/tasks/abc256_g
Problem Statement There is a regular N -gon with side length D . Starting from a vertex, we place black or white stones on the circumference at intervals of 1 . As a result, each edge of the N -gon will have (D+1) stones on it, for a total of ND stones. How many ways are there to place stones so that all edges have the same number of white stones on them? Find the count modulo 998244353 .
[ { "input": "3 2\n", "output": "10\n" }, { "input": "299792458 3141\n", "output": "138897974\n" } ]
https://atcoder.jp/contests/abc256/tasks/abc256_h
Problem Statement You are given N , Q , and A=(a_1,\ldots,a_N) . Process Q queries described below. Each query is of one of the following three kinds: 1 L R x : for i=L,L+1,\dots,R , update the value of a_i to \displaystyle \left\lfloor \frac{a_i}{x} \right\rfloor . 2 L R y : for i=L,L+1,\dots,R , update the value of a_i to y . 3 L R : print \displaystyle\sum_{i=L}^R a_i .
[ { "input": "3 5\n2 5 6\n3 1 3\n1 2 3 2\n3 1 2\n2 1 2 3\n3 1 3\n", "output": "13\n4\n9\n" }, { "input": "6 11\n10 3 5 20 6 7\n3 1 6\n1 2 4 3\n3 1 3\n2 1 4 10\n3 3 6\n1 3 6 2\n2 1 4 5\n3 1 6\n2 1 3 100\n1 2 5 6\n3 1 4\n", "output": "51\n12\n33\n26\n132\n" } ]
https://atcoder.jp/contests/abc255/tasks/abc255_a
Problem Statement Given integers R , C , and a 2 \times 2 matrix A , print A_{R,C} .
[ { "input": "1 2\n1 0\n0 1\n", "output": "0\n" }, { "input": "2 2\n1 2\n3 4\n", "output": "4\n" }, { "input": "2 1\n90 80\n70 60\n", "output": "70\n" } ]
https://atcoder.jp/contests/abc255/tasks/abc255_b
Problem Statement There are N people numbered 1, 2, \dots, N in the xy -plane. Person i is at the coordinates (X_i, Y_i) . K of these people, Persons A_1, A_2, \dots, A_K , will receive lights of the same strength. When a person at coordinates (x, y) has a light of strength R , it lights up the interior of a circle of radius R centered at (x, y) (including the boundary). Find the minimum strength of the lights needed for every person to be lit by at least one light.
[ { "input": "4 2\n2 3\n0 0\n0 1\n1 2\n2 0\n", "output": "2.23606797749978969\n" }, { "input": "2 1\n2\n-100000 -100000\n100000 100000\n", "output": "282842.712474619009\n" }, { "input": "8 3\n2 6 8\n-17683 17993\n93038 47074\n58079 -57520\n-41515 -89802\n-72739 68805\n24324 -73073\n71049 ...
https://atcoder.jp/contests/abc255/tasks/abc255_c
Problem Statement You are given an integer X . The following action on this integer is called an operation . Choose and do one of the following. Add 1 to X . Subtract 1 from X . The terms in the arithmetic progression S with N terms whose initial term is A and whose common difference is D are called good numbers . Consider performing zero or more operations to make X a good number. Find the minimum number of operations required to do so.
[ { "input": "6 2 3 3\n", "output": "1\n" }, { "input": "0 0 0 1\n", "output": "0\n" }, { "input": "998244353 -10 -20 30\n", "output": "998244363\n" }, { "input": "-555555555555555555 -1000000000000000000 1000000 1000000000000\n", "output": "444445\n" } ]
https://atcoder.jp/contests/abc255/tasks/abc255_d
Problem Statement You are given a sequence of length N : A=(A_1,A_2,\dots,A_N) . The following action on this sequence is called an operation . First, choose an integer i such that 1 \le i \le N . Next, choose and do one of the following. Add 1 to A_i . Subtract 1 from A_i . Answer Q questions. The i -th question is the following. Consider performing zero or more operations to change every element of A to X_i . Find the minimum number of operations required to do so.
[ { "input": "5 3\n6 11 2 5 5\n5\n20\n0\n", "output": "10\n71\n29\n" }, { "input": "10 5\n1000000000 314159265 271828182 141421356 161803398 0 777777777 255255255 536870912 998244353\n555555555\n321654987\n1000000000\n789456123\n0\n", "output": "3316905982\n2811735560\n5542639502\n4275864946\n4457...
https://atcoder.jp/contests/abc255/tasks/abc255_e
Problem Statement You are given a sequence of N-1 integers S = (S_1, S_2, \ldots, S_{N-1}) , and M distinct integers X_1, X_2, \ldots, X_M , which are called lucky numbers . A sequence of N integers A = (A_1, A_2, \ldots, A_N) satisfying the following condition is called a good sequence . A_i + A_{i+1} = S_i holds for every i = 1, 2, \ldots, N-1 . Find the maximum possible number of terms that are lucky numbers in a good sequence A , that is, the maximum possible number of integers i between 1 and N such that A_i \in \lbrace X_1, X_2, \ldots, X_M \rbrace .
[ { "input": "9 2\n2 3 3 4 -4 -7 -4 -1\n-1 5\n", "output": "4\n" }, { "input": "20 10\n-183260318 206417795 409343217 238245886 138964265 -415224774 -499400499 -313180261 283784093 498751662 668946791 965735441 382033304 177367159 31017484 27914238 757966050 878978971 73210901\n-470019195 -379631053 -...
https://atcoder.jp/contests/abc255/tasks/abc255_f
Problem Statement Consider a binary tree with N vertices numbered 1, 2, \ldots, N . Here, a binary tree is a rooted tree where each vertex has at most two children. Specifically, each vertex in a binary tree has at most one left child and at most one right child . Determine whether there exists a binary tree rooted at Vertex 1 satisfying the conditions below, and present such a tree if it exists. The depth-first traversal of the tree in pre-order is (P_1, P_2, \ldots, P_N) . The depth-first traversal of the tree in in-order is (I_1, I_2, \ldots, I_N) .
[ { "input": "6\n1 3 5 6 4 2\n3 5 1 4 6 2\n", "output": "3 6\n0 0\n0 5\n0 0\n0 0\n4 2\n" }, { "input": "2\n2 1\n1 2\n", "output": "-1\n" } ]
https://atcoder.jp/contests/abc255/tasks/abc255_g
Problem Statement Takahashi and Aoki will play a game against each other using N piles of stones. Initially, for each i = 1, 2, \ldots, N , the i -th pile is composed of A_i stones. The players alternately perform the following action, with Takahashi going first. Choose a pile with at least one stone remaining, and remove one or more stones. However, there are M forbidden moves. For each i = 1, 2, \ldots, M , it is not allowed to remove exactly Y_i stones from a pile composed of exactly X_i stones. The first player to become unable to perform the action loses, resulting in the other player's victory. Which player will win when both players employ the optimal strategy for the victory?
[ { "input": "3 4\n1 2 4\n2 1\n3 3\n3 1\n1 1\n", "output": "Takahashi\n" }, { "input": "1 5\n5\n5 1\n5 2\n5 3\n5 4\n5 5\n", "output": "Aoki\n" } ]
https://atcoder.jp/contests/abc255/tasks/abc255_h
Problem Statement There are N trees. On Day 0 , each tree bears no fruits. On the morning of Day 1 and subsequent days, the i -th tree bears i new fruits for each i = 1, 2, \ldots, N . Takahashi will perform Q harvesting. For each i = 1, 2, \ldots, Q , the i -th harvesting takes place on the night of Day D_i , collecting all fruits remaining on the L_i -th through R_i -th trees at that point. For each of the Q harvesting, print the number of fruits Takahashi will collect, modulo 998244353 .
[ { "input": "5 3\n2 2 3\n3 3 4\n5 1 5\n", "output": "10\n15\n50\n" }, { "input": "711741968710511029 1\n82803157126515475 516874290286751784 588060532191410838\n", "output": "603657470\n" } ]
https://atcoder.jp/contests/abc254/tasks/abc254_a
Problem Statement You are given an integer N at least 100 . Print the last two digits of N . Strictly speaking, print the tens and ones digits of N in this order.
[ { "input": "254\n", "output": "54\n" }, { "input": "101\n", "output": "01\n" } ]
https://atcoder.jp/contests/abc254/tasks/abc254_b
Problem Statement Find the N integer sequences A_0,\ldots,A_{N-1} defined as follows. For each i (0\leq i \leq N-1) , the length of A_i is i+1 . For each i and j (0\leq i \leq N-1, 0 \leq j \leq i) , the (j+1) -th term of A_i , denoted by a_{i,j} , is defined as follows. a_{i,j}=1 , if j=0 or j=i . a_{i,j} = a_{i-1,j-1} + a_{i-1,j} , otherwise.
[ { "input": "3\n", "output": "1\n1 1\n1 2 1\n" }, { "input": "10\n", "output": "1\n1 1\n1 2 1\n1 3 3 1\n1 4 6 4 1\n1 5 10 10 5 1\n1 6 15 20 15 6 1\n1 7 21 35 35 21 7 1\n1 8 28 56 70 56 28 8 1\n1 9 36 84 126 126 84 36 9 1\n" } ]
https://atcoder.jp/contests/abc254/tasks/abc254_c
Problem Statement We have a sequence of length N : A=(a_1,\ldots,a_N) . Additionally, you are given an integer K . You can perform the following operation zero or more times. Choose an integer i such that 1 \leq i \leq N-K , then swap the values of a_i and a_{i+K} . Determine whether it is possible to sort A in ascending order.
[ { "input": "5 2\n3 4 1 3 4\n", "output": "Yes\n" }, { "input": "5 3\n3 4 1 3 4\n", "output": "No\n" }, { "input": "7 5\n1 2 3 4 5 5 10\n", "output": "Yes\n" } ]
https://atcoder.jp/contests/abc254/tasks/abc254_d
Problem Statement You are given an integer N . Find the number of pairs (i,j) of positive integers at most N that satisfy the following condition: i \times j is a square number.
[ { "input": "4\n", "output": "6\n" }, { "input": "254\n", "output": "896\n" } ]
https://atcoder.jp/contests/abc254/tasks/abc254_e
Problem Statement We have a simple undirected graph with N vertices and M edges. The vertices are numbered 1,\ldots,N . For each i=1,\ldots,M , the i -th edge connects Vertex a_i and Vertex b_i . Additionally, the degree of each vertex is at most 3 . For each i=1,\ldots,Q , answer the following query. Find the sum of indices of vertices whose distances from Vertex x_i are at most k_i .
[ { "input": "6 5\n2 3\n3 4\n3 5\n5 6\n2 6\n7\n1 1\n2 2\n2 0\n2 3\n4 1\n6 0\n4 3\n", "output": "1\n20\n2\n20\n7\n6\n20\n" } ]
https://atcoder.jp/contests/abc254/tasks/abc254_f
Problem Statement You are given a positive integer N and sequences of N positive integers each: A=(A_1,A_2,\dots,A_N) and B=(B_1,B_2,\dots,B_N) . We have an N \times N grid. The square at the i -th row from the top and the j -th column from the left is called the square (i,j) . For each pair of integers (i,j) such that 1 \le i,j \le N , the square (i,j) has the integer A_i + B_j written on it. Process Q queries of the following form. You are given a quadruple of integers h_1,h_2,w_1,w_2 such that 1 \le h_1 \le h_2 \le N,1 \le w_1 \le w_2 \le N . Find the greatest common divisor of the integers contained in the rectangle region whose top-left and bottom-right corners are (h_1,w_1) and (h_2,w_2) , respectively.
[ { "input": "3 5\n3 5 2\n8 1 3\n1 2 2 3\n1 3 1 3\n1 1 1 1\n2 2 2 2\n3 3 1 1\n", "output": "2\n1\n11\n6\n10\n" }, { "input": "1 1\n9\n100\n1 1 1 1\n", "output": "109\n" } ]
https://atcoder.jp/contests/abc254/tasks/abc254_g
Problem Statement There is a complex composed of N 10^9 -story skyscrapers. The skyscrapers are numbered 1 to N , and the floors are numbered 1 to 10^9 . From any floor of any skyscraper, one can use a skybridge to get to the same floor of any other skyscraper in one minute. Additionally, there are M elevators. The i -th elevator runs between Floor B_i and Floor C_i of Skyscraper A_i . With this elevator, one can get from Floor x to Floor y of Skyscraper A_i in |x-y| minutes, for every pair of integers x,y such that B_i \le x,y \le C_i . Answer the following Q queries. Determine whether it is possible to get from Floor Y_i of Skyscraper X_i to Floor W_i of Skyscraper Z_i , and find the shortest time needed to get there if it is possible.
[ { "input": "3 4 3\n1 2 10\n2 3 7\n3 9 14\n3 1 3\n1 3 3 14\n3 1 2 7\n1 100 1 101\n", "output": "12\n7\n-1\n" }, { "input": "1 1 1\n1 1 2\n1 1 1 2\n", "output": "1\n" } ]
https://atcoder.jp/contests/abc254/tasks/abc254_h
Problem Statement You are given multisets with N non-negative integers each: A=\{ a_1,\ldots,a_N \} and B=\{ b_1,\ldots,b_N \} . You can perform the operations below any number of times in any order. Choose a non-negative integer x in A . Delete one instance of x from A and add one instance of 2x instead. Choose a non-negative integer x in A . Delete one instance of x from A and add one instance of \left\lfloor \frac{x}{2} \right\rfloor instead. ( \lfloor x \rfloor is the greatest integer not exceeding x .) Your objective is to make A and B equal (as multisets). Determine whether it is achievable, and find the minimum number of operations needed to achieve it if it is achievable.
[ { "input": "3\n3 4 5\n2 4 6\n", "output": "2\n" }, { "input": "1\n0\n1\n", "output": "-1\n" } ]
https://atcoder.jp/contests/past202206-open/tasks/past202206_a
Problem Statement A hare and a tortoise are in a race. The first to run X meters wins. The hare runs A meters per second, and the tortoise runs B meters per second. However, at \frac{X}{2} meters from the start, the hare stops running and sleeps for C seconds. Then, it wakes up and continues running A meters per second. Find the winner of the race. If the two finish simultaneously, report that fact.
[ { "input": "100 5 3 15\n", "output": "Tortoise\n" }, { "input": "240 6 2 80\n", "output": "Tie\n" }, { "input": "10000 100 1 1000\n", "output": "Hare\n" } ]
https://atcoder.jp/contests/past202206-open/tasks/past202206_b
Problem Statement You are given a string S consisting of lowercase English letters. Now you will write, for every integer i such that 1 \le i \le |S|-1 , a two-letter string obtained by concatenating the i -th and the (i+1) -th characters of S . Find the string that you will write the most times. If there are multiple such strings, find the lexicographically smallest one.
[ { "input": "abcab\n", "output": "ab\n" }, { "input": "zyxwv\n", "output": "wv\n" }, { "input": "iiiiiiiiiiiiiiiiiiiiiiiiiiiiii\n", "output": "ii\n" } ]
https://atcoder.jp/contests/past202206-open/tasks/past202206_c
Problem Statement For integers N and M , let us define S_{N,M} as follows. S_{N,M} is a string of length M consisting of o and x . The k -th character from the beginning of S_{N,M} is o if N^k≤10^9 and x otherwise. Given the integers N and M , print S_{N,M} .
[ { "input": "1000 5\n", "output": "oooxx\n" }, { "input": "2 30\n", "output": "ooooooooooooooooooooooooooooox\n" }, { "input": "31622 30\n", "output": "ooxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" } ]
https://atcoder.jp/contests/past202206-open/tasks/past202206_d
Problem Statement We have pairs of lowercase English letters (a_1,b_1),(a_2,b_2),\dots,(a_N,b_N) . Two lowercase English letters u and v are said to be similar if there exists i such that (u,v)=(a_i,b_i) or (u,v)=(b_i,a_i) . Two strings U and V with the same length are said to be similar if U and V satisfy the following condition: You can make U equal V by repeatedly (possibly zero times) choosing one of the characters of U and replacing it with a similar character. Given strings S and T with the same length, determine if S and T are similar.
[ { "input": "1\nm n\nman\nnan\n", "output": "Yes\n" }, { "input": "1\nm n\nman\nmen\n", "output": "No\n" }, { "input": "3\na b\na e\nd e\nabc\nedc\n", "output": "Yes\n" }, { "input": "1\na b\nxyz\nxyz\n", "output": "Yes\n" } ]
https://atcoder.jp/contests/past202206-open/tasks/past202206_e
Problem Statement Find the N -th term of the sequence A constructed as follows. Initially, A is empty. For every integer i from 1 through 10^{100} in ascending order, do the following. If i \ge 2 , append i,i-1,\dots,2 in this order to the end of A . Then, append 1,2,\dots,i in this order to the end of A .
[ { "input": "10\n", "output": "4\n" }, { "input": "1\n", "output": "1\n" }, { "input": "123456789012345678\n", "output": "268452372\n" } ]
https://atcoder.jp/contests/past202206-open/tasks/past202206_f
Problem Statement Takahashi is playing with a shooter video game. The game screen has a grid with H horizontal rows and W vertical columns. The square at the i -th (1 \leq i \leq H) row from the top and j -th (1 \leq j \leq W) column from the left in the grid is called (i, j) . Each square is either an empty square or a brick square . Each brick square is painted in the color represented by a positive integer. The state of Square (i, j) is expressed by a non-negative integer S_{i, j} . If S_{i,j} = 0 , then the square is an empty square; if S_{i, j} is a positive integer, then the square is a brick square of color S_{i, j} . Let us call a brick square in the row other than the H -th row from the top a floating brick if the adjacent square below is an empty square. Initially, there is no floating brick. Now, Takahashi will do N operations, the i -th (1 \leq i \leq N) of which is as follows: Shoot Square (r_i, c_i) . If the square is an empty square, nothing happens; if it is a brick square, it changes to an empty square. Then, all the floating bricks fall. Strictly speaking, the following operation is performed until there is no floating brick: Choose one floating brick and swap that square with the adjacent square below. Print the state of each square after the N operations have finished, in the same format as S_{i, j} .
[ { "input": "3 3\n0 1 0\n1 2 0\n3 4 4\n1\n3 2\n", "output": "0 0 0\n1 1 0\n3 2 4\n" }, { "input": "1 1\n1\n1\n1 1\n", "output": "0\n" }, { "input": "2 3\n18459 0 35959\n150312 573935 612940\n3\n2 3\n1 2\n1 1\n", "output": "0 0 0\n150312 573935 35959\n" } ]
https://atcoder.jp/contests/past202206-open/tasks/past202206_g
Problem Statement You are given an undirected graph with N vertices and N-1 edges. The i -th (1 \leq i \leq N-1) edge connects Vertex A_i and Vertex B_i . Determine whether the given graph is a tree.
[ { "input": "4\n1 3\n3 4\n2 3\n", "output": "Yes\n" }, { "input": "4\n1 2\n2 3\n1 3\n", "output": "No\n" } ]
https://atcoder.jp/contests/past202206-open/tasks/past202206_h
Problem Statement Takahashi has N snacks. For i = 1, 2, \ldots, N , the i -th snack has a weight of w_i and a tastiness of v_i . Takahashi is going to choose any number (possibly 0 ) of the N snacks to take on tomorrow's school trip. Takahashi has two knapsacks. Every snack he takes on the school trip should fit in one of the two knapsacks. Here, the sum of weights of snacks in the first knapsack should be at most A , and the sum of weights of snacks in the second knapsack should be at most B . Find the maximum possible total tastiness of snacks that he can take on the school trip.
[ { "input": "6 8 9\n2 6\n4 1\n5 9\n3 1\n5 3\n5 8\n", "output": "24\n" }, { "input": "20 70 60\n7 94\n18 33\n14 26\n10 1\n9 57\n2 80\n19 74\n16 10\n15 18\n10 38\n13 90\n12 23\n3 3\n8 11\n18 10\n3 42\n3 66\n3 90\n10 2\n5 45\n", "output": "772\n" } ]
https://atcoder.jp/contests/past202206-open/tasks/past202206_i
Problem Statement You are given a rectangular grid with H horizontal rows and W vertical columns. The square at the i -th row from the top and the j -th column from the left is called Sqaure (i, j) . Each square is either a "square without a wall" or a "square with a wall". The initial state of Square (i, j) is represented by S_{i,j} , which means: . : it is a square without a wall. s : it is a square without a wall, and Snuke is there. a : it is a square without a wall, and there is a cargo in it. g : it is a square without a wall, and is a destination of the cargo. # : it is a square with a wall, which cannot be visited. It is guaranteed that there is exactly one cargo, one destination, and one Snuke in the grid. Snuke's objective is to carry the cargo to the destination. Snuke can do one of the following moves any number of times: Move to a square, without the cargo or a wall, vertically or horizontally adjacent to Snuke's current square. Push the cargo in the square vertically or horizontally adjacent to Snuke's current square to the next square without a wall in the direction of the cargo from Snuke, and move to the square that the cargo was originally in. Print the minimum number of moves required for Snuke to achieve the objective. If the objective is unachievable, print -1 instead.
[ { "input": "3 3\ns..\n.a.\n..g\n", "output": "5\n" }, { "input": "4 4\ns...\n.a#.\n....\n###g\n", "output": "13\n" }, { "input": "4 4\n...a\n.s..\n..g.\n....\n", "output": "-1\n" } ]
https://atcoder.jp/contests/past202206-open/tasks/past202206_j
Problem Statement You eat an apple every Saturday and Sunday. You do not eat one any other day. Given two dates S and T in YYYY-MM-DD format, find the number of apples you eat between S and T (inclusive). Here are some facts that may be useful. January 1 , 2022 is Saturday. The year X is a leap year if and only if one of the following is satisfied. X is a multiple of 4 but not a multiple of 100 X is a multiple of 400 What is YYYY-MM-DD format? YYYY-MM-DD format is a concatenation of a 4 -digit integer representing the year, a 2 -digit integer representing the month, and a 2 -digit integer representing the day, in this order, separated by - . Here, the month and the day are zero-padded to two digits. For example, January 23 , 4567 in YYYY-MM-DD format is 4567-01-23 .
[ { "input": "2022-01-01\n2022-01-08\n", "output": "3\n" }, { "input": "2024-02-26\n2024-03-02\n", "output": "1\n" }, { "input": "2022-01-01\n9999-12-31\n", "output": "832544\n" } ]
https://atcoder.jp/contests/past202206-open/tasks/past202206_k
Problem Statement You are given two strings S and T . Find the number of strings that are subsequences of at least one of S and T , modulo 998244353 . Here, a string A is said to be a subsequence of string B when the length of A is at least 1 and A can result from removing zero or more characters from B . For example, atcoder and ac are subsequences of atcoder , while atcoderr and ca are not.
[ { "input": "abc\nbca\n", "output": "10\n" }, { "input": "aa\naaaaa\n", "output": "5\n" }, { "input": "xzyyzxxzyy\nyzxyzyxzyx\n", "output": "758\n" } ]
https://atcoder.jp/contests/past202206-open/tasks/past202206_l
Problem Statement You are given strings S and T consisting of lowercase English letters. You can do the following operation between 0 and K times: choose one of the characters of S and replace it with any lowercase English letter. What is the length of the longest possible string that is a subsequence of S after the replacements and also a subsequence of T ? Here, a string x is said to be a subsequence of a string y if x can result from removing zero or more characters from y and concatenating the remaining strings without changing the order.
[ { "input": "strength\nslang\n1\n", "output": "4\n" }, { "input": "asdbvaklwebjkalhakslhgweqwbq\nobqweogkmawgjkoanboebeoqejkazxcnmvqse\n10\n", "output": "19\n" } ]
https://atcoder.jp/contests/past202206-open/tasks/past202206_m
Problem Statement Takahashi and Aoki will play a match where the first to win N games wins. The two players are equally skilled, and each of them wins a game with probability \frac{1}{2} , independently of the outcomes of the other games. There are no draws. A reversal is said to occur when the player with more wins changes in two games, as in going from 2-1 to 2-3 (now Aoki is in the lead). Find the expected value of the number of reversals until one player wins the match, modulo 998244353 (see Notes).
[ { "input": "2\n", "output": "748683265\n" }, { "input": "4\n", "output": "405536769\n" }, { "input": "621998\n", "output": "76706976\n" } ]
https://atcoder.jp/contests/past202206-open/tasks/past202206_n
Problem Statement There is a grid with H horizontal rows and W vertical columns. The square at the i -th row from the top and the j -th column from the left is called (i, j) . The state of each square is either an "empty square" or a "square with a wall". Initially, every square is an empty square. Ms. A wants to travel on the grid from (1, 1) to (H, W) . When Ms. A is at (i, j) , she may move to one of (i+1,j),(i,j+1),(i-1,j) , and (i,j-1) . She cannot go outside the grid or move to the square with a wall. Ms. B has decided to choose some of the squares to build walls so that it is impossible for Ms. A to reach (H, W) no matter how she moves. It costs Ms. B c_{i, j} yen (the currency in Japan) to build a wall on (i, j) . She cannot build a wall on (1, 1) or (H, W) . What is the minimum cost Ms. B has to pay so that the condition is satisfied? Also, print the way of building walls to achieve the minimum cost.
[ { "input": "2 3\n0 6 4\n2 3 0\n", "output": "7\n..#\n.#.\n" }, { "input": "4 3\n0 9 2\n9 3 9\n2 3 9\n3 9 0\n", "output": "7\n..#\n.#.\n#..\n...\n" }, { "input": "2 2\n0 1000000000\n1000000000 0\n", "output": "2000000000\n.#\n#.\n" } ]
https://atcoder.jp/contests/past202206-open/tasks/past202206_o
Problem Statement There is a coordinate plane P . Consider performing operations of plotting points in this plane and deleting them. Initially, no points are plotted in P . Process a total of Q queries in the following forms in the given order. + x y Plot a point at (x,y) in P . - x y Delete a point plotted at (x,y) in P . After processing each query, answer the following question. Consider all (unordered) pairs of points currently plotted in P ; there are \frac{k(k-1)}{2} such pairs, where k is the current number of points plotted in P . Find the sum of the following value (which is always an integer) over all those pairs modulo 998244353 : the doubled area of the triangle whose vertices are the origin (the point (0,0) ) and the points in the pair (if these three points lie on the same line, the area is assumed to be 0 ).
[ { "input": "5\n+ 2 3\n+ -1 2\n+ 1 -2\n- -1 2\n+ -1 -1\n", "output": "0\n7\n14\n7\n11\n" }, { "input": "10\n+ 100000 100000\n+ 100000 -100000\n+ 100000 100000\n+ 100000 -100000\n+ 100000 100000\n- 100000 100000\n- 100000 -100000\n- 100000 100000\n- 100000 -100000\n- 100000 100000\n", "output": "0...
https://atcoder.jp/contests/arc141/tasks/arc141_a
Problem Statement For a positive integer n , let \mathrm{str}(n) be the string representing n in decimal. We say that a positive integer n is periodic when there exists a positive integer m such that \mathrm{str}(n) is the concatenation of two or more copies of \mathrm{str}(m) . For example, 11 , 1212 , and 123123123 are periodic. You are given a positive integer N at least 11 . Find the greatest periodic number at most N . It can be proved that there is at least one periodic number at most N . You will get T test cases to solve.
[ { "input": "3\n1412\n23\n498650499498649123\n", "output": "1313\n22\n498650498650498650\n" } ]
https://atcoder.jp/contests/arc141/tasks/arc141_b
Problem Statement You are given positive integers N and M . Find the number of sequences A=(A_1,\ A_2,\ \dots,\ A_N) of N positive integers that satisfy the following conditions, modulo 998244353 . 1 \leq A_1 < A_2 < \dots < A_N \leq M . B_1 < B_2 < \dots < B_N , where B_i = A_1 \oplus A_2 \oplus \dots \oplus A_i . Here, \oplus denotes bitwise \mathrm{XOR} . What is bitwise \mathrm{XOR} ? The bitwise \mathrm{XOR} of non-negative integers A and B , A \oplus B , is defined as follows: When A \oplus B is written in base two, the digit in the 2^k 's place ( k \geq 0 ) is 1 if exactly one of A and B is 1 , and 0 otherwise. For example, we have 3 \oplus 5 = 6 (in base two: 011 \oplus 101 = 110 ). Generally, the bitwise \mathrm{XOR} of k non-negative integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k) . We can prove that this value does not depend on the order of p_1, p_2, p_3, \dots, p_k .
[ { "input": "2 4\n", "output": "5\n" }, { "input": "4 4\n", "output": "0\n" }, { "input": "10 123456789\n", "output": "205695670\n" } ]
https://atcoder.jp/contests/arc141/tasks/arc141_c
Problem Statement A string of length 2N , S=S_1S_2\dots S_{2N} , is said to be a parenthesis sequence when S is composed of N ( s and N ) s. Additionally, a parenthesis sequence S is said to be correct when it is one of the following. An empty string. The concatenation of ( , A , ) in this order, where A is a correct parenthesis sequence. The concatenation of A , B in this order, where A and B are non-empty correct parenthesis sequences. You are given two permutations P=(P_1,\ P_2,\ \dots,\ P_{2N}) and Q=(Q_1,\ Q_2,\ \dots,\ Q_{2N}) of the integers from 1 to 2N . Determine whether there exists a parenthesis sequence S=S_1S_2\dots S_{2N} that satisfies the following conditions. P is the lexicographically smallest permutation p of the integers from 1 to 2N such that S_{p_1}S_{p_2}\dots S_{p_{2N}} is a correct parenthesis sequence. Q is the lexicographically largest permutation p of the integers from 1 to 2N such that S_{p_1}S_{p_2}\dots S_{p_{2N}} is a correct parenthesis sequence. If such a parenthesis sequence exists, find one.
[ { "input": "2\n1 2 4 3\n4 3 1 2\n", "output": "())(\n" }, { "input": "2\n1 3 2 4\n4 3 2 1\n", "output": "-1\n" }, { "input": "3\n2 1 5 3 4 6\n6 5 3 4 2 1\n", "output": "-1\n" } ]
https://atcoder.jp/contests/arc141/tasks/arc141_d
Problem Statement We say that a set S of positive integers is good when, for any a,\ b \in S\ (a\neq b) , b is not a multiple of a . You are given a set of N integers between 1 and 2M (inclusive): S=\lbrace A_1,\ A_2,\ \dots,\ A_N\rbrace . For each i=1,\ 2,\ \dots,\ N , determine whether there exists a good set with M elements that is a subset of S containing A_i .
[ { "input": "5 3\n1 2 3 4 5\n", "output": "No\nYes\nYes\nYes\nYes\n" }, { "input": "4 4\n2 4 6 8\n", "output": "No\nNo\nNo\nNo\n" }, { "input": "13 10\n2 3 4 6 7 9 10 11 13 15 17 19 20\n", "output": "No\nNo\nYes\nYes\nYes\nYes\nYes\nYes\nYes\nYes\nYes\nYes\nNo\n" } ]
https://atcoder.jp/contests/arc141/tasks/arc141_e
Problem Statement There is an undirected graph with N^2 vertices. Initially, it has no edges. For each pair of integers (i,\ j) such that 0 \leq i,\ j < N , the graph has a corresponding vertex called (i,\ j) . You will get Q queries, which should be processed in order. The i -th query, which gives you four integers a_i,\ b_i,\ c_i,\ d_i , is as follows. For each k (0 \leq k < N) , add an edge between two vertices ((a_i+k) \bmod N,\ (b_i+k) \bmod N) and ((c_i+k) \bmod N,\ (d_i+k) \bmod N) . Then, print the current number of connected components in the graph.
[ { "input": "3 3\n0 0 1 2\n2 0 0 0\n1 1 2 2\n", "output": "6\n4\n4\n" }, { "input": "4 3\n0 0 2 2\n2 3 1 2\n1 1 3 3\n", "output": "14\n11\n11\n" }, { "input": "6 5\n0 0 1 1\n1 2 3 4\n1 1 5 3\n2 0 1 5\n5 0 3 3\n", "output": "31\n27\n21\n21\n19\n" } ]
https://atcoder.jp/contests/arc141/tasks/arc141_f
Problem Statement You are given N srings S_i\ (1\le i \le N) consisting of A , B , C , D . Consider the operation below on a string T consisting of A , B , C , D . Repeat the following until T contains none of the strings S_i as a substring. Choose an S_i and one of its occurrences in T , remove that occurrence from T , and concatenate the remaining parts. What is a substring? A substring of a string is its contiguous subsequence. For example, A , AB , and BC are substrings of ABC , while BA and AC are not. We say that the string T is bad when multiple strings can result from the operation above. Determine whether a bad string exists.
[ { "input": "3\nA\nB\nC\n", "output": "No\n" }, { "input": "1\nABA\n", "output": "Yes\n" }, { "input": "4\nCBA\nACB\nAD\nCAB\n", "output": "Yes\n" } ]
https://atcoder.jp/contests/abc253/tasks/abc253_a
Problem Statement Given integers a , b , and c , determine if b is the median of these integers.
[ { "input": "5 3 2\n", "output": "Yes\n" }, { "input": "2 5 3\n", "output": "No\n" }, { "input": "100 100 100\n", "output": "Yes\n" } ]
https://atcoder.jp/contests/abc253/tasks/abc253_b
Problem Statement There is a grid with H horizontal rows and W vertical columns, in which two distinct squares have a piece. The state of the squares is represented by H strings S_1, \dots, S_H of length W . S_{i, j} = o means that there is a piece in the square at the i -th row from the top and j -th column from the left; S_{i, j} = - means that the square does not have a piece. Here, S_{i, j} denotes the j -th character of the string S_i . Consider repeatedly moving one of the pieces to one of the four adjacent squares. It is not allowed to move the piece outside the grid. How many moves are required at minimum for the piece to reach the square with the other piece?
[ { "input": "2 3\n--o\no--\n", "output": "3\n" }, { "input": "5 4\n-o--\n----\n----\n----\n-o--\n", "output": "4\n" } ]
https://atcoder.jp/contests/abc253/tasks/abc253_c
Problem Statement We have a multiset of integers S , which is initially empty. Given Q queries, process them in order. Each query is of one of the following types. 1 x : Insert an x into S . 2 x c : Remove an x from S m times, where m = \mathrm{min}(c,( the number of x 's contained in S)) . 3 : Print ( maximum value of S)-( minimum value of S) . It is guaranteed that S is not empty when this query is given.
[ { "input": "8\n1 3\n1 2\n3\n1 2\n1 7\n3\n2 2 3\n3\n", "output": "1\n5\n4\n" }, { "input": "4\n1 10000\n1 1000\n2 100 3\n1 10\n", "output": "\n" } ]
https://atcoder.jp/contests/abc253/tasks/abc253_d
Problem Statement Find the sum of integers between 1 and N (inclusive) that are not multiples of A or B .
[ { "input": "10 3 5\n", "output": "22\n" }, { "input": "1000000000 314 159\n", "output": "495273003954006262\n" } ]
https://atcoder.jp/contests/abc253/tasks/abc253_e
Problem Statement How many integer sequences A=(A_1,\ldots,A_N) of length N satisfy all the conditions below? 1\le A_i \le M (1 \le i \le N) |A_i - A_{i+1}| \geq K (1 \le i \le N - 1) Since the count can be enormous, find it modulo 998244353 .
[ { "input": "2 3 1\n", "output": "6\n" }, { "input": "3 3 2\n", "output": "2\n" }, { "input": "100 1000 500\n", "output": "657064711\n" } ]
https://atcoder.jp/contests/abc253/tasks/abc253_f
Problem Statement We have an N \times M matrix, whose elements are initially all 0 . Process Q given queries. Each query is in one of the following formats. 1 l r x : Add x to every element in the l -th, (l+1) -th, \ldots , and r -th column. 2 i x : Replace every element in the i -th row with x . 3 i j : Print the (i, j) -th element.
[ { "input": "3 3 9\n1 1 2 1\n3 2 2\n2 3 2\n3 3 3\n3 3 1\n1 2 3 3\n3 3 2\n3 2 3\n3 1 2\n", "output": "1\n2\n2\n5\n3\n4\n" }, { "input": "1 1 10\n1 1 1 1000000000\n1 1 1 1000000000\n1 1 1 1000000000\n1 1 1 1000000000\n1 1 1 1000000000\n1 1 1 1000000000\n1 1 1 1000000000\n1 1 1 1000000000\n1 1 1 1000000...
https://atcoder.jp/contests/abc253/tasks/abc253_g
Problem Statement For an integer N greater than or equal to 2 , there are \frac{N(N - 1)}{2} pairs of integers (x, y) such that 1 \leq x \lt y \leq N . Consider the sequence of these pairs sorted in the increasing lexicographical order. Let (x_1, y_1), \dots, (x_{R - L + 1}, y_{R - L + 1}) be its L -th, (L+1) -th, \ldots , and R -th elements, respectively. On a sequence A = (1, \dots, N) , We will perform the following operation for i = 1, \dots, R-L+1 in this order: Swap A_{x_i} and A_{y_i} . Find the final A after all the operations. We say that (a, b) is smaller than (c, d) in the lexicographical order if and only if one of the following holds: a \lt c a = c and b \lt d
[ { "input": "5 3 6\n", "output": "5 1 2 3 4\n" }, { "input": "10 12 36\n", "output": "1 10 9 8 7 4 3 2 5 6\n" } ]
https://atcoder.jp/contests/abc253/tasks/abc253_h
Problem Statement We have a graph G with N vertices numbered 1 through N and 0 edges. You are given sequences u=(u_1,u_2,\ldots,u_M),v=(v_1,v_2,\ldots,v_M) of length M . You will perform the following operation (N-1) times. Choose i (1 \leq i \leq M) uniformly at random. Add to G an undirected edge connecting Vertices u_i and v_i . Note that the operation above will add a new edge connecting Vertices u_i and v_i even if G already has one or more edges between them. In other words, the resulting G may contain multiple edges. For each K=1,2,\ldots,N-1 , find the probability, modulo 998244353 , that G is a forest after the K -th operation. What is a forest? An undirected graph without a cycle is called a forest. A forest is not necessarily connected. Definition of probability modulo 998244353 We can prove that the sought probability is always a rational number. Moreover, under the Constraints of this problem, it is guaranteed that, when the sought probability is represented by an irreducible fraction \frac{y}{x} , x is indivisible by 998244353 . Then, we can uniquely determine an integer z between 0 and 998244352 (inclusive) such that xz \equiv y \pmod{998244353} . Print this z .
[ { "input": "3 2\n1 2\n2 3\n", "output": "1\n499122177\n" }, { "input": "4 5\n1 2\n1 2\n1 4\n2 3\n2 4\n", "output": "1\n758665709\n918384805\n" } ]