url
stringlengths
49
92
description
stringlengths
22
4.78k
cases
listlengths
0
6
https://atcoder.jp/contests/abc297/tasks/abc297_d
Problem Statement You are given positive integers A and B . You will repeat the following operation until A=B : compare A and B to perform one of the following two: if A > B , replace A with A-B ; if A < B , replace B with B-A . How many times will you repeat it until A=B ? It is guaranteed that a finite repetition makes A=B .
[ { "input": "3 8\n", "output": "4\n" }, { "input": "1234567890 1234567890\n", "output": "0\n" }, { "input": "1597 987\n", "output": "15\n" } ]
https://atcoder.jp/contests/abc297/tasks/abc297_e
Problem Statement In AtCoder Kingdom, N kinds of takoyaki s (ball-shaped Japanese food) are sold. A takoyaki of the i -th kind is sold for A_i yen. Takahashi will buy at least one takoyaki in total. He is allowed to buy multiple takoyakis of the same kind. Find the K -th lowest price that Takahashi may pay. Here, if there are multiple sets of takoyakis that cost the same price, the price is counted only once.
[ { "input": "4 6\n20 25 30 100\n", "output": "50\n" }, { "input": "2 10\n2 1\n", "output": "10\n" }, { "input": "10 200000\n955277671 764071525 871653439 819642859 703677532 515827892 127889502 881462887 330802980 503797872\n", "output": "5705443819\n" } ]
https://atcoder.jp/contests/abc297/tasks/abc297_f
Problem Statement We have a grid with H rows and W columns. We choose K cells in this grid uniformly at random. The score is the number of cells in the minimum rectangle (whose edges are parallel to the axes of the grid) that contains all of the chosen cells. Find the expected score modulo 998244353 . What is rational number modulo 998244353 ? We can prove that the sought expected value is always a rational number. Moreover, under the Constraints of this problem, when the value is represented as \frac{P}{Q} by two coprime integers P and Q , we can prove that there is a unique integer R such that R \times Q \equiv P\pmod{998244353} and 0 \leq R \lt 998244353 . Find such R .
[ { "input": "2 2 2\n", "output": "665496238\n" }, { "input": "10 10 1\n", "output": "1\n" }, { "input": "314 159 2653\n", "output": "639716353\n" } ]
https://atcoder.jp/contests/abc297/tasks/abc297_g
Problem Statement There are N piles of stones. Initially, the i -th pile contains A_i stones. With these piles, Taro the First and Jiro the Second play a game against each other. Taro the First and Jiro the Second make the following move alternately, with Taro the First going first: Choose a pile of stones, and remove between L and R stones (inclusive) from it. A player who is unable to make a move loses, and the other player wins. Who wins if they optimally play to win?
[ { "input": "3 1 2\n2 3 3\n", "output": "First\n" }, { "input": "5 1 1\n3 1 4 1 5\n", "output": "Second\n" }, { "input": "7 3 14\n10 20 30 40 50 60 70\n", "output": "First\n" } ]
https://atcoder.jp/contests/abc297/tasks/abc297_h
Problem Statement A positive-integer sequence is said to be splendid if no two adjacent elements are equal. Find the sum, modulo 998244353 , of the lengths of all splendid sequences whose elements have a sum of N .
[ { "input": "4\n", "output": "8\n" }, { "input": "297\n", "output": "475867236\n" }, { "input": "123456\n", "output": "771773807\n" } ]
https://atcoder.jp/contests/arc159/tasks/arc159_a
Problem Statement You are given an N -by- N matrix A=(a_{i,j}) , where a_{i,j} \in \{0,1\} . We have the following directed graph. The graph has NK vertices numbered 1,2,\ldots,NK . The edges correspond to the NK -by- NK matrix X=(x_{i,j}) obtained by arranging K^2 copies of A in K rows and K columns (see Sample Input/Output 1 for an example). If x_{i,j}=1 , there is a directed edge from vertex i to vertex j ; if x_{i,j}=0 , that edge does not exist. Answer the following question for i=1,2,\ldots,Q . Find the shortest length (number of edges) of a path from vertex s_i to vertex t_i . If there is no such path, print -1 instead.
[ { "input": "3 2\n1 1 1\n1 1 0\n0 1 0\n4\n1 2\n1 4\n1 6\n6 3\n", "output": "1\n1\n1\n3\n" }, { "input": "4 1000000000\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n1\n1 4000000000\n", "output": "-1\n" } ]
https://atcoder.jp/contests/arc159/tasks/arc159_b
Problem Statement We have variables a and b . Initially, a=A and b=B . Takahashi will repeat the following operation while both a and b are greater than or equal to 1 . Let g be the greatest common divisor of a and b , and replace a and b with a-g and b-g , respectively. How many times will he perform the operation?
[ { "input": "15 9\n", "output": "2\n" }, { "input": "1 1\n", "output": "1\n" }, { "input": "12345678910 10987654321\n", "output": "36135\n" } ]
https://atcoder.jp/contests/arc159/tasks/arc159_c
Problem Statement You are given a sequence of positive integers: A=(a_1,\ldots,a_N) . Determine whether it is possible to make all elements of A equal by repeating the following operation between 0 and 10^4 times, inclusive. If it is possible, show one way to do so. Choose a permutation (p_1,\ldots,p_N) of (1,\ldots,N) , and replace A with (a_1+p_1,\ldots,a_N+p_N) .
[ { "input": "2\n15 9\n", "output": "Yes\n8\n1 2\n1 2\n1 2\n1 2\n2 1\n1 2\n1 2\n1 2\n" }, { "input": "5\n1 2 3 10 10\n", "output": "No\n" }, { "input": "4\n1 1 1 1\n", "output": "Yes\n0\n" } ]
https://atcoder.jp/contests/arc159/tasks/arc159_d
Problem Statement We have a sequence X , which is initially empty. Takahashi has performed the following operation for i=1,2,\ldots,N in this order. Append l_i,l_i+1,\ldots,r_i in this order to the end of X . Find the greatest length of a strictly increasing subsequence of the final X .
[ { "input": "4\n1 1\n2 4\n10 11\n7 10\n", "output": "8\n" }, { "input": "4\n1 1\n1 1\n1 1\n1 1\n", "output": "1\n" }, { "input": "1\n1 1000000000\n", "output": "1000000000\n" } ]
https://atcoder.jp/contests/arc159/tasks/arc159_e
Problem Statement You are given a positive integer N , and M pairs of positive integers: (a_0,b_0),\ldots,(a_{M-1},b_{M-1}) (note that a_i and b_i start with index 0 ). We have the following sequence of non-negative integers X=(x_1,\ldots,x_N) . x_i is determined as follows. Let l=1 , r=N , and t=0 . Let m=\left \lfloor \dfrac{a_{t \bmod M} \times l + b_{t \bmod M} \times r}{a_{t \bmod M} +b_{t \bmod M}} \right \rfloor ( \lfloor x \rfloor is the greatest integer not exceeding x ). If m=i , let x_i=t and terminate. If l \leq i \lt m , let r=m-1 ; otherwise, let l=m+1 . Increment t by 1 and return to step 2. Find \sum_{j=c_i}^{d_i-1} |x_j - x_{j+1}| for i=1,2,\ldots,Q . It can be proved that the answers are at most 10^{18} under the constraints of this problem.
[ { "input": "5 1\n1 1\n3\n1 2\n2 4\n3 5\n", "output": "1\n3\n2\n" }, { "input": "1000000000000000 2\n15 9\n9 15\n3\n100 10000\n5000 385723875\n150 17095708\n", "output": "19792\n771437738\n34191100\n" } ]
https://atcoder.jp/contests/arc159/tasks/arc159_f
Problem Statement A sequence X is called good when the following holds. X can be emptied by repeating the following operation zero or more times. Delete two adjacent elements x_i and x_{i+1} of X such that x_i \neq x_{i+1} . You are given a sequence with 2N elements: A=(a_1,\ldots,a_{2N}) . Among the 2^{2N-1} ways to divide A into one or more contiguous subsequences, how many are such that all those contiguous subsequences are good? Find the count modulo 998244353 .
[ { "input": "3\n1 1 2 3 4 5\n", "output": "2\n" }, { "input": "1\n1 2\n", "output": "1\n" }, { "input": "1\n1 1\n", "output": "0\n" }, { "input": "12\n4 2 17 12 18 15 17 4 22 6 9 20 21 16 23 16 13 2 20 15 16 3 7 15\n", "output": "2048\n" } ]
https://atcoder.jp/contests/abc296/tasks/abc296_a
Problem Statement There is a row of N people. They are described by a string S of length N . The i -th person from the front is male if the i -th character of S is M , and female if it is F . Determine whether the men and women are alternating. It is said that the men and women are alternating if and only if there is no position where two men or two women are adjacent.
[ { "input": "6\nMFMFMF\n", "output": "Yes\n" }, { "input": "9\nFMFMMFMFM\n", "output": "No\n" }, { "input": "1\nF\n", "output": "Yes\n" } ]
https://atcoder.jp/contests/abc296/tasks/abc296_b
Problem Statement Locate a piece on a chessboard. We have a grid with 8 rows and 8 columns of squares. Each of the squares has a 2 -character name determined as follows. The first character of the name of a square in the 1 -st column from the left is a . Similarly, the first character of the name of a square in the 2 -nd, 3 -rd, \ldots , 8 -th column from the left is b , c , d , e , f , g , h , respectively. The second character of the name of a square in the 1 -st row from the bottom is 1 . Similarly, the second character of the name of a square in the 2 -nd, 3 -rd, \ldots , 8 -th row from the bottom is 2 , 3 , 4 , 5 , 6 , 7 , 8 , respectively. For instance, the bottom-left square is named a1 , the bottom-right square is named h1 , and the top-right square is named h8 . You are given 8 strings S_1,\ldots,S_8 , each of length 8 , representing the state of the grid. The j -th character of S_i is * if the square at the i -th row from the top and j -th column from the left has a piece on it, and . otherwise. The character * occurs exactly once among S_1,\ldots,S_8 . Find the name of the square that has a piece on it.
[ { "input": "........\n........\n........\n........\n........\n........\n........\n*.......\n", "output": "a1\n" }, { "input": "........\n........\n........\n........\n........\n.*......\n........\n........\n", "output": "b3\n" } ]
https://atcoder.jp/contests/abc296/tasks/abc296_c
Problem Statement You are given a sequence of N numbers: A=(A_1,\ldots,A_N) . Determine whether there is a pair (i,j) with 1\leq i,j \leq N such that A_i-A_j=X .
[ { "input": "6 5\n3 1 4 1 5 9\n", "output": "Yes\n" }, { "input": "6 -4\n-2 -7 -1 -8 -2 -8\n", "output": "No\n" }, { "input": "2 0\n141421356 17320508\n", "output": "Yes\n" } ]
https://atcoder.jp/contests/abc296/tasks/abc296_d
Problem Statement You are given positive integers N and M . Find the smallest positive integer X that satisfies both of the conditions below, or print -1 if there is no such integer. X can be represented as the product of two integers a and b between 1 and N , inclusive. Here, a and b may be the same. X is at least M .
[ { "input": "5 7\n", "output": "8\n" }, { "input": "2 5\n", "output": "-1\n" }, { "input": "100000 10000000000\n", "output": "10000000000\n" } ]
https://atcoder.jp/contests/abc296/tasks/abc296_e
Problem Statement You are given a sequence of N numbers: A=(A_1,A_2,\ldots,A_N) . Here, each A_i (1\leq i\leq N) satisfies 1\leq A_i \leq N . Takahashi and Aoki will play N rounds of a game. For each i=1,2,\ldots,N , the i -th game will be played as follows. Aoki specifies a positive integer K_i . After knowing K_i Aoki has specified, Takahashi chooses an integer S_i between 1 and N , inclusive, and writes it on a blackboard. Repeat the following K_i times. Replace the integer x written on the blackboard with A_x . If i is written on the blackboard after the K_i iterations, Takahashi wins the i -th round; otherwise, Aoki wins. Here, K_i and S_i can be chosen independently for each i=1,2,\ldots,N . Find the number of rounds Takahashi wins if both players play optimally to win.
[ { "input": "3\n2 2 3\n", "output": "2\n" }, { "input": "2\n2 1\n", "output": "2\n" } ]
https://atcoder.jp/contests/abc296/tasks/abc296_f
Problem Statement You are given two sequences of N numbers: A=(A_1,A_2,\ldots,A_N) and B=(B_1,B_2,\ldots,B_N) . Takahashi can repeat the following operation any number of times (possibly zero). Choose three pairwise distinct integers i , j , and k between 1 and N . Swap the i -th and j -th elements of A , and swap the i -th and k -th elements of B . If there is a way for Takahashi to repeat the operation to make A and B equal, print Yes ; otherwise, print No . Here, A and B are said to be equal when, for every 1\leq i\leq N , the i -th element of A and that of B are equal.
[ { "input": "3\n1 2 1\n1 1 2\n", "output": "Yes\n" }, { "input": "3\n1 2 2\n1 1 2\n", "output": "No\n" }, { "input": "5\n1 2 3 2 1\n3 2 2 1 1\n", "output": "Yes\n" }, { "input": "8\n1 2 3 4 5 6 7 8\n7 8 5 6 4 3 1 2\n", "output": "No\n" } ]
https://atcoder.jp/contests/abc296/tasks/abc296_g
Problem Statement There is a convex N -gon S in the two-dimensional coordinate plane where the positive x -axis points to the right and the positive y -axis points upward. The vertices of S have the coordinates (X_1,Y_1),\ldots,(X_N,Y_N) in counter-clockwise order. For each of Q points (A_i,B_i) , answer the following question: is that point inside or outside or on the boundary of S ?
[ { "input": "4\n0 4\n-2 2\n-1 0\n3 1\n3\n-1 3\n0 2\n2 0\n", "output": "ON\nIN\nOUT\n" }, { "input": "3\n0 0\n1 0\n0 1\n3\n0 0\n1 0\n0 1\n", "output": "ON\nON\nON\n" } ]
https://atcoder.jp/contests/abc296/tasks/abc296_h
Problem Statement We have a grid with N rows and M columns, where each square is painted black or white. Here, at least one square is painted black. The initial state of the grid is given as N strings S_1,S_2,\ldots,S_N of length M . The square at the i -th row from the top and j -th column from the left is painted black if the j -th character of S_i is # , and white if it is . . Takahashi wants to repaint some white squares (possibly zero) black so that the squares painted black are connected . Find the minimum number of squares he needs to repaint to achieve his objective. Here, the squares painted black are said to be connected when, for every pair of squares (S,T) painted black, there are a positive integer K and a sequence of K squares X=(x_1,x_2,\ldots,x_K) painted black such that x_1=S , x_K=T , and x_i and x_{i+1} share a side for every 1\leq i\leq K-1 . It can be proved that, under the constraints of the problem, there is always a way for Takahashi to achieve his objective.
[ { "input": "3 5\n...#.\n.#...\n....#\n", "output": "3\n" }, { "input": "3 3\n###\n###\n###\n", "output": "0\n" }, { "input": "10 1\n.\n#\n.\n.\n.\n.\n.\n.\n#\n.\n", "output": "6\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day3/tasks/pakencamp_2022_day3_a
問題文 2N+1 行、 2N+1 列のマス目がありたす。䞊から i 行目、巊から j 列目のマス目のこずをマス (i,j) ず呌ぶこずにしたす。 たた、 (2N+1) \times (2N+1) 敎数行列 A が䞎えられたす。 ここで、マス (1,N+1),(2N+1,N+1) を陀く各マスには壁を蚭眮するこずができ、マス (i,j) に壁を蚭眮する コスト は A_{i,j} です。 いた、マス (1,N+1) に駒が眮かれおいたす。この駒は今いるマス目ず䞊䞋巊右に隣接し、か぀壁が眮かれおいないマス目に移動するずいう操䜜を任意の回数行うこずができたす。 厳密には、マス (x,y) に駒が眮かれおいる時、マス (x',y') であっお次の条件を党お満たすマスに移動するこずができたす。 1 \le x',y' \le 2N+1 マス (x',y') に壁が蚭眮されおいない。 |x-x'|+|y-y'| = 1 あなたは、壁をいく぀か配眮するこずで駒がどんな動きをしたずしおもマス (2N+1,N+1) に到達できないようにしたいです。このずき、配眮した壁のコストの和ずしお考えられる最小倀を求めおください。
[ { "input": "1\n1 -1 5\n2 4 6\n3 -1 7\n", "output": "10\n" }, { "input": "2\n8 1 -1 5 5\n7 4 9 9 3\n3 2 1 1 8\n4 4 3 3 4\n3 5 -1 5 8\n", "output": "10\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day3/tasks/pakencamp_2022_day3_b
問題文 長さ N の正敎数列 A が䞎えられたす。これからこの数列に以䞋の M 回の凊理を行いたす。 i\ (1 \le i \le M) 回目の凊理 : 1 \le j \le N を満たす敎数 j を遞ぶ。 A_j を \max(A_j,B_i) に眮き換える。 凊理の結果できる A ずしお考えられるものの個数を 998244353 で割った䜙りを求めおください。
[ { "input": "6 1\n1 2 3 4 5 6\n5\n", "output": "5\n" }, { "input": "3 3\n1 1 1\n1 1 1\n", "output": "1\n" }, { "input": "6 6\n1 2 3 4 5 6\n1 2 3 4 5 6\n", "output": "203\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day3/tasks/pakencamp_2022_day3_c
問題文 この問題においおは、「 1 番目の文字」で文字 a を、「 2 番目の文字」で文字 b を、...、「 26 番目の文字」で文字 z を指すものずしたす。 英小文字からなる文字列 S が䞎えられたす。 あなたは 1\leq L\leq R\leq |S| ずなる敎数 L, R および (1,2,\ldots,26) の順列 (p_1,p_2,\ldots,p_{26}) を遞びたす。その埌、以䞋の手順で新たな文字列 T を䜜りたす。 S' を S の L 文字目から R 文字目を取り出しおできる文字列ずする。 1 以䞊 26 以䞋の党おの敎数 i に぀いお、 S' に含たれる「 i 番目の文字」を「 p_i 番目の文字」で眮き換える。この操䜜は党おの i に察しお同時に行う。この結果できる文字列を T ずする。 T ずしお考えられる文字列のうち蟞曞順で 最倧 のものを求めおください。
[ { "input": "abcba\n", "output": "zyzx\n" }, { "input": "nolemonnomelon\n", "output": "zzyxwvyz\n" }, { "input": "hhhhhqqqhhhhhjjhhhhhpppp\n", "output": "zzzzzyyzzzzzxxxx\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day3/tasks/pakencamp_2022_day3_d
問題文 ここに N 個の箱があり、箱 i には A_i 個のボヌルが入っおいたす。あなたは、以䞋の操䜜を 0 回以䞊 2 \times 10^6 回以䞋行うこずができたす。 敎数察 (X,Y)(X \neq Y) であっお、「箱 X に入っおいるボヌルの個数」が「箱 Y に入っおいるボヌルの個数」以䞋であるものを遞ぶ。 そしお、箱 X に入っおいるボヌルの数だけ箱 Y から箱 X にボヌルを移す。 䞀぀の箱にすべおのボヌルが入っおいる状態にできるか刀定し、可胜ならば操䜜列を䞀぀構築しおください。
[ { "input": "3\n1 3 4\n", "output": "Yes\n3\n1 2\n2 1\n3 2\n" }, { "input": "4\n7 2 3 1\n", "output": "No\n" }, { "input": "5\n2 6 2 1 5\n", "output": "Yes\n6\n4 5\n1 2\n4 3\n2 1\n5 4\n5 2\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day3/tasks/pakencamp_2022_day3_e
問題文 1 以䞊 2 \times 10^6 以䞋の敎数からなる長さ 10^5 の数列の組 (A,B) であっお、以䞋の条件を満たすものを䞀぀構築しおください。 1 以䞊 2 \times 10^6 以䞋のすべおの敎数 k に぀いお、 A_i\times B_j=k を満たす敎数察 (i,j) が存圚する。
[]
https://atcoder.jp/contests/pakencamp-2022-day3/tasks/pakencamp_2022_day3_f
問題文 0 以䞊 2^M 未満の敎数からなる長さ N の数列 A=(A_0,A_1,\ldots,A_{N-1}) であっお、以䞋の条件を満たすものの個数を 998244353 で割った䜙りを求めおください。 すべおの敎数 i\ (0 \le i \le N-1) に぀いお、 A_i \oplus A_{(i+1) \bmod N} \neq T_i か぀ A_i \oplus A_{(i+1) \bmod N} \neq U_i が成り立぀ ただし \oplus はビット単䜍 \mathrm{XOR} 挔算を衚したす。 ビット単䜍 \mathrm{XOR} 挔算ずは 非負敎数 A,B のビット単䜍 \mathrm{XOR} 挔算、 A\oplus B は、以䞋のように定矩されたす。 A\oplus B を二進衚蚘した際の 2^k\ (k\geq 0) の䜍の数は、 A,B を二進衚蚘した際の 2^k の䜍の数のうち䞀方のみが 1 であれば 1 、そうでなければ 0 である。 䟋えば、 3\oplus 5 = 6 ずなりたす二進衚蚘するず: 011\oplus 101 = 110 )。
[ { "input": "3 2\n0 1\n1 2\n2 3\n", "output": "8\n" }, { "input": "3 1\n0 1\n0 1\n0 1\n", "output": "0\n" }, { "input": "5 10\n31 415\n92 653\n58 979\n32 384\n62 643\n", "output": "552613140\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day3/tasks/pakencamp_2022_day3_g
問題文 N\times N 敎数行列 A が䞎えられたす。以䞋の条件を満たす N \times N 敎数行列 B が存圚するか刀定しおください。 すべおの敎数察 (i,j) に぀いお、 A_{i,j} \neq -1 ならば A_{i,j}=B_{i,j} B のどの 4 \times 4 の郚分正方圢を取り出しおも、その郚分正方圢は 1 以䞊 16 以䞋の敎数をすべお含む。
[ { "input": "4\n1 2 -1 -1\n5 -1 7 -1\n9 10 -1 -1\n13 14 15 -1\n", "output": "Yes\n" }, { "input": "5\n1 -1 -1 -1 -1\n-1 -1 -1 -1 -1\n-1 -1 -1 -1 -1\n-1 -1 -1 1 -1\n-1 -1 -1 -1 -1\n", "output": "No\n" }, { "input": "6\n1 -1 -1 -1 -1 1\n-1 -1 -1 -1 -1 -1\n-1 -1 -1 -1 -1 -1\n-1 -1 -1 -1 -1 -1\n-1 -1 -1 -1 -1 -1\n-1 -1 -1 -1 -1 -1\n", "output": "No\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day3/tasks/pakencamp_2022_day3_h
問題文 想定解に誀りが芋぀かったため、正しい解法に差し替えたうえで問題の制玄を倉曎したした。( M \le 10^{12} から M \le 10^9 )圱響を受けた方、申し蚳ありたせん。(15:29) 正敎数 N,M が䞎えられたす。 0\leq K\leq N-1 を満たす党おの K に察しお、次の倀を 998244353 で割った䜙りを求めおください。 1 以䞊 M 以䞋の敎数のみからなる長さ N の数列 A=(A_1,A_2,\ldots,A_N) であっお、 A_i \times A_{i+1} が平方数であるような i\,(1\leq i\leq N-1) がちょうど K 個存圚するようなものの総数
[ { "input": "3 2\n", "output": "2\n4\n2\n" }, { "input": "7 9\n", "output": "1236360\n1801104\n1168800\n444960\n111630\n17796\n2319\n" }, { "input": "10 1000000000\n", "output": "463421383\n80897715\n609130572\n681545366\n345958046\n718253740\n76864047\n286280738\n642996694\n527041309\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day3/tasks/pakencamp_2022_day3_i
問題文 長さ N の非負敎数列 A=(A_1,A_2,\ldots,A_N) が䞎えられたす。 A の芁玠を自由に䞊び替えるこずができるずき、以䞋の倀ずしおありうる最小倀を求めおください。 \displaystyle \sum_{k=1}^N(A_1\lor A_2\lor\cdots\lor A_k) ただし \lor はビット単䜍 \operatorname{OR} 挔算を衚したす。 ビット単䜍 \operatorname{OR} 挔算ずは 非負敎数 A,B に察するビット単䜍 \operatorname{OR} 挔算 A\lor B は、以䞋のように定矩されたす。 A\lor B を二進衚蚘した際の 2^k\,(k\geq0) の䜍の数は、 A,B を二進衚蚘した際の 2^k の䜍の数のうち少なくずも䞀方が 1 であれば 1 、そうでなければ 0 である。 䟋えば、 3\lor5=7 ずなりたす二進衚蚘するず 010\lor101=111 。
[ { "input": "3\n4 1 2\n", "output": "11\n" }, { "input": "3\n2 5 6\n", "output": "15\n" }, { "input": "4\n13 13 13 13\n", "output": "52\n" }, { "input": "6\n11 15 10 9 4 12\n", "output": "74\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day3/tasks/pakencamp_2022_day3_j
問題文 長さ N の敎数列 A が䞎えられたす。長さ N の順列 P が以䞋の条件を満たすずき、 \displaystyle \max_{1 \le i,j \le N} |iP_i-jP_j| ずしおありうる最小の倀を求めおください。 すべおの i に぀いお、 A_i \neq -1 ならば A_i = P_i
[ { "input": "3\n-1 -1 2\n", "output": "4\n" }, { "input": "1\n-1\n", "output": "0\n" }, { "input": "5\n2 4 5 1 3\n", "output": "13\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day3/tasks/pakencamp_2022_day3_k
問題文 長さ N の敎数列 A = (A_0, A_1, \ldots, A_{N-1}),B=(B_1,B_2,\ldots,B_N) 、長さ M の敎数列 C=(C_0,C_1,\ldots,C_{M-1}),D=(D_1,D_2,\ldots,D_M) ず非負敎数 K が䞎えられたす。 ここで、長さ K+1 の数列 F=(F_0,F_1,\ldots,F_K),G=(G_0,G_1,\ldots,G_K) を以䞋で定矩したす。 F_i = A_i\ (0 \le i < N) \displaystyle F_i = \sum_{k=1}^N B_kF_{i-k}\ (N \le i \le K) G_j = C_j\ (0 \le j < M) \displaystyle G_j = \sum_{k=1}^M D_kG_{j-k}\ (M \le j \le K) \displaystyle \sum_{i=0}^K F_iG_i を 998244353 で割った䜙りを求めおください。
[ { "input": "2 2 5\n0 1\n1 1\n0 1\n1 1\n", "output": "40\n" }, { "input": "3 2 1\n1 2 3\n4 5 6\n7 8\n9 10\n", "output": "23\n" }, { "input": "6 7 924844033\n3 1 4 1 5 9\n2 6 5 3 5 8\n9 7 9 3 2 3 8\n4 6 2 6 4 3 3\n", "output": "142556085\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day3/tasks/pakencamp_2022_day3_l
問題文 0 以䞊 2^M 未満の敎数からなる長さ N の数列 A すべおに぀いお以䞋の倀を考え、その総和を 998244353 で割った䜙りを求めおください。 (A_1\times A_2) \oplus (A_2\times A_3) \oplus \ldots \oplus (A_{N-1}\times A_N) を 2^M で割った䜙り ただし \oplus はビット単䜍 \mathrm{XOR} 挔算を衚したす。 ビット単䜍 \mathrm{XOR} 挔算ずは 非負敎数 A,B のビット単䜍 \mathrm{XOR} 挔算、 A\oplus B は、以䞋のように定矩されたす。 A\oplus B を二進衚蚘した際の 2^k\ (k\geq 0) の䜍の数は、 A,B を二進衚蚘した際の 2^k の䜍の数のうち䞀方のみが 1 であれば 1 、そうでなければ 0 である。 䟋えば、 3\oplus 5 = 6 ずなりたす二進衚蚘するず: 011\oplus 101 = 110 )。
[ { "input": "2 1\n", "output": "1\n" }, { "input": "2 2\n", "output": "16\n" }, { "input": "314 159\n", "output": "856758166\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day2/tasks/pakencamp_2022_day2_a
問題文 パ研王囜は N 個の街ずそれを繋ぐ M 本の道路からなりたす。街には暙高が高い順に 1, 2, \ldots, N の番号が぀いおおり、党おの道路は暙高が高い街から䜎い街ぞ䞀方向に移動するこずができたす。具䜓的には、 i 番目の道路は街 A_i から街 B_i ぞの䞀方向に移動するこずができたす (A_i < B_i) 。 さお、今幎もパ研合宿が開催されたす。今幎のパ研合宿ではいく぀かの街に䌚堎を蚭け、パ研王囜のどの街からでも 0 本以䞊の道路を通るこずで䌚堎のある街に移動できるようにしたす。 しかし、予算に制玄があるため、䌚堎を蚭ける街をできるだけ少なくしたいです。このずき、䌚堎を蚭ける街の集合を䞀぀定めお䞋さい。
[ { "input": "4 4\n1 2\n1 3\n1 4\n2 4\n", "output": "2\n3 4\n" }, { "input": "10 20\n4 6\n7 9\n1 8\n4 7\n2 3\n5 6\n5 8\n8 10\n8 9\n3 4\n7 8\n3 6\n1 2\n4 9\n8 10\n6 10\n5 9\n2 7\n6 10\n1 8\n", "output": "2\n9 10\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day2/tasks/pakencamp_2022_day2_b
問題文 パ研孊園では各幎床に N 個の孊期があり、 1, 2, \ldots, N の番号が付いおいたす。各孊期ごずに成瞟が付き、成瞟は 1 から N たでの敎数倀で衚されたす。 さお、パ研孊園の生埒であるあなたは、䞀幎の最埌に自分の各孊期の成瞟衚を眺めおいたした。するず、次のこずに気が付きたした。ここで i 番目の孊期の成瞟を G_i ずしたす。 G は長さ N の順列である。 2 孊期以降に぀いお、前の孊期より成瞟が䞋がっおいる孊期が存圚し、それらは連続する 1 ぀の区間になっおいる。 このずき、これらの条件を満たす成瞟 G_1, G_2, \ldots, G_N の個数を求めお䞋さい。ただし、答えはずおも倧きくなるこずがあるので、答えを 998244353 で割った䜙りを出力しおください。
[ { "input": "4\n", "output": "18\n" }, { "input": "13\n", "output": "398574\n" }, { "input": "2947\n", "output": "663703367\n" }, { "input": "999999\n", "output": "946508973\n" }, { "input": "1000000000000000000\n", "output": "856673235\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day2/tasks/pakencamp_2022_day2_c
問題文 パ研町にはパ研矎術通ずこたば矎術通がありたす。それぞれの矎術通には N 個の矎術品が䞀列に展瀺されおおり、パ研矎術通の巊から i 番目の矎術品の䟡倀は A_i で、こたば矎術通の巊から i 番目の矎術品の䟡倀は B_i です。来堎者をより楜したせるために、矎術品は巊から右に向かっお䟡倀の䜎い物から高い物の順に䞊んでいたす。 パ研矎術通ずこたば矎術通はコラボ展瀺をするこずになりたした。コラボには Q 個のプランがあり、 i 番目のプランでは、パ研矎術通の巊から L_i 番目から R_i 番目の矎術品ず、こたば矎術通の巊から X_i 番目から Y_i 番目の矎術品を展瀺したす。コラボ展瀺でも同様に、矎術品は巊から右に向かっお䟡倀の䜎い物から高い物の順に䞊べたす。 ある展瀺の満足床を、隣り合う矎術品の䟡倀の差の最小倀ずしたしょう。各 i \ (1 \leq i \leq Q) に぀いお、 i 番目のプランで展瀺を行った際の満足床を求めおください。
[ { "input": "6\n1 3 6 8 9 11\n2 2 4 7 10 11\n4\n1 6 2 5\n1 6 5 5\n2 5 5 5\n4 6 3 6\n", "output": "1\n1\n1\n0\n" }, { "input": "10\n4 14 18 31 47 51 59 72 78 96\n2 23 25 36 58 60 63 85 88 97\n7\n1 3 5 5\n6 10 3 4\n8 10 1 4\n7 10 2 4\n6 7 1 3\n2 4 6 9\n2 3 2 9\n", "output": "4\n6\n2\n2\n2\n3\n2\n" }, { "input": "15\n1141 1334 2083 2504 4031 4238 4328 5260 5315 6427 6991 8127 8199 8356 9136\n189 403 1035 1126 1165 1477 2021 2100 2528 3892 4108 4459 5792 7050 8709\n8\n4 7 7 15\n8 11 8 11\n7 11 2 15\n2 13 6 11\n11 12 3 10\n8 10 3 12\n3 11 3 10\n3 11 8 10\n", "output": "24\n55\n39\n17\n39\n39\n17\n17\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day2/tasks/pakencamp_2022_day2_d
問題文 0 ず 1 からなる長さ N の文字列であっお 1 以䞊 M 以䞋の党おの敎数 i に぀いお次の条件を満たすものの個数を 998244353 で割った䜙りを求めおください。 L_i 文字目から R_i 文字目の間に文字 C_i が存圚する
[ { "input": "3 2\n1 3 0\n1 2 1\n", "output": "5\n" }, { "input": "5 4\n1 2 0\n2 3 0\n3 4 0\n4 5 0\n", "output": "13\n" }, { "input": "7 9\n4 6 0\n1 2 0\n5 7 1\n3 4 0\n7 7 0\n6 7 1\n2 5 1\n3 5 0\n1 7 1\n", "output": "13\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day2/tasks/pakencamp_2022_day2_e
問題文 パ研楜団には、 N 人の挔奏者がいたす。たた、パ研楜団は M 皮類の楜噚を所有しおいたす。 i 人目の挔奏者の熟緎床は A_i で担圓楜噚は B_i 、楜噚の移行コストは C_i です。 i 人目の挔奏者は C_i の移行コストで担圓の楜噚を奜きなものに倉曎するこずができたす。 楜団の調和床を以䞋のように定矩したす。 M 皮類の楜噚それぞれに぀いお、その楜噚を担圓する人の熟緎床の最倧倀を考える。この M 個の倀の最小倀が調和床である。ただし、担圓する人がいない楜噚が存圚する堎合、調和床は 0 である。 x = X_1, X_2, \ldots ,X_Q に぀いお、移行コストの総和が x を超えずに実珟できる最倧の調和床を求めおください。
[ { "input": "5 3\n7 1 4\n10 3 3\n8 3 3\n9 3 5\n7 1 4\n6\n2 9 12 15 25 46\n", "output": "0\n8\n8\n8\n8\n8\n" }, { "input": "10 6\n10 3 4\n12 2 5\n2 1 3\n7 3 15\n12 1 10\n8 3 3\n11 3 14\n13 6 9\n3 3 12\n4 5 1\n10\n1 3 7 37 53 85 88 91 94 148\n", "output": "0\n4\n8\n8\n8\n8\n8\n8\n8\n8\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day2/tasks/pakencamp_2022_day2_f
問題文 パ研君は、次の問題を解いおいたす。 N 頂点の朚が䞎えられる。頂点には 1, 2, \ldots, N の番号が付けられおいる。 各頂点に぀いお、その頂点から最も離れた頂点を䞀぀求めよ。耇数ある堎合はどれを求めおもいい。 パ研君はこの問題を解き、 1 \leq i \leq N に぀いお頂点 i から最も離れた頂点のうち䞀぀が頂点 A_i であるず分かりたした。 しかしなんずいう事でしょう、せっかく問題を解けたのに、元の朚や A の䞀郚の倀が分からなくなっおしたいたした。あなたはパ研君のために、数列 A を埩元しおあげたいず考えたした。 今あなたには長さ N の数列 B が䞎えられたす。この数列は残っおいる A の情報を衚しおいたす。このずき、次の党おの条件を満たす長さ N の数列 A を考えたす。 B_i \neq -1 ならば A_i = B_i である ある朚が存圚し、 1 \leq i \leq N に぀いお、頂点 i から最も離れた頂点の䞀぀が頂点 A_i である このような条件を満たす A がいく぀あるか、パ研君に教えおあげおください。ただし、パ研君が問題を解き間違えおいた可胜性があるため、条件を満たす A が存圚しない堎合もありたす。 なお、答えは倧きくなる堎合があるので、代わりに答えを 998244353 で割ったあたりを求めお䞋さい。
[ { "input": "3\n2 -1 -1\n", "output": "3\n" }, { "input": "8\n-1 8 7 5 -1 1 2 3\n", "output": "47\n" }, { "input": "4\n1 2 3 4\n", "output": "0\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day2/tasks/pakencamp_2022_day2_g
問題文 この問題はむンタラクティブな問題です。 パ研町には N 戞の家があり、 1, 2, \ldots, N の番号が付いおいたす。たた家ず家を双方向に結ぶ 0 本以䞊の道路があり、道路は 300 本以䞋であるこずが分かっおいたすが、䜕本あるかや、それぞれの道路がどの家ずどの家を結んでいるかは分かりたせん。ただし、䞡端が同じ家を結んでいる道路はなく、耇数の道路が同じ家の組を結んでいるこずもありたせん。 パ研町の町長ずなったあなたは、これから毎日 1 回ず぀集䌚を開きたす。 N 戞の家それぞれに぀いお、その家の䜏人を招埅するかしないか決めるこずができたす。 しかし、パ研町は非垞に険悪な町です。特に隣人、぀たり 1 ぀の道路で結ばれた 2 ぀の家に䜏む䜏人は、集䌚で顔を合わせるだけで集䌚の党員を巻き蟌んだ乱闘が起きおしたいたす。 さお、あなたはパ研町の道路の本数や、それぞれの道路がどの家ずどの家を結んでいるかを調べたいず思いたした。そこで、各日に集䌚に招埅する人を工倫し、乱闘が起きるかどうかを調べるこずで、パ研町の構造を調べるこずにしたした。 これは明らかに職暩濫甚なので、 3200 日間たでしか集䌚を開けたせん。集䌚に招埅する人を決める戊略を定めるプログラムを䜜成しおください。 なお、あなた自身は誰ずも隣人でないものずしたす。たた、誰も呌ばずに䞀人で集䌚を行うこずもできたす。
[]
https://atcoder.jp/contests/pakencamp-2022-day2/tasks/pakencamp_2022_day2_h
問題文 パ研郚員が䞀列に N 人䞊んでいたす。巊から i \ (1 \leq i \leq N) 番目の郚員を郚員 i ず呌ぶこずにしたす。珟圚の郚員 i のパ゜コン力は A_i です。隣り合う郚員同士には仲良し床ずいう指暙が存圚し、 1 \leq i \leq N - 1 に぀いお珟圚の郚員 i ず郚員 i + 1 の仲良し床は B_i です。ここで B_i は盞異なりたす。 パ研郚員は掟閥争いが倧奜きです。普段は掟閥争いが犁止されおいたすが、䞇が䞀掟閥争いが起こっおしたったずきのこずを考えおみたしょう。 人 l, l + 1, \cdots , r \ (1 \leq l \leq r \leq N) が掟閥争いを始めるず、以䞋の事柄が起こりたす。 はじめ、掟閥 l, l + 1, \cdots ,r が存圚し、郚員 i \ (l \leq i \leq r) は掟閥 i に属する。それ以倖の郚員は掟閥に属さない。 掟閥 a, b \ (l \leq a < b \leq r) の仲良し床は、以䞋のように定矩される。 どの時点においおも、掟閥 a に人 i が所属し、掟閥 b に人 i+1 が所属しおいるような i は高々 1 ぀存圚するこずが蚌明できる。仲良し床は、このような i が存圚しない堎合 -10^{100} であり、存圚する堎合 B_i である。 掟閥が 1 ぀になるたで、以䞋のむベントが繰り返される。 仲良し床が最倧である 2 掟閥 a, b \ (a < b) を考える。各掟閥に所属する人のパ゜コン力の総和を s, t ずする。このずき s \geq t なら掟閥 b に所属する人が党員掟閥 a に所属を倉え、 s < t なら掟閥 a に所属する人が党員掟閥 b に所属を倉える。たた、所属する人がいなくなった掟閥は消滅する。 パ研の掻動は明日からの Q 日の間掻発なので、郚員のパ゜コン力や仲良し床は頻繁に倉わりたす。具䜓的には、 j \ (1 \leq j \leq Q) 日目には以䞋の倉化が起こりたす。 T_j = 1 の堎合、郚員 P_j のパ゜コン力が V_j になる。 T_j = 2 の堎合、郚員 P_j ず郚員 P_j + 1 の間の仲良し床が V_j になる。 ただし、いずれの時点においおも郚員同士の仲良し床は盞異なるこずが保蚌されたす。 掟閥争いを想像するのが倧奜きな掟閥倪郎君は、毎日掟閥争いの思考実隓を行いたす。具䜓的には、 j \ (1 \leq j \leq Q) 日目の終わりには、郚員 L_j, L_j + 1, \cdots ,R_j が掟閥争いを始めたずき最埌に残る掟閥を求めたす。 各思考実隓の結果を求めおください。
[ { "input": "5\n5 9 9 7 9\n6 7 3 4\n3\n1 4 7 4 5\n2 4 10 1 3\n1 1 6 1 3\n", "output": "5\n2\n2\n" }, { "input": "8\n14 10 12 20 18 1 6 13\n2 16 1 3 4 12 11\n5\n1 2 3 1 8\n1 3 8 1 8\n1 1 4 1 8\n1 7 15 1 8\n1 4 19 1 8\n", "output": "8\n8\n8\n7\n7\n" }, { "input": "8\n10 17 13 17 16 1 19 8\n11 1 9 4 2 12 10\n5\n2 2 7 1 8\n2 4 15 1 8\n2 3 5 1 8\n2 2 13 1 8\n2 6 14 1 8\n", "output": "4\n4\n2\n2\n2\n" }, { "input": "8\n11 11 2 2 10 5 2 15\n9 1 10 19 7 14 20\n3\n1 1 11 8 8\n1 1 11 4 6\n1 1 11 2 6\n", "output": "8\n5\n5\n" }, { "input": "20\n30 95 72 93 69 100 45 51 7 19 40 61 73 51 98 71 25 51 15 91\n20 69 94 58 63 70 80 17 66 10 83 48 40 86 23 67 60 44 96\n6\n2 9 71 6 15\n1 2 46 11 15\n1 6 15 6 19\n2 8 8 1 13\n2 8 26 6 18\n1 9 31 5 14\n", "output": "12\n12\n12\n4\n12\n8\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day1/tasks/pakencamp_2022_day1_a
問題文 座暙平面䞊の点 P(X, Y) を、次の操䜜を繰り返しお原点に動かしたいです。 ある実数 a を遞び、 P の珟圚の座暙を (x, y) ずしお、 P を (x, a) たたは (a, y) に動かす。 必芁な最小の操䜜回数を求めおください。
[ { "input": "6 4\n", "output": "2\n" }, { "input": "0 1\n", "output": "1\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day1/tasks/pakencamp_2022_day1_b
問題文 XXXX 幎のパ研合宿には N 人の参加者がいたした。 パ゜コンを充電するために 1 人 1 ぀のコンセントが必芁です。しかし、䌚堎には䜿甚可胜なコンセントが 1 ぀しかありたせんでした。そのため、運営は M 個口の電源タップをいく぀か甚意するこずにしたした。 M 個口の電源タップ 1 ぀を未䜿甚の䜿甚可胜なコンセント 1 ぀に挿すこずで、新たに䜿甚可胜なコンセントが M 個できたす。 参加者 1 人に぀き最䜎でも 1 ぀䜿甚可胜なコンセントがあるようにするには最䜎で䜕個の電源タップを甚意する必芁がありたすか ただし、 1 人 1 ぀のコンセントを甚意するこずが䞍可胜である堎合はそのこずを報告しおください。
[ { "input": "5 5\n", "output": "1\n" }, { "input": "30 1\n", "output": "-1\n" }, { "input": "998244353 3\n", "output": "499122176\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day1/tasks/pakencamp_2022_day1_c
問題文 英小文字ず ? のみからなる文字列 S が䞎えられたす。 文字列 S に含たれる ? をそれぞれ奜きな英小文字に眮き換えるこずで䜜れる回文は䜕通りあるか求めおください。 ただし、答えは非垞に倧きくなる可胜性があるので、 998244353 で割ったあたりを出力しおください。 回文の定矩 文字列 T が回文であるずは、 1 \leq i \leq | T | を満たすすべおの敎数 i に぀いお、 T の前から i 文字目の文字ず埌ろから i 文字目の文字が同じ文字であるこずをいいたす。
[ { "input": "a??\n", "output": "26\n" }, { "input": "atcoder\n", "output": "0\n" }, { "input": "?????????????\n", "output": "45855352\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day1/tasks/pakencamp_2022_day1_d
問題文 N 個の怅子が暪䞀列に䞊んでいたす。これらの怅子に K 人の人を座らせたす。 1 ぀の怅子に座る人は高々 1 人である必芁がありたす。このずき次の条件を満たす i \,(2 \leq i \leq N-1) の個数を最小化したいです。 巊から i-1 , i , i+1 番目の怅子党おに人が座っおいる このような座り方を䞀぀芋぀けおください。
[ { "input": "6 4\n", "output": "1 2 4 6 \n" }, { "input": "6 5\n", "output": "1 2 4 5 6 \n" } ]
https://atcoder.jp/contests/pakencamp-2022-day1/tasks/pakencamp_2022_day1_e
問題文 瞊の長さが H 、暪の長さが W の長方圢の䞭に半埄の長さが等しい 2 ぀の円板を入れるこずを考えたす。このずき、円板が長方圢からはみ出したり、 2 ぀の円板の共通郚分の面積が 0 より倧きくなったりしおはいけたせん。このずき、円板の半埄の最倧倀を解答しおください。
[ { "input": "6 4\n", "output": "1.535898384862246\n" }, { "input": "4 1\n", "output": "0.500000000000000\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day1/tasks/pakencamp_2022_day1_f
問題文 パ研合宿の K 運営長はある䞀日に開催するむベントを䜕にするか決めたいです。 珟圚 N 個のむベントの候補があり、パ研合宿の参加者は M 人です。 i 個目のむベントの時間の長さは L_i です。たた、 i 個目のむベントに察する j 人目の参加者の満足床は A_{i,j} です。 1 個以䞊の行うむベントを定めたずき、そのむベントの集合に察する良さは党おの人に察しおのむベントの満足床の最倧倀の総和ず定矩されたす。 䞀日の時間は限られおいるため、むベントの時間の長さの合蚈は D 以䞋である必芁がありたす。このずき、行うむベントの集合の良さは最倧でいく぀になりたすか ただし、むベントを䞀個も行うこずができない堎合はそのこずを報告しおください。
[ { "input": "2 4 10\n5 6\n1 2 3 4\n5 6 7 8\n", "output": "26\n" }, { "input": "1 4 10\n11\n3 6 1 8\n", "output": "-1\n" }, { "input": "4 8 100\n30 40 50 110\n3 1 4 1 5 9 2 6\n5 3 5 8 9 7 9 3\n2 3 8 4 6 2 6 4\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n", "output": "54\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day1/tasks/pakencamp_2022_day1_g
問題文 N 頂点の根付き朚が䞎えられたす。頂点には 1 から N たでの番号が振られおおり、根は頂点 1 で、頂点 i\,(2\leq i\leq N) の芪は頂点 P_i です。 Q 個の質問が䞎えられるので答えおください。 i 番目の質問では敎数 X_i, Y_i が䞎えられたす。頂点 X_i は頂点 Y_i の祖先であり、二頂点間の距離を d ずするず d\geq2 です。頂点 X_i ず Y_i を結ぶパス䞊の頂点の番号を順に v_0,v_1,\ldots,v_d ただし、 v_0=X_i, v_d=Y_i ずするずき、 v_1 を出力しおください。
[ { "input": "7\n1 1 3 3 5 5\n3\n1 7\n3 6\n3 7\n", "output": "3\n5\n5\n" }, { "input": "7\n1 2 3 4 5 6\n5\n1 6\n2 5\n3 7\n3 7\n1 4\n", "output": "2\n3\n4\n4\n2\n" }, { "input": "7\n1 1 1 4 3 2\n3\n1 6\n1 7\n1 5\n", "output": "3\n2\n4\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day1/tasks/pakencamp_2022_day1_h
問題文 hiikunZ 君はクむズ倧䌚の䞻催者ですが、埗点の管理が苊手です。そこで、あなたが「アタックサバむバル」ずいうルヌルにおいお埗点の管理を行うプログラムを曞いおあげるこずにしたした。以䞋の問題を解くプログラムを曞いおください。 クむズ倧䌚に N 人の参加者が参加しおいお、 1 から N たでの番号が぀けられおいたす。各参加者は「䜓力」ずいうパラメヌタを持っおいたす。はじめ、参加者 i の䜓力は A_i です。そしお Q 回、以䞋の 2 皮類のむベントが発生したす。 1 x y : 参加者 x が配点 y の問題に正解する。するず、参加者 x 以倖の䜓力が 0 より倧きいすべおの参加者の䜓力が y 枛る。 2 x y : 参加者 x が配点 y の問題に誀答する。するず、参加者 x の䜓力が y 枛る。 各むベントごずに、むベントの前には䜓力が 0 より倧きかったが、むベントの埌には䜓力が 0 以䞋になった参加者の番号を 昇順に 列挙しおください。
[ { "input": "4 2\n5 5 5 10\n1 2 5\n2 4 5\n", "output": "2 1 3\n1 4\n" }, { "input": "4 3\n20 10 5 5\n1 2 1\n1 2 500\n2 2 500\n", "output": "0\n3 1 3 4\n1 2\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day1/tasks/pakencamp_2022_day1_i
問題文 あなたは長さ N の数列 X=(X_1,X_2,\ldots,X_N) を持っおいたしたが、 X の各芁玠の具䜓的な倀は忘れおしたいたした。あなたは次のこずを芚えおいたす。 (15:18 修正) X のすべおの芁玠は正敎数である。 i=1,2,\ldots,P に぀いお、 X_{A_i}=X_{B_i} i=1,2,\ldots,Q に぀いお、 X_{C_i}\neq X_{D_i} X ずしお考えられるもののうち、蟞曞順で最小のものを求めおください。ただし、 X ずしお考えられるものが存圚しない堎合はそのこずを報告しおください。
[ { "input": "5 2 3\n1 2\n2 4\n1 3\n4 5\n3 5\n", "output": "1 1 2 1 3\n" }, { "input": "5 1 1\n1 2\n1 2\n", "output": "-1\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day1/tasks/pakencamp_2022_day1_j
問題文 パ研王囜には N 個の街があり、 1,2,3,\ldots,N ず番号が぀いおいたす。 王囜には N-1 本の道路があり、 i 本目の道路は街 A_i ず街 B_i を結んでいお、 C_i ずいう重みが定たっおいたす。パ研王囜のすべおの街は道路を䜿っお行き来するこずができたす。 パ研王囜の K 囜王は倉わっおいるので、 Q 個の街の組をあなたに蚀い枡し、各組 (S_i,T_i)(1 \leq i \leq Q) に぀いお街 S_i から街 T_i ぞのパス䞊の道路の重みの䞭倮倀を求めたいず蚀いたした。召䜿のあなたは K 囜王の代わりに王が求めたい倀を求めおあげおください。 (2023/03/27 15:36远蚘:) 数列 X に察する䞭倮倀は、芁玠数が偶数の堎合は芁玠を昇順に䞊べた列を X_1,X_2,...,X_n ずするずき、 (X_{n/2} + X_{n/2+1}) / 2 で定矩されたす。 奇数の堎合は、芁玠を昇順に䞊べた列を X_1,X_2,...,X_n ずするずき、 X_{(n+1)/2} で定矩されたす。
[ { "input": "5\n1 2 50\n2 3 100\n1 4 20\n4 5 30\n1\n3 5\n", "output": "40\n" }, { "input": "3\n1 2 100\n2 3 50\n1\n1 3\n", "output": "75\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day1/tasks/pakencamp_2022_day1_k
問題文 数列 X=(X_1,X_2,\ldots,X_M) の 矎しさ を以䞋のように定めたす。 Y_i\coloneqq \max(X_1,X_2,\ldots,X_i) ずしたずきの、 Y_1,Y_2,\ldots,Y_M に含たれる数の皮類数 長さ N の数列 A=(A_1,A_2,\ldots,A_N) が䞎えられたす。 Q 個のク゚リを凊理しおください。 i 番目のク゚リでは敎数 L_i,R_i が䞎えられるので、数列 (A_{L_i},A_{L_i+1},\ldots,A_{R_i}) の 矎しさ を出力しおください。
[ { "input": "5\n1 5 2 4 3\n4\n1 5\n2 5\n3 3\n3 5\n", "output": "2\n1\n1\n2\n" }, { "input": "5\n3 2 2 4 5\n4\n1 5\n1 5\n4 5\n4 5\n", "output": "3\n3\n2\n2\n" }, { "input": "5\n4 4 1 3 5\n15\n1 1\n1 2\n1 3\n1 4\n1 5\n2 2\n2 3\n2 4\n2 5\n3 3\n3 4\n3 5\n4 4\n4 5\n5 5\n", "output": "1\n1\n1\n1\n2\n1\n1\n1\n2\n1\n2\n3\n1\n2\n1\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day1/tasks/pakencamp_2022_day1_l
問題文 有限個の非負敎数からなる倚重集合 S に察しお、 \operatorname{mex}(S) を S に含たれない最小の非負敎数ず定矩したす。䟋えば、 \operatorname{mex}(\lbrace 0,0,1,3 \rbrace)=2,\operatorname{mex}(\lbrace 1 \rbrace)=0,\operatorname{mex}(\lbrace\rbrace)=0 です。 黒板に長さ N の非負敎数列 A=(A_1,A_2,\ldots,A_N) が曞かれおいたす。 あなたは、以䞋の操䜜をちょうど K 回行いたす。 A の䞭から非負敎数を 0 個以䞊遞ぶ。遞んだ非負敎数からなる倚重集合を S ずしお、 \operatorname{mex}(S) を A の埌ろに远加する。 最終的に黒板に曞かれおいる非負敎数列 A ずしおありうるものの個数を 998244353 で割ったあたりを求めおください。
[ { "input": "3 1\n0 1 3\n", "output": "3\n" }, { "input": "5 10\n3 1 4 1 5\n", "output": "14476910\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day1/tasks/pakencamp_2022_day1_m
問題文 頂点 1 を根ずする N 頂点の根付き朚がありたす。頂点 i の芪は頂点 P_i です。これから各頂点に 0 たたは 1 を曞き蟌みたいです。 以䞋の䞎えられた条件を満たすような頂点に察する 0 ず 1 の割り圓お方を䞀぀求めおください。たた、そのような割り圓お方が存圚しない堎合はそのこずを報告しおください。 K 個の頂点 M_1 ,M_2, \cdots M_K にはそれぞれ S_1 ,S_2, \cdots S_K が曞き蟌たれおいる。 頂点 i に 1 が曞き蟌たれおいるならば、頂点 A_i ずその子孫の頂点には 1 が曞き蟌たれおいる。 頂点 i に 0 が曞き蟌たれおいるならば、頂点 A_i ずその子孫の頂点には 0 が曞き蟌たれおいる。
[ { "input": "4\n1 1 2\n1\n2 1\n3 4 3 2\n", "output": "1111\n" }, { "input": "5\n1 2 3 4\n2\n1 1\n2 0\n3 1 4 1 5\n", "output": "IMPOSSIBLE\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day1/tasks/pakencamp_2022_day1_n
問題文 はじめに敎数 T が䞎えられたす。 T 個のテストケヌスに぀いお次の問題を解いおください。 hiikunZ 君は、誕生日プレれントずしおパ研マシンをもらいたした。 パ研マシンには、ディスプレむ、ラむト、ボタンが 1 ぀ず぀぀いおいたす。 はじめ、ディスプレむには敎数 s が衚瀺されおいお、ラむトは赀に点灯しおいたす。 hiikunZ 君がボタンを抌すたびに、ディスプレむに衚瀺される敎数 x ずラむトの色は、次の通りに倉化したす。 ボタンを抌す前に、ラむトが赀に点灯しおいた堎合、 x は A_r \times x + B_r を玠数 P で割ったあたりに倉化し、ラむトは青に点灯する。 ボタンを抌す前に、ラむトが青に点灯しおいた堎合、 x は A_b \times x + B_b を玠数 P で割ったあたりに倉化し、ラむトは黄に点灯する。 ボタンを抌す前に、ラむトが黄に点灯しおいた堎合、 x は A_y \times x + B_y を玠数 P で割ったあたりに倉化し、ラむトは赀に点灯する。 hiikunZ 君はボタンを䜕回か ( 0 回でもよい ) 抌すこずでディスプレむに衚瀺されおいる敎数を t に倉化させたいです。 これが可胜かどうか刀定し、可胜な堎合はこれを達成するために必芁なボタンを抌す回数の最小倀を求めおください。
[ { "input": "3\n1 8 13\n1 2 3 4 5 6\n0 1 3\n1 0 1 0 1 0\n123456789 634 998244353\n1 23 456 7890 123456789 987654321\n", "output": "4\n-1\n164941630\n" } ]
https://atcoder.jp/contests/pakencamp-2022-day1/tasks/pakencamp_2022_day1_o
問題文 パ研ランドには N 個のアトラクションがあり、 1 から N たでの番号が぀けられおいたす。 たた、これらのアトラクションを結ぶ道が N - 1 本あり、 1 から N - 1 たでの番号が぀けられおいたす。 道 i (1 \leq i \leq N - 1) はアトラクション A_i ず アトラクション B_i を双方向に結んでいたす。 さらに、パ研ランドのそれぞれの道には「楜しさ」ずいう倀が蚭定されおいお、道 i (1 \leq i \leq N - 1) の楜しさは C_i です。 パ研ランドでのアトラクション間の移動も楜しみたい hiikunZ 君のために、すべおのアトラクション i (1 \leq i \leq N) に぀いお、次の問題を解いおください。 hiikunZ 君はアトラクション i から、アトラクション i ずは異なるアトラクション j (i \neq j,1 \leq j \leq N) を 1 ぀決め、アトラクション i から アトラクション j に道を通る回数が最小になるように移動しようず考えおいたす。 アトラクション j を適切に遞んだずき、hiikunZ 君が通る道の「楜しさ」の平均倀を最倧でいく぀にできるかを求めおください。 ただし、答えは敎数では衚せない堎合があるので、答えの小数点以䞋を切り捚おた倀を出力しおください。
[ { "input": "3\n1 2 3\n2 3 6\n", "output": "4\n6\n6\n" } ]
https://atcoder.jp/contests/abc295/tasks/abc295_a
Problem Statement You are given N strings W_1,W_2,\dots,W_N consisting of lowercase English letters. If one or more of these strings equal and , not , that , the , or you , then print Yes ; otherwise, print No .
[ { "input": "10\nin that case you should print yes and not no\n", "output": "Yes\n" }, { "input": "10\nin diesem fall sollten sie no und nicht yes ausgeben\n", "output": "No\n" } ]
https://atcoder.jp/contests/abc295/tasks/abc295_b
Problem Statement We have a board with R rows running horizontally and C columns running vertically. Let (i,j) denote the square at the i -th row from the top and j -th column from the left. You are given characters B_{i,j} representing the current states of (i,j) . . represents an empty square; # represents a square with a wall; 1 , 2 , \dots , 9 represent a square with a bomb of power 1,2,\dots,9 , respectively. At the next moment, all bombs will explode simultaneously . When a bomb explodes, every square whose Manhattan distance from the square with the bomb is not greater than the power of the bomb will turn into an empty square. Here, the Manhattan distance from (r_1,c_1) to (r_2,c_2) is |r_1-r_2|+|c_1-c_2| . Print the board after the explosions.
[ { "input": "4 4\n.1.#\n###.\n.#2.\n#.##\n", "output": "...#\n#...\n....\n#...\n" }, { "input": "2 5\n..#.#\n###.#\n", "output": "..#.#\n###.#\n" }, { "input": "2 3\n11#\n###\n", "output": "...\n..#\n" }, { "input": "4 6\n#.#3#.\n###.#.\n##.###\n#1..#.\n", "output": "......\n#.....\n#....#\n....#.\n" } ]
https://atcoder.jp/contests/abc295/tasks/abc295_c
Problem Statement You have N socks. The color of the i -th sock is A_i . You want to perform the following operation as many times as possible. How many times can it be performed at most? Choose two same-colored socks that are not paired yet, and pair them.
[ { "input": "6\n4 1 7 4 1 4\n", "output": "2\n" }, { "input": "1\n158260522\n", "output": "0\n" }, { "input": "10\n295 2 29 295 29 2 29 295 2 29\n", "output": "4\n" } ]
https://atcoder.jp/contests/abc295/tasks/abc295_d
Problem Statement The string 20230322 can be rearranged into 02320232 , which is a repetition of 0232 twice. Similarly, a string consisting of digits is said to be happy when it can be rearranged into (or already is) a repetition of some string twice. You are given a string S consisting of digits. Find the number of pairs of integers (l,r) satisfying all of the following conditions. 1 \le l \le r \le |S| . ( |S| is the length of S .) The (contiguous) substring formed of the l -th through r -th characters of S is happy.
[ { "input": "20230322\n", "output": "4\n" }, { "input": "0112223333444445555556666666777777778888888889999999999\n", "output": "185\n" }, { "input": "3141592653589793238462643383279502884197169399375105820974944\n", "output": "9\n" } ]
https://atcoder.jp/contests/abc295/tasks/abc295_e
Problem Statement We have a sequence of length N consisting of integers between 0 and M , inclusive: A=(A_1,A_2,\dots,A_N) . Snuke will perform the following operations 1 and 2 in order. For each i such that A_i=0 , independently choose a uniform random integer between 1 and M , inclusive, and replace A_i with that integer. Sort A in ascending order. Print the expected value of A_K after the two operations, modulo 998244353 . How to print a number modulo 998244353 ? It can be proved that the sought expected value is always rational. Additionally, under the Constraints of this problem, when that value is represented as \frac{P}{Q} using two coprime integers P and Q , it can be proved that there is a unique integer R such that R \times Q \equiv P\pmod{998244353} and 0 \leq R \lt 998244353 . Print this R .
[ { "input": "3 5 2\n2 0 4\n", "output": "3\n" }, { "input": "2 3 1\n0 0\n", "output": "221832080\n" }, { "input": "10 20 7\n6 5 0 2 0 0 0 15 0 0\n", "output": "617586310\n" } ]
https://atcoder.jp/contests/abc295/tasks/abc295_f
Problem Statement You are given a string S consisting of digits and positive integers L and R for each of T test cases. Solve the following problem. For a positive integer x , let us define f(x) as the number of contiguous substrings of the decimal representation of x (without leading zeros) that equal S . For instance, if S= 22 , we have f(122) = 1 , f(123) = 0 , f(226) = 1 , and f(222) = 2 . Find \displaystyle \sum_{k=L}^{R} f(k) .
[ { "input": "6\n22 23 234\n0295 295 295\n0 1 9999999999999999\n2718 998244353 9982443530000000\n869120 1234567890123456 2345678901234567\n2023032520230325 1 9999999999999999\n", "output": "12\n0\n14888888888888889\n12982260572545\n10987664021\n1\n" } ]
https://atcoder.jp/contests/abc295/tasks/abc295_g
Problem Statement We have a directed graph G_S with N vertices numbered 1 to N . It has N-1 edges. The i -th edge (1\leq i \leq N-1) goes from vertex p_i\ (1\leq p_i \leq i) to vertex i+1 . We have another directed graph G with N vertices numbered 1 to N . Initially, G equals G_S . Process Q queries on G in the order they are given. There are two kinds of queries as follows. 1 u v : Add an edge to G that goes from vertex u to vertex v . It is guaranteed that the following conditions are satisfied. u \neq v . On G_S , vertex u is reachable from vertex v via some edges. 2 x : Print the smallest vertex number of a vertex reachable from vertex x via some edges on G (including vertex x ).
[ { "input": "5\n1 2 3 3\n5\n2 4\n1 4 2\n2 4\n1 5 1\n2 4\n", "output": "4\n2\n1\n" }, { "input": "7\n1 1 2 2 3 3\n10\n2 5\n1 5 2\n2 5\n1 2 1\n1 7 1\n1 6 3\n2 5\n2 6\n2 1\n1 7 1\n", "output": "5\n2\n1\n1\n1\n" } ]
https://atcoder.jp/contests/abc295/tasks/abc295_h
Problem Statement We have a grid A with N rows and M columns. Initially, 0 is written on every square. Let us perform the following operation. For each integer i such that 1 \le i \le N , in the i -th row, turn the digits in zero or more leftmost squares into 1 . For each integer j such that 1 \le j \le M , in the j -th column, turn the digits in zero or more topmost squares into 1 . Let S be the set of grids that can be obtained in this way. You are given a grid X with N rows and M columns consisting of 0 , 1 , and ? . There are 2^q grids that can be obtained by replacing each ? with 0 or 1 , where q is the number of ? in X . How many of them are in S ? This count can be enormous, so find it modulo 998244353 .
[ { "input": "2 3\n0?1\n?1?\n", "output": "6\n" }, { "input": "5 3\n101\n010\n101\n010\n101\n", "output": "0\n" }, { "input": "18 18\n??????????????????\n??????????????????\n??????????????????\n??????????????????\n??????????????????\n??????????????????\n??????????????????\n??????????????????\n??????????????????\n??????????????????\n??????????????????\n??????????????????\n??????????????????\n??????????????????\n??????????????????\n??????????????????\n??????????????????\n??????????????????\n", "output": "462237431\n" } ]
https://atcoder.jp/contests/utpc2022/tasks/utpc2022_a
問題文 正敎数 N に察しお、 f(N) を以䞋で定めたす。 N の各桁の数字を䞊び替えお埗られる敎数の集合を S ずする。ただし、䞊び替えた結果先頭に 0 が続く堎合 leading zero ずしお解釈する。䟋えば、 N=102 のずき S=\lbrace 12,21,102,120,201,210\rbrace である。 S の芁玠党おを割り切る最倧の敎数を f(N) ずする。 10^{18} 以䞋の正敎数 K が䞎えられたす。 f(N)=K を満たすような 10^{18} 以䞋の正敎数 N が存圚するか刀定し、存圚する堎合 1 ぀求めおください。 T 個のテストケヌスが䞎えられるので、それぞれに぀いお答えおください。
[ { "input": "2\n3\n10\n", "output": "123\n-1\n" } ]
https://atcoder.jp/contests/utpc2022/tasks/utpc2022_b
問題文 2 次元平面䞊に nok0 君ず N 匹のスラむムがいたす。はじめ nok0 君の座暙は (0, 0) 、 i\ (1\le i\le N) 番目のスラむムの座暙は (X_i, Y_i) です。 nok0 君は平面䞊の任意の地点で䜕床でも以䞋の行動ができたす。 x 軞正負方向、 y 軞正負方向の合蚈 4 方向に同時にビヌムを打぀。nok0 君ず等しい x 座暙たたは y 座暙をも぀スラむムが消滅する。 nok0 君は平面䞊を任意の方向に連続的に移動するこずができたす。nok0 君が平面䞊にいるすべおのスラむムを消滅させるのに必芁な移動距離の最小倀を求めおください。
[ { "input": "3\n1 -1\n2 6\n8 3\n", "output": "3.605551275463989\n" }, { "input": "1\n100000000 -100000000\n", "output": "100000000.000000000000000\n" }, { "input": "18\n2092413 29557322\n-83061793 -86609930\n41750783 34587912\n94366440 62086679\n42714686 -18841496\n10264522 -60895144\n94721140 -72749181\n-68416594 -56662164\n85327102 82520146\n-97103434 30343456\n54136952 -27352836\n-38212546 92787647\n-72073444 28721824\n32088712 -55984481\n-38640098 22126296\n84969832 -72730101\n-4649799 14505075\n92214627 89508662\n", "output": "225535157.051134686276782\n" } ]
https://atcoder.jp/contests/utpc2022/tasks/utpc2022_c
問題文 UT 君ず PC 君は Nim ずいうゲヌムで遊んでいたす。 N 個の正敎数 A_1, A_2,\ldots, A_N に察し、 \mathrm{Nim}(A_1, A_2, \ldots, A_N) ずは次のゲヌムのこずを指したす。 いく぀かの石からなる N 個の山があり、はじめ i\ (1\le i\le N) 番目の山には A_i 個の石がある。UT 君から始めお、 2 人は亀互に以䞋の操䜜を 1 回ず぀繰り返す。 (操䜜) 石が 1 個以䞊残っおいる山を 1 ぀遞ぶ。その山から石を 1 個以䞊取り陀く。 石が党お取り陀かれた時点でゲヌムを終了し、最埌に操䜜を行ったプレむダヌを勝ち、もう䞀方のプレむダヌを負けずする。 ゲヌム開始時点からゲヌム終了時点たでに 2 人が行った操䜜の回数の合蚈を T ずしたずき、勝ったプレむダヌが 10^{100}-T 点、負けたプレむダヌが T-10^{100} 点を埗る。 1 \le A_i \le M\ (1\le i\le N) を満たす長さ N の敎数列 (A_1, A_2, \ldots, A_N) は M^N 通りありたすが、その党おに察しお、 2 人は 1 回ず぀ \mathrm{Nim}(A_1, A_2, \ldots, A_N) を遊びたす。 それぞれが党おのゲヌムで獲埗する点数の合蚈を最倧化するように行動したずき、これら M^N 回のゲヌムの操䜜回数の合蚈は䜕回になるでしょうか答えは非垞に倧きくなる可胜性があるので、 998244353 で割った䜙りを求めおください。
[ { "input": "2 2\n", "output": "12\n" }, { "input": "4 5\n", "output": "6748\n" }, { "input": "1 222\n", "output": "222\n" }, { "input": "987654321 456\n", "output": "897555885\n" } ]
https://atcoder.jp/contests/utpc2022/tasks/utpc2022_d
問題文 この問題は むンタラクティブな問題 あなたの䜜成したプログラムずゞャッゞプログラムが暙準入出力を介しお察話を行う圢匏の問題です。 N 個のおもりが手元にありたす。 i 番目のおもりの重さを P_i ずしたずき、 (P_1, \dots, P_N) は (1, \dots, N) の順列ずなるこずが分かっおいたすが、 P_i の具䜓的な倀は分かっおいたせん。 たた、 -2N 以䞊 2N 以䞋の敎数の目盛りが぀いた棒が、䜍眮 0 のずころで぀るされおいたす。目盛りが぀いおいる䜍眮には穎が開いおおり、それぞれの穎には 1 個たでおもりをぶら䞋げるこずができたす。 はじめはおもりが 1 ぀もぶら䞋がっおおらず、棒は぀りあっおいたす。 zkou くんは最終的に、手元にある N 個のおもりをすべお棒にぶら䞋げお、なおか぀棒を぀りあわせたいです。 あなたは zkou くんに以䞋のいずれかの操䜜をするよう指瀺できたす。 手元にあるおもりを 1 ぀遞び、おもりがただぶら䞋がっおいない穎にぶら䞋げる。 棒にぶら䞋がっおいるおもりを 1 ぀遞び、手元に戻す。 zkou くんは操䜜を終えるたびに、棒が巊右どちらに傟いたか、あるいは぀りあったかを、あなたに䌝えたす。 10000 回以䞋の指瀺で、手元にある N 個のおもりをすべお棒にぶら䞋げお、なおか぀棒を぀りあわせおください。 なお、制玄䞋で垞に぀りあわせる方法が存圚するこずが蚌明できたす。 棒の傟きず぀りあいに぀いお 棒に k 個のおもりがのっおいお、 j 番目のおもりが重さ w_j で䜍眮 x_j にあるずき、棒のモヌメント M を M = \sum_{j = 1}^{k} w_j x_j ず定矩したす。 M > 0 なら棒が右に傟き、 M = 0 なら棒は぀りあい、 M < 0 なら棒が巊に傟きたす。
[]
https://atcoder.jp/contests/utpc2022/tasks/utpc2022_e
問題文 長さ N の順列 P=(P_1,P_2,\ldots,P_N), Q=(Q_1,Q_2,\ldots,Q_N) がありたす。はじめ P_i = Q_i = i (1 \leq i \leq N) です。 この 2 ぀の順列に察しお、以䞋の操䜜を 0 回以䞊奜きなだけ繰り返したす。 1 \le i \le M を満たす敎数 i を遞ぶ。 P の a_i 番目ず b_i 番目の芁玠を入れ替え、 Q の c_i 番目ず d_i 番目の芁玠を入れ替える。 操䜜埌の P, Q の状態の組ずしおありうるものの個数を 998244353 で割ったあたりを求めおください。
[ { "input": "3 1\n1 2 2 3\n", "output": "2\n" }, { "input": "4 3\n1 2 2 4\n2 3 1 2\n1 4 2 3\n", "output": "288\n" }, { "input": "2 0\n", "output": "1\n" } ]
https://atcoder.jp/contests/utpc2022/tasks/utpc2022_f
問題文 (1, 2, \ldots, N) の順列 P = (P_1, P_2, \ldots, P_N) ず敎数 K が䞎えられたす。 以䞋の疑䌌コヌドで衚されるアルゎリズムを実行した堎合、(1) の匏は䜕回実行されたすか for (int i = 1; i <= N; i++) { for (int j = 1; j <= N - i - K + 2; j++) { if ((P[j], P[j + 1], ..., P[j + K - 1]) が昇順に䞊んでいない) { (P[j], P[j + 1], ..., P[j + K - 1]) を昇順に䞊び替える ... (1) } } }
[ { "input": "4 2\n1 3 4 2\n", "output": "2\n" }, { "input": "6 3\n5 1 6 4 3 2\n", "output": "6\n" }, { "input": "20 7\n10 17 8 1 16 13 14 5 20 19 4 15 18 3 11 2 12 9 7 6\n", "output": "23\n" } ]
https://atcoder.jp/contests/utpc2022/tasks/utpc2022_g
問題文 黒板に N 個の敎数 A_1, A_2, \ldots, A_N が曞かれおいたす。 あなたは次の操䜜を N - 1 回行いたす。 黒板に曞かれおいる数を 2 ぀遞んで消す。消した数を x ず y ずしお、 K - x - y を新たに黒板に曞く。 N - 1 回の操䜜を終えた埌、黒板にはただ䞀぀の敎数が残りたすが、この敎数ずしお考えられる最倧倀はいく぀ですか
[ { "input": "4 3\n1 2 3 4\n", "output": "7\n" }, { "input": "4 7\n1 2 3 4\n", "output": "5\n" }, { "input": "10 3\n1 4 1 5 9 2 6 5 3 5\n", "output": "32\n" } ]
https://atcoder.jp/contests/utpc2022/tasks/utpc2022_h
問題文 敎数 N が䞎えられたす。あなたは、敎数 N に察しお以䞋の 操䜜 を 1 回だけ行えたす。 操䜜 : N の桁のうち、いく぀かの桁を遞び 0 個でも良い、それらを党お独立に 0 ~ 9 のいずれかに等確率で眮き換える。ただし、先頭に 0 が続く堎合 leading zero ずしお解釈する。 あなたの目的は、出来るだけ高い確率で N の倀を操䜜前よりも倧きくするこずです。最適な戊略をずった堎合に操䜜前の N よりも倧きい数を埗られる確率を \text{mod } 998244353 で求めおください。 確率 \text{mod } 998244353 の定矩 この問題で求める確率は必ず有理数になるこずが蚌明できたす。 たた、この問題の制玄䞋では、求める確率を既玄分数 \frac{y}{x} で衚したずきに x が 998244353 で割り切れないこずが蚌明できたす。 このずき xz \equiv y \pmod{998244353} を満たすような 0 以䞊 998244352 以䞋の敎数 z が䞀意に定たりたす。この z を答えおください。
[ { "input": "502\n", "output": "509104621\n" }, { "input": "520\n", "output": "698771048\n" } ]
https://atcoder.jp/contests/utpc2022/tasks/utpc2022_i
問題文 ( , ) のみからなる文字列のうち、以䞋のいずれかに該圓するものを「正しい括匧列」ずいいたす。 空文字列 ある「正しい括匧列」 A が存圚しお ( , A , ) をこの順に぀なげた文字列 ある空でない「正しい括匧列」 A,\ B が存圚しお A,\ B をこの順に぀なげた文字列 長さ 4N で ( , ) のみからなる文字列 S ず 2N 個の 2 敎数の組 (L_i, R_i)\ (1\leq L_i < R_i \leq 4N) が䞎えられたす。ここで、 (L_1,L_2,\dots,L_{2N}) は (1,2,\dots,2N) の、 (R_1,R_2,\dots,R_{2N}) は (2N+1,2N+2,\dots,4N) の順列です。 i=1, 2,\dots,2N の順に S の L_i,\ R_i 文字目のいずれかに印を぀けた埌、印を぀けた文字のみを巊から順に読み、長さ 2N の文字列ずしお解釈したものを S' ずしたす。 S' を「正しい括匧列」にできるか刀定しおください。 T 個のテストケヌスに぀いお答えおください。
[ { "input": "3\n1\n()()\n1 3\n2 4\n2\n))()((()\n1 6\n2 5\n3 7\n4 8\n8\n))()))))()())((((()))(()(()))(((\n8 26\n2 25\n1 18\n4 22\n7 17\n12 32\n10 31\n5 30\n15 27\n9 23\n13 19\n11 24\n14 29\n6 28\n16 20\n3 21\n", "output": "Yes\nNo\nYes\n" } ]
https://atcoder.jp/contests/utpc2022/tasks/utpc2022_j
問題文 (1,2,\ldots,N) の順列 P = (P_1,P_2,\ldots,P_N) が䞎えられたす。あなたは以䞋の操䜜を 0 回以䞊 15 回以䞋行うこずができたす。 1\leq l \leq r \leq N か぀ r-l+1 が奇数であるような敎数組 (l,r) を遞び、数列 (P_l,P_{l+1},\ldots,P_r) の䞭倮倀を M ずする。このずき P_x = M なる敎数 x が䞀意に定たる。 P の l 番目から x-1 番目を存圚すれば昇順に䞊び替え、 x+1 番目から r 番目を存圚すれば昇順に䞊び替える。 操䜜によっお P を昇順に䞊び替えられるか刀定し、可胜な堎合はそのような操䜜列を䞀぀求めおください。 䞭倮倀ずは 長さ 2n - 1 の数列の䞭倮倀は、数列を昇順に䞊び替えたずき、前から n 番目の芁玠の倀ずしお定矩されたす。䟋えば、 (5, 4,2) の䞭倮倀は 4 、 (3,1,5,2,4) の䞭倮倀は 3 、 (9) の䞭倮倀は 9 です。
[ { "input": "5\n2 1 3 5 4\n", "output": "1\n1 5\n" }, { "input": "4\n1 2 3 4\n", "output": "2\n1 3\n2 4\n" }, { "input": "2\n2 1\n", "output": "-1\n" } ]
https://atcoder.jp/contests/utpc2022/tasks/utpc2022_k
問題文 2N\times 2M のグリッド状のマス目がありたす。䞊から i 行目、巊から j 列目のマス目を (i,j) で衚したす。はじめ、各マスの色はすべお癜色です。 これから以䞋の条件を満たすように NM 個のマス目を赀く塗りたす。 赀く塗られたマス目 (i,j) に぀いお i+j は偶数である。 赀く塗られたマス目は端点を共有しない。厳密には、赀く塗られた 2 ぀の異なるマス目 (i,j),(k,l) であっお、 |i-k|\leq 1 か぀ |j-l| \leq 1 を満たすものが存圚しない。 K 個のマス目 (X_i,Y_i) は必ず赀く塗る。 条件を満たすように NM 個のマス目を赀く塗る方法の数を 998244353 で割ったあたりを求めおください。
[ { "input": "2 2 1\n3 1\n", "output": "3\n" }, { "input": "1 2 2\n1 1\n2 2\n", "output": "0\n" }, { "input": "20 20 10\n26 26\n27 9\n7 21\n38 20\n30 34\n36 14\n17 7\n30 40\n19 3\n38 8\n", "output": "908257345\n" } ]
https://atcoder.jp/contests/utpc2022/tasks/utpc2022_l
問題文 N 人が参加するじゃんけん倧䌚が開催されたす。参加者は、人 1 、人 2 、 \ldots 、人 N ず呌ばれたす。どの参加者もそれぞれ埗意な手を持っおいお、毎詊合埗意な手のみを出したす。 参加者の埗意な手は、 R , P , S からなる長さ N の文字列 S によっお衚されたす。人 i の埗意な手は、 S の i 文字目 S_i です。ここで文字 R はグヌを、 P はパヌを、 S はチョキを衚したす。 倧䌚では、人 1 、人 2 、 \ldots 、人 N をこの順に暪䞀列に䞊べた埌、 0 回以䞊の「詊合」を行いたす。「詊合」は、次のように行われたす。 列で隣り合っおいる 2 人であっお、じゃんけんをしたずきにあいこにならないような 2 人を無䜜為に遞び、じゃんけんをさせる。負けたほうの人を列から抜けさせる。 「詊合」が行えなくなった時点で、列に残っおいる人党員の優勝ずなりたす。特に、列に残っおいる人が 1 人だけになった堎合、その人の単独優勝ずなりたす。 単独優勝する可胜性のある人の人数を求めおください。 じゃんけんのルヌル じゃんけんの結果は、 2 人の出した手に応じお次のように決たりたす。 䞀方がグヌで他方がチョキのずき、グヌを出した人が勝ち、チョキを出した人は負け 䞀方がチョキで他方がパヌのずき、チョキを出した人が勝ち、パヌを出した人は負け 䞀方がパヌで他方がグヌのずき、パヌを出した人が勝ち、グヌを出した人は負け 䞡者が同じ手を出したずき、あいこ匕き分け
[ { "input": "4\nRSPR\n", "output": "2\n" }, { "input": "3\nRSR\n", "output": "0\n" }, { "input": "6\nSRPPSR\n", "output": "3\n" } ]
https://atcoder.jp/contests/utpc2022/tasks/utpc2022_m
問題文 長さ n の非負敎数列 X=(X_1,X_2,\dots,X_n) に察し f(X) を、 Y_1+Y_2+\dots+Y_n=X_1+X_2+\dots+X_n を満たす長さ n の非負敎数列 Y=(Y_1,Y_2,\dots,Y_n) 党おにおける Y_1 \oplus Y_2 \oplus \dots \oplus Y_n の最小倀ずしたす。ただしここで、 \oplus はビット単䜍 \mathrm{XOR} 挔算を衚したす。 長さ N の非負敎数列 A=(A_1,A_2,\dots,A_N) が䞎えられたす。 A の空でない郚分列 B は 2^N-1 個考えられたすが、それらすべおに察する f(B) の総和を 998244353 で割ったあたりを求めおください。 ビット単䜍 \mathrm{XOR} 挔算ずは 非負敎数 A, B のビット単䜍 \mathrm{XOR} 、 A \oplus B は、以䞋のように定矩されたす。 A \oplus B を二進衚蚘した際の 2^k ( k \geq 0 ) の䜍の数は、 A, B を二進衚蚘した際の 2^k の䜍の数のうち䞀方のみが 1 であれば 1 、そうでなければ 0 である。 䟋えば、 3 \oplus 5 = 6 ずなりたす (二進衚蚘するず: 011 \oplus 101 = 110 )。 䞀般に k 個の非負敎数 p_1, p_2, p_3, \dots, p_k のビット単䜍 \mathrm{XOR} は (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k) ず定矩され、これは p_1, p_2, p_3, \dots, p_k の順番によらないこずが蚌明できたす。
[ { "input": "3\n0 1 2\n", "output": "8\n" }, { "input": "15\n99412 355422 750910 993699 41414 435678 325371 637849 939332 512546 112254 175315 865362 459658 311661\n", "output": "7032514\n" } ]
https://atcoder.jp/contests/utpc2022/tasks/utpc2022_n
問題文 敎数 N ず、 0 ず 1 のみからなる長さ N の文字列 T が䞎えられたす。 0 ず 1 のみからなる、長さ 2N の文字列 S = s_1 s_2 \ldots s_{2N} ず T を甚いお、Alice ず Bob がゲヌムをしたす。二人は Alice から始めお、 S の党おの文字に印が぀くたで亀互に以䞋の操䜜をしたす。 1 \le i \le 2N を満たす敎数 i であっお、 s_i にただ印が぀いおいないようなものを遞ぶ。その埌、 Alice の手番であれば、その桁に o の印を぀ける。 Bob の手番であれば、その桁に x の印を぀ける。 ゲヌムが終了した時に、 o の印が぀いおいる桁だけを巊から読み、 N 文字の文字列ずしお解釈したものが、蟞曞順で T 以䞊であれば Alice の勝利、そうでなければ Bob の勝利です。 開始時に甚いる文字列 S ずしお考えられるものは 2^{2N} 通りありたす。このうち、䞡者がそれぞれ自身が勝぀ために最適な戊略をずる堎合に、 Alice が勝぀ようなものの個数を 998244353 で割ったあたりを求めおください。
[ { "input": "1\n0\n", "output": "4\n" }, { "input": "1\n1\n", "output": "3\n" }, { "input": "12\n011011000111\n", "output": "13225655\n" } ]
https://atcoder.jp/contests/utpc2022/tasks/utpc2022_o
問題文 頂点 1 を根ずする、 N 頂点の根付き朚がありたす。頂点 i (2 \le i \le N) の芪は P_i です。頂点 1 には怅子が 10^{100} 個眮いおあり、それ以倖の頂点には怅子が 1 個ず぀眮いおありたす。それぞれの怅子には、人が 1 人たで座るこずができたす。 これから M 人の人たちが順番にこの朚に䌑憩しに蚪れたす。 i = 1, 2, \dots, M の順に、 i 番目の人は以䞋の行動をずりたす。 頂点 A_i に蚪れる。その埌、空いおいる怅子がある頂点にたどり着くたで根の方向に向かっお進む。空いおいる怅子がある頂点にたどり着いたら、その怅子に座り行動を終了する。 党員が行動を終了するたでに i 番目の人が通る蟺の本数を d_i ずしたずきに、 d_1 + d_2 + \dots + d_M の倀を求めおください。
[ { "input": "3\n1 1\n4\n1 2 3 2\n", "output": "1\n" }, { "input": "7\n1 1 3 4 5 5\n6\n3 5 3 6 6 2\n", "output": "3\n" } ]
https://atcoder.jp/contests/abc294/tasks/abc294_a
Problem Statement You are given a sequence of N integers: A=(A _ 1,A _ 2,\ldots,A _ N) . Print all even numbers in A without changing the order.
[ { "input": "5\n1 2 3 5 6\n", "output": "2 6\n" }, { "input": "5\n2 2 2 3 3\n", "output": "2 2 2\n" }, { "input": "10\n22 3 17 8 30 15 12 14 11 17\n", "output": "22 8 30 12 14\n" } ]
https://atcoder.jp/contests/abc294/tasks/abc294_b
Problem Statement You are given an H -by- W matrix A consisting of integers between 0 and 26 . The element at the i -th row from the top and j -th column from the left is A_{i,j} . Let S_1, S_2, \dots, S_H be H strings of length W that satisfy the following. The j -th character of S_i is a period ( . ) if A_{i,j} is 0 , and the A_{i,j} -th uppercase English letter otherwise. (For instance, the 4 -th letter is D .) Print S_1, S_2, \dots, S_H in order.
[ { "input": "2 3\n0 1 2\n0 0 3\n", "output": ".AB\n..C\n" }, { "input": "3 3\n24 0 0\n0 25 0\n0 0 26\n", "output": "X..\n.Y.\n..Z\n" }, { "input": "3 1\n2\n9\n4\n", "output": "B\nI\nD\n" }, { "input": "24 60\n0 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 2 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\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 13 14 0 0 0 10 0 0 0 0 0 15 24 14 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\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 23 7 25 24 13 10 0 10 12 0 0 0 0 19 9 23 0 0 0 0 10 10 14 0 0 0 10 14 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 13 5 0 0 23 11 14 14 0 0 12 9 1 21 19 0 0 9 12 10 25 3 10 6 0 0 9 13 23 24 14 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 14 6 0 0 0 10 5 25 13 0 0 25 0 0 0 0 0 0 0 0 0 0 10 16 0 0 13 21 13 13 14 23 14 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 13 8 2 0 0 0 0 0 13 11 13 19 0 0 1 2 5 9 12 12 5 9 9 20 6 0 14 14 14 9 0 0 0 14 14 18 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 10 23 13 13 13 13 13 13 14 14 14 13 14 14 13 7 0 0 0 0 0 0 0 0 0 0 0 0 13 13 13 2 0 0 0 0 13 11 13 16 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 13 1 0 0 0 9 20 9 20 20 20 20 13 20 20 13 20 23 8 8 8 20 8 20 7 8 17 7 10 13 14 13 19 0 0 0 0 0 22 14 25 13 16 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 13 9 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 20 20 20 13 13 7 20 26 13 8 6 14 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 5 0 0 0 0 0 0 0 1 2 20 20 23 13 2 7 2 10 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 20 23 13 0 0 0 0 0 0 0 0 0\n0 0 0 0 13 0 0 0 0 0 0 0 0 1 0 0 0 13 12 9 14 13 13 9 9 20 12 0 0 0 0 0 0 0 0 0 0 0 1 9 9 9 9 12 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0 0\n0 0 0 2 0 0 0 0 0 0 0 0 0 0 19 0 0 14 13 14 14 13 13 0 0 9 5 16 0 0 0 0 0 0 5 20 20 13 2 2 20 9 13 14 14 20 12 12 0 0 0 0 9 13 0 0 0 0 0 0\n0 0 1 9 0 0 0 0 0 0 0 0 0 0 0 13 10 13 13 13 13 2 5 12 10 5 0 0 0 0 0 0 0 0 20 16 0 0 0 13 14 13 13 13 13 0 0 10 8 0 0 0 0 0 20 7 0 0 0 0\n0 0 4 2 0 0 0 0 0 0 0 0 0 0 0 0 9 7 14 10 10 14 13 5 0 0 0 0 0 0 0 0 0 0 0 23 13 12 13 13 13 13 13 9 13 0 14 4 0 0 0 0 0 0 0 9 16 0 0 0\n0 0 22 13 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 20 13 13 13 2 9 14 2 20 14 0 0 0 0 0 0 0 0 0 2 0 0 0\n0 0 0 5 13 0 0 0 2 7 13 13 13 13 13 13 13 2 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 20 20 9 0 0 0 0 0 0 0 0 0 0 0 0 0 19 0 0 0\n0 0 0 0 20 13 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 21 14 7 2 20 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 6 0 0 0\n0 0 0 0 0 0 9 14 14 0 0 20 20 13 13 20 13 9 0 0 10 0 0 0 0 0 0 9 23 13 9 0 0 0 0 0 0 0 10 6 0 0 7 0 0 9 20 13 13 14 2 0 0 0 0 5 0 0 0 0\n0 0 0 0 0 0 0 0 20 13 14 0 0 0 0 0 0 0 0 13 9 0 0 0 0 0 0 0 0 13 11 0 0 0 0 0 0 0 14 9 0 0 0 20 25 14 7 0 0 0 0 9 1 14 7 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 9 13 13 20 0 0 0 0 0 9 12 0 0 0 0 0 0 0 14 13 14 0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 9 9 20 14 14 4 14 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 2 9 0 0 0 0 9 9 20 21 7 13 20 0 0 20 23 7 7 2 12 7 6 0 0 0 0 0 0 0 0 0 0 0 0 5 5 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 20 9 9 20 24 10 12 10 0 0 0 0 0 0 0 0 20 20 0 0 0 0 0 20 7 7 13 22 2 5 9 2 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 20 5 20 5 2 5 7 20 5 14 14 5 11 5 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", "output": "................................B...........................\n...................MMN...J.....OXN..........................\n.................MWGYXMJ.JL....SIW....JJN...JN..............\n...............IME..WKNN..LIAUS..ILJYCJF..IMWXN.............\n..............MNF...JEYM..Y..........JP..MUMMNWN............\n.............MHB.....MKMS..ABEILLEIITF.NNNI...NNR...........\n..........JWMMMMMMNNNMNNMG............MMMB....MKMP..........\n........MA...ITITTTTMTTMTWHHHTHTGHQGJMNMS.....VNYMP.........\n......MI...............................TTTMMGTZMHFN.........\n.....E.......ABTTWMBGBJL.......................TTWM.........\n....M........A...MLINMMIITL...........AIIIIL.......T........\n...B..........S..NMNNMM..IEP......ETTMBBTIMNNTLL....IM......\n..AI...........MJMMMMBELJE........TP...MNMMMM..JH.....TG....\n..DB............IGNJJNME...........WMLMMMMMIM.ND.......IP...\n..VM.................................TMMMBINBTN.........B...\n...EM...BGMMMMMMMBI....................ITTI.............S...\n....TML..................UNGBTX........................IF...\n......INN..TTMMTMI..J......IWMI.......JF..G..ITMMNB....E....\n........TMN........MI........MK.......NI...TYNG....IANG.....\n..........IMMT.....IL.......NMN.......F........IITNNDN......\n..............TBI....IITUGMT..TWGGBLGF............EE........\n.................TTIITXJLJ........TT.....TGGMVBEIB..........\n.........................ITETEBEGTENNEKEB...................\n............................................................\n" } ]
https://atcoder.jp/contests/abc294/tasks/abc294_c
Problem Statement You are given strictly increasing sequences of length N and M : A=(A _ 1,A _ 2,\ldots,A _ N) and B=(B _ 1,B _ 2,\ldots,B _ M) . Here, A _ i\neq B _ j for every i and j (1\leq i\leq N,1\leq j\leq M) . Let C=(C _ 1,C _ 2,\ldots,C _ {N+M}) be a strictly increasing sequence of length N+M that results from the following procedure. Let C be the concatenation of A and B . Formally, let C _ i=A _ i for i=1,2,\ldots,N , and C _ i=B _ {i-N} for i=N+1,N+2,\ldots,N+M . Sort C in ascending order. For each of A _ 1,A _ 2,\ldots,A _ N, B _ 1,B _ 2,\ldots,B _ M , find its position in C . More formally, for each i=1,2,\ldots,N , find k such that C _ k=A _ i , and for each j=1,2,\ldots,M , find k such that C _ k=B _ j .
[ { "input": "4 3\n3 14 15 92\n6 53 58\n", "output": "1 3 4 7\n2 5 6\n" }, { "input": "4 4\n1 2 3 4\n100 200 300 400\n", "output": "1 2 3 4\n5 6 7 8\n" }, { "input": "8 12\n3 4 10 15 17 18 22 30\n5 7 11 13 14 16 19 21 23 24 27 28\n", "output": "1 2 5 9 11 12 15 20\n3 4 6 7 8 10 13 14 16 17 18 19\n" } ]
https://atcoder.jp/contests/abc294/tasks/abc294_d
Problem Statement N people, with ID numbers 1 , 2 , \dots , N , are lining up in front of a bank. There will be Q events. The following three kinds of events can happen. 1 : The teller calls the person with the smallest ID number who has not been called. 2 x : The person with the ID number x comes to the teller for the first time. (Here, person x has already been called by the teller at least once.) 3 : The teller again calls the person with the smallest ID number who has already been called but has not come. Print the ID numbers of the people called by the teller in events of the third kind.
[ { "input": "4 10\n1\n1\n3\n2 1\n1\n2 3\n3\n1\n2 2\n3\n", "output": "1\n2\n4\n" } ]
https://atcoder.jp/contests/abc294/tasks/abc294_e
Problem Statement We have a grid with 2 rows and L columns. Let (i,j) denote the square at the i -th row from the top (i\in\lbrace1,2\rbrace) and j -th column from the left (1\leq j\leq L) . (i,j) has an integer x _ {i,j} written on it. Find the number of integers j such that x _ {1,j}=x _ {2,j} . Here, the description of x _ {i,j} is given to you as the run-length compressions of (x _ {1,1},x _ {1,2},\ldots,x _ {1,L}) and (x _ {2,1},x _ {2,2},\ldots,x _ {2,L}) into sequences of lengths N _ 1 and N _ 2 , respectively: ((v _ {1,1},l _ {1,1}),\ldots,(v _ {1,N _ 1},l _ {1,N _ 1})) and ((v _ {2,1},l _ {2,1}),\ldots,(v _ {2,N _ 2},l _ {2,N _ 2})) . Here, the run-length compression of a sequence A is a sequence of pairs (v _ i,l _ i) of an element v _ i of A and a positive integer l _ i obtained as follows. Split A between each pair of different adjacent elements. For each sequence B _ 1,B _ 2,\ldots,B _ k after the split, let v _ i be the element of B _ i and l _ i be the length of B _ i .
[ { "input": "8 4 3\n1 2\n3 2\n2 3\n3 1\n1 4\n2 1\n3 3\n", "output": "4\n" }, { "input": "10000000000 1 1\n1 10000000000\n1 10000000000\n", "output": "10000000000\n" }, { "input": "1000 4 7\n19 79\n33 463\n19 178\n33 280\n19 255\n33 92\n34 25\n19 96\n12 11\n19 490\n33 31\n", "output": "380\n" } ]
https://atcoder.jp/contests/abc294/tasks/abc294_f
Problem Statement Takahashi and Aoki have N and M bottles of sugar water, respectively. Takahashi's i -th sugar water is composed of A_i grams of sugar and B_i grams of water. Aoki's i -th sugar water is composed of C_i grams of sugar and D_i grams of water. There are NM ways to choose one from Takahashi's sugar waters and one from Aoki's and mix them. Among the NM sugar waters that can be obtained in this way, find the concentration of sugar in the sugar water with the K -th highest concentration of sugar. Here, the concentration of sugar in sugar water composed of x grams of sugar and y grams of water is \dfrac{100x}{x+y} percent. We will ignore saturation.
[ { "input": "3 1 1\n1 2\n4 1\n1 4\n1 4\n", "output": "50.000000000000000\n" }, { "input": "2 2 2\n6 4\n10 1\n5 8\n9 6\n", "output": "62.500000000000000\n" }, { "input": "4 5 10\n5 4\n1 6\n7 4\n9 8\n2 2\n5 6\n6 7\n5 3\n8 1\n", "output": "54.166666666666664\n" } ]
https://atcoder.jp/contests/abc294/tasks/abc294_g
Problem Statement You are given a tree T with N vertices. Edge i (1\leq i\leq N-1) connects vertices u _ i and v _ i , and has a weight of w _ i . Process Q queries in order. There are two kinds of queries as follows. 1 i w : Change the weight of edge i to w . 2 u v Print the distance between vertex u and vertex v . Here, the distance between two vertices u and v of a tree is the smallest total weight of edges in a path whose endpoints are u and v .
[ { "input": "5\n1 2 3\n1 3 6\n1 4 9\n4 5 10\n4\n2 2 3\n2 1 5\n1 3 1\n2 1 5\n", "output": "9\n19\n11\n" }, { "input": "7\n1 2 1000000000\n2 3 1000000000\n3 4 1000000000\n4 5 1000000000\n5 6 1000000000\n6 7 1000000000\n3\n2 1 6\n1 1 294967296\n2 1 6\n", "output": "5000000000\n4294967296\n" }, { "input": "1\n1\n2 1 1\n", "output": "0\n" }, { "input": "8\n1 2 105\n1 3 103\n2 4 105\n2 5 100\n5 6 101\n3 7 106\n3 8 100\n18\n2 2 8\n2 3 6\n1 4 108\n2 3 4\n2 3 5\n2 5 5\n2 3 1\n2 4 3\n1 1 107\n2 3 1\n2 7 6\n2 3 8\n2 1 5\n2 7 6\n2 4 7\n2 1 7\n2 5 3\n2 8 6\n", "output": "308\n409\n313\n316\n0\n103\n313\n103\n525\n100\n215\n525\n421\n209\n318\n519\n" } ]
https://atcoder.jp/contests/abc294/tasks/abc294_h
Problem Statement You are given a simple undirected graph with N vertices numbered 1 to N and M edges numbered 1 to M . Edge i connects vertex u_i and vertex v_i . Find the number, modulo 998244353 , of ways to write an integer between 1 and K , inclusive, on each vertex of this graph to satisfy the following condition: two vertices connected by an edge always have different numbers written on them.
[ { "input": "4 3 2\n1 2\n2 4\n2 3\n", "output": "2\n" }, { "input": "4 0 10\n", "output": "10000\n" }, { "input": "5 10 5\n3 5\n1 3\n1 2\n1 4\n3 4\n2 5\n4 5\n1 5\n2 3\n2 4\n", "output": "120\n" }, { "input": "5 6 294\n1 2\n2 4\n1 3\n2 3\n4 5\n3 5\n", "output": "838338733\n" }, { "input": "7 12 1000000000\n4 5\n2 7\n3 4\n6 7\n3 5\n5 6\n5 7\n1 3\n4 7\n1 5\n2 3\n3 6\n", "output": "418104233\n" } ]
https://atcoder.jp/contests/ahc019/tasks/ahc019_a
Problem Statement AtCoder is developing an educational toy that combines blocks to create a three-dimensional object with a specified silhouette. The toy consists of a set of polycube-shaped blocks consisting of 1\times 1\times 1 cubes joined face to face and a pair of two 2D monochrome silhouette images. You will win the game if you can construct a single 3D object by combining the blocks, so that the two silhouettes of the created object, viewed from the front and from the right side, completely match the given silhouette images. In order to allow children to play for a long time, we want to prepare multiple pairs of silhouettes for a single set of blocks. For example, the set of 6 blocks shown on the left figure can be used to create two pairs of silhouettes, as shown on the right. As a toy designer, you are given two pairs of front/right silhouettes, and your task is to find a set of blocks from which you can construct two objects having the given pairs of silhouettes, and a way to construct them. Because children may accidentally swallow small blocks, sets of blocks consisting only of a small number of large blocks are preferable. Detailed puzzle rules You don't have to use all the blocks (but you will get a better score if you do). Each block can be rotated by 90 degrees around the x-axis, y-axis, and z-axis, but cannot be flipped. The blocks must be arranged so that each vertex has integer coordinates, and different blocks must not have a positive common volume. The constructed object do not have to be connected. For the sake of simplicity, we assume that a floating arrangement is also possible (you may interpret this as there are a large number of additional transparent blocks, which can be used to support blocks). About silhouette Take the x-axis from left to right, the y-axis from front to back, and the z-axis from top to bottom. We assume that all blocks are within a cubic region that has (0,0,0) and (D,D,D) as diagonal corners. We define a three-dimensional 01-array b(x,y,z) as b(x,y,z)=1 if some block occupies the cubic region that has (x,y,z) and (x+1,y+1,z+1) as diagonal corners, and b(x,y,z)=0 otherwise. Then, the silhouette viewed from the front is a two-dimensional 01-array f(z,x) defined as follows. \[ f(z,x)=\begin{cases} 1&(\sum_{y=0}^{D-1}b(x,y,z)\geq 1)\\ 0&(\sum_{y=0}^{D-1}b(x,y,z)=0) \end{cases} \] Similarly, the silhouette viewed from the right is a two-dimensional 01-array r(z,y) , defined as follows \[ r(z,y)=\begin{cases} 1&(\sum_{x=0}^{D-1}b(x,y,z)\geq 1)\\ 0&(\sum_{x=0}^{D-1}b(x,y,z)=0) \end{cases} \]
[ { "input": "5\n10001\n11011\n11111\n10101\n10001\n01110\n11011\n10000\n11011\n01110\n11110\n00011\n01110\n11000\n11111\n11110\n00011\n01110\n00011\n11110\n", "output": "19\n0 1 2 3 0 4 5 0 3 3 6 0 0 0 3 7 7 0 3 3 0 7 0 8 0 0 5 9 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 0 9 10 0 0 0 0 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 9 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 12 12 0 13 0 0 12 14 0 0 0 0 14 0 0 0 14 14 0 0 0 14 0\n15 0 0 0 3 14 0 0 0 3 0 0 0 0 9 16 0 0 9 9 0 0 0 17 0 0 0 0 0 0 14 0 10 0 3 14 0 10 0 9 0 0 0 18 0 0 0 0 0 0 0 0 0 0 3 0 0 12 0 3 14 0 12 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 12 0 0 14 0 7 0 0 0 7 7 0 5 0 19 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 7 0 0 5 0 0 0 0 0\n" } ]
https://atcoder.jp/contests/toyota2023spring-final-open/tasks/toyota2023spring_final_a
問題文 N 行 M 列からなる盀面があり各マス目には row-major 順に 1 から N \times M たでの敎数が曞かれおいたす ぀たり䞊から i 行目巊から j 列目のマスに曞かれおいる敎数を A_{i,j} で衚すこずにするず A_{i,j}=(i-1) \times M + j です この盀面の郚分長方圢であっおその内郚に曞かれた倀の総和がちょうど V になるものの個数を数えおください より厳密に蚀えば敎数の 4 ぀組 (a,b,c,d) ( 1 \leq a \leq b \leq N , 1 \leq c \leq d \leq M ) であっお \sum_{a \leq i \leq b,\ c \leq j \leq d} A_{i,j}=V を満たすものの個数を数えおください
[ { "input": "2 2 3\n", "output": "2\n" }, { "input": "2 2 5\n", "output": "0\n" }, { "input": "13 8 1032\n", "output": "5\n" } ]
https://atcoder.jp/contests/toyota2023spring-final-open/tasks/toyota2023spring_final_b
問題文 1 から N たでの番号が぀いた N 問のクむズがありあなたはこれらを䜿ったゲヌムに参加したす このゲヌムではあなたはクむズに 1 問ず぀回答しおいきたす クむズに回答する順番は自由に遞ぶこずができたす クむズ i に回答するず P_i % の確率で正解したす 正解した堎合あなたは S_i 点を獲埗しただ未回答のクむズがあるなら次のクむズの回答ぞず移りたす 正解しなかった堎合即座にゲヌムが終了し残りのクむズに回答するこずができなくなりたす あなたは獲埗する埗点の合蚈の期埅倀を最倧化したいです 目的を達成するための戊略クむズに回答する順番を求めおください なお各クむズに正解するか吊かの確率は互いにすべお独立であるものずしたす
[ { "input": "2\n1000 10\n300 50\n", "output": "2 1\n" }, { "input": "6\n1 0\n1 20\n1 40\n1 60\n1 80\n1 100\n", "output": "6 5 4 3 2 1\n" }, { "input": "9\n88994950 78\n405248480 35\n561113280 28\n22802150 2\n946582650 25\n201425280 52\n669650 41\n128877450 71\n1396050 25\n", "output": "1 5 8 2 3 6 4 7 9\n" } ]
https://atcoder.jp/contests/toyota2023spring-final-open/tasks/toyota2023spring_final_c
問題文 敎数 L,R が䞎えられたす 以䞋の条件を満たす敎数の組 (A,B) の個数を数えおください L \leq A < B \leq R A は A \oplus B で割り切れる B は A \oplus B で割り切れる ただしここで \oplus はビット単䜍 \mathrm{XOR} 挔算を衚したす ビット単䜍 \mathrm{XOR} 挔算ずは 非負敎数 A, B のビット単䜍 \mathrm{XOR} 、 A \oplus B は、以䞋のように定矩されたす。 A \oplus B を二進衚蚘した際の 2^k ( k \geq 0 ) の䜍の数は、 A, B を二進衚蚘した際の 2^k の䜍の数のうち䞀方のみが 1 であれば 1 、そうでなければ 0 である。 䟋えば、 3 \oplus 5 = 6 ずなりたす (二進衚蚘するず: 011 \oplus 101 = 110 )。 䞀般に k 個の非負敎数 p_1, p_2, p_3, \dots, p_k のビット単䜍 \mathrm{XOR} は (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k) ず定矩され、これは p_1, p_2, p_3, \dots, p_k の順番によらないこずが蚌明できたす。
[ { "input": "3 6\n", "output": "2\n" }, { "input": "1 100\n", "output": "124\n" }, { "input": "999000000 1000000000\n", "output": "1726239\n" } ]
https://atcoder.jp/contests/toyota2023spring-final-open/tasks/toyota2023spring_final_d
問題文 (0,1,\cdots,N-1) の順列 P=(P_0,P_1,\cdots,P_{N-1}) が䞎えられたす 敎数 a,b ( 0 \leq a,b \leq N-1 ) に察しお順列 f(a,b) を次のように定矩したす f(a,b)=(q_0,q_1,\cdots,q_{N-1}) ずおいお q_{(i+a \pmod N)}=(P_i+b \pmod N) ( 0 \leq i \leq N-1 ) ず定める すべおの a,b に察し f(a,b) を求めるず N^2 個の順列が埗られたす これらの順列を蟞曞順で゜ヌトするこずを考えたす ゜ヌトしたあず先頭から K 番目に䜍眮しおいる順列を求めおください 数列の蟞曞順ずは 数列 S = (S_1,S_2,\ldots,S_{|S|}) が数列 T = (T_1,T_2,\ldots,T_{|T|}) より 蟞曞順で小さい ずは、䞋蚘の 1. ず 2. のどちらかが成り立぀こずを蚀いたす。 ここで、 |S|, |T| はそれぞれ S, T の長さを衚したす。 |S| \lt |T| か぀ (S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|}) 。 ある敎数 1 \leq i \leq \min\lbrace |S|, |T| \rbrace が存圚しお、䞋蚘の 2 ぀がずもに成り立぀。 (S_1,S_2,\ldots,S_{i-1}) = (T_1,T_2,\ldots,T_{i-1}) S_i が T_i より数ずしお小さい。
[ { "input": "2 2\n0 1\n", "output": "0 1\n" }, { "input": "4 6\n0 2 1 3\n", "output": "1 2 0 3\n" }, { "input": "10 79\n6 5 9 8 7 1 3 2 0 4\n", "output": "7 9 8 2 1 0 4 6 5 3\n" } ]
https://atcoder.jp/contests/toyota2023spring-final-open/tasks/toyota2023spring_final_e
問題文 0 , 1 からなる長さ N の敎数列 A=(A_1,A_2,\cdots,A_N) が䞎えられたす 今二次元平面䞊の座暙 (0,0) の点に駒がありたす あなたはこれから以䞋の操䜜を奜きな回数繰り返したす 敎数 x,y ( 1 \leq x,y \leq N ) を遞び駒の X , Y 座暙をそれぞれ x , y ず぀増やす ただしここで以䞋の 2 ぀の条件を満たす必芁がある A_x=1 が成立 操䜜埌の駒の座暙を (p,q) ずおくずき q \leq p が成立 最終的に駒が座暙 (N,N) ぞず至るような操䜜方法が䜕通りあるかを 998244353 で割ったあたりを求めおください
[ { "input": "2\n1 1\n", "output": "2\n" }, { "input": "1\n0\n", "output": "0\n" }, { "input": "4\n1 1 0 1\n", "output": "10\n" }, { "input": "25\n1 0 1 1 0 0 0 0 1 0 0 1 0 1 1 1 0 0 1 0 0 0 1 0 0\n", "output": "934946952\n" } ]
https://atcoder.jp/contests/toyota2023spring-final-open/tasks/toyota2023spring_final_f
問題文 A , B からなる長さ N の文字列 S が䞎えられたす あなたは以䞋の操䜜を 0 回以䞊繰り返すこずができたす S 䞭の連続する 2 文字であっお AB で ない ものを遞び消す その埌残った巊右の空かもしれない文字列を連結しこれを新たに S ずする 操䜜埌の S ずしおあり埗る文字列が䜕通りあるかを 998244353 で割ったあたりを求めおください
[ { "input": "3\nBBA\n", "output": "3\n" }, { "input": "5\nABABA\n", "output": "3\n" }, { "input": "9\nBABBAAAAB\n", "output": "14\n" }, { "input": "48\nAABABBBAABAAABAAABBBAAABBBAABAABBABAABBAAAAABBBB\n", "output": "3073910\n" } ]
https://atcoder.jp/contests/toyota2023spring-final-open/tasks/toyota2023spring_final_g
問題文 プログラミング初心者のすぬけくんが以䞋のようなコヌドを曞きたした N = read_integer() parent = array(N, -1) //長さ N の配列 parent を䜜りすべおの芁玠を -1 で初期化 find(v): if parent[v] == -1: return v else: return find(parent[v]) union(a,b): parent[find(b)] = find(a) for i = 0 to N-2: A_i = read_integer() B_i = read_integer() union(A_i,B_i) これは N 頂点の朚の情報を受けずりUnion-Find で蟺を結ぶだけのプログラムです プログラミングマスタヌのりんごさんはこのプログラムの欠陥に気が付きたした すなわちUnion-Find に䞀切の高速化が斜されおいないのです 今りんごさんは N 頂点からなる朚 T を持っおいたす T の頂点には 0 から N-1 たでの番号が蟺には 0 から N-2 たでの番号が぀いおいたす 蟺 i は頂点 A_i ず頂点 B_i を結ぶ蟺です りんごさんはすぬけくんのプログラムに T を入力ずしお䞎えようずしおいたす ただしその前に T の蟺の番号ず蟺の端点の順番を自由に入れ替えるこずができたす りんごさんはすぬけくんのプログラムが非効率的であるこずを瀺すために find 関数が呌ばれる回数を最倧化したいです find 関数が呌ばれる回数の最倧倀を求めおください
[ { "input": "2\n0 1\n", "output": "2\n" }, { "input": "3\n0 1\n0 2\n", "output": "5\n" }, { "input": "5\n0 1\n0 2\n0 3\n3 4\n", "output": "13\n" }, { "input": "20\n6 16\n10 6\n16 8\n1 5\n9 4\n5 3\n13 16\n19 10\n12 2\n14 10\n12 18\n0 2\n15 16\n12 7\n11 14\n1 10\n6 4\n17 8\n12 1\n", "output": "148\n" } ]
https://atcoder.jp/contests/toyota2023spring-final/tasks/toyota2023spring_final_a
問題文 N 行 M 列からなる盀面があり各マス目には row-major 順に 1 から N \times M たでの敎数が曞かれおいたす ぀たり䞊から i 行目巊から j 列目のマスに曞かれおいる敎数を A_{i,j} で衚すこずにするず A_{i,j}=(i-1) \times M + j です この盀面の郚分長方圢であっおその内郚に曞かれた倀の総和がちょうど V になるものの個数を数えおください より厳密に蚀えば敎数の 4 ぀組 (a,b,c,d) ( 1 \leq a \leq b \leq N , 1 \leq c \leq d \leq M ) であっお \sum_{a \leq i \leq b,\ c \leq j \leq d} A_{i,j}=V を満たすものの個数を数えおください
[ { "input": "2 2 3\n", "output": "2\n" }, { "input": "2 2 5\n", "output": "0\n" }, { "input": "13 8 1032\n", "output": "5\n" } ]
https://atcoder.jp/contests/toyota2023spring-final/tasks/toyota2023spring_final_b
問題文 1 から N たでの番号が぀いた N 問のクむズがありあなたはこれらを䜿ったゲヌムに参加したす このゲヌムではあなたはクむズに 1 問ず぀回答しおいきたす クむズに回答する順番は自由に遞ぶこずができたす クむズ i に回答するず P_i % の確率で正解したす 正解した堎合あなたは S_i 点を獲埗しただ未回答のクむズがあるなら次のクむズの回答ぞず移りたす 正解しなかった堎合即座にゲヌムが終了し残りのクむズに回答するこずができなくなりたす あなたは獲埗する埗点の合蚈の期埅倀を最倧化したいです 目的を達成するための戊略クむズに回答する順番を求めおください なお各クむズに正解するか吊かの確率は互いにすべお独立であるものずしたす
[ { "input": "2\n1000 10\n300 50\n", "output": "2 1\n" }, { "input": "6\n1 0\n1 20\n1 40\n1 60\n1 80\n1 100\n", "output": "6 5 4 3 2 1\n" }, { "input": "9\n88994950 78\n405248480 35\n561113280 28\n22802150 2\n946582650 25\n201425280 52\n669650 41\n128877450 71\n1396050 25\n", "output": "1 5 8 2 3 6 4 7 9\n" } ]
https://atcoder.jp/contests/toyota2023spring-final/tasks/toyota2023spring_final_c
問題文 敎数 L,R が䞎えられたす 以䞋の条件を満たす敎数の組 (A,B) の個数を数えおください L \leq A < B \leq R A は A \oplus B で割り切れる B は A \oplus B で割り切れる ただしここで \oplus はビット単䜍 \mathrm{XOR} 挔算を衚したす ビット単䜍 \mathrm{XOR} 挔算ずは 非負敎数 A, B のビット単䜍 \mathrm{XOR} 、 A \oplus B は、以䞋のように定矩されたす。 A \oplus B を二進衚蚘した際の 2^k ( k \geq 0 ) の䜍の数は、 A, B を二進衚蚘した際の 2^k の䜍の数のうち䞀方のみが 1 であれば 1 、そうでなければ 0 である。 䟋えば、 3 \oplus 5 = 6 ずなりたす (二進衚蚘するず: 011 \oplus 101 = 110 )。 䞀般に k 個の非負敎数 p_1, p_2, p_3, \dots, p_k のビット単䜍 \mathrm{XOR} は (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k) ず定矩され、これは p_1, p_2, p_3, \dots, p_k の順番によらないこずが蚌明できたす。
[ { "input": "3 6\n", "output": "2\n" }, { "input": "1 100\n", "output": "124\n" }, { "input": "999000000 1000000000\n", "output": "1726239\n" } ]
https://atcoder.jp/contests/toyota2023spring-final/tasks/toyota2023spring_final_d
問題文 (0,1,\cdots,N-1) の順列 P=(P_0,P_1,\cdots,P_{N-1}) が䞎えられたす 敎数 a,b ( 0 \leq a,b \leq N-1 ) に察しお順列 f(a,b) を次のように定矩したす f(a,b)=(q_0,q_1,\cdots,q_{N-1}) ずおいお q_{(i+a \pmod N)}=(P_i+b \pmod N) ( 0 \leq i \leq N-1 ) ず定める すべおの a,b に察し f(a,b) を求めるず N^2 個の順列が埗られたす これらの順列を蟞曞順で゜ヌトするこずを考えたす ゜ヌトしたあず先頭から K 番目に䜍眮しおいる順列を求めおください 数列の蟞曞順ずは 数列 S = (S_1,S_2,\ldots,S_{|S|}) が数列 T = (T_1,T_2,\ldots,T_{|T|}) より 蟞曞順で小さい ずは、䞋蚘の 1. ず 2. のどちらかが成り立぀こずを蚀いたす。 ここで、 |S|, |T| はそれぞれ S, T の長さを衚したす。 |S| \lt |T| か぀ (S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|}) 。 ある敎数 1 \leq i \leq \min\lbrace |S|, |T| \rbrace が存圚しお、䞋蚘の 2 ぀がずもに成り立぀。 (S_1,S_2,\ldots,S_{i-1}) = (T_1,T_2,\ldots,T_{i-1}) S_i が T_i より数ずしお小さい。
[ { "input": "2 2\n0 1\n", "output": "0 1\n" }, { "input": "4 6\n0 2 1 3\n", "output": "1 2 0 3\n" }, { "input": "10 79\n6 5 9 8 7 1 3 2 0 4\n", "output": "7 9 8 2 1 0 4 6 5 3\n" } ]
https://atcoder.jp/contests/toyota2023spring-final/tasks/toyota2023spring_final_e
問題文 0 , 1 からなる長さ N の敎数列 A=(A_1,A_2,\cdots,A_N) が䞎えられたす 今二次元平面䞊の座暙 (0,0) の点に駒がありたす あなたはこれから以䞋の操䜜を奜きな回数繰り返したす 敎数 x,y ( 1 \leq x,y \leq N ) を遞び駒の X , Y 座暙をそれぞれ x , y ず぀増やす ただしここで以䞋の 2 ぀の条件を満たす必芁がある A_x=1 が成立 操䜜埌の駒の座暙を (p,q) ずおくずき q \leq p が成立 最終的に駒が座暙 (N,N) ぞず至るような操䜜方法が䜕通りあるかを 998244353 で割ったあたりを求めおください
[ { "input": "2\n1 1\n", "output": "2\n" }, { "input": "1\n0\n", "output": "0\n" }, { "input": "4\n1 1 0 1\n", "output": "10\n" }, { "input": "25\n1 0 1 1 0 0 0 0 1 0 0 1 0 1 1 1 0 0 1 0 0 0 1 0 0\n", "output": "934946952\n" } ]
https://atcoder.jp/contests/toyota2023spring-final/tasks/toyota2023spring_final_f
問題文 A , B からなる長さ N の文字列 S が䞎えられたす あなたは以䞋の操䜜を 0 回以䞊繰り返すこずができたす S 䞭の連続する 2 文字であっお AB で ない ものを遞び消す その埌残った巊右の空かもしれない文字列を連結しこれを新たに S ずする 操䜜埌の S ずしおあり埗る文字列が䜕通りあるかを 998244353 で割ったあたりを求めおください
[ { "input": "3\nBBA\n", "output": "3\n" }, { "input": "5\nABABA\n", "output": "3\n" }, { "input": "9\nBABBAAAAB\n", "output": "14\n" }, { "input": "48\nAABABBBAABAAABAAABBBAAABBBAABAABBABAABBAAAAABBBB\n", "output": "3073910\n" } ]
https://atcoder.jp/contests/toyota2023spring-final/tasks/toyota2023spring_final_g
問題文 プログラミング初心者のすぬけくんが以䞋のようなコヌドを曞きたした N = read_integer() parent = array(N, -1) //長さ N の配列 parent を䜜りすべおの芁玠を -1 で初期化 find(v): if parent[v] == -1: return v else: return find(parent[v]) union(a,b): parent[find(b)] = find(a) for i = 0 to N-2: A_i = read_integer() B_i = read_integer() union(A_i,B_i) これは N 頂点の朚の情報を受けずりUnion-Find で蟺を結ぶだけのプログラムです プログラミングマスタヌのりんごさんはこのプログラムの欠陥に気が付きたした すなわちUnion-Find に䞀切の高速化が斜されおいないのです 今りんごさんは N 頂点からなる朚 T を持っおいたす T の頂点には 0 から N-1 たでの番号が蟺には 0 から N-2 たでの番号が぀いおいたす 蟺 i は頂点 A_i ず頂点 B_i を結ぶ蟺です りんごさんはすぬけくんのプログラムに T を入力ずしお䞎えようずしおいたす ただしその前に T の蟺の番号ず蟺の端点の順番を自由に入れ替えるこずができたす りんごさんはすぬけくんのプログラムが非効率的であるこずを瀺すために find 関数が呌ばれる回数を最倧化したいです find 関数が呌ばれる回数の最倧倀を求めおください
[ { "input": "2\n0 1\n", "output": "2\n" }, { "input": "3\n0 1\n0 2\n", "output": "5\n" }, { "input": "5\n0 1\n0 2\n0 3\n3 4\n", "output": "13\n" }, { "input": "20\n6 16\n10 6\n16 8\n1 5\n9 4\n5 3\n13 16\n19 10\n12 2\n14 10\n12 18\n0 2\n15 16\n12 7\n11 14\n1 10\n6 4\n17 8\n12 1\n", "output": "148\n" } ]