url
stringlengths
49
92
description
stringlengths
22
4.78k
cases
listlengths
0
6
https://atcoder.jp/contests/ahc011/tasks/ahc011_a
Problem Statement There are N^2-1 tiles on an N \times N board. Let (i, j) denote the coordinates of row i (0\leq i \leq N-1) from the top and column j (0\leq j\leq N-1) from the left. Each tile contains a figure with lines from its center towards one or more of four directions: up, down, left, and right. We represent each tile using a bitmask with 1 for left, 2 for up, 4 for right, and 8 for down, as follows. Tile Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Hex 0 1 2 3 4 5 6 7 8 9 a b c d e f The number 0 represents an empty square, and there is exactly one empty square. With a single operation, you can slide one of the tiles adjacent to the empty square in the four directions to the location of the empty square. After the move, the square from which the tile was moved becomes an empty square. You can repeat the sliding operation at most T=2\times N^3 times. After finishing the operations, consider a graph with N^2-1 squares other than the empty square as vertices and the following edges. For each (i, j) (0\leq i\leq N-2, 0\leq j\leq N-1) , if (i,j) is a tile with a downward line and (i+1,j) is a tile with an upward line, then construct an edge between (i,j) and (i+1,j) . For each (i, j) (0\leq i\leq N-1, 0\leq j\leq N-2) , if (i,j) is a tile with a rightward line and (i,j+1) is a tile with a leftward line, then construct an edge between (i,j) and (i,j+1) . Your task is to find a short sequence of operations such that the size of the largest tree in this graph, i.e., the number of vertices of the largest connected component without cycles, is as large as possible. It is guaranteed that within T operations you can construct a tree of size N^2-1 with the empty square in (N-1,N-1) . Note that the final position of the empty square is arbitrary and you do not have to move it to (N-1,N-1) .
[ { "input": "6 432\n62ce43\na068f9\na89da9\n5d93cb\n276253\n424ba8\n", "output": "RRRDLUULDDDDLUUUR\n" } ]
https://atcoder.jp/contests/abc252/tasks/abc252_a
Problem Statement The ASCII values of the lowercase English letters a , b , \ldots , z are 97,98,\ldots,122 in this order. Given an integer N between 97 and 122 , print the letter whose ASCII value is N .
[ { "input": "97\n", "output": "a\n" }, { "input": "122\n", "output": "z\n" } ]
https://atcoder.jp/contests/abc252/tasks/abc252_b
Problem Statement Takahashi has N foods in his house. The i -th food has the tastiness of A_i . He dislikes K of these foods: for each i=1,2,\ldots,K , he dislikes the B_i -th food. Out of the foods with the greatest tastiness among the N foods, Takahashi will randomly choose one and eat it. If he has a chance to eat something he dislikes, print Yes ; otherwise, print No .
[ { "input": "5 3\n6 8 10 7 10\n2 3 4\n", "output": "Yes\n" }, { "input": "5 2\n100 100 100 1 1\n5 4\n", "output": "No\n" }, { "input": "2 1\n100 1\n2\n", "output": "No\n" } ]
https://atcoder.jp/contests/abc252/tasks/abc252_c
Problem Statement There is a slot machine with N reels. The placement of symbols on the i -th reel is represented by a string S_i of length 10 containing each of 0 , 1 , \ldots , 9 exactly once. Each reel has a corresponding button. For each non-negative integer t , Takahashi can press one of the buttons of his choice (or do nothing) t seconds after the reels start spinning. If the button for the i -th reel is pressed t seconds after the start of the spin, the i -th reel will stop to display the ((t\bmod{10})+1) -th character of S_i . Here, t\bmod{10} denotes the remainder when t is divided by 10 . Takahashi wants to stop all reels to make them display the same character. Find the minimum number of seconds needed to achieve his objective after the start of the spin.
[ { "input": "3\n1937458062\n8124690357\n2385760149\n", "output": "6\n" }, { "input": "5\n0123456789\n0123456789\n0123456789\n0123456789\n0123456789\n", "output": "40\n" } ]
https://atcoder.jp/contests/abc252/tasks/abc252_d
Problem Statement You are given a sequence of length N : A=(A_1,A_2,\ldots,A_N) . Find the number of triples (i,j,k) that satisfy both of the following conditions. 1\leq i \lt j \lt k \leq N A_i , A_j , and A_k are distinct.
[ { "input": "4\n3 1 4 1\n", "output": "2\n" }, { "input": "10\n99999 99998 99997 99996 99995 99994 99993 99992 99991 99990\n", "output": "120\n" }, { "input": "15\n3 1 4 1 5 9 2 6 5 3 5 8 9 7 9\n", "output": "355\n" } ]
https://atcoder.jp/contests/abc252/tasks/abc252_e
Problem Statement The Kingdom of AtCoder has N cities called City 1,2,\ldots,N and M roads called Road 1,2,\ldots,M . Road i connects Cities A_i and B_i bidirectionally and has a length of C_i . One can travel between any two cities using some roads. Under financial difficulties, the kingdom has decided to maintain only N-1 roads so that one can still travel between any two cities using those roads and abandon the rest. Let d_i be the total length of the roads one must use when going from City 1 to City i using only maintained roads. Print a choice of roads to maintain that minimizes d_2+d_3+\ldots+d_N .
[ { "input": "3 3\n1 2 1\n2 3 2\n1 3 10\n", "output": "1 2\n" }, { "input": "4 6\n1 2 1\n1 3 1\n1 4 1\n2 3 1\n2 4 1\n3 4 1\n", "output": "3 1 2\n" } ]
https://atcoder.jp/contests/abc252/tasks/abc252_f
Problem Statement We have a loaf of bread of length L , which we will cut and distribute to N children. The i -th child (1\leq i\leq N) wants a loaf of length A_i . Now, Takahashi will repeat the operation below to cut the loaf into lengths A_1, A_2, \ldots, A_N for the children. Choose a loaf of length k and an integer x between 1 and k-1 (inclusive). Cut the loaf into two loaves of length x and k-x . This operation incurs a cost of k regardless of the value of x . Each child i must receive a loaf of length exactly A_i , but it is allowed to leave some loaves undistributed. Find the minimum cost needed to distribute loaves to all children.
[ { "input": "5 7\n1 2 1 2 1\n", "output": "16\n" }, { "input": "3 1000000000000000\n1000000000 1000000000 1000000000\n", "output": "1000005000000000\n" } ]
https://atcoder.jp/contests/abc252/tasks/abc252_g
Problem Statement There is a rooted tree with N vertices called Vertex 1 , 2 , \ldots , N , rooted at Vertex 1 . We performed a depth-first search starting at the root and obtained a preorder traversal of the tree: P_1, P_2, \ldots, P_N . During the search, when the current vertex had multiple children, we chose the unvisited vertex with the smallest index. What is a preorder traversal? We start at the root and repeat the following procedure to list the vertices of the tree. If the current vertex u is not recorded yet, record it. Then, if u has an unvisited vertex, go to that vertex. Otherwise, terminate if u is the root, and go to the parent of u if it is not. The list of vertices in the order they are recorded here is the preorder traversal of the tree. Find the number of rooted trees consistent with the preorder traversal, modulo 998244353 . Two rooted trees (with N vertices and rooted at Vertex 1 ) are considered different when there is a non-root vertex whose parent is different in the two trees.
[ { "input": "4\n1 2 4 3\n", "output": "3\n" }, { "input": "8\n1 2 3 5 6 7 8 4\n", "output": "202\n" } ]
https://atcoder.jp/contests/abc252/tasks/abc252_h
Problem Statement We have N gemstones. The color and beauty of the i -th gemstone are D_i and V_i , respectively. Here, the color of each gemstone is one of 1, 2, \ldots, C , and there is at least one gemstone of each color. Out of the N gemstones, we will choose C with distinct colors and use them to make a necklace. (The order does not matter.) The beautifulness of the necklace will be the bitwise \rm XOR of the chosen gemstones. Among all possible ways to make a necklace, find the beautifulness of the necklace made in the way with the K -th greatest beautifulness. (If there are multiple ways with the same beautifulness, we count all of them.) What is bitwise \rm XOR ? The bitwise \rm XOR of integers A and B , A \oplus B , is defined as follows: When A \oplus B is written in base two, the digit in the 2^k 's place ( k \geq 0 ) is 1 if either A or B , but not both, has 1 in the 2^k 's place, and 0 otherwise. For example, 3 \oplus 5 = 6 . (In base two: 011 \oplus 101 = 110 .)
[ { "input": "4 2 3\n2 4\n2 6\n1 2\n1 3\n", "output": "5\n" }, { "input": "3 1 2\n1 0\n1 0\n1 0\n", "output": "0\n" }, { "input": "10 3 11\n1 414213562373095048\n1 732050807568877293\n2 236067977499789696\n2 449489742783178098\n2 645751311064590590\n2 828427124746190097\n3 162277660168379331\n3 316624790355399849\n3 464101615137754587\n3 605551275463989293\n", "output": "766842905529259824\n" } ]
https://atcoder.jp/contests/arc140/tasks/arc140_a
Problem Statement For a string T consisting of lowercase English letters, consider the question below, and let f(T) be the answer. Find the number of different strings obtained by performing the following operation any number of times: delete the first character from T and append it to the end. You are given a string S of length N consisting of lowercase English letters. You can perform the operation below at most K times (possibly zero). Choose a character of S and change it to any lowercase English letter. Find the minimum possible value of f(S) after your operations.
[ { "input": "4 1\nabac\n", "output": "2\n" }, { "input": "10 0\naaaaaaaaaa\n", "output": "1\n" }, { "input": "6 1\nabcaba\n", "output": "3\n" } ]
https://atcoder.jp/contests/arc140/tasks/arc140_b
Problem Statement You are given a string S of length N consisting of A , R , C . As long as S contains three consecutive characters that are ARC , you can perform the operation below. In an odd-numbered ( 1 -st, 3 -rd, 5 -th, ...) operation, choose in S three consecutive characters that are ARC , and replace them with R . In an even-numbered ( 2 -nd, 4 -th, 6 -th, ...) operation, choose in S three consecutive characters that are ARC , and replace them with AC . Find the maximum number of operations that can be performed.
[ { "input": "6\nAARCCC\n", "output": "2\n" }, { "input": "5\nAAAAA\n", "output": "0\n" }, { "input": "9\nARCARCARC\n", "output": "3\n" } ]
https://atcoder.jp/contests/arc140/tasks/arc140_c
Problem Statement The happiness of a permutation P=(P_1,P_2,\ldots,P_N) of (1,\dots,N) is defined as follows. Let A=(A_1,A_2,\ldots,A_{N-1}) be a sequence of length N-1 with A_i = |P_i-P_{i+1}|(1\leq i \leq N-1) . The happiness of P is the length of a longest strictly increasing subsequence of A . Print a permutation P such that P_1 = X with the greatest happiness.
[ { "input": "3 2\n", "output": "2 1 3\n" }, { "input": "3 1\n", "output": "1 2 3\n" } ]
https://atcoder.jp/contests/arc140/tasks/arc140_d
Problem Statement For an integer sequence X=(X_1,X_2,\dots,X_N) of length N whose elements are all between 1 and N (inclusive), consider the question below, and let f(X) be the answer. There is an undirected graph G with N vertices (and possibly multi-edges and self-loops). G has N edges, the i -th of which connects Vertex i and Vertex X_i . Find the number of connected components in G . You are given an integer sequence A=(A_1,A_2,\dots,A_N) of length N , where each A_i is an integer between 1 and N (inclusive) or -1 . Consider an integer sequence X=(X_1,X_2,\dots,X_N) of length N whose elements are all between 1 and N such that A_i \neq -1 \Rightarrow A_i = X_i . Find the sum of f(X) over all such X , modulo 998244353 .
[ { "input": "3\n-1 1 3\n", "output": "5\n" }, { "input": "1\n1\n", "output": "1\n" }, { "input": "8\n-1 3 -1 -1 8 -1 -1 -1\n", "output": "433760\n" } ]
https://atcoder.jp/contests/arc140/tasks/arc140_e
Problem Statement We have a grid with N \times M squares. You will fill every square with an integer between 1 and 25 (inclusive). Let a_{i,j} be the integer to be written in the square at the i -th row from the top and j -th column from the left. Find a way to fill the squares to satisfy the condition below. It can be proved that, under the Constraints of this problem, such a way always exists. For any integers 1\leq x_1 < x_2\leq N,1\leq y_1 < y_2 \leq M , it must not be the case that a_{x_1,y_1},a_{x_1,y_2},a_{x_2,y_1},a_{x_2,y_2} are all equal.
[ { "input": "2 3\n", "output": "1 1 1\n1 2 3\n" } ]
https://atcoder.jp/contests/arc140/tasks/arc140_f
Problem Statement Find the number of permutations P=(P_1,P_2,\dots,P_N) of (1,2,\dots,N) that satisfy the following, modulo 998244353 , for each K=0,1,2,\dots,N-1 . There are exactly K integers i such that 1 \le i \le N-1 and |P_i - P_{i+1}|=M .
[ { "input": "3 1\n", "output": "0 4 2 \n" }, { "input": "4 3\n", "output": "12 12 0 0 \n" }, { "input": "10 5\n", "output": "1263360 1401600 710400 211200 38400 3840 0 0 0 0 \n" } ]
https://atcoder.jp/contests/abc251/tasks/abc251_a
Problem Statement You are given a string S consisting of lowercase English characters. The length of S is between 1 and 3 , inclusive. Print the string of length 6 that is a repetition of S . It can be shown that there uniquely exists such a string under the Constraints of this problem.
[ { "input": "abc\n", "output": "abcabc\n" }, { "input": "zz\n", "output": "zzzzzz\n" } ]
https://atcoder.jp/contests/abc251/tasks/abc251_b
Problem Statement There are N weights called Weight 1 , Weight 2 , \dots , Weight N . Weight i has a mass of A_i . Let us say a positive integer n is a good integer if the following condition is satisfied: We can choose at most 3 different weights so that they have a total mass of n . How many positive integers less than or equal to W are good integers?
[ { "input": "2 10\n1 3\n", "output": "3\n" }, { "input": "2 1\n2 3\n", "output": "0\n" }, { "input": "4 12\n3 3 3 3\n", "output": "3\n" }, { "input": "7 251\n202 20 5 1 4 2 100\n", "output": "48\n" } ]
https://atcoder.jp/contests/abc251/tasks/abc251_c
Problem Statement Poem Online Judge (POJ) is an online judge that gives scores to submitted strings. There were N submissions to POJ. In the i -th earliest submission, string S_i was submitted, and a score of T_i was given. (The same string may have been submitted multiple times.) Note that POJ may not necessarily give the same score to submissions with the same string . A submission is said to be an original submission if the string in the submission is never submitted in any earlier submission. A submission is said to be the best submission if it is an original submission with the highest score. If there are multiple such submissions, only the earliest one is considered the best submission. Find the index of the best submission.
[ { "input": "3\naaa 10\nbbb 20\naaa 30\n", "output": "2\n" }, { "input": "5\naaa 9\nbbb 10\nccc 10\nddd 10\nbbb 11\n", "output": "2\n" }, { "input": "10\nbb 3\nba 1\naa 4\nbb 1\nba 5\naa 9\naa 2\nab 6\nbb 5\nab 3\n", "output": "8\n" } ]
https://atcoder.jp/contests/abc251/tasks/abc251_d
Problem Statement You are given an integer W . You are going to prepare some weights so that all of the conditions below are satisfied. The number of weights is between 1 and 300 , inclusive. Each weight has a mass of positive integer not exceeding 10^6 . Every integer between 1 and W , inclusive, is a good integer . Here, a positive integer n is said to be a good integer if the following condition is satisfied: We can choose at most 3 different weights from the prepared weights with a total mass of n . Print a combination of weights that satisfies the conditions.
[ { "input": "6\n", "output": "3\n1 2 3\n" }, { "input": "12\n", "output": "6\n2 5 1 2 5 1\n" } ]
https://atcoder.jp/contests/abc251/tasks/abc251_e
Problem Statement Takahashi is with N animals. The N animals are called Animal 1 , Animal 2 , \ldots , Animal N . Takahashi will perform the following N kinds of action. Each action can be performed any number of (possibly zero) times. Pay A_1 yen (the currency in Japan) to feed Animals 1 and 2 . Pay A_2 yen to feed Animals 2 and 3 . Pay A_3 yen to feed Animals 3 and 4 . \cdots Pay A_i yen to feed Animals i and (i+1) . \cdots Pay A_{N-2} yen to feed Animals (N-2) and (N-1) . Pay A_{N-1} yen to feed Animals (N-1) and N . Pay A_N yen to feed Animals N and 1 . Note that the N -th action above feeds "Animals N and 1 ." Print the minimum possible total cost to feed every animal at least once.
[ { "input": "5\n2 5 3 2 5\n", "output": "7\n" }, { "input": "20\n29 27 79 27 30 4 93 89 44 88 70 75 96 3 78 39 97 12 53 62\n", "output": "426\n" } ]
https://atcoder.jp/contests/abc251/tasks/abc251_f
Problem Statement You are given an undirected graph G with N vertices and M edges. G is simple (it has no self-loops and multiple edges) and connected . For i = 1, 2, \ldots, M , the i -th edge is an undirected edge \lbrace u_i, v_i \rbrace connecting Vertices u_i and v_i . Construct two spanning trees T_1 and T_2 of G that satisfy both of the two conditions below. ( T_1 and T_2 do not necessarily have to be different spanning trees.) T_1 satisfies the following. If we regard T_1 as a rooted tree rooted at Vertex 1 , for any edge \lbrace u, v \rbrace of G not contained in T_1 , one of u and v is an ancestor of the other in T_1 . T_2 satisfies the following. If we regard T_2 as a rooted tree rooted at Vertex 1 , there is no edge \lbrace u, v \rbrace of G not contained in T_2 such that one of u and v is an ancestor of the other in T_2 . We can show that there always exists T_1 and T_2 that satisfy the conditions above.
[ { "input": "6 8\n5 1\n4 3\n1 4\n3 5\n1 2\n2 6\n1 6\n4 2\n", "output": "1 4\n4 3\n5 3\n4 2\n6 2\n1 5\n5 3\n1 4\n2 1\n1 6\n" }, { "input": "4 3\n3 1\n1 2\n1 4\n", "output": "1 2\n1 3\n1 4\n1 4\n1 3\n1 2\n" } ]
https://atcoder.jp/contests/abc251/tasks/abc251_g
Problem Statement The vertices of a convex N -gon P in an xy -plane are given as (x_1, y_1), (x_2, y_2), \ldots, (x_N, y_N) in the counterclockwise order. (Here, the positive direction of the x -axis is right, and the positive direction of the y -axis is up.) Based on this polygon P , we consider M convex N -gons P_1, P_2, \ldots, P_M . For i = 1, 2, \ldots, M , the polygon P_i is obtained by shifting P in the positive direction of the x -axis by u_i and in the positive direction of the y -axis by v_i . In other words, P_i is a convex N -gon whose vertices are (x_1+u_i, y_1+v_i), (x_2+u_i, y_2+v_i), \ldots, (x_N+u_i, y_N+v_i) . For each of Q points (a_1, b_1), (a_2, b_2), \ldots, (a_Q, b_Q) , determine if "the point is contained in all of the M polygons P_1, P_2, \ldots, P_M ." Here, we regard a point is also contained in a polygon if the point is on the polygon's boundary.
[ { "input": "5\n-2 -3\n0 -2\n1 0\n0 2\n-2 1\n2\n0 1\n1 0\n6\n0 0\n1 0\n0 1\n1 1\n-1 -1\n-1 -2\n", "output": "Yes\nNo\nYes\nYes\nYes\nNo\n" }, { "input": "10\n45 100\n-60 98\n-95 62\n-95 28\n-78 -41\n-54 -92\n-8 -99\n87 -94\n98 23\n87 91\n5\n-57 -40\n-21 -67\n25 39\n-30 25\n39 -20\n16\n4 5\n-34 -8\n-63 53\n78 84\n19 -16\n64 9\n-13 7\n13 53\n-20 4\n2 -7\n3 18\n-12 10\n-69 -93\n2 9\n27 64\n-92 -100\n", "output": "Yes\nYes\nNo\nNo\nYes\nNo\nYes\nNo\nYes\nYes\nYes\nYes\nNo\nYes\nNo\nNo\n" } ]
https://atcoder.jp/contests/abc251/tasks/abc251_h
Problem Statement Blocks are stacked in a triangle. The i -th column from the top has i blocks. You are given a sequence P = ((a_1, c_1), (a_2, c_2), ..., (a_M, c_M)) which is a result of the run-length compression of a sequence A = (A_1, A_2, ..., A_N) consisting of non-negative integers less than or equal to 6 . For example, when A = (2, 2, 2, 5, 5, 1) , you are given P = ((2, 3), (5, 2), (1, 1)) . You will write a number on each block so that the following conditions are satisfied, where B_{i,j} denotes the number to write on the j -th block from the left in the i -th column from the top: For all integers i such that 1 \leq i \leq N , it holds that B_{N,i} = A_{i} . For all pairs of integers (i, j) such that 1 \leq j \leq i \leq N-1 , it holds that B_{i,j}= (B_{i+1,j}+B_{i+1,j+1})\bmod 7 . Enumerate the numbers written on the blocks in the K -th column from the top. What is run-length compression? The run-length compression is a conversion from a given sequence A to a sequence of pairs of integers obtained by the following procedure. Split A off at the positions where two different elements are adjacent to each other. For each subsequence B that has been split off, replace B with a integer pair of "the number which B consists of" and "the length of B ". Construct a sequence consisting of the integer pairs after the replacement without changing the order.
[ { "input": "6 3 4\n2 3\n5 2\n1 1\n", "output": "1 4 3 2\n" }, { "input": "1 1 1\n6 1\n", "output": "6\n" }, { "input": "111111111 9 9\n0 1\n1 10\n2 100\n3 1000\n4 10000\n5 100000\n6 1000000\n0 10000000\n1 100000000\n", "output": "1 0 4 2 5 5 5 6 3\n" } ]
https://atcoder.jp/contests/abc250/tasks/abc250_a
Problem Statement There is a grid with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i -th row from the top and the j -th column from the left. Find the number of squares that share a side with Square (R, C) . Here, two squares (a,b) and (c,d) are said to share a side if and only if |a-c|+|b-d|=1 (where |x| denotes the absolute value of x ).
[ { "input": "3 4\n2 2\n", "output": "4\n" }, { "input": "3 4\n1 3\n", "output": "3\n" }, { "input": "3 4\n3 4\n", "output": "2\n" }, { "input": "1 10\n1 5\n", "output": "2\n" }, { "input": "8 1\n8 1\n", "output": "1\n" }, { "input": "1 1\n1 1\n", "output": "0\n" } ]
https://atcoder.jp/contests/abc250/tasks/abc250_b
Problem Statement Tiles are aligned in N horizontal rows and N vertical columns. Each tile has a grid with A horizontal rows and B vertical columns. On the whole, the tiles form a grid X with (A\times N) horizontal rows and (B\times N) vertical columns. For 1\leq i,j \leq N , Tile (i,j) denotes the tile at the i -th row from the top and the j -th column from the left. Each square of X is painted as follows. Each tile is either a white tile or a black tile . Every square in a white tile is painted white; every square in a black tile is painted black. Tile (1,1) is a white tile. Two tiles sharing a side have different colors. Here, Tile (a,b) and Tile (c,d) are said to be sharing a side if and only if |a-c|+|b-d|=1 (where |x| denotes the absolute value of x ). Print the grid X in the format specified in the Output section.
[ { "input": "4 3 2\n", "output": "..##..##\n..##..##\n..##..##\n##..##..\n##..##..\n##..##..\n..##..##\n..##..##\n..##..##\n##..##..\n##..##..\n##..##..\n" }, { "input": "5 1 5\n", "output": ".....#####.....#####.....\n#####.....#####.....#####\n.....#####.....#####.....\n#####.....#####.....#####\n.....#####.....#####.....\n" }, { "input": "4 4 1\n", "output": ".#.#\n.#.#\n.#.#\n.#.#\n#.#.\n#.#.\n#.#.\n#.#.\n.#.#\n.#.#\n.#.#\n.#.#\n#.#.\n#.#.\n#.#.\n#.#.\n" }, { "input": "1 4 4\n", "output": "....\n....\n....\n....\n" } ]
https://atcoder.jp/contests/abc250/tasks/abc250_c
Problem Statement N balls are lined up in a row from left to right. Initially, the i -th ( 1 \leq i \leq N ) ball from the left has an integer i written on it. Takahashi has performed Q operations. The i -th ( 1 \leq i \leq Q ) operation was as follows. Swap the ball with the integer x_i written on it with the next ball to the right. If the ball with the integer x_i written on it was originally the rightmost ball, swap it with the next ball to the left instead. Let a_i be the integer written on the i -th ( 1 \leq i \leq N ) ball after the operations. Find a_1,\ldots,a_N .
[ { "input": "5 5\n1\n2\n3\n4\n5\n", "output": "1 2 3 5 4\n" }, { "input": "7 7\n7\n7\n7\n7\n7\n7\n7\n", "output": "1 2 3 4 5 7 6\n" }, { "input": "10 6\n1\n5\n2\n9\n6\n6\n", "output": "1 2 3 4 5 7 6 8 10 9\n" } ]
https://atcoder.jp/contests/abc250/tasks/abc250_d
Problem Statement Let us regard an integer k as "similar to 250 " if the following condition is satisfied: k is represented as k=p \times q^3 with primes p<q . How many integers less than or equal to N are "similar to 250 "?
[ { "input": "250\n", "output": "2\n" }, { "input": "1\n", "output": "0\n" }, { "input": "123456789012345\n", "output": "226863\n" } ]
https://atcoder.jp/contests/abc250/tasks/abc250_e
Problem Statement You are given integer sequences A = (a_1,\ldots,a_N) and B = (b_1,\ldots,b_N) , each of length N . For i=1,...,Q , answer the query in the following format. If the set of values contained in the first x_i terms of A , (a_1,\ldots,a_{x_i}) , and the set of values contained in the first y_i terms of B , (b_1,\ldots,b_{y_i}) , are equal, then print Yes ; otherwise, print No .
[ { "input": "5\n1 2 3 4 5\n1 2 2 4 3\n7\n1 1\n2 2\n2 3\n3 3\n4 4\n4 5\n5 5\n", "output": "Yes\nYes\nYes\nNo\nNo\nYes\nNo\n" } ]
https://atcoder.jp/contests/abc250/tasks/abc250_f
Problem Statement ABC 250 is a commemorable quarter milestone for Takahashi, who aims to hold ABC 1000, so he is going to celebrate this contest by eating as close to 1/4 of a pizza he bought as possible. The pizza that Takahashi bought has a planar shape of convex N -gon. When the pizza is placed on an xy -plane, the i -th vertex has coordinates (X_i, Y_i) . Takahashi has decided to cut and eat the pizza as follows. First, Takahashi chooses two non-adjacent vertices from the vertices of the pizza and makes a cut with a knife along the line passing through those two points, dividing the pizza into two pieces. Then, he chooses one of the pieces at his choice and eats it. Let a be the quarter ( =1/4 ) of the area of the pizza that Takahashi bought, and b be the area of the piece of pizza that Takahashi eats. Find the minimum possible value of 8 \times |a-b| . We can prove that this value is always an integer.
[ { "input": "5\n3 0\n2 3\n-1 3\n-3 1\n-1 -1\n", "output": "1\n" }, { "input": "4\n400000000 400000000\n-400000000 400000000\n-400000000 -400000000\n400000000 -400000000\n", "output": "1280000000000000000\n" }, { "input": "6\n-816 222\n-801 -757\n-165 -411\n733 131\n835 711\n-374 979\n", "output": "157889\n" } ]
https://atcoder.jp/contests/abc250/tasks/abc250_g
Problem Statement You are going to trade stocks of Company X for the next N days. As a precognitive, you know that the stock price on the i -th day of trading will be P_i yen (the currency in Japan) per unit. Every day, you can choose to do exactly one of the following. Buy one unit of stock for P_i yen. You will obtain one unit of stock and your money will decrease by P_i yen. Sell one unit of stock for P_i yen. You will lose one unit of stock and your money will increase by P_i yen. Do nothing. You initially have 10^{100} yen, so you will never be short of money. Find the maximum possible amount of money you will have gained when the N -th day has ended. Even if you still possess some amount of stocks of Company X when the N -th day has ended, it is considered that they are worth 0 yen.
[ { "input": "8\n2 5 4 3 7 1 8 6\n", "output": "16\n" }, { "input": "5\n10000 1000 100 10 1\n", "output": "0\n" }, { "input": "15\n300 1 4000 1 50000 900000000 20 600000 50000 300 50000 80000000 900000000 7000000 900000000\n", "output": "2787595378\n" } ]
https://atcoder.jp/contests/abc250/tasks/abc250_h
Problem Statement There are N points numbered 1 through N , and M roads. The i -th ( 1 \leq i \leq M ) road connects Point a_i and Point b_i bidirectionally and requires c_i minutes to pass through. One can travel from any point to any other point using some number of roads. There is a house on Points 1,\ldots, K . For i=1,\ldots,Q , solve the following problem. Takahashi is currently at the house at Point x_i and wants to travel to the house at Point y_i . Once t_i minutes have passed since his last sleep, he cannot continue moving anymore. He can get sleep only at a point with a house, but he may do so any number of times. If he can travel from Point x_i to Point y_i , print Yes ; otherwise, print No .
[ { "input": "6 6 3\n1 4 1\n4 6 4\n2 5 2\n3 5 3\n5 6 5\n1 2 15\n3\n2 3 4\n2 3 5\n1 3 12\n", "output": "No\nYes\nYes\n" } ]
https://atcoder.jp/contests/agc057/tasks/agc057_a
Problem Statement A set A of positive integers is said to be good when it satisfies the following condition. For any two distinct elements a, b \in A , the string representing a in base ten is not a substring of the string representing b in base ten. What is a substring? A substring of a string is its contiguous subsequence. For example, 1 , 12 , and 23 are substrings of 123 , while 21 and 13 are not. You are given positive integers L and R . Find the maximum possible number of elements in a good set A consisting of integers between L and R (inclusive). We will give you T test cases; solve each of them.
[ { "input": "3\n3 8\n3 18\n1 1000\n", "output": "6\n10\n900\n" } ]
https://atcoder.jp/contests/agc057/tasks/agc057_b
Problem Statement You are given a sequence A = (A_1, A_2, \ldots, A_N) of positive integers and a positive integer X . You can perform the operation below any number of times, possibly zero: Choose an index i ( 1\leq i\leq N ) and a non-negative integer x such that 0\leq x\leq X . Change A_i to 2A_i+x . Find the smallest possible value of \max\{A_1,A_2,\ldots,A_N\}-\min\{A_1,A_2,\ldots,A_N\} after your operations.
[ { "input": "4 2\n5 8 12 20\n", "output": "6\n" }, { "input": "4 5\n24 25 26 27\n", "output": "0\n" }, { "input": "4 1\n24 25 26 27\n", "output": "3\n" }, { "input": "10 5\n39 23 3 7 16 19 40 16 33 6\n", "output": "13\n" } ]
https://atcoder.jp/contests/agc057/tasks/agc057_c
Problem Statement You are given a positive integer N and a sequence A = (A_0, A_1, \ldots, A_{2^N-1}) of 2^N terms, where each A_i is an integer between 0 and 2^N-1 (inclusive) and A_i\neq A_j holds if i\neq j . You can perform the following two kinds of operations on A : Operation + : For every i , change A_i to (A_i + 1) \bmod 2^N . Operation \oplus : Choose an integer x between 0 and 2^N-1 . For every i , change A_i to A_i\oplus x . Here, \oplus denotes bitwise \mathrm{XOR} . What is bitwise \mathrm{XOR} ? The bitwise \mathrm{XOR} of non-negative integers A and B , A \oplus B , is defined as follows: When A \oplus B is written in base two, the digit in the 2^k 's place ( k \geq 0 ) is 1 if exactly one of A and B is 1 , and 0 otherwise. For example, we have 3 \oplus 5 = 6 (in base two: 011 \oplus 101 = 110 ). Your objective is to make A_i = i for every i . Determine whether it is achievable. It can be proved that, under the Constraints of this problem, one can achieve the objective with at most 10^6 operations if it is achievable at all. Print such a sequence of operations.
[ { "input": "3\n5 0 3 6 1 4 7 2\n", "output": "Yes\n4\n-1 6 -1 1\n" }, { "input": "3\n2 5 4 3 6 1 0 7\n", "output": "No\n" }, { "input": "3\n0 1 2 3 4 5 6 7\n", "output": "Yes\n0\n\n" } ]
https://atcoder.jp/contests/agc057/tasks/agc057_d
Problem Statement You are given positive integers S and K . A sequence of positive integers A = (A_1, A_2, \ldots, A_N) is said to be a good sequence when satisfying the following two conditions. 1\leq A_1 < A_2 < \cdots < A_N \leq S - 1 holds. \sum_{i=1}^NA_ix_i\neq S holds for any sequence of non-negative integers (x_1, x_2, \ldots, x_N) . Let A = (A_1, A_2, \ldots, A_N) be the lexicographically smallest of the good sequences with the maximum number N of terms. Print the K -th term A_K of this sequence, or -1 if K > N . We will give you T test cases; solve each of them.
[ { "input": "13\n3 1\n3 2\n7 1\n7 2\n7 3\n7 4\n10 1\n10 2\n10 3\n10 4\n10 5\n2022 507\n1000000000000000000 999999999999999999\n", "output": "2\n-1\n2\n4\n6\n-1\n3\n6\n8\n9\n-1\n1351\n-1\n" } ]
https://atcoder.jp/contests/agc057/tasks/agc057_e
Problem Statement Consider the following operations on an H\times W matrix A = (A_{i,j}) ( 1\leq i\leq H, 1\leq j\leq W ). Row-sort : Sort every row in ascending order. That is, sort A_{i,1},\ldots,A_{i,W} in ascending order for every i . Column-sort : Sort every column in ascending order. That is, sort A_{1,j},\ldots,A_{H,j} in ascending order for every j . You are given an H\times W matrix B = (B_{i,j}) . Find the number of H\times W matrices A that satisfy both of the following conditions, modulo 998244353 . Performing row-sort and then column-sort on A produces B . Performing column-sort and then row-sort on A produces B .
[ { "input": "2 2\n0 0\n1 2\n", "output": "4\n" }, { "input": "3 3\n0 1 3\n2 4 7\n5 6 8\n", "output": "576\n" }, { "input": "3 5\n0 0 0 1 1\n0 0 1 1 2\n0 1 1 2 2\n", "output": "10440\n" }, { "input": "1 7\n2 3 3 6 8 8 9\n", "output": "1260\n" } ]
https://atcoder.jp/contests/agc057/tasks/agc057_f
Problem Statement There are three indistinguishable stones at integer coordinates on a number line. Consider the following operation on these stones: Let A, B, C be the three stones in ascending order of coordinate (ties broken arbitrarily). Perform one of the following. Move A to the symmetric position with respect to B . Move C to the symmetric position with respect to B . You are given the initial coordinates a, b, c of the three stones. Find the number, modulo 998244353 , of possible combinations of coordinates of the three stones after zero or more operations. Note that the stones are indistinguishable. More strictly speaking, count the possible multisets of coordinates of the three stones. We will give you T test cases; solve each of them.
[ { "input": "6\n1 3 5\n-2 -2 5\n0 1 3\n31 41 59\n-123456789 0 987654321\n-1000000000000000000 0 1000000000000000000\n", "output": "5\n2\n9\n70\n182333351\n5\n" } ]
https://atcoder.jp/contests/nadafes2022_day2/tasks/nadafes2022_day2_a
問題文 長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N) のうち、 B_i=(A_i+K)\bmod M と定義される数列 B が A の並び替えになっているようなものは存在しますか?
[ { "input": "15\n6 9\n", "output": "Yes\n" }, { "input": "2\n5 7\n", "output": "No\n" } ]
https://atcoder.jp/contests/nadafes2022_day2/tasks/nadafes2022_day2_b
問題文 長さ N の整数列 A,B が与えられます。 1 \le l \le r \le N を満たす整数の組 (l,r) に対する (\max_{l \le i \le r} A_i) \times (\min_{l \le i \le r} B_i) の最小値を求めてください。
[ { "input": "3\n3 4 8\n4 2 5\n", "output": "8\n" }, { "input": "3\n-3 -5 8\n4 2 -5\n", "output": "-40\n" } ]
https://atcoder.jp/contests/nadafes2022_day2/tasks/nadafes2022_day2_c
問題文 kentand君は爆弾処理をします。 H×W のマス目があり、上から i \lparen 1 \le i \le H \rparen 行目、左から j \lparen 1 \le j \le W \rparen 列目のマス \lparen i,j\rparen は G_{i,j} が # のとき壁で、 . のとき道です。 マス目上の道に爆弾が N 個あり、 k \lparen 1 \le k \le N \rparen 個目の爆弾はマス \lparen C_k, D_k \rparen にあります。 kentand君は時刻 0 にマス \lparen 1,1 \rparen におり、 1 単位時間ごとに、今いるマスに上下左右に隣接する道のマスに移動します。爆弾のあるマスに移動することも可能です。同じマスにとどまることはできません。 そして、 k 個目の爆弾があるマスに S_k,S_k+1,\ldots,T_k のいずれかの時刻にいることでその爆弾を解除できます。解除にかかる時間は無視できます。 適切に行動したとき最大で何個の爆弾を解除できますか?
[ { "input": "3 3 2\n1 3 2 5\n3 2 3 4\n.#.\n...\n#.#\n", "output": "1\n" }, { "input": "1 2 1\n1 2 1000000000 1000000000\n..\n", "output": "0\n" } ]
https://atcoder.jp/contests/nadafes2022_day2/tasks/nadafes2022_day2_d
問題文 この問題はインタラクティブな問題です。 KoD 君は N 頂点の単純無向グラフを持っており、頂点には 1 から N の番号が付けられています。このグラフは連結とは限りません。 あなたの目標は、KoD 君が持っているグラフに閉路が存在するかどうか判定することです。そのために、以下の質問を行うことができます。 頂点集合を指定し、両端がその集合に属するような辺が何本あるかを聞く。 4500 回以下の質問回数で判定してください。
[]
https://atcoder.jp/contests/nadafes2022_day2/tasks/nadafes2022_day2_e
問題文 N 頂点の完全グラフがあります。頂点 i と頂点 j を結ぶ辺の長さは \min(|A_i-A_j|,|B_i-B_j|) です。 頂点 1 を出発し、頂点 2 から N をそれぞれ 1 回ずつ通り、頂点 1 に帰ってくるサイクル全てについて、サイクルの長さの総和を 998244353 で割った余りを求めてください。
[ { "input": "3\n1 2\n3 5\n4 2\n", "output": "6\n" } ]
https://atcoder.jp/contests/nadafes2022_day2/tasks/nadafes2022_day2_f
問題文 N 個の給水機と 1 つの空のコップがあります。PCT 君はこのコップにできるだけ多くの水を入れたいです。 給水機 i\,(1 \leq i \leq N) は時刻 S_i から時刻 T_i のみ使えます。また、 1 秒あたりの給水量は A_i リットルで、蓄えている水の量は A_i\times B_i リットルです。つまり、合計 B_i 秒よりも長く給水することはできません。 適切に行動すると最大で何リットルの水をコップに入れることができますか?ただし、同時に 2 つ以上の給水機からコップに水を入れることはできません。
[ { "input": "3\n1 6 1 2\n2 4 3 1\n2 4 2 1\n", "output": "7\n" }, { "input": "1\n1 100 100 3\n", "output": "300\n" }, { "input": "4\n1 3 4 5\n1 4 3 4\n3 4 5 2\n4 6 2 2\n", "output": "17\n" } ]
https://atcoder.jp/contests/nadafes2022_day2/tasks/nadafes2022_day2_g
問題文 長さ N の正整数列 A = (A_1, \dots, A_N), B = (B_1, \dots, B_N) が与えられます。 k = 1, \dots, M について、以下の条件を全て満たす正整数列 C = (C_1, \dots, C_N) の総数を 998244353 で割った余りを求めてください。 A_i \leq C_i \leq B_i \, (1 \leq i \leq N) \mathrm{gcd} (C_1, \dots, C_N) = k
[ { "input": "3 10\n1 9\n4 8\n7 10\n", "output": "151\n20\n3\n3\n1\n0\n1\n1\n0\n0\n" }, { "input": "1 1\n2 100000\n", "output": "0\n" } ]
https://atcoder.jp/contests/nadafes2022_day2/tasks/nadafes2022_day2_h
問題文 N 頂点 M 辺の単純連結無向グラフが与えられます。 i \, (1 \leq i \leq M) 本目の辺は頂点 A_i, B_i を結びます。 1 \leq s \lt t \leq N を満たす整数の組 (s, t) に対し、以下の値を f(s, t) とおきます。 頂点 s を出発し、頂点 t に到達した後頂点 s に戻ってくるような経路における、 s 以外の頂点のうち 2 回以上通るものの個数の最小値 \displaystyle \sum_{s = 1}^{N - 1} \sum_{t = s + 1}^N f(s, t) を求めてください。
[ { "input": "4 4\n1 2\n2 3\n1 3\n3 4\n", "output": "2\n" }, { "input": "2 1\n1 2\n", "output": "0\n" } ]
https://atcoder.jp/contests/nadafes2022_day2/tasks/nadafes2022_day2_i
問題文 長さ N の正整数列 A = (A_1, \dots, A_N) と N の正の約数 K が与えられます。 (1, 2,\dots, N) を並べ替えて得られる数列 P に対し、以下のように P の スコア を定めます。 i = 1, \dots, N に対し、 B_i = A_{P_i} と定める。 i = 1, \dots, \frac{N}{K} に対し、 G_i = \gcd(B_{(i - 1)K + 1}, B_{(i - 1)K + 2} \dots, B_{iK}) と定める。 \mathrm{lcm}(G_1, \dots, G_{\frac{N}{K}}) が P のスコアである。 P としてあり得るものは N! 通りありますが、それら全てに対する P のスコアの 総積 を \bold{10^9 + 7} で割った余り を求めてください。
[ { "input": "4 2\n1 2 3 4\n", "output": "256\n" }, { "input": "9 3\n12 6 8 9 10 16 4 18 15\n", "output": "822906664\n" } ]
https://atcoder.jp/contests/nadafes2022_day2/tasks/nadafes2022_day2_j
問題文 11 個の整数 c_0, \dots, c_{10} が与えられます。 f(x) = \sum_{n = 0}^{10} c_n x^n と定めます。 以下の条件を全て満たす整数列 A, B が存在するか判定し、存在するならばその一例を示してください。 A, B の長さは 1 以上 5000 以下 |A_i| \leq 5000 \, (1 \leq i \leq |A|) |B_i| \leq 5000 \, (1 \leq i \leq |B|) A_i \neq A_j \, (1 \leq i \lt j \leq |A|) B_i \neq B_j \, (1 \leq i \lt j \leq |B|) A_i \neq B_j \, (1 \leq i \leq |A|, 1 \leq j \leq |B|) \sum_{i = 1}^{|A|} f(A_i) = \sum_{j = 1}^{|B|} f(B_j)
[ { "input": "5 -1 2 0 0 0 0 0 0 0 0\n", "output": "2 0 1\n1 2\n" } ]
https://atcoder.jp/contests/nadafes2022_day2/tasks/nadafes2022_day2_k
問題文 NPCA 美術館では、東西方向にまっすぐに伸びる廊下に N 枚の絵が飾られています。絵 i (1\leq i\leq N) は廊下の西端から X_i メートルの位置に飾られており、その価値は V_i です。 館長の PCT 君は以下の条件を満たすように N-M 枚の絵を取り外し、 M 枚の絵だけを残すことにしました。 どの 2 つの絵についても、位置が D メートル以上離れている。 残った絵の価値の総和の最大値を求めてください。
[ { "input": "4 2 3\n0 2 4 5\n5 1 -4 3\n", "output": "8\n" }, { "input": "4 3 2\n0 1 2 3\n1 1 1 1\n", "output": "impossible\n" }, { "input": "5 2 4\n0 3 5 6 9\n-4 -8 3 -10 -6\n", "output": "-1\n" } ]
https://atcoder.jp/contests/nadafes2022_day2/tasks/nadafes2022_day2_l
問題文 長さ N の整数列 A=(A_1,A_2,\ldots,A_N),B=(B_1,B_2,\ldots,B_N) が与えられます。 区間 [l,r] \,(1 \le l \le r \le N) のスコアを以下で定義します。 |\min(A_l,A_{l+1},\ldots,A_r)-\min(B_l,B_{l+1},\ldots,B_r)| k=0,1,\ldots,N-1 について、 r-l=k を満たす [l,r] を選んだときのスコアとしてあり得る最小値を求めてください。
[ { "input": "3\n1 3 4\n8 5 7\n", "output": "2\n2\n4\n" }, { "input": "1\n1\n1\n", "output": "0\n" }, { "input": "6\n3 1 4 1 5 9\n2 6 5 3 5 8\n", "output": "0\n0\n1\n1\n1\n1\n" } ]
https://atcoder.jp/contests/nadafes2022_day2/tasks/nadafes2022_day2_m
問題文 以下のいずれかの条件を満たす文字列を 正しい括弧列 と定義します。 空文字列 ある正しい括弧列 A が存在して、 ( , A , ) をこの順に連結した文字列 ある空でない正しい括弧列 A , B が存在して、 A , B をこの順に連結した文字列 そして、正しい括弧列から末尾の文字を取り除くことを何回か繰り返すことで得られる文字列を 美しい括弧列 と定義します。 例えば、 (((() や ()() 、空文字列などは美しい括弧列であり、 ))() 、 ()) などは美しい括弧列ではありません。 文字列 S と整数 L が与えられます。 k=1,2,\ldots ,|S|-L+1 について独立に、以下の問いに答えてください。 S の隣り合う 2 文字を入れ替えることを繰り返すことで、 S の k 文字目から k+L-1 文字目までを取り出した部分文字列が美しい括弧列となるようにしたい。操作回数の最小値を求めよ。
[ { "input": "))((\n2\n", "output": "2\n1\n0\n" }, { "input": "))())\n4\n", "output": "-1\n-1\n" }, { "input": "))()(()\n3\n", "output": "4\n2\n0\n1\n0\n" } ]
https://atcoder.jp/contests/nadafes2022_day2/tasks/nadafes2022_day2_n
問題文 N 行 M 列のマス目に整数を書き込む方法のうち、以下が成り立つものが存在するか判定し、存在する場合は 1 個構築してください。 ここで、 A_{i,j} を上から i 番目、左から j 番目のマスに書かれた整数とします。 A_{i,j} は 0 以上 X-1 以下である。 A_{i,j} \equiv A_{i-1,j} + A_{i+1,j} + A_{i,j-1} + A_{i,j+1}\ (\bmod X) \displaystyle \sum_{i=1}^{N} \sum_{j=1}^{M} A_{i,j} > 0 ただし、 A_{0,i},A_{N+1,i},A_{i,0},A_{i,M+1} は全て 0 とします。 T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
[ { "input": "3\n2 2 3\n2 2 2\n52 53 2\n", "output": "2 1 \n1 2 \n-1\n-1\n" } ]
https://atcoder.jp/contests/nadafes2022_day2/tasks/nadafes2022_day2_o
問題文 長さ N の順列全てに対する転倒数の M 乗の総和を 998244353 で割ったあまりを求めてください。 T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
[ { "input": "4\n3 2\n4 1\n53 54\n1234 12\n", "output": "19\n72\n403070452\n49172397\n" } ]
https://atcoder.jp/contests/nadafes2022_day2/tasks/nadafes2022_day2_p
問題文 N 枚のカードがあり、 1 から N の番号が付けられており、全てのカードは初め裏を向けて並べられています。 PCT 君は全てのカードが表を向くまで以下の操作を繰り返します。 1 以上 N 以下の整数をランダムに 1 個選ぶ。選んだ整数を x とおく。カード x から x+M-1 のうち、 裏を向けているカードのみ をひっくり返す。ただし、カード i \, (i > N) はカード i-N のこととする。 操作回数の期待値 \bmod\ 998244353 を求めてください。
[ { "input": "3 3\n", "output": "1\n" }, { "input": "3 1\n", "output": "499122182\n" }, { "input": "20220 5253\n", "output": "134463961\n" } ]
https://atcoder.jp/contests/nadafes2022_day1/tasks/nadafes2022_day1_a
問題文 長さ N の攪乱順列の転倒数の最大値を求めてください。 攪乱順列とは、 1 \le i \le N に対して P_i \neq i を満たす順列のことです。 転倒数とは、 1 \le i < j \le N かつ P_i > P_j を満たす整数の組 (i,j) の個数です。
[ { "input": "3\n", "output": "2\n" } ]
https://atcoder.jp/contests/nadafes2022_day1/tasks/nadafes2022_day1_b
問題文 \lbrace 1,2,...,N \rbrace の部分集合 A のうち、以下の条件を全て満たすものの個数を 998244353 で割ったあまりを求めてください。 |A| = K 1 \le i \le N を満たす整数 i 全てに対し、 A の中に i の倍数が存在する。
[ { "input": "3 2\n", "output": "1\n" }, { "input": "100 53\n", "output": "19600\n" } ]
https://atcoder.jp/contests/nadafes2022_day1/tasks/nadafes2022_day1_c
問題文 maguro君は文化祭の展示でとあるゲームを作りました。 そのゲームでは H \times W のグリッド型のダンジョンが使われます。上から i\ (1 \leq i \leq H) 番目、左から j\ (1 \leq j \leq W) 番目のマスを (i,j) とします。 (i,j) のスタート時の状態は、 A_{i,j} が . のとき道、 # のとき壁、 @ のときマグマです。 プレイヤーは最初 (1, 1) にいて、スタートから 1 秒ごとに上下左右に隣接する道のマスに 1 マス動きます(ただし、ダンジョンの外は壁となっており進めません)。しかし、スタートしてから K, 2K, 3K \dots 秒経ったタイミングでマグマが上下左右に隣接する道のマスに浸食します。プレイヤーがマグマのあるマスに入るか、プレイヤーがいるマスにマグマが浸食するとゲームオーバーです。 なお、プレイヤーが (H, W) に到達した瞬間にプレイヤーのいるマスにマグマが浸食してもゲームオーバーとなります。 プレイヤーが (H,W) のマスに到達し、かつその瞬間にマグマがプレイヤーのいるマスに浸食しなかった場合、ゲームクリアとなります。 maguro君はこのゲームがクリア可能かどうかを確かめようとしましたが、文化祭の準備で忙しかったので、プログラマーであるあなたに頼んできました。このゲームがクリア可能か判定してください。
[ { "input": "4 4 2\n..@#\n#.#.\n....\n@##.\n", "output": "Yes\n" }, { "input": "4 4 2\n...@\n.#.#\n....\n@##.\n", "output": "No\n" }, { "input": "4 4 2\n.#.#\n....\n###.\n@...\n", "output": "No\n" }, { "input": "4 4 2\n....\n#...\n@#.#\n..#.\n", "output": "No\n" } ]
https://atcoder.jp/contests/nadafes2022_day1/tasks/nadafes2022_day1_d
問題文 正整数 N が与えられます。 (0,1, \dots, N - 1) を並べ替えて得られる数列 P = (P_1, \dots, P_N) であって、以下の条件を満たすものは存在しますか? 1 \leq i \leq N に対し Q_i = \left(\sum_{j = 1}^i P_j\right) \bmod N とおくと、 Q_1, \dots, Q_N は (0, 1,\dots, N - 1) を並べ替えて得られる。 存在するならばその一例を示し、存在しないならばそのことを報告してください。
[ { "input": "4\n", "output": "0 1 2 3\n" }, { "input": "3\n", "output": "-1\n" } ]
https://atcoder.jp/contests/nadafes2022_day1/tasks/nadafes2022_day1_e
問題文 N 頂点 M 辺の単純無向グラフが与えられます。 頂点には 1, \dots, N の番号がつけられており、頂点 i \, (1 \leq i \leq N) には整数 A_i が書かれています。 また、 i \, (1 \leq i \leq M) 番目の辺は頂点 B_i と頂点 C_i を結びます。 KoD 君はグラフから好きな本数( 0 本でもよい)の辺を選んで取り除きます。 KoD 君が獲得する スコア を、操作後のグラフにおいて次数が偶数である頂点に書かれた整数の和として定めます。ただし、操作後に次数が偶数である頂点が存在しない場合、スコアは 0 です。 KoD 君が獲得するスコアとしてあり得る最大の値を求めてください。
[ { "input": "3 2\n-10 2 3\n1 2\n2 3\n", "output": "3\n" }, { "input": "4 2\n1 2 3 -100\n1 3\n2 3\n", "output": "-94\n" } ]
https://atcoder.jp/contests/nadafes2022_day1/tasks/nadafes2022_day1_f
問題文 長さ N の順列 P_1,P_2,\dots,P_N のうち、以下を満たすものの個数を 998244353 で割ったあまりを求めてください。 整数 X=1 がある。 X を P_X で置き換える操作を M 回繰り返した後に X=K となっている。
[ { "input": "3 2 1\n", "output": "4\n" }, { "input": "2022 53 54\n", "output": "294327113\n" } ]
https://atcoder.jp/contests/nadafes2022_day1/tasks/nadafes2022_day1_g
問題文 N 行 M 列のマス目があります。上から i 番目、左から j 番目のマス目を (i,j) で表します。 PCT 君は、 (1,1) から、右のマスか下のマスに移動することを繰り返し (N,M) に行こうとしています。 しかし、下のマスが存在する (N-1)M マスのうちいずれかでは下のマスに移動することができません。 下に移動することのできないマスの組は 2^{(N-1)M} 通りありますが、そのうち PCT 君が (1,1) から (N,M) に移動できるものの個数を 998244353 で割ったあまりを求めてください。
[ { "input": "2 2\n", "output": "3\n" }, { "input": "2022 5354\n", "output": "63679225\n" } ]
https://atcoder.jp/contests/nadafes2022_day1/tasks/nadafes2022_day1_h
問題文 NPCA 王国には N 個の町と N-1 個の道があります。 i 番目の道は長さが 1 で、町 u_i と町 v_i を双方向に結んでいます。また、どの町からどの町へもいくつかの道路を通ることで移動することができます。 クエリが Q 個与えられます。 i 番目のクエリは以下です。 旅人の PCT 君が、好きな町を出発し、 K_i 個の町 A_{i,1},A_{i,2},\ldots,A_{i,K_i} を訪れ、好きな町で旅を終えるとき、移動する距離の最小値を求めよ。ただし、訪れる順番は何でもよい。 最短距離で移動したい PCT 君のために、 Q 個のクエリをすべて処理してください。
[ { "input": "3 2\n1 2\n2 3\n2 1 2\n3 1 2 3\n", "output": "1\n2\n" }, { "input": "2 1\n1 2\n2 1 2\n", "output": "1\n" }, { "input": "6 4\n2 5\n3 5\n1 4\n4 5\n4 6\n3 1 2 3\n3 2 4 5\n2 1 6\n6 1 2 3 4 5 6\n", "output": "5\n2\n2\n7\n" } ]
https://atcoder.jp/contests/nadafes2022_day1/tasks/nadafes2022_day1_i
問題文 NPCA 王国は 2 次元平面上の N 個の町からなり、町 i\,(1 \leq i \leq N) の座標は (x_i,y_i) です。 次が成り立つとき、またこの時に限り、町 i と町 j を双方向に結ぶ長さ c_i+c_j の道があります。 \min(|x_i-x_j|,|y_i-y_j|) \leq K 町 1 から町 N への最短距離を求めてください。
[ { "input": "3 3\n1 3 10\n3 10 5\n7 13 5\n", "output": "25\n" }, { "input": "2 1\n1 1 10\n10 10 10\n", "output": "-1\n" }, { "input": "6 100\n105 203 115\n56 51 299\n47 85 234\n108 277 160\n260 237 25\n170 147 127\n", "output": "242\n" } ]
https://atcoder.jp/contests/nadafes2022_day1/tasks/nadafes2022_day1_j
問題文 長さ N の正整数列 A = (A_1, \dots, A_N) が与えられます。 Q 個のクエリを処理してください。 i \, (1 \leq i \leq Q) 個目のクエリでは、 K_i 個の整数 p_{i, 1}, \dots, p_{i, K_i} が与えられるので、 A_1, \dots, A_N のうち A_{p_{i, 1}}, \dots, A_{p_{i, K_i}} を除いた N - K_i 個の整数の最小公倍数を 998244353 で割った余りを求めてください。
[ { "input": "4\n2 4 3 6\n3\n1 1\n3 1 3 4\n2 2 3\n", "output": "12\n4\n6\n" }, { "input": "10\n1000000 1000000 999999 999998 999997 999996 999995 999994 999993 999992\n1\n1 1\n", "output": "143975436\n" } ]
https://atcoder.jp/contests/nadafes2022_day1/tasks/nadafes2022_day1_k
問題文 以下の図のタイル L を A 枚、タイル I を B 枚使い、 2 \times N の長方形を敷き詰める方法の個数を 998244353 で割った余りを求めてください。 ただし、同じタイルは区別がつかず、またタイルを回転や反転させて使ってもよいです。 回転や反転によって一致するタイルの敷き詰め方は異なるものとします。
[ { "input": "4 2 1\n", "output": "6\n" }, { "input": "250 100 100\n", "output": "174250488\n" } ]
https://atcoder.jp/contests/nadafes2022_day1/tasks/nadafes2022_day1_l
問題文 N 頂点の木が与えられます。 i 番目の辺は長さが 1 で、頂点 u_i と頂点 v_i を結んでいます。 頂点 1 を出発し、全ての辺を 2 回ずつ通り、頂点 1 に戻ってくる移動方法を考えます。 i 番目の要素が i 番目に通った頂点の深さと定義される数列としてありうる辞書順最小のものを求めてください。ただし、頂点の深さとは頂点 1 との距離を指すとします。
[ { "input": "4\n1 2\n2 3\n1 4\n", "output": "0 1 0 1 2 1 0\n" }, { "input": "2\n1 2\n", "output": "0 1 0\n" }, { "input": "6\n2 5\n3 5\n1 4\n4 5\n4 6\n", "output": "0 1 2 1 2 3 2 3 2 1 0\n" } ]
https://atcoder.jp/contests/nadafes2022_day1/tasks/nadafes2022_day1_m
問題文 N 枚のカードがあります。それぞれのカードには 1 から N の番号がつけられています。blackyuki 君と KoD 君はこのカードを使ってゲームをすることにしました。 blackyuki 君から初めて、全てのカードがなくなるまで交互に以下の操作を繰り返します。 残っているカードから 1 枚選ぶ、選んだカードの番号を i とする。 \frac{A_i}{B_i} の確率でカード i を手に入れ、操作の初めに戻る。 1-\frac{A_i}{B_i} の確率で、何もせず操作を終了する。 ゲーム終了時に blackyuki 君の持っているカードの枚数が KoD 君の持っているカードの枚数以上であれば blackyuki 君の勝ち、そうでなければ KoD 君の勝ちとします。 2 人は、以下のように行動します。 まず、自分がゲーム終了時に持っているカードの枚数の期待値を最大化するようにカードを選ぶ。 上記のみでは選ぶカードの候補が複数ある場合、その中で最も番号が小さいカードを選ぶ。 blackyuki 君の勝つ確率 \bmod\ 998244353 を求めてください。
[ { "input": "1\n1 2\n", "output": "665496236\n" }, { "input": "2\n1 1\n1 1\n", "output": "1\n" }, { "input": "8\n100660 113169\n10964 152336\n57329 77239\n98640 167660\n103515 136455\n98695 99571\n14421 149410\n12488 21041\n", "output": "743177673\n" } ]
https://atcoder.jp/contests/nadafes2022_day1/tasks/nadafes2022_day1_n
問題文 長さ N の整数列 A = (A_1, \dots, A_N) があります。はじめ、全ての要素は 0 です。 \{(a, b) \, | \, a \in \mathbb{N}, b \in \mathbb{N}, 1 \leq a \leq b \leq N \} の部分集合 S であって、以下の条件を全て満たすものの総数を 998244353 で割った余りを求めてください。 |S| = M S の全ての要素 (l, r) に対して以下を行った後、全ての 1 \leq i \leq N について A_i = B_i が成り立つ。 l \leq i \leq r を満たす全ての i に対し、 A_i を 1 - A_i で置き換える。
[ { "input": "3 2\n1 1 0\n", "output": "2\n" }, { "input": "3 1\n1 0 1\n", "output": "0\n" }, { "input": "10 20\n1 0 1 1 0 1 1 1 1 1\n", "output": "71696207\n" } ]
https://atcoder.jp/contests/nadafes2022_day1/tasks/nadafes2022_day1_o
問題文 N 個の机が横一列に並んでいます。机 i\,(1 \leq i \leq N) は初期状態で左から i 番目にあり、重さは A_i です。PCT 君は以下の操作を任意回行うことができます。 隣り合っている 2 つの机を選ぶ。これらの重さの和が K 以下のとき、これらの場所を入れ替える。 Q 個の独立なクエリが与えられます。 i 番目のクエリでは、PCT 君が机 s_i を左から t_i 番目に移動させることができるか判定してください。
[ { "input": "3 3 4\n1 2 3\n1 3\n3 1\n2 3\n", "output": "Yes\nNo\nNo\n" }, { "input": "4 1 1\n100 100 100 100\n1 1\n", "output": "Yes\n" }, { "input": "8 3 10\n4 1 1 3 6 4 3 6\n5 7\n3 3\n1 1\n", "output": "Yes\nYes\nYes\n" } ]
https://atcoder.jp/contests/nadafes2022_day1/tasks/nadafes2022_day1_p
問題文 長さ N かつ全ての要素が 1 以上 M 以下である整数列 A に対する \max_{1 \le i < j \le N} (A_j - A_i) の総和を 998244353 で割った余りを求めてください。
[ { "input": "2 2\n", "output": "0\n" }, { "input": "3 4\n", "output": "76\n" }, { "input": "100 20000\n", "output": "388129947\n" } ]
https://atcoder.jp/contests/arc139/tasks/arc139_a
Problem Statement For a positive integer x , let \mathrm{ctz}(x) be the number of trailing zeros in the binary representation of x . For example, we have \mathrm{ctz}(8)=3 because the binary representation of 8 is 1000 , and \mathrm{ctz}(5)=0 because the binary representation of 5 is 101 . You are given a sequence of non-negative integers T = (T_1,T_2,\dots,T_N) . Consider making a sequence of positive integers A = (A_1, A_2, \dots, A_N) of your choice so that it satisfies the following conditions. A_1 \lt A_2 \lt \cdots \lt A_{N-1} \lt A_N holds. In other words, A is strictly increasing. \mathrm{ctz}(A_i) = T_i holds for every integer i such that 1 \leq i \leq N . What is the minimum possible value of A_N here?
[ { "input": "4\n0 1 3 2\n", "output": "12\n" }, { "input": "5\n4 3 2 1 0\n", "output": "31\n" }, { "input": "1\n40\n", "output": "1099511627776\n" }, { "input": "8\n2 0 2 2 0 4 2 4\n", "output": "80\n" } ]
https://atcoder.jp/contests/arc139/tasks/arc139_b
Problem Statement We have an integer P=0 . Find the minimum total cost to make P=N by doing the following three kinds of operations any number of times in any order. Increase P by 1 , at a cost of X . Increase P by A , at a cost of Y . Increase P by B , at a cost of Z . Solve each of the T test cases given to you.
[ { "input": "5\n10 3 5 2 3 6\n10 3 5 1 1000000000 1000000000\n139 2 139 1 1 1\n139 1 1 1 1 1\n139 7 10 3845 26982 30923\n", "output": "11\n10\n1\n139\n436604\n" } ]
https://atcoder.jp/contests/arc139/tasks/arc139_c
Problem Statement You are given positive integers N and M . Let us call a sequence of pairs of integers ((X_1,Y_1),(X_2,Y_2),\dots,(X_K,Y_K)) wonderful when it satisfies the following. 1 \le X_i \le N 1 \le Y_i \le M X_i+3Y_i \neq X_j+3Y_j and 3X_i+Y_i \neq 3X_j+Y_j , if i \neq j . Make a wonderful sequence of pairs of integers whose length, K , is the maximum possible.
[ { "input": "3 4\n", "output": "10\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3\n3 4\n" } ]
https://atcoder.jp/contests/arc139/tasks/arc139_d
Problem Statement You are given a multiset of integers with N elements: A=\lbrace A_1,A_2,...,A_N \rbrace . It is guaranteed that every element of A is between 1 and M (inclusive). Let us repeat the following operation K times. Choose an integer between 1 and M (inclusive) and add it to A . Then, delete the X -th smallest value in A . Here, the X -th smallest value in A is the X -th value from the front in the sequence of the elements of A sorted in non-decreasing order. There are M^K ways to choose an integer K times between 1 and M . Assume that, for each of these ways, we have found the sum of the elements of A after the operations with the corresponding choices. Find the sum, modulo 998244353 , of the M^K values computed.
[ { "input": "2 4 2 1\n1 3\n", "output": "99\n" }, { "input": "5 9 6 3\n3 7 1 9 9\n", "output": "15411789\n" } ]
https://atcoder.jp/contests/arc139/tasks/arc139_e
Problem Statement We have a grid with H rows and W columns. Let (i, j) denote the square at the i -th row from the top and j -th column from the left. Assume that this grid is a torus; that is, in addition to the pairs of squares horizontally or vertically adjacent to each other, assume the following pairs of squares to be adjacent to each other. (i,1) and (i,W) , for every integer i such that 1 \leq i \leq H . (1,j) and (H,j) , for every integer j such that 1 \leq j \leq W . Consider placing some number of pieces on the squares in the grid. Here, there can be at most one piece on each square, and there cannot be two pieces on adjacent squares. Let L be the maximum number of pieces that can be placed. Find the number of ways, modulo 998244353 , to place L pieces.
[ { "input": "3 2\n", "output": "6\n" }, { "input": "139 424\n", "output": "148734121\n" }, { "input": "12345 1234567890\n", "output": "227996418\n" } ]
https://atcoder.jp/contests/arc139/tasks/arc139_f
Problem Statement PCT made the following problem. Xor Optimization Problem You are given a sequence of non-negative integers of length N : A_1,A_2,...,A_N . When it is allowed to choose any number of elements in A , what is the maximum possible \mathrm{XOR} of the chosen values? Nyaan thought it was too easy and revised it to the following. Many Xor Optimization Problems There are 2^{NM} sequences of length N consisting of integers between 0 and 2^M-1 . Find the sum, modulo 998244353 , of the answers to Xor Optimization Problem for all those sequences. Solve Many Xor Optimization Problems . What is bitwise \mathrm{XOR} ? The bitwise \mathrm{XOR} of non-negative integers A and B , A \oplus B , is defined as follows: When A \oplus B is written in base two, the digit in the 2^k 's place ( k \geq 0 ) is 1 if exactly one of A and B is 1 , and 0 otherwise. For example, we have 3 \oplus 5 = 6 (in base two: 011 \oplus 101 = 110 ). Generally, the bitwise \mathrm{XOR} of k non-negative integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k) . We can prove that this value does not depend on the order of p_1, p_2, p_3, \dots, p_k .
[ { "input": "2 1\n", "output": "3\n" }, { "input": "3 4\n", "output": "52290\n" }, { "input": "1234 5678\n", "output": "495502261\n" } ]
https://atcoder.jp/contests/ahc010/tasks/ahc010_a
Problem Statement You are given tiles containing railroad lines arranged in a 30 x 30 square. There are 8 types of tiles by distinguishing rotations which are numbered as follows. 0 1 2 3 4 5 6 7 Tiles 0 to 3 contain one curved line, tiles 4 and 5 contain two curved lines, and tiles 6 and 7 contain one straight line. Each tile can be rotated every 90 degrees. By rotating a tile 90 degrees counterclockwise, the tile will become as follows. 1 2 3 0 5 4 7 6 Since there are no branches on the lines, each line is part of a path or cycle. A set of lines forming a cycle is called a "loop line," and its length is defined as the number of times to move from a tile to its adjacent tile in a round trip along the loop line. For example, the loop line below consists of 7 tiles, but its length is 8 because it passes through the center tile twice. Your task is to determine the number of times to rotate each tile.
[ { "input": "436204751575440756541724746755\n347475575404531645424640344414\n556644445442451553264555436757\n761235545455474254546631467555\n356447405421445153457656535564\n014274425356522445253477726464\n311765446655556346633757446600\n471744514426443445162555525455\n616053450444473274742055767455\n254124557527462423444450075637\n046546764557475436717475255501\n752005462554554414031525515356\n452524742177476245065554577605\n664465643742341605007655253777\n444571276444165545442447340356\n435050335454565235025507452540\n467560030465475447567644441426\n735730577745561712541450443547\n472675153755474445700540444544\n507472724556677621365747544757\n535177720776402476665547676174\n636275455643650141456764547131\n164624553536572554544165746536\n521574724335644274433544442556\n576732703453654464555315065544\n656244747015464523316444145414\n555646775254464367454454067475\n665624154657072514445150474444\n570004746554540445465051654541\n635504417414262014475547424275\n", "output": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n" } ]
https://atcoder.jp/contests/abc249/tasks/abc249_a
Problem Statement Takahashi and Aoki decided to jog. Takahashi repeats the following: "walk at B meters a second for A seconds and take a rest for C seconds." Aoki repeats the following: "walk at E meters a second for D seconds and take a rest for F seconds." When X seconds have passed since they simultaneously started to jog, which of Takahashi and Aoki goes ahead?
[ { "input": "4 3 3 6 2 5 10\n", "output": "Takahashi\n" }, { "input": "3 1 4 1 5 9 2\n", "output": "Aoki\n" }, { "input": "1 1 1 1 1 1 1\n", "output": "Draw\n" } ]
https://atcoder.jp/contests/abc249/tasks/abc249_b
Problem Statement Let us call a string consisting of uppercase and lowercase English alphabets a wonderful string if all of the following conditions are satisfied: The string contains an uppercase English alphabet. The string contains a lowercase English alphabet. All characters in the string are pairwise distinct. For example, AtCoder and Aa are wonderful strings, while atcoder and Perfect are not. Given a string S , determine if S is a wonderful string.
[ { "input": "AtCoder\n", "output": "Yes\n" }, { "input": "Aa\n", "output": "Yes\n" }, { "input": "atcoder\n", "output": "No\n" }, { "input": "Perfect\n", "output": "No\n" } ]
https://atcoder.jp/contests/abc249/tasks/abc249_c
Problem Statement You are given N strings S_1,S_2,\dots,S_N consisting of lowercase English alphabets. Consider choosing some number of strings from S_1,S_2,\dots,S_N . Find the maximum number of distinct alphabets that satisfy the following condition: "the alphabet is contained in exactly K of the chosen strings."
[ { "input": "4 2\nabi\naef\nbc\nacg\n", "output": "3\n" }, { "input": "2 2\na\nb\n", "output": "0\n" }, { "input": "5 2\nabpqxyz\naz\npq\nbc\ncy\n", "output": "7\n" } ]
https://atcoder.jp/contests/abc249/tasks/abc249_d
Problem Statement You are given an integer sequence A = (A_1, \dots, A_N) of length N . Find the number of triplets of integers (i, j, k) satisfying all of the conditions below. 1 \leq i, j, k \leq N \frac{A_i}{A_j} = A_k
[ { "input": "3\n6 2 3\n", "output": "2\n" }, { "input": "1\n2\n", "output": "0\n" }, { "input": "10\n1 3 2 4 6 8 2 2 3 7\n", "output": "62\n" } ]
https://atcoder.jp/contests/abc249/tasks/abc249_e
Problem Statement Consider the following procedure of, given a string X consisting of lowercase English alphabets, obtaining a new string: Split the string X off at the positions where two different characters are adjacent to each other. For each string Y that has been split off, replace Y with a string consisting of the character which Y consists of, followed by the length of Y . Concatenate the replaced strings without changing the order. For example, aaabbcccc is divided into aaa , bb , cccc , which are replaced by a3 , b2 , c4 , respectively, which in turn are concatenated without changing the order, resulting in a3b2c4 .If the given string is aaaaaaaaaa , the new string is a10 . Find the number, modulo P , of strings S of lengths N consisting of lowercase English alphabets, such that the length of T is smaller than that of S , where T is the string obtained by the procedure above against the string S .
[ { "input": "3 998244353\n", "output": "26\n" }, { "input": "2 998244353\n", "output": "0\n" }, { "input": "5 998244353\n", "output": "2626\n" }, { "input": "3000 924844033\n", "output": "607425699\n" } ]
https://atcoder.jp/contests/abc249/tasks/abc249_f
Problem Statement Takahashi has an integer x . Initially, x = 0 . There are N operations. The i -th operation (1 \leq i \leq N) is represented by two integers t_i and y_i as follows: If t_i = 1 , replace x with y_i . If t_i = 2 , replace x with x + y_i . Takahashi may skip any number between 0 and K (inclusive) of the operations. When he performs the remaining operations once each without changing the order, find the maximum possible final value of x .
[ { "input": "5 1\n2 4\n2 -3\n1 2\n2 1\n2 -3\n", "output": "3\n" }, { "input": "1 0\n2 -1000000000\n", "output": "-1000000000\n" }, { "input": "10 3\n2 3\n2 -1\n1 4\n2 -1\n2 5\n2 -9\n2 2\n1 -6\n2 5\n2 -3\n", "output": "15\n" } ]
https://atcoder.jp/contests/abc249/tasks/abc249_g
Problem Statement There are N cards numbered 1, \dots, N . Card i \, (1 \leq i \leq N) has an integer A_i written on the front and an integer B_i written on the back. Consider choosing one or more cards so that the exclusive logical sum of the integers written on the front of the chosen cards is at most K . Find the maximum possible exclusive logical sum of the integers written on the back of the chosen cards. What is the exclusive logical sum? The exclusive logical sum a \oplus b of two integers a and b is defined as follows. The 2^k 's place ( k \geq 0 ) in the binary notation of a \oplus b is 1 if exactly one of the 2^k 's places in the binary notation of a and b is 1 ; otherwise, it is 0 . For example, 3 \oplus 5 = 6 (In binary notation: 011 \oplus 101 = 110 ). In general, the exclusive logical sum of k integers p_1, \dots, p_k is defined as (\cdots ((p_1 \oplus p_2) \oplus p_3) \oplus \cdots \oplus p_k) . We can prove that it is independent of the order of p_1, \dots, p_k .
[ { "input": "4 2\n1 1\n3 2\n2 2\n0 1\n", "output": "3\n" }, { "input": "1 2\n3 4\n", "output": "-1\n" }, { "input": "10 326872757\n487274679 568989827\n267359104 968688210\n669234369 189421955\n1044049637 253386228\n202278801 233212012\n436646715 769734012\n478066962 376960084\n491389944 1033137442\n214977048 1051768288\n803550682 1053605300\n", "output": "1064164329\n" } ]
https://atcoder.jp/contests/abc249/tasks/abc249_h
Problem Statement There are N balls numbered 1 through N . Initially, Ball i is painted in Color A_i . Colors are represented by integers between 1 and N , inclusive. Consider repeating the following operation until all the colors of the balls become the same. There are 2^N subsets (including the empty set) of the set consisting of the N balls; choose one of the subsets uniformly at random. Let X_1,X_2,...,X_K be the indices of the chosen balls. Next, choose a permutation obtained by choosing K integers out of integers (1,2,\dots,N) uniformly at random. Let P=(P_1,P_2,\dots,P_K) be the chosen permutation. For each integer i such that 1 \le i \le K , change the color of Ball X_i to P_i . Find the expected value of number of operations, modulo 998244353 . Here a permutation obtained by choosing K integers out of integers (1,2,\dots,N) is a sequence of K integers between 1 and N , inclusive, whose elements are pairwise distinct.
[ { "input": "2\n1 2\n", "output": "4\n" }, { "input": "3\n1 1 1\n", "output": "0\n" }, { "input": "10\n3 1 4 1 5 9 2 6 5 3\n", "output": "900221128\n" } ]
https://atcoder.jp/contests/abc248/tasks/abc248_a
Problem Statement You are given a string S of length exactly 9 consisting of digits. One but all digits from 0 to 9 appear exactly once in S . Print the only digit missing in S .
[ { "input": "023456789\n", "output": "1\n" }, { "input": "459230781\n", "output": "6\n" } ]
https://atcoder.jp/contests/abc248/tasks/abc248_b
Problem Statement There are A slimes. Each time Snuke shouts, the slimes multiply by K times. In order to have B or more slimes, at least how many times does Snuke need to shout?
[ { "input": "1 4 2\n", "output": "2\n" }, { "input": "7 7 10\n", "output": "0\n" }, { "input": "31 415926 5\n", "output": "6\n" } ]
https://atcoder.jp/contests/abc248/tasks/abc248_c
Problem Statement How many integer sequences of length N , A=(A_1, \ldots, A_N) , satisfy all of the conditions below? 1\le A_i \le M (1 \le i \le N) \displaystyle\sum _{i=1}^N A_i \leq K Since the count can get enormous, find it modulo 998244353 .
[ { "input": "2 3 4\n", "output": "6\n" }, { "input": "31 41 592\n", "output": "798416518\n" } ]
https://atcoder.jp/contests/abc248/tasks/abc248_d
Problem Statement You are given a sequence of length N : A=(A_1,\ldots,A_N) . Answer Q queries given in the following format. You are given integers L , R , and X . Find the number of elements among A_L, \ldots, A_R whose values are equal to X .
[ { "input": "5\n3 1 4 1 5\n4\n1 5 1\n2 4 3\n1 5 2\n1 3 3\n", "output": "2\n0\n0\n1\n" } ]
https://atcoder.jp/contests/abc248/tasks/abc248_e
Problem Statement You are given N points in the coordinate plane. For each 1\leq i\leq N , the i -th point is at the coordinates (X_i, Y_i) . Find the number of lines in the plane that pass K or more of the N points. If there are infinitely many such lines, print Infinity .
[ { "input": "5 2\n0 0\n1 0\n0 1\n-1 0\n0 -1\n", "output": "6\n" }, { "input": "1 1\n0 0\n", "output": "Infinity\n" } ]
https://atcoder.jp/contests/abc248/tasks/abc248_f
Problem Statement You are given an integer N greater than or equal to 2 and a prime P . Consider the graph G with 2N vertices and (3N-2) edges shown in the figure below. More specifically, the edges connect the vertices as follows, where the vertices are labeled as Vertex 1 , Vertex 2 , \ldots , Vertex 2N , and the edges are labeled as Edge 1 , Edge 2 , \ldots , Edge (3N-2) . For each 1\leq i\leq N-1 , Edge i connects Vertex i and Vertex i+1 . For each 1\leq i\leq N-1 , Edge (N-1+i) connects Vertex N+i and Vertex N+i+1 . For each 1\leq i\leq N , Edge (2N-2+i) connects Vertex i and Vertex N+i . For each i=1,2,\ldots ,N-1 , solve the following problem. Find the number of ways, modulo P , to remove exactly i of the 3N-2 edges of G so that the resulting graph is still connected.
[ { "input": "3 998244353\n", "output": "7 15\n" }, { "input": "16 999999937\n", "output": "46 1016 14288 143044 1079816 6349672 29622112 110569766 330377828 784245480 453609503 38603306 44981526 314279703 408855776\n" } ]
https://atcoder.jp/contests/abc248/tasks/abc248_g
Problem Statement You are given an undirected tree with N vertices. Let us call the vertices Vertex 1 , Vertex 2 , \ldots , Vertex N . For each 1\leq i\leq N-1 , the i -th edge connects Vertex U_i and Vertex V_i . Additionally, each vertex is assigned a positive integer: Vertex i is assigned A_i . The cost between two distinct vertices s and t , C(s,t) , is defined as follows. Let p_1(=s) , p_2 , \ldots , p_k(=t) be the vertices of the simple path connecting Vertex s and Vertex t , where k is the number of vertices in the path (including the endpoints). Then, let C(s,t)=k\times \gcd (A_{p_1},A_{p_2},\ldots,A_{p_k}) , where \gcd (X_1,X_2,\ldots, X_k) denotes the greatest common divisor of X_1,X_2,\ldots, X_k . Find \displaystyle\sum_{i=1}^{N-1}\sum_{j=i+1}^N C(i,j) , modulo 998244353 .
[ { "input": "4\n24 30 28 7\n1 2\n1 3\n3 4\n", "output": "47\n" }, { "input": "10\n180 168 120 144 192 200 198 160 156 150\n1 2\n2 3\n2 4\n2 5\n5 6\n4 7\n7 8\n7 9\n9 10\n", "output": "1184\n" } ]
https://atcoder.jp/contests/abc248/tasks/abc248_h
Problem Statement You are given a permutation P=(P_1,\ldots,P_N) of (1,\ldots,N) , and an integer K . Find the number of pairs of integers (L, R) that satisfy all of the following conditions: 1 \leq L \leq R \leq N \mathrm{max}(P_L,\ldots,P_R) - \mathrm{min}(P_L,\ldots,P_R) \leq R - L + K
[ { "input": "4 1\n1 4 2 3\n", "output": "9\n" }, { "input": "2 0\n2 1\n", "output": "3\n" }, { "input": "10 3\n3 7 10 1 9 5 4 8 6 2\n", "output": "37\n" } ]
https://atcoder.jp/contests/abc247/tasks/abc247_a
Problem Statement There are 4 squares lined up horizontally. You are given a string S of length 4 consisting of 0 and 1 . If the i -th character of S is 1 , there is a person in the i -th square from the left; if the i -th character of S is 0 , there is no person in the i -th square from the left. Now, everyone will move to the next square to the right simultaneously. By this move, the person who was originally in the rightmost square will disappear. Determine if there will be a person in each square after the move. Print the result as a string in the same format as S . (See also Sample Input / Output for clarity.)
[ { "input": "1011\n", "output": "0101\n" }, { "input": "0000\n", "output": "0000\n" }, { "input": "1111\n", "output": "0111\n" } ]
https://atcoder.jp/contests/abc247/tasks/abc247_f
Problem Statement There are N cards numbered 1,\ldots,N . Card i has P_i written on the front and Q_i written on the back. Here, P=(P_1,\ldots,P_N) and Q=(Q_1,\ldots,Q_N) are permutations of (1, 2, \dots, N) . How many ways are there to choose some of the N cards such that the following condition is satisfied? Find the count modulo 998244353 . Condition: every number 1,2,\ldots,N is written on at least one of the chosen cards.
[ { "input": "3\n1 2 3\n2 1 3\n", "output": "3\n" }, { "input": "5\n2 3 5 4 1\n4 2 1 3 5\n", "output": "12\n" }, { "input": "8\n1 2 3 4 5 6 7 8\n1 2 3 4 5 6 7 8\n", "output": "1\n" } ]
https://atcoder.jp/contests/abc247/tasks/abc247_g
Problem Statement There are N competitive programmers. The i -th competitive programmer belongs to University A_i , is good at Subject B_i , and has a power of C_i . Consider a team consisting of some of the N people. Let us call such a team a dream team if both of the following conditions are satisfied: Any two people belonging to the team belong to different universities. Any two people belonging to the team are good at different subjects. Let k be the maximum possible number of members of a dream team. For each i=1,2,\ldots,k , solve the following question. Question: find the maximum sum of power of people belonging to a dream team consisting of i people.
[ { "input": "3\n1 1 100\n1 20 10\n2 1 1\n", "output": "2\n100\n11\n" }, { "input": "10\n1 4 142135623\n2 6 457513110\n3 1 622776601\n5 1 961524227\n2 2 360679774\n2 4 494897427\n3 7 416573867\n5 2 915026221\n1 7 320508075\n5 3 851648071\n", "output": "4\n961524227\n1537802822\n2032700249\n2353208324\n" } ]
https://atcoder.jp/contests/abc247/tasks/abc247_h
Problem Statement There are N people called Person 1 , Person 2 , \dots , Person N , lined up in a row in the order of (1,2,\dots,N) from the front. Person i is wearing Color c_i . Takahashi repeated the following operation K times: choose two People i and j arbitrarily and swap the positions of Person i and Person j . After the K operations have ended, the color that the i -th person from the front is wearing coincided with c_i , for every integer i such that 1 \leq i \leq N . How many possible permutations of people after the K operations are there? Find the count modulo 998244353 .
[ { "input": "4 1\n1 1 2 1\n", "output": "3\n" }, { "input": "3 3\n1 1 2\n", "output": "1\n" }, { "input": "10 4\n2 7 1 8 2 8 1 8 2 8\n", "output": "132\n" } ]
https://atcoder.jp/contests/arc138/tasks/arc138_a
Problem Statement We have an integer sequence of length N : A=(A_1,A_2,\cdots,A_N) . Below in this problem, let the score of A be the sum of the first K terms of A . Additionally, let s be the score of the sequence A given in input. You can do the following operation any number of times. Choose two adjacent elements of A and swap them. Your objective is to make the score at least s+1 . Determine whether the objective is achievable. If it is, find the minimum number of operations needed to achieve it.
[ { "input": "4 2\n2 1 1 2\n", "output": "2\n" }, { "input": "3 1\n3 2 1\n", "output": "-1\n" }, { "input": "20 13\n90699850 344821203 373822335 437633059 534203117 523743511 568996900 694866636 683864672 836230375 751240939 942020833 865334948 142779837 22252499 197049878 303376519 366683358 545670804 580980054\n", "output": "13\n" } ]
https://atcoder.jp/contests/arc138/tasks/arc138_b
Problem Statement Snuke is about to make an integer sequence of length N consisting of 0 and 1 . He starts with an empty sequence x and does the following two kinds of operations N times in total, in any order he likes. Operation A: Flip every element of x , that is, convert each 0 to 1 and vice versa. Then, add 0 to the beginning of x . Operation B: Add 0 to the end of x . You are given an integer sequence of length N consisting of 0 and 1 : A=(A_1,A_2,\cdots,A_N) . Determine whether it is possible to make x equal to A .
[ { "input": "4\n0 1 1 0\n", "output": "Yes\n" }, { "input": "4\n1 0 0 0\n", "output": "No\n" }, { "input": "4\n0 0 0 1\n", "output": "No\n" } ]
https://atcoder.jp/contests/arc138/tasks/arc138_c
Problem Statement There are N cards, indexed 1 to N . Card i has an integer A_i written on it. Here, N is even. Snuke and Mr. Min will play a game. The game consists of N turns, alternately taken by the two players, with Snuke going first. In each turn, the player does the following operation. In Snuke's turn: He takes any card of his choice that is not taken by anyone yet. In Mr. Min's turn: He takes the card with the minimum index that is not taken by anyone yet. Snuke's score will be the sum of the integers written on the cards taken by Snuke. Snuke plays optimally to maximize the score. Incidentally, being a big fan of Snuke, you are planning to do something nasty to maximize the score. Specifically, before the start of the game, you will do the following action once. Choose an integer k ( 0 \leq k \leq N-1 ) and cyclically shift the integers written on the cards by k to the left: the cards 1,2,\cdots,N will have A_{k+1},A_{k+2},\cdots,A_N,A_1,\cdots,A_k written on them. Find the value k that you should choose to maximize the score, and the resulting score when choosing that k .
[ { "input": "4\n3 4 1 2\n", "output": "1 7\n" }, { "input": "2\n1 1\n", "output": "0 1\n" }, { "input": "10\n716893678 779607519 555600775 393111963 950925400 636571379 912411962 44228139 15366410 2063694\n", "output": "7 3996409938\n" } ]
https://atcoder.jp/contests/arc138/tasks/arc138_d
Problem Statement You are given integers N and K . Determine whether there exists a permutation P=(P_0,P_1,\cdots,P_{2^N-1}) of (0,1,\cdots,2^N-1) satisfying the condition below, and construct one such sequence if it exists. Note that P is 0 -indexed. For every i ( 0 \leq i \leq 2^N-1 ), P_i and P_{i+1 \mod 2^N} differ by exactly K bits in binary representation. The comparison is made after zero-padding both integers to N bits.
[ { "input": "3 1\n", "output": "Yes\n0 1 3 2 6 7 5 4\n" }, { "input": "2 2\n", "output": "No\n" } ]