url
stringlengths
49
92
description
stringlengths
22
4.78k
cases
listlengths
0
6
https://atcoder.jp/contests/abc357/tasks/abc357_c
Problem Statement For a non-negative integer K , we define a level- K carpet as follows: A level- 0 carpet is a 1 \times 1 grid consisting of a single black cell. For K > 0 , a level- K carpet is a 3^K \times 3^K grid. When this grid is divided into nine 3^{K-1} \times 3^{K-1} blocks: The central block consists entirely of white cells. The other eight blocks are level- (K-1) carpets. You are given a non-negative integer N . Print a level- N carpet according to the specified format.
[ { "input": "1\n", "output": "###\n#.#\n###\n" }, { "input": "2\n", "output": "#########\n#.##.##.#\n#########\n###...###\n#.#...#.#\n###...###\n#########\n#.##.##.#\n#########\n" } ]
https://atcoder.jp/contests/abc357/tasks/abc357_d
Problem Statement For a positive integer N , let V_N be the integer formed by concatenating N exactly N times. More precisely, consider N as a string, concatenate N copies of it, and treat the result as an integer to get V_N . For example, V_3=333 and V_{10}=10101010101010101010 . Find the remainder when V_N is divided by 998244353 .
[ { "input": "5\n", "output": "55555\n" }, { "input": "9\n", "output": "1755646\n" }, { "input": "10000000000\n", "output": "468086693\n" } ]
https://atcoder.jp/contests/abc357/tasks/abc357_e
Problem Statement There is a directed graph with N vertices numbered 1 to N and N edges. The out-degree of every vertex is 1 , and the edge from vertex i points to vertex a_i . Count the number of pairs of vertices (u, v) such that vertex v is reachable from vertex u . Here, vertex v is reachable from vertex u if there exists a sequence of vertices w_0, w_1, \dots, w_K of length K+1 that satisfies the following conditions. In particular, if u = v , it is always reachable. w_0 = u . w_K = v . For every 0 \leq i \lt K , there is an edge from vertex w_i to vertex w_{i+1} .
[ { "input": "4\n2 1 1 4\n", "output": "8\n" }, { "input": "5\n2 4 3 1 2\n", "output": "14\n" }, { "input": "10\n6 10 4 1 5 9 8 6 5 1\n", "output": "41\n" } ]
https://atcoder.jp/contests/abc357/tasks/abc357_f
Problem Statement You are given sequences of length N , A=(A_1,A_2,\ldots,A_N) and B=(B_1,B_2,\ldots,B_N) . You are also given Q queries to process in order. There are three types of queries: 1 l r x : Add x to each of A_l, A_{l+1}, \ldots, A_r . 2 l r x : Add x to each of B_l, B_{l+1}, \ldots, B_r . 3 l r : Print the remainder of \displaystyle\sum_{i=l}^r (A_i\times B_i) when divided by 998244353 .
[ { "input": "5 6\n1 3 5 6 8\n3 1 2 1 2\n3 1 3\n1 2 5 3\n3 1 3\n1 1 3 1\n2 5 5 2\n3 1 5\n", "output": "16\n25\n84\n" }, { "input": "2 3\n1000000000 1000000000\n1000000000 1000000000\n3 1 1\n1 2 2 1000000000\n3 1 2\n", "output": "716070898\n151723988\n" } ]
https://atcoder.jp/contests/abc357/tasks/abc357_g
Problem Statement There is a special grid with N rows. ( N is even.) The i -th row from the top has \left \lceil \frac{i}{2} \right \rceil \times 2 cells from the left end. For example, when N = 6 , the grid looks like the following: Let (i, j) denote the cell at the i -th row from the top and j -th column from the left. Each cell is either an empty cell or a wall cell. There are M wall cells, and the i -th wall cell is (a_i, b_i) . Here, (1, 1) and (N, N) are empty. Starting from (1, 1) , how many ways are there to reach (N, N) by repeatedly moving right or down to an adjacent empty cell? Find the count modulo 998244353 .
[ { "input": "4 2\n2 1\n4 2\n", "output": "2\n" }, { "input": "6 3\n2 1\n3 3\n4 2\n", "output": "0\n" }, { "input": "100 10\n36 9\n38 5\n38 30\n45 1\n48 40\n71 52\n85 27\n86 52\n92 34\n98 37\n", "output": "619611437\n" }, { "input": "100000 10\n552 24\n4817 255\n7800 954\n23347 9307\n28028 17652\n39207 11859\n48670 22013\n74678 53158\n75345 45891\n88455 4693\n", "output": "175892766\n" } ]
https://atcoder.jp/contests/arc179/tasks/arc179_a
Problem Statement You are given integers N and K . The cumulative sums of an integer sequence X=(X_1,X_2,\dots ,X_N) of length N is defined as a sequence Y=(Y_0,Y_1,\dots ,Y_N) of length N+1 as follows: Y_0=0 Y_i=\displaystyle\sum_{j=1}^{i}X_j\ (i=1,2,\dots ,N) An integer sequence X=(X_1,X_2,\dots ,X_N) of length N is called a good sequence if and only if it satisfies the following condition: Any value in the cumulative sums of X that is less than K appears before any value that is not less than K . Formally, for the cumulative sums Y of X , for any pair of integers (i,j) such that 0 \le i,j \le N , if (Y_i < K and Y_j \ge K) , then i < j . You are given an integer sequence A=(A_1,A_2,\dots ,A_N) of length N . Determine whether the elements of A can be rearranged to a good sequence. If so, print one such rearrangement.
[ { "input": "4 1\n-1 2 -3 4\n", "output": "Yes\n-3 -1 2 4\n" }, { "input": "4 -1\n1 -2 3 -4\n", "output": "No\n" }, { "input": "10 1000000000\n-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n", "output": "Yes\n-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n" } ]
https://atcoder.jp/contests/arc179/tasks/arc179_b
Problem Statement You are given a sequence (X_1, X_2, \dots, X_M) of length M consisting of integers between 1 and M , inclusive. Find the number, modulo 998244353 , of sequences A = (A_1, A_2, \dots, A_N) of length N consisting of integers between 1 and M , inclusive, that satisfy the following condition: For each B = 1, 2, \dots, M , the value X_B exists between any two different occurrences of B in A (including both ends). More formally, for each B = 1, 2, \dots, M , the following condition must hold: For every pair of integers (l, r) such that 1 \leq l < r \leq N and A_l = A_r = B , there exists an integer m ( l \leq m \leq r ) such that A_m = X_B .
[ { "input": "3 4\n2 1 2\n", "output": "14\n" }, { "input": "4 8\n1 2 3 4\n", "output": "65536\n" }, { "input": "4 9\n2 3 4 1\n", "output": "628\n" } ]
https://atcoder.jp/contests/arc179/tasks/arc179_c
Problem Statement This is an interactive problem (where your program interacts with the judge via input and output). You are given a positive integer N . The judge has a hidden positive integer R and N integers A_1, A_2, \dots, A_N . It is guaranteed that |A_i|\le R and \left|\displaystyle\sum_{i=1}^{N}A_i\right| \le R . There is a blackboard on which you can write integers with absolute values not exceeding R . Initially, the blackboard is empty. The judge has written the values A_1, A_2, \dots, A_N on the blackboard in this order . Your task is to make the blackboard contain only one value \displaystyle\sum_{i=1}^{N}A_i . You cannot learn the values of R and A_i directly, but you can interact with the judge up to 25000 times. For a positive integer i , let X_i be the i -th integer written on the blackboard. Specifically, X_i = A_i for i=1,2,\dots,N . In one interaction, you can specify two distinct positive integers i and j and choose one of the following actions: Perform addition. The judge will erase X_i and X_j from the blackboard and write X_i + X_j on the blackboard. |X_i + X_j| \leq R must hold. Perform comparison. The judge will tell you whether X_i < X_j is true or false. Here, at the beginning of each interaction, the i -th and j -th integers written on the blackboard must not have been erased. Perform the interactions appropriately so that after all interactions, the blackboard contains only one value \displaystyle\sum_{i=1}^{N}A_i . The values of R and A_i are determined before the start of the conversation between your program and the judge.
[]
https://atcoder.jp/contests/arc179/tasks/arc179_d
Problem Statement You are given a tree with N vertices numbered 1, 2, \dots, N . The i -th edge connects vertices u_i and v_i bidirectionally. Initially, all vertices are painted white. To efficiently visit all vertices of this tree, Alice has invented a magical gate. She uses one piece and one gate to travel according to the following procedure. First, she chooses a vertex and places both the piece and the gate on that vertex. Then, she repeatedly performs the following operations until all vertices are painted black. Choose one of the following actions: Paint the vertex where the piece is placed black. Choose a vertex adjacent to the vertex where the piece is placed and move the piece to that vertex. The cost of this action is 1 . Move the piece to the vertex where the gate is placed. Move the gate to the vertex where the piece is placed. Note that only the second action incurs a cost. It can be proved that it is possible to paint all vertices black in a finite number of operations. Find the minimum total cost required.
[ { "input": "4\n1 2\n1 3\n1 4\n", "output": "3\n" }, { "input": "10\n1 7\n7 10\n10 8\n8 3\n8 4\n10 9\n9 6\n9 5\n7 2\n", "output": "10\n" } ]
https://atcoder.jp/contests/arc179/tasks/arc179_e
Problem Statement For positive integers h and w , let (h,w) denote a rectangle with height h and width w . In this problem, we do not consider rotating the rectangles, and the rectangles (h,w) and (w,h) are distinguished when h \neq w . A sequence of rectangles ((h_1,w_1),(h_2,w_2),\dots ,(h_n,w_n)) is called a rectangle generation sequence if there exists a method that successfully follows the steps below: Let the rectangle X be (h_1,w_1) . Hereafter, let H and W respectively denote the height and width of the rectangle X at each step. For i=2,3,\dots ,n , perform one of the following operations. If neither can be performed, the procedure unsuccessfully terminates. If the height of X is equal to h_i , attach the rectangle (h_i,w_i) horizontally to X . Formally, if H=h_i at that time, replace X with the rectangle (H,W+w_i) . If the width of X is equal to w_i , attach the rectangle (h_i,w_i) vertically to X . Formally, if W=w_i at that time, replace X with the rectangle (H+h_i,W) . If the above series of operations does not fail, the procedure successfully terminates. You are given N rectangles. The i -th rectangle has a height of H_i and a width of W_i . Find the number of pairs of positive integers (l,r) that satisfy 1 \le l \le r \le N and the following condition: The sequence of rectangles ((H_l,W_l),(H_{l+1},W_{l+1}),\dots ,(H_r,W_r)) is a rectangle generation sequence.
[ { "input": "4\n1 2\n1 3\n2 3\n3 1\n", "output": "7\n" }, { "input": "5\n2 1\n2 1\n1 2\n3 2\n1 4\n", "output": "10\n" }, { "input": "1\n1000000 1000000\n", "output": "1\n" }, { "input": "10\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n", "output": "55\n" } ]
https://atcoder.jp/contests/arc179/tasks/arc179_f
Problem Statement You are given a string S of length N consisting of the characters A and B . For a string X of length N consisting of the characters 1 , 2 , and 3 , the score is determined by the following procedure: First, initialize the variables h_1, h_2, h_3, P to 0 . Then, for i = 1, 2, \dots, N in this order, perform the following operations: If the i -th character of S is A , perform operation A; if it is B , perform operation B. Let d be the number represented by the i -th character of X . Operation A : Add 2 to h_d . Operation B : If d = 2 or h_d \neq h_2 , set P to -10^{100} . Otherwise, add 1 to both h_d and h_2 . If h_1 = h_2 = h_3 , add 1 to P . The final value of P is the score. Print one string X that maximizes the score. You have T test cases to solve.
[ { "input": "5\n4\nABBA\n5\nAAAAA\n6\nBBBBBB\n7\nABABABA\n20\nAAABBBBBBBBAAABBBABA\n", "output": "1333\n12321\n333333\n1313212\n33311111133121111311\n" } ]
https://atcoder.jp/contests/abc356/tasks/abc356_a
Problem Statement You are given positive integers N , L , and R . For a sequence A = (1, 2, \dots, N) of length N , an operation of reversing the L -th through R -th elements was performed once. Print the sequence after this operation.
[ { "input": "5 2 3\n", "output": "1 3 2 4 5\n" }, { "input": "7 1 1\n", "output": "1 2 3 4 5 6 7\n" }, { "input": "10 1 10\n", "output": "10 9 8 7 6 5 4 3 2 1\n" } ]
https://atcoder.jp/contests/abc356/tasks/abc356_b
Problem Statement Takahashi is health-conscious and concerned about whether he is getting enough of M types of nutrients from his diet. For the i -th nutrient, his goal is to take at least A_i units per day. Today, he ate N foods, and from the i -th food, he took X_{i,j} units of nutrient j . Determine whether he has met the goal for all M types of nutrients.
[ { "input": "2 3\n10 20 30\n20 0 10\n0 100 100\n", "output": "Yes\n" }, { "input": "2 4\n10 20 30 40\n20 0 10 30\n0 100 100 0\n", "output": "No\n" } ]
https://atcoder.jp/contests/abc356/tasks/abc356_c
Problem Statement You have N keys numbered 1, 2, \dots, N . Some of these are real keys, while the others are dummies. There is a door, Door X, into which you can insert any number of keys. Door X will open if and only if at least K real keys are inserted. You have conducted M tests on these keys. The i -th test went as follows: You inserted C_i keys A_{i,1}, A_{i,2}, \dots, A_{i,C_i} into Door X. The test result is represented by a single English letter R_i . R_i = o means that Door X opened in the i -th test. R_i = x means that Door X did not open in the i -th test. There are 2^N possible combinations of which keys are real and which are dummies. Among these, find the number of combinations that do not contradict any of the test results. It is possible that the given test results are incorrect and no combination satisfies the conditions. In such a case, report 0 .
[ { "input": "3 2 2\n3 1 2 3 o\n2 2 3 x\n", "output": "2\n" }, { "input": "4 5 3\n3 1 2 3 o\n3 2 3 4 o\n3 3 4 1 o\n3 4 1 2 o\n4 1 2 3 4 x\n", "output": "0\n" }, { "input": "11 4 9\n10 1 2 3 4 5 6 7 8 9 10 o\n11 1 2 3 4 5 6 7 8 9 10 11 o\n10 11 10 9 8 7 6 5 4 3 2 x\n10 11 9 1 4 3 7 5 6 2 10 x\n", "output": "8\n" } ]
https://atcoder.jp/contests/abc356/tasks/abc356_d
Problem Statement Given integers N and M , compute the sum \displaystyle \sum_{k=0}^{N} \rm{popcount} (k \mathbin{\&} M) , modulo 998244353 . Here, \mathbin{\&} represents the bitwise \rm{AND} operation. What is the bitwise \rm{AND} operation? The result x = a \mathbin{\&} b of the bitwise \rm{AND} operation between non-negative integers a and b is defined as follows: x is the unique non-negative integer that satisfies the following conditions for all non-negative integers k : If the 2^k place in the binary representation of a and the 2^k place in the binary representation of b are both 1 , then the 2^k place in the binary representation of x is 1 . Otherwise, the 2^k place in the binary representation of x is 0 . For example, 3=11_{(2)} and 5=101_{(2)} , so 3 \mathbin{\&} 5 = 1 . What is \rm{popcount} ? \rm{popcount} (x) represents the number of 1 s in the binary representation of x . For example, 13=1101_{(2)} , so \rm{popcount} (13) = 3 .
[ { "input": "4 3\n", "output": "4\n" }, { "input": "0 0\n", "output": "0\n" }, { "input": "1152921504606846975 1152921504606846975\n", "output": "499791890\n" } ]
https://atcoder.jp/contests/abc356/tasks/abc356_e
Problem Statement You are given a sequence A=(A_1,\ldots,A_N) of length N . Find \displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^{N}\left\lfloor\frac{\max(A_i,A_j)}{\min(A_i,A_j)}\right\rfloor . Here, \lfloor x \rfloor represents the greatest integer not greater than x . For example, \lfloor 3.14 \rfloor=3 and \lfloor 2 \rfloor=2 .
[ { "input": "3\n3 1 4\n", "output": "8\n" }, { "input": "6\n2 7 1 8 2 8\n", "output": "53\n" }, { "input": "12\n3 31 314 3141 31415 314159 2 27 271 2718 27182 271828\n", "output": "592622\n" } ]
https://atcoder.jp/contests/abc356/tasks/abc356_f
Problem Statement You are given an integer K . For a set S that is initially empty, process Q queries of the following two types in order: 1 x : An integer x is given. If x is in S , remove x from S . Otherwise, add x to S . 2 x : An integer x that is in S is given. Consider a graph where the vertices are the numbers in S , and there is an edge between two numbers if and only if the absolute difference between them is at most K . Print the count of vertices in the connected component that contains x .
[ { "input": "7 5\n1 3\n1 10\n2 3\n1 7\n2 3\n1 10\n2 3\n", "output": "1\n3\n2\n" }, { "input": "11 1000000000000000000\n1 1\n1 100\n1 10000\n1 1000000\n1 100000000\n1 10000000000\n1 1000000000000\n1 100000000000000\n1 10000000000000000\n1 1000000000000000000\n2 1\n", "output": "10\n" }, { "input": "8 0\n1 1\n1 2\n2 1\n1 1\n1 2\n1 1\n1 2\n2 1\n", "output": "1\n1\n" } ]
https://atcoder.jp/contests/abc356/tasks/abc356_g
Problem Statement Takahashi can swim in N different styles. When he swims in the i -th style, he consumes A_i stamina per second and advances B_i meters per second. Answer Q queries. The i -th query is as follows: Determine if it is possible to advance D_i meters while keeping the total stamina consumption at most C_i . If it is possible, find the minimum number of seconds required. Here, he can freely combine different swimming styles, and the time to switch styles is negligible. Specifically, he can swim using the following steps: Choose a positive integer m , a sequence of positive real numbers t=(t_1,t_2,\dots,t_m) of length m , and a sequence of integers x=(x_1,x_2,\dots,x_m) of length m where each element is between 1 and N , inclusive. Then, swim in the x_i -th style for t_i seconds in the order i=1,2,\dots,m .
[ { "input": "4\n1 2\n2 3\n3 3\n4 4\n5\n4 7\n7 7\n49 100\n1000 500\n4 5\n", "output": "3.000000000000000000\n1.750000000000000000\n-1\n125.000000000000000000\n1.500000000000000000\n" } ]
https://atcoder.jp/contests/past19-open/tasks/past19_a
Problem Statement There is a bus stop where buses depart at X minutes past each hour. For example, if X=10 , buses depart at 0:10 , 1:10 , \ldots , and 23:10 . No buses depart at any other time. It is currently M minutes past H ( M\neq X ). How many minutes are left before the next bus departs?
[ { "input": "30 14 10\n", "output": "20\n" }, { "input": "12 23 30\n", "output": "42\n" }, { "input": "1 17 34\n", "output": "27\n" } ]
https://atcoder.jp/contests/past19-open/tasks/past19_b
Problem Statement Takahashi made (N - 1) transactions at a bank. The transactions are described by integers A_1, A_2, \ldots, A_N . The i -th transaction is a deposit of (A_{i + 1} - A_i) yen if A_i < A_{i + 1} , and a withdrawal of (A_i - A_{i + 1}) yen if A_i > A_{i + 1} . (Yen is a currency of Japan.) Here, it is guaranteed that the given input satisfies A_i \neq A_{i + 1} . Find the total amounts of money deposited and withdrawn through these transactions.
[ { "input": "4\n2 9 5 8\n", "output": "10 4\n" }, { "input": "5\n10 8 6 4 2\n", "output": "0 8\n" } ]
https://atcoder.jp/contests/past19-open/tasks/past19_c
Problem Statement A good number is defined as follows. A positive integer x is said to be a good number if and only if: every pair of adjacent digits in x has a difference of 1 or less. Formally, if x has k digits and its decimal representation is d_1d_2 \dots d_k , then |d_i - d_{i+1}| \le 1 for all integers i with 1 \le i < k . For example, 1,56,777 , and 3234 are good numbers, but 13,1235 , and 909 are not. A goodish number is defined as follows. A positive integer x is said to be a goodish number if and only if: one can modify at most one digit of x to make it a good number. Here, it is disallowed to produce a leading zero. Formally, if x has k digit and its decimal representation is d_1d_2 \dots d_k , there exists an integer pair (p,q) such that: 1 \le p \le k , 0 \le q \le 9 , (p,q) \neq (1,0) , and setting d_p in x to q makes x a good number. Note that a good number is always a goodish number. For example, given 7176 , one can set d_2 to 8 to make it 7876 , which is a good number, so 7176 is a goodish number. Given an integer N , determine if N is a goodish number.
[ { "input": "7176\n", "output": "Yes\n" }, { "input": "2020\n", "output": "No\n" }, { "input": "3728\n", "output": "No\n" }, { "input": "987654321012345678\n", "output": "Yes\n" } ]
https://atcoder.jp/contests/past19-open/tasks/past19_d
Problem Statement N teams competed in a programming contest. The contest lasted T minutes. Team i solved A_i problems, and the last acceptance time was B_i . In this contest, the ranks are determined as follows: Those who solved more problems rank higher. Among those with the same number of solved problems, those with earlier last acceptance time rank higher. Among those with the same number of solved problems and last acceptance time, those with a smaller team number rank higher. Let A' be the number of problems solved by the team ranked first, and B' be its last acceptance time. Find the following value G_i for each team. G_i = T \times (A' - A_i) + (B_i - B')
[ { "input": "6 120\n3 80\n4 90\n5 120\n5 100\n3 110\n4 70\n", "output": "220\n110\n20\n0\n250\n90\n" } ]
https://atcoder.jp/contests/past19-open/tasks/past19_e
Problem Statement You want to implement a C-like comment-out system. You are given a length- N string S consisting of / , * , and lowercase English letters. You are asked to perform the following procedure against S . Prepare a variable x that takes on an integer value. x is initialized with 1 . Seek for an n such that: x \leq n \leq |S| - 1 ; and the n -th character of S is / and the (n + 1) -th character of S is * . If no n satisfies the conditions in step 2, terminate the procedure. Otherwise, let i be the smallest such n . Seek for an n such that: i + 2 \leq n \leq |S| - 1 ; and the n -th character of S is * and the (n + 1) -th character of S is / . If no n satisfies the conditions in step 4, terminate the procedure. Otherwise, let j be the smallest such n . Remove the i -th through (j + 1) -th characters of S , concatenate the remaining strings without changing the order, and set S to the resulting string. Set x to i , and go back to step 2. Print S resulting from the procedure.
[ { "input": "7\na/*b*/c\n", "output": "ac\n" }, { "input": "10\n/*a*//*b*/\n", "output": "\n" }, { "input": "18\n/*/a/*b*/c/*d*/e*/\n", "output": "ce*/\n" } ]
https://atcoder.jp/contests/past19-open/tasks/past19_f
Problem Statement You are lining up dominoes. N clues are given: for each 1 \leq i \leq N , if domino S_i falls, then domino T_i will also fall. Determine if the given clues imply that if domino X falls, then domino Y will also fall.
[ { "input": "5\nsecond fourth\nfirst second\nsecond third\nthird fourth\nfourth fifth\nfifth sixth\n", "output": "Yes\n" }, { "input": "5\nfourth second\nfirst second\nsecond third\nthird fourth\nfourth fifth\nfifth sixth\n", "output": "No\n" }, { "input": "6\ne d\na b\nb a\na c\nc d\nd e\ne a\n", "output": "Yes\n" }, { "input": "1\na b\nx y\n", "output": "No\n" } ]
https://atcoder.jp/contests/past19-open/tasks/past19_g
Problem Statement You are given an N -by- N matrix A . The element in the i -th row and j -th column of A is denoted by A(i,j) . Exactly one element of A is 0 , and each of the other (N^2-1) elements is an integer between 1 and N . How many ways are there to replace the only element 0 in A with an integer between 1 and N , inclusive, such that the resulting A satisfies the following condition? A(A(i,j),k)=A(i,A(j,k)) for all 1\leq i,j,k\leq N .
[ { "input": "3\n1 2 3\n2 0 1\n3 1 2\n", "output": "1\n" }, { "input": "3\n2 1 0\n1 2 3\n1 3 2\n", "output": "0\n" }, { "input": "6\n4 5 5 2 4 2\n5 2 2 4 5 4\n5 2 0 4 5 4\n2 4 4 5 2 5\n4 5 5 2 4 2\n2 4 4 5 2 5\n", "output": "2\n" } ]
https://atcoder.jp/contests/past19-open/tasks/past19_h
Problem Statement You are given an integer S , and N integers a_1,a_2,\dots,a_N . Can you construct a mathematical expression that evaluates to S using + , \times , and the N integers? Here, use each of the N integers exactly once. You can freely rearrange the integers. You may not use parentheses. You may not join integers without an operator in between (for example, join 1 and 2 to form 12 ).
[ { "input": "3 11\n1 2 5\n", "output": "Yes\n1+2x5\n" }, { "input": "5 2\n1 1 1 1 1\n", "output": "Yes\n1+1x1x1x1\n" }, { "input": "2 12\n1 2\n", "output": "No\n" }, { "input": "6 302934\n614 490 585 613 420 945\n", "output": "Yes\n420+490x613+585+614+945\n" } ]
https://atcoder.jp/contests/past19-open/tasks/past19_i
Problem Statement AtCoderLand has N islands: island 1 , island 2 , \ldots , and island N , and M roads: road 1 , road 2 , \ldots , and road M . Road i connects island A_i and island B_i bidirectionally. Road i\ (1\leq i\leq M) will be submerged on day D_i , so it will not be available on or after day D_i . The islands are said to be connected if one can travel between any pair of islands via available roads. On day 0 (when no road is submerged), the islands are connected. Find the integer X such that the islands are connected on day (X-1) but not on day X .
[ { "input": "5 6\n1 2 10\n1 3 25\n2 3 35\n2 4 15\n3 4 30\n4 5 40\n", "output": "25\n" }, { "input": "3 4\n1 2 31\n2 3 4\n2 3 15\n3 3 9\n", "output": "15\n" }, { "input": "15 20\n6 7 54\n5 15 63\n8 14 30\n5 8 52\n5 9 96\n1 13 51\n8 13 89\n4 15 81\n12 12 80\n4 10 48\n1 11 63\n1 9 72\n10 14 83\n3 5 30\n7 12 60\n1 10 60\n2 11 68\n5 7 52\n6 12 34\n1 8 41\n", "output": "30\n" } ]
https://atcoder.jp/contests/past19-open/tasks/past19_j
Problem Statement There are 3N people numbered from 1 to 3N . Person i has A_i yen (currency in Japan). You are going to group the 3N people into N groups of three people each. Let S_i be the total amount of yen in the i -th group. Find the minimum possible \displaystyle \max_{1 \leq k \leq N} S_k - \min_{1 \leq k \leq N}S_k .
[ { "input": "2\n1 3 4 6 2 9\n", "output": "1\n" }, { "input": "2\n0 0 0 0 0 100000000\n", "output": "100000000\n" }, { "input": "5\n614 490 420 945 613 585 760 38 926 725 667 685 449 455 873\n", "output": "35\n" } ]
https://atcoder.jp/contests/past19-open/tasks/past19_k
Problem Statement There is an N -vertex tree whose vertices are numbered 1 through N . The i -th edge connects vertex u_i and vertex v_i bidirectionally. Vertex i has an integer A_i written on it. Determine if there is a way to choose K vertices among the N so that no pair of chosen vertices are adjacent. If there is, find the maximum sum of integers written on the chosen K vertices.
[ { "input": "5 2\n1 2\n2 3\n1 4\n2 5\n3 1 4 1 5\n", "output": "9\n" }, { "input": "10 6\n7 3\n3 8\n5 1\n3 9\n2 5\n6 1\n5 4\n10 7\n7 1\n1 3 10 6 7 9 2 2 10 4\n", "output": "34\n" }, { "input": "10 6\n2 10\n1 10\n7 4\n8 9\n8 7\n4 3\n1 4\n6 7\n5 3\n2 5 1 2 3 6 2 9 9 7\n", "output": "-1\n" } ]
https://atcoder.jp/contests/past19-open/tasks/past19_l
Problem Statement A length- k sequence A=(A_1,\ldots,A_k) is a zigzag sequence if: A_1 < A_2> A_3 < A_4 > \ldots , or A_1 > A_2 < A_3 > A_4 <\ldots . More formally, a sequence is said to be a zigzag sequence if and only if: A_i \neq A_{i+1}\ (1\leq i \leq k-1) , and (A_{i+1}-A_i)(A_i-A_{i-1}) < 0\ (2\leq i \leq k-1) . Especially, note that a sequence of length one is always a zigzag sequence. Given a length- N sequence B=(B_1,\ldots,B_N) , find the maximum length of a subsequence of B that is a zigzag sequence. What is a subsequence? A subsequence of a sequence is obtained by removing zero or more elements from the original sequence and concatenating the rest preserving the order. For example, (10,30) is a subsequence of (10,20,30) , but (20,10) is not a subsequence of (10,20,30) .
[ { "input": "7\n5 1 2 3 4 3 3\n", "output": "4\n" }, { "input": "5\n1 2 3 4 5\n", "output": "2\n" } ]
https://atcoder.jp/contests/past19-open/tasks/past19_m
Problem Statement You are given an N -vertex M -edge simple directed graph. The vertices are numbered 1 through N , and the edges are numbered 1 through M . Edge j directs from vertex U_j to vertex V_j . Each vertex has an integer attribute point : the point of vertex i is P_i . Also, each edge has a real attribute decay between 0 and 1 : the decay of edge j is W_j . It is guaranteed that the given graph does not have a closed cycle. (In other words, there is no way to travel from a vertex i along edges to go back to vertex i .) Takahashi is going to play a game on this graph. Initially, vertex 1 has a piece on it, and Takahashi's score is 0 . He performs the following procedure to move the piece to vertex N while accumulating his score. Let i be the current number of vertex with the piece. Add the point P_i of vertex i to his score. If i=N , he ends the game successfully . If there is no edge going from vertex i , he ends the game unsuccessfully . Otherwise, choose an edge going from vertex i . Let j be the number of the chosen edge. Multiply the point of each vertex by the decay of edge j . In other words, let P_i \leftarrow P_i\times W_j for each i\ (1\leq i\leq N) . Move the piece to vertex V_j , and jump to step 1. Determine if he can end the game successfully. If it is possible, print the maximum possible final score of the game.
[ { "input": "4 4\n2 5 3 1\n1 3 0.500000\n1 2 0.250000\n2 3 0.250000\n3 4 0.500000\n", "output": "3.7500000000\n" }, { "input": "3 1\n1 1 1\n1 2 0.000000\n", "output": "-1\n" }, { "input": "6 12\n22 75 26 45 72 81\n4 6 0.185514\n3 6 0.758252\n2 3 0.622989\n2 4 0.984614\n1 3 0.465086\n1 5 0.396959\n5 3 0.618272\n5 2 0.016128\n1 2 0.869673\n1 4 0.363219\n2 6 0.097935\n5 4 0.877468\n", "output": "138.6258141601\n" } ]
https://atcoder.jp/contests/past19-open/tasks/past19_n
Problem Statement You are given a length- N sequence A=(A _ 1,A _ 2,\ldots,A _ N) of non-negative integers, and a real value x strictly greater than 0 and less than or equal to 1 . For an integer pair (l,r)\ (1\leq l\leq r\leq N) , we define f(l,r) as follows: \begin{aligned}f(l,r)&=A _ l+A _ {l+1}x+A _ {l+2}x ^ 2+\cdots+A _ {r}x ^ {r-l}\\&=\sum _ {i=0} ^ {r - l}A _ {i+l}x ^ i.\end{aligned} Answer Q queries. In the i -th query (1\leq i\leq Q) , given an integer pair (l _ i,r _ i)\ (1\leq l _ i\leq r _ i\leq N) , find f(l _ i,r _ i) .
[ { "input": "7\n3 1 4 1 5 9 2\n0.25\n4\n1 3\n3 6\n1 7\n2 2\n", "output": "3.5\n4.703125\n3.54443359375\n1\n" }, { "input": "15\n6 45 81 83 28 40 31 30 51 31 59 50 94 36 0\n0.998244353\n20\n1 2\n1 5\n1 10\n2 5\n2 10\n3 3\n3 8\n3 12\n4 4\n4 14\n5 5\n5 11\n5 13\n5 14\n6 7\n7 7\n8 10\n10 13\n10 14\n12 14\n", "output": "50.920995885\n242.004326432639783195178618854259\n422.764240065920208662445303267128\n236.419395434977014285377699356\n417.497217803865912440022892137641\n81\n292.066191815728552247199317241863\n480.544234637661958884554506013369\n83\n528.144974561396676686531955107577\n28\n268.416129630805326045766513470066\n410.492717775411750526215653552396\n445.927866482402907904585917660160\n70.945574943\n31\n111.801707440188046879\n233.226782486727123857410847838\n268.974634315843968519190799533618\n179.708673560669989924\n" } ]
https://atcoder.jp/contests/past19-open/tasks/past19_o
Problem Statement A 4\times 4 grid is said to be good if it is in the following state: For every integer i between 1 and 15 , inclusive, there is exactly one cell with that integer written on it. No other cell has something written on it. In other words, there is exactly one cell with nothing written on it. You are given two distinct good grids S and T . Here, the good grid S is given by 16 integers S_{i,j} (1\leq i,j\leq 4) . If 1\leq S_{i,j}\leq 15 , it means that the cell in the i -th row from the top and j -th column from the left has S_{i,j} written on it; if S_{i,j}=-1 , it means that the cell in the i -th row from the top and j -th column from the left has nothing written on it. Likewise, T is given by 16 integers T_{i,j} (1\leq i,j\leq 4) . Find the minimum number of times of performing the following operation to make the grid equal to T , starting from S . If the grid cannot be made equal to T with 30 operations or less (including the case where any number of operations can do so), print -1 instead. Choose a cell adjacent to the cell with nothing written on it, and write the integer written on the chosen cell to the empty cell. Then, erase the integer written on the chosen cell.
[ { "input": "1 2 3 4\n5 6 7 8\n9 10 11 12\n13 14 15 -1\n1 2 3 4\n5 6 -1 8\n9 10 7 11\n13 14 15 12\n", "output": "3\n" }, { "input": "-1 11 10 9\n1 12 15 8\n2 13 14 7\n3 4 5 6\n6 5 4 3\n7 12 15 2\n8 13 14 1\n9 10 11 -1\n", "output": "-1\n" }, { "input": "1 12 11 10\n2 13 -1 9\n3 14 15 8\n4 5 6 7\n1 10 5 14\n2 11 6 15\n3 12 7 -1\n4 13 8 9\n", "output": "30\n" } ]
https://atcoder.jp/contests/abc355/tasks/abc355_a
Problem Statement Takahashi's cake has been eaten by someone. There are three suspects: person 1 , person 2 , and person 3 . There are two witnesses, Ringo and Snuke. Ringo remembers that person A is not the culprit, and Snuke remembers that person B is not the culprit. Determine if the culprit can be uniquely identified based on the memories of the two witnesses. If the culprit can be identified, print the person's number.
[ { "input": "1 2\n", "output": "3\n" }, { "input": "1 1\n", "output": "-1\n" }, { "input": "3 1\n", "output": "2\n" } ]
https://atcoder.jp/contests/abc355/tasks/abc355_b
Problem Statement You are given a sequence A=(A_1,A_2,\dots,A_N) of length N and a sequence B=(B_1,B_2,\dots,B_M) of length M . Here, all elements of A and B are pairwise distinct. Determine whether the sequence C=(C_1,C_2,\dots,C_{N+M}) formed by sorting all elements of A and B in ascending order contains two consecutive elements appearing in A .
[ { "input": "3 2\n3 2 5\n4 1\n", "output": "Yes\n" }, { "input": "3 2\n3 1 5\n4 2\n", "output": "No\n" }, { "input": "1 1\n1\n2\n", "output": "No\n" } ]
https://atcoder.jp/contests/abc355/tasks/abc355_c
Problem Statement There is an N \times N grid, where the cell at the i -th row from the top and the j -th column from the left contains the integer N \times (i-1) + j . Over T turns, integers will be announced. On Turn i , the integer A_i is announced, and the cell containing A_i is marked. Determine the turn on which Bingo is achieved for the first time. If Bingo is not achieved within T turns, print -1 . Here, achieving Bingo means satisfying at least one of the following conditions: There exists a row in which all N cells are marked. There exists a column in which all N cells are marked. There exists a diagonal line (from top-left to bottom-right or from top-right to bottom-left) in which all N cells are marked.
[ { "input": "3 5\n5 1 8 9 7\n", "output": "4\n" }, { "input": "3 5\n4 2 9 7 5\n", "output": "-1\n" }, { "input": "4 12\n13 9 6 5 2 7 16 14 8 3 10 11\n", "output": "9\n" } ]
https://atcoder.jp/contests/abc355/tasks/abc355_d
Problem Statement You are given N intervals of real numbers. The i -th (1 \leq i \leq N) interval is [l_i, r_i] . Find the number of pairs (i, j)\,(1 \leq i < j \leq N) such that the i -th and j -th intervals intersect.
[ { "input": "3\n1 5\n7 8\n3 7\n", "output": "2\n" }, { "input": "3\n3 4\n2 5\n1 6\n", "output": "3\n" }, { "input": "2\n1 2\n3 4\n", "output": "0\n" } ]
https://atcoder.jp/contests/abc355/tasks/abc355_e
Problem Statement This is an interactive problem (where your program interacts with the judge via input and output). You are given a positive integer N and integers L and R such that 0 \leq L \leq R < 2^N . The judge has a hidden sequence A = (A_0, A_1, \dots, A_{2^N-1}) consisting of integers between 0 and 99 , inclusive. Your goal is to find the remainder when A_L + A_{L+1} + \dots + A_R is divided by 100 . However, you cannot directly know the values of the elements in the sequence A . Instead, you can ask the judge the following question: Choose non-negative integers i and j such that 2^i(j+1) \leq 2^N . Let l = 2^i j and r = 2^i (j+1) - 1 . Ask for the remainder when A_l + A_{l+1} + \dots + A_r is divided by 100 . Let m be the minimum number of questions required to determine the remainder when A_L + A_{L+1} + \dots + A_R is divided by 100 for any sequence A . You need to find this remainder within m questions.
[]
https://atcoder.jp/contests/abc355/tasks/abc355_f
Problem Statement You are given a weighted undirected connected graph G with N vertices and N-1 edges, where vertices are numbered 1 to N and edges are numbered 1 to N-1 . Edge i connects vertices a_i and b_i with a weight of c_i . You are given Q queries to process sequentially. The i -th query is described as follows: You are given integers u_i, v_i, w_i . Add an edge with weight w_i between vertices u_i and v_i in G . Then, print the sum of the weights of the edges in a minimum spanning tree of G .
[ { "input": "4 4\n1 2 6\n2 3 5\n2 4 4\n1 3 3\n1 2 3\n1 4 10\n3 4 1\n", "output": "12\n10\n10\n7\n" }, { "input": "8 6\n1 8 8\n1 6 10\n1 5 8\n2 6 6\n6 7 6\n1 3 9\n2 4 7\n1 3 4\n1 6 7\n3 4 6\n1 5 1\n7 8 4\n3 5 3\n", "output": "49\n46\n45\n38\n34\n33\n" } ]
https://atcoder.jp/contests/abc355/tasks/abc355_g
Problem Statement You are given a sequence P=(P_1,P_2,\dots,P_N) of length N . Takahashi and Aoki will play a game using the sequence P . First, Takahashi will choose K distinct integers x_1,x_2,\dots,x_K from 1,2,\dots,N . Next, Aoki will choose an integer y from 1,2,\dots,N with a probability proportional to P_y . That is, the probability that integer y is chosen is \dfrac{P_y}{\sum_{y'=1}^N P_{y'}} . Then, Aoki's score will be \displaystyle \min_{i=1,2,\dots,K} |x_i-y| . Takahashi wants to minimize the expected value of Aoki's score. Find the expected value of Aoki's score when Takahashi chooses x_1,x_2,\dots,x_K so that this value is minimized, multiplied by \sum_{y'=1}^N P_{y'} . It is guaranteed that the value to be printed is an integer.
[ { "input": "5 2\n1 1 1 1 1\n", "output": "3\n" }, { "input": "5 1\n0 0 1 0 0\n", "output": "0\n" }, { "input": "1 1\n100\n", "output": "0\n" }, { "input": "20 7\n4262 9522 2426 3823 7364 964 2743 2423 1955 5274 3684 847 363 35 278 3220 203 2904 6304 1928\n", "output": "22809\n" } ]
https://atcoder.jp/contests/arc178/tasks/arc178_a
Problem Statement You are given a positive integer N and a sequence of M positive integers A = (A_{1}, A_{2}, \dots, A_{M}) . Here, all elements of A are distinct integers between 1 and N , inclusive. A permutation P = (P_{1}, P_{2}, \dots, P_{N}) of (1, 2, \dots, N) is called a good permutation when it satisfies the following condition for all integers i such that 1 \leq i \leq M : No contiguous subsequence of P is a permutation of (1, 2, \dots, A_{i}) . Determine whether a good permutation exists, and if it does, find the lexicographically smallest good permutation . What is lexicographical order? 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 one of the following conditions holds. Here, |S| and |T| denote the lengths of S and T , respectively. |S| \lt |T| and (S_1, S_2, \ldots, S_{|S|}) = (T_1, T_2, \ldots, T_{|S|}) . There exists an integer 1 \leq i \leq \min\lbrace |S|, |T| \rbrace such that both of the following hold: (S_1, S_2, \ldots, S_{i-1}) = (T_1, T_2, \ldots, T_{i-1}) . S_i is smaller than T_i (as a number).
[ { "input": "4 1\n2\n", "output": "1 3 2 4\n" }, { "input": "5 3\n4 3 2\n", "output": "1 3 4 5 2\n" }, { "input": "92 4\n16 7 1 67\n", "output": "-1\n" }, { "input": "43 2\n43 2\n", "output": "-1\n" } ]
https://atcoder.jp/contests/arc178/tasks/arc178_b
Problem Statement You are given positive integers A_{1}, A_{2}, A_{3} . Find the number, modulo 998244353 , of tuples of positive integers (X_{1}, X_{2}, X_{3}) that satisfy all of the following conditions. X_{1} is a positive integer with A_{1} digits in decimal notation. X_{2} is a positive integer with A_{2} digits in decimal notation. X_{3} is a positive integer with A_{3} digits in decimal notation. X_{1} + X_{2} = X_{3} . You are given T test cases per input file; solve each of them.
[ { "input": "4\n1 1 1\n1 6 7\n167 167 167\n111 666 777\n", "output": "36\n45\n731780675\n0\n" } ]
https://atcoder.jp/contests/arc178/tasks/arc178_c
Problem Statement You are given positive integers N and L , and a sequence of positive integers A = (A_{1}, A_{2}, \dots , A_{N}) of length N . For each i = 1, 2, \dots , N , answer the following question: Determine if there exists a sequence of L non-negative integers B = (B_{1}, B_{2}, \dots, B_{L}) such that \displaystyle \sum_{j = 1} ^ {L - 1} \sum_{k = j + 1} ^ {L} |B_{j} - B_{k}| = A_{i} . If it exists, find the minimum value of \max(B) for such a sequence B .
[ { "input": "2 4\n10 5\n", "output": "3\n-1\n" }, { "input": "6 8\n167 924 167167 167924 116677 154308\n", "output": "11\n58\n10448\n10496\n7293\n9645\n" } ]
https://atcoder.jp/contests/arc178/tasks/arc178_d
Problem Statement You are given a positive integer N and a sequence of M non-negative integers A = (A_{1}, A_{2}, \dots, A_{M}) . Here, all elements of A are distinct integers between 0 and N-1 , inclusive. Find the number, modulo 998244353 , of permutations P of (0, 1, \dots, N-1) that satisfy the following condition. After initializing a sequence B = (B_{1}, B_{2}, \dots, B_{N}) to P , it is possible to make B = A by repeating the following operation some number of times: Choose l and r such that 1 \leq l \leq r \leq |B| , and if \mathrm{mex}(\{B_{l}, B_{l+1}, \dots, B_{r}\}) is contained in B , remove it from B . What is \mathrm{mex}(X) ? For a finite set X of non-negative integers, \mathrm{mex}(X) is defined as the smallest non-negative integer that is not in X .
[ { "input": "4 2\n1 3\n", "output": "8\n" }, { "input": "4 4\n0 3 2 1\n", "output": "1\n" }, { "input": "16 7\n9 2 4 0 1 6 7\n", "output": "3520\n" }, { "input": "92 4\n1 67 16 7\n", "output": "726870122\n" } ]
https://atcoder.jp/contests/arc178/tasks/arc178_e
Problem Statement There are N servals on a bridge of length L . The i -th serval is located at position A_{i} from the left end of the bridge. Here, 0 < A_{1} < A_{2} < \cdots < A_{N} < L holds. For each i = 1, 2, \dots, N , answer the following question: The servals will perform the following three actions in order: Action 1 : The N - 1 servals other than the i -th serval face left or right. Action 2 : The i -th serval faces left or right. Action 3 : All servals start moving simultaneously. All servals move at a constant speed of exactly 1 unit distance per unit time. When a serval reaches the end of the bridge, it leaves the bridge. If two servals collide, they both reverse their direction and continue moving. The i -th serval is smart and loves this bridge, so when choosing a direction in Action 2 , it will observe the directions of the other N-1 servals and choose the direction that allows it to stay on the bridge the longer during Action 3 . There are 2^{N-1} possible combinations of directions for the N-1 servals in Action 1 . Find the sum, modulo 998244353 , over all these combinations, of the durations the i -th serval can stay on the bridge. It can be proved that the output value is an integer.
[ { "input": "2 167\n9 24\n", "output": "182\n301\n" }, { "input": "1 924\n167\n", "output": "757\n" }, { "input": "10 924924167\n46001560 235529797 272749755 301863061 359726177 470023587 667800476 696193062 741860924 809211293\n", "output": "112048251\n409175578\n167800512\n997730745\n278651538\n581491882\n884751575\n570877705\n747965896\n80750577\n" } ]
https://atcoder.jp/contests/arc178/tasks/arc178_f
Problem Statement You are given positive integers N , M , and K , and a sequence of M non-negative integers A = (A_{0}, A_{1}, \dots, A_{M-1}) . Here, 2^{N - 1} \leq K < 2^{N} holds. In the input, K is given as an N -digit number in binary notation, while the other integers are given in decimal notation. Additionally, A is not given directly in the input. Instead, for each i = 0, 1, \dots, M - 1 , you are given a sequence of L_i integers X_{i} = (X_{i,0}, X_{i,1}, \dots, X_{i,L_{i}-1}) such that A_{i} = \sum_{j=0}^{L_{i}-1} 2^{X_{i,j}} . Here, 0 \leq X_{i,0} < X_{i,1} < \cdots < X_{i,L_{i}-1} < N holds. Find the inversion number, modulo 998244353 , of the sequence B = (B_{0}, B_{1}, \dots, B_{MK-1}) defined as follows. For any integer a such that 0 \leq a < K and any integer b such that 0 \leq b < M , the following holds: B_{aM+b} is equal to the remainder when \operatorname{popcount}(a \operatorname{AND} A_{b}) is divided by 2 . What is \operatorname{AND} ? The bitwise \operatorname{AND} of integers A and B , denoted as A \operatorname{AND} B , is defined as follows: In the binary representation of A \operatorname{AND} B , the digit at the 2^k ( k \geq 0 ) place is 1 if and only if the digits at the 2^k place in the binary representations of both A and B are 1 ; otherwise, it is 0 . For example, 3 \operatorname{AND} 5 = 1 (in binary: 011 \operatorname{AND} 101 = 001 ). Generally, the bitwise \operatorname{AND} of k integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1 \operatorname{AND} p_2) \operatorname{AND} p_3) \operatorname{AND} \dots \operatorname{AND} p_k) , and it can be proved that this is independent of the order of p_1, p_2, p_3, \dots, p_k . What is \operatorname{popcount} ? For a non-negative integer x , \operatorname{popcount}(x) is the number of 1 s in the binary representation of x . More precisely, for a non-negative integer x such that \displaystyle x = \sum_{i=0}^{\infty} b_i 2^i\ (b_i \in {0, 1}) , it holds that \displaystyle \operatorname{popcount}(x) = \sum_{i=0}^{\infty} b_i . For example, 13 is 1101 in binary, so \operatorname{popcount}(13) = 3 .
[ { "input": "2 4\n11\n1 0\n2 0 1\n0\n1 1\n", "output": "9\n" }, { "input": "3 3\n101\n2 1 2\n2 0 1\n1 0\n", "output": "23\n" }, { "input": "16 7\n1101010000100110\n11 0 1 2 3 7 10 11 12 13 14 15\n7 4 6 8 10 11 12 13\n6 0 1 6 8 10 12\n8 0 3 5 6 10 11 12 13\n10 0 1 2 3 4 5 6 8 12 13\n9 3 4 5 6 8 9 11 14 15\n8 0 4 7 9 10 11 13 14\n", "output": "97754354\n" }, { "input": "92 4\n10101100101111111111011101111111101011001011111110011110111111101111111110100111100010111011\n23 1 2 5 13 14 20 28 32 34 39 52 56 59 60 62 64 67 69 71 78 84 87 91\n20 15 17 22 28 36 40 43 47 52 53 57 67 72 77 78 81 87 89 90 91\n23 7 8 9 10 11 13 16 19 22 23 30 33 42 49 51 52 58 64 71 73 76 79 83\n22 1 13 19 26 27 28 29 35 39 40 41 46 55 60 62 64 67 74 79 82 89 90\n", "output": "291412708\n" } ]
https://atcoder.jp/contests/abc354/tasks/abc354_a
Problem Statement Takahashi is growing a plant. Its height at the time of germination is 0\,\mathrm{cm} . Considering the day of germination as day 0 , its height increases by 2^i\,\mathrm{cm} day i 's night (0 \le i) . Takahashi's height is H\,\mathrm{cm} . Every morning, Takahashi measures his height against this plant. Find the first day such that the plant's height is strictly greater than Takahashi's height in the morning.
[ { "input": "54\n", "output": "6\n" }, { "input": "7\n", "output": "4\n" }, { "input": "262144\n", "output": "19\n" } ]
https://atcoder.jp/contests/abc354/tasks/abc354_b
Problem Statement N AtCoder users have gathered to play AtCoder RPS 2 . The i -th user's name is S_i and their rating is C_i . AtCoder RPS 2 is played as follows: Assign the numbers 0, 1, \dots, N - 1 to the users in lexicographical order of their usernames. Let T be the sum of the ratings of the N users. The user assigned the number T \bmod N is the winner. Print the winner's username. What is lexicographical order? Lexicographical order, simply put, means "the order in which words appear in a dictionary." More precisely, the algorithm to determine the order of two distinct strings S and T consisting of lowercase English letters is as follows: Here, "the i -th character of S " is denoted as S_i . If S is lexicographically smaller than T , we write S \lt T , and if S is larger, we write S \gt T . Let L be the length of the shorter string among S and T . Check if S_i and T_i match for i=1,2,\dots,L . If there exists an i such that S_i \neq T_i , let j be the smallest such i . Compare S_j and T_j . If S_j is alphabetically smaller than T_j , then S \lt T . Otherwise, S \gt T . The algorithm ends here. If there is no i such that S_i \neq T_i , compare the lengths of S and T . If S is shorter than T , then S \lt T . If S is longer, then S \gt T . The algorithm ends here.
[ { "input": "3\ntakahashi 2\naoki 6\nsnuke 5\n", "output": "snuke\n" }, { "input": "3\ntakahashi 2813\ntakahashixx 1086\ntakahashix 4229\n", "output": "takahashix\n" } ]
https://atcoder.jp/contests/abc354/tasks/abc354_c
Problem Statement Takahashi has N cards from the card game "AtCoder Magics." The i -th card will be called card i . Each card has two parameters: strength and cost. Card i has a strength of A_i and a cost of C_i . He does not like weak cards, so he will discard them. Specifically, he will repeat the following operation until it can no longer be performed: Choose two cards x and y such that A_x > A_y and C_x < C_y . Discard card y . It can be proved that the set of remaining cards when the operations can no longer be performed is uniquely determined. Find this set of cards.
[ { "input": "3\n2 4\n1 1\n3 2\n", "output": "2\n2 3\n" }, { "input": "5\n1 1\n10 2\n100 3\n1000 4\n10000 5\n", "output": "5\n1 2 3 4 5\n" }, { "input": "6\n32 101\n65 78\n2 29\n46 55\n103 130\n52 40\n", "output": "4\n2 3 5 6\n" } ]
https://atcoder.jp/contests/abc354/tasks/abc354_d
Problem Statement The pattern of AtCoder's wallpaper can be represented on the xy -plane as follows: The plane is divided by the following three types of lines: x = n (where n is an integer) y = n (where n is an even number) x + y = n (where n is an even number) Each region is painted black or white. Any two regions adjacent along one of these lines are painted in different colors. The region containing (0.5, 0.5) is painted black. The following figure shows a part of the pattern. You are given integers A, B, C, D . Consider a rectangle whose sides are parallel to the x - and y -axes, with its bottom-left vertex at (A, B) and its top-right vertex at (C, D) . Calculate the area of the regions painted black inside this rectangle, and print twice that area. It can be proved that the output value will be an integer.
[ { "input": "0 0 3 3\n", "output": "10\n" }, { "input": "-1 -2 1 3\n", "output": "11\n" }, { "input": "-1000000000 -1000000000 1000000000 1000000000\n", "output": "4000000000000000000\n" } ]
https://atcoder.jp/contests/abc354/tasks/abc354_e
Problem Statement Takahashi and Aoki are playing a game using N cards. The front side of the i -th card has A_i written on it, and the back side has B_i written on it. Initially, the N cards are laid out on the table. With Takahashi going first, the two players take turns performing the following operation: Choose a pair of cards from the table such that either the numbers on their front sides are the same or the numbers on their back sides are the same, and remove these two cards from the table. If no such pair of cards exists, the player cannot perform the operation. The player who is first to be unable to perform the operation loses, and the other player wins. Determine who wins if both players play optimally.
[ { "input": "5\n1 9\n2 5\n4 9\n1 4\n2 5\n", "output": "Aoki\n" }, { "input": "9\n3 2\n1 7\n4 1\n1 8\n5 2\n9 8\n2 1\n6 8\n5 2\n", "output": "Takahashi\n" } ]
https://atcoder.jp/contests/abc354/tasks/abc354_f
Problem Statement You are given an integer sequence A of length N . For each t = 1, 2, \dots, N , determine whether A_t is included in a longest increasing subsequence of A . Here, A_t is included in a longest increasing subsequence of A if and only if the following holds: Let L be the length of a longest increasing subsequence of A . There exists a strictly increasing integer sequence i = (i_1, i_2, \dots, i_L) \ (i_1 < i_2 < \dots < i_L) , where each element is between 1 and N , inclusive, that satisfies all of the following conditions: A_{i_1} < A_{i_2} < \dots < A_{i_L} . i_k = t for some k \ (1 \leq k \leq L) . You are given T test cases; solve each of them. What is a longest increasing subsequence? A subsequence of a sequence A is a sequence that can be derived by extracting some elements from A without changing the order. A longest increasing subsequence of a sequence A is a subsequence of A that is strictly increasing with the greatest possible length.
[ { "input": "1\n5\n2 1 4 5 3\n", "output": "4\n1 2 3 4\n" }, { "input": "2\n6\n2 5 3 4 3 4\n5\n10000 1000 100 1 10\n", "output": "5\n1 3 4 5 6\n2\n4 5\n" } ]
https://atcoder.jp/contests/abc354/tasks/abc354_g
Problem Statement You are given N strings S_1, S_2, \ldots, S_N consisting of lowercase English letters and N positive integers A_1, A_2, \ldots, A_N . A subset T of \lbrace 1, 2, \ldots, N \rbrace is called a good set if there is no pair i, j \in T (i \neq j) such that S_i is a substring of S_j . Find the maximum possible value of \displaystyle \sum_{i \in T} A_i for a good set T . What is a substring? A substring of a string S is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S . For example, ab is a substring of abc , but ac is not a substring of abc .
[ { "input": "4\natcoder\nat\ncoder\ncode\n5 2 3 4\n", "output": "6\n" }, { "input": "10\nabcd\nabc\nab\na\nb\nc\nd\nab\nbc\ncd\n100 10 50 30 60 90 80 70 40 20\n", "output": "260\n" } ]
https://atcoder.jp/contests/ahc033/tasks/ahc033_a
Problem Statement There is a container terminal with an N \times N grid. Let (0,0) be the coordinates of the top-left square and (i,j) be the coordinates of the square located i squares down and j squares to the right from there. N^2 containers, numbered from 0 to N^2-1 , will be brought into the terminal. Using N cranes, please manage to dispatch the containers in the specified order. There are Receiving Gates and Dispatch Gates on the left and right edges of the map, respectively: Receiving Gates: Each square on the left edge is a Receiving Gate, and from each Receiving Gate, N containers are brought in sequentially. It is known in advance which containers will be brought in. The j -th container brought in from the Receiving Gate at (i,0) has the number A_{i,j} . Dispatch Gates: Each square on the right edge is a Dispatch Gate, and containers placed at each Dispatch Gate are immediately dispatched externally. From the Dispatch Gate at (i,N-1) , containers numbered N \times i, N \times i+1, \ldots, N \times i+N-1 are to be dispatched in this order. Each square, including the Receiving Gates and Dispatch Gates, can hold at most one container. Squares other than the Dispatch Gates can be used as temporary storage to adjust the order of dispatch since containers placed in these squares will not be dispatched. There are two types of cranes: one large crane and N-1 small cranes. Initially, the large crane is placed in the square (0,0) , and each small crane is placed in the squares (1,0), (2,0), \ldots, (N-1,0) . Each crane can perform actions such as picking up a container, releasing a container, and moving to an adjacent square. Cranes can always move to squares that do not have containers. If not carrying a container, cranes can also move to squares with containers. However, whether a crane carrying a container can move to a square with another container depends on the type of crane. The large crane can lift containers high, so it can move to squares with other containers even while carrying a container. Small cranes, on the other hand, lift containers to a lower height and cannot move to squares with other containers while carrying a container. Each turn, for each crane, you can independently perform one of the following actions: P : Pick up the container at the current square. If the crane is already holding a container or there is no container at the current square, this results in WA . Q : Release the container being held and place it at the current square. If the crane is not holding a container or there is already a container at the current square, this results in WA . U , D , L , R : Move to the adjacent square in the up, down, left, or right direction. If a small crane is carrying a container, the destination square must not have a container. Cranes cannot move outside the N \times N grid. . : Do nothing and stay in the current square. B : Bomb the crane. If the crane is holding a container, it cannot bomb. The crane is removed from the grid and can no longer perform any actions except . . If you perform a prohibited action, it results in WA . Each crane cannot overlap or pass each other, regardless of whether they are holding containers. In other words, multiple cranes cannot occupy the same square, and two cranes cannot move in such a way that they swap their positions. Since the actions of each crane are performed simultaneously, it is possible to move the crane from square p to square q in the same turn as moving the crane from square q to square r ( r \neq p ) or bombing the crane at square q . However, if r = p , it results in an invalid move because it would be a passing movement. Each turn consists of the following three steps: For each Receiving Gate where containers are still to be brought in, if there is neither a container nor a crane holding a container at that square, the next container in sequence is placed in that square. Perform the actions of each crane. For each Dispatch Gate, if there is a container at that square, dispatch the container and remove it from the grid. Operations can be repeated for up to 10000 turns. Your task is to find a sequence of operations that dispatches the containers in the specified order as closely as possible within a short number of turns.
[ { "input": "5\n24 10 17 15 13\n14 11 2 1 5\n7 9 6 21 20\n8 4 19 3 16\n18 23 22 0 12\n", "output": "PRDDDDRRRQLLLUUPRRRUQ\nB\nPRQB\nPRRRRUUQB\nPRRRRQB\n" } ]
https://atcoder.jp/contests/arc177/tasks/arc177_a
Problem Statement In Japan, there are six types of coins in circulation: 1 yen, 5 yen, 10 yen, 50 yen, 100 yen, and 500 yen. Answer the following question regarding these coins. Mr. AtCoder's wallet contains A 1 -yen coins, B 5 -yen coins, C 10 -yen coins, D 50 -yen coins, E 100 -yen coins, and F 500 -yen coins. He is planning to shop at N stores in sequence. Specifically, at the i -th store (1 \leq i \leq N) , he plans to buy one item that costs X_i yen (including tax). Giving and receiving change takes time, so he wants to choose his coins so that he can pay the exact amount at each store. Determine if this is possible.
[ { "input": "0 0 6 3 4 1\n3\n700 250 160\n", "output": "Yes\n" }, { "input": "0 0 0 2 4 0\n3\n100 200 300\n", "output": "No\n" }, { "input": "0 0 0 0 8 8\n1\n250\n", "output": "No\n" }, { "input": "20 5 9 7 10 6\n5\n177 177 177 177 177\n", "output": "Yes\n" }, { "input": "17 5 9 7 10 6\n5\n177 177 177 177 177\n", "output": "No\n" } ]
https://atcoder.jp/contests/arc177/tasks/arc177_b
Problem Statement Mr. AtCoder has created a device consisting of N small light bulbs arranged in a row from left to right, and two switches A and B. Each light bulb can be in one of two states: 0 (OFF) and 1 (ON). Pressing each switch causes the following: Pressing switch A turns the leftmost light bulb in the 0 state into 1 . Pressing switch B turns the leftmost light bulb in the 1 state into 0 . If there is no applicable light bulb, you cannot press the switch. Initially, all light bulbs are in the 0 state. He wants the states of the light bulbs to be S_1, S_2, \dots, S_N from left to right. Determine the order and number of times the switches should be pressed to achieve this. It is not necessary to minimize the number of presses, but it should be at most 10^6 so that the operations can finish in a realistic time. It can be proved that a solution exists under the constraints of this problem.
[ { "input": "5\n01100\n", "output": "4\nAAAB\n" } ]
https://atcoder.jp/contests/arc177/tasks/arc177_c
Problem Statement There is a grid with N rows and N columns. Let (i, j) (1 \leq i \leq N, 1 \leq j \leq N) denote the cell at the i -th row from the top and the j -th column from the left. Each cell is initially painted red or blue, with cell (i, j) being red if c_{i,j}= R and blue if c_{i,j}= B . You want to change the colors of some cells to purple so that the following two conditions are simultaneously satisfied: Condition 1 : You can move from cell (1, 1) to cell (N, N) by only passing through cells that are red or purple. Condition 2 : You can move from cell (1, N) to cell (N, 1) by only passing through cells that are blue or purple. Here, " You can move " means that you can reach the destination from the starting point by repeatedly moving to a horizontally or vertically adjacent cell of the relevant colors. What is the minimum number of cells that must be changed to purple to satisfy these conditions?
[ { "input": "5\nRBRBB\nRBRRR\nRRRBR\nRBBRB\nBBRBR\n", "output": "3\n" }, { "input": "5\nRBBBB\nBBBBB\nBBBBB\nBBBBB\nBBBBR\n", "output": "7\n" }, { "input": "10\nRRBBBBBBBB\nBRRBBBBBBB\nBBRRBBBBBB\nBBBRRBBBBB\nBBBBRRBBBB\nBBBBBRRBBB\nBBBBBBRRBB\nBBBBBBBRRB\nBBBBBBBBRR\nBBBBBBBBBR\n", "output": "2\n" }, { "input": "17\nRBBRRBRRRRRBBBBBB\nBBRBRBRRBRRBRRBBR\nBRBRBBBRBBRBBRBBB\nRBRRBBBBBBRRBRRRR\nRRRRRBRBRRRBBRBBR\nRRRRRBRRBRBBRRRBB\nBBBRRRBRBRBBRRRBB\nBBRRRBRBBBRBRRRBR\nRRBBBBBBBBBBBRBRR\nRRRBRRBRBRBRBRBBB\nRRBRRRRBRBRRBRBBR\nRRRBBRBRBBBRBBRBR\nBBRBBRRBRRRBBRBBB\nBBBRBRRRRRRRBBRBB\nRRRRRBRBRBBRRBRRR\nBRRRRBBBRRRBRRBBB\nBBRRBBRRRBBBRBBBR\n", "output": "8\n" } ]
https://atcoder.jp/contests/arc177/tasks/arc177_d
Problem Statement AtCoder Street is a road represented by a straight line on flat ground. There are N utility poles of height H standing on this road. The poles are numbered 1, 2, \dots, N in chronological order. Pole i ( 1 \leq i \leq N ) is vertically positioned at coordinate X_i . The base of each pole is fixed to the ground. Assume that the poles are sufficiently thin. The street will experience N earthquakes. During the i -th earthquake (1 \leq i \leq N) , the following events occur: If pole i has not yet fallen, it falls to the left or the right on the number line, each with a probability of \frac{1}{2} . If a falling pole collides with another pole that has not yet fallen (including collision at the base of the pole), the latter pole will also fall in the same direction. This may trigger a chain reaction. The direction in which a pole falls during step 1 is independent of the direction in which other poles have fallen. The following figure is an example of how poles might fall during one earthquake: For earthquake preparedness, for each t = 1, 2, \dots, N , find the probability that all poles have fallen by exactly the t -th earthquake. Multiply it by 2^N and print the result modulo 998244353 . It can be proved that the values to be printed are integers.
[ { "input": "3 2\n0 3 1\n", "output": "4 2 2\n" }, { "input": "4 10\n10 55 20 45\n", "output": "0 4 4 8\n" }, { "input": "8 1\n5 0 6 3 8 1 7 2\n", "output": "0 64 32 48 24 28 28 32\n" }, { "input": "40 20\n695 793 11 502 114 861 559 4 212 609 894 435 429 94 91 258 161 45 33 605 673 634 629 163 283 826 409 84 507 548 31 248 588 340 357 168 926 949 322 912\n", "output": "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 41942627 41942627 360709869\n" } ]
https://atcoder.jp/contests/arc177/tasks/arc177_e
Problem Statement In the AtCoder World Tour Finals 2800, N contestants participated, and a total of five problems were presented. Each problem is assigned an integer score of at least 1 point, and the problems are numbered so that the scores are non-decreasing from problem 1 to problem 5 . There are no partial points. Similar to the usual AtCoder rules, ranking is done as follows. In this problem, we do not consider the situation where multiple contestants have the same total score and penalty. Ranking The contestant with the higher total score ranks higher. In case of a tie, the one with the smaller penalty ranks higher. Now, Aoki, a reporter covering the finals, noted the following information: The number of participants N . Which problems each contestant solved. A_{i,j}=1 means the i -th contestant (1 \leq i \leq N) correctly solved problem j , and A_{i,j}=0 means they did not. The rank of each contestant. The i -th contestant (1 \leq i \leq N) was ranked R_i -th. However, when he started writing the article, he realized he did not note the scores and penalties. Furthermore, he realized there might be inconsistencies in the information he noted. Now, solve the following problem. Assume that he correctly noted information 1 and 2. Let D_i be the actual rank of contestant i (1 \leq i \leq N) , and find the minimum possible total squared error (D_1 - R_1)^2 + (D_2 - R_2)^2 + \dots + (D_N - R_N)^2 . You have T test cases to process.
[ { "input": "6\n4\n0 1 1 0 0\n1 0 0 1 0\n1 1 0 1 0\n1 0 1 0 0\n1 2 3 4\n8\n0 1 0 0 0\n1 1 0 1 0\n0 1 1 0 1\n1 0 0 0 0\n1 1 0 1 0\n0 1 0 0 0\n0 0 0 1 0\n0 1 1 1 1\n7 4 2 8 3 6 5 1\n6\n1 1 0 0 0\n0 0 1 0 0\n1 1 1 0 0\n0 0 0 1 0\n1 1 1 1 0\n0 0 0 0 1\n1 2 3 4 5 6\n6\n1 1 0 0 0\n0 0 1 0 0\n1 1 1 0 0\n0 0 0 1 0\n1 1 1 1 0\n0 0 0 0 1\n6 5 4 3 2 1\n20\n0 0 0 0 1\n0 0 1 0 0\n1 1 0 0 1\n1 0 1 0 1\n0 0 0 1 1\n0 0 1 1 1\n1 1 1 1 0\n1 1 0 1 0\n0 0 1 1 0\n1 0 1 0 0\n0 1 0 0 1\n0 1 1 1 1\n1 1 1 1 1\n0 1 0 1 0\n1 0 0 0 1\n1 1 1 0 0\n0 1 1 1 0\n0 0 0 1 0\n1 1 1 0 1\n1 1 0 1 1\n7 18 3 5 19 11 13 2 4 10 14 15 17 6 16 9 8 12 1 20\n15\n0 0 1 1 0\n0 0 0 1 0\n0 0 0 0 1\n0 0 1 1 1\n1 1 0 0 1\n0 1 1 1 0\n1 1 1 1 1\n0 1 1 0 1\n1 1 0 1 0\n1 0 0 1 1\n1 0 1 0 0\n1 1 0 1 1\n0 1 0 1 0\n1 1 0 0 0\n0 1 0 0 1\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15\n", "output": "6\n0\n26\n0\n1054\n428\n" } ]
https://atcoder.jp/contests/arc177/tasks/arc177_f
Problem Statement The Republic of AtCoder consists of L+1 islands aligned east-west, numbered 0 to L from west to east. The islands are connected by air routes, where for each 1 \leq i \leq L , there is a bidirectional air route between islands i-1 and i , as shown in the figure below. Each air route is owned by company A or company J, and the route between islands i-1 and i is owned by company S_i . The nation has N residents, numbered 1 to N . Resident i is currently on island X_i . Each resident has a coupon of one of the companies. Specifically, resident i has a coupon of company C_i . With a coupon, a resident can take that company's routes for free any number of times , but taking a route of the other company costs 1 coin per trip. Now, there is a treasure chest on island 0 . The residents want to cooperate to carry it to the capital, which is on island L . Determine the minimum total number of coins required to achieve this goal. The residents can pass the treasure chest to each other, but not their coupons.
[ { "input": "4 3\nAAJJ\n3 A\n1 J\n1 J\n", "output": "2\n" }, { "input": "8 3\nJJAAJJAJ\n2 A\n6 A\n8 J\n", "output": "6\n" }, { "input": "8 6\nJJAAJJAJ\n2 A\n6 A\n8 J\n8 J\n8 J\n8 J\n", "output": "4\n" } ]
https://atcoder.jp/contests/abc353/tasks/abc353_a
Problem Statement There are N buildings aligned in a row. The i -th building from the left has a height of H_i . Determine if there is a building taller than the first one from the left. If such a building exists, find the position of the leftmost such building from the left.
[ { "input": "4\n3 2 5 2\n", "output": "3\n" }, { "input": "3\n4 3 2\n", "output": "-1\n" }, { "input": "7\n10 5 10 2 10 13 15\n", "output": "6\n" } ]
https://atcoder.jp/contests/abc353/tasks/abc353_b
Problem Statement The AtCoder amusement park has an attraction that can accommodate K people. Now, there are N groups lined up in the queue for this attraction. The i -th group from the front (1\leq i\leq N) consists of A_i people. For all i (1\leq i\leq N) , it holds that A_i \leq K . Takahashi, as a staff member of this attraction, will guide the groups in the queue according to the following procedure. Initially, no one has been guided to the attraction, and there are K empty seats. If there are no groups in the queue, start the attraction and end the guidance. Compare the number of empty seats in the attraction with the number of people in the group at the front of the queue, and do one of the following: If the number of empty seats is less than the number of people in the group at the front, start the attraction. Then, the number of empty seats becomes K again. Otherwise, guide the entire group at the front of the queue to the attraction. The front group is removed from the queue, and the number of empty seats decreases by the number of people in the group. Go back to step 1. Here, no additional groups will line up after the guidance has started. Under these conditions, it can be shown that this procedure will end in a finite number of steps. Determine how many times the attraction will be started throughout the guidance.
[ { "input": "7 6\n2 5 1 4 1 2 3\n", "output": "4\n" }, { "input": "7 10\n1 10 1 10 1 10 1\n", "output": "7\n" }, { "input": "15 100\n73 8 55 26 97 48 37 47 35 55 5 17 62 2 60\n", "output": "8\n" } ]
https://atcoder.jp/contests/abc353/tasks/abc353_c
Problem Statement For positive integers x and y , define f(x, y) as the remainder of (x + y) divided by 10^8 . You are given a sequence of positive integers A = (A_1, \ldots, A_N) of length N . Find the value of the following expression: \displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^N f(A_i,A_j) .
[ { "input": "3\n3 50000001 50000002\n", "output": "100000012\n" }, { "input": "5\n1 3 99999999 99999994 1000000\n", "output": "303999988\n" } ]
https://atcoder.jp/contests/abc353/tasks/abc353_d
Problem Statement For positive integers x and y , define f(x, y) as follows: Interpret the decimal representations of x and y as strings and concatenate them in this order to obtain a string z . The value of f(x, y) is the value of z when interpreted as a decimal integer. For example, f(3, 14) = 314 and f(100, 1) = 1001 . You are given a sequence of positive integers A = (A_1, \ldots, A_N) of length N . Find the value of the following expression modulo 998244353 : \displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^N f(A_i,A_j) .
[ { "input": "3\n3 14 15\n", "output": "2044\n" }, { "input": "5\n1001 5 1000000 1000000000 100000\n", "output": "625549048\n" } ]
https://atcoder.jp/contests/abc353/tasks/abc353_e
Problem Statement For strings x and y , define f(x, y) as follows: f(x, y) is the length of the longest common prefix of x and y . You are given N strings (S_1, \ldots, S_N) consisting of lowercase English letters. Find the value of the following expression: \displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^N f(S_i,S_j) .
[ { "input": "3\nab abc arc\n", "output": "4\n" }, { "input": "11\nab bb aaa bba baba babb aaaba aabbb a a b\n", "output": "32\n" } ]
https://atcoder.jp/contests/abc353/tasks/abc353_f
Problem Statement Tiles are laid out on a coordinate plane. There are two types of tiles: small tiles of size 1\times1 and large tiles of size K\times K , laid out according to the following rules: For each pair of integers (i,j) , the square \lbrace(x,y)\mid i\leq x\leq i+1\wedge j\leq y\leq j+1\rbrace is contained within either one small tile or one large tile. If \left\lfloor\dfrac iK\right\rfloor+\left\lfloor\dfrac jK\right\rfloor is even, it is contained within a small tile. Otherwise, it is contained within a large tile. Tiles include their boundaries, and no two different tiles have a positive area of intersection. For example, when K=3 , tiles are laid out as follows: Takahashi starts at the point (S_x+0.5,S_y+0.5) on the coordinate plane. He can repeat the following movement any number of times: Choose a direction (up, down, left, or right) and a positive integer n . Move n units in that direction. Each time he crosses from one tile to another, he must pay a toll of 1 . Determine the minimum toll Takahashi must pay to reach the point (T_x+0.5,T_y+0.5) .
[ { "input": "3\n7 2\n1 6\n", "output": "5\n" }, { "input": "1\n41 42\n13 56\n", "output": "42\n" }, { "input": "100\n100 99\n199 1\n", "output": "0\n" }, { "input": "96929423\n5105216413055191 10822465733465225\n1543712011036057 14412421458305526\n", "output": "79154049\n" } ]
https://atcoder.jp/contests/abc353/tasks/abc353_g
Problem Statement The Kingdom of AtCoder has N towns: towns 1 , 2 , \ldots , N . To move from town i to town j , you must pay a toll of C \times |i-j| yen. Takahashi, a merchant, is considering participating in zero or more of M upcoming markets. The i -th market (1 \leq i \leq M) is described by the pair of integers (T_i, P_i) , where the market is held in town T_i and he will earn P_i yen if he participates. For all 1 \leq i < M , the i -th market ends before the (i+1) -th market begins. The time it takes for him to move is negligible. He starts with 10^{10^{100}} yen and is initially in town 1 . Determine the maximum profit he can make by optimally choosing which markets to participate in and how to move. Formally, let 10^{10^{100}} + X be his final amount of money if he maximizes the amount of money he has after the M markets. Find X .
[ { "input": "6 3\n4\n5 30\n2 10\n4 25\n2 15\n", "output": "49\n" }, { "input": "6 1000000000\n4\n5 30\n2 10\n4 25\n2 15\n", "output": "0\n" }, { "input": "50 10\n15\n37 261\n28 404\n49 582\n19 573\n18 633\n3 332\n31 213\n30 377\n50 783\n17 798\n4 561\n41 871\n15 525\n16 444\n26 453\n", "output": "5000\n" }, { "input": "50 1000000000\n15\n30 60541209756\n48 49238708511\n1 73787345006\n24 47221018887\n9 20218773368\n34 40025202486\n14 28286410866\n24 82115648680\n37 62913240066\n14 92020110916\n24 20965327730\n32 67598565422\n39 79828753874\n40 52778306283\n40 67894622518\n", "output": "606214471001\n" } ]
https://atcoder.jp/contests/abc352/tasks/abc352_a
Problem Statement The AtCoder railway line has N stations, numbered 1, 2, \ldots, N . On this line, there are inbound trains that start at station 1 and stop at the stations 2, 3, \ldots, N in order, and outbound trains that start at station N and stop at the stations N - 1, N - 2, \ldots, 1 in order. Takahashi is about to travel from station X to station Y using only one of the inbound and outbound trains. Determine whether the train stops at station Z during this travel.
[ { "input": "7 6 1 3\n", "output": "Yes\n" }, { "input": "10 3 2 9\n", "output": "No\n" }, { "input": "100 23 67 45\n", "output": "Yes\n" } ]
https://atcoder.jp/contests/abc352/tasks/abc352_b
Problem Statement Takahashi tried to type a string S consisting of lowercase English letters using a keyboard. He was typing while looking only at the keyboard, not the screen. Whenever he mistakenly typed a different lowercase English letter, he immediately pressed the backspace key. However, the backspace key was broken, so the mistakenly typed letter was not deleted, and the actual string typed was T . He did not mistakenly press any keys other than those for lowercase English letters. The characters in T that were not mistakenly typed are called correctly typed characters . Determine the positions in T of the correctly typed characters.
[ { "input": "abc\naxbxyc\n", "output": "1 3 6\n" }, { "input": "aaaa\nbbbbaaaa\n", "output": "5 6 7 8\n" }, { "input": "atcoder\natcoder\n", "output": "1 2 3 4 5 6 7\n" } ]
https://atcoder.jp/contests/abc352/tasks/abc352_c
Problem Statement There are N giants, named 1 to N . When giant i stands on the ground, their shoulder height is A_i , and their head height is B_i . You can choose a permutation (P_1, P_2, \ldots, P_N) of (1, 2, \ldots, N) and stack the N giants according to the following rules: First, place giant P_1 on the ground. The giant P_1 's shoulder will be at a height of A_{P_1} from the ground, and their head will be at a height of B_{P_1} from the ground. For i = 1, 2, \ldots, N - 1 in order, place giant P_{i + 1} on the shoulders of giant P_i . If giant P_i 's shoulders are at a height of t from the ground, then giant P_{i + 1} 's shoulders will be at a height of t + A_{P_{i + 1}} from the ground, and their head will be at a height of t + B_{P_{i + 1}} from the ground. Find the maximum possible height of the head of the topmost giant P_N from the ground.
[ { "input": "3\n4 10\n5 8\n2 9\n", "output": "18\n" }, { "input": "5\n1 1\n1 1\n1 1\n1 1\n1 1\n", "output": "5\n" }, { "input": "10\n690830957 868532399\n741145463 930111470\n612846445 948344128\n540375785 925723427\n723092548 925021315\n928915367 973970164\n563314352 832796216\n562681294 868338948\n923012648 954764623\n691107436 891127278\n", "output": "7362669937\n" } ]
https://atcoder.jp/contests/abc352/tasks/abc352_d
Problem Statement You are given a permutation P = (P_1, P_2, \dots, P_N) of (1, 2, \dots, N) . A length- K sequence of indices (i_1, i_2, \dots, i_K) is called a good index sequence if it satisfies both of the following conditions: 1 \leq i_1 < i_2 < \dots < i_K \leq N . The subsequence (P_{i_1}, P_{i_2}, \dots, P_{i_K}) can be obtained by rearranging some consecutive K integers. Formally, there exists an integer a such that \lbrace P_{i_1},P_{i_2},\dots,P_{i_K} \rbrace = \lbrace a,a+1,\dots,a+K-1 \rbrace . Find the minimum value of i_K - i_1 among all good index sequences. It can be shown that at least one good index sequence exists under the constraints of this problem.
[ { "input": "4 2\n2 3 1 4\n", "output": "1\n" }, { "input": "4 1\n2 3 1 4\n", "output": "0\n" }, { "input": "10 5\n10 1 6 8 7 2 5 9 3 4\n", "output": "5\n" } ]
https://atcoder.jp/contests/abc352/tasks/abc352_e
Problem Statement You are given a weighted undirected graph G with N vertices, numbered 1 to N . Initially, G has no edges. You will perform M operations to add edges to G . The i -th operation (1 \leq i \leq M) is as follows: You are given a subset of vertices S_i=\lbrace A_{i,1},A_{i,2},\dots,A_{i,K_i}\rbrace consisting of K_i vertices. For every pair u, v such that u, v \in S_i and u < v , add an edge between vertices u and v with weight C_i . After performing all M operations, determine whether G is connected. If it is, find the total weight of the edges in a minimum spanning tree of G .
[ { "input": "4 3\n3 3\n1 2 3\n2 2\n1 2\n3 4\n1 3 4\n", "output": "9\n" }, { "input": "3 2\n2 1\n1 2\n2 1\n1 2\n", "output": "-1\n" }, { "input": "10 5\n6 158260522\n1 3 6 8 9 10\n10 877914575\n1 2 3 4 5 6 7 8 9 10\n4 602436426\n2 6 7 9\n6 24979445\n2 3 4 5 8 10\n4 861648772\n2 4 8 9\n", "output": "1202115217\n" } ]
https://atcoder.jp/contests/abc352/tasks/abc352_f
Problem Statement There are N people, numbered 1 to N . A competition was held among these N people, and they were ranked accordingly. The following information is given about their ranks: Each person has a unique rank. For each 1 \leq i \leq M , if person A_i is ranked x -th and person B_i is ranked y -th, then x - y = C_i . The given input guarantees that there is at least one possible ranking that does not contradict the given information. Answer N queries. The answer to the i -th query is an integer determined as follows: If the rank of person i can be uniquely determined, return that rank. Otherwise, return -1 .
[ { "input": "5 2\n2 3 3\n5 4 3\n", "output": "3 -1 -1 -1 -1\n" }, { "input": "3 0\n", "output": "-1 -1 -1\n" }, { "input": "8 5\n6 7 3\n8 1 7\n4 5 1\n7 2 1\n6 2 4\n", "output": "1 -1 -1 -1 -1 -1 -1 8\n" } ]
https://atcoder.jp/contests/abc352/tasks/abc352_g
Problem Statement Takahashi has various colors of socks in his chest of drawers. The colors of the socks are represented by integers from 1 to N , and there are A_i\ (\geq 2) socks of color i . He is about to choose which socks to wear today by performing the following operation: Continue to randomly draw one sock at a time from the chest, with equal probability, until he can make a pair of socks of the same color from those he has already drawn. Once a sock is drawn, it will not be returned to the chest. Find the expected value, modulo 998244353 , of the number of times he has to draw a sock from the chest. What does it mean to find the expected value modulo 998244353 ? It can be proved that the sought expected value is always rational. Furthermore, the constraints of this problem guarantee that if the expected value is expressed as an irreducible fraction \frac{y}{x} , then x is not divisible by 998244353 . Here, there exists a unique integer z between 0 and 998244352 , inclusive, such that xz \equiv y \pmod{998244353} . Find this z .
[ { "input": "2\n2 2\n", "output": "665496238\n" }, { "input": "1\n352\n", "output": "2\n" }, { "input": "6\n1796 905 2768 253 2713 1448\n", "output": "887165507\n" } ]
https://atcoder.jp/contests/abc351/tasks/abc351_a
Problem Statement Team Takahashi and Team Aoki are playing a baseball game, with Team Takahashi batting first. Currently, the game has finished through the top of the ninth inning, and the bottom of the ninth is about to begin. Team Takahashi scored A_i runs in the top of the i -th inning (1\leq i\leq 9) , and Team Aoki scored B_j runs in the bottom of the j -th inning (1\leq j\leq 8) . At the end of the top of the ninth, Team Takahashi's score is not less than Team Aoki's score. Determine the minimum number of runs Team Aoki needs to score in the bottom of the ninth to win the game. Here, if the game is tied at the end of the bottom of the ninth, it results in a draw. Therefore, for Team Aoki to win, they must score strictly more runs than Team Takahashi by the end of the bottom of the ninth. Team Takahashi's score at any point is the total runs scored in the tops of the innings up to that point, and Team Aoki's score is the total runs scored in the bottoms of the innings.
[ { "input": "0 1 0 1 2 2 0 0 1\n1 1 0 0 0 0 1 0\n", "output": "5\n" }, { "input": "0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0\n", "output": "1\n" } ]
https://atcoder.jp/contests/abc351/tasks/abc351_b
Problem Statement You are given two grids, each with N rows and N columns, referred to as grid A and grid B . Each cell in the grids contains a lowercase English letter. The character at the i -th row and j -th column of grid A is A_{i, j} . The character at the i -th row and j -th column of grid B is B_{i, j} . The two grids differ in exactly one cell. That is, there exists exactly one pair (i, j) of positive integers not greater than N such that A_{i, j} \neq B_{i, j} . Find this (i, j) .
[ { "input": "3\nabc\ndef\nghi\nabc\nbef\nghi\n", "output": "2 1\n" }, { "input": "1\nf\nq\n", "output": "1 1\n" }, { "input": "10\neixfumagit\nvtophbepfe\npxbfgsqcug\nugpugtsxzq\nbvfhxyehfk\nuqyfwtmglr\njaitenfqiq\nacwvufpfvv\njhaddglpva\naacxsyqvoj\neixfumagit\nvtophbepfe\npxbfgsqcug\nugpugtsxzq\nbvfhxyehok\nuqyfwtmglr\njaitenfqiq\nacwvufpfvv\njhaddglpva\naacxsyqvoj\n", "output": "5 9\n" } ]
https://atcoder.jp/contests/abc351/tasks/abc351_c
Problem Statement You have an empty sequence and N balls. The size of the i -th ball (1 \leq i \leq N) is 2^{A_i} . You will perform N operations. In the i -th operation, you add the i -th ball to the right end of the sequence, and repeat the following steps: If the sequence has one or fewer balls, end the operation. If the rightmost ball and the second rightmost ball in the sequence have different sizes, end the operation. If the rightmost ball and the second rightmost ball in the sequence have the same size, remove these two balls and add a new ball to the right end of the sequence with a size equal to the sum of the sizes of the two removed balls. Then, go back to step 1 and repeat the process. Determine the number of balls remaining in the sequence after the N operations.
[ { "input": "7\n2 1 1 3 5 3 3\n", "output": "3\n" }, { "input": "5\n0 0 0 1 2\n", "output": "4\n" } ]
https://atcoder.jp/contests/abc351/tasks/abc351_d
Problem Statement There is a grid of H rows and W columns. Some cells (possibly zero) contain magnets. The state of the grid is represented by H strings S_1, S_2, \ldots, S_H of length W . If the j -th character of S_i is # , it indicates that there is a magnet in the cell at the i -th row from the top and j -th column from the left; if it is . , it indicates that the cell is empty. Takahashi, wearing an iron armor, can move in the grid as follows: If any of the cells vertically or horizontally adjacent to the current cell contains a magnet, he cannot move at all. Otherwise, he can move to any one of the vertically or horizontally adjacent cells. However, he cannot exit the grid. For each cell without a magnet, define its degree of freedom as the number of cells he can reach by repeatedly moving from that cell. Find the maximum degree of freedom among all cells without magnets in the grid. Here, in the definition of degree of freedom, "cells he can reach by repeatedly moving" mean cells that can be reached from the initial cell by some sequence of moves (possibly zero moves). It is not necessary that there is a sequence of moves that visits all such reachable cells starting from the initial cell. Specifically, each cell itself (without a magnet) is always included in the cells reachable from that cell.
[ { "input": "3 5\n.#...\n.....\n.#..#\n", "output": "9\n" }, { "input": "3 3\n..#\n#..\n..#\n", "output": "1\n" } ]
https://atcoder.jp/contests/abc351/tasks/abc351_e
Problem Statement On a coordinate plane, there are N points P_1, P_2, \ldots, P_N , where point P_i has coordinates (X_i, Y_i) . The distance \text{dist}(A, B) between two points A and B is defined as follows: A rabbit is initially at point A . A rabbit at position (x, y) can jump to (x+1, y+1) , (x+1, y-1) , (x-1, y+1) , or (x-1, y-1) in one jump. \text{dist}(A, B) is defined as the minimum number of jumps required to get from point A to point B . If it is impossible to get from point A to point B after any number of jumps, let \text{dist}(A, B) = 0 . Calculate the sum \displaystyle\sum_{i=1}^{N-1}\displaystyle\sum_{j=i+1}^N \text{dist}(P_i, P_j) .
[ { "input": "3\n0 0\n1 3\n5 6\n", "output": "3\n" }, { "input": "5\n0 5\n1 7\n2 9\n3 8\n4 6\n", "output": "11\n" } ]
https://atcoder.jp/contests/abc351/tasks/abc351_f
Problem Statement You are given an integer sequence A = (A_1, A_2, \dots, A_N) . Calculate the following expression: \displaystyle \sum_{i=1}^N \sum_{j=i+1}^N \max(A_j - A_i, 0) The constraints guarantee that the answer is less than 2^{63} .
[ { "input": "3\n2 5 3\n", "output": "4\n" }, { "input": "10\n5 9 3 0 4 8 7 5 4 0\n", "output": "58\n" } ]
https://atcoder.jp/contests/abc351/tasks/abc351_g
Problem Statement You are given a rooted tree with N vertices numbered 1 to N . Vertex 1 is the root, and the parent of vertex i (2 \leq i \leq N) is vertex p_i (p_i < i) . Additionally, there is a sequence A = (A_1, A_2, \dots, A_N) . The hash value of the rooted tree is calculated as follows: Define f(n) (1 \leq n \leq N) as follows in the order n = N, N-1, \dots, 2, 1 . If vertex n is a leaf, f(n) = A_n . If vertex n is not a leaf, \displaystyle f(n) = A_n + \prod_{c \in C(n)} f(c) , where C(n) is the set of children of n . The hash value of the rooted tree is f(1) \bmod{998244353} . Process Q queries in the order they are given. Each query provides v and x , so update A_v to x and then compute the hash value of the rooted tree.
[ { "input": "3 2\n1 1\n3 5 1\n3 4\n2 1\n", "output": "23\n7\n" }, { "input": "5 4\n1 1 2 2\n2 5 4 4 1\n3 3\n5 0\n4 5\n5 2\n", "output": "29\n17\n17\n47\n" }, { "input": "10 10\n1 2 1 2 5 6 3 5 1\n766294629 440423913 59187619 725560240 585990756 965580535 623321125 550925213 122410708 549392044\n1 21524934\n9 529970099\n6 757265587\n8 219853537\n5 687675301\n5 844033519\n8 780395611\n2 285523485\n6 13801766\n3 487663184\n", "output": "876873846\n952166813\n626349486\n341294449\n466546009\n331098453\n469507939\n414882732\n86695436\n199797684\n" } ]
https://atcoder.jp/contests/arc176/tasks/arc176_a
Problem Statement There is an N \times N grid. Let (i, j) denote the cell at the i -th row from the top and the j -th column from the left. You are to fill each cell with 0 or 1 . Construct one method to fill the grid that satisfies all of the following conditions: The cells (A_1,B_1), (A_2,B_2), \dots, (A_M,B_M) contain 1 . The integers in the i -th row sum to M . (1 \le i \le N) The integers in the i -th column sum to M . (1 \le i \le N) It can be proved that under the constraints of this problem, there is at least one method to fill the grid that satisfies the conditions.
[ { "input": "4 2\n1 4\n3 2\n", "output": "8\n1 2\n1 4\n2 1\n2 4\n3 2\n3 3\n4 1\n4 3\n" }, { "input": "3 3\n3 1\n2 3\n1 3\n", "output": "9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3\n" }, { "input": "7 3\n1 7\n7 6\n6 1\n", "output": "21\n1 6\n2 4\n4 1\n7 3\n3 6\n4 5\n6 1\n1 7\n7 6\n3 5\n2 2\n6 3\n6 7\n5 4\n5 2\n2 5\n5 3\n1 4\n7 1\n4 7\n3 2\n" } ]
https://atcoder.jp/contests/arc176/tasks/arc176_b
Problem Statement Find the last digit of the remainder when 2^N is divided by 2^M - 2^K . You are given T test cases, each of which must be solved.
[ { "input": "5\n9 6 2\n123 84 50\n95 127 79\n1000000007 998244353 924844033\n473234053352300580 254411431220543632 62658522328486675\n", "output": "2\n8\n8\n8\n4\n" } ]
https://atcoder.jp/contests/arc176/tasks/arc176_c
Problem Statement Print the number, modulo 998244353 , of permutations P=(P_1,P_2,\dots,P_N) of (1,2,\dots,N) that satisfy all of the following conditions: \max(P_{A_i},P_{B_i}) = C_i\ (1 \le i \le M) .
[ { "input": "4 2\n1 2 4\n2 3 2\n", "output": "2\n" }, { "input": "6 3\n1 4 3\n2 5 6\n3 4 2\n", "output": "8\n" }, { "input": "20 17\n9 16 13\n5 14 20\n15 20 14\n5 13 17\n18 20 14\n14 20 20\n6 13 11\n12 16 19\n2 15 10\n6 17 11\n7 18 7\n8 18 12\n8 16 13\n6 16 13\n2 18 10\n9 10 15\n7 14 20\n", "output": "1209600\n" } ]
https://atcoder.jp/contests/arc176/tasks/arc176_d
Problem Statement You are given a permutation P=(P_1,P_2,\dots,P_N) of (1,2,\dots,N) . You will perform the following operation M times: Choose a pair of integers (i, j) such that 1 \le i < j \le N , and swap P_i and P_j . There are \left(\frac{N(N-1)}{2}\right)^M possible sequences of operations. For each of them, consider the value \sum_{i=1}^{N-1} |P_i - P_{i+1}| after all the operations. Find the sum, modulo 998244353 , of all those values.
[ { "input": "3 1\n1 3 2\n", "output": "8\n" }, { "input": "2 5\n2 1\n", "output": "1\n" }, { "input": "5 2\n3 5 1 4 2\n", "output": "833\n" }, { "input": "20 24\n14 1 20 6 11 3 19 2 7 10 9 18 13 12 17 8 15 5 4 16\n", "output": "203984325\n" } ]
https://atcoder.jp/contests/arc176/tasks/arc176_e
Problem Statement You are given two length- N sequences of positive integers: X=(X_1,X_2,\dots,X_N) and Y=(Y_1,Y_2,\dots,Y_N) . Additionally, you are given M length- N sequences of positive integers. The i -th sequence is A_i = (A_{i,1},A_{i,2},\dots,A_{i,N}) . For each i = 1,2,\dots,M , you must perform one of the following operations. You can independently choose which operation to perform for each i . Replace X_j with \max(X_j,A_{i,j}) for all integers j such that 1 \le j \le N . Replace Y_j with \max(Y_j,A_{i,j}) for all integers j such that 1 \le j \le N . Find the minimum possible value of \sum_{j=1}^{N} (X_j + Y_j) after all operations.
[ { "input": "3 2\n4 4 2\n3 1 5\n2 5 2\n1 2 4\n", "output": "21\n" }, { "input": "3 5\n4 13 10\n14 9 4\n4 6 4\n13 18 16\n8 13 5\n7 18 17\n20 20 14\n", "output": "84\n" }, { "input": "5 12\n330 68 248 387 491\n295 366 376 262 192\n280 121 17 168 455\n288 179 210 378 490\n150 275 165 264 287\n66 331 207 282 367\n303 215 456 214 18\n227 326 103 443 427\n395 57 107 350 227\n318 231 146 2 116\n57 325 124 383 260\n147 319 23 177 445\n254 198 32 85 56\n68 177 356 41 471\n", "output": "3595\n" } ]
https://atcoder.jp/contests/arc176/tasks/arc176_f
Problem Statement There is a tree with NM+1 vertices numbered 0 to NM . The i -th edge ( 1 \le i \le NM ) connects vertices i and \max(i-N,0) . Initially, vertex i is colored with color i \bmod N . You can perform the following operation zero or more times: Choose two vertices u and v connected by an edge. Repaint the color of vertex u with that of vertex v . Find the number, modulo 998244353 , of possible trees after the operations. Two trees are distinguished if and only if some vertex has different colors.
[ { "input": "3 1\n", "output": "42\n" }, { "input": "4 2\n", "output": "219100\n" }, { "input": "20 24\n", "output": "984288778\n" }, { "input": "123456 112233\n", "output": "764098676\n" } ]
https://atcoder.jp/contests/abc350/tasks/abc350_a
Problem Statement You are given a string S of length 6 . It is guaranteed that the first three characters of S are ABC and the last three characters are digits. Determine if S is the abbreviation of a contest held and concluded on AtCoder before the start of this contest. Here, a string T is "the abbreviation of a contest held and concluded on AtCoder before the start of this contest" if and only if it equals one of the following 348 strings: ABC001 , ABC002 , \ldots , ABC314 , ABC315 , ABC317 , ABC318 , \ldots , ABC348 , ABC349 . Note that ABC316 is not included.
[ { "input": "ABC349\n", "output": "Yes\n" }, { "input": "ABC350\n", "output": "No\n" }, { "input": "ABC316\n", "output": "No\n" } ]
https://atcoder.jp/contests/abc350/tasks/abc350_b
Problem Statement Takahashi has N teeth, one in each of the holes numbered 1, 2, \dots, N . Dentist Aoki will perform Q treatments on these teeth and holes. In the i -th treatment, hole T_i is treated as follows: If there is a tooth in hole T_i , remove the tooth from hole T_i . If there is no tooth in hole T_i (i.e., the hole is empty), grow a tooth in hole T_i . After all treatments are completed, how many teeth does Takahashi have?
[ { "input": "30 6\n2 9 18 27 18 9\n", "output": "28\n" }, { "input": "1 7\n1 1 1 1 1 1 1\n", "output": "0\n" }, { "input": "9 20\n9 5 1 2 2 2 8 9 2 1 6 2 6 5 8 7 8 5 9 8\n", "output": "5\n" } ]
https://atcoder.jp/contests/abc350/tasks/abc350_c
Problem Statement You are given a permutation A=(A_1,\ldots,A_N) of (1,2,\ldots,N) . Transform A into (1,2,\ldots,N) by performing the following operation between 0 and N-1 times, inclusive: Operation: Choose any pair of integers (i,j) such that 1\leq i < j \leq N . Swap the elements at the i -th and j -th positions of A . It can be proved that under the given constraints, it is always possible to transform A into (1,2,\ldots,N) .
[ { "input": "5\n3 4 1 2 5\n", "output": "2\n1 3\n2 4\n" }, { "input": "4\n1 2 3 4\n", "output": "0\n" }, { "input": "3\n3 1 2\n", "output": "2\n1 2\n2 3\n" } ]
https://atcoder.jp/contests/abc350/tasks/abc350_d
Problem Statement There is an SNS used by N users, labeled with numbers from 1 to N . In this SNS, two users can become friends with each other. Friendship is bidirectional; if user X is a friend of user Y, user Y is always a friend of user X. Currently, there are M pairs of friendships on the SNS, with the i -th pair consisting of users A_i and B_i . Determine the maximum number of times the following operation can be performed: Operation: Choose three users X, Y, and Z such that X and Y are friends, Y and Z are friends, but X and Z are not. Make X and Z friends.
[ { "input": "4 3\n1 2\n2 3\n1 4\n", "output": "3\n" }, { "input": "3 0\n", "output": "0\n" }, { "input": "10 8\n1 2\n2 3\n3 4\n4 5\n6 7\n7 8\n8 9\n9 10\n", "output": "12\n" } ]
https://atcoder.jp/contests/abc350/tasks/abc350_e
Problem Statement You are given an integer N . You can perform the following two types of operations: Pay X yen to replace N with \displaystyle\left\lfloor\frac{N}{A}\right\rfloor . Pay Y yen to roll a die (dice) that shows an integer between 1 and 6 , inclusive, with equal probability. Let b be the outcome of the die, and replace N with \displaystyle\left\lfloor\frac{N}{b}\right\rfloor . Here, \lfloor s \rfloor denotes the greatest integer less than or equal to s . For example, \lfloor 3 \rfloor=3 and \lfloor 2.5 \rfloor=2 . Determine the minimum expected cost paid before N becomes 0 when optimally choosing operations. The outcome of the die in each operation is independent of other rolls, and the choice of operation can be made after observing the results of the previous operations.
[ { "input": "3 2 10 20\n", "output": "20.000000000000000\n" }, { "input": "3 2 20 20\n", "output": "32.000000000000000\n" }, { "input": "314159265358979323 4 223606797 173205080\n", "output": "6418410657.7408381\n" } ]
https://atcoder.jp/contests/abc350/tasks/abc350_f
Problem Statement You are given a string S = S_1 S_2 S_3 \dots S_{|S|} consisting of uppercase and lowercase English letters, ( , and ) . The parentheses in the string S are properly matched. Repeat the following operation until no more operations can be performed: First, select one pair of integers (l, r) that satisfies all of the following conditions: l < r S_l = ( S_r = ) Each of the characters S_{l+1}, S_{l+2}, \dots, S_{r-1} is an uppercase or lowercase English letter. Let T = \overline{S_{r-1} S_{r-2} \dots S_{l+1}} . Here, \overline{x} denotes the string obtained by toggling the case of each character in x (uppercase to lowercase and vice versa). Then, delete the l -th through r -th characters of S and insert T at the position where the deletion occurred. Refer to the sample inputs and outputs for clarification. It can be proved that it is possible to remove all ( s and ) s from the string using the above operations, and the final string is independent of how and in what order the operations are performed. Determine the final string. What does it mean that the parentheses in S are properly matched? First, a correct parenthesis sequence is defined as follows: A correct parenthesis sequence is a string that satisfies one of the following conditions: It is an empty string. There exists a correct parenthesis sequence A , and the string is formed by concatenating ( , A , ) in this order. There exist non-empty correct parenthesis sequences A and B , and the string is formed by concatenating A and B in this order. The parentheses in S are properly matched if and only if the ( s and ) s extracted from S without changing the order form a correct parenthesis sequence.
[ { "input": "((A)y)x\n", "output": "YAx\n" }, { "input": "((XYZ)n(X(y)Z))\n", "output": "XYZNXYZ\n" }, { "input": "(((()))(()))(())\n", "output": "\n" }, { "input": "dF(qT(plC())NnnfR(GsdccC))PO()KjsiI((ysA)eWW)ve\n", "output": "dFGsdccCrFNNnplCtQPOKjsiIwwEysAve\n" } ]
https://atcoder.jp/contests/abc350/tasks/abc350_g
Problem Statement Beware the special input format and the smaller memory limit than usual. There is an undirected graph with vertices 1, 2, \dots, N , initially without edges. You need to process the following Q queries on this graph: 1 u v Type 1 : Add an edge between vertices u and v . Before adding the edge, u and v belong to different connected components (i.e., the graph always remains a forest). 2 u v Type 2 : If there is a vertex adjacent to both u and v , report its vertex number; otherwise, report 0 . Given that the graph always remains a forest, the answer to this query is uniquely determined. The queries are given in an encrypted form. The original query is defined by three integers A, B, C , and the encrypted query is given as three integers a, b, c . Let X_k be the answer to the k -th type- 2 query. Define X_k = 0 for k = 0 . Restore A, B, C from the given a, b, c as follows: Let l be the number of type- 2 queries given before this query (not counting the query itself). Then, use the following: A = 1 + (((a \times (1+X_l)) \mod 998244353) \mod 2) B = 1 + (((b \times (1+X_l)) \mod 998244353) \mod N) C = 1 + (((c \times (1+X_l)) \mod 998244353) \mod N)
[ { "input": "6 12\n143209915 123840720 97293110\n89822758 207184717 689046893\n67757230 270920641 26993265\n952858464 221010240 871605818\n730183361 147726243 445163345\n963432357 295317852 195433903\n120904472 106195318 615645575\n817920568 27584394 770578609\n38727673 250957656 506822697\n139174867 566158852 412971999\n205467538 606353836 855642999\n159292205 319166257 51234344\n", "output": "0\n2\n0\n2\n6\n0\n1\n" }, { "input": "2 1\n377373366 41280240 33617925\n", "output": "\n" } ]
https://atcoder.jp/contests/masters2024-final-open/tasks/masters2024_final_a
Problem Statement Takahashi wants to operate a flying drone on a two-dimensional plane and visit N destinations in arbitrary order. Let x -axis be east-west and y -axis be north-south. The drone's flight area is a 2\cdot 10^5\times 2\cdot 10^5 square surrounded by four walls with x=-10^5,x=+10^5,y=-10^5,y=+10^5 . Furthermore, in addition to these outer walls, there can exist other walls inside. The drone has a state with position (x,y) and velocity (v_x,v_y) . The position and velocity are always integer values. Starting from an initial state of position (s_x,s_y) and velocity (0,0) , you can perform either of the following two types of operations each turn. Acceleration: Specify an integer vector (a_x,a_y) to change the velocity of the drone from (v_x,v_y) to (v_x+a_x,v_y+a_y) . It must satisfy a_x^2+a_y^2\leq 500^2 . Measurement: Specify a non-zero integer vector (b_x,b_y) and measure the distance from the current drone position (x,y) to the nearest wall in the (b_x,b_y) direction with uncertainty. It must satisfy b_x^2+b_y^2\leq 10^{10} . Extend a ray from the point (x, y) in the direction towards (b_x, b_y) (the ray passing through (x+b_x,y+b_y) ) and calculate the Euclidean distance d = \sqrt{(x-x')^2 + (y-y')^2} to the point (x', y') where it first intersects a wall. The resulting value is \mathrm{round}(d\times \alpha) , where \alpha is a value sampled from a normal distribution with mean 1 and standard deviation \delta , given as an input parameter. Resample \alpha if \alpha \leq 0 . If the ray passes through an endpoint of a wall, it is considered to have hit the wall. However, if the ray is parallel to a wall, it does not count as a hit. After an operation, the wind causes the drone's velocity to change slightly from (v_x, v_y) . Based on the input parameter \epsilon , two samples are drawn from a normal distribution with mean 0 and standard deviation \epsilon . After rounding these sampled values, they are denoted as (f_x, f_y) . The new velocity of the drone then becomes (v_x + f_x, v_y + f_y) . At the end of each turn, the current position is updated to (x+v_x, y+v_y) based on the velocity (v_x, v_y) at that moment. However, if the line segment connecting (x, y) and (x+v_x, y+v_y) intersects with a wall (including cases where the line segment and the wall are parallel), it is considered a collision with the wall, and the position update is not performed; instead, the velocity is reset to (0, 0) . It is guaranteed that the drone is not in contact with any wall in its initial state, and in the event of a collision, the drone returns to its position before the collision, ensuring that the drone does not begin moving while in contact with a wall. If there is no collision with a wall, it is checked whether any destination has been reached. A destination (p_x, p_y) , which has not yet been visited, is considered reached if the distance between the line segment connecting (x, y) and (x+v_x, y+v_y) and the destination point is 1000 or less. This distance is defined as the minimum distance between any point q on segment S and point p . You can perform up to 5000 turns of operation, and the operation is completed immediately upon visiting all destinations.
[]
https://atcoder.jp/contests/masters2024-final/tasks/masters2024_final_a
Problem Statement Takahashi wants to operate a flying drone on a two-dimensional plane and visit N destinations in arbitrary order. Let x -axis be east-west and y -axis be north-south. The drone's flight area is a 2\cdot 10^5\times 2\cdot 10^5 square surrounded by four walls with x=-10^5,x=+10^5,y=-10^5,y=+10^5 . Furthermore, in addition to these outer walls, there can exist other walls inside. The drone has a state with position (x,y) and velocity (v_x,v_y) . The position and velocity are always integer values. Starting from an initial state of position (s_x,s_y) and velocity (0,0) , you can perform either of the following two types of operations each turn. Acceleration: Specify an integer vector (a_x,a_y) to change the velocity of the drone from (v_x,v_y) to (v_x+a_x,v_y+a_y) . It must satisfy a_x^2+a_y^2\leq 500^2 . Measurement: Specify a non-zero integer vector (b_x,b_y) and measure the distance from the current drone position (x,y) to the nearest wall in the (b_x,b_y) direction with uncertainty. It must satisfy b_x^2+b_y^2\leq 10^{10} . Extend a ray from the point (x, y) in the direction towards (b_x, b_y) (the ray passing through (x+b_x,y+b_y) ) and calculate the Euclidean distance d = \sqrt{(x-x')^2 + (y-y')^2} to the point (x', y') where it first intersects a wall. The resulting value is \mathrm{round}(d\times \alpha) , where \alpha is a value sampled from a normal distribution with mean 1 and standard deviation \delta , given as an input parameter. Resample \alpha if \alpha \leq 0 . If the ray passes through an endpoint of a wall, it is considered to have hit the wall. However, if the ray is parallel to a wall, it does not count as a hit. After an operation, the wind causes the drone's velocity to change slightly from (v_x, v_y) . Based on the input parameter \epsilon , two samples are drawn from a normal distribution with mean 0 and standard deviation \epsilon . After rounding these sampled values, they are denoted as (f_x, f_y) . The new velocity of the drone then becomes (v_x + f_x, v_y + f_y) . At the end of each turn, the current position is updated to (x+v_x, y+v_y) based on the velocity (v_x, v_y) at that moment. However, if the line segment connecting (x, y) and (x+v_x, y+v_y) intersects with a wall (including cases where the line segment and the wall are parallel), it is considered a collision with the wall, and the position update is not performed; instead, the velocity is reset to (0, 0) . It is guaranteed that the drone is not in contact with any wall in its initial state, and in the event of a collision, the drone returns to its position before the collision, ensuring that the drone does not begin moving while in contact with a wall. If there is no collision with a wall, it is checked whether any destination has been reached. A destination (p_x, p_y) , which has not yet been visited, is considered reached if the distance between the line segment connecting (x, y) and (x+v_x, y+v_y) and the destination point is 1000 or less. This distance is defined as the minimum distance between any point q on segment S and point p . You can perform up to 5000 turns of operation, and the operation is completed immediately upon visiting all destinations.
[]
https://atcoder.jp/contests/abc349/tasks/abc349_a
Problem Statement There are N people labeled 1 to N , who have played several one-on-one games without draws. Initially, each person started with 0 points. In each game, the winner's score increased by 1 and the loser's score decreased by 1 (scores can become negative). Determine the final score of person N if the final score of person i\ (1\leq i\leq N-1) is A_i . It can be shown that the final score of person N is uniquely determined regardless of the sequence of games.
[ { "input": "4\n1 -2 -1\n", "output": "2\n" }, { "input": "3\n0 0\n", "output": "0\n" }, { "input": "6\n10 20 30 40 50\n", "output": "-150\n" } ]
https://atcoder.jp/contests/abc349/tasks/abc349_b
Problem Statement A string S consisting of lowercase English letters is a good string if and only if it satisfies the following property for all integers i not less than 1 : There are exactly zero or exactly two different letters that appear exactly i times in S . Given a string S , determine if it is a good string.
[ { "input": "commencement\n", "output": "Yes\n" }, { "input": "banana\n", "output": "No\n" }, { "input": "ab\n", "output": "Yes\n" } ]
https://atcoder.jp/contests/abc349/tasks/abc349_c
Problem Statement A string T of length 3 consisting of uppercase English letters is an airport code for a string S of lowercase English letters if and only if T can be derived from S by one of the following methods: Take a subsequence of length 3 from S (not necessarily contiguous) and convert it to uppercase letters to form T . Take a subsequence of length 2 from S (not necessarily contiguous), convert it to uppercase letters, and append X to the end to form T . Given strings S and T , determine if T is an airport code for S .
[ { "input": "narita\nNRT\n", "output": "Yes\n" }, { "input": "losangeles\nLAX\n", "output": "Yes\n" }, { "input": "snuke\nRNG\n", "output": "No\n" } ]
https://atcoder.jp/contests/abc349/tasks/abc349_d
Problem Statement For non-negative integers l and r (l < r) , let S(l, r) denote the sequence (l, l+1, \ldots, r-2, r-1) formed by arranging integers from l through r-1 in order. Furthermore, a sequence is called a good sequence if and only if it can be represented as S(2^i j, 2^i (j+1)) using non-negative integers i and j . You are given non-negative integers L and R (L < R) . Divide the sequence S(L, R) into the fewest number of good sequences, and print that number of sequences and the division. More formally, find the minimum positive integer M for which there is a sequence of pairs of non-negative integers (l_1, r_1), (l_2, r_2), \ldots, (l_M, r_M) that satisfies the following, and print such (l_1, r_1), (l_2, r_2), \ldots, (l_M, r_M) . L = l_1 < r_1 = l_2 < r_2 = \cdots = l_M < r_M = R S(l_1, r_1), S(l_2, r_2), \ldots, S(l_M, r_M) are good sequences. It can be shown that there is only one division that minimizes M .
[ { "input": "3 19\n", "output": "5\n3 4\n4 8\n8 16\n16 18\n18 19\n" }, { "input": "0 1024\n", "output": "1\n0 1024\n" }, { "input": "3940649673945088 11549545024454656\n", "output": "8\n3940649673945088 3940649673949184\n3940649673949184 4503599627370496\n4503599627370496 9007199254740992\n9007199254740992 11258999068426240\n11258999068426240 11540474045136896\n11540474045136896 11549270138159104\n11549270138159104 11549545016066048\n11549545016066048 11549545024454656\n" } ]