url
stringlengths
49
92
description
stringlengths
22
4.78k
cases
listlengths
0
6
https://atcoder.jp/contests/abc305/tasks/abc305_a
Problem Statement There is an ultramarathon course totaling 100\;\mathrm{km} . Water stations are set up every 5\;\mathrm{km} along the course, including the start and goal, for a total of 21 . Takahashi is at the N\;\mathrm{km} point of this course. Find the position of the nearest water station to him. Under the constraints of this problem, it can be proven that the nearest water station is uniquely determined.
[ { "input": "53\n", "output": "55\n" }, { "input": "21\n", "output": "20\n" }, { "input": "100\n", "output": "100\n" } ]
https://atcoder.jp/contests/abc305/tasks/abc305_b
Problem Statement There are 7 points A , B , C , D , E , F , and G on a straight line, in this order. (See also the figure below.) The distances between adjacent points are as follows. Between A and B : 3 Between B and C : 1 Between C and D : 4 Between D and E : 1 Between E and F : 5 Between F and G : 9 You are given two uppercase English letters p and q . Each of p and q is A , B , C , D , E , F , or G , and it holds that p \neq q . Find the distance between the points p and q .
[ { "input": "A C\n", "output": "4\n" }, { "input": "G B\n", "output": "20\n" }, { "input": "C F\n", "output": "10\n" } ]
https://atcoder.jp/contests/abc305/tasks/abc305_c
Problem Statement There is a grid with H rows and W columns. Let (i, j) denote the square at the i -th row from the top and the j -th column from the left. Initially, there was one cookie on each square inside a rectangle whose height and width were at least 2 squares long , and no cookie on the other squares. Formally, there was exactly one quadruple of integers (a,b,c,d) that satisfied all of the following conditions. 1 \leq a \lt b \leq H 1 \leq c \lt d \leq W There was one cookie on each square (i, j) such that a \leq i \leq b, c \leq j \leq d , and no cookie on the other squares. However, Snuke took and ate one of the cookies on the grid. The square that contained that cookie is now empty. As the input, you are given the state of the grid after Snuke ate the cookie. The state of the square (i, j) is given as the character S_{i,j} , where # means a square with a cookie, and . means a square without one. Find the square that contained the cookie eaten by Snuke. (The answer is uniquely determined.)
[ { "input": "5 6\n......\n..#.#.\n..###.\n..###.\n......\n", "output": "2 4\n" }, { "input": "3 2\n#.\n##\n##\n", "output": "1 2\n" }, { "input": "6 6\n..####\n..##.#\n..####\n..####\n..####\n......\n", "output": "2 5\n" } ]
https://atcoder.jp/contests/abc305/tasks/abc305_d
Problem Statement Takahashi keeps a sleep log. The log is represented as an odd-length sequence A=(A _ 1(=0), A _ 2,\ldots,A _ N) , where odd-numbered elements represent times he got up, and even-numbered elements represent times he went to bed. More formally, he had the following sleep sessions after starting the sleep log. For every integer i such that 1\leq i\leq\dfrac{N-1}2 , he fell asleep exactly A _ {2i} minutes after starting the sleep log and woke up exactly A _ {2i+1} minutes after starting the sleep log. He did not fall asleep or wake up at any other time. Answer the following Q questions. For the i -th question, you are given a pair of integers (l _ i,r _ i) such that 0\leq l _ i\leq r _ i\leq A _ N . What is the total number of minutes for which Takahashi was asleep during the r _ i-l _ i minutes from exactly l _ i minutes to r _ i minutes after starting the sleep log?
[ { "input": "7\n0 240 720 1320 1440 1800 2160\n3\n480 1920\n720 1200\n0 2160\n", "output": "480\n0\n960\n" }, { "input": "21\n0 20 62 192 284 310 323 324 352 374 409 452 486 512 523 594 677 814 838 946 1000\n10\n77 721\n255 541\n478 970\n369 466\n343 541\n42 165\n16 618\n222 592\n730 983\n338 747\n", "output": "296\n150\n150\n49\n89\n20\n279\n183\n61\n177\n" } ]
https://atcoder.jp/contests/abc305/tasks/abc305_e
Problem Statement There is a simple undirected graph with N vertices and M edges, where vertices are numbered from 1 to N , and edges are numbered from 1 to M . Edge i connects vertex a_i and vertex b_i . K security guards numbered from 1 to K are on some vertices. Guard i is on vertex p_i and has a stamina of h_i . All p_i are distinct. A vertex v is said to be guarded when the following condition is satisfied: there is at least one guard i such that the distance between vertex v and vertex p_i is at most h_i . Here, the distance between vertex u and vertex v is the minimum number of edges in the path connecting vertices u and v . List all guarded vertices in ascending order .
[ { "input": "5 5 2\n1 2\n2 3\n2 4\n3 5\n1 5\n1 1\n5 2\n", "output": "4\n1 2 3 5\n" }, { "input": "3 0 1\n2 3\n", "output": "1\n2\n" }, { "input": "10 10 2\n2 1\n5 1\n6 1\n2 4\n2 5\n2 10\n8 5\n8 6\n9 6\n7 9\n3 4\n8 2\n", "output": "7\n1 2 3 5 6 8 9\n" } ]
https://atcoder.jp/contests/abc305/tasks/abc305_f
Problem Statement This is an interactive problem (where your program and the judge program interact through Standard Input and Output). There is a simple connected undirected graph with N vertices and M edges. The vertices are numbered with integers from 1 to N . Initially, you are at vertex 1 . Repeat moving to an adjacent vertex at most 2N times to reach vertex N . Here, you do not initially know all edges of the graph, but you will be informed of the vertices adjacent to the vertex you are at.
[]
https://atcoder.jp/contests/abc305/tasks/abc305_g
Problem Statement You are given a set S=\lbrace s _ 1,s _ 2,\ldots,s _ M\rbrace of non-empty strings of length at most 6 consisting of a and b . Find the number of strings T of length N consisting of a and b that satisfy the following condition: T does not contain s _ i as a consecutive substring for any s _ i\in S . Since the answer can be enormous, find it modulo 998244353 .
[ { "input": "4 3\naab\nbbab\nabab\n", "output": "10\n" }, { "input": "20 1\naa\n", "output": "17711\n" }, { "input": "1000000007 28\nbbabba\nbbbbaa\naabbab\nbbbaba\nbaaabb\nbabaab\nbbaaba\naabaaa\naaaaaa\naabbaa\nbbaaaa\nbbaabb\nbbabab\naababa\nbaaaba\nababab\nabbaba\naabaab\nababaa\nabbbba\nbaabaa\naabbbb\nabbbab\nbaaaab\nbaabbb\nababbb\nbaabba\nabaaaa\n", "output": "566756841\n" } ]
https://atcoder.jp/contests/abc305/tasks/abc305_h
Problem Statement You have decided to practice . Practice means solving a large number of problems in AtCoder. Practice takes place over several days. The practice for one day is carried out in the following steps. Let M be the number of problems to be solved on that day. Each problem has a value called difficulty , which is a pair of non-negative integers (A, B) . First, rearrange the M problems in any order. Then, solve the problems one by one in that order. You have a value called fatigue . The fatigue at the beginning of the day is 0 , and it changes from x to Ax + B when solving a problem with difficulty (A, B) . The fatigue at the end of solving all M problems is called the energy consumed for that day. There is a sequence of N problems in AtCoder, called problem 1 , problem 2 , \dots , problem N in order. The difficulty of problem i is (A_i, B_i) . You have decided to solve all N problems through practice. Practice is carried out in the following steps. Below, let [L, R] denote the following sequence of problems: problem L , problem L+1 , ... , problem R . Choose an integer K freely between 1 and N , inclusive. The practice will last K days. Divide the sequence of N problems into K non-empty continuous subsequences, and let S_i be the i -th sequence. Formally, choose a strictly increasing non-negative integer sequence x_0, x_1, \dots, x_K such that 1 = x_0 and x_K = N + 1 , and let S_i = [x_{i-1}, x_i - 1] for 1 \leq i \leq K . Then, for i=1, 2, \dots, K , solve all problems in S_i on the i -th day of practice. You have decided to practice so that the total energy consumed throughout the practice is at most X . Let D be the minimum possible value of K , the number of days, for such a practice. (Here, it is guaranteed that \sum_{i = 1}^N B_i \leq X . Under this constraint, such D always exists.) Also, let M be the minimum possible total energy consumed throughout a practice such that K=D . Find D and M .
[ { "input": "3 100\n2 2\n3 4\n5 7\n", "output": "1 52\n" }, { "input": "3 30\n2 2\n3 4\n5 7\n", "output": "2 17\n" }, { "input": "5 50000000\n100000 10000000\n100000 10000000\n100000 10000000\n100000 10000000\n100000 10000000\n", "output": "5 50000000\n" }, { "input": "10 100000000\n5 88\n66 4\n52 1\n3 1\n12 1\n53 25\n11 12\n12 2\n1 20\n47 10\n", "output": "2 73647\n" }, { "input": "15 100000000\n2387 3178\n2369 5772\n1 29\n36 3\n52 2981\n196 1\n36 704\n3 3\n1501 5185\n23 628\n3623 810\n80 101\n6579 15\n681 7\n183 125\n", "output": "4 54468135\n" } ]
https://atcoder.jp/contests/abc304/tasks/abc304_a
Problem Statement There are N people numbered 1, 2, \ldots, N , sitting in this clockwise order around a round table. In particular, person 1 is sitting next to person N in the clockwise direction. For each i = 1, 2, \ldots, N , person i has a name S_i and an age A_i . Here, no two people have the same name or the same age. Starting from the youngest person, print the names of all N people in the order of their seating positions in clockwise order.
[ { "input": "5\nalice 31\nbob 41\ncarol 5\ndave 92\nellen 65\n", "output": "carol\ndave\nellen\nalice\nbob\n" }, { "input": "2\ntakahashi 1000000000\naoki 999999999\n", "output": "aoki\ntakahashi\n" } ]
https://atcoder.jp/contests/abc304/tasks/abc304_b
Problem Statement You are given an integer N . Print an approximation of N according to the following instructions. If N is less than or equal to 10^3-1 , print N as it is. If N is between 10^3 and 10^4-1 , inclusive, truncate the ones digit of N and print the result. If N is between 10^4 and 10^5-1 , inclusive, truncate the tens digit and all digits below it of N and print the result. If N is between 10^5 and 10^6-1 , inclusive, truncate the hundreds digit and all digits below it of N and print the result. If N is between 10^6 and 10^7-1 , inclusive, truncate the thousands digit and all digits below it of N and print the result. If N is between 10^7 and 10^8-1 , inclusive, truncate the ten-thousands digit and all digits below it of N and print the result. If N is between 10^8 and 10^9-1 , inclusive, truncate the hundred-thousands digit and all digits below it of N and print the result.
[ { "input": "20230603\n", "output": "20200000\n" }, { "input": "0\n", "output": "0\n" }, { "input": "304\n", "output": "304\n" }, { "input": "500600\n", "output": "500000\n" } ]
https://atcoder.jp/contests/abc304/tasks/abc304_c
Problem Statement There are N people numbered 1, 2, \ldots, N on a two-dimensional plane, and person i is at the point represented by the coordinates (X_i,Y_i) . Person 1 has been infected with a virus. The virus spreads to people within a distance of D from an infected person. Here, the distance is defined as the Euclidean distance, that is, for two points (a_1, a_2) and (b_1, b_2) , the distance between these two points is \sqrt {(a_1-b_1)^2 + (a_2-b_2)^2} . After a sufficient amount of time has passed, that is, when all people within a distance of D from person i are infected with the virus if person i is infected, determine whether person i is infected with the virus for each i .
[ { "input": "4 5\n2 -1\n3 1\n8 8\n0 5\n", "output": "Yes\nYes\nNo\nYes\n" }, { "input": "3 1\n0 0\n-1000 -1000\n1000 1000\n", "output": "Yes\nNo\nNo\n" }, { "input": "9 4\n3 2\n6 -1\n1 6\n6 5\n-2 -3\n5 3\n2 -3\n2 1\n2 6\n", "output": "Yes\nNo\nNo\nYes\nYes\nYes\nYes\nYes\nNo\n" } ]
https://atcoder.jp/contests/abc304/tasks/abc304_d
Problem Statement There is a rectangular cake with some strawberries on the xy -plane. The cake occupies the rectangular area \lbrace (x, y) : 0 \leq x \leq W, 0 \leq y \leq H \rbrace . There are N strawberries on the cake, and the coordinates of the i -th strawberry are (p_i, q_i) for i = 1, 2, \ldots, N . No two strawberries have the same coordinates. Takahashi will cut the cake into several pieces with a knife, as follows. First, cut the cake along A different lines parallel to the y -axis: lines x = a_1 , x = a_2 , \ldots , x = a_A . Next, cut the cake along B different lines parallel to the x -axis: lines y = b_1 , y = b_2 , \ldots , y = b_B . As a result, the cake will be divided into (A+1)(B+1) rectangular pieces. Takahashi will choose just one of these pieces to eat. Print the minimum and maximum possible numbers of strawberries on the chosen piece. Here, it is guaranteed that there are no strawberries along the edges of the final pieces. For a more formal description, refer to the constraints below.
[ { "input": "7 6\n5\n6 1\n3 1\n4 2\n1 5\n6 2\n2\n2 5\n2\n3 4\n", "output": "0 2\n" }, { "input": "4 4\n4\n1 1\n3 1\n3 3\n1 3\n1\n2\n1\n2\n", "output": "1 1\n" } ]
https://atcoder.jp/contests/abc304/tasks/abc304_e
Problem Statement You are given an undirected graph G with N vertices and M edges. For i = 1, 2, \ldots, M , the i -th edge is an undirected edge connecting vertices u_i and v_i . A graph with N vertices is called good if the following condition holds for all i = 1, 2, \ldots, K : there is no path connecting vertices x_i and y_i in G . The given graph G is good. You are given Q independent questions. Answer all of them. For i = 1, 2, \ldots, Q , the i -th question is as follows. Is the graph G^{(i)} obtained by adding an undirected edge connecting vertices p_i and q_i to the given graph G good?
[ { "input": "6 6\n1 2\n2 3\n2 3\n3 1\n5 4\n5 5\n3\n1 5\n2 6\n4 3\n4\n2 5\n2 6\n5 6\n5 4\n", "output": "No\nNo\nYes\nYes\n" } ]
https://atcoder.jp/contests/abc304/tasks/abc304_f
Problem Statement Takahashi and Aoki will work part-time for the next N days. Takahashi's shift schedule is given by the string S , where the i -th character of S is # if he works on the i -th day, and . if he is absent on the i -th day. Based on this, Aoki created his shift schedule as follows. First, take a positive divisor M of N that is not equal to N . Next, decide the attendance for the first M days. Finally, for i = 1, 2, \ldots, N - M in this order, decide the attendance for the (M + i) -th day so that it matches the attendance for the i -th day. Note that different values of M may lead to the same final shift schedule. Find the number of possible shift schedules for Aoki such that at least one of Takahashi and Aoki works on each of the N days, modulo 998244353 .
[ { "input": "6\n##.#.#\n", "output": "3\n" }, { "input": "7\n...####\n", "output": "1\n" }, { "input": "12\n####.####.##\n", "output": "19\n" } ]
https://atcoder.jp/contests/abc304/tasks/abc304_g
Problem Statement You are given a sequence of length 2N : A = (A_1, A_2, \ldots, A_{2N}) . Find the maximum value that can be obtained as the median of the length- N sequence (A_1 \oplus A_2, A_3 \oplus A_4, \ldots, A_{2N-1} \oplus A_{2N}) by rearranging the elements of the sequence A . Here, \oplus represents bitwise exclusive OR. What is bitwise exclusive OR? The bitwise exclusive OR of non-negative integers A and B , denoted as A \oplus B , is defined as follows: The number at the 2^k position ( k \geq 0 ) in the binary representation of A \oplus B is 1 if and only if exactly one of the numbers at the 2^k position in the binary representation of A and B is 1 , and it is 0 otherwise. For example, 3 \oplus 5 = 6 (in binary notation: 011 \oplus 101 = 110 ). Also, for a sequence B of length L , the median of B is the value at the \lfloor \frac{L}{2} \rfloor + 1 -th position of the sequence B' obtained by sorting B in ascending order.
[ { "input": "4\n4 0 0 11 2 7 9 5\n", "output": "14\n" }, { "input": "1\n998244353 1000000007\n", "output": "1755654\n" }, { "input": "5\n1 2 4 8 16 32 64 128 256 512\n", "output": "192\n" } ]
https://atcoder.jp/contests/abc304/tasks/abc304_h
Problem Statement You are given a directed graph with N vertices and M edges. For i = 1, 2, \ldots, M , the i -th edge is directed from vertex s_i to vertex t_i . Determine whether there is a permutation P = (P_1, P_2, \ldots, P_N) of (1, 2, \ldots, N) that satisfies both of the following conditions, and if there is, provide an example. P_{s_i} \lt P_{t_i} for all i = 1, 2, \ldots, M . L_i \leq P_i \leq R_i for all i = 1, 2, \ldots, N .
[ { "input": "5 4\n1 5\n2 1\n2 5\n4 3\n1 5\n1 3\n3 4\n1 3\n4 5\n", "output": "Yes\n3 1 4 2 5\n" }, { "input": "2 2\n1 2\n2 1\n1 2\n1 2\n", "output": "No\n" } ]
https://atcoder.jp/contests/past15-open/tasks/past202306_a
Problem Statement Takahashi participated in a contest. He made A wrong submissions before being accepted T minutes into the contest. Compute the penalty value represented by the following expression: 5\times A + T.
[ { "input": "3 10\n", "output": "25\n" }, { "input": "1 8\n", "output": "13\n" }, { "input": "0 10\n", "output": "10\n" } ]
https://atcoder.jp/contests/past15-open/tasks/past202306_b
Problem Statement In AtCoder, the programming contest hosting website, those whose maximum rating is 2800 or greater is in the "hall of fame". Takahashi's current rating is C and the maximum rating is H . Print o if he is in the "hall of fame", and x otherwise.
[ { "input": "3085 3085\n", "output": "o\n" }, { "input": "2580 2631\n", "output": "x\n" }, { "input": "0 2800\n", "output": "o\n" } ]
https://atcoder.jp/contests/past15-open/tasks/past202306_c
Problem Statement On a two-dimensional plane, there is a circle of radius R centered at point (X, Y) . For an integer pair (i, j) , let us define S_{i,j} = # if point (i, j) is in the interior or on the circumference of the circle, and S_{i,j} = . otherwise. Print S_{i,j} for all integer pairs (i, j) such that -N \leq i,j \leq N .
[ { "input": "1 -2 1 3\n", "output": ". . . . . . .\n. . . . . . .\n. . . . . . .\n. # . . . . .\n# # # . . . .\n. # . . . . .\n. . . . . . .\n" }, { "input": "-3 1 4 8\n", "output": ". . . . . . . . . . . . . . . . .\n. . . . . . . . . # . . . . . . .\n. . . . . . . # # # # # . . . . .\n. . . . . . # # # # # # # . . . .\n. . . . . . # # # # # # # . . . .\n. . . . . # # # # # # # # # . . .\n. . . . . . # # # # # # # . . . .\n. . . . . . # # # # # # # . . . .\n. . . . . . . # # # # # . . . . .\n. . . . . . . . . # . . . . . . .\n. . . . . . . . . . . . . . . . .\n. . . . . . . . . . . . . . . . .\n. . . . . . . . . . . . . . . . .\n. . . . . . . . . . . . . . . . .\n. . . . . . . . . . . . . . . . .\n. . . . . . . . . . . . . . . . .\n. . . . . . . . . . . . . . . . .\n" }, { "input": "0 0 10 10\n", "output": ". . . . . . . . . . # . . . . . . . . . .\n. . . . . . # # # # # # # # # . . . . . .\n. . . . # # # # # # # # # # # # # . . . .\n. . . # # # # # # # # # # # # # # # . . .\n. . # # # # # # # # # # # # # # # # # . .\n. . # # # # # # # # # # # # # # # # # . .\n. # # # # # # # # # # # # # # # # # # # .\n. # # # # # # # # # # # # # # # # # # # .\n. # # # # # # # # # # # # # # # # # # # .\n. # # # # # # # # # # # # # # # # # # # .\n# # # # # # # # # # # # # # # # # # # # #\n. # # # # # # # # # # # # # # # # # # # .\n. # # # # # # # # # # # # # # # # # # # .\n. # # # # # # # # # # # # # # # # # # # .\n. # # # # # # # # # # # # # # # # # # # .\n. . # # # # # # # # # # # # # # # # # . .\n. . # # # # # # # # # # # # # # # # # . .\n. . . # # # # # # # # # # # # # # # . . .\n. . . . # # # # # # # # # # # # # . . . .\n. . . . . . # # # # # # # # # . . . . . .\n. . . . . . . . . . # . . . . . . . . . .\n" } ]
https://atcoder.jp/contests/past15-open/tasks/past202306_d
Problem Statement Takahashi is trying to record a certain television program (hereafter called "the program") with a recorder. At time 0 , the program is scheduled to be broadcasted from time A to time A + R . However, the broadcast time of the program may change due to programming circumstances. Therefore, Takahashi has decided to take the following actions to record the entire program. Perform the following operation at all times that are multiples of D after time 0 (including time 0 ). (This operation takes no time.) Let t be the current time, and p and q be the program's start and end times scheduled at time t , respectively. If t \lt q , reserve to record from time \max(p, t) to time q . Here, note that Takahashi's recorder can handle multiple recording reservations. In other words, even if Takahashi makes a new recording reservation when there is already one or more recording reservations, the previous recording reservations are not canceled , and all recording reservations are carried out. As mentioned earlier, the program was scheduled to be broadcasted from time A to time A + R at time 0 . However, at time B , the broadcast time of the program was changed to be from time C to time C + R . (Here, it is guaranteed that B < A , B < C , A \neq C , and none of A, B, C is a multiple of D .) Please determine whether Takahashi can record the entire program. More precisely, determine whether the following proposition is true. For all times t between C and C + R , inclusive, there is at least one recording reservation that satisfies the following conditions. If the recording reservation is from time p to time q , it holds that p \leq t \leq q .
[ { "input": "5 3 15 20 20\n", "output": "Yes\n" }, { "input": "5 3 27 30 20\n", "output": "No\n" }, { "input": "930 542 605 509 582\n", "output": "No\n" } ]
https://atcoder.jp/contests/past15-open/tasks/past202306_e
Problem Statement In a certain programming contest, N problems are set, and if you answer the i -th problem correctly, you are given A_i points. Consider choosing and solving exactly K problems out of these N problems. When you calculate the "total score obtained when all the selected problems are solved" for all ways of choosing problems, what is the sum of those total scores?
[ { "input": "4 2\n100 200 300 400\n", "output": "3000\n" }, { "input": "1 1\n2718\n", "output": "2718\n" }, { "input": "8 4\n597 1035 2048 994 1843 601 7 659\n", "output": "272440\n" } ]
https://atcoder.jp/contests/past15-open/tasks/past202306_f
Problem Statement Given a sequence A of length N , find a sequence B that satisfies all of the following conditions. We can prove that this sequence B is unique. B is a sequence obtained by rearranging (1,2,...,N) . For all integer pairs (i,j)(i \neq j) , the following conditions are satisfied: if A_i<A_j , then B_i<B_j ; if A_i=A_j and i<j , then B_i<B_j .
[ { "input": "10\n2 3 4 4 4 4 3 3 2 1\n", "output": "2 4 7 8 9 10 5 6 3 1\n" }, { "input": "1\n1000000000\n", "output": "1\n" }, { "input": "20\n30 10 40 10 50 90 20 60 50 30 50 80 90 70 90 30 20 30 80 40\n", "output": "5 1 9 2 11 18 3 14 12 6 13 16 19 15 20 7 4 8 17 10\n" } ]
https://atcoder.jp/contests/past15-open/tasks/past202306_g
Problem Statement Determine if there is a way to assign 0 or 1 to each of N variables x_1,x_2,\ldots,x_N so that: for all i=1,2,\ldots,M , there exists an integer j (1 \leq j \leq k_i) such that x_{a_{i,j}}=b_{i,j} .
[ { "input": "5 3\n1\n4 0\n1\n5 0\n2\n2 0\n3 1\n", "output": "Yes\n" }, { "input": "1 2\n1\n1 0\n1\n1 1\n", "output": "No\n" } ]
https://atcoder.jp/contests/past15-open/tasks/past202306_h
Problem Statement When you represent an integer N as the sum of one or more distinct positive integers, at most how many positive integers can be used?
[ { "input": "7\n", "output": "3\n" }, { "input": "1\n", "output": "1\n" }, { "input": "164312478120341038\n", "output": "573258192\n" } ]
https://atcoder.jp/contests/past15-open/tasks/past202306_i
Problem Statement You are given N integers A_1, A_2, \ldots, A_N . Consider choosing K of them. Find the maximum possible greatest common divisor of the chosen integers.
[ { "input": "3 2\n2 3 4\n", "output": "2\n" }, { "input": "3 3\n5 5 5\n", "output": "5\n" }, { "input": "10 4\n225707 265514 765350 617700 837712 187034 799142 35549 784661 961512\n", "output": "2\n" } ]
https://atcoder.jp/contests/past15-open/tasks/past202306_j
Problem Statement A ninja will fight against N monsters one by one in order. The attack power of the i -th monster is a_i , and its health is b_i . A monster is immediately defeated when its health becomes 0 or less. Each monster is either on the ground or flying. If x_i = 0 , the i -th monster is on the ground, and if x_i = 1 , it is flying. The ninja initially has M shurikens. Each shuriken can be used only once by throwing it. The ninja and the i -th monster fight as follows: First, the ninja chooses zero or more shurikens from the ones he has and throws them at the monster. The monster's health decreases by the number of shurikens thrown, and if one or more shurikens are thrown at a flying monster, it lands on the ground. Then, the ninja and the monster take turns attacking each other. Each attack by the ninja decreases the monster's health by 1 , and each attack by the monster decreases the ninja's health by a_i . Here, if the monster is on the ground, the ninja attacks first, and if it is flying, the monster attacks first. Find the minimum possible difference between the ninja's initial health and his health after defeating all the monsters. Assume that the ninja's health is sufficiently large.
[ { "input": "2 1\n2 2 1\n5 2 0\n", "output": "4\n" }, { "input": "1 2\n1 4 1\n", "output": "1\n" }, { "input": "4 3\n5 4 1\n6 2 0\n4 5 1\n3 2 0\n", "output": "25\n" } ]
https://atcoder.jp/contests/past15-open/tasks/past202306_k
Problem Statement There is a sequence of length N , P=(P_1,P_2,\ldots,P_N) , which is a permutation of (1,2,\ldots,N) . You can perform the following operation on this sequence any number of times, possibly zero: Choose an integer i between 1 and N-1 , inclusive, and swap the i -th element and the (i+1) -th element of P . The cost of this operation is the sum of the two numbers being swapped. Find the minimum cost required to sort P in ascending order.
[ { "input": "4\n3 2 1 4\n", "output": "12\n" }, { "input": "9\n3 1 4 5 9 2 6 8 7\n", "output": "96\n" }, { "input": "6\n1 2 3 4 5 6\n", "output": "0\n" } ]
https://atcoder.jp/contests/past15-open/tasks/past202306_l
Problem Statement You are given an H -by- W matrix A consisting of non-negative integers. You can perform the following operations any number of times (possibly zero): Choose an integer i satisfying 1 \le i \le H and a non-negative integer X , and replace each element A_{i,j} in the i -th row of the matrix A with A_{i,j}\ \mathrm{XOR}\ X . Choose an integer j satisfying 1 \le j \le W and a non-negative integer X , and replace each element A_{i,j} in the j -th column of the matrix A with A_{i,j}\ \mathrm{XOR}\ X . You are also given an H -by- W matrix B consisting of non-negative integers and -1 . Determine if it is possible to make A_{i,j}=B_{i,j} for all pairs of integers (i,j) such that B_{i,j} \neq -1 by performing the operations. What is \mathrm{XOR} ? The bitwise \mathrm{XOR} of non-negative integers A, B , denoted as A \oplus B , is defined as follows: The 2^k ( k \geq 0 ) place of A \oplus B in binary notation is 1 if exactly one of the 2^k places of A and B in binary notation is 1 , and 0 otherwise. For example, 3 \oplus 5 = 6 (in binary notation: 011 \oplus 101 = 110 ). In general, the bitwise \mathrm{XOR} of k 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) , and it can be proven that this does not depend on the order of p_1, p_2, p_3, \dots p_k .
[ { "input": "2 2\n1 2\n3 4\n-1 3\n1 7\n", "output": "Yes\n" }, { "input": "2 2\n1 2\n3 4\n5 3\n1 7\n", "output": "No\n" } ]
https://atcoder.jp/contests/past15-open/tasks/past202306_m
Problem Statement There are N points on a number line, numbered from 1 to N . The N points satisfy the following condition: The distance between point i and point i+1 is d_i . (1 \leq i \leq N - 1) Find the minimum possible distance between point 1 and point N .
[ { "input": "4\n5 2 4\n", "output": "1\n" }, { "input": "10\n314 159 265 358 979 323 846 264 338\n", "output": "2\n" } ]
https://atcoder.jp/contests/past15-open/tasks/past202306_n
Problem Statement There are N real numbers x_1,\ldots,x_N between 0 (inclusive) and M (exclusive). Among these N numbers, it is known that there are C_k numbers between k (inclusive) and k+1 (exclusive) for k=0,1,\ldots,M-1 . You are curious about how small the absolute difference between the median and the mean of x_1,\ldots,x_N can be. Find the largest b such that the absolute difference between the median and the mean of x_1,\ldots,x_N cannot be smaller than b .
[ { "input": "3 3\n0 1 2\n", "output": "0.0\n" }, { "input": "4 5\n2 1 0 0 1\n", "output": "0.25\n" }, { "input": "10 10\n1 0 0 0 0 0 0 0 0 9\n", "output": "0.4\n" } ]
https://atcoder.jp/contests/past15-open/tasks/past202306_o
Problem Statement You are given integers N , A , and B . Solve the following problem. An arithmetic sequence S = (S_1, S_2, \dots, S_N) of length N is defined as S_i = A \times (i - 1) + B . Find the number of prime numbers in S .
[ { "input": "10 3 4\n", "output": "4\n" }, { "input": "99 1 2\n", "output": "25\n" }, { "input": "314159 265358 499999999999\n", "output": "23166\n" } ]
https://atcoder.jp/contests/arc161/tasks/arc161_a
Problem Statement Let N be a positive odd number. A sequence of integers of length N , S = (S_1, S_2, \dots, S_N) , is said to be M-type if "for each even number i = 2, 4, \dots, N - 1 , it holds that S_{i-1} < S_i and S_i > S_{i+1} ". You are given a sequence of positive integers of length N , A = (A_1, A_2, \dots, A_N) . Determine if it is possible to rearrange A to be M-type.
[ { "input": "5\n1 2 3 4 5\n", "output": "Yes\n" }, { "input": "5\n1 6 1 6 1\n", "output": "Yes\n" }, { "input": "5\n1 6 6 6 1\n", "output": "No\n" } ]
https://atcoder.jp/contests/arc161/tasks/arc161_b
Problem Statement For a positive integer X , let f(X) be the number of 1 s that appear when X is represented in binary notation. For example, 6 = 110_{(2)}, \ 11 = 1011_{(2)}, \ 16 = 10000_{(2)} , so f(6) = 2, \ f(11) = 3, \ f(16) = 1 . You are given a positive integer N . Determine whether there is a positive integer X less than or equal to N such that f(X) = 3 , and if so, find the maximum such X . There are T test cases to solve.
[ { "input": "4\n16\n161\n4\n1000000000000000000\n", "output": "14\n161\n-1\n936748722493063168\n" } ]
https://atcoder.jp/contests/arc161/tasks/arc161_c
Problem Statement You are given a tree with N vertices. The vertices are labeled from 1 to N , and the i -th edge connects vertex A_i and vertex B_i . Moreover, for every vertex, the number of incident edges is odd . You will color each vertex of the given tree either black ( B ) or white ( W ). Here, the string obtained by arranging the colors ( B or W ) of the vertices in the order of vertex labels is called a color sequence . You are given a color sequence S . Determine whether the color sequence S can result from performing the following operation once when all vertices are colored, and if it can, find one possible color sequence before the operation. Operation: For each vertex k = 1, 2, \dots, N , let C_k be the color that occupies the majority (more than half) of the colors of the vertices connected to k by an edge. For every vertex k , change its color to C_k simultaneously. There are T test cases to solve.
[ { "input": "2\n4\n1 2\n1 3\n1 4\nBWWW\n4\n1 2\n1 3\n1 4\nBBWW\n", "output": "WBBW\n-1\n" } ]
https://atcoder.jp/contests/arc161/tasks/arc161_d
Problem Statement We define the density of a non-empty simple undirected graph as \displaystyle\frac{(\text{number\ of\ edges})}{(\text{number\ of\ vertices})} . You are given positive integers N and D . Determine whether there is a simple undirected graph G with N vertices and DN edges that satisfies the following condition. If it exists, find one such graph. Condition: Let V be the vertex set of G . For any non-empty proper subset X of V , the density of the induced subgraph of G by X is strictly less than D . What is an induced subgraph? For a vertex subset X of graph G , the induced subgraph of G by X is the graph with vertex set X and edge set containing all edges of G that connect two vertices in X . In the above condition, note that we only consider vertex subsets that are neither empty nor the entire set.
[ { "input": "3 1\n", "output": "Yes\n1 2\n1 3\n2 3\n" }, { "input": "4 2\n", "output": "No\n" } ]
https://atcoder.jp/contests/arc161/tasks/arc161_e
Problem Statement You are given a connected simple undirected graph with N vertices and \displaystyle\frac{3}{2}N edges, where N is a positive even number . The vertices are labeled from 1 to N , and the i -th edge connects vertex A_i and vertex B_i . Moreover, for every vertex, the number of incident edges is exactly 3 . You will color each vertex of the given graph either black ( B ) or white ( W ). Here, the string obtained by arranging the colors ( B or W ) of the vertices in the order of vertex labels is called a color sequence . Determine whether there is a color sequence that cannot result from performing the following operation once when all vertices are colored, and if there is such a color sequence, find one. Operation: For each vertex k = 1, 2, \dots, N , let C_k be the color that occupies the majority (more than half) of the colors of the vertices connected to k by an edge. For every vertex k , change its color to C_k simultaneously. There are T test cases to solve.
[ { "input": "2\n4\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4\n10\n1 2\n1 3\n1 4\n2 3\n2 4\n3 5\n4 5\n5 6\n6 7\n6 8\n7 9\n7 10\n8 9\n8 10\n9 10\n", "output": "BWWW\nBWWWBWWWBB\n" } ]
https://atcoder.jp/contests/arc161/tasks/arc161_f
Problem Statement We define the density of a non-empty simple undirected graph as \displaystyle\frac{(\text{number\ of\ edges})}{(\text{number\ of\ vertices})} . You are given positive integers N , D , and a simple undirected graph G with N vertices and DN edges. The vertices of G are numbered from 1 to N , and the i -th edge connects vertex A_i and vertex B_i . Determine whether G satisfies the following condition. Condition: Let V be the vertex set of G . For any non-empty proper subset X of V , the density of the induced subgraph of G by X is strictly less than D . There are T test cases to solve. What is an induced subgraph? For a vertex subset X of graph G , the induced subgraph of G by X is the graph with vertex set X and edge set containing all edges of G that connect two vertices in X . In the above condition, note that we only consider vertex subsets that are neither empty nor the entire set.
[ { "input": "2\n3 1\n1 2\n1 3\n2 3\n4 1\n1 2\n1 3\n2 3\n3 4\n", "output": "Yes\nNo\n" } ]
https://atcoder.jp/contests/abc303/tasks/abc303_a
Problem Statement Two characters x and y are called similar characters if and only if one of the following conditions is satisfied: x and y are the same character. One of x and y is 1 and the other is l . One of x and y is 0 and the other is o . Two strings S and T , each of length N , are called similar strings if and only if: for all i\ (1\leq i\leq N) , the i -th character of S and the i -th character of T are similar characters. Given two length- N strings S and T consisting of lowercase English letters and digits, determine if S and T are similar strings.
[ { "input": "3\nl0w\n1ow\n", "output": "Yes\n" }, { "input": "3\nabc\narc\n", "output": "No\n" }, { "input": "4\nnok0\nn0ko\n", "output": "Yes\n" } ]
https://atcoder.jp/contests/abc303/tasks/abc303_b
Problem Statement N people numbered 1,2,\ldots,N were in M photos. In each of the photos, they stood in a single line. In the i -th photo, the j -th person from the left is person a_{i,j} . Two people who did not stand next to each other in any of the photos may be in a bad mood. How many pairs of people may be in a bad mood? Here, we do not distinguish a pair of person x and person y , and a pair of person y and person x .
[ { "input": "4 2\n1 2 3 4\n4 3 1 2\n", "output": "2\n" }, { "input": "3 3\n1 2 3\n3 1 2\n1 2 3\n", "output": "0\n" }, { "input": "10 10\n4 10 7 2 8 3 9 1 6 5\n3 6 2 9 1 8 10 7 4 5\n9 3 4 5 7 10 1 8 2 6\n7 3 1 8 4 9 5 6 2 10\n5 2 1 4 10 7 9 8 3 6\n5 8 1 6 9 3 2 4 7 10\n8 10 3 4 5 7 2 9 6 1\n3 10 2 7 8 5 1 4 9 6\n10 6 1 5 4 2 3 8 9 7\n4 5 9 1 8 2 7 6 3 10\n", "output": "6\n" } ]
https://atcoder.jp/contests/abc303/tasks/abc303_c
Problem Statement On a two-dimensional plane, Takahashi is initially at point (0, 0) , and his initial health is H . M items to recover health are placed on the plane; the i -th of them is placed at (x_i,y_i) . Takahashi will make N moves. The i -th move is as follows. Let (x,y) be his current coordinates. He consumes a health of 1 to move to the following point, depending on S_i , the i -th character of S : (x+1,y) if S_i is R ; (x-1,y) if S_i is L ; (x,y+1) if S_i is U ; (x,y-1) if S_i is D . If Takahashi's health has become negative, he collapses and stops moving. Otherwise, if an item is placed at the point he has moved to, and his health is strictly less than K , then he consumes the item there to make his health K . Determine if Takahashi can complete the N moves without being stunned.
[ { "input": "4 2 3 1\nRUDL\n-1 -1\n1 0\n", "output": "Yes\n" }, { "input": "5 2 1 5\nLDRLD\n0 0\n-1 -1\n", "output": "No\n" } ]
https://atcoder.jp/contests/abc303/tasks/abc303_d
Problem Statement Your computer has a keyboard with three keys: 'a' key, Shift key, and Caps Lock key. The Caps Lock key has a light on it. Initially, the light on the Caps Lock key is off, and the screen shows an empty string. You can do the following three actions any number of times in any order: Spend X milliseconds to press only the 'a' key. If the light on the Caps Lock key is off, a is appended to the string on the screen; if it is on, A is. Spend Y milliseconds to press the 'a' key and Shift key simultaneously. If the light on the Caps Lock key is off, A is appended to the string on the screen; if it is on, a is. Spend Z milliseconds to press the Caps Lock key. If the light on the Caps Lock key is off, it turns on; if it is on, it turns off. Given a string S consisting of A and a , determine at least how many milliseconds you need to spend to make the string shown on the screen equal to S .
[ { "input": "1 3 3\nAAaA\n", "output": "9\n" }, { "input": "1 1 100\naAaAaA\n", "output": "6\n" }, { "input": "1 2 4\naaAaAaaAAAAaAaaAaAAaaaAAAAA\n", "output": "40\n" } ]
https://atcoder.jp/contests/abc303/tasks/abc303_e
Problem Statement A graph with (k+1) vertices and k edges is called a level- k\ (k\geq 2) star if and only if: it has a vertex that is connected to each of the other k vertices with an edge, and there are no other edges. At first, Takahashi had a graph consisting of stars. He repeated the following operation until every pair of vertices in the graph was connected: choose two vertices in the graph. Here, the vertices must be disconnected, and their degrees must be both 1 . Add an edge that connects the chosen two vertices. He then arbitrarily assigned an integer from 1 through N to each of the vertices in the graph after the procedure. The resulting graph is a tree; we call it T . T has (N-1) edges, the i -th of which connects u_i and v_i . Takahashi has now forgotten the number and levels of the stars that he initially had. Find them, given T .
[ { "input": "6\n1 2\n2 3\n3 4\n4 5\n5 6\n", "output": "2 2\n" }, { "input": "9\n3 9\n7 8\n8 6\n4 6\n4 1\n5 9\n7 3\n5 2\n", "output": "2 2 2\n" }, { "input": "20\n8 3\n8 18\n2 19\n8 20\n9 17\n19 7\n8 7\n14 12\n2 15\n14 10\n2 13\n2 16\n2 1\n9 5\n10 15\n14 6\n2 4\n2 11\n5 12\n", "output": "2 3 4 7\n" } ]
https://atcoder.jp/contests/abc303/tasks/abc303_f
Problem Statement A monster with health H has appeared in front of you, and a turn-based battle has started. In each turn 1,2,… , you cast one of N spells, spells 1,…,N . If you cast spell i in turn j , the monster's health reduces by d_i in each turn j,j+1,…,j+t_i -1 . Find the earliest turn that you can make the monster's health 0 or less.
[ { "input": "2 20\n2 2\n5 1\n", "output": "6\n" }, { "input": "10 200\n1 21\n1 1\n1 1\n8 4\n30 1\n3 1\n10 2\n8 1\n9 1\n4 4\n", "output": "9\n" } ]
https://atcoder.jp/contests/abc303/tasks/abc303_g
Problem Statement N bags are arranged in a row. The i -th bag contains x_i yen (the currency in Japan). Takahashi and Aoki, who have sufficient money, take alternating turns to perform the following action: choose one of the following three actions and perform it. choose the leftmost or rightmost bag and take it. pay A yen to Snuke. Then, repeat the following action \min(B,n) times (where n is the number of the remaining bags): choose the leftmost or rightmost bag and take it. pay C yen to Snuke. Then, repeat the following action \min(D,n) times (where n is the number of the remaining bags): choose the leftmost or rightmost bag and take it. When all the bags are taken, Takahashi's benefit is defined by "(total amount of money in the bags that Takahashi took) - (total amount of money that Takahashi paid to snuke)"; let this amount be X yen. We similarly define Aoki's benefit, denoting the amount by Y yen. Find X-Y if Takahashi and Aoki make optimal moves to respectively maximize and minimize X-Y .
[ { "input": "5 10 2 1000000000 1\n1 100 1 1 1\n", "output": "90\n" }, { "input": "10 45 3 55 4\n5 10 15 20 25 30 35 40 45 50\n", "output": "85\n" }, { "input": "15 796265 10 165794055 1\n18804175 185937909 1934689 18341 68370722 1653 1 2514380 31381214 905 754483 11 5877098 232 31600\n", "output": "302361955\n" } ]
https://atcoder.jp/contests/abc303/tasks/abc303_h
Problem Statement You are given an integer N and a set S=\lbrace S_1,S_2,\ldots,S_K\rbrace consisting of integers between 1 and N-1 . Find the number, modulo 998244353 , of trees T with N vertices numbered 1 through N such that: d_i\in S for all i\ (1\leq i \leq N) , where d_i is the degree of vertex i in T .
[ { "input": "4 2\n1 3\n", "output": "4\n" }, { "input": "10 5\n1 2 3 5 6\n", "output": "68521950\n" }, { "input": "100 5\n1 2 3 14 15\n", "output": "888770956\n" } ]
https://atcoder.jp/contests/agc062/tasks/agc062_a
Problem Statement For a string T=T_1T_2\dots T_n of length n\ (2\leq n) consisting of A and B , we define a string f(T) of length n-1 as follows. Let a_1 < a_2 < \dots < a_{m} be the indices i\ (1\leq i \leq n-1) such that T_i={} A , and b_1 < b_2 < \dots < b_k be the indices i\ (1\leq i \leq n-1) such that T_i={} B . Then, let f(T)=T_{a_1+1}T_{a_2+1}\dots T_{a_m+1}T_{b_1+1}T_{b_2+1}\dots T_{b_k+1} . For example, for the string T={} ABBABA , the indices i\ (1\leq i \leq 5) with T_i={} A are i=1,4 , and the indices i\ (1\leq i \leq 5) with T_i={} B are i=2,3,5 , so f(T) is T_{1+1}T_{4+1}T_{2+1}T_{3+1}T_{5+1}={} BBBAA . You are given a string S of length N consisting of A and B . Find S after replacing S with f(S) (N-1) times. You have T test cases to solve.
[ { "input": "3\n2\nAB\n3\nAAA\n4\nABAB\n", "output": "B\nA\nA\n" } ]
https://atcoder.jp/contests/agc062/tasks/agc062_b
Problem Statement There is a permutation A=(A_1,A_2,\dots,A_N) of the integers from 1 to N . Initially, we have A_i=i\ (1\leq i \leq N) . Takahashi will perform the following operation on this sequence K times. Choose an integer k such that 0 \leq k < N . Take the last k terms of A and insert them among the first N-k . More precisely, replace A with any permutation A' of the N integers that satisfies the following conditions. (A_1,A_2,\dots,A_{N-k}) is a (not necessarily contiguous) subsequence of A' . (A_{N-k+1},A_{N-k+2},\dots,A_{N}) is a (not necessarily contiguous) subsequence of A' . The cost of the series of operations is \sum_{i=1}^{K}k_iC_i , where k_i is the k chosen in the i -th operation. Takahashi wants to perform K operations so that A=(P_1,P_2,\dots,P_N) afterward. Determine whether it is possible to perform such a series of operations. If it is possible, find its minimum cost.
[ { "input": "4 1\n3\n3 1 2 4\n", "output": "6\n" }, { "input": "4 1\n3\n4 3 2 1\n", "output": "-1\n" }, { "input": "20 10\n874735445 684260477 689935252 116941558 915603029 923404262 843759669 656978932 286318130 255195090\n11 15 20 10 6 8 18 2 12 4 9 13 19 3 16 7 14 17 5 1\n", "output": "7372920743\n" } ]
https://atcoder.jp/contests/agc062/tasks/agc062_c
Problem Statement You are given an integer sequence of length N : A=(A_1,A_2,\dots,A_N) . Find the K smallest positive integers X that satisfy the following condition. There is no non-empty (not necessarily contiguous) subsequence of A whose elements sum to X .
[ { "input": "3 3\n1 2 5\n", "output": "4 9 10\n" }, { "input": "20 10\n324 60 1 15 60 15 1 60 319 1 327 1 2 60 2 345 1 2 2 15\n", "output": "14 29 44 59 74 89 104 119 134 149\n" } ]
https://atcoder.jp/contests/agc062/tasks/agc062_d
Problem Statement Takahashi, with a notepad, is standing at the origin (0,0) of a two-dimensional plane. His notepad has N even numbers D_i\ (1\leq i \leq N) written. Takahashi will perform the following actions N times on the plane. He chooses and erases one even number written on the notepad. Let d be the chosen even number. He then moves to a point exactly d distance away in terms of Manhattan distance. More precisely, if Takahashi is currently at the coordinates (x,y) , he moves to a point (x',y') such that |x-x'|+|y-y'|=d . After performing N actions, Takahashi must return to the origin (0,0) . Determine if it is possible to perform N actions in such a way. If it is possible, find the minimum value of \displaystyle \max_{1\leq i \leq N}(|x_i|+|y_i|) , where (x_i,y_i) are the coordinates where Takahashi is located after the i -th action (we can prove that this value is an integer).
[ { "input": "3\n2 4 6\n", "output": "4\n" }, { "input": "5\n2 2 2 2 6\n", "output": "3\n" }, { "input": "2\n2 200000\n", "output": "-1\n" } ]
https://atcoder.jp/contests/agc062/tasks/agc062_e
Problem Statement You are given an odd number N and a non-negative integer K . Find the number, modulo 998244353 , of sequences of integer pairs ((L_1,R_1),(L_2,R_2),\dots,(L_N,R_N)) satisfying all of the following conditions. (L_1,R_1,L_2,R_2,\dots,L_N,R_N) is a permutation of the integers from 1 to 2N . L_1 \leq L_2 \leq \dots \leq L_N . L_i \leq R_i \ (1 \leq i \leq N) . There are exactly K indices i\ (1\leq i \leq N) such that L_i+1=R_i . There is a rooted binary tree T with N vertices numbered from 1 to N such that the following holds: vertices i and j have an ancestor-descendant relationship in T \iff intervals [L_i,R_i] and [L_j,R_j] intersect. Here, a rooted binary tree is a rooted tree where each vertex has 0 or 2 children. Vertices i and j are said to have an ancestor-descendant relationship in a tree T if either vertex j exists on the simple path connecting the root to vertex i , or vice versa.
[ { "input": "3 1\n", "output": "2\n" }, { "input": "1 0\n", "output": "0\n" }, { "input": "521 400\n", "output": "0\n" }, { "input": "199999 2023\n", "output": "283903125\n" } ]
https://atcoder.jp/contests/agc062/tasks/agc062_f
Problem Statement There are N decks of cards, each card in which has an integer between 1 and M , inclusive, written on it. The i -th deck (1\leq i \leq N) contains K_i cards, and the number written on the j -th card from the top is A_{i,j}\ (1\leq j \leq K_i) . Initially, the integers written on the cards satisfy the following conditions: For each integer x\ (1\leq x \leq M) , there are exactly two cards with x written on them, each in a different deck. The integers written on the top cards of all the decks are pairwise different. We can perform the following operation on the N decks: Choose a deck that still has cards and discard the top card. After discarding, the integers written on the top cards of the decks that still have cards must remain pairwise different. Determine the maximum number of times this operation can be performed.
[ { "input": "2 4\n4 1 2 3 4\n4 3 2 1 4\n", "output": "1\n" }, { "input": "4 6\n2 6 4\n3 2 5 3\n4 3 1 5 6\n3 4 1 2\n", "output": "12\n" }, { "input": "3 3\n2 1 2\n2 2 3\n2 3 1\n", "output": "0\n" }, { "input": "12 40\n4 35 39 11 21\n7 31 29 16 15 30 32 34\n4 21 27 38 40\n11 17 28 26 23 33 22 3 36 8 1 20\n1 30\n4 40 38 24 6\n8 8 36 9 10 5 7 20 4\n10 5 10 9 3 22 33 23 26 28 17\n4 15 16 29 31\n11 19 13 12 18 25 2 39 35 7 14 37\n3 4 1 14\n13 24 27 11 2 25 18 12 13 19 32 37 6 34\n", "output": "53\n" } ]
https://atcoder.jp/contests/abc302/tasks/abc302_a
Problem Statement There is an enemy with stamina A . Every time you attack the enemy, its stamina reduces by B . At least how many times do you need to attack the enemy to make its stamina 0 or less?
[ { "input": "7 3\n", "output": "3\n" }, { "input": "123456789123456789 987654321\n", "output": "124999999\n" }, { "input": "999999999999999998 2\n", "output": "499999999999999999\n" } ]
https://atcoder.jp/contests/abc302/tasks/abc302_b
Problem Statement There is a grid with H horizontal rows and W vertical columns. Each cell has a lowercase English letter written on it. We denote by (i, j) the cell at the i -th row from the top and j -th column from the left. The letters written on the grid are represented by H strings S_1,S_2,\ldots, S_H , each of length W . The j -th letter of S_i represents the letter written on (i, j) . There is a unique set of contiguous cells (going vertically, horizontally, or diagonally) in the grid with s , n , u , k , and e written on them in this order. Find the positions of such cells and print them in the format specified in the Output section. A tuple of five cells (A_1,A_2,A_3,A_4,A_5) is said to form a set of contiguous cells (going vertically, horizontally, or diagonally) with s , n , u , k , and e written on them in this order if and only if all of the following conditions are satisfied. A_1,A_2,A_3,A_4 and A_5 have letters s , n , u , k , and e written on them, respectively. For all 1\leq i\leq 4 , cells A_i and A_{i+1} share a corner or a side. The centers of A_1,A_2,A_3,A_4 , and A_5 are on a common line at regular intervals.
[ { "input": "6 6\nvgxgpu\namkxks\nzhkbpp\nhykink\nesnuke\nzplvfj\n", "output": "5 2\n5 3\n5 4\n5 5\n5 6\n" }, { "input": "5 5\nezzzz\nzkzzz\nezuzs\nzzznz\nzzzzs\n", "output": "5 5\n4 4\n3 3\n2 2\n1 1\n" }, { "input": "10 10\nkseeusenuk\nusesenesnn\nkskekeeses\nnesnusnkkn\nsnenuuenke\nkukknkeuss\nneunnennue\nsknuessuku\nnksneekknk\nneeeuknenk\n", "output": "9 3\n8 3\n7 3\n6 3\n5 3\n" } ]
https://atcoder.jp/contests/abc302/tasks/abc302_c
Problem Statement You are given N strings S_1,S_2,\dots,S_N , each of length M , consisting of lowercase English letter. Here, S_i are pairwise distinct. Determine if one can rearrange these strings to obtain a new sequence of strings T_1,T_2,\dots,T_N such that: for all integers i such that 1 \le i \le N-1 , one can alter exactly one character of T_i to another lowercase English letter to make it equal to T_{i+1} .
[ { "input": "4 4\nbbed\nabcd\nabed\nfbed\n", "output": "Yes\n" }, { "input": "2 5\nabcde\nabced\n", "output": "No\n" }, { "input": "8 4\nfast\nface\ncast\nrace\nfact\nrice\nnice\ncase\n", "output": "Yes\n" } ]
https://atcoder.jp/contests/abc302/tasks/abc302_d
Problem Statement Takahashi has decided to give one gift to Aoki and one gift to Snuke. There are N candidates of gifts for Aoki, and their values are A_1, A_2, \ldots,A_N . There are M candidates of gifts for Snuke, and their values are B_1, B_2, \ldots,B_M . Takahashi wants to choose gifts so that the difference in values of the two gifts is at most D . Determine if he can choose such a pair of gifts. If he can, print the maximum sum of values of the chosen gifts.
[ { "input": "2 3 2\n3 10\n2 5 15\n", "output": "8\n" }, { "input": "3 3 0\n1 3 3\n6 2 7\n", "output": "-1\n" }, { "input": "1 1 1000000000000000000\n1000000000000000000\n1000000000000000000\n", "output": "2000000000000000000\n" }, { "input": "8 6 1\n2 5 6 5 2 1 7 9\n7 2 5 5 2 4\n", "output": "14\n" } ]
https://atcoder.jp/contests/abc302/tasks/abc302_e
Problem Statement There is an undirected graph with N vertices numbered 1 through N , and initially with 0 edges. Given Q queries, process them in order. After processing each query, print the number of vertices that are not connected to any other vertices by an edge. The i -th query, \mathrm{query}_i , is of one of the following two kinds. 1 u v : connect vertex u and vertex v with an edge. It is guaranteed that, when this query is given, vertex u and vertex v are not connected by an edge. 2 v : remove all edges that connect vertex v and the other vertices. (Vertex v itself is not removed.)
[ { "input": "3 7\n1 1 2\n1 1 3\n1 2 3\n2 1\n1 1 2\n2 2\n1 1 2\n", "output": "1\n0\n0\n1\n0\n3\n1\n" }, { "input": "2 1\n2 1\n", "output": "2\n" } ]
https://atcoder.jp/contests/abc302/tasks/abc302_f
Problem Statement On a blackboard, there are N sets S_1,S_2,\dots,S_N consisting of integers between 1 and M . Here, S_i = \lbrace S_{i,1},S_{i,2},\dots,S_{i,A_i} \rbrace . You may perform the following operation any number of times (possibly zero): choose two sets X and Y with at least one common element. Erase them from the blackboard, and write X\cup Y on the blackboard instead. Here, X\cup Y denotes the set consisting of the elements contained in at least one of X and Y . Determine if one can obtain a set containing both 1 and M . If it is possible, find the minimum number of operations required to obtain it.
[ { "input": "3 5\n2\n1 2\n2\n2 3\n3\n3 4 5\n", "output": "2\n" }, { "input": "1 2\n2\n1 2\n", "output": "0\n" }, { "input": "3 5\n2\n1 3\n2\n2 4\n3\n2 4 5\n", "output": "-1\n" }, { "input": "4 8\n3\n1 3 5\n2\n1 2\n3\n2 4 7\n4\n4 6 7 8\n", "output": "2\n" } ]
https://atcoder.jp/contests/abc302/tasks/abc302_g
Problem Statement You are given a sequence A=(A_1,A_2,\ldots,A_N) of length N , consisting of integers between 1 and 4 . Takahashi may perform the following operation any number of times (possibly zero): choose a pair of integer (i,j) such that 1\leq i<j\leq N , and swap A_i and A_j . Find the minimum number of operations required to make A non-decreasing. A sequence is said to be non-decreasing if and only if A_i\leq A_{i+1} for all 1\leq i\leq N-1 .
[ { "input": "6\n3 4 1 1 2 4\n", "output": "3\n" }, { "input": "4\n2 3 4 1\n", "output": "3\n" } ]
https://atcoder.jp/contests/abc302/tasks/abc302_h
Problem Statement We have a tree with N vertices. The i -th (1 \le i \le N-1) edge is an undirected edge between vertices U_i and V_i . Vertex i (1 \le i \le N) has a ball with A_i written on it and another with B_i . For each v = 2,3,\dots,N , answer the following question. (Each query is independent.) Consider traveling from vertex 1 to vertex v on the shortest path. Every time you visit a vertex (including vertices 1 and v ), you pick up one ball placed there. Find the maximum number of distinct integers written on the picked-up balls.
[ { "input": "4\n1 2\n2 3\n3 1\n1 2\n1 2\n2 3\n3 4\n", "output": "2 3 3\n" }, { "input": "10\n2 5\n2 2\n8 8\n4 3\n6 10\n8 1\n9 10\n1 7\n9 3\n5 10\n9 3\n1 9\n3 6\n4 1\n3 8\n10 9\n5 4\n7 2\n9 7\n", "output": "4 3 2 3 4 3 4 2 3\n" } ]
https://atcoder.jp/contests/arc160/tasks/arc160_a
Problem Statement You are given a permutation A = (A_1, A_2, \dots, A_N) of (1, 2, \dots, N) . For a pair of integers (L, R) such that 1 \leq L \leq R \leq N , let f(L, R) be the permutation obtained by reversing the L -th through R -th elements of A , that is, replacing A_L, A_{L+1}, \dots, A_{R-1}, A_R with A_R, A_{R-1}, \dots, A_{L+1}, A_{L} simultaneously. There are \frac{N(N + 1)}{2} ways to choose (L, R) such that 1 \leq L \leq R \leq N . If the permutations f(L, R) for all such pairs (L, R) are listed and sorted in lexicographical order, what is the K -th permutation from the front? 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 is an integer 1 \leq i \leq \min\lbrace |S|, |T| \rbrace that satisfies both of the following. (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 5\n1 3 2\n", "output": "2 3 1\n" }, { "input": "5 15\n1 2 3 4 5\n", "output": "5 4 3 2 1\n" }, { "input": "10 37\n9 2 1 3 8 7 10 4 5 6\n", "output": "9 2 1 6 5 4 10 7 8 3\n" } ]
https://atcoder.jp/contests/arc160/tasks/arc160_b
Problem Statement You are given a positive integer N . Find the number, modulo 998244353 , of triples of positive integers (x,y,z) that satisfy the following condition. All of xy , yz , zx are less than or equal to N . You have T test cases to solve.
[ { "input": "4\n1\n2\n5\n998244353\n", "output": "1\n4\n17\n727512986\n" } ]
https://atcoder.jp/contests/arc160/tasks/arc160_c
Problem Statement You are given a multiset of positive integers with N elements: A=\lbrace A_1,A_2,\dots,A_N \rbrace . You may repeat the following operation any number of times (possibly zero). Choose a positive integer x that occurs at least twice in A . Delete two occurrences of x from A , and add one occurrence of x+1 to A . Find the number, modulo 998244353 , of multisets that A can be in the end.
[ { "input": "4\n1 1 2 4\n", "output": "3\n" }, { "input": "5\n1 2 3 4 5\n", "output": "1\n" }, { "input": "13\n3 1 4 1 5 9 2 6 5 3 5 8 9\n", "output": "66\n" } ]
https://atcoder.jp/contests/arc160/tasks/arc160_d
Problem Statement Find the number, modulo 998244353 , of sequences of N non-negative integers A=(A_1,A_2,\dots,A_N) totaling M that satisfy the following condition. It is possible to make all elements of A equal 0 by repeatedly choosing one of the following operations and performing it. Choose an integer i such that 1 \le i \le N and decrease A_i by K . Choose an integer i such that 1 \le i \le N-K+1 and decrease each of A_i,A_{i+1},\dots,A_{i+K-1} by 1 .
[ { "input": "3 2 2\n", "output": "5\n" }, { "input": "100 998244353 100\n", "output": "0\n" }, { "input": "2000 545782618661124208 533\n", "output": "908877889\n" } ]
https://atcoder.jp/contests/arc160/tasks/arc160_e
Problem Statement You are given an undirected tree G with N vertices. The degree of every vertex in G is at most 3 . The vertices are numbered 1 to N . The edges are numbered 1 to N-1 , and edge i connects vertex u_i and vertex v_i . Each vertex has a fixed weight, and the weight of vertex i is W_i . You will add zero or more edges to G . The cost of adding an edge between vertex i and vertex j is W_i + W_j . Print one way to add edges to satisfy the following condition for the minimum possible total cost. G is 2 -vertex-connected. In other words, for every vertex v in G , removing v and its incident edges from G would not disconnect G . You have T test cases to solve.
[ { "input": "2\n3\n2 3 5\n1 2\n2 3\n7\n1 10 100 1000 10000 100000 1000000\n1 2\n2 3\n2 4\n3 5\n3 6\n4 7\n", "output": "1\n1 3\n2\n7 6\n1 5\n" } ]
https://atcoder.jp/contests/arc160/tasks/arc160_f
Problem Statement There are an integer N and M pairs of integers: (a_1, b_1), (a_2, b_2), \dots, (a_M, b_M) . Each pair (a_i, b_i) satisfies 1 \leq a_i \lt b_i \leq N . Initally, you have all N! permutations of (1,2,\dots,N) . You will perform M operations. The i -th operation is as follows. Do the following for each of your N! permutations. Compare the a_i -th and b_i -th elements. If the former is greater, swap the two elements. For each 1 \leq i \leq M , let S_i be the number of permutations of yours that are already sorted in ascending order at the end of the i -th operation. Print S_1, S_2, \dots, S_M . Here, the input gives you pairs of integers (x_i, y_i) instead of (a_i, b_i) . The values of (a_i, b_i) can be obtained using x_i , y_i , and S_{i-1} as follows. (Let S_0 = 1 for convenience.) Let c_i = ((x_i + S_{i-1}) \bmod N) + 1 . Let d_i = ((y_i + S_{i-1} \times 2) \bmod N) + 1 . (Here, it is guaranteed that c_i \neq d_i .) Let a_i = \min(c_i, d_i) . Let b_i = \max(c_i, d_i) .
[ { "input": "2 1\n1 1\n", "output": "2\n" }, { "input": "3 4\n0 1\n2 1\n1 1\n0 1\n", "output": "2\n4\n4\n6\n" }, { "input": "5 5\n4 4\n0 4\n1 1\n2 4\n1 2\n", "output": "2\n4\n4\n8\n16\n" } ]
https://atcoder.jp/contests/abc301/tasks/abc301_a
Problem Statement Takahashi and Aoki played N games. You are given a string S of length N , representing the results of these games. Takahashi won the i -th game if the i -th character of S is T , and Aoki won that game if it is A . The overall winner between Takahashi and Aoki is the one who won more games than the other. If they had the same number of wins, the overall winner is the one who reached that number of wins first. Find the overall winner: Takahashi or Aoki.
[ { "input": "5\nTTAAT\n", "output": "T\n" }, { "input": "6\nATTATA\n", "output": "T\n" }, { "input": "1\nA\n", "output": "A\n" } ]
https://atcoder.jp/contests/abc301/tasks/abc301_b
Problem Statement We have a sequence of length N consisting of positive integers: A=(A_1,\ldots,A_N) . Any two adjacent terms have different values. Let us insert some numbers into this sequence by the following procedure. If every pair of adjacent terms in A has an absolute difference of 1 , terminate the procedure. Let A_i, A_{i+1} be the pair of adjacent terms nearest to the beginning of A whose absolute difference is not 1 . If A_i < A_{i+1} , insert A_i+1,A_i+2,\ldots,A_{i+1}-1 between A_i and A_{i+1} . If A_i > A_{i+1} , insert A_i-1,A_i-2,\ldots,A_{i+1}+1 between A_i and A_{i+1} . Return to step 1. Print the sequence when the procedure ends.
[ { "input": "4\n2 5 1 2\n", "output": "2 3 4 5 4 3 2 1 2\n" }, { "input": "6\n3 4 5 6 5 4\n", "output": "3 4 5 6 5 4\n" } ]
https://atcoder.jp/contests/abc301/tasks/abc301_c
Problem Statement A single-player card game is popular in AtCoder Inc. Each card in the game has a lowercase English letter or the symbol @ written on it. There is plenty number of cards for each kind. The game goes as follows. Arrange the same number of cards in two rows. Replace each card with @ with one of the following cards: a , t , c , o , d , e , r . If the two rows of cards coincide, you win. Otherwise, you lose. To win this game, you will do the following cheat. Freely rearrange the cards within a row whenever you want after step 1. You are given two strings S and T , representing the two rows you have after step 1. Determine whether it is possible to win with cheating allowed.
[ { "input": "ch@ku@ai\nchoku@@i\n", "output": "Yes\n" }, { "input": "ch@kud@i\nakidu@ho\n", "output": "Yes\n" }, { "input": "aoki\n@ok@\n", "output": "No\n" }, { "input": "aa\nbb\n", "output": "No\n" } ]
https://atcoder.jp/contests/abc301/tasks/abc301_d
Problem Statement You are given an integer N and a string S consisting of 0 , 1 , and ? . Let T be the set of values that can be obtained by replacing each ? in S with 0 or 1 and interpreting the result as a binary integer. For instance, if S= ?0? , we have T=\lbrace 000_{(2)},001_{(2)},100_{(2)},101_{(2)}\rbrace=\lbrace 0,1,4,5\rbrace . Print (as a decimal integer) the greatest value in T less than or equal to N . If T does not contain a value less than or equal to N , print -1 instead.
[ { "input": "?0?\n2\n", "output": "1\n" }, { "input": "101\n4\n", "output": "-1\n" }, { "input": "?0?\n1000000000000000000\n", "output": "5\n" } ]
https://atcoder.jp/contests/abc301/tasks/abc301_e
Problem Statement We have a grid with H rows and W columns. Let (i,j) denote the square at the i -th row from the top and j -th column from the left. Each square in the grid is one of the following: the start square, the goal square, an empty square, a wall square, and a candy square. (i,j) is represented by a character A_{i,j} , and is the start square if A_{i,j}= S , the goal square if A_{i,j}= G , an empty square if A_{i,j}= . , a wall square if A_{i,j}= # , and a candy square if A_{i,j}= o . Here, it is guaranteed that there are exactly one start, exactly one goal, and at most 18 candy squares. Takahashi is now at the start square. He can repeat moving to a vertically or horizontally adjacent non-wall square. He wants to reach the goal square in at most T moves. Determine whether it is possible. If it is possible, find the maximum number of candy squares he can visit on the way to the goal square, where he must finish. Each candy square counts only once, even if it is visited multiple times.
[ { "input": "3 3 5\nS.G\no#o\n.#.\n", "output": "1\n" }, { "input": "3 3 1\nS.G\n.#o\no#.\n", "output": "-1\n" }, { "input": "5 10 2000000\nS.o..ooo..\n..o..o.o..\n..o..ooo..\n..o..o.o..\n..o..ooo.G\n", "output": "18\n" } ]
https://atcoder.jp/contests/abc301/tasks/abc301_f
Problem Statement A DDoS -type string is a string of length 4 consisting of uppercase and lowercase English letters satisfying both of the following conditions. The first, second, and fourth characters are uppercase English letters, and the third character is a lowercase English letter. The first and second characters are equal. For instance, DDoS and AAaA are DDoS -type strings, while neither ddos nor IPoE is. You are given a string S consisting of uppercase and lowercase English letters and ? . Let q be the number of occurrences of ? in S . There are 52^q strings that can be obtained by independently replacing each ? in S with an uppercase or lowercase English letter. Among these strings, find the number of ones that do not contain a DDoS -type string as a subsequence, modulo 998244353 .
[ { "input": "DD??S\n", "output": "676\n" }, { "input": "????????????????????????????????????????\n", "output": "858572093\n" }, { "input": "?D??S\n", "output": "136604\n" } ]
https://atcoder.jp/contests/abc301/tasks/abc301_g
Problem Statement There are N people in a three-dimensional space. The i -th person is at the coordinates (X_i,Y_i,Z_i) . All people are at different coordinates, and we have X_i>0 for every i . You will choose a point p=(x,y,z) such that x<0 , and take a photo in the positive x -direction. If the point p and the positions A, B of two people are on the same line in the order p,A,B , then the person at B will not be in the photo. There are no other potential obstacles. Find the number of people in the photo when p is chosen to minimize this number.
[ { "input": "3\n1 1 1\n2 2 2\n100 99 98\n", "output": "2\n" }, { "input": "8\n1 1 1\n1 1 -1\n1 -1 1\n1 -1 -1\n3 2 2\n3 2 -2\n3 -2 2\n3 -2 -2\n", "output": "4\n" } ]
https://atcoder.jp/contests/abc301/tasks/abc301_h
Problem Statement We have a connected undirected graph with N vertices numbered 1 to N and M edges numbered 1 to M . Edge i connects vertex U_i and vertex V_i , and has an integer weight of W_i . For 1\leq s,t \leq N,\ s\neq t , let us define d(s,t) as follows. For every path connecting vertex s and vertex t , consider the maximum weight of an edge along that path. d(s,t) is the minimum value of this weight. Answer Q queries. The j -th query is as follows. You are given A_j,S_j,T_j . By what value will d(S_j,T_j) increase when the weight of edge A_j is increased by 1 ? Note that each query does not actually change the weight of the edge.
[ { "input": "6 6\n1 2 1\n3 1 5\n4 1 5\n3 4 3\n5 6 4\n2 6 5\n7\n1 4 6\n2 4 6\n3 4 6\n4 4 6\n5 4 6\n6 4 6\n5 6 5\n", "output": "0\n0\n0\n0\n0\n1\n1\n" }, { "input": "2 2\n1 2 1\n1 2 1\n1\n1 1 2\n", "output": "0\n" } ]
https://atcoder.jp/contests/abc300/tasks/abc300_a
Problem Statement Given integers A and B , find A+B . This is a N -choice problem; the i -th choice is C_i . Print the index of the correct choice.
[ { "input": "3 125 175\n200 300 400\n", "output": "2\n" }, { "input": "1 1 1\n2\n", "output": "1\n" }, { "input": "5 123 456\n135 246 357 468 579\n", "output": "5\n" } ]
https://atcoder.jp/contests/abc300/tasks/abc300_b
Problem Statement Takahashi is developing an RPG. He has decided to write a code that checks whether two maps are equal. We have grids A and B with H horizontal rows and W vertical columns. Each cell in the grid has a symbol # or . written on it. The symbols written on the cell at the i -th row from the top and j -th column from the left in A and B are denoted by A_{i, j} and B_{i, j} , respectively. The following two operations are called a vertical shift and horizontal shift . For each j=1, 2, \dots, W , simultaneously do the following: simultaneously replace A_{1,j}, A_{2,j}, \dots, A_{H-1, j}, A_{H,j} with A_{2,j}, A_{3,j}, \dots, A_{H,j}, A_{1,j} . For each i = 1, 2, \dots, H , simultaneously do the following: simultaneously replace A_{i,1}, A_{i,2}, \dots, A_{i,W-1}, A_{i,W} with A_{i, 2}, A_{i, 3}, \dots, A_{i,W}, A_{i,1} . Is there a pair of non-negative integers (s, t) that satisfies the following condition? Print Yes if there is, and No otherwise. After applying a vertical shift s times and a horizontal shift t times, A is equal to B . Here, A is said to be equal to B if and only if A_{i, j} = B_{i, j} for all integer pairs (i, j) such that 1 \leq i \leq H and 1 \leq j \leq W .
[ { "input": "4 3\n..#\n...\n.#.\n...\n#..\n...\n.#.\n...\n", "output": "Yes\n" }, { "input": "3 2\n##\n##\n#.\n..\n#.\n#.\n", "output": "No\n" }, { "input": "4 5\n#####\n.#...\n.##..\n..##.\n...##\n#...#\n#####\n...#.\n", "output": "Yes\n" }, { "input": "10 30\n..........##########..........\n..........####....###.....##..\n.....##....##......##...#####.\n....####...##..#####...##...##\n...##..##..##......##..##....#\n#.##....##....##...##..##.....\n..##....##.##..#####...##...##\n..###..###..............##.##.\n.#..####..#..............###..\n#..........##.................\n................#..........##.\n######....................####\n....###.....##............####\n.....##...#####......##....##.\n.#####...##...##....####...##.\n.....##..##....#...##..##..##.\n##...##..##.....#.##....##....\n.#####...##...##..##....##.##.\n..........##.##...###..###....\n...........###...#..####..#...\n", "output": "Yes\n" } ]
https://atcoder.jp/contests/abc300/tasks/abc300_c
Problem Statement We have a grid with H horizontal rows and W vertical columns. We denote by (i, j) the cell at the i -th row from the top and j -th column from the left of the grid. Each cell in the grid has a symbol # or . written on it. Let C[i][j] be the character written on (i, j) . For integers i and j such that at least one of 1 \leq i \leq H and 1 \leq j \leq W is violated, we define C[i][j] to be . . (4n+1) squares, consisting of (a, b) and (a+d,b+d),(a+d,b-d),(a-d,b+d),(a-d,b-d) (1 \leq d \leq n, 1 \leq n) , are said to be a cross of size n centered at (a,b) if and only if all of the following conditions are satisfied: C[a][b] is # . C[a+d][b+d],C[a+d][b-d],C[a-d][b+d] , and C[a-d][b-d] are all # , for all integers d such that 1 \leq d \leq n , At least one of C[a+n+1][b+n+1],C[a+n+1][b-n-1],C[a-n-1][b+n+1] , and C[a-n-1][b-n-1] is . . For example, the grid in the following figure has a cross of size 1 centered at (2, 2) and another of size 2 centered at (3, 7) . The grid has some crosses. No # is written on the cells except for those comprising a cross. Additionally, no two squares that comprise two different crosses share a corner. The two grids in the following figure are the examples of grids where two squares that comprise different crosses share a corner; such grids are not given as an input . For example, the left grid is invalid because (3, 3) and (4, 4) share a corner. Let N = \min(H, W) , and S_n be the number of crosses of size n . Find S_1, S_2, \dots, S_N .
[ { "input": "5 9\n#.#.#...#\n.#...#.#.\n#.#...#..\n.....#.#.\n....#...#\n", "output": "1 1 0 0 0\n" }, { "input": "3 3\n...\n...\n...\n", "output": "0 0 0\n" }, { "input": "3 16\n#.#.....#.#..#.#\n.#.......#....#.\n#.#.....#.#..#.#\n", "output": "3 0 0\n" }, { "input": "15 20\n#.#..#.............#\n.#....#....#.#....#.\n#.#....#....#....#..\n........#..#.#..#...\n#.....#..#.....#....\n.#...#....#...#..#.#\n..#.#......#.#....#.\n...#........#....#.#\n..#.#......#.#......\n.#...#....#...#.....\n#.....#..#.....#....\n........#.......#...\n#.#....#....#.#..#..\n.#....#......#....#.\n#.#..#......#.#....#\n", "output": "5 0 1 0 0 0 1 0 0 0 0 0 0 0 0\n" } ]
https://atcoder.jp/contests/abc300/tasks/abc300_d
Problem Statement How many positive integers no greater than N can be represented as a^2 \times b \times c^2 with three primes a,b , and c such that a<b<c ?
[ { "input": "1000\n", "output": "3\n" }, { "input": "1000000000000\n", "output": "2817785\n" } ]
https://atcoder.jp/contests/abc300/tasks/abc300_e
Problem Statement You have an integer 1 and a die that shows integers between 1 and 6 (inclusive) with equal probability. You repeat the following operation while your integer is strictly less than N : Cast a die. If it shows x , multiply your integer by x . Find the probability, modulo 998244353 , that your integer ends up being N . How to find a probability modulo 998244353 ? We can prove that the sought probability is always rational. Additionally, under the constraints of this problem, when that value is represented as \frac{P}{Q} with two coprime integers P and Q , we can prove that there is a unique integer R such that R \times Q \equiv P\pmod{998244353} and 0 \leq R \lt 998244353 . Find this R .
[ { "input": "6\n", "output": "239578645\n" }, { "input": "7\n", "output": "0\n" }, { "input": "300\n", "output": "183676961\n" }, { "input": "979552051200000000\n", "output": "812376310\n" } ]
https://atcoder.jp/contests/abc300/tasks/abc300_f
Problem Statement You are given a string S of length N consisting of o and x , and integers M and K . S is guaranteed to contain at least one x . Let T be the string of length NM obtained by concatenating M copies of S . Consider replacing exactly K x 's in T with o . Your objective is to have as long a contiguous substring consisting of o as possible in the resulting T . Find the maximum length of a contiguous substring consisting of o that you can obtain.
[ { "input": "10 1 2\nooxxooooox\n", "output": "9\n" }, { "input": "5 3 4\noxxox\n", "output": "8\n" }, { "input": "30 1000000000 9982443530\noxoxooxoxoxooxoxooxxxoxxxooxox\n", "output": "19964887064\n" } ]
https://atcoder.jp/contests/abc300/tasks/abc300_g
Problem Statement A positive integer is called a k -smooth number if none of its prime factors exceeds k . Given an integer N and a prime P not exceeding 100 , find the number of P -smooth numbers not exceeding N .
[ { "input": "36 3\n", "output": "14\n" }, { "input": "10000000000000000 97\n", "output": "2345134674\n" } ]
https://atcoder.jp/contests/abc300/tasks/abc300_h
Problem Statement We define the general term a_n of a sequence a_0, a_1, a_2, \dots by: a_n = \begin{cases} 1 & (0 \leq n \lt K) \\ \displaystyle{\sum_{i=1}^K} a_{n-i} & (K \leq n). \\ \end{cases} Given an integer N , find the sum, modulo 998244353 , of a_m over all non-negative integers m such that m\text{ AND }N = m . ( \text{AND} denotes the bitwise AND.) What is bitwise AND? The bitwise AND of non-negative integers A and B , A\text{ AND }B , is defined as follows. ・When A\text{ AND }B is written in binary, its 2^k s place ( k \geq 0 ) is 1 if 2^k s places of A and B are both 1 , and 0 otherwise.
[ { "input": "2 6\n", "output": "21\n" }, { "input": "2 8\n", "output": "35\n" }, { "input": "1 123456789\n", "output": "65536\n" }, { "input": "300 20230429\n", "output": "125461938\n" }, { "input": "42923 999999999558876113\n", "output": "300300300\n" } ]
https://atcoder.jp/contests/abc299/tasks/abc299_a
Problem Statement You are given a string S of length N consisting of three kinds of characters: . , | , and * . S contains exactly two | and exactly one * . Determine whether the * is between the two | , and if so, print in ; otherwise, print out . More formally, determine whether one of the characters before the * is | and one of the characters after the * is | .
[ { "input": "10\n.|..*...|.\n", "output": "in\n" }, { "input": "10\n.|..|.*...\n", "output": "out\n" }, { "input": "3\n|*|\n", "output": "in\n" } ]
https://atcoder.jp/contests/abc299/tasks/abc299_b
Problem Statement N players with ID numbers 1, 2, \ldots, N are playing a card game. Each player plays one card. Each card has two parameters: color and rank , both of which are represented by positive integers. For i = 1, 2, \ldots, N , the card played by player i has a color C_i and a rank R_i . All of R_1, R_2, \ldots, R_N are different. Among the N players, one winner is decided as follows. If one or more cards with the color T are played, the player who has played the card with the greatest rank among those cards is the winner. If no card with the color T is played, the player who has played the card with the greatest rank among the cards with the color of the card played by player 1 is the winner. (Note that player 1 may win.) Print the ID number of the winner.
[ { "input": "4 2\n1 2 1 2\n6 3 4 5\n", "output": "4\n" }, { "input": "4 2\n1 3 1 4\n6 3 4 5\n", "output": "1\n" }, { "input": "2 1000000000\n1000000000 1\n1 1000000000\n", "output": "1\n" } ]
https://atcoder.jp/contests/abc299/tasks/abc299_c
Problem Statement For a positive integer L , a level- L dango string is a string that satisfies the following conditions. It is a string of length L+1 consisting of o and - . Exactly one of the first character and the last character is - , and the other L characters are o . For instance, ooo- is a level- 3 dango string, but none of -ooo- , oo , and o-oo- is a dango string (more precisely, none of them is a level- L dango string for any positive integer L ). You are given a string S of length N consisting of the two characters o and - . Find the greatest positive integer X that satisfies the following condition. There is a contiguous substring of S that is a level- X dango string. If there is no such integer, print -1 .
[ { "input": "10\no-oooo---o\n", "output": "4\n" }, { "input": "1\n-\n", "output": "-1\n" }, { "input": "30\n-o-o-oooo-oo-o-ooooooo--oooo-o\n", "output": "7\n" } ]
https://atcoder.jp/contests/abc299/tasks/abc299_d
Problem Statement This is an interactive task , where your program and the judge interact via Standard Input and Output. The judge has a string of length N consisting of 0 and 1 : S = S_1S_2\ldots S_N . Here, S_1 = 0 and S_N = 1 . You are given the length N of S , but not the contents of S . Instead, you can ask the judge at most 20 questions as follows. Choose an integer i such that 1 \leq i \leq N and ask the value of S_i . Print an integer p such that 1 \leq p \leq N-1 and S_p \neq S_{p+1} . It can be shown that such p always exists under the settings of this problem.
[]
https://atcoder.jp/contests/abc299/tasks/abc299_e
Problem Statement You are given a simple connected undirected graph with N vertices and M edges (a simple graph contains no self-loop and no multi-edges). For i = 1, 2, \ldots, M , the i -th edge connects vertex u_i and vertex v_i bidirectionally. Determine whether there is a way to paint each vertex black or white to satisfy both of the following conditions, and show one such way if it exists. At least one vertex is painted black. For every i = 1, 2, \ldots, K , the following holds: the minimum distance between vertex p_i and a vertex painted black is exactly d_i . Here, the distance between vertex u and vertex v is the minimum number of edges in a path connecting u and v .
[ { "input": "5 5\n1 2\n2 3\n3 1\n3 4\n4 5\n2\n1 0\n5 2\n", "output": "Yes\n10100\n" }, { "input": "5 5\n1 2\n2 3\n3 1\n3 4\n4 5\n5\n1 1\n2 1\n3 1\n4 1\n5 1\n", "output": "No\n" }, { "input": "1 0\n0\n", "output": "Yes\n1\n" } ]
https://atcoder.jp/contests/abc299/tasks/abc299_f
Problem Statement You are given a string S consisting of lowercase English letters. Print the number of non-empty strings T that satisfy the following condition, modulo 998244353 . The concatenation TT of two copies of T is a subsequence of S (not necessarily contiguous).
[ { "input": "ababbaba\n", "output": "8\n" }, { "input": "zzz\n", "output": "1\n" }, { "input": "ppppqqppqqqpqpqppqpqqqqpppqppq\n", "output": "580\n" } ]
https://atcoder.jp/contests/abc299/tasks/abc299_g
Problem Statement We have a sequence A of length N consisting of integers between 1 and M . Here, every integer from 1 to M appears at least once in A . Among the length- M subsequences of A where each of 1, \ldots, M appears once, find the lexicographically smallest one.
[ { "input": "4 3\n2 3 1 3\n", "output": "2 1 3\n" }, { "input": "4 4\n2 3 1 4\n", "output": "2 3 1 4\n" }, { "input": "20 10\n6 3 8 5 8 10 9 3 6 1 8 3 3 7 4 7 2 7 8 5\n", "output": "3 5 8 10 9 6 1 4 2 7\n" } ]
https://atcoder.jp/contests/abc299/tasks/abc299_h
Problem Statement Takahashi has an unbiased six-sided die and a positive integer R less than 10^9 . Each time the die is cast, it shows one of the numbers 1,2,3,4,5,6 with equal probability, independently of the outcomes of the other trials. Takahashi will perform the following procedure. Initially, C=0 . Cast the die and increment C by 1 . Let X be the sum of the numbers shown so far. If X-R is a multiple of 10^9 , quit the procedure. Go back to step 1. Find the expected value of C at the end of the procedure, modulo 998244353 .
[ { "input": "1\n", "output": "291034221\n" }, { "input": "720357616\n", "output": "153778832\n" } ]
https://atcoder.jp/contests/abc298/tasks/abc298_a
Problem Statement Takahashi had a job interview. You are given the number of interviewers, N , and a string S of length N representing the interviewers' evaluations of him. For each i=1,2,\ldots,N , the i -th character of S corresponds to the i -th interviewer's evaluation; o means Good, - means Fair, and x means Poor. Takahashi will pass if both of the following conditions are satisfied, and fail otherwise. At least one interviewer's evaluation is Good. No interviewer's evaluation is Poor. Determine whether Takahashi passes.
[ { "input": "4\noo--\n", "output": "Yes\n" }, { "input": "3\n---\n", "output": "No\n" }, { "input": "1\no\n", "output": "Yes\n" }, { "input": "100\nooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooox\n", "output": "No\n" } ]
https://atcoder.jp/contests/abc298/tasks/abc298_b
Problem Statement You are given N -by- N matrices A and B where each element is 0 or 1 . Let A_{i,j} and B_{i,j} denote the element at the i -th row and j -th column of A and B , respectively. Determine whether it is possible to rotate A so that B_{i,j} = 1 for every pair of integers (i, j) such that A_{i,j} = 1 . Here, to rotate A is to perform the following operation zero or more times: for every pair of integers (i, j) such that 1 \leq i, j \leq N , simultaneously replace A_{i,j} with A_{N + 1 - j,i} .
[ { "input": "3\n0 1 1\n1 0 0\n0 1 0\n1 1 0\n0 0 1\n1 1 1\n", "output": "Yes\n" }, { "input": "2\n0 0\n0 0\n1 1\n1 1\n", "output": "Yes\n" }, { "input": "5\n0 0 1 1 0\n1 0 0 1 0\n0 0 1 0 1\n0 1 0 1 0\n0 1 0 0 1\n1 1 0 0 1\n0 1 1 1 0\n0 0 1 1 1\n1 0 1 0 1\n1 1 0 1 0\n", "output": "No\n" } ]
https://atcoder.jp/contests/abc298/tasks/abc298_c
Problem Statement We have N boxes numbered 1 to N that are initially empty, and an unlimited number of blank cards. Process Q queries in order. There are three kinds of queries as follows. 1 i j \colon Write the number i on a blank card and put it into box j . 2 i \colon Report all numbers written on the cards in box i , in ascending order . 3 i \colon Report all box numbers of the boxes that contain a card with the number i , in ascending order . Here, note the following. In a query of the second kind, if box i contains multiple cards with the same number, that number should be printed the number of times equal to the number of those cards. In a query of the third kind, even if a box contains multiple cards with the number i , the box number of that box should be printed only once.
[ { "input": "5\n8\n1 1 1\n1 2 4\n1 1 4\n2 4\n1 1 4\n2 4\n3 1\n3 2\n", "output": "1 2\n1 1 2\n1 4\n4\n" }, { "input": "1\n5\n1 1 1\n1 2 1\n1 200000 1\n2 1\n3 200000\n", "output": "1 2 200000\n1\n" } ]
https://atcoder.jp/contests/abc298/tasks/abc298_d
Problem Statement We have a string S . Initially, S= 1 . Process Q queries in the following formats in order. 1 x : Append a digit x at the end of S . 2 : Delete the digit at the beginning of S . 3 : Print the number represented by S in decimal, modulo 998244353 .
[ { "input": "3\n3\n1 2\n3\n", "output": "1\n12\n" }, { "input": "3\n1 5\n2\n3\n", "output": "5\n" }, { "input": "11\n1 9\n1 9\n1 8\n1 2\n1 4\n1 4\n1 3\n1 5\n1 3\n2\n3\n", "output": "0\n" } ]
https://atcoder.jp/contests/abc298/tasks/abc298_e
Problem Statement Takahashi and Aoki will play a game of sugoroku. Takahashi starts at point A , and Aoki starts at point B . They will take turns throwing dice. Takahashi's die shows 1, 2, \ldots, P with equal probability, and Aoki's shows 1, 2, \ldots, Q with equal probability. When a player at point x throws his die and it shows i , he goes to point \min(x + i, N) . The first player to reach point N wins the game. Find the probability that Takahashi wins if he goes first, modulo 998244353 . How to find a probability modulo 998244353 It can be proved that the sought probability is always rational. Additionally, the constraints of this problem guarantee that, if that probability is represented as an irreducible fraction \frac{y}{x} , then x is indivisible by 998244353 . Here, there is a unique integer z between 0 and 998244352 such that xz \equiv y \pmod {998244353} . Report this z .
[ { "input": "4 2 3 3 2\n", "output": "665496236\n" }, { "input": "6 4 2 1 1\n", "output": "1\n" }, { "input": "100 1 1 10 10\n", "output": "264077814\n" } ]
https://atcoder.jp/contests/abc298/tasks/abc298_f
Problem Statement We have a grid with 10^9 rows and 10^9 columns. Let (i,j) denote the square at the i -th row from the top and j -th column from the left. For i=1,2,\ldots,N , a positive integer x_i is written on (r_i,c_i) . On the other 10^{18}-N squares, 0 is written. You choose a square (R,C) and compute the sum S of the integers written on the 2 \times 10^9 - 1 squares that share a row or column with (R,C) . Find the maximum possible value of S .
[ { "input": "4\n1 1 2\n1 2 9\n2 1 8\n3 2 3\n", "output": "20\n" }, { "input": "1\n1 1000000000 1\n", "output": "1\n" }, { "input": "15\n158260522 877914575 602436426\n24979445 861648772 623690081\n433933447 476190629 262703497\n211047202 971407775 628894325\n731963982 822804784 450968417\n430302156 982631932 161735902\n880895728 923078537 707723857\n189330739 910286918 802329211\n404539679 303238506 317063340\n492686568 773361868 125660016\n650287940 839296263 462224593\n492601449 384836991 191890310\n576823355 782177068 404011431\n818008580 954291757 160449218\n155374934 840594328 164163676\n", "output": "1510053068\n" } ]
https://atcoder.jp/contests/abc298/tasks/abc298_g
Problem Statement We have a rectangular cake. It is partitioned into H rows and W columns of sections, and the section at the i -th row from the top and j -th column from the left has s_{i,j} strawberries on it. You will make T cuts to divide the cake into T+1 pieces. Each cut will be done in one of the following two manners. Choose an existing piece with two or more rows of sections. Additionally, choose two adjacent rows from that piece, and cut the piece along the boundary between them to divide it into two smaller pieces. Choose an existing piece with two or more columns of sections. Additionally, choose two adjacent columns from that piece, and cut the piece along the boundary between them to divide it into two smaller pieces. You want to distribute the strawberries onto the resulting pieces as evenly as possible. Let x_1,x_2,\ldots,x_{T+1} be the numbers of strawberries on the resulting T+1 pieces, and M and m be the maximum and minimum among those numbers, respectively. Find the minimum possible value of M-m .
[ { "input": "2 3 4\n2 3 4\n4 1 3\n", "output": "2\n" }, { "input": "2 2 3\n0 0\n0 0\n", "output": "0\n" } ]
https://atcoder.jp/contests/abc298/tasks/abc298_h
Problem Statement You are given 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 . Let d(x,y) denote the distance between vertex x and y in this tree. Here, the distance between vertex x and y is the number of edges on the shortest path from vertex x to y . Answer Q queries in order. The i -th query is as follows. You are given integers L_i and R_i . Find \displaystyle\sum_{j = 1}^{N} \min(d(j, L_i), d(j, R_i)) .
[ { "input": "5\n3 4\n4 5\n2 5\n1 5\n3\n4 1\n1 2\n5 3\n", "output": "4\n6\n3\n" }, { "input": "8\n4 2\n4 1\n5 6\n6 1\n7 6\n8 1\n3 7\n7\n8 4\n4 4\n7 2\n4 4\n5 3\n4 4\n6 1\n", "output": "14\n16\n10\n16\n14\n16\n8\n" } ]
https://atcoder.jp/contests/abc297/tasks/abc297_a
Problem Statement Takahashi turned on a computer at time 0 and clicked the mouse N times. The i -th (1 \le i \le N) click was at time T_i . If he consecutively clicked the mouse at time x_1 and time x_2 (where x_1 < x_2 ), a double click is said to be fired at time x_2 if and only if x_2 - x_1 \le D . What time was a double click fired for the first time? If no double click was fired, print -1 instead.
[ { "input": "4 500\n300 900 1300 1700\n", "output": "1300\n" }, { "input": "5 99\n100 200 300 400 500\n", "output": "-1\n" }, { "input": "4 500\n100 600 1100 1600\n", "output": "600\n" } ]
https://atcoder.jp/contests/abc297/tasks/abc297_b
Problem Statement Takahashi is playing a game called Chess960. He has decided to write a code that determines if a random initial state satisfies the conditions of Chess960. You are given a string S of length eight. S has exactly one K and Q , and exactly two R 's, B 's , and N 's. Determine if S satisfies all of the following conditions. Suppose that the x -th and y -th (x < y) characters from the left of S are B ; then, x and y have different parities. K is between two R 's. More formally, suppose that the x -th and y -th (x < y) characters from the left of S are R and the z -th is K ; then x< z < y .
[ { "input": "RNBQKBNR\n", "output": "Yes\n" }, { "input": "KRRBBNNQ\n", "output": "No\n" }, { "input": "BRKRBQNN\n", "output": "No\n" } ]
https://atcoder.jp/contests/abc297/tasks/abc297_c
Problem Statement Planning to place many PCs in his room, Takahashi has decided to write a code that finds how many PCs he can place in his room. You are given H strings S_1,S_2,\ldots,S_H , each of length W , consisting of . and T . Takahashi may perform the following operation any number of times (possibly zero): Choose integers satisfying 1\leq i \leq H and 1 \leq j \leq W-1 such that the j -th and (j+1) -th characters of S_i are both T . Replace the j -th character of S_i with P , and (j+1) -th with C . He tries to maximize the number of times he performs the operation. Find possible resulting S_1,S_2,\ldots,S_H .
[ { "input": "2 3\nTTT\nT.T\n", "output": "PCT\nT.T\n" }, { "input": "3 5\nTTT..\n.TTT.\nTTTTT\n", "output": "PCT..\n.PCT.\nPCTPC\n" } ]